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 4362c9308c..a1e56b8587 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/BattleNet.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/BattleNet.kt @@ -244,6 +244,11 @@ object BattleNet { "/" + rootfs.relativize(resolved).joinToString("/") } }.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 + } 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 2ada13c310..4b3b81968e 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/BattleNetTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/BattleNetTest.kt @@ -216,6 +216,26 @@ class BattleNetTest { assertNull(BattleNet.hostFolder(context, "G:\\etc\\passwd")) } + @Test fun aDriveAtTheAppDataFolderHoldingTheRuntimeListsTheGamesInside() { + val root = LinuxRuntime.rootDir(context) + val data = context.filesDir.parentFile!! + val pfx = File(BattleNet.prefix(context), "pfx") + val devices = File(pfx, "dosdevices").apply { mkdirs() } + java.nio.file.Files.createSymbolicLink(File(devices, "d:").toPath(), data.absoluteFile.toPath()) + java.nio.file.Files.createSymbolicLink(File(devices, "e:").toPath(), java.nio.file.Paths.get("/root/Games/Card")) + val wow = File(root, "root/Games/Stores/Battle.net/World of Warcraft").apply { mkdirs() } + File(wow, "_forever_").mkdirs() + val drive = "D:/" + data.absoluteFile.toPath().relativize(wow.absoluteFile.toPath()).joinToString("/") + File(pfx, "drive_c/ProgramData/Battle.net/Agent").mkdirs() + File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db").writeBytes(message( + field(1, message(field(1, "wowf"), field(2, "wowf"), field(3, message(field(1, drive))))), + )) + assertEquals(wow, BattleNet.hostFolder(context, drive)) + assertEquals(wow, BattleNet.games(context).single().folder) + assertEquals(File(root, "root/Games/Card/Battle.net/X"), BattleNet.hostFolder(context, "E:\\Battle.net\\X")) + assertNull(BattleNet.hostFolder(context, "D:\\..\\etc")) + } + @Test fun wowForeverIsListedBesideRetailInTheSharedWorldOfWarcraftFolder() { val pfx = File(BattleNet.prefix(context), "pfx").apply { mkdirs() } diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-battlenet b/tools/linuxfs/overlay/usr/local/bin/droiddeck-battlenet index d24ead6eb5..5ad55f656b 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-battlenet +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-battlenet @@ -299,6 +299,27 @@ def map_drive(root, volume): return None +def rootfs(): + host = os.environ.get("DDNATV_ROOT") + return os.path.realpath(host) if host else None + + +def guest_path(path): + host = rootfs() + if not host or not inside(path, host): + return None + return "/" + path[len(host):].strip("/") + + +def rootfs_drive(root, text): + found = re.match(r"(?i)^([abd-y]):[\\/]", text) + host = rootfs() + if not found or not host: + return False + link = os.path.join(dosdevices(root), found.group(1).lower() + ":") + return os.path.islink(link) and inside(host, os.path.realpath(link)) + + def install_path(root, library): library = library.rstrip("/") or "/" try: @@ -308,6 +329,9 @@ def install_path(root, library): return "Z:" + library if volume == "/": return "Z:" + library + host = rootfs() + if host and inside(host, volume): + return "Z:" + (guest_path(path) or library) drive = mapped_drive(root, path, volume) if drive is None and not wine_processes(os.path.join(prefix(root), "pfx")): drive = map_drive(root, volume) @@ -586,11 +610,15 @@ def drive_folder(root, value): text = value.decode("utf-8") except UnicodeDecodeError: return value - if not re.match(r"(?i)^z:[\\/]", text): + holds_root = rootfs_drive(root, text) + if not holds_root and not re.match(r"(?i)^z:[\\/]", text): return value folder = windows_path(root, text) if not folder or not os.path.isdir(folder): return value + if holds_root: + guest = guest_path(os.path.realpath(folder)) + return value if guest is None else ("Z:" + guest).encode("utf-8") moved = install_path(root, folder) return value if moved.startswith("Z:") else moved.replace("\\", "/").encode("utf-8") @@ -725,7 +753,7 @@ def configure(root, library=None): return try: if move_installs(root): - say("Battle.net's installed games on a mapped volume now use its Wine drive") + say("Battle.net's installed games now use the Wine drive of their volume") except OSError as error: say("Battle.net's installed games not moved to their Wine drive: %s" % error) try: diff --git a/tools/tests/test_battlenet.py b/tools/tests/test_battlenet.py index b7bbafa3b4..22c78b9e9b 100644 --- a/tools/tests/test_battlenet.py +++ b/tools/tests/test_battlenet.py @@ -376,6 +376,38 @@ class DriveTest(Temp): self.assertEqual(os.readlink(self.devices / "d:"), str(self.card)) self.assertFalse(BNET["move_installs"](self.root)) + def test_the_volume_holding_the_native_rootfs_gets_no_drive(self): + rootfs = self.card / "files/linuxfs" + library = rootfs / "root/Games/Stores/Battle.net" + library.mkdir(parents=True) + with mock.patch.dict(os.environ, {"DDNATV_ROOT": str(rootfs)}): + self.assertEqual(BNET["install_path"](self.root, str(library)), "Z:/root/Games/Stores/Battle.net") + self.assertEqual(BNET["install_path"](self.root, str(self.library)), "Z:" + str(self.library)) + self.assertFalse(self.devices.exists()) + self.assertEqual(BNET["install_path"](self.root, str(self.library)), "D:\\Games\\Battle.net") + + def test_games_moved_to_a_drive_holding_the_native_rootfs_go_back_to_the_root_drive(self): + rootfs = self.card / "files/linuxfs" + game = rootfs / "root/Games/Stores/Battle.net/World of Warcraft" + game.mkdir(parents=True) + card = self.tmp / "sd" + (card / "Diablo").mkdir(parents=True) + self.devices.mkdir(parents=True) + os.symlink(str(self.card), self.devices / "d:") + os.symlink(str(card), self.devices / "e:") + client = self.product("battle.net", "C:/Program Files (x86)/Battle.net") + sd = self.product("d3", "E:/Diablo") + gone = self.product("gone", "D:/files/linuxfs/root/Games/Missing") + path = self.database(client, self.product("wowf", "D:/files/linuxfs/root/Games/Stores/Battle.net/World of Warcraft"), sd, gone) + before = path.read_bytes() + self.assertFalse(BNET["move_installs"](self.root)) + self.assertEqual(path.read_bytes(), before) + with mock.patch.dict(os.environ, {"DDNATV_ROOT": str(rootfs)}): + self.assertTrue(BNET["move_installs"](self.root)) + self.assertEqual(path.read_bytes(), client + self.product("wowf", "Z:/root/Games/Stores/Battle.net/World of Warcraft") + + sd + gone + self.field(7, b"tail")) + self.assertFalse(BNET["move_installs"](self.root)) + def test_registrations_stay_while_the_prefix_runs_or_the_database_is_unreadable(self): game = self.library / "World of Warcraft" game.mkdir()