mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 04:55:44 +00:00
fix(battlenet): validate folder registrations before launch and deletion
This commit is contained in:
1 parent
9ddb52c5fc
commit
39d8149a16
6 files changed
+76
-12
No files matched your search
@@ -223,10 +223,8 @@ object BattleNet {
|
||||
*/
|
||||
fun launchCode(context: Context, uid: String, folder: File?): String? {
|
||||
if (folder == null) return runCatching { games(context) }.getOrNull()?.let { launchCode(it, uid, null) }
|
||||
// games() keeps one copy per launch code; the registrations of this folder are authoritative.
|
||||
runCatching { registrationsAt(context, uid, folder) }.getOrNull()?.firstOrNull()
|
||||
?.let { registered -> title(registered)?.code?.let { return it } }
|
||||
return runCatching { games(context) }.getOrNull()?.let { launchCode(it, uid, folder) }
|
||||
if (!folder.isDirectory) return null
|
||||
return runCatching { installedRegistrationAt(context, uid, folder) }.getOrNull()?.second?.code
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -245,7 +243,7 @@ object BattleNet {
|
||||
val pfx = File(prefix(context), "pfx")
|
||||
val agent = File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db")
|
||||
val registry = File(pfx, "system.reg")
|
||||
if (agent.isFile && runCatching { agent.readBytes() }.isFailure) return false
|
||||
if (agent.exists() && runCatching { productDbRegistrations(agent.readBytes()) }.getOrNull() == null) return false
|
||||
if (registry.isFile && runCatching { registry.useLines { uninstallEntries(it) } }.isFailure) return false
|
||||
return true
|
||||
}
|
||||
@@ -257,6 +255,44 @@ object BattleNet {
|
||||
return same.singleOrNull()?.code ?: title(uid)?.code
|
||||
}
|
||||
|
||||
internal fun installedRegistrationAt(context: Context, id: String, folder: File): Pair<String, Title>? {
|
||||
if (!folder.isDirectory) return null
|
||||
val pfx = File(prefix(context), "pfx")
|
||||
val agentFile = File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db")
|
||||
val registryFile = File(pfx, "system.reg")
|
||||
val agent = if (agentFile.exists()) {
|
||||
val bytes = agentFile.readBytes()
|
||||
if (productDbRegistrations(bytes) == null) return null
|
||||
productDbInstalls(bytes).orEmpty()
|
||||
} else null
|
||||
val registry = if (registryFile.exists()) registryFile.useLines { uninstallEntries(it) } else emptyList()
|
||||
val candidates = if (agent == null) registry.map { AgentInstall(it.first, it.second, "") }
|
||||
else agent
|
||||
return candidates.firstNotNullOfOrNull { row ->
|
||||
val found = title(row.uid) ?: titleForProduct(row.product) ?: return@firstNotNullOfOrNull null
|
||||
if (!sameTitle(found.uid, id)) return@firstNotNullOfOrNull null
|
||||
val registered = hostFolder(context, row.path) ?: return@firstNotNullOfOrNull null
|
||||
if (registered.absolutePath != folder.absolutePath &&
|
||||
(found.flavor != folder.name || registered.absolutePath != folder.parentFile?.absolutePath)
|
||||
) return@firstNotNullOfOrNull null
|
||||
row.uid to found
|
||||
}
|
||||
}
|
||||
|
||||
/** A registration for any title in this tree makes recursive deletion unsafe. */
|
||||
internal fun folderHasRegistrations(context: Context, folder: File): Boolean? = runCatching {
|
||||
val pfx = File(prefix(context), "pfx")
|
||||
val agentFile = File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db")
|
||||
val registryFile = File(pfx, "system.reg")
|
||||
val agent = if (agentFile.exists()) productDbRegistrations(agentFile.readBytes()) ?: return null else emptyList()
|
||||
val registry = if (registryFile.exists()) registryFile.useLines { uninstallEntries(it) } else emptyList()
|
||||
val target = folder.canonicalFile.toPath()
|
||||
(agent.map { it.path } + registry.map { it.second }).any { path ->
|
||||
val registered = hostFolder(context, path)?.canonicalFile?.toPath() ?: return null
|
||||
registered.startsWith(target)
|
||||
}
|
||||
}.getOrNull()
|
||||
|
||||
internal fun registrationsAt(context: Context, id: String, folder: File): List<String>? {
|
||||
val pfx = File(prefix(context), "pfx")
|
||||
val agentFile = File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db")
|
||||
|
||||
@@ -351,7 +351,7 @@ object StoresState {
|
||||
if (game.sidecar.store == Store.BATTLENET) {
|
||||
val listed = com.droiddeck.launcher.runtime.BattleNet.games(app).any { it.folder.absolutePath == game.folder.absolutePath }
|
||||
// A registration we could not read is not the same as no registration: keep the folder.
|
||||
if (listed || !com.droiddeck.launcher.runtime.BattleNet.registrationsReadable(app)) {
|
||||
if (listed || com.droiddeck.launcher.runtime.BattleNet.folderHasRegistrations(app, game.folder) != false) {
|
||||
main.post { uninstalling.remove(key); onDone() }
|
||||
return
|
||||
}
|
||||
|
||||
@@ -155,7 +155,7 @@ object BattleNetInstall {
|
||||
|
||||
/** The registration of this folder: games() keeps one copy per launch code, so a second copy is not in it. */
|
||||
internal fun installUid(context: Context, game: InstalledStoreGame): String? =
|
||||
runCatching { BattleNet.registrationsAt(context, game.sidecar.id, game.folder) }.getOrNull()?.firstOrNull()
|
||||
runCatching { BattleNet.installedRegistrationAt(context, game.sidecar.id, game.folder) }.getOrNull()?.first
|
||||
|
||||
fun uninstall(context: Context, game: InstalledStoreGame): Boolean {
|
||||
if (product(game.sidecar.id) == null || !BattleNet.installed(context)) return false
|
||||
|
||||
@@ -345,10 +345,10 @@ object DownloadQueue {
|
||||
item = items[key] ?: return
|
||||
if (item.entry.state == DownloadState.RUNNING || item.entry.state == DownloadState.QUEUED) return
|
||||
if (item.entry.state == DownloadState.FAILED || item.entry.state == DownloadState.PAUSED) {
|
||||
cleanup = beginCleanup(key)
|
||||
if (cleanup) {
|
||||
val candidate = item.entry.cleanupKey?.takeIf { item.factory != null }
|
||||
if (candidate != null && cancelling.add(candidate)) cleanupKey = candidate
|
||||
val candidate = item.entry.cleanupKey?.takeIf { item.factory != null }
|
||||
if (candidate == null || cancelling.add(candidate)) {
|
||||
cleanup = beginCleanup(key)
|
||||
if (cleanup) cleanupKey = candidate else cleanupFinished(candidate)
|
||||
}
|
||||
}
|
||||
items.remove(key)
|
||||
|
||||
@@ -472,6 +472,34 @@ class BattleNetTest {
|
||||
assertEquals("Fen", BattleNet.launchCode(context, "fen", folder))
|
||||
}
|
||||
|
||||
@Test fun aMissingInstallFolderCannotLaunchEvenWhenRegistered() {
|
||||
val pfx = File(BattleNet.prefix(context), "pfx")
|
||||
val db = File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db").apply { parentFile!!.mkdirs() }
|
||||
val folder = File(pfx, "drive_c/Games/D3")
|
||||
db.writeBytes(message(field(1, message(field(1, "d3cn"), field(3, message(field(1, "C:/Games/D3")))))))
|
||||
assertNull(BattleNet.launchCode(context, "diablo3", folder))
|
||||
assertEquals(true, BattleNet.folderHasRegistrations(context, folder))
|
||||
}
|
||||
|
||||
@Test fun anUnreadableAgentDatabaseDoesNotAuthorizeFolderDeletion() {
|
||||
val pfx = File(BattleNet.prefix(context), "pfx")
|
||||
val db = File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db").apply { parentFile!!.mkdirs() }
|
||||
val folder = File(pfx, "drive_c/Games/D3").apply { mkdirs() }
|
||||
db.writeBytes(byteArrayOf(0x0a, 0x7f))
|
||||
assertFalse(BattleNet.registrationsReadable(context))
|
||||
assertNull(BattleNet.folderHasRegistrations(context, folder))
|
||||
}
|
||||
|
||||
@Test fun productFallbackResolvesBothLaunchCodeAndManagementUid() {
|
||||
val pfx = File(BattleNet.prefix(context), "pfx")
|
||||
val db = File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db").apply { parentFile!!.mkdirs() }
|
||||
val folder = File(pfx, "drive_c/Games/WoW").apply { mkdirs() }
|
||||
db.writeBytes(message(field(1, message(field(1, "wowf_live"), field(2, "wowf"), field(3, message(field(1, "C:/Games/WoW")))))))
|
||||
assertEquals("WoWF", BattleNet.launchCode(context, "wow_forever", folder))
|
||||
val sidecar = StoreGameSidecar(Store.BATTLENET, "wow_forever", "World of Warcraft", "")
|
||||
assertEquals("wowf_live", BattleNetInstall.installUid(context, InstalledStoreGame(sidecar, folder)))
|
||||
}
|
||||
|
||||
@Test fun aRegistrationForAMissingFolderStillCountsAsPreexisting() {
|
||||
val pfx = File(BattleNet.prefix(context), "pfx").apply { mkdirs() }
|
||||
File(pfx, "drive_c/ProgramData/Battle.net/Agent").mkdirs()
|
||||
|
||||
@@ -166,7 +166,7 @@ class BattleNetInstallTest {
|
||||
assertEquals(2, scheduled.size)
|
||||
assertEquals(60_000L, scheduled[1].second)
|
||||
|
||||
var next = 2
|
||||
var next = 1
|
||||
repeat(4) {
|
||||
if (next < scheduled.size) {
|
||||
scheduled[next].first()
|
||||
|
||||
Reference in new issue
Block a user