/*************************************************************************/ /* nativescript.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* http://www.godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "nativescript.h" #include "modules/gdnative/godot/gdnative.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 ////// Script stuff void NativeScript::_bind_methods() { ClassDB::bind_method(D_METHOD("set_class_name", "class_name:String"), &NativeScript::set_class_name); ClassDB::bind_method(D_METHOD("get_class_name:String"), &NativeScript::get_class_name); ClassDB::bind_method(D_METHOD("set_library", "library:GDNativeLibrary"), &NativeScript::set_library); ClassDB::bind_method(D_METHOD("get_library:GDNativeLibrary"), &NativeScript::get_library); 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"); ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &NativeScript::_new, MethodInfo(Variant::OBJECT, "new")); } #define NSL NativeScriptLanguage::get_singleton() #ifdef TOOLS_ENABLED void NativeScript::_update_placeholder(PlaceHolderScriptInstance *p_placeholder) { NativeScriptDesc *script_data = get_script_desc(); ERR_FAIL_COND(!script_data); List info; Map values; for (Map::Element *E = script_data->properties.front(); E; E = E->next()) { PropertyInfo p = E->get().info; p.name = String(E->key()); info.push_back(p); values[p.name] = E->get().default_value; } p_placeholder->update(info, values); } void NativeScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) { placeholders.erase(p_placeholder); } #endif void NativeScript::set_class_name(String p_class_name) { class_name = p_class_name; } String NativeScript::get_class_name() const { return class_name; } void NativeScript::set_library(Ref p_library) { if (!library.is_null()) { WARN_PRINT("library on NativeScript already set. Do nothing."); return; } library = p_library; // See if this library was "registered" already. lib_path = library->get_active_library_path(); Map >::Element *E = NSL->library_gdnatives.find(lib_path); if (!E) { Ref gdn; gdn.instance(); gdn->set_library(library); // TODO(karroffel): check the return value? gdn->initialize(); NSL->library_gdnatives.insert(lib_path, gdn); NSL->library_classes.insert(lib_path, Map()); if (!NSL->library_script_users.has(lib_path)) NSL->library_script_users.insert(lib_path, Set()); NSL->library_script_users[lib_path].insert(this); void *args[1] = { (void *)&lib_path }; // here the library registers all the classes and stuff. gdn->call_native_raw(NSL->_init_call_type, NSL->_init_call_name, NULL, 1, args, NULL); } else { // already initialized. Nice. } } Ref NativeScript::get_library() const { return library; } bool NativeScript::can_instance() const { NativeScriptDesc *script_data = get_script_desc(); #ifdef TOOLS_ENABLED return script_data || (!is_tool() && !ScriptServer::is_scripting_enabled()); #else return script_data; #endif } // TODO(karroffel): implement this Ref