added games: any number of Games folders from anywhere, each bound under /root/Games on its own

Folders are a list (the single folder from before carries over); each gets /root/Games/<name>,
with a short hash of its path only when two chosen folders share a name, so adding or removing
one never moves another's games (their appids follow the guest exe path). Per-game exe choices
are keyed by the folder's path now. The cog lists each folder with its game count and a Forget,
plus an Add row.
This commit is contained in:
The412Banner committed 2026-09-23 09:12:26 -04:00
1 parent 8f299d8752
commit b834101cad
5 files changed
+95 -57

No files matched your search

@@ -116,8 +116,8 @@ class MainActivity : ComponentActivity() {
}
private val pickAddedGamesDir = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { r ->
if (r.resultCode == RESULT_OK) InAppFilePicker.pickedPath(r.data)?.let { path ->
SessionPrefs.setAddedGamesDir(this, path)
addedGamesDir = path
SessionPrefs.setAddedGamesDirs(this, addedGamesDirs + path)
addedGamesDirs = SessionPrefs.addedGamesDirs(this)
refreshAddedGames()
refresh()
}
@@ -132,7 +132,7 @@ class MainActivity : ComponentActivity() {
refresh()
}
}
private var addedGamesDir by mutableStateOf("")
private var addedGamesDirs by mutableStateOf<List<String>>(emptyList())
private var addedGames by mutableStateOf<List<com.steamdeck.launcher.ui.AddedGameRow>>(emptyList())
private val pickRomsDir = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { r ->
if (r.resultCode == RESULT_OK) InAppFilePicker.pickedPath(r.data)?.let { path ->
@@ -392,7 +392,7 @@ class MainActivity : ComponentActivity() {
storageOptions = storageOptions,
fexPreset = if (mode == SessionService.MODE_STEAM) fexPreset else null,
steamChannel = if (mode == SessionService.MODE_STEAM) steamChannel else null,
addedGamesDir = if (mode == SessionService.MODE_STEAM) addedGamesDir else null,
addedGamesDirs = if (mode == SessionService.MODE_STEAM) addedGamesDirs else null,
addedGames = if (mode == SessionService.MODE_STEAM) addedGames else emptyList(),
),
ModeSettingsActions(
@@ -424,12 +424,12 @@ class MainActivity : ComponentActivity() {
},
onFexPreset = { id -> SessionPrefs.setFexPreset(this, id); fexPreset = id },
onSteamChannel = { id -> SessionPrefs.setSteamChannel(this, id); steamChannel = id },
onPickAddedGamesDir = { pickAddedGamesDir.launch(InAppFilePicker.buildDirIntent(this, "Choose the folder of your own games", addedGamesDir.ifEmpty { null })) },
onClearAddedGamesDir = { SessionPrefs.setAddedGamesDir(this, ""); addedGamesDir = ""; addedGames = emptyList(); refresh() },
onPickAddedGamesDir = { pickAddedGamesDir.launch(InAppFilePicker.buildDirIntent(this, "Choose a folder of your own games", addedGamesDirs.lastOrNull())) },
onForgetAddedGamesDir = { dir -> SessionPrefs.setAddedGamesDirs(this, addedGamesDirs - dir); addedGamesDirs = SessionPrefs.addedGamesDirs(this); refreshAddedGames(); refresh() },
onAddedGameExe = { folder, path -> SessionPrefs.setAddedGameExe(this, folder, path); refreshAddedGames(); refresh() },
onPickAddedGameExe = { folder ->
pendingAddedGame = folder
pickAddedGameExe.launch(InAppFilePicker.buildIntent(this, listOf("exe"), "Choose the game's .exe", File(addedGamesDir, folder).path))
pickAddedGameExe.launch(InAppFilePicker.buildIntent(this, listOf("exe"), "Choose the game's .exe", folder))
},
onDismiss = { settingsMode = null },
),
@@ -467,7 +467,7 @@ class MainActivity : ComponentActivity() {
/** The added games as the settings page lists them; a scan of the folder, on this thread (one level, small). */
private fun refreshAddedGames() {
addedGames = com.steamdeck.launcher.frontend.AddedGames.scan(this).map { g ->
com.steamdeck.launcher.ui.AddedGameRow(g.folderName(), g.exe.path, g.exe.name, g.candidates.map { c -> c.path to c.name }.distinctBy { it.first })
com.steamdeck.launcher.ui.AddedGameRow(g.folder.path, g.folderName(), g.exe.path, g.exe.name, g.candidates.map { c -> c.path to c.name }.distinctBy { it.first })
}
}
@@ -476,7 +476,7 @@ class MainActivity : ComponentActivity() {
resolutionCap = SessionPrefs.resolutionCap(this, mode)
fexPreset = SessionPrefs.fexPreset(this)
steamChannel = SessionPrefs.steamChannel(this)
addedGamesDir = SessionPrefs.addedGamesDir(this)
addedGamesDirs = SessionPrefs.addedGamesDirs(this)
refreshAddedGames()
shapeMode = SessionPrefs.shapeMode(this)
hdrOn = SessionPrefs.hdr(this, mode)
@@ -16,8 +16,8 @@ import java.util.zip.CRC32
* largest, never an installer, redistributable or crash reporter - unless the user picked one
* for that game. Each game becomes a non-Steam shortcut in the client, under the ARM64 Proton,
* with the appid the client itself would derive, so launching from the rail and from the
* client's own library are the same thing. The folder has to be somewhere the session can see:
* internal storage (/root/Storage), the ROMs folder, or the SD-card game library.
* client's own library are the same thing. Any number of Games folders, from anywhere on the
* device: each is bound into the session under /root/Games on its own.
*/
object AddedGames {
private const val TAG = "AddedGames"
@@ -33,25 +33,42 @@ object AddedGames {
fun folderName(): String = folder.name
}
/** One Games folder and where the session sees it. */
class Root(val host: File, val guest: String)
/**
* The chosen folders with their guest paths: /root/Games/<folder name>, or with a short hash of
* the host path when two chosen folders share a name (so a folder's guest path, and with it
* every shortcut's appid, does not change when another folder is added or removed).
*/
fun roots(context: Context): List<Root> {
val hosts = SessionPrefs.addedGamesDirs(context).map { File(it) }
val names = hosts.groupingBy { it.name.lowercase() }.eachCount()
return hosts.map { host ->
val name = host.name.ifEmpty { "games" }
val guestName = if ((names[name.lowercase()] ?: 0) > 1) name + "-" + "%08x".format(CRC32().apply { update(host.path.toByteArray()) }.value).take(4) else name
Root(host, "$GUEST_DIR/$guestName")
}
}
private val SKIP = Regex(
"(?i)^(unins.*|setup.*|.*redist.*|vcredist.*|dxsetup.*|dxwebsetup.*|.*crash.*|.*report.*|dotnet.*|directx.*|.*prereq.*" +
"|.*installer.*|.*uninstall.*|.*updater?.*|.*config(ur.*)?|.*settings.*|.*editor.*|.*server.*|.*benchmark.*|.*helper.*|.*eac.*|.*easyanticheat.*|.*battleye.*)\\.exe$",
)
/** Where a host path appears inside the session, or null when the session cannot see it. */
/** Where the chosen Games folder itself is bound inside the session, wherever it is on the device. */
/** Under here the chosen Games folders are bound inside the session, one each. */
const val GUEST_DIR = "/root/Games"
fun guestPath(context: Context, host: File): String? {
val path = host.absolutePath
// The Games folder is bound on its own, so a folder anywhere - an SD card, a USB drive -
// works without being inside one of the other binds.
SessionPrefs.addedGamesDir(context).takeIf { it.isNotEmpty() }?.let { dir ->
if (path == dir) return GUEST_DIR
if (path.startsWith("$dir/")) return "$GUEST_DIR/" + path.removePrefix("$dir/")
// The Games folders are bound on their own, so a folder anywhere - an SD card, a USB
// drive - works without being inside one of the other binds.
for (root in roots(context)) {
val dir = root.host.absolutePath
if (path == dir) return root.guest
if (path.startsWith("$dir/")) return root.guest + "/" + path.removePrefix("$dir/")
}
val storage = Environment.getExternalStorageDirectory().absolutePath
if (path.startsWith("$storage/")) return "/root/Storage/" + path.removePrefix("$storage/")
SessionPrefs.romsDir(context).takeIf { it.isNotEmpty() }?.let { roms ->
if (path.startsWith("$roms/")) return "/root/ROMs/" + path.removePrefix("$roms/")
}
@@ -78,22 +95,28 @@ object AddedGames {
}
fun scan(context: Context): List<Game> {
val dir = SessionPrefs.addedGamesDir(context).takeIf { it.isNotEmpty() }?.let { File(it) } ?: return emptyList()
if (!dir.isDirectory) return emptyList()
val out = ArrayList<Game>()
for (folder in dir.listFiles { f -> f.isDirectory }?.sortedBy { it.name.lowercase() } ?: emptyList()) {
for (root in roots(context)) {
val dir = root.host
if (!dir.isDirectory) { Log.w(TAG, "$dir is not a folder; skipped"); continue }
for (folder in dir.listFiles { f -> f.isDirectory }?.sortedBy { it.name.lowercase() } ?: emptyList()) scanGame(context, folder, out)
}
return out
}
private fun scanGame(context: Context, folder: File, out: MutableList<Game>) {
run {
val candidates = candidates(folder)
val chosen = SessionPrefs.addedGameExe(context, folder.name).takeIf { it.isNotEmpty() }?.let { File(it) }?.takeIf { it.isFile }
val exe = chosen ?: candidates.firstOrNull() ?: continue
val chosen = SessionPrefs.addedGameExe(context, folder.path).takeIf { it.isNotEmpty() }?.let { File(it) }?.takeIf { it.isFile }
val exe = chosen ?: candidates.firstOrNull() ?: return
val guestExe = guestPath(context, exe)
if (guestExe == null) { Log.w(TAG, "${folder.name}: the session cannot see ${exe.path}"); continue }
val guestDir = guestPath(context, exe.parentFile ?: folder) ?: continue
if (guestExe == null) { Log.w(TAG, "${folder.name}: the session cannot see ${exe.path}"); return }
val guestDir = guestPath(context, exe.parentFile ?: folder) ?: return
val name = folder.name
val crc = CRC32().apply { update(("\"$guestExe\"" + name).toByteArray()) }.value
val appId = crc or 0x80000000L
out.add(Game(folder, name, exe, guestExe, guestDir, appId, (appId shl 32) or 0x02000000L, candidates))
}
return out
}
/** The list the session hands the runtime's shortcuts writer; one file per session start. */
@@ -233,18 +233,27 @@ object SessionPrefs {
prefs(context).edit().putString("steamChannel", id).apply()
}
/** The folder of the user's own Windows games (one subfolder each), "" = none. */
fun addedGamesDir(context: Context): String = prefs(context).getString("addedGamesDir", "") ?: ""
fun setAddedGamesDir(context: Context, path: String) {
prefs(context).edit().putString("addedGamesDir", path).apply()
/**
* The folders of the user's own Windows games (one game per subfolder), any number of them
* from anywhere on the device. The single folder an earlier build kept is carried in.
*/
fun addedGamesDirs(context: Context): List<String> {
val p = prefs(context)
val list = p.getString("addedGamesDirs", null)
if (list != null) return list.split('\n').filter { it.isNotEmpty() }
val old = p.getString("addedGamesDir", "") ?: ""
return if (old.isEmpty()) emptyList() else listOf(old)
}
/** The .exe the user chose for one game folder (by folder name), "" = the scanner's pick. */
fun addedGameExe(context: Context, folderName: String): String = prefs(context).getString("addedExe:$folderName", "") ?: ""
fun setAddedGamesDirs(context: Context, dirs: List<String>) {
prefs(context).edit().putString("addedGamesDirs", dirs.distinct().joinToString("\n")).remove("addedGamesDir").apply()
}
fun setAddedGameExe(context: Context, folderName: String, path: String) {
prefs(context).edit().putString("addedExe:$folderName", path).apply()
/** The .exe the user chose for one game folder (by its path), "" = the scanner's pick. */
fun addedGameExe(context: Context, folderPath: String): String = prefs(context).getString("addedExe:$folderPath", "") ?: ""
fun setAddedGameExe(context: Context, folderPath: String, path: String) {
prefs(context).edit().putString("addedExe:$folderPath", path).apply()
}
/** The app's colour theme (ui/Themes ids); Paper on black unless chosen otherwise. */
@@ -396,13 +396,14 @@ class SessionService : Service() {
}
// The user's own games folder (the Steam cog's "Added games"), bound at a fixed place so
// the shortcuts the app writes point somewhere whatever storage the folder is on.
val games = SessionPrefs.addedGamesDir(this).takeIf { it.isNotEmpty() }?.let { File(it) }
if (games != null && games.isDirectory && games.canRead()) {
File(home, "Games").mkdirs()
binds.add(games.path + ":" + com.steamdeck.launcher.frontend.AddedGames.GUEST_DIR)
Log.i(TAG, "added games: $games -> ${com.steamdeck.launcher.frontend.AddedGames.GUEST_DIR}")
} else if (games != null) {
Log.w(TAG, "added games: $games is not a readable folder this session")
for (root in com.steamdeck.launcher.frontend.AddedGames.roots(this)) {
if (root.host.isDirectory && root.host.canRead()) {
File(LinuxRuntime.rootDir(this), root.guest.removePrefix("/")).mkdirs()
binds.add(root.host.path + ":" + root.guest)
Log.i(TAG, "added games: ${root.host} -> ${root.guest}")
} else {
Log.w(TAG, "added games: ${root.host} is not a readable folder this session")
}
}
val roms = SessionPrefs.romsDir(this).takeIf { it.isNotEmpty() }?.let { File(it) }
if (roms != null && roms.isDirectory && roms.canRead()) {
@@ -38,12 +38,13 @@ class ModeSettings(
/** Steam only: the client branch forced on the command line. */
val steamChannel: String? = null,
/** Steam only: the user's own games folder and what was found in it. */
val addedGamesDir: String? = null,
/** The chosen Games folders (null = not a Steam page). */
val addedGamesDirs: List<String>? = null,
val addedGames: List<AddedGameRow> = emptyList(),
)
/** One added game as the settings page shows it: its folder, the chosen .exe, the other .exe files it could be. */
class AddedGameRow(val folderName: String, val exePath: String, val exeName: String, val candidates: List<Pair<String, String>>)
class AddedGameRow(val folderPath: String, val folderName: String, val exePath: String, val exeName: String, val candidates: List<Pair<String, String>>)
class ModeSettingsActions(
val onResolution: (Int) -> Unit,
@@ -65,9 +66,9 @@ class ModeSettingsActions(
val onFexPreset: (String) -> Unit = {},
val onSteamChannel: (String) -> Unit = {},
val onPickAddedGamesDir: () -> Unit = {},
val onClearAddedGamesDir: () -> Unit = {},
val onAddedGameExe: (folderName: String, path: String) -> Unit = { _, _ -> },
val onPickAddedGameExe: (folderName: String) -> Unit = {},
val onForgetAddedGamesDir: (path: String) -> Unit = {},
val onAddedGameExe: (folderPath: String, path: String) -> Unit = { _, _ -> },
val onPickAddedGameExe: (folderPath: String) -> Unit = {},
val onDismiss: () -> Unit,
)
@@ -145,20 +146,24 @@ fun ModeSettingsPage(s: ModeSettings, a: ModeSettingsActions) {
onPick = a.onSteamChannel,
)
}
if (steam && s.addedGamesDir != null) SettingsGroup("Added games") {
if (steam && s.addedGamesDirs != null) SettingsGroup("Added games") {
for (dir in s.addedGamesDirs) {
val n = s.addedGames.count { it.folderPath.startsWith("$dir/") }
ActionRow(
dir.substringAfterLast('/').ifEmpty { dir }, dir + " · " + (if (n == 0) "no game folders with a .exe found" else "$n game${if (n == 1) "" else "s"}") + ". Forget: the games leave the client's library at the next session start; nothing on disk is touched.",
"Forget", onClick = { a.onForgetAddedGamesDir(dir) },
)
}
ActionRow(
"Games folder",
if (s.addedGamesDir.isEmpty()) "Your own Windows games, one subfolder each, anywhere on internal storage, in the ROMs folder or on the SD-card library. Each goes into the client's library as a non-Steam game under the ARM64 Proton, at the next session start."
else s.addedGamesDir + " · " + (if (s.addedGames.isEmpty()) "no game folders with a .exe found" else "${s.addedGames.size} game${if (s.addedGames.size == 1) "" else "s"}"),
if (s.addedGamesDir.isEmpty()) "Choose…" else "Change…",
onClick = a.onPickAddedGamesDir,
if (s.addedGamesDirs.isEmpty()) "Games folder" else "Another games folder",
"Your own Windows games, one subfolder each, anywhere: internal storage, the SD card, a USB drive. As many folders as you like. Each game goes into the client's library as a non-Steam game under the ARM64 Proton, at the next session start.",
"Add…", onClick = a.onPickAddedGamesDir,
)
if (s.addedGamesDir.isNotEmpty()) ActionRow("Forget the folder", "The games leave the client's library at the next session start; nothing on disk is touched.", "Forget", onClick = a.onClearAddedGamesDir)
for (g in s.addedGames) ChoiceRow(
host, "added:" + g.folderName, g.folderName, "Launches ${g.exeName}",
host, "added:" + g.folderPath, g.folderName, "Launches ${g.exeName}" + (if (s.addedGamesDirs.size > 1) " · in " + g.folderPath.substringBeforeLast('/').substringAfterLast('/') else ""),
g.candidates + ("__pick__" to "Choose another file…"), g.exePath,
note = "The .exe files found in the game's folder; the one named after the folder, else the largest, is picked unless you choose.",
onPick = { path -> if (path == "__pick__") a.onPickAddedGameExe(g.folderName) else a.onAddedGameExe(g.folderName, path) },
onPick = { path -> if (path == "__pick__") a.onPickAddedGameExe(g.folderPath) else a.onAddedGameExe(g.folderPath, path) },
)
}
if (steam && s.fexPreset != null) SettingsGroup("Games") {