deferred savegame collection for faster startup

This commit is contained in:
minjaesong
2023-09-21 16:22:29 +09:00
parent 8508956ad6
commit e2a71467db
4 changed files with 25 additions and 8 deletions

View File

@@ -26,10 +26,10 @@ import kotlin.math.roundToInt
/**
* Created by minjaesong on 2023-06-24.
*/
class SavegameCollection(files0: List<DiskSkimmer>) {
class SavegameCollection(files0: List<DiskSkimmer>, prefiltered: Boolean) {
/** Sorted in reverse by the last modified time of the files, index zero being the most recent */
val files = files0.sortedBy { it.diskFile.name }.sortedByDescending {
val files = if (prefiltered) files0 else files0.sortedBy { it.diskFile.name }.sortedByDescending {
it.getLastModifiedTime().shl(2) or
it.diskFile.extension.matches(Regex("^[abc]${'$'}")).toLong(1) or
it.diskFile.extension.isBlank().toLong(0)
@@ -40,19 +40,28 @@ class SavegameCollection(files0: List<DiskSkimmer>) {
val manualSaves = files.filter { !it.diskFile.extension.matches(Regex("[a-z]")) }
init {
printdbg(this, "Rebuilding skimmers (${files.size})")
// files.forEach { it.rebuild() }
}
fun rebuildLoadable() {
loadable().rebuild()
}
fun rebuildAll() {
files.forEach { it.rebuild() }
}
companion object {
fun collectFromBaseFilename(skimmers: List<DiskSkimmer>, name: String): SavegameCollection {
return SavegameCollection(skimmers)
return SavegameCollection(skimmers, true)
}
fun collectFromBaseFilename(dir: File, name: String): SavegameCollection {
val files = dir.listFiles().filter { it.name.startsWith(name) }
.mapNotNull { try { DiskSkimmer(it, true) } catch (e: Throwable) { null } }
return SavegameCollection(files)
return SavegameCollection(files, false)
}
}

View File

@@ -799,7 +799,7 @@ fun AppUpdateListOfSavegames() {
null
}
}.sortedByDescending { it.getLastModifiedTime() }
val filteringResults = arrayListOf<List<DiskSkimmer>>()
val filteringResults = arrayListOf<List<DiskSkimmer>>() // first element of the list is always file with no suffix
worldsDirLs.forEach {
val li = arrayListOf(it)
listOf(".1",".2",".3",".a",".b",".c").forEach { suffix ->
@@ -818,6 +818,8 @@ fun AppUpdateListOfSavegames() {
printdbg("ListSavegames", " collecting...")
val collection = SavegameCollection.collectFromBaseFilename(list, it.diskFile.name)
printdbg("ListSavegames", " disk rebuilding...")
collection.rebuildLoadable()
printdbg("ListSavegames", " get UUID...")
val worldUUID = collection.getUUID()
@@ -862,6 +864,8 @@ fun AppUpdateListOfSavegames() {
printdbg("ListSavegames", " collecting...")
val collection = SavegameCollection.collectFromBaseFilename(list, it.diskFile.name)
printdbg("ListSavegames", " disk rebuilding...")
collection.rebuildLoadable()
printdbg("ListSavegames", " get UUID...")
val playerUUID = collection.getUUID()

View File

@@ -523,6 +523,7 @@ class UIItemPlayerCells(
val loadable = App.savegamePlayers[playerUUID]!!.loadable()
printdbg(this, "UUID: ${playerUUID}")
printdbg(this, "File: ${loadable.diskFile.absolutePath}")
loadable.rebuild()
loadable.getFile(SAVEGAMEINFO)?.bytes?.let {
var lastPlayTime0 = 0L

View File

@@ -68,6 +68,7 @@ class UILoadManage(val full: UILoadSavegame) : UICanvas() {
if (altDown && savegameIsNotNew) {
mode = MODE_PREV_SAVES
loadPrevGameInfo()
}
else {
if (full.loadables.saveAvaliable()) {
@@ -242,19 +243,20 @@ class UILoadManage(val full: UILoadSavegame) : UICanvas() {
super.show()
}
internal fun loadSavegameInfo() {
private fun loadPrevGameInfo() {
val players = App.savegamePlayers[full.playerButtonSelected!!.playerUUID]!!.files
val worlds = App.savegameWorlds[full.playerButtonSelected!!.worldUUID]!!.files
val playerSavesInfo = players.map { it.getSavegameMeta() }
val worldSavesInfo = worlds.map { it.getSavegameMeta() }
sortedPlayerWorldList = getChronologicalPair(playerSavesInfo, worldSavesInfo)
px48 = playerModTextboxX + (modulesTextboxW - tbw48) / 2
wx48 = worldModTextboxX + (modulesTextboxW - tbw48) / 2
totalTBH48 = sortedPlayerWorldList.size * 32
}
internal fun loadSavegameInfo() {
playerName = App.savegamePlayersName[full.playerButtonSelected!!.playerUUID] ?: "Player"
loadOrderPlayer =
@@ -437,6 +439,7 @@ class UILoadManage(val full: UILoadSavegame) : UICanvas() {
}
private fun DiskSkimmer.getSavegameMeta(): SavegameMeta {
if (!this.initialised) this.rebuild()
this.getFile(SAVEGAMEINFO)!!.bytes.let {
var lastPlayTime = 0L
var versionString = ""