diff options
-rw-r--r-- | src/init.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/init.cpp b/src/init.cpp index 75a89e9..2b73cf1 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -2,6 +2,71 @@ #include "WebRTCLibPeerConnection.hpp" #include "net/WebRTCPeerConnectionNative.hpp" #include <gdnative_api_struct.gen.h> +#include <net/godot_net.h> + +/* Singleton */ +static bool _singleton = false; +static const godot_object *_singleton_lib = NULL; +static const godot_gdnative_core_api_struct *_singleton_api = NULL; +static godot_class_constructor _create_ns_cb = NULL; +static godot_method_bind *_set_script_mb = NULL; +static godot_method_bind *_set_class_name_mb = NULL; +static godot_method_bind *_set_library_mb = NULL; + +void unregistered() { + _singleton = false; // We are no longer the active singleton +} + +godot_error create_peer_connection_wp(godot_object *out) { + ERR_FAIL_COND_V(!_singleton, GODOT_FAILED); + // Create Script + godot_object *script = _create_ns_cb(); + ERR_FAIL_COND_V(!script, GODOT_FAILED); + + const void *args[] = { (void *)_singleton_lib }; + _singleton_api->godot_method_bind_ptrcall(_set_library_mb, script, args, nullptr); + + godot_string s; + _singleton_api->godot_string_new(&s); + _singleton_api->godot_string_parse_utf8(&s, "WebRTCLibPeerConnection"); + const void *args2[] = { (void *)&s }; + _singleton_api->godot_method_bind_ptrcall(_set_class_name_mb, script, args2, nullptr); + _singleton_api->godot_string_destroy(&s); + + // Bind script to Object + const void *args3[] = { (void *)script }; + _singleton_api->godot_method_bind_ptrcall(_set_script_mb, out, args3, nullptr); + + return GODOT_OK; +} + +godot_net_webrtc_library library = { + {3, 2}, + &unregistered, + &create_peer_connection_wp, + NULL, +}; + +extern "C" void GDN_EXPORT godot_gdnative_singleton() { + if (WebRTCPeerConnectionNative::_net_api) { + ERR_FAIL_COND(!godot::gdnlib); + _singleton_lib = godot::gdnlib; + ERR_FAIL_COND(!godot::api); + _singleton_api = godot::api; + _create_ns_cb = godot::api->godot_get_class_constructor("NativeScript"); + ERR_FAIL_COND(!_create_ns_cb); + _set_script_mb = godot::api->godot_method_bind_get_method("Object", "set_script"); + ERR_FAIL_COND(!_set_script_mb); + _set_class_name_mb = godot::api->godot_method_bind_get_method("NativeScript", "set_class_name"); + ERR_FAIL_COND(!_set_class_name_mb); + _set_library_mb = godot::api->godot_method_bind_get_method("NativeScript", "set_library"); + ERR_FAIL_COND(!_set_library_mb); + // If registration is successful _singleton will be set to true + _singleton = WebRTCPeerConnectionNative::_net_api->godot_net_set_webrtc_library(&library) == GODOT_OK; + if (!_singleton) + ERR_PRINT("Failed initializing webrtc singleton library"); + } +} /* Godot export stuff */ extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) { @@ -25,6 +90,9 @@ extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) { } extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o) { + if (_singleton) { // If we are the active singleton, unregister + WebRTCPeerConnectionNative::_net_api->godot_net_set_webrtc_library(NULL); + } godot::Godot::gdnative_terminate(o); } |