fix: Auto components update only DroidDeck-Proton and delete old builds (#2193)

This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-06 20:43:44 -05:00
1 parent 0b4bc827e7
commit 18c7d56409
39 files changed
+344 -83

No files matched your search

@@ -2166,9 +2166,11 @@ class FrontEndActivity : ComponentActivity() {
private fun importComponent(uri: Uri) {
val name = displayNameOf(uri) ?: "imported.wcp"
components.componentAction(getString(R.string.comp_busy_importing)) {
val tmp = File(cacheDir, "component-import.wcp")
contentResolver.openInputStream(uri)?.use { input -> tmp.outputStream().use { input.copyTo(it) } } ?: error(getString(R.string.comp_cannot_read))
try { getString(R.string.comp_imported, ComponentsManager.importPackage(this, tmp, name).version) } finally { tmp.delete() }
val tmp = File.createTempFile("component-import", ".wcp", cacheDir)
try {
contentResolver.openInputStream(uri)?.use { input -> tmp.outputStream().use { input.copyTo(it) } } ?: error(getString(R.string.comp_cannot_read))
getString(R.string.comp_imported, ComponentUpdater.importPackage(this, tmp, name).version)
} finally { tmp.delete() }
}
}
@@ -15,6 +15,7 @@ object ComponentUpdater {
private const val BOOTED_KEPT = 200
private const val HANDED_BACK = "handedBack"
internal const val HAND_BACK_FILES = "handBackFiles"
private const val IMPORTED = "imported"
private val lock = Any()
private val io = Any()
@@ -40,8 +41,10 @@ object ComponentUpdater {
}
}
private fun Map<String, Map<String, ComponentsManager.Choice>>.fileOf(p: ComponentsManager.Proton, comp: String): String? =
this[p.id]?.get(comp)?.takeIf { it.protonVersion == p.version }?.file
private fun Map<String, Map<String, ComponentsManager.Choice>>.choiceOf(p: ComponentsManager.Proton, comp: String): ComponentsManager.Choice? =
this[p.id]?.get(comp)?.takeIf { it.protonVersion == p.version }
private fun Map<String, Map<String, ComponentsManager.Choice>>.fileOf(p: ComponentsManager.Proton, comp: String): String? = choiceOf(p, comp)?.file
private class Target(val comp: String, val p: ComponentsManager.Proton, val line: ComponentUpdates.Line, val ls: JSONObject) {
val key: String get() = line.key
@@ -63,7 +66,7 @@ object ComponentUpdater {
val choices = ComponentsManager.activeChoices(context).filterKeys { it in own }.values.flatMap { it.entries }
for (comp in ComponentsManager.COMPONENTS) {
if (SessionPrefs.componentModeChosen(context, comp)) continue
val manual = choices.any { (c, choice) -> c == comp && !choice.auto && choice.file !in owned && !choice.file.startsWith("original ") }
val manual = choices.any { (c, choice) -> c == comp && ComponentUpdates.manual(choice, null, owned) }
SessionPrefs.setComponentMode(context, comp, if (manual) ComponentUpdates.MANUAL else ComponentUpdates.AUTO)
}
}
@@ -158,7 +161,7 @@ object ComponentUpdater {
complete = false
continue
}
ls.put("owned", JSONArray(ls.strings("owned") + current))
own(state, ls, current)
}
val target = ComponentUpdates.pick(catalog, line, ls.strings("bad"))
if (target == null || current == target.file) continue
@@ -186,7 +189,7 @@ object ComponentUpdater {
}
ls.put("previous", if (missing && relisted == null) ComponentUpdates.ORIGINAL else current.ifEmpty { ComponentUpdates.ORIGINAL })
.put("current", target.file).put("since", latestStamp(context)).put("failed", JSONObject())
if (!stored) ls.put("owned", JSONArray(ls.strings("owned") + target.file))
if (!stored) own(state, ls, target.file)
ls.remove("note")
notes += context.getString(R.string.comp_auto_updated, ComponentsManager.LABEL[comp], target.file.removeSuffix(".wcp"))
}
@@ -195,10 +198,11 @@ object ComponentUpdater {
private fun apply(context: Context, state: JSONObject, protons: List<ComponentsManager.Proton>, wanted: List<String>, notes: MutableList<String>) {
val lines = state.obj("lines")
val auto = placed(state)
val choices = ComponentsManager.activeChoices(context)
val targets = wanted.flatMap { comp ->
protons.mapNotNull { p -> lineOf(context, state, p, comp)?.let { line -> lines.optJSONObject(line.key)?.let { Target(comp, p, line, it) } } }
}
val choices = ComponentsManager.activeChoices(context)
}.filterNot { t -> ComponentUpdates.manual(choices.choiceOf(t.p, t.comp), t.ls.optJSONObject("applied")?.optString(t.p.id), auto, t.ls.optString("current")) }
for (t in targets) {
val from = t.ls.optJSONObject("restore")?.optString(t.p.id).orEmpty()
val current = t.ls.optString("current")
@@ -208,7 +212,8 @@ object ComponentUpdater {
} else if (current.isNotEmpty() && choices.fileOf(t.p, t.comp) != current) {
if (ComponentUpdates.needsArm64x(t.line, listOf(ComponentsManager.arm64x(context, t.p, t.comp))) &&
!ComponentsManager.packageArm64x(context, current, t.comp)) continue
runCatching { ComponentsManager.swap(context, t.p.id, current) }.onFailure { Log.w(TAG, "${t.p.name} ${t.key}", it) }
runCatching { ComponentsManager.swap(context, t.p.id, current) }
.onFailure { Log.w(TAG, "${t.p.name} ${t.key}", it) }
}
}
val after = ComponentsManager.activeChoices(context)
@@ -218,9 +223,13 @@ object ComponentUpdater {
val from = restore?.optString(t.p.id).orEmpty()
val current = t.ls.optString("current")
if (from.isNotEmpty()) {
if (after.fileOf(t.p, t.comp) == from) unfinished += t.key else restore?.remove(t.p.id)
} else if (current.isNotEmpty() && after.fileOf(t.p, t.comp) != current) {
unfinished += t.key
if (after.fileOf(t.p, t.comp) == from) unfinished += t.key
else {
restore?.remove(t.p.id)
if (after.choiceOf(t.p, t.comp)?.file?.startsWith("original ") != false) t.ls.optJSONObject("applied")?.remove(t.p.id)
}
} else if (current.isNotEmpty()) {
if (after.fileOf(t.p, t.comp) == current) t.ls.obj("applied").put(t.p.id, current) else unfinished += t.key
}
}
for (key in lines.keys().asSequence().toList()) {
@@ -233,13 +242,32 @@ object ComponentUpdater {
}
}
private fun own(state: JSONObject, ls: JSONObject, file: String) {
ls.put("owned", JSONArray(ls.strings("owned") + file))
state.put(IMPORTED, JSONArray(state.strings(IMPORTED) - file))
}
fun importPackage(context: Context, source: File, name: String): ComponentsManager.Package = synchronized(lock) {
ComponentsManager.importPackage(context, source, name).also { disown(context, it.file) }
}
fun disown(context: Context, file: String) = synchronized(lock) {
val state = load(context)
state.optJSONObject("lines")?.let { lines ->
lines.keys().asSequence().mapNotNull { lines.optJSONObject(it) }.filter { file in it.strings("owned") }
.forEach { it.put("owned", JSONArray(it.strings("owned") - file)) }
}
state.put(IMPORTED, JSONArray(state.strings(IMPORTED) + file))
save(context, state)
}
internal fun placed(state: JSONObject): Set<String> {
val remembered = state.strings(HAND_BACK_FILES)
val lines = state.optJSONObject("lines") ?: return remembered
val lines = state.optJSONObject("lines") ?: return remembered - state.strings(IMPORTED)
return remembered + lines.keys().asSequence().flatMap { k ->
val ls = lines.optJSONObject(k) ?: return@flatMap emptySequence()
(ls.strings("owned") + ls.strings("bad") + ls.optString("current") + ls.optString("previous")).asSequence()
}.filter { it.isNotEmpty() }.toSet()
}.filter { it.isNotEmpty() }.toSet() - state.strings(IMPORTED)
}
private fun handBack(context: Context, state: JSONObject, protons: List<ComponentsManager.Proton>) {
@@ -303,9 +331,13 @@ object ComponentUpdater {
private fun rollBack(context: Context, state: JSONObject, protons: List<ComponentsManager.Proton>, comp: String, key: String, notes: MutableList<String>) {
val ls = state.obj("lines").obj(key)
val bad = ls.optString("current")
val back = ls.optString("previous")
val onLine = protons.filter { lineOf(context, state, it, comp)?.key == key }
val line = ComponentUpdates.LINES.first { it.key == key }
val back = ls.optString("previous").takeIf { prev -> prev == ComponentUpdates.ORIGINAL || (ComponentsManager.isStored(context, prev) &&
(!ComponentUpdates.needsArm64x(line, onLine.map { ComponentsManager.arm64x(context, it, comp) }) || ComponentsManager.packageArm64x(context, prev, comp))) }
?: ComponentUpdates.ORIGINAL
val choices = ComponentsManager.activeChoices(context)
val targets = protons.filter { lineOf(context, state, it, comp)?.key == key && choices.fileOf(it, comp) == bad }
val targets = onLine.filter { choices.fileOf(it, comp) == bad }
for (p in targets) {
runCatching {
if (back == ComponentUpdates.ORIGINAL) ComponentsManager.restore(context, p.id, comp, ComponentsManager.safeName(p.version))
@@ -314,6 +346,10 @@ object ComponentUpdater {
}
val after = ComponentsManager.activeChoices(context)
val unfinished = targets.filter { after.fileOf(it, comp) == bad }
for (p in targets - unfinished.toSet()) {
if (back == ComponentUpdates.ORIGINAL) ls.optJSONObject("applied")?.remove(p.id)
else if (after.fileOf(p, comp) == back) ls.obj("applied").put(p.id, back)
}
ls.put("bad", JSONArray(ls.strings("bad") + bad)).put("current", back).put("previous", ComponentUpdates.ORIGINAL)
.put("since", latestStamp(context)).put("failed", JSONObject())
if (back == ComponentUpdates.ORIGINAL) ls.obj("restore").let { restore -> unfinished.forEach { restore.put(it.id, bad) } }
@@ -338,10 +374,13 @@ object ComponentUpdater {
val ls = lines.optJSONObject(k) ?: return@flatMap emptySequence()
ComponentUpdates.keep(ls.optString("current"), ls.optString("previous"), selected).asSequence()
}.toSet()
val stored = ComponentsManager.storedFiles(context)
val followed = ComponentUpdates.LINES.filter { managed(context, it.comp) }
for (k in lines.keys().asSequence().toList()) {
val ls = lines.optJSONObject(k) ?: continue
val owned = ls.strings("owned")
val gone = owned.filter { it !in keep && runCatching { ComponentsManager.deletePackage(context, it) }.isSuccess }
val stale = ComponentUpdates.stale(followed.firstOrNull { it.key == k }, owned, stored, keep, ls.optString("current"), ls.strings("bad").toSet())
val gone = stale.filter { runCatching { ComponentsManager.deleteUnused(context, it) }.getOrDefault(false) }
ls.put("owned", JSONArray(owned - gone.toSet()))
}
}
@@ -89,6 +89,11 @@ object ComponentUpdates {
b.getInt(header) == 0x4550 && (b.getShort(header + 4).toInt() and 0xffff) == 0xaa64 && arm64ec(pe) == true
}.getOrDefault(false)
fun allArm64x(dlls: Map<String, ByteArray>, owned: Set<String>): Boolean {
val graphics = dlls.filterKeys { it.lowercase() in owned }
return graphics.isNotEmpty() && graphics.values.all(::arm64x)
}
fun needsArm64x(line: Line, stocks: List<Boolean?>): Boolean =
line.key.endsWith("-arm64ec") && stocks.any { it != false }
@@ -148,4 +153,15 @@ object ComponentUpdates {
fun keep(current: String, previous: String, selected: Set<String>): Set<String> =
(setOf(current, previous) + selected) - ORIGINAL
fun manual(choice: ComponentsManager.Choice?, applied: String?, auto: Set<String>, current: String? = null): Boolean =
choice != null && !choice.auto && !choice.file.startsWith("original ") && choice.file != current &&
(if (applied.isNullOrEmpty()) choice.file !in auto else choice.file != applied)
fun stale(line: Line?, owned: Set<String>, stored: List<String>, keep: Set<String>, current: String, bad: Set<String> = emptySet()): List<String> {
val ranked = line?.takeIf { belongs(current, it) }
fun newer(file: String) = ranked != null && belongs(file, ranked) && ProtonExtras.compareVersions(file, current) > 0
return (owned + stored.filter { ranked != null && belongs(it, ranked) })
.filter { it !in keep && ((it in bad && it in owned) || !newer(it)) }.distinct()
}
}
@@ -782,7 +782,11 @@ object ComponentsManager {
internal fun liveSelection(key: String, entry: JSONObject, proton: Proton?): Boolean =
proton != null && (key == "queued" || entry.optString("protonVersion") == proton.version)
private fun packagesInUse(state: JSONObject, installed: Map<String, Proton>): Set<String> {
internal fun liveOnAny(key: String, entry: JSONObject, installs: List<Proton>?): Boolean =
installs?.any { liveSelection(key, entry, it) } == true
internal fun packagesInUse(state: JSONObject, installed: List<Proton>): Set<String> {
val byId = installed.groupBy { it.id }
val names = HashSet<String>()
for (key in listOf("active", "queued")) {
val byProton = state.optJSONObject(key) ?: continue
@@ -790,7 +794,7 @@ object ComponentsManager {
val comps = byProton.optJSONObject(pid) ?: continue
for (comp in comps.keys()) {
val entry = comps.optJSONObject(comp) ?: continue
if (liveSelection(key, entry, installed[pid])) entry.optString("file").takeIf { it.isNotEmpty() }?.let(names::add)
if (liveOnAny(key, entry, byId[pid])) entry.optString("file").takeIf { it.isNotEmpty() }?.let(names::add)
}
}
}
@@ -812,7 +816,7 @@ object ComponentsManager {
fun stored(context: Context): Stored = synchronized(lock) {
val state = loadState(context)
val installed = protons(context).distinctBy { it.id }.associateBy { it.id }
val used = packagesInUse(state, installed)
val used = packagesInUse(state, protons(context))
val originals = originalsDir(context).listFiles()?.filter { it.isDirectory }?.flatMap { proton ->
proton.listFiles()?.filter { it.isDirectory }?.flatMap { version ->
COMPONENTS.mapNotNull { comp ->
@@ -839,15 +843,15 @@ object ComponentsManager {
}
private fun checkPackageUnused(context: Context, state: JSONObject, name: String) {
val installed = protons(context).distinctBy { it.id }.associateBy { it.id }
val installed = protons(context).groupBy { it.id }
for (key in listOf("queued", "active")) {
val byProton = state.sub(key)
for (pid in byProton.keys()) {
val comps = byProton.optJSONObject(pid) ?: continue
for (comp in comps.keys()) {
val entry = comps.optJSONObject(comp) ?: continue
check(entry.optString("file") != name || !liveSelection(key, entry, installed[pid])) {
context.getString(R.string.cmgr_package_in_use, name, installed[pid]?.name ?: pid)
check(entry.optString("file") != name || !liveOnAny(key, entry, installed[pid])) {
context.getString(R.string.cmgr_package_in_use, name, installed[pid]?.firstOrNull()?.name ?: pid)
}
}
}
@@ -937,7 +941,7 @@ object ComponentsManager {
fun deletePackage(context: Context, file: String): String = synchronized(lock) {
val name = safeName(file)
val state = loadState(context)
val installed = protons(context).distinctBy { it.id }.associateBy { it.id }
val installed = protons(context).groupBy { it.id }
var changed = false
for (key in listOf("queued", "active")) {
val byProton = state.sub(key)
@@ -946,7 +950,7 @@ object ComponentsManager {
for (comp in comps.keys().asSequence().toList()) {
val entry = comps.optJSONObject(comp) ?: continue
if (entry.optString("file") != name) continue
check(!liveSelection(key, entry, installed[pid])) { context.getString(R.string.cmgr_package_in_use, name, installed[pid]?.name ?: pid) }
check(!liveOnAny(key, entry, installed[pid])) { context.getString(R.string.cmgr_package_in_use, name, installed[pid]?.firstOrNull()?.name ?: pid) }
comps.remove(comp)
changed = true
}
@@ -1076,9 +1080,20 @@ object ComponentsManager {
fun arm64x(context: Context, p: Proton, comp: String): Boolean? = stockProbe(context, p, comp)?.let(ComponentUpdates::arm64x)
fun packageArm64x(context: Context, file: String, comp: String): Boolean {
val rel = "${COMP_DIR[comp] ?: return false}/aarch64-windows/${ARM64EC_PROBE.getValue(comp)}"
val dir = "${COMP_DIR[comp] ?: return false}/aarch64-windows/"
val f = File(packagesDir(context), safeName(file)).takeIf { it.isFile } ?: return false
return runCatching { readWcpEntry(f, rel) }.getOrNull()?.let(ComponentUpdates::arm64x) == true
val dlls = runCatching {
readWcp(f) { tar ->
val found = HashMap<String, ByteArray>()
while (true) {
val e = tar.nextTarEntry ?: break
val rel = normalize(e.name)
if (!e.isDirectory && rel.startsWith(dir) && '/' !in rel.removePrefix(dir)) found[rel.removePrefix(dir)] = tar.readBytes()
}
found
}
}.getOrNull() ?: return false
return ComponentUpdates.allArm64x(dlls, COMP_DLLS.getValue(comp))
}
private fun readWcpEntry(f: File, rel: String): ByteArray? = readWcp(f) { tar ->
@@ -1122,23 +1137,20 @@ object ComponentsManager {
}
fun selectedPackages(context: Context): Set<String> = synchronized(lock) {
val state = loadState(context)
buildSet {
for (key in listOf("active", "queued")) {
val byProton = state.optJSONObject(key) ?: continue
for (pid in byProton.keys()) {
val comps = byProton.optJSONObject(pid) ?: continue
for (c in comps.keys()) comps.optJSONObject(c)?.optString("file")?.takeIf(String::isNotEmpty)?.let(::add)
}
}
val profiles = state.optJSONObject("profiles")
profiles?.keys()?.forEach { scope ->
val profile = profiles.optJSONObject(scope) ?: return@forEach
for (c in COMPONENTS) profile.optString(c).takeIf(String::isNotEmpty)?.let(::add)
}
}
packagesInUse(loadState(context), protons(context))
}
fun deleteUnused(context: Context, file: String): Boolean = synchronized(lock) {
val name = safeName(file)
val f = File(packagesDir(context), name)
if (!f.isFile || name in packagesInUse(loadState(context), protons(context))) return false
deletePackage(context, name)
true
}
fun storedFiles(context: Context): List<String> =
packagesDir(context).listFiles()?.filter { it.isFile && it.name.endsWith(".wcp") }?.map { it.name } ?: emptyList()
fun isStored(context: Context, file: String): Boolean = File(packagesDir(context), safeName(file)).isFile
internal fun forcedPackageWanted(activeFile: String?, protonVersionMatches: Boolean): Boolean =