From 0d6fc71f2eeeb67ecac815a5079e0c61a8bee21c Mon Sep 17 00:00:00 2001 From: Bektur Date: Sat, 13 Nov 2021 02:04:21 +0600 Subject: [PATCH] Android: pass non-critical server and mod errors to Java (#224) --- .../main/java/com/multicraft/game/GameActivity.java | 4 ++++ build/android/build.gradle | 4 ++-- src/game.cpp | 6 ++++++ src/guiEngine.cpp | 6 ++++++ src/porting_android.cpp | 12 ++++++++++++ src/porting_android.h | 5 +++++ 6 files changed, 35 insertions(+), 2 deletions(-) diff --git a/build/android/app/src/main/java/com/multicraft/game/GameActivity.java b/build/android/app/src/main/java/com/multicraft/game/GameActivity.java index f74bdbc7..3958a13b 100644 --- a/build/android/app/src/main/java/com/multicraft/game/GameActivity.java +++ b/build/android/app/src/main/java/com/multicraft/game/GameActivity.java @@ -191,4 +191,8 @@ public class GameActivity extends NativeActivity { public void finishGame(String exc) { finish(); } + + public void handleError(String message) { + // Log errors from native + } } diff --git a/build/android/build.gradle b/build/android/build.gradle index f256bb6f..6c158f25 100644 --- a/build/android/build.gradle +++ b/build/android/build.gradle @@ -7,8 +7,8 @@ buildscript { maven { url 'https://plugins.gradle.org/m2/' } } dependencies { - classpath 'com.android.tools.build:gradle:7.0.0' - classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21' + classpath 'com.android.tools.build:gradle:7.0.3' + classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31' classpath 'de.undercouch:gradle-download-task:4.1.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/src/game.cpp b/src/game.cpp index 65694f24..131cae5e 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -4982,9 +4982,15 @@ void the_game(bool *kill, } catch (ServerError &e) { error_message = e.what(); errorstream << "ServerError: " << error_message << std::endl; +#ifdef __ANDROID__ + porting::handleError("ServerError", error_message); +#endif } catch (ModError &e) { error_message = e.what() + strgettext("\nCheck debug.txt for details."); errorstream << "ModError: " << error_message << std::endl; +#ifdef __ANDROID__ + porting::handleError("ModError", error_message); +#endif } game.shutdown(); g_game = NULL; diff --git a/src/guiEngine.cpp b/src/guiEngine.cpp index 1754ddb5..9456dca8 100644 --- a/src/guiEngine.cpp +++ b/src/guiEngine.cpp @@ -230,6 +230,9 @@ GUIEngine::GUIEngine( irr::IrrlichtDevice* dev, } catch (LuaError &e) { errorstream << "Main menu error: " << e.what() << std::endl; m_data->script_data.errormessage = e.what(); +#ifdef __ANDROID__ + porting::handleError("Main menu error", e.what()); +#endif } m_menu->quitMenu(); @@ -255,6 +258,9 @@ bool GUIEngine::loadMainMenuScript() } catch (const ModError &e) { errorstream << "GUIEngine: execution of menu script failed: " << e.what() << std::endl; +#ifdef __ANDROID__ + porting::handleError("Main menu load error", e.what()); +#endif } return false; diff --git a/src/porting_android.cpp b/src/porting_android.cpp index 2604c4d7..73dfdc14 100644 --- a/src/porting_android.cpp +++ b/src/porting_android.cpp @@ -288,6 +288,18 @@ bool hasRealKeyboard() return device_has_keyboard; } +void handleError(const std::string &errType, const std::string &err) { + jmethodID report_err = jnienv->GetMethodID(nativeActivity, + "handleError","(Ljava/lang/String;)V"); + + FATAL_ERROR_IF(report_err == nullptr, + "porting::handleError unable to find java handleError method"); + + std::string errorMessage = errType + ": " + err; + jstring jerr = jnienv->NewStringUTF(errorMessage.c_str()); + jnienv->CallVoidMethod(app_global->activity->clazz, report_err, jerr); +} + void notifyServerConnect(bool is_multiplayer) { jmethodID notifyConnect = jnienv->GetMethodID(nativeActivity, diff --git a/src/porting_android.h b/src/porting_android.h index 4fbea1ca..7bab1589 100644 --- a/src/porting_android.h +++ b/src/porting_android.h @@ -91,4 +91,9 @@ std::string getInputDialogValue(); * call Android function to finish */ void finishGame(const std::string &exc); + + /** + * call Android function to handle not-critical error + */ + void handleError(const std::string &errType, const std::string &err); }