Browse Source

add a [Signal] attribute to CSharpScripts

Paul Joannon 7 years ago
parent
commit
efd52cd172

+ 49 - 1
modules/mono/csharp_script.cpp

@@ -721,8 +721,10 @@ void CSharpLanguage::reload_assemblies_if_needed(bool p_soft_reload) {
 	for (Map<Ref<CSharpScript>, Map<ObjectID, List<Pair<StringName, Variant> > > >::Element *E = to_reload.front(); E; E = E->next()) {
 
 		Ref<CSharpScript> scr = E->key();
+		scr->signals_invalidated = true;
 		scr->exports_invalidated = true;
 		scr->reload(p_soft_reload);
+		scr->update_signals();
 		scr->update_exports();
 
 		//restore state if saved
@@ -755,8 +757,10 @@ void CSharpLanguage::reload_assemblies_if_needed(bool p_soft_reload) {
 		//if instance states were saved, set them!
 	}
 
-	if (Engine::get_singleton()->is_editor_hint())
+	if (Engine::get_singleton()->is_editor_hint()) {
 		EditorNode::get_singleton()->get_property_editor()->update_tree();
+		NodeDock::singleton->update_lists();
+	}
 }
 #endif
 
@@ -1545,6 +1549,42 @@ bool CSharpScript::_update_exports() {
 	return false;
 }
 
+bool CSharpScript::_update_signals() {
+#ifdef TOOLS_ENABLED
+	if (!valid)
+		return false;
+
+	bool changed = false;
+
+	if (signals_invalidated) {
+		signals_invalidated = false;
+
+		GDMonoClass *top = script_class;
+
+		_signals.clear();
+		changed = true; // TODO Do a real check for change
+
+		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];
+
+				if (delegate->has_attribute(CACHED_CLASS(SignalAttribute))) {
+					StringName name = delegate->get_name();
+
+					_signals[name] = Vector<StringName>(); // TODO Retrieve arguments
+				}
+			}
+
+			top = top->get_parent_class();
+		}
+	}
+
+	return changed;
+#endif
+	return false;
+}
+
 #ifdef TOOLS_ENABLED
 bool CSharpScript::_get_member_export(GDMonoClass *p_class, GDMonoClassMember *p_member, PropertyInfo &r_prop_info, bool &r_exported) {
 
@@ -1866,6 +1906,7 @@ ScriptInstance *CSharpScript::instance_create(Object *p_this) {
 		PlaceHolderScriptInstance *si = memnew(PlaceHolderScriptInstance(CSharpLanguage::get_singleton(), Ref<Script>(this), p_this));
 		placeholders.insert(si);
 		_update_exports();
+		_update_signals();
 		return si;
 #else
 		return NULL;
@@ -2035,6 +2076,12 @@ void CSharpScript::update_exports() {
 #endif
 }
 
+void CSharpScript::update_signals() {
+#ifdef TOOLS_ENABLED
+	_update_signals();
+#endif
+}
+
 Ref<Script> CSharpScript::get_base_script() const {
 
 	// TODO search in metadata file once we have it, not important any way?
@@ -2099,6 +2146,7 @@ CSharpScript::CSharpScript() :
 #ifdef TOOLS_ENABLED
 	source_changed_cache = false;
 	exports_invalidated = true;
+	signals_invalidated = true;
 #endif
 
 	_resource_path_changed();

+ 5 - 0
modules/mono/csharp_script.h

@@ -91,6 +91,8 @@ class CSharpScript : public Script {
 	Set<PlaceHolderScriptInstance *> placeholders;
 	bool source_changed_cache;
 	bool exports_invalidated;
+	Map<StringName, Vector<StringName> > _signals;
+	bool signals_invalidated;
 
 	void _update_exports_values(Map<StringName, Variant> &values, List<PropertyInfo> &propnames);
 	virtual void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder);
@@ -104,6 +106,8 @@ class CSharpScript : public Script {
 
 	void _clear();
 
+	bool _update_signals();
+
 	bool _update_exports();
 #ifdef TOOLS_ENABLED
 	bool _get_member_export(GDMonoClass *p_class, GDMonoClassMember *p_member, PropertyInfo &r_prop_info, bool &r_exported);
@@ -139,6 +143,7 @@ public:
 
 	/* 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 void update_signals();
 
 	/* TODO */ virtual bool get_property_default_value(const StringName &p_property, Variant &r_value) const;
 	virtual void get_script_property_list(List<PropertyInfo> *p_list) const;

+ 12 - 0
modules/mono/glue/cs_files/SignalAttribute.cs

@@ -0,0 +1,12 @@
+using System;
+
+namespace Godot
+{
+    [AttributeUsage(AttributeTargets.Delegate)]
+    public class SignalAttribute : Attribute
+    {
+        public SignalAttribute()
+        {
+        }
+    }
+}

+ 28 - 0
modules/mono/mono_gd/gd_mono_class.cpp

@@ -404,6 +404,33 @@ const Vector<GDMonoProperty *> &GDMonoClass::get_all_properties() {
 	return properties_list;
 }
 
+const Vector<GDMonoClass *> &GDMonoClass::get_all_delegates() {
+	if (delegates_fetched)
+		return delegates_list;
+
+	void *iter = NULL;
+	MonoClass *raw_class = NULL;
+	while ((raw_class = mono_class_get_nested_types(mono_class, &iter)) != NULL) {
+		if (mono_class_is_delegate(raw_class)) {
+			StringName name = mono_class_get_name(raw_class);
+
+			Map<StringName, GDMonoClass *>::Element *match = delegates.find(name);
+
+			if (match) {
+				delegates_list.push_back(match->get());
+			} else {
+				GDMonoClass *delegate = memnew(GDMonoClass(mono_class_get_namespace(raw_class), mono_class_get_name(raw_class), raw_class, assembly));
+				delegates.insert(name, delegate);
+				delegates_list.push_back(delegate);
+			}
+		}
+	}
+
+	delegates_fetched = true;
+
+	return delegates_list;
+}
+
 GDMonoClass::GDMonoClass(const StringName &p_namespace, const StringName &p_name, MonoClass *p_class, GDMonoAssembly *p_assembly) {
 
 	namespace_name = p_namespace;
@@ -417,6 +444,7 @@ GDMonoClass::GDMonoClass(const StringName &p_namespace, const StringName &p_name
 	methods_fetched = false;
 	fields_fetched = false;
 	properties_fetched = false;
+	delegates_fetched = false;
 }
 
 GDMonoClass::~GDMonoClass() {

+ 6 - 0
modules/mono/mono_gd/gd_mono_class.h

@@ -90,6 +90,10 @@ class GDMonoClass {
 	Map<StringName, GDMonoProperty *> properties;
 	Vector<GDMonoProperty *> properties_list;
 
+	bool delegates_fetched;
+	Map<StringName, GDMonoClass *> delegates;
+	Vector<GDMonoClass *> delegates_list;
+
 	friend class GDMonoAssembly;
 	GDMonoClass(const StringName &p_namespace, const StringName &p_name, MonoClass *p_class, GDMonoAssembly *p_assembly);
 
@@ -133,6 +137,8 @@ public:
 	GDMonoProperty *get_property(const StringName &p_name);
 	const Vector<GDMonoProperty *> &get_all_properties();
 
+	const Vector<GDMonoClass *> &get_all_delegates();
+
 	~GDMonoClass();
 };
 

+ 2 - 0
modules/mono/mono_gd/gd_mono_utils.cpp

@@ -114,6 +114,7 @@ void MonoCache::clear_members() {
 	class_ExportAttribute = NULL;
 	field_ExportAttribute_hint = NULL;
 	field_ExportAttribute_hintString = NULL;
+	class_SignalAttribute = NULL;
 	class_ToolAttribute = NULL;
 	class_RemoteAttribute = NULL;
 	class_SyncAttribute = NULL;
@@ -201,6 +202,7 @@ void update_godot_api_cache() {
 	CACHE_CLASS_AND_CHECK(ExportAttribute, GODOT_API_CLASS(ExportAttribute));
 	CACHE_FIELD_AND_CHECK(ExportAttribute, hint, CACHED_CLASS(ExportAttribute)->get_field("hint"));
 	CACHE_FIELD_AND_CHECK(ExportAttribute, hintString, CACHED_CLASS(ExportAttribute)->get_field("hintString"));
+	CACHE_CLASS_AND_CHECK(SignalAttribute, GODOT_API_CLASS(SignalAttribute));
 	CACHE_CLASS_AND_CHECK(ToolAttribute, GODOT_API_CLASS(ToolAttribute));
 	CACHE_CLASS_AND_CHECK(RemoteAttribute, GODOT_API_CLASS(RemoteAttribute));
 	CACHE_CLASS_AND_CHECK(SyncAttribute, GODOT_API_CLASS(SyncAttribute));

+ 1 - 0
modules/mono/mono_gd/gd_mono_utils.h

@@ -108,6 +108,7 @@ struct MonoCache {
 	GDMonoClass *class_ExportAttribute;
 	GDMonoField *field_ExportAttribute_hint;
 	GDMonoField *field_ExportAttribute_hintString;
+	GDMonoClass *class_SignalAttribute;
 	GDMonoClass *class_ToolAttribute;
 	GDMonoClass *class_RemoteAttribute;
 	GDMonoClass *class_SyncAttribute;