2023-05-23 19:01:39 +02:00
|
|
|
/*************************************************************************/
|
2023-10-23 14:07:57 +02:00
|
|
|
/* PandemoniumGlobal.cpp */
|
2023-05-23 19:01:39 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
2023-10-23 14:07:57 +02:00
|
|
|
/* PANDEMONIUM ENGINE */
|
|
|
|
/* https://pandemoniumengine.org */
|
2023-05-23 19:01:39 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
2023-10-23 14:07:57 +02:00
|
|
|
/* Copyright (c) 2014-2022 Pandemonium Engine contributors (cf. AUTHORS.md). */
|
2023-05-23 19:01:39 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2023-10-23 14:41:55 +02:00
|
|
|
#include "pandemonium_global.h"
|
2023-05-23 19:01:39 +02:00
|
|
|
|
2023-10-23 14:41:55 +02:00
|
|
|
#include "array.h"
|
2023-10-23 19:24:53 +02:00
|
|
|
#include "ustring.h"
|
2023-05-23 19:01:39 +02:00
|
|
|
|
2023-10-23 14:41:55 +02:00
|
|
|
#include "wrapped.h"
|
2023-05-23 19:01:39 +02:00
|
|
|
|
2023-05-31 13:31:45 +02:00
|
|
|
static GDCALLINGCONV void *wrapper_create(void *data, const void *type_tag, pandemonium_object *instance) {
|
2023-10-23 17:11:10 +02:00
|
|
|
_Wrapped *wrapper_memory = (_Wrapped *)Pandemonium::api->pandemonium_alloc(sizeof(_Wrapped));
|
2023-05-23 19:01:39 +02:00
|
|
|
|
|
|
|
if (!wrapper_memory)
|
|
|
|
return NULL;
|
|
|
|
wrapper_memory->_owner = instance;
|
|
|
|
wrapper_memory->_type_tag = (size_t)type_tag;
|
|
|
|
|
|
|
|
return (void *)wrapper_memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GDCALLINGCONV void wrapper_destroy(void *data, void *wrapper) {
|
|
|
|
if (wrapper)
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::api->pandemonium_free(wrapper);
|
2023-05-23 19:01:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void *_RegisterState::nativescript_handle;
|
|
|
|
int _RegisterState::language_index;
|
|
|
|
|
2023-10-23 22:07:56 +02:00
|
|
|
const pandemonium_gdnative_core_api_struct *Pandemonium::api = NULL;
|
2023-05-23 19:01:39 +02:00
|
|
|
|
2023-10-23 22:07:56 +02:00
|
|
|
const pandemonium_gdnative_ext_nativescript_api_struct *Pandemonium::nativescript_api = NULL;
|
|
|
|
const pandemonium_gdnative_ext_pluginscript_api_struct *Pandemonium::pluginscript_api = NULL;
|
|
|
|
const pandemonium_gdnative_ext_android_api_struct *Pandemonium::android_api = NULL;
|
|
|
|
const pandemonium_gdnative_ext_videodecoder_api_struct *Pandemonium::videodecoder_api = NULL;
|
|
|
|
const pandemonium_gdnative_ext_net_api_struct *Pandemonium::net_api = NULL;
|
2023-05-23 19:01:39 +02:00
|
|
|
|
2023-10-23 22:07:56 +02:00
|
|
|
const void *Pandemonium::gdnlib = NULL;
|
2023-05-23 19:01:39 +02:00
|
|
|
|
2023-10-23 14:07:57 +02:00
|
|
|
void Pandemonium::print(const String &message) {
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::api->pandemonium_print((pandemonium_string *)&message);
|
2023-05-23 19:01:39 +02:00
|
|
|
}
|
|
|
|
|
2023-10-23 14:07:57 +02:00
|
|
|
void Pandemonium::print_warning(const String &description, const String &function, const String &file, int line) {
|
2023-05-23 19:01:39 +02:00
|
|
|
int len;
|
|
|
|
|
|
|
|
char *c_desc = description.alloc_c_string();
|
|
|
|
char *c_func = function.alloc_c_string();
|
|
|
|
char *c_file = file.alloc_c_string();
|
|
|
|
|
|
|
|
if (c_desc != nullptr && c_func != nullptr && c_file != nullptr) {
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::api->pandemonium_print_warning(c_desc, c_func, c_file, line);
|
2023-05-23 19:01:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (c_desc != nullptr)
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::api->pandemonium_free(c_desc);
|
2023-05-23 19:01:39 +02:00
|
|
|
if (c_func != nullptr)
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::api->pandemonium_free(c_func);
|
2023-05-23 19:01:39 +02:00
|
|
|
if (c_file != nullptr)
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::api->pandemonium_free(c_file);
|
2023-05-23 19:01:39 +02:00
|
|
|
}
|
|
|
|
|
2023-10-23 14:07:57 +02:00
|
|
|
void Pandemonium::print_error(const String &description, const String &function, const String &file, int line) {
|
2023-05-23 19:01:39 +02:00
|
|
|
int len;
|
|
|
|
|
|
|
|
char *c_desc = description.alloc_c_string();
|
|
|
|
char *c_func = function.alloc_c_string();
|
|
|
|
char *c_file = file.alloc_c_string();
|
|
|
|
|
|
|
|
if (c_desc != nullptr && c_func != nullptr && c_file != nullptr) {
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::api->pandemonium_print_error(c_desc, c_func, c_file, line);
|
2023-05-23 19:01:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (c_desc != nullptr)
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::api->pandemonium_free(c_desc);
|
2023-05-23 19:01:39 +02:00
|
|
|
if (c_func != nullptr)
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::api->pandemonium_free(c_func);
|
2023-05-23 19:01:39 +02:00
|
|
|
if (c_file != nullptr)
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::api->pandemonium_free(c_file);
|
2023-05-23 19:01:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ___register_types();
|
|
|
|
void ___init_method_bindings();
|
|
|
|
|
2023-10-23 14:07:57 +02:00
|
|
|
void Pandemonium::gdnative_init(pandemonium_gdnative_init_options *options) {
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::api = options->api_struct;
|
|
|
|
Pandemonium::gdnlib = options->gd_native_library;
|
2023-05-23 19:01:39 +02:00
|
|
|
|
2023-10-23 17:11:10 +02:00
|
|
|
const pandemonium_gdnative_api_struct *core_extension = Pandemonium::api->next;
|
2023-05-23 19:01:39 +02:00
|
|
|
|
2023-05-31 13:43:51 +02:00
|
|
|
/*
|
2023-05-23 19:01:39 +02:00
|
|
|
while (core_extension) {
|
|
|
|
if (core_extension->version.major == 1 && core_extension->version.minor == 1) {
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::core_1_1_api = (const pandemonium_gdnative_core_1_1_api_struct *)core_extension;
|
2023-05-23 19:01:39 +02:00
|
|
|
} else if (core_extension->version.major == 1 && core_extension->version.minor == 2) {
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::core_1_2_api = (const pandemonium_gdnative_core_1_2_api_struct *)core_extension;
|
2023-05-23 19:01:39 +02:00
|
|
|
}
|
|
|
|
core_extension = core_extension->next;
|
|
|
|
}
|
2023-05-31 13:43:51 +02:00
|
|
|
*/
|
2023-05-23 19:01:39 +02:00
|
|
|
|
|
|
|
// now find our extensions
|
2023-10-23 17:11:10 +02:00
|
|
|
for (int i = 0; i < Pandemonium::api->num_extensions; i++) {
|
|
|
|
switch (Pandemonium::api->extensions[i]->type) {
|
2023-05-23 19:01:39 +02:00
|
|
|
case GDNATIVE_EXT_NATIVESCRIPT: {
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::nativescript_api = (const pandemonium_gdnative_ext_nativescript_api_struct *)Pandemonium::api->extensions[i];
|
2023-05-23 19:01:39 +02:00
|
|
|
|
2023-05-31 13:43:51 +02:00
|
|
|
/*
|
2023-10-23 17:11:10 +02:00
|
|
|
const pandemonium_gdnative_api_struct *extension = Pandemonium::nativescript_api->next;
|
2023-05-23 19:01:39 +02:00
|
|
|
|
|
|
|
while (extension) {
|
|
|
|
if (extension->version.major == 1 && extension->version.minor == 1) {
|
2023-10-23 17:11:10 +02:00
|
|
|
//Pandemonium::nativescript_1_1_api = (const pandemonium_gdnative_ext_nativescript_1_1_api_struct *)extension;
|
2023-05-23 19:01:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension = extension->next;
|
|
|
|
}
|
2023-05-31 13:43:51 +02:00
|
|
|
*/
|
2023-05-23 19:01:39 +02:00
|
|
|
} break;
|
|
|
|
case GDNATIVE_EXT_PLUGINSCRIPT: {
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::pluginscript_api = (const pandemonium_gdnative_ext_pluginscript_api_struct *)Pandemonium::api->extensions[i];
|
2023-05-23 19:01:39 +02:00
|
|
|
} break;
|
|
|
|
case GDNATIVE_EXT_ANDROID: {
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::android_api = (const pandemonium_gdnative_ext_android_api_struct *)Pandemonium::api->extensions[i];
|
2023-05-23 19:01:39 +02:00
|
|
|
} break;
|
|
|
|
case GDNATIVE_EXT_VIDEODECODER: {
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::videodecoder_api = (const pandemonium_gdnative_ext_videodecoder_api_struct *)Pandemonium::api->extensions[i];
|
2023-05-23 19:01:39 +02:00
|
|
|
} break;
|
|
|
|
case GDNATIVE_EXT_NET: {
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::net_api = (const pandemonium_gdnative_ext_net_api_struct *)Pandemonium::api->extensions[i];
|
2023-05-23 19:01:39 +02:00
|
|
|
|
2023-05-31 13:43:51 +02:00
|
|
|
/*
|
2023-10-23 17:11:10 +02:00
|
|
|
const pandemonium_gdnative_api_struct *extension = Pandemonium::net_api->next;
|
2023-05-23 19:01:39 +02:00
|
|
|
|
|
|
|
while (extension) {
|
|
|
|
if (extension->version.major == 3 && extension->version.minor == 2) {
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::net_3_2_api = (const pandemonium_gdnative_ext_net_3_2_api_struct *)extension;
|
2023-05-23 19:01:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension = extension->next;
|
|
|
|
}
|
2023-05-31 13:43:51 +02:00
|
|
|
*/
|
2023-05-23 19:01:39 +02:00
|
|
|
} break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the `language_index` here since `__register_types()` makes use of it.
|
2023-05-31 13:31:45 +02:00
|
|
|
pandemonium_instance_binding_functions binding_funcs = {};
|
2023-05-23 19:01:39 +02:00
|
|
|
binding_funcs.alloc_instance_binding_data = wrapper_create;
|
|
|
|
binding_funcs.free_instance_binding_data = wrapper_destroy;
|
|
|
|
|
2023-10-23 17:11:10 +02:00
|
|
|
_RegisterState::language_index = Pandemonium::nativescript_api->pandemonium_nativescript_register_instance_binding_data_functions(binding_funcs);
|
2023-05-23 19:01:39 +02:00
|
|
|
|
|
|
|
// register these now
|
|
|
|
___register_types();
|
|
|
|
___init_method_bindings();
|
|
|
|
}
|
|
|
|
|
2023-10-23 14:07:57 +02:00
|
|
|
void Pandemonium::gdnative_terminate(pandemonium_gdnative_terminate_options *options) {
|
2023-05-23 19:01:39 +02:00
|
|
|
// reserved for future use.
|
|
|
|
}
|
|
|
|
|
2023-10-23 14:07:57 +02:00
|
|
|
void Pandemonium::gdnative_profiling_add_data(const char *p_signature, uint64_t p_time) {
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::nativescript_api->pandemonium_nativescript_profiling_add_data(p_signature, p_time);
|
2023-05-23 19:01:39 +02:00
|
|
|
}
|
|
|
|
|
2023-10-23 14:07:57 +02:00
|
|
|
void Pandemonium::nativescript_init(void *handle) {
|
2023-10-23 17:11:10 +02:00
|
|
|
_RegisterState::nativescript_handle = handle;
|
2023-05-23 19:01:39 +02:00
|
|
|
}
|
|
|
|
|
2023-10-23 14:07:57 +02:00
|
|
|
void Pandemonium::nativescript_terminate(void *handle) {
|
2023-10-23 17:11:10 +02:00
|
|
|
Pandemonium::nativescript_api->pandemonium_nativescript_unregister_instance_binding_data_functions(_RegisterState::language_index);
|
2023-05-23 19:01:39 +02:00
|
|
|
}
|