From 980f5f32f492ad7e55915f37a6104789d43c89e1 Mon Sep 17 00:00:00 2001 From: Yuri Sizov Date: Fri, 12 Aug 2022 21:43:14 +0300 Subject: Make `property_*_revert` methods multilevel and expose them for scripting --- modules/mono/csharp_script.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++ modules/mono/csharp_script.h | 5 +++ 2 files changed, 75 insertions(+) (limited to 'modules/mono') diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp index 475b483d6c..b95b63cf1f 100644 --- a/modules/mono/csharp_script.cpp +++ b/modules/mono/csharp_script.cpp @@ -1853,6 +1853,74 @@ Variant::Type CSharpInstance::get_property_type(const StringName &p_name, bool * return Variant::NIL; } +bool CSharpInstance::property_can_revert(const StringName &p_name) const { + ERR_FAIL_COND_V(!script.is_valid(), false); + + GD_MONO_SCOPE_THREAD_ATTACH; + + MonoObject *mono_object = get_mono_object(); + ERR_FAIL_NULL_V(mono_object, false); + + GDMonoClass *top = script->script_class; + + while (top && top != script->native) { + GDMonoMethod *method = top->get_method(CACHED_STRING_NAME(_property_can_revert), 1); + + if (method) { + Variant name = p_name; + const Variant *args[1] = { &name }; + + MonoObject *ret = method->invoke(mono_object, args); + + if (ret) { + bool can_revert = GDMonoMarshal::mono_object_to_variant(ret); + if (can_revert) { + return true; + } + } + + break; + } + + top = top->get_parent_class(); + } + + return false; +} + +bool CSharpInstance::property_get_revert(const StringName &p_name, Variant &r_ret) const { + ERR_FAIL_COND_V(!script.is_valid(), false); + + GD_MONO_SCOPE_THREAD_ATTACH; + + MonoObject *mono_object = get_mono_object(); + ERR_FAIL_NULL_V(mono_object, false); + + GDMonoClass *top = script->script_class; + + while (top && top != script->native) { + GDMonoMethod *method = top->get_method(CACHED_STRING_NAME(_property_get_revert), 1); + + if (method) { + Variant name = p_name; + const Variant *args[1] = { &name }; + + MonoObject *ret = method->invoke(mono_object, args); + + if (ret) { + r_ret = GDMonoMarshal::mono_object_to_variant(ret); + return true; + } + + break; + } + + top = top->get_parent_class(); + } + + return false; +} + void CSharpInstance::get_method_list(List *p_list) const { if (!script->is_valid() || !script->script_class) { return; @@ -3705,6 +3773,8 @@ CSharpLanguage::StringNameCache::StringNameCache() { _set = StaticCString::create("_set"); _get = StaticCString::create("_get"); _get_property_list = StaticCString::create("_get_property_list"); + _property_can_revert = StaticCString::create("_property_can_revert"); + _property_get_revert = StaticCString::create("_property_get_revert"); _notification = StaticCString::create("_notification"); _script_source = StaticCString::create("script/source"); on_before_serialize = StaticCString::create("OnBeforeSerialize"); diff --git a/modules/mono/csharp_script.h b/modules/mono/csharp_script.h index 48129e69cb..823de91bf6 100644 --- a/modules/mono/csharp_script.h +++ b/modules/mono/csharp_script.h @@ -293,6 +293,9 @@ public: void get_property_list(List *p_properties) const override; Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid) const override; + bool property_can_revert(const StringName &p_name) const override; + bool property_get_revert(const StringName &p_name, Variant &r_ret) const override; + void get_method_list(List *p_list) const override; bool has_method(const StringName &p_method) const override; Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override; @@ -371,6 +374,8 @@ class CSharpLanguage : public ScriptLanguage { StringName _set; StringName _get; StringName _get_property_list; + StringName _property_can_revert; + StringName _property_get_revert; StringName _notification; StringName _script_source; StringName dotctor; // .ctor -- cgit v1.2.3