diff options
Diffstat (limited to 'modules/gdnative/gdnative.cpp')
-rw-r--r-- | modules/gdnative/gdnative.cpp | 1308 |
1 files changed, 180 insertions, 1128 deletions
diff --git a/modules/gdnative/gdnative.cpp b/modules/gdnative/gdnative.cpp index dad9a54df6..e810c33f1c 100644 --- a/modules/gdnative/gdnative.cpp +++ b/modules/gdnative/gdnative.cpp @@ -29,732 +29,70 @@ /*************************************************************************/ #include "gdnative.h" -#include "global_config.h" #include "global_constants.h" #include "io/file_access_encrypted.h" #include "os/file_access.h" #include "os/os.h" +#include "project_settings.h" #include "scene/main/scene_tree.h" -#include "scene/resources/scene_format_text.h" -#if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED) -#include "api_generator.h" -#endif - -#ifdef TOOLS_ENABLED -#include "editor/editor_node.h" -#endif - -Error NativeLibrary::initialize(NativeLibrary *&p_native_lib, const StringName p_path) { - - if (GDNativeScriptLanguage::get_singleton()->initialized_libraries.has(p_path)) { - p_native_lib = GDNativeScriptLanguage::get_singleton()->initialized_libraries[p_path]; - return OK; - } - - NativeLibrary *lib = memnew(NativeLibrary); - lib->path = p_path; - - p_native_lib = lib; - - // Open the file - - Error error; - error = OS::get_singleton()->open_dynamic_library(p_path, lib->handle); - if (error) return error; - ERR_FAIL_COND_V(!lib->handle, ERR_BUG); - - // Get the method - - void *library_init; - error = OS::get_singleton()->get_dynamic_library_symbol_handle(lib->handle, GDNativeScriptLanguage::get_init_symbol_name(), library_init); - if (error) return error; - ERR_FAIL_COND_V(!library_init, ERR_BUG); - - void (*library_init_fpointer)(godot_native_init_options *) = (void (*)(godot_native_init_options *))library_init; - - godot_native_init_options options; - - options.in_editor = SceneTree::get_singleton()->is_editor_hint(); - options.core_api_hash = ClassDB::get_api_hash(ClassDB::API_CORE); - options.editor_api_hash = ClassDB::get_api_hash(ClassDB::API_EDITOR); - options.no_api_hash = ClassDB::get_api_hash(ClassDB::API_NONE); - - library_init_fpointer(&options); // Catch errors? - - GDNativeScriptLanguage::get_singleton()->initialized_libraries[p_path] = lib; - - return OK; -} - -Error NativeLibrary::terminate(NativeLibrary *&p_native_lib) { - - if (!GDNativeScriptLanguage::get_singleton()->initialized_libraries.has(p_native_lib->path)) { - OS::get_singleton()->close_dynamic_library(p_native_lib->handle); - p_native_lib->handle = 0; - return OK; - } - - Error error = OK; - void *library_terminate; - error = OS::get_singleton()->get_dynamic_library_symbol_handle(p_native_lib->handle, GDNativeScriptLanguage::get_terminate_symbol_name(), library_terminate); - if (!error) { - - void (*library_terminate_pointer)(godot_native_terminate_options *) = (void (*)(godot_native_terminate_options *))library_terminate; - - godot_native_terminate_options options; - options.in_editor = SceneTree::get_singleton()->is_editor_hint(); - - library_terminate_pointer(&options); - } - - GDNativeScriptLanguage::get_singleton()->initialized_libraries.erase(p_native_lib->path); - - OS::get_singleton()->close_dynamic_library(p_native_lib->handle); - p_native_lib->handle = 0; - - return OK; -} - -// Script -#ifdef TOOLS_ENABLED - -void GDNativeScript::_update_placeholder(PlaceHolderScriptInstance *p_placeholder) { - ERR_FAIL_COND(!script_data); - - List<PropertyInfo> pinfo; - Map<StringName, Variant> values; - - for (Map<StringName, GDNativeScriptData::Property>::Element *E = script_data->properties.front(); E; E = E->next()) { - PropertyInfo p = E->get().info; - p.name = String(E->key()); - pinfo.push_back(p); - values[p.name] = E->get().default_value; - } - - p_placeholder->update(pinfo, values); -} - -void GDNativeScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) { - - placeholders.erase(p_placeholder); -} - -#endif - -bool GDNativeScript::can_instance() const { -#ifdef TOOLS_ENABLED - return script_data || (!is_tool() && !ScriptServer::is_scripting_enabled()); -#else - // allow defaultlibrary without editor features - if (!library.is_valid()) { - String path = GLOBAL_GET("gdnative/default_gdnativelibrary"); - - RES lib = ResourceLoader::load(path); - - if (lib.is_valid() && lib->cast_to<GDNativeLibrary>()) { - return true; - } - } +const String init_symbol = "godot_gdnative_init"; +const String terminate_symbol = "godot_gdnative_terminate"; - return script_data; -#endif - //return script_data || (!tool && !ScriptServer::is_scripting_enabled()); - // change to true enable in editor stuff. -} - -Ref<Script> GDNativeScript::get_base_script() const { - Ref<GDNativeScript> base_script; - base_script->library = library; - base_script->script_data = script_data; - base_script->script_name = script_data->base; - return base_script; -} - -StringName GDNativeScript::get_instance_base_type() const { - return script_data->base_native_type; -} - -ScriptInstance *GDNativeScript::instance_create(Object *p_this) { - -#ifdef TOOLS_ENABLED - - // find a good way to initialize stuff in the editor - if (!ScriptServer::is_scripting_enabled() && !is_tool()) { - // placeholder, for nodes. But for tools we want the real thing - - PlaceHolderScriptInstance *sins = memnew(PlaceHolderScriptInstance(GDNativeScriptLanguage::singleton, Ref<Script>((Script *)this), p_this)); - placeholders.insert(sins); - - if (!library.is_valid()) - return sins; - - if (!library->native_library) { - Error err = library->_initialize(); - if (err != OK) { - return sins; - } - } - - if (!script_data) { - script_data = library->get_script_data(script_name); - } - if (script_data && script_data->create_func.create_func) { - script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data); - } - - _update_placeholder(sins); - - return sins; - } - -#endif +String GDNativeLibrary::platform_names[NUM_PLATFORMS] = { + "X11_32bit", + "X11_64bit", + "Windows_32bit", + "Windows_64bit", + "OSX", - if (!library.is_valid()) { - String path = GLOBAL_GET("gdnative/default_gdnativelibrary"); + "Android", + "iOS", - RES lib = ResourceLoader::load(path); - - if (lib.is_valid() && lib->cast_to<GDNativeLibrary>()) { - set_library(lib); - } - } + "WebAssembly" +}; +String GDNativeLibrary::platform_lib_ext[NUM_PLATFORMS] = { + "so", + "so", + "dll", + "dll", + "dylib", - GDNativeInstance *new_instance = memnew(GDNativeInstance); + "so", + "dylib", - new_instance->owner = p_this; - new_instance->script = Ref<GDNativeScript>(this); + "wasm" +}; -#ifndef TOOLS_ENABLED - if (!ScriptServer::is_scripting_enabled()) { - new_instance->userdata = 0; - } else { - new_instance->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data); - } +// TODO(karroffel): make this actually do the right thing. +GDNativeLibrary::Platform GDNativeLibrary::current_platform = +#if defined(X11_ENABLED) + X11_64BIT; +#elif defined(WINDOWS_ENABLED) + WINDOWS_64BIT; +#elif defined(OSX_ENABLED) + OSX; #else - new_instance->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data); + X11_64BIT; // need a sensible default.. #endif - instances.insert(p_this); - return new_instance; -} - -bool GDNativeScript::instance_has(const Object *p_this) const { - return instances.has((Object *)p_this); // TODO -} - -bool GDNativeScript::has_source_code() const { - return false; -} - -String GDNativeScript::get_source_code() const { - return ""; -} - -Error GDNativeScript::reload(bool p_keep_state) { - return FAILED; -} - -bool GDNativeScript::has_method(const StringName &p_method) const { - if (!script_data) - return false; - GDNativeScriptData *data = script_data; - - while (data) { - if (data->methods.has(p_method)) - return true; - - data = data->base_data; - } - - return false; -} - -MethodInfo GDNativeScript::get_method_info(const StringName &p_method) const { - if (!script_data) - return MethodInfo(); - GDNativeScriptData *data = script_data; - - while (data) { - if (data->methods.has(p_method)) - return data->methods[p_method].info; - - data = data->base_data; - } - - ERR_FAIL_COND_V(!script_data->methods.has(p_method), MethodInfo()); - return MethodInfo(); -} - -void GDNativeScript::get_script_method_list(List<MethodInfo> *p_list) const { - if (!script_data) return; - - Set<MethodInfo> methods; - GDNativeScriptData *data = script_data; - - while (data) { - for (Map<StringName, GDNativeScriptData::Method>::Element *E = data->methods.front(); E; E = E->next()) { - methods.insert(E->get().info); - } - data = data->base_data; - } - - for (Set<MethodInfo>::Element *E = methods.front(); E; E = E->next()) { - p_list->push_back(E->get()); - } -} - -void GDNativeScript::get_script_property_list(List<PropertyInfo> *p_list) const { - if (!script_data) return; - - Set<PropertyInfo> properties; - GDNativeScriptData *data = script_data; - - while (data) { - for (Map<StringName, GDNativeScriptData::Property>::Element *E = data->properties.front(); E; E = E->next()) { - properties.insert(E->get().info); - } - data = data->base_data; - } - - for (Set<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) { - p_list->push_back(E->get()); - } -} - -bool GDNativeScript::get_property_default_value(const StringName &p_property, Variant &r_value) const { - if (!script_data) return false; - - GDNativeScriptData *data = script_data; - - while (data) { - if (data->properties.has(p_property)) { - r_value = data->properties[p_property].default_value; - return true; - } - - data = data->base_data; - } - - return false; -} - -bool GDNativeScript::is_tool() const { - ERR_FAIL_COND_V(!script_data, false); - return script_data->is_tool; -} - -String GDNativeScript::get_node_type() const { - return ""; // ? -} - -ScriptLanguage *GDNativeScript::get_language() const { - return GDNativeScriptLanguage::singleton; -} - -bool GDNativeScript::has_script_signal(const StringName &p_signal) const { - if (!script_data) - return false; - - GDNativeScriptData *data = script_data; - - while (data) { - if (data->signals_.has(p_signal)) { - return true; - } - - data = data->base_data; - } - - return false; -} - -void GDNativeScript::get_script_signal_list(List<MethodInfo> *r_signals) const { - if (!script_data) - return; - - Set<MethodInfo> signals_; - GDNativeScriptData *data = script_data; - - while (data) { - - for (Map<StringName, GDNativeScriptData::Signal>::Element *S = data->signals_.front(); S; S = S->next()) { - signals_.insert(S->get().signal); - } - - data = data->base_data; - } - - for (Set<MethodInfo>::Element *E = signals_.front(); E; E = E->next()) { - r_signals->push_back(E->get()); - } -} - -Variant GDNativeScript::_new(const Variant **p_args, int p_argcount, Variant::CallError &r_error) { - - /* STEP 1, CREATE */ - - if (!library.is_valid() || ((String)script_name).empty() || !script_data) { - r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD; - return Variant(); - } - - r_error.error = Variant::CallError::CALL_OK; - REF ref; - Object *owner = NULL; - - GDNativeScriptData *_baseptr = script_data; - while (_baseptr->base_data) { - _baseptr = _baseptr->base_data; - } - - if (!(_baseptr->base_native_type == "")) { - owner = ClassDB::instance(_baseptr->base_native_type); - } else { - owner = memnew(Reference); //by default, no base means use reference - } - - Reference *r = owner->cast_to<Reference>(); - if (r) { - ref = REF(r); - } - - // GDScript does it like this: _create_instance(p_args, p_argcount, owner, r != NULL, r_error); - // @Todo support varargs for constructors. - GDNativeInstance *instance = (GDNativeInstance *)instance_create(owner); - - owner->set_script_instance(instance); - if (!instance) { - if (ref.is_null()) { - memdelete(owner); //no owner, sorry - } - return Variant(); - } - - if (ref.is_valid()) { - return ref; - } else { - return owner; - } -} - -Ref<GDNativeLibrary> GDNativeScript::get_library() const { - return library; -} - -void GDNativeScript::set_library(Ref<GDNativeLibrary> p_library) { - library = p_library; - -#ifdef TOOLS_ENABLED - if (!ScriptServer::is_scripting_enabled()) - return; -#endif - if (library.is_valid()) { - Error initalize_status = library->_initialize(); - ERR_FAIL_COND(initalize_status != OK); - if (script_name) { - script_data = library->native_library->scripts[script_name]; - ERR_FAIL_COND(!script_data); - } - } -} - -StringName GDNativeScript::get_script_name() const { - return script_name; -} - -void GDNativeScript::set_script_name(StringName p_script_name) { - script_name = p_script_name; - - if (library.is_valid()) { -#ifdef TOOLS_ENABLED - if (!library->native_library) { - library->_initialize(); - } -#endif - if (library->native_library) { - script_data = library->get_script_data(script_name); - ERR_FAIL_COND(!script_data); - } - } -} - -void GDNativeScript::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_library"), &GDNativeScript::get_library); - ClassDB::bind_method(D_METHOD("set_library", "library"), &GDNativeScript::set_library); - ClassDB::bind_method(D_METHOD("get_script_name"), &GDNativeScript::get_script_name); - ClassDB::bind_method(D_METHOD("set_script_name", "script_name"), &GDNativeScript::set_script_name); - - ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &GDNativeScript::_new, MethodInfo(Variant::OBJECT, "new")); - - ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "GDNativeLibrary"), "set_library", "get_library"); - ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "script_name"), "set_script_name", "get_script_name"); -} - -GDNativeScript::GDNativeScript() { - script_data = NULL; - GDNativeScriptLanguage::get_singleton()->script_list.insert(this); -} - -GDNativeScript::~GDNativeScript() { - //hmm - GDNativeScriptLanguage::get_singleton()->script_list.erase(this); -} - -// Library - -GDNativeLibrary *GDNativeLibrary::currently_initialized_library = NULL; - -GDNativeLibrary *GDNativeLibrary::get_currently_initialized_library() { - return currently_initialized_library; -} - -static const char *_dl_platforms_info[] = { - "|unix|so|Unix", - "unix|x11|so|X11", - "unix|server|so|Server", - "unix|android|so|Android", - "unix|haiku|so|Haiku", // Right? - "|mac|dylib|Mac", - "mac|ios|dylib|iOS", - "mac|osx|dylib|OSX", - "|html5|js|HTML5", - "|windows|dll|Windows", - "windows|uwp|dll|UWP", - NULL // Finishing condition -}; - -void GDNativeLibrary::set_platform_file(StringName p_platform, String p_file) { - if (p_file.empty()) { - platform_files.erase(p_platform); - } else { - platform_files[p_platform] = p_file; - } -} - -String GDNativeLibrary::get_platform_file(StringName p_platform) const { - if (platform_files.has(p_platform)) { - return platform_files[p_platform]; - } else { - return ""; - } -} - -Error GDNativeLibrary::_initialize() { - _THREAD_SAFE_METHOD_ - - // Get the file - - const String platform_name = OS::get_singleton()->get_name(); - String platform_file(""); - char **platform_info = (char **)_dl_platforms_info; - - if (platform_files.has(platform_name.to_lower())) { - platform_file = platform_files[platform_name.to_lower()]; - } - - while (*platform_info) { - String platform_info_string(*platform_info); - - if (platform_name == platform_info_string.get_slicec('|', 3)) { - String platform_key = platform_info_string.get_slicec('|', 1); - String fallback_platform_key = platform_info_string.get_slicec('|', 0); - - if (platform_files.has(platform_key)) { - platform_file = platform_files[platform_key]; - } else if (!fallback_platform_key.empty() && platform_files.has(fallback_platform_key)) { - platform_file = platform_files[fallback_platform_key]; - } else { - return ERR_UNAVAILABLE; - } - } - platform_info++; - } - ERR_FAIL_COND_V(platform_file == "", ERR_DOES_NOT_EXIST); - - StringName path = GlobalConfig::get_singleton()->globalize_path(platform_file); - - GDNativeLibrary::currently_initialized_library = this; - - Error ret = NativeLibrary::initialize(native_library, path); - native_library->dllib = this; - - GDNativeLibrary::currently_initialized_library = NULL; - - return ret; -} - -Error GDNativeLibrary::_terminate() { - ERR_FAIL_COND_V(!native_library, ERR_BUG); - ERR_FAIL_COND_V(!native_library->handle, ERR_BUG); - - // de-init stuff - - for (Map<StringName, GDNativeScriptData *>::Element *E = native_library->scripts.front(); E; E = E->next()) { - for (Map<StringName, GDNativeScriptData::Method>::Element *M = E->get()->methods.front(); M; M = M->next()) { - if (M->get().method.free_func) { - M->get().method.free_func(M->get().method.method_data); - } - } - if (E->get()->create_func.free_func) { - E->get()->create_func.free_func(E->get()->create_func.method_data); - } - if (E->get()->destroy_func.free_func) { - E->get()->destroy_func.free_func(E->get()->destroy_func.method_data); - } - - for (Set<GDNativeScript *>::Element *S = GDNativeScriptLanguage::get_singleton()->script_list.front(); S; S = S->next()) { - if (S->get()->script_data == E->get()) { - S->get()->script_data = NULL; - } - } - - memdelete(E->get()); - } - - Error ret = NativeLibrary::terminate(native_library); - native_library->scripts.clear(); - - return ret; -} - -void GDNativeLibrary::_register_script(const StringName p_name, const StringName p_base, godot_instance_create_func p_instance_func, godot_instance_destroy_func p_destroy_func) { - ERR_FAIL_COND(!native_library); - ERR_FAIL_COND(native_library->scripts.has(p_name)); - - GDNativeScriptData *s = memnew(GDNativeScriptData); - s->base = p_base; - s->create_func = p_instance_func; - s->destroy_func = p_destroy_func; - Map<StringName, GDNativeScriptData *>::Element *E = native_library->scripts.find(p_base); - if (E) { - s->base_data = E->get(); - s->base_native_type = s->base_data->base_native_type; - } else { - if (!ClassDB::class_exists(p_base)) { - memdelete(s); - ERR_EXPLAIN("Invalid base for registered type '" + p_name + "'"); - ERR_FAIL(); - } - s->base_native_type = p_base; - } - - native_library->scripts.insert(p_name, s); -} - -void GDNativeLibrary::_register_tool_script(const StringName p_name, const StringName p_base, godot_instance_create_func p_instance_func, godot_instance_destroy_func p_destroy_func) { - ERR_FAIL_COND(!native_library); - ERR_FAIL_COND(native_library->scripts.has(p_name)); - - GDNativeScriptData *s = memnew(GDNativeScriptData); - s->base = p_base; - s->create_func = p_instance_func; - s->destroy_func = p_destroy_func; - s->is_tool = true; - Map<StringName, GDNativeScriptData *>::Element *E = native_library->scripts.find(p_base); - if (E) { - s->base_data = E->get(); - s->base_native_type = s->base_data->base_native_type; - } else { - if (!ClassDB::class_exists(p_base)) { - memdelete(s); - ERR_EXPLAIN("Invalid base for registered type '" + p_name + "'"); - ERR_FAIL(); - } - s->base_native_type = p_base; - } - - native_library->scripts.insert(p_name, s); -} - -void GDNativeLibrary::_register_script_method(const StringName p_name, const StringName p_method, godot_method_attributes p_attr, godot_instance_method p_func, MethodInfo p_info) { - ERR_FAIL_COND(!native_library); - ERR_FAIL_COND(!native_library->scripts.has(p_name)); - - p_info.name = p_method; - GDNativeScriptData::Method method; - - method = GDNativeScriptData::Method(p_func, p_info, p_attr.rpc_type); - - native_library->scripts[p_name]->methods.insert(p_method, method); -} - -void GDNativeLibrary::_register_script_property(const StringName p_name, const String p_path, godot_property_attributes *p_attr, godot_property_set_func p_setter, godot_property_get_func p_getter) { - ERR_FAIL_COND(!native_library); - ERR_FAIL_COND(!native_library->scripts.has(p_name)); - - GDNativeScriptData::Property p; - - PropertyInfo pi; - pi.name = p_path; - - if (p_attr != NULL) { - pi = PropertyInfo((Variant::Type)p_attr->type, p_path, (PropertyHint)p_attr->hint, *(String *)&p_attr->hint_string, p_attr->usage); - - p = GDNativeScriptData::Property(p_setter, p_getter, pi, *(Variant *)&p_attr->default_value, p_attr->rset_type); - } - - native_library->scripts[p_name]->properties.insert(p_path, p); +GDNativeLibrary::GDNativeLibrary() + : library_paths() { } -void GDNativeLibrary::_register_script_signal(const StringName p_name, const godot_signal *p_signal) { - ERR_FAIL_COND(!native_library); - ERR_FAIL_COND(!native_library->scripts.has(p_name)); - ERR_FAIL_COND(!p_signal); - - GDNativeScriptData::Signal signal; - - signal.signal.name = *(String *)&p_signal->name; - - { - List<PropertyInfo> arguments; - for (int i = 0; i < p_signal->num_args; i++) { - PropertyInfo info; - godot_signal_argument attrib = p_signal->args[i]; - - String *name = (String *)&attrib.name; - info.name = *name; - info.type = (Variant::Type)attrib.type; - info.hint = (PropertyHint)attrib.hint; - info.hint_string = *(String *)&attrib.hint_string; - info.usage = attrib.usage; - - arguments.push_back(info); - } - - signal.signal.arguments = arguments; - } - - { - Vector<Variant> default_arguments; - for (int i = 0; i < p_signal->num_default_args; i++) { - Variant *v; - godot_signal_argument attrib = p_signal->args[i]; - - v = (Variant *)&attrib.default_value; - - default_arguments.push_back(*v); - } - - signal.signal.default_arguments = default_arguments; - } - - native_library->scripts[p_name]->signals_.insert(*(String *)&p_signal->name, signal); +GDNativeLibrary::~GDNativeLibrary() { } -GDNativeScriptData *GDNativeLibrary::get_script_data(const StringName p_name) { - ERR_FAIL_COND_V(!native_library, NULL); - - ERR_FAIL_COND_V(!native_library->scripts.has(p_name), NULL); - - return native_library->scripts[p_name]; +void GDNativeLibrary::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_library_path", "platform", "path"), &GDNativeLibrary::set_library_path); + ClassDB::bind_method(D_METHOD("get_library_path", "platform"), &GDNativeLibrary::get_library_path); } bool GDNativeLibrary::_set(const StringName &p_name, const Variant &p_value) { String name = p_name; if (name.begins_with("platform/")) { - set_platform_file(name.get_slice("/", 1), p_value); + set_library_path(name.get_slice("/", 1), p_value); return true; } return false; @@ -763,517 +101,231 @@ bool GDNativeLibrary::_set(const StringName &p_name, const Variant &p_value) { bool GDNativeLibrary::_get(const StringName &p_name, Variant &r_ret) const { String name = p_name; if (name.begins_with("platform/")) { - r_ret = get_platform_file(name.get_slice("/", 1)); + r_ret = get_library_path(name.get_slice("/", 1)); return true; } return false; } void GDNativeLibrary::_get_property_list(List<PropertyInfo> *p_list) const { - char **platform_info = (char **)_dl_platforms_info; - - Set<String> registered_platform_names; - { - List<StringName> ep; - // ep.push_back("X11"); - // EditorImportExport::get_singleton()->get_export_platforms(&ep); - - // @Todo - // get export platforms with the new export system somehow. - for (List<StringName>::Element *E = ep.front(); E; E = E->next()) { - registered_platform_names.insert(String(E->get()).to_lower()); - } + for (int i = 0; i < NUM_PLATFORMS; i++) { + p_list->push_back(PropertyInfo(Variant::STRING, + "platform/" + platform_names[i], + PROPERTY_HINT_FILE, + "*." + platform_lib_ext[i])); } +} - while (*platform_info) { - String platform_info_string(*platform_info); - String fallback_platform_key = platform_info_string.get_slicec('|', 0); - String platform_key = platform_info_string.get_slicec('|', 1); - String platform_extension = platform_info_string.get_slicec('|', 2); - String platform_name = platform_info_string.get_slicec('|', 3); - - registered_platform_names.erase(platform_name); - - if (fallback_platform_key.empty()) { - p_list->push_back(PropertyInfo(Variant::STRING, "platform/" + platform_key, PROPERTY_HINT_FILE, "*." + platform_extension)); - - } else { - if (platform_files.has(platform_key)) { - p_list->push_back(PropertyInfo(Variant::STRING, "platform/" + platform_key, PROPERTY_HINT_FILE, "*." + platform_extension, PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_CHECKABLE | PROPERTY_USAGE_CHECKED)); - } else { - p_list->push_back(PropertyInfo(Variant::STRING, "platform/" + platform_key, PROPERTY_HINT_FILE, "*." + platform_extension, PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_CHECKABLE)); - } +void GDNativeLibrary::set_library_path(StringName p_platform, String p_path) { + int i; + for (i = 0; i <= NUM_PLATFORMS; i++) { + if (i == NUM_PLATFORMS) break; + if (platform_names[i] == p_platform) { + break; } - platform_info++; - } - - while (registered_platform_names.size()) { - const String platform_name = registered_platform_names.front()->get(); - registered_platform_names.erase(platform_name); - p_list->push_back(PropertyInfo(Variant::STRING, "platform/" + platform_name.to_lower(), PROPERTY_HINT_FILE, "*")); } -} - -void GDNativeLibrary::_notification(int what) { - // TODO -} - -void GDNativeLibrary::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_platform_file", "platform", "file"), &GDNativeLibrary::set_platform_file); - ClassDB::bind_method(D_METHOD("get_platform_file", "platform"), &GDNativeLibrary::get_platform_file); -} - -GDNativeLibrary::GDNativeLibrary() { - native_library = NULL; -} - -GDNativeLibrary::~GDNativeLibrary() { - if (!native_library) { - return; + if (i == NUM_PLATFORMS) { + ERR_EXPLAIN(String("No such platform: ") + p_platform); + ERR_FAIL(); } - if (native_library->handle) { - _terminate(); - } + library_paths[i] = p_path; } -// Instance - -bool GDNativeInstance::set(const StringName &p_name, const Variant &p_value) { - if (!script->script_data) - return false; - if (script->script_data->properties.has(p_name)) { - script->script_data->properties[p_name].setter.set_func((godot_object *)owner, script->script_data->properties[p_name].setter.method_data, userdata, *(godot_variant *)&p_value); - return true; +String GDNativeLibrary::get_library_path(StringName p_platform) const { + int i; + for (i = 0; i <= NUM_PLATFORMS; i++) { + if (i == NUM_PLATFORMS) break; + if (platform_names[i] == p_platform) { + break; + } } - return false; -} -bool GDNativeInstance::get(const StringName &p_name, Variant &r_ret) const { - if (!script->script_data) - return false; - if (script->script_data->properties.has(p_name)) { - godot_variant value = script->script_data->properties[p_name].getter.get_func((godot_object *)owner, script->script_data->properties[p_name].getter.method_data, userdata); - r_ret = *(Variant *)&value; - return true; + if (i == NUM_PLATFORMS) { + ERR_EXPLAIN(String("No such platform: ") + p_platform); + ERR_FAIL_V(""); } - return false; -} - -void GDNativeInstance::get_property_list(List<PropertyInfo> *p_properties) const { - script->get_script_property_list(p_properties); - // TODO: dynamic properties -} -Variant::Type GDNativeInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const { - if (script->script_data->properties.has(p_name)) { - *r_is_valid = true; - return script->script_data->properties[p_name].info.type; - } - *r_is_valid = false; - return Variant::NIL; + return library_paths[i]; } -void GDNativeInstance::get_method_list(List<MethodInfo> *p_list) const { - script->get_script_method_list(p_list); +String GDNativeLibrary::get_active_library_path() const { + return library_paths[GDNativeLibrary::current_platform]; } -bool GDNativeInstance::has_method(const StringName &p_method) const { - return script->has_method(p_method); +GDNative::GDNative() { + initialized = false; + native_handle = NULL; } -Variant GDNativeInstance::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) { - // TODO: validated methods & errors - - GDNativeScriptData *data_ptr = script->script_data; - while (data_ptr) { - Map<StringName, GDNativeScriptData::Method>::Element *E = data_ptr->methods.find(p_method); - if (E) { - godot_variant result = E->get().method.method((godot_object *)owner, E->get().method.method_data, userdata, p_argcount, (godot_variant **)p_args); - return *(Variant *)&result; - } - data_ptr = data_ptr->base_data; - } - r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD; - return Variant(); +GDNative::~GDNative() { + // TODO(karroffel): implement ALL the things! } -void GDNativeInstance::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) { - // TODO: validated methods & errors +extern "C" void _api_anchor(); - GDNativeScriptData *data_ptr = script->script_data; - while (data_ptr) { - Map<StringName, GDNativeScriptData::Method>::Element *E = data_ptr->methods.find(p_method); - if (E) { - E->get().method.method((godot_object *)owner, E->get().method.method_data, userdata, p_argcount, (godot_variant **)p_args); - } - data_ptr = data_ptr->base_data; - } +void GDNative::_compile_dummy_for_api() { + _api_anchor(); } -void GDNativeInstance::_ml_call_reversed(GDNativeScriptData *data_ptr, const StringName &p_method, const Variant **p_args, int p_argcount) { - // TODO: validated methods & errors +void GDNative::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_library", "library:GDNativeLibrary"), &GDNative::set_library); + ClassDB::bind_method(D_METHOD("get_library:GDNativeLibrary"), &GDNative::get_library); - if (data_ptr->base_data) - _ml_call_reversed(data_ptr->base_data, p_method, p_args, p_argcount); + ClassDB::bind_method(D_METHOD("initialize"), &GDNative::initialize); + ClassDB::bind_method(D_METHOD("terminate"), &GDNative::terminate); - // Variant::CallError ce; + // TODO(karroffel): get_native_(raw_)call_types binding? - Map<StringName, GDNativeScriptData::Method>::Element *E = data_ptr->methods.find(p_method); - if (E) { - E->get().method.method((godot_object *)owner, E->get().method.method_data, userdata, p_argcount, (godot_variant **)p_args); - } -} + // TODO(karroffel): make this a varargs function? + ClassDB::bind_method(D_METHOD("call_native:Variant", "procedure_name", "arguments:Array"), &GDNative::call_native); -void GDNativeInstance::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) { - if (script.ptr() && script->script_data) { - _ml_call_reversed(script->script_data, p_method, p_args, p_argcount); - } -} - -void GDNativeInstance::notification(int p_notification) { - Variant value = p_notification; - const Variant *args[1] = { &value }; - call_multilevel(GDNativeScriptLanguage::singleton->strings._notification, args, 1); + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "GDNativeLibrary"), "set_library", "get_library"); } -Ref<Script> GDNativeInstance::get_script() const { - return script; +void GDNative::set_library(Ref<GDNativeLibrary> p_library) { + library = p_library; } -ScriptLanguage *GDNativeInstance::get_language() { - return GDNativeScriptLanguage::singleton; +Ref<GDNativeLibrary> GDNative::get_library() { + return library; } -ScriptInstance::RPCMode GDNativeInstance::get_rpc_mode(const StringName &p_method) const { - GDNativeScriptData::Method m = script->script_data->methods[p_method]; - switch (m.rpc_mode) { - case GODOT_METHOD_RPC_MODE_DISABLED: - return RPC_MODE_DISABLED; - case GODOT_METHOD_RPC_MODE_REMOTE: - return RPC_MODE_REMOTE; - case GODOT_METHOD_RPC_MODE_SYNC: - return RPC_MODE_SYNC; - case GODOT_METHOD_RPC_MODE_MASTER: - return RPC_MODE_MASTER; - case GODOT_METHOD_RPC_MODE_SLAVE: - return RPC_MODE_SLAVE; - default: - return RPC_MODE_DISABLED; +bool GDNative::initialize() { + if (library.is_null()) { + ERR_PRINT("No library set, can't initialize GDNative object"); + return false; } -} -ScriptInstance::RPCMode GDNativeInstance::get_rset_mode(const StringName &p_variable) const { - GDNativeScriptData::Property p = script->script_data->properties[p_variable]; - switch (p.rset_mode) { - case GODOT_METHOD_RPC_MODE_DISABLED: - return RPC_MODE_DISABLED; - case GODOT_METHOD_RPC_MODE_REMOTE: - return RPC_MODE_REMOTE; - case GODOT_METHOD_RPC_MODE_SYNC: - return RPC_MODE_SYNC; - case GODOT_METHOD_RPC_MODE_MASTER: - return RPC_MODE_MASTER; - case GODOT_METHOD_RPC_MODE_SLAVE: - return RPC_MODE_SLAVE; - default: - return RPC_MODE_DISABLED; + String lib_path = library->get_active_library_path(); + if (lib_path.empty()) { + ERR_PRINT("No library set for this platform"); + return false; } -} - -GDNativeInstance::GDNativeInstance() { - owner = NULL; - userdata = NULL; -} -GDNativeInstance::~GDNativeInstance() { - if (script.is_valid()) { - if (owner) { - script->instances.erase(owner); - } - if (!script->script_data) - return; - script->script_data->destroy_func.destroy_func((godot_object *)owner, script->script_data->destroy_func.method_data, userdata); - if (script->script_data->destroy_func.free_func) - script->script_data->destroy_func.free_func(script->script_data->destroy_func.method_data); - if (script->script_data->create_func.free_func) - script->script_data->create_func.free_func(script->script_data->create_func.method_data); + String path = ProjectSettings::get_singleton()->globalize_path(lib_path); + Error err = OS::get_singleton()->open_dynamic_library(path, native_handle); + if (err != OK) { + return false; } -} - -// Language - -GDNativeScriptLanguage *GDNativeScriptLanguage::singleton = NULL; - -String GDNativeScriptLanguage::get_name() const { - return "Native"; -} - -void _add_reload_node() { -#ifdef TOOLS_ENABLED - GDNativeReloadNode *rn = memnew(GDNativeReloadNode); - EditorNode::get_singleton()->add_child(rn); -#endif -} - -void GDNativeScriptLanguage::init() { - // TODO: Expose globals - GLOBAL_DEF("gdnative/default_gdnativelibrary", ""); - PropertyInfo prop_info(Variant::STRING, "gdnative/default_gdnativelibrary", PROPERTY_HINT_FILE, "tres,res,dllib"); - GlobalConfig::get_singleton()->set_custom_property_info("gdnative/default_gdnativelibrary", prop_info); - -// generate bindings -#if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED) - - List<String> args = OS::get_singleton()->get_cmdline_args(); - List<String>::Element *E = args.find("--gdnative-generate-json-api"); + void *library_init; + err = OS::get_singleton()->get_dynamic_library_symbol_handle( + native_handle, + init_symbol, + library_init); - if (E && E->next()) { - if (generate_c_api(E->next()->get()) != OK) { - ERR_PRINT("Failed to generate C API\n"); - } + if (err || !library_init) { + return false; } -#endif - -#ifdef TOOLS_ENABLED - // if (SceneTree::get_singleton()->is_editor_hint()) { - EditorNode::add_init_callback(&_add_reload_node); -// } -#endif -} -String GDNativeScriptLanguage::get_type() const { - return "Native"; -} - -String GDNativeScriptLanguage::get_extension() const { - return "gdn"; -} - -Error GDNativeScriptLanguage::execute_file(const String &p_path) { - return OK; // ?? -} - -void GDNativeScriptLanguage::finish() { - // cleanup is for noobs -} + godot_gdnative_init_fn library_init_fpointer; + library_init_fpointer = (godot_gdnative_init_fn)library_init; -// scons doesn't want to link in the api source so we need to call a dummy function to cause it to link -extern "C" void _api_anchor(); - -void GDNativeScriptLanguage::_compile_dummy_for_the_api() { - _api_anchor(); -} + godot_gdnative_init_options options; -Ref<Script> GDNativeScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const { - GDNativeScript *src = memnew(GDNativeScript); - src->set_script_name(p_class_name); - return Ref<GDNativeScript>(src); -} - -bool GDNativeScriptLanguage::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) const { - return false; // TODO -} + options.in_editor = SceneTree::get_singleton()->is_editor_hint(); + options.core_api_hash = ClassDB::get_api_hash(ClassDB::API_CORE); + options.editor_api_hash = ClassDB::get_api_hash(ClassDB::API_EDITOR); + options.no_api_hash = ClassDB::get_api_hash(ClassDB::API_NONE); -Script *GDNativeScriptLanguage::create_script() const { - GDNativeScript *scr = memnew(GDNativeScript); - return scr; -} + library_init_fpointer(&options); -bool GDNativeScriptLanguage::has_named_classes() const { return true; } -int GDNativeScriptLanguage::find_function(const String &p_function, const String &p_code) const { - return -1; // No source code! -} - -String GDNativeScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const { - return ""; // No source code! -} - -void GDNativeScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) { - // TODO TODO TODO -} - -// TODO: Any debugging? (research) -String GDNativeScriptLanguage::debug_get_error() const { - return ""; -} - -int GDNativeScriptLanguage::debug_get_stack_level_count() const { - return 1; // ? -} +bool GDNative::terminate() { -int GDNativeScriptLanguage::debug_get_stack_level_line(int p_level) const { - return -1; -} - -String GDNativeScriptLanguage::debug_get_stack_level_function(int p_level) const { - return "[native code]"; // ? -} - -String GDNativeScriptLanguage::debug_get_stack_level_source(int p_level) const { - return ""; -} - -void GDNativeScriptLanguage::debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {} - -void GDNativeScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {} - -String GDNativeScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) { - return ""; // ?? -} + if (native_handle == NULL) { + ERR_PRINT("No valid library handle, can't terminate GDNative object"); + return false; + } -void GDNativeScriptLanguage::reload_all_scripts() { - // @Todo -} + Error error = OK; + void *library_terminate; + error = OS::get_singleton()->get_dynamic_library_symbol_handle( + native_handle, + terminate_symbol, + library_terminate); + if (error) { + OS::get_singleton()->close_dynamic_library(native_handle); + native_handle = NULL; + return true; + } -void GDNativeScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) { - // @Todo - OS::get_singleton()->print("reload tool scripts\n"); -} + godot_gdnative_terminate_fn library_terminate_pointer; + library_terminate_pointer = (godot_gdnative_terminate_fn)library_terminate; -void GDNativeScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const { - p_extensions->push_back("gdn"); // Container file format -} + // TODO(karroffel): remove this? Should be part of NativeScript, not + // GDNative IMO + godot_gdnative_terminate_options options; + options.in_editor = SceneTree::get_singleton()->is_editor_hint(); -void GDNativeScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const { -} + library_terminate_pointer(&options); -void GDNativeScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const { -} + // GDNativeScriptLanguage::get_singleton()->initialized_libraries.erase(p_native_lib->path); -// TODO: all profilling -void GDNativeScriptLanguage::profiling_start() { -} + OS::get_singleton()->close_dynamic_library(native_handle); + native_handle = NULL; -void GDNativeScriptLanguage::profiling_stop() { + return false; } -int GDNativeScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) { - return 0; +void GDNativeCallRegistry::register_native_call_type(StringName p_call_type, native_call_cb p_callback) { + native_calls.insert(p_call_type, p_callback); } -int GDNativeScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) { - return 0; +void GDNativeCallRegistry::register_native_raw_call_type(StringName p_raw_call_type, native_raw_call_cb p_callback) { + native_raw_calls.insert(p_raw_call_type, p_callback); } -void GDNativeScriptLanguage::frame() { -} +Vector<StringName> GDNativeCallRegistry::get_native_call_types() { + Vector<StringName> call_types; + call_types.resize(native_calls.size()); -String GDNativeScriptLanguage::get_init_symbol_name() { - return "godot_native_init"; // TODO: Maybe make some internal function which would do the actual stuff -} + size_t idx = 0; + for (Map<StringName, native_call_cb>::Element *E = native_calls.front(); E; E = E->next(), idx++) { + call_types[idx] = E->key(); + } -String GDNativeScriptLanguage::get_terminate_symbol_name() { - return "godot_native_terminate"; + return call_types; } -GDNativeScriptLanguage::GDNativeScriptLanguage() { - ERR_FAIL_COND(singleton); - strings._notification = StringName("_notification"); - singleton = this; - initialized_libraries = Map<StringName, NativeLibrary *>(); -} +Vector<StringName> GDNativeCallRegistry::get_native_raw_call_types() { + Vector<StringName> call_types; + call_types.resize(native_raw_calls.size()); -GDNativeScriptLanguage::~GDNativeScriptLanguage() { - singleton = NULL; -} - -// DLReloadNode + size_t idx = 0; + for (Map<StringName, native_raw_call_cb>::Element *E = native_raw_calls.front(); E; E = E->next(), idx++) { + call_types[idx] = E->key(); + } -void GDNativeReloadNode::_bind_methods() { - ClassDB::bind_method("_notification", &GDNativeReloadNode::_notification); + return call_types; } -void GDNativeReloadNode::_notification(int p_what) { -#ifdef TOOLS_ENABLED - - switch (p_what) { - case MainLoop::NOTIFICATION_WM_FOCUS_IN: { - - Set<NativeLibrary *> libs_to_reload; - - for (Map<StringName, NativeLibrary *>::Element *L = GDNativeScriptLanguage::get_singleton()->initialized_libraries.front(); L; L = L->next()) { - // check if file got modified at all - // @Todo - - libs_to_reload.insert(L->get()); - } - - for (Set<NativeLibrary *>::Element *L = libs_to_reload.front(); L; L = L->next()) { - - GDNativeLibrary *lib = L->get()->dllib; - - lib->_terminate(); - lib->_initialize(); - - // update placeholders (if any) - - Set<GDNativeScript *> scripts; - - for (Set<GDNativeScript *>::Element *S = GDNativeScriptLanguage::get_singleton()->script_list.front(); S; S = S->next()) { - - if (lib->native_library->scripts.has(S->get()->get_script_name())) { - GDNativeScript *script = S->get(); - script->script_data = lib->get_script_data(script->get_script_name()); - scripts.insert(script); - } - } - - for (Set<GDNativeScript *>::Element *S = scripts.front(); S; S = S->next()) { - GDNativeScript *script = S->get(); - if (script->placeholders.size() == 0) - continue; +Variant GDNative::call_native(StringName p_native_call_type, StringName p_procedure_name, Array p_arguments) { - for (Set<PlaceHolderScriptInstance *>::Element *P = script->placeholders.front(); P; P = P->next()) { - PlaceHolderScriptInstance *p = P->get(); - script->_update_placeholder(p); - } - } - } - - } break; - default: { - }; + Map<StringName, native_call_cb>::Element *E = GDNativeCallRegistry::singleton->native_calls.find(p_native_call_type); + if (!E) { + ERR_PRINT((String("No handler for native call type \"" + p_native_call_type) + "\" found").utf8().get_data()); + return Variant(); } -#endif -} -// Resource loader/saver + String procedure_name = p_procedure_name; + godot_variant result = E->get()(native_handle, (godot_string *)&procedure_name, (godot_array *)&p_arguments); -RES ResourceFormatLoaderGDNativeScript::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 *(Variant *)&result; } -void ResourceFormatLoaderGDNativeScript::get_recognized_extensions(List<String> *p_extensions) const { - p_extensions->push_back("gdn"); -} -bool ResourceFormatLoaderGDNativeScript::handles_type(const String &p_type) const { - return (p_type == "Script" || p_type == "Native"); -} -String ResourceFormatLoaderGDNativeScript::get_resource_type(const String &p_path) const { - String el = p_path.get_extension().to_lower(); - if (el == "gdn") - return "Native"; - return ""; -} +void GDNative::call_native_raw(StringName p_raw_call_type, StringName p_procedure_name, void *data, int num_args, void **args, void *r_return) { -Error ResourceFormatSaverGDNativeScript::save(const String &p_path, const RES &p_resource, uint32_t p_flags) { - ResourceFormatSaverText rfst; - return rfst.save(p_path, p_resource, p_flags); -} - -bool ResourceFormatSaverGDNativeScript::recognize(const RES &p_resource) const { - return p_resource->cast_to<GDNativeScript>() != NULL; -} - -void ResourceFormatSaverGDNativeScript::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const { - if (p_resource->cast_to<GDNativeScript>()) { - p_extensions->push_back("gdn"); + Map<StringName, native_raw_call_cb>::Element *E = GDNativeCallRegistry::singleton->native_raw_calls.find(p_raw_call_type); + if (!E) { + ERR_PRINT((String("No handler for native raw call type \"" + p_raw_call_type) + "\" found").utf8().get_data()); + return; } + + String procedure_name = p_procedure_name; + E->get()(native_handle, (godot_string *)&procedure_name, data, num_args, args, r_return); } |