diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/gdnative/nativescript/godot_nativescript.cpp | 28 | ||||
-rw-r--r-- | modules/gdnative/nativescript/nativescript.cpp | 342 | ||||
-rw-r--r-- | modules/gdnative/nativescript/nativescript.h | 29 | ||||
-rw-r--r-- | modules/gdnative/pluginscript/pluginscript_instance.cpp | 32 | ||||
-rw-r--r-- | modules/gdnative/pluginscript/pluginscript_instance.h | 9 | ||||
-rw-r--r-- | modules/gdnative/pluginscript/pluginscript_script.cpp | 105 | ||||
-rw-r--r-- | modules/gdnative/pluginscript/pluginscript_script.h | 17 | ||||
-rw-r--r-- | modules/gdscript/gdscript.cpp | 155 | ||||
-rw-r--r-- | modules/gdscript/gdscript.h | 23 | ||||
-rw-r--r-- | modules/mono/csharp_script.cpp | 215 | ||||
-rw-r--r-- | modules/mono/csharp_script.h | 28 | ||||
-rw-r--r-- | modules/visual_script/visual_script.cpp | 125 | ||||
-rw-r--r-- | modules/visual_script/visual_script.h | 23 |
13 files changed, 961 insertions, 170 deletions
diff --git a/modules/gdnative/nativescript/godot_nativescript.cpp b/modules/gdnative/nativescript/godot_nativescript.cpp index e19e675344..f953206a34 100644 --- a/modules/gdnative/nativescript/godot_nativescript.cpp +++ b/modules/gdnative/nativescript/godot_nativescript.cpp @@ -36,6 +36,7 @@ #include "core/project_settings.h" #include "core/variant.h" #include "gdnative/gdnative.h" +#include <stdint.h> #include "nativescript.h" @@ -67,6 +68,14 @@ void GDAPI godot_nativescript_register_class(void *p_gdnative_handle, const char if (classes->has(p_base)) { desc.base_data = &(*classes)[p_base]; desc.base_native_type = desc.base_data->base_native_type; + + const NativeScriptDesc *b = desc.base_data; + while (b) { + desc.rpc_count += b->rpc_count; + desc.rset_count += b->rset_count; + b = b->base_data; + } + } else { desc.base_data = NULL; desc.base_native_type = p_base; @@ -87,10 +96,20 @@ void GDAPI godot_nativescript_register_tool_class(void *p_gdnative_handle, const desc.destroy_func = p_destroy_func; desc.is_tool = true; desc.base = p_base; + desc.rpc_count = 0; + desc.rset_count = 0; if (classes->has(p_base)) { desc.base_data = &(*classes)[p_base]; desc.base_native_type = desc.base_data->base_native_type; + + const NativeScriptDesc *b = desc.base_data; + while (b) { + desc.rpc_count += b->rpc_count; + desc.rset_count += b->rset_count; + b = b->base_data; + } + } else { desc.base_data = NULL; desc.base_native_type = p_base; @@ -109,6 +128,11 @@ void GDAPI godot_nativescript_register_method(void *p_gdnative_handle, const cha NativeScriptDesc::Method method; method.method = p_method; method.rpc_mode = p_attr.rpc_type; + method.rpc_method_id = UINT16_MAX; + if (p_attr.rpc_type != GODOT_METHOD_RPC_MODE_DISABLED) { + method.rpc_method_id = E->get().rpc_count; + E->get().rpc_count += 1; + } method.info = MethodInfo(p_function_name); E->get().methods.insert(p_function_name, method); @@ -125,6 +149,10 @@ void GDAPI godot_nativescript_register_property(void *p_gdnative_handle, const c property.default_value = *(Variant *)&p_attr->default_value; property.getter = p_get_func; property.rset_mode = p_attr->rset_type; + if (p_attr->rset_type != GODOT_METHOD_RPC_MODE_DISABLED) { + property.rset_property_id = E->get().rset_count; + E->get().rset_count += 1; + } property.setter = p_set_func; property.info = PropertyInfo((Variant::Type)p_attr->type, p_path, diff --git a/modules/gdnative/nativescript/nativescript.cpp b/modules/gdnative/nativescript/nativescript.cpp index 8b06af6c7b..df85155ff5 100644 --- a/modules/gdnative/nativescript/nativescript.cpp +++ b/modules/gdnative/nativescript/nativescript.cpp @@ -30,6 +30,8 @@ #include "nativescript.h" +#include <stdint.h> + #include "gdnative/gdnative.h" #include "core/core_string_names.h" @@ -402,6 +404,262 @@ void NativeScript::get_script_property_list(List<PropertyInfo> *p_list) const { } } +Vector<ScriptNetData> NativeScript::get_rpc_methods() const { + + Vector<ScriptNetData> v; + + NativeScriptDesc *script_data = get_script_desc(); + + while (script_data) { + + for (Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.front(); E; E = E->next()) { + if (E->get().rpc_mode != GODOT_METHOD_RPC_MODE_DISABLED) { + ScriptNetData nd; + nd.name = E->key(); + nd.mode = MultiplayerAPI::RPCMode(E->get().rpc_mode); + v.push_back(nd); + } + } + + script_data = script_data->base_data; + } + + return v; +} + +uint16_t NativeScript::get_rpc_method_id(const StringName &p_method) const { + NativeScriptDesc *script_data = get_script_desc(); + + while (script_data) { + + Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method); + if (E) { + return E->get().rpc_method_id; + } + + script_data = script_data->base_data; + } + + return UINT16_MAX; +} + +StringName NativeScript::get_rpc_method(uint16_t p_id) const { + ERR_FAIL_COND_V(p_id == UINT16_MAX, StringName()); + + NativeScriptDesc *script_data = get_script_desc(); + + while (script_data) { + + for (Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.front(); E; E = E->next()) { + if (E->get().rpc_method_id == p_id) { + return E->key(); + } + } + + script_data = script_data->base_data; + } + + return StringName(); +} + +MultiplayerAPI::RPCMode NativeScript::get_rpc_mode_by_id(uint16_t p_id) const { + + ERR_FAIL_COND_V(p_id == UINT16_MAX, MultiplayerAPI::RPC_MODE_DISABLED); + + NativeScriptDesc *script_data = get_script_desc(); + + while (script_data) { + + for (Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.front(); E; E = E->next()) { + if (E->get().rpc_method_id == p_id) { + switch (E->get().rpc_mode) { + case GODOT_METHOD_RPC_MODE_DISABLED: + return MultiplayerAPI::RPC_MODE_DISABLED; + case GODOT_METHOD_RPC_MODE_REMOTE: + return MultiplayerAPI::RPC_MODE_REMOTE; + case GODOT_METHOD_RPC_MODE_MASTER: + return MultiplayerAPI::RPC_MODE_MASTER; + case GODOT_METHOD_RPC_MODE_PUPPET: + return MultiplayerAPI::RPC_MODE_PUPPET; + case GODOT_METHOD_RPC_MODE_REMOTESYNC: + return MultiplayerAPI::RPC_MODE_REMOTESYNC; + case GODOT_METHOD_RPC_MODE_MASTERSYNC: + return MultiplayerAPI::RPC_MODE_MASTERSYNC; + case GODOT_METHOD_RPC_MODE_PUPPETSYNC: + return MultiplayerAPI::RPC_MODE_PUPPETSYNC; + default: + return MultiplayerAPI::RPC_MODE_DISABLED; + } + } + } + + script_data = script_data->base_data; + } + + return MultiplayerAPI::RPC_MODE_DISABLED; +} + +MultiplayerAPI::RPCMode NativeScript::get_rpc_mode(const StringName &p_method) const { + + NativeScriptDesc *script_data = get_script_desc(); + + while (script_data) { + + Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method); + if (E) { + switch (E->get().rpc_mode) { + case GODOT_METHOD_RPC_MODE_DISABLED: + return MultiplayerAPI::RPC_MODE_DISABLED; + case GODOT_METHOD_RPC_MODE_REMOTE: + return MultiplayerAPI::RPC_MODE_REMOTE; + case GODOT_METHOD_RPC_MODE_MASTER: + return MultiplayerAPI::RPC_MODE_MASTER; + case GODOT_METHOD_RPC_MODE_PUPPET: + return MultiplayerAPI::RPC_MODE_PUPPET; + case GODOT_METHOD_RPC_MODE_REMOTESYNC: + return MultiplayerAPI::RPC_MODE_REMOTESYNC; + case GODOT_METHOD_RPC_MODE_MASTERSYNC: + return MultiplayerAPI::RPC_MODE_MASTERSYNC; + case GODOT_METHOD_RPC_MODE_PUPPETSYNC: + return MultiplayerAPI::RPC_MODE_PUPPETSYNC; + default: + return MultiplayerAPI::RPC_MODE_DISABLED; + } + } + + script_data = script_data->base_data; + } + + return MultiplayerAPI::RPC_MODE_DISABLED; +} + +Vector<ScriptNetData> NativeScript::get_rset_properties() const { + Vector<ScriptNetData> v; + + NativeScriptDesc *script_data = get_script_desc(); + + while (script_data) { + + for (OrderedHashMap<StringName, NativeScriptDesc::Property>::Element E = script_data->properties.front(); E; E = E.next()) { + if (E.get().rset_mode != GODOT_METHOD_RPC_MODE_DISABLED) { + ScriptNetData nd; + nd.name = E.key(); + nd.mode = MultiplayerAPI::RPCMode(E.get().rset_mode); + v.push_back(nd); + } + } + script_data = script_data->base_data; + } + + return v; +} + +uint16_t NativeScript::get_rset_property_id(const StringName &p_variable) const { + NativeScriptDesc *script_data = get_script_desc(); + + while (script_data) { + + OrderedHashMap<StringName, NativeScriptDesc::Property>::Element E = script_data->properties.find(p_variable); + if (E) { + return E.get().rset_property_id; + } + + script_data = script_data->base_data; + } + + return UINT16_MAX; +} + +StringName NativeScript::get_rset_property(uint16_t p_id) const { + ERR_FAIL_COND_V(p_id == UINT16_MAX, StringName()); + + NativeScriptDesc *script_data = get_script_desc(); + + while (script_data) { + + for (OrderedHashMap<StringName, NativeScriptDesc::Property>::Element E = script_data->properties.front(); E; E = E.next()) { + if (E.get().rset_property_id == p_id) { + return E.key(); + } + } + + script_data = script_data->base_data; + } + + return StringName(); +} + +MultiplayerAPI::RPCMode NativeScript::get_rset_mode_by_id(uint16_t p_id) const { + + ERR_FAIL_COND_V(p_id == UINT16_MAX, MultiplayerAPI::RPC_MODE_DISABLED); + + NativeScriptDesc *script_data = get_script_desc(); + + while (script_data) { + + for (OrderedHashMap<StringName, NativeScriptDesc::Property>::Element E = script_data->properties.front(); E; E = E.next()) { + if (E.get().rset_property_id == p_id) { + switch (E.get().rset_mode) { + case GODOT_METHOD_RPC_MODE_DISABLED: + return MultiplayerAPI::RPC_MODE_DISABLED; + case GODOT_METHOD_RPC_MODE_REMOTE: + return MultiplayerAPI::RPC_MODE_REMOTE; + case GODOT_METHOD_RPC_MODE_MASTER: + return MultiplayerAPI::RPC_MODE_MASTER; + case GODOT_METHOD_RPC_MODE_PUPPET: + return MultiplayerAPI::RPC_MODE_PUPPET; + case GODOT_METHOD_RPC_MODE_REMOTESYNC: + return MultiplayerAPI::RPC_MODE_REMOTESYNC; + case GODOT_METHOD_RPC_MODE_MASTERSYNC: + return MultiplayerAPI::RPC_MODE_MASTERSYNC; + case GODOT_METHOD_RPC_MODE_PUPPETSYNC: + return MultiplayerAPI::RPC_MODE_PUPPETSYNC; + default: + return MultiplayerAPI::RPC_MODE_DISABLED; + } + } + } + + script_data = script_data->base_data; + } + + return MultiplayerAPI::RPC_MODE_DISABLED; +} + +MultiplayerAPI::RPCMode NativeScript::get_rset_mode(const StringName &p_variable) const { + + NativeScriptDesc *script_data = get_script_desc(); + + while (script_data) { + + OrderedHashMap<StringName, NativeScriptDesc::Property>::Element E = script_data->properties.find(p_variable); + if (E) { + switch (E.get().rset_mode) { + case GODOT_METHOD_RPC_MODE_DISABLED: + return MultiplayerAPI::RPC_MODE_DISABLED; + case GODOT_METHOD_RPC_MODE_REMOTE: + return MultiplayerAPI::RPC_MODE_REMOTE; + case GODOT_METHOD_RPC_MODE_MASTER: + return MultiplayerAPI::RPC_MODE_MASTER; + case GODOT_METHOD_RPC_MODE_PUPPET: + return MultiplayerAPI::RPC_MODE_PUPPET; + case GODOT_METHOD_RPC_MODE_REMOTESYNC: + return MultiplayerAPI::RPC_MODE_REMOTESYNC; + case GODOT_METHOD_RPC_MODE_MASTERSYNC: + return MultiplayerAPI::RPC_MODE_MASTERSYNC; + case GODOT_METHOD_RPC_MODE_PUPPETSYNC: + return MultiplayerAPI::RPC_MODE_PUPPETSYNC; + default: + return MultiplayerAPI::RPC_MODE_DISABLED; + } + } + + script_data = script_data->base_data; + } + + return MultiplayerAPI::RPC_MODE_DISABLED; +} + String NativeScript::get_class_documentation() const { NativeScriptDesc *script_data = get_script_desc(); @@ -803,72 +1061,44 @@ Ref<Script> NativeScriptInstance::get_script() const { return script; } -MultiplayerAPI::RPCMode NativeScriptInstance::get_rpc_mode(const StringName &p_method) const { - - NativeScriptDesc *script_data = GET_SCRIPT_DESC(); - - while (script_data) { +Vector<ScriptNetData> NativeScriptInstance::get_rpc_methods() const { + return script->get_rpc_methods(); +} - Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method); - if (E) { - switch (E->get().rpc_mode) { - case GODOT_METHOD_RPC_MODE_DISABLED: - return MultiplayerAPI::RPC_MODE_DISABLED; - case GODOT_METHOD_RPC_MODE_REMOTE: - return MultiplayerAPI::RPC_MODE_REMOTE; - case GODOT_METHOD_RPC_MODE_MASTER: - return MultiplayerAPI::RPC_MODE_MASTER; - case GODOT_METHOD_RPC_MODE_PUPPET: - return MultiplayerAPI::RPC_MODE_PUPPET; - case GODOT_METHOD_RPC_MODE_REMOTESYNC: - return MultiplayerAPI::RPC_MODE_REMOTESYNC; - case GODOT_METHOD_RPC_MODE_MASTERSYNC: - return MultiplayerAPI::RPC_MODE_MASTERSYNC; - case GODOT_METHOD_RPC_MODE_PUPPETSYNC: - return MultiplayerAPI::RPC_MODE_PUPPETSYNC; - default: - return MultiplayerAPI::RPC_MODE_DISABLED; - } - } +uint16_t NativeScriptInstance::get_rpc_method_id(const StringName &p_method) const { + return script->get_rpc_method_id(p_method); +} - script_data = script_data->base_data; - } +StringName NativeScriptInstance::get_rpc_method(uint16_t p_id) const { + return script->get_rpc_method(p_id); +} - return MultiplayerAPI::RPC_MODE_DISABLED; +MultiplayerAPI::RPCMode NativeScriptInstance::get_rpc_mode_by_id(uint16_t p_id) const { + return script->get_rpc_mode_by_id(p_id); } -MultiplayerAPI::RPCMode NativeScriptInstance::get_rset_mode(const StringName &p_variable) const { +MultiplayerAPI::RPCMode NativeScriptInstance::get_rpc_mode(const StringName &p_method) const { + return script->get_rpc_mode(p_method); +} - NativeScriptDesc *script_data = GET_SCRIPT_DESC(); +Vector<ScriptNetData> NativeScriptInstance::get_rset_properties() const { + return script->get_rset_properties(); +} - while (script_data) { +uint16_t NativeScriptInstance::get_rset_property_id(const StringName &p_variable) const { + return script->get_rset_property_id(p_variable); +} - OrderedHashMap<StringName, NativeScriptDesc::Property>::Element E = script_data->properties.find(p_variable); - if (E) { - switch (E.get().rset_mode) { - case GODOT_METHOD_RPC_MODE_DISABLED: - return MultiplayerAPI::RPC_MODE_DISABLED; - case GODOT_METHOD_RPC_MODE_REMOTE: - return MultiplayerAPI::RPC_MODE_REMOTE; - case GODOT_METHOD_RPC_MODE_MASTER: - return MultiplayerAPI::RPC_MODE_MASTER; - case GODOT_METHOD_RPC_MODE_PUPPET: - return MultiplayerAPI::RPC_MODE_PUPPET; - case GODOT_METHOD_RPC_MODE_REMOTESYNC: - return MultiplayerAPI::RPC_MODE_REMOTESYNC; - case GODOT_METHOD_RPC_MODE_MASTERSYNC: - return MultiplayerAPI::RPC_MODE_MASTERSYNC; - case GODOT_METHOD_RPC_MODE_PUPPETSYNC: - return MultiplayerAPI::RPC_MODE_PUPPETSYNC; - default: - return MultiplayerAPI::RPC_MODE_DISABLED; - } - } +StringName NativeScriptInstance::get_rset_property(uint16_t p_id) const { + return script->get_rset_property(p_id); +} - script_data = script_data->base_data; - } +MultiplayerAPI::RPCMode NativeScriptInstance::get_rset_mode_by_id(uint16_t p_id) const { + return script->get_rset_mode_by_id(p_id); +} - return MultiplayerAPI::RPC_MODE_DISABLED; +MultiplayerAPI::RPCMode NativeScriptInstance::get_rset_mode(const StringName &p_variable) const { + return script->get_rset_mode(p_variable); } ScriptLanguage *NativeScriptInstance::get_language() { diff --git a/modules/gdnative/nativescript/nativescript.h b/modules/gdnative/nativescript/nativescript.h index cf787e1f6a..2ff08e32cd 100644 --- a/modules/gdnative/nativescript/nativescript.h +++ b/modules/gdnative/nativescript/nativescript.h @@ -54,6 +54,7 @@ struct NativeScriptDesc { godot_instance_method method; MethodInfo info; int rpc_mode; + uint16_t rpc_method_id; String documentation; }; struct Property { @@ -62,6 +63,7 @@ struct NativeScriptDesc { PropertyInfo info; Variant default_value; int rset_mode; + uint16_t rset_property_id; String documentation; }; @@ -70,7 +72,9 @@ struct NativeScriptDesc { String documentation; }; + uint16_t rpc_count; Map<StringName, Method> methods; + uint16_t rset_count; OrderedHashMap<StringName, Property> properties; Map<StringName, Signal> signals_; // QtCreator doesn't like the name signals StringName base; @@ -86,7 +90,9 @@ struct NativeScriptDesc { bool is_tool; inline NativeScriptDesc() : + rpc_count(0), methods(), + rset_count(0), properties(), signals_(), base(), @@ -174,6 +180,18 @@ public: virtual void get_script_method_list(List<MethodInfo> *p_list) const; virtual void get_script_property_list(List<PropertyInfo> *p_list) const; + virtual Vector<ScriptNetData> get_rpc_methods() const; + virtual uint16_t get_rpc_method_id(const StringName &p_method) const; + virtual StringName get_rpc_method(uint16_t p_id) const; + virtual MultiplayerAPI::RPCMode get_rpc_mode_by_id(uint16_t p_id) const; + virtual MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const; + + virtual Vector<ScriptNetData> get_rset_properties() const; + virtual uint16_t get_rset_property_id(const StringName &p_variable) const; + virtual StringName get_rset_property(uint16_t p_id) const; + virtual MultiplayerAPI::RPCMode get_rset_mode_by_id(uint16_t p_id) const; + virtual MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const; + String get_class_documentation() const; String get_method_documentation(const StringName &p_method) const; String get_signal_documentation(const StringName &p_signal_name) const; @@ -210,8 +228,19 @@ public: virtual void notification(int p_notification); String to_string(bool *r_valid); virtual Ref<Script> get_script() const; + + virtual Vector<ScriptNetData> get_rpc_methods() const; + virtual uint16_t get_rpc_method_id(const StringName &p_method) const; + virtual StringName get_rpc_method(uint16_t p_id) const; + virtual MultiplayerAPI::RPCMode get_rpc_mode_by_id(uint16_t p_id) const; virtual MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const; + + virtual Vector<ScriptNetData> get_rset_properties() const; + virtual uint16_t get_rset_property_id(const StringName &p_variable) const; + virtual StringName get_rset_property(uint16_t p_id) const; + virtual MultiplayerAPI::RPCMode get_rset_mode_by_id(uint16_t p_id) const; virtual MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const; + virtual ScriptLanguage *get_language(); virtual void call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount); diff --git a/modules/gdnative/pluginscript/pluginscript_instance.cpp b/modules/gdnative/pluginscript/pluginscript_instance.cpp index 0d6dac3268..26a1d5f47a 100644 --- a/modules/gdnative/pluginscript/pluginscript_instance.cpp +++ b/modules/gdnative/pluginscript/pluginscript_instance.cpp @@ -93,10 +93,42 @@ void PluginScriptInstance::notification(int p_notification) { _desc->notification(_data, p_notification); } +Vector<ScriptNetData> PluginScriptInstance::get_rpc_methods() const { + return _script->get_rpc_methods(); +} + +uint16_t PluginScriptInstance::get_rpc_method_id(const StringName &p_variable) const { + return _script->get_rpc_method_id(p_variable); +} + +StringName PluginScriptInstance::get_rpc_method(uint16_t p_id) const { + return _script->get_rpc_method(p_id); +} + +MultiplayerAPI::RPCMode PluginScriptInstance::get_rpc_mode_by_id(uint16_t p_id) const { + return _script->get_rpc_mode_by_id(p_id); +} + MultiplayerAPI::RPCMode PluginScriptInstance::get_rpc_mode(const StringName &p_method) const { return _script->get_rpc_mode(p_method); } +Vector<ScriptNetData> PluginScriptInstance::get_rset_properties() const { + return _script->get_rset_properties(); +} + +uint16_t PluginScriptInstance::get_rset_property_id(const StringName &p_variable) const { + return _script->get_rset_property_id(p_variable); +} + +StringName PluginScriptInstance::get_rset_property(uint16_t p_id) const { + return _script->get_rset_property(p_id); +} + +MultiplayerAPI::RPCMode PluginScriptInstance::get_rset_mode_by_id(uint16_t p_id) const { + return _script->get_rset_mode_by_id(p_id); +} + MultiplayerAPI::RPCMode PluginScriptInstance::get_rset_mode(const StringName &p_variable) const { return _script->get_rset_mode(p_variable); } diff --git a/modules/gdnative/pluginscript/pluginscript_instance.h b/modules/gdnative/pluginscript/pluginscript_instance.h index dc1229a44d..154bebd72a 100644 --- a/modules/gdnative/pluginscript/pluginscript_instance.h +++ b/modules/gdnative/pluginscript/pluginscript_instance.h @@ -76,7 +76,16 @@ public: void set_path(const String &p_path); + virtual Vector<ScriptNetData> get_rpc_methods() const; + virtual uint16_t get_rpc_method_id(const StringName &p_method) const; + virtual StringName get_rpc_method(uint16_t p_id) const; + virtual MultiplayerAPI::RPCMode get_rpc_mode_by_id(uint16_t p_id) const; virtual MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const; + + virtual Vector<ScriptNetData> get_rset_properties() const; + virtual uint16_t get_rset_property_id(const StringName &p_variable) const; + virtual StringName get_rset_property(uint16_t p_id) const; + virtual MultiplayerAPI::RPCMode get_rset_mode_by_id(uint16_t p_id) const; virtual MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const; virtual void refcount_incremented(); diff --git a/modules/gdnative/pluginscript/pluginscript_script.cpp b/modules/gdnative/pluginscript/pluginscript_script.cpp index cc5bdee0a1..c370062262 100644 --- a/modules/gdnative/pluginscript/pluginscript_script.cpp +++ b/modules/gdnative/pluginscript/pluginscript_script.cpp @@ -34,6 +34,8 @@ #include "pluginscript_instance.h" #include "pluginscript_script.h" +#include <stdint.h> + #ifdef DEBUG_ENABLED #define __ASSERT_SCRIPT_REASON "Cannot retrieve PluginScript class for this script, is your code correct?" #define ASSERT_SCRIPT_VALID() \ @@ -298,18 +300,31 @@ Error PluginScript::reload(bool p_keep_state) { _member_lines[*key] = (*members)[*key]; } Array *methods = (Array *)&manifest.methods; + _rpc_methods.clear(); + _rpc_variables.clear(); + if (_ref_base_parent.is_valid()) { + _rpc_methods = _ref_base_parent->get_rpc_methods(); + _rpc_variables = _ref_base_parent->get_rset_properties(); + } for (int i = 0; i < methods->size(); ++i) { Dictionary v = (*methods)[i]; MethodInfo mi = MethodInfo::from_dict(v); _methods_info[mi.name] = mi; // rpc_mode is passed as an optional field and is not part of MethodInfo Variant var = v["rpc_mode"]; - if (var == Variant()) { - _methods_rpc_mode[mi.name] = MultiplayerAPI::RPC_MODE_DISABLED; - } else { - _methods_rpc_mode[mi.name] = MultiplayerAPI::RPCMode(int(var)); + if (var != Variant()) { + ScriptNetData nd; + nd.name = mi.name; + nd.mode = MultiplayerAPI::RPCMode(int(var)); + if (_rpc_methods.find(nd) == -1) { + _rpc_methods.push_back(nd); + } } } + + // Sort so we are 100% that they are always the same. + _rpc_methods.sort_custom<SortNetData>(); + Array *signals = (Array *)&manifest.signals; for (int i = 0; i < signals->size(); ++i) { Variant v = (*signals)[i]; @@ -324,13 +339,19 @@ Error PluginScript::reload(bool p_keep_state) { _properties_default_values[pi.name] = v["default_value"]; // rset_mode is passed as an optional field and is not part of PropertyInfo Variant var = v["rset_mode"]; - if (var == Variant()) { - _methods_rpc_mode[pi.name] = MultiplayerAPI::RPC_MODE_DISABLED; - } else { - _methods_rpc_mode[pi.name] = MultiplayerAPI::RPCMode(int(var)); + if (var != Variant()) { + ScriptNetData nd; + nd.name = pi.name; + nd.mode = MultiplayerAPI::RPCMode(int(var)); + if (_rpc_variables.find(nd) == -1) { + _rpc_variables.push_back(nd); + } } } + // Sort so we are 100% that they are always the same. + _rpc_variables.sort_custom<SortNetData>(); + #ifdef TOOLS_ENABLED /*for (Set<PlaceHolderScriptInstance*>::Element *E=placeholders.front();E;E=E->next()) { @@ -455,24 +476,70 @@ int PluginScript::get_member_line(const StringName &p_member) const { return -1; } -MultiplayerAPI::RPCMode PluginScript::get_rpc_mode(const StringName &p_method) const { +Vector<ScriptNetData> PluginScript::get_rpc_methods() const { + return _rpc_methods; +} + +uint16_t PluginScript::get_rpc_method_id(const StringName &p_method) const { + ASSERT_SCRIPT_VALID_V(UINT16_MAX); + for (int i = 0; i < _rpc_methods.size(); i++) { + if (_rpc_methods[i].name == p_method) { + return i; + } + } + return UINT16_MAX; +} + +StringName PluginScript::get_rpc_method(const uint16_t p_rpc_method_id) const { + ASSERT_SCRIPT_VALID_V(StringName()); + if (p_rpc_method_id >= _rpc_methods.size()) + return StringName(); + return _rpc_methods[p_rpc_method_id].name; +} + +MultiplayerAPI::RPCMode PluginScript::get_rpc_mode_by_id(const uint16_t p_rpc_method_id) const { ASSERT_SCRIPT_VALID_V(MultiplayerAPI::RPC_MODE_DISABLED); - const Map<StringName, MultiplayerAPI::RPCMode>::Element *e = _methods_rpc_mode.find(p_method); - if (e != NULL) { - return e->get(); - } else { + if (p_rpc_method_id >= _rpc_methods.size()) return MultiplayerAPI::RPC_MODE_DISABLED; + return _rpc_methods[p_rpc_method_id].mode; +} + +MultiplayerAPI::RPCMode PluginScript::get_rpc_mode(const StringName &p_method) const { + ASSERT_SCRIPT_VALID_V(MultiplayerAPI::RPC_MODE_DISABLED); + return get_rpc_mode_by_id(get_rpc_method_id(p_method)); +} + +Vector<ScriptNetData> PluginScript::get_rset_properties() const { + return _rpc_variables; +} + +uint16_t PluginScript::get_rset_property_id(const StringName &p_property) const { + ASSERT_SCRIPT_VALID_V(UINT16_MAX); + for (int i = 0; i < _rpc_variables.size(); i++) { + if (_rpc_variables[i].name == p_property) { + return i; + } } + return UINT16_MAX; } -MultiplayerAPI::RPCMode PluginScript::get_rset_mode(const StringName &p_variable) const { +StringName PluginScript::get_rset_property(const uint16_t p_rset_property_id) const { + ASSERT_SCRIPT_VALID_V(StringName()); + if (p_rset_property_id >= _rpc_variables.size()) + return StringName(); + return _rpc_variables[p_rset_property_id].name; +} + +MultiplayerAPI::RPCMode PluginScript::get_rset_mode_by_id(const uint16_t p_rset_property_id) const { ASSERT_SCRIPT_VALID_V(MultiplayerAPI::RPC_MODE_DISABLED); - const Map<StringName, MultiplayerAPI::RPCMode>::Element *e = _variables_rset_mode.find(p_variable); - if (e != NULL) { - return e->get(); - } else { + if (p_rset_property_id >= _rpc_variables.size()) return MultiplayerAPI::RPC_MODE_DISABLED; - } + return _rpc_variables[p_rset_property_id].mode; +} + +MultiplayerAPI::RPCMode PluginScript::get_rset_mode(const StringName &p_variable) const { + ASSERT_SCRIPT_VALID_V(MultiplayerAPI::RPC_MODE_DISABLED); + return get_rset_mode_by_id(get_rset_property_id(p_variable)); } PluginScript::PluginScript() : diff --git a/modules/gdnative/pluginscript/pluginscript_script.h b/modules/gdnative/pluginscript/pluginscript_script.h index f67c88c794..f6bca8a9cb 100644 --- a/modules/gdnative/pluginscript/pluginscript_script.h +++ b/modules/gdnative/pluginscript/pluginscript_script.h @@ -60,8 +60,8 @@ private: Map<StringName, PropertyInfo> _properties_info; Map<StringName, MethodInfo> _signals_info; Map<StringName, MethodInfo> _methods_info; - Map<StringName, MultiplayerAPI::RPCMode> _variables_rset_mode; - Map<StringName, MultiplayerAPI::RPCMode> _methods_rpc_mode; + Vector<ScriptNetData> _rpc_methods; + Vector<ScriptNetData> _rpc_variables; Set<Object *> _instances; //exported members @@ -118,8 +118,17 @@ public: virtual int get_member_line(const StringName &p_member) const; - MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const; - MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const; + virtual Vector<ScriptNetData> get_rpc_methods() const; + virtual uint16_t get_rpc_method_id(const StringName &p_method) const; + virtual StringName get_rpc_method(const uint16_t p_rpc_method_id) const; + virtual MultiplayerAPI::RPCMode get_rpc_mode_by_id(const uint16_t p_rpc_method_id) const; + virtual MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const; + + virtual Vector<ScriptNetData> get_rset_properties() const; + virtual uint16_t get_rset_property_id(const StringName &p_property) const; + virtual StringName get_rset_property(const uint16_t p_rset_property_id) const; + virtual MultiplayerAPI::RPCMode get_rset_mode_by_id(const uint16_t p_rpc_method_id) const; + virtual MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const; PluginScript(); void init(PluginScriptLanguage *language); diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index a255b92257..e4da60b5b9 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -30,6 +30,8 @@ #include "gdscript.h" +#include <stdint.h> + #include "core/core_string_names.h" #include "core/engine.h" #include "core/global_constants.h" @@ -610,6 +612,53 @@ Error GDScript::reload(bool p_keep_state) { _set_subclass_path(E->get(), path); } + // Copy the base rpc methods so we don't mask their IDs. + rpc_functions.clear(); + rpc_variables.clear(); + if (base.is_valid()) { + rpc_functions = base->rpc_functions; + rpc_variables = base->rpc_variables; + } + + GDScript *cscript = this; + Map<StringName, Ref<GDScript> >::Element *sub_E = subclasses.front(); + while (cscript) { + // RPC Methods + for (Map<StringName, GDScriptFunction *>::Element *E = cscript->member_functions.front(); E; E = E->next()) { + if (E->get()->get_rpc_mode() != MultiplayerAPI::RPC_MODE_DISABLED) { + ScriptNetData nd; + nd.name = E->key(); + nd.mode = E->get()->get_rpc_mode(); + if (-1 == rpc_functions.find(nd)) { + rpc_functions.push_back(nd); + } + } + } + // RSet + for (Map<StringName, MemberInfo>::Element *E = cscript->member_indices.front(); E; E = E->next()) { + if (E->get().rpc_mode != MultiplayerAPI::RPC_MODE_DISABLED) { + ScriptNetData nd; + nd.name = E->key(); + nd.mode = E->get().rpc_mode; + if (-1 == rpc_variables.find(nd)) { + rpc_variables.push_back(nd); + } + } + } + + if (cscript != this) + sub_E = sub_E->next(); + + if (sub_E) + cscript = sub_E->get().ptr(); + else + cscript = NULL; + } + + // Sort so we are 100% that they are always the same. + rpc_functions.sort_custom<SortNetData>(); + rpc_variables.sort_custom<SortNetData>(); + return OK; } @@ -635,6 +684,60 @@ void GDScript::get_members(Set<StringName> *p_members) { } } +Vector<ScriptNetData> GDScript::get_rpc_methods() const { + return rpc_functions; +} + +uint16_t GDScript::get_rpc_method_id(const StringName &p_method) const { + for (int i = 0; i < rpc_functions.size(); i++) { + if (rpc_functions[i].name == p_method) { + return i; + } + } + return UINT16_MAX; +} + +StringName GDScript::get_rpc_method(const uint16_t p_rpc_method_id) const { + ERR_FAIL_COND_V(p_rpc_method_id >= rpc_functions.size(), StringName()); + return rpc_functions[p_rpc_method_id].name; +} + +MultiplayerAPI::RPCMode GDScript::get_rpc_mode_by_id(const uint16_t p_rpc_method_id) const { + ERR_FAIL_COND_V(p_rpc_method_id >= rpc_functions.size(), MultiplayerAPI::RPC_MODE_DISABLED); + return rpc_functions[p_rpc_method_id].mode; +} + +MultiplayerAPI::RPCMode GDScript::get_rpc_mode(const StringName &p_method) const { + return get_rpc_mode_by_id(get_rpc_method_id(p_method)); +} + +Vector<ScriptNetData> GDScript::get_rset_properties() const { + return rpc_variables; +} + +uint16_t GDScript::get_rset_property_id(const StringName &p_variable) const { + for (int i = 0; i < rpc_variables.size(); i++) { + if (rpc_variables[i].name == p_variable) { + return i; + } + } + return UINT16_MAX; +} + +StringName GDScript::get_rset_property(const uint16_t p_rset_member_id) const { + ERR_FAIL_COND_V(p_rset_member_id >= rpc_variables.size(), StringName()); + return rpc_variables[p_rset_member_id].name; +} + +MultiplayerAPI::RPCMode GDScript::get_rset_mode_by_id(const uint16_t p_rset_member_id) const { + ERR_FAIL_COND_V(p_rset_member_id >= rpc_functions.size(), MultiplayerAPI::RPC_MODE_DISABLED); + return rpc_functions[p_rset_member_id].mode; +} + +MultiplayerAPI::RPCMode GDScript::get_rset_mode(const StringName &p_variable) const { + return get_rset_mode_by_id(get_rset_property_id(p_variable)); +} + Variant GDScript::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) { GDScript *top = this; @@ -1291,40 +1394,44 @@ ScriptLanguage *GDScriptInstance::get_language() { return GDScriptLanguage::get_singleton(); } -MultiplayerAPI::RPCMode GDScriptInstance::get_rpc_mode(const StringName &p_method) const { +Vector<ScriptNetData> GDScriptInstance::get_rpc_methods() const { + return script->get_rpc_methods(); +} - const GDScript *cscript = script.ptr(); +uint16_t GDScriptInstance::get_rpc_method_id(const StringName &p_method) const { + return script->get_rpc_method_id(p_method); +} - while (cscript) { - const Map<StringName, GDScriptFunction *>::Element *E = cscript->member_functions.find(p_method); - if (E) { +StringName GDScriptInstance::get_rpc_method(const uint16_t p_rpc_method_id) const { + return script->get_rpc_method(p_rpc_method_id); +} - if (E->get()->get_rpc_mode() != MultiplayerAPI::RPC_MODE_DISABLED) { - return E->get()->get_rpc_mode(); - } - } - cscript = cscript->_base; - } +MultiplayerAPI::RPCMode GDScriptInstance::get_rpc_mode_by_id(const uint16_t p_rpc_method_id) const { + return script->get_rpc_mode_by_id(p_rpc_method_id); +} - return MultiplayerAPI::RPC_MODE_DISABLED; +MultiplayerAPI::RPCMode GDScriptInstance::get_rpc_mode(const StringName &p_method) const { + return script->get_rpc_mode(p_method); } -MultiplayerAPI::RPCMode GDScriptInstance::get_rset_mode(const StringName &p_variable) const { +Vector<ScriptNetData> GDScriptInstance::get_rset_properties() const { + return script->get_rset_properties(); +} - const GDScript *cscript = script.ptr(); +uint16_t GDScriptInstance::get_rset_property_id(const StringName &p_variable) const { + return script->get_rset_property_id(p_variable); +} - while (cscript) { - const Map<StringName, GDScript::MemberInfo>::Element *E = cscript->member_indices.find(p_variable); - if (E) { +StringName GDScriptInstance::get_rset_property(const uint16_t p_rset_member_id) const { + return script->get_rset_property(p_rset_member_id); +} - if (E->get().rpc_mode) { - return E->get().rpc_mode; - } - } - cscript = cscript->_base; - } +MultiplayerAPI::RPCMode GDScriptInstance::get_rset_mode_by_id(const uint16_t p_rset_member_id) const { + return script->get_rset_mode_by_id(p_rset_member_id); +} - return MultiplayerAPI::RPC_MODE_DISABLED; +MultiplayerAPI::RPCMode GDScriptInstance::get_rset_mode(const StringName &p_variable) const { + return script->get_rset_mode(p_variable); } void GDScriptInstance::reload_members() { diff --git a/modules/gdscript/gdscript.h b/modules/gdscript/gdscript.h index 4ae52238ce..4af574cd9d 100644 --- a/modules/gdscript/gdscript.h +++ b/modules/gdscript/gdscript.h @@ -85,6 +85,8 @@ class GDScript : public Script { Map<StringName, MemberInfo> member_indices; //members are just indices to the instanced script. Map<StringName, Ref<GDScript> > subclasses; Map<StringName, Vector<StringName> > _signals; + Vector<ScriptNetData> rpc_functions; + Vector<ScriptNetData> rpc_variables; #ifdef TOOLS_ENABLED @@ -213,6 +215,18 @@ public: virtual void get_constants(Map<StringName, Variant> *p_constants); virtual void get_members(Set<StringName> *p_members); + virtual Vector<ScriptNetData> get_rpc_methods() const; + virtual uint16_t get_rpc_method_id(const StringName &p_method) const; + virtual StringName get_rpc_method(const uint16_t p_rpc_method_id) const; + virtual MultiplayerAPI::RPCMode get_rpc_mode_by_id(const uint16_t p_rpc_method_id) const; + virtual MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const; + + virtual Vector<ScriptNetData> get_rset_properties() const; + virtual uint16_t get_rset_property_id(const StringName &p_variable) const; + virtual StringName get_rset_property(const uint16_t p_variable_id) const; + virtual MultiplayerAPI::RPCMode get_rset_mode_by_id(const uint16_t p_variable_id) const; + virtual MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const; + #ifdef TOOLS_ENABLED virtual bool is_placeholder_fallback_enabled() const { return placeholder_fallback_enabled; } #endif @@ -264,7 +278,16 @@ public: void reload_members(); + virtual Vector<ScriptNetData> get_rpc_methods() const; + virtual uint16_t get_rpc_method_id(const StringName &p_method) const; + virtual StringName get_rpc_method(const uint16_t p_rpc_method_id) const; + virtual MultiplayerAPI::RPCMode get_rpc_mode_by_id(const uint16_t p_rpc_method_id) const; virtual MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const; + + virtual Vector<ScriptNetData> get_rset_properties() const; + virtual uint16_t get_rset_property_id(const StringName &p_variable) const; + virtual StringName get_rset_property(const uint16_t p_variable_id) const; + virtual MultiplayerAPI::RPCMode get_rset_mode_by_id(const uint16_t p_variable_id) const; virtual MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const; GDScriptInstance(); diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp index 2847f3b414..93b23791ae 100644 --- a/modules/mono/csharp_script.cpp +++ b/modules/mono/csharp_script.cpp @@ -31,6 +31,7 @@ #include "csharp_script.h" #include <mono/metadata/threads.h> +#include <stdint.h> #include "core/io/json.h" #include "core/os/file_access.h" @@ -1979,67 +1980,44 @@ bool CSharpInstance::refcount_decremented() { return ref_dying; } -MultiplayerAPI::RPCMode CSharpInstance::_member_get_rpc_mode(IMonoClassMember *p_member) const { - - if (p_member->has_attribute(CACHED_CLASS(RemoteAttribute))) - return MultiplayerAPI::RPC_MODE_REMOTE; - if (p_member->has_attribute(CACHED_CLASS(MasterAttribute))) - return MultiplayerAPI::RPC_MODE_MASTER; - if (p_member->has_attribute(CACHED_CLASS(PuppetAttribute))) - return MultiplayerAPI::RPC_MODE_PUPPET; - if (p_member->has_attribute(CACHED_CLASS(SlaveAttribute))) - return MultiplayerAPI::RPC_MODE_PUPPET; - if (p_member->has_attribute(CACHED_CLASS(RemoteSyncAttribute))) - return MultiplayerAPI::RPC_MODE_REMOTESYNC; - if (p_member->has_attribute(CACHED_CLASS(SyncAttribute))) - return MultiplayerAPI::RPC_MODE_REMOTESYNC; - if (p_member->has_attribute(CACHED_CLASS(MasterSyncAttribute))) - return MultiplayerAPI::RPC_MODE_MASTERSYNC; - if (p_member->has_attribute(CACHED_CLASS(PuppetSyncAttribute))) - return MultiplayerAPI::RPC_MODE_PUPPETSYNC; +Vector<ScriptNetData> CSharpInstance::get_rpc_methods() const { + return script->get_rpc_methods(); +} - return MultiplayerAPI::RPC_MODE_DISABLED; +uint16_t CSharpInstance::get_rpc_method_id(const StringName &p_method) const { + return script->get_rpc_method_id(p_method); } -MultiplayerAPI::RPCMode CSharpInstance::get_rpc_mode(const StringName &p_method) const { +StringName CSharpInstance::get_rpc_method(const uint16_t p_rpc_method_id) const { + return script->get_rpc_method(p_rpc_method_id); +} - GD_MONO_SCOPE_THREAD_ATTACH; +MultiplayerAPI::RPCMode CSharpInstance::get_rpc_mode_by_id(const uint16_t p_rpc_method_id) const { + return script->get_rpc_mode_by_id(p_rpc_method_id); +} - GDMonoClass *top = script->script_class; +MultiplayerAPI::RPCMode CSharpInstance::get_rpc_mode(const StringName &p_method) const { + return script->get_rpc_mode(p_method); +} - while (top && top != script->native) { - GDMonoMethod *method = top->get_fetched_method_unknown_params(p_method); +Vector<ScriptNetData> CSharpInstance::get_rset_properties() const { + return script->get_rset_properties(); +} - if (method && !method->is_static()) - return _member_get_rpc_mode(method); +uint16_t CSharpInstance::get_rset_property_id(const StringName &p_variable) const { + return script->get_rset_property_id(p_variable); +} - top = top->get_parent_class(); - } +StringName CSharpInstance::get_rset_property(const uint16_t p_rset_member_id) const { + return script->get_rset_property(p_rset_member_id); +} - return MultiplayerAPI::RPC_MODE_DISABLED; +MultiplayerAPI::RPCMode CSharpInstance::get_rset_mode_by_id(const uint16_t p_rset_member_id) const { + return script->get_rset_mode_by_id(p_rset_member_id); } MultiplayerAPI::RPCMode CSharpInstance::get_rset_mode(const StringName &p_variable) const { - - GD_MONO_SCOPE_THREAD_ATTACH; - - GDMonoClass *top = script->script_class; - - while (top && top != script->native) { - GDMonoField *field = top->get_field(p_variable); - - if (field && !field->is_static()) - return _member_get_rpc_mode(field); - - GDMonoProperty *property = top->get_property(p_variable); - - if (property && !property->is_static()) - return _member_get_rpc_mode(property); - - top = top->get_parent_class(); - } - - return MultiplayerAPI::RPC_MODE_DISABLED; + return script->get_rset_mode(p_variable); } void CSharpInstance::notification(int p_notification) { @@ -3251,6 +3229,69 @@ Error CSharpScript::reload(bool p_keep_state) { _update_exports(); } + rpc_functions.clear(); + rpc_variables.clear(); + + GDMonoClass *top = script_class; + while (top && top != native) { + { + Vector<GDMonoMethod *> methods = top->get_all_methods(); + for (int i = 0; i < methods.size(); i++) { + if (!methods[i]->is_static()) { + MultiplayerAPI::RPCMode mode = _member_get_rpc_mode(methods[i]); + if (MultiplayerAPI::RPC_MODE_DISABLED != mode) { + ScriptNetData nd; + nd.name = methods[i]->get_name(); + nd.mode = mode; + if (-1 == rpc_functions.find(nd)) { + rpc_functions.push_back(nd); + } + } + } + } + } + + { + Vector<GDMonoField *> fields = top->get_all_fields(); + for (int i = 0; i < fields.size(); i++) { + if (!fields[i]->is_static()) { + MultiplayerAPI::RPCMode mode = _member_get_rpc_mode(fields[i]); + if (MultiplayerAPI::RPC_MODE_DISABLED != mode) { + ScriptNetData nd; + nd.name = fields[i]->get_name(); + nd.mode = mode; + if (-1 == rpc_variables.find(nd)) { + rpc_variables.push_back(nd); + } + } + } + } + } + + { + Vector<GDMonoProperty *> properties = top->get_all_properties(); + for (int i = 0; i < properties.size(); i++) { + if (!properties[i]->is_static()) { + MultiplayerAPI::RPCMode mode = _member_get_rpc_mode(properties[i]); + if (MultiplayerAPI::RPC_MODE_DISABLED != mode) { + ScriptNetData nd; + nd.name = properties[i]->get_name(); + nd.mode = mode; + if (-1 == rpc_variables.find(nd)) { + rpc_variables.push_back(nd); + } + } + } + } + } + + top = top->get_parent_class(); + } + + // Sort so we are 100% that they are always the same. + rpc_functions.sort_custom<SortNetData>(); + rpc_variables.sort_custom<SortNetData>(); + return OK; } @@ -3324,6 +3365,82 @@ int CSharpScript::get_member_line(const StringName &p_member) const { return -1; } +MultiplayerAPI::RPCMode CSharpScript::_member_get_rpc_mode(IMonoClassMember *p_member) const { + + if (p_member->has_attribute(CACHED_CLASS(RemoteAttribute))) + return MultiplayerAPI::RPC_MODE_REMOTE; + if (p_member->has_attribute(CACHED_CLASS(MasterAttribute))) + return MultiplayerAPI::RPC_MODE_MASTER; + if (p_member->has_attribute(CACHED_CLASS(PuppetAttribute))) + return MultiplayerAPI::RPC_MODE_PUPPET; + if (p_member->has_attribute(CACHED_CLASS(SlaveAttribute))) + return MultiplayerAPI::RPC_MODE_PUPPET; + if (p_member->has_attribute(CACHED_CLASS(RemoteSyncAttribute))) + return MultiplayerAPI::RPC_MODE_REMOTESYNC; + if (p_member->has_attribute(CACHED_CLASS(SyncAttribute))) + return MultiplayerAPI::RPC_MODE_REMOTESYNC; + if (p_member->has_attribute(CACHED_CLASS(MasterSyncAttribute))) + return MultiplayerAPI::RPC_MODE_MASTERSYNC; + if (p_member->has_attribute(CACHED_CLASS(PuppetSyncAttribute))) + return MultiplayerAPI::RPC_MODE_PUPPETSYNC; + + return MultiplayerAPI::RPC_MODE_DISABLED; +} + +Vector<ScriptNetData> CSharpScript::get_rpc_methods() const { + return rpc_functions; +} + +uint16_t CSharpScript::get_rpc_method_id(const StringName &p_method) const { + for (int i = 0; i < rpc_functions.size(); i++) { + if (rpc_functions[i].name == p_method) { + return i; + } + } + return UINT16_MAX; +} + +StringName CSharpScript::get_rpc_method(const uint16_t p_rpc_method_id) const { + ERR_FAIL_COND_V(p_rpc_method_id >= rpc_functions.size(), StringName()); + return rpc_functions[p_rpc_method_id].name; +} + +MultiplayerAPI::RPCMode CSharpScript::get_rpc_mode_by_id(const uint16_t p_rpc_method_id) const { + ERR_FAIL_COND_V(p_rpc_method_id >= rpc_functions.size(), MultiplayerAPI::RPC_MODE_DISABLED); + return rpc_functions[p_rpc_method_id].mode; +} + +MultiplayerAPI::RPCMode CSharpScript::get_rpc_mode(const StringName &p_method) const { + return get_rpc_mode_by_id(get_rpc_method_id(p_method)); +} + +Vector<ScriptNetData> CSharpScript::get_rset_properties() const { + return rpc_variables; +} + +uint16_t CSharpScript::get_rset_property_id(const StringName &p_variable) const { + for (int i = 0; i < rpc_variables.size(); i++) { + if (rpc_variables[i].name == p_variable) { + return i; + } + } + return UINT16_MAX; +} + +StringName CSharpScript::get_rset_property(const uint16_t p_rset_member_id) const { + ERR_FAIL_COND_V(p_rset_member_id >= rpc_variables.size(), StringName()); + return rpc_variables[p_rset_member_id].name; +} + +MultiplayerAPI::RPCMode CSharpScript::get_rset_mode_by_id(const uint16_t p_rset_member_id) const { + ERR_FAIL_COND_V(p_rset_member_id >= rpc_functions.size(), MultiplayerAPI::RPC_MODE_DISABLED); + return rpc_functions[p_rset_member_id].mode; +} + +MultiplayerAPI::RPCMode CSharpScript::get_rset_mode(const StringName &p_variable) const { + return get_rset_mode_by_id(get_rset_property_id(p_variable)); +} + Error CSharpScript::load_source_code(const String &p_path) { Error ferr = read_all_file_utf8(p_path, source); diff --git a/modules/mono/csharp_script.h b/modules/mono/csharp_script.h index f244bc4119..32a5b30c18 100644 --- a/modules/mono/csharp_script.h +++ b/modules/mono/csharp_script.h @@ -113,6 +113,9 @@ class CSharpScript : public Script { Map<StringName, Vector<Argument> > _signals; bool signals_invalidated; + Vector<ScriptNetData> rpc_functions; + Vector<ScriptNetData> rpc_variables; + #ifdef TOOLS_ENABLED List<PropertyInfo> exported_members_cache; // members_cache Map<StringName, Variant> exported_members_defval_cache; // member_default_values_cache @@ -146,6 +149,8 @@ class CSharpScript : public Script { static Ref<CSharpScript> create_for_managed_type(GDMonoClass *p_class, GDMonoClass *p_native); static void initialize_for_managed_type(Ref<CSharpScript> p_script, GDMonoClass *p_class, GDMonoClass *p_native); + MultiplayerAPI::RPCMode _member_get_rpc_mode(IMonoClassMember *p_member) const; + protected: static void _bind_methods(); @@ -187,6 +192,18 @@ public: virtual int get_member_line(const StringName &p_member) const; + virtual Vector<ScriptNetData> get_rpc_methods() const; + virtual uint16_t get_rpc_method_id(const StringName &p_method) const; + virtual StringName get_rpc_method(const uint16_t p_rpc_method_id) const; + virtual MultiplayerAPI::RPCMode get_rpc_mode_by_id(const uint16_t p_rpc_method_id) const; + virtual MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const; + + virtual Vector<ScriptNetData> get_rset_properties() const; + virtual uint16_t get_rset_property_id(const StringName &p_variable) const; + virtual StringName get_rset_property(const uint16_t p_variable_id) const; + virtual MultiplayerAPI::RPCMode get_rset_mode_by_id(const uint16_t p_variable_id) const; + virtual MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const; + #ifdef TOOLS_ENABLED virtual bool is_placeholder_fallback_enabled() const { return placeholder_fallback_enabled; } #endif @@ -232,8 +249,6 @@ class CSharpInstance : public ScriptInstance { void _call_multilevel(MonoObject *p_mono_object, const StringName &p_method, const Variant **p_args, int p_argcount); - MultiplayerAPI::RPCMode _member_get_rpc_mode(IMonoClassMember *p_member) const; - void get_properties_state_for_reloading(List<Pair<StringName, Variant> > &r_state); public: @@ -265,7 +280,16 @@ public: virtual void refcount_incremented(); virtual bool refcount_decremented(); + virtual Vector<ScriptNetData> get_rpc_methods() const; + virtual uint16_t get_rpc_method_id(const StringName &p_method) const; + virtual StringName get_rpc_method(const uint16_t p_rpc_method_id) const; + virtual MultiplayerAPI::RPCMode get_rpc_mode_by_id(const uint16_t p_rpc_method_id) const; virtual MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const; + + virtual Vector<ScriptNetData> get_rset_properties() const; + virtual uint16_t get_rset_property_id(const StringName &p_variable) const; + virtual StringName get_rset_property(const uint16_t p_variable_id) const; + virtual MultiplayerAPI::RPCMode get_rset_mode_by_id(const uint16_t p_variable_id) const; virtual MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const; virtual void notification(int p_notification); diff --git a/modules/visual_script/visual_script.cpp b/modules/visual_script/visual_script.cpp index c591e3b5c2..be7443adfa 100644 --- a/modules/visual_script/visual_script.cpp +++ b/modules/visual_script/visual_script.cpp @@ -30,6 +30,8 @@ #include "visual_script.h" +#include <stdint.h> + #include "core/core_string_names.h" #include "core/os/os.h" #include "core/project_settings.h" @@ -1102,6 +1104,60 @@ bool VisualScript::are_subnodes_edited() const { } #endif +Vector<ScriptNetData> VisualScript::get_rpc_methods() const { + return rpc_functions; +} + +uint16_t VisualScript::get_rpc_method_id(const StringName &p_method) const { + for (int i = 0; i < rpc_functions.size(); i++) { + if (rpc_functions[i].name == p_method) { + return i; + } + } + return UINT16_MAX; +} + +StringName VisualScript::get_rpc_method(const uint16_t p_rpc_method_id) const { + ERR_FAIL_COND_V(p_rpc_method_id >= rpc_functions.size(), StringName()); + return rpc_functions[p_rpc_method_id].name; +} + +MultiplayerAPI::RPCMode VisualScript::get_rpc_mode_by_id(const uint16_t p_rpc_method_id) const { + ERR_FAIL_COND_V(p_rpc_method_id >= rpc_functions.size(), MultiplayerAPI::RPC_MODE_DISABLED); + return rpc_functions[p_rpc_method_id].mode; +} + +MultiplayerAPI::RPCMode VisualScript::get_rpc_mode(const StringName &p_method) const { + return get_rpc_mode_by_id(get_rpc_method_id(p_method)); +} + +Vector<ScriptNetData> VisualScript::get_rset_properties() const { + return rpc_variables; +} + +uint16_t VisualScript::get_rset_property_id(const StringName &p_variable) const { + for (int i = 0; i < rpc_variables.size(); i++) { + if (rpc_variables[i].name == p_variable) { + return i; + } + } + return UINT16_MAX; +} + +StringName VisualScript::get_rset_property(const uint16_t p_rset_property_id) const { + ERR_FAIL_COND_V(p_rset_property_id >= rpc_variables.size(), StringName()); + return rpc_variables[p_rset_property_id].name; +} + +MultiplayerAPI::RPCMode VisualScript::get_rset_mode_by_id(const uint16_t p_rset_variable_id) const { + ERR_FAIL_COND_V(p_rset_variable_id >= rpc_functions.size(), MultiplayerAPI::RPC_MODE_DISABLED); + return rpc_functions[p_rset_variable_id].mode; +} + +MultiplayerAPI::RPCMode VisualScript::get_rset_mode(const StringName &p_variable) const { + return get_rset_mode_by_id(get_rset_property_id(p_variable)); +} + void VisualScript::_set_data(const Dictionary &p_data) { Dictionary d = p_data; @@ -1206,6 +1262,30 @@ void VisualScript::_set_data(const Dictionary &p_data) { is_tool_script = d["is_tool_script"]; else is_tool_script = false; + + // Takes all the rpc methods + rpc_functions.clear(); + rpc_variables.clear(); + for (Map<StringName, Function>::Element *E = functions.front(); E; E = E->next()) { + if (E->get().function_id >= 0 && E->get().nodes.find(E->get().function_id)) { + Ref<VisualScriptFunction> vsf = E->get().nodes[E->get().function_id].node; + if (vsf.is_valid()) { + if (vsf->get_rpc_mode() != MultiplayerAPI::RPC_MODE_DISABLED) { + ScriptNetData nd; + nd.name = E->key(); + nd.mode = vsf->get_rpc_mode(); + if (rpc_functions.find(nd) == -1) { + rpc_functions.push_back(nd); + } + } + } + } + } + + // Visual script doesn't have rset :( + + // Sort so we are 100% that they are always the same. + rpc_functions.sort_custom<SortNetData>(); } Dictionary VisualScript::_get_data() const { @@ -2043,31 +2123,44 @@ Ref<Script> VisualScriptInstance::get_script() const { return script; } -MultiplayerAPI::RPCMode VisualScriptInstance::get_rpc_mode(const StringName &p_method) const { +Vector<ScriptNetData> VisualScriptInstance::get_rpc_methods() const { + return script->get_rpc_methods(); +} - if (p_method == script->get_default_func()) - return MultiplayerAPI::RPC_MODE_DISABLED; +uint16_t VisualScriptInstance::get_rpc_method_id(const StringName &p_method) const { + return script->get_rpc_method_id(p_method); +} - const Map<StringName, VisualScript::Function>::Element *E = script->functions.find(p_method); - if (!E) { - return MultiplayerAPI::RPC_MODE_DISABLED; - } +StringName VisualScriptInstance::get_rpc_method(const uint16_t p_rpc_method_id) const { + return script->get_rpc_method(p_rpc_method_id); +} - if (E->get().function_id >= 0 && E->get().nodes.has(E->get().function_id)) { +MultiplayerAPI::RPCMode VisualScriptInstance::get_rpc_mode_by_id(const uint16_t p_rpc_method_id) const { + return script->get_rpc_mode_by_id(p_rpc_method_id); +} - Ref<VisualScriptFunction> vsf = E->get().nodes[E->get().function_id].node; - if (vsf.is_valid()) { +MultiplayerAPI::RPCMode VisualScriptInstance::get_rpc_mode(const StringName &p_method) const { + return script->get_rpc_mode(p_method); +} - return vsf->get_rpc_mode(); - } - } +Vector<ScriptNetData> VisualScriptInstance::get_rset_properties() const { + return script->get_rset_properties(); +} - return MultiplayerAPI::RPC_MODE_DISABLED; +uint16_t VisualScriptInstance::get_rset_property_id(const StringName &p_variable) const { + return script->get_rset_property_id(p_variable); } -MultiplayerAPI::RPCMode VisualScriptInstance::get_rset_mode(const StringName &p_variable) const { +StringName VisualScriptInstance::get_rset_property(const uint16_t p_rset_property_id) const { + return script->get_rset_property(p_rset_property_id); +} - return MultiplayerAPI::RPC_MODE_DISABLED; +MultiplayerAPI::RPCMode VisualScriptInstance::get_rset_mode_by_id(const uint16_t p_rset_variable_id) const { + return script->get_rset_mode_by_id(p_rset_variable_id); +} + +MultiplayerAPI::RPCMode VisualScriptInstance::get_rset_mode(const StringName &p_variable) const { + return script->get_rset_mode(p_variable); } void VisualScriptInstance::create(const Ref<VisualScript> &p_script, Object *p_owner) { diff --git a/modules/visual_script/visual_script.h b/modules/visual_script/visual_script.h index 9305226dc6..d3569bb040 100644 --- a/modules/visual_script/visual_script.h +++ b/modules/visual_script/visual_script.h @@ -245,6 +245,8 @@ private: Map<StringName, Function> functions; Map<StringName, Variable> variables; Map<StringName, Vector<Argument> > custom_signals; + Vector<ScriptNetData> rpc_functions; + Vector<ScriptNetData> rpc_variables; Map<Object *, VisualScriptInstance *> instances; @@ -362,6 +364,18 @@ public: virtual int get_member_line(const StringName &p_member) const; + virtual Vector<ScriptNetData> get_rpc_methods() const; + virtual uint16_t get_rpc_method_id(const StringName &p_method) const; + virtual StringName get_rpc_method(const uint16_t p_rpc_method_id) const; + virtual MultiplayerAPI::RPCMode get_rpc_mode_by_id(const uint16_t p_rpc_method_id) const; + virtual MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const; + + virtual Vector<ScriptNetData> get_rset_properties() const; + virtual uint16_t get_rset_property_id(const StringName &p_property) const; + virtual StringName get_rset_property(const uint16_t p_rset_property_id) const; + virtual MultiplayerAPI::RPCMode get_rset_mode_by_id(const uint16_t p_rpc_method_id) const; + virtual MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const; + #ifdef TOOLS_ENABLED virtual bool are_subnodes_edited() const; #endif @@ -441,7 +455,16 @@ public: virtual ScriptLanguage *get_language(); + virtual Vector<ScriptNetData> get_rpc_methods() const; + virtual uint16_t get_rpc_method_id(const StringName &p_method) const; + virtual StringName get_rpc_method(const uint16_t p_rpc_method_id) const; + virtual MultiplayerAPI::RPCMode get_rpc_mode_by_id(const uint16_t p_rpc_method_id) const; virtual MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const; + + virtual Vector<ScriptNetData> get_rset_properties() const; + virtual uint16_t get_rset_property_id(const StringName &p_property) const; + virtual StringName get_rset_property(const uint16_t p_rset_property_id) const; + virtual MultiplayerAPI::RPCMode get_rset_mode_by_id(const uint16_t p_rpc_method_id) const; virtual MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const; VisualScriptInstance(); |