diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ada01e93..36d5c1ed 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -155,8 +155,8 @@ jobs: uses: dsnopek/action-download-artifact@1322f74e2dac9feed2ee76a32d9ae1ca3b4cf4e9 if: ${{ matrix.run-tests }} with: - repo: godotengine/godot - branch: master + repo: AThousandShips/godot + branch: the_angry_count event: push workflow: linux_builds.yml workflow_conclusion: success diff --git a/gdextension/gdextension_interface.h b/gdextension/gdextension_interface.h index 765952cf..659d266a 100644 --- a/gdextension/gdextension_interface.h +++ b/gdextension/gdextension_interface.h @@ -408,6 +408,8 @@ typedef GDExtensionBool (*GDExtensionCallableCustomLessThan)(void *callable_user typedef void (*GDExtensionCallableCustomToString)(void *callable_userdata, GDExtensionBool *r_is_valid, GDExtensionStringPtr r_out); +typedef GDExtensionInt (*GDExtensionCallableCustomGetArgumentCount)(void *callable_userdata, GDExtensionBool *r_is_valid); + typedef struct { /* Only `call_func` and `token` are strictly required, however, `object_id` should be passed if its not a static method. * @@ -437,7 +439,40 @@ typedef struct { GDExtensionCallableCustomLessThan less_than_func; GDExtensionCallableCustomToString to_string_func; -} GDExtensionCallableCustomInfo; +} GDExtensionCallableCustomInfo; // Deprecated. Use GDExtensionCallableCustomInfo2 instead. + +typedef struct { + /* Only `call_func` and `token` are strictly required, however, `object_id` should be passed if its not a static method. + * + * `token` should point to an address that uniquely identifies the GDExtension (for example, the + * `GDExtensionClassLibraryPtr` passed to the entry symbol function. + * + * `hash_func`, `equal_func`, and `less_than_func` are optional. If not provided both `call_func` and + * `callable_userdata` together are used as the identity of the callable for hashing and comparison purposes. + * + * The hash returned by `hash_func` is cached, `hash_func` will not be called more than once per callable. + * + * `is_valid_func` is necessary if the validity of the callable can change before destruction. + * + * `free_func` is necessary if `callable_userdata` needs to be cleaned up when the callable is freed. + */ + void *callable_userdata; + void *token; + + GDObjectInstanceID object_id; + + GDExtensionCallableCustomCall call_func; + GDExtensionCallableCustomIsValid is_valid_func; + GDExtensionCallableCustomFree free_func; + + GDExtensionCallableCustomHash hash_func; + GDExtensionCallableCustomEqual equal_func; + GDExtensionCallableCustomLessThan less_than_func; + + GDExtensionCallableCustomToString to_string_func; + + GDExtensionCallableCustomGetArgumentCount get_argument_count_func; +} GDExtensionCallableCustomInfo2; /* SCRIPT INSTANCE EXTENSION */ @@ -464,6 +499,8 @@ typedef void (*GDExtensionScriptInstanceFreeMethodList)(GDExtensionScriptInstanc typedef GDExtensionBool (*GDExtensionScriptInstanceHasMethod)(GDExtensionScriptInstanceDataPtr p_instance, GDExtensionConstStringNamePtr p_name); +typedef GDExtensionInt (*GDExtensionScriptInstanceGetMethodArgumentCount)(GDExtensionScriptInstanceDataPtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionBool *r_is_valid); + typedef void (*GDExtensionScriptInstanceCall)(GDExtensionScriptInstanceDataPtr p_self, GDExtensionConstStringNamePtr p_method, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_argument_count, GDExtensionVariantPtr r_return, GDExtensionCallError *r_error); typedef void (*GDExtensionScriptInstanceNotification)(GDExtensionScriptInstanceDataPtr p_instance, int32_t p_what); // Deprecated. Use GDExtensionScriptInstanceNotification2 instead. typedef void (*GDExtensionScriptInstanceNotification2)(GDExtensionScriptInstanceDataPtr p_instance, int32_t p_what, GDExtensionBool p_reversed); @@ -520,7 +557,7 @@ typedef struct { GDExtensionScriptInstanceFree free_func; -} GDExtensionScriptInstanceInfo; // Deprecated. Use GDExtensionScriptInstanceInfo2 instead. +} GDExtensionScriptInstanceInfo; // Deprecated. Use GDExtensionScriptInstanceInfo3 instead. typedef struct { GDExtensionScriptInstanceSet set_func; @@ -561,7 +598,50 @@ typedef struct { GDExtensionScriptInstanceFree free_func; -} GDExtensionScriptInstanceInfo2; +} GDExtensionScriptInstanceInfo2; // Deprecated. Use GDExtensionScriptInstanceInfo3 instead. + +typedef struct { + GDExtensionScriptInstanceSet set_func; + GDExtensionScriptInstanceGet get_func; + GDExtensionScriptInstanceGetPropertyList get_property_list_func; + GDExtensionScriptInstanceFreePropertyList free_property_list_func; + GDExtensionScriptInstanceGetClassCategory get_class_category_func; // Optional. Set to NULL for the default behavior. + + GDExtensionScriptInstancePropertyCanRevert property_can_revert_func; + GDExtensionScriptInstancePropertyGetRevert property_get_revert_func; + + GDExtensionScriptInstanceGetOwner get_owner_func; + GDExtensionScriptInstanceGetPropertyState get_property_state_func; + + GDExtensionScriptInstanceGetMethodList get_method_list_func; + GDExtensionScriptInstanceFreeMethodList free_method_list_func; + GDExtensionScriptInstanceGetPropertyType get_property_type_func; + GDExtensionScriptInstanceValidateProperty validate_property_func; + + GDExtensionScriptInstanceHasMethod has_method_func; + + GDExtensionScriptInstanceGetMethodArgumentCount get_method_argument_count_func; + + GDExtensionScriptInstanceCall call_func; + GDExtensionScriptInstanceNotification2 notification_func; + + GDExtensionScriptInstanceToString to_string_func; + + GDExtensionScriptInstanceRefCountIncremented refcount_incremented_func; + GDExtensionScriptInstanceRefCountDecremented refcount_decremented_func; + + GDExtensionScriptInstanceGetScript get_script_func; + + GDExtensionScriptInstanceIsPlaceholder is_placeholder_func; + + GDExtensionScriptInstanceSet set_fallback_func; + GDExtensionScriptInstanceGet get_fallback_func; + + GDExtensionScriptInstanceGetLanguage get_language_func; + + GDExtensionScriptInstanceFree free_func; + +} GDExtensionScriptInstanceInfo3; /* INITIALIZATION */ @@ -2343,7 +2423,7 @@ typedef void (*GDExtensionInterfaceRefSetObject)(GDExtensionRefPtr p_ref, GDExte /** * @name script_instance_create * @since 4.1 - * @deprecated in Godot 4.2. Use `script_instance_create2` instead. + * @deprecated in Godot 4.2. Use `script_instance_create3` instead. * * Creates a script instance that contains the given info and instance data. * @@ -2357,6 +2437,7 @@ typedef GDExtensionScriptInstancePtr (*GDExtensionInterfaceScriptInstanceCreate) /** * @name script_instance_create2 * @since 4.2 + * @deprecated in Godot 4.3. Use `script_instance_create3` instead. * * Creates a script instance that contains the given info and instance data. * @@ -2367,6 +2448,19 @@ typedef GDExtensionScriptInstancePtr (*GDExtensionInterfaceScriptInstanceCreate) */ typedef GDExtensionScriptInstancePtr (*GDExtensionInterfaceScriptInstanceCreate2)(const GDExtensionScriptInstanceInfo2 *p_info, GDExtensionScriptInstanceDataPtr p_instance_data); +/** + * @name script_instance_create3 + * @since 4.3 + * + * Creates a script instance that contains the given info and instance data. + * + * @param p_info A pointer to a GDExtensionScriptInstanceInfo3 struct. + * @param p_instance_data A pointer to a data representing the script instance in the GDExtension. This will be passed to all the function pointers on p_info. + * + * @return A pointer to a ScriptInstanceExtension object. + */ +typedef GDExtensionScriptInstancePtr (*GDExtensionInterfaceScriptInstanceCreate3)(const GDExtensionScriptInstanceInfo3 *p_info, GDExtensionScriptInstanceDataPtr p_instance_data); + /** * @name placeholder_script_instance_create * @since 4.2 @@ -2416,6 +2510,7 @@ typedef GDExtensionScriptInstanceDataPtr (*GDExtensionInterfaceObjectGetScriptIn /** * @name callable_custom_create * @since 4.2 + * @deprecated in Godot 4.3. Use `callable_custom_create2` instead. * * Creates a custom Callable object from a function pointer. * @@ -2426,6 +2521,19 @@ typedef GDExtensionScriptInstanceDataPtr (*GDExtensionInterfaceObjectGetScriptIn */ typedef void (*GDExtensionInterfaceCallableCustomCreate)(GDExtensionUninitializedTypePtr r_callable, GDExtensionCallableCustomInfo *p_callable_custom_info); +/** + * @name callable_custom_create2 + * @since 4.3 + * + * Creates a custom Callable object from a function pointer. + * + * Provided struct can be safely freed once the function returns. + * + * @param r_callable A pointer that will receive the new Callable. + * @param p_callable_custom_info The info required to construct a Callable. + */ +typedef void (*GDExtensionInterfaceCallableCustomCreate2)(GDExtensionUninitializedTypePtr r_callable, GDExtensionCallableCustomInfo2 *p_callable_custom_info); + /** * @name callable_custom_get_userdata * @since 4.2 diff --git a/include/godot_cpp/godot.hpp b/include/godot_cpp/godot.hpp index 1b420d38..8fb4eb8c 100644 --- a/include/godot_cpp/godot.hpp +++ b/include/godot_cpp/godot.hpp @@ -167,11 +167,11 @@ extern "C" GDExtensionInterfaceObjectGetInstanceFromId gdextension_interface_obj extern "C" GDExtensionInterfaceObjectGetInstanceId gdextension_interface_object_get_instance_id; extern "C" GDExtensionInterfaceObjectHasScriptMethod gdextension_interface_object_has_script_method; extern "C" GDExtensionInterfaceObjectCallScriptMethod gdextension_interface_object_call_script_method; -extern "C" GDExtensionInterfaceCallableCustomCreate gdextension_interface_callable_custom_create; +extern "C" GDExtensionInterfaceCallableCustomCreate2 gdextension_interface_callable_custom_create2; extern "C" GDExtensionInterfaceCallableCustomGetUserData gdextension_interface_callable_custom_get_userdata; extern "C" GDExtensionInterfaceRefGetObject gdextension_interface_ref_get_object; extern "C" GDExtensionInterfaceRefSetObject gdextension_interface_ref_set_object; -extern "C" GDExtensionInterfaceScriptInstanceCreate2 gdextension_interface_script_instance_create2; +extern "C" GDExtensionInterfaceScriptInstanceCreate3 gdextension_interface_script_instance_create3; extern "C" GDExtensionInterfacePlaceHolderScriptInstanceCreate gdextension_interface_placeholder_script_instance_create; extern "C" GDExtensionInterfacePlaceHolderScriptInstanceUpdate gdextension_interface_placeholder_script_instance_update; extern "C" GDExtensionInterfaceClassdbConstructObject gdextension_interface_classdb_construct_object; diff --git a/include/godot_cpp/variant/callable_custom.hpp b/include/godot_cpp/variant/callable_custom.hpp index 34328f9c..48a81422 100644 --- a/include/godot_cpp/variant/callable_custom.hpp +++ b/include/godot_cpp/variant/callable_custom.hpp @@ -41,6 +41,7 @@ class Object; class CallableCustomBase { public: virtual ObjectID get_object() const = 0; + virtual int get_argument_count(bool &r_is_valid) const; virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const = 0; virtual ~CallableCustomBase() {} }; diff --git a/include/godot_cpp/variant/callable_method_pointer.hpp b/include/godot_cpp/variant/callable_method_pointer.hpp index 159f976d..0c0ff340 100644 --- a/include/godot_cpp/variant/callable_method_pointer.hpp +++ b/include/godot_cpp/variant/callable_method_pointer.hpp @@ -73,6 +73,11 @@ public: return ObjectID(data.instance->get_instance_id()); } + virtual int get_argument_count(bool &r_is_valid) const override { + r_is_valid = true; + return sizeof...(P); + } + virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override { call_with_variant_args(data.instance, data.method, p_arguments, p_argcount, r_call_error); } @@ -110,6 +115,11 @@ public: return ObjectID(data.instance->get_instance_id()); } + virtual int get_argument_count(bool &r_is_valid) const override { + r_is_valid = true; + return sizeof...(P); + } + virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override { call_with_variant_args_ret(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error); } @@ -147,6 +157,11 @@ public: return ObjectID(data.instance->get_instance_id()); } + virtual int get_argument_count(bool &r_is_valid) const override { + r_is_valid = true; + return sizeof...(P); + } + virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override { call_with_variant_args_retc(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error); } @@ -182,6 +197,11 @@ public: return ObjectID(); } + virtual int get_argument_count(bool &r_is_valid) const override { + r_is_valid = true; + return sizeof...(P); + } + virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override { call_with_variant_args_static_ret(data.method, p_arguments, p_argcount, r_return_value, r_call_error); r_return_value = Variant(); @@ -218,6 +238,11 @@ public: return ObjectID(); } + virtual int get_argument_count(bool &r_is_valid) const override { + r_is_valid = true; + return sizeof...(P); + } + virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override { call_with_variant_args_static_ret(data.method, p_arguments, p_argcount, r_return_value, r_call_error); } diff --git a/src/godot.cpp b/src/godot.cpp index a80ad90d..d1413a62 100644 --- a/src/godot.cpp +++ b/src/godot.cpp @@ -173,11 +173,11 @@ GDExtensionInterfaceObjectGetInstanceFromId gdextension_interface_object_get_ins GDExtensionInterfaceObjectGetInstanceId gdextension_interface_object_get_instance_id = nullptr; GDExtensionInterfaceObjectHasScriptMethod gdextension_interface_object_has_script_method = nullptr; GDExtensionInterfaceObjectCallScriptMethod gdextension_interface_object_call_script_method = nullptr; -GDExtensionInterfaceCallableCustomCreate gdextension_interface_callable_custom_create = nullptr; +GDExtensionInterfaceCallableCustomCreate2 gdextension_interface_callable_custom_create2 = nullptr; GDExtensionInterfaceCallableCustomGetUserData gdextension_interface_callable_custom_get_userdata = nullptr; GDExtensionInterfaceRefGetObject gdextension_interface_ref_get_object = nullptr; GDExtensionInterfaceRefSetObject gdextension_interface_ref_set_object = nullptr; -GDExtensionInterfaceScriptInstanceCreate2 gdextension_interface_script_instance_create2 = nullptr; +GDExtensionInterfaceScriptInstanceCreate3 gdextension_interface_script_instance_create3 = nullptr; GDExtensionInterfacePlaceHolderScriptInstanceCreate gdextension_interface_placeholder_script_instance_create = nullptr; GDExtensionInterfacePlaceHolderScriptInstanceUpdate gdextension_interface_placeholder_script_instance_update = nullptr; GDExtensionInterfaceClassdbConstructObject gdextension_interface_classdb_construct_object = nullptr; @@ -413,11 +413,11 @@ GDExtensionBool GDExtensionBinding::init(GDExtensionInterfaceGetProcAddress p_ge LOAD_PROC_ADDRESS(object_get_instance_id, GDExtensionInterfaceObjectGetInstanceId); LOAD_PROC_ADDRESS(object_has_script_method, GDExtensionInterfaceObjectHasScriptMethod); LOAD_PROC_ADDRESS(object_call_script_method, GDExtensionInterfaceObjectCallScriptMethod); - LOAD_PROC_ADDRESS(callable_custom_create, GDExtensionInterfaceCallableCustomCreate); + LOAD_PROC_ADDRESS(callable_custom_create2, GDExtensionInterfaceCallableCustomCreate2); LOAD_PROC_ADDRESS(callable_custom_get_userdata, GDExtensionInterfaceCallableCustomGetUserData); LOAD_PROC_ADDRESS(ref_get_object, GDExtensionInterfaceRefGetObject); LOAD_PROC_ADDRESS(ref_set_object, GDExtensionInterfaceRefSetObject); - LOAD_PROC_ADDRESS(script_instance_create2, GDExtensionInterfaceScriptInstanceCreate2); + LOAD_PROC_ADDRESS(script_instance_create3, GDExtensionInterfaceScriptInstanceCreate3); LOAD_PROC_ADDRESS(placeholder_script_instance_create, GDExtensionInterfacePlaceHolderScriptInstanceCreate); LOAD_PROC_ADDRESS(placeholder_script_instance_update, GDExtensionInterfacePlaceHolderScriptInstanceUpdate); LOAD_PROC_ADDRESS(classdb_construct_object, GDExtensionInterfaceClassdbConstructObject); diff --git a/src/variant/callable_custom.cpp b/src/variant/callable_custom.cpp index e0540fa1..ae8cc485 100644 --- a/src/variant/callable_custom.cpp +++ b/src/variant/callable_custom.cpp @@ -35,6 +35,11 @@ namespace godot { +int CallableCustomBase::get_argument_count(bool &r_is_valid) const { + r_is_valid = false; + return 0; +} + static void callable_custom_call(void *p_userdata, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_argument_count, GDExtensionVariantPtr r_return, GDExtensionCallError *r_error) { CallableCustom *callable_custom = (CallableCustom *)p_userdata; callable_custom->call((const Variant **)p_args, p_argument_count, *(Variant *)r_return, *r_error); @@ -84,13 +89,21 @@ static GDExtensionBool callable_custom_less_than_func(void *p_a, void *p_b) { return func_a(a, b); } +static GDExtensionInt custom_callable_get_argument_count_func(void *p_userdata, GDExtensionBool *r_is_valid) { + CallableCustom *callable_custom = (CallableCustom *)p_userdata; + bool valid = false; + int ret = callable_custom->get_argument_count(valid); + *r_is_valid = valid; + return ret; +} + bool CallableCustom::is_valid() const { // The same default implementation as in Godot. return ObjectDB::get_instance(get_object()); } Callable::Callable(CallableCustom *p_callable_custom) { - GDExtensionCallableCustomInfo info = {}; + GDExtensionCallableCustomInfo2 info = {}; info.callable_userdata = p_callable_custom; info.token = internal::token; info.object_id = p_callable_custom->get_object(); @@ -101,8 +114,9 @@ Callable::Callable(CallableCustom *p_callable_custom) { info.equal_func = &callable_custom_equal_func; info.less_than_func = &callable_custom_less_than_func; info.to_string_func = &callable_custom_to_string; + info.get_argument_count_func = &custom_callable_get_argument_count_func; - ::godot::internal::gdextension_interface_callable_custom_create(_native_ptr(), &info); + ::godot::internal::gdextension_interface_callable_custom_create2(_native_ptr(), &info); } CallableCustom *Callable::get_custom() const { diff --git a/src/variant/callable_method_pointer.cpp b/src/variant/callable_method_pointer.cpp index 520ed05a..23b7ef50 100644 --- a/src/variant/callable_method_pointer.cpp +++ b/src/variant/callable_method_pointer.cpp @@ -77,6 +77,14 @@ static GDExtensionBool custom_callable_mp_less_than_func(void *p_a, void *p_b) { return memcmp(a->get_comp_ptr(), b->get_comp_ptr(), a->get_comp_size() * 4) < 0; } +static GDExtensionInt custom_callable_mp_get_argument_count_func(void *p_userdata, GDExtensionBool *r_is_valid) { + CallableCustomMethodPointerBase *callable_method_pointer = (CallableCustomMethodPointerBase *)p_userdata; + bool valid = false; + int ret = callable_method_pointer->get_argument_count(valid); + *r_is_valid = valid; + return ret; +} + void CallableCustomMethodPointerBase::_setup(uint32_t *p_base_ptr, uint32_t p_ptr_size) { comp_ptr = p_base_ptr; comp_size = p_ptr_size / 4; @@ -93,7 +101,7 @@ void CallableCustomMethodPointerBase::_setup(uint32_t *p_base_ptr, uint32_t p_pt namespace internal { Callable create_callable_from_ccmp(CallableCustomMethodPointerBase *p_callable_method_pointer) { - GDExtensionCallableCustomInfo info = {}; + GDExtensionCallableCustomInfo2 info = {}; info.callable_userdata = p_callable_method_pointer; info.token = internal::token; info.object_id = p_callable_method_pointer->get_object(); @@ -103,9 +111,10 @@ Callable create_callable_from_ccmp(CallableCustomMethodPointerBase *p_callable_m info.hash_func = &custom_callable_mp_hash; info.equal_func = &custom_callable_mp_equal_func; info.less_than_func = &custom_callable_mp_less_than_func; + info.get_argument_count_func = &custom_callable_mp_get_argument_count_func; Callable callable; - ::godot::internal::gdextension_interface_callable_custom_create(callable._native_ptr(), &info); + ::godot::internal::gdextension_interface_callable_custom_create2(callable._native_ptr(), &info); return callable; } diff --git a/test/project/main.gd b/test/project/main.gd index d2cbd261..7389335d 100644 --- a/test/project/main.gd +++ b/test/project/main.gd @@ -102,6 +102,7 @@ func _ready(): # mp_callable() with void method. var mp_callable: Callable = example.test_callable_mp() assert_equal(mp_callable.is_valid(), true) + assert_equal(mp_callable.get_argument_count(), 3) mp_callable.call(example, "void", 36) assert_equal(custom_signal_emitted, ["unbound_method1: Example - void", 36]) @@ -117,14 +118,17 @@ func _ready(): # mp_callable() with return value. var mp_callable_ret: Callable = example.test_callable_mp_ret() + assert_equal(mp_callable_ret.get_argument_count(), 3) assert_equal(mp_callable_ret.call(example, "test", 77), "unbound_method2: Example - test - 77") # mp_callable() with const method and return value. var mp_callable_retc: Callable = example.test_callable_mp_retc() + assert_equal(mp_callable_retc.get_argument_count(), 3) assert_equal(mp_callable_retc.call(example, "const", 101), "unbound_method3: Example - const - 101") # mp_callable_static() with void method. var mp_callable_static: Callable = example.test_callable_mp_static() + assert_equal(mp_callable_static.get_argument_count(), 3) mp_callable_static.call(example, "static", 83) assert_equal(custom_signal_emitted, ["unbound_static_method1: Example - static", 83]) @@ -140,6 +144,7 @@ func _ready(): # mp_callable_static() with return value. var mp_callable_static_ret: Callable = example.test_callable_mp_static_ret() + assert_equal(mp_callable_static_ret.get_argument_count(), 3) assert_equal(mp_callable_static_ret.call(example, "static-ret", 84), "unbound_static_method2: Example - static-ret - 84") # CallableCustom. @@ -150,6 +155,7 @@ func _ready(): assert_equal(custom_callable.hash(), 27); assert_equal(custom_callable.get_object(), null); assert_equal(custom_callable.get_method(), ""); + assert_equal(custom_callable.get_argument_count(), 2) assert_equal(str(custom_callable), ""); # PackedArray iterators diff --git a/test/src/example.cpp b/test/src/example.cpp index 53d11f4a..4adf4403 100644 --- a/test/src/example.cpp +++ b/test/src/example.cpp @@ -49,6 +49,11 @@ public: return ObjectID(); } + virtual int get_argument_count(bool &r_is_valid) const { + r_is_valid = true; + return 2; + } + virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const { r_return_value = "Hi"; r_call_error.error = GDEXTENSION_CALL_OK;