feat(proton): list prerelease builds under Experimental and install from any archive (#2889)

* feat(proton): install prerelease builds

* fix(proton): harden prerelease archive installs

* refactor(proton): drop the superseded test build path

* fix(proton): list only current prerelease builds and label the file install

* fix(proton): hide prerelease rows that are already installed

* fix(proton): keep relative links and modes when unpacking zip and 7z builds

* fix(proton): name file installs without the archive suffix

* fix(proton): confirm a release is gone before removing its build and tighten archive handling

* Show the Experimental section without Developer tools

* Share the refresh guard, recheck sessions before removal, skip other architectures

The update check guard is shared between menu instances and held until the result is published, so a recreated screen joins the running check and results arrive in order. Automatic removal rechecks for a running session after each release lookup. Experimental assets for other CPU architectures are ignored, and in-tree links may start with the current directory.

* Keep an experimental build in place when a session starts during its removal

Removal and session start now share one lock. The tree is renamed out of the
tools folder under that lock only while no session is running, and deleted
afterwards, so a session that starts first keeps its build.
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-19 13:41:57 -05:00
1 parent 71989b4cdb
commit de73e429cc
45 files changed
+897 -392

No files matched your search

@@ -99,6 +99,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
this, ui,
idle = { !busy && flood == null && compatLaunch == null && pkgStage == null && components.compBusy == null },
onInstalled = { if (!OfflineMode.enabled(this)) components.checkComponentUpdates(force = false) },
onRemoved = { components.refreshComponents() },
)
private var installed by mutableStateOf<String?>(null)
@@ -2433,17 +2434,18 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
protons = com.droiddeck.launcher.ui.ProtonsState(
rows = protons.protonRows, busyId = protons.protonBusyId, stage = protons.protonStage, percent = protons.protonPercent,
runtimeReady = ready && !busy, sessionRunning = SessionState.running, experimental = protons.experimentalDirs,
experimentalReleases = protons.experimentalReleases,
),
protonActions = com.droiddeck.launcher.ui.ProtonsActions(
onInstall = { id -> protons.installProton(id) },
onInstallTest = { id -> protons.installProton(id, test = true) },
onCancel = { id -> ProtonExtras.tools.firstOrNull { it.id == id }?.let { ProtonExtras.unqueue(this, it) }; protons.refreshProtons() },
onRemove = { id, dir -> protons.removeProton(id, dir) },
onRemoveOlder = { id -> protons.removeProton(id, older = true) },
onExperimentalFile = {
pickExperimentalProton.launch(InAppFilePicker.buildIntent(this, ZIP_EXT, getString(R.string.xproton_pick),
pickExperimentalProton.launch(InAppFilePicker.buildIntent(this, EXPERIMENTAL_PROTON_EXT, getString(R.string.xproton_install_file),
android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).path))
},
onExperimentalRelease = { release -> protons.installExperimentalRelease(release) },
onRemoveExperimental = { dir -> protons.removeExperimental(dir) },
),
protonOptions = {
@@ -3413,6 +3415,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
private const val KEY_SWITCH_DEADLINE = "switch_deadline"
private val SESSION_ENDED = setOf(SessionPhase.IDLE, SessionPhase.FAILED)
private val ZIP_EXT = listOf("zip")
private val EXPERIMENTAL_PROTON_EXT = listOf("zip", "7z", "tar", "gz", "xz", "zst", "bz2", "tgz", "txz", "tbz2", "tzst")
private val WCP_EXT = listOf("wcp")
}
}
@@ -20,6 +20,7 @@ internal class ProtonMenu(
private val activity: android.app.Activity,
private val ui: Handler,
private val onInstalled: () -> Unit = {},
private val onRemoved: () -> Unit = {},
private val idle: () -> Boolean = { true },
) {
private object Operation {
@@ -29,6 +30,9 @@ internal class ProtonMenu(
val percent = mutableIntStateOf(-1)
val problem = mutableStateOf<String?>(null)
val experimental = mutableStateOf<List<String>>(emptyList())
val experimentalReleases = mutableStateOf<List<ExperimentalProtons.Release>>(emptyList())
var fetchedReleases = emptyList<ExperimentalProtons.Release>()
val checking = java.util.concurrent.atomic.AtomicBoolean(false)
}
var protonRows by Operation.rows
var protonBusyId by Operation.busyId
@@ -36,14 +40,16 @@ internal class ProtonMenu(
var protonPercent by Operation.percent
var protonProblem by Operation.problem
var experimentalDirs by Operation.experimental
var experimentalReleases by Operation.experimentalReleases
companion object {
const val EXPERIMENTAL = "experimental"
}
private val checking = java.util.concurrent.atomic.AtomicBoolean(false)
@VisibleForTesting internal var installer: (ProtonExtras.Tool, Boolean, (String, Int) -> Unit) -> String? =
{ tool, test, progress -> ProtonExtras.install(activity, tool, test, progress) }
@VisibleForTesting internal var latestCheck: (Boolean) -> Boolean = { force -> ProtonExtras.checkLatest(activity, force) }
@VisibleForTesting internal var experimentalRefresh: () -> List<ExperimentalProtons.Release>? = { ExperimentalProtons.refresh(activity) }
@VisibleForTesting internal var installer: (ProtonExtras.Tool, (String, Int) -> Unit) -> String? =
{ tool, progress -> ProtonExtras.install(activity, tool, progress) }
fun installProton(id: String, automatic: Boolean = false, test: Boolean = false) {
fun installProton(id: String, automatic: Boolean = false) {
val tool = ProtonExtras.tools.firstOrNull { it.id == id } ?: return
if (protonBusyId != null || ProtonExtras.installInProgress || SessionState.running) return
ProtonExtras.unqueue(activity, tool)
@@ -53,7 +59,7 @@ internal class ProtonMenu(
protonPercent = -1
Thread({
val problem = runCatching {
installer(tool, test) { label, value ->
installer(tool) { label, value ->
ui.post { protonStage = label; protonPercent = value }
}
}.getOrElse { it.message ?: it.javaClass.simpleName }
@@ -114,6 +120,10 @@ internal class ProtonMenu(
}, "install-proton-experimental").start()
}
fun installExperimentalRelease(release: ExperimentalProtons.Release) {
installExperimental { progress -> ExperimentalProtons.installRelease(activity, release, progress) }
}
fun removeExperimental(dir: String) {
if (protonBusyId != null || SessionState.running) return
protonBusyId = EXPERIMENTAL
@@ -131,22 +141,37 @@ internal class ProtonMenu(
}, "remove-proton-experimental").start()
}
private fun refreshExperimental() {
val installed = ExperimentalProtons.installed(activity)
experimentalDirs = installed.map { it.dir }
experimentalReleases = ExperimentalProtons.available(Operation.fetchedReleases, installed)
}
fun refreshProtons() {
experimentalDirs = ExperimentalProtons.installed(activity).map { it.dir }
refreshExperimental()
protonRows = ProtonExtras.tools.map {
val all = ProtonExtras.installedAll(activity, it)
ProtonRow(it.id, it.name, all.lastOrNull(), ProtonExtras.queued(activity, it), all, ProtonExtras.update(activity, it), ProtonExtras.testBuild(activity, it))
ProtonRow(it.id, it.name, all.lastOrNull(), ProtonExtras.queued(activity, it), all, ProtonExtras.update(activity, it))
}
}
fun checkUpdates(force: Boolean = false) {
if (!checking.compareAndSet(false, true)) return
if (!Operation.checking.compareAndSet(false, true)) return
Thread({
val asked = try { ProtonExtras.checkLatest(activity, force) } catch (_: Throwable) { false } finally { checking.set(false) }
ui.post {
if (asked) refreshProtons()
val asked = runCatching { latestCheck(force) }.getOrDefault(false)
val experimental = runCatching { experimentalRefresh() }.getOrNull()
val published = ui.post {
try {
val before = experimentalDirs
Operation.fetchedReleases = experimental ?: emptyList()
if (asked) refreshProtons() else refreshExperimental()
if (!experimentalDirs.containsAll(before)) onRemoved()
} finally {
Operation.checking.set(false)
}
ensureAutoUpdates()
}
if (!published) Operation.checking.set(false)
}, "proton-updates").start()
}
@@ -122,6 +122,12 @@ public final class Downloader {
return null;
}
public static Boolean gone(String url) {
Fetch fetch = fetch(url);
if (fetch.body != null) return Boolean.FALSE;
return fetch.fatal ? Boolean.TRUE : null;
}
public static String downloadDirect(String url) {
LAST_FAILURE.remove();
return fetch(url).body;
@@ -3,16 +3,23 @@ package com.droiddeck.launcher.session
import android.content.Context
import android.os.StatFs
import android.util.Log
import com.droiddeck.launcher.core.Downloader
import com.droiddeck.launcher.core.Hashes
import com.droiddeck.launcher.R
import com.droiddeck.launcher.runtime.LinuxRuntime
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.nio.file.Files
import java.nio.file.attribute.PosixFilePermission
import java.security.DigestOutputStream
import java.security.MessageDigest
import java.util.zip.ZipFile
import org.apache.commons.compress.archivers.sevenz.SevenZFile
import org.apache.commons.compress.archivers.zip.ZipFile as CommonsZipFile
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.deleteRecursively
import org.json.JSONArray
import org.json.JSONObject
object ExperimentalProtons {
@@ -22,12 +29,17 @@ object ExperimentalProtons {
private const val STAGING = ".droiddeck-experimental"
private const val BACKUPS = ".droiddeck-experimental-old"
private const val INCOMING = ".droiddeck-experimental-new"
private const val REMOVED = ".droiddeck-experimental-gone"
private const val GUEST_TOOLS = "/root/.local/share/Steam/compatibilitytools.d"
private const val NEED_BYTES = 4L * 1024 * 1024 * 1024
private const val USER_SETTINGS = "user_settings.py"
private val TARBALL = Regex("(?i)\\.tar(\\.(gz|xz|zst|bz2))?$")
internal val TARBALL = Regex("(?i)\\.tar(\\.(gz|xz|zst|bz2))?$")
internal val FOREIGN_ARCH = Regex("(?i)(^|[^a-z0-9])(x86|x64|amd64|i[3-6]86)([^a-z0-9]|$)")
private val DOWNLOAD_PREFIX = "droiddeck-"
data class Installed(val dir: String, val label: String, val source: String)
data class Installed(val dir: String, val label: String, val source: String, val releaseTag: String? = null)
data class Release(val tag: String, val asset: String, val url: String, val sha512: String?, val size: Long, val name: String = tag)
enum class ArchiveType { ZIP, SEVEN_Z, TAR, UNKNOWN }
private fun toolsDir(context: Context) = File(LinuxRuntime.rootDir(context), GUEST_TOOLS.removePrefix("/"))
@@ -36,7 +48,7 @@ object ExperimentalProtons {
?.filter { it.isDirectory && File(it, MARKER).isFile }
?.map { dir ->
val info = runCatching { JSONObject(File(dir, MARKER).readText()) }.getOrDefault(JSONObject())
Installed(dir.name, info.optString("label").ifEmpty { dir.name }, info.optString("source"))
Installed(dir.name, info.optString("label").ifEmpty { dir.name }, info.optString("source"), info.optString("releaseTag").ifEmpty { null })
}
?.sortedBy { it.dir }
?: emptyList()
@@ -45,9 +57,15 @@ object ExperimentalProtons {
val tools = toolsDir(context)
val target = File(tools, dir)
if (!File(target, MARKER).isFile) return false
val gone = deleteTree(target) && deleteTree(File(File(tools, BACKUPS), dir)) && deleteTree(File(File(tools, INCOMING), dir))
if (gone) ComponentsManager.forgetProton(context, dir)
return gone
val doomed = File(File(tools, REMOVED).apply { mkdirs() }, dir)
if (!deleteTree(doomed)) return false
val detached = synchronized(SessionState.startLock) { !SessionState.running && target.renameTo(doomed) }
if (!detached) return false
ComponentsManager.forgetProton(context, dir)
deleteTree(doomed)
deleteTree(File(File(tools, BACKUPS), dir))
deleteTree(File(File(tools, INCOMING), dir))
return true
}
internal fun dirName(tree: String, label: String?): String {
@@ -56,6 +74,49 @@ object ExperimentalProtons {
return PREFIX + base + (tag?.let { "-$it" } ?: "")
}
fun refresh(context: Context): List<Release>? {
val tool = ProtonExtras.tools.first { it.id == ProtonExtras.OWN }
val fetched = ProtonExtras.releasesForRemoval(tool) ?: return null
val assets = ProtonExtras.assets(tool, fetched.releases, prerelease = true)
val rows = assets.map { Release(it.tag, it.name, it.url, it.sha512, it.size, it.releaseName) }.distinctBy { it.tag }
if (!SessionState.running) {
reconcile(context, fetched)
if (fetched.complete) removeStaleDownloads(context, fetched.releases.tags())
}
return rows
}
private fun JSONArray.tags(): Set<String> = (0 until length()).mapNotNull { optJSONObject(it)?.optString("tag_name")?.takeIf(String::isNotEmpty) }.toSet()
internal fun removeStaleDownloads(context: Context, availableTags: Set<String>) {
val downloads = File(context.filesDir, "proton-downloads")
downloads.listFiles()?.filter { file ->
file.isFile && file.name.startsWith(DOWNLOAD_PREFIX) && availableTags.none { tag -> file.name.startsWith("$DOWNLOAD_PREFIX${ComponentsManager.safeName(tag)}-") }
}?.forEach { it.delete() }
}
internal fun available(releases: List<Release>, installed: List<Installed>): List<Release> =
releases.filter { release -> installed.none { it.releaseTag == release.tag } }
internal fun shouldRemove(installedReleaseTag: String?, fetched: ProtonExtras.ReleaseFetch, gone: (String) -> Boolean): Boolean {
val tag = installedReleaseTag ?: return false
val release = (0 until fetched.releases.length()).asSequence()
.mapNotNull { fetched.releases.optJSONObject(it) }
.firstOrNull { it.optString("tag_name") == tag }
return if (release != null) !release.optBoolean("prerelease", false) else fetched.complete && gone(tag)
}
internal fun reconcile(
context: Context,
fetched: ProtonExtras.ReleaseFetch,
gone: (String) -> Boolean = { tag -> ProtonExtras.releaseGone(ProtonExtras.tools.first { it.id == ProtonExtras.OWN }, tag) },
) {
for (build in installed(context)) {
if (SessionState.running) return
if (shouldRemove(build.releaseTag, fetched, gone)) remove(context, build.dir)
}
}
internal fun compatVdf(name: String): String = """"compatibilitytools"
{
"compat_tools"
@@ -71,21 +132,55 @@ object ExperimentalProtons {
}
"""
fun installFromFile(context: Context, zip: File, onProgress: (String, Int) -> Unit): String? {
fun installFromFile(context: Context, file: File, onProgress: (String, Int) -> Unit): String? {
preflight(context)?.let { return it }
if (StatFs(LinuxRuntime.rootDir(context).path).availableBytes < NEED_BYTES + zip.length()) {
if (StatFs(LinuxRuntime.rootDir(context).path).availableBytes < NEED_BYTES + file.length()) {
return context.getString(R.string.xproton_no_space)
}
return install(context, zip, zip.name.substringBeforeLast('.'), zip.name, onProgress)
return install(context, file, fileLabel(file.name), file.name, null, onProgress, false)
}
internal fun fileLabel(name: String): String =
if (TARBALL.containsMatchIn(name)) name.replace(TARBALL, "") else name.substringBeforeLast('.')
fun installRelease(context: Context, release: Release, onProgress: (String, Int) -> Unit): String? {
preflight(context)?.let { return it }
val downloads = File(context.filesDir, "proton-downloads").apply { mkdirs() }
val archive = File(downloads, "droiddeck-${ComponentsManager.safeName(release.tag)}-${release.asset}")
if (release.size > 0 && archive.length() > release.size) archive.delete()
val missing = (release.size - archive.length()).coerceAtLeast(0L)
val copyBytes = if (sameFileStore(archive, toolsDir(context))) 0L else release.size
if (StatFs(LinuxRuntime.rootDir(context).path).availableBytes < NEED_BYTES + missing + copyBytes) return context.getString(R.string.xproton_no_space)
val title = context.getString(R.string.pextra_downloading, "Proton", release.tag)
onProgress(title, 0)
if (!Downloader.downloadFile(release.url, archive, true) { fraction ->
onProgress(title, if (fraction < 0) -1 else (fraction * 100f).toInt().coerceIn(0, 100))
}) return context.getString(R.string.pextra_download_failed)
if (release.size > 0 && archive.length() != release.size) return context.getString(R.string.pextra_incomplete)
if (release.sha512 != null) {
val expected = Downloader.downloadDirect(release.sha512)?.let(::checksumIn)
?: return context.getString(R.string.pextra_checksum_unreadable)
if (!expected.equals(Hashes.sha512(archive), true)) {
archive.delete()
return context.getString(R.string.pextra_checksum_mismatch)
}
}
return try { install(context, archive, release.tag, release.tag, release.tag, onProgress, true) }
finally { archive.delete() }
}
private fun sameFileStore(file: File, directory: File): Boolean = runCatching {
directory.mkdirs()
Files.getFileStore((file.takeIf(File::exists) ?: file.parentFile).toPath()) == Files.getFileStore(directory.toPath())
}.getOrDefault(false)
private fun preflight(context: Context): String? = when {
SessionState.running -> context.getString(R.string.pextra_stop_session)
!LinuxRuntime.isInstalled(context) -> context.getString(R.string.user_apps_runtime_required)
else -> null
}
private fun install(context: Context, zip: File, label: String, source: String, onProgress: (String, Int) -> Unit): String? {
private fun install(context: Context, archive: File, label: String, source: String, releaseTag: String?, onProgress: (String, Int) -> Unit, downloaded: Boolean): String? {
val tools = toolsDir(context).apply { mkdirs() }
val staging = File(tools, STAGING)
val forget = { name: String -> ComponentsManager.forgetProton(context, name) }
@@ -95,23 +190,40 @@ object ExperimentalProtons {
try {
LinuxRuntime.writeAccounts(context)
SessionFiles.stage(context, LinuxRuntime.rootDir(context))
onProgress(context.getString(R.string.xproton_unzipping), -1)
val tarball = runCatching { extractTarball(zip, staging) }
.getOrElse { return it.message ?: context.getString(R.string.xproton_bad_zip) }
?: return context.getString(R.string.xproton_bad_zip)
onProgress(context.getString(R.string.xproton_unpacking), -1)
val unpacked = File(staging, "unpacked").apply { mkdirs() }
val type = archiveType(archive)
if (SessionState.running) return context.getString(R.string.pextra_session_started)
onProgress(context.getString(R.string.xproton_unpacking), -1)
val unpacked = File(staging, "unpacked").apply { mkdirs() }
val status = runGuest(context, listOf("tar", "-xf", "$GUEST_TOOLS/$STAGING/${tarball.name}", "-C", "$GUEST_TOOLS/$STAGING/unpacked"))
tarball.delete()
if (status != 0) return context.getString(R.string.xproton_untar_failed, status)
when (type) {
ArchiveType.ZIP -> {
val tarball = extractTarball(archive, staging)
if (tarball == null) {
extractDirect(archive, unpacked, ArchiveType.ZIP)
} else {
val status = runGuest(context, listOf("tar", "-xf", "$GUEST_TOOLS/$STAGING/${tarball.name}", "-C", "$GUEST_TOOLS/$STAGING/unpacked"))
tarball.delete()
if (status != 0) return context.getString(R.string.xproton_untar_failed, status)
}
}
ArchiveType.SEVEN_Z -> extractDirect(archive, unpacked, ArchiveType.SEVEN_Z)
ArchiveType.TAR -> {
val stagedArchive = File(staging, archive.name)
if (!archive.canonicalFile.equals(stagedArchive.canonicalFile) && !(downloaded && archive.renameTo(stagedArchive))) {
archive.copyTo(stagedArchive, overwrite = true)
}
val status = runGuest(context, listOf("tar", "-xf", "$GUEST_TOOLS/$STAGING/${stagedArchive.name}", "-C", "$GUEST_TOOLS/$STAGING/unpacked"))
if (status != 0) return context.getString(R.string.xproton_untar_failed, status)
}
ArchiveType.UNKNOWN -> return context.getString(R.string.xproton_archive_invalid)
}
val tree = buildTree(unpacked) ?: return context.getString(R.string.xproton_no_tree)
val name = dirName(tree.name, label)
ownFile(tree, "compatibilitytool.vdf").writeText(compatVdf(name))
ownFile(tree, MARKER).writeText(
JSONObject().put("label", name).put("source", source).put("at", System.currentTimeMillis() / 1000).toString(),
JSONObject().put("label", name).put("source", source).putOpt("releaseTag", releaseTag).put("at", System.currentTimeMillis() / 1000).toString(),
)
val final = File(tools, name)
val settings = File(final, USER_SETTINGS)
@@ -125,7 +237,7 @@ object ExperimentalProtons {
return null
} catch (e: Exception) {
Log.e(TAG, "install $source", e)
return e.message ?: context.getString(R.string.xproton_bad_zip)
return if (e is UnsafeArchiveException) context.getString(R.string.xproton_archive_unsafe) else context.getString(R.string.xproton_archive_invalid)
} finally {
deleteTree(staging)
}
@@ -171,10 +283,22 @@ object ExperimentalProtons {
}
internal fun restoreInterrupted(tools: File, forget: (String) -> Boolean) {
deleteTree(File(tools, REMOVED))
File(tools, INCOMING).listFiles()?.forEach { finishPlacing(tools, it.name, forget) }
File(tools, BACKUPS).listFiles()?.forEach { if (File(tools, it.name).exists()) deleteTree(it) else it.renameTo(File(tools, it.name)) }
}
internal fun archiveType(file: File): ArchiveType {
val head = runCatching { file.inputStream().use { ByteArray(300).also { b -> it.read(b) } } }.getOrNull() ?: return ArchiveType.UNKNOWN
fun at(i: Int, vararg bytes: Int) = bytes.indices.all { head.getOrNull(i + it) == bytes[it].toByte() }
return when {
at(0, 0x50, 0x4B) -> ArchiveType.ZIP
at(0, 0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C) -> ArchiveType.SEVEN_Z
at(0, 0x1F, 0x8B) || at(0, 0x42, 0x5A, 0x68) || at(0, 0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00) || at(0, 0x28, 0xB5, 0x2F, 0xFD) || at(257, 0x75, 0x73, 0x74, 0x61, 0x72) -> ArchiveType.TAR
else -> ArchiveType.UNKNOWN
}
}
private fun extractTarball(zip: File, into: File): File? = ZipFile(zip).use { z ->
val entries = z.entries().toList().filter { !it.isDirectory }
val entry = entries.filter { TARBALL.containsMatchIn(it.name) }.maxByOrNull { it.size } ?: return null
@@ -194,6 +318,78 @@ object ExperimentalProtons {
out
}
internal class UnsafeArchiveException : Exception()
internal fun extractDirect(archive: File, into: File, type: ArchiveType) {
val root = into.toPath().toAbsolutePath().normalize()
fun target(name: String): File {
val clean = name.replace('\\', '/')
val parts = clean.split('/')
if (clean.startsWith('/') || parts.any { it == ".." }) throw UnsafeArchiveException()
val path = root.resolve(clean).normalize()
if (!path.startsWith(root)) throw UnsafeArchiveException()
var parent = path.parent
while (parent != null && parent.startsWith(root) && parent != root) {
if (Files.isSymbolicLink(parent)) throw UnsafeArchiveException()
parent = parent.parent
}
return path.toFile()
}
fun applyMode(file: File, mode: Int) {
val permissions = mutableSetOf<PosixFilePermission>()
if (mode and 0x100 != 0) permissions += PosixFilePermission.OWNER_READ
if (mode and 0x80 != 0) permissions += PosixFilePermission.OWNER_WRITE
if (mode and 0x40 != 0) permissions += PosixFilePermission.OWNER_EXECUTE
if (mode and 0x20 != 0) permissions += PosixFilePermission.GROUP_READ
if (mode and 0x10 != 0) permissions += PosixFilePermission.GROUP_WRITE
if (mode and 0x08 != 0) permissions += PosixFilePermission.GROUP_EXECUTE
if (mode and 0x04 != 0) permissions += PosixFilePermission.OTHERS_READ
if (mode and 0x02 != 0) permissions += PosixFilePermission.OTHERS_WRITE
if (mode and 0x01 != 0) permissions += PosixFilePermission.OTHERS_EXECUTE
if (permissions.isNotEmpty()) Files.setPosixFilePermissions(file.toPath(), permissions)
}
val directoryModes = mutableListOf<Pair<File, Int>>()
fun place(name: String, mode: Int, directory: Boolean, content: () -> InputStream) {
val file = target(name)
when {
mode and 0xF000 == 0xA000 -> {
val link = content().use { it.readBytes().decodeToString() }
val parent = file.parentFile ?: throw UnsafeArchiveException()
val climbsLater = link.split('/').filter { it != "." }.dropWhile { it == ".." }.any { it == ".." }
if (link.startsWith('/') || climbsLater || !parent.toPath().resolve(link).normalize().startsWith(root)) throw UnsafeArchiveException()
parent.mkdirs()
Files.deleteIfExists(file.toPath())
Files.createSymbolicLink(file.toPath(), java.nio.file.Paths.get(link))
}
directory -> {
file.mkdirs()
directoryModes += file to mode
}
else -> {
if (Files.isSymbolicLink(file.toPath())) throw UnsafeArchiveException()
file.parentFile?.mkdirs()
content().use { input -> file.outputStream().use { output -> input.copyTo(output, 1 shl 16) } }
applyMode(file, mode)
}
}
}
when (type) {
ArchiveType.ZIP -> CommonsZipFile.builder().setFile(archive).get().use { zip ->
for (entry in zip.entries) place(entry.name, entry.unixMode, entry.isDirectory) { zip.getInputStream(entry) }
}
ArchiveType.SEVEN_Z -> Files.newByteChannel(archive.toPath()).use { channel ->
SevenZFile.builder().setSeekableByteChannel(channel).get().use { seven ->
for (entry in seven.entries) {
val unix = entry.hasWindowsAttributes && entry.windowsAttributes and 0x8000 != 0
place(entry.name, if (unix) entry.windowsAttributes ushr 16 else 0, entry.isDirectory) { seven.getInputStream(entry) }
}
}
}
else -> error("Unsupported direct archive")
}
directoryModes.sortedByDescending { it.first.path.length }.forEach { (directory, mode) -> applyMode(directory, mode) }
}
internal fun checksumIn(text: String): String? = Regex("(?i)\\b[0-9a-f]{128}\\b").find(text)?.value
internal fun checksumEntry(entries: List<String>, tarball: String): String? {
@@ -16,8 +16,10 @@ import org.json.JSONObject
object ProtonExtras {
private const val TAG = "ProtonExtras"
private const val NEED_BYTES = 4L * 1024 * 1024 * 1024
private const val RELEASES = "https://api.github.com/repos/%s/releases?per_page=15&page=%d"
private const val RELEASE_PAGE_SIZE = 15
private const val RELEASES = "https://api.github.com/repos/%s/releases?per_page=$RELEASE_PAGE_SIZE&page=%d"
private const val RELEASE_PAGES = 5
private const val RELEASE_TAG = "https://api.github.com/repos/%s/releases/tags/%s"
const val CHECK_EVERY_MS = 60L * 60 * 1000
@Volatile
var installInProgress = false
@@ -28,7 +30,8 @@ object ProtonExtras {
class Tool(val id: String, val name: String, val prefix: String, val repo: String, val assetPattern: Regex)
internal data class Asset(val tag: String, val name: String, val url: String, val sha512: String?, val size: Long,
val sha256: String? = null)
val sha256: String? = null, val releaseName: String = tag)
internal data class ReleaseFetch(val releases: JSONArray, val complete: Boolean)
const val OWN = "droiddeck"
@@ -106,12 +109,6 @@ object ProtonExtras {
fun cachedLatest(context: Context, tool: Tool): String? =
readJson(latestFile(context)).optJSONObject(tool.id)?.optString("tag")?.ifEmpty { null }
fun testBuild(context: Context, tool: Tool): String? =
testFor(installedTags(context, tool), readJson(latestFile(context)).optJSONObject(tool.id)?.optString("test")?.ifEmpty { null })
internal fun testFor(installed: List<String>, test: String?): String? =
test?.takeIf { it !in installed && (installed.isEmpty() || updateFor(installed, it) != null) }
private val checkLock = Any()
private const val RETRY_MS = 15L * 60 * 1000
private const val USER_SETTINGS = "user_settings.py"
@@ -135,20 +132,18 @@ object ProtonExtras {
}
}
internal fun recordLatest(latest: JSONObject, tool: Tool, tag: String, test: Boolean, now: Long): Boolean {
if (test) return false
internal fun recordLatest(latest: JSONObject, tool: Tool, tag: String, now: Long): Boolean {
val entry = latest.optJSONObject(tool.id)
val known = entry?.optString("tag")
if (!known.isNullOrEmpty() && compareVersions(tag, known) < 0) return false
val ahead = entry?.optString("test")?.takeIf { it.isNotEmpty() && compareVersions(it, tag) > 0 }
latest.put(tool.id, JSONObject().put("tag", tag).put("at", now).putOpt("test", ahead))
latest.put(tool.id, JSONObject().put("tag", tag).put("at", now))
return true
}
private fun record(context: Context, tool: Tool, dir: String?, tag: String, test: Boolean) {
private fun record(context: Context, tool: Tool, dir: String?, tag: String) {
runCatching {
val latest = readJson(latestFile(context))
if (recordLatest(latest, tool, tag, test, System.currentTimeMillis())) latestFile(context).writeText(latest.toString())
if (recordLatest(latest, tool, tag, System.currentTimeMillis())) latestFile(context).writeText(latest.toString())
if (dir != null) {
val releases = readJson(releasesFile(context))
val present = toolsDir(context).list()?.toSet() ?: emptySet()
@@ -221,17 +216,17 @@ object ProtonExtras {
}
@Synchronized
fun install(context: Context, tool: Tool, test: Boolean = false, onProgress: (String, Int) -> Unit): String? {
fun install(context: Context, tool: Tool, onProgress: (String, Int) -> Unit): String? {
if (installInProgress) return context.getString(R.string.pextra_busy)
installInProgress = true
return try { installNow(context, tool, test, onProgress) } finally { installInProgress = false }
return try { installNow(context, tool, onProgress) } finally { installInProgress = false }
}
private fun installNow(context: Context, tool: Tool, test: Boolean, onProgress: (String, Int) -> Unit): String? {
private fun installNow(context: Context, tool: Tool, onProgress: (String, Int) -> Unit): String? {
if (SessionState.running) return context.getString(R.string.pextra_stop_session)
if (!LinuxRuntime.isInstalled(context)) return context.getString(R.string.user_apps_runtime_required)
val asset = releases(tool)?.let { pickAsset(tool, it, test && tool.id == OWN) } ?: return context.getString(R.string.pextra_no_release, tool.name)
val asset = releases(tool)?.let { pickAsset(tool, it) } ?: return context.getString(R.string.pextra_no_release, tool.name)
val downloads = File(context.filesDir, "proton-downloads").apply { mkdirs() }
val archive = File(downloads, "${tool.id}-${asset.name}")
if (asset.size > 0 && archive.length() > asset.size) archive.delete()
@@ -312,7 +307,7 @@ object ProtonExtras {
else {
archive.delete()
unqueue(context, tool)
record(context, tool, placed, asset.tag, test)
record(context, tool, placed, asset.tag)
if (tool.id == OWN) replaceOlder(context, tool)
if (EsyncPacks.enabled(context)) {
onProgress(context.getString(R.string.esync_fetching), -1)
@@ -349,7 +344,7 @@ object ProtonExtras {
}
}
private fun releases(tool: Tool): JSONArray? = releases(tool) { page ->
internal fun releases(tool: Tool): JSONArray? = releases(tool) { page ->
Downloader.downloadString(RELEASES.format(Locale.ROOT, tool.repo, page))?.let { body ->
try {
JSONArray(body)
@@ -361,51 +356,70 @@ object ProtonExtras {
}
internal fun releases(tool: Tool, fetch: (Int) -> JSONArray?): JSONArray? {
return releasesResult(tool, fetch, stopAtStable = true)?.releases
}
internal fun releasesForRemoval(tool: Tool, fetch: (Int) -> JSONArray? = { page ->
Downloader.downloadString(RELEASES.format(Locale.ROOT, tool.repo, page))?.let { body ->
runCatching { JSONArray(body) }.getOrNull()
}
}): ReleaseFetch? = releasesResult(tool, fetch, stopAtStable = false)
internal fun releaseGone(tool: Tool, tag: String): Boolean =
Downloader.gone(RELEASE_TAG.format(Locale.ROOT, tool.repo, java.net.URLEncoder.encode(tag, "UTF-8").replace("+", "%20"))) == true
private fun releasesResult(tool: Tool, fetch: (Int) -> JSONArray?, stopAtStable: Boolean): ReleaseFetch? {
val all = fetch(1) ?: return null
var page = 1
var last = all.length()
while (last > 0 && page < RELEASE_PAGES && pickAsset(tool, all) == null) {
var complete = last < RELEASE_PAGE_SIZE
while (!complete && page < RELEASE_PAGES && (!stopAtStable || pickAsset(tool, all) == null)) {
val next = fetch(++page) ?: return null
for (i in 0 until next.length()) all.put(next.opt(i))
last = next.length()
complete = last < RELEASE_PAGE_SIZE
}
return all
return ReleaseFetch(all, complete)
}
internal fun pickAsset(tool: Tool, array: JSONArray, test: Boolean = false): Asset? {
internal fun assets(tool: Tool, array: JSONArray, prerelease: Boolean): List<Asset> {
val releases = (0 until array.length()).mapNotNull { array.optJSONObject(it) }.filter { !it.optBoolean("draft", false) }
val stableAt = releases.filter { !it.optBoolean("prerelease", false) }.maxOfOrNull { it.optString("published_at") }.orEmpty()
return releases
.filter { it.optBoolean("prerelease", false) == prerelease && (!prerelease || it.optString("published_at") > stableAt) }
.mapNotNull { assetOf(tool, it, experimental = prerelease) }
}
internal fun pickAsset(tool: Tool, array: JSONArray): Asset? {
val candidates = (0 until array.length()).asSequence()
.mapNotNull { array.optJSONObject(it) }
.filter { !it.optBoolean("draft", false) && it.optBoolean("prerelease", false) == test }
if (!test) return candidates.firstNotNullOfOrNull { assetOf(tool, it) }
val stable = pickAsset(tool, array)?.tag ?: return null
return candidates
.filter { it.optString("tag_name").startsWith(tool.prefix) && updateFor(listOf(stable), it.optString("tag_name")) != null }
.mapNotNull { assetOf(tool, it, requireChecksum = true) }
.maxWithOrNull { a, b -> compareVersions(a.tag, b.tag) }
.filter { !it.optBoolean("draft", false) && !it.optBoolean("prerelease", false) }
return candidates.firstNotNullOfOrNull { assetOf(tool, it) }
}
internal fun updateLatest(cache: JSONObject, tool: Tool, array: JSONArray?, now: Long) {
if (array == null) return
val test = if (tool.id == OWN) pickAsset(tool, array, test = true)?.tag else null
val tag = pickAsset(tool, array)?.tag
if (tag == null) {
cache.optJSONObject(tool.id)?.remove("test")
return
}
cache.put(tool.id, JSONObject().put("tag", tag).put("at", now).putOpt("test", test))
cache.put(tool.id, JSONObject().put("tag", tag).put("at", now))
}
private fun assetOf(tool: Tool, release: JSONObject, requireChecksum: Boolean = false): Asset? = try {
internal fun assetOf(tool: Tool, release: JSONObject, requireChecksum: Boolean = false, experimental: Boolean = false): Asset? = try {
val assets = release.optJSONArray("assets")
val entries = assets?.let { list -> (0 until list.length()).mapNotNull { list.optJSONObject(it) } }.orEmpty()
entries.sortedBy { it.optString("name") }.filter { tool.assetPattern.containsMatchIn(it.optString("name")) }.mapNotNull { archive ->
entries.sortedBy { it.optString("name") }.filter {
val name = it.optString("name")
if (experimental && tool.id == OWN) name.startsWith(release.optString("tag_name")) && ExperimentalProtons.TARBALL.containsMatchIn(name) && !ExperimentalProtons.FOREIGN_ARCH.containsMatchIn(name) else tool.assetPattern.containsMatchIn(name)
}.mapNotNull { archive ->
val name = archive.optString("name")
val stem = name.substringBefore(".tar")
val checksum = entries.firstOrNull {
it.optString("name").startsWith(stem) && it.optString("name").contains("sha512", ignoreCase = true)
}?.optString("browser_download_url")?.takeIf { it.startsWith("http") }
archive.optString("browser_download_url").takeIf { it.startsWith("http") }?.let { url ->
Asset(release.optString("tag_name"), name, url, checksum, archive.optLong("size", 0L), Hashes.githubSha256(archive.optString("digest")))
Asset(release.optString("tag_name"), name, url, checksum, archive.optLong("size", 0L), Hashes.githubSha256(archive.optString("digest")), release.optString("name").ifEmpty { release.optString("tag_name") })
}
}.let { found -> if (requireChecksum) found.firstOrNull { it.sha256 != null || it.sha512 != null } else found.firstOrNull() }
} catch (e: Exception) {
@@ -417,10 +417,12 @@ class SessionService : Service() {
SessionPrefs.screenEffectsProfileId(SessionState.mode, resolvedGameId, SessionState.program),
)
com.droiddeck.launcher.stores.battlenet.BattleNetClient.clearRequests(LinuxRuntime.sessionRoot(this))
val gen = synchronized(stopLock) {
SessionState.steamClientArch = null
SessionState.running = true
++sessionGen
val gen = synchronized(SessionState.startLock) {
synchronized(stopLock) {
SessionState.steamClientArch = null
SessionState.running = true
++sessionGen
}
}
if (SessionState.stopRequested) {
stopSession(0)
@@ -43,6 +43,8 @@ object SessionState {
@Volatile
var stopRequested = false
val startLock = Any()
@Volatile
var running = false
@Volatile
@@ -84,6 +84,7 @@ import com.droiddeck.launcher.R
import com.droiddeck.launcher.session.ComponentUpdater
import com.droiddeck.launcher.session.ComponentUpdates
import com.droiddeck.launcher.session.ComponentsManager
import com.droiddeck.launcher.session.ExperimentalProtons
import com.droiddeck.launcher.session.ComponentsManager.CatalogItem
import com.droiddeck.launcher.session.ComponentsManager.Snapshot
import kotlinx.coroutines.delay
@@ -597,11 +598,9 @@ fun ComponentsPage(
if (rows.isEmpty()) EmptyLine(stringResource(R.string.comp_nothing_new))
for (row in rows) {
val idle = protons.busyId == null && protons.runtimeReady && !protons.sessionRunning
val test = row.test != null && (nightlyPicked[row.id] == true || row.installed != null && row.update == null)
GetLine(
row.name,
when {
test -> row.test!!
row.update != null -> stringResource(R.string.proton_update_available, row.update)
row.queued -> stringResource(R.string.proton_queued)
else -> stringResource(R.string.proton_not_installed)
@@ -610,11 +609,8 @@ fun ComponentsPage(
stage = if (protons.busyId == row.id) protons.stage else null,
enabled = idle,
onCancel = if (row.queued && protons.busyId == null && !protons.sessionRunning) ({ protonActions.onCancel(row.id) }) else null,
action = if (!test && row.update != null) stringResource(R.string.common_update) else null,
channel = if (row.test != null) ({
ChannelToggle(test, enabled = idle && (row.installed == null || row.update != null), other = R.string.comp_channel_test) { nightlyPicked[row.id] = !test }
}) else null,
) { if (test) protonActions.onInstallTest(row.id) else protonActions.onInstall(row.id) }
action = if (row.update != null) stringResource(R.string.common_update) else null,
) { protonActions.onInstall(row.id) }
}
}
else -> {
@@ -742,11 +738,9 @@ fun ComponentsPage(
}
}
}
if (l == PROTON_TAB && DeveloperTools.on) {
if (l == PROTON_TAB) {
Spacer(Modifier.height(12.dp))
ExperimentalProtonGroup(protons, protonActions)
}
if (l == PROTON_TAB) {
Spacer(Modifier.height(12.dp))
Note(stringResource(R.string.comp_about_text))
}
@@ -1645,8 +1639,15 @@ private fun ExperimentalProtonGroup(protons: ProtonsState, actions: ProtonsActio
val idle = protons.busyId == null && protons.runtimeReady && !protons.sessionRunning
RowGroup(stringResource(R.string.xproton_title)) {
if (busy) GetLine(stringResource(R.string.xproton_title), "", progress = protons.percent, enabled = false, stage = protons.stage) {}
else Row(horizontalArrangement = Arrangement.spacedBy(14.dp), modifier = Modifier.padding(start = 14.dp, top = 4.dp, bottom = 8.dp)) {
SmallButton(stringResource(R.string.xproton_pick_zip), enabled = idle, onClick = actions.onExperimentalFile)
else {
protons.experimentalReleases.forEach { release ->
GetLine(release.name, release.tag, progress = null, enabled = idle, action = stringResource(R.string.proton_install_now)) {
actions.onExperimentalRelease(release)
}
}
Row(horizontalArrangement = Arrangement.spacedBy(14.dp), modifier = Modifier.padding(start = 14.dp, top = 4.dp, bottom = 8.dp)) {
SmallButton(stringResource(R.string.xproton_install_file), enabled = idle, onClick = actions.onExperimentalFile)
}
}
}
}
@@ -1659,23 +1660,24 @@ class ProtonsState(
val runtimeReady: Boolean = true,
val sessionRunning: Boolean = false,
val experimental: List<String> = emptyList(),
val experimentalReleases: List<ExperimentalProtons.Release> = emptyList(),
)
class ProtonsActions(
val onInstall: (String) -> Unit = {},
val onInstallTest: (String) -> Unit = {},
val onCancel: (String) -> Unit = {},
val onRemove: (String, String) -> Unit = { _, _ -> },
val onRemoveOlder: (String) -> Unit = {},
val onExperimentalFile: () -> Unit = {},
val onExperimentalRelease: (ExperimentalProtons.Release) -> Unit = {},
val onRemoveExperimental: (String) -> Unit = {},
)
class ProtonRow(
val id: String, val name: String, val installed: String?, val queued: Boolean,
val installedAll: List<String> = listOfNotNull(installed), val update: String? = null, val test: String? = null,
val installedAll: List<String> = listOfNotNull(installed), val update: String? = null,
) {
val available: Boolean get() = installed == null || update != null || test != null
val available: Boolean get() = installed == null || update != null
}
@Composable