diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-03-10 14:09:17 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-10 14:09:17 +0100 |
commit | 9b597364737cbc04ccb5ee8e5c18bccb4dd31362 (patch) | |
tree | 5e2cc8006e77d01596e323ad9bac586920fc080a /core | |
parent | 90faf04f293fcdf636f641bfe3d88c5857695d9b (diff) | |
parent | 6f51eca1e3045571ccc68414a922e8b0229111f0 (diff) |
Merge pull request #58972 from reduz/expose-more-gdextension
Diffstat (limited to 'core')
-rw-r--r-- | core/extension/gdnative_interface.h | 2 | ||||
-rw-r--r-- | core/extension/native_extension.cpp | 1 | ||||
-rw-r--r-- | core/io/resource.cpp | 20 | ||||
-rw-r--r-- | core/object/class_db.cpp | 13 | ||||
-rw-r--r-- | core/object/class_db.h | 17 | ||||
-rw-r--r-- | core/object/make_virtuals.py | 5 | ||||
-rw-r--r-- | core/object/object.h | 9 | ||||
-rw-r--r-- | core/register_core_types.cpp | 32 |
8 files changed, 76 insertions, 23 deletions
diff --git a/core/extension/gdnative_interface.h b/core/extension/gdnative_interface.h index 62934d1d73..76e87eaf23 100644 --- a/core/extension/gdnative_interface.h +++ b/core/extension/gdnative_interface.h @@ -194,6 +194,7 @@ typedef void *GDExtensionClassInstancePtr; typedef GDNativeBool (*GDNativeExtensionClassSet)(GDExtensionClassInstancePtr p_instance, const GDNativeStringNamePtr p_name, const GDNativeVariantPtr p_value); typedef GDNativeBool (*GDNativeExtensionClassGet)(GDExtensionClassInstancePtr p_instance, const GDNativeStringNamePtr p_name, GDNativeVariantPtr r_ret); +typedef uint64_t (*GDNativeExtensionClassGetRID)(GDExtensionClassInstancePtr p_instance); typedef struct { uint32_t type; @@ -228,6 +229,7 @@ typedef struct { GDNativeExtensionClassCreateInstance create_instance_func; /* this one is mandatory */ GDNativeExtensionClassFreeInstance free_instance_func; /* this one is mandatory */ GDNativeExtensionClassGetVirtual get_virtual_func; + GDNativeExtensionClassGetRID get_rid_func; void *class_userdata; } GDNativeExtensionClassCreationInfo; diff --git a/core/extension/native_extension.cpp b/core/extension/native_extension.cpp index 325ccec6c4..1a39c937e8 100644 --- a/core/extension/native_extension.cpp +++ b/core/extension/native_extension.cpp @@ -158,6 +158,7 @@ void NativeExtension::_register_extension_class(const GDNativeExtensionClassLibr extension->native_extension.create_instance = p_extension_funcs->create_instance_func; extension->native_extension.free_instance = p_extension_funcs->free_instance_func; extension->native_extension.get_virtual = p_extension_funcs->get_virtual_func; + extension->native_extension.get_rid = p_extension_funcs->get_rid_func; ClassDB::register_extension_class(&extension->native_extension); } diff --git a/core/io/resource.cpp b/core/io/resource.cpp index 66d5c54b53..f90a6e9304 100644 --- a/core/io/resource.cpp +++ b/core/io/resource.cpp @@ -290,6 +290,21 @@ void Resource::_take_over_path(const String &p_path) { } RID Resource::get_rid() const { + if (get_script_instance()) { + Callable::CallError ce; + RID ret = get_script_instance()->callp(SNAME("_get_rid"), nullptr, 0, ce); + if (ce.error == Callable::CallError::CALL_OK && ret.is_valid()) { + return ret; + } + } + if (_get_extension() && _get_extension()->get_rid) { + RID ret; + ret.from_uint64(_get_extension()->get_rid(_get_extension_instance())); + if (ret.is_valid()) { + return ret; + } + } + return RID(); } @@ -428,6 +443,11 @@ void Resource::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resource_local_to_scene"), "set_local_to_scene", "is_local_to_scene"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_path", "get_path"); ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "resource_name"), "set_name", "get_name"); + + MethodInfo get_rid_bind("_get_rid"); + get_rid_bind.return_val.type = Variant::RID; + + ::ClassDB::add_virtual_method(get_class_static(), get_rid_bind, true, Vector<String>(), true); } Resource::Resource() : diff --git a/core/object/class_db.cpp b/core/object/class_db.cpp index 4a71597f84..08e12dfcaa 100644 --- a/core/object/class_db.cpp +++ b/core/object/class_db.cpp @@ -557,6 +557,19 @@ bool ClassDB::can_instantiate(const StringName &p_class) { return (!ti->disabled && ti->creation_func != nullptr && !(ti->native_extension && !ti->native_extension->create_instance)); } +bool ClassDB::is_virtual(const StringName &p_class) { + OBJTYPE_RLOCK; + + ClassInfo *ti = classes.getptr(p_class); + ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'."); +#ifdef TOOLS_ENABLED + if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) { + return false; + } +#endif + return (!ti->disabled && ti->creation_func != nullptr && !(ti->native_extension && !ti->native_extension->create_instance) && ti->is_virtual); +} + void ClassDB::_add_class2(const StringName &p_class, const StringName &p_inherits) { OBJTYPE_WLOCK; diff --git a/core/object/class_db.h b/core/object/class_db.h index 5d258a29bf..580ae3582f 100644 --- a/core/object/class_db.h +++ b/core/object/class_db.h @@ -118,6 +118,7 @@ public: StringName name; bool disabled = false; bool exposed = false; + bool is_virtual = false; Object *(*creation_func)() = nullptr; ClassInfo() {} @@ -156,20 +157,21 @@ public: } template <class T> - static void register_class() { + static void register_class(bool p_virtual = false) { GLOBAL_LOCK_FUNCTION; T::initialize_class(); ClassInfo *t = classes.getptr(T::get_class_static()); ERR_FAIL_COND(!t); t->creation_func = &creator<T>; t->exposed = true; + t->is_virtual = p_virtual; t->class_ptr = T::get_class_ptr_static(); t->api = current_api; T::register_custom_data_to_otdb(); } template <class T> - static void register_virtual_class() { + static void register_abstract_class() { GLOBAL_LOCK_FUNCTION; T::initialize_class(); ClassInfo *t = classes.getptr(T::get_class_static()); @@ -210,6 +212,7 @@ public: static bool class_exists(const StringName &p_class); static bool is_parent_class(const StringName &p_class, const StringName &p_inherits); static bool can_instantiate(const StringName &p_class); + static bool is_virtual(const StringName &p_class); static Object *instantiate(const StringName &p_class); static void set_object_extension_instance(Object *p_object, const StringName &p_class, GDExtensionClassInstancePtr p_instance); @@ -436,9 +439,13 @@ _FORCE_INLINE_ Vector<Error> errarray(P... p_args) { if (!GD_IS_DEFINED(ClassDB_Disable_##m_class)) { \ ::ClassDB::register_class<m_class>(); \ } -#define GDREGISTER_VIRTUAL_CLASS(m_class) \ - if (!GD_IS_DEFINED(ClassDB_Disable_##m_class)) { \ - ::ClassDB::register_virtual_class<m_class>(); \ +#define GDREGISTER_VIRTUAL_CLASS(m_class) \ + if (!GD_IS_DEFINED(ClassDB_Disable_##m_class)) { \ + ::ClassDB::register_class<m_class>(true); \ + } +#define GDREGISTER_ABSTRACT_CLASS(m_class) \ + if (!GD_IS_DEFINED(ClassDB_Disable_##m_class)) { \ + ::ClassDB::register_abstract_class<m_class>(); \ } #include "core/disabled_classes.gen.h" diff --git a/core/object/make_virtuals.py b/core/object/make_virtuals.py index 552f2fa2bf..2e909b8042 100644 --- a/core/object/make_virtuals.py +++ b/core/object/make_virtuals.py @@ -3,6 +3,7 @@ proto = """ StringName _gdvirtual_##m_name##_sn = #m_name;\\ mutable bool _gdvirtual_##m_name##_initialized = false;\\ mutable GDNativeExtensionClassCallVirtual _gdvirtual_##m_name = nullptr;\\ +template<bool required>\\ _FORCE_INLINE_ bool _gdvirtual_##m_name##_call($CALLARGS) $CONST { \\ ScriptInstance *script_instance = ((Object*)(this))->get_script_instance();\\ if (script_instance) {\\ @@ -25,6 +26,10 @@ _FORCE_INLINE_ bool _gdvirtual_##m_name##_call($CALLARGS) $CONST { \\ $CALLPTRRET\\ return true;\\ }\\ + \\ + if (required) {\\ + WARN_PRINT_ONCE("Required virtual method: "+get_class()+"::" + #m_name + " must be overriden before calling.");\\ + }\\ \\ return false;\\ }\\ diff --git a/core/object/object.h b/core/object/object.h index 8a8b6b5487..3aa7e3f9f2 100644 --- a/core/object/object.h +++ b/core/object/object.h @@ -255,6 +255,7 @@ struct ObjectNativeExtension { GDNativeExtensionClassToString to_string; GDNativeExtensionClassReference reference; GDNativeExtensionClassReference unreference; + GDNativeExtensionClassGetRID get_rid; _FORCE_INLINE_ bool is_class(const String &p_class) const { const ObjectNativeExtension *e = this; @@ -273,8 +274,12 @@ struct ObjectNativeExtension { GDNativeExtensionClassGetVirtual get_virtual; }; -#define GDVIRTUAL_CALL(m_name, ...) _gdvirtual_##m_name##_call(__VA_ARGS__) -#define GDVIRTUAL_CALL_PTR(m_obj, m_name, ...) m_obj->_gdvirtual_##m_name##_call(__VA_ARGS__) +#define GDVIRTUAL_CALL(m_name, ...) _gdvirtual_##m_name##_call<false>(__VA_ARGS__) +#define GDVIRTUAL_CALL_PTR(m_obj, m_name, ...) m_obj->_gdvirtual_##m_name##_call<false>(__VA_ARGS__) + +#define GDVIRTUAL_REQUIRED_CALL(m_name, ...) _gdvirtual_##m_name##_call<true>(__VA_ARGS__) +#define GDVIRTUAL_REQUIRED_CALL_PTR(m_obj, m_name, ...) m_obj->_gdvirtual_##m_name##_call<true>(__VA_ARGS__) + #ifdef DEBUG_METHODS_ENABLED #define GDVIRTUAL_BIND(m_name, ...) ::ClassDB::add_virtual_method(get_class_static(), _gdvirtual_##m_name##_get_method_info(), true, sarray(__VA_ARGS__)); #else diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index 63aa8050c4..2aa47c6c96 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -141,7 +141,7 @@ void register_core_types() { GDREGISTER_CLASS(Object); - GDREGISTER_VIRTUAL_CLASS(Script); + GDREGISTER_ABSTRACT_CLASS(Script); GDREGISTER_CLASS(RefCounted); GDREGISTER_CLASS(WeakRef); @@ -149,12 +149,12 @@ void register_core_types() { GDREGISTER_CLASS(Image); GDREGISTER_CLASS(Shortcut); - GDREGISTER_VIRTUAL_CLASS(InputEvent); - GDREGISTER_VIRTUAL_CLASS(InputEventWithModifiers); - GDREGISTER_VIRTUAL_CLASS(InputEventFromWindow); + GDREGISTER_ABSTRACT_CLASS(InputEvent); + GDREGISTER_ABSTRACT_CLASS(InputEventWithModifiers); + GDREGISTER_ABSTRACT_CLASS(InputEventFromWindow); GDREGISTER_CLASS(InputEventKey); GDREGISTER_CLASS(InputEventShortcut); - GDREGISTER_VIRTUAL_CLASS(InputEventMouse); + GDREGISTER_ABSTRACT_CLASS(InputEventMouse); GDREGISTER_CLASS(InputEventMouseButton); GDREGISTER_CLASS(InputEventMouseMotion); GDREGISTER_CLASS(InputEventJoypadButton); @@ -162,21 +162,21 @@ void register_core_types() { GDREGISTER_CLASS(InputEventScreenDrag); GDREGISTER_CLASS(InputEventScreenTouch); GDREGISTER_CLASS(InputEventAction); - GDREGISTER_VIRTUAL_CLASS(InputEventGesture); + GDREGISTER_ABSTRACT_CLASS(InputEventGesture); GDREGISTER_CLASS(InputEventMagnifyGesture); GDREGISTER_CLASS(InputEventPanGesture); GDREGISTER_CLASS(InputEventMIDI); // Network - GDREGISTER_VIRTUAL_CLASS(IP); + GDREGISTER_ABSTRACT_CLASS(IP); - GDREGISTER_VIRTUAL_CLASS(StreamPeer); + GDREGISTER_ABSTRACT_CLASS(StreamPeer); GDREGISTER_CLASS(StreamPeerExtension); GDREGISTER_CLASS(StreamPeerBuffer); GDREGISTER_CLASS(StreamPeerTCP); GDREGISTER_CLASS(TCPServer); - GDREGISTER_VIRTUAL_CLASS(PacketPeer); + GDREGISTER_ABSTRACT_CLASS(PacketPeer); GDREGISTER_CLASS(PacketPeerExtension); GDREGISTER_CLASS(PacketPeerStream); GDREGISTER_CLASS(PacketPeerUDP); @@ -200,7 +200,7 @@ void register_core_types() { resource_format_loader_crypto.instantiate(); ResourceLoader::add_resource_format_loader(resource_format_loader_crypto); - GDREGISTER_VIRTUAL_CLASS(MultiplayerPeer); + GDREGISTER_ABSTRACT_CLASS(MultiplayerPeer); GDREGISTER_CLASS(MultiplayerPeerExtension); GDREGISTER_CLASS(MultiplayerAPI); GDREGISTER_CLASS(MainLoop); @@ -226,19 +226,19 @@ void register_core_types() { GDREGISTER_CLASS(PCKPacker); GDREGISTER_CLASS(PackedDataContainer); - GDREGISTER_VIRTUAL_CLASS(PackedDataContainerRef); + GDREGISTER_ABSTRACT_CLASS(PackedDataContainerRef); GDREGISTER_CLASS(AStar); GDREGISTER_CLASS(AStar2D); GDREGISTER_CLASS(EncodedObjectAsID); GDREGISTER_CLASS(RandomNumberGenerator); - GDREGISTER_VIRTUAL_CLASS(ResourceImporter); + GDREGISTER_ABSTRACT_CLASS(ResourceImporter); GDREGISTER_CLASS(NativeExtension); - GDREGISTER_VIRTUAL_CLASS(NativeExtensionManager); + GDREGISTER_ABSTRACT_CLASS(NativeExtensionManager); - GDREGISTER_VIRTUAL_CLASS(ResourceUID); + GDREGISTER_ABSTRACT_CLASS(ResourceUID); GDREGISTER_CLASS(EngineProfiler); @@ -276,7 +276,7 @@ void register_core_settings() { void register_core_singletons() { GDREGISTER_CLASS(ProjectSettings); - GDREGISTER_VIRTUAL_CLASS(IP); + GDREGISTER_ABSTRACT_CLASS(IP); GDREGISTER_CLASS(core_bind::Geometry2D); GDREGISTER_CLASS(core_bind::Geometry3D); GDREGISTER_CLASS(core_bind::ResourceLoader); @@ -286,7 +286,7 @@ void register_core_singletons() { GDREGISTER_CLASS(core_bind::special::ClassDB); GDREGISTER_CLASS(core_bind::Marshalls); GDREGISTER_CLASS(TranslationServer); - GDREGISTER_VIRTUAL_CLASS(Input); + GDREGISTER_ABSTRACT_CLASS(Input); GDREGISTER_CLASS(InputMap); GDREGISTER_CLASS(Expression); GDREGISTER_CLASS(core_bind::EngineDebugger); |