From 7878329e6fc828dc7ca0a8300ebdbaa0674a769c Mon Sep 17 00:00:00 2001 From: Karroffel Date: Sun, 3 Sep 2017 12:40:41 +0200 Subject: merged gdnative and nativescript module --- modules/gdnative/nativescript/nativescript.cpp | 1215 ++++++++++++++++++++++++ 1 file changed, 1215 insertions(+) create mode 100644 modules/gdnative/nativescript/nativescript.cpp (limited to 'modules/gdnative/nativescript/nativescript.cpp') diff --git a/modules/gdnative/nativescript/nativescript.cpp b/modules/gdnative/nativescript/nativescript.cpp new file mode 100644 index 0000000000..724b8390da --- /dev/null +++ b/modules/gdnative/nativescript/nativescript.cpp @@ -0,0 +1,1215 @@ +/*************************************************************************/ +/* nativescript.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://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 "gdnative/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" + +#ifndef NO_THREADS +#include "os/thread.h" +#endif + +#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"), &NativeScript::set_class_name); + ClassDB::bind_method(D_METHOD("get_class_name"), &NativeScript::get_class_name); + + ClassDB::bind_method(D_METHOD("set_library", "library"), &NativeScript::set_library); + ClassDB::bind_method(D_METHOD("get_library"), &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; + get_script_property_list(&info); + Map values; + for (List::Element *E = info.front(); E; E = E->next()) { + Variant value; + get_property_default_value(E->get().name, value); + values[E->get().name] = 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; + lib_path = library->get_active_library_path(); + +#ifndef NO_THREADS + if (Thread::get_caller_id() != Thread::get_main_id()) { + NSL->defer_init_library(p_library, this); + } else +#endif + { + NSL->init_library(p_library); + NSL->register_script(this); + } +} + +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