From c27af379b82bae4a3c2b0bc661b9e9e624cbdb51 Mon Sep 17 00:00:00 2001 From: Karroffel Date: Mon, 24 Jul 2017 14:23:09 +0200 Subject: [PATCH] updated to use the new NativeScript and GDNative interface --- SConstruct | 4 ++-- binding_generator.py | 6 +++--- include/core/Array.hpp | 2 +- include/core/Color.hpp | 2 +- include/core/Dictionary.hpp | 2 +- include/core/Godot.hpp | 40 ++++++++++++++++-------------------- include/core/GodotGlobal.hpp | 6 ++++++ include/core/NodePath.hpp | 2 +- include/core/PoolArrays.hpp | 2 +- include/core/RID.hpp | 2 +- include/core/String.hpp | 2 +- include/core/Variant.hpp | 2 +- include/core/Vector2.hpp | 2 +- src/core/Array.cpp | 2 -- src/core/Color.cpp | 2 +- src/core/Dictionary.cpp | 1 - src/core/GodotGlobal.cpp | 23 ++++++++++++++++++++- src/core/NodePath.cpp | 2 +- src/core/PoolArrays.cpp | 2 +- src/core/RID.cpp | 2 +- src/core/String.cpp | 2 +- src/core/Variant.cpp | 2 +- src/core/Vector2.cpp | 2 +- 23 files changed, 67 insertions(+), 47 deletions(-) diff --git a/SConstruct b/SConstruct index daf66d3..4cc61d0 100644 --- a/SConstruct +++ b/SConstruct @@ -15,7 +15,7 @@ env = Environment() if ARGUMENTS.get("use_llvm", "no") == "yes": env["CXX"] = "clang++" -target = ARGUMENTS.get("target", "core") +target = ARGUMENTS.get("target", "debug") platform = ARGUMENTS.get("p", "linux") @@ -33,7 +33,7 @@ if platform == "osx": env.Append(LINKFLAGS = ['-arch', 'x86_64', '-framework', 'Cocoa', '-Wl,-undefined,dynamic_lookup']) if platform == "linux": - env.Append(CCFLAGS = ['-g','-O3', '-std=c++14']) + env.Append(CCFLAGS = ['-fPIC', '-g','-O3', '-std=c++14']) env.Append(CPPPATH=['.', godot_headers_path, 'include', 'include/core']) diff --git a/binding_generator.py b/binding_generator.py index c376b42..a2e6fe1 100644 --- a/binding_generator.py +++ b/binding_generator.py @@ -64,7 +64,7 @@ def generate_class_header(used_classes, c): source.append("") source.append("") - source.append("#include ") + source.append("#include ") source.append("#include ") source.append("") @@ -419,7 +419,7 @@ def generate_icall_header(icalls): source.append("") - source.append("#include ") + source.append("#include ") source.append("#include ") source.append("") @@ -469,7 +469,7 @@ def generate_icall_implementation(icalls): source.append("") - source.append("#include ") + source.append("#include ") source.append("#include ") source.append("") diff --git a/include/core/Array.hpp b/include/core/Array.hpp index 1b60788..6a64872 100644 --- a/include/core/Array.hpp +++ b/include/core/Array.hpp @@ -1,7 +1,7 @@ #ifndef ARRAY_H #define ARRAY_H -#include +#include #include "String.hpp" diff --git a/include/core/Color.hpp b/include/core/Color.hpp index 528e674..5232573 100644 --- a/include/core/Color.hpp +++ b/include/core/Color.hpp @@ -1,7 +1,7 @@ #ifndef COLOR_H #define COLOR_H -#include +#include #include diff --git a/include/core/Dictionary.hpp b/include/core/Dictionary.hpp index 4c719f5..31744b7 100644 --- a/include/core/Dictionary.hpp +++ b/include/core/Dictionary.hpp @@ -5,7 +5,7 @@ #include "Array.hpp" -#include +#include namespace godot { diff --git a/include/core/Godot.hpp b/include/core/Godot.hpp index 3754be7..fd1d709 100644 --- a/include/core/Godot.hpp +++ b/include/core/Godot.hpp @@ -4,7 +4,8 @@ #include #include -#include +#include +#include #include @@ -39,16 +40,10 @@ public: -#if !defined(_WIN32) -#define GD_EXPORT -#else -#define GD_EXPORT __declspec(dllexport) -#endif - - -#define GODOT_NATIVE_INIT(arg) extern "C" void GD_EXPORT godot_native_init(arg) -#define GODOT_NATIVE_TERMINATE(arg) extern "C" void GD_EXPORT godot_native_terminate(arg) +#define GDNATIVE_INIT(arg) void gdnative_init(arg) +#define GDNATIVE_TERMINATE(arg) void gdnative_terminate(arg) +#define NATIVESCRIPT_INIT() void nativescript_init() #define GODOT_CLASS(Name) \ public: inline static char *___get_type_name() { return (char *) #Name; } \ @@ -92,7 +87,7 @@ struct _ArgCast { template T *as(Object *obj) { - return (T *) godot_native_get_userdata(obj); + return (T *) godot_nativescript_get_userdata(obj); } // instance and destroy funcs @@ -114,7 +109,6 @@ void _godot_class_destroy_func(godot_object *p, void *method_data, void *data) } - template void register_class() { @@ -125,7 +119,7 @@ void register_class() destroy.destroy_func = _godot_class_destroy_func; - godot_script_register_class(T::___get_type_name(), T::___get_base_type_name(), create, destroy); + godot_nativescript_register_class(godot::_RegisterState::nativescript_handle, T::___get_type_name(), T::___get_base_type_name(), create, destroy); T::_register_methods(); } @@ -139,7 +133,7 @@ void register_tool_class() destroy.destroy_func = _godot_class_destroy_func; - godot_script_register_tool_class(T::___get_type_name(), T::___get_base_type_name(), create, destroy); + godot_nativescript_register_tool_class(godot::_RegisterState::nativescript_handle, T::___get_type_name(), T::___get_base_type_name(), create, destroy); T::_register_methods(); } @@ -292,7 +286,7 @@ void register_method(char *name, M method_ptr, godot_method_rpc_mode rpc_type = godot_method_attributes attr = {}; attr.rpc_type = rpc_type; - godot_script_register_method(___get_method_class_name(method_ptr), name, attr, method); + godot_nativescript_register_method(godot::_RegisterState::nativescript_handle, ___get_method_class_name(method_ptr), name, attr, method); } @@ -300,12 +294,12 @@ void register_method(char *name, M method_ptr, godot_method_rpc_mode rpc_type = template struct _PropertySetFunc { void (T::*f)(P); - static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant value) + static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant *value) { _PropertySetFunc *set_func = (_PropertySetFunc *) method_data; T *obj = (T *) user_data; - Variant *v = (Variant *) &value; + Variant *v = (Variant *) value; (obj->*(set_func->f))(_ArgCast

