Change registered initializers / terminators to the single function with level argument.

pull/750/head
bruvzg 2022-05-04 11:00:14 +03:00
parent 20a17eb71d
commit 8bf5a532de
No known key found for this signature in database
GPG Key ID: 7960FCF39844EC38
6 changed files with 548 additions and 886 deletions

File diff suppressed because it is too large Load Diff

View File

@ -545,6 +545,9 @@ typedef struct {
void (*classdb_register_extension_class_property_subgroup)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_subgroup_name, const char *p_prefix);
void (*classdb_register_extension_class_signal)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_signal_name, const GDNativePropertyInfo *p_argument_info, GDNativeInt p_argument_count);
void (*classdb_unregister_extension_class)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name); /* Unregistering a parent class before a class that inherits it will result in failure. Inheritors must be unregistered first. */
void (*get_library_path)(const GDNativeExtensionClassLibraryPtr p_library, GDNativeStringPtr r_path);
} GDNativeInterface;
/* INITIALIZATION */
@ -553,7 +556,6 @@ typedef enum {
GDNATIVE_INITIALIZATION_CORE,
GDNATIVE_INITIALIZATION_SERVERS,
GDNATIVE_INITIALIZATION_SCENE,
GDNATIVE_INITIALIZATION_DRIVER,
GDNATIVE_INITIALIZATION_EDITOR,
GDNATIVE_MAX_INITIALIZATION_LEVEL,
} GDNativeInitializationLevel;

View File

@ -43,12 +43,20 @@ extern "C" void *token;
} // namespace internal
enum ModuleInitializationLevel {
MODULE_INITIALIZATION_LEVEL_CORE = GDNATIVE_INITIALIZATION_CORE,
MODULE_INITIALIZATION_LEVEL_SERVERS = GDNATIVE_INITIALIZATION_SERVERS,
MODULE_INITIALIZATION_LEVEL_SCENE = GDNATIVE_INITIALIZATION_SCENE,
MODULE_INITIALIZATION_LEVEL_EDITOR = GDNATIVE_INITIALIZATION_EDITOR
};
class GDExtensionBinding {
public:
using Callback = void (*)();
using Callback = void (*)(ModuleInitializationLevel p_level);
static Callback init_callbacks[GDNATIVE_MAX_INITIALIZATION_LEVEL];
static Callback terminate_callbacks[GDNATIVE_MAX_INITIALIZATION_LEVEL];
static Callback init_callback;
static Callback terminate_callback;
static GDNativeInitializationLevel minimum_initialization_level;
static GDNativeBool init(const GDNativeInterface *p_interface, const GDNativeExtensionClassLibraryPtr p_library, GDNativeInitialization *r_initialization);
public:
@ -66,16 +74,9 @@ public:
library(p_library),
initialization(r_initialization){};
void register_core_initializer(Callback p_core_init) const;
void register_server_initializer(Callback p_server_init) const;
void register_driver_initializer(Callback p_driver_init) const;
void register_scene_initializer(Callback p_scene_init) const;
void register_editor_initializer(Callback p_editor_init) const;
void register_core_terminator(Callback p_core_terminate) const;
void register_server_terminator(Callback p_server_terminate) const;
void register_scene_terminator(Callback p_scene_terminate) const;
void register_driver_terminator(Callback p_driver_terminate) const;
void register_editor_terminator(Callback p_editor_terminate) const;
void register_initializer(Callback p_init) const;
void register_terminator(Callback p_init) const;
void set_minimum_library_initialization_level(ModuleInitializationLevel p_level) const;
GDNativeBool init() const;
};

View File

