diff options
Diffstat (limited to 'modules/gdnative/nativescript')
-rw-r--r-- | modules/gdnative/nativescript/SCsub | 8 | ||||
-rw-r--r-- | modules/gdnative/nativescript/api_generator.cpp | 3 | ||||
-rw-r--r-- | modules/gdnative/nativescript/godot_nativescript.cpp | 14 | ||||
-rw-r--r-- | modules/gdnative/nativescript/nativescript.cpp | 255 | ||||
-rw-r--r-- | modules/gdnative/nativescript/nativescript.h | 73 | ||||
-rw-r--r-- | modules/gdnative/nativescript/register_types.cpp | 4 |
6 files changed, 304 insertions, 53 deletions
diff --git a/modules/gdnative/nativescript/SCsub b/modules/gdnative/nativescript/SCsub index ee3b9c351d..5841ad5531 100644 --- a/modules/gdnative/nativescript/SCsub +++ b/modules/gdnative/nativescript/SCsub @@ -1,12 +1,10 @@ #!/usr/bin/env python Import('env') +Import('env_gdnative') -mod_env = env.Clone() -mod_env.add_source_files(env.modules_sources, "*.cpp") -mod_env.Append(CPPFLAGS=['-DGDAPI_BUILT_IN']) +env_gdnative.add_source_files(env.modules_sources, '*.cpp') +env_gdnative.Append(CPPFLAGS=['-DGDAPI_BUILT_IN']) if "platform" in env and env["platform"] in ["x11", "iphone"]: env.Append(LINKFLAGS=["-rdynamic"]) - -Export('mod_env') diff --git a/modules/gdnative/nativescript/api_generator.cpp b/modules/gdnative/nativescript/api_generator.cpp index 70ca8d68b8..8c6dace847 100644 --- a/modules/gdnative/nativescript/api_generator.cpp +++ b/modules/gdnative/nativescript/api_generator.cpp @@ -35,8 +35,8 @@ #include "core/class_db.h" #include "core/engine.h" #include "core/global_constants.h" +#include "core/os/file_access.h" #include "core/pair.h" -#include "os/file_access.h" // helper stuff @@ -292,6 +292,7 @@ List<ClassAPI> generate_c_api_classes() { method_api.has_varargs = method_bind && method_bind->is_vararg(); // Method flags + method_api.is_virtual = false; if (method_info.flags) { const uint32_t flags = method_info.flags; method_api.is_editor = flags & METHOD_FLAG_EDITOR; diff --git a/modules/gdnative/nativescript/godot_nativescript.cpp b/modules/gdnative/nativescript/godot_nativescript.cpp index ace2ecac5c..c39efe126f 100644 --- a/modules/gdnative/nativescript/godot_nativescript.cpp +++ b/modules/gdnative/nativescript/godot_nativescript.cpp @@ -30,12 +30,12 @@ #include "nativescript/godot_nativescript.h" -#include "class_db.h" -#include "error_macros.h" +#include "core/class_db.h" +#include "core/error_macros.h" +#include "core/global_constants.h" +#include "core/project_settings.h" +#include "core/variant.h" #include "gdnative/gdnative.h" -#include "global_constants.h" -#include "project_settings.h" -#include "variant.h" #include "nativescript.h" @@ -365,6 +365,10 @@ void GDAPI *godot_nativescript_get_instance_binding_data(int p_idx, godot_object return NativeScriptLanguage::get_singleton()->get_instance_binding_data(p_idx, (Object *)p_object); } +void GDAPI godot_nativescript_profiling_add_data(const char *p_signature, uint64_t p_time) { + NativeScriptLanguage::get_singleton()->profiling_add_data(StringName(p_signature), p_time); +} + #ifdef __cplusplus } #endif diff --git a/modules/gdnative/nativescript/nativescript.cpp b/modules/gdnative/nativescript/nativescript.cpp index 23747af86b..bcaf3f346e 100644 --- a/modules/gdnative/nativescript/nativescript.cpp +++ b/modules/gdnative/nativescript/nativescript.cpp @@ -33,10 +33,10 @@ #include "gdnative/gdnative.h" #include "core/global_constants.h" +#include "core/io/file_access_encrypted.h" +#include "core/os/file_access.h" +#include "core/os/os.h" #include "core/project_settings.h" -#include "io/file_access_encrypted.h" -#include "os/file_access.h" -#include "os/os.h" #include "scene/main/scene_tree.h" #include "scene/resources/scene_format_text.h" @@ -44,7 +44,7 @@ #include <stdlib.h> #ifndef NO_THREADS -#include "os/thread.h" +#include "core/os/thread.h" #endif #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED) @@ -62,13 +62,21 @@ void NativeScript::_bind_methods() { ClassDB::bind_method(D_METHOD("set_library", "library"), &NativeScript::set_library); ClassDB::bind_method(D_METHOD("get_library"), &NativeScript::get_library); + ClassDB::bind_method(D_METHOD("set_script_class_name", "class_name"), &NativeScript::set_script_class_name); + ClassDB::bind_method(D_METHOD("get_script_class_name"), &NativeScript::get_script_class_name); + ClassDB::bind_method(D_METHOD("set_script_class_icon_path", "icon_path"), &NativeScript::set_script_class_icon_path); + ClassDB::bind_method(D_METHOD("get_script_class_icon_path"), &NativeScript::get_script_class_icon_path); + ClassDB::bind_method(D_METHOD("get_class_documentation"), &NativeScript::get_class_documentation); ClassDB::bind_method(D_METHOD("get_method_documentation", "method"), &NativeScript::get_method_documentation); ClassDB::bind_method(D_METHOD("get_signal_documentation", "signal_name"), &NativeScript::get_signal_documentation); ClassDB::bind_method(D_METHOD("get_property_documentation", "path"), &NativeScript::get_property_documentation); - ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "class_name"), "set_class_name", "get_class_name"); - ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "GDNativeLibrary"), "set_library", "get_library"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "class_name"), "set_class_name", "get_class_name"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "GDNativeLibrary"), "set_library", "get_library"); + ADD_GROUP("Script Class", "script_class_"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "script_class_name"), "set_script_class_name", "get_script_class_name"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "script_class_icon_path", PROPERTY_HINT_FILE), "set_script_class_icon_path", "get_script_class_icon_path"); ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &NativeScript::_new, MethodInfo(Variant::OBJECT, "new")); } @@ -131,6 +139,22 @@ Ref<GDNativeLibrary> NativeScript::get_library() const { return library; } +void NativeScript::set_script_class_name(String p_type) { + script_class_name = p_type; +} + +String NativeScript::get_script_class_name() const { + return script_class_name; +} + +void NativeScript::set_script_class_icon_path(String p_icon_path) { + script_class_icon_path = p_icon_path; +} + +String NativeScript::get_script_class_icon_path() const { + return script_class_icon_path; +} + bool NativeScript::can_instance() const { NativeScriptDesc *script_data = get_script_desc(); @@ -270,6 +294,10 @@ MethodInfo NativeScript::get_method_info(const StringName &p_method) const { return MethodInfo(); } +bool NativeScript::is_valid() const { + return true; +} + bool NativeScript::is_tool() const { NativeScriptDesc *script_data = get_script_desc(); @@ -786,18 +814,16 @@ MultiplayerAPI::RPCMode NativeScriptInstance::get_rpc_mode(const StringName &p_m return MultiplayerAPI::RPC_MODE_DISABLED; case GODOT_METHOD_RPC_MODE_REMOTE: return MultiplayerAPI::RPC_MODE_REMOTE; - case GODOT_METHOD_RPC_MODE_SYNC: - return MultiplayerAPI::RPC_MODE_SYNC; case GODOT_METHOD_RPC_MODE_MASTER: return MultiplayerAPI::RPC_MODE_MASTER; - case GODOT_METHOD_RPC_MODE_SLAVE: - return MultiplayerAPI::RPC_MODE_SLAVE; + 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_SLAVESYNC: - return MultiplayerAPI::RPC_MODE_SLAVESYNC; + case GODOT_METHOD_RPC_MODE_PUPPETSYNC: + return MultiplayerAPI::RPC_MODE_PUPPETSYNC; default: return MultiplayerAPI::RPC_MODE_DISABLED; } @@ -822,12 +848,16 @@ MultiplayerAPI::RPCMode NativeScriptInstance::get_rset_mode(const StringName &p_ return MultiplayerAPI::RPC_MODE_DISABLED; case GODOT_METHOD_RPC_MODE_REMOTE: return MultiplayerAPI::RPC_MODE_REMOTE; - case GODOT_METHOD_RPC_MODE_SYNC: - return MultiplayerAPI::RPC_MODE_SYNC; case GODOT_METHOD_RPC_MODE_MASTER: return MultiplayerAPI::RPC_MODE_MASTER; - case GODOT_METHOD_RPC_MODE_SLAVE: - return MultiplayerAPI::RPC_MODE_SLAVE; + 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; } @@ -986,6 +1016,20 @@ NativeScriptLanguage::NativeScriptLanguage() { has_objects_to_register = false; mutex = Mutex::create(); #endif + +#ifdef DEBUG_ENABLED + profiling = false; +#endif + + _init_call_type = "nativescript_init"; + _init_call_name = "nativescript_init"; + _terminate_call_name = "nativescript_terminate"; + _noarg_call_type = "nativescript_no_arg"; + _frame_call_name = "nativescript_frame"; +#ifndef NO_THREADS + _thread_enter_call_name = "nativescript_thread_enter"; + _thread_exit_call_name = "nativescript_thread_exit"; +#endif } NativeScriptLanguage::~NativeScriptLanguage() { @@ -1060,7 +1104,7 @@ Ref<Script> NativeScriptLanguage::get_template(const String &p_class_name, const s->set_class_name(p_class_name); return Ref<NativeScript>(s); } -bool NativeScriptLanguage::validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List<String> *r_functions, Set<int> *r_safe_lines) const { +bool NativeScriptLanguage::validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List<String> *r_functions, List<ScriptLanguage::Warning> *r_warnings, Set<int> *r_safe_lines) const { return true; } @@ -1128,17 +1172,105 @@ void NativeScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_ } void NativeScriptLanguage::profiling_start() { +#ifdef DEBUG_ENABLED +#ifndef NO_THREADS + MutexLock lock(mutex); +#endif + + profile_data.clear(); + profiling = true; +#endif } void NativeScriptLanguage::profiling_stop() { +#ifdef DEBUG_ENABLED +#ifndef NO_THREADS + MutexLock lock(mutex); +#endif + + profiling = false; +#endif } int NativeScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) { +#ifdef DEBUG_ENABLED +#ifndef NO_THREADS + MutexLock lock(mutex); +#endif + int current = 0; + + for (Map<StringName, ProfileData>::Element *d = profile_data.front(); d; d = d->next()) { + if (current >= p_info_max) + break; + + p_info_arr[current].call_count = d->get().call_count; + p_info_arr[current].self_time = d->get().self_time; + p_info_arr[current].total_time = d->get().total_time; + p_info_arr[current].signature = d->get().signature; + current++; + } + + return current; +#else return 0; +#endif } int NativeScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) { +#ifdef DEBUG_ENABLED +#ifndef NO_THREADS + MutexLock lock(mutex); +#endif + int current = 0; + + for (Map<StringName, ProfileData>::Element *d = profile_data.front(); d; d = d->next()) { + if (current >= p_info_max) + break; + + if (d->get().last_frame_call_count) { + p_info_arr[current].call_count = d->get().last_frame_call_count; + p_info_arr[current].self_time = d->get().last_frame_self_time; + p_info_arr[current].total_time = d->get().last_frame_total_time; + p_info_arr[current].signature = d->get().signature; + current++; + } + } + + return current; +#else return 0; +#endif +} + +void NativeScriptLanguage::profiling_add_data(StringName p_signature, uint64_t p_time) { +#ifdef DEBUG_ENABLED +#ifndef NO_THREADS + MutexLock lock(mutex); +#endif + + Map<StringName, ProfileData>::Element *d = profile_data.find(p_signature); + if (d) { + d->get().call_count += 1; + d->get().total_time += p_time; + d->get().frame_call_count += 1; + d->get().frame_total_time += p_time; + } else { + ProfileData data; + + data.signature = p_signature; + data.call_count = 1; + data.self_time = 0; + data.total_time = p_time; + data.frame_call_count = 1; + data.frame_self_time = 0; + data.frame_total_time = p_time; + data.last_frame_call_count = 0; + data.last_frame_self_time = 0; + data.last_frame_total_time = 0; + + profile_data.insert(p_signature, data); + } +#endif } int NativeScriptLanguage::register_binding_functions(godot_instance_binding_functions p_binding_functions) { @@ -1254,6 +1386,54 @@ void NativeScriptLanguage::free_instance_binding_data(void *p_data) { delete &binding_data; } +void NativeScriptLanguage::refcount_incremented_instance_binding(Object *p_object) { + + void *data = p_object->get_script_instance_binding(lang_idx); + + if (!data) + return; + + Vector<void *> &binding_data = *(Vector<void *> *)data; + + for (int i = 0; i < binding_data.size(); i++) { + if (!binding_data[i]) + continue; + + if (!binding_functions[i].first) + continue; + + if (binding_functions[i].second.refcount_incremented_instance_binding) { + binding_functions[i].second.refcount_incremented_instance_binding(binding_data[i], p_object); + } + } +} + +bool NativeScriptLanguage::refcount_decremented_instance_binding(Object *p_object) { + + void *data = p_object->get_script_instance_binding(lang_idx); + + if (!data) + return true; + + Vector<void *> &binding_data = *(Vector<void *> *)data; + + bool can_die = true; + + for (int i = 0; i < binding_data.size(); i++) { + if (!binding_data[i]) + continue; + + if (!binding_functions[i].first) + continue; + + if (binding_functions[i].second.refcount_decremented_instance_binding) { + can_die = can_die && binding_functions[i].second.refcount_decremented_instance_binding(binding_data[i], p_object); + } + } + + return can_die; +} + void NativeScriptLanguage::set_global_type_tag(int p_idx, StringName p_class_name, const void *p_type_tag) { if (!global_type_tags.has(p_idx)) { global_type_tags.insert(p_idx, HashMap<StringName, const void *>()); @@ -1381,6 +1561,24 @@ void NativeScriptLanguage::frame() { has_objects_to_register = false; } #endif + +#ifdef DEBUG_ENABLED + { +#ifndef NO_THREADS + MutexLock lock(mutex); +#endif + + for (Map<StringName, ProfileData>::Element *d = profile_data.front(); d; d = d->next()) { + d->get().last_frame_call_count = d->get().frame_call_count; + d->get().last_frame_self_time = d->get().frame_self_time; + d->get().last_frame_total_time = d->get().frame_total_time; + d->get().frame_call_count = 0; + d->get().frame_self_time = 0; + d->get().frame_total_time = 0; + } + } +#endif + call_libraries_cb(_frame_call_name); } @@ -1396,6 +1594,26 @@ void NativeScriptLanguage::thread_exit() { #endif // NO_THREADS +bool NativeScriptLanguage::handles_global_class_type(const String &p_type) const { + return p_type == "NativeScript"; +} + +String NativeScriptLanguage::get_global_class_name(const String &p_path, String *r_base_type, String *r_icon_path) const { + Ref<NativeScript> script = ResourceLoader::load(p_path, "NativeScript"); + if (script.is_valid()) { + if (r_base_type) + *r_base_type = script->get_instance_base_type(); + if (r_icon_path) + *r_icon_path = script->get_script_class_icon_path(); + return script->get_script_class_name(); + } + if (r_base_type) + *r_base_type = String(); + if (r_icon_path) + *r_icon_path = String(); + return String(); +} + void NativeReloadNode::_bind_methods() { ClassDB::bind_method(D_METHOD("_notification"), &NativeReloadNode::_notification); } @@ -1497,8 +1715,7 @@ void NativeReloadNode::_notification(int p_what) { } RES ResourceFormatLoaderNativeScript::load(const String &p_path, const String &p_original_path, Error *r_error) { - ResourceFormatLoaderText rsflt; - return rsflt.load(p_path, p_original_path, r_error); + return ResourceFormatLoaderText::singleton->load(p_path, p_original_path, r_error); } void ResourceFormatLoaderNativeScript::get_recognized_extensions(List<String> *p_extensions) const { diff --git a/modules/gdnative/nativescript/nativescript.h b/modules/gdnative/nativescript/nativescript.h index 1b39b63ad9..e6f3c06ee5 100644 --- a/modules/gdnative/nativescript/nativescript.h +++ b/modules/gdnative/nativescript/nativescript.h @@ -31,21 +31,21 @@ #ifndef NATIVE_SCRIPT_H #define NATIVE_SCRIPT_H +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" +#include "core/oa_hash_map.h" +#include "core/ordered_hash_map.h" +#include "core/os/thread_safe.h" #include "core/resource.h" #include "core/script_language.h" #include "core/self_list.h" -#include "io/resource_loader.h" -#include "io/resource_saver.h" -#include "oa_hash_map.h" -#include "ordered_hash_map.h" -#include "os/thread_safe.h" #include "scene/main/node.h" #include "modules/gdnative/gdnative.h" #include <nativescript/godot_nativescript.h> #ifndef NO_THREADS -#include "os/mutex.h" +#include "core/os/mutex.h" #endif struct NativeScriptDesc { @@ -70,8 +70,6 @@ struct NativeScriptDesc { String documentation; }; - String documentation; - Map<StringName, Method> methods; OrderedHashMap<StringName, Property> properties; Map<StringName, Signal> signals_; // QtCreator doesn't like the name signals @@ -81,6 +79,8 @@ struct NativeScriptDesc { godot_instance_create_func create_func; godot_instance_destroy_func destroy_func; + String documentation; + const void *type_tag; bool is_tool; @@ -118,6 +118,9 @@ class NativeScript : public Script { String class_name; + String script_class_name; + String script_class_icon_path; + #ifndef NO_THREADS Mutex *owners_lock; #endif @@ -135,6 +138,11 @@ public: void set_library(Ref<GDNativeLibrary> p_library); Ref<GDNativeLibrary> get_library() const; + void set_script_class_name(String p_type); + String get_script_class_name() const; + void set_script_class_icon_path(String p_icon_path); + String get_script_class_icon_path() const; + virtual bool can_instance() const; virtual Ref<Script> get_base_script() const; //for script inheritance @@ -152,6 +160,7 @@ public: virtual MethodInfo get_method_info(const StringName &p_method) const; virtual bool is_tool() const; + virtual bool is_valid() const; virtual ScriptLanguage *get_language() const; @@ -246,6 +255,22 @@ private: Map<int, HashMap<StringName, const void *> > global_type_tags; + struct ProfileData { + StringName signature; + uint64_t call_count; + uint64_t self_time; + uint64_t total_time; + uint64_t frame_call_count; + uint64_t frame_self_time; + uint64_t frame_total_time; + uint64_t last_frame_call_count; + uint64_t last_frame_self_time; + uint64_t last_frame_total_time; + }; + + Map<StringName, ProfileData> profile_data; + bool profiling; + public: // These two maps must only be touched on the main thread Map<String, Map<StringName, NativeScriptDesc> > library_classes; @@ -253,18 +278,14 @@ public: Map<String, Set<NativeScript *> > library_script_users; - const StringName _init_call_type = "nativescript_init"; - const StringName _init_call_name = "nativescript_init"; - - const StringName _terminate_call_name = "nativescript_terminate"; - - const StringName _noarg_call_type = "nativescript_no_arg"; - - const StringName _frame_call_name = "nativescript_frame"; - + StringName _init_call_type; + StringName _init_call_name; + StringName _terminate_call_name; + StringName _noarg_call_type; + StringName _frame_call_name; #ifndef NO_THREADS - const StringName _thread_enter_call_name = "nativescript_thread_enter"; - const StringName _thread_exit_call_name = "nativescript_thread_exit"; + StringName _thread_enter_call_name; + StringName _thread_exit_call_name; #endif NativeScriptLanguage(); @@ -295,7 +316,7 @@ public: virtual void get_comment_delimiters(List<String> *p_delimiters) const; virtual void get_string_delimiters(List<String> *p_delimiters) const; virtual Ref<Script> get_template(const String &p_class_name, const String &p_base_class_name) const; - virtual bool validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List<String> *r_functions, Set<int> *r_safe_lines = NULL) const; + virtual bool validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List<String> *r_functions, List<ScriptLanguage::Warning> *r_warnings = NULL, Set<int> *r_safe_lines = NULL) const; virtual Script *create_script() const; virtual bool has_named_classes() const; virtual bool supports_builtin_mode() const; @@ -329,9 +350,16 @@ public: virtual void *alloc_instance_binding_data(Object *p_object); virtual void free_instance_binding_data(void *p_data); + virtual void refcount_incremented_instance_binding(Object *p_object); + virtual bool refcount_decremented_instance_binding(Object *p_object); void set_global_type_tag(int p_idx, StringName p_class_name, const void *p_type_tag); const void *get_global_type_tag(int p_idx, StringName p_class_name) const; + + virtual bool handles_global_class_type(const String &p_type) const; + virtual String get_global_class_name(const String &p_path, String *r_base_type, String *r_icon_path) const; + + void profiling_add_data(StringName p_signature, uint64_t p_time); }; inline NativeScriptDesc *NativeScript::get_script_desc() const { @@ -341,11 +369,14 @@ inline NativeScriptDesc *NativeScript::get_script_desc() const { class NativeReloadNode : public Node { GDCLASS(NativeReloadNode, Node) - bool unloaded = false; + bool unloaded; public: static void _bind_methods(); void _notification(int p_what); + + NativeReloadNode() : + unloaded(false) {} }; class ResourceFormatLoaderNativeScript : public ResourceFormatLoader { diff --git a/modules/gdnative/nativescript/register_types.cpp b/modules/gdnative/nativescript/register_types.cpp index 9a0e764391..4433c0a638 100644 --- a/modules/gdnative/nativescript/register_types.cpp +++ b/modules/gdnative/nativescript/register_types.cpp @@ -30,8 +30,8 @@ #include "register_types.h" -#include "io/resource_loader.h" -#include "io/resource_saver.h" +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "nativescript.h" |