diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/BattleNet.kt b/app/src/main/java/com/droiddeck/launcher/runtime/BattleNet.kt index a1e56b8587..86a3295da4 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/BattleNet.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/BattleNet.kt @@ -207,7 +207,8 @@ object BattleNet { val taken = HashSet() fun options(title: Title, folder: File) = listOfNotNull(folder, title.flavor?.let { File(folder, it) }?.takeIf { it.isDirectory }) for ((code, game) in games) { - val keep = saved[code]?.let(::File)?.takeIf { it in options(game.first, game.second) && it !in taken } ?: continue + val want = saved[code]?.let { runCatching { File(it).canonicalFile }.getOrNull() } ?: continue + val keep = options(game.first, game.second).firstOrNull { runCatching { it.canonicalFile }.getOrNull() == want }?.takeIf { it !in taken } ?: continue out[code] = keep taken.add(keep) } @@ -245,9 +246,12 @@ object BattleNet { } }.trimEnd('/') if (mapped?.startsWith("/") == true) { - val host = File("$target/$relative") val rootfs = LinuxRuntime.rootDir(context) - if (runCatching { host.canonicalFile.startsWith(rootfs.canonicalFile) }.getOrDefault(false)) return host + runCatching { + val home = rootfs.canonicalFile.toPath() + val host = File("$target/$relative").canonicalFile.toPath() + if (host.startsWith(home)) File(rootfs, home.relativize(host).toString()) else null + }.getOrNull()?.let { return it } } return com.droiddeck.launcher.frontend.AddedGames.hostPath(context, "$target/$relative") } diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/BattleNetTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/BattleNetTest.kt index 4b3b81968e..35bedaee0b 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/BattleNetTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/BattleNetTest.kt @@ -236,6 +236,21 @@ class BattleNetTest { assertNull(BattleNet.hostFolder(context, "D:\\..\\etc")) } + @Test fun aDriveAtAnAliasOfTheAppDataFolderListsTheGameUnderTheRuntimePath() { + val root = LinuxRuntime.rootDir(context) + val data = context.filesDir.parentFile!! + val alias = File(data.parentFile, "alias-" + data.name) + java.nio.file.Files.createSymbolicLink(alias.toPath(), data.absoluteFile.toPath()) + try { + val devices = File(BattleNet.prefix(context), "pfx/dosdevices").apply { mkdirs() } + java.nio.file.Files.createSymbolicLink(File(devices, "d:").toPath(), alias.absoluteFile.toPath()) + val game = File(root, "root/Games/Stores/Battle.net/StarCraft").apply { mkdirs() } + val drive = "D:\\" + data.absoluteFile.toPath().relativize(game.absoluteFile.toPath()).joinToString("\\") + assertEquals(game.path, BattleNet.hostFolder(context, drive)?.path) + } finally { + java.nio.file.Files.delete(alias.toPath()) + } + } @Test fun wowForeverIsListedBesideRetailInTheSharedWorldOfWarcraftFolder() { val pfx = File(BattleNet.prefix(context), "pfx").apply { mkdirs() } @@ -294,6 +309,21 @@ class BattleNetTest { assertEquals(mapOf("WoWF" to root), BattleNet.ownFolders(mapOf("WoWF" to (forever to root)), mapOf("WoWF" to "/elsewhere"))) } + @Test fun aFolderSavedThroughAnAliasOfTheAppDataFolderIsKept() { + val data = context.filesDir.parentFile!! + val root = File(context.filesDir, "World of Warcraft").apply { mkdirs() } + val foreverDir = File(root, "_forever_").apply { mkdirs() } + val forever = BattleNet.title("wow_forever")!! + val alias = File(data.parentFile, "alias-" + data.name) + java.nio.file.Files.createSymbolicLink(alias.toPath(), data.absoluteFile.toPath()) + try { + val saved = File(alias, data.absoluteFile.toPath().relativize(foreverDir.absoluteFile.toPath()).toString()).path + assertEquals(mapOf("WoWF" to foreverDir), BattleNet.ownFolders(mapOf("WoWF" to (forever to root)), mapOf("WoWF" to saved))) + } finally { + java.nio.file.Files.delete(alias.toPath()) + } + } + @Test fun theListedFolderIsRememberedAcrossScans() { val pfx = File(BattleNet.prefix(context), "pfx").apply { mkdirs() } val wow = File(pfx, "drive_c/Program Files (x86)/World of Warcraft").apply { mkdirs() } diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-battlenet b/tools/linuxfs/overlay/usr/local/bin/droiddeck-battlenet index 5ad55f656b..0f426d9029 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-battlenet +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-battlenet @@ -636,11 +636,115 @@ def move_installs(root): return False except (OSError, ValueError): return False + write_bytes(path, moved) + return True + + +def rootfs_drives(root): + host = rootfs() + folder = dosdevices(root) + if not host: + return {} + try: + names = sorted(os.listdir(folder)) + except OSError: + return {} + drives = {} + for name in names: + link = os.path.join(folder, name) + if len(name) == 2 and name[0].lower() in DRIVES and name[1] == ":" and os.path.islink(link): + target = os.path.realpath(link) + if inside(host, target): + drives[name[0].lower()] = host[len(target):].strip("/") + return drives + + +def write_bytes(path, data): with open(path + ".tmp", "wb") as f: - f.write(moved) + f.write(data) shutil.copymode(path, path + ".tmp") os.replace(path + ".tmp", path) - return True + + +def install_folders(root): + try: + with open(os.path.join(drive_c(root), *PRODUCT_DB), "rb") as f: + data = f.read() + except FileNotFoundError: + return [] + folders = [] + rewrite_field(data, INSTALL_FOLDER, lambda value: folders.append(value) or value) + return folders + + +def json_strings(value): + if isinstance(value, str): + yield value + elif isinstance(value, dict): + for key, item in value.items(): + yield key + yield from json_strings(item) + elif isinstance(value, list): + for item in value: + yield from json_strings(item) + + +def escapes(tail): + depth = 0 + for part in re.split(rb"[\\/]+", tail): + if part == b"..": + depth -= 1 + if depth < 0: + return True + elif part and part != b".": + depth += 1 + return False + + +def drop_rootfs_drives(root): + pfx = os.path.join(prefix(root), "pfx") + drives = rootfs_drives(root) + if not drives or wine_processes(pfx): + return [] + try: + folders = install_folders(root) + except ValueError: + return [] + texts = {} + for path in (os.path.join(pfx, "system.reg"), os.path.join(pfx, "user.reg"), os.path.join(drive_c(root), *CONFIG)): + try: + with open(path, "rb") as f: + texts[path] = f.read() + except FileNotFoundError: + pass + sep = rb"(?:\\\\|/)" + changed = dict(texts) + for letter, rest in drives.items(): + head = rb"(?