diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt index f0808edab5..0c72812d18 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt @@ -243,6 +243,8 @@ object AddedGames { fun addFolder(context: Context, dir: File, onGame: (Game) -> Unit = {}, onChecking: (String?) -> Unit = {}): FolderResult { val app = context.applicationContext + // Binds are mounted at session start, so a new one reaches the session from the next start. + if (guestPath(app, dir) == null) bindFolder(app, dir) val removed = SessionPrefs.removedAddedGames(app).toSet() val listed = listedFolders(app, removed) var already = 0 @@ -266,6 +268,20 @@ object AddedGames { return FolderResult(added, already) } + @Synchronized + internal fun bindFolder(context: Context, dir: File) { + if (!dir.isDirectory || !dir.canRead()) return + val binds = SessionPrefs.gameFolderBinds(context) + if (binds.any { it.first == dir.path }) return + val taken = binds.map { it.second }.toSet() + val base = GamesFolderMigration.legacyRoots(binds.map { it.first } + dir.path).last().guest + var guest = base + var n = 2 + while (guest in taken) guest = "$base-${n++}" + SessionPrefs.setGameFolderBinds(context, binds + (dir.path to guest)) + Log.i(TAG, "games: bound $dir at $guest") + } + private fun listedFolders(context: Context, removed: Set): List { val out = ArrayList() GamesFolderMigration.run(context) diff --git a/app/src/test/java/com/droiddeck/launcher/frontend/AddedExesTest.kt b/app/src/test/java/com/droiddeck/launcher/frontend/AddedExesTest.kt index 2d0a29de40..6cc36e909b 100644 --- a/app/src/test/java/com/droiddeck/launcher/frontend/AddedExesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/frontend/AddedExesTest.kt @@ -4,6 +4,7 @@ import android.os.Environment import com.droiddeck.launcher.session.SessionPrefs import java.io.File import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test @@ -92,6 +93,50 @@ class AddedExesTest { assertEquals(0, AddedGames.scan(app).size) } + @Test fun addingAFolderTheSessionCannotSeeBindsItAndAddsItsGames() { + noGamesFolders() + val games = File(tmp.newFolder("card"), "Winlator/Games") + exe(File(games, "Alpha/Alpha.exe")) + exe(File(games, "Beta/Beta.exe")) + File(games, "steamapps").mkdirs() + assertNull(AddedGames.guestPath(app, games)) + + val result = AddedGames.addFolder(app, games) + assertEquals(listOf("Alpha", "Beta"), result.added.map { it.name }) + assertEquals(listOf(games.path to "${AddedGames.GUEST_DIR}/Games"), SessionPrefs.gameFolderBinds(app)) + assertEquals("${AddedGames.GUEST_DIR}/Games/Alpha/Alpha.exe", result.added.first().guestExe) + + val again = AddedGames.addFolder(app, games) + assertEquals(0, again.added.size) + assertEquals(2, again.already) + assertEquals(1, SessionPrefs.gameFolderBinds(app).size) + } + + @Test fun aSecondFolderWithTheSameNameGetsItsOwnGuestPath() { + noGamesFolders() + val first = File(tmp.newFolder("a"), "Games").apply { exe(File(this, "One/One.exe")) } + val second = File(tmp.newFolder("b"), "Games").apply { exe(File(this, "Two/Two.exe")) } + AddedGames.addFolder(app, first) + AddedGames.addFolder(app, second) + val guests = SessionPrefs.gameFolderBinds(app).map { it.second } + assertEquals(2, guests.toSet().size) + assertEquals("${AddedGames.GUEST_DIR}/Games", guests.first()) + } + + @Test fun aGuestPathAlreadyTakenIsNeverReused() { + noGamesFolders() + val a = File(tmp.newFolder("a"), "Games").apply { mkdirs() } + val b = File(tmp.newFolder("b"), "Games").apply { mkdirs() } + val c = File(tmp.newFolder("c"), "Games").apply { exe(File(this, "Three/Three.exe")) } + val clash = GamesFolderMigration.legacyRoots(listOf(a.path, b.path, c.path)).last().guest + SessionPrefs.setGameFolderBinds(app, listOf(a.path to "${AddedGames.GUEST_DIR}/Games", b.path to clash)) + AddedGames.addFolder(app, c) + val guests = SessionPrefs.gameFolderBinds(app).map { it.second } + assertEquals(3, guests.size) + assertEquals(3, guests.toSet().size) + assertEquals("$clash-2", guests.last()) + } + @Test fun aRemovedSingleExeGameStaysOffUntilPickedAgain() { noGamesFolders() val pick = onStorage("Download/Celeste/Celeste.exe")