@ -47,8 +47,9 @@ void *token = nullptr;
} // namespace internal
GDExtensionBinding::Callback GDExtensionBinding::init_callbacks[GDNATIVE_MAX_INITIALIZATION_LEVEL] = {};
GDExtensionBinding::Callback GDExtensionBinding::terminate_callbacks[GDNATIVE_MAX_INITIALIZATION_LEVEL] = {};
GDExtensionBinding::Callback GDExtensionBinding::init_callback = nullptr;
GDExtensionBinding::Callback GDExtensionBinding::terminate_callback = nullptr;
GDNativeInitializationLevel GDExtensionBinding::minimum_initialization_level = GDNATIVE_INITIALIZATION_CORE;
GDNativeBool GDExtensionBinding::init(const GDNativeInterface *p_interface, const GDNativeExtensionClassLibraryPtr p_library, GDNativeInitialization *r_initialization) {
internal::gdn_interface = p_interface;
@ -57,15 +58,9 @@ GDNativeBool GDExtensionBinding::init(const GDNativeInterface *p_interface, cons
r_initialization->initialize = initialize_level;
r_initialization->deinitialize = deinitialize_level;
r_initialization->minimum_initialization_level = minimum_initialization_level;
bool has_init = false;
for (int i = 0; i < GDNATIVE_MAX_INITIALIZATION_LEVEL; i++) {
if (init_callbacks[i]) {
r_initialization->minimum_initialization_level = GDNativeInitializationLevel(i);
has_init = true;
}
}
ERR_FAIL_COND_V_MSG(!has_init, false, "At least one initialization callback must be defined.");
ERR_FAIL_COND_V_MSG(init_callback == nullptr, false, "Initialization callback must be defined.");
Variant::init_bindings();
@ -75,8 +70,8 @@ GDNativeBool GDExtensionBinding::init(const GDNativeInterface *p_interface, cons
void GDExtensionBinding::initialize_level(void *userdata, GDNativeInitializationLevel p_level) {
ClassDB::current_level = p_level;
if (init_callbacks[p_level]) {
init_callbacks[p_level]();
if (init_callback) {
init_callback(static_cast<ModuleInitializationLevel>(p_level));
}
ClassDB::initialize(p_level);
@ -86,49 +81,21 @@ void GDExtensionBinding::deinitialize_level(void *userdata, GDNativeInitializati
ClassDB::current_level = p_level;
ClassDB::deinitialize(p_level);
if (terminate_callbacks[p_level]) {
terminate_callbacks[p_level]();
if (terminate_callback) {
terminate_callback(static_cast<ModuleInitializationLevel>(p_level));
}
}
void GDExtensionBinding::InitObject::register_core_initializer(Callback p_core_init) const {
GDExtensionBinding::init_callbacks[GDNATIVE_INITIALIZATION_CORE] = p_core_init;
void GDExtensionBinding::InitObject::register_initializer(Callback p_init) const {
GDExtensionBinding::init_callback = p_init;
}
void GDExtensionBinding::InitObject::register_server_initializer(Callback p_server_init) const {
GDExtensionBinding::init_callbacks[GDNATIVE_INITIALIZATION_SERVERS] = p_server_init;
void GDExtensionBinding::InitObject::register_terminator(Callback p_terminate) const {
GDExtensionBinding::terminate_callback = p_terminate;
}
void GDExtensionBinding::InitObject::register_scene_initializer(Callback p_scene_init) const {
GDExtensionBinding::init_callbacks[GDNATIVE_INITIALIZATION_SCENE] = p_scene_init;
}
void GDExtensionBinding::InitObject::register_driver_initializer(Callback p_driver_init) const {
GDExtensionBinding::init_callbacks[GDNATIVE_INITIALIZATION_DRIVER] = p_driver_init;
}
void GDExtensionBinding::InitObject::register_editor_initializer(Callback p_editor_init) const {
GDExtensionBinding::init_callbacks[GDNATIVE_INITIALIZATION_EDITOR] = p_editor_init;
}
void GDExtensionBinding::InitObject::register_core_terminator(Callback p_core_terminate) const {
GDExtensionBinding::terminate_callbacks[GDNATIVE_INITIALIZATION_CORE] = p_core_terminate;
}
void GDExtensionBinding::InitObject::register_server_terminator(Callback p_server_terminate) const {
GDExtensionBinding::terminate_callbacks[GDNATIVE_INITIALIZATION_SERVERS] = p_server_terminate;
}
void GDExtensionBinding::InitObject::register_scene_terminator(Callback p_scene_terminate) const {
GDExtensionBinding::terminate_callbacks[GDNATIVE_INITIALIZATION_SCENE] = p_scene_terminate;
}
void GDExtensionBinding::InitObject::register_driver_terminator(Callback p_driver_terminate) const {
GDExtensionBinding::terminate_callbacks[GDNATIVE_INITIALIZATION_DRIVER] = p_driver_terminate;
}
void GDExtensionBinding::InitObject::register_editor_terminator(Callback p_editor_terminate) const {
GDExtensionBinding::terminate_callbacks[GDNATIVE_INITIALIZATION_EDITOR] = p_editor_terminate;
void GDExtensionBinding::InitObject::set_minimum_library_initialization_level(ModuleInitializationLevel p_level) const {
GDExtensionBinding::minimum_initialization_level = static_cast<GDNativeInitializationLevel>(p_level);
}
GDNativeBool GDExtensionBinding::InitObject::init() const {

View File

@ -40,12 +40,20 @@
using namespace godot;
void register_example_types() {
void initialize_example_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
ClassDB::register_class<ExampleRef>();
ClassDB::register_class<Example>();
}
void unregister_example_types() {}
void uninitialize_example_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
}
extern "C" {
@ -54,8 +62,9 @@ extern "C" {
GDNativeBool GDN_EXPORT example_library_init(const GDNativeInterface *p_interface, const GDNativeExtensionClassLibraryPtr p_library, GDNativeInitialization *r_initialization) {
godot::GDExtensionBinding::InitObject init_obj(p_interface, p_library, r_initialization);
init_obj.register_scene_initializer(register_example_types);
init_obj.register_scene_terminator(unregister_example_types);
init_obj.register_initializer(initialize_example_module);
init_obj.register_terminator(uninitialize_example_module);
init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE);
return init_obj.init();
}

View File

@ -31,7 +31,10 @@
#ifndef EXAMPLE_REGISTER_TYPES_H
#define EXAMPLE_REGISTER_TYPES_H
void register_example_types();
void unregister_example_types();
#include <godot_cpp/core/class_db.hpp>
using namespace godot;
void initialize_example_module(ModuleInitializationLevel p_level);
void uninitialize_example_module(ModuleInitializationLevel p_level);
#endif // ! EXAMPLE_REGISTER_TYPES_H