Переглянути джерело

replace emergency save with pause autosave

Laserlicht 6 днів тому
батько
коміт
a267901989

+ 0 - 1
android/vcmi-app/src/main/java/eu/vcmi/vcmi/NativeMethods.java

@@ -27,7 +27,6 @@ public class NativeMethods
 
     public static native void initClassloader();
     public static native void heroesDataUpdate();
-    public static native boolean tryToSaveTheGame();
 
     public static void setupMsg(final Messenger msg)
     {

+ 0 - 16
android/vcmi-app/src/main/java/eu/vcmi/vcmi/VcmiSDLActivity.java

@@ -114,22 +114,6 @@ public class VcmiSDLActivity extends SDLActivity
     @Override
     protected void onDestroy()
     {
-        try
-        {
-            // since android can kill the activity unexpectedly (e.g. memory is low or device is inactive for some time), let's try creating
-            // an autosave so user might be able to resume the game; this isn't a very good impl (we shouldn't really sleep here and hope that the
-            // save is created, but for now it might suffice
-            // (better solution: listen for game's confirmation that the save has been created -- this would allow us to inform the users
-            // on the next app launch that there is an automatic save that they can use)
-            if (NativeMethods.tryToSaveTheGame())
-            {
-                Thread.sleep(1000L);
-            }
-        }
-        catch (final InterruptedException ignored)
-        {
-        }
-
         unbindServer();
 
         super.onDestroy();

+ 0 - 23
client/Client.cpp

@@ -513,29 +513,6 @@ void CClient::removeGUI() const
 	GAME->setInterfaceInstance(nullptr);
 }
 
-#ifdef VCMI_ANDROID
-extern "C" JNIEXPORT jboolean JNICALL Java_eu_vcmi_vcmi_NativeMethods_tryToSaveTheGame(JNIEnv * env, jclass cls)
-{
-	std::scoped_lock interfaceLock(ENGINE->interfaceMutex);
-
-	logGlobal->info("Received emergency save game request");
-	if(!GAME->interface() || !GAME->interface()->cb)
-	{
-		logGlobal->info("... but no active player interface found!");
-		return false;
-	}
-
-	if (!GAME->server().logicConnection)
-	{
-		logGlobal->info("... but no active connection found!");
-		return false;
-	}
-
-	GAME->interface()->cb->save("Saves/_Android_Autosave");
-	return true;
-}
-#endif
-
 void CClient::registerBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents, PlayerColor color)
 {
 	additionalBattleInts[color].push_back(battleEvents);

+ 3 - 0
client/GameEngineUser.h

@@ -23,6 +23,9 @@ public:
 	/// Called when app shutdown has been requested in any way - exit button, Alt-F4, etc
 	virtual void onShutdownRequested(bool askForConfirmation) = 0;
 
+	/// Called when mobile app pauses or resumes
+	virtual void onAppPaused(bool resume) = 0;
+
 	/// Returns true if all input events should be captured and ignored
 	virtual bool capturedAllEvents() = 0;
 };

+ 27 - 0
client/GameInstance.cpp

@@ -20,6 +20,7 @@
 
 #include "../lib/CConfigHandler.h"
 #include "../lib/GameLibrary.h"
+#include "../lib/callback/CCallback.h"
 #include "../lib/texts/CGeneralTextHandler.h"
 
 std::unique_ptr<GameInstance> GAME = nullptr;
@@ -112,3 +113,29 @@ void GameInstance::onShutdownRequested(bool ask)
 			CInfoWindow::showYesNoDialog(LIBRARY->generaltexth->allTexts[69], {}, doQuit, {}, PlayerColor(1));
 	}
 }
+
+void GameInstance::onAppPaused(bool resume)
+{
+	const std::string autoSaveName = "Saves/PauseAutosave";
+
+	logGlobal->info("Received pause save game request");
+	if(!GAME->interface() || !GAME->interface()->cb)
+	{
+		logGlobal->info("... but no active player interface found!");
+		return;
+	}
+
+	if (!GAME->server().logicConnection)
+	{
+		logGlobal->info("... but no active connection found!");
+		return;
+	}
+
+	if(GAME->interface()->cb->getActiveBattles().size() > 0)
+	{
+		logGlobal->info("... but player is in battle, skipping autosave!");
+		return;
+	}
+
+	GAME->interface()->cb->save(autoSaveName);
+}

+ 1 - 0
client/GameInstance.h

@@ -55,6 +55,7 @@ public:
 	void onUpdate() final;
 	bool capturedAllEvents() final;
 	void onShutdownRequested(bool askForConfirmation) final;
+	void onAppPaused(bool resume) final;
 };
 
 extern std::unique_ptr<GameInstance> GAME;

+ 12 - 0
client/eventsSDL/InputHandler.cpp

@@ -217,6 +217,18 @@ void InputHandler::preprocessEvent(const SDL_Event & ev)
 #endif
 		return;
 	}
+	else if(ev.type == SDL_APP_WILLENTERBACKGROUND)
+	{
+		std::scoped_lock interfaceLock(ENGINE->interfaceMutex);
+		ENGINE->user().onAppPaused(false);
+		return;
+	}
+	else if(ev.type == SDL_APP_WILLENTERFOREGROUND)
+	{
+		std::scoped_lock interfaceLock(ENGINE->interfaceMutex);
+		ENGINE->user().onAppPaused(true);
+		return;
+	}
 	else if(ev.type == SDL_KEYDOWN)
 	{
 		if(ev.key.keysym.sym == SDLK_F4 && (ev.key.keysym.mod & KMOD_ALT))