feat: Refactor autoloadContent to streamline directory scanning for NSPs

This commit is contained in:
Jochem Kuipers 2025-10-27 12:37:16 +01:00
parent 42fa39672b
commit 4e94f4731b

View file

@ -15,8 +15,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.kenjinx.android.KenjinxNative
import org.kenjinx.android.KeyboardMode
import org.kenjinx.android.MainActivity
import java.io.File
import java.util.Locale
import kotlin.concurrent.thread
@ -125,7 +125,7 @@ class HomeViewModel(
// Helper function to compare update versions from filenames
// Returns true if newPath represents a newer version than currentPath
private fun shouldSelectNewerUpdate(currentPath: String, newPath: String, allPaths: List<String>): Boolean {
private fun shouldSelectNewerUpdate(currentPath: String, newPath: String): Boolean {
// Extract version numbers from filenames using regex pattern [vXXXXXX]
val versionPattern = Regex("\\[v(\\d+)]")
@ -135,21 +135,13 @@ class HomeViewModel(
return newVersion > currentVersion
}
// Scans configured directories for NSPs containing DLCs/Updates and associates them to known titles.
// Scans configured directory for NSPs containing DLCs/Updates and associates them to known titles.
private fun autoloadContent() {
val prefs = sharedPref ?: return
// Prefer a single, explicitly selected updates/DLC folder (updatesFolder). Fallback to legacy autoloadDirs.
val updatesFolder = prefs.getString("updatesFolder", "") ?: ""
val dirs: List<String> = if (updatesFolder.isNotBlank()) {
listOf(updatesFolder.trim())
} else {
// Legacy: semicolon-separated list under key autoloadDirs
val raw = prefs.getString("autoloadDirs", "") ?: ""
if (raw.isBlank()) emptyList() else raw.split(';').map { it.trim() }.filter { it.isNotEmpty() }
}
if (dirs.isEmpty()) return
if (updatesFolder.isEmpty()) return
// Build a map of titleId -> helpers
val gamesByTitle = loadedCache.mapNotNull { g ->
@ -160,9 +152,8 @@ class HomeViewModel(
var updatesAdded = 0
var dlcAdded = 0
for (dir in dirs) {
val base = java.io.File(dir)
if (!base.exists() || !base.isDirectory) continue
val base = File(updatesFolder)
if (!base.exists() || !base.isDirectory) return
base.walkTopDown().forEach fileLoop@{ f ->
if (!f.isFile) return@fileLoop
@ -230,7 +221,7 @@ class HomeViewModel(
// or if no update is currently selected
val currentSelected = vm.data?.selected ?: ""
val shouldSelect = currentSelected.isEmpty() ||
shouldSelectNewerUpdate(currentSelected, path, vm.data?.paths ?: mutableListOf())
shouldSelectNewerUpdate(currentSelected, path)
if (shouldSelect) {
vm.data?.selected = path
@ -243,4 +234,3 @@ class HomeViewModel(
}
}
}
}