Skip to content

Commit

Permalink
Expose GDExtension new interface functions from Godot 4.2, remove pyt…
Browse files Browse the repository at this point in the history
…honscript_gdstringname_new given Godot now provides an ad-hoc helper
  • Loading branch information
touilleMan committed Jun 17, 2024
1 parent 629fda5 commit e3f95cd
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 108 deletions.
18 changes: 9 additions & 9 deletions src/_pythonscript_editor.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ cdef public void _pythonscript_get_reserved_words(
"yield",
]
for keyword in keywords:
string = gd_string_from_utf8(keyword, keyword.len())
string = gd_string_from_utf8_and_len(keyword, keyword.len())
gd_packed_string_array_append(&arr, &string)
gd_string_del(&string)

Expand All @@ -66,7 +66,7 @@ cdef public void _pythonscript_get_recognized_extensions(
"py",
]
for keyword in keywords:
string = gd_string_from_utf8(keyword, keyword.len())
string = gd_string_from_utf8_and_len(keyword, keyword.len())
gd_packed_string_array_append(&arr, &string)
gd_string_del(&string)

Expand All @@ -86,7 +86,7 @@ cdef public void _pythonscript_get_string_delimiters(
"\"\"\" \"\"\"",
]
for keyword in keywords:
string = gd_string_from_utf8(keyword, keyword.len())
string = gd_string_from_utf8_and_len(keyword, keyword.len())
gd_packed_string_array_append(&arr, &string)
gd_string_del(&string)

Expand All @@ -96,7 +96,7 @@ cdef _register_editor_methods():
pass

# cdef GDExtensionClassMethodInfo info
# pythonscript_gdstringname_new(&info.name, "_get_reserved_words")
# info.name = gd_string_name_from_utf8("_get_reserved_words")
# info.method_userdata = NULL
# info.call_func = NULL
# # info.ptrcall_func = &_pythonscript_get_reserved_words
Expand All @@ -113,9 +113,9 @@ cdef _register_editor_methods():
# "PythonScriptLanguageExtension",
# &info,
# )
# pythonscript_gdstringname_delete(&info.name)
# gd_string_name_del(&info.name)

# pythonscript_gdstringname_new(&info.name, "_get_recognized_extensions")
# info.name = gd_string_name_from_utf8("_get_recognized_extensions")
# info.method_userdata = NULL
# info.call_func = NULL
# # info.ptrcall_func = &_pythonscript_get_recognized_extensions
Expand All @@ -132,9 +132,9 @@ cdef _register_editor_methods():
# "PythonScriptLanguageExtension",
# &info,
# )
# pythonscript_gdstringname_delete(&info.name)
# gd_string_name_del(&info.name)

