From 9cba5ef772e8a4e354bef939a71565d22e304470 Mon Sep 17 00:00:00 2001 From: Paul Joannon Date: Thu, 18 Jan 2018 23:27:43 +0100 Subject: [PATCH] implement signal related methods in csharp_script so signals can be used with emit (cherry picked from commit cfbd7fd21e6630cc513ac3a36849c0f796d142c3) --- modules/mono/csharp_script.cpp | 68 +++++++++++++++++++++++-- modules/mono/csharp_script.h | 12 +++-- modules/mono/mono_gd/gd_mono_method.cpp | 14 +++++ modules/mono/mono_gd/gd_mono_method.h | 3 ++ 4 files changed, 89 insertions(+), 8 deletions(-) diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp index 609cb1c0139..fb45136575f 100644 --- a/modules/mono/csharp_script.cpp +++ b/modules/mono/csharp_script.cpp @@ -1566,13 +1566,13 @@ bool CSharpScript::_update_signals() { while (top && top != native) { const Vector &delegates = top->get_all_delegates(); - for (int i = delegates.size() - 1; i >= 0; i--) { + for (int i = delegates.size() - 1; i >= 0; --i) { + Vector parameters; + GDMonoClass *delegate = delegates[i]; - if (delegate->has_attribute(CACHED_CLASS(SignalAttribute))) { - StringName name = delegate->get_name(); - - _signals[name] = Vector(); // TODO Retrieve arguments + if (_get_signal(top, delegate, parameters)) { + _signals[delegate->get_name()] = parameters; } } @@ -1585,6 +1585,41 @@ bool CSharpScript::_update_signals() { return false; } +bool CSharpScript::_get_signal(GDMonoClass *p_class, GDMonoClass *p_delegate, Vector ¶ms) { + if (p_delegate->has_attribute(CACHED_CLASS(SignalAttribute))) { + MonoType *raw_type = GDMonoClass::get_raw_type(p_delegate); + + if (mono_type_get_type(raw_type) == MONO_TYPE_CLASS) { + // Arguments are accessibles as arguments of .Invoke method + GDMonoMethod *invoke = p_delegate->get_method("Invoke", -1); + + Vector names; + Vector types; + invoke->get_parameter_names(names); + invoke->get_parameter_types(types); + + if (names.size() == types.size()) { + for (int i = 0; i < names.size(); ++i) { + Argument arg; + arg.name = names[i]; + arg.type = GDMonoMarshal::managed_to_variant_type(types[i]); + + if (arg.type == Variant::NIL) { + ERR_PRINTS("Unknown type of signal parameter: " + arg.name + " in " + p_class->get_full_name()); + return false; + } + + params.push_back(arg); + } + + return true; + } + } + } + + return false; +} + #ifdef TOOLS_ENABLED bool CSharpScript::_get_member_export(GDMonoClass *p_class, GDMonoClassMember *p_member, PropertyInfo &r_prop_info, bool &r_exported) { @@ -1913,6 +1948,8 @@ ScriptInstance *CSharpScript::instance_create(Object *p_this) { #endif } + update_signals(); + if (native) { String native_name = native->get_name(); if (!ClassDB::is_parent_class(p_this->get_class_name(), native_name)) { @@ -2076,6 +2113,27 @@ void CSharpScript::update_exports() { #endif } +bool CSharpScript::has_script_signal(const StringName &p_signal) const { + if (_signals.has(p_signal)) + return true; + + return false; +} + +void CSharpScript::get_script_signal_list(List *r_signals) const { + for (const Map >::Element *E = _signals.front(); E; E = E->next()) { + MethodInfo mi; + + mi.name = E->key(); + for (int i = 0; i < E->get().size(); i++) { + PropertyInfo arg; + arg.name = E->get()[i].name; + mi.arguments.push_back(arg); + } + r_signals->push_back(mi); + } +} + void CSharpScript::update_signals() { #ifdef TOOLS_ENABLED _update_signals(); diff --git a/modules/mono/csharp_script.h b/modules/mono/csharp_script.h index 75ed770739f..ffb1d2e0f42 100644 --- a/modules/mono/csharp_script.h +++ b/modules/mono/csharp_script.h @@ -85,13 +85,18 @@ class CSharpScript : public Script { SelfList script_list; + struct Argument { + String name; + Variant::Type type; + }; + #ifdef TOOLS_ENABLED List exported_members_cache; // members_cache Map exported_members_defval_cache; // member_default_values_cache Set placeholders; bool source_changed_cache; bool exports_invalidated; - Map > _signals; + Map > _signals; bool signals_invalidated; void _update_exports_values(Map &values, List &propnames); @@ -107,6 +112,7 @@ class CSharpScript : public Script { void _clear(); bool _update_signals(); + bool _get_signal(GDMonoClass *p_class, GDMonoClass *p_delegate, Vector ¶ms); bool _update_exports(); #ifdef TOOLS_ENABLED @@ -141,8 +147,8 @@ public: virtual Error reload(bool p_keep_state = false); - /* TODO */ virtual bool has_script_signal(const StringName &p_signal) const { return false; } - /* TODO */ virtual void get_script_signal_list(List *r_signals) const {} + virtual bool has_script_signal(const StringName &p_signal) const; + virtual void get_script_signal_list(List *r_signals) const; virtual void update_signals(); /* TODO */ virtual bool get_property_default_value(const StringName &p_property, Variant &r_value) const; diff --git a/modules/mono/mono_gd/gd_mono_method.cpp b/modules/mono/mono_gd/gd_mono_method.cpp index 1f8e9a1926c..df0985f6ac4 100644 --- a/modules/mono/mono_gd/gd_mono_method.cpp +++ b/modules/mono/mono_gd/gd_mono_method.cpp @@ -229,6 +229,20 @@ String GDMonoMethod::get_signature_desc(bool p_namespaces) const { return res; } +void GDMonoMethod::get_parameter_names(Vector &names) const { + const char *_names[params_count]; + mono_method_get_param_names(mono_method, _names); + for (int i = 0; i < params_count; ++i) { + names.push_back(StringName(_names[i])); + } +} + +void GDMonoMethod::get_parameter_types(Vector &types) const { + for (int i = 0; i < param_types.size(); ++i) { + types.push_back(param_types[i]); + } +} + GDMonoMethod::GDMonoMethod(StringName p_name, MonoMethod *p_method) { name = p_name; diff --git a/modules/mono/mono_gd/gd_mono_method.h b/modules/mono/mono_gd/gd_mono_method.h index 14df8dcfb4a..a173af83f46 100644 --- a/modules/mono/mono_gd/gd_mono_method.h +++ b/modules/mono/mono_gd/gd_mono_method.h @@ -80,6 +80,9 @@ public: String get_ret_type_full_name() const; String get_signature_desc(bool p_namespaces = false) const; + void get_parameter_names(Vector &names) const; + void get_parameter_types(Vector &types) const; + GDMonoMethod(StringName p_name, MonoMethod *p_method); ~GDMonoMethod(); };