From 09c8bf94df95d62d393fc6edd4fb6af33a3d7352 Mon Sep 17 00:00:00 2001 From: Marc Gilleron Date: Sat, 30 May 2020 19:20:13 +0100 Subject: [PATCH] Rework the way custom class instances are created from C++ --- include/core/Godot.hpp | 151 ++++++++++++++++++++++++++++------------- 1 file changed, 105 insertions(+), 46 deletions(-) diff --git a/include/core/Godot.hpp b/include/core/Godot.hpp index 47896328..c196da26 100644 --- a/include/core/Godot.hpp +++ b/include/core/Godot.hpp @@ -17,59 +17,118 @@ #include "GodotGlobal.hpp" -#include -#include - namespace godot { +namespace detail { -template -T *as(const Object *obj) { - return (obj) ? (T *)godot::nativescript_api->godot_nativescript_get_userdata(obj->_owner) : nullptr; -} - +// Godot classes are wrapped by heap-allocated instances mimicking them through the C API. +// They all inherit `_Wrapped`. template T *get_wrapper(godot_object *obj) { return (T *)godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, obj); } -#define GODOT_CLASS(Name, Base) \ - \ -public: \ - inline static const char *___get_type_name() { return static_cast(#Name); } \ - enum { ___CLASS_IS_SCRIPT = 1, \ - }; \ - inline static Name *_new() { \ - godot::NativeScript *script = godot::NativeScript::_new(); \ - script->set_library(godot::get_wrapper((godot_object *)godot::gdnlib)); \ - script->set_class_name(#Name); \ - Name *instance = godot::as(script->new_()); \ - return instance; \ - } \ - inline static size_t ___get_id() { return typeid(Name).hash_code(); } \ - inline static size_t ___get_base_id() { return typeid(Base).hash_code(); } \ - inline static const char *___get_base_type_name() { return Base::___get_class_name(); } \ - inline static Object *___get_from_variant(godot::Variant a) { return (godot::Object *)godot::as(godot::Object::___get_from_variant(a)); } \ - \ +// Custom class instances are not obtainable by just casting the pointer to the base class they inherit, +// partly because in Godot, scripts are not instances of the classes themselves, they are only attached to them. +// Yet we want to "fake" it as if they were the same entity. +template +T *get_custom_class_instance(const Object *obj) { + return (obj) ? (T *)godot::nativescript_api->godot_nativescript_get_userdata(obj->_owner) : nullptr; +} + +template +inline T *create_custom_class_instance() { + // Usually, script instances hold a reference to their NativeScript resource. + // that resource is obtained from a `.gdns` file, which in turn exists because + // of the resource system of Godot. We can't cleanly hardcode that here, + // so the easiest for now (though not really clean) is to create new resource instances, + // individually attached to the script instances. + + // We cannot use wrappers because of https://github.com/godotengine/godot/issues/39181 + // godot::NativeScript *script = godot::NativeScript::_new(); + // script->set_library(get_wrapper((godot_object *)godot::gdnlib)); + // script->set_class_name(T::___get_type_name()); + + // So we use the C API directly. + static godot_class_constructor script_constructor = godot::api->godot_get_class_constructor("NativeScript"); + static godot_method_bind *mb_set_library = godot::api->godot_method_bind_get_method("NativeScript", "set_library"); + static godot_method_bind *mb_set_class_name = godot::api->godot_method_bind_get_method("NativeScript", "set_class_name"); + godot_object *script = script_constructor(); + { + const void *args[] = { godot::gdnlib }; + godot::api->godot_method_bind_ptrcall(mb_set_library, script, args, nullptr); + } + { + const String class_name = T::___get_type_name(); + const void *args[] = { &class_name }; + godot::api->godot_method_bind_ptrcall(mb_set_class_name, script, args, nullptr); + } + + // Now to instanciate T, we initially did this, however in case of Reference it returns a variant with refcount + // already initialized, which woud cause inconsistent behavior compared to other classes (we still have to return a pointer). + //Variant instance_variant = script->new_(); + //T *instance = godot::get_custom_class_instance(instance_variant); + + // So we should do this instead, however while convenient, it uses unnecessary wrapper objects. + // Object *base_obj = T::___new_godot_base(); + // base_obj->set_script(script); + // return get_custom_class_instance(base_obj); + + // Again using the C API to do exactly what we have to do. + static godot_class_constructor base_constructor = godot::api->godot_get_class_constructor(T::___get_godot_base_class_name()); + static godot_method_bind *mb_set_script = godot::api->godot_method_bind_get_method("Object", "set_script"); + godot_object *base_obj = base_constructor(); + { + const void *args[] = { script }; + godot::api->godot_method_bind_ptrcall(mb_set_script, base_obj, args, nullptr); + } + + return (T *)godot::nativescript_api->godot_nativescript_get_userdata(base_obj); +} + +} // namespace detail + +// Used in the definition of a custom class where the base is a Godot class +#define GODOT_CLASS(Name, Base) \ + \ +public: \ + inline static const char *___get_type_name() { return #Name; } \ + enum { ___CLASS_IS_SCRIPT = 1 }; \ + inline static const char *___get_godot_base_class_name() { \ + return Base::___get_class_name(); \ + } \ + inline static Name *_new() { \ + return godot::detail::create_custom_class_instance(); \ + } \ + inline static size_t ___get_id() { return typeid(Name).hash_code(); } \ + inline static size_t ___get_base_id() { return typeid(Base).hash_code(); } \ + inline static const char *___get_base_type_name() { return Base::___get_class_name(); } \ + inline static godot::Object *___get_from_variant(godot::Variant a) { \ + return (godot::Object *)godot::detail::get_custom_class_instance( \ + godot::Object::___get_from_variant(a)); \ + } \ + \ private: -#define GODOT_SUBCLASS(Name, Base) \ - \ -public: \ - inline static const char *___get_type_name() { return static_cast(#Name); } \ - enum { ___CLASS_IS_SCRIPT = 1, \ - }; \ - inline static Name *_new() { \ - godot::NativeScript *script = godot::NativeScript::_new(); \ - script->set_library(godot::get_wrapper((godot_object *)godot::gdnlib)); \ - script->set_class_name(#Name); \ - Name *instance = godot::as(script->new_()); \ - return instance; \ - } \ - inline static size_t ___get_id() { return typeid(Name).hash_code(); }; \ - inline static size_t ___get_base_id() { return typeid(Base).hash_code(); }; \ - inline static const char *___get_base_type_name() { return #Base; } \ - inline static Object *___get_from_variant(godot::Variant a) { return (godot::Object *)godot::as(godot::Object::___get_from_variant(a)); } \ - \ +// Used in the definition of a custom class where the base is another custom class +#define GODOT_SUBCLASS(Name, Base) \ + \ +public: \ + inline static const char *___get_type_name() { return #Name; } \ + enum { ___CLASS_IS_SCRIPT = 1 }; \ + inline static const char *___get_godot_base_class_name() { \ + return Base::___get_godot_base_class_name(); \ + } \ + inline static Name *_new() { \ + return godot::detail::create_custom_class_instance(); \ + } \ + inline static size_t ___get_id() { return typeid(Name).hash_code(); }; \ + inline static size_t ___get_base_id() { return typeid(Base).hash_code(); }; \ + inline static const char *___get_base_type_name() { return Base::___get_type_name(); } \ + inline static godot::Object *___get_from_variant(godot::Variant a) { \ + return (godot::Object *)godot::detail::get_custom_class_instance( \ + godot::Object::___get_from_variant(a)); \ + } \ + \ private: template @@ -316,7 +375,7 @@ void register_property(const char *name, P(T::*var), P default_value, godot_meth usage = (godot_property_usage_flags)((int)usage | GODOT_PROPERTY_USAGE_SCRIPT_VARIABLE); if (def_val.get_type() == Variant::OBJECT) { - Object *o = get_wrapper(def_val.operator godot_object *()); + Object *o = detail::get_wrapper(def_val.operator godot_object *()); if (o && o->is_class("Resource")) { hint = (godot_property_hint)((int)hint | GODOT_PROPERTY_HINT_RESOURCE_TYPE); hint_string = o->get_class(); @@ -460,7 +519,7 @@ T *Object::cast_to(const Object *obj) { } if (godot::_TagDB::is_type_compatible(typeid(T).hash_code(), have_tag)) { - return (T::___CLASS_IS_SCRIPT) ? godot::as(obj) : (T *)obj; + return (T::___CLASS_IS_SCRIPT) ? detail::get_custom_class_instance(obj) : (T *)obj; } else { return nullptr; }