Add in driver types and rejig code to make it easier to extend

pull/602/head
Bastiaan Olij 2021-09-11 12:31:10 +10:00
parent aef0f1e248
commit 02802b1d0e
3 changed files with 39 additions and 77 deletions

View File

@ -449,6 +449,8 @@ typedef enum {
GDNATIVE_INITIALIZATION_SERVERS, GDNATIVE_INITIALIZATION_SERVERS,
GDNATIVE_INITIALIZATION_SCENE, GDNATIVE_INITIALIZATION_SCENE,
GDNATIVE_INITIALIZATION_EDITOR, GDNATIVE_INITIALIZATION_EDITOR,
GDNATIVE_INITIALIZATION_DRIVER,
GDNATIVE_MAX_INITIALIZATION_LEVEL,
} GDNativeInitializationLevel; } GDNativeInitializationLevel;
typedef struct { typedef struct {

View File

@ -47,14 +47,8 @@ class GDExtensionBinding {
public: public:
using Callback = void (*)(); using Callback = void (*)();
static Callback core_init; static Callback init_callbacks[GDNATIVE_MAX_INITIALIZATION_LEVEL];
static Callback server_init; static Callback terminate_callbacks[GDNATIVE_MAX_INITIALIZATION_LEVEL];
static Callback scene_init;
static Callback editor_init;
static Callback core_terminate;
static Callback server_terminate;
static Callback scene_terminate;
static Callback editor_terminate;
static GDNativeBool init(const GDNativeInterface *p_interface, const GDNativeExtensionClassLibraryPtr p_library, GDNativeInitialization *r_initialization); static GDNativeBool init(const GDNativeInterface *p_interface, const GDNativeExtensionClassLibraryPtr p_library, GDNativeInitialization *r_initialization);
public: public:
@ -73,16 +67,18 @@ public:
InitObject(const GDNativeInterface *p_interface, const GDNativeExtensionClassLibraryPtr p_library, GDNativeInitialization *r_initialization) : InitObject(const GDNativeInterface *p_interface, const GDNativeExtensionClassLibraryPtr p_library, GDNativeInitialization *r_initialization) :
interface(p_interface), interface(p_interface),
library(p_library), library(p_library),
initialization(r_initialization) {} initialization(r_initialization) {};
void register_core_initializer(Callback p_core_init) const; void register_core_initializer(Callback p_core_init) const;
void register_server_initializer(Callback p_server_init) const; void register_server_initializer(Callback p_server_init) const;
void register_scene_initializer(Callback p_scene_init) const; void register_scene_initializer(Callback p_scene_init) const;
void register_editor_initializer(Callback p_editor_init) const; void register_editor_initializer(Callback p_editor_init) const;
void register_driver_initializer(Callback p_driver_init) const;
void register_core_terminator(Callback p_core_terminate) const; void register_core_terminator(Callback p_core_terminate) const;
void register_server_terminator(Callback p_server_terminate) const; void register_server_terminator(Callback p_server_terminate) const;
void register_scene_terminator(Callback p_scene_terminate) const; void register_scene_terminator(Callback p_scene_terminate) const;
void register_editor_terminator(Callback p_editor_terminate) const; void register_editor_terminator(Callback p_editor_terminate) const;
void register_driver_terminator(Callback p_driver_terminate) const;
GDNativeBool init() const; GDNativeBool init() const;
}; };

View File

@ -47,14 +47,8 @@ void *token = nullptr;
} // namespace internal } // namespace internal
GDExtensionBinding::Callback GDExtensionBinding::core_init = nullptr; GDExtensionBinding::Callback GDExtensionBinding::init_callbacks[GDNATIVE_MAX_INITIALIZATION_LEVEL] = {};
GDExtensionBinding::Callback GDExtensionBinding::server_init = nullptr; GDExtensionBinding::Callback GDExtensionBinding::terminate_callbacks[GDNATIVE_MAX_INITIALIZATION_LEVEL] = {};
GDExtensionBinding::Callback GDExtensionBinding::scene_init = nullptr;
GDExtensionBinding::Callback GDExtensionBinding::editor_init = nullptr;
GDExtensionBinding::Callback GDExtensionBinding::core_terminate = nullptr;
GDExtensionBinding::Callback GDExtensionBinding::server_terminate = nullptr;
GDExtensionBinding::Callback GDExtensionBinding::scene_terminate = nullptr;
GDExtensionBinding::Callback GDExtensionBinding::editor_terminate = nullptr;
GDNativeBool GDExtensionBinding::init(const GDNativeInterface *p_interface, const GDNativeExtensionClassLibraryPtr p_library, GDNativeInitialization *r_initialization) { GDNativeBool GDExtensionBinding::init(const GDNativeInterface *p_interface, const GDNativeExtensionClassLibraryPtr p_library, GDNativeInitialization *r_initialization) {
internal::interface = p_interface; internal::interface = p_interface;
@ -64,17 +58,14 @@ GDNativeBool GDExtensionBinding::init(const GDNativeInterface *p_interface, cons
r_initialization->initialize = initialize_level; r_initialization->initialize = initialize_level;
r_initialization->deinitialize = deinitialize_level; r_initialization->deinitialize = deinitialize_level;
if (core_init) { bool has_init = false;
r_initialization->minimum_initialization_level = GDNATIVE_INITIALIZATION_CORE; for (int i = 0; i < GDNATIVE_MAX_INITIALIZATION_LEVEL; i++) {
} else if (server_init) { if (init_callbacks[i]) {
r_initialization->minimum_initialization_level = GDNATIVE_INITIALIZATION_SERVERS; r_initialization->minimum_initialization_level = GDNativeInitializationLevel(i);
} else if (scene_init) { has_init = true;
r_initialization->minimum_initialization_level = GDNATIVE_INITIALIZATION_SCENE;
} else if (editor_init) {
r_initialization->minimum_initialization_level = GDNATIVE_INITIALIZATION_EDITOR;
} else {
ERR_FAIL_V_MSG(false, "At least one initialization callback must be defined.");
} }
}
ERR_FAIL_COND_V_MSG(!has_init, false, "At least one initialization callback must be defined.");
Variant::init_bindings(); Variant::init_bindings();
@ -83,55 +74,20 @@ GDNativeBool GDExtensionBinding::init(const GDNativeInterface *p_interface, cons
void GDExtensionBinding::initialize_level(void *userdata, GDNativeInitializationLevel p_level) { void GDExtensionBinding::initialize_level(void *userdata, GDNativeInitializationLevel p_level) {
ClassDB::current_level = p_level; ClassDB::current_level = p_level;
switch (p_level) {
case GDNATIVE_INITIALIZATION_CORE: if (init_callbacks[p_level]) {
if (core_init) { init_callbacks[p_level]();
core_init();
}
break;
case GDNATIVE_INITIALIZATION_SERVERS:
if (server_init) {
server_init();
}
break;
case GDNATIVE_INITIALIZATION_SCENE:
if (scene_init) {
scene_init();
}
break;
case GDNATIVE_INITIALIZATION_EDITOR:
if (editor_init) {
editor_init();
}
break;
} }
ClassDB::initialize(p_level); ClassDB::initialize(p_level);
} }
void GDExtensionBinding::deinitialize_level(void *userdata, GDNativeInitializationLevel p_level) { void GDExtensionBinding::deinitialize_level(void *userdata, GDNativeInitializationLevel p_level) {
ClassDB::current_level = p_level; ClassDB::current_level = p_level;
ClassDB::deinitialize(p_level); ClassDB::deinitialize(p_level);
switch (p_level) {
case GDNATIVE_INITIALIZATION_CORE: if (terminate_callbacks[p_level]) {
if (core_terminate) { terminate_callbacks[p_level]();
core_terminate();
}
break;
case GDNATIVE_INITIALIZATION_SERVERS:
if (server_terminate) {
server_terminate();
}
break;
case GDNATIVE_INITIALIZATION_SCENE:
if (scene_terminate) {
scene_terminate();
}
break;
case GDNATIVE_INITIALIZATION_EDITOR:
if (editor_terminate) {
editor_terminate();
}
break;
} }
} }
@ -147,35 +103,43 @@ void GDExtensionBinding::free_instance_callback(void *p_token, void *p_instance,
} }
void GDExtensionBinding::InitObject::register_core_initializer(Callback p_core_init) const { void GDExtensionBinding::InitObject::register_core_initializer(Callback p_core_init) const {
GDExtensionBinding::core_init = p_core_init; GDExtensionBinding::init_callbacks[GDNATIVE_INITIALIZATION_CORE] = p_core_init;
} }
void GDExtensionBinding::InitObject::register_server_initializer(Callback p_server_init) const { void GDExtensionBinding::InitObject::register_server_initializer(Callback p_server_init) const {
GDExtensionBinding::server_init = p_server_init; GDExtensionBinding::init_callbacks[GDNATIVE_INITIALIZATION_SERVERS] = p_server_init;
} }
void GDExtensionBinding::InitObject::register_scene_initializer(Callback p_scene_init) const { void GDExtensionBinding::InitObject::register_scene_initializer(Callback p_scene_init) const {
GDExtensionBinding::scene_init = p_scene_init; GDExtensionBinding::init_callbacks[GDNATIVE_INITIALIZATION_SCENE] = p_scene_init;
} }
void GDExtensionBinding::InitObject::register_editor_initializer(Callback p_editor_init) const { void GDExtensionBinding::InitObject::register_editor_initializer(Callback p_editor_init) const {
GDExtensionBinding::editor_init = p_editor_init; GDExtensionBinding::init_callbacks[GDNATIVE_INITIALIZATION_EDITOR] = p_editor_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_core_terminator(Callback p_core_terminate) const { void GDExtensionBinding::InitObject::register_core_terminator(Callback p_core_terminate) const {
GDExtensionBinding::core_terminate = p_core_terminate; GDExtensionBinding::terminate_callbacks[GDNATIVE_INITIALIZATION_CORE] = p_core_terminate;
} }
void GDExtensionBinding::InitObject::register_server_terminator(Callback p_server_terminate) const { void GDExtensionBinding::InitObject::register_server_terminator(Callback p_server_terminate) const {
GDExtensionBinding::server_terminate = p_server_terminate; GDExtensionBinding::terminate_callbacks[GDNATIVE_INITIALIZATION_SERVERS] = p_server_terminate;
} }
void GDExtensionBinding::InitObject::register_scene_terminator(Callback p_scene_terminate) const { void GDExtensionBinding::InitObject::register_scene_terminator(Callback p_scene_terminate) const {
GDExtensionBinding::scene_terminate = p_scene_terminate; GDExtensionBinding::terminate_callbacks[GDNATIVE_INITIALIZATION_SCENE] = p_scene_terminate;
} }
void GDExtensionBinding::InitObject::register_editor_terminator(Callback p_editor_terminate) const { void GDExtensionBinding::InitObject::register_editor_terminator(Callback p_editor_terminate) const {
GDExtensionBinding::editor_terminate = p_editor_terminate; GDExtensionBinding::terminate_callbacks[GDNATIVE_INITIALIZATION_EDITOR] = p_editor_terminate;
}
void GDExtensionBinding::InitObject::register_driver_terminator(Callback p_driver_terminate) const {
GDExtensionBinding::terminate_callbacks[GDNATIVE_INITIALIZATION_DRIVER] = p_driver_terminate;
} }
GDNativeBool GDExtensionBinding::InitObject::init() const { GDNativeBool GDExtensionBinding::InitObject::init() const {