diff options
author | Paul Joannon <hello@pauljoannon.com> | 2018-01-18 23:27:43 +0100 |
---|---|---|
committer | Paul Joannon <hello@pauljoannon.com> | 2018-02-17 19:37:02 +0100 |
commit | cfbd7fd21e6630cc513ac3a36849c0f796d142c3 (patch) | |
tree | 60d5f142ec6c1a989f26223d16ce2a327e016641 /modules | |
parent | efd52cd1725145dc9c8038477dbe133b23868e99 (diff) |
implement signal related methods in csharp_script so signals can be used with emit
Diffstat (limited to 'modules')
-rw-r--r-- | modules/mono/csharp_script.cpp | 68 | ||||
-rw-r--r-- | modules/mono/csharp_script.h | 12 | ||||
-rw-r--r-- | modules/mono/mono_gd/gd_mono_method.cpp | 14 | ||||
-rw-r--r-- | 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 609cb1c013..fb45136575 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<GDMonoClass *> &delegates = top->get_all_delegates(); - for (int i = delegates.size() - 1; i >= 0; i--) { - GDMonoClass *delegate = delegates[i]; + for (int i = delegates.size() - 1; i >= 0; --i) { + Vector<Argument> parameters; - if (delegate->has_attribute(CACHED_CLASS(SignalAttribute))) { - StringName name = delegate->get_name(); + GDMonoClass *delegate = delegates[i]; - _signals[name] = Vector<StringName>(); // 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<Argument> ¶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<StringName> names; + Vector<ManagedType> 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<MethodInfo> *r_signals) const { + for (const Map<StringName, Vector<Argument> >::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 75ed770739..ffb1d2e0f4 100644 --- a/modules/mono/csharp_script.h +++ b/modules/mono/csharp_script.h @@ -85,13 +85,18 @@ class CSharpScript : public Script { SelfList<CSharpScript> script_list; + struct Argument { + String name; + Variant::Type type; + }; + #ifdef TOOLS_ENABLED List<PropertyInfo> exported_members_cache; // members_cache Map<StringName, Variant> exported_members_defval_cache; // member_default_values_cache Set<PlaceHolderScriptInstance *> placeholders; bool source_changed_cache; bool exports_invalidated; - Map<StringName, Vector<StringName> > _signals; + Map<StringName, Vector<Argument> > _signals; bool signals_invalidated; void _update_exports_values(Map<StringName, Variant> &values, List<PropertyInfo> &propnames); @@ -107,6 +112,7 @@ class CSharpScript : public Script { void _clear(); bool _update_signals(); + bool _get_signal(GDMonoClass *p_class, GDMonoClass *p_delegate, Vector<Argument> ¶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<MethodInfo> *r_signals) const {} + virtual bool has_script_signal(const StringName &p_signal) const; + virtual void get_script_signal_list(List<MethodInfo> *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 1f8e9a1926..df0985f6ac 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<StringName> &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<ManagedType> &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 14df8dcfb4..a173af83f4 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<StringName> &names) const; + void get_parameter_types(Vector<ManagedType> &types) const; + GDMonoMethod(StringName p_name, MonoMethod *p_method); ~GDMonoMethod(); }; |