diff options
Diffstat (limited to 'modules/gdnative/nativescript')
-rw-r--r-- | modules/gdnative/nativescript/SCsub | 10 | ||||
-rw-r--r-- | modules/gdnative/nativescript/api_generator.cpp | 487 | ||||
-rw-r--r-- | modules/gdnative/nativescript/api_generator.h | 38 | ||||
-rw-r--r-- | modules/gdnative/nativescript/godot_nativescript.cpp | 205 | ||||
-rw-r--r-- | modules/gdnative/nativescript/nativescript.cpp | 1215 | ||||
-rw-r--r-- | modules/gdnative/nativescript/nativescript.h | 325 | ||||
-rw-r--r-- | modules/gdnative/nativescript/register_types.cpp | 118 | ||||
-rw-r--r-- | modules/gdnative/nativescript/register_types.h | 31 |
8 files changed, 2429 insertions, 0 deletions
diff --git a/modules/gdnative/nativescript/SCsub b/modules/gdnative/nativescript/SCsub new file mode 100644 index 0000000000..e980e40e8e --- /dev/null +++ b/modules/gdnative/nativescript/SCsub @@ -0,0 +1,10 @@ +#!/usr/bin/env python + +Import('env') + +mod_env = env.Clone() +mod_env.add_source_files(env.modules_sources, "*.cpp") +mod_env.Append(CPPPATH='#modules/gdnative') +mod_env.Append(CPPFLAGS=['-DGDAPI_BUILT_IN']) + +Export('mod_env') diff --git a/modules/gdnative/nativescript/api_generator.cpp b/modules/gdnative/nativescript/api_generator.cpp new file mode 100644 index 0000000000..fdd5a2ea19 --- /dev/null +++ b/modules/gdnative/nativescript/api_generator.cpp @@ -0,0 +1,487 @@ +/*************************************************************************/ +/* api_generator.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 "api_generator.h" + +#ifdef TOOLS_ENABLED + +#include "class_db.h" +#include "core/global_constants.h" +#include "core/pair.h" +#include "core/project_settings.h" +#include "os/file_access.h" + +// helper stuff + +static Error save_file(const String &p_path, const List<String> &p_content) { + + FileAccessRef file = FileAccess::open(p_path, FileAccess::WRITE); + + ERR_FAIL_COND_V(!file, ERR_FILE_CANT_WRITE); + + for (const List<String>::Element *e = p_content.front(); e != NULL; e = e->next()) { + file->store_string(e->get()); + } + + file->close(); + + return OK; +} + +// helper stuff end + +struct MethodAPI { + String method_name; + String return_type; + + List<String> argument_types; + List<String> argument_names; + + Map<int, Variant> default_arguments; + + int argument_count; + bool has_varargs; + bool is_editor; + bool is_noscript; + bool is_const; + bool is_reverse; + bool is_virtual; + bool is_from_script; +}; + +struct PropertyAPI { + String name; + String getter; + String setter; + String type; +}; + +struct ConstantAPI { + String constant_name; + int constant_value; +}; + +struct SignalAPI { + String name; + List<String> argument_types; + List<String> argument_names; + Map<int, Variant> default_arguments; +}; + +struct EnumAPI { + String name; + List<Pair<int, String> > values; +}; + +struct ClassAPI { + String class_name; + String super_class_name; + + ClassDB::APIType api_type; + + bool is_singleton; + bool is_instanciable; + // @Unclear + bool is_creatable; + bool is_reference; + + List<MethodAPI> methods; + List<PropertyAPI> properties; + List<ConstantAPI> constants; + List<SignalAPI> signals_; + List<EnumAPI> enums; +}; + +static String get_type_name(const PropertyInfo &info) { + if (info.type == Variant::INT && (info.usage & PROPERTY_USAGE_CLASS_IS_ENUM)) { + return String("enum.") + String(info.class_name).replace(".", "::"); + } + if (info.class_name != StringName()) { + return info.class_name; + } + if (info.hint == PROPERTY_HINT_RESOURCE_TYPE) { + return info.hint_string; + } + if (info.type == Variant::NIL && (info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) { + return "Variant"; + } + if (info.type == Variant::NIL) { + return "void"; + } + return Variant::get_type_name(info.type); +} + +/* + * Reads the entire Godot API to a list + */ +List<ClassAPI> generate_c_api_classes() { + + List<ClassAPI> api; + + List<StringName> classes; + ClassDB::get_class_list(&classes); + + // Register global constants as a fake GlobalConstants singleton class + { + ClassAPI global_constants_api; + global_constants_api.class_name = L"GlobalConstants"; + global_constants_api.api_type = ClassDB::API_CORE; + global_constants_api.is_singleton = true; + global_constants_api.is_instanciable = false; + const int constants_count = GlobalConstants::get_global_constant_count(); + for (int i = 0; i < constants_count; ++i) { + ConstantAPI constant_api; + constant_api.constant_name = GlobalConstants::get_global_constant_name(i); + constant_api.constant_value = GlobalConstants::get_global_constant_value(i); + global_constants_api.constants.push_back(constant_api); + } + api.push_back(global_constants_api); + } + + for (List<StringName>::Element *e = classes.front(); e != NULL; e = e->next()) { + StringName class_name = e->get(); + + ClassAPI class_api; + class_api.api_type = ClassDB::get_api_type(e->get()); + class_api.class_name = class_name; + class_api.super_class_name = ClassDB::get_parent_class(class_name); + { + String name = class_name; + if (name.begins_with("_")) { + name.remove(0); + } + class_api.is_singleton = ProjectSettings::get_singleton()->has_singleton(name); + } + class_api.is_instanciable = !class_api.is_singleton && ClassDB::can_instance(class_name); + + { + List<StringName> inheriters; + ClassDB::get_inheriters_from_class("Reference", &inheriters); + bool is_reference = !!inheriters.find(class_name); + // @Unclear + class_api.is_reference = !class_api.is_singleton && is_reference; + } + + // constants + { + List<String> constant; + ClassDB::get_integer_constant_list(class_name, &constant, true); + for (List<String>::Element *c = constant.front(); c != NULL; c = c->next()) { + ConstantAPI constant_api; + constant_api.constant_name = c->get(); + constant_api.constant_value = ClassDB::get_integer_constant(class_name, c->get()); + + class_api.constants.push_back(constant_api); + } + } + + // signals + { + List<MethodInfo> signals_; + ClassDB::get_signal_list(class_name, &signals_, true); + + for (int i = 0; i < signals_.size(); i++) { + SignalAPI signal; + + MethodInfo method_info = signals_[i]; + signal.name = method_info.name; + + for (int j = 0; j < method_info.arguments.size(); j++) { + PropertyInfo argument = method_info.arguments[j]; + String type; + String name = argument.name; + + if (argument.name.find(":") != -1) { + type = argument.name.get_slice(":", 1); + name = argument.name.get_slice(":", 0); + } else { + type = get_type_name(argument); + } + + signal.argument_names.push_back(name); + signal.argument_types.push_back(type); + } + + Vector<Variant> default_arguments = method_info.default_arguments; + + int default_start = signal.argument_names.size() - default_arguments.size(); + + for (int j = 0; j < default_arguments.size(); j++) { + signal.default_arguments[default_start + j] = default_arguments[j]; + } + + class_api.signals_.push_back(signal); + } + } + + //properties + { + List<PropertyInfo> properties; + ClassDB::get_property_list(class_name, &properties, true); + + for (List<PropertyInfo>::Element *p = properties.front(); p != NULL; p = p->next()) { + PropertyAPI property_api; + + property_api.name = p->get().name; + property_api.getter = ClassDB::get_property_getter(class_name, p->get().name); + property_api.setter = ClassDB::get_property_setter(class_name, p->get().name); + + if (p->get().name.find(":") != -1) { + property_api.type = p->get().name.get_slice(":", 1); + property_api.name = p->get().name.get_slice(":", 0); + } else { + property_api.type = get_type_name(p->get()); + } + + if (!property_api.setter.empty() || !property_api.getter.empty()) { + class_api.properties.push_back(property_api); + } + } + } + + //methods + { + List<MethodInfo> methods; + ClassDB::get_method_list(class_name, &methods, true); + + for (List<MethodInfo>::Element *m = methods.front(); m != NULL; m = m->next()) { + MethodAPI method_api; + MethodBind *method_bind = ClassDB::get_method(class_name, m->get().name); + MethodInfo &method_info = m->get(); + + //method name + method_api.method_name = m->get().name; + //method return type + if (method_api.method_name.find(":") != -1) { + method_api.return_type = method_api.method_name.get_slice(":", 1); + method_api.method_name = method_api.method_name.get_slice(":", 0); + } else { + method_api.return_type = get_type_name(m->get().return_val); + } + + method_api.argument_count = method_info.arguments.size(); + method_api.has_varargs = method_bind && method_bind->is_vararg(); + + // Method flags + if (method_info.flags) { + const uint32_t flags = method_info.flags; + method_api.is_editor = flags & METHOD_FLAG_EDITOR; + method_api.is_noscript = flags & METHOD_FLAG_NOSCRIPT; + method_api.is_const = flags & METHOD_FLAG_CONST; + method_api.is_reverse = flags & METHOD_FLAG_REVERSE; + method_api.is_virtual = flags & METHOD_FLAG_VIRTUAL; + method_api.is_from_script = flags & METHOD_FLAG_FROM_SCRIPT; + } + + method_api.is_virtual = method_api.is_virtual || method_api.method_name[0] == '_'; + + // method argument name and type + + for (int i = 0; i < method_api.argument_count; i++) { + String arg_name; + String arg_type; + PropertyInfo arg_info = method_info.arguments[i]; + + arg_name = arg_info.name; + + if (arg_info.name.find(":") != -1) { + arg_type = arg_info.name.get_slice(":", 1); + arg_name = arg_info.name.get_slice(":", 0); + } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) { + arg_type = arg_info.hint_string; + } else if (arg_info.type == Variant::NIL) { + arg_type = "Variant"; + } else { + arg_type = Variant::get_type_name(arg_info.type); + } + + method_api.argument_names.push_back(arg_name); + method_api.argument_types.push_back(arg_type); + + if (method_bind && method_bind->has_default_argument(i)) { + method_api.default_arguments[i] = method_bind->get_default_argument(i); + } + } + + class_api.methods.push_back(method_api); + } + } + + // enums + { + List<EnumAPI> enums; + List<StringName> enum_names; + ClassDB::get_enum_list(class_name, &enum_names, true); + for (List<StringName>::Element *E = enum_names.front(); E; E = E->next()) { + List<StringName> value_names; + EnumAPI enum_api; + enum_api.name = E->get(); + ClassDB::get_enum_constants(class_name, E->get(), &value_names, true); + for (List<StringName>::Element *val_e = value_names.front(); val_e; val_e = val_e->next()) { + int int_val = ClassDB::get_integer_constant(class_name, val_e->get(), NULL); + enum_api.values.push_back(Pair<int, String>(int_val, val_e->get())); + } + enum_api.values.sort_custom<PairSort<int, String> >(); + class_api.enums.push_back(enum_api); + } + } + + api.push_back(class_api); + } + + return api; +} + +/* + * Generates the JSON source from the API in p_api + */ +static List<String> generate_c_api_json(const List<ClassAPI> &p_api) { + + // I'm sorry for the \t mess + + List<String> source; + + source.push_back("[\n"); + + for (const List<ClassAPI>::Element *c = p_api.front(); c != NULL; c = c->next()) { + ClassAPI api = c->get(); + + source.push_back("\t{\n"); + + source.push_back("\t\t\"name\": \"" + api.class_name + "\",\n"); + source.push_back("\t\t\"base_class\": \"" + api.super_class_name + "\",\n"); + source.push_back(String("\t\t\"api_type\": \"") + (api.api_type == ClassDB::API_CORE ? "core" : (api.api_type == ClassDB::API_EDITOR ? "tools" : "none")) + "\",\n"); + source.push_back(String("\t\t\"singleton\": ") + (api.is_singleton ? "true" : "false") + ",\n"); + source.push_back(String("\t\t\"instanciable\": ") + (api.is_instanciable ? "true" : "false") + ",\n"); + source.push_back(String("\t\t\"is_reference\": ") + (api.is_reference ? "true" : "false") + ",\n"); + // @Unclear + // source.push_back(String("\t\t\"createable\": ") + (api.is_creatable ? "true" : "false") + ",\n"); + + source.push_back("\t\t\"constants\": {\n"); + for (List<ConstantAPI>::Element *e = api.constants.front(); e; e = e->next()) { + source.push_back("\t\t\t\"" + e->get().constant_name + "\": " + String::num_int64(e->get().constant_value) + (e->next() ? "," : "") + "\n"); + } + source.push_back("\t\t},\n"); + + source.push_back("\t\t\"properties\": [\n"); + for (List<PropertyAPI>::Element *e = api.properties.front(); e; e = e->next()) { + source.push_back("\t\t\t{\n"); + source.push_back("\t\t\t\t\"name\": \"" + e->get().name + "\",\n"); + source.push_back("\t\t\t\t\"type\": \"" + e->get().type + "\",\n"); + source.push_back("\t\t\t\t\"getter\": \"" + e->get().getter + "\",\n"); + source.push_back("\t\t\t\t\"setter\": \"" + e->get().setter + "\"\n"); + source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n"); + } + source.push_back("\t\t],\n"); + + source.push_back("\t\t\"signals\": [\n"); + for (List<SignalAPI>::Element *e = api.signals_.front(); e; e = e->next()) { + source.push_back("\t\t\t{\n"); + source.push_back("\t\t\t\t\"name\": \"" + e->get().name + "\",\n"); + source.push_back("\t\t\t\t\"arguments\": [\n"); + for (int i = 0; i < e->get().argument_names.size(); i++) { + source.push_back("\t\t\t\t\t{\n"); + source.push_back("\t\t\t\t\t\t\"name\": \"" + e->get().argument_names[i] + "\",\n"); + source.push_back("\t\t\t\t\t\t\"type\": \"" + e->get().argument_types[i] + "\",\n"); + source.push_back("\t\t\t\t\t\t\"default_value\": \"" + (e->get().default_arguments.has(i) ? (String)e->get().default_arguments[i] : "") + "\"\n"); + source.push_back(String("\t\t\t\t\t}") + ((i < e->get().argument_names.size() - 1) ? "," : "") + "\n"); + } + source.push_back("\t\t\t\t]\n"); + source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n"); + } + source.push_back("\t\t],\n"); + + source.push_back("\t\t\"methods\": [\n"); + for (List<MethodAPI>::Element *e = api.methods.front(); e; e = e->next()) { + source.push_back("\t\t\t{\n"); + source.push_back("\t\t\t\t\"name\": \"" + e->get().method_name + "\",\n"); + source.push_back("\t\t\t\t\"return_type\": \"" + e->get().return_type + "\",\n"); + source.push_back(String("\t\t\t\t\"is_editor\": ") + (e->get().is_editor ? "true" : "false") + ",\n"); + source.push_back(String("\t\t\t\t\"is_noscript\": ") + (e->get().is_noscript ? "true" : "false") + ",\n"); + source.push_back(String("\t\t\t\t\"is_const\": ") + (e->get().is_const ? "true" : "false") + ",\n"); + source.push_back(String("\t\t\t\t\"is_reverse\": ") + (e->get().is_reverse ? "true" : "false") + ",\n"); + source.push_back(String("\t\t\t\t\"is_virtual\": ") + (e->get().is_virtual ? "true" : "false") + ",\n"); + source.push_back(String("\t\t\t\t\"has_varargs\": ") + (e->get().has_varargs ? "true" : "false") + ",\n"); + source.push_back(String("\t\t\t\t\"is_from_script\": ") + (e->get().is_from_script ? "true" : "false") + ",\n"); + source.push_back("\t\t\t\t\"arguments\": [\n"); + for (int i = 0; i < e->get().argument_names.size(); i++) { + source.push_back("\t\t\t\t\t{\n"); + source.push_back("\t\t\t\t\t\t\"name\": \"" + e->get().argument_names[i] + "\",\n"); + source.push_back("\t\t\t\t\t\t\"type\": \"" + e->get().argument_types[i] + "\",\n"); + source.push_back(String("\t\t\t\t\t\t\"has_default_value\": ") + (e->get().default_arguments.has(i) ? "true" : "false") + ",\n"); + source.push_back("\t\t\t\t\t\t\"default_value\": \"" + (e->get().default_arguments.has(i) ? (String)e->get().default_arguments[i] : "") + "\"\n"); + source.push_back(String("\t\t\t\t\t}") + ((i < e->get().argument_names.size() - 1) ? "," : "") + "\n"); + } + source.push_back("\t\t\t\t]\n"); + source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n"); + } + source.push_back("\t\t],\n"); + + source.push_back("\t\t\"enums\": [\n"); + for (List<EnumAPI>::Element *e = api.enums.front(); e; e = e->next()) { + source.push_back("\t\t\t{\n"); + source.push_back("\t\t\t\t\"name\": \"" + e->get().name + "\",\n"); + source.push_back("\t\t\t\t\"values\": {\n"); + for (List<Pair<int, String> >::Element *val_e = e->get().values.front(); val_e; val_e = val_e->next()) { + source.push_back("\t\t\t\t\t\"" + val_e->get().second + "\": " + itos(val_e->get().first)); + source.push_back(String((val_e->next() ? "," : "")) + "\n"); + } + source.push_back("\t\t\t\t}\n"); + source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n"); + } + source.push_back("\t\t]\n"); + + source.push_back(String("\t}") + (c->next() ? "," : "") + "\n"); + } + source.push_back("]"); + + return source; +} + +// + +#endif + +/* + * Saves the whole Godot API to a JSON file located at + * p_path + */ +Error generate_c_api(const String &p_path) { + +#ifndef TOOLS_ENABLED + return ERR_BUG; +#else + + List<ClassAPI> api = generate_c_api_classes(); + + List<String> json_source = generate_c_api_json(api); + + return save_file(p_path, json_source); +#endif +} diff --git a/modules/gdnative/nativescript/api_generator.h b/modules/gdnative/nativescript/api_generator.h new file mode 100644 index 0000000000..56c2d786e6 --- /dev/null +++ b/modules/gdnative/nativescript/api_generator.h @@ -0,0 +1,38 @@ +/*************************************************************************/ +/* api_generator.h */ +/*************************************************************************/ +/* 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. */ +/*************************************************************************/ +#ifndef API_GENERATOR_H +#define API_GENERATOR_H + +#include "core/ustring.h" +#include "typedefs.h" + +Error generate_c_api(const String &p_path); + +#endif // API_GENERATOR_H diff --git a/modules/gdnative/nativescript/godot_nativescript.cpp b/modules/gdnative/nativescript/godot_nativescript.cpp new file mode 100644 index 0000000000..61ac13b796 --- /dev/null +++ b/modules/gdnative/nativescript/godot_nativescript.cpp @@ -0,0 +1,205 @@ +/*************************************************************************/ +/* godot_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/godot_nativescript.h" + +#include "class_db.h" +#include "error_macros.h" +#include "gdnative/gdnative.h" +#include "global_constants.h" +#include "project_settings.h" +#include "variant.h" + +#include "nativescript.h" + +#ifdef __cplusplus +extern "C" { +#endif + +extern "C" void _native_script_hook() { +} + +#define NSL NativeScriptLanguage::get_singleton() + +// Script API + +void GDAPI godot_nativescript_register_class(void *p_gdnative_handle, const char *p_name, const char *p_base, godot_instance_create_func p_create_func, godot_instance_destroy_func p_destroy_func) { + + String *s = (String *)p_gdnative_handle; + + Map<StringName, NativeScriptDesc> *classes = &NSL->library_classes[*s]; + + NativeScriptDesc desc; + + desc.create_func = p_create_func; + desc.destroy_func = p_destroy_func; + desc.is_tool = false; + + desc.base = p_base; + + if (classes->has(p_base)) { + desc.base_data = &(*classes)[p_base]; + desc.base_native_type = desc.base_data->base_native_type; + } else { + desc.base_data = NULL; + desc.base_native_type = p_base; + } + + classes->insert(p_name, desc); +} + +void GDAPI godot_nativescript_register_tool_class(void *p_gdnative_handle, const char *p_name, const char *p_base, godot_instance_create_func p_create_func, godot_instance_destroy_func p_destroy_func) { + + String *s = (String *)p_gdnative_handle; + + Map<StringName, NativeScriptDesc> *classes = &NSL->library_classes[*s]; + + NativeScriptDesc desc; + + desc.create_func = p_create_func; + desc.destroy_func = p_destroy_func; + desc.is_tool = true; + desc.base = p_base; + + if (classes->has(p_base)) { + desc.base_data = &(*classes)[p_base]; + desc.base_native_type = desc.base_data->base_native_type; + } else { + desc.base_data = NULL; + desc.base_native_type = p_base; + } + + classes->insert(p_name, desc); +} + +void GDAPI godot_nativescript_register_method(void *p_gdnative_handle, const char *p_name, const char *p_function_name, godot_method_attributes p_attr, godot_instance_method p_method) { + + String *s = (String *)p_gdnative_handle; + + Map<StringName, NativeScriptDesc>::Element *E = NSL->library_classes[*s].find(p_name); + + if (!E) { + ERR_EXPLAIN("Attempt to register method on non-existant class!"); + ERR_FAIL(); + } + + NativeScriptDesc::Method method; + method.method = p_method; + method.rpc_mode = p_attr.rpc_type; + method.info = MethodInfo(p_function_name); + + E->get().methods.insert(p_function_name, method); +} + +void GDAPI godot_nativescript_register_property(void *p_gdnative_handle, const char *p_name, const char *p_path, godot_property_attributes *p_attr, godot_property_set_func p_set_func, godot_property_get_func p_get_func) { + + String *s = (String *)p_gdnative_handle; + + Map<StringName, NativeScriptDesc>::Element *E = NSL->library_classes[*s].find(p_name); + + if (!E) { + ERR_EXPLAIN("Attempt to register method on non-existant class!"); + ERR_FAIL(); + } + + NativeScriptDesc::Property property; + property.default_value = *(Variant *)&p_attr->default_value; + property.getter = p_get_func; + property.rset_mode = p_attr->rset_type; + property.setter = p_set_func; + property.info = PropertyInfo((Variant::Type)p_attr->type, + p_path, + (PropertyHint)p_attr->hint, + *(String *)&p_attr->hint_string, + (PropertyUsageFlags)p_attr->usage); + + E->get().properties.insert(p_path, property); +} + +void GDAPI godot_nativescript_register_signal(void *p_gdnative_handle, const char *p_name, const godot_signal *p_signal) { + + String *s = (String *)p_gdnative_handle; + + Map<StringName, NativeScriptDesc>::Element *E = NSL->library_classes[*s].find(p_name); + + if (!E) { + ERR_EXPLAIN("Attempt to register method on non-existant class!"); + ERR_FAIL(); + } + + List<PropertyInfo> args; + Vector<Variant> default_args; + + for (int i = 0; i < p_signal->num_args; i++) { + PropertyInfo info; + + godot_signal_argument arg = p_signal->args[i]; + + info.hint = (PropertyHint)arg.hint; + info.hint_string = *(String *)&arg.hint_string; + info.name = *(String *)&arg.name; + info.type = (Variant::Type)arg.type; + info.usage = (PropertyUsageFlags)arg.usage; + + args.push_back(info); + } + + 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_args.push_back(*v); + } + + MethodInfo method_info; + method_info.name = *(String *)&p_signal->name; + method_info.arguments = args; + method_info.default_arguments = default_args; + + NativeScriptDesc::Signal signal; + signal.signal = method_info; + + E->get().signals_.insert(*(String *)&p_signal->name, signal); +} + +void GDAPI *godot_nativescript_get_userdata(godot_object *p_instance) { + Object *instance = (Object *)p_instance; + if (!instance) + return NULL; + if (instance->get_script_instance() && instance->get_script_instance()->get_language() == NativeScriptLanguage::get_singleton()) { + return ((NativeScriptInstance *)instance->get_script_instance())->userdata; + } + return NULL; +} + +#ifdef __cplusplus +} +#endif 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<PropertyInfo> info; + get_script_property_list(&info); + Map<StringName, Variant> values; + for (List<PropertyInfo>::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<GDNativeLibrary> 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<GDNativeLibrary> 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<Script> NativeScript::get_base_script() const { + NativeScriptDesc *script_data = get_script_desc(); + + if (!script_data) + return Ref<Script>(); + + Ref<NativeScript> ns = Ref<NativeScript>(NSL->create_script()); + ns->set_class_name(script_data->base); + ns->set_library(get_library()); + return ns; +} + +StringName NativeScript::get_instance_base_type() const { + NativeScriptDesc *script_data = get_script_desc(); + + if (!script_data) + return ""; + + return script_data->base_native_type; +} + +ScriptInstance *NativeScript::instance_create(Object *p_this) { + + NativeScriptDesc *script_data = get_script_desc(); + + if (!script_data) { + return NULL; + } + +#ifdef TOOLS_ENABLED + if (!ScriptServer::is_scripting_enabled() && !is_tool()) { + // placeholder for nodes. For tools we want the rool thing. + + PlaceHolderScriptInstance *sins = memnew(PlaceHolderScriptInstance(NSL, Ref<Script>(this), p_this)); + placeholders.insert(sins); + + if (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 + + NativeScriptInstance *nsi = memnew(NativeScriptInstance); + + nsi->owner = p_this; + nsi->script = Ref<NativeScript>(this); + +#ifndef TOOLS_ENABLED + if (!ScriptServer::is_scripting_enabled()) { + nsi->userdata = NULL; + } else { + nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data); + } +#else + nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data); +#endif + +#ifndef NO_THREADS + owners_lock->lock(); +#endif + + instance_owners.insert(p_this); + +#ifndef NO_THREADS + owners_lock->unlock(); +#endif + + return nsi; +} + +bool NativeScript::instance_has(const Object *p_this) const { + return instance_owners.has((Object *)p_this); +} + +bool NativeScript::has_source_code() const { + return false; +} + +String NativeScript::get_source_code() const { + return ""; +} + +void NativeScript::set_source_code(const String &p_code) { +} + +Error NativeScript::reload(bool p_keep_state) { + return FAILED; +} + +bool NativeScript::has_method(const StringName &p_method) const { + NativeScriptDesc *script_data = get_script_desc(); + + while (script_data) { + if (script_data->methods.has(p_method)) + return true; + + script_data = script_data->base_data; + } + return false; +} + +MethodInfo NativeScript::get_method_info(const StringName &p_method) const { + NativeScriptDesc *script_data = get_script_desc(); + + if (!script_data) + return MethodInfo(); + + while (script_data) { + Map<StringName, NativeScriptDesc::Method>::Element *M = script_data->methods.find(p_method); + + if (M) + return M->get().info; + + script_data = script_data->base_data; + } + return MethodInfo(); +} + +bool NativeScript::is_tool() const { + NativeScriptDesc *script_data = get_script_desc(); + + if (script_data) + return script_data->is_tool; + + return false; +} + +String NativeScript::get_node_type() const { + return ""; // NOTE(karroffel): uhm? +} + +ScriptLanguage *NativeScript::get_language() const { + return NativeScriptLanguage::get_singleton(); +} + +bool NativeScript::has_script_signal(const StringName &p_signal) const { + NativeScriptDesc *script_data = get_script_desc(); + + while (script_data) { + if (script_data->signals_.has(p_signal)) + return true; + script_data = script_data->base_data; + } + return false; +} + +void NativeScript::get_script_signal_list(List<MethodInfo> *r_signals) const { + NativeScriptDesc *script_data = get_script_desc(); + + if (!script_data) + return; + + Set<MethodInfo> signals_; + + while (script_data) { + + for (Map<StringName, NativeScriptDesc::Signal>::Element *S = script_data->signals_.front(); S; S = S->next()) { + signals_.insert(S->get().signal); + } + + script_data = script_data->base_data; + } + + for (Set<MethodInfo>::Element *E = signals_.front(); E; E = E->next()) { + r_signals->push_back(E->get()); + } +} + +bool NativeScript::get_property_default_value(const StringName &p_property, Variant &r_value) const { + NativeScriptDesc *script_data = get_script_desc(); + + Map<StringName, NativeScriptDesc::Property>::Element *P = NULL; + while (!P && script_data) { + P = script_data->properties.find(p_property); + script_data = script_data->base_data; + } + if (!P) + return false; + + r_value = P->get().default_value; + return true; +} + +void NativeScript::update_exports() { +} + +void NativeScript::get_script_method_list(List<MethodInfo> *p_list) const { + NativeScriptDesc *script_data = get_script_desc(); + + if (!script_data) + return; + + Set<MethodInfo> methods; + + while (script_data) { + + for (Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.front(); E; E = E->next()) { + methods.insert(E->get().info); + } + + script_data = script_data->base_data; + } + + for (Set<MethodInfo>::Element *E = methods.front(); E; E = E->next()) { + p_list->push_back(E->get()); + } +} + +void NativeScript::get_script_property_list(List<PropertyInfo> *p_list) const { + NativeScriptDesc *script_data = get_script_desc(); + + if (!script_data) + return; + + Set<PropertyInfo> properties; + + while (script_data) { + + for (Map<StringName, NativeScriptDesc::Property>::Element *E = script_data->properties.front(); E; E = E->next()) { + properties.insert(E->get().info); + } + + script_data = script_data->base_data; + } + + for (Set<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) { + p_list->push_back(E->get()); + } +} + +Variant NativeScript::_new(const Variant **p_args, int p_argcount, Variant::CallError &r_error) { + + if (lib_path.empty() || class_name.empty() || library.is_null()) { + r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL; + return Variant(); + } + + NativeScriptDesc *script_data = get_script_desc(); + + if (!script_data) { + r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL; + return Variant(); + } + + r_error.error = Variant::CallError::CALL_OK; + + REF ref; + Object *owner = NULL; + + if (!(script_data->base_native_type == "")) { + owner = ClassDB::instance(script_data->base_native_type); + } else { + owner = memnew(Reference); + } + + Reference *r = Object::cast_to<Reference>(owner); + if (r) { + ref = REF(r); + } + + NativeScriptInstance *instance = (NativeScriptInstance *)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; + } +} + +// TODO(karroffel): implement this +NativeScript::NativeScript() { + library = Ref<GDNative>(); + lib_path = ""; + class_name = ""; +#ifndef NO_THREADS + owners_lock = Mutex::create(); +#endif +} + +// TODO(karroffel): implement this +NativeScript::~NativeScript() { + NSL->unregister_script(this); + +#ifndef NO_THREADS + memdelete(owners_lock); +#endif +} + +////// ScriptInstance stuff + +#define GET_SCRIPT_DESC() script->get_script_desc() + +void NativeScriptInstance::_ml_call_reversed(NativeScriptDesc *script_data, const StringName &p_method, const Variant **p_args, int p_argcount) { + if (script_data->base_data) { + _ml_call_reversed(script_data->base_data, p_method, p_args, p_argcount); + } + + Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method); + if (E) { + godot_variant res = E->get().method.method((godot_object *)owner, E->get().method.method_data, userdata, p_argcount, (godot_variant **)p_args); + godot_variant_destroy(&res); + } +} + +bool NativeScriptInstance::set(const StringName &p_name, const Variant &p_value) { + NativeScriptDesc *script_data = GET_SCRIPT_DESC(); + + while (script_data) { + Map<StringName, NativeScriptDesc::Property>::Element *P = script_data->properties.find(p_name); + if (P) { + P->get().setter.set_func((godot_object *)owner, + P->get().setter.method_data, + userdata, + (godot_variant *)&p_value); + return true; + } + + Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_set"); + if (E) { + Variant name = p_name; + const Variant *args[2] = { &name, &p_value }; + + E->get().method.method((godot_object *)owner, + E->get().method.method_data, + userdata, + 2, + (godot_variant **)args); + return true; + } + + script_data = script_data->base_data; + } + return false; +} +bool NativeScriptInstance::get(const StringName &p_name, Variant &r_ret) const { + NativeScriptDesc *script_data = GET_SCRIPT_DESC(); + + while (script_data) { + Map<StringName, NativeScriptDesc::Property>::Element *P = script_data->properties.find(p_name); + if (P) { + godot_variant value; + value = P->get().getter.get_func((godot_object *)owner, + P->get().getter.method_data, + userdata); + r_ret = *(Variant *)&value; + godot_variant_destroy(&value); + return true; + } + + Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_get"); + if (E) { + Variant name = p_name; + const Variant *args[1] = { &name }; + + godot_variant result; + result = E->get().method.method((godot_object *)owner, + E->get().method.method_data, + userdata, + 1, + (godot_variant **)args); + r_ret = *(Variant *)&result; + godot_variant_destroy(&result); + if (r_ret.get_type() == Variant::NIL) { + return false; + } + return true; + } + + script_data = script_data->base_data; + } + return false; +} + +void NativeScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const { + script->get_script_property_list(p_properties); + + NativeScriptDesc *script_data = GET_SCRIPT_DESC(); + + while (script_data) { + + Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_get_property_list"); + if (E) { + + godot_variant result; + result = E->get().method.method((godot_object *)owner, + E->get().method.method_data, + userdata, + 0, + NULL); + Variant res = *(Variant *)&result; + godot_variant_destroy(&result); + + if (res.get_type() != Variant::ARRAY) { + ERR_EXPLAIN("_get_property_list must return an array of dictionaries"); + ERR_FAIL(); + } + + Array arr = res; + for (int i = 0; i < arr.size(); i++) { + Dictionary d = arr[i]; + + ERR_CONTINUE(!d.has("name")); + ERR_CONTINUE(!d.has("type")); + + PropertyInfo info; + + info.type = Variant::Type(d["type"].operator int64_t()); + ERR_CONTINUE(info.type < 0 || info.type >= Variant::VARIANT_MAX); + + info.name = d["name"]; + ERR_CONTINUE(info.name == ""); + + if (d.has("hint")) { + info.hint = PropertyHint(d["hint"].operator int64_t()); + } + + if (d.has("hint_string")) { + info.hint_string = d["hint_string"]; + } + + if (d.has("usage")) { + info.usage = d["usage"]; + } + + p_properties->push_back(info); + } + } + + script_data = script_data->base_data; + } + return; +} + +Variant::Type NativeScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const { + + NativeScriptDesc *script_data = GET_SCRIPT_DESC(); + + while (script_data) { + + Map<StringName, NativeScriptDesc::Property>::Element *P = script_data->properties.find(p_name); + if (P) { + *r_is_valid = true; + return P->get().info.type; + } + + script_data = script_data->base_data; + } + return Variant::NIL; +} + +void NativeScriptInstance::get_method_list(List<MethodInfo> *p_list) const { + script->get_method_list(p_list); +} + +bool NativeScriptInstance::has_method(const StringName &p_method) const { + return script->has_method(p_method); +} + +Variant NativeScriptInstance::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) { + + NativeScriptDesc *script_data = GET_SCRIPT_DESC(); + + while (script_data) { + Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method); + if (E) { + godot_variant result; + result = E->get().method.method((godot_object *)owner, + E->get().method.method_data, + userdata, + p_argcount, + (godot_variant **)p_args); + Variant res = *(Variant *)&result; + godot_variant_destroy(&result); + r_error.error = Variant::CallError::CALL_OK; + return res; + } + + script_data = script_data->base_data; + } + + r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD; + return Variant(); +} + +void NativeScriptInstance::notification(int p_notification) { + Variant value = p_notification; + const Variant *args[1] = { &value }; + call_multilevel("_notification", args, 1); +} + +void NativeScriptInstance::refcount_incremented() { + Variant::CallError err; + call("_refcount_incremented", NULL, 0, err); + if (err.error != Variant::CallError::CALL_OK && err.error != Variant::CallError::CALL_ERROR_INVALID_METHOD) { + ERR_PRINT("Failed to invoke _refcount_incremented - should not happen"); + } +} + +bool NativeScriptInstance::refcount_decremented() { + Variant::CallError err; + Variant ret = call("_refcount_decremented", NULL, 0, err); + if (err.error != Variant::CallError::CALL_OK && err.error != Variant::CallError::CALL_ERROR_INVALID_METHOD) { + ERR_PRINT("Failed to invoke _refcount_decremented - should not happen"); + return true; // assume we can destroy the object + } + if (err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) { + // the method does not exist, default is true + return true; + } + return ret; +} + +Ref<Script> NativeScriptInstance::get_script() const { + return script; +} + +NativeScriptInstance::RPCMode NativeScriptInstance::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 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; + } + } + + script_data = script_data->base_data; + } + + return RPC_MODE_DISABLED; +} + +// TODO(karroffel): implement this +NativeScriptInstance::RPCMode NativeScriptInstance::get_rset_mode(const StringName &p_variable) const { + + NativeScriptDesc *script_data = GET_SCRIPT_DESC(); + + while (script_data) { + + Map<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 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; + } + } + + script_data = script_data->base_data; + } + + return RPC_MODE_DISABLED; +} + +ScriptLanguage *NativeScriptInstance::get_language() { + return NativeScriptLanguage::get_singleton(); +} + +void NativeScriptInstance::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) { + NativeScriptDesc *script_data = GET_SCRIPT_DESC(); + + while (script_data) { + Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method); + if (E) { + godot_variant res = E->get().method.method((godot_object *)owner, + E->get().method.method_data, + userdata, + p_argcount, + (godot_variant **)p_args); + godot_variant_destroy(&res); + } + script_data = script_data->base_data; + } +} + +void NativeScriptInstance::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) { + NativeScriptDesc *script_data = GET_SCRIPT_DESC(); + + if (script_data) { + _ml_call_reversed(script_data, p_method, p_args, p_argcount); + } +} + +NativeScriptInstance::~NativeScriptInstance() { + + NativeScriptDesc *script_data = GET_SCRIPT_DESC(); + + if (!script_data) + return; + + script_data->destroy_func.destroy_func((godot_object *)owner, script_data->destroy_func.method_data, userdata); + + if (owner) { + +#ifndef NO_THREADS + script->owners_lock->lock(); +#endif + + script->instance_owners.erase(owner); + +#ifndef NO_THREADS + script->owners_lock->unlock(); +#endif + } +} + +////// ScriptingLanguage stuff + +NativeScriptLanguage *NativeScriptLanguage::singleton; + +extern "C" void _native_script_hook(); +void NativeScriptLanguage::_hacky_api_anchor() { + _native_script_hook(); +} + +void NativeScriptLanguage::_unload_stuff() { + for (Map<String, Map<StringName, NativeScriptDesc> >::Element *L = library_classes.front(); L; L = L->next()) { + for (Map<StringName, NativeScriptDesc>::Element *C = L->get().front(); C; C = C->next()) { + + // free property stuff first + for (Map<StringName, NativeScriptDesc::Property>::Element *P = C->get().properties.front(); P; P = P->next()) { + if (P->get().getter.free_func) + P->get().getter.free_func(P->get().getter.method_data); + + if (P->get().setter.free_func) + P->get().setter.free_func(P->get().setter.method_data); + } + + // free method stuff + for (Map<StringName, NativeScriptDesc::Method>::Element *M = C->get().methods.front(); M; M = M->next()) { + if (M->get().method.free_func) + M->get().method.free_func(M->get().method.method_data); + } + + // free constructor/destructor + if (C->get().create_func.free_func) + C->get().create_func.free_func(C->get().create_func.method_data); + + if (C->get().destroy_func.free_func) + C->get().destroy_func.free_func(C->get().destroy_func.method_data); + } + } +} + +NativeScriptLanguage::NativeScriptLanguage() { + NativeScriptLanguage::singleton = this; +#ifndef NO_THREADS + mutex = Mutex::create(); +#endif +} + +// TODO(karroffel): implement this +NativeScriptLanguage::~NativeScriptLanguage() { + // _unload_stuff(); // NOTE(karroffel): This gets called in ::finish() + + for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) { + + L->get()->terminate(); + NSL->library_classes.clear(); + NSL->library_gdnatives.clear(); + NSL->library_script_users.clear(); + } + +#ifndef NO_THREADS + memdelete(mutex); +#endif +} + +String NativeScriptLanguage::get_name() const { + return "NativeScript"; +} + +void _add_reload_node() { +#ifdef TOOLS_ENABLED + NativeReloadNode *rn = memnew(NativeReloadNode); + EditorNode::get_singleton()->add_child(rn); +#endif +} + +// TODO(karroffel): implement this +void NativeScriptLanguage::init() { + +#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"); + + if (E && E->next()) { + if (generate_c_api(E->next()->get()) != OK) { + ERR_PRINT("Failed to generate C API\n"); + } + } +#endif + +#ifdef TOOLS_ENABLED + EditorNode::add_init_callback(&_add_reload_node); +#endif +} +String NativeScriptLanguage::get_type() const { + return "NativeScript"; +} +String NativeScriptLanguage::get_extension() const { + return "gdns"; +} +Error NativeScriptLanguage::execute_file(const String &p_path) { + return OK; // Qué? +} +void NativeScriptLanguage::finish() { + _unload_stuff(); +} +void NativeScriptLanguage::get_reserved_words(List<String> *p_words) const { +} +void NativeScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const { +} +void NativeScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const { +} + +// TODO(karroffel): implement this +Ref<Script> NativeScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const { + NativeScript *s = memnew(NativeScript); + s->set_class_name(p_class_name); + // TODO(karroffel): use p_base_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) const { + return false; +} + +Script *NativeScriptLanguage::create_script() const { + NativeScript *script = memnew(NativeScript); + return script; +} +bool NativeScriptLanguage::has_named_classes() const { + return true; +} +int NativeScriptLanguage::find_function(const String &p_function, const String &p_code) const { + return -1; +} +String NativeScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const { + return ""; +} +void NativeScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const { +} +void NativeScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) { +} + +// Debugging stuff here. Not used for now. +String NativeScriptLanguage::debug_get_error() const { + return ""; +} +int NativeScriptLanguage::debug_get_stack_level_count() const { + return -1; +} +int NativeScriptLanguage::debug_get_stack_level_line(int p_level) const { + return -1; +} +String NativeScriptLanguage::debug_get_stack_level_function(int p_level) const { + return ""; +} +String NativeScriptLanguage::debug_get_stack_level_source(int p_level) const { + return ""; +} +void NativeScriptLanguage::debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) { +} +void NativeScriptLanguage::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 NativeScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) { +} +String NativeScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) { + return ""; +} +// Debugging stuff end. + +void NativeScriptLanguage::reload_all_scripts() { +} + +void NativeScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) { +} +void NativeScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const { + p_extensions->push_back("gdns"); +} + +void NativeScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const { +} + +void NativeScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const { +} + +void NativeScriptLanguage::profiling_start() { +} + +void NativeScriptLanguage::profiling_stop() { +} + +int NativeScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) { + return 0; +} + +int NativeScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) { + return 0; +} + +#ifndef NO_THREADS +void NativeScriptLanguage::defer_init_library(Ref<GDNativeLibrary> lib, NativeScript *script) { + MutexLock lock(mutex); + libs_to_init.insert(lib); + scripts_to_register.insert(script); + has_objects_to_register = true; +} +#endif + +void NativeScriptLanguage::init_library(const Ref<GDNativeLibrary> &lib) { +#ifndef NO_THREADS + MutexLock lock(mutex); +#endif + // See if this library was "registered" already. + const String &lib_path = lib->get_active_library_path(); + ERR_EXPLAIN(lib->get_name() + " does not have a library for the current platform"); + ERR_FAIL_COND(lib_path.length() == 0); + Map<String, Ref<GDNative> >::Element *E = library_gdnatives.find(lib_path); + + if (!E) { + Ref<GDNative> gdn; + gdn.instance(); + gdn->set_library(lib); + + // TODO(karroffel): check the return value? + gdn->initialize(); + + library_gdnatives.insert(lib_path, gdn); + + library_classes.insert(lib_path, Map<StringName, NativeScriptDesc>()); + + if (!library_script_users.has(lib_path)) + library_script_users.insert(lib_path, Set<NativeScript *>()); + + void *args[1] = { + (void *)&lib_path + }; + + // here the library registers all the classes and stuff. + gdn->call_native_raw(_init_call_type, + _init_call_name, + NULL, + 1, + args, + NULL); + } else { + // already initialized. Nice. + } +} + +void NativeScriptLanguage::register_script(NativeScript *script) { +#ifndef NO_THREADS + MutexLock lock(mutex); +#endif + library_script_users[script->lib_path].insert(script); +} + +void NativeScriptLanguage::unregister_script(NativeScript *script) { +#ifndef NO_THREADS + MutexLock lock(mutex); +#endif + Map<String, Set<NativeScript *> >::Element *S = library_script_users.find(script->lib_path); + if (S) { + S->get().erase(script); + if (S->get().size() == 0) { + library_script_users.erase(S); + } + } +#ifndef NO_THREADS + scripts_to_register.erase(script); +#endif +} + +void NativeScriptLanguage::call_libraries_cb(const StringName &name) { + // library_gdnatives is modified only from the main thread, so it's safe not to use mutex here + for (Map<String, Ref<GDNative> >::Element *L = library_gdnatives.front(); L; L = L->next()) { + if (L->get()->is_initialized()) { + L->get()->call_native_raw( + _noarg_call_type, + name, + NULL, + 0, + NULL, + NULL); + } + } +} + +void NativeScriptLanguage::frame() { +#ifndef NO_THREADS + if (has_objects_to_register) { + MutexLock lock(mutex); + for (Set<Ref<GDNativeLibrary> >::Element *L = libs_to_init.front(); L; L = L->next()) { + init_library(L->get()); + } + libs_to_init.clear(); + for (Set<NativeScript *>::Element *S = scripts_to_register.front(); S; S = S->next()) { + register_script(S->get()); + } + scripts_to_register.clear(); + has_objects_to_register = false; + } +#endif + call_libraries_cb(_frame_call_name); +} + +#ifndef NO_THREADS + +void NativeScriptLanguage::thread_enter() { + call_libraries_cb(_thread_enter_call_name); +} + +void NativeScriptLanguage::thread_exit() { + call_libraries_cb(_thread_exit_call_name); +} + +#endif // NO_THREADS + +void NativeReloadNode::_bind_methods() { + ClassDB::bind_method(D_METHOD("_notification"), &NativeReloadNode::_notification); +} + +void NativeReloadNode::_notification(int p_what) { +#ifdef TOOLS_ENABLED + + switch (p_what) { + case MainLoop::NOTIFICATION_WM_FOCUS_OUT: { + + if (unloaded) + break; +#ifndef NO_THREADS + MutexLock lock(NSL->mutex); +#endif + NSL->_unload_stuff(); + for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) { + + L->get()->terminate(); + NSL->library_classes.erase(L->key()); + } + + unloaded = true; + + } break; + + case MainLoop::NOTIFICATION_WM_FOCUS_IN: { + + if (!unloaded) + break; +#ifndef NO_THREADS + MutexLock lock(NSL->mutex); +#endif + Set<StringName> libs_to_remove; + for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) { + + if (!L->get()->initialize()) { + libs_to_remove.insert(L->key()); + continue; + } + + NSL->library_classes.insert(L->key(), Map<StringName, NativeScriptDesc>()); + + void *args[1] = { + (void *)&L->key() + }; + + // here the library registers all the classes and stuff. + L->get()->call_native_raw(NSL->_init_call_type, + NSL->_init_call_name, + NULL, + 1, + args, + NULL); + + for (Map<String, Set<NativeScript *> >::Element *U = NSL->library_script_users.front(); U; U = U->next()) { + for (Set<NativeScript *>::Element *S = U->get().front(); S; S = S->next()) { + NativeScript *script = S->get(); + + if (script->placeholders.size() == 0) + continue; + + for (Set<PlaceHolderScriptInstance *>::Element *P = script->placeholders.front(); P; P = P->next()) { + script->_update_placeholder(P->get()); + } + } + } + } + + unloaded = false; + + for (Set<StringName>::Element *R = libs_to_remove.front(); R; R = R->next()) { + NSL->library_gdnatives.erase(R->get()); + } + + } break; + default: { + }; + } +#endif +} + +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); +} + +void ResourceFormatLoaderNativeScript::get_recognized_extensions(List<String> *p_extensions) const { + p_extensions->push_back("gdns"); +} + +bool ResourceFormatLoaderNativeScript::handles_type(const String &p_type) const { + return (p_type == "Script" || p_type == "NativeScript"); +} + +String ResourceFormatLoaderNativeScript::get_resource_type(const String &p_path) const { + String el = p_path.get_extension().to_lower(); + if (el == "gdns") + return "NativeScript"; + return ""; +} + +Error ResourceFormatSaverNativeScript::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 ResourceFormatSaverNativeScript::recognize(const RES &p_resource) const { + return Object::cast_to<NativeScript>(*p_resource) != NULL; +} + +void ResourceFormatSaverNativeScript::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const { + if (Object::cast_to<NativeScript>(*p_resource)) { + p_extensions->push_back("gdns"); + } +} diff --git a/modules/gdnative/nativescript/nativescript.h b/modules/gdnative/nativescript/nativescript.h new file mode 100644 index 0000000000..6c55e3e327 --- /dev/null +++ b/modules/gdnative/nativescript/nativescript.h @@ -0,0 +1,325 @@ +/*************************************************************************/ +/* nativescript.h */ +/*************************************************************************/ +/* 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. */ +/*************************************************************************/ +#ifndef NATIVE_SCRIPT_H +#define NATIVE_SCRIPT_H + +#include "io/resource_loader.h" +#include "io/resource_saver.h" +#include "os/thread_safe.h" +#include "resource.h" +#include "scene/main/node.h" +#include "script_language.h" +#include "self_list.h" + +#include "modules/gdnative/gdnative.h" +#include <nativescript/godot_nativescript.h> + +#ifndef NO_THREADS +#include "os/mutex.h" +#endif + +struct NativeScriptDesc { + + struct Method { + godot_instance_method method; + MethodInfo info; + int rpc_mode; + }; + struct Property { + godot_property_set_func setter; + godot_property_get_func getter; + PropertyInfo info; + Variant default_value; + int rset_mode; + }; + + struct Signal { + MethodInfo signal; + }; + + Map<StringName, Method> methods; + Map<StringName, Property> properties; + Map<StringName, Signal> signals_; // QtCreator doesn't like the name signals + StringName base; + StringName base_native_type; + NativeScriptDesc *base_data; + godot_instance_create_func create_func; + godot_instance_destroy_func destroy_func; + + bool is_tool; + + inline NativeScriptDesc() + : methods(), + properties(), + signals_(), + base(), + base_native_type() { + zeromem(&create_func, sizeof(godot_instance_create_func)); + zeromem(&destroy_func, sizeof(godot_instance_destroy_func)); + } +}; + +class NativeScript : public Script { + GDCLASS(NativeScript, Script) + +#ifdef TOOLS_ENABLED + Set<PlaceHolderScriptInstance *> placeholders; + void _update_placeholder(PlaceHolderScriptInstance *p_placeholder); + virtual void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder); +#endif + + friend class NativeScriptInstance; + friend class NativeScriptLanguage; + friend class NativeReloadNode; + friend class GDNativeLibrary; + + Ref<GDNativeLibrary> library; + + String lib_path; + + String class_name; + +#ifndef NO_THREADS + Mutex *owners_lock; +#endif + Set<Object *> instance_owners; + +protected: + static void _bind_methods(); + +public: + inline NativeScriptDesc *get_script_desc() const; + + void set_class_name(String p_class_name); + String get_class_name() const; + + void set_library(Ref<GDNativeLibrary> p_library); + Ref<GDNativeLibrary> get_library() const; + + virtual bool can_instance() const; + + virtual Ref<Script> get_base_script() const; //for script inheritance + + virtual StringName get_instance_base_type() const; // this may not work in all scripts, will return empty if so + virtual ScriptInstance *instance_create(Object *p_this); + virtual bool instance_has(const Object *p_this) const; + + virtual bool has_source_code() const; + virtual String get_source_code() const; + virtual void set_source_code(const String &p_code); + virtual Error reload(bool p_keep_state = false); + + virtual bool has_method(const StringName &p_method) const; + virtual MethodInfo get_method_info(const StringName &p_method) const; + + virtual bool is_tool() const; + + virtual String get_node_type() const; + + virtual ScriptLanguage *get_language() const; + + virtual bool has_script_signal(const StringName &p_signal) const; + virtual void get_script_signal_list(List<MethodInfo> *r_signals) const; + + virtual bool get_property_default_value(const StringName &p_property, Variant &r_value) const; + + virtual void update_exports(); //editor tool + virtual void get_script_method_list(List<MethodInfo> *p_list) const; + virtual void get_script_property_list(List<PropertyInfo> *p_list) const; + + Variant _new(const Variant **p_args, int p_argcount, Variant::CallError &r_error); + + NativeScript(); + ~NativeScript(); +}; + +class NativeScriptInstance : public ScriptInstance { + + friend class NativeScript; + + Object *owner; + Ref<NativeScript> script; + + void _ml_call_reversed(NativeScriptDesc *script_data, const StringName &p_method, const Variant **p_args, int p_argcount); + +public: + void *userdata; + + virtual bool set(const StringName &p_name, const Variant &p_value); + virtual bool get(const StringName &p_name, Variant &r_ret) const; + virtual void get_property_list(List<PropertyInfo> *p_properties) const; + virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid) const; + virtual void get_method_list(List<MethodInfo> *p_list) const; + virtual bool has_method(const StringName &p_method) const; + virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error); + virtual void notification(int p_notification); + virtual Ref<Script> get_script() const; + virtual RPCMode get_rpc_mode(const StringName &p_method) const; + virtual 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); + virtual void call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount); + + virtual void refcount_incremented(); + virtual bool refcount_decremented(); + + ~NativeScriptInstance(); +}; + +class NativeReloadNode; + +class NativeScriptLanguage : public ScriptLanguage { + + friend class NativeScript; + friend class NativeScriptInstance; + friend class NativeReloadNode; + +private: + static NativeScriptLanguage *singleton; + + void _unload_stuff(); + +#ifndef NO_THREADS + Mutex *mutex; + + Set<Ref<GDNativeLibrary> > libs_to_init; + Set<NativeScript *> scripts_to_register; + volatile bool has_objects_to_register; // so that we don't lock mutex every frame - it's rarely needed + void defer_init_library(Ref<GDNativeLibrary> lib, NativeScript *script); +#endif + + void init_library(const Ref<GDNativeLibrary> &lib); + void register_script(NativeScript *script); + void unregister_script(NativeScript *script); + + void call_libraries_cb(const StringName &name); + +public: + // These two maps must only be touched on the main thread + Map<String, Map<StringName, NativeScriptDesc> > library_classes; + Map<String, Ref<GDNative> > library_gdnatives; + + Map<String, Set<NativeScript *> > library_script_users; + + const StringName _init_call_type = "nativescript_init"; + const StringName _init_call_name = "godot_nativescript_init"; + + const StringName _noarg_call_type = "nativescript_no_arg"; + + const StringName _frame_call_name = "godot_nativescript_frame"; + +#ifndef NO_THREADS + const StringName _thread_enter_call_name = "godot_nativescript_thread_enter"; + const StringName _thread_exit_call_name = "godot_nativescript_thread_exit"; +#endif + + NativeScriptLanguage(); + ~NativeScriptLanguage(); + + inline static NativeScriptLanguage *get_singleton() { + return singleton; + } + + void _hacky_api_anchor(); + +#ifndef NO_THREADS + virtual void thread_enter(); + virtual void thread_exit(); +#endif + + virtual void frame(); + + virtual String get_name() const; + virtual void init(); + virtual String get_type() const; + virtual String get_extension() const; + virtual Error execute_file(const String &p_path); + virtual void finish(); + virtual void get_reserved_words(List<String> *p_words) const; + 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) const; + virtual Script *create_script() const; + virtual bool has_named_classes() const; + virtual int find_function(const String &p_function, const String &p_code) const; + virtual String make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const; + virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const; + virtual void add_global_constant(const StringName &p_variable, const Variant &p_value); + virtual String debug_get_error() const; + virtual int debug_get_stack_level_count() const; + virtual int debug_get_stack_level_line(int p_level) const; + virtual String debug_get_stack_level_function(int p_level) const; + virtual String debug_get_stack_level_source(int p_level) const; + virtual void debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth); + virtual void debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems, int p_max_depth); + virtual void debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth); + virtual String debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth); + virtual void reload_all_scripts(); + virtual void reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload); + virtual void get_recognized_extensions(List<String> *p_extensions) const; + virtual void get_public_functions(List<MethodInfo> *p_functions) const; + virtual void get_public_constants(List<Pair<String, Variant> > *p_constants) const; + virtual void profiling_start(); + virtual void profiling_stop(); + virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max); + virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max); +}; + +inline NativeScriptDesc *NativeScript::get_script_desc() const { + Map<StringName, NativeScriptDesc>::Element *E = NativeScriptLanguage::singleton->library_classes[lib_path].find(class_name); + return E ? &E->get() : NULL; +} + +class NativeReloadNode : public Node { + GDCLASS(NativeReloadNode, Node) + bool unloaded = false; + +public: + static void _bind_methods(); + void _notification(int p_what); +}; + +class ResourceFormatLoaderNativeScript : public ResourceFormatLoader { +public: + virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL); + virtual void get_recognized_extensions(List<String> *p_extensions) const; + virtual bool handles_type(const String &p_type) const; + virtual String get_resource_type(const String &p_path) const; +}; + +class ResourceFormatSaverNativeScript : public ResourceFormatSaver { + virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0); + virtual bool recognize(const RES &p_resource) const; + virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const; +}; + +#endif // GDNATIVE_H diff --git a/modules/gdnative/nativescript/register_types.cpp b/modules/gdnative/nativescript/register_types.cpp new file mode 100644 index 0000000000..b846710ab8 --- /dev/null +++ b/modules/gdnative/nativescript/register_types.cpp @@ -0,0 +1,118 @@ +/*************************************************************************/ +/* register_types.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 "register_types.h" + +#include "io/resource_loader.h" +#include "io/resource_saver.h" + +#include "nativescript.h" + +#include "core/os/os.h" + +NativeScriptLanguage *native_script_language; + +typedef void (*native_script_init_fn)(void *); + +void init_call_cb(void *p_handle, godot_string *p_proc_name, void *p_data, int p_num_args, void **args, void *r_ret) { + if (p_handle == NULL) { + ERR_PRINT("No valid library handle, can't call nativescript init procedure"); + return; + } + + void *library_proc; + Error err = OS::get_singleton()->get_dynamic_library_symbol_handle( + p_handle, + *(String *)p_proc_name, + library_proc, + true); // we print our own message + if (err != OK) { + ERR_PRINT((String("GDNative procedure \"" + *(String *)p_proc_name) + "\" does not exists and can't be called").utf8().get_data()); + return; + } + + native_script_init_fn fn = (native_script_init_fn)library_proc; + + fn(args[0]); +} + +typedef void (*native_script_empty_callback)(); + +void noarg_call_cb(void *p_handle, godot_string *p_proc_name, void *p_data, int p_num_args, void **args, void *r_ret) { + if (p_handle == NULL) { + ERR_PRINT("No valid library handle, can't call nativescript callback"); + return; + } + + void *library_proc; + Error err = OS::get_singleton()->get_dynamic_library_symbol_handle( + p_handle, + *(String *)p_proc_name, + library_proc, + true); + if (err != OK) { + // it's fine if thread callbacks are not present in the library. + return; + } + + native_script_empty_callback fn = (native_script_empty_callback)library_proc; + fn(); +} + +ResourceFormatLoaderNativeScript *resource_loader_gdns = NULL; +ResourceFormatSaverNativeScript *resource_saver_gdns = NULL; + +void register_nativescript_types() { + native_script_language = memnew(NativeScriptLanguage); + + ClassDB::register_class<NativeScript>(); + + ScriptServer::register_language(native_script_language); + + GDNativeCallRegistry::singleton->register_native_raw_call_type(native_script_language->_init_call_type, init_call_cb); + GDNativeCallRegistry::singleton->register_native_raw_call_type(native_script_language->_noarg_call_type, noarg_call_cb); + + resource_saver_gdns = memnew(ResourceFormatSaverNativeScript); + ResourceSaver::add_resource_format_saver(resource_saver_gdns); + + resource_loader_gdns = memnew(ResourceFormatLoaderNativeScript); + ResourceLoader::add_resource_format_loader(resource_loader_gdns); +} + +void unregister_nativescript_types() { + + memdelete(resource_loader_gdns); + + memdelete(resource_saver_gdns); + + if (native_script_language) { + ScriptServer::unregister_language(native_script_language); + memdelete(native_script_language); + } +} diff --git a/modules/gdnative/nativescript/register_types.h b/modules/gdnative/nativescript/register_types.h new file mode 100644 index 0000000000..7ac558f68f --- /dev/null +++ b/modules/gdnative/nativescript/register_types.h @@ -0,0 +1,31 @@ +/*************************************************************************/ +/* register_types.h */ +/*************************************************************************/ +/* 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. */ +/*************************************************************************/ +void register_nativescript_types(); +void unregister_nativescript_types(); |