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 e3a64e9628..72e77145c9 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/BattleNet.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/BattleNet.kt @@ -17,13 +17,14 @@ object BattleNet { const val PROGRAM = "/usr/local/bin/droiddeck-battlenet" - class Title(val uid: String, val name: String, val code: String, val steam: Int? = null, val shop: String? = null, val product: String? = null) + class Title(val uid: String, val name: String, val code: String, val steam: Int? = null, val shop: String? = null, val product: String? = null, val flavor: String? = null) internal val TITLES = listOf( - Title("wow", "World of Warcraft", "WoW", shop = "world-of-warcraft-midnight", product = "wow"), - Title("wow_classic", "World of Warcraft Classic", "WoWC", shop = "world-of-warcraft-burning-crusade-classic-anniversary-edition-upgrades", product = "wow_classic"), - Title("wow_classic_era", "World of Warcraft Classic", "WoWC", product = "wow_classic_era"), - Title("wow_classic_anniversary", "World of Warcraft Classic", "WoWC", product = "wow_anniversary"), + Title("wow", "World of Warcraft", "WoW", shop = "world-of-warcraft-midnight", product = "wow", flavor = "_retail_"), + Title("wow_classic", "World of Warcraft Classic", "WoWC", shop = "world-of-warcraft-burning-crusade-classic-anniversary-edition-upgrades", product = "wow_classic", flavor = "_classic_"), + Title("wow_classic_era", "World of Warcraft Classic", "WoWC", product = "wow_classic_era", flavor = "_classic_era_"), + Title("wow_classic_anniversary", "World of Warcraft Classic", "WoWC", product = "wow_anniversary", flavor = "_anniversary_"), + Title("wow_forever", "World of Warcraft: Forever", "WoWF", shop = "world-of-warcraft-forever", product = "wowf", flavor = "_forever_"), Title("diablo3", "Diablo III", "D3", shop = "diablo-iii", product = "d3"), Title("d3cn", "Diablo III", "D3CN", shop = "diablo-iii"), Title("s2", "StarCraft II", "S2", shop = "starcraft-ii", product = "s2"), @@ -58,7 +59,7 @@ object BattleNet { Title("aqua", "Avowed", "AQUA", 2457220, shop = "avowed", product = "aqua"), ) - val STORE: List get() = TITLES.distinctBy { it.code }.filter { it.uid != "d3cn" && it.code != "WoWC" } + val STORE: List<Title> get() = TITLES.distinctBy { it.code }.filter { it.uid != "d3cn" && it.code != "WoWC" && it.code != "WoWF" } fun notInstalled(title: Title, installedIds: Collection<String?>) = installedIds.mapNotNull { id -> id?.let(::title)?.code }.none { it == title.code } @@ -159,24 +160,56 @@ object BattleNet { return if (base.isNotEmpty() && id.substringAfterLast('_') in LOCALES) TITLES.firstOrNull { it.uid == base } else null } + internal fun titleForProduct(product: String): Title? { + val code = product.trim() + if (code.isEmpty()) return null + return TITLES.firstOrNull { it.product.equals(code, ignoreCase = true) } ?: TITLES.firstOrNull { it.code.equals(code, ignoreCase = true) } + } + fun games(context: Context): List<Game> { val pfx = File(prefix(context), "pfx") + val agent = runCatching { productDbInstalls(File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db").readBytes()) }.getOrNull() val entries = installs( runCatching { File(pfx, "system.reg").useLines { uninstallEntries(it) } }.getOrDefault(emptyList()), - runCatching { productDbEntries(File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db").readBytes()) }.getOrNull(), + agent?.map { it.uid to it.path }, ) - val found = LinkedHashMap<String, Game>() + val products = agent.orEmpty().filter { it.product.isNotEmpty() }.associate { it.uid.lowercase() to it.product } + val listed = LinkedHashMap<String, Triple<Title, String, File>>() for ((uid, path) in entries) { - val title = title(uid) + val title = title(uid) ?: products[uid.lowercase()]?.let(::titleForProduct) if (title == null) { if (uid.lowercase() !in NOT_GAMES) Log.i(TAG, "not listed: $uid (no launch code known)") continue } - if (title.code in found) continue + if (title.code in listed) continue val folder = hostFolder(context, path)?.takeIf { it.isDirectory } ?: continue - found[title.code] = Game(title.uid, title.name, title.code, folder, uid) + listed[title.code] = Triple(title, uid, folder) } - return found.values.toList() + val saved = listed.mapValues { (code, _) -> com.droiddeck.launcher.session.SessionPrefs.battleNetFolder(context, code) } + val folders = ownFolders(listed.mapValues { it.value.first to it.value.third }, saved) + return listed.map { (code, found) -> + val folder = folders.getValue(code) + if (saved[code] != folder.path) com.droiddeck.launcher.session.SessionPrefs.setBattleNetFolder(context, code, folder.path) + Game(found.first.uid, found.first.name, code, folder, found.second) + } + } + + internal fun ownFolders(games: Map<String, Pair<Title, File>>, saved: Map<String, String?>): Map<String, File> { + val out = HashMap<String, File>() + val taken = HashSet<File>() + 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 + out[code] = keep + taken.add(keep) + } + for ((code, game) in games) { + if (code in out) continue + val pick = options(game.first, game.second).firstOrNull { it !in taken } ?: game.second + out[code] = pick + taken.add(pick) + } + return games.keys.associateWith { out.getValue(it) } } internal fun installs(registry: List<Pair<String, String>>, agent: List<Pair<String, String>>?): List<Pair<String, String>> { @@ -250,17 +283,22 @@ object BattleNet { return out.toString() } - internal fun productDbEntries(bytes: ByteArray): List<Pair<String, String>>? { + internal class AgentInstall(val uid: String, val path: String, val product: String) + + internal fun productDbEntries(bytes: ByteArray): List<Pair<String, String>>? = productDbInstalls(bytes)?.map { it.uid to it.path } + + internal fun productDbInstalls(bytes: ByteArray): List<AgentInstall>? { val installs = runCatching { fields(bytes, 0, bytes.size) }.getOrNull()?.filter { it.first == 1 }?.takeIf { it.isNotEmpty() } ?: return null return installs.map { (_, start, end) -> runCatching { val install = fields(bytes, start, end) val uid = install.firstOrNull { it.first == 1 }?.let { String(bytes, it.second, it.third - it.second) } + val product = install.firstOrNull { it.first == 2 }?.let { String(bytes, it.second, it.third - it.second) }.orEmpty() val path = install.firstOrNull { it.first == 3 }?.let { s -> fields(bytes, s.second, s.third).firstOrNull { it.first == 1 } } ?.let { String(bytes, it.second, it.third - it.second) } val installed = install.firstOrNull { it.first == 4 }?.let { s -> fields(bytes, s.second, s.third).firstOrNull { it.first == 1 } } ?.let { s -> varints(bytes, s.second, s.third)[1] } - if (uid.isNullOrEmpty() || path.isNullOrEmpty() || installed == 0L) null else uid to path + if (uid.isNullOrEmpty() || path.isNullOrEmpty() || installed == 0L) null else AgentInstall(uid, path, product) }.getOrElse { return null } }.filterNotNull() } diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt index a6bcaed7cb..bbe6ee2a9c 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -841,6 +841,9 @@ object SessionPrefs { fun battleNetNoSteamSet(context: Context): Boolean = prefs(context).getBoolean("battleNetNoSteamSet", false) fun setBattleNetNoSteamSet(context: Context) { prefs(context).edit().putBoolean("battleNetNoSteamSet", true).apply() } + fun battleNetFolder(context: Context, code: String): String? = prefs(context).getString("battleNetFolder:$code", null) + fun setBattleNetFolder(context: Context, code: String, path: String) { prefs(context).edit().putString("battleNetFolder:$code", path).apply() } + fun clearCachedApps(context: Context): Boolean = prefs(context).getBoolean("clearCachedApps", false) fun setClearCachedApps(context: Context, on: Boolean) { prefs(context).edit().putBoolean("clearCachedApps", on).apply() } 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 a2c5e483bb..77800dc1e4 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/BattleNetTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/BattleNetTest.kt @@ -259,6 +259,78 @@ class BattleNetTest { BattleNet.hostFolder(context, "Z:/root/Games/Stores/Battle.net/Hearthstone/")) } + @Test fun wowForeverIsListedBesideRetailInTheSharedWorldOfWarcraftFolder() { + val pfx = File(BattleNet.prefix(context), "pfx").apply { mkdirs() } + val wow = File(pfx, "drive_c/Program Files (x86)/World of Warcraft").apply { mkdirs() } + File(wow, "_retail_").mkdirs() + File(wow, "_forever_").mkdirs() + 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, "wow"), field(2, "wow"), field(3, message(field(1, "C:/Program Files (x86)/World of Warcraft"))))), + field(1, message(field(1, "wow_forever"), field(2, "wowf"), field(3, message(field(1, "C:/Program Files (x86)/World of Warcraft"))))), + )) + val games = BattleNet.games(context) + assertEquals(listOf("WoW", "WoWF"), games.map { it.code }) + assertEquals(listOf(wow, File(wow, "_forever_")), games.map { it.folder }) + assertEquals("World of Warcraft: Forever", games[1].name) + assertEquals(2, games.map { it.folder.canonicalPath }.distinct().size) + val shortcut = BattleNet.shortcut(context, games[1])!! + assertTrue(shortcut.launchOptions.endsWith("--exec=\"launch WoWF\"")) + } + + @Test fun anInstallWithAnUnknownUidIsListedByItsProductCode() { + val pfx = File(BattleNet.prefix(context), "pfx").apply { mkdirs() } + File(pfx, "drive_c/Games/World of Warcraft").mkdirs() + 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_live"), field(2, "wowf"), field(3, message(field(1, "C:/Games/World of Warcraft"))))), + field(1, message(field(1, "unknown_thing"), field(2, "nothing"), field(3, message(field(1, "C:/Games/World of Warcraft"))))), + )) + val games = BattleNet.games(context) + assertEquals(listOf("WoWF"), games.map { it.code }) + assertEquals("wowf_live", games[0].installUid) + assertEquals("wow_forever", games[0].uid) + assertEquals("WoWF", BattleNet.titleForProduct("WOWF")?.code) + assertEquals("WTCG", BattleNet.titleForProduct("wtcg")?.code) + assertNull(BattleNet.titleForProduct("")) + assertEquals(listOf(Triple("wowf_live", "C:/Games/World of Warcraft", "wowf")), + BattleNet.productDbInstalls(message(field(1, message(field(1, "wowf_live"), field(2, "wowf"), field(3, message(field(1, "C:/Games/World of Warcraft")))))))!! + .map { Triple(it.uid, it.path, it.product) }) + } + + @Test fun aGameKeepsTheFolderItWasListedWithWhenAnotherVersionComesOrGoes() { + val root = File(context.filesDir, "World of Warcraft").apply { mkdirs() } + val retailDir = File(root, "_retail_").apply { mkdirs() } + val foreverDir = File(root, "_forever_").apply { mkdirs() } + val wow = BattleNet.title("wow")!! + val forever = BattleNet.title("wow_forever")!! + val classic = BattleNet.title("wow_classic")!! + assertEquals(mapOf("WoWF" to root), BattleNet.ownFolders(mapOf("WoWF" to (forever to root)), mapOf("WoWF" to null))) + assertEquals(mapOf("WoW" to root, "WoWF" to foreverDir), + BattleNet.ownFolders(linkedMapOf("WoW" to (wow to root), "WoWF" to (forever to root)), mapOf("WoW" to null, "WoWF" to null))) + assertEquals(mapOf("WoWF" to foreverDir), BattleNet.ownFolders(mapOf("WoWF" to (forever to root)), mapOf("WoWF" to foreverDir.path))) + assertEquals(mapOf("WoW" to retailDir, "WoWC" to root), + BattleNet.ownFolders(linkedMapOf("WoW" to (wow to root), "WoWC" to (classic to root)), mapOf("WoW" to null, "WoWC" to root.path))) + assertEquals(mapOf("WoW" to root, "WoWC" to root), + BattleNet.ownFolders(linkedMapOf("WoW" to (wow to root), "WoWC" to (classic to root)), mapOf("WoW" to null, "WoWC" to null))) + assertEquals(mapOf("WoWF" to root), BattleNet.ownFolders(mapOf("WoWF" to (forever to root)), mapOf("WoWF" to "/elsewhere"))) + } + + @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() } + File(wow, "_forever_").mkdirs() + val db = File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db").apply { parentFile!!.mkdirs() } + val retail = field(1, message(field(1, "wow"), field(2, "wow"), field(3, message(field(1, "C:/Program Files (x86)/World of Warcraft"))))) + val forever = field(1, message(field(1, "wow_forever"), field(2, "wowf"), field(3, message(field(1, "C:/Program Files (x86)/World of Warcraft"))))) + db.writeBytes(message(retail, forever)) + assertEquals(listOf(wow, File(wow, "_forever_")), BattleNet.games(context).map { it.folder }) + db.writeBytes(message(forever)) + assertEquals(listOf(File(wow, "_forever_")), BattleNet.games(context).map { it.folder }) + db.writeBytes(message(forever, retail)) + assertEquals(listOf(File(wow, "_forever_"), wow), BattleNet.games(context).map { it.folder }) + } + @Test fun aGamesShortcutHasBattleNetLaunchItInItsPrefix() { val folder = File(BattleNet.prefix(context), "pfx/drive_c/Program Files (x86)/Diablo IV").apply { mkdirs() } File(folder, "Diablo IV Launcher.exe").writeBytes(byteArrayOf(0x4d, 0x5a)) @@ -291,6 +363,7 @@ class BattleNetTest { assertEquals(codes.distinct(), codes) assertTrue("Fen" in codes && "OSI" in codes && "WTCG" in codes) assertFalse(BattleNet.STORE.any { it.uid == "d3cn" || it.uid == "fen" }) + assertFalse(BattleNet.STORE.any { it.code == "WoWC" || it.code == "WoWF" }) assertEquals("fenris", BattleNet.STORE.first { it.code == "Fen" }.uid) assertTrue(BattleNet.STORE.all { it.uid.matches(Regex("[a-z0-9_]{1,32}")) }) val installed = listOf("fenris", "diablo3_enus", null)