Merge pull request #16770 from paulloz/csharp-signal-attribute

C# Signal attribute
This commit is contained in:
Rémi Verschelde 2018-02-17 20:14:39 +01:00 committed by GitHub
commit 2d0c07bd5a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 186 additions and 3 deletions

View file

@ -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,77 @@ 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) {
Vector<Argument> parameters;
GDMonoClass *delegate = delegates[i];
if (_get_signal(top, delegate, parameters)) {
_signals[delegate->get_name()] = parameters;
}
}
top = top->get_parent_class();
}
}
return changed;
#endif
return false;
}
bool CSharpScript::_get_signal(GDMonoClass *p_class, GDMonoClass *p_delegate, Vector<Argument> &params) {
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) {
@ -1866,12 +1941,15 @@ 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;
#endif
}
update_signals();
if (native) {
String native_name = native->get_name();
if (!ClassDB::is_parent_class(p_this->get_class_name(), native_name)) {
@ -2035,6 +2113,33 @@ 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();
#endif
}
Ref<Script> CSharpScript::get_base_script() const {
// TODO search in metadata file once we have it, not important any way?
@ -2099,6 +2204,7 @@ CSharpScript::CSharpScript() :
#ifdef TOOLS_ENABLED
source_changed_cache = false;
exports_invalidated = true;
signals_invalidated = true;
#endif
_resource_path_changed();

View file

@ -85,12 +85,19 @@ 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<Argument> > _signals;
bool signals_invalidated;
void _update_exports_values(Map<StringName, Variant> &values, List<PropertyInfo> &propnames);
virtual void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder);
@ -104,6 +111,9 @@ class CSharpScript : public Script {
void _clear();
bool _update_signals();
bool _get_signal(GDMonoClass *p_class, GDMonoClass *p_delegate, Vector<Argument> &params);
bool _update_exports();
#ifdef TOOLS_ENABLED
bool _get_member_export(GDMonoClass *p_class, GDMonoClassMember *p_member, PropertyInfo &r_prop_info, bool &r_exported);
@ -137,8 +147,9 @@ 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;
virtual void get_script_property_list(List<PropertyInfo> *p_list) const;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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