::_arg_cast(*v)); } @@ -338,12 +332,12 @@ struct _PropertyGetFunc { template struct _PropertyDefaultSetFunc { P (T::*f); - static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant value) + static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant *value) { _PropertyDefaultSetFunc *set_func = (_PropertyDefaultSetFunc *) method_data; T *obj = (T *) user_data; - Variant *v = (Variant *) &value; + Variant *v = (Variant *) value; (obj->*(set_func->f)) = _ArgCast

::_arg_cast(*v); } @@ -410,7 +404,7 @@ void register_property(char *name, P (T::*var), P default_value, godot_method_rp get_func.free_func = godot_free; get_func.get_func = &_PropertyDefaultGetFunc::_wrapped_getter; - godot_script_register_property(T::___get_type_name(), name, &attr, set_func, get_func); + godot_nativescript_register_property(godot::_RegisterState::nativescript_handle, T::___get_type_name(), name, &attr, set_func, get_func); } @@ -444,7 +438,7 @@ void register_property(char *name, void (T::*setter)(P), P (T::*getter)(), P def get_func.free_func = godot_free; get_func.get_func = &_PropertyGetFunc::_wrapped_getter; - godot_script_register_property(T::___get_type_name(), name, &attr, set_func, get_func); + godot_nativescript_register_property(godot::_RegisterState::nativescript_handle, T::___get_type_name(), name, &attr, set_func, get_func); } @@ -473,11 +467,13 @@ void register_signal(String name, Dictionary args = Dictionary()) signal.args[i].type = args.values()[i]; } - godot_script_register_signal(T::___get_type_name(), &signal); + godot_nativescript_register_signal(godot::_RegisterState::nativescript_handle, T::___get_type_name(), &signal); for (int i = 0; i < signal.num_args; i++) { godot_string_destroy(&signal.args[i].name); } + + godot_free(signal.args); } diff --git a/include/core/GodotGlobal.hpp b/include/core/GodotGlobal.hpp index 18e2306..9340a6b 100644 --- a/include/core/GodotGlobal.hpp +++ b/include/core/GodotGlobal.hpp @@ -15,6 +15,12 @@ public: }; + + +struct _RegisterState { + static void *nativescript_handle; +}; + } #endif diff --git a/include/core/NodePath.hpp b/include/core/NodePath.hpp index 91cb1e1..f4b4b25 100644 --- a/include/core/NodePath.hpp +++ b/include/core/NodePath.hpp @@ -3,7 +3,7 @@ #include "String.hpp" -#include +#include namespace godot { diff --git a/include/core/PoolArrays.hpp b/include/core/PoolArrays.hpp index e2a51f1..6c07ae1 100644 --- a/include/core/PoolArrays.hpp +++ b/include/core/PoolArrays.hpp @@ -8,7 +8,7 @@ #include "Vector2.hpp" #include "Vector3.hpp" -#include +#include namespace godot { diff --git a/include/core/RID.hpp b/include/core/RID.hpp index 5855049..3f83e79 100644 --- a/include/core/RID.hpp +++ b/include/core/RID.hpp @@ -1,7 +1,7 @@ #ifndef RID_H #define RID_H -#include +#include namespace godot { diff --git a/include/core/String.hpp b/include/core/String.hpp index 0b4e4fb..dec8104 100644 --- a/include/core/String.hpp +++ b/include/core/String.hpp @@ -1,7 +1,7 @@ #ifndef STRING_H #define STRING_H -#include +#include namespace godot { diff --git a/include/core/Variant.hpp b/include/core/Variant.hpp index 670c5c2..9ffa398 100644 --- a/include/core/Variant.hpp +++ b/include/core/Variant.hpp @@ -1,7 +1,7 @@ #ifndef VARIANT_H #define VARIANT_H -#include +#include #include "Defs.hpp" diff --git a/include/core/Vector2.hpp b/include/core/Vector2.hpp index 9cdde2a..da45ccf 100644 --- a/include/core/Vector2.hpp +++ b/include/core/Vector2.hpp @@ -1,7 +1,7 @@ #ifndef VECTOR2_H #define VECTOR2_H -#include +#include #include "Defs.hpp" diff --git a/src/core/Array.cpp b/src/core/Array.cpp index bb388c4..5ba66b2 100644 --- a/src/core/Array.cpp +++ b/src/core/Array.cpp @@ -2,8 +2,6 @@ #include -#include - #include "Variant.hpp" namespace godot { diff --git a/src/core/Color.cpp b/src/core/Color.cpp index 985e04e..a41615e 100644 --- a/src/core/Color.cpp +++ b/src/core/Color.cpp @@ -1,6 +1,6 @@ #include "Color.hpp" -#include +#include #include diff --git a/src/core/Dictionary.cpp b/src/core/Dictionary.cpp index 8ea2d17..61383e7 100644 --- a/src/core/Dictionary.cpp +++ b/src/core/Dictionary.cpp @@ -4,7 +4,6 @@ #include "Array.hpp" -#include namespace godot { diff --git a/src/core/GodotGlobal.cpp b/src/core/GodotGlobal.cpp index cd910ff..96a5fa6 100644 --- a/src/core/GodotGlobal.cpp +++ b/src/core/GodotGlobal.cpp @@ -2,10 +2,12 @@ #include "String.hpp" -#include +#include namespace godot { +void *_RegisterState::nativescript_handle; + void Godot::print(const String& message) { godot_print((godot_string *) &message); @@ -22,3 +24,22 @@ void Godot::print_error(const String& description, const String& function, const } }; + +void gdnative_init(godot_gdnative_init_options *options); +extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *options) +{ + gdnative_init(options); +} + +void gdnative_terminate(godot_gdnative_terminate_options *options); +extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *options) +{ + gdnative_terminate(options); +} + +void nativescript_init(); +extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) +{ + godot::_RegisterState::nativescript_handle = handle; + nativescript_init(); +} diff --git a/src/core/NodePath.cpp b/src/core/NodePath.cpp index 08ec27a..98c40c9 100644 --- a/src/core/NodePath.cpp +++ b/src/core/NodePath.cpp @@ -2,7 +2,7 @@ #include "String.hpp" -#include +#include namespace godot { diff --git a/src/core/PoolArrays.cpp b/src/core/PoolArrays.cpp index 3e77814..e33ce0e 100644 --- a/src/core/PoolArrays.cpp +++ b/src/core/PoolArrays.cpp @@ -7,7 +7,7 @@ #include "Vector2.hpp" #include "Vector3.hpp" -#include +#include namespace godot { diff --git a/src/core/RID.cpp b/src/core/RID.cpp index 1431a8a..75419ba 100644 --- a/src/core/RID.cpp +++ b/src/core/RID.cpp @@ -1,6 +1,6 @@ #include "RID.hpp" -#include +#include namespace godot { diff --git a/src/core/String.cpp b/src/core/String.cpp index 84c8ee8..846241e 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -2,7 +2,7 @@ #include "NodePath.hpp" -#include +#include #include diff --git a/src/core/Variant.cpp b/src/core/Variant.cpp index 1c6eef2..fa72b66 100644 --- a/src/core/Variant.cpp +++ b/src/core/Variant.cpp @@ -1,6 +1,6 @@ #include "Variant.hpp" -#include +#include #include "Defs.hpp" diff --git a/src/core/Vector2.cpp b/src/core/Vector2.cpp index d17f828..46f1125 100644 --- a/src/core/Vector2.cpp +++ b/src/core/Vector2.cpp @@ -2,7 +2,7 @@ #include -#include +#include #include "String.hpp"