[GDNative] new GDNative API
This adds GDNative as a separate class type. It can be used to interface with native libraries by using "native calls", which can be registered by modules (and in future other GDNative libraries?). It also reworks the currently called "GDNativeScript" into a "NativeScript" that just makes use of the new GDNative instead of it being the component that implements that functionality.
This commit is contained in:
parent
85aa0adeef
commit
534d62d2f4
50 changed files with 2611 additions and 2074 deletions
10
modules/nativescript/SCsub
Normal file
10
modules/nativescript/SCsub
Normal file
|
|
@ -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')
|
||||
444
modules/nativescript/api_generator.cpp
Normal file
444
modules/nativescript/api_generator.cpp
Normal file
|
|
@ -0,0 +1,444 @@
|
|||
/*************************************************************************/
|
||||
/* api_generator.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 "api_generator.h"
|
||||
|
||||
#ifdef TOOLS_ENABLED
|
||||
|
||||
#include "class_db.h"
|
||||
#include "core/global_constants.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 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_;
|
||||
};
|
||||
|
||||
/*
|
||||
* 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);
|
||||
|
||||
{
|
||||
bool is_reference = false;
|
||||
List<StringName> inheriters;
|
||||
ClassDB::get_inheriters_from_class("Reference", &inheriters);
|
||||
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 if (argument.hint == PROPERTY_HINT_RESOURCE_TYPE) {
|
||||
type = argument.hint_string;
|
||||
} else if (argument.type == Variant::NIL) {
|
||||
type = "Variant";
|
||||
} else {
|
||||
type = Variant::get_type_name(argument.type);
|
||||
}
|
||||
|
||||
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 if (p->get().hint == PROPERTY_HINT_RESOURCE_TYPE) {
|
||||
property_api.type = p->get().hint_string;
|
||||
} else if (p->get().type == Variant::NIL) {
|
||||
property_api.type = "Variant";
|
||||
} else {
|
||||
property_api.type = Variant::get_type_name(p->get().type);
|
||||
}
|
||||
|
||||
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_bind && method_bind->get_return_type() != StringName()) {
|
||||
method_api.return_type = method_bind->get_return_type();
|
||||
} else 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 if (m->get().return_val.type != Variant::NIL) {
|
||||
method_api.return_type = m->get().return_val.hint == PROPERTY_HINT_RESOURCE_TYPE ? m->get().return_val.hint_string : Variant::get_type_name(m->get().return_val.type);
|
||||
} else if (m->get().return_val.name != "") {
|
||||
method_api.return_type = m->get().return_val.name;
|
||||
} else {
|
||||
method_api.return_type = "void";
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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(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
|
||||
}
|
||||
38
modules/nativescript/api_generator.h
Normal file
38
modules/nativescript/api_generator.h
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/*************************************************************************/
|
||||
/* api_generator.h */
|
||||
/*************************************************************************/
|
||||
/* 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. */
|
||||
/*************************************************************************/
|
||||
#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
|
||||
8
modules/nativescript/config.py
Normal file
8
modules/nativescript/config.py
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
|
||||
def can_build(platform):
|
||||
return True
|
||||
|
||||
|
||||
def configure(env):
|
||||
env.use_ptrcall = True
|
||||
205
modules/nativescript/godot_nativescript.cpp
Normal file
205
modules/nativescript/godot_nativescript.cpp
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
/*************************************************************************/
|
||||
/* godot_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 "godot_nativescript.h"
|
||||
|
||||
#include "nativescript.h"
|
||||
|
||||
#include "class_db.h"
|
||||
#include "error_macros.h"
|
||||
#include "gdnative.h"
|
||||
#include "global_constants.h"
|
||||
#include "project_settings.h"
|
||||
#include "variant.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())->get_userdata();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
227
modules/nativescript/godot_nativescript.h
Normal file
227
modules/nativescript/godot_nativescript.h
Normal file
|
|
@ -0,0 +1,227 @@
|
|||
/*************************************************************************/
|
||||
/* godot.h */
|
||||
/*************************************************************************/
|
||||
/* 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. */
|
||||
/*************************************************************************/
|
||||
#ifndef GODOT_NATIVESCRIPT_H
|
||||
#define GODOT_NATIVESCRIPT_H
|
||||
|
||||
#include <godot/gdnative.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef GDAPI_BUILT_IN
|
||||
#define GDAPI_EXPORT
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#if defined(GDAPI_EXPORT)
|
||||
#define GDCALLINGCONV
|
||||
#define GDAPI __declspec(dllexport) GDCALLINGCONV
|
||||
#else
|
||||
#define GDCALLINGCONV
|
||||
#define GDAPI __declspec(dllimport) GDCALLINGCONV
|
||||
#endif
|
||||
#elif defined(__APPLE__)
|
||||
#include "TargetConditionals.h"
|
||||
#if TARGET_OS_IPHONE
|
||||
#define GDCALLINGCONV
|
||||
#define GDAPI
|
||||
#elif TARGET_OS_MAC
|
||||
#define GDCALLINGCONV __attribute__((sysv_abi))
|
||||
#define GDAPI GDCALLINGCONV
|
||||
#endif
|
||||
#else
|
||||
#define GDCALLINGCONV __attribute__((sysv_abi))
|
||||
#define GDAPI GDCALLINGCONV
|
||||
#endif
|
||||
|
||||
// This is for libraries *using* the header, NOT GODOT EXPOSING STUFF!!
|
||||
#ifdef _WIN32
|
||||
#define GDN_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define GDN_EXPORT
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum {
|
||||
GODOT_METHOD_RPC_MODE_DISABLED,
|
||||
GODOT_METHOD_RPC_MODE_REMOTE,
|
||||
GODOT_METHOD_RPC_MODE_SYNC,
|
||||
GODOT_METHOD_RPC_MODE_MASTER,
|
||||
GODOT_METHOD_RPC_MODE_SLAVE,
|
||||
} godot_method_rpc_mode;
|
||||
|
||||
typedef enum {
|
||||
GODOT_PROPERTY_HINT_NONE, ///< no hint provided.
|
||||
GODOT_PROPERTY_HINT_RANGE, ///< hint_text = "min,max,step,slider; //slider is optional"
|
||||
GODOT_PROPERTY_HINT_EXP_RANGE, ///< hint_text = "min,max,step", exponential edit
|
||||
GODOT_PROPERTY_HINT_ENUM, ///< hint_text= "val1,val2,val3,etc"
|
||||
GODOT_PROPERTY_HINT_EXP_EASING, /// exponential easing funciton (Math::ease)
|
||||
GODOT_PROPERTY_HINT_LENGTH, ///< hint_text= "length" (as integer)
|
||||
GODOT_PROPERTY_HINT_SPRITE_FRAME,
|
||||
GODOT_PROPERTY_HINT_KEY_ACCEL, ///< hint_text= "length" (as integer)
|
||||
GODOT_PROPERTY_HINT_FLAGS, ///< hint_text= "flag1,flag2,etc" (as bit flags)
|
||||
GODOT_PROPERTY_HINT_LAYERS_2D_RENDER,
|
||||
GODOT_PROPERTY_HINT_LAYERS_2D_PHYSICS,
|
||||
GODOT_PROPERTY_HINT_LAYERS_3D_RENDER,
|
||||
GODOT_PROPERTY_HINT_LAYERS_3D_PHYSICS,
|
||||
GODOT_PROPERTY_HINT_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
|
||||
GODOT_PROPERTY_HINT_DIR, ///< a directort path must be passed
|
||||
GODOT_PROPERTY_HINT_GLOBAL_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
|
||||
GODOT_PROPERTY_HINT_GLOBAL_DIR, ///< a directort path must be passed
|
||||
GODOT_PROPERTY_HINT_RESOURCE_TYPE, ///< a resource object type
|
||||
GODOT_PROPERTY_HINT_MULTILINE_TEXT, ///< used for string properties that can contain multiple lines
|
||||
GODOT_PROPERTY_HINT_COLOR_NO_ALPHA, ///< used for ignoring alpha component when editing a color
|
||||
GODOT_PROPERTY_HINT_IMAGE_COMPRESS_LOSSY,
|
||||
GODOT_PROPERTY_HINT_IMAGE_COMPRESS_LOSSLESS,
|
||||
GODOT_PROPERTY_HINT_OBJECT_ID,
|
||||
GODOT_PROPERTY_HINT_TYPE_STRING, ///< a type string, the hint is the base type to choose
|
||||
GODOT_PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE, ///< so something else can provide this (used in scripts)
|
||||
GODOT_PROPERTY_HINT_METHOD_OF_VARIANT_TYPE, ///< a method of a type
|
||||
GODOT_PROPERTY_HINT_METHOD_OF_BASE_TYPE, ///< a method of a base type
|
||||
GODOT_PROPERTY_HINT_METHOD_OF_INSTANCE, ///< a method of an instance
|
||||
GODOT_PROPERTY_HINT_METHOD_OF_SCRIPT, ///< a method of a script & base
|
||||
GODOT_PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE, ///< a property of a type
|
||||
GODOT_PROPERTY_HINT_PROPERTY_OF_BASE_TYPE, ///< a property of a base type
|
||||
GODOT_PROPERTY_HINT_PROPERTY_OF_INSTANCE, ///< a property of an instance
|
||||
GODOT_PROPERTY_HINT_PROPERTY_OF_SCRIPT, ///< a property of a script & base
|
||||
GODOT_PROPERTY_HINT_MAX,
|
||||
} godot_property_hint;
|
||||
|
||||
typedef enum {
|
||||
|
||||
GODOT_PROPERTY_USAGE_STORAGE = 1,
|
||||
GODOT_PROPERTY_USAGE_EDITOR = 2,
|
||||
GODOT_PROPERTY_USAGE_NETWORK = 4,
|
||||
GODOT_PROPERTY_USAGE_EDITOR_HELPER = 8,
|
||||
GODOT_PROPERTY_USAGE_CHECKABLE = 16, //used for editing global variables
|
||||
GODOT_PROPERTY_USAGE_CHECKED = 32, //used for editing global variables
|
||||
GODOT_PROPERTY_USAGE_INTERNATIONALIZED = 64, //hint for internationalized strings
|
||||
GODOT_PROPERTY_USAGE_GROUP = 128, //used for grouping props in the editor
|
||||
GODOT_PROPERTY_USAGE_CATEGORY = 256,
|
||||
GODOT_PROPERTY_USAGE_STORE_IF_NONZERO = 512, //only store if nonzero
|
||||
GODOT_PROPERTY_USAGE_STORE_IF_NONONE = 1024, //only store if false
|
||||
GODOT_PROPERTY_USAGE_NO_INSTANCE_STATE = 2048,
|
||||
GODOT_PROPERTY_USAGE_RESTART_IF_CHANGED = 4096,
|
||||
GODOT_PROPERTY_USAGE_SCRIPT_VARIABLE = 8192,
|
||||
GODOT_PROPERTY_USAGE_STORE_IF_NULL = 16384,
|
||||
GODOT_PROPERTY_USAGE_ANIMATE_AS_TRIGGER = 32768,
|
||||
GODOT_PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED = 65536,
|
||||
|
||||
GODOT_PROPERTY_USAGE_DEFAULT = GODOT_PROPERTY_USAGE_STORAGE | GODOT_PROPERTY_USAGE_EDITOR | GODOT_PROPERTY_USAGE_NETWORK,
|
||||
GODOT_PROPERTY_USAGE_DEFAULT_INTL = GODOT_PROPERTY_USAGE_STORAGE | GODOT_PROPERTY_USAGE_EDITOR | GODOT_PROPERTY_USAGE_NETWORK | GODOT_PROPERTY_USAGE_INTERNATIONALIZED,
|
||||
GODOT_PROPERTY_USAGE_NOEDITOR = GODOT_PROPERTY_USAGE_STORAGE | GODOT_PROPERTY_USAGE_NETWORK,
|
||||
} godot_property_usage_flags;
|
||||
|
||||
typedef struct {
|
||||
godot_method_rpc_mode rset_type;
|
||||
|
||||
godot_int type;
|
||||
godot_property_hint hint;
|
||||
godot_string hint_string;
|
||||
godot_property_usage_flags usage;
|
||||
godot_variant default_value;
|
||||
} godot_property_attributes;
|
||||
|
||||
typedef struct {
|
||||
// instance pointer, method_data - return user data
|
||||
GDCALLINGCONV void *(*create_func)(godot_object *, void *);
|
||||
void *method_data;
|
||||
GDCALLINGCONV void (*free_func)(void *);
|
||||
} godot_instance_create_func;
|
||||
|
||||
typedef struct {
|
||||
// instance pointer, method data, user data
|
||||
GDCALLINGCONV void (*destroy_func)(godot_object *, void *, void *);
|
||||
void *method_data;
|
||||
GDCALLINGCONV void (*free_func)(void *);
|
||||
} godot_instance_destroy_func;
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
typedef struct {
|
||||
godot_method_rpc_mode rpc_type;
|
||||
} godot_method_attributes;
|
||||
|
||||
typedef struct {
|
||||
// instance pointer, method data, user data, num args, args - return result as varaint
|
||||
GDCALLINGCONV godot_variant (*method)(godot_object *, void *, void *, int, godot_variant **);
|
||||
void *method_data;
|
||||
GDCALLINGCONV void (*free_func)(void *);
|
||||
} godot_instance_method;
|
||||
|
||||
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);
|
||||
|
||||
typedef struct {
|
||||
// instance pointer, method data, user data, value
|
||||
GDCALLINGCONV void (*set_func)(godot_object *, void *, void *, godot_variant *);
|
||||
void *method_data;
|
||||
GDCALLINGCONV void (*free_func)(void *);
|
||||
} godot_property_set_func;
|
||||
|
||||
typedef struct {
|
||||
// instance pointer, method data, user data, value
|
||||
GDCALLINGCONV godot_variant (*get_func)(godot_object *, void *, void *);
|
||||
void *method_data;
|
||||
GDCALLINGCONV void (*free_func)(void *);
|
||||
} godot_property_get_func;
|
||||
|
||||
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);
|
||||
|
||||
typedef struct {
|
||||
godot_string name;
|
||||
godot_int type;
|
||||
godot_property_hint hint;
|
||||
godot_string hint_string;
|
||||
godot_property_usage_flags usage;
|
||||
godot_variant default_value;
|
||||
} godot_signal_argument;
|
||||
|
||||
typedef struct {
|
||||
godot_string name;
|
||||
int num_args;
|
||||
godot_signal_argument *args;
|
||||
int num_default_args;
|
||||
godot_variant *default_args;
|
||||
} godot_signal;
|
||||
|
||||
void GDAPI godot_nativescript_register_signal(void *p_gdnative_handle, const char *p_name, const godot_signal *p_signal);
|
||||
|
||||
void GDAPI *godot_nativescript_get_userdata(godot_object *p_instance);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
1088
modules/nativescript/nativescript.cpp
Normal file
1088
modules/nativescript/nativescript.cpp
Normal file
File diff suppressed because it is too large
Load diff
281
modules/nativescript/nativescript.h
Normal file
281
modules/nativescript/nativescript.h
Normal file
|
|
@ -0,0 +1,281 @@
|
|||
/*************************************************************************/
|
||||
/* nativescript.h */
|
||||
/*************************************************************************/
|
||||
/* 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. */
|
||||
/*************************************************************************/
|
||||
#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 "godot_nativescript.h"
|
||||
#include "modules/gdnative/gdnative.h"
|
||||
|
||||
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;
|
||||
|
||||
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> 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 *userdata;
|
||||
|
||||
void _ml_call_reversed(NativeScriptDesc *script_data, const StringName &p_method, const Variant **p_args, int p_argcount);
|
||||
|
||||
public:
|
||||
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);
|
||||
|
||||
~NativeScriptInstance();
|
||||
};
|
||||
|
||||
class NativeReloadNode;
|
||||
|
||||
class NativeScriptLanguage : public ScriptLanguage {
|
||||
|
||||
friend class NativeScript;
|
||||
friend class NativeScriptInstance;
|
||||
friend class NativeReloadNode;
|
||||
|
||||
private:
|
||||
static NativeScriptLanguage *singleton;
|
||||
|
||||
void _unload_stuff();
|
||||
|
||||
public:
|
||||
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";
|
||||
|
||||
NativeScriptLanguage();
|
||||
~NativeScriptLanguage();
|
||||
|
||||
inline static NativeScriptLanguage *get_singleton() {
|
||||
return singleton;
|
||||
}
|
||||
|
||||
void _hacky_api_anchor();
|
||||
|
||||
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)
|
||||
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
|
||||
93
modules/nativescript/register_types.cpp
Normal file
93
modules/nativescript/register_types.cpp
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
/*************************************************************************/
|
||||
/* register_types.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 "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);
|
||||
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]);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
31
modules/nativescript/register_types.h
Normal file
31
modules/nativescript/register_types.h
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/*************************************************************************/
|
||||
/* register_types.h */
|
||||
/*************************************************************************/
|
||||
/* 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. */
|
||||
/*************************************************************************/
|
||||
void register_nativescript_types();
|
||||
void unregister_nativescript_types();
|
||||
Loading…
Add table
Add a link
Reference in a new issue