Browse Source

Allow users to catch errors for android platforms and prevent exception throwing

Xavier Sellier 7 years ago
parent
commit
6fc89d0ce3

+ 8 - 0
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 onGLDrawFrame(GL10 gl) {}
 		protected void onGLSurfaceChanged(GL10 gl, int width, int height) {} // singletons will always miss first onGLSurfaceChanged call
 		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() {}
 		public void registerMethods() {}
 	}
 	}
 
 
@@ -934,4 +935,11 @@ public class Godot extends Activity implements SensorEventListener, IDownloaderC
 		mProgressFraction.setText(Helpers.getDownloadProgressString(progress.mOverallProgress,
 		mProgressFraction.setText(Helpers.getDownloadProgressString(progress.mOverallProgress,
 				progress.mOverallTotal));
 				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);
+		}
+	}
 }
 }

+ 13 - 1
platform/android/java_glue.cpp

@@ -624,6 +624,7 @@ static jmethodID _getDataDir = 0;
 static jmethodID _getLocale = 0;
 static jmethodID _getLocale = 0;
 static jmethodID _getClipboard = 0;
 static jmethodID _getClipboard = 0;
 static jmethodID _setClipboard = 0;
 static jmethodID _setClipboard = 0;
+static jmethodID _emitErrorSignal = 0;
 static jmethodID _getModel = 0;
 static jmethodID _getModel = 0;
 static jmethodID _getScreenDPI = 0;
 static jmethodID _getScreenDPI = 0;
 static jmethodID _showKeyboard = 0;
 static jmethodID _showKeyboard = 0;
@@ -675,6 +676,16 @@ static void _set_clipboard(const String &p_text) {
 	env->CallVoidMethod(_godot_instance, _setClipboard, jStr);
 	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() {
 static String _get_model() {
 
 
 	JNIEnv *env = ThreadAndroid::get_env();
 	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");
 		_alertDialog = env->GetMethodID(cls, "alert", "(Ljava/lang/String;Ljava/lang/String;)V");
 		_getClipboard = env->GetMethodID(cls, "getClipboard", "()Ljava/lang/String;");
 		_getClipboard = env->GetMethodID(cls, "getClipboard", "()Ljava/lang/String;");
 		_setClipboard = env->GetMethodID(cls, "setClipboard", "(Ljava/lang/String;)V");
 		_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");
 		jclass clsio = env->FindClass("org/godotengine/godot/Godot");
 		if (cls) {
 		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);
 	os_android->set_need_reload_hooks(p_need_reload_hook);
 
 
 	char wd[500];
 	char wd[500];

+ 18 - 8
platform/android/os_android.cpp

@@ -202,11 +202,6 @@ void OS_Android::print(const char *p_format, ...) {
 	va_end(argp);
 	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) {
 void OS_Android::alert(const String &p_alert, const String &p_title) {
 
 
 	print("ALERT: %s\n", p_alert.utf8().get_data());
 	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) {
 	switch (p_type) {
 		case ERR_ERROR:
 		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;
 			break;
 
 
 		case ERR_WARNING:
 		case ERR_WARNING:
 			print("WARNING: %s: %s\n", p_function, err_details);
 			print("WARNING: %s: %s\n", p_function, err_details);
 			print("   At: %s:%i\n", p_file, p_line);
 			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;
 			break;
 
 
 		case ERR_SCRIPT:
 		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;
 			break;
 	}
 	}
 }
 }
@@ -797,7 +805,7 @@ String OS_Android::get_joy_guid(int p_device) const {
 	return input->get_joy_guid_remapped(p_device);
 	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;
 	use_apk_expansion = p_use_apk_expansion;
 	default_videomode.width = 800;
 	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;
 	set_clipboard_func = p_set_clipboard_func;
 	get_clipboard_func = p_get_clipboard_func;
 	get_clipboard_func = p_get_clipboard_func;
+
+	emit_error_signal = p_emit_error_signal;
 }
 }
 
 
 OS_Android::~OS_Android() {
 OS_Android::~OS_Android() {

+ 3 - 2
platform/android/os_android.h

@@ -63,6 +63,7 @@ typedef String (*GetDataDirFunc)();
 typedef String (*GetLocaleFunc)();
 typedef String (*GetLocaleFunc)();
 typedef void (*SetClipboardFunc)(const String &);
 typedef void (*SetClipboardFunc)(const String &);
 typedef String (*GetClipboardFunc)();
 typedef String (*GetClipboardFunc)();
+typedef void (*EmitErrorSignal)(const String &, const String &, const String &, const String &, int);
 typedef String (*GetModelFunc)();
 typedef String (*GetModelFunc)();
 typedef int (*GetScreenDPIFunc)();
 typedef int (*GetScreenDPIFunc)();
 typedef String (*GetUniqueIDFunc)();
 typedef String (*GetUniqueIDFunc)();
@@ -143,6 +144,7 @@ private:
 	GetLocaleFunc get_locale_func;
 	GetLocaleFunc get_locale_func;
 	SetClipboardFunc set_clipboard_func;
 	SetClipboardFunc set_clipboard_func;
 	GetClipboardFunc get_clipboard_func;
 	GetClipboardFunc get_clipboard_func;
+	EmitErrorSignal emit_error_signal;
 	GetModelFunc get_model_func;
 	GetModelFunc get_model_func;
 	GetScreenDPIFunc get_screen_dpi_func;
 	GetScreenDPIFunc get_screen_dpi_func;
 	ShowVirtualKeyboardFunc show_virtual_keyboard_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);
 	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 vprint(const char *p_format, va_list p_list, bool p_stderr = false);
 	virtual void print(const char *p_format, ...);
 	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 alert(const String &p_alert, const String &p_title = "ALERT!");
 
 
 	virtual void set_mouse_show(bool p_show);
 	virtual void set_mouse_show(bool p_show);
@@ -262,7 +263,7 @@ public:
 	virtual String get_joy_guid(int p_device) const;
 	virtual String get_joy_guid(int p_device) const;
 	void joy_connection_changed(int p_device, bool p_connected, String p_name);
 	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();
 	~OS_Android();
 };
 };