diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/core_bind.cpp | 6 | ||||
-rw-r--r-- | core/core_bind.h | 2 | ||||
-rw-r--r-- | core/extension/gdextension_interface.cpp | 56 | ||||
-rw-r--r-- | core/extension/gdextension_interface.h | 51 | ||||
-rw-r--r-- | core/object/callable_method_pointer.h | 25 | ||||
-rw-r--r-- | core/object/class_db.cpp | 25 | ||||
-rw-r--r-- | core/object/class_db.h | 1 | ||||
-rw-r--r-- | core/object/object.cpp | 55 | ||||
-rw-r--r-- | core/object/object.h | 2 | ||||
-rw-r--r-- | core/object/script_instance.cpp | 22 | ||||
-rw-r--r-- | core/object/script_instance.h | 2 | ||||
-rw-r--r-- | core/object/script_language.cpp | 16 | ||||
-rw-r--r-- | core/object/script_language.h | 9 | ||||
-rw-r--r-- | core/object/script_language_extension.cpp | 3 | ||||
-rw-r--r-- | core/object/script_language_extension.h | 18 | ||||
-rw-r--r-- | core/variant/callable.cpp | 19 | ||||
-rw-r--r-- | core/variant/callable.h | 2 | ||||
-rw-r--r-- | core/variant/callable_bind.cpp | 16 | ||||
-rw-r--r-- | core/variant/callable_bind.h | 2 | ||||
-rw-r--r-- | core/variant/variant_call.cpp | 5 | ||||
-rw-r--r-- | core/variant/variant_callable.cpp | 9 | ||||
-rw-r--r-- | core/variant/variant_callable.h | 1 |
22 files changed, 346 insertions, 1 deletions
diff --git a/core/core_bind.cpp b/core/core_bind.cpp index a0df1b6240..6927db002b 100644 --- a/core/core_bind.cpp +++ b/core/core_bind.cpp @@ -1434,6 +1434,10 @@ bool ClassDB::class_has_method(const StringName &p_class, const StringName &p_me return ::ClassDB::has_method(p_class, p_method, p_no_inheritance); } +int ClassDB::class_get_method_argument_count(const StringName &p_class, const StringName &p_method, bool p_no_inheritance) const { + return ::ClassDB::get_method_argument_count(p_class, p_method, nullptr, p_no_inheritance); +} + TypedArray<Dictionary> ClassDB::class_get_method_list(const StringName &p_class, bool p_no_inheritance) const { List<MethodInfo> methods; ::ClassDB::get_method_list(p_class, &methods, p_no_inheritance); @@ -1562,6 +1566,8 @@ void ClassDB::_bind_methods() { ::ClassDB::bind_method(D_METHOD("class_has_method", "class", "method", "no_inheritance"), &ClassDB::class_has_method, DEFVAL(false)); + ::ClassDB::bind_method(D_METHOD("class_get_method_argument_count", "class", "method", "no_inheritance"), &ClassDB::class_get_method_argument_count, DEFVAL(false)); + ::ClassDB::bind_method(D_METHOD("class_get_method_list", "class", "no_inheritance"), &ClassDB::class_get_method_list, DEFVAL(false)); ::ClassDB::bind_method(D_METHOD("class_get_integer_constant_list", "class", "no_inheritance"), &ClassDB::class_get_integer_constant_list, DEFVAL(false)); diff --git a/core/core_bind.h b/core/core_bind.h index 64ab4dd7e2..305f616cef 100644 --- a/core/core_bind.h +++ b/core/core_bind.h @@ -447,6 +447,8 @@ public: bool class_has_method(const StringName &p_class, const StringName &p_method, bool p_no_inheritance = false) const; + int class_get_method_argument_count(const StringName &p_class, const StringName &p_method, bool p_no_inheritance = false) const; + TypedArray<Dictionary> class_get_method_list(const StringName &p_class, bool p_no_inheritance = false) const; PackedStringArray class_get_integer_constant_list(const StringName &p_class, bool p_no_inheritance = false) const; diff --git a/core/extension/gdextension_interface.cpp b/core/extension/gdextension_interface.cpp index 67ec09d764..ca58d589bd 100644 --- a/core/extension/gdextension_interface.cpp +++ b/core/extension/gdextension_interface.cpp @@ -59,6 +59,8 @@ class CallableCustomExtension : public CallableCustom { GDExtensionCallableCustomToString to_string_func; + GDExtensionCallableCustomGetArgumentCount get_argument_count_func; + uint32_t _hash; static bool default_compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) { @@ -143,6 +145,21 @@ public: return object; } + int get_argument_count(bool &r_is_valid) const override { + if (get_argument_count_func != nullptr) { + GDExtensionBool is_valid = false; + + GDExtensionInt ret = get_argument_count_func(userdata, &is_valid); + + if (is_valid) { + r_is_valid = true; + return ret; + } + } + r_is_valid = false; + return 0; + } + void *get_userdata(void *p_token) const { return (p_token == token) ? userdata : nullptr; } @@ -157,6 +174,7 @@ public: r_call_error.expected = error.expected; } +#ifndef DISABLE_DEPRECATED CallableCustomExtension(GDExtensionCallableCustomInfo *p_info) { userdata = p_info->callable_userdata; token = p_info->token; @@ -172,6 +190,35 @@ public: to_string_func = p_info->to_string_func; + get_argument_count_func = nullptr; + + // Pre-calculate the hash. + if (p_info->hash_func != nullptr) { + _hash = p_info->hash_func(userdata); + } else { + _hash = hash_murmur3_one_64((uint64_t)call_func); + _hash = hash_murmur3_one_64((uint64_t)userdata, _hash); + } + } +#endif + + CallableCustomExtension(GDExtensionCallableCustomInfo2 *p_info) { + userdata = p_info->callable_userdata; + token = p_info->token; + + object = p_info->object_id; + + call_func = p_info->call_func; + is_valid_func = p_info->is_valid_func; + free_func = p_info->free_func; + + equal_func = p_info->equal_func; + less_than_func = p_info->less_than_func; + + to_string_func = p_info->to_string_func; + + get_argument_count_func = p_info->get_argument_count_func; + // Pre-calculate the hash. if (p_info->hash_func != nullptr) { _hash = p_info->hash_func(userdata); @@ -1378,9 +1425,15 @@ static GDExtensionScriptInstancePtr gdextension_object_get_script_instance(GDExt return script_instance_extension->instance; } +#ifndef DISABLE_DEPRECATED static void gdextension_callable_custom_create(GDExtensionUninitializedTypePtr r_callable, GDExtensionCallableCustomInfo *p_custom_callable_info) { memnew_placement(r_callable, Callable(memnew(CallableCustomExtension(p_custom_callable_info)))); } +#endif + +static void gdextension_callable_custom_create2(GDExtensionUninitializedTypePtr r_callable, GDExtensionCallableCustomInfo2 *p_custom_callable_info) { + memnew_placement(r_callable, Callable(memnew(CallableCustomExtension(p_custom_callable_info)))); +} static void *gdextension_callable_custom_get_userdata(GDExtensionTypePtr p_callable, void *p_token) { const Callable &callable = *reinterpret_cast<const Callable *>(p_callable); @@ -1595,7 +1648,10 @@ void gdextension_setup_interface() { REGISTER_INTERFACE_FUNC(placeholder_script_instance_create); REGISTER_INTERFACE_FUNC(placeholder_script_instance_update); REGISTER_INTERFACE_FUNC(object_get_script_instance); +#ifndef DISABLE_DEPRECATED REGISTER_INTERFACE_FUNC(callable_custom_create); +#endif // DISABLE_DEPRECATED + REGISTER_INTERFACE_FUNC(callable_custom_create2); REGISTER_INTERFACE_FUNC(callable_custom_get_userdata); REGISTER_INTERFACE_FUNC(classdb_construct_object); REGISTER_INTERFACE_FUNC(classdb_get_method_bind); diff --git a/core/extension/gdextension_interface.h b/core/extension/gdextension_interface.h index e7497a9d4c..c863507019 100644 --- a/core/extension/gdextension_interface.h +++ b/core/extension/gdextension_interface.h @@ -442,6 +442,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. * @@ -471,7 +473,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 */ @@ -2510,6 +2545,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. * @@ -2521,6 +2557,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/core/object/callable_method_pointer.h b/core/object/callable_method_pointer.h index f8e8c4d7e9..09fe9679f7 100644 --- a/core/object/callable_method_pointer.h +++ b/core/object/callable_method_pointer.h @@ -93,6 +93,11 @@ public: return data.instance->get_instance_id(); } + virtual int get_argument_count(bool &r_is_valid) const { + r_is_valid = true; + return sizeof...(P); + } + virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const { ERR_FAIL_NULL_MSG(ObjectDB::get_instance(ObjectID(data.object_id)), "Invalid Object id '" + uitos(data.object_id) + "', can't call method."); call_with_variant_args(data.instance, data.method, p_arguments, p_argcount, r_call_error); @@ -140,6 +145,11 @@ public: return data.instance->get_instance_id(); } + virtual int get_argument_count(bool &r_is_valid) const { + r_is_valid = true; + return sizeof...(P); + } + virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const { ERR_FAIL_NULL_MSG(ObjectDB::get_instance(ObjectID(data.object_id)), "Invalid Object id '" + uitos(data.object_id) + "', can't call method."); call_with_variant_args_ret(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error); @@ -187,6 +197,11 @@ public: return 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, Callable::CallError &r_call_error) const override { ERR_FAIL_NULL_MSG(ObjectDB::get_instance(ObjectID(data.object_id)), "Invalid Object id '" + uitos(data.object_id) + "', can't call method."); call_with_variant_args_retc(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error); @@ -238,6 +253,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, Callable::CallError &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(); @@ -280,6 +300,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, Callable::CallError &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/core/object/class_db.cpp b/core/object/class_db.cpp index 231a8e4d68..80a2703c2f 100644 --- a/core/object/class_db.cpp +++ b/core/object/class_db.cpp @@ -1666,6 +1666,31 @@ bool ClassDB::has_method(const StringName &p_class, const StringName &p_method, return false; } +int ClassDB::get_method_argument_count(const StringName &p_class, const StringName &p_method, bool *r_is_valid, bool p_no_inheritance) { + OBJTYPE_RLOCK; + + ClassInfo *type = classes.getptr(p_class); + + while (type) { + MethodBind **method = type->method_map.getptr(p_method); + if (method && *method) { + if (r_is_valid) { + *r_is_valid = true; + } + return (*method)->get_argument_count(); + } + if (p_no_inheritance) { + break; + } + type = type->inherits_ptr; + } + + if (r_is_valid) { + *r_is_valid = false; + } + return 0; +} + void ClassDB::bind_method_custom(const StringName &p_class, MethodBind *p_method) { _bind_method_custom(p_class, p_method, false); } diff --git a/core/object/class_db.h b/core/object/class_db.h index 7f117b4a9b..3b146dd06e 100644 --- a/core/object/class_db.h +++ b/core/object/class_db.h @@ -430,6 +430,7 @@ public: static void get_method_list(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance = false, bool p_exclude_from_properties = false); static void get_method_list_with_compatibility(const StringName &p_class, List<Pair<MethodInfo, uint32_t>> *p_methods_with_hash, bool p_no_inheritance = false, bool p_exclude_from_properties = false); static bool get_method_info(const StringName &p_class, const StringName &p_method, MethodInfo *r_info, bool p_no_inheritance = false, bool p_exclude_from_properties = false); + static int get_method_argument_count(const StringName &p_class, const StringName &p_method, bool *r_is_valid = nullptr, bool p_no_inheritance = false); static MethodBind *get_method(const StringName &p_class, const StringName &p_name); static MethodBind *get_method_with_compatibility(const StringName &p_class, const StringName &p_name, uint64_t p_hash, bool *r_method_exists = nullptr, bool *r_is_deprecated = nullptr); static Vector<uint32_t> get_method_compatibility_hashes(const StringName &p_class, const StringName &p_name); diff --git a/core/object/object.cpp b/core/object/object.cpp index 6a5a9efefa..e5d771844b 100644 --- a/core/object/object.cpp +++ b/core/object/object.cpp @@ -688,6 +688,59 @@ bool Object::has_method(const StringName &p_method) const { return false; } +int Object::_get_method_argument_count_bind(const StringName &p_method) const { + return get_method_argument_count(p_method); +} + +int Object::get_method_argument_count(const StringName &p_method, bool *r_is_valid) const { + if (p_method == CoreStringNames::get_singleton()->_free) { + if (r_is_valid) { + *r_is_valid = true; + } + return 0; + } + + if (script_instance) { + bool valid = false; + int ret = script_instance->get_method_argument_count(p_method, &valid); + if (valid) { + if (r_is_valid) { + *r_is_valid = true; + } + return ret; + } + } + + { + bool valid = false; + int ret = ClassDB::get_method_argument_count(get_class_name(), p_method, &valid); + if (valid) { + if (r_is_valid) { + *r_is_valid = true; + } + return ret; + } + } + + const Script *scr = Object::cast_to<Script>(this); + while (scr != nullptr) { + bool valid = false; + int ret = scr->get_script_method_argument_count(p_method, &valid); + if (valid) { + if (r_is_valid) { + *r_is_valid = true; + } + return ret; + } + scr = scr->get_base_script().ptr(); + } + + if (r_is_valid) { + *r_is_valid = false; + } + return 0; +} + Variant Object::getvar(const Variant &p_key, bool *r_valid) const { if (r_valid) { *r_valid = false; @@ -1644,6 +1697,8 @@ void Object::_bind_methods() { ClassDB::bind_method(D_METHOD("has_method", "method"), &Object::has_method); + ClassDB::bind_method(D_METHOD("get_method_argument_count", "method"), &Object::_get_method_argument_count_bind); + ClassDB::bind_method(D_METHOD("has_signal", "signal"), &Object::has_signal); ClassDB::bind_method(D_METHOD("get_signal_list"), &Object::_get_signal_list); ClassDB::bind_method(D_METHOD("get_signal_connection_list", "signal"), &Object::_get_signal_connection_list); diff --git a/core/object/object.h b/core/object/object.h index cb1495296d..2efcf70670 100644 --- a/core/object/object.h +++ b/core/object/object.h @@ -654,6 +654,7 @@ private: Variant _get_bind(const StringName &p_name) const; void _set_indexed_bind(const NodePath &p_name, const Variant &p_value); Variant _get_indexed_bind(const NodePath &p_name) const; + int _get_method_argument_count_bind(const StringName &p_name) const; _FORCE_INLINE_ void _construct_object(bool p_reference); @@ -865,6 +866,7 @@ public: Variant property_get_revert(const StringName &p_name) const; bool has_method(const StringName &p_method) const; + int get_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const; void get_method_list(List<MethodInfo> *p_list) const; Variant callv(const StringName &p_method, const Array &p_args); virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error); diff --git a/core/object/script_instance.cpp b/core/object/script_instance.cpp index 303b127db1..65f44e8a6b 100644 --- a/core/object/script_instance.cpp +++ b/core/object/script_instance.cpp @@ -32,6 +32,28 @@ #include "core/object/script_language.h" +int ScriptInstance::get_method_argument_count(const StringName &p_method, bool *r_is_valid) const { + // Default implementation simply traverses hierarchy. + Ref<Script> script = get_script(); + while (script.is_valid()) { + bool valid = false; + int ret = script->get_script_method_argument_count(p_method, &valid); + if (valid) { + if (r_is_valid) { + *r_is_valid = true; + } + return ret; + } + + script = script->get_base_script(); + } + + if (r_is_valid) { + *r_is_valid = false; + } + return 0; +} + Variant ScriptInstance::call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { return callp(p_method, p_args, p_argcount, r_error); } diff --git a/core/object/script_instance.h b/core/object/script_instance.h index 45d51534fc..2c8132ec1f 100644 --- a/core/object/script_instance.h +++ b/core/object/script_instance.h @@ -53,6 +53,8 @@ public: virtual void get_method_list(List<MethodInfo> *p_list) const = 0; virtual bool has_method(const StringName &p_method) const = 0; + virtual int get_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const; + virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) = 0; template <typename... VarArgs> diff --git a/core/object/script_language.cpp b/core/object/script_language.cpp index 14894e1710..1196c2f787 100644 --- a/core/object/script_language.cpp +++ b/core/object/script_language.cpp @@ -102,6 +102,22 @@ Dictionary Script::_get_script_constant_map() { return ret; } +int Script::get_script_method_argument_count(const StringName &p_method, bool *r_is_valid) const { + MethodInfo mi = get_method_info(p_method); + + if (mi == MethodInfo()) { + if (r_is_valid) { + *r_is_valid = false; + } + return 0; + } + + if (r_is_valid) { + *r_is_valid = true; + } + return mi.arguments.size(); +} + #ifdef TOOLS_ENABLED PropertyInfo Script::get_class_category() const { diff --git a/core/object/script_language.h b/core/object/script_language.h index 95e9d2b4af..be50e58d79 100644 --- a/core/object/script_language.h +++ b/core/object/script_language.h @@ -151,6 +151,8 @@ public: virtual bool has_method(const StringName &p_method) const = 0; virtual bool has_static_method(const StringName &p_method) const { return false; } + virtual int get_script_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const; + virtual MethodInfo get_method_info(const StringName &p_method) const = 0; virtual bool is_tool() const = 0; @@ -442,6 +444,13 @@ public: virtual void get_method_list(List<MethodInfo> *p_list) const override; virtual bool has_method(const StringName &p_method) const override; + virtual int get_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const override { + if (r_is_valid) { + *r_is_valid = false; + } + return 0; + } + virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override { r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; return Variant(); diff --git a/core/object/script_language_extension.cpp b/core/object/script_language_extension.cpp index ec99c7cf4e..a18ef8d4d7 100644 --- a/core/object/script_language_extension.cpp +++ b/core/object/script_language_extension.cpp @@ -56,6 +56,9 @@ void ScriptExtension::_bind_methods() { GDVIRTUAL_BIND(_has_method, "method"); GDVIRTUAL_BIND(_has_static_method, "method"); + + GDVIRTUAL_BIND(_get_script_method_argument_count, "method"); + GDVIRTUAL_BIND(_get_method_info, "method"); GDVIRTUAL_BIND(_is_tool); diff --git a/core/object/script_language_extension.h b/core/object/script_language_extension.h index 18105ec8cd..efb317b839 100644 --- a/core/object/script_language_extension.h +++ b/core/object/script_language_extension.h @@ -101,6 +101,19 @@ public: EXBIND1RC(bool, has_method, const StringName &) EXBIND1RC(bool, has_static_method, const StringName &) + GDVIRTUAL1RC(Variant, _get_script_method_argument_count, const StringName &) + virtual int get_script_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const override { + Variant ret; + if (GDVIRTUAL_CALL(_get_script_method_argument_count, p_method, ret) && ret.get_type() == Variant::INT) { + if (r_is_valid) { + *r_is_valid = true; + } + return ret.operator int(); + } + // Fallback to default. + return Script::get_script_method_argument_count(p_method, r_is_valid); + } + GDVIRTUAL1RC(Dictionary, _get_method_info, const StringName &) virtual MethodInfo get_method_info(const StringName &p_method) const override { Dictionary mi; @@ -807,6 +820,11 @@ public: return false; } + virtual int get_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const override { + // Fallback to default. + return ScriptInstance::get_method_argument_count(p_method, r_is_valid); + } + virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override { Variant ret; if (native_info->call_func) { diff --git a/core/variant/callable.cpp b/core/variant/callable.cpp index 47271118a0..c6fbfd93a1 100644 --- a/core/variant/callable.cpp +++ b/core/variant/callable.cpp @@ -184,6 +184,20 @@ StringName Callable::get_method() const { return method; } +int Callable::get_argument_count(bool *r_is_valid) const { + if (is_custom()) { + bool valid = false; + return custom->get_argument_count(r_is_valid ? *r_is_valid : valid); + } else if (!is_null()) { + return get_object()->get_method_argument_count(method, r_is_valid); + } else { + if (r_is_valid) { + *r_is_valid = false; + } + return 0; + } +} + int Callable::get_bound_arguments_count() const { if (!is_null() && is_custom()) { return custom->get_bound_arguments_count(); @@ -438,6 +452,11 @@ const Callable *CallableCustom::get_base_comparator() const { return nullptr; } +int CallableCustom::get_argument_count(bool &r_is_valid) const { + r_is_valid = false; + return 0; +} + int CallableCustom::get_bound_arguments_count() const { return 0; } diff --git a/core/variant/callable.h b/core/variant/callable.h index bba69d453e..63757d9d6e 100644 --- a/core/variant/callable.h +++ b/core/variant/callable.h @@ -109,6 +109,7 @@ public: ObjectID get_object_id() const; StringName get_method() const; CallableCustom *get_custom() const; + int get_argument_count(bool *r_is_valid = nullptr) const; int get_bound_arguments_count() const; void get_bound_arguments_ref(Vector<Variant> &r_arguments, int &r_argcount) const; // Internal engine use, the exposed one is below. Array get_bound_arguments() const; @@ -155,6 +156,7 @@ public: virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const = 0; virtual Error rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const; virtual const Callable *get_base_comparator() const; + virtual int get_argument_count(bool &r_is_valid) const; virtual int get_bound_arguments_count() const; virtual void get_bound_arguments(Vector<Variant> &r_arguments, int &r_argcount) const; diff --git a/core/variant/callable_bind.cpp b/core/variant/callable_bind.cpp index 6d6c60cbd4..d82aa3583d 100644 --- a/core/variant/callable_bind.cpp +++ b/core/variant/callable_bind.cpp @@ -91,6 +91,14 @@ const Callable *CallableCustomBind::get_base_comparator() const { return callable.get_base_comparator(); } +int CallableCustomBind::get_argument_count(bool &r_is_valid) const { + int ret = callable.get_argument_count(&r_is_valid); + if (r_is_valid) { + return ret - binds.size(); + } + return 0; +} + int CallableCustomBind::get_bound_arguments_count() const { return callable.get_bound_arguments_count() + binds.size(); } @@ -225,6 +233,14 @@ const Callable *CallableCustomUnbind::get_base_comparator() const { return callable.get_base_comparator(); } +int CallableCustomUnbind::get_argument_count(bool &r_is_valid) const { + int ret = callable.get_argument_count(&r_is_valid); + if (r_is_valid) { + return ret + argcount; + } + return 0; +} + int CallableCustomUnbind::get_bound_arguments_count() const { return callable.get_bound_arguments_count() - argcount; } diff --git a/core/variant/callable_bind.h b/core/variant/callable_bind.h index 5798797a3d..43cebb45f0 100644 --- a/core/variant/callable_bind.h +++ b/core/variant/callable_bind.h @@ -53,6 +53,7 @@ public: virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override; virtual Error rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const override; virtual const Callable *get_base_comparator() const override; + virtual int get_argument_count(bool &r_is_valid) const override; virtual int get_bound_arguments_count() const override; virtual void get_bound_arguments(Vector<Variant> &r_arguments, int &r_argcount) const override; Callable get_callable() { return callable; } @@ -81,6 +82,7 @@ public: virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override; virtual Error rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const override; virtual const Callable *get_base_comparator() const override; + virtual int get_argument_count(bool &r_is_valid) const override; virtual int get_bound_arguments_count() const override; virtual void get_bound_arguments(Vector<Variant> &r_arguments, int &r_argcount) const override; diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 060ea007ff..40c9a588d8 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1053,6 +1053,10 @@ struct _VariantCall { r_ret = callable->bindp(p_args, p_argcount); } + static int func_Callable_get_argument_count(Callable *p_callable) { + return p_callable->get_argument_count(); + } + static void func_Signal_emit(Variant *v, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) { Signal *signal = VariantGetInternalPtr<Signal>::get_ptr(v); signal->emit(p_args, p_argcount); @@ -2048,6 +2052,7 @@ static void _register_variant_builtin_methods() { bind_method(Callable, get_object, sarray(), varray()); bind_method(Callable, get_object_id, sarray(), varray()); bind_method(Callable, get_method, sarray(), varray()); + bind_function(Callable, get_argument_count, _VariantCall::func_Callable_get_argument_count, sarray(), varray()); bind_method(Callable, get_bound_arguments_count, sarray(), varray()); bind_method(Callable, get_bound_arguments, sarray(), varray()); bind_method(Callable, hash, sarray(), varray()); diff --git a/core/variant/variant_callable.cpp b/core/variant/variant_callable.cpp index dc31b6d1ac..21f9a4f52b 100644 --- a/core/variant/variant_callable.cpp +++ b/core/variant/variant_callable.cpp @@ -68,6 +68,15 @@ ObjectID VariantCallable::get_object() const { return ObjectID(); } +int VariantCallable::get_argument_count(bool &r_is_valid) const { + if (!Variant::has_builtin_method(variant.get_type(), method)) { + r_is_valid = false; + return 0; + } + r_is_valid = true; + return Variant::get_builtin_method_argument_count(variant.get_type(), method); +} + void VariantCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const { Variant v = variant; v.callp(method, p_arguments, p_argcount, r_return_value, r_call_error); diff --git a/core/variant/variant_callable.h b/core/variant/variant_callable.h index 3f2b058aaf..1811f3bb9a 100644 --- a/core/variant/variant_callable.h +++ b/core/variant/variant_callable.h @@ -50,6 +50,7 @@ public: bool is_valid() const override; StringName get_method() const override; ObjectID get_object() const override; + int get_argument_count(bool &r_is_valid) const override; void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override; VariantCallable(const Variant &p_variant, const StringName &p_method); |