From 0865365e21b9d5b33f12e48fb6f5f50e19f48e7d Mon Sep 17 00:00:00 2001 From: Karroffel Date: Sun, 19 Nov 2017 23:41:22 +0100 Subject: [PATCH] [GDNative] loading error and version error procs --- modules/gdnative/SCsub | 13 --------- modules/gdnative/gdnative.cpp | 5 ++++ modules/gdnative/gdnative/gdnative.cpp | 28 ++++++++++++++++++++ modules/gdnative/include/gdnative/gdnative.h | 17 +++++++++++- 4 files changed, 49 insertions(+), 14 deletions(-) diff --git a/modules/gdnative/SCsub b/modules/gdnative/SCsub index 66b8d5cbdd8..485bf4b9df0 100644 --- a/modules/gdnative/SCsub +++ b/modules/gdnative/SCsub @@ -49,19 +49,6 @@ def _build_gdnative_api_struct_header(api): 'extern "C" {', '#endif', '', - 'typedef struct godot_gdnative_api_version {', - '\tunsigned int major;', - '\tunsigned int minor;', - '} godot_gdnative_api_version;', - '', - 'typedef struct godot_gdnative_api_struct godot_gdnative_api_struct;', - '', - 'struct godot_gdnative_api_struct {', - '\tunsigned int type;', - '\tgodot_gdnative_api_version version;', - '\tconst godot_gdnative_api_struct *next;', - '};', - '', 'enum GDNATIVE_API_TYPES {', '\tGDNATIVE_' + api['core']['type'] + ',' ] diff --git a/modules/gdnative/gdnative.cpp b/modules/gdnative/gdnative.cpp index 44d6dffc851..7949ca182f5 100644 --- a/modules/gdnative/gdnative.cpp +++ b/modules/gdnative/gdnative.cpp @@ -109,6 +109,9 @@ Ref GDNative::get_library() { return library; } +extern "C" void _gdnative_report_version_mismatch(const godot_object *p_library, const char *p_ext, godot_gdnative_api_version p_want, godot_gdnative_api_version p_have); +extern "C" void _gdnative_report_loading_error(const godot_object *p_library, const char *p_what); + bool GDNative::initialize() { if (library.is_null()) { ERR_PRINT("No library set, can't initialize GDNative object"); @@ -168,6 +171,8 @@ bool GDNative::initialize() { options.core_api_hash = ClassDB::get_api_hash(ClassDB::API_CORE); options.editor_api_hash = ClassDB::get_api_hash(ClassDB::API_EDITOR); options.no_api_hash = ClassDB::get_api_hash(ClassDB::API_NONE); + options.report_version_mismatch = &_gdnative_report_version_mismatch; + options.report_loading_error = &_gdnative_report_loading_error; options.gd_native_library = (godot_object *)(get_library().ptr()); options.active_library_path = (godot_string *)&path; diff --git a/modules/gdnative/gdnative/gdnative.cpp b/modules/gdnative/gdnative/gdnative.cpp index 6dfa7ec20bf..34b73558d52 100644 --- a/modules/gdnative/gdnative/gdnative.cpp +++ b/modules/gdnative/gdnative/gdnative.cpp @@ -36,6 +36,8 @@ #include "os/os.h" #include "variant.h" +#include "modules/gdnative/gdnative.h" + #ifdef __cplusplus extern "C" { #endif @@ -137,6 +139,32 @@ void GDAPI godot_print(const godot_string *p_message) { print_line(*(String *)p_message); } +void _gdnative_report_version_mismatch(const godot_object *p_library, const char *p_ext, godot_gdnative_api_version p_want, godot_gdnative_api_version p_have) { + String message = "Error loading GDNative file "; + GDNativeLibrary *library = (GDNativeLibrary *)p_library; + + message += library->get_current_library_path() + ": Extension \"" + p_ext + "\" can't be loaded.\n"; + + Dictionary versions; + versions["have_major"] = p_have.major; + versions["have_minor"] = p_have.minor; + versions["want_major"] = p_want.major; + versions["want_minor"] = p_want.minor; + + message += String("Got version {have_major}.{have_minor} but needs {want_major}.{want_minor}!").format(versions); + + _err_print_error("gdnative_init", library->get_current_library_path().utf8().ptr(), 0, message.utf8().ptr()); +} + +void _gdnative_report_loading_error(const godot_object *p_library, const char *p_what) { + String message = "Error loading GDNative file "; + GDNativeLibrary *library = (GDNativeLibrary *)p_library; + + message += library->get_current_library_path() + ": " + p_what; + + _err_print_error("gdnative_init", library->get_current_library_path().utf8().ptr(), 0, message.utf8().ptr()); +} + #ifdef __cplusplus } #endif diff --git a/modules/gdnative/include/gdnative/gdnative.h b/modules/gdnative/include/gdnative/gdnative.h index 38a42ab6586..e8d509508e1 100644 --- a/modules/gdnative/include/gdnative/gdnative.h +++ b/modules/gdnative/include/gdnative/gdnative.h @@ -229,13 +229,28 @@ void GDAPI godot_method_bind_ptrcall(godot_method_bind *p_method_bind, godot_obj godot_variant GDAPI godot_method_bind_call(godot_method_bind *p_method_bind, godot_object *p_instance, const godot_variant **p_args, const int p_arg_count, godot_variant_call_error *p_call_error); ////// Script API -struct godot_gdnative_api_struct; // Forward declaration +typedef struct godot_gdnative_api_version { + unsigned int major; + unsigned int minor; +} godot_gdnative_api_version; + +typedef struct godot_gdnative_api_struct godot_gdnative_api_struct; + +struct godot_gdnative_api_struct { + unsigned int type; + godot_gdnative_api_version version; + const godot_gdnative_api_struct *next; +}; + +#define GDNATIVE_VERSION_COMPATIBLE(want, have) (want.major == have.major && want.minor <= have.minor) typedef struct { godot_bool in_editor; uint64_t core_api_hash; uint64_t editor_api_hash; uint64_t no_api_hash; + void (*report_version_mismatch)(const godot_object *p_library, const char *p_what, godot_gdnative_api_version p_want, godot_gdnative_api_version p_have); + void (*report_loading_error)(const godot_object *p_library, const char *p_what); godot_object *gd_native_library; // pointer to GDNativeLibrary that is being initialized const struct godot_gdnative_core_api_struct *api_struct; const godot_string *active_library_path;