diff options
author | Fredia Huya-Kouadio <fhuya@meta.com> | 2023-01-26 02:55:47 -0800 |
---|---|---|
committer | Fredia Huya-Kouadio <fhuya@meta.com> | 2023-01-26 02:59:39 -0800 |
commit | ec4d720850091c7c2d3715031318bfcdbb391f62 (patch) | |
tree | f446a147685599212aaff22aff6c84549ab1996e /platform/android/java/lib/src | |
parent | 18a2e7ff6ebf0a477138c12c3efa63c15270a2ee (diff) |
Fix the issue causing the Godot Android Editor to crash when returning from the launched and running game
The issue was caused because the running game pid was not set, and thus had a value of `0`. When trying to stop the running game, the `EditorRun::stop()` logic would kill the process with pid 0, which on Android corresponds to the running app's own process, thus causing the editor to crash.
This issue did not happen on Godot 3 because pid with value of `0` are not considered valid.
Diffstat (limited to 'platform/android/java/lib/src')
3 files changed, 59 insertions, 31 deletions
diff --git a/platform/android/java/lib/src/org/godotengine/godot/FullScreenGodotApp.java b/platform/android/java/lib/src/org/godotengine/godot/FullScreenGodotApp.java index 863e67f1e9..65032d6a68 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/FullScreenGodotApp.java +++ b/platform/android/java/lib/src/org/godotengine/godot/FullScreenGodotApp.java @@ -74,28 +74,36 @@ public abstract class FullScreenGodotApp extends FragmentActivity implements God public void onDestroy() { Log.v(TAG, "Destroying Godot app..."); super.onDestroy(); - onGodotForceQuit(godotFragment); + terminateGodotInstance(godotFragment); } @Override public final void onGodotForceQuit(Godot instance) { + runOnUiThread(() -> { + terminateGodotInstance(instance); + }); + } + + private void terminateGodotInstance(Godot instance) { if (instance == godotFragment) { Log.v(TAG, "Force quitting Godot instance"); - ProcessPhoenix.forceQuit(this); + ProcessPhoenix.forceQuit(FullScreenGodotApp.this); } } @Override public final void onGodotRestartRequested(Godot instance) { - if (instance == godotFragment) { - // It's very hard to properly de-initialize Godot on Android to restart the game - // from scratch. Therefore, we need to kill the whole app process and relaunch it. - // - // Restarting only the activity, wouldn't be enough unless it did proper cleanup (including - // releasing and reloading native libs or resetting their state somehow and clearing statics). - Log.v(TAG, "Restarting Godot instance..."); - ProcessPhoenix.triggerRebirth(this); - } + runOnUiThread(() -> { + if (instance == godotFragment) { + // It's very hard to properly de-initialize Godot on Android to restart the game + // from scratch. Therefore, we need to kill the whole app process and relaunch it. + // + // Restarting only the activity, wouldn't be enough unless it did proper cleanup (including + // releasing and reloading native libs or resetting their state somehow and clearing statics). + Log.v(TAG, "Restarting Godot instance..."); + ProcessPhoenix.triggerRebirth(FullScreenGodotApp.this); + } + }); } @Override diff --git a/platform/android/java/lib/src/org/godotengine/godot/Godot.java b/platform/android/java/lib/src/org/godotengine/godot/Godot.java index 905db13c85..50263bc392 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/Godot.java +++ b/platform/android/java/lib/src/org/godotengine/godot/Godot.java @@ -348,11 +348,9 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC } public void restart() { - runOnUiThread(() -> { - if (godotHost != null) { - godotHost.onGodotRestartRequested(this); - } - }); + if (godotHost != null) { + godotHost.onGodotRestartRequested(this); + } } public void alert(final String message, final String title) { @@ -889,11 +887,20 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC private void forceQuit() { // TODO: This is a temp solution. The proper fix will involve tracking down and properly shutting down each // native Godot components that is started in Godot#onVideoInit. - runOnUiThread(() -> { - if (godotHost != null) { - godotHost.onGodotForceQuit(this); - } - }); + forceQuit(0); + } + + @Keep + private boolean forceQuit(int instanceId) { + if (godotHost == null) { + return false; + } + if (instanceId == 0) { + godotHost.onGodotForceQuit(this); + return true; + } else { + return godotHost.onGodotForceQuit(instanceId); + } } private boolean obbIsCorrupted(String f, String main_pack_md5) { @@ -1052,11 +1059,10 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC } @Keep - private void createNewGodotInstance(String[] args) { - runOnUiThread(() -> { - if (godotHost != null) { - godotHost.onNewGodotInstanceRequested(args); - } - }); + private int createNewGodotInstance(String[] args) { + if (godotHost != null) { + return godotHost.onNewGodotInstanceRequested(args); + } + return 0; } } diff --git a/platform/android/java/lib/src/org/godotengine/godot/GodotHost.java b/platform/android/java/lib/src/org/godotengine/godot/GodotHost.java index 256d04e3a5..7700b9b628 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/GodotHost.java +++ b/platform/android/java/lib/src/org/godotengine/godot/GodotHost.java @@ -55,21 +55,35 @@ public interface GodotHost { default void onGodotMainLoopStarted() {} /** - * Invoked on the UI thread as the last step of the Godot instance clean up phase. + * Invoked on the render thread to terminate the given Godot instance. */ default void onGodotForceQuit(Godot instance) {} /** - * Invoked on the UI thread when the Godot instance wants to be restarted. It's up to the host + * Invoked on the render thread to terminate the Godot instance with the given id. + * @param godotInstanceId id of the Godot instance to terminate. See {@code onNewGodotInstanceRequested} + * + * @return true if successful, false otherwise. + */ + default boolean onGodotForceQuit(int godotInstanceId) { + return false; + } + + /** + * Invoked on the render thread when the Godot instance wants to be restarted. It's up to the host * to perform the appropriate action(s). */ default void onGodotRestartRequested(Godot instance) {} /** - * Invoked on the UI thread when a new Godot instance is requested. It's up to the host to + * Invoked on the render thread when a new Godot instance is requested. It's up to the host to * perform the appropriate action(s). * * @param args Arguments used to initialize the new instance. + * + * @return the id of the new instance. See {@code onGodotForceQuit} */ - default void onNewGodotInstanceRequested(String[] args) {} + default int onNewGodotInstanceRequested(String[] args) { + return 0; + } } |