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) 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..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 @@ -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,48 @@ 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) + + //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() } 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"