# pythonscript_gdstringname_new(&info.name, "_get_string_delimiters")
# info.name = gd_string_name_from_utf8("_get_string_delimiters")
# info.method_userdata = NULL
# info.call_func = NULL
# # info.ptrcall_func = &_pythonscript_get_string_delimiters
Expand All @@ -151,4 +151,4 @@ cdef _register_editor_methods():
# "PythonScriptLanguageExtension",
# &info,
# )
# pythonscript_gdstringname_delete(&info.name)
# gd_string_name_del(&info.name)
5 changes: 2 additions & 3 deletions src/godot/builtins_pyx/class.pyx.j2
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
def {{ c.name }}(self):
cdef gd_variant_t gdvariant
# TODO: cache the constant on first fetch ?
cdef gd_string_name_t gdname
pythonscript_gdstringname_new(&gdname, "{{ c.original_name }}")
cdef gd_string_name_t gdname = gd_string_name_from_utf8("{{ c.original_name }}")
pythonscript_gdextension.variant_get_constant_value({{ builtin.variant_type_name }}, &gdname, &gdvariant)
pythonscript_gdstringname_delete(&gdname)
gd_string_name_del(&gdname)
return gd_variant_steal_into_pyobj(&gdvariant)
# No need to destroy gdvariant given the conversion has stolen ownership on data !
{% endif %}
Expand Down
5 changes: 2 additions & 3 deletions src/godot/classes.pyx.j2
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,9 @@ cdef class {{ cls.cy_type }}
cdef object _load_singleton(str name):
cdef object cls = _load_class(name)
{# cdef GDExtensionObjectPtr gdobj = pythonscript_gdextension.global_get_singleton(&(<StringName>cls._gd_name)._gd_data) #}
cdef gd_string_name_t gdname
pythonscript_gdstringname_new(&gdname, name.encode())
cdef gd_string_name_t gdname = gd_string_name_from_utf8(name.encode())
cdef GDExtensionObjectPtr gdobj = pythonscript_gdextension.global_get_singleton(&gdname)
pythonscript_gdstringname_delete(&gdname)
gd_string_name_del(&gdname)
return cls._from_ptr(<size_t>gdobj)
{# {% for singleton in api.singletons %}
cdef {{ singleton.type.py_type }} singleton_{{ singleton.name }} = {{ singleton.type.py_type }}.__new__({{ singleton.type.py_type }})
Expand Down
9 changes: 3 additions & 6 deletions src/godot/hazmat/extension_class.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ cdef inline list _get_extension_gc_protector() noexcept:


cdef inline void unregister_extension_class(bytes class_name) noexcept:
cdef gd_string_name_t gd_class_name
pythonscript_gdstringname_new(&gd_class_name, <char*>class_name)
cdef gd_string_name_t gd_class_name = gd_string_name_from_utf8(<char*>class_name)
pythonscript_gdextension.classdb_unregister_extension_class(
pythonscript_gdextension_library,
&gd_class_name,
Expand Down Expand Up @@ -80,10 +79,8 @@ cdef inline void register_extension_class_creation(
# Don't increment refcount given we rely on gc protector
info.class_userdata = <void*>spec # void*

cdef gd_string_name_t gdname
cdef gd_string_name_t gdname_parent
pythonscript_gdstringname_new(&gdname, <char*>class_name)
pythonscript_gdstringname_new(&gdname_parent, <char*>parent_class_name)
cdef gd_string_name_t gdname = gd_string_name_from_utf8(<char*>class_name)
cdef gd_string_name_t gdname_parent = gd_string_name_from_utf8(<char*>parent_class_name)
# TODO: correct me once https://github.com/godotengine/godot/pull/67121 is merged
pythonscript_gdextension.classdb_register_extension_class(
pythonscript_gdextension_library,
Expand Down
22 changes: 20 additions & 2 deletions src/godot/hazmat/gdapi.pxd.j2
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ cdef extern from "Python.h":
object PyUnicode_DecodeUTF32(const char *s, ssize_t size, const char *errors, int *byteorder)


cdef inline gd_string_t gd_string_from_utf8(const char *cstr, ssize_t cstr_size):
cdef inline gd_string_t gd_string_from_utf8(const char *cstr):
cdef gd_string_t gdstr
pythonscript_gdextension.string_new_with_utf8_chars(&gdstr, cstr)
return gdstr


cdef inline gd_string_t gd_string_from_utf8_and_len(const char *cstr, ssize_t cstr_size):
cdef gd_string_t gdstr
pythonscript_gdextension.string_new_with_utf8_chars_and_len(&gdstr, cstr, cstr_size)
return gdstr
Expand All @@ -55,7 +61,7 @@ cdef inline gd_string_t gd_string_from_utf8(const char *cstr, ssize_t cstr_size)
cdef inline gd_string_t gd_string_from_unchecked_pystr(object pystr):
cdef ssize_t buffsize
cdef const char *buff = PyUnicode_AsUTF8AndSize(pystr, &buffsize)
return gd_string_from_utf8(buff, buffsize)
return gd_string_from_utf8_and_len(buff, buffsize)


cdef inline object gd_string_to_pystr(gd_string_t *gdstr):
Expand All @@ -77,6 +83,18 @@ cdef inline gd_string_name_t gd_string_name_from_pybytes(bytes cstr):
return gdstrname


cdef inline gd_string_name_t gd_string_name_from_utf8(const char *cstr):
cdef gd_string_name_t gdstr
pythonscript_gdextension.string_name_new_with_utf8_chars(&gdstr, cstr)
return gdstr


cdef inline gd_string_name_t gd_string_name_from_utf8_and_len(const char *cstr, ssize_t cstr_size):
cdef gd_string_name_t gdstr
pythonscript_gdextension.string_name_new_with_utf8_chars_and_len(&gdstr, cstr, cstr_size)
return gdstr


##############################################################################
# Builtins stucts #
##############################################################################
Expand Down
25 changes: 17 additions & 8 deletions src/godot/hazmat/gdnative_ptrs.pxd.j2
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ cdef struct LoadedGDExtensionInterface:
void (*string_new_with_wide_chars)(GDExtensionUninitializedStringPtr r_dest, const wchar_t *p_contents)
void (*string_new_with_latin1_chars_and_len)(GDExtensionUninitializedStringPtr r_dest, const char *p_contents, GDExtensionInt p_size)
void (*string_new_with_utf8_chars_and_len)(GDExtensionUninitializedStringPtr r_dest, const char *p_contents, GDExtensionInt p_size)
void (*string_new_with_utf16_chars_and_len)(GDExtensionUninitializedStringPtr r_dest, const char16_t *p_contents, GDExtensionInt p_size)
void (*string_new_with_utf32_chars_and_len)(GDExtensionUninitializedStringPtr r_dest, const char32_t *p_contents, GDExtensionInt p_size)
void (*string_new_with_wide_chars_and_len)(GDExtensionUninitializedStringPtr r_dest, const wchar_t *p_contents, GDExtensionInt p_size)
void (*string_new_with_utf16_chars_and_len)(GDExtensionUninitializedStringPtr r_dest, const char16_t *p_contents, GDExtensionInt p_char_count)
void (*string_new_with_utf32_chars_and_len)(GDExtensionUninitializedStringPtr r_dest, const char32_t *p_contents, GDExtensionInt p_char_count)
void (*string_new_with_wide_chars_and_len)(GDExtensionUninitializedStringPtr r_dest, const wchar_t *p_contents, GDExtensionInt p_char_count)
GDExtensionInt (*string_to_latin1_chars)(GDExtensionConstStringPtr p_self, char *r_text, GDExtensionInt p_max_write_length)
GDExtensionInt (*string_to_utf8_chars)(GDExtensionConstStringPtr p_self, char *r_text, GDExtensionInt p_max_write_length)
GDExtensionInt (*string_to_utf16_chars)(GDExtensionConstStringPtr p_self, char16_t *r_text, GDExtensionInt p_max_write_length)
Expand All @@ -87,6 +87,10 @@ cdef struct LoadedGDExtensionInterface:
void (*string_operator_plus_eq_cstr)(GDExtensionStringPtr p_self, const char *p_b)
void (*string_operator_plus_eq_wcstr)(GDExtensionStringPtr p_self, const wchar_t *p_b)
void (*string_operator_plus_eq_c32str)(GDExtensionStringPtr p_self, const char32_t *p_b)
GDExtensionInt (*string_resize)(GDExtensionStringPtr p_self, GDExtensionInt p_resize)
void (*string_name_new_with_latin1_chars)(GDExtensionUninitializedStringNamePtr r_dest, const char *p_contents, GDExtensionBool p_is_static)
void (*string_name_new_with_utf8_chars)(GDExtensionUninitializedStringNamePtr r_dest, const char *p_contents)
void (*string_name_new_with_utf8_chars_and_len)(GDExtensionUninitializedStringNamePtr r_dest, const char *p_contents, GDExtensionInt p_size)
GDExtensionInt (*xml_parser_open_buffer)(GDExtensionObjectPtr p_instance, const uint8_t *p_buffer, size_t p_size)
void (*file_access_store_buffer)(GDExtensionObjectPtr p_instance, const uint8_t *p_src, uint64_t p_length)
uint64_t (*file_access_get_buffer)(GDExtensionConstObjectPtr p_instance, uint8_t *p_dst, uint64_t p_length)
Expand Down Expand Up @@ -122,6 +126,7 @@ cdef struct LoadedGDExtensionInterface:
GDExtensionObjectPtr (*global_get_singleton)(GDExtensionConstStringNamePtr p_name)
void *(*object_get_instance_binding)(GDExtensionObjectPtr p_o, void *p_token, const GDExtensionInstanceBindingCallbacks *p_callbacks)
void (*object_set_instance_binding)(GDExtensionObjectPtr p_o, void *p_token, void *p_binding, const GDExtensionInstanceBindingCallbacks *p_callbacks)
void (*object_free_instance_binding)(GDExtensionObjectPtr p_o, void *p_token)
void (*object_set_instance)(GDExtensionObjectPtr p_o, GDExtensionConstStringNamePtr p_classname, GDExtensionClassInstancePtr p_instance)
GDExtensionBool (*object_get_class_name)(GDExtensionConstObjectPtr p_object, GDExtensionClassLibraryPtr p_library, GDExtensionUninitializedStringNamePtr r_class_name)
GDExtensionObjectPtr (*object_cast_to)(GDExtensionConstObjectPtr p_object, void *p_class_tag)
Expand All @@ -130,13 +135,21 @@ cdef struct LoadedGDExtensionInterface:
GDExtensionObjectPtr (*ref_get_object)(GDExtensionConstRefPtr p_ref)
void (*ref_set_object)(GDExtensionRefPtr p_ref, GDExtensionObjectPtr p_object)
GDExtensionScriptInstancePtr (*script_instance_create)(const GDExtensionScriptInstanceInfo *p_info, GDExtensionScriptInstanceDataPtr p_instance_data)
GDExtensionScriptInstancePtr (*script_instance_create2)(const GDExtensionScriptInstanceInfo2 *p_info, GDExtensionScriptInstanceDataPtr p_instance_data)
GDExtensionScriptInstancePtr (*place_holder_script_instance_create)(GDExtensionObjectPtr p_language, GDExtensionObjectPtr p_script, GDExtensionObjectPtr p_owner)
void (*place_holder_script_instance_update)(GDExtensionScriptInstancePtr p_placeholder, GDExtensionConstTypePtr p_properties, GDExtensionConstTypePtr p_values)
GDExtensionScriptInstanceDataPtr (*object_get_script_instance)(GDExtensionConstObjectPtr p_object, GDExtensionObjectPtr p_language)
void (*callable_custom_create)(GDExtensionUninitializedTypePtr r_callable, GDExtensionCallableCustomInfo *p_callable_custom_info)
void *(*callable_custom_get_user_data)(GDExtensionConstTypePtr p_callable, void *p_token)
GDExtensionObjectPtr (*classdb_construct_object)(GDExtensionConstStringNamePtr p_classname)
GDExtensionMethodBindPtr (*classdb_get_method_bind)(GDExtensionConstStringNamePtr p_classname, GDExtensionConstStringNamePtr p_methodname, GDExtensionInt p_hash)
void *(*classdb_get_class_tag)(GDExtensionConstStringNamePtr p_classname)
void (*classdb_register_extension_class)(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo *p_extension_funcs)
void (*classdb_register_extension_class2)(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo2 *p_extension_funcs)
void (*classdb_register_extension_class_method)(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionClassMethodInfo *p_method_info)
void (*classdb_register_extension_class_integer_constant)(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_enum_name, GDExtensionConstStringNamePtr p_constant_name, GDExtensionInt p_constant_value, GDExtensionBool p_is_bitfield)
void (*classdb_register_extension_class_property)(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionPropertyInfo *p_info, GDExtensionConstStringNamePtr p_setter, GDExtensionConstStringNamePtr p_getter)
void (*classdb_register_extension_class_property_indexed)(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionPropertyInfo *p_info, GDExtensionConstStringNamePtr p_setter, GDExtensionConstStringNamePtr p_getter, GDExtensionInt p_index)
void (*classdb_register_extension_class_property_group)(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringPtr p_group_name, GDExtensionConstStringPtr p_prefix)
void (*classdb_register_extension_class_property_subgroup)(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringPtr p_subgroup_name, GDExtensionConstStringPtr p_prefix)
void (*classdb_register_extension_class_signal)(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_signal_name, const GDExtensionPropertyInfo *p_argument_info, GDExtensionInt p_argument_count)
Expand All @@ -163,13 +176,9 @@ cdef extern from * nogil:
#endif
DLL_IMPORT extern GDExtensionInterfaceGetProcAddress pythonscript_gdextension_get_proc_address;
DLL_IMPORT extern GDExtensionClassLibraryPtr pythonscript_gdextension_library;
DLL_IMPORT extern void pythonscript_gdstringname_new(GDExtensionStringNamePtr ptr, const char *cstr);
DLL_IMPORT extern void pythonscript_gdstringname_delete(GDExtensionStringNamePtr ptr);
"""
cdef const GDExtensionInterfaceGetProcAddress pythonscript_gdextension_get_proc_address;
cdef const GDExtensionInterfaceGetProcAddress pythonscript_gdextension_get_proc_address
cdef const GDExtensionClassLibraryPtr pythonscript_gdextension_library
cdef void pythonscript_gdstringname_new(GDExtensionStringNamePtr ptr, const char *cstr)
cdef void pythonscript_gdstringname_delete(GDExtensionStringNamePtr ptr)


##############################################################################
Expand Down
Loading

0 comments on commit e3f95cd

Please sign in to comment.