1
1
package com.swordfish.lemuroid.lib.saves
2
2
3
3
import com.swordfish.lemuroid.common.kotlin.runCatchingWithRetry
4
+ import com.swordfish.lemuroid.lib.library.SystemCoreConfig
4
5
import com.swordfish.lemuroid.lib.library.db.entity.Game
6
+ import com.swordfish.lemuroid.lib.saves.migrators.getSavesMigrator
5
7
import com.swordfish.lemuroid.lib.storage.DirectoriesManager
6
8
import kotlinx.coroutines.Dispatchers
7
9
import kotlinx.coroutines.withContext
8
10
import java.io.File
9
11
10
12
class SavesManager (private val directoriesManager : DirectoriesManager ) {
11
- suspend fun getSaveRAM (game : Game ): ByteArray? =
12
- withContext(Dispatchers .IO ) {
13
+ suspend fun getSaveRAM (
14
+ game : Game ,
15
+ systemCoreConfig : SystemCoreConfig ,
16
+ ): ByteArray? {
17
+ return withContext(Dispatchers .IO ) {
13
18
val result =
14
19
runCatchingWithRetry(FILE_ACCESS_RETRIES ) {
15
20
val saveFile = getSaveFile(getSaveRAMFileName(game))
16
21
if (saveFile.exists() && saveFile.length() > 0 ) {
17
22
saveFile.readBytes()
18
23
} else {
19
- null
24
+ val savesMigrator = systemCoreConfig.getSavesMigrator()
25
+ savesMigrator?.loadPreviousSaveForGame(game, directoriesManager)
20
26
}
21
27
}
22
28
result.getOrNull()
23
29
}
30
+ }
24
31
25
32
suspend fun setSaveRAM (
26
33
game : Game ,
27
34
data : ByteArray ,
28
- ): Unit =
35
+ ) {
29
36
withContext(Dispatchers .IO ) {
30
37
val result =
31
38
runCatchingWithRetry(FILE_ACCESS_RETRIES ) {
@@ -38,19 +45,22 @@ class SavesManager(private val directoriesManager: DirectoriesManager) {
38
45
}
39
46
result.getOrNull()
40
47
}
48
+ }
41
49
42
- suspend fun getSaveRAMInfo (game : Game ): SaveInfo =
43
- withContext(Dispatchers .IO ) {
50
+ suspend fun getSaveRAMInfo (game : Game ): SaveInfo {
51
+ return withContext(Dispatchers .IO ) {
44
52
val saveFile = getSaveFile(getSaveRAMFileName(game))
45
53
val fileExists = saveFile.exists() && saveFile.length() > 0
46
54
SaveInfo (fileExists, saveFile.lastModified())
47
55
}
56
+ }
48
57
49
- private suspend fun getSaveFile (fileName : String ): File =
50
- withContext(Dispatchers .IO ) {
58
+ private suspend fun getSaveFile (fileName : String ): File {
59
+ return withContext(Dispatchers .IO ) {
51
60
val savesDirectory = directoriesManager.getSavesDirectory()
52
61
File (savesDirectory, fileName)
53
62
}
63
+ }
54
64
55
65
/* * This name should make it compatible with RetroArch so that users can freely sync saves across the two application. */
56
66
private fun getSaveRAMFileName (game : Game ) = " ${game.fileName.substringBeforeLast(" ." )} .srm"
0 commit comments