From 6fc89d0ce3c1958937b28aeddeea0e4290e05f31 Mon Sep 17 00:00:00 2001 From: Xavier Sellier Date: Mon, 21 May 2018 11:39:40 -0400 Subject: [PATCH] Allow users to catch errors for android platforms and prevent exception throwing --- .../java/src/org/godotengine/godot/Godot.java | 8 ++++++ platform/android/java_glue.cpp | 14 +++++++++- platform/android/os_android.cpp | 26 +++++++++++++------ platform/android/os_android.h | 5 ++-- 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/platform/android/java/src/org/godotengine/godot/Godot.java b/platform/android/java/src/org/godotengine/godot/Godot.java index 4bd38dbc7c9..bd5be3f3f30 100644 --- a/platform/android/java/src/org/godotengine/godot/Godot.java +++ b/platform/android/java/src/org/godotengine/godot/Godot.java @@ -190,6 +190,7 @@ public class Godot extends Activity implements SensorEventListener, IDownloaderC protected void onGLDrawFrame(GL10 gl) {} protected void onGLSurfaceChanged(GL10 gl, int width, int height) {} // singletons will always miss first onGLSurfaceChanged call + protected void onError(final String type, final String functionName, final String details, final String filename, final int line) {} public void registerMethods() {} } @@ -934,4 +935,11 @@ public class Godot extends Activity implements SensorEventListener, IDownloaderC mProgressFraction.setText(Helpers.getDownloadProgressString(progress.mOverallProgress, progress.mOverallTotal)); } + + public void emitErrorSignal(final String type, final String functionName, final String details, final String filename, final int line) { + // Allow users to use 3rd party modules to catch godot's errors. + for (int i = 0; i < singleton_count; i++) { + singletons[i].onError(type, functionName, details, filename, line); + } + } } diff --git a/platform/android/java_glue.cpp b/platform/android/java_glue.cpp index fb688219284..f68cee7cfa2 100644 --- a/platform/android/java_glue.cpp +++ b/platform/android/java_glue.cpp @@ -624,6 +624,7 @@ static jmethodID _getDataDir = 0; static jmethodID _getLocale = 0; static jmethodID _getClipboard = 0; static jmethodID _setClipboard = 0; +static jmethodID _emitErrorSignal = 0; static jmethodID _getModel = 0; static jmethodID _getScreenDPI = 0; static jmethodID _showKeyboard = 0; @@ -675,6 +676,16 @@ static void _set_clipboard(const String &p_text) { env->CallVoidMethod(_godot_instance, _setClipboard, jStr); } +static void _emit_error_signal(const String &p_error_type, const String &p_function, const String &p_err_details, const String &p_file, int p_line) { + JNIEnv *env = ThreadAndroid::get_env(); + jstring j_error_type = env->NewStringUTF(p_error_type.utf8().get_data()); + jstring j_function = env->NewStringUTF(p_function.utf8().get_data()); + jstring j_err_details = env->NewStringUTF(p_err_details.utf8().get_data()); + jstring j_file = env->NewStringUTF(p_file.utf8().get_data()); + + env->CallVoidMethod(_godot_instance, _emitErrorSignal, j_error_type, j_function, j_err_details, j_file, p_line); +} + static String _get_model() { JNIEnv *env = ThreadAndroid::get_env(); @@ -787,6 +798,7 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *en _alertDialog = env->GetMethodID(cls, "alert", "(Ljava/lang/String;Ljava/lang/String;)V"); _getClipboard = env->GetMethodID(cls, "getClipboard", "()Ljava/lang/String;"); _setClipboard = env->GetMethodID(cls, "setClipboard", "(Ljava/lang/String;)V"); + _emitErrorSignal = env->GetMethodID(cls, "emitErrorSignal", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V"); jclass clsio = env->FindClass("org/godotengine/godot/Godot"); if (cls) { @@ -845,7 +857,7 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *en } } - os_android = new OS_Android(_gfx_init_func, env, _open_uri, _get_data_dir, _get_locale, _get_model, _get_screen_dpi, _show_vk, _hide_vk, _set_screen_orient, _get_unique_id, _get_system_dir, _play_video, _is_video_playing, _pause_video, _stop_video, _set_keep_screen_on, _alert, use_apk_expansion, _set_clipboard, _get_clipboard); + os_android = new OS_Android(_gfx_init_func, env, _open_uri, _get_data_dir, _get_locale, _get_model, _get_screen_dpi, _show_vk, _hide_vk, _set_screen_orient, _get_unique_id, _get_system_dir, _play_video, _is_video_playing, _pause_video, _stop_video, _set_keep_screen_on, _alert, use_apk_expansion, _set_clipboard, _get_clipboard, _emit_error_signal); os_android->set_need_reload_hooks(p_need_reload_hook); char wd[500]; diff --git a/platform/android/os_android.cpp b/platform/android/os_android.cpp index cab539bba89..4c8ce0bfc8d 100644 --- a/platform/android/os_android.cpp +++ b/platform/android/os_android.cpp @@ -202,11 +202,6 @@ void OS_Android::print(const char *p_format, ...) { va_end(argp); } -void OS_Android::printfatal(const char *cond, const char *p_error_type, const char *p_function, const char *p_err_details, const char *p_file, int p_line) { - - __android_log_assert(cond, "godot", "%s exception: %s: %s(%s:%i)\n", p_error_type, p_function, p_err_details, p_file, p_line); -} - void OS_Android::alert(const String &p_alert, const String &p_title) { print("ALERT: %s\n", p_alert.utf8().get_data()); @@ -778,17 +773,30 @@ void OS_Android::print_error(const char *p_function, const char *p_file, int p_l switch (p_type) { case ERR_ERROR: - printfatal(NULL, "Error", p_function, err_details, p_file, p_line); + print("ERROR: %s: %s\n", p_function, err_details); + print(" At: %s:%i\n", p_file, p_line); + + if (emit_error_signal) { + emit_error_signal("Error", p_function, err_details, p_file, p_line); + } break; case ERR_WARNING: print("WARNING: %s: %s\n", p_function, err_details); print(" At: %s:%i\n", p_file, p_line); + + if (emit_error_signal) { + emit_error_signal("Warning", p_function, err_details, p_file, p_line); + } break; case ERR_SCRIPT: - printfatal(NULL, "Script error", p_function, err_details, p_file, p_line); + print("SCRIPT ERROR: %s: %s\n", p_function, err_details); + print(" At: %s:%i\n", p_file, p_line); + if (emit_error_signal) { + emit_error_signal("Script error", p_function, err_details, p_file, p_line); + } break; } } @@ -797,7 +805,7 @@ String OS_Android::get_joy_guid(int p_device) const { return input->get_joy_guid_remapped(p_device); } -OS_Android::OS_Android(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetDataDirFunc p_get_data_dir_func, GetLocaleFunc p_get_locale_func, GetModelFunc p_get_model_func, GetScreenDPIFunc p_get_screen_dpi_func, ShowVirtualKeyboardFunc p_show_vk, HideVirtualKeyboardFunc p_hide_vk, SetScreenOrientationFunc p_screen_orient, GetUniqueIDFunc p_get_unique_id, GetSystemDirFunc p_get_sdir_func, VideoPlayFunc p_video_play_func, VideoIsPlayingFunc p_video_is_playing_func, VideoPauseFunc p_video_pause_func, VideoStopFunc p_video_stop_func, SetKeepScreenOnFunc p_set_keep_screen_on_func, AlertFunc p_alert_func, bool p_use_apk_expansion, SetClipboardFunc p_set_clipboard_func, GetClipboardFunc p_get_clipboard_func) { +OS_Android::OS_Android(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetDataDirFunc p_get_data_dir_func, GetLocaleFunc p_get_locale_func, GetModelFunc p_get_model_func, GetScreenDPIFunc p_get_screen_dpi_func, ShowVirtualKeyboardFunc p_show_vk, HideVirtualKeyboardFunc p_hide_vk, SetScreenOrientationFunc p_screen_orient, GetUniqueIDFunc p_get_unique_id, GetSystemDirFunc p_get_sdir_func, VideoPlayFunc p_video_play_func, VideoIsPlayingFunc p_video_is_playing_func, VideoPauseFunc p_video_pause_func, VideoStopFunc p_video_stop_func, SetKeepScreenOnFunc p_set_keep_screen_on_func, AlertFunc p_alert_func, bool p_use_apk_expansion, SetClipboardFunc p_set_clipboard_func, GetClipboardFunc p_get_clipboard_func, EmitErrorSignal p_emit_error_signal) { use_apk_expansion = p_use_apk_expansion; default_videomode.width = 800; @@ -836,6 +844,8 @@ OS_Android::OS_Android(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, OpenURI set_clipboard_func = p_set_clipboard_func; get_clipboard_func = p_get_clipboard_func; + + emit_error_signal = p_emit_error_signal; } OS_Android::~OS_Android() { diff --git a/platform/android/os_android.h b/platform/android/os_android.h index 79de29e693f..a1f38c07712 100644 --- a/platform/android/os_android.h +++ b/platform/android/os_android.h @@ -63,6 +63,7 @@ typedef String (*GetDataDirFunc)(); typedef String (*GetLocaleFunc)(); typedef void (*SetClipboardFunc)(const String &); typedef String (*GetClipboardFunc)(); +typedef void (*EmitErrorSignal)(const String &, const String &, const String &, const String &, int); typedef String (*GetModelFunc)(); typedef int (*GetScreenDPIFunc)(); typedef String (*GetUniqueIDFunc)(); @@ -143,6 +144,7 @@ private: GetLocaleFunc get_locale_func; SetClipboardFunc set_clipboard_func; GetClipboardFunc get_clipboard_func; + EmitErrorSignal emit_error_signal; GetModelFunc get_model_func; GetScreenDPIFunc get_screen_dpi_func; ShowVirtualKeyboardFunc show_virtual_keyboard_func; @@ -183,7 +185,6 @@ public: void print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type); virtual void vprint(const char *p_format, va_list p_list, bool p_stderr = false); virtual void print(const char *p_format, ...); - virtual void printfatal(const char *cond, const char *p_error_type, const char *p_function, const char *p_err_details, const char *p_file, int p_line); virtual void alert(const String &p_alert, const String &p_title = "ALERT!"); virtual void set_mouse_show(bool p_show); @@ -262,7 +263,7 @@ public: virtual String get_joy_guid(int p_device) const; void joy_connection_changed(int p_device, bool p_connected, String p_name); - OS_Android(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetDataDirFunc p_get_data_dir_func, GetLocaleFunc p_get_locale_func, GetModelFunc p_get_model_func, GetScreenDPIFunc p_get_screen_dpi_func, ShowVirtualKeyboardFunc p_show_vk, HideVirtualKeyboardFunc p_hide_vk, SetScreenOrientationFunc p_screen_orient, GetUniqueIDFunc p_get_unique_id, GetSystemDirFunc p_get_sdir_func, VideoPlayFunc p_video_play_func, VideoIsPlayingFunc p_video_is_playing_func, VideoPauseFunc p_video_pause_func, VideoStopFunc p_video_stop_func, SetKeepScreenOnFunc p_set_keep_screen_on_func, AlertFunc p_alert_func, bool p_use_apk_expansion, SetClipboardFunc p_set_clipboard_func, GetClipboardFunc p_get_clipboard_func); + OS_Android(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetDataDirFunc p_get_data_dir_func, GetLocaleFunc p_get_locale_func, GetModelFunc p_get_model_func, GetScreenDPIFunc p_get_screen_dpi_func, ShowVirtualKeyboardFunc p_show_vk, HideVirtualKeyboardFunc p_hide_vk, SetScreenOrientationFunc p_screen_orient, GetUniqueIDFunc p_get_unique_id, GetSystemDirFunc p_get_sdir_func, VideoPlayFunc p_video_play_func, VideoIsPlayingFunc p_video_is_playing_func, VideoPauseFunc p_video_pause_func, VideoStopFunc p_video_stop_func, SetKeepScreenOnFunc p_set_keep_screen_on_func, AlertFunc p_alert_func, bool p_use_apk_expansion, SetClipboardFunc p_set_clipboard_func, GetClipboardFunc p_get_clipboard_func, EmitErrorSignal p_emit_error_signal); ~OS_Android(); };