mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
Merge remote-tracking branch 'origin/main' into review/malibridge-1950
This commit is contained in:
commit
f05e0df39e
135 files changed
+5415
-979
No files matched your search
@@ -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<Title> 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()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.droiddeck.launcher.runtime
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.droiddeck.launcher.core.Downloader
|
||||
import com.droiddeck.launcher.core.FileUtils
|
||||
import com.droiddeck.launcher.core.Hashes
|
||||
import com.droiddeck.launcher.session.DesktopLocale
|
||||
import java.io.File
|
||||
import java.util.Locale
|
||||
|
||||
object DesktopLanguage {
|
||||
private const val TAG = "DesktopLanguage"
|
||||
private const val INDEX = "desktop-kde.i18n.txt"
|
||||
private const val ABSENT = "absent"
|
||||
|
||||
class Pack(val locale: String, val sha256: String, val size: Long)
|
||||
|
||||
internal fun parseIndex(text: String): Map<String, Pack> = text.lineSequence().mapNotNull { line ->
|
||||
val parts = line.trim().split(' ')
|
||||
if (parts.size != 3 || !parts[0].matches(Regex("[A-Za-z_]+")) || !parts[1].matches(Regex("[0-9a-fA-F]{64}"))) return@mapNotNull null
|
||||
Pack(parts[0], parts[1], parts[2].toLongOrNull() ?: return@mapNotNull null)
|
||||
}.associateBy { it.locale }
|
||||
|
||||
/** KDE ships region packs (pt_BR, zh_TW) beside bare-language ones (pt, es). */
|
||||
internal fun candidates(locale: Locale): List<String> {
|
||||
val region = DesktopLocale.lang(locale).removeSuffix(".UTF-8")
|
||||
if (region == DesktopLocale.DEFAULT.removeSuffix(".UTF-8") || region.startsWith("en")) return emptyList()
|
||||
return listOfNotNull(region, "zh_TW".takeIf { region == "zh_HK" }, region.substringBefore('_')).distinct()
|
||||
}
|
||||
|
||||
internal fun baseUrl(desktopUrl: String) = desktopUrl.substringBeforeLast('/') + "/"
|
||||
|
||||
private fun marker(root: File, tag: String, name: String) = File(root, ".droiddeck-i18n-$tag-$name")
|
||||
|
||||
/** Installs the desktop's translations for [locale]; false when a download failed and is worth retrying. */
|
||||
@Synchronized
|
||||
fun ensure(context: Context, locale: Locale): Boolean {
|
||||
val root = LinuxRuntime.rootDir(context)
|
||||
if (!DesktopCatalog.desktopInstalled(root)) return true
|
||||
val entry = DesktopCatalog.desktopEntry(context)
|
||||
val pending = candidates(locale).filterNot { marker(root, entry.version, it).isFile }
|
||||
if (pending.isEmpty()) return true
|
||||
val base = baseUrl(entry.url)
|
||||
val index = parseIndex(Downloader.downloadString(base + INDEX) ?: return false)
|
||||
var complete = true
|
||||
for (name in pending) {
|
||||
val pack = index[name]
|
||||
if (pack == null) {
|
||||
FileUtils.writeString(marker(root, entry.version, name), ABSENT)
|
||||
continue
|
||||
}
|
||||
val download = File(context.cacheDir, "i18n-$name-${pack.sha256.take(12)}.download")
|
||||
try {
|
||||
val ok = Downloader.downloadFile(
|
||||
base + "desktop-kde-i18n-$name.tar.zst", download, true, pack.sha256, java.util.function.Consumer<Float> { },
|
||||
) && pack.sha256.equals(Hashes.sha256(download), ignoreCase = true) &&
|
||||
LinuxRuntimeInstaller.extract(context, download, root, null)
|
||||
if (ok) FileUtils.writeString(marker(root, entry.version, name), pack.sha256) else complete = false
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "language pack $name: $e")
|
||||
complete = false
|
||||
} finally {
|
||||
download.delete()
|
||||
}
|
||||
}
|
||||
return complete
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user