Skip to content

Commit

Permalink
Import previous melonds saves when found.
Browse files Browse the repository at this point in the history
  • Loading branch information
Swordfish90 committed Apr 9, 2024
1 parent 1013a76 commit 28a1f11
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ import java.io.File

class GameLoader(
private val lemuroidLibrary: LemuroidLibrary,
private val legacyStatesManager: StatesManager,
private val legacySavesManager: SavesManager,
private val legacyCoreVariablesManager: CoreVariablesManager,
private val statesManager: StatesManager,
private val savesManager: SavesManager,
private val coreVariablesManager: CoreVariablesManager,
private val retrogradeDatabase: RetrogradeDatabase,
private val savesCoherencyEngine: SavesCoherencyEngine,
private val directoriesManager: DirectoriesManager,
Expand Down Expand Up @@ -97,7 +97,7 @@ class GameLoader(

val saveRAMData =
runCatching {
legacySavesManager.getSaveRAM(game)
savesManager.getSaveRAM(game, systemCoreConfig)
}.getOrElse { throw GameLoaderException(GameLoaderError.Saves) }

val quickSaveData =
Expand All @@ -106,14 +106,14 @@ class GameLoader(
!savesCoherencyEngine.shouldDiscardAutoSaveState(game, systemCoreConfig.coreID)

if (systemCoreConfig.statesSupported && loadSave && shouldDiscardSave) {
legacyStatesManager.getAutoSave(game, systemCoreConfig.coreID)
statesManager.getAutoSave(game, systemCoreConfig.coreID)
} else {
null
}
}.getOrElse { throw GameLoaderException(GameLoaderError.Saves) }

val coreVariables =
legacyCoreVariablesManager.getOptionsForCore(system.id, systemCoreConfig)
coreVariablesManager.getOptionsForCore(system.id, systemCoreConfig)
.toTypedArray()

val systemDirectory = directoriesManager.getSystemDirectory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,8 @@ data class GameSystem(
CoreVariable("melonds_number_of_screen_layouts", "1"),
CoreVariable("melonds_touch_mode", "Touch"),
CoreVariable("melonds_threaded_renderer", "enabled"),
// TODO... As soon as libretrodroid supports the microphone we can remove this.
CoreVariable("melonds_mic_input", "blow"),

This comment has been minimized.

Copy link
@StevenPZChan

StevenPZChan Oct 21, 2024

https://melonds.kuribo64.net/board/thread.php?id=1633
This 'blow' option did not work on some games. I tried 'noise' and it worked for my current game.

),
controllerConfigs =
hashMapOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
package com.swordfish.lemuroid.lib.saves

import com.swordfish.lemuroid.common.kotlin.runCatchingWithRetry
import com.swordfish.lemuroid.lib.library.SystemCoreConfig
import com.swordfish.lemuroid.lib.library.db.entity.Game
import com.swordfish.lemuroid.lib.saves.migrators.getSavesMigrator
import com.swordfish.lemuroid.lib.storage.DirectoriesManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File

class SavesManager(private val directoriesManager: DirectoriesManager) {
suspend fun getSaveRAM(game: Game): ByteArray? =
withContext(Dispatchers.IO) {
suspend fun getSaveRAM(
game: Game,
systemCoreConfig: SystemCoreConfig,
): ByteArray? {
return withContext(Dispatchers.IO) {
val result =
runCatchingWithRetry(FILE_ACCESS_RETRIES) {
val saveFile = getSaveFile(getSaveRAMFileName(game))
if (saveFile.exists() && saveFile.length() > 0) {
saveFile.readBytes()
} else {
null
val savesMigrator = systemCoreConfig.getSavesMigrator()
savesMigrator?.loadPreviousSaveForGame(game, directoriesManager)
}
}
result.getOrNull()
}
}

suspend fun setSaveRAM(
game: Game,
data: ByteArray,
): Unit =
) {
withContext(Dispatchers.IO) {
val result =
runCatchingWithRetry(FILE_ACCESS_RETRIES) {
Expand All @@ -38,19 +45,22 @@ class SavesManager(private val directoriesManager: DirectoriesManager) {
}
result.getOrNull()
}
}

suspend fun getSaveRAMInfo(game: Game): SaveInfo =
withContext(Dispatchers.IO) {
suspend fun getSaveRAMInfo(game: Game): SaveInfo {
return withContext(Dispatchers.IO) {
val saveFile = getSaveFile(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 getSaveFile(fileName: String): File {
return withContext(Dispatchers.IO) {
val savesDirectory = directoriesManager.getSavesDirectory()
File(savesDirectory, 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"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.swordfish.lemuroid.lib.saves.migrators

import com.swordfish.lemuroid.lib.library.db.entity.Game
import com.swordfish.lemuroid.lib.storage.DirectoriesManager
import java.io.File

object MelonDsSavesMigrator : SavesMigrator {
override fun loadPreviousSaveForGame(
game: Game,
directoriesManager: DirectoriesManager,
): ByteArray? {
val savesDirectory = directoriesManager.getSavesDirectory()
val previousSaveFileName = "${game.fileName.substringBeforeLast(".")}.sav"
val previousSaveFile = File(savesDirectory, previousSaveFileName)

return if (previousSaveFile.exists() && previousSaveFile.length() > 0) {
previousSaveFile.readBytes()
} else {
null
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.swordfish.lemuroid.lib.saves.migrators

import com.swordfish.lemuroid.lib.library.CoreID
import com.swordfish.lemuroid.lib.library.SystemCoreConfig
import com.swordfish.lemuroid.lib.library.db.entity.Game
import com.swordfish.lemuroid.lib.storage.DirectoriesManager

interface SavesMigrator {
fun loadPreviousSaveForGame(
game: Game,
directoriesManager: DirectoriesManager,
): ByteArray?
}

fun SystemCoreConfig.getSavesMigrator(): SavesMigrator? {
return when (this.coreID) {
CoreID.MELONDS -> MelonDsSavesMigrator
else -> null
}
}

0 comments on commit 28a1f11

Please sign in to comment.