Add support for getting argument count from Callables

This commit is contained in:
A Thousand Ships
2024-01-30 13:33:03 +01:00
parent e6b6df5893
commit fb79d5ff98
9 changed files with 117 additions and 8 deletions

View File

@@ -173,7 +173,7 @@ 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;
@@ -413,7 +413,7 @@ 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);

View File

@@ -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 {

View File

@@ -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;
}