From ff49b7db8a0bc5819eafe2498652d721afa388e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Sat, 10 Sep 2022 11:08:25 +0200 Subject: [PATCH 1/3] save sram when onPause is called --- .../swordfish/lemuroid/app/shared/game/BaseGameActivity.kt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lemuroid-app/src/main/java/com/swordfish/lemuroid/app/shared/game/BaseGameActivity.kt b/lemuroid-app/src/main/java/com/swordfish/lemuroid/app/shared/game/BaseGameActivity.kt index b70ee86d46..10d2deee22 100644 --- a/lemuroid-app/src/main/java/com/swordfish/lemuroid/app/shared/game/BaseGameActivity.kt +++ b/lemuroid-app/src/main/java/com/swordfish/lemuroid/app/shared/game/BaseGameActivity.kt @@ -789,6 +789,13 @@ abstract class BaseGameActivity : ImmersiveActivity() { } } + override fun onPause() { + GlobalScope.launch { + saveSRAM(game) + } + super.onPause() + } + private suspend fun autoSaveAndFinish() = withLoading { saveSRAM(game) saveAutoSave(game) From 2b511e2a246a8635804a02cefdeaeb1ce2914fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Sat, 15 Jul 2023 17:18:44 +0200 Subject: [PATCH 2/3] create seperate save-folder for each system --- .../lemuroid/lib/saves/SavesManager.kt | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/retrograde-app-shared/src/main/java/com/swordfish/lemuroid/lib/saves/SavesManager.kt b/retrograde-app-shared/src/main/java/com/swordfish/lemuroid/lib/saves/SavesManager.kt index 8f3557452d..729f7dff59 100644 --- a/retrograde-app-shared/src/main/java/com/swordfish/lemuroid/lib/saves/SavesManager.kt +++ b/retrograde-app-shared/src/main/java/com/swordfish/lemuroid/lib/saves/SavesManager.kt @@ -11,7 +11,7 @@ class SavesManager(private val directoriesManager: DirectoriesManager) { suspend fun getSaveRAM(game: Game): ByteArray? = withContext(Dispatchers.IO) { val result = runCatchingWithRetry(FILE_ACCESS_RETRIES) { - val saveFile = getSaveFile(getSaveRAMFileName(game)) + val saveFile = getSaveFile(game.systemId, getSaveRAMFileName(game)) if (saveFile.exists() && saveFile.length() > 0) { saveFile.readBytes() } else { @@ -26,23 +26,40 @@ class SavesManager(private val directoriesManager: DirectoriesManager) { if (data.isEmpty()) return@runCatchingWithRetry - val saveFile = getSaveFile(getSaveRAMFileName(game)) + val saveFile = getSystemSaveFile(game.systemId, getSaveRAMFileName(game)) saveFile.writeBytes(data) } result.getOrNull() } suspend fun getSaveRAMInfo(game: Game): SaveInfo = withContext(Dispatchers.IO) { - val saveFile = getSaveFile(getSaveRAMFileName(game)) + val saveFile = getSaveFile(game.systemId, getSaveRAMFileName(game)) val fileExists = saveFile.exists() && saveFile.length() > 0 SaveInfo(fileExists, saveFile.lastModified()) } - private suspend fun getSaveFile(fileName: String): File = withContext(Dispatchers.IO) { + private suspend fun getLegacySaveFile(fileName: String): File = withContext(Dispatchers.IO) { val savesDirectory = directoriesManager.getSavesDirectory() File(savesDirectory, fileName) } + private suspend fun getSystemSaveFile(system: String, fileName: String): File = withContext(Dispatchers.IO) { + val savesDirectory = directoriesManager.getSavesDirectory() + val systemDir = File(savesDirectory, system) + + if(!systemDir.exists()) { + systemDir.mkdir() + } + File(systemDir, fileName) + } + private suspend fun getSaveFile(system: String, fileName: String): File = withContext(Dispatchers.IO) { + val save = getSystemSaveFile(system, fileName) + if(save.exists()){ + return@withContext save + } + getLegacySaveFile(fileName) + } + /** This name should make it compatible with RetroArch so that users can freely sync saves across the two application. */ private fun getSaveRAMFileName(game: Game) = "${game.fileName.substringBeforeLast(".")}.srm" From 5090a0b70bfa29fe4f604e6c3e6311354b4d48db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Sat, 15 Jul 2023 17:24:04 +0200 Subject: [PATCH 3/3] delete legacy savefile AFTER saving to new folder. --- .../java/com/swordfish/lemuroid/lib/saves/SavesManager.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/retrograde-app-shared/src/main/java/com/swordfish/lemuroid/lib/saves/SavesManager.kt b/retrograde-app-shared/src/main/java/com/swordfish/lemuroid/lib/saves/SavesManager.kt index 729f7dff59..b0251964d0 100644 --- a/retrograde-app-shared/src/main/java/com/swordfish/lemuroid/lib/saves/SavesManager.kt +++ b/retrograde-app-shared/src/main/java/com/swordfish/lemuroid/lib/saves/SavesManager.kt @@ -28,6 +28,14 @@ class SavesManager(private val directoriesManager: DirectoriesManager) { val saveFile = getSystemSaveFile(game.systemId, getSaveRAMFileName(game)) saveFile.writeBytes(data) + + //clean up the legacy file. But only if the byteArrays match! + if(saveFile.readBytes().contentEquals(data)) { + val legacySaveFile = getLegacySaveFile(getSaveRAMFileName(game)) + if(legacySaveFile.exists()) { + legacySaveFile.delete() + } + } } result.getOrNull() }