Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort Savegames by system in internal storage #690

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"

Expand Down