diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/GamesFolderMigration.kt b/app/src/main/java/com/droiddeck/launcher/frontend/GamesFolderMigration.kt index 5ecf529126..3a9b31232e 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/GamesFolderMigration.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/GamesFolderMigration.kt @@ -13,6 +13,7 @@ object GamesFolderMigration { @Synchronized fun run(context: Context) { + repathInternalGuest(context) val dirs = SessionPrefs.addedGamesDirs(context) if (dirs.isEmpty()) return val roots = legacyRoots(SessionPrefs.gameFolderBinds(context).map { it.first } + dirs) @@ -58,6 +59,23 @@ object GamesFolderMigration { SessionPrefs.setGamesFoldersMigrated(context) } + private fun repathInternalGuest(context: Context) { + val binds = SessionPrefs.gameFolderBinds(context) + if (binds.none { it.second == StoreInstallRoot.INTERNAL_GUEST }) return + val hosts = binds.map { it.first } + val roots = legacyRoots(hosts).associateBy { it.host.path } + val taken = binds.map { it.second }.filter { it != StoreInstallRoot.INTERNAL_GUEST }.toMutableSet() + SessionPrefs.setGameFolderBinds(context, binds.map { (host, guest) -> + if (guest != StoreInstallRoot.INTERNAL_GUEST) return@map host to guest + val base = roots.getValue(host).guest + var next = base + var n = 2 + while (next in taken) next = "$base-${n++}" + taken.add(next) + host to next + }) + } + internal fun legacyRoots(paths: List): List { val hosts = paths.distinct().map { File(it) } val names = hosts.groupingBy { it.name.lowercase() }.eachCount() diff --git a/app/src/test/java/com/droiddeck/launcher/frontend/GamesFolderMigrationTest.kt b/app/src/test/java/com/droiddeck/launcher/frontend/GamesFolderMigrationTest.kt index 43dd005852..2ce069e0c1 100644 --- a/app/src/test/java/com/droiddeck/launcher/frontend/GamesFolderMigrationTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/frontend/GamesFolderMigrationTest.kt @@ -195,6 +195,22 @@ class GamesFolderMigrationTest { assertEquals(binds.getValue(stores.path) + "/One/One.exe", games.getValue("One").guestExe) } + @Test fun aSavedBindAtTheInternalStoreGuestIsRepathed() { + val stores = File(tmp.newFolder("card"), "Stores").apply { exe(this, "One/One.exe") } + val pc = tmp.newFolder("PC").apply { exe(this, "Two/Two.exe") } + SessionPrefs.setGameFolderBinds( + app, + listOf(stores.path to StoreInstallRoot.INTERNAL_GUEST, pc.path to AddedGames.GUEST_DIR + "/PC"), + ) + GamesFolderMigration.run(app) + val binds = SessionPrefs.gameFolderBinds(app).toMap() + assertTrue(binds.getValue(stores.path) != StoreInstallRoot.INTERNAL_GUEST) + assertTrue(binds.getValue(stores.path).startsWith("${AddedGames.GUEST_DIR}/Stores-")) + assertEquals(AddedGames.GUEST_DIR + "/PC", binds.getValue(pc.path)) + assertTrue(binds.values.none { it == StoreInstallRoot.INTERNAL_GUEST }) + assertEquals(binds.getValue(stores.path) + "/One/One.exe", AddedGames.guestPath(app, File(stores, "One/One.exe"))) + } + @Test fun twoFoldersOfOneNameKeepTheirOwnGuestPaths() { val a = File(tmp.newFolder("a"), "Games").apply { mkdirs() } val b = File(tmp.newFolder("b"), "Games").apply { mkdirs() }