chore: merge main to catch up with 0.5.0

This commit is contained in:
xXJsonDeruloXx committed 2026-10-18 17:05:49 -04:00
commit 6d105c1649
1480 files changed
+186541 -49002

No files matched your search

@@ -5,11 +5,6 @@ import com.droiddeck.launcher.runtime.BwrapSpawner
import com.droiddeck.launcher.session.CrashHandler
import com.droiddeck.launcher.session.GpuClockPin
/**
* Process-wide setup: the crash handler, so a session's folder is finished even when we die, and
* the GPU clock pin cleared in case a killed process left it set, and the socket Flatpak's
* sandboxes are started through (BwrapSpawner).
*/
class App : Application() {
override fun attachBaseContext(base: android.content.Context) =
super.attachBaseContext(com.droiddeck.launcher.core.AppLanguage.wrap(base))
@@ -21,13 +16,21 @@ class App : Application() {
override fun onCreate() {
super.onCreate()
// Helper processes must not initialize the main USB provider or guest graphics context.
val process = if (android.os.Build.VERSION.SDK_INT >= 28) Application.getProcessName()
else (getSystemService(ACTIVITY_SERVICE) as android.app.ActivityManager)
.runningAppProcesses?.firstOrNull { it.pid == android.os.Process.myPid() }?.processName
if (process == "$packageName:vr_recovery" || process == "$packageName:rgb_diagnostic") return
CrashHandler.install(this)
com.droiddeck.launcher.session.QuestExperimental.initialize(this)
com.droiddeck.launcher.session.SessionPrefs.settleGpuDriverMode(this)
com.droiddeck.launcher.update.AppUpdates.init(this)
com.droiddeck.launcher.core.DownloadSource.init(this)
GpuClockPin.clearLeftover(this)
com.droiddeck.launcher.glasses.Glasses.init(this)
com.droiddeck.launcher.glasses.GlassesMenuMirror.initialize(this)
BwrapSpawner.start(this)
com.droiddeck.launcher.frontend.GameFileSync.start(this)
// The game stores the Stores section can talk to; the native engine loads only when one needs it.
com.droiddeck.launcher.stores.StoresState.init(this)
com.droiddeck.launcher.stores.StoresState.register(com.droiddeck.launcher.stores.gog.GogBackend)
com.droiddeck.launcher.stores.StoresState.register(com.droiddeck.launcher.stores.epic.EpicBackend)
@@ -8,11 +8,7 @@ import androidx.compose.runtime.setValue
import com.droiddeck.launcher.session.ComponentsManager
import com.droiddeck.launcher.session.ProtonDefault
/**
* The Components page's state and work: the Protons and their DXVK/VKD3D sets, the Nightlies
* catalog, downloads and the swap/restore actions. The launcher screen keeps one and shows it.
*/
internal class ComponentsMenu(private val activity: android.app.Activity, private val ui: android.os.Handler) {
internal class ComponentsMenu(private val activity: android.app.Activity, private val ui: Handler) {
var compSnapshot by mutableStateOf<ComponentsManager.Snapshot?>(null)
var compCatalog by mutableStateOf<List<ComponentsManager.CatalogItem>>(emptyList())
var compCatalogAt by mutableStateOf(0L)
@@ -20,10 +16,12 @@ internal class ComponentsMenu(private val activity: android.app.Activity, privat
var compComp by mutableStateOf(com.droiddeck.launcher.ui.GPU_TAB)
var compChecking by mutableStateOf(false)
var compBusy by mutableStateOf<String?>(null)
var compDownloads by mutableStateOf<Map<String, Int>>(emptyMap())
var compDownloads: Map<String, Int>
get() = downloads
private set(value) { downloads = value }
val compDownloadRevision: Long get() = downloadRevision
private var shownDefault: String? = null
/** Reads the Protons (and on first open saves their originals) off the UI thread; the Nightlies list comes from its cache. */
fun refreshComponents(snapshotFirst: Boolean = false) {
Thread({
if (snapshotFirst) runCatching { ComponentsManager.snapshotAll(activity) }
@@ -72,15 +70,15 @@ internal class ComponentsMenu(private val activity: android.app.Activity, privat
}, "components-action").start()
}
fun refreshComponentCatalog() {
fun refreshComponentCatalog(force: Boolean = true) {
if (compChecking) return
compChecking = true
Thread({
val cat = runCatching { ComponentsManager.catalog(activity, true) }.getOrNull()
val cat = runCatching { ComponentsManager.catalog(activity, force) }.getOrNull()
ui.post {
compChecking = false
if (cat == null || cat.items.isEmpty()) android.widget.Toast.makeText(activity, activity.getString(R.string.comp_nightlies_unreachable), android.widget.Toast.LENGTH_LONG).show()
else { compCatalog = cat.items; compCatalogAt = cat.fetchedAt }
if (cat != null && cat.items.isNotEmpty()) { compCatalog = cat.items; compCatalogAt = cat.fetchedAt }
else if (force) android.widget.Toast.makeText(activity, activity.getString(R.string.comp_nightlies_unreachable), android.widget.Toast.LENGTH_LONG).show()
}
}, "components-catalog").start()
}
@@ -95,6 +93,7 @@ internal class ComponentsMenu(private val activity: android.app.Activity, privat
}.getOrElse { e -> activity.getString(R.string.comp_download_failed, e.message ?: e.javaClass.simpleName) }
ui.post {
compDownloads = compDownloads - item.file
downloadRevision++
android.widget.Toast.makeText(activity, message, android.widget.Toast.LENGTH_SHORT).show()
refreshComponents()
}
@@ -102,6 +101,8 @@ internal class ComponentsMenu(private val activity: android.app.Activity, privat
}
private companion object {
const val TAG = "MainActivity"
const val TAG = "ComponentsMenu"
var downloads by mutableStateOf<Map<String, Int>>(emptyMap())
var downloadRevision by mutableStateOf(0L)
}
}
@@ -9,17 +9,12 @@ import com.droiddeck.launcher.runtime.DeckyManager
import com.droiddeck.launcher.session.SessionState
import java.io.File
/**
* Decky Loader's state in the launcher: what is installed, the releases on offer and an install
* in progress. The launcher screen keeps one and shows it.
*/
internal class DeckyMenu(private val activity: android.app.Activity, private val ui: android.os.Handler) {
var deckyInstalled by mutableStateOf<String?>(null)
var deckyReleases by mutableStateOf<List<DeckyManager.Release>>(emptyList())
var deckyChecking by mutableStateOf(false)
var deckyStage by mutableStateOf<String?>(null)
var deckyPercent by mutableIntStateOf(-1)
/** True while the stage is the plugin's binary download (DeckyManager.downloadingBinary). */
var deckyDownloadingBinary by mutableStateOf(false)
var deckySupervisor by mutableStateOf(false)
var deckyReleaseRequest = 0
@@ -32,8 +27,6 @@ internal class DeckyMenu(private val activity: android.app.Activity, private val
.getOrElse { DeckyManager.ReleaseChannels(emptyList(), emptyList()) }
ui.post {
if (request == deckyReleaseRequest) {
// Use the newest compatible stable build, or the newest compatible
// prerelease when the fork has not published a stable ARM64 asset.
deckyReleases = channels.stable.ifEmpty { channels.prerelease }
deckyChecking = false
}
@@ -11,6 +11,8 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.runtime.mutableIntStateOf
import com.droiddeck.launcher.gpu.AutoDriverTrial
import com.droiddeck.launcher.gpu.AutoPicks
import com.droiddeck.launcher.gpu.DriverBundle
import com.droiddeck.launcher.gpu.DriverPairs
import com.droiddeck.launcher.gpu.GpuInfo
@@ -20,42 +22,45 @@ import com.droiddeck.launcher.gpu.TurnipDriver
import com.droiddeck.launcher.gpu.TurnipReleases
import com.droiddeck.launcher.session.SessionService
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.session.QuestExperimental
import com.droiddeck.launcher.session.SessionState
import com.droiddeck.launcher.ui.DriverRow
import com.droiddeck.launcher.ui.DriverSlot
import com.droiddeck.launcher.ui.GPU_MODE_AUTO
import com.droiddeck.launcher.ui.GPU_MODE_MIX
import com.droiddeck.launcher.ui.GPU_MODE_PAIR
import com.droiddeck.launcher.ui.GpuDriversState
import com.droiddeck.launcher.ui.PairRow
import com.droiddeck.launcher.wayland.CompositorHost
/**
* The GPU drivers: what this GPU is, the matched driver pairs the release repos offer for it, and
* - in Auto, the default - keeping the recommended pair installed and set. Under Advanced, the
* Linux (runtime) and Android (display) lists as before, each picked on its own - except a
* bundle's two halves (DriverBundle), which are always picked, and deleted, together. The launcher
* screen keeps one and hands it to the Components page's GPU drivers tab.
*/
internal class DriverMenus(private val activity: Activity, private val ui: Handler) {
var linuxRows by mutableStateOf<List<DriverRow>>(emptyList())
/** The latest Banners-Turnip release as each driver menu offers it (see [refreshReleaseRows]). */
var linuxDownloads by mutableStateOf<List<com.droiddeck.launcher.ui.DownloadRow>>(emptyList())
var androidDownloads by mutableStateOf<List<com.droiddeck.launcher.ui.DownloadRow>>(emptyList())
// Empty until the first refresh: the activity has no context yet while its fields are made.
var releaseStatus by mutableStateOf("")
var releaseChecking by mutableStateOf(false)
var canRestoreBundled by mutableStateOf(false)
/** Asset name -> download percent, while it downloads. */
private val releaseProgress = HashMap<String, Int>()
var linuxSelected by mutableStateOf("")
val gpu: GpuInfo = GpuInfo.detect()
// Read in refreshDrivers/ensureAuto: the activity has no context yet while its fields are made.
var mode by mutableStateOf(SessionPrefs.GPU_DRIVERS_AUTO)
var pairRows by mutableStateOf<List<PairRow>>(emptyList())
/** The pair being installed, and how far along. */
var pairBusy by mutableStateOf<String?>(null)
var pairPercent by mutableIntStateOf(-1)
/** What Auto last did or found, for the line under the pair in use. */
var autoStatus by mutableStateOf("")
/** The bundle both drivers are set to, as "DD-Turnip 0.1.0", or null when they are not one. */
var activeBundle by mutableStateOf<String?>(null)
private var autoCheckedThisProcess = false
private var lastAutoCheck = 0L
private var pairModeOpen by mutableStateOf(false)
var restartPending by mutableStateOf(false)
private set
private var runtimePending by mutableStateOf(false)
private var sessionRunning by mutableStateOf(false)
val uiMode: String get() = when (mode) {
SessionPrefs.GPU_DRIVERS_AUTO -> GPU_MODE_AUTO
SessionPrefs.GPU_DRIVERS_MIX -> GPU_MODE_MIX
else -> if (pairModeOpen || pairRows.any { it.active }) GPU_MODE_PAIR else GPU_MODE_MIX
}
fun state() = GpuDriversState(
gpuName = gpu.displayName(activity), gpuFamily = gpu.family.label(activity), soc = gpu.soc, supportText = gpu.supportText(activity),
@@ -65,7 +70,56 @@ internal class DriverMenus(private val activity: Activity, private val ui: Handl
linuxRows = linuxRows, linuxSelected = linuxSelected, androidRows = androidRows, androidSelected = androidSelected,
linuxDownloads = linuxDownloads, androidDownloads = androidDownloads, canRestoreBundled = canRestoreBundled,
activeBundle = activeBundle,
mode = uiMode, display = displaySlot(), runtime = runtimeSlot(),
canRestart = !sessionRunning,
checkedAt = TurnipReleases.cached(activity)?.checkedAt ?: 0L,
)
private fun slotSource(chosenDefault: Boolean, defaultLabel: Int): String = activity.getString(when {
uiMode == GPU_MODE_AUTO -> R.string.gpu_src_auto
uiMode == GPU_MODE_PAIR && pairRows.any { it.active } -> R.string.gpu_src_pair
activeBundle != null -> R.string.gpu_src_bundle
chosenDefault -> defaultLabel
else -> R.string.gpu_src_chosen
})
private fun displaySlot(): DriverSlot {
val row = androidRows.firstOrNull { it.id == androidSelected }
val live = androidRows.firstOrNull { it.id == CompositorHost.displayChoice }?.name
return DriverSlot(
name = row?.name ?: activity.getString(R.string.gpu_auto_by_gpu),
source = slotSource(androidSelected == TurnipDriver.AUTO, R.string.gpu_src_by_gpu),
pending = restartPending,
status = when {
restartPending -> activity.getString(R.string.gpu_display_pending, live ?: activity.getString(R.string.gpu_previous_driver))
CompositorHost.isStarted -> activity.getString(R.string.gpu_display_live)
else -> activity.getString(R.string.gpu_display_next)
},
)
}
private fun runtimeSlot(): DriverSlot {
val row = linuxRows.firstOrNull { it.id == linuxSelected }
val live = SessionState.runtimeDriver?.let { id -> linuxRows.firstOrNull { it.id == id }?.name }
return DriverSlot(
name = row?.name ?: activity.getString(R.string.gpu_runtime_default),
source = slotSource(linuxSelected.isEmpty(), R.string.gpu_src_default),
pending = runtimePending,
status = when {
runtimePending -> activity.getString(R.string.gpu_runtime_pending, live ?: activity.getString(R.string.gpu_previous_driver))
sessionRunning -> activity.getString(R.string.gpu_runtime_live)
else -> activity.getString(R.string.gpu_runtime_hint)
},
)
}
fun restartApp() {
if (SessionState.running) return
val launch = activity.packageManager.getLaunchIntentForPackage(activity.packageName) ?: return
activity.startActivity(android.content.Intent.makeRestartActivityTask(launch.component)
.putExtra(com.droiddeck.launcher.stores.download.StoreDownloadService.EXTRA_NAV, "components"))
Runtime.getRuntime().exit(0)
}
var androidRows by mutableStateOf<List<DriverRow>>(emptyList())
var androidSelected by mutableStateOf("")
@@ -103,23 +157,30 @@ internal class DriverMenus(private val activity: Activity, private val ui: Handl
canRestoreBundled = td.hiddenBundled().isNotEmpty()
androidSelected = SessionPrefs.androidDriver(activity)
activeBundle = DriverBundle.active(activity)?.label
restartPending = CompositorHost.isStarted && CompositorHost.displayChoice.let { it != null && it != androidSelected }
sessionRunning = SessionState.running
runtimePending = sessionRunning && SessionState.runtimeDriver.let { it != null && it != linuxSelected }
refreshReleaseRows()
refreshPairs()
}
/** One line for the settings row that opens this: "Auto · WinNative · Balanced". */
fun summary(): String {
val active = pairRows.firstOrNull { it.active }?.name ?: activeBundle
val label = activity.getString(if (mode == SessionPrefs.GPU_DRIVERS_AUTO) R.string.common_auto else R.string.drivers_manual)
return active?.let { activity.getString(R.string.drivers_summary, label, it) } ?: label
val display = displaySlot().name
val base = when {
uiMode == GPU_MODE_AUTO -> activity.getString(R.string.drivers_summary, activity.getString(R.string.common_auto), active ?: display)
uiMode == GPU_MODE_PAIR && active != null -> activity.getString(R.string.drivers_summary, activity.getString(R.string.gpu_mode_pair_short), active)
else -> activity.getString(R.string.drivers_summary, activity.getString(R.string.gpu_mode_mix_short),
runtimeSlot().name.let { if (it == display) it else activity.getString(R.string.gpu_summary_mix, display, it) })
}
return if (restartPending) activity.getString(R.string.gpu_summary_restart, base) else base
}
/** The pairs the last check found, with what is installed and in use; this GPU's first. */
fun refreshPairs() {
val lm = LinuxVulkanDriverManager(activity)
val td = TurnipDriver(activity)
val pairs = DriverPairs.from(TurnipReleases.cached(activity))
val recommended = DriverPairs.recommendedKey(gpu, pairs)
val recommended = if (QuestExperimental.isSupported()) null else recommendedKey(pairs)
pairRows = pairs.map { p ->
val bundle = p.bundle?.let { installedBundle(it) }
val displayId = if (p.bundle != null) bundle?.androidId else p.display?.let { TurnipReleases.installedId(activity, it, td::isInstalled) }
@@ -136,24 +197,61 @@ internal class DriverMenus(private val activity: Activity, private val ui: Handl
}.sortedByDescending { it.recommended }
}
fun setMode(auto: Boolean) {
mode = if (auto) SessionPrefs.GPU_DRIVERS_AUTO else SessionPrefs.GPU_DRIVERS_MANUAL
private fun recommendedKey(pairs: List<DriverPairs.DriverPair>) = DriverPairs.recommendedKey(
gpu, pairs, TurnipReleases.autoPicks(activity), AutoDriverTrial.bad(activity),
blob = AutoPicks.blobVersion(com.droiddeck.launcher.gpu.VulkanInfo.cachedOrNull()?.get("driver_version")),
)
fun chooseMode(next: String) {
pairModeOpen = next == GPU_MODE_PAIR
setMode(auto = next == GPU_MODE_AUTO, mix = next == GPU_MODE_MIX)
}
fun setMode(auto: Boolean, mix: Boolean = false) {
mode = when {
auto -> SessionPrefs.GPU_DRIVERS_AUTO
mix -> SessionPrefs.GPU_DRIVERS_MIX
else -> SessionPrefs.GPU_DRIVERS_MANUAL
}
SessionPrefs.setGpuDriverMode(activity, mode)
autoStatus = ""
if (auto) ensureAuto(force = false)
}
/**
* Auto: make the recommended pair for this GPU the one installed and set. Looks online when
* [force]d (refresh), or when the last check is a day old - Banners-Turnip rebuilds hourly, and
* a new pair a day is plenty - otherwise works from the last check. Once per app start on its
* own; the pair it replaces is removed, anything the user imported or picked is left alone.
*/
fun checkOnOpen() {
if (SessionPrefs.gpuDriverMode(activity) == SessionPrefs.GPU_DRIVERS_AUTO) ensureAuto(force = false)
else if (!releaseChecking && releasesStale() && com.droiddeck.launcher.frontend.AutoComponents.isOnline(activity)) checkLatestTurnip()
}
private fun releasesStale(): Boolean {
val cached = TurnipReleases.cached(activity)
return cached == null || System.currentTimeMillis() - cached.checkedAt > RELEASE_CHECK_INTERVAL_MS ||
cached.assets.none { it.pair.isNotEmpty() }
}
fun ensureAuto(force: Boolean) {
mode = SessionPrefs.gpuDriverMode(activity)
if (mode != SessionPrefs.GPU_DRIVERS_AUTO || pairBusy != null || releaseChecking) return
if (!force && autoCheckedThisProcess) return
autoCheckedThisProcess = true
if (SessionState.running) {
if (force) autoStatus = activity.getString(R.string.drivers_auto_waits_for_session)
return
}
AutoDriverTrial.rollbackDue(activity)?.let { trial ->
AutoDriverTrial.rollBack(activity, trial)
autoStatus = activity.getString(R.string.drivers_rolled_back, trial.name)
refreshDrivers()
return
}
if (!force && System.currentTimeMillis() - lastAutoCheck < RELEASE_CHECK_INTERVAL_MS) return
lastAutoCheck = System.currentTimeMillis()
val displayChanged = SessionPrefs.androidDriver(activity) != QuestExperimental.defaultAndroidDriver()
if (QuestExperimental.applyAutoDrivers(activity)) {
autoStatus = activity.getString(
if (displayChanged && CompositorHost.isStarted) R.string.drivers_using_quest_restart else R.string.drivers_using_quest,
)
refreshDrivers()
return
}
if (DriverPairs.recommendedKey(gpu, emptyList()) == null) {
autoStatus = activity.getString(R.string.drivers_none_to_set, gpu.supportText(activity).lowercase())
return
@@ -161,16 +259,15 @@ internal class DriverMenus(private val activity: Activity, private val ui: Handl
releaseChecking = true
autoStatus = activity.getString(R.string.drivers_checking_latest)
Thread({
val cached = TurnipReleases.cached(activity)
val stale = cached == null || System.currentTimeMillis() - cached.checkedAt > 24 * 3_600_000L ||
cached.assets.none { it.pair.isNotEmpty() }
val problem = if (force || stale) runCatching { TurnipReleases.refresh(activity) }.exceptionOrNull()?.message else null
val problem = if (force || releasesStale()) runCatching { TurnipReleases.refresh(activity) }.exceptionOrNull()?.message else null
val holdBack = !force && com.droiddeck.launcher.frontend.AutoComponents.constrained(activity)
runCatching { com.droiddeck.launcher.gpu.VulkanInfo.query() }
ui.post {
releaseChecking = false
refreshReleaseRows()
refreshPairs()
val pairs = DriverPairs.from(TurnipReleases.cached(activity))
val key = DriverPairs.recommendedKey(gpu, pairs)
val key = recommendedKey(pairs)
val pair = pairs.firstOrNull { it.key == key }
val row = pairRows.firstOrNull { it.key == key }
when {
@@ -178,32 +275,30 @@ internal class DriverMenus(private val activity: Activity, private val ui: Handl
?: activity.getString(R.string.upd_current_status)
pair == null || !pair.complete ->
autoStatus = problem?.let { activity.getString(R.string.upd_couldnt_check, it) } ?: activity.getString(R.string.drivers_recommended_unpublished)
AutoDriverTrial.release(pair) in AutoDriverTrial.bad(activity) ->
autoStatus = activity.getString(R.string.drivers_skipping_bad, pair.name, pair.version)
SessionState.running -> autoStatus = activity.getString(R.string.drivers_auto_waits_for_session)
holdBack && row?.installed != true -> autoStatus = activity.getString(R.string.drivers_auto_waits_for_network, pair.name, pair.version)
else -> installPair(pair, auto = true)
}
}
}, "gpu-driver-auto").start()
}
/** Manual: install (if need be) and set both halves of a pair. */
fun selectPair(key: String) {
if (pairBusy != null) return
val pair = DriverPairs.from(TurnipReleases.cached(activity)).firstOrNull { it.key == key && it.complete } ?: return
pairModeOpen = true
if (mode != SessionPrefs.GPU_DRIVERS_MANUAL) setMode(auto = false)
installPair(pair, auto = false)
}
/**
* Download what is missing of [pair], then set both halves - only once both are in, so a
* failed download never leaves a half-changed pair. Auto's own downloads are recorded, and the
* ones the new pair replaces are removed.
*/
private fun installPair(pair: DriverPairs.DriverPair, auto: Boolean) {
if (!pair.complete) return
pairBusy = pair.key
pairPercent = 0
if (auto) autoStatus = activity.getString(R.string.drivers_downloading_pair, pair.name, pair.version)
Thread({
val lm = LinuxVulkanDriverManager(activity)
val td = TurnipDriver(activity)
var downloaded = false
val result = runCatching {
val zips = pair.assets
@@ -217,22 +312,27 @@ internal class DriverMenus(private val activity: Activity, private val ui: Handl
pairBusy = null
pairPercent = -1
result.onSuccess { (displayId, linuxId) ->
val displayChanged = SessionPrefs.androidDriver(activity) != displayId
if (auto && SessionState.running) {
lastAutoCheck = 0L
autoStatus = activity.getString(R.string.drivers_auto_waits_for_session)
return@onSuccess
}
val previousDisplay = SessionPrefs.androidDriver(activity)
val previousLinux = SessionPrefs.linuxDriver(activity)
val displayChanged = previousDisplay != displayId
SessionPrefs.setAndroidDriver(activity, displayId)
SessionPrefs.setLinuxDriver(activity, linuxId)
val restart = displayChanged && CompositorHost.isStarted
if (auto) {
val previous = SessionPrefs.gpuAutoInstalled(activity)
for (id in previous - setOf(displayId, linuxId)) {
val bundle = DriverBundle.containing(activity, id, lm.isInstalled(id))
when {
bundle != null -> { DriverBundle.remove(activity, bundle); TurnipReleases.forget(activity, bundle.id) }
lm.isInstalled(id) -> lm.removeDriver(id)
else -> td.remove(id)
}
TurnipReleases.forget(activity, id)
}
SessionPrefs.setGpuAutoInstalled(activity, setOf(displayId, linuxId))
val trial = AutoDriverTrial.current(activity)
val lastGood = trial?.let { setOf(it.previousDisplay, it.previousLinux) } ?: setOf(previousDisplay, previousLinux)
val changed = previousDisplay != displayId || previousLinux != linuxId
SessionPrefs.setGpuAutoInstalled(activity, SessionPrefs.gpuAutoInstalled(activity) + setOf(displayId, linuxId))
if (changed) AutoDriverTrial.begin(
activity, AutoDriverTrial.release(pair), "${pair.name} ${pair.version}", displayId, linuxId,
trial?.previousDisplay ?: previousDisplay, trial?.previousLinux ?: previousLinux,
)
if (!SessionState.running) AutoDriverTrial.prune(activity, lastGood + setOf(displayId, linuxId))
autoStatus = activity.getString(
if (downloaded) { if (restart) R.string.drivers_updated_to_restart else R.string.drivers_updated_to }
else { if (restart) R.string.drivers_switched_to_restart else R.string.drivers_switched_to },
@@ -252,21 +352,15 @@ internal class DriverMenus(private val activity: Activity, private val ui: Handl
}, "gpu-driver-pair").start()
}
/** The bundle a release bundle was downloaded as, while both its halves are installed. */
private fun installedBundle(asset: TurnipReleases.Asset): DriverBundle.Bundle? =
TurnipReleases.installedId(activity, asset) { DriverBundle.get(activity, it) != null }?.let { DriverBundle.get(activity, it) }
/** The id a release asset was installed as (a bundle's id for a bundle), or null when it is not installed. */
private fun installedIdOf(asset: TurnipReleases.Asset): String? = when {
asset.bundle -> installedBundle(asset)?.id
asset.linux -> TurnipReleases.installedId(activity, asset, LinuxVulkanDriverManager(activity)::isInstalled)
else -> TurnipReleases.installedId(activity, asset, TurnipDriver(activity)::isInstalled)
}
/**
* One release asset, downloaded, checked and installed through the importer; returns its id,
* a bundle's id for a bundle.
*/
private fun installAsset(asset: TurnipReleases.Asset, progress: (Int) -> Unit): String {
var file: File? = null
try {
@@ -284,12 +378,6 @@ internal class DriverMenus(private val activity: Activity, private val ui: Handl
}
}
/**
* Import off the main thread - a driver zip is a few MB and the glibc check reads the whole
* library - then say what happened. A refusal's message is the user-facing reason. A bundle,
* from any import, installs both halves and is set as both drivers at once. [linux] null (the
* tab's own Import .zip) takes a single driver to whichever list its libc belongs in.
*/
fun importDriver(uri: Uri, linux: Boolean?) {
val name = activity.displayNameOf(uri)
Thread({
@@ -328,10 +416,6 @@ internal class DriverMenus(private val activity: Activity, private val ui: Handl
}, "import-driver").start()
}
/**
* Pick one driver from a list, which is Manual (Auto would put its pair back). A bundle's half
* sets the bundle's other half in the other list as well.
*/
fun selectDriver(id: String, linux: Boolean) {
val bundle = DriverBundle.containing(activity, id, linux)
when {
@@ -339,26 +423,21 @@ internal class DriverMenus(private val activity: Activity, private val ui: Handl
linux -> SessionPrefs.setLinuxDriver(activity, id)
else -> SessionPrefs.setAndroidDriver(activity, id)
}
setMode(false)
pairModeOpen = false
setMode(auto = false, mix = true)
refreshDrivers()
}
/** Set both halves of [bundle] as the drivers in use; returns what to tell the user. */
private fun useBundle(bundle: DriverBundle.Bundle): String {
val displayChanged = SessionPrefs.androidDriver(activity) != bundle.androidId
DriverBundle.select(activity, bundle)
if (mode == SessionPrefs.GPU_DRIVERS_AUTO) setMode(false)
if (mode == SessionPrefs.GPU_DRIVERS_AUTO) setMode(auto = false, mix = true)
return activity.getString(
if (displayChanged && CompositorHost.isStarted) R.string.drivers_using_bundle_restart else R.string.drivers_using_bundle,
bundle.label,
)
}
/**
* Delete an imported or downloaded driver. A mode still set to it goes back to its default, so a
* session never starts on a driver that is gone; a release download is forgotten, so the menu
* offers it again.
*/
fun deleteDriver(id: String, linux: Boolean) {
val bundle = DriverBundle.containing(activity, id, linux)
if (bundle != null) {
@@ -381,7 +460,6 @@ internal class DriverMenus(private val activity: Activity, private val ui: Handl
refreshDrivers()
}
/** The download entries and the refresh line, from what the last check found. */
fun refreshReleaseRows() {
val check = TurnipReleases.cached(activity)
fun rows(linux: Boolean) = check?.assets.orEmpty()
@@ -413,7 +491,6 @@ internal class DriverMenus(private val activity: Activity, private val ui: Handl
}
}
/** Only when the user taps refresh: nothing goes online on its own. */
fun checkLatestTurnip() {
if (releaseChecking) return
releaseChecking = true
@@ -431,7 +508,6 @@ internal class DriverMenus(private val activity: Activity, private val ui: Handl
}, "turnip-release-check").start()
}
/** Download one release driver and import it through the same importer a picked zip uses. */
fun downloadReleaseDriver(assetName: String) {
val asset = TurnipReleases.cached(activity)?.assets?.firstOrNull { it.name == assetName } ?: return
if (releaseProgress.containsKey(assetName)) return
@@ -464,6 +540,7 @@ internal class DriverMenus(private val activity: Activity, private val ui: Handl
private companion object {
const val TAG = "MainActivity"
const val RELEASE_CHECK_INTERVAL_MS = 3_600_000L
}
}
@@ -5,7 +5,6 @@ import android.content.Intent
import android.os.Bundle
import com.droiddeck.launcher.session.SessionService
/** Receives the system Home launch and routes it without consuming KEYCODE_HOME. */
class HomeActivity : Activity() {
override fun attachBaseContext(newBase: android.content.Context) {
super.attachBaseContext(newBase)
@@ -13,7 +13,6 @@ import androidx.core.graphics.drawable.toBitmap
import com.droiddeck.launcher.session.SessionService
import com.droiddeck.launcher.session.SessionState
/** Android Home role and the launchable Android apps shown by the in-session app drawer. */
object HomeApp {
data class LaunchableApp(
val label: String,
@@ -22,17 +21,10 @@ object HomeApp {
val icon: Bitmap?,
)
/**
* Whether the app offers itself as a Home app at all. HomeActivity is declared disabled and
* only enabled when the user turns on "Use as a Home screen": a player who does not want a
* launcher never gets Android's "which Home app?" question, and it is one apk either way.
* The setting is the component's own state, which Android keeps across updates.
*/
fun isHomeScreenEnabled(context: Context): Boolean =
context.packageManager.getComponentEnabledSetting(ComponentName(context, HomeActivity::class.java)) ==
PackageManager.COMPONENT_ENABLED_STATE_ENABLED
/** Turned off while DroidDeck is the Home app, the phone goes back to its own launcher. */
fun setHomeScreenEnabled(context: Context, enabled: Boolean) {
context.packageManager.setComponentEnabledSetting(
ComponentName(context, HomeActivity::class.java),
@@ -113,7 +105,6 @@ object HomeApp {
try {
context.startActivity(settingsIntent())
} catch (_: Exception) {
// A role request remains available on Android 10+ if the settings page is missing.
val request = roleRequestIntent(context)
if (request != null) context.startActivity(request)
}
File diff suppressed because it is too large. Load diff
@@ -9,15 +9,12 @@ import com.droiddeck.launcher.session.ProtonExtras
import com.droiddeck.launcher.ui.ProtonRow
import com.droiddeck.launcher.session.SessionState
/**
* The optional ARM64 Protons (GE-Proton, proton-cachyos): the rows the page lists and an install or
* removal in progress. The launcher screen keeps one and shows it.
*/
internal class ProtonMenu(private val activity: android.app.Activity, private val ui: android.os.Handler) {
var protonRows by mutableStateOf<List<ProtonRow>>(emptyList())
var protonBusyId by mutableStateOf<String?>(null)
var protonStage by mutableStateOf<String?>(null)
var protonPercent by mutableIntStateOf(-1)
private val checking = java.util.concurrent.atomic.AtomicBoolean(false)
fun installProton(id: String) {
val tool = ProtonExtras.tools.firstOrNull { it.id == id } ?: return
@@ -40,14 +37,14 @@ internal class ProtonMenu(private val activity: android.app.Activity, private va
}, "install-proton-$id").start()
}
fun removeProton(id: String) {
fun removeProton(id: String, dir: String? = null, older: Boolean = false) {
val tool = ProtonExtras.tools.firstOrNull { it.id == id } ?: return
if (protonBusyId != null || SessionState.running) return
protonBusyId = id
protonStage = activity.getString(R.string.main_removing_named, tool.name)
protonStage = activity.getString(R.string.main_removing_named, dir ?: tool.name)
protonPercent = -1
Thread({
ProtonExtras.remove(activity, tool)
if (older) ProtonExtras.removeOlder(activity, tool) else ProtonExtras.remove(activity, tool, dir)
ui.post {
protonBusyId = null
protonStage = null
@@ -57,6 +54,17 @@ internal class ProtonMenu(private val activity: android.app.Activity, private va
}
fun refreshProtons() {
protonRows = ProtonExtras.tools.map { ProtonRow(it.id, it.name, ProtonExtras.installed(activity, it), ProtonExtras.queued(activity, it)) }
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))
}
}
fun checkUpdates(force: Boolean = false) {
if (!checking.compareAndSet(false, true)) return
Thread({
val asked = try { ProtonExtras.checkLatest(activity, force) } catch (_: Throwable) { false } finally { checking.set(false) }
if (asked) ui.post { refreshProtons() }
}, "proton-updates").start()
}
}
@@ -0,0 +1,118 @@
package com.droiddeck.launcher
import android.app.NativeActivity
import android.os.Bundle
import android.os.SystemClock
import android.util.Log
import android.widget.Toast
import com.droiddeck.launcher.gpu.SteamVr
import com.droiddeck.launcher.session.SessionService
import com.droiddeck.launcher.session.SessionState
import com.droiddeck.launcher.wayland.CompositorHost
import com.droiddeck.launcher.wayland.WaylandCompositor
class QuestXrActivity : NativeActivity() {
private var steamVrStarted = false
@Volatile private var closing = false
@Volatile private var steamVrStatus = SteamVr.Status.UNAVAILABLE
private var sessionId: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
sessionId = SessionState.sessionId
SessionService.setXrActivityVisible(this, true)
super.onCreate(savedInstanceState)
}
fun onXrActive(active: Boolean) {
if (active) {
check(CompositorHost.isStarted) { "DroidDeck compositor is not running" }
SessionService.resume(this)
WaylandCompositor.nativeSetCardboard(false, 1, 0.064f, 3f, 4f, 0f, 0f, 90f, 0f, 0f, false, 0f)
WaylandCompositor.nativeSetFrameGenArmed(false, 0, 0)
WaylandCompositor.nativeSetZeroCopy(false)
CompositorHost.attachXr()
WaylandCompositor.nativeVsync(System.nanoTime())
check(WaylandCompositor.nativeWaitSurface(3000)) { "XR frame GPU handshake timed out" }
}
SessionService.setXrActivityVisible(this, active)
if (active) startSteamVr()
}
private fun startSteamVr() {
if (!steamVrStarted) {
steamVrStarted = true
Thread({
try {
val started = synchronized(steamVrLock) {
if (closing || !sessionAlive()) false else {
steamVrOwner = this
SteamVr.run(this, "start")
}
}
if (!started) {
if (!closing) leaveXr("SteamVR start failed", sessionAlive())
return@Thread
}
val startupDeadline = SystemClock.elapsedRealtime() + 30_000
var lastRunning: Long? = null
while (!closing) {
if (!sessionAlive()) {
leaveXr("Steam session ended", false)
break
}
steamVrStatus = SteamVr.status(this)
val now = SystemClock.elapsedRealtime()
if (steamVrStatus == SteamVr.Status.RUNNING) lastRunning = now
val deadline = lastRunning?.plus(5_000) ?: startupDeadline
if (steamVrStatus != SteamVr.Status.RUNNING && now >= deadline) {
leaveXr("SteamVR stopped or did not finish starting", true)
break
}
Thread.sleep(1000)
}
} finally {
synchronized(steamVrLock) {
// A retiring activity must not stop a replacement's SteamVR session.
if (steamVrOwner === this) {
if (sessionAlive()) SteamVr.run(this, "stop")
steamVrOwner = null
}
}
}
}, "quest-steamvr-start").start()
}
}
private fun sessionAlive() = SessionState.running && !SessionState.stopRequested &&
sessionId != null && SessionState.sessionId == sessionId
private fun leaveXr(reason: String, failed: Boolean) {
Log.w("DroidDeckXR", reason)
closing = true
runOnUiThread {
if (failed) Toast.makeText(this, R.string.cardboard_steamvr_failed, Toast.LENGTH_LONG).show()
finish()
}
}
fun onXrFrame(frameTimeNanos: Long) {
WaylandCompositor.nativeVsync(frameTimeNanos)
}
fun onXrStatus(): Int = steamVrStatus.ordinal
override fun onDestroy() {
closing = true
SessionService.setXrActivityVisible(this, false)
super.onDestroy()
}
override fun onStop() {
SessionService.setXrActivityVisible(this, false)
super.onStop()
}
companion object {
private val steamVrLock = Any()
private var steamVrOwner: QuestXrActivity? = null
}
}
File diff suppressed because it is too large. Load diff
@@ -18,13 +18,7 @@ import com.droiddeck.launcher.ui.UpdatesState
import com.droiddeck.launcher.update.AppUpdates
import com.droiddeck.launcher.update.SelfInstaller
/**
* The Updates page's state: the channels' latest builds, the one followed, and an update being
* downloaded or installed. Checked when the launcher comes to the front - opening the app, and
* coming back from a session - at most once a minute.
*/
internal class UpdatesMenu(private val activity: Activity, private val ui: Handler) {
// Read in start(): the activity has no context yet while its fields are made.
var catalog by mutableStateOf<AppUpdates.Catalog?>(null)
var follow by mutableStateOf(AppUpdates.Follow(AppUpdates.Channel.STABLE))
var checking by mutableStateOf(false)
@@ -32,7 +26,6 @@ internal class UpdatesMenu(private val activity: Activity, private val ui: Handl
var stage by mutableStateOf<String?>(null)
var percent by mutableIntStateOf(-1)
var askPermission by mutableStateOf(false)
// An install waiting for the user to allow "Install unknown apps"; it goes on when they come back.
private var pending: AppUpdates.Release? = null
private var lastCheck = 0L
private var request = 0
@@ -53,7 +46,6 @@ internal class UpdatesMenu(private val activity: Activity, private val ui: Handl
val hasUpdate: Boolean get() = AppUpdates.hasUpdate(catalog, follow)
/** On every return to the launcher: go on with an install the user just allowed, and check if due. */
fun onResume() {
pending?.let { r ->
if (SelfInstaller.canInstall(activity)) { pending = null; install(r) }
@@ -107,7 +99,6 @@ internal class UpdatesMenu(private val activity: Activity, private val ui: Handl
}, "app-update-install").start()
}
/** PackageInstaller's answer: Android's confirm prompt to show, or why it failed. Success restarts the app. */
private val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (val status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE)) {
@@ -127,7 +118,6 @@ internal class UpdatesMenu(private val activity: Activity, private val ui: Handl
}
}
/** From onCreate: the last check, the channel followed, and PackageInstaller's answers. */
fun start() {
catalog = AppUpdates.cached(activity)
follow = AppUpdates.follow(activity, catalog)
@@ -2,26 +2,16 @@ package com.droiddeck.launcher.agent
import android.content.Context
/**
* Who may run code through the agent bridge. Reading state, starting and stopping sessions,
* launching and quitting games and sending input only need the shell (the provider's DUMP
* permission). Running commands in the guest, evaluating JavaScript in the Steam client, session
* environment, binary overrides and preference writes reach inside the app's sandbox - the guest
* holds the Steam login - which the shell of a stock device otherwise cannot. They require
* Setup > Session > Debugging tools to be on, in every build. The bridge itself cannot turn it on.
*/
object AgentAccess {
const val MAX_REQUEST_CHARS = 256 * 1024
private const val PREFS = "agent"
private const val KEY_COMMANDS = "commands"
/** The preferences file the bridge must never write: set-pref refuses it. */
const val PROTECTED_PREFS = PREFS
fun commandsAllowed(context: Context): Boolean =
prefs(context).getBoolean(KEY_COMMANDS, false)
/** The Setup toggle's own state. */
fun commandsEnabled(context: Context): Boolean = prefs(context).getBoolean(KEY_COMMANDS, false)
fun setCommandsEnabled(context: Context, enabled: Boolean) {
@@ -33,7 +23,7 @@ object AgentAccess {
fun requireCommands(context: Context) {
if (!commandsAllowed(context)) throw AgentException(
"AGENT_COMMANDS_DISABLED",
"Debugging tools are off: turn on Setup > Session > Debugging tools on the device",
"Debugging tools are off: turn on Setup > Diagnostics > Debugging tools on the device",
)
}
@@ -44,5 +34,4 @@ object AgentAccess {
private fun prefs(context: Context) = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
}
/** A refusal with a stable code, answered as {"ok": false, "error": {code, message}}. */
class AgentException(val code: String, message: String) : RuntimeException(message)
@@ -27,14 +27,6 @@ import java.io.File
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
/**
* The shell's control surface (docs/agent-control.md), in every build: the provider and its start
* Activity need android.permission.DUMP, held by the shell and privileged or explicitly granted callers. Commands that
* reach into the app's sandbox are further gated by [AgentAccess].
*
* A method's request is JSON, base64-encoded in the "request" extra; every answer is JSON in the
* "json" key of the returned Bundle, {"ok": true, ...} or {"ok": false, "error": {code, message}}.
*/
class AgentBridgeProvider : ContentProvider() {
override fun onCreate(): Boolean {
val appContext = context?.applicationContext ?: return false
@@ -58,7 +50,6 @@ class AgentBridgeProvider : ContentProvider() {
val response = try {
val request = decode(extras?.getString(EXTRA_REQUEST))
// Binder identity is the shell's; everything below acts as the app.
val token = Binder.clearCallingIdentity()
try {
dispatch(context, method, request)
@@ -101,11 +92,111 @@ class AgentBridgeProvider : ContentProvider() {
AgentEnv.override(context, request.optString("name")).put("ok", true).put("command", "override")
}
"prefs" -> prefs(context, request)
"glasses" -> glasses(context, request)
"access" -> JSONObject().put("ok", true).put("commands", AgentAccess.commandsAllowed(context))
.put("debugBuild", BuildConfig.DEBUG).put("inbox", AgentEnv.inbox(context).absolutePath)
else -> error("UNKNOWN_COMMAND", "Unknown agent command '$method'")
}
private fun glasses(context: Context, request: JSONObject): JSONObject {
val g = com.droiddeck.launcher.glasses.Glasses
when (request.optString("op", "status")) {
"status" -> {}
"allow" -> g.requestControl()
"set" -> {
val key = request.optString("key")
val value = request.optString("value")
fun on() = value == "1" || value.equals("true", true) || value.equals("on", true)
val number = value.toIntOrNull()
val numeric = setOf("refreshHz", "sbsHz", "brightness", "volume", "picture3d", "screenWidthDm", "screenDistanceDm", "vrFovDeg", "sbsSupport", "eyeHeightCm")
if (key in numeric && number == null) return error("INVALID_VALUE", "'$key' takes a number")
when (key) {
"refreshHz" -> g.setRefreshHz(number!!)
"sbsHz" -> g.setSbsHz(number!!)
"resolution2d" -> g.setResolution(com.droiddeck.launcher.glasses.GlassesDisplayProfile.TWO_D, value)
"resolution3d" -> g.setResolution(com.droiddeck.launcher.glasses.GlassesDisplayProfile.THREE_D, value)
"resolutionVr" -> g.setResolution(com.droiddeck.launcher.glasses.GlassesDisplayProfile.VR, value)
"brightness" -> g.setBrightness(number!!)
"volume" -> g.setVolume(number!!)
"sunblock" -> g.setSunblock(on())
"3d" -> g.set3d(on())
"3dof" -> g.set3dof(on())
"gameOnGlasses" -> g.setGameOnGlasses(on())
"headTracking" -> g.setHeadTracking(on())
"auto3d" -> g.setAuto3d(on())
"picture3d" -> g.setPicture3d(number!!)
"screenWidthDm" -> g.setScreenWidthDm(number!!)
"screenDistanceDm" -> g.setScreenDistanceDm(number!!)
"vrFovDeg" -> g.setVrFovDeg(number!!)
"sbsSupport" -> g.setSbsSupport(number!!)
"positionalTracking" -> g.setPositionalTracking(on())
"handControllers" -> g.setHandControllers(on())
"joyConTracking" -> g.setJoyConTracking(on())
"eyeHeightCm" -> g.setEyeHeight(number!!)
else -> return error("INVALID_KEY", "Unknown glasses setting '$key'")
}
}
"simulate" -> {
fun angle(key: String) = if (request.has(key)) request.optDouble(key).takeIf { it.isFinite() }?.coerceIn(-90.0, 90.0) else null
if (!g.simulate(request.optInt("seconds", 20).coerceIn(1, 600), angle("yaw"), angle("pitch"))) return error("BUSY", "Already simulating")
}
"stop-simulate" -> g.stopSimulation()
"calibrate" -> g.startCalibration()
"cancel-calibration" -> g.cancelCalibration()
"reset-calibration" -> g.resetCalibration()
"spatial-diagnostics" -> com.droiddeck.launcher.glasses.SpatialTracking.diagnostics(request.optBoolean("enabled",true))
"spatial-recenter" -> com.droiddeck.launcher.glasses.SpatialTracking.recenter(request.optBoolean("screen",false))
"spatial-floor" -> g.setSpatialFloor()
"spatial-restart" -> g.restartSpatialTracking()
"spatial-hand-options" -> {
AgentAccess.requireCommands(context)
if(request.has("lite")) com.droiddeck.launcher.glasses.SpatialTracking.setHandModel(request.getBoolean("lite"))
if(request.has("parallel")) com.droiddeck.launcher.glasses.SpatialTracking.setHandCpuParallel(request.getBoolean("parallel"))
if(request.has("halfRate")) com.droiddeck.launcher.glasses.SpatialTracking.setHandHalfRate(request.getBoolean("halfRate"))
if(request.has("adaptive")) com.droiddeck.launcher.glasses.SpatialTracking.setHandAdaptiveInput(request.getBoolean("adaptive"))
if(request.has("scheduled")) com.droiddeck.launcher.glasses.SpatialTracking.setHandScheduledTracking(request.getBoolean("scheduled"))
}
"spatial-hand-benchmark" -> {
AgentAccess.requireCommands(context)
com.droiddeck.launcher.glasses.SpatialTracking.benchmarkSavedCapture(request.getString("capture"))
}
"spatial-hand-capture" -> {
AgentAccess.requireCommands(context)
com.droiddeck.launcher.glasses.SpatialTracking.captureDiagnostic()
}
"spatial-rgb-capture", "spatial-rgb-status" -> {
AgentAccess.requireCommands(context)
return (if (request.getString("op") == "spatial-rgb-capture")
com.droiddeck.launcher.glasses.LumaRgbDiagnostic.capture(context!!,request.optString("transport","sdk"),
request.optInt("width",640),request.optInt("height",480),request.optInt("fps",30),
request.optInt("durationMs",5000))
else com.droiddeck.launcher.glasses.LumaRgbDiagnostic.status(context!!)).put("ok", true)
}
"query" -> { g.query(); Thread.sleep(600) }
"mode" -> {
val mode = hex(request.optString("mode"), 0xFF) ?: return error("INVALID_VALUE", "mode must be one hex byte, e.g. 0x31")
g.sendMode(mode)
Thread.sleep(600)
}
"imu" -> return g.sampleImu(request.optLong("ms", 2000).coerceIn(200, 20_000)).put("ok", true).put("command", "glasses")
"raw" -> {
AgentAccess.requireCommands(context)
val msgId = hex(request.optString("msgId"), 0xFFFF) ?: return error("INVALID_VALUE", "msgId must be a hex number up to 0xFFFF")
val digits = request.optString("payload").removePrefix("0x")
if (digits.length % 2 != 0 || digits.any { Character.digit(it, 16) < 0 }) return error("INVALID_VALUE", "payload must be whole hex bytes")
val payload = digits.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
if (!g.sendRaw(msgId, payload)) return error("NOT_CONNECTED", "The glasses' control link is not open")
Thread.sleep(300)
}
else -> return error("INVALID_OP", "Unknown glasses op")
}
if (request.optString("op") in setOf("set", "allow")) Thread.sleep(400)
return g.status().put("ok", true).put("command", "glasses").put("frames", g.recentFrames())
}
private fun hex(text: String, max: Int): Int? =
text.removePrefix("0x").removePrefix("0X").takeIf { it.isNotEmpty() }?.toIntOrNull(16)?.takeIf { it in 0..max }
private fun state(context: Context): JSONObject {
val runtimeVersion = LinuxRuntimeInstaller.installedVersion(context)
val dir = SessionState.logDirectory ?: SessionState.logFile?.parentFile ?: SessionPaths.sessionFolders(context).lastOrNull()
@@ -118,6 +209,7 @@ class AgentBridgeProvider : ContentProvider() {
.put("steamUi", SessionState.steamUi ?: JSONObject.NULL)
.put("steamUrl", SessionState.steamUrl ?: JSONObject.NULL)
.put("suspended", SessionState.suspended)
.put("steamVr", SessionState.steamVr)
.put("pip", SessionState.pipActive)
.put("firstFrame", SessionState.firstFrameSeen)
.put("output", JSONArray().put(SessionState.outputSize.first).put(SessionState.outputSize.second))
@@ -136,8 +228,6 @@ class AgentBridgeProvider : ContentProvider() {
.put("status", SessionState.failureStatus ?: JSONObject.NULL)
session.put("failure", failure)
// gamescope's focus as last published by the guest agent, without the window lists
// (the focus command returns those, fresh).
val focus = AgentGuest.focus(context)?.let { snap ->
JSONObject()
.put("focusedApp", snap.opt("focusedApp") ?: JSONObject.NULL)
@@ -225,11 +315,9 @@ class AgentBridgeProvider : ContentProvider() {
return result.put("ok", true).put("command", "prefs")
}
/** A guest agent request; its own answer already carries ok or error. */
private fun guest(context: Context, request: JSONObject, timeoutMs: Long): JSONObject =
AgentGuest.call(context, request, timeoutMs)
/** The guest's own timeout plus slack for the round trip. */
private fun timeoutMs(request: JSONObject, defaultSeconds: Double, slackMs: Long): Long {
val seconds = if (request.has("timeout")) request.opt("timeout")?.toString()?.toDoubleOrNull()
?: throw AgentException("INVALID_REQUEST", "timeout must be a number") else defaultSeconds
@@ -262,7 +350,6 @@ class AgentBridgeProvider : ContentProvider() {
companion object {
const val RESULT_JSON = "json"
const val EXTRA_REQUEST = "request"
/** 2 added agent and session.focus; everything in 1 is unchanged. */
const val SCHEMA = 2
private val recoveryComplete = CountDownLatch(1)
@@ -10,18 +10,6 @@ import java.nio.file.Files
import java.nio.file.LinkOption.NOFOLLOW_LINKS
import java.nio.file.StandardOpenOption.READ
/**
* Experiment switches an agent sets without touching Download/droiddeck-env or rebuilding the apk:
*
* - environment lines (KEY=VALUE) for the session, applied after the user's own droiddeck-env, so
* they win. "next" lines are taken by the next session that starts and dropped when it ends;
* "persistent" ones stay until cleared.
* - binary overrides: a file pushed to the app's external files (agent-inbox/<name>) is installed
* as /opt/droiddeck-agent/bin/<name> in the rootfs and put first on the next session's PATH -
* a gamescope build under test, say. Removed when that session ends.
*
* Kept in the app's own files (agent-env.json), never in a place the shell can write.
*/
object AgentEnv {
private const val TAG = "AgentEnv"
private const val FILE = "agent-env.json"
@@ -38,7 +26,6 @@ object AgentEnv {
load(context)
}
/** Add [lines] for the next session ([persistent] = every session until cleared). */
fun set(context: Context, lines: List<String>, persistent: Boolean): JSONObject = synchronized(lock) {
AgentAccess.requireCommands(context)
if (lines.any { line -> KEY.matchEntire(line.substringBefore('=', "")) == null || !line.contains('=') || line.any { it == '\n' || it == '\r' || it == '\u0000' } }) {
@@ -52,7 +39,6 @@ object AgentEnv {
state
}
/** Drop lines: [scope] is "next", "persistent" or "all". */
fun clear(context: Context, scope: String): JSONObject = synchronized(lock) {
AgentAccess.requireCommands(context)
val state = load(context)
@@ -66,7 +52,6 @@ object AgentEnv {
state
}
/** Install agent-inbox/[name] as an override for the next session. */
fun override(context: Context, name: String): JSONObject = synchronized(lock) {
AgentAccess.requireCommands(context)
if (NAME.matchEntire(name) == null || name in setOf(".", "..")) throw AgentException("INVALID_NAME", "Override names are letters, digits, '.', '_' and '-'")
@@ -93,10 +78,6 @@ object AgentEnv {
state.put("installed", "$GUEST_BIN/$name")
}
/**
* At session start: the lines this session gets, after the user's droiddeck-env. Pending lines
* and overrides become this session's own, to be dropped by [endSession].
*/
fun beginSession(context: Context): List<String> = synchronized(lock) {
if (!AgentAccess.commandsAllowed(context)) {
revoke(context)
@@ -108,7 +89,6 @@ object AgentEnv {
state.put("active", JSONArray(active)).put("pending", JSONArray())
state.put("activeOverrides", JSONArray(overrides)).put("pendingOverrides", JSONArray())
save(context, state)
// Anything else there was left by a session that never reached endSession (the app died).
bin(context).listFiles()
?.filter { it.name !in overrides }?.forEach { it.delete() }
val lines = strings(state.optJSONArray("persistent")) + active
@@ -124,7 +104,6 @@ object AgentEnv {
bin(context).listFiles()?.forEach { it.delete() }
}
/** At session end: this session's lines and override binaries go. */
fun endSession(context: Context) = synchronized(lock) {
val state = load(context)
val overrides = strings(state.optJSONArray("activeOverrides"))
@@ -135,7 +114,6 @@ object AgentEnv {
save(context, state)
}
/** A PATH= line among [lines] (the user's or an agent's) is extended rather than replaced. */
private fun pathOf(lines: List<String>) =
lines.lastOrNull { it.startsWith("PATH=") }?.substringAfter('=')?.takeIf { it.isNotBlank() } ?: BASE_PATH
@@ -178,7 +156,6 @@ object AgentEnv {
}
}
/** Typed writes to the app's SharedPreferences, for driving a settings matrix without the UI. */
object AgentPrefs {
fun set(context: Context, file: String, key: String, type: String, value: String?): JSONObject {
AgentAccess.requireCommands(context)
@@ -1,6 +1,11 @@
package com.droiddeck.launcher.agent
import android.content.Context
import android.os.Handler
import android.os.Looper
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.setValue
import android.os.FileObserver
import android.util.Log
import com.droiddeck.launcher.runtime.LinuxRuntime
@@ -10,12 +15,10 @@ import org.json.JSONObject
import java.io.File
import java.util.UUID
/**
* The app's side of the guest agent (tools/linuxfs/overlay/usr/local/bin/droiddeck-agent): requests
* go in as files under the session root's agent directory and answers come back the same way, so
* whatever runs does so inside the live session with its own environment.
*/
object AgentGuest {
private val main by lazy { Handler(Looper.getMainLooper()) }
var focusRevision by mutableIntStateOf(0)
private set
private const val TAG = "AgentGuest"
private const val POLL_MS = 50L
private val lock = Any()
@@ -24,11 +27,6 @@ object AgentGuest {
fun dir(context: Context) = File(LinuxRuntime.sessionRoot(context), "agent")
/**
* Before each session: nothing of the last one's daemon, answers or focus may be read as
* current. Then watch for the new daemon's hello and focus snapshots, which go into the
* session's events.jsonl.
*/
fun reset(context: Context) {
synchronized(lock) {
observer?.stopWatching()
@@ -51,7 +49,6 @@ object AgentGuest {
}
}
/** At the end of a session: no more events for a folder that is being closed. */
fun stop() {
synchronized(lock) {
observer?.stopWatching()
@@ -70,10 +67,10 @@ object AgentGuest {
if (key == lastFocusKey) return
lastFocusKey = key
}
main.post { focusRevision++ }
SessionEvents.record("focus.changed", fields + ("seq" to snapshot.optInt("seq")))
}
/** The daemon's hello, when it is running in the current session. */
fun hello(context: Context): JSONObject? {
if (!SessionState.running) return null
val hello = readJson(File(dir(context), "hello.json")) ?: return null
@@ -81,13 +78,70 @@ object AgentGuest {
return hello.takeIf { pid > 1 && File("/proc/$pid").exists() }
}
/** The newest focus snapshot the daemon published, when a session is running. */
fun focus(context: Context): JSONObject? =
if (SessionState.running) readJson(File(dir(context), "focus.json")) else null
/** Send one request and wait for its answer. Throws [AgentException] with a stable code. */
fun call(context: Context, request: JSONObject, timeoutMs: Long): JSONObject {
if (request.optString("kind") in setOf("exec", "cdp")) AgentAccess.requireCommands(context)
fun selectedSteamAppId(context: Context): Long? {
val result = call(context, JSONObject().put("kind", "steam-selection"), 6_000)
if (!result.optBoolean("ok") || result.isNull("appId")) return null
return result.optLong("appId").takeIf { it in 1..4_294_967_295L }
}
fun activeSteamAppId(context: Context, installedAppIds: Set<Long>): Long? {
val snapshot = focus(context) ?: return null
val focusedAppId = snapshot.optLong("focusedApp").takeIf { snapshot.has("focusedApp") }
val focusableAppIds = snapshot.optJSONArray("focusableApps")?.let { apps ->
buildList {
for (index in 0 until apps.length()) {
apps.optLong(index).takeIf { it > 0 }?.let(::add)
}
}
}.orEmpty()
val baselayerAppIds = snapshot.optJSONArray("baselayerAppIds")?.let { apps ->
buildList {
for (index in 0 until apps.length()) {
apps.optLong(index).takeIf { it > 0 }?.let(::add)
}
}
}.orEmpty()
return activeInstalledSteamAppId(focusedAppId, focusableAppIds, installedAppIds, baselayerAppIds)
}
internal fun focusedInstalledSteamAppId(focusedAppId: Long?, installedAppIds: Set<Long>): Long? =
focusedAppId?.takeIf { it in installedAppIds }
internal fun activeInstalledSteamAppId(
focusedAppId: Long?,
focusableAppIds: List<Long>,
installedAppIds: Set<Long>,
baselayerAppIds: List<Long> = emptyList(),
): Long? = focusedInstalledSteamAppId(focusedAppId, installedAppIds)
?: focusableAppIds.filter { it in installedAppIds }.distinct().singleOrNull()
?: baselayerAppIds.filter { it in installedAppIds }.distinct().singleOrNull()
internal fun preferredInstalledSteamAppId(
selectedAppId: Long?,
activeAppId: Long?,
installedAppIds: Set<Long>,
): Long? = selectedAppId?.takeIf { it in installedAppIds }
?: activeAppId?.takeIf { it in installedAppIds }
internal fun selectedOrActiveInstalledSteamAppId(
selectedAppId: Result<Long?>,
activeAppId: Long?,
installedAppIds: Set<Long>,
): Long? {
preferredInstalledSteamAppId(selectedAppId.getOrNull(), activeAppId, installedAppIds)?.let { return it }
selectedAppId.exceptionOrNull()?.let { throw it }
return null
}
fun steamEval(context: Context, expression: String, timeoutMs: Long): JSONObject =
call(context, JSONObject().put("kind", "cdp").put("expression", expression).put("timeout", timeoutMs / 1000.0),
timeoutMs + 2_000, trusted = true)
fun call(context: Context, request: JSONObject, timeoutMs: Long, trusted: Boolean = false): JSONObject {
if (!trusted && request.optString("kind") in setOf("exec", "cdp")) AgentAccess.requireCommands(context)
if (!SessionState.running) throw AgentException("NO_ACTIVE_SESSION", "There is no running session")
hello(context) ?: throw AgentException("AGENT_UNAVAILABLE", "The guest agent is not running in this session (yet)")
val dir = dir(context)
@@ -5,13 +5,7 @@ import com.droiddeck.launcher.session.SessionState
import com.droiddeck.launcher.wayland.WaylandCompositor
import org.json.JSONObject
/**
* Input into the running session the way a player gives it: keys and touches through the
* compositor (what the session screen sends), and the virtual pad through the same PadBridge the
* on-screen controls drive, so games that ignore touch still see a controller.
*/
object AgentInput {
/** Out of the way of real fingers, which Android numbers from 0. */
private const val TOUCH_ID = 9
private const val TOUCH_SCALE_X = 1919f
private const val TOUCH_SCALE_Y = 1079f
@@ -56,7 +50,6 @@ object AgentInput {
return JSONObject().put("code", code)
}
/** Typed as key presses (US layout), which reaches X programs that have no text-input support. */
private fun text(request: JSONObject): JSONObject {
val text = request.optString("text")
if (request.optBoolean("commit")) {
@@ -99,11 +92,6 @@ object AgentInput {
return JSONObject().put("from", org.json.JSONArray().put(x1).put(y1)).put("to", org.json.JSONArray().put(x2).put(y2))
}
/**
* A point in the compositor's touch space (0..1919 x 0..1079 whatever the session's size), from
* the session's output pixels (space "output", the default: what the guest draws at) or 0..1
* fractions (space "fraction").
*/
private fun point(request: JSONObject, xKey: String, yKey: String): Pair<Int, Int> {
if (!request.has(xKey) || !request.has(yKey)) throw AgentException("INVALID_INPUT", "$xKey and $yKey are required")
val x = request.getDouble(xKey)
@@ -183,7 +171,6 @@ object AgentInput {
"guide" to PadState.GUIDE, "steam" to PadState.GUIDE,
)
/** Linux evdev codes (input-event-codes.h) by name. */
private val KEYS: Map<String, Int> = buildMap {
"1234567890".forEachIndexed { i, c -> put(c.toString(), 2 + i) }
"QWERTYUIOP".forEachIndexed { i, c -> put(c.toString(), 16 + i) }
@@ -202,7 +189,6 @@ object AgentInput {
))
}
/** US-layout characters as (evdev code, shift). */
private val TYPED: Map<Char, Pair<Int, Boolean>> = buildMap {
for (c in 'a'..'z') put(c, KEYS.getValue(c.uppercase()) to false)
for (c in 'A'..'Z') put(c, KEYS.getValue(c.toString()) to true)
@@ -10,7 +10,6 @@ import com.droiddeck.launcher.session.SessionService
import org.json.JSONArray
import org.json.JSONObject
/** Shell-launched trampoline: Android permits the shell to start this protected debug Activity. */
class AgentStartActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -98,6 +97,7 @@ class AgentStartActivity : Activity() {
request.optString("steamUrl").takeIf { it.startsWith("steam://") }?.let {
putExtra(SessionService.EXTRA_STEAM_URL, it)
}
if (request.optBoolean("steamVr")) putExtra(SessionActivity.EXTRA_STEAMVR, true)
}
}
startActivity(sessionIntent)
@@ -19,16 +19,8 @@ import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicReference
/**
* The app's own UI as an agent sees it: every Compose node in every window of the process (popups
* and menus included), read from Compose's semantics rather than through UI Automator, which on a
* dual-screen device dumps whichever window has focus - often not this app's. Nodes carry their
* test tag (ui/TestTags.kt), text, content description, role, state and on-screen bounds; click
* and set-text act on a node by tag or text, so a test needs no coordinates.
*/
object AgentUi {
private const val TIMEOUT_MS = 5_000L
/** Setup's Debugging tools toggle: the bridge never acts on its own gate (AgentAccess). */
private const val GATE_TAG = "setting-agent-commands"
fun handle(request: JSONObject): JSONObject = when (val op = request.optString("op", "dump")) {
@@ -106,10 +98,6 @@ object AgentUi {
return json
}
/**
* The node a request names by "tag" (exact) or "match" (text or description containing it,
* ignoring case); "index" picks among several, in tree order.
*/
private fun act(request: JSONObject, action: (SemanticsNode) -> Unit): JSONObject {
val tag = request.optString("tag").takeIf { it.isNotEmpty() }
val text = if (tag == null) request.optString("match").takeIf { it.isNotEmpty() } else null
@@ -9,34 +9,12 @@ import com.droiddeck.launcher.core.HostProcess;
import java.io.File;
import java.util.ArrayList;
/**
* The DirectAudio relay helper, which owns Android's speaker and microphone on behalf of a Linux
* session.
*
* <p>Nothing inside the session can open Android's audio itself. A game is a glibc process in a
* rootfs that binds no {@code /system}, and a glibc process cannot call libaaudio at all; the
* session is also never given a way to spawn a bionic helper of its own. So the helper runs out
* here, under the app's own uid, and the driver inside Wine talks to it over a unix socket. The uid
* matters for more than tidiness: Android checks the microphone permission against whoever opens
* the stream, so a helper started any other way could play but never record.
*
* <p>It is executed from the app's native library directory because Android will not execute a
* binary from anywhere else - the same reason the PulseAudio daemon ships as {@code
* libpulseaudio.so} despite being a program rather than a library.
*
* <p>Started before the client, because the driver asks once whether a helper is there, when Wine
* picks an audio driver, and reports itself unavailable if not. Ported from Bannerlator (GPL-3.0);
* the helper binary is the one built by The412Banner/directaudio's relay workflow.
*/
public class DirectAudioRelayComponent extends SessionPart {
private static final String TAG = "DirectAudioRelay";
/** The helper, named as a library so Android will run it. */
public static final String BINARY = "libdirectaudiorelay.so";
private final File socketPath;
/** Shared with PulseAudioComponent: one audio.log per session, appended by both. */
private File logFile;
/** Null when the microphone was not asked for; the helper then opens no input stream. */
private final File micFifoPath;
private volatile int pid = -1;
@@ -45,7 +23,6 @@ public class DirectAudioRelayComponent extends SessionPart {
this.micFifoPath = micFifoPath;
}
/** Send the helper's output to this file as well as logcat. Set before {@link #start()}. */
public void setLogFile(File file) {
this.logFile = file;
}
@@ -60,21 +37,10 @@ public class DirectAudioRelayComponent extends SessionPart {
}
File parent = socketPath.getParentFile();
if (parent != null) {
//noinspection ResultOfMethodCallIgnored
parent.mkdirs();
}
// A socket left by a session that did not shut down cleanly is not a listener, and the
// helper would fail to bind onto it.
//noinspection ResultOfMethodCallIgnored
socketPath.delete();
// PulseAudio's pipe module insists on creating the pipe itself and fails if one is already
// there. The helper creates one too when the path is missing, and it starts a few
// milliseconds after the daemon is exec'd - well before the daemon has read its config. So
// left to itself the helper won its own race, the daemon's mkfifo failed with EEXIST, and
// the microphone was lost while everything else came up. Wait for the daemon to make the
// pipe before the helper runs; it appears within a second, and if it never does the helper
// starts anyway and creates it, which is the right thing when the daemon is not involved.
if (micFifoPath != null) {
long deadline = System.currentTimeMillis() + 3000L;
while (!micFifoPath.exists() && System.currentTimeMillis() < deadline) {
@@ -91,12 +57,8 @@ public class DirectAudioRelayComponent extends SessionPart {
StringBuilder command = new StringBuilder(binary.getAbsolutePath());
command.append(" --socket ").append(socketPath.getAbsolutePath());
// Diagnostics into the session's audio.log: every 1000 callbacks the device buffer,
// Android's own xrun count and the ring's fill, plus every buffer change.
command.append(" --log");
if (micFifoPath != null) {
// The helper creates the pipe if it is not there. PulseAudio is told the same path and
// reads from the other end of it.
command.append(" --mic-fifo ").append(micFifoPath.getAbsolutePath());
}
ArrayList<String> env = new ArrayList<>();
@@ -126,8 +88,6 @@ public class DirectAudioRelayComponent extends SessionPart {
@Override
public void stop() {
if (pid != -1) {
// The helper exits on a signal and tears down its streams; there is nothing below it to
// orphan, unlike the proot tree.
Process.killProcess(pid);
pid = -1;
}
@@ -17,30 +17,15 @@ import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
/**
* PulseAudio 13.0 running in the app process, with Android's AAudio as its sink, so the guest's
* libpulse clients (Steam and everything it launches) have a server to talk to. The socket lives
* in the app's files directory and is bound into the session at its own path, so PULSE_SERVER
* needs no translating.
*/
public class PulseAudioComponent extends SessionPart {
private static final String TAG = "PulseAudio";
/** Where the guest reaches the daemon; the session exports PULSE_SERVER=unix:<this>. */
public static final String SOCKET_NAME = "PS0";
/** Identifies the bundled pulseaudio.tzst; a change here re-unpacks it over what a device has. */
private static final String BUNDLE_STAMP = "2026-10-08-pa13-directaudio-native";
private final File workingDir;
/** Where the daemon's own output is kept for this session, or null for logcat only. */
private File logFile;
/**
* A named pipe carrying microphone audio, or null for no microphone. The bundle ships
* module-aaudio-sink but no matching source, and the daemon runs where Android permits
* recording - so pointing module-pipe-source at a pipe the DirectAudio relay helper writes
* turns that one stream into a source the client can see, named DirectAudioMic.
*/
private final String micFifoPath;
private volatile int pid = -1;
@@ -48,13 +33,11 @@ public class PulseAudioComponent extends SessionPart {
this(context, null);
}
/** As above, with a microphone fed from {@code micFifoPath}; null for output only. */
public PulseAudioComponent(Context context, String micFifoPath) {
this.workingDir = new File(context.getFilesDir(), "pulseaudio");
this.micFifoPath = micFifoPath;
}
/** CRC-32 of the bundled pulseaudio.tzst, or "?" if it cannot be read. About 600 KB; cheap. */
private String bundleChecksum() {
java.util.zip.CRC32 crc = new java.util.zip.CRC32();
byte[] buf = new byte[64 * 1024];
@@ -67,7 +50,6 @@ public class PulseAudioComponent extends SessionPart {
return Long.toHexString(crc.getValue());
}
/** Send the daemon's output to this file as well as logcat. Set before {@link #start()}. */
public void setLogFile(File file) {
this.logFile = file;
}
@@ -80,18 +62,9 @@ public class PulseAudioComponent extends SessionPart {
public void start() {
stop();
if (!workingDir.isDirectory()) {
//noinspection ResultOfMethodCallIgnored
workingDir.mkdirs();
FileUtils.chmod(workingDir, 0771);
}
// The loadable modules (module-aaudio-sink - the app's own, from tools/aaudio-sink - the
// native protocol, the pipe modules) ride in the apk; the daemon and its libraries come from the native library directory, the one
// place an app may execute a file from. The bundle is unpacked once per BUNDLE_STAMP, not
// once ever: an installed app kept the modules it unpacked on its first run, so a bundle
// fixed in a later build never reached the device - which is how a 17.0 glibc build of
// module-pipe-source sat beside the 13.0 daemon, failed to dlopen, and the microphone
// never appeared. The stamp carries a checksum of the bundle itself, so any change to it -
// a rebuilt sink module included - re-unpacks, whether or not BUNDLE_STAMP was bumped.
File modulesDir = new File(workingDir, "modules");
File stamp = new File(modulesDir, ".bundle");
String have = FileUtils.readString(stamp);
@@ -112,38 +85,18 @@ public class PulseAudioComponent extends SessionPart {
if (pactl.isFile()) FileUtils.chmod(pactl, 0771);
copyFromLibraryDir();
//noinspection ResultOfMethodCallIgnored
socket().delete();
// module-pipe-source creates the pipe with mkfifo and fails outright if one is already
// there - EEXIST, reported as "Unknown error 17" - and the module then does not load at
// all, so the source never appears and the client reports no microphone. Ours lives in the
// app's files directory and survives a session, so after the very first run the path would
// always be occupied. Removed here, before the daemon reads this config: the daemon makes
// it, and the relay helper starts afterwards and is content to find one already made.
if (micFifoPath != null && !micFifoPath.isEmpty()) {
//noinspection ResultOfMethodCallIgnored
new File(micFifoPath).delete();
}
ArrayList<String> config = new ArrayList<>();
config.add("load-module module-native-protocol-unix auth-anonymous=1 auth-cookie-enabled=0 socket=\""
+ socket().getAbsolutePath() + "\"");
// volume=1.0 is not optional: with no volume argument module-aaudio-sink defaults
// the sink to 0% and the session plays silence.
// The sink's name is what the client's Audio settings show as the output device, so it
// says which road the sound takes.
// The client's sound: DirectAudio's own engine, in this process, one step from Android.
// decay=0: shrinking the device buffer after a quiet spell probes below what works and that
// probe is a click; the client plays menus. Between .nofail and .fail a module that cannot
// open its stream does not take the daemon down; ensureClientSink() then loads the plain
// AAudio sink in its place.
config.add(".nofail");
config.add("load-module module-directaudio-native-sink sink_name=DirectAudio performance_mode=1 adaptive=1 decay=0 limiter=1 watchdog=1 volume=1.0");
config.add("set-default-sink DirectAudio");
config.add(".fail");
if (micFifoPath != null && !micFifoPath.isEmpty()) {
// The format is the helper's, fixed at s16le/48000/mono: it resamples when the device
// grants another input rate, so the daemon is never told a rate the bytes are not.
// A pipe has no clock, so nothing here corrects drift - acceptable for voice.
config.add("load-module module-pipe-source source_name=DirectAudioMic file=\""
+ micFifoPath + "\" format=s16le rate=48000 channels=1");
config.add("set-default-source DirectAudioMic");
@@ -159,12 +112,7 @@ public class PulseAudioComponent extends SessionPart {
String command = workingDir.getAbsolutePath() + "/libpulseaudio.so"
+ " --system=false --disable-shm=true --fail=false"
+ " -n --file=default.pa --daemonize=false --use-pid-file=false --exit-idle-time=-1"
// Info level: the classic sink reports the stream Android granted (burst, capacity,
// starting buffer) only there, and that is what a stutter report needs.
+ " --log-level=info";
// A module that refuses to load, a pipe that could not be made, the daemon exiting at
// startup: all of it used to reach logcat and nothing else, so a user's folder said nothing
// at all about sound. Four separate faults hid behind "no input device" in one night.
final java.io.PrintWriter out = openLog();
pid = HostProcess.start(command, env.toArray(new String[0]), workingDir, null,
line -> {
@@ -176,11 +124,6 @@ public class PulseAudioComponent extends SessionPart {
guard.start();
}
/**
* The fallback: once the daemon answers, if the DirectAudio sink is not
* there (the module failed to load or to open its stream), load the plain AAudio sink so the
* client is never left without an output. Says so in the audio log either way.
*/
private void ensureClientSink(java.io.PrintWriter out) {
try {
long deadline = System.currentTimeMillis() + 15000L;
@@ -208,7 +151,6 @@ public class PulseAudioComponent extends SessionPart {
}
}
/** Runs pactl against this daemon; its stdout, or null when it failed or timed out. */
private String pactl(String... args) {
File pactl = new File(workingDir, "pactl");
if (!pactl.isFile() || pid <= 1) return null;
@@ -298,7 +240,6 @@ public class PulseAudioComponent extends SessionPart {
}
}
/** The session's audio log, appended to by both the daemon and the relay helper. */
private java.io.PrintWriter openLog() {
if (logFile == null) return null;
try {
@@ -6,31 +6,20 @@ import android.content.res.Configuration
import android.os.LocaleList
import java.util.Locale
/**
* The language the app shows itself in.
*
* Nothing chosen (the default) means the system's: Android's own language, or the per-app language
* Android 13+ lets a user set for DroidDeck in its settings, exactly as Android resolves it. Picking
* one in Setup overrides that until the user picks "System default" again. Each activity and service
* applies the choice to its own context ([wrap], [applyTo]), so it works on every Android version
* the app runs on rather than only where the platform's per-app language API exists.
*
* Steam follows the same choice: [SteamLanguage] turns [effective] into the client's language.
*/
object AppLanguage {
/** The stored value meaning "follow the system". */
const val SYSTEM = ""
/** The languages the app ships, as BCP 47 tags, in the order the picker lists them. */
val supported: List<String> = listOf("en", "es", "fr", "ru", "ja", "ko", "zh-CN", "zh-TW", "zh-HK")
val supported: List<String> = listOf(
"en", "id", "ms", "cs", "da", "de", "es", "es-419", "fr", "it", "hu", "nl", "nb", "pl",
"pt-BR", "pt-PT", "ro", "fi", "sv", "vi", "tr", "el", "bg", "ru", "uk", "ar", "hi", "th",
"ja", "ko", "zh-CN", "zh-TW", "zh-HK",
)
private const val PREFS = "language"
private const val KEY = "app_language"
/** The locale Android gave the process before any choice here was applied. */
@Volatile private var systemLocale: Locale? = null
/** The stored choice: one of [supported], or [SYSTEM]. */
fun chosen(context: Context): String =
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getString(KEY, SYSTEM)
?.takeIf { it in supported } ?: SYSTEM
@@ -40,7 +29,6 @@ object AppLanguage {
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit().putString(KEY, value).commit()
val locale = value.takeIf { it.isNotEmpty() }?.let(Locale::forLanguageTag) ?: system(context)
Locale.setDefault(locale)
// Code that reads strings through the application context sees the new language too.
val app = context.applicationContext
@Suppress("DEPRECATION")
app.resources.updateConfiguration(
@@ -49,24 +37,16 @@ object AppLanguage {
)
}
/** The locale Android chose for the app, ignoring the choice made here. */
fun system(context: Context): Locale =
systemLocale ?: context.resources.configuration.locales[0] ?: Locale.getDefault()
/** The locale the app is shown in: the chosen language, or else the system's. */
fun effective(context: Context): Locale =
chosen(context).takeIf { it.isNotEmpty() }?.let(Locale::forLanguageTag) ?: system(context)
/** Remembers the system's locale; call with the untouched base context, before [wrap]. */
fun noteSystem(base: Context) {
if (systemLocale == null) systemLocale = base.resources.configuration.locales[0]
}
/**
* [base], showing the chosen language: for an Application's or a Service's attachBaseContext.
* Only the locale is overridden, so screen size, density and night mode keep following the
* device as they change.
*/
fun wrap(base: Context): Context {
noteSystem(base)
val tag = chosen(base).ifEmpty { return base }
@@ -75,11 +55,6 @@ object AppLanguage {
return base.createConfigurationContext(override(locale))
}
/**
* Applies the chosen language to an activity; call from its attachBaseContext, after super.
* applyOverrideConfiguration keeps the locale through rotation and window resizes, which an
* activity here handles itself rather than being recreated.
*/
fun applyTo(activity: Activity, base: Context) {
noteSystem(base)
val tag = chosen(base).ifEmpty { return }
@@ -88,18 +63,23 @@ object AppLanguage {
activity.applyOverrideConfiguration(override(locale))
}
/** Follows a change of the system's language while the process lives (Application.onConfigurationChanged). */
fun systemChanged(config: Configuration) {
config.locales[0]?.let { systemLocale = it }
}
// A new Configuration's fontScale is 1, which as an override would undo the user's font size;
// 0 leaves it undefined, so only the locale is changed.
private fun override(locale: Locale) = Configuration().apply { fontScale = 0f; setLocales(LocaleList(locale)) }
/** Which of the app's languages [locale] reads as, or null when the app does not ship it. */
private val latinAmerica = setOf(
"419", "AR", "BO", "BR", "BZ", "CL", "CO", "CR", "CU", "DO", "EC", "GT", "HN", "MX", "NI", "PA",
"PE", "PR", "PY", "SV", "US", "UY", "VE",
)
fun supportedTag(locale: Locale): String? = when (locale.language) {
"en", "es", "fr", "ru", "ja", "ko" -> locale.language
"en", "fr", "ru", "ja", "ko", "ms", "cs", "da", "de", "it", "hu", "nl", "nb", "pl", "ro", "fi",
"sv", "vi", "tr", "el", "bg", "uk", "ar", "hi", "th" -> locale.language
"in", "id" -> "id"
"es" -> if (locale.country in latinAmerica) "es-419" else "es"
"pt" -> if (locale.country.isEmpty() || locale.country == "BR") "pt-BR" else "pt-PT"
"zh" -> when {
locale.script == "Hans" -> "zh-CN"
locale.country == "HK" || locale.country == "MO" -> "zh-HK"
@@ -109,16 +89,38 @@ object AppLanguage {
else -> null
}
/** [locale]'s language named in itself, without its region unless the app tells them apart. */
fun displayName(locale: Locale): String =
supportedTag(locale)?.let(::nativeName) ?: locale.getDisplayLanguage(locale).replaceFirstChar { it.titlecase(locale) }
/** A language's name in that language, for the picker: what a reader of it looks for. */
fun nativeName(tag: String): String = when (tag) {
"en" -> "English"
"es" -> "Español"
"id" -> "Bahasa Indonesia"
"ms" -> "Bahasa Melayu"
"cs" -> "Čeština"
"da" -> "Dansk"
"de" -> "Deutsch"
"es" -> "Español (España)"
"es-419" -> "Español (Latinoamérica)"
"fr" -> "Français"
"it" -> "Italiano"
"hu" -> "Magyar"
"nl" -> "Nederlands"
"nb" -> "Norsk bokmål"
"pl" -> "Polski"
"pt-BR" -> "Português (Brasil)"
"pt-PT" -> "Português (Portugal)"
"ro" -> "Română"
"fi" -> "Suomi"
"sv" -> "Svenska"
"vi" -> "Tiếng Việt"
"tr" -> "Türkçe"
"el" -> "Ελληνικά"
"bg" -> "Български"
"ru" -> "Русский"
"uk" -> "Українська"
"ar" -> "العربية"
"hi" -> "हिन्दी"
"th" -> "ไทย"
"ja" -> "日本語"
"ko" -> "한국어"
"zh-CN" -> "简体中文"
@@ -3,18 +3,9 @@ package com.droiddeck.launcher.core;
import java.io.File;
import java.io.IOException;
/**
* Where an archive entry may land. An entry name ("../x", "/etc/x") or a symlink already written
* by an earlier entry can point outside the directory being filled; the canonical path follows
* both, so anything that does not resolve inside it is refused.
*/
public final class ArchivePaths {
private ArchivePaths() {}
/**
* The file for {@code name} under {@code destination}, or null when it would resolve outside
* it. The destination itself counts as inside (a "./" entry).
*/
public static File inside(File destination, String name) throws IOException {
String base = destination.getCanonicalPath();
File file = new File(destination, name);
@@ -0,0 +1,27 @@
package com.droiddeck.launcher.core
import com.droiddeck.launcher.gpu.GpuInfo
import com.droiddeck.launcher.gpu.SocNames
object CpuAllocationDefaults {
data class Allocation(val client: String, val game: String)
val device: Allocation? by lazy {
forDevice(SocNames.model(), GpuInfo.systemProperty("ro.board.platform"),
CpuCores.all.associateWith(CpuCores::maxGhz))
}
fun forDevice(model: String, platform: String, frequencies: Map<Int, Double?>): Allocation? {
val chip = model.trim().uppercase().substringBefore('-')
val gen2 = chip in setOf("SM8550", "QCS8550") ||
(chip.isEmpty() && platform.trim().equals("kalama", ignoreCase = true))
if (!gen2 || frequencies.size != 8 || frequencies.keys.any { it < 0 } ||
frequencies.values.any { it == null || !it.isFinite() || it <= 0.0 }) return null
val clusters = frequencies.entries.groupBy { it.value }.toSortedMap(compareBy { it ?: 0.0 }).values.toList()
if (clusters.map { it.size } != listOf(3, 4, 1)) return null
return Allocation(
client = CpuCores.format(clusters[2].map { it.key }.toSet()),
game = CpuCores.format(clusters[1].map { it.key }.toSet()),
)
}
}
@@ -2,17 +2,9 @@ package com.droiddeck.launcher.core
import java.io.File
/**
* The device's cores as taskset names them, for the two masks a Steam session can carry.
*
* A list is stored the way taskset's `-c` takes it - comma-separated core numbers - so what the
* dialog produces is what the session script and the Proton wrappers consume unchanged.
*/
object CpuCores {
/** Core numbers present on this device, in order. */
val all: List<Int> by lazy { (0 until Runtime.getRuntime().availableProcessors()).toList() }
/** The core's ceiling in GHz for the dialog's labels, or null where the kernel hides it. */
fun maxGhz(core: Int): Double? =
FileUtils.readString(File("/sys/devices/system/cpu/cpu$core/cpufreq/cpuinfo_max_freq"))
?.trim()?.toLongOrNull()?.let { it / 1_000_000.0 }
@@ -22,18 +14,14 @@ object CpuCores {
fun format(cores: Set<Int>): String = cores.sorted().joinToString(",")
/** The stored list, or every core when nothing (or nothing valid) is stored. */
fun listOrAll(list: String): String {
val cores = parse(list)
return format(if (cores.isEmpty()) all.toSet() else cores)
}
/**
* The list only when it is a real restriction, else "". Empty, absent, or naming every core
* all mean "no preference": pinning a process to all cores is what the scheduler does
* unaided, and passing it would only put a meaningless line in the session log. Bannerlator's
* rule, kept for the game mask.
*/
fun gameLaunchList(list: String, clientPinned: Boolean): String =
restrictionOrEmpty(list).ifEmpty { if (clientPinned) listOrAll("") else "" }
fun restrictionOrEmpty(list: String): String {
val cores = parse(list)
return if (cores.isEmpty() || cores.size >= all.size) "" else format(cores)
@@ -2,10 +2,15 @@ package com.droiddeck.launcher.core
import android.app.ActivityManager
import android.content.Context
import android.hardware.display.DisplayManager
import android.os.Build
import android.os.Environment
import android.os.PerformanceHintManager
import android.os.PowerManager
import android.os.StatFs
import android.provider.Settings
import android.util.Log
import android.view.Display
import com.droiddeck.launcher.gpu.LinuxVulkanDriver
import com.droiddeck.launcher.gpu.LinuxVulkanDriverManager
import com.droiddeck.launcher.gpu.TurnipDriver
@@ -21,15 +26,6 @@ import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
/**
* `device.txt`: what this device is and what the session was told to do, written the moment a
* session starts so a log can be read months later without asking the reporter a single question.
*
* **Nothing identifying goes in here.** No serial number, no Android ID, no IMEI, no account name,
* no network names - only what the hardware is, what the ROM is, and which of our own settings
* were in effect. The one thing that comes close is the build fingerprint, which names the ROM and
* its build date and is the same string on every unit of that model.
*/
object DeviceReport {
private const val TAG = "DeviceReport"
@@ -41,6 +37,57 @@ object DeviceReport {
}
}
/** Name of an ApplicationExitInfo reason code, so a report says "LOW_MEMORY" instead of "3". */
internal fun exitReasonName(reason: Int): String = when (reason) {
0 -> "UNKNOWN"
1 -> "EXIT_SELF"
2 -> "SIGNALED"
3 -> "LOW_MEMORY"
4 -> "CRASH"
5 -> "CRASH_NATIVE"
6 -> "ANR"
7 -> "INITIALIZATION_FAILURE"
8 -> "PERMISSION_CHANGE"
9 -> "EXCESSIVE_RESOURCE_USAGE"
10 -> "USER_REQUESTED"
11 -> "USER_STOPPED"
12 -> "DEPENDENCY_DIED"
13 -> "OTHER"
14 -> "FREEZER"
15 -> "PACKAGE_STATE_CHANGE"
16 -> "PACKAGE_UPDATED"
else -> "REASON_$reason"
}
internal fun thermalStatusName(status: Int): String = when (status) {
0 -> "NONE"
1 -> "LIGHT"
2 -> "MODERATE"
3 -> "SEVERE"
4 -> "CRITICAL"
5 -> "EMERGENCY"
6 -> "SHUTDOWN"
else -> "STATUS_$status"
}
internal fun gameModeName(mode: Int): String = when (mode) {
0 -> "UNSUPPORTED"
1 -> "STANDARD"
2 -> "PERFORMANCE"
3 -> "BATTERY"
4 -> "CUSTOM"
else -> "MODE_$mode"
}
internal fun refreshModesSummary(rates: List<Float>): String =
rates.map { Math.round(it) }.distinct().sortedDescending().joinToString(", ") { "$it Hz" }
internal fun exitSummary(processName: String?, reason: Int, status: Int, description: String?): String =
buildString {
append(processName ?: "?").append(" ").append(exitReasonName(reason)).append(" status=").append(status)
if (!description.isNullOrBlank()) append(" ").append(description.trim())
}
fun build(context: Context, mode: String): String {
val b = StringBuilder()
fun h(title: String) {
@@ -77,6 +124,20 @@ object DeviceReport {
k("Fingerprint", Build.FINGERPRINT)
k("Kernel", System.getProperty("os.version"))
k("ABIs", Build.SUPPORTED_ABIS.joinToString(", "))
k("Memory page size", runCatching { android.system.Os.sysconf(android.system.OsConstants._SC_PAGESIZE) }.getOrNull()?.let { "$it bytes" })
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
h("Recent process exits")
val exits = runCatching {
context.getSystemService(android.app.ActivityManager::class.java).getHistoricalProcessExitReasons(context.packageName, 0, 5)
}.getOrNull().orEmpty()
if (exits.isEmpty()) k("Exits", "none recorded") else {
val stamp = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US)
for (e in exits) {
k(stamp.format(Date(e.timestamp)), exitSummary(e.processName, e.reason, e.status, e.description))
}
}
}
h("CPU and memory")
k("Cores", CpuCores.all.size)
@@ -91,6 +152,19 @@ object DeviceReport {
k("RAM total", FileUtils.sizeToString(mi.totalMem))
k("RAM available", FileUtils.sizeToString(mi.availMem))
}
runCatching {
val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) k("Thermal status", thermalStatusName(pm.currentThermalStatus))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
k("Thermal headroom (10 s)", pm.getThermalHeadroom(10).let { if (it.isNaN()) "not reported" else String.format(Locale.US, "%.2f", it) })
}
k("Power saver", pm.isPowerSaveMode)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val rate = runCatching { context.getSystemService(PerformanceHintManager::class.java)?.preferredUpdateRateNanos }.getOrNull()
k("ADPF hint sessions", if (rate == null || rate <= 0) "not supported" else "supported (preferred update ${rate / 1000} us)")
k("Game Mode", runCatching { context.getSystemService(android.app.GameManager::class.java)?.gameMode }.getOrNull()?.let { gameModeName(it) })
}
runCatching {
val fs = StatFs(context.filesDir.path)
k("App storage free", FileUtils.sizeToString(fs.availableBlocksLong * fs.blockSizeLong))
@@ -108,6 +182,13 @@ object DeviceReport {
h("Display")
k("Session output", SessionState.outputSize?.let { "${it.first}x${it.second}" })
k("Session refresh", String.format(Locale.US, "%.2f Hz", SessionState.refreshHz))
runCatching {
val display = context.getSystemService(DisplayManager::class.java).getDisplay(Display.DEFAULT_DISPLAY)
k("Panel refresh now", String.format(Locale.US, "%.2f Hz", display.refreshRate))
k("Panel refresh modes", refreshModesSummary(display.supportedModes.map { it.refreshRate }))
}
k("peak_refresh_rate", runCatching { Settings.System.getString(context.contentResolver, "peak_refresh_rate") }.getOrNull() ?: "not set")
k("refresh_rate_mode", runCatching { Settings.Secure.getString(context.contentResolver, "refresh_rate_mode") }.getOrNull() ?: "not set")
val panelSize = com.droiddeck.launcher.session.SessionDisplay.panelSize(context)
for ((label, mode) in listOf("Resolution (Steam)" to SessionService.MODE_STEAM, "Resolution (Desktop)" to SessionService.MODE_DESKTOP)) {
val choice = SessionPrefs.resolutionChoice(context, mode, panelSize)
@@ -121,7 +202,8 @@ object DeviceReport {
k("GPU", "${gpu.name} · ${gpu.family.label} · ${gpu.supportText}" + if (gpu.oneUi8Gen2) " · One UI 8 Gen 2" else "")
k("Driver mode", SessionPrefs.gpuDriverMode(context) +
(com.droiddeck.launcher.gpu.DriverPairs.recommendedKey(gpu, com.droiddeck.launcher.gpu.DriverPairs.from(
com.droiddeck.launcher.gpu.TurnipReleases.cached(context)))?.let { " (recommended pair: $it)" } ?: ""))
com.droiddeck.launcher.gpu.TurnipReleases.cached(context)), com.droiddeck.launcher.gpu.TurnipReleases.autoPicks(context),
com.droiddeck.launcher.gpu.AutoDriverTrial.bad(context))?.let { " (recommended pair: $it)" } ?: ""))
val turnip = TurnipDriver(context)
val androidChoice = SessionPrefs.androidDriver(context)
k("Display driver (chosen)", if (androidChoice.isEmpty()) "Auto -> ${turnip.autoId()}" else androidChoice)
@@ -147,7 +229,7 @@ object DeviceReport {
h("Settings in effect")
k("Client core override", SessionPrefs.clientCpusOverride(context))
k(" client cores", CpuCores.listOrAll(SessionPrefs.clientCpus(context)))
k(" game cores", CpuCores.restrictionOrEmpty(SessionPrefs.gameCpus(context)).ifEmpty { "every core (nothing sent)" })
k(" game cores", CpuCores.restrictionOrEmpty(SessionPrefs.gameCpus(context)).ifEmpty { "every core" })
k("Turnip sysmem", SessionPrefs.tuSysmem(context))
k("Zink lazy descriptors", SessionPrefs.zinkLazy(context))
k("Threaded GL (glthread)", SessionPrefs.glThread(context))
@@ -5,26 +5,14 @@ import android.os.Build
import com.droiddeck.launcher.R
import java.io.File
/**
* Whether this device can draw a Linux session at all.
*
* The runtime draws with Turnip, an Adreno driver. On Mali, Xclipse and PowerVR the compositor
* gets no usable Vulkan device and a session comes up as sound over a black screen - a failure
* with nothing in it to read. The app cannot fix that, so it says so before the download rather
* than after it. Adreno is recognised by what only Qualcomm's stack has: the KGSL node, or the
* vendor's own Vulkan driver at its usual path.
*/
object DeviceSupport {
fun adreno(): Boolean =
File("/sys/class/kgsl/kgsl-3d0").exists() || File("/vendor/lib64/hw/vulkan.adreno.so").exists()
/** The chip as the device names it, for the card that explains the refusal. */
fun gpuName(context: Context): String {
val soc = if (Build.VERSION.SDK_INT >= 31) Build.SOC_MODEL.takeIf { it.isNotBlank() && it != Build.UNKNOWN } else null
return soc?.let { "$it (${Build.HARDWARE})" } ?: Build.HARDWARE.ifBlank { context.getString(R.string.gpu_this_gpu) }
}
/** False on the Odin 3 (CQ8725S): a low-latency Wi-Fi lock makes its WLAN firmware hang, and
* that firmware reboots the whole device instead of restarting Wi-Fi. */
fun lowLatencyWifiSafe(): Boolean = Build.VERSION.SDK_INT < 31 || Build.SOC_MODEL != "CQ8725S"
}
@@ -0,0 +1,208 @@
package com.droiddeck.launcher.core
import android.content.Context
import android.content.SharedPreferences
import java.net.HttpURLConnection
import java.net.URL
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicLong
object DownloadSource {
const val MODE_AUTO = "auto"
const val MODE_DIRECT = "direct"
const val MODE_MIRROR = "mirror"
const val MODE_CUSTOM = "custom"
private const val PREFS = "download-source"
private const val KEY_MODE = "mode"
private const val KEY_MIRROR = "mirror"
private const val KEY_CUSTOM = "custom"
private const val KEY_FASTEST = "fastest"
private const val PROBE = "https://raw.githubusercontent.com/Droid-Deck/DroidDeck/main/README.md"
private const val PROBE_TIMEOUT_MS = 4_000
private const val PROBE_COOLDOWN_MS = 30_000L
private const val UA = "DroidDeck-Android"
private val MODES = listOf(MODE_AUTO, MODE_DIRECT, MODE_MIRROR, MODE_CUSTOM)
private val GITHUB_HOSTS = listOf(
"github.com",
"raw.githubusercontent.com",
"api.github.com",
)
private val CATALOGS = listOf(
"https://raw.githubusercontent.com/Droid-Deck/DroidDeck-CI/",
"https://raw.githubusercontent.com/Droid-Deck/DroidDeck-Components/",
"https://raw.githubusercontent.com/Droid-Deck/Drivers-CI/",
)
data class Mirror(val host: String, val api: Boolean)
val MIRRORS = listOf(
Mirror("gh-proxy.com", true),
Mirror("gh-proxy.org", true),
Mirror("ghproxy.net", false),
Mirror("ghfast.top", false),
)
@Volatile
private var app: Context? = null
@Volatile
private var lastGood: String? = null
private val probing = AtomicBoolean(false)
private val probedAt = AtomicLong(0L)
fun init(context: Context) {
app = context.applicationContext
lastGood = prefs()?.getString(KEY_FASTEST, null)
}
fun probe() {
if (app == null || prefs()?.getString(KEY_MODE, MODE_AUTO) == MODE_DIRECT) return
if (!probing.compareAndSet(false, true)) return
val now = System.currentTimeMillis()
if (now - probedAt.get() < PROBE_COOLDOWN_MS) {
probing.set(false)
return
}
probedAt.set(now)
Thread {
try {
val times = ConcurrentHashMap<String, Long>()
val sources = listOf<String?>(null) + MIRRORS.map { it.host }
val latch = CountDownLatch(sources.size)
for (mirror in sources) {
Thread {
try {
val ms = latency(mirror?.let { "https://$it/$PROBE" } ?: PROBE)
if (ms > 0) times[mirror ?: ""] = ms
} finally {
latch.countDown()
}
}.apply { isDaemon = true }.start()
}
latch.await(PROBE_TIMEOUT_MS * 3L, TimeUnit.MILLISECONDS)
val best = times.minByOrNull { it.value }?.key ?: return@Thread
lastGood = best
prefs()?.edit()?.putString(KEY_FASTEST, best)?.apply()
} finally {
probing.set(false)
}
}.apply { isDaemon = true }.start()
}
private fun latency(url: String): Long {
val started = System.nanoTime()
var connection: HttpURLConnection? = null
return try {
val c = (URL(url).openConnection() as HttpURLConnection).apply {
connectTimeout = PROBE_TIMEOUT_MS
readTimeout = PROBE_TIMEOUT_MS
instanceFollowRedirects = true
setRequestProperty("Range", "bytes=0-1023")
setRequestProperty("User-Agent", UA)
}
connection = c
if (c.responseCode / 100 != 2) 0L else {
c.inputStream.use { it.read() }
(System.nanoTime() - started) / 1_000_000
}
} catch (e: Exception) {
0L
} finally {
runCatching { connection?.disconnect() }
}
}
fun isGithub(url: String): Boolean = GITHUB_HOSTS.any { url.startsWith("https://$it/") }
/** Catalogs carry the digests everything else is checked against, so a mirror must never supply one. */
fun isCatalog(url: String): Boolean = CATALOGS.any { url.startsWith(it) }
fun candidates(target: String): List<String> {
// GitHub API metadata carries executable URLs and digests; never proxy it.
if (isCatalog(target) || target.startsWith("https://api.github.com/")) return listOf(target)
val prefs = prefs() ?: return listOf(target)
if (!isGithub(target)) return listOf(target)
val api = target.startsWith("https://api.github.com/")
val proxies = MIRRORS.filter { !api || it.api }.map { it.proxy(target) }
val ordered = when (prefs.getString(KEY_MODE, MODE_AUTO) ?: MODE_AUTO) {
MODE_DIRECT -> listOf(target)
MODE_MIRROR -> listOfNotNull(picked(prefs, api)?.proxy(target)) + listOf(target) + quickest(proxies)
MODE_CUSTOM -> listOfNotNull(customProxy(prefs, target)) + listOf(target) + quickest(proxies)
else -> if (api) listOf(target) else listOf(target) + quickest(proxies)
}
return ordered.distinct()
}
private fun quickest(sources: List<String>): List<String> =
sources.sortedBy { if (host(it) == lastGood) 0 else 1 }
fun succeeded(source: String) {
lastGood = host(source)
}
fun failed() {
lastGood = null
prefs()?.edit()?.remove(KEY_FASTEST)?.apply()
}
fun mode(context: Context): String = prefs(context)?.getString(KEY_MODE, MODE_AUTO) ?: MODE_AUTO
fun setMode(context: Context, mode: String) {
prefs(context)?.edit()?.putString(KEY_MODE, mode.takeIf { it in MODES } ?: MODE_AUTO)?.apply()
}
fun mirror(context: Context): String {
val host = prefs(context)?.getString(KEY_MIRROR, "") ?: ""
return MIRRORS.firstOrNull { it.host == host }?.host ?: MIRRORS.first().host
}
fun setMirror(context: Context, host: String) {
prefs(context)?.edit()?.putString(KEY_MIRROR, host.takeIf { h -> MIRRORS.any { it.host == h } } ?: "")?.apply()
}
fun custom(context: Context): String = prefs(context)?.getString(KEY_CUSTOM, "") ?: ""
fun setCustom(context: Context, prefix: String) {
prefs(context)?.edit()?.putString(KEY_CUSTOM, normalizePrefix(prefix))?.apply()
}
fun reset(context: Context) {
lastGood = null
prefs(context)?.edit()?.clear()?.apply()
}
private fun prefs(): SharedPreferences? = app?.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
private fun prefs(context: Context): SharedPreferences? =
context.applicationContext.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
private fun Mirror.proxy(target: String): String = "https://$host/$target"
private fun host(url: String): String =
MIRRORS.firstOrNull { url.startsWith("https://${it.host}/") }?.host ?: ""
private fun picked(prefs: SharedPreferences, api: Boolean): Mirror? {
val chosen = MIRRORS.firstOrNull { it.host == (prefs.getString(KEY_MIRROR, "") ?: "") } ?: MIRRORS.firstOrNull()
return if (api && chosen != null && !chosen.api) MIRRORS.firstOrNull { it.api } else chosen
}
private fun custom(prefs: SharedPreferences): String = prefs.getString(KEY_CUSTOM, "") ?: ""
private fun customProxy(prefs: SharedPreferences, target: String): String? =
normalizePrefix(custom(prefs)).takeIf { it.isNotEmpty() }?.let { "$it/$target" }
private fun normalizePrefix(value: String): String {
val trimmed = value.trim().trimEnd('/')
if (trimmed.isEmpty() || trimmed.startsWith("http://")) return ""
return if (trimmed.startsWith("https://")) trimmed else "https://$trimmed"
}
}
@@ -6,75 +6,201 @@ import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Properties;
/**
* The two fetches this app makes: the runtime catalog (a few hundred bytes of JSON) and the
* runtime tarball itself (~790 MB). Plain HttpURLConnection - nothing here needs a client library,
* and one fewer dependency is one fewer thing that can fail a release build.
*/
public final class Downloader {
private static final String TAG = "Downloader";
private static final int TIMEOUT_MS = 30_000;
private static final int CONNECT_TIMEOUT_MS = 10_000;
private static final int STALL_TIMEOUT_MS = 15_000;
private static final int HTTP_RANGE_NOT_SATISFIABLE = 416;
private Downloader() {}
public static String downloadString(String url) {
HttpURLConnection connection = null;
try {
connection = open(url);
if (connection.getResponseCode() / 100 != 2) {
Log.w(TAG, url + " -> HTTP " + connection.getResponseCode());
return null;
List<String> sources = DownloadSource.INSTANCE.candidates(url);
for (int i = 0; i < sources.size(); i++) {
if (i > 0) Log.i(TAG, "retrying " + url + " via " + sources.get(i));
Fetch fetch = fetch(sources.get(i));
if (fetch.body != null) {
DownloadSource.INSTANCE.succeeded(sources.get(i));
return fetch.body;
}
try (InputStream in = connection.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
FileUtils.copy(in, out);
return new String(out.toByteArray(), StandardCharsets.UTF_8);
if (fetch.fatal && sources.get(i).equals(url)) return null;
}
DownloadSource.INSTANCE.failed();
return null;
}
/** Fetches without the mirror list, for a URL whose body is a digest the payload is checked against. */
public static String downloadDirect(String url) {
return fetch(url).body;
}
public static boolean downloadFile(String url, File destination, boolean resume,
java.util.function.Consumer<Float> progress) {
return downloadFile(url, destination, resume, null, progress);
}
/** Verify each candidate before accepting it, so a bad mirror can fall back. */
public static boolean downloadFile(String url, File destination, boolean resume, String expectedSha256,
java.util.function.Consumer<Float> progress) {
if (expectedSha256 != null && !expectedSha256.matches("(?i)[0-9a-f]{64}"))
throw new IllegalArgumentException("Expected SHA-256 must be 64 hex characters");
return downloadCandidates(url, DownloadSource.INSTANCE.candidates(url), destination, resume, expectedSha256, progress);
}
// Package-private for the local multi-source HTTP test.
static boolean downloadCandidates(String original, List<String> sources, File destination, boolean resume,
String expectedSha256, java.util.function.Consumer<Float> progress) {
Identity identity = loadIdentity(destination, resume, expectedSha256);
for (int i = 0; i < sources.size(); i++) {
String source = sources.get(i);
if (i > 0) Log.i(TAG, "retrying " + original + " via " + source);
Outcome outcome = attempt(source, destination, resume, identity, expectedSha256, progress);
if (outcome == Outcome.DONE) {
if (expectedSha256 != null && !expectedSha256.equalsIgnoreCase(Hashes.sha256(destination))) {
Log.w(TAG, "checksum mismatch from " + source + "; trying another source");
if (!discardPartial(destination, identity)) return false;
continue;
}
resumeFile(destination).delete();
DownloadSource.INSTANCE.succeeded(source);
if (progress != null) progress.accept(1f);
return true;
}
} catch (Exception e) {
Log.w(TAG, "GET " + url, e);
return null;
} finally {
if (connection != null) connection.disconnect();
if (outcome == Outcome.FATAL && source.equals(original)) return false;
}
DownloadSource.INSTANCE.failed();
return false;
}
private enum Outcome { DONE, RETRY, FATAL }
private static final class Identity {
private String source;
private String etag;
}
private static File resumeFile(File dest) {
return new File(dest.getPath() + ".resume.properties");
}
private static boolean strongETag(String value) {
return value != null && !value.isEmpty() && !value.startsWith("W/");
}
/** Only reuse saved bytes when we persisted a strong validator and their source. */
private static Identity loadIdentity(File dest, boolean resume, String expectedSha256) {
Identity identity = new Identity();
File marker = resumeFile(dest);
if (!resume || !dest.isFile() || dest.length() == 0) {
marker.delete();
return identity;
}
try (FileInputStream in = new FileInputStream(marker)) {
Properties p = new Properties();
p.load(in);
String source = p.getProperty("source");
String etag = p.getProperty("etag");
String expected = p.getProperty("sha256", "");
if (source != null && source.startsWith("http") && strongETag(etag)
&& expected.equals(expectedSha256 == null ? "" : expectedSha256.toLowerCase(java.util.Locale.ROOT))) {
identity.source = source;
identity.etag = etag;
return identity;
}
} catch (IOException e) {
// Legacy partials or interrupted metadata writes have no trustworthy identity.
}
if (!discardPartial(dest, identity)) Log.w(TAG, "could not discard unidentified partial " + dest);
return identity;
}
private static boolean discardPartial(File dest, Identity identity) {
identity.source = null;
identity.etag = null;
File marker = resumeFile(dest);
return (!dest.exists() || dest.delete()) && (!marker.exists() || marker.delete());
}
private static void saveIdentity(File dest, Identity identity, String expectedSha256) {
File marker = resumeFile(dest);
if (!strongETag(identity.etag)) {
marker.delete();
return;
}
Properties p = new Properties();
p.setProperty("source", identity.source);
p.setProperty("etag", identity.etag);
p.setProperty("sha256", expectedSha256 == null ? "" : expectedSha256.toLowerCase(java.util.Locale.ROOT));
try (FileOutputStream out = new FileOutputStream(marker)) {
p.store(out, "DroidDeck partial download validator");
} catch (IOException e) {
marker.delete(); // No valid metadata means no future resume.
Log.w(TAG, "cannot persist download validator", e);
}
}
/**
* Downloads to {@code destination}, reporting a 0..1 fraction (or -1 when the server sends no
* length). {@code resume} continues a partial file with a Range request, which is what makes a
* 790 MB download survive a dropped connection instead of starting over.
*/
public static boolean downloadFile(String url, File destination, boolean resume,
java.util.function.Consumer<Float> progress) {
private static Outcome attempt(String url, File destination, boolean resume, Identity identity,
String expectedSha256, java.util.function.Consumer<Float> progress) {
HttpURLConnection connection = null;
long have = resume && destination.isFile() ? destination.length() : 0;
try {
connection = open(url);
if (have > 0) connection.setRequestProperty("Range", "bytes=" + have + "-");
if (have > 0 && (!url.equals(identity.source) || !strongETag(identity.etag))) {
// A new source or unvalidated bytes must restart from byte zero.
if (!discardPartial(destination, identity)) return Outcome.RETRY;
have = 0;
}
connection = open(url, STALL_TIMEOUT_MS);
if (have > 0) {
connection.setRequestProperty("Range", "bytes=" + have + "-");
connection.setRequestProperty("If-Range", identity.etag);
}
int code = connection.getResponseCode();
boolean appending = code == HttpURLConnection.HTTP_PARTIAL;
if (have > 0 && code == HTTP_RANGE_NOT_SATISFIABLE) {
connection.disconnect();
if (!discardPartial(destination, identity)) return Outcome.RETRY;
return attempt(url, destination, false, identity, expectedSha256, progress);
}
if (code / 100 != 2) {
// The server ignored or refused the range: start the file again.
if (have > 0) {
connection.disconnect();
//noinspection ResultOfMethodCallIgnored
destination.delete();
return downloadFile(url, destination, false, progress);
}
Log.w(TAG, url + " -> HTTP " + code);
return false;
return code == HttpURLConnection.HTTP_NOT_FOUND || code == HttpURLConnection.HTTP_GONE
? Outcome.FATAL : Outcome.RETRY;
}
boolean appending = code == HttpURLConnection.HTTP_PARTIAL;
if (appending && !rangeStartsAt(connection.getHeaderField("Content-Range"), have)) {
connection.disconnect();
if (!discardPartial(destination, identity)) return Outcome.RETRY;
return attempt(url, destination, false, identity, expectedSha256, progress);
}
String etag = connection.getHeaderField("ETag");
if (have > 0 && appending && (!strongETag(etag) || !etag.equals(identity.etag))) {
connection.disconnect();
if (!discardPartial(destination, identity)) return Outcome.RETRY;
return attempt(url, destination, false, identity, expectedSha256, progress);
}
if (!appending && have > 0) {
// If-Range may legitimately return full 200 when representation changed.
if (!discardPartial(destination, identity)) return Outcome.RETRY;
have = 0;
}
if (!appending) have = 0;
long length = connection.getContentLengthLong();
long total = length < 0 ? -1 : length + have;
long total = appending ? rangeTotal(connection.getHeaderField("Content-Range")) : -1;
if (total < 0) total = length < 0 ? -1 : length + have;
File parent = destination.getParentFile();
if (parent != null && !parent.isDirectory()) //noinspection ResultOfMethodCallIgnored
parent.mkdirs();
if (parent != null && !parent.isDirectory()) parent.mkdirs();
identity.source = url;
identity.etag = strongETag(etag) ? etag : null;
saveIdentity(destination, identity, expectedSha256);
long written = have;
try (InputStream in = new BufferedInputStream(connection.getInputStream(), 1 << 16);
OutputStream out = new FileOutputStream(destination, appending)) {
@@ -83,28 +209,72 @@ public final class Downloader {
for (int read = in.read(buffer); read > 0; read = in.read(buffer)) {
out.write(buffer, 0, read);
written += read;
// A callback per 64 KB would be tens of thousands of UI hops; once per MB is
// enough to move a progress bar smoothly.
if (progress != null && written - lastReport > (1 << 20)) {
lastReport = written;
progress.accept(total > 0 ? written / (float) total : -1f);
}
}
}
if (progress != null) progress.accept(1f);
return total <= 0 || written >= total;
if (total > 0 && written < total) return Outcome.RETRY;
return Outcome.DONE;
} catch (Exception e) {
Log.w(TAG, "download " + url, e);
return false;
return Outcome.RETRY;
} finally {
if (connection != null) connection.disconnect();
}
}
private static HttpURLConnection open(String url) throws java.io.IOException {
/** True when a `Content-Range: bytes START-END/TOTAL` header starts exactly at {@code start}. */
static boolean rangeStartsAt(String contentRange, long start) {
if (contentRange == null) return false;
java.util.regex.Matcher m = java.util.regex.Pattern.compile("^\\s*bytes\\s+(\\d+)-\\d+/(?:\\d+|\\*)\\s*$").matcher(contentRange);
return m.matches() && Long.parseLong(m.group(1)) == start;
}
/** The complete length in a `Content-Range` header, or -1 when the header leaves it unknown. */
static long rangeTotal(String contentRange) {
if (contentRange == null) return -1;
java.util.regex.Matcher m = java.util.regex.Pattern.compile("^\\s*bytes\\s+\\d+-\\d+/(\\d+)\\s*$").matcher(contentRange);
return m.matches() ? Long.parseLong(m.group(1)) : -1;
}
private static final class Fetch {
final String body;
final boolean fatal;
Fetch(String body, boolean fatal) {
this.body = body;
this.fatal = fatal;
}
}
private static Fetch fetch(String url) {
HttpURLConnection connection = null;
try {
connection = open(url, STALL_TIMEOUT_MS);
int code = connection.getResponseCode();
if (code / 100 != 2) {
Log.w(TAG, url + " -> HTTP " + code);
return new Fetch(null, code == HttpURLConnection.HTTP_NOT_FOUND || code == HttpURLConnection.HTTP_GONE);
}
try (InputStream in = connection.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
FileUtils.copy(in, out);
return new Fetch(new String(out.toByteArray(), StandardCharsets.UTF_8), false);
}
} catch (Exception e) {
Log.w(TAG, "GET " + url, e);
return new Fetch(null, false);
} finally {
if (connection != null) connection.disconnect();
}
}
private static HttpURLConnection open(String url, int readTimeoutMs) throws java.io.IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setConnectTimeout(TIMEOUT_MS);
connection.setReadTimeout(TIMEOUT_MS);
connection.setConnectTimeout(CONNECT_TIMEOUT_MS);
connection.setReadTimeout(readTimeoutMs);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("User-Agent", "DroidDeck-Android");
return connection;
@@ -28,6 +28,12 @@ object FexPreset {
fun byId(id: String): Preset = all.firstOrNull { it.id == id } ?: all[0]
/** Game-launch variables in KEY=VALUE form. */
fun env(id: String): List<String> = byId(id).env
val environmentKeys: Set<String> by lazy {
all.flatMap { it.env }.mapTo(linkedSetOf()) { it.substringBefore('=') }
}
fun environment(id: String): Map<String, String> =
env(id).associate { it.substringBefore('=') to it.substringAfter('=') }
}
@@ -18,14 +18,12 @@ public final class FileUtils {
private FileUtils() {}
/** Recursive delete that does not follow symlinks - a rootfs is full of them. */
public static void delete(File file) {
if (file == null || !file.exists() && !isSymlink(file)) return;
if (file.isDirectory() && !isSymlink(file)) {
File[] children = file.listFiles();
if (children != null) for (File child : children) delete(child);
}
//noinspection ResultOfMethodCallIgnored
file.delete();
}
@@ -37,7 +35,6 @@ public final class FileUtils {
}
}
/** Empties a directory without removing it. */
public static void clear(File dir) {
if (dir == null || !dir.isDirectory()) return;
File[] children = dir.listFiles();
@@ -47,7 +44,7 @@ public final class FileUtils {
public static boolean writeString(File file, String content) {
try {
File parent = file.getParentFile();
if (parent != null && !parent.isDirectory()) //noinspection ResultOfMethodCallIgnored
if (parent != null && !parent.isDirectory())
parent.mkdirs();
Files.write(file.toPath(), content.getBytes(StandardCharsets.UTF_8));
return true;
@@ -81,10 +78,9 @@ public final class FileUtils {
}
}
/** Copies an asset out of the apk, creating the parent directory. */
public static boolean copyAsset(Context context, String assetPath, File destination) {
File parent = destination.getParentFile();
if (parent != null && !parent.isDirectory()) //noinspection ResultOfMethodCallIgnored
if (parent != null && !parent.isDirectory())
parent.mkdirs();
try (InputStream in = context.getAssets().open(assetPath);
OutputStream out = new FileOutputStream(destination)) {
@@ -1,5 +1,7 @@
package com.droiddeck.launcher.core
import com.droiddeck.launcher.session.SessionPrefs
object GameEnvironment {
data class Config(
val shared: Map<String, String?> = emptyMap(),
@@ -29,21 +31,88 @@ object GameEnvironment {
"MESA_SHADER_CACHE_DISABLE" to "false",
"VKD3D_FEATURE_LEVEL" to "12_2",
"VKD3D_SHADER_MODEL" to "6_9",
// .NET 7 and later reserve 256 GB of address space for the GC heap at startup, more than an
// Android process has free, and the runtime fails to start. 32 GB is plenty for a game.
// Older NativeAOT runtimes require bare hex digits. CoreCLR accepts this form too.
"DOTNET_GCRegionRange" to "800000000",
// .NET's W^X writes JIT code through a second mapping that FEX does not watch, so FEX keeps
// running the stale translation and the game hangs at startup.
"DOTNET_EnableWriteXorExecute" to "0",
).apply {
FexPreset.env(preset).forEach { put(it.substringBefore('='), it.substringAfter('=')) }
// Steam settings' Force SSBS: droiddeck-game-env preloads libssbs.so into the game's Wine.
if (forceSsbs) put(FORCE_SSBS, "1")
}
const val FORCE_SSBS = "DROIDDECK_FORCE_SSBS"
const val NO_STEAM = "DROIDDECK_NO_STEAM"
fun gameNoSteam(config: Config, scope: String): Boolean =
scope.isNotEmpty() && config.games[scope]?.get(NO_STEAM)?.lowercase() in setOf("1", "on", "true", "yes")
fun withGameNoSteam(config: Config, scope: String, on: Boolean): Config {
require(scope.isNotEmpty() && validScope(scope))
return withScopeValue(config, scope, NO_STEAM, if (on) "1" else null)
}
fun effective(config: Config, preset: String, scope: String, forceSsbs: Boolean = false): Map<String, String?> =
defaults(preset, forceSsbs) + config.shared + if (scope.isEmpty()) emptyMap() else config.games[scope].orEmpty()
fun hasOverride(config: Config, scope: String, name: String): Boolean =
scope.isNotEmpty() && config.entries(scope).containsKey(name)
fun withScopeValue(config: Config, scope: String, name: String, value: String?): Config {
require(scope.isEmpty() || validScope(scope))
val entries = config.entries(scope).toMutableMap()
if (value == null) entries.remove(name) else entries[name] = value
return config.withEntries(scope, entries)
}
fun gameFexPreset(config: Config, scope: String): String? {
require(scope.isNotEmpty() && validScope(scope))
val entries = config.games[scope].orEmpty()
if (FexPreset.environmentKeys.none(entries::containsKey)) return null
return FexPreset.all.firstOrNull { preset ->
val expected = FexPreset.environment(preset.id)
FexPreset.environmentKeys.all { key -> entries.containsKey(key) && entries[key] == expected[key] }
}?.id ?: CUSTOM_FEX_PRESET
}
fun withGameFexPreset(config: Config, scope: String, preset: String?): Config {
require(scope.isNotEmpty() && validScope(scope))
require(preset == null || FexPreset.all.any { it.id == preset })
val entries = config.entries(scope).filterKeys { it !in FexPreset.environmentKeys }.toMutableMap()
if (preset != null) {
val values = FexPreset.environment(preset)
FexPreset.environmentKeys.forEach { key -> entries[key] = values[key] }
}
return config.withEntries(scope, entries)
}
const val CUSTOM_FEX_PRESET = "__custom__"
val syncBackends = listOf(SessionPrefs.SYNC_NTSYNC, SessionPrefs.SYNC_FSYNC, SessionPrefs.SYNC_ESYNC, SessionPrefs.SYNC_WINESERVER)
val syncKeys = listOf("BL_SYNC", "BL_FSYNC_FIRST", "BL_SYNC_FALLBACK", "PROTON_NO_ESYNC", "PROTON_NO_FSYNC", "PROTON_NO_NTSYNC", "WINEESYNC")
fun syncEnvironment(backend: String): Map<String, String> {
require(backend in syncBackends) { "unknown sync $backend" }
return buildMap {
put("BL_SYNC", if (backend == SessionPrefs.SYNC_NTSYNC) "1" else "0")
put("BL_FSYNC_FIRST", if (backend == SessionPrefs.SYNC_FSYNC) "1" else "0")
put("BL_SYNC_FALLBACK", if (backend == SessionPrefs.SYNC_WINESERVER) "0" else "1")
if (backend == SessionPrefs.SYNC_FSYNC) put("PROTON_NO_ESYNC", "1")
}
}
fun gameSync(config: Config, scope: String): String? {
require(scope.isNotEmpty() && validScope(scope))
val entries = config.games[scope].orEmpty().filterKeys { it in syncKeys }
if (entries.isEmpty()) return null
return syncBackends.firstOrNull { entries == syncEnvironment(it) } ?: CUSTOM_SYNC
}
fun withGameSync(config: Config, scope: String, backend: String?): Config {
require(scope.isNotEmpty() && validScope(scope))
val entries = config.entries(scope).filterKeys { it !in syncKeys }.toMutableMap()
if (backend != null) entries += syncEnvironment(backend)
return config.withEntries(scope, entries)
}
const val CUSTOM_SYNC = "__custom__"
}
@@ -3,7 +3,6 @@ package com.droiddeck.launcher.core
import java.io.File
import java.security.MessageDigest
/** File checksums as lowercase hex, and GitHub's release-asset digests, in one place. */
object Hashes {
private val GITHUB_SHA256 = Regex("(?i)sha256:[0-9a-f]{64}")
@@ -13,11 +12,9 @@ object Hashes {
@JvmStatic
fun sha512(file: File): String = hex(file, "SHA-512")
/** True when [digest] is GitHub's asset digest form, "sha256:" and 64 hex digits. */
@JvmStatic
fun isGithubSha256(digest: String?): Boolean = digest != null && digest.matches(GITHUB_SHA256)
/** The hex of a GitHub asset digest ("sha256:<hex>"), or null when [digest] is not one. */
@JvmStatic
fun githubSha256(digest: String?): String? = digest?.takeIf { isGithubSha256(it) }?.substringAfter(':')
@@ -1,6 +1,5 @@
package com.droiddeck.launcher.core
/** The variables a host process is started with, in the order they were set. */
class HostEnvironment {
private val entries = LinkedHashMap<String, String>()
@@ -8,6 +7,5 @@ class HostEnvironment {
entries[name] = value
}
/** `NAME=VALUE` lines, the shape ProcessBuilder and proot take. */
fun asArray(): Array<String> = entries.map { (name, value) -> "$name=$value" }.toTypedArray()
}
@@ -4,27 +4,9 @@ import android.util.Log
import java.io.File
import java.util.function.Consumer
/**
* Starts a process on the Android side - proot, the audio daemon, logcat - and hands back its pid.
*
* The pid matters: stopping a session means killing that exact process (proot runs with
* --kill-on-exit, so the guest tree goes with it), and java.lang.Process does not expose it before
* API 33, so it is read from the implementation's private field. Output can be drained line by
* line to a callback; without one it goes to /dev/null, never to a pipe nobody reads, which would
* block the child the moment the pipe filled.
*/
object HostProcess {
private const val TAG = "HostProcess"
/**
* @param command the program and its arguments as one line; a backslash escapes a space, since
* paths under /data are full of them.
* @param env `NAME=VALUE` lines added to the app's own environment (proot needs nothing from
* the inherited set, and the guest starts with `env -i` anyway), or null.
* @param onExit told the exit status from a waiting thread, or null.
* @param onLine told each line of stdout and stderr, or null to discard both.
* @return the pid, or -1 when the process could not be started.
*/
@JvmStatic
fun start(command: String, env: Array<String>?, dir: File?, onExit: Consumer<Int>?, onLine: Consumer<String>?): Int {
val argv = split(command)
@@ -46,14 +28,13 @@ object HostProcess {
try {
process.inputStream.bufferedReader().useLines { lines -> lines.forEach { onLine.accept(it) } }
} catch (e: Exception) {
// The process ended or the pipe closed; nothing more to read.
}
}, "proc-out-$pid").apply { priority = Thread.NORM_PRIORITY - 1; isDaemon = true; start() }
}
if (onExit != null) {
Thread({
var status = -1
try { status = process.waitFor() } catch (e: InterruptedException) { /* reported as -1 */ }
try { status = process.waitFor() } catch (e: InterruptedException) { }
onExit.accept(status)
}, "proc-wait-$pid").apply { priority = Thread.NORM_PRIORITY - 1; start() }
}
@@ -75,7 +56,6 @@ object HostProcess {
-1
}
/** Splits a command line on spaces; a backslash keeps the space that follows it. */
@JvmStatic
fun split(command: String): List<String> {
val parts = ArrayList<String>()
@@ -1,64 +1,28 @@
package com.droiddeck.launcher.core
/**
* Scrubs credentials out of a log line before it is written somewhere a user will share.
*
* The Steam client's own logs are the reason this exists: they carry the account's session token
* (logged as a bare number after "Using JWT"), machine-auth GUIDs, `token=`/`sessionid=` pairs,
* Steam Guard codes, 32-hex WebAPI keys and the account's SteamID. A session folder from this app
* is meant to be attached to a bug report as it is, so none of that may be in it.
*
* Ported from Bannerlator's {@code SteamLogRedactor} (GPL-3.0), which was written against real
* client logs; the account-name registration is left out because this app never learns it.
*
* What is deliberately KEPT, because it is what makes a log worth reading: EResult codes, CM host
* names and Valve's server addresses, timings, token expiry dates, app IDs, pids, connection-state
* changes and file paths. A SteamID is masked rather than deleted - first four and last four digits
* survive - so a reader can still tell two accounts' lines apart without the value identifying either.
*
* The device's OWN addresses are not kept: the client's IPv6 check logs "external address <ours>"
* about twenty times a session, and a public IPv6 address is the user's. [learnOwnAddresses] reads
* the link the session was given (etc/droiddeck-net) and every address on the same /64 - privacy
* addresses rotate inside it - or an own public IPv4 is replaced wherever it appears. Likewise every
* Steam account and persona name on the device ([learnAccounts]), for users who sign in with an
* account name rather than an email.
*/
object LogRedactor {
private val EMAIL = Regex("[A-Za-z0-9._%+\\-]+@[A-Za-z0-9.\\-]+\\.[A-Za-z]{2,}")
/** "Using JWT 25484942796017334" - the client logs its session token as digits, not base64. */
private val JWT_LABELLED = Regex("(?i)(\\bJWT[\\s=:]+)(\\d{8,})")
/** A 3-part base64url JWT logged verbatim (a refresh or access token). */
private val JWT_BASE64 = Regex("ey[A-Za-z0-9_\\-]{6,}\\.[A-Za-z0-9_\\-]{6,}\\.[A-Za-z0-9_\\-]{6,}")
/** Machine-auth and other GUIDs. */
private val GUID = Regex("\\b[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}\\b")
/**
* key=value secrets. A bare `key` is excluded on purpose so "key=english" survives; a real
* WebAPI key is caught by [WEBAPI_KEY]. The value class excludes `<` and `>` so running this
* twice over an already-scrubbed line changes nothing.
*/
private val SECRET_KV = Regex(
"(?i)\\b(access[_-]?token|refresh[_-]?token|auth[_-]?token|authtoken|token|authcode|" +
"auth[_-]?ticket|ticket|sessionid|steamloginsecure|webapikey|api[_-]?key|" +
"machine[_-]?auth(?:[_-]?token)?|machineauth|password|passwd|pwd|secret)" +
"(\\s*[=:]\\s*|=)([^\\s\"'<>&;,]{4,})"
)
/** A Steam Guard code, only where the text around it says that is what it is. */
private val GUARD_CODE = Regex(
"(?i)((?:steam\\s*)?guard\\s*code[\\s:=]*|two[\\s-]?factor[\\s:=]*|2fa[\\s:=]*)([A-Za-z0-9]{5})"
)
/** Exactly 32 hex - a WebAPI key. Bounded so a 40-hex depot chunk id is left alone. */
private val WEBAPI_KEY = Regex("\\b[0-9A-Fa-f]{32}\\b")
/** A long opaque run after a sensitive word, for anything the rules above missed. */
private val RESIDUAL = Regex(
"(?i)\\b(jwt|token|ticket|sessionid|steamloginsecure|machineauth)\\b[\\s=:]*([A-Za-z0-9+/=_\\-]{12,})"
)
private val LONG_TOKEN = Regex("[A-Za-z0-9_\\-]{88,}")
private val STEAMID64 = Regex("\\b(76561)(\\d{8})(\\d{4})\\b")
private val STEAMID3 = Regex("\\[U:1:(\\d+)]")
/** "external address 2607:..." / "external IP: 203.0.113.9" - the client stating ours. */
private val EXTERNAL_ADDR = Regex("(?i)(external\\s+(?:ip\\s+)?address\\s*[:=]?\\s*|external\\s+ip\\s*[:=]?\\s*)([0-9A-Fa-f:.]{7,})")
private val IPV4 = Regex("(?<![0-9.])(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})(?![0-9.])")
/** An IPv6 literal: "::" somewhere, or all eight groups (a clock's 10:03:05 is neither). */
private val IPV6 = Regex("(?<![0-9A-Za-z:])[0-9A-Fa-f]{0,4}(?::[0-9A-Fa-f]{0,4}){2,7}(?:%[A-Za-z0-9_.]+)?(?![0-9A-Za-z:])")
private val MAC = Regex("(?i)(?<![0-9a-z:-])[0-9a-f]{2}([:-])(?:[0-9a-f]{2}\\1){4}[0-9a-f]{2}(?![0-9a-z:-])")
private val MAC_FIELD = Regex(
@@ -70,20 +34,9 @@ object LogRedactor {
"[\"']?\\s*[:=]\\s*)(\"[^\"\\r\\n<>]+\"|'[^'\\r\\n<>]+'|[^\\s\"'<>&;,\\[\\]{}]+)"
)
/**
* The account a Steam UI login line names ("Login: OnLoginStateChange <account> 2 1 0 0"): a
* user who signs in with a plain account name, not an email, had it in every log. The single
* space then a non-space keeps "OnLoginStateChange 0 1 0 0" (no account yet) as it is.
*/
private val LOGIN_STATE = Regex("(?i)(OnLoginStateChange:? )([^\\s<]\\S*)")
private val LOGIN_USERS = Regex("(OnLoginUsersChanged )(\\S.*)$")
/**
* An account name as a field: `AccountName`, `account_name`, `username` (`"AccountName"
* "someone"` in a VDF, `account_name=...`, `"username": "..."`) and `login` - the last only
* as `login=`, a quoted key or a VDF pair, so "Login: OnLoginStateChange ..." keeps its label.
* A quoted value is blanked whole; one already blanked is left as it is.
*/
private const val ACCOUNT_VALUE = "(\"[^\"<\\r\\n]+\"|'[^'<\\r\\n]+'|[^\\s\"'<>&;,}]+)"
private val ACCOUNT_KV = Regex(
"(?i)(?<![A-Za-z0-9_])(account[_-]?name|user[_-]?name|epicusername)" +
@@ -93,49 +46,39 @@ object LogRedactor {
"(?i)(?<![A-Za-z0-9_])(login)(\\s*=\\s*|[\"']\\s*[=:]\\s*|[\"']\\s+)" + ACCOUNT_VALUE
)
/** A learned pattern and the text (lowercase) a line must hold for it to match; null when none can be said. */
private class Learned(val regex: Regex, val needs: String?)
/** The device's Steam accounts and persona names as patterns (see [learnAccounts]). */
@Volatile
private var accounts: List<Learned> = emptyList()
/**
* Learns every Steam account on the device from the client's loginusers.vdf - its AccountName
* and PersonaName - so they are replaced wherever a log mentions them, whoever the user is.
* Names under three characters are skipped: blanking every "a" would ruin a log.
*/
fun learnAccounts(loginUsers: java.io.File) {
val found = ArrayList<Learned>()
try {
if (loginUsers.isFile) {
val kv = Regex("\"(AccountName|PersonaName)\"\\s+\"([^\"]*)\"")
kv.findAll(loginUsers.readText()).map { it.groupValues[2].trim() }.filter { it.length >= 3 }.distinct().forEach { name ->
val needs = name.lowercase().takeIf { name.all { it.code < 0x80 } }
found += Learned(Regex("(?i)(?<![A-Za-z0-9_])" + Regex.escape(name) + "(?![A-Za-z0-9_])"), needs)
}
}
} catch (e: Exception) {
return
fun learnAccounts(vararg loginUsers: java.io.File) {
val names = LinkedHashSet<String>()
val kv = Regex("\"(AccountName|PersonaName)\"\\s+\"([^\"]*)\"")
var unread = false
for (file in loginUsers) {
if (!file.isFile) continue
val text = try { file.readText() } catch (e: Exception) { unread = true; continue }
kv.findAll(text).map { it.groupValues[2].trim() }.filter { it.length >= 3 }.forEach { names += it }
}
accounts = found
val learned = names.map { name ->
val needs = name.lowercase().takeIf { name.all { it.code < 0x80 } }
Learned(Regex("(?i)(?<![A-Za-z0-9_])" + Regex.escape(name) + "(?![A-Za-z0-9_])"), needs)
}
// A file that could not be read keeps the names learned from it before.
accounts = if (unread) (accounts + learned).distinctBy { it.regex.pattern } else learned
}
/** Everything the session's runtime can tell about whose logs these are: addresses and accounts. */
fun learnFromRuntime(root: java.io.File) {
learnOwnAddresses(java.io.File(root, "etc/droiddeck-net"))
learnAccounts(java.io.File(root, "root/.local/share/Steam/config/loginusers.vdf"))
learnAccounts(
java.io.File(root, "root/.local/share/Steam/config/loginusers.vdf"),
java.io.File(root, "root/.droiddeck-x64/.local/share/Steam/config/loginusers.vdf"),
)
}
/** This device's public addresses as patterns (see [learnOwnAddresses]); empty until learned. */
@Volatile
private var own: List<Learned> = emptyList()
/**
* Learns the device's addresses from the link file the app writes for the session
* (`addr <address> <prefix>` lines). Global IPv6 become their /64, public IPv4 stay exact;
* private, link-local and loopback addresses identify nobody and are left out.
*/
fun learnOwnAddresses(linkFile: java.io.File) {
val found = ArrayList<Learned>()
try {
@@ -144,7 +87,6 @@ object LogRedactor {
when (kind(addr)) {
"public IPv6" -> {
val groups = expand6(addr)?.take(4) ?: return@forEachLine
// Any spelling of an address in this /64: leading zeros dropped, :: anywhere after.
val prefix = groups.joinToString(":") { g -> "0{0,3}" + Regex.escape(g.trimStart('0').ifEmpty { "0" }) }
found += Learned(
Regex("(?i)(?<![0-9A-Fa-f:])$prefix(?::[0-9A-Fa-f]{0,4}){1,4}(?:%[A-Za-z0-9_.]+)?"),
@@ -160,7 +102,6 @@ object LogRedactor {
own = found
}
/** "public IPv6", "private IPv4", "link-local IPv6", ... for an address literal; null if it is not one. */
fun kind(address: String): String? {
val a = address.substringBefore('%')
IPV4.matchEntire(a)?.let { m ->
@@ -184,7 +125,6 @@ object LogRedactor {
}
}
/** The eight groups of an IPv6 literal, or null. */
private fun expand6(address: String): List<String>? {
val a = address.substringBefore('%')
if (!a.contains(':') || a.count { it == ':' } > 7 || a.contains(":::")) return null
@@ -200,17 +140,12 @@ object LogRedactor {
return parts.map { it.lowercase() }
}
/**
* Every IP address in [text] replaced by what kind it is ("<public IPv6>"), for reports whose
* point is the shape of the network, not its numbers.
*/
fun describeAddresses(text: String): String {
var out = IPV6.replace(text) { m -> kind(m.value)?.let { "<$it>" } ?: m.value }
out = IPV4.replace(out) { m -> kind(m.value)?.let { "<$it>" } ?: m.value }
return out
}
/** A file worth scrubbing: text (no NUL in its first 4 KB) and not huge. */
fun isText(file: java.io.File): Boolean = try {
file.isFile && file.length() < 64L * 1024 * 1024 && file.inputStream().use { input ->
val buf = ByteArray(4096)
@@ -221,34 +156,16 @@ object LogRedactor {
false
}
/**
* Bump whenever a pattern is added or tightened. A session folder records the version it was
* scrubbed under (SessionArtifacts.SCRUBBED_TREE_MARKER), and a share copies a file as it is
* only when that matches, so a folder scrubbed under older rules is scrubbed again on the way
* out.
*/
const val RULES_VERSION = 3
/**
* A private IPv4 address (10/8, 172.16/12, 192.168/16, link-local 169.254/16): the home
* network's layout, e.g. its router as the resolver. Blanked in a shared zip only; public
* server addresses stay. Not part of a longer dotted number (a version like 10.0.19041.1).
*/
private val LAN_IPV4 = Regex(
"(?<![\\d.])(?:10\\.\\d{1,3}|192\\.168|172\\.(?:1[6-9]|2\\d|3[01])|169\\.254)\\.\\d{1,3}\\.\\d{1,3}(?!\\.?\\d)"
)
/** [src]'s lines, scrubbed, to [out]. */
fun scrubTo(src: java.io.File, out: java.io.Writer) {
src.forEachLine { line -> out.write(redact(line)); out.write("\n") }
}
/**
* The last pass for a file going into a shared zip: [redact], then the rules the stores' log
* uses too ([SecretScrub]) - URLs without query, fragment and user:password and with any
* token-bearing path segment blanked, token values and Authorization / Cookie headers blanked
* - and private IPv4 addresses as `<lan-address>`. A line already clean comes out unchanged.
*/
fun redactForShare(line: String): String {
if (line.isEmpty()) return line
return try {
@@ -259,23 +176,15 @@ object LogRedactor {
}
}
/** [src]'s lines through [redactForShare], to [out]: the pass every text file in a shared zip gets. */
fun scrubForShare(src: java.io.File, out: java.io.Writer) {
src.forEachLine { line -> out.write(redactForShare(line)); out.write("\n") }
}
/** `<redacted:account>`, inside [value]'s quotes when it had them. */
private fun quoted(value: String): String {
val q = value.first().takeIf { it == '"' || it == '\'' }?.toString().orEmpty()
return "$q<redacted:account>$q"
}
/**
* A line on its way through [redact]. Each rule runs only on a line holding what it cannot match
* without - a word, enough separators, a long enough run: most of a Steam log is lines no rule
* touches, and a session's logs went through every rule at about a megabyte a second. A line with anything but ASCII in it goes through
* every case-insensitive rule, since Android's regex folds case more widely than lowercase().
*/
private class Line(var text: String) {
private val unfolded = text.any { it.code >= 0x80 }
private var lower: String? = null
@@ -308,7 +217,6 @@ object LogRedactor {
private fun isHex(c: Char) = c in '0'..'9' || c in 'a'..'f' || c in 'A'..'F'
/** [line] with every credential shape replaced. Null- and exception-safe by construction. */
fun redact(line: String): String {
if (line.isEmpty()) return line
return try {
@@ -328,7 +236,6 @@ object LogRedactor {
}
if (out.has("guard", "factor", "2fa")) out.set(GUARD_CODE.replace(out.text) { "${it.groupValues[1]}<redacted:code>" })
if (out.hasRun(32, ::isHex)) out.set(WEBAPI_KEY.replace(out.text, "<redacted:key>"))
// Mask, not delete: the last four digits let a reader correlate lines to one account.
if ("76561" in out.text) out.set(STEAMID64.replace(out.text) { "${it.groupValues[1]}********${it.groupValues[3]}" })
if ("[U:1:" in out.text) out.set(STEAMID3.replace(out.text) { m ->
val id = m.groupValues[1]
@@ -352,8 +259,6 @@ object LogRedactor {
}
out.text
} catch (t: Throwable) {
// A log line is never worth crashing a session over, but an unscrubbed one must not
// reach the file either.
"<redaction failed; line withheld>"
}
}
@@ -10,24 +10,11 @@ import java.io.File
import java.net.Inet4Address
import java.net.Inet6Address
/**
* `network.txt`: what the session was given to work with, for the reports that say "the client
* thinks WiFi is off" or "Firefox cannot resolve anything".
*
* The runtime's NetworkManager bridge reports Android's active link. Include whether the phone
* had a validated link and which DNS servers the app wrote into the runtime's `resolv.conf`.
*
* **No network names.** The SSID is identifying and is deliberately not collected; the transport
* type, the addresses' families and the DNS servers are what a diagnosis needs.
*/
object NetworkReport {
private const val TAG = "NetworkReport"
fun write(context: Context, target: File) {
try {
// Addresses as their kind ("<public IPv6>"), never the numbers: a public address is the
// user's, and this file goes into shared session logs. The session's made-up hardware
// address is stable per device (seeded from ANDROID_ID), so it is masked too.
val text = LogRedactor.describeAddresses(build(context))
.replace(Regex("(?m)^(\\s*mac )\\S+"), "$1<masked>")
target.writeText(text)
@@ -82,8 +69,6 @@ object NetworkReport {
append("\nWhat the runtime was given\n")
append("--------------------------\n")
// The app writes this file from the live link at every change; it is what everything inside
// the session resolves with, so a mismatch with the system's list above is the bug itself.
val resolv = File(LinuxRuntime.rootDir(context), "etc/resolv.conf")
if (resolv.isFile) {
append("etc/resolv.conf:\n")
@@ -5,7 +5,6 @@ import android.os.Build
import android.provider.Settings
import com.droiddeck.launcher.R
/** Android's phantom process monitor can kill the many child processes used by a Steam session. */
enum class PhantomProcessStatus {
NOT_APPLICABLE,
DISABLED,
@@ -27,10 +26,12 @@ object PhantomProcessLimit {
usesDeviceConfig(sdk) && enabled -> listOf(
"device_config delete activity_manager max_phantom_processes",
"device_config set_sync_disabled_for_tests none",
"settings put global $SETTING true",
)
usesDeviceConfig(sdk) -> listOf(
"device_config set_sync_disabled_for_tests persistent",
"device_config put activity_manager max_phantom_processes $MAX_PHANTOM",
"settings put global $SETTING false",
)
else -> listOf("settings put global $SETTING ${if (enabled) "true" else "false"}")
}
@@ -58,7 +59,12 @@ object PhantomProcessLimit {
if (sdk < Build.VERSION_CODES.S) return PhantomProcessStatus.NOT_APPLICABLE
if (usesDeviceConfig(sdk)) {
val off = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getBoolean(PREF_ANDROID_12_OFF, false)
return if (off) PhantomProcessStatus.DISABLED else PhantomProcessStatus.UNREADABLE
val global = try {
Settings.Global.getString(context.contentResolver, SETTING)
} catch (_: Exception) {
null
}
return android12Status(off, global, systemProperty(OVERRIDE_PROPERTY))
}
val global = try {
Settings.Global.getString(context.contentResolver, SETTING)
@@ -68,12 +74,10 @@ object PhantomProcessLimit {
return status(global, systemProperty(OVERRIDE_PROPERTY))
}
/**
* The value Android itself acts on, in FeatureFlagUtils.isEnabled's order: the global setting
* when it is set, otherwise the feature-flag override property. Developer options' "Disable
* child process restrictions" writes only the property, so a device switched off there has no
* global setting at all and must not be reported as unset.
*/
fun android12Status(rememberedOff: Boolean, global: String?, override: String?): PhantomProcessStatus =
if (rememberedOff || status(global, override) == PhantomProcessStatus.DISABLED) PhantomProcessStatus.DISABLED
else PhantomProcessStatus.UNREADABLE
fun status(global: String?, override: String?): PhantomProcessStatus {
val value = global?.trim()?.lowercase()?.takeIf { it.isNotEmpty() }
?: override?.trim()?.lowercase()?.takeIf { it.isNotEmpty() }
@@ -122,7 +126,6 @@ object PhantomProcessLimit {
fun fixSentence(context: Context, sdk: Int = Build.VERSION.SDK_INT): String =
context.getString(if (hasDeveloperToggle(sdk)) R.string.phantom_fix_toggle else R.string.phantom_fix_no_toggle)
/** English: for the device report. */
fun reportValue(status: PhantomProcessStatus): String = when (status) {
PhantomProcessStatus.DISABLED -> "disabled (good - the OS will not kill the session's children)"
PhantomProcessStatus.ENABLED -> "ENABLED (the OS may kill the session with no log; turn off Restrict child processes)"
@@ -1,21 +1,5 @@
package com.droiddeck.launcher.core
/**
* The one set of rules that keeps URLs and tokens out of anything the app writes or shares: the
* stores' log ([com.droiddeck.launcher.stores.StoreLog.redactLine]) and the last pass over every
* text file in a shared log zip ([LogRedactor.redactForShare]).
*
* - Every URL loses its query, fragment and `user:password@`. [Urls.SHORT] then keeps only the host
* and a plain first path segment (the stores' lines, where a GOG secure link carries its token in
* the path); [Urls.KEEP_PATH] keeps the path but blanks any segment that carries a token.
* - Outside URLs, the values of token-like keys (`token`, `__token__`, `f_token`, `hdnts`, access /
* refresh / id tokens; exchange and authorization codes of four characters or more, and a bare
* `code=` where it is one: in a query string or on a line with OAuth keys such as `client_id`,
* `redirect_uri` or `state`, never an 8-hex exception code like Proton's `code=c0000005`) and of
* Authorization / Cookie headers are blanked with [mark].
*
* A line already scrubbed comes out unchanged.
*/
object SecretScrub {
enum class Urls { SHORT, KEEP_PATH }
@@ -23,12 +7,9 @@ object SecretScrub {
private val SAFE_SEGMENT = Regex("""[A-Za-z0-9._\-]{1,40}""")
private val TOKEN_SEGMENT = Regex("""(?i)(token|hdnts|hmac|signature|sig|exp)=""")
private val TOKEN = Regex("""(?i)(?<![A-Za-z0-9_])(__token__|f_token|hdnts|access_token|refresh_token|id_token|token)(["']?\s*[=:]\s*["']?)[^\s&;,"'<>}]+""")
/** Codes that are only ever OAuth grants. */
private val GRANT_CODE = Regex("""(?i)(?<![A-Za-z0-9_])(exchange_code|authorization_?code)(["']?\s*=\s*["']?)[^\s&;,"'<>}]{4,}""")
/** A bare `code=`: a grant only right after `?` / `&` (a query string) or beside OAuth keys ([OAUTH_CONTEXT]). */
private val CODE = Regex("""(?i)(?<![A-Za-z0-9_])(code)(["']?\s*=\s*["']?)([^\s&;,"'<>}]{4,})""")
private val OAUTH_CONTEXT = Regex("""(?i)(?<![A-Za-z0-9_])(client_id|client_secret|redirect_uri|state|grant_type|response_type|access_token|refresh_token|id_token)["']?\s*[=:]""")
/** A Windows exception or status code (`code=c0000005`, `code=0x80000003`): never a grant. */
private val STATUS_CODE = Regex("""(?i)(0x)?[0-9a-f]{8}""")
private val HEADER = Regex("""(?i)\b(authorization|cookie|set-cookie)(\s*[:=]\s*).+""")
@@ -4,33 +4,16 @@ import android.os.Process
import android.util.Log
import java.io.BufferedWriter
import java.io.File
import java.io.FileOutputStream
/**
* `app.log`: what the app itself said while the session ran, and `crash.log`: what Android's crash
* buffer holds if something died hard.
*
* Everything this app decides is reported through `Log` - which driver was chosen and why, the
* audio line, a rival client being stopped, the session's exit status, a helper that was missing.
* None of it reaches a user's folder, so a report used to arrive with the settings visible in
* `device.txt` and no record of what actually happened. An app may always read back its own log
* entries, so the simplest complete answer is to run `logcat` filtered to our own pid and keep
* what it says.
*
* `logcat -b crash` earns its own file: proot once died before `main` over a missing library, and
* that appeared in the crash buffer and *nowhere else* - not in the session log, not in the main
* buffer. It is the first thing to read when a session "exits instantly".
*
* Both are best-effort. Where a ROM refuses to hand an app its own entries the files are written
* with a line saying so, which is better than their absence looking like silence.
*/
object SessionLogCapture {
private const val TAG = "SessionLogCapture"
private var pid = -1
private var writer: BufferedWriter? = null
private var target: File? = null
private var syncer: Thread? = null
/** Start mirroring this process's log lines into [target]. Safe to call twice. */
@Synchronized
fun start(target: File) {
stop()
@@ -41,8 +24,6 @@ object SessionLogCapture {
out.flush()
writer = out
this.target = target
// -T 1 starts at the newest line rather than replaying the whole buffer; -v threadtime
// keeps the timestamps and thread ids that make two logs line up.
pid = HostProcess.start(
"/system/bin/logcat -v threadtime -T 1 --pid=" + Process.myPid(),
null, target.parentFile, null,
@@ -51,7 +32,6 @@ object SessionLogCapture {
try {
writer?.apply { write(line); newLine(); flush() }
} catch (e: Exception) {
// A full or unmounted card must not take the session with it.
}
}
}
@@ -59,21 +39,32 @@ object SessionLogCapture {
out.write("logcat could not be started; this ROM may not hand an app its own entries.\n")
out.flush()
}
target.parentFile?.let { syncer = startSyncing(it) }
Log.i(TAG, "app log -> $target (logcat pid $pid)")
} catch (e: Exception) {
Log.w(TAG, "could not start the app log", e)
}
}
/** Stops the capture only if it is the one writing into [dir]: a newer session's is left alone. */
@Synchronized
fun stopFor(dir: File) {
val t = target ?: return
if (t.parentFile?.absolutePath == dir.absolutePath) stop()
}
/** flush() only reaches the page cache, and a hard reset on f2fs lost the last minutes of every
* log. Every 2 s the session folder's files (ours and the native parts') are synced to storage. */
private fun startSyncing(dir: File): Thread = Thread({
while (true) {
try { Thread.sleep(2000) } catch (e: InterruptedException) { break }
dir.listFiles()?.forEach { runCatching { FileOutputStream(it, true).use { s -> s.fd.sync() } } }
}
}, "session-log-sync").apply { isDaemon = true; start() }
@Synchronized
fun stop() {
syncer?.interrupt()
syncer = null
target = null
if (pid != -1) {
try {
@@ -86,23 +77,14 @@ object SessionLogCapture {
try {
writer?.close()
} catch (e: Exception) {
// nothing useful to do with a failure to close a log
}
writer = null
}
/**
* Dump Android's crash buffer into [target]. Called at teardown: a session that was killed is
* exactly when this matters, and the buffer keeps its entries after the process is gone.
*/
fun dumpCrashBuffer(target: File) {
try {
val lines = StringBuilder()
val done = java.util.concurrent.CountDownLatch(1)
// Wait on the process ENDING, not on the first line arriving: the lines come through a
// callback on another thread, so a poll for "is it empty yet" declared an empty buffer
// every time and still cost the wait. exec() returns a pid, never an exit status - the
// first version printed that pid as "rc", which was nonsense in the file.
val pid = HostProcess.start(
"/system/bin/logcat -b crash -d -v threadtime -t 400", null, target.parentFile,
{ done.countDown() },
@@ -2,12 +2,6 @@ package com.droiddeck.launcher.core;
import android.content.Context;
/**
* One thing a running session depends on beside the guest process itself: the audio daemon, the
* rumble listener, the battery the client reads, the network-link file. The session starts its
* parts in order before the guest and stops them in reverse after it. A part is handed the
* application context, never an activity's, so nothing here can keep a screen alive.
*/
public abstract class SessionPart {
private Context app;
@@ -15,7 +9,6 @@ public abstract class SessionPart {
this.app = context.getApplicationContext();
}
/** The application context, valid after {@link #attach}. */
protected final Context app() {
return app;
}
@@ -13,11 +13,6 @@ import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/**
* The small zstd tarballs that ship inside the apk (the Turnip driver, the PulseAudio modules).
* The runtime rootfs is not one of these - it needs hard links and symlinks preserved, which
* {@link com.droiddeck.launcher.runtime.LinuxRuntimeInstaller} handles itself.
*/
public final class TarZst {
private static final String TAG = "TarZst";
@@ -33,7 +28,6 @@ public final class TarZst {
}
public static boolean extract(InputStream source, File destination) {
//noinspection ResultOfMethodCallIgnored
destination.mkdirs();
try (InputStream in = new ZstdCompressorInputStream(new BufferedInputStream(source, 1 << 16));
TarArchiveInputStream tar = new TarArchiveInputStream(in)) {
@@ -42,15 +36,13 @@ public final class TarZst {
File file = ArchivePaths.inside(destination, entry.getName());
if (file == null) continue;
if (entry.isDirectory()) {
//noinspection ResultOfMethodCallIgnored
file.mkdirs();
continue;
}
File parent = file.getParentFile();
if (parent != null && !parent.isDirectory()) //noinspection ResultOfMethodCallIgnored
if (parent != null && !parent.isDirectory())
parent.mkdirs();
if (entry.isSymbolicLink()) {
//noinspection ResultOfMethodCallIgnored
file.delete();
FileUtils.symlink(entry.getLinkName(), file.getAbsolutePath());
continue;
@@ -59,7 +51,7 @@ public final class TarZst {
try (OutputStream out = new FileOutputStream(file)) {
FileUtils.copy(tar, out);
}
if ((entry.getMode() & 0111) != 0) //noinspection ResultOfMethodCallIgnored
if ((entry.getMode() & 0111) != 0)
file.setExecutable(true, false);
}
return true;
@@ -5,29 +5,15 @@ import kotlin.math.ln
import kotlin.math.max
import kotlin.math.min
/**
* Texture filtering for DirectX 9-11 games, as DXVK configuration options
* (d3d9/d3d11.samplerAnisotropy and samplerLodBias). VKD3D-Proton and WineD3D do not read them.
* Published into the game environment and read by DXVK when a game starts, so a change lands on
* the next launch, not the running game.
*/
object TextureFiltering {
/** 0 = the game's own filtering; otherwise that anisotropy on every linearly filtered sampler. */
val ANISOTROPY = listOf(0, 2, 4, 8, 16)
/** Negative mip LOD bias added to the game's own: sharper textures at the cost of some shimmer. */
const val LOD_BIAS_OFF = "0"
/** Derived from how far the session is enlarged to the panel (see [autoLodBias]). */
const val LOD_BIAS_AUTO = "auto"
val LOD_BIAS = listOf(LOD_BIAS_OFF, LOD_BIAS_AUTO, "-0.25", "-0.5", "-0.75", "-1.0")
private const val LOD_BIAS_MIN = -2f
/**
* The mip LOD bias for a picture enlarged by [scale] (panel size over session size, long side
* to long side): -log2(scale), AMD's FSR 1 guidance (a 1.5x enlargement - 720p on a 1080p
* panel - gives -0.58). 0 when the session is not enlarged or the scale is unknown.
*/
fun autoLodBias(scale: Float): Float {
if (scale.isNaN() || scale <= 1f) return 0f
return max((-ln(scale.toDouble()) / ln(2.0)).toFloat(), LOD_BIAS_MIN)
@@ -39,7 +25,6 @@ object TextureFiltering {
else -> choice.toFloatOrNull()?.let { max(min(it, 0f), LOD_BIAS_MIN) } ?: 0f
}
/** The DXVK_CONFIG options for the two settings, "" when both are left to the game. */
fun dxvkOptions(anisotropy: Int, lodBiasChoice: String, autoBias: Float): String {
val parts = ArrayList<String>(4)
val aniso = if (anisotropy in ANISOTROPY) anisotropy else 0
@@ -49,7 +34,6 @@ object TextureFiltering {
}
val bias = lodBias(lodBiasChoice, autoBias)
if (bias < 0f) {
// Locale.US: DXVK parses "-0.58", never "-0,58".
val b = String.format(Locale.US, "%.2f", bias)
parts += "d3d9.samplerLodBias = $b"
parts += "d3d11.samplerLodBias = $b"
@@ -7,11 +7,9 @@ import android.location.LocationManager
import android.os.Build
import com.droiddeck.launcher.session.SessionPrefs
/** Location is used only for Android's Wi-Fi names and scan results, never coordinates. */
object WifiDiscovery {
val permissions = arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
// targetSdk 28 accepts either grant for Wi-Fi scanning.
fun permissionGranted(context: Context): Boolean = permissions.any {
context.checkSelfPermission(it) == PackageManager.PERMISSION_GRANTED
}
@@ -16,7 +16,6 @@ import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
import java.util.ArrayDeque
/** A narrowly scoped Wireless debugging client for the child-process setting repair. */
object WirelessAdbFix {
private const val CERT_FILE = "wireless-adb-cert.pem"
private const val KEY_FILE = "wireless-adb-key.pem"
@@ -66,7 +65,6 @@ object WirelessAdbFix {
return SavedConnection(host, port)
}
/** A saved pairing skips the PIN; an unknown connection port can be filled in from Settings. */
fun savedConnectionAddress(context: Context): String? = savedConnection(context)?.let { saved ->
val host = if (':' in saved.host) "[${saved.host}]" else saved.host
"$host:${saved.port ?: ""}"
@@ -80,7 +78,6 @@ object WirelessAdbFix {
)
}
/** Finds this paired device's separate TLS connection port from Android's ADB mDNS record. */
fun isLocalAddress(address: InetAddress): Boolean = runCatching {
if (address.isLoopbackAddress) return true
NetworkInterface.getNetworkInterfaces()?.toList().orEmpty().any { network ->
@@ -184,7 +181,6 @@ object WirelessAdbFix {
saveConnection(context, host, port)
}
/** Tries the app's saved pairing without asking for a new pairing code. */
fun setUsingSavedPairing(context: Context, enabled: Boolean) {
val appContext = context.applicationContext
val saved = savedConnection(appContext)
@@ -0,0 +1,34 @@
package com.droiddeck.launcher.core
import android.app.ActivityManager
import android.content.Context
import android.os.Build
import java.io.File
object ZramSupport {
private const val GIB = 1L shl 30
fun supported(): Boolean = kernelAtLeast(System.getProperty("os.version").orEmpty(), 5, 4) && swapTotalKb() > 0
fun recommended(context: Context): Boolean {
if (!supported()) return false
val memory = ActivityManager.MemoryInfo()
context.getSystemService(ActivityManager::class.java).getMemoryInfo(memory)
val advertised = if (Build.VERSION.SDK_INT >= 34) memory.advertisedMem else 0L
return belowTwelveGb(advertised, memory.totalMem)
}
fun belowTwelveGb(advertisedBytes: Long, totalBytes: Long): Boolean =
if (advertisedBytes > 0) advertisedBytes < 11 * GIB else totalBytes < 10 * GIB
fun kernelAtLeast(release: String, major: Int, minor: Int): Boolean {
val parts = Regex("^(\\d+)\\.(\\d+)").find(release)?.groupValues ?: return false
val ma = parts[1].toInt()
val mi = parts[2].toInt()
return ma > major || (ma == major && mi >= minor)
}
fun swapTotalKb(meminfo: List<String> = runCatching { File("/proc/meminfo").readLines() }.getOrDefault(emptyList())): Long =
meminfo.firstOrNull { it.startsWith("SwapTotal:") }
?.substringAfter(':')?.trim()?.substringBefore(' ')?.toLongOrNull() ?: 0L
}
@@ -23,15 +23,6 @@ import java.nio.file.Path
import java.nio.file.SimpleFileVisitor
import java.nio.file.attribute.BasicFileAttributes
/**
* The app's own data folder (the Linux runtime, settings, logs) in Android's file picker, under the
* app's name beside the phone's storage - what MT Manager's "documents provider" injection does, as
* part of the app. Only the system's documents UI can bind it (MANAGE_DOCUMENTS); another app sees
* a file only when the user picks it there.
*
* A document id is the file's absolute path, and nothing outside the data folder is ever handed out
* or accepted: every id is resolved and checked to lie inside it.
*/
class AppFilesProvider : DocumentsProvider() {
private lateinit var base: File
private lateinit var baseCanonical: String
@@ -65,8 +56,6 @@ class AppFilesProvider : DocumentsProvider() {
override fun queryChildDocuments(parentDocumentId: String, projection: Array<String>?, sortOrder: String?): Cursor {
val result = MatrixCursor(projection ?: DOCUMENT_COLUMNS)
// A link in the runtime that leads out of the data folder (/etc/localtime and the like) is
// left out rather than listed as something that cannot be opened.
fileFor(parentDocumentId).listFiles()?.filter { inside(it) }?.forEach { addRow(result, it) }
return result
}
@@ -104,10 +93,6 @@ class AppFilesProvider : DocumentsProvider() {
}
}
/**
* Deletes a file or a folder and what is in it without ever following a link: the runtime holds
* links to places outside it, and deleting a folder must remove the link, not what it points at.
*/
private fun deleteTree(root: Path) {
if (!Files.isDirectory(root, LinkOption.NOFOLLOW_LINKS)) {
Files.delete(root)
@@ -158,7 +143,6 @@ class AppFilesProvider : DocumentsProvider() {
val needle = query.lowercase()
val pending = ArrayDeque(listOf(base))
var visited = 0
// The runtime is tens of thousands of files: a bounded walk, not the whole tree.
while (pending.isNotEmpty() && result.count < 50 && visited++ < 20000) {
val f = pending.removeFirst()
if (!inside(f)) continue
@@ -168,7 +152,6 @@ class AppFilesProvider : DocumentsProvider() {
return result
}
/** The file an id names, when it exists and lies inside the data folder (links followed). */
private fun fileFor(documentId: String): File {
val f = File(documentId)
if (!f.isAbsolute || !inside(f)) throw FileNotFoundException("$documentId is outside the app's files")
@@ -177,7 +160,6 @@ class AppFilesProvider : DocumentsProvider() {
}
private fun inside(f: File): Boolean {
// The folder's own entries are listed as they are; a link is judged by where it leads.
val abs = f.absoluteFile.normalize().path
if (abs != base.absolutePath && !abs.startsWith(base.absolutePath + "/")) return false
val real = runCatching { f.canonicalPath }.getOrNull() ?: return false
@@ -225,11 +207,6 @@ class AppFilesProvider : DocumentsProvider() {
Document.COLUMN_LAST_MODIFIED, Document.COLUMN_FLAGS, Document.COLUMN_SIZE,
)
/**
* The name in the picker. The same app ships under other package names (for the performance
* modes some phones give only named apps); each says which it is, so four installs are not
* four identical "DroidDeck" entries.
*/
fun titleFor(context: Context): String {
val name = context.getString(R.string.app_name)
return when (context.packageName) {
@@ -49,38 +49,26 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
/** One tappable entry in a [CollapsibleRail] (a container tab, a file-manager location, …). */
data class RailItem(
val label: String,
val icon: ImageVector,
val selected: Boolean,
/** >0 shows a small accent count badge on the item's icon (expanded and collapsed). */
val badge: Int = 0,
/** The under-icon label when the rail is collapsed, for names too long to fit; null derives one. */
val shortLabel: String? = null,
// onClick stays LAST so existing call sites can pass it as a trailing lambda; badge is an
// optional named param before it (default 0).
val onClick: () -> Unit,
)
/** A group of [RailItem]s under an optional small section header (STORAGE / QUICK / FAVORITES …). */
data class RailSection(val header: String?, val items: List<RailItem>)
/**
* The tiny under-icon label for a COLLAPSED rail item. The rail width never changes, so labels must
* fit the narrow (~58dp) column: multi-word names wrap to a second line; single words too long to
* fit are abbreviated; short ones pass through. (Explicit abbreviations first, then a length guard.)
*/
private fun collapsedLabel(label: String): String {
when (label) {
"ENVIROMENT" -> return "ENVIRON" // the app's existing (mis)spelling, kept uppercase
"ENVIROMENT" -> return "ENVIRON"
"WIN COMPONENTS" -> return "WIN COMP"
}
if (label.contains(' ')) return label // multi-word → wraps to 2 lines
if (label.contains(' ')) return label
return if (label.length > 9) label.take(8) + "…" else label
}
/** A rail item's 20dp icon, with a small accent count badge on its corner when [badge] > 0. */
@Composable
private fun RailItemIcon(item: RailItem, tint: androidx.compose.ui.graphics.Color) {
if (item.badge > 0) {
@@ -99,21 +87,10 @@ private fun RailItemIcon(item: RailItem, tint: androidx.compose.ui.graphics.Colo
}
}
/** A secondary link shown under the header (e.g. "What is all this?", "Reset to app defaults"). */
data class RailLink(val label: String, val icon: ImageVector, val onClick: () -> Unit)
/**
* State for a [CollapsibleRail], with the app-wide unified rule:
* - PORTRAIT → always collapsed (icon-only), the toggle is HIDDEN and it can never expand.
* - LANDSCAPE → expanded by default; the user's collapse choice is remembered PER-SCREEN
* ([screenKey]) and is NOT overridden by rotation. portrait→landscape restores that choice.
*
* Backed by rememberSaveable (survives process death) and by the fact the app UI activity keeps
* orientation in configChanges (no recreate) so the selected item / fields / scroll are preserved.
*/
class RailState internal constructor(
val collapsed: Boolean,
/** True in portrait: the rail is locked collapsed and must not show an expand toggle. */
val portraitLocked: Boolean,
val onToggle: () -> Unit,
)
@@ -121,15 +98,12 @@ class RailState internal constructor(
@Composable
fun rememberRailState(screenKey: String): RailState {
val context = LocalContext.current
// The same file the File Manager keeps its own browse settings in (no androidx.preference here).
val prefs = remember { context.getSharedPreferences("file_manager", android.content.Context.MODE_PRIVATE) }
val isLandscape = LocalConfiguration.current.orientation == Configuration.ORIENTATION_LANDSCAPE
val choseKey = "rail_${screenKey}_userChose"
val collapsedKey = "rail_${screenKey}_collapsed"
// Keep the landscape choice in saveable state regardless of orientation so portrait→landscape
// restores it. In portrait we simply report collapsed+locked and ignore it.
var userChose by rememberSaveable(screenKey) { mutableStateOf(prefs.getBoolean(choseKey, false)) }
var landscapeCollapsed by rememberSaveable(screenKey) {
mutableStateOf(if (prefs.getBoolean(choseKey, false)) prefs.getBoolean(collapsedKey, false) else false)
@@ -152,16 +126,6 @@ fun rememberRailState(screenKey: String): RailState {
)
}
/**
* The shared collapsible left rail (mockup "Option 3"): app glyph + title, optional links, and the
* navigation items, replacing a top tab bar so content runs full height beside it. Width animates
* 190dp ↔ 58dp. When collapsed it is icon-only (the caller surfaces the active item's name over the
* content). Used by the container editors, File Manager and Save Manager.
*
* [footer] is an optional slot pinned to the bottom of the rail (a weighted spacer pushes it down),
* for a persistent per-screen summary (e.g. the Save Manager's "N games need syncing" line). The
* caller renders whatever it wants there and can key it off [RailState.collapsed] to go icon-only.
*/
@Composable
fun CollapsibleRail(
state: RailState,
@@ -170,12 +134,7 @@ fun CollapsibleRail(
modifier: Modifier = Modifier,
links: List<RailLink> = emptyList(),
footer: (@Composable ColumnScope.() -> Unit)? = null,
// Optional header glyph (a themed drawable). When null the rail shows its plain solid square.
// Used by the container editors to show the wine-glass container icon; other rails leave it null.
@DrawableRes headerIcon: Int? = null,
// When true each item gets a rounded outlined-button look (the selected one takes the accent
// border + accent-dim fill), matching the File Manager's "New Folder" button. Off by default so
// the container/save rails keep their lighter flat rows (5 stacked outlined tabs read too heavy).
outlinedItems: Boolean = false,
) {
val collapsed = state.collapsed
@@ -187,7 +146,6 @@ fun CollapsibleRail(
.fillMaxHeight()
.background(MaterialTheme.colorScheme.surfaceContainerHigh)
) {
// ── Header: app glyph + title + collapse handle (handle hidden when portrait-locked) ──
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = if (collapsed) Arrangement.Center else Arrangement.Start,
@@ -228,14 +186,12 @@ fun CollapsibleRail(
}
}
}
// Collapsed (landscape only) → an expand handle; portrait is locked with no handle.
if (collapsed && !state.portraitLocked) {
IconButton(onClick = state.onToggle, modifier = Modifier.fillMaxWidth().height(24.dp)) {
Text("››", color = MaterialTheme.colorScheme.onSurfaceVariant, fontSize = 15.sp)
}
}
// ── Links ──
if (links.isNotEmpty()) {
if (collapsed) {
Row(horizontalArrangement = Arrangement.Center, modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp)) {
@@ -258,8 +214,6 @@ fun CollapsibleRail(
HorizontalDivider(color = MaterialTheme.colorScheme.outline.copy(alpha = 0.4f))
// ── Sections + items (SCROLLABLE so a tall rail - e.g. many File Manager favourites - is
// fully reachable in both orientations; the header above and footer below stay pinned) ──
Column(
modifier = Modifier
.weight(1f)
@@ -283,8 +237,6 @@ fun CollapsibleRail(
}
section.items.forEach { item ->
val active = item.selected
// Outlined variant: each item is a rounded outlined button; selected = accent border +
// accent-dim fill. Flat variant (default): a simple accent-tinted highlight for selected.
val shape = RoundedCornerShape(9.dp)
val base = Modifier.fillMaxWidth()
val styled = if (outlinedItems) {
@@ -308,9 +260,6 @@ fun CollapsibleRail(
val iconTint = if (active) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurfaceVariant
val labelColor = if (active) MaterialTheme.colorScheme.onSurface else MaterialTheme.colorScheme.onSurfaceVariant
if (collapsed) {
// Icon-only width is unchanged - a TINY label goes UNDER the icon (2 lines max,
// centre-aligned, wrapping/abbreviated) so every button stays identifiable without
// widening the rail.
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = styled,
@@ -350,9 +299,8 @@ fun CollapsibleRail(
}
}
} // end scrollable section list (its weight fills the gap so the footer stays pinned)
}
// ── Optional pinned footer (e.g. the sync summary) ──
if (footer != null) {
HorizontalDivider(color = MaterialTheme.colorScheme.outline.copy(alpha = 0.25f))
footer()
@@ -360,16 +308,6 @@ fun CollapsibleRail(
}
}
/**
* Portrait counterpart to [CollapsibleRail]: the same navigation [items] laid out as a horizontal
* icon-over-label bar pinned across the top, with an accent underline under the selected one. Mirrors
* the container editor's top tab bar so every editor reads the same - pin this in portrait wherever a
* [CollapsibleRail] is used in landscape. Optional [links] (Help / Reset …) are appended after a
* faint separator, exactly like the rail's links.
*
* (The container editor still carries its own private copy of this bar; new screens should prefer
* this shared one, and the container editor can migrate to it later.)
*/
@Composable
fun RailTopTabs(
items: List<RailItem>,
@@ -391,7 +329,6 @@ fun RailTopTabs(
)
}
if (links.isNotEmpty()) {
// Faint separator: everything past here is an action button, not a tab.
Box(
modifier = Modifier
.padding(horizontal = 2.dp)
@@ -408,8 +345,6 @@ fun RailTopTabs(
}
}
/** One cell of [RailTopTabs] - icon over a small label, with an accent underline (zero-width when
* unselected, so every cell keeps the same height). Mirrors the container editor's TopCell. */
@Composable
private fun TopTabCell(
icon: ImageVector,
@@ -3,15 +3,6 @@ package com.droiddeck.launcher.files
import android.content.Context
import org.json.JSONArray
/**
* Persists the File Manager's favourite / bookmarked directories.
*
* Stores an ordered (insertion-order) list of absolute path strings as a JSON array
* under "file_manager_favorites" in SharedPreferences "file_manager_prefs". Only the
* absolute path is kept - everything shown on a favourite card (drive label, container
* name, display path) is derived live, so a renamed or deleted container resolves
* correctly for free.
*/
object FavoritesStore {
private const val PREFS_NAME = "file_manager_prefs"
@@ -20,7 +11,6 @@ object FavoritesStore {
private fun prefs(context: Context) =
context.applicationContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
/** Ordered list of favourited absolute paths (insertion order). */
fun list(context: Context): List<String> {
val raw = prefs(context).getString(KEY, null) ?: return emptyList()
return runCatching {
@@ -31,7 +21,6 @@ object FavoritesStore {
fun isFavorite(context: Context, path: String): Boolean = list(context).contains(path)
/** Add [path] if not already present (deduped); keeps insertion order. */
fun add(context: Context, path: String) {
val current = list(context)
if (current.contains(path)) return
@@ -44,7 +33,6 @@ object FavoritesStore {
save(context, current.filterNot { it == path })
}
/** Toggle [path]; returns true if it is now a favourite, false if it was removed. */
fun toggle(context: Context, path: String): Boolean =
if (isFavorite(context, path)) {
remove(context, path); false
@@ -4,30 +4,18 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import java.io.File
// Reading and setting a file's read-only and hidden attributes (the DOS attribute xattr).
// ── DOS file attributes (Read-only / Hidden) ──
// The in-container file manager (wfm.exe) exposes these in its Properties dialog; this mirrors the
// same two toggles for files browsed from the app side. No root - we only touch permission bits the
// app owns (container files) and the Wine DOS-attribute xattr.
// FILE_ATTRIBUTE_HIDDEN, as Wine encodes it in the user.DOSATTRIB extended attribute.
private const val FILE_ATTRIBUTE_HIDDEN = 0x2
private const val DOSATTRIB_XATTR = "user.DOSATTRIB"
// Snapshot of a file's two toggleable attributes. hiddenSupported is false when the underlying
// filesystem can't store the DOSATTRIB xattr (the FUSE /storage volumes) - the Hidden toggle is then
// disabled while Read-only keeps working.
internal data class FileAttrState(
val readOnly: Boolean,
val hidden: Boolean,
val hiddenSupported: Boolean,
)
// Wine (and Samba) store user.DOSATTRIB as an ASCII hex string ("0x22"), sometimes with trailing
// Samba fields after a separator. We only need the leading DOS-attribute hex, so read from the "0x"
// prefix and stop at the first non-hex character.
private fun parseDosAttrib(raw: ByteArray): Int {
val s = String(raw, Charsets.US_ASCII).trim()
val hex = if (s.startsWith("0x") || s.startsWith("0X")) s.substring(2) else s
@@ -35,11 +23,8 @@ private fun parseDosAttrib(raw: ByteArray): Int {
return digits.toIntOrNull(16) ?: 0
}
// Read the current read-only + Wine-hidden state. Never throws: an xattr-unsupported filesystem
// comes back hiddenSupported=false (Hidden toggle disabled), Read-only always resolves.
internal fun readFileAttrs(file: File): FileAttrState {
val path = file.absolutePath
// Read-only reflects the OWNER write bit - that's what Wine maps to FILE_ATTRIBUTE_READONLY.
val readOnly = runCatching {
(android.system.Os.stat(path).st_mode and android.system.OsConstants.S_IWUSR) == 0
}.getOrDefault(!file.canWrite())
@@ -49,8 +34,6 @@ internal fun readFileAttrs(file: File): FileAttrState {
try {
hidden = (parseDosAttrib(android.system.Os.getxattr(path, DOSATTRIB_XATTR)) and FILE_ATTRIBUTE_HIDDEN) != 0
} catch (e: android.system.ErrnoException) {
// ENODATA: xattr namespace works, the attribute just isn't set yet -> still toggleable.
// ENOTSUP/EOPNOTSUPP/anything else: the fs can't store it -> disable the Hidden toggle.
supported = e.errno == android.system.OsConstants.ENODATA
} catch (e: Exception) {
supported = false
@@ -58,10 +41,6 @@ internal fun readFileAttrs(file: File): FileAttrState {
return FileAttrState(readOnly, hidden, supported)
}
// Toggle read-only by flipping the write bits, preserving every other permission bit. Setting
// read-only clears owner/group/other write (so Wine sees FILE_ATTRIBUTE_READONLY regardless of which
// write bit it checks); clearing it restores owner write. Falls back to File.setWritable if chmod is
// somehow refused. Returns true on success.
internal fun setReadOnly(file: File, readOnly: Boolean): Boolean = runCatching {
val path = file.absolutePath
val mode = android.system.Os.stat(path).st_mode
@@ -73,10 +52,6 @@ internal fun setReadOnly(file: File, readOnly: Boolean): Boolean = runCatching {
true
}.getOrElse { file.setWritable(!readOnly, true) }
// Flip Wine's DOS hidden bit in user.DOSATTRIB, preserving the other DOS-attribute bits
// (archive/system/read-only) already encoded there, and synthesizing a minimal value when absent.
// Writes Wine's own "0x%x" format, which Wine reads back natively. Returns false when the fs can't
// store the xattr, so the caller can disable just the Hidden toggle.
internal fun setHidden(file: File, hidden: Boolean): Boolean = try {
val path = file.absolutePath
var attr = try {
@@ -13,7 +13,6 @@ import androidx.compose.ui.Modifier
import com.droiddeck.launcher.ui.DroidDeckTheme
import java.io.File
/** The File Manager as a screen of its own, from the Files tile on the main screen. */
class FileManagerActivity : ComponentActivity() {
override fun attachBaseContext(newBase: android.content.Context) {
super.attachBaseContext(newBase)
@@ -77,23 +77,16 @@ import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
// The File Manager's rows, tiles, favourites and context menu.
// Semantic identity colours for the favourite-card drive badge. Intentionally NOT theme
// accent colours - they identify the storage source at a glance. Returns (background, foreground).
private fun badgeColors(loc: FavLocation): Pair<Color, Color> {
val white = Color(0xFFFFFFFF)
return when {
loc.storage == FavStorage.INTERNAL -> Color(0xFF2E5FB0) to white // blue
loc.storage == FavStorage.SD -> Color(0xFF2E7D32) to white // green
else -> Color(0xFF555555) to white // grey
loc.storage == FavStorage.INTERNAL -> Color(0xFF2E5FB0) to white
loc.storage == FavStorage.SD -> Color(0xFF2E7D32) to white
else -> Color(0xFF555555) to white
}
}
// The context-menu item list shared by the list rows (FileItemRow) and the grid tiles
// (FileGridTile), so both open the identical menu. Gating (isDir/canRun/isInno/looksLikeArchive) is
// recomputed from [file] here - one source of truth for what each item shows. Every item dismisses
// the menu first, then runs its action.
@Composable
private fun FileContextMenuItems(
file: File,
@@ -114,15 +107,12 @@ private fun FileContextMenuItems(
onClick = { onDismissMenu(); onSelect() },
)
MenuItemDivider()
// Properties: basic info + Read-only / Hidden toggles, for ANY file or folder (handy for config
// files like .txt/.cfg/.ini). Kept near the top since it's a common reason to open this menu.
DropdownMenuItem(
text = { Text(stringResource(R.string.fm_properties)) },
leadingIcon = { Icon(Icons.Filled.Info, null, tint = MaterialTheme.colorScheme.primary) },
onClick = { onDismissMenu(); onProperties() },
)
MenuItemDivider()
// Favorites are directories - only folders get the pin toggle.
if (isDir) {
DropdownMenuItem(
text = { Text(if (isFavorite) stringResource(R.string.fm_favorite_remove) else stringResource(R.string.fm_favorite_add)) },
@@ -189,11 +179,8 @@ internal fun FileItemRow(
val context = LocalContext.current
val isDir = file.isDirectory
val isExe = !isDir && file.name.lowercase().let { it.endsWith(".exe") || it.endsWith(".bat") || it.endsWith(".msi") || it.endsWith(".sh") }
// Image files show a real thumbnail instead of the generic file icon (handy when picking a
// wallpaper/icon). Coil sizes the decode to the 36dp slot and caches it, so scrolling stays smooth.
val isImage = !isDir && file.extension.lowercase() in IMAGE_THUMB_EXTS
// For real PE executables, try to pull out the embedded application icon (async, off the main thread).
var exeIcon by remember(file.absolutePath) { mutableStateOf<ImageBitmap?>(null) }
if (!isDir && file.name.lowercase().endsWith(".exe")) {
LaunchedEffect(file.absolutePath) {
@@ -207,8 +194,6 @@ internal fun FileItemRow(
.fillMaxWidth()
.padding(horizontal = 12.dp, vertical = 3.dp)
.combinedClickable(
// In selection mode a tap toggles instead of opening, so you can rattle through a
// folder without long-pressing every single row.
onClick = { if (selectionMode) onToggleSelect() else onTap() },
onLongClick = onLongPress,
),
@@ -231,7 +216,6 @@ internal fun FileItemRow(
Spacer(Modifier.width(4.dp))
}
when {
// Show the executable's own embedded icon when we managed to extract one.
exeIcon != null -> Image(
bitmap = exeIcon!!,
contentDescription = null,
@@ -249,7 +233,6 @@ internal fun FileItemRow(
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier.size(if (compact) 24.dp else 36.dp),
)
// Real image preview. Falls back to the generic file icon while loading or on decode failure.
isImage -> AsyncImage(
model = file,
contentDescription = null,
@@ -313,8 +296,6 @@ internal fun FileItemRow(
}
}
// The ".." row at the top of a folder: one level up, styled like a folder row so it reads as part
// of the list and takes controller focus the same way.
@Composable
internal fun ParentFolderRow(compact: Boolean = false, onTap: () -> Unit) {
Card(
@@ -354,8 +335,6 @@ internal fun ParentFolderRow(compact: Boolean = false, onTap: () -> Unit) {
}
}
// Dedicated Favorites list that replaces the file list while the star toggle is on.
// Reads the store keyed on [favTick] so it recomposes after any pin/unpin.
@Composable
internal fun FavoritesList(
currentDir: File,
@@ -431,9 +410,6 @@ internal fun FavoritesList(
}
}
// A single favourite - matches the FileItemRow card style (surfaceContainer + outline +
// RoundedCornerShape(10.dp)). Shows the folder name, a coloured drive badge + origin text,
// and the full display path; tapping jumps into it, the filled star unpins.
@Composable
private fun FavoriteCard(
file: File,
@@ -472,7 +448,6 @@ private fun FavoriteCard(
overflow = TextOverflow.Ellipsis,
)
Spacer(Modifier.height(2.dp))
// Origin line: coloured drive badge + source description.
Row(verticalAlignment = Alignment.CenterVertically) {
Text(
text = stringResource(loc.driveLabel),
@@ -517,13 +492,6 @@ private fun FavoriteCard(
}
}
/**
* One entry in the File Manager's grid view: a big thumbnail with the name under it.
*
* Deliberately drops size and date - at this width they truncate to noise. The grid is for
* recognising things by sight (screenshots, covers, game folders); the list stays the view for
* reading details.
*/
@OptIn(ExperimentalFoundationApi::class)
@Composable
internal fun FileGridTile(
@@ -554,7 +522,6 @@ internal fun FileGridTile(
if (bmp != null) exeIcon = bmp.asImageBitmap()
}
}
// The tile has no ⋮ button - long-press opens this menu, anchored to the Box around the Card.
Box {
Card(
modifier = Modifier
@@ -608,8 +575,6 @@ internal fun FileGridTile(
}
}
Spacer(Modifier.height(4.dp))
// Always reserve two lines (issue #475): a one-line name used to make its card shorter than
// its neighbours, so grid rows had ragged heights. Fixed min/max keeps every tile the same size.
Text(
file.name,
color = MaterialTheme.colorScheme.onSurface,
@@ -12,33 +12,8 @@ import androidx.annotation.StringRes
import com.droiddeck.launcher.R
import java.io.File
// The File Manager's non-UI helpers: sorting, paths, conflict choices and favourite locations.
/*
* Bannerlator's File Manager (ui/screens/FileManagerScreen.kt), carried over whole: the same
* browsing, grid and list, sorting, search, multi-select, copy/cut/paste with conflicts and
* progress, rename, delete, new folder, properties, favourites and the locations rail, and the
* same pick modes. What this app has no use for is not here: running a file in a Wine container,
* adding it to a container's shortcuts, the Drive C: / Drive Z: locations and the archive
* unpacker that installs into a prefix. Everything else is line for line the same so a fix in
* either app carries to the other.
*/
/**
* What to do when a pasted item already exists at the destination.
*
* OVERWRITE and MERGE resolve to the same call - `copyWithProgress` recurses into an existing
* directory and truncates existing files - but they mean different things to the user, so both are
* offered and the wording is chosen per item type (files overwrite, folders merge).
*/
enum class ConflictChoice { OVERWRITE, MERGE, KEEP_BOTH, SKIP }
/**
* Shortens a path from the LEFT, keeping whole segments.
*
* Compose's TextOverflow can only ellipsise the tail, which for a path throws away the part that
* matters - `/storage/emulated/0/Games/Racing/Dir…` tells you nothing about where you are.
*/
internal fun elidePathStart(path: String, max: Int): String {
if (path.length <= max) return path
val parts = path.split('/').filter { it.isNotEmpty() }
@@ -50,12 +25,9 @@ internal fun elidePathStart(path: String, max: Int): String {
return if (out.isEmpty()) "…" + path.takeLast(max - 1) else "…$out"
}
/** Folder-first ordering stays fixed across both sort directions. */
internal fun comparatorFor(sortBy: String, desc: Boolean): Comparator<File> {
val inner: Comparator<File> = when (sortBy) {
"date" -> compareBy { it.lastModified() }
// Directory length() is meaningless, so folders sort by name within the size ordering
// instead of pretending to have one.
"size" -> compareBy { if (it.isDirectory) -1L else it.length() }
else -> compareBy { it.name.lowercase() }
}
@@ -63,27 +35,23 @@ internal fun comparatorFor(sortBy: String, desc: Boolean): Comparator<File> {
return compareBy<File> { if (it.isDirectory) 0 else 1 }.then(directed)
}
// Image extensions that get a real thumbnail (via Coil) instead of the generic file icon.
internal val IMAGE_THUMB_EXTS = setOf("jpg", "jpeg", "png", "webp", "bmp", "gif")
// True when [child] is [ancestor] itself or lives anywhere inside it.
internal fun isWithin(child: File, ancestor: File): Boolean {
val c = runCatching { child.canonicalPath }.getOrDefault(child.absolutePath)
val a = runCatching { ancestor.canonicalPath }.getOrDefault(ancestor.absolutePath)
return c == a || c.startsWith(a + File.separator)
}
// ── Favorites: origin resolution ──
enum class FavStorage { INTERNAL, SD, OTHER }
data class FavLocation(
val storage: FavStorage,
@StringRes val driveLabel: Int, // Internal, SD card, or Storage
val displayPath: String // the unix absolute path
@StringRes val driveLabel: Int,
val displayPath: String
)
// Resolve where [file] lives (storage source + a friendly label) by prefix-matching its path.
fun describeLocation(file: File): FavLocation {
val abs = file.absolutePath
@@ -99,7 +67,6 @@ fun describeLocation(file: File): FavLocation {
}
}
// The Linux system's root (the app's files/linuxfs, the sessions' /) and anything under it.
if (Regex("/files/linuxfs(/|$)").containsMatchIn(abs)) {
return FavLocation(FavStorage.OTHER, R.string.fm_drive_linux, abs)
}
@@ -98,12 +98,7 @@ import java.util.Date
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun FileManagerScreen(
// Pick mode (issue #73): reuse this File Manager as a themed file picker. When on, editing/run
// features are gated off and tapping a matching file returns it via [onPick]. Defaults keep the
// full-featured File Manager nav destination unchanged.
pickMode: Boolean = false,
// Directory-pick mode (issue #70): only folders are listed, files are hidden, and a
// "Select this folder" action returns the current directory via [onPick]. Implies pickMode.
pickDirMode: Boolean = false,
pickExtensions: List<String> = emptyList(),
initialDir: File? = null,
@@ -113,7 +108,6 @@ fun FileManagerScreen(
val context = LocalContext.current
val scope = rememberCoroutineScope()
// Only matching files are shown in pick mode (directories are always shown). Empty = all files.
val lowerExts = remember(pickExtensions) { pickExtensions.map { it.lowercase() } }
fun matchesPickExt(file: File): Boolean {
if (lowerExts.isEmpty()) return true
@@ -124,18 +118,10 @@ fun FileManagerScreen(
val pickPrefs = remember { context.getSharedPreferences("file_manager", android.content.Context.MODE_PRIVATE) }
val browsePrefs = pickPrefs
val rootDir = remember {
// Both modes: honour an explicit caller-supplied start dir (e.g. Log Manager's game-log
// folder), else open at the INTERNAL STORAGE ROOT. Selection screens (drive-folder pick,
// local component pick, imports) previously defaulted to Download which - combined with the
// currentRoot floor below - trapped users in Download with no way up (reported bug).
initialDir?.takeIf { it.isDirectory } ?: File("/storage/emulated/0")
}
var currentDir by remember { mutableStateOf(rootDir) }
// The up/back FLOOR - back + the up-arrow are disabled while currentDir == currentRoot. It MUST be
// the VOLUME ROOT of the start dir (internal /storage/emulated/0, or an SD card /storage/XXXX-XXXX),
// NOT the start dir itself: otherwise opening at any subfolder disables up/back and traps the user
// there. (Mirrors the volume-root logic in favLocationOf above.)
var currentRoot by remember {
val abs = rootDir.absolutePath
val internal = "/storage/emulated/0"
@@ -145,8 +131,6 @@ fun FileManagerScreen(
val name = abs.removePrefix("/storage/").substringBefore('/')
if (name.isNotEmpty() && name != "emulated" && name != "self") File("/storage/$name") else rootDir
}
// A game's own folder or Proton prefix lives in the app's private storage, which has no
// volume above it: floor at the app's files dir so up/back still reach the library roots.
else -> {
val appFiles = runCatching { context.filesDir.canonicalPath }.getOrNull()
generateSequence(rootDir) { it.parentFile }
@@ -159,40 +143,26 @@ fun FileManagerScreen(
var entries by remember { mutableStateOf(listOf<File>()) }
var selectedEntry by remember { mutableStateOf<File?>(null) }
var showMenuFor by remember { mutableStateOf<File?>(null) }
// Clipboard holds a LIST so one paste can carry a whole selection. Cut/copy semantics are a
// flag on the batch rather than per item - mixing the two in one clipboard has no sane meaning.
var clipboardFiles by remember { mutableStateOf<List<File>>(emptyList()) }
var isCutOperation by remember { mutableStateOf(false) }
// Multi-select. Keyed by absolute path rather than File so a directory reload (which builds
// fresh File objects) doesn't silently drop the selection.
var selectionMode by remember { mutableStateOf(false) }
var selectedPaths by remember { mutableStateOf<Set<String>>(emptySet()) }
// Paste conflict resolution, surfaced from the IO coroutine and answered by the dialog.
var pendingConflict by remember { mutableStateOf<File?>(null) }
var conflictChoice by remember { mutableStateOf<ConflictChoice?>(null) }
var conflictApplyToAll by remember { mutableStateOf(false) }
// Set while a copy/move runs so the progress UI can offer a cancel.
var operationJob by remember { mutableStateOf<kotlinx.coroutines.Job?>(null) }
var pendingBulkDelete by remember { mutableStateOf<List<File>>(emptyList()) }
// Browse controls. Persisted so the list doesn't reset its order every time you open a folder.
var searchQuery by remember { mutableStateOf("") }
var showSearch by remember { mutableStateOf(false) }
var sortBy by remember { mutableStateOf(browsePrefs.getString("fmSortBy", "name") ?: "name") }
var sortDesc by remember { mutableStateOf(browsePrefs.getBoolean("fmSortDesc", false)) }
var showHidden by remember { mutableStateOf(browsePrefs.getBoolean("fmShowHidden", true)) }
var showSortMenu by remember { mutableStateOf(false) }
// View mode: list of cards (default) or a thumbnail grid. Density applies to the list only -
// a grid tile has no second line to compact.
// The grid/list toggle is the SOURCE OF TRUTH in BOTH orientations (it drives the view and its
// choice persists across rotation). Grid is the default - most useful in landscape, and in
// portrait GridCells.Adaptive naturally renders fewer columns (~2). Do NOT force portrait to list:
// that broke the toggle on-device (tapping it did nothing in portrait).
var gridView by remember { mutableStateOf(browsePrefs.getBoolean("fmGridView", true)) }
val showGrid = gridView
var compactRows by remember { mutableStateOf(browsePrefs.getBoolean("fmCompactRows", false)) }
var showNewFolderDialog by remember { mutableStateOf(false) }
var renameTarget by remember { mutableStateOf<File?>(null) }
// Properties sheet (basic info + Read-only / Hidden toggles) target; null when closed.
var propertiesTarget by remember { mutableStateOf<File?>(null) }
var isOperationRunning by remember { mutableStateOf(false) }
var operationLabel by remember { mutableStateOf("") }
@@ -201,24 +171,16 @@ fun FileManagerScreen(
val listState = rememberLazyListState()
val pullState = rememberPullToRefreshState()
// Favorites view: when on, a dedicated bookmarks list replaces the file list.
// favTick is bumped on any add/remove/toggle so the favorites view + per-row star recompute.
var showFavorites by remember { mutableStateOf(false) }
var favTick by remember { mutableIntStateOf(0) }
// resetScroll: jump to the top of the list (true for navigation; false for in-place reloads
// after delete/paste/rename/refresh so the user keeps their scroll position).
fun loadDirectory(dir: File, resetScroll: Boolean = true) {
currentDir = dir
// Remember the browsed directory so the next pick resumes here.
if (pickMode) pickPrefs.edit().putString("lastFilePickerDir", dir.absolutePath).apply()
scope.launch {
val list = withContext(Dispatchers.IO) {
dir.listFiles()?.toList()
// Dir-pick mode: folders only. File-pick: folders + matching files. Else: all.
?.filter { if (pickDirMode) it.isDirectory else !pickMode || it.isDirectory || matchesPickExt(it) }
// Dotfiles are noise in a storage root (.aya, .$recycle_bin$) but occasionally
// the thing you came for, so it's a toggle rather than a permanent filter.
?.filter { showHidden || !it.name.startsWith(".") }
?.sortedWith(comparatorFor(sortBy, sortDesc)) ?: emptyList()
}
@@ -227,7 +189,6 @@ fun FileManagerScreen(
}
}
// Pull-to-refresh: re-list the current directory, keeping scroll position.
if (pullState.isRefreshing) {
LaunchedEffect(true) {
loadDirectory(currentDir, resetScroll = false)
@@ -235,19 +196,13 @@ fun FileManagerScreen(
}
}
// Jump to a drive's root; pins the Back boundary so we don't climb above it.
fun openDrive(dir: File) {
currentRoot = dir
loadDirectory(dir)
}
// Open the start dir without openDrive(): that pins the floor to the start dir itself, which
// greyed out up/back whenever the manager was opened at a game's folder.
LaunchedEffect(Unit) { loadDirectory(rootDir) }
// System/gesture Back: while the Favorites view is open it closes that first; otherwise
// it goes up one directory. Only at the current drive's root with Favorites closed is it
// disabled, letting Back propagate to close the File Manager.
BackHandler(enabled = showFavorites || currentDir != currentRoot) {
if (showFavorites) {
showFavorites = false
@@ -257,15 +212,12 @@ fun FileManagerScreen(
if (parent != null && parent.exists()) loadDirectory(parent)
}
// One level up, never above the current drive's root: shared by the toolbar arrow and the
// ".." row at the top of the list.
val canGoUp = currentDir != currentRoot && currentDir.parentFile?.exists() == true
fun goUp() {
val parent = currentDir.parentFile
if (currentDir != currentRoot && parent != null && parent.exists()) loadDirectory(parent)
}
// Resolve a non-colliding destination in [dir] for [name] (foo.txt -> "foo (1).txt").
fun uniqueDestination(dir: File, name: String): File {
var candidate = File(dir, name)
if (!candidate.exists()) return candidate
@@ -291,12 +243,9 @@ fun FileManagerScreen(
}
}
/** Waits for the user to answer the conflict dialog for [file]; null if they cancelled it. */
suspend fun askConflict(file: File): ConflictChoice? {
pendingConflict = file
conflictChoice = null
// Poll rather than plumb a CompletableDeferred through Compose state - the dialog answers
// by setting conflictChoice, and this coroutine is already off the critical path.
while (pendingConflict != null && conflictChoice == null) kotlinx.coroutines.delay(50)
return conflictChoice
}
@@ -319,12 +268,10 @@ fun FileManagerScreen(
var done = 0
for (src in sources) {
// Pasting a folder into itself or its own subtree would recurse forever.
if (src.isDirectory && isWithin(dstDir, src)) {
failed++
continue
}
// Moving into the folder it already sits in is a no-op.
if (cut && src.parentFile?.absolutePath == dstDir.absolutePath) {
skipped++
continue
@@ -336,16 +283,12 @@ fun FileManagerScreen(
if (conflictApplyToAll) applyToAll = it
} ?: run { skipped++; null } ?: continue
when (choice) {
// Overwrite and Merge both paste onto the real destination: copyWithProgress
// recurses into an existing directory and truncates existing files, so the
// two differ only in what the user expects, not in what we call.
ConflictChoice.OVERWRITE, ConflictChoice.MERGE -> Unit
ConflictChoice.KEEP_BOTH -> dst = uniqueDestination(dstDir, src.name)
ConflictChoice.SKIP -> { skipped++; continue }
}
}
// Progress is per item; with a batch the label carries the overall position.
operationLabel = if (sources.size > 1) {
context.getString(if (cut) R.string.fm_op_moving_batch else R.string.fm_op_copying_batch, done + 1, sources.size, src.name)
} else {
@@ -419,8 +362,6 @@ fun FileManagerScreen(
}
var showDriveMenu by remember { mutableStateOf(false) }
// Re-enumerated whenever we come back to the screen: returning from a container can leave this
// process on a stale storage view, and the volume set has to be re-read rather than cached.
var storageTick by remember { mutableIntStateOf(0) }
val lifecycleOwner = LocalLifecycleOwner.current
DisposableEffect(lifecycleOwner) {
@@ -435,7 +376,6 @@ fun FileManagerScreen(
com.droiddeck.launcher.runtime.LinuxRuntime.rootDir(context).takeIf { File(it, "usr").isDirectory }
}
// ── Dialogs ──
if (showNewFolderDialog) {
var folderName by remember { mutableStateOf("") }
@@ -488,8 +428,6 @@ fun FileManagerScreen(
FilePropertiesDialog(
file = file,
onDismiss = { propertiesTarget = null },
// Attribute changes affect Hidden/read-only which the listing filters/sorts on, so refresh
// in place (keeping scroll) after any toggle applies.
onChanged = { loadDirectory(currentDir, resetScroll = false) },
)
}
@@ -519,7 +457,6 @@ fun FileManagerScreen(
Column {
Text(stringResource(R.string.fm_cannot_undo))
Spacer(Modifier.height(6.dp))
// Name a few so an accidental Select-All is obvious before it's too late.
victims.take(5).forEach {
Text("• ${it.name}", color = MaterialTheme.colorScheme.onSurfaceVariant, fontSize = 12.sp)
}
@@ -557,7 +494,6 @@ fun FileManagerScreen(
)
}
// Paste conflict - one per colliding item, with "apply to all" for a long batch.
pendingConflict?.let { conflict ->
val isDir = conflict.isDirectory
OutlinedAlertDialog(
@@ -605,7 +541,6 @@ fun FileManagerScreen(
}
Column(modifier = Modifier.fillMaxSize()) {
// ── Pick-mode title ──
if (pickMode && !pickerTitle.isNullOrEmpty()) {
Text(
text = pickerTitle,
@@ -618,7 +553,6 @@ fun FileManagerScreen(
.padding(horizontal = 16.dp, vertical = 10.dp),
)
}
// ── Dir-pick action bar: confirm the currently-browsed folder ──
if (pickDirMode) {
Button(
onClick = { onPick?.invoke(currentDir) },
@@ -631,7 +565,6 @@ fun FileManagerScreen(
Text(stringResource(R.string.fm_select_this_folder))
}
}
// ── Path bar ──
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
@@ -644,12 +577,8 @@ fun FileManagerScreen(
}
val currentDriveLabel = stringResource(describeLocation(currentDir).driveLabel)
// Dim the drive chip while the Favorites list is open (it's not the active context).
val driveChipAlpha = if (showFavorites) 0.45f else 1f
Box {
// The drive/location selector opens the drive dropdown, so give it the same outlined
// look as the "New Folder" button + the rail location items - it reads as a button, not
// plain text. Border uses the theme accent token; behaviour unchanged.
val driveChipShape = RoundedCornerShape(8.dp)
Text(
text = " $currentDriveLabel ▾",
@@ -720,13 +649,8 @@ fun FileManagerScreen(
modifier = Modifier.weight(1f),
)
} else if (LocalConfiguration.current.orientation == Configuration.ORIENTATION_PORTRAIT) {
// PORTRAIT: hide the current-folder name - it's redundant with the path bar directly
// below (which shows the full path). The spacer keeps the action icons right-aligned.
Spacer(Modifier.weight(1f))
} else {
// LANDSCAPE: the CURRENT FOLDER, not the full path. A path ellipsised on the right
// hides its tail - the only part that says where you are ("…/Games/Racing/Dir…"). The
// full path moves to the line below, where it has room.
Text(
text = currentDir.name.ifBlank { currentDir.absolutePath },
color = MaterialTheme.colorScheme.onSurface,
@@ -739,7 +663,6 @@ fun FileManagerScreen(
}
if (!showFavorites) {
// Keep New Folder in the toolbar so the file list uses the full height.
if (!pickMode) {
OutlinedButton(
onClick = { showNewFolderDialog = true },
@@ -777,8 +700,6 @@ fun FileManagerScreen(
Text(if (sortBy == key) "$label ${if (sortDesc) "↓" else "↑"}" else label)
},
onClick = {
// Tapping the active field flips direction; a different
// field switches to it ascending.
if (sortBy == key) sortDesc = !sortDesc else { sortBy = key; sortDesc = false }
browsePrefs.edit().putString("fmSortBy", sortBy)
.putBoolean("fmSortDesc", sortDesc).apply()
@@ -809,7 +730,6 @@ fun FileManagerScreen(
}
}
// Star toggle: open/close the dedicated Favorites list.
IconButton(onClick = { showFavorites = !showFavorites }) {
if (showFavorites) {
Icon(Icons.Filled.Star, stringResource(R.string.fm_hide_favorites), tint = MaterialTheme.colorScheme.primary)
@@ -819,7 +739,6 @@ fun FileManagerScreen(
}
}
// ── Search field ── filters the current folder only; it is not a recursive search.
if (showSearch && !showFavorites) {
OutlinedTextField(
value = searchQuery,
@@ -830,7 +749,6 @@ fun FileManagerScreen(
)
}
// Free space on the volume being browsed - worth knowing before starting a 60 GB copy.
val freeSpace = remember(currentDir.absolutePath, entries) {
runCatching { currentDir.usableSpace }.getOrDefault(0L)
}
@@ -840,14 +758,10 @@ fun FileManagerScreen(
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 2.dp),
) {
Text(
// Elided from the LEFT: the deepest part of a path is the informative part, so
// when it doesn't fit we drop the /storage/emulated/0 prefix, not the tail.
text = elidePathStart(currentDir.absolutePath, 52),
color = MaterialTheme.colorScheme.onSurfaceVariant,
fontSize = 12.sp,
maxLines = 1,
// fill = true: the path takes all remaining width, so the free-space figure
// is pinned to the right edge instead of sliding around with the path length.
modifier = Modifier.weight(1f),
)
if (freeSpace > 0) {
@@ -863,7 +777,6 @@ fun FileManagerScreen(
HorizontalDivider(color = MaterialTheme.colorScheme.outline)
// ── Selection bar ── replaces the paste banner while picking items.
if (selectionMode) {
Row(
verticalAlignment = Alignment.CenterVertically,
@@ -878,7 +791,6 @@ fun FileManagerScreen(
fontSize = 13.sp,
modifier = Modifier.weight(1f),
)
// Compact outlined buttons so all five fit one row alongside the count.
val selBarPadding = PaddingValues(horizontal = 10.dp, vertical = 2.dp)
OutlinedButton(
onClick = {
@@ -924,7 +836,6 @@ fun FileManagerScreen(
}
}
// ── Paste banner ──
if (clipboardFiles.isNotEmpty()) {
Row(
verticalAlignment = Alignment.CenterVertically,
@@ -948,7 +859,6 @@ fun FileManagerScreen(
}
}
// ── Progress overlay ──
if (isOperationRunning) {
Column(
modifier = Modifier
@@ -966,8 +876,6 @@ fun FileManagerScreen(
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(1f),
)
// A multi-gigabyte copy onto a slow card is exactly when you discover you
// picked the wrong folder; without this the only way out was killing the app.
if (operationJob != null) {
TextButton(onClick = {
operationJob?.cancel()
@@ -996,10 +904,6 @@ fun FileManagerScreen(
}
}
// ── Left locations rail (mockup Option 2) + content ──
// Shared collapsible rail: landscape expanded by default, portrait collapsed icon-only. Not
// shown in pick mode (the themed picker keeps its slim layout). Built each recompose (cheap)
// so it tracks the current drive/favourites without stale click lambdas.
val fmRailState = rememberRailState("filemanager")
fun locItem(label: String, icon: androidx.compose.ui.graphics.vector.ImageVector, dir: File, shortLabel: String? = null) =
RailItem(label, icon, !showFavorites && currentRoot.absolutePath == dir.absolutePath, shortLabel = shortLabel) {
@@ -1012,15 +916,12 @@ fun FileManagerScreen(
showFavorites = false; if (d.readable) openDrive(d.dir)
})
}
// The Linux system the sessions run in: its own / (the desktop's root), once installed.
linuxRoot?.let { add(locItem(stringResource(R.string.fm_drive_linux), Icons.Filled.Terminal, it)) }
}
val quickItems = buildList {
File("/storage/emulated/0/Download").takeIf { it.isDirectory }?.let { add(locItem(stringResource(R.string.fm_rail_downloads), Icons.Filled.Download, it, stringResource(R.string.fm_rail_downloads_short))) }
// The ROMs folder chosen on the main screen: what the session shows as /root/ROMs.
SessionPrefs.romsDir(context).takeIf { it.isNotEmpty() }?.let(::File)?.takeIf { it.isDirectory }
?.let { add(locItem(stringResource(R.string.fm_rail_roms), Icons.Filled.SportsEsports, it)) }
// The app's own logs folder: private, which this file manager (the app itself) can read.
com.droiddeck.launcher.runtime.LinuxRuntime.logDir(context).takeIf { it.isDirectory }?.let { add(locItem(stringResource(R.string.fm_rail_session_logs), Icons.Filled.Description, it)) }
File("/storage/emulated/0/Pictures").takeIf { it.isDirectory }?.let { add(locItem(stringResource(R.string.fm_rail_pictures), Icons.Filled.Image, it)) }
}
@@ -1033,12 +934,10 @@ fun FileManagerScreen(
}
Row(modifier = Modifier.weight(1f).fillMaxWidth()) {
// The slim picker keeps its layout: no rail.
if (!pickMode) {
CollapsibleRail(state = fmRailState, title = stringResource(R.string.fm_rail_title), sections = locationSections, outlinedItems = true)
}
Box(modifier = Modifier.weight(1f).fillMaxSize()) {
// ── Favorites list OR file list ──
if (showFavorites) {
FavoritesList(
currentDir = currentDir,
@@ -1060,7 +959,6 @@ fun FileManagerScreen(
modifier = Modifier.fillMaxSize(),
)
} else {
// ── File list (pull down to refresh) ──
Box(
modifier = Modifier
.fillMaxSize()
@@ -1088,8 +986,6 @@ fun FileManagerScreen(
selected = file.absolutePath in selectedPaths,
onLongPress = {
if (!pickMode) {
// In selection mode a long-press toggles; otherwise it opens the
// same context menu the list rows show (the tile has no ⋮ button).
if (selectionMode) {
selectedPaths = if (file.absolutePath in selectedPaths)
selectedPaths - file.absolutePath
@@ -1168,8 +1064,6 @@ fun FileManagerScreen(
selectionMode = selectionMode,
selected = file.absolutePath in selectedPaths,
onLongPress = {
// In selection mode a long-press toggles; otherwise it opens the
// per-item context menu (matching the grid tiles).
if (!pickMode) {
if (selectionMode) {
selectedPaths = if (file.absolutePath in selectedPaths)
@@ -1223,8 +1117,6 @@ fun FileManagerScreen(
}
}
}
// material3 1.2.0's PullToRefreshContainer draws its indicator even at rest;
// only show it while the user is actively pulling or a refresh is running.
if (pullState.verticalOffset > 0.5f || pullState.isRefreshing) {
PullToRefreshContainer(
state = pullState,
@@ -1233,7 +1125,7 @@ fun FileManagerScreen(
}
}
}
} // end content Box (beside the rail)
} // end rail + content Row
}
}
}
}
@@ -10,15 +10,9 @@ import java.io.IOException
import java.nio.file.Files
import java.util.Locale
/**
* The file operations the File Manager needs, as Bannerlator's FileUtils has them: a delete that
* says whether it worked, a copy and a move that report bytes as they go, and the size format
* the rows use. Kept apart from core/FileUtils, whose callers want a quiet delete.
*/
object FileOps {
private const val TAG = "FileOps"
/** Byte-accurate copy progress (copied/total bytes), reported as the copy proceeds. */
fun interface ProgressCallback { fun onProgress(copiedBytes: Long, totalBytes: Long) }
fun formatBytes(context: Context, bytes: Long): String {
@@ -30,7 +24,6 @@ object FileOps {
fun isSymlink(file: File): Boolean = try { Files.isSymbolicLink(file.toPath()) } catch (e: Exception) { false }
/** Recursive delete that never follows a symlink; true when everything went. */
fun delete(target: File?): Boolean {
if (target == null) return false
if (target.isDirectory && !isSymlink(target)) {
@@ -46,20 +39,12 @@ object FileOps {
return ok
}
/** Recursive total size in bytes of files under [f] (skips symlinks). Used to size a progress bar. */
fun totalSize(f: File?): Long {
if (f == null || !f.exists() || isSymlink(f)) return 0
if (f.isDirectory) return f.listFiles()?.sumOf { totalSize(it) } ?: 0
return f.length()
}
/**
* Moves [src] to [dst], relinking instead of copying whenever possible. A rename is instant
* regardless of size but only works within one filesystem; renameTo returning false is the
* signal that a boundary was crossed, so it doubles as the check, and the fallback is
* copy-then-delete. Only attempted when the destination does not exist: renaming onto an
* existing directory does not merge.
*/
fun moveWithProgress(src: File, dst: File, progress: ProgressCallback?): Boolean {
if (sameFile(src, dst)) return true
if (!dst.exists() && src.renameTo(dst)) {
@@ -70,7 +55,6 @@ object FileOps {
return delete(src)
}
/** A copy that reports byte progress, smooth even within one large file. */
fun copyWithProgress(src: File, dst: File, progress: ProgressCallback?): Boolean {
val total = maxOf(totalSize(src), 1L)
return copyWithProgress(src, dst, longArrayOf(0), total, progress)
@@ -96,7 +80,7 @@ object FileOps {
}
val parent = dst.parentFile
if (!src.exists() || (parent != null && !parent.exists() && !parent.mkdirs())) return false
val chunk = 4L * 1024 * 1024 // report progress every 4 MiB
val chunk = 4L * 1024 * 1024
try {
FileInputStream(src).channel.use { input ->
FileOutputStream(dst).channel.use { output ->
@@ -123,7 +107,6 @@ object FileOps {
private fun sameFile(a: File, b: File): Boolean =
try { a.canonicalPath == b.canonicalPath } catch (e: IOException) { a.absolutePath == b.absolutePath }
/** True when [child] is [ancestor] itself or lives anywhere inside it. */
private fun isWithin(child: File, ancestor: File): Boolean {
val c = try { child.canonicalPath } catch (e: IOException) { child.absolutePath }
val a = try { ancestor.canonicalPath } catch (e: IOException) { ancestor.absolutePath }
@@ -16,16 +16,6 @@ import com.droiddeck.launcher.R
import com.droiddeck.launcher.ui.DroidDeckTheme
import java.io.File
/**
* Themed host for [FileManagerScreen] in pick mode. Launched through [InAppFilePicker] and answers with the
* picked path in [EXTRA_SELECTED_FILE] on RESULT_OK.
*
* Extras:
* - [EXTRA_EXTENSIONS] allowed lowercase extensions, no dot; absent or empty = all files
* - [EXTRA_PICK_DIRECTORY] true to pick a folder instead of a file
* - [EXTRA_INITIAL_DIRECTORY] where to open; absent = the last folder picked from, then internal storage
* - [EXTRA_PICKER_TITLE] the header
*/
class FilePickerActivity : ComponentActivity() {
override fun attachBaseContext(newBase: android.content.Context) {
super.attachBaseContext(newBase)
@@ -36,7 +26,6 @@ class FilePickerActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestedOrientation = com.droiddeck.launcher.session.SessionPrefs.orientation(this)
// targetSdk 28: the old storage permission is exactly what lets us list the card.
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1)
}
@@ -44,7 +33,6 @@ class FilePickerActivity : ComponentActivity() {
val extensions = intent.getStringArrayExtra(EXTRA_EXTENSIONS)?.toList() ?: emptyList()
val pickDir = intent.getBooleanExtra(EXTRA_PICK_DIRECTORY, false)
val title = intent.getStringExtra(EXTRA_PICKER_TITLE)
// The screen remembers the folder it last picked from; an explicit start wins over it.
val prefs = getSharedPreferences("file_manager", MODE_PRIVATE)
val initialDir = (intent.getStringExtra(EXTRA_INITIAL_DIRECTORY) ?: prefs.getString("lastFilePickerDir", null))
?.let { File(it) }?.takeIf { it.isDirectory }
@@ -70,7 +58,6 @@ class FilePickerActivity : ComponentActivity() {
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
// The screen re-lists on its own when the grant lands; nothing to relay.
if (requestCode == 1 && grantResults.firstOrNull() == PackageManager.PERMISSION_GRANTED) recreate()
}
@@ -39,17 +39,7 @@ import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
// The File Manager's properties dialog.
/**
* Properties sheet for a single file or folder: basic info plus the two Windows/Wine file attributes
* the in-container file manager (wfm.exe) also exposes - Read-only and Hidden. Each toggle applies
* immediately (off the main thread) and refreshes the listing via [onChanged].
*
* State is keyed on the file so it always reflects the entry it was opened for. On a filesystem that
* can't store Wine's DOSATTRIB xattr (the FUSE /storage volumes) only the Hidden toggle is disabled;
* Read-only keeps working everywhere.
*/
@Composable
internal fun FilePropertiesDialog(
file: File,
@@ -60,7 +50,6 @@ internal fun FilePropertiesDialog(
val scope = rememberCoroutineScope()
val dateFormat = remember { SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault()) }
var attrs by remember(file.absolutePath) { mutableStateOf<FileAttrState?>(null) }
// Guards against a second toggle landing while the first is still being applied off-thread.
var busy by remember(file.absolutePath) { mutableStateOf(false) }
LaunchedEffect(file.absolutePath) {
attrs = withContext(Dispatchers.IO) { readFileAttrs(file) }
@@ -71,7 +60,6 @@ internal fun FilePropertiesDialog(
title = { Text(stringResource(R.string.fm_properties)) },
text = {
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
// ── Basic info ──
Text(
text = file.name,
color = MaterialTheme.colorScheme.onBackground,
@@ -95,7 +83,6 @@ internal fun FilePropertiesDialog(
Spacer(Modifier.height(6.dp))
val state = attrs
// ── Read-only ── checked when the owner can't write (Wine's FILE_ATTRIBUTE_READONLY).
AttributeToggleRow(
label = stringResource(R.string.fm_read_only),
description = stringResource(R.string.fm_read_only_hint),
@@ -116,7 +103,6 @@ internal fun FilePropertiesDialog(
},
)
Spacer(Modifier.height(2.dp))
// ── Hidden ── Wine's DOS hidden bit in the user.DOSATTRIB xattr.
val hiddenSupported = state?.hiddenSupported == true
AttributeToggleRow(
label = stringResource(R.string.fm_hidden),
@@ -134,7 +120,6 @@ internal fun FilePropertiesDialog(
attrs = attrs?.copy(hidden = want)
onChanged()
} else {
// The write failed after all - disable the toggle rather than lie.
attrs = attrs?.copy(hiddenSupported = false)
Toast.makeText(context, R.string.fm_hidden_failed, Toast.LENGTH_SHORT).show()
}
@@ -147,7 +132,6 @@ internal fun FilePropertiesDialog(
)
}
// One "label: value" line in the Properties info block.
@Composable
private fun PropertyLine(label: String, value: String) {
Row(modifier = Modifier.fillMaxWidth().padding(vertical = 2.dp)) {
@@ -168,7 +152,6 @@ private fun PropertyLine(label: String, value: String) {
}
}
// A labelled attribute switch with a one-line description, used by the Properties sheet.
@Composable
private fun AttributeToggleRow(
label: String,
@@ -5,14 +5,7 @@ import android.content.Intent
import android.net.Uri
import java.io.File
/**
* The app's own file picker, the way Bannerlator's File Manager offers its pick mode: one intent
* builder per kind of pick, and the picked path read back from the result. Every import on the
* main screen goes through here rather than Android's document picker, which cannot show an SD
* card as a card, remembers nothing, and hands back a content URI the runtime cannot bind.
*/
object InAppFilePicker {
/** Pick one file. [extensions] are lowercase, without the dot; empty = any file. */
fun buildIntent(
context: Context,
extensions: List<String> = emptyList(),
@@ -24,7 +17,6 @@ object InAppFilePicker {
initialDir?.let { putExtra(FilePickerActivity.EXTRA_INITIAL_DIRECTORY, it) }
}
/** Pick a folder: only folders are listed, and "Use this folder" returns the one open. */
fun buildDirIntent(
context: Context,
title: String? = null,
@@ -39,6 +31,5 @@ object InAppFilePicker {
fun pickedFile(data: Intent?): File? = pickedPath(data)?.let { File(it) }
/** The picked file as the `file:` URI the import code already accepts. */
fun pickedUri(data: Intent?): Uri? = pickedFile(data)?.let { Uri.fromFile(it) }
}
@@ -10,9 +10,6 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
// Shared outlined-card look for the app's popup menus, matching the FileManager / CommunityCard
// idiom (surfaceContainer fill, 1dp outline, rounded 10dp). Applied to a DropdownMenu's modifier so
// the popup reads as an outlined card. Keep all three menu call-sites on this so they stay identical.
@Composable
internal fun Modifier.outlinedMenuCard(): Modifier {
val cs = MaterialTheme.colorScheme
@@ -23,7 +20,6 @@ internal fun Modifier.outlinedMenuCard(): Modifier {
.border(1.dp, cs.outline, shape)
}
// The thin low-alpha separator that sits between items inside an outlined menu card.
@Composable
internal fun MenuItemDivider() {
HorizontalDivider(color = MaterialTheme.colorScheme.outline.copy(alpha = 0.5f))
@@ -12,12 +12,6 @@ import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.DialogProperties
// Single source of truth for the app's AlertDialogs: a drop-in wrapper around the Material3
// slot-based AlertDialog that adds a 1dp outline around the whole popup box - matching the
// outline that Modifier.outlinedMenuCard() (MenuStyle.kt) puts on the app's menus. Signature
// mirrors androidx.compose.material3.AlertDialog exactly (compose-bom 2024.02.00 → material3
// 1.2.0), so every call site can swap AlertDialog( → OutlinedAlertDialog( unchanged. The
// border shares the dialog's shape so its corner radius follows the rounded dialog.
@Composable
internal fun OutlinedAlertDialog(
onDismissRequest: () -> Unit,
@@ -6,21 +6,11 @@ import java.io.File
import java.io.RandomAccessFile
import kotlin.math.max
/**
* Extracts the embedded application icon from a Windows PE executable (.exe).
*
* Walks the PE resource directory to the first RT_GROUP_ICON, picks the largest /
* deepest-colour entry it references, then decodes that RT_ICON image. Icon images
* are either PNG (Vista+) - decoded directly - or a classic DIB (BITMAPINFOHEADER +
* XOR pixels + 1-bpp AND mask) - decoded by hand here.
*
* Everything is best-effort: any malformed/oversized/unsupported input returns null.
*/
object PeIconExtractor {
private const val RT_ICON = 3
private const val RT_GROUP_ICON = 14
private const val MAX_RES_SECTION = 64 * 1024 * 1024 // don't slurp absurd resource sections
private const val MAX_RES_SECTION = 64 * 1024 * 1024
private const val MAX_ICON_DIM = 1024
fun extract(file: File): Bitmap? = try {
@@ -31,11 +21,11 @@ object PeIconExtractor {
private fun parse(raf: RandomAccessFile): Bitmap? {
val dos = read(raf, 0, 0x40) ?: return null
if (u8(dos, 0) != 0x4D || u8(dos, 1) != 0x5A) return null // "MZ"
if (u8(dos, 0) != 0x4D || u8(dos, 1) != 0x5A) return null
val peOff = u32(dos, 0x3C).toInt()
val coff = read(raf, peOff.toLong(), 24) ?: return null
if (u8(coff, 0) != 0x50 || u8(coff, 1) != 0x45 || u8(coff, 2) != 0 || u8(coff, 3) != 0) return null // "PE\0\0"
if (u8(coff, 0) != 0x50 || u8(coff, 1) != 0x45 || u8(coff, 2) != 0 || u8(coff, 3) != 0) return null
val numSections = u16(coff, 6)
val sizeOpt = u16(coff, 20)
if (numSections <= 0 || numSections > 96 || sizeOpt <= 0) return null
@@ -43,8 +33,8 @@ object PeIconExtractor {
val optOff = peOff + 24L
val opt = read(raf, optOff, sizeOpt) ?: return null
val magic = u16(opt, 0)
val ddOff = if (magic == 0x20B) 112 else 96 // PE32+ vs PE32 data-directory offset
val resDirEntry = ddOff + 2 * 8 // data directory index 2 = resource table
val ddOff = if (magic == 0x20B) 112 else 96
val resDirEntry = ddOff + 2 * 8
if (resDirEntry + 8 > opt.size) return null
val resRVA = u32(opt, resDirEntry).toInt()
val resSize = u32(opt, resDirEntry + 4).toInt()
@@ -75,7 +65,6 @@ object PeIconExtractor {
val resDirStart = resRVA - secVA
if (resDirStart < 0 || resDirStart + 16 > res.size) return null
// RT_GROUP_ICON -> first group's data entry -> GRPICONDIR
val grpTypeDir = findIdSubdir(res, resDirStart, RT_GROUP_ICON, resDirStart) ?: return null
val grpData = firstDataEntry(res, grpTypeDir, resDirStart) ?: return null
val grp = dataEntryBytes(res, grpData, secVA) ?: return null
@@ -97,7 +86,6 @@ object PeIconExtractor {
}
if (bestId < 0) return null
// RT_ICON with the chosen id -> image bytes
val iconTypeDir = findIdSubdir(res, resDirStart, RT_ICON, resDirStart) ?: return null
val iconIdDir = findIdSubdir(res, iconTypeDir, bestId, resDirStart) ?: return null
val iconData = firstDataEntry(res, iconIdDir, resDirStart) ?: return null
@@ -106,9 +94,7 @@ object PeIconExtractor {
return decodeIcon(img)
}
// ── resource-directory navigation ──
/** Resolve the sub-directory under [dirIdx] whose id matches [wantId]; null if absent or not a directory. */
private fun findIdSubdir(res: ByteArray, dirIdx: Int, wantId: Int, resDirStart: Int): Int? {
if (dirIdx + 16 > res.size) return null
val total = u16(res, dirIdx + 12) + u16(res, dirIdx + 14)
@@ -126,7 +112,6 @@ object PeIconExtractor {
return null
}
/** Descend the first entry of each level until an IMAGE_RESOURCE_DATA_ENTRY; returns its index. */
private fun firstDataEntry(res: ByteArray, dirIdx: Int, resDirStart: Int): Int? {
if (dirIdx + 16 > res.size) return null
if (u16(res, dirIdx + 12) + u16(res, dirIdx + 14) == 0) return null
@@ -140,7 +125,6 @@ object PeIconExtractor {
}
}
/** Read the bytes an IMAGE_RESOURCE_DATA_ENTRY points at (its OffsetToData is an RVA). */
private fun dataEntryBytes(res: ByteArray, entryIdx: Int, secVA: Int): ByteArray? {
if (entryIdx < 0 || entryIdx + 8 > res.size) return null
val rva = u32(res, entryIdx).toInt()
@@ -150,7 +134,6 @@ object PeIconExtractor {
return res.copyOfRange(start, start + size)
}
// ── icon image decoding ──
private fun decodeIcon(b: ByteArray): Bitmap? {
if (b.size >= 8 && u8(b, 0) == 0x89 && u8(b, 1) == 0x50 && u8(b, 2) == 0x4E && u8(b, 3) == 0x47) {
@@ -164,7 +147,7 @@ object PeIconExtractor {
val headerSize = u32(b, 0).toInt()
val width = s32(b, 4)
val heightFull = s32(b, 8)
val height = heightFull / 2 // DIB icon height counts XOR + AND mask
val height = heightFull / 2
val bitCount = u16(b, 14)
if (width <= 0 || height <= 0 || width > MAX_ICON_DIM || height > MAX_ICON_DIM) return null
@@ -183,7 +166,6 @@ object PeIconExtractor {
val andRowSize = ((width + 31) / 32) * 4
val andStart = xorStart + rowSize * height
// 32-bit icons sometimes ship an all-zero alpha channel; fall back to the AND mask then.
var hasAlpha = false
if (bitCount == 32) {
var y = 0
@@ -245,7 +227,6 @@ object PeIconExtractor {
return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888)
}
/** AND-mask alpha for a pixel: returns 0xFF000000 (opaque) or 0 (transparent), shifted into place. */
private fun maskAlpha(b: ByteArray, andStart: Int, andRowSize: Int, srcRow: Int, x: Int): Int {
val o = andStart + srcRow * andRowSize + x / 8
if (o >= b.size) return 0xFF shl 24
@@ -253,7 +234,6 @@ object PeIconExtractor {
return if (bit == 1) 0 else (0xFF shl 24)
}
// ── little-endian readers ──
private fun u8(b: ByteArray, off: Int): Int = b[off].toInt() and 0xFF
private fun u16(b: ByteArray, off: Int): Int = u8(b, off) or (u8(b, off + 1) shl 8)
@@ -8,52 +8,24 @@ import android.os.storage.StorageVolume
import com.droiddeck.launcher.R
import java.io.File
/**
* A storage volume offered in the file manager's drive menu.
*
* [dir] is the best path we found for the volume: its root when that is listable, otherwise the
* deepest directory we can actually read on the way down to the app-specific directory. A volume
* always gets exactly one entry, even if several paths lead to it.
*/
data class StorageRoot(
val label: String,
val dir: File,
val removable: Boolean,
/** False when no path on the volume could be listed; the entry is still shown. */
val readable: Boolean,
)
/**
* Enumerates mounted storage volumes.
*
* Listing `/storage` is not enough on its own: a volume can be absent from this process's mount
* view (notably after a container exits and the app process is restarted onto a stale storage
* sandbox) even though it is mounted and healthy. The framework knows about it regardless, so the
* volume set is built from several independent sources:
*
* 1. [StorageManager.getStorageVolumes] - the authoritative list, read over Binder.
* 2. [Context.getExternalFilesDirs] - per-app directories, granted separately from shared storage.
* 3. `/storage` and `/mnt/media_rw` directory listings - the filesystem view, as a backstop.
*
* Those sources overlap heavily: one SD card is reachable as `/storage/<uuid>`, as
* `/mnt/media_rw/<uuid>`, and via its app-specific directory, and typically only some of them are
* readable. So paths are grouped by the *volume* they belong to rather than by path, and each
* volume contributes a single entry pointing at the best path available for it.
*/
object StorageRoots {
private const val EMULATED_PREFIX = "/storage/emulated"
private const val INTERNAL_PATH = "/storage/emulated/0"
/** Identifies the physical volume a path belongs to; paths sharing a key are the same card. */
private const val PRIMARY_KEY = "primary"
private class Volume(val key: String) {
var label: String? = null
var removable: Boolean = key != PRIMARY_KEY
/** Candidate directories, best-guess first. */
val candidates = LinkedHashSet<File>()
/** App-specific directories, used to find a readable path inside an unreadable root. */
val seeds = LinkedHashSet<File>()
}
@@ -62,7 +34,6 @@ object StorageRoots {
fun volumeOf(key: String) = volumes.getOrPut(key) { Volume(key) }
// Internal storage is never removable and never absent; seed it first so it heads the menu.
volumeOf(PRIMARY_KEY).apply {
label = context.getString(R.string.fm_drive_internal)
removable = false
@@ -72,7 +43,6 @@ object StorageRoots {
val appDirs = appSpecificDirs(context)
val storageManager = context.getSystemService(StorageManager::class.java)
// 1 + 2 - framework-reported volumes. Authoritative, and unaffected by our mount view.
storageManager?.storageVolumes.orEmpty()
.filter { isMounted(it.state) }
.forEach { volume ->
@@ -86,14 +56,11 @@ object StorageRoots {
}
if (uuid != null) {
entry.candidates += File("/storage/$uuid")
// Last resort only: the raw mount is normally unreadable to an app, so it must
// never outrank /storage/<uuid> and must never become an entry of its own.
entry.candidates += File("/mnt/media_rw/$uuid")
}
appDirs.filter { belongsTo(storageManager!!, it, volume) }.forEach { entry.seeds += it }
}
// 3 - filesystem backstop, for any volume the framework did not report.
File("/storage").listFiles().orEmpty().forEach { child ->
if (!child.isDirectory || child.name == "self" || child.name == "emulated") return@forEach
volumeOf(child.name).candidates += child
@@ -102,7 +69,6 @@ object StorageRoots {
if (child.isDirectory) volumeOf(child.name).candidates += child
}
// App-specific directories, which stay reachable when shared storage is not.
appDirs.forEach { dir ->
val root = volumeRootOf(dir) ?: return@forEach
val entry = volumeOf(if (isEmulated(root)) PRIMARY_KEY else root.name)
@@ -117,11 +83,9 @@ object StorageRoots {
}
private fun Volume.toRoot(context: Context): StorageRoot {
// Prefer a path we can actually list; a readable child beats an unreadable root.
val dir = candidates.firstOrNull(::canBrowse)
?: candidates.firstNotNullOfOrNull { root -> seeds.firstNotNullOfOrNull { deepestReadable(root, it) } }
?: seeds.firstOrNull(::canBrowse)
// Report the volume anyway; an entry that lists empty beats one that silently vanished.
?: candidates.firstOrNull()
?: File("/storage/$key")
@@ -135,7 +99,6 @@ object StorageRoots {
private fun Volume.defaultLabel(context: Context): String = if (removable) context.getString(R.string.fm_drive_sd) else key
/** Appends the volume id to any label used by more than one volume, so entries stay tellable apart. */
private fun disambiguate(roots: List<StorageRoot>): List<StorageRoot> {
val counts = roots.groupingBy { it.label }.eachCount()
return roots.map { root ->
@@ -143,7 +106,6 @@ object StorageRoots {
}
}
/** Walks up from [seed] towards [root] and returns the highest directory we can list. */
private fun deepestReadable(root: File, seed: File): File? {
var current: File? = seed.absoluteFile
var best: File? = null
@@ -154,7 +116,6 @@ object StorageRoots {
return best
}
/** `/storage/ABCD-1234/Android/data/<pkg>/files` -> `/storage/ABCD-1234`. */
private fun volumeRootOf(appDir: File): File? =
generateSequence(appDir.absoluteFile) { it.parentFile }
.firstOrNull { it.name.equals("Android", ignoreCase = true) }
@@ -178,11 +139,6 @@ object StorageRoots {
}
}
/**
* Names a volume the way the rest of the file manager does. The framework description is only
* used when it says something more useful than "SD card" - on some devices it is a bare disk
* name like "android", which tells the user nothing about which card they are looking at.
*/
private fun labelFor(context: Context, volume: StorageVolume, uuid: String?): String {
if (volume.isPrimary) return context.getString(R.string.fm_drive_internal)
if (volume.isRemovable) return context.getString(R.string.fm_drive_sd)
@@ -5,15 +5,7 @@ import org.json.JSONArray
import org.json.JSONObject
import java.io.File
/**
* Games added from the Games tab's + rather than a Games folder: one picked .exe, or each game
* "Add all games in this folder" found. Kept by the game's folder in `files/added-exes.json`,
* which [AddedGames.scan] merges in. The exe chosen later, the shortcut appid, the edits and the
* removal live where every added game keeps them ([com.droiddeck.launcher.session.SessionPrefs]);
* this holds which folders are games and the exe they came with.
*/
object AddedExes {
/** [picked]: the user chose [exe]; false = [GameExePicker]'s best guess, which a scan ranks again. */
class Entry(val folder: String, val exe: String, val picked: Boolean = true)
private fun file(context: Context) = File(context.filesDir, "added-exes.json")
@@ -30,7 +22,6 @@ object AddedExes {
emptyList()
}
/** Adds [entry], replacing one for the same folder. */
@Synchronized
fun put(context: Context, entry: Entry) = write(context, list(context).filter { it.folder != entry.folder } + entry)
@@ -11,14 +11,6 @@ import java.net.HttpURLConnection
import java.net.URL
import java.net.URLEncoder
/**
* Artwork for an added game: what the user put in the game's folder first, else what Steam has
* for it - by the Steam appid its files name, else by a store search on its title - else what
* SteamGridDB has ([SteamGridDb]), fetched once into the app's cache. Four pieces, named the way
* the client names them in its grid folder: the portrait capsule, the wide header, the hero
* background and the logo; plus an icon pulled out of the game's own .exe. The session hands
* the paths to the runtime's shortcuts writer, which copies them into the client's grid.
*/
object AddedGameArt {
private const val TAG = "AddedGameArt"
private const val STORE_SEARCH = "https://store.steampowered.com/api/storesearch/?l=english&cc=US&term="
@@ -29,15 +21,10 @@ object AddedGameArt {
val any get() = portrait != null || header != null || hero != null || logo != null
}
/**
* The pieces a user can choose, as the client takes them: [assetType] is its custom-artwork
* number (null: the icon, set by path); [cacheName] the automatic file in the game's cache.
*/
enum class Slot(val key: String, val cacheName: String, val assetType: Int?) {
COVER("p", "p.jpg", 0), BACKGROUND("hero", "hero.jpg", 1), LOGO("logo", "logo.png", 2), ICON("icon", "icon.png", null),
COVER("p", "p.jpg", 0), WIDE("header", "header.jpg", 3), BACKGROUND("hero", "hero.jpg", 1), LOGO("logo", "logo.png", 2), ICON("icon", "icon.png", null),
}
/** Where a piece came from; [AUTO] = nothing found yet. */
const val FOLDER = "folder"
const val STEAM = "steam"
const val SGDB = "sgdb"
@@ -45,16 +32,18 @@ object AddedGameArt {
const val EXE = "exe"
const val AUTO = ""
/** A slot as it stands: the file shown and where it came from (one of [FOLDER].. [AUTO]); [chosen] = the user picked it. */
class SlotState(val slot: Slot, val file: File?, val source: String, val chosen: Boolean)
/** One image the chooser offers: its group ([FOLDER], [STEAM], [SGDB]), a thumbnail and the full image (URL or path). */
class Option(val group: String, val thumb: String, val full: String)
private val CHOSEN = Regex("""chosen-(\w+)\.(\w+)""")
class Choices(val options: List<Option>, val sgdb: SteamGridDb.Status)
private fun chosen(cache: File, slot: Slot): File? =
cache.listFiles { f -> CHOSEN.matchEntire(f.name)?.groupValues?.get(1) == slot.key }?.firstOrNull()
class Found<T>(val items: List<T>, val status: SteamGridDb.Status)
private val CHOSEN = Regex("""chosen-(p|header|hero|logo|icon)(?:-\d+)?\.(\w+)""")
internal fun chosen(cache: File, slot: Slot): File? =
cache.listFiles { f -> CHOSEN.matchEntire(f.name)?.groupValues?.get(1) == slot.key }?.maxWithOrNull(compareBy({ it.lastModified() }, { it.name }))
private val IMAGE = listOf("png", "jpg", "jpeg")
private val PORTRAIT = listOf("cover", "poster", "grid", "boxart", "capsule", "folder", "library_600x900", "portrait")
@@ -64,7 +53,6 @@ object AddedGameArt {
internal fun cacheDir(context: Context, game: AddedGames.Game) = File(context.filesDir, "added-art/${game.appId}")
/** An image in the folder (or its `art` subfolder) under one of the given names, any image extension. */
private fun find(folder: File, names: List<String>): File? {
val dirs = listOf(folder, File(folder, "art"), File(folder, ".art"))
for (dir in dirs) for (name in names) for (ext in IMAGE) {
@@ -74,7 +62,6 @@ object AddedGameArt {
return null
}
/** What the folder itself offers; the file named after the folder counts as the portrait. */
private fun local(game: AddedGames.Game): Art {
val folder = game.folder
return Art(
@@ -86,21 +73,19 @@ object AddedGameArt {
)
}
/** The user's choice for each piece, else the folder's art, else the cached art; no network. */
fun resolve(context: Context, game: AddedGames.Game): Art {
val own = local(game)
val cache = cacheDir(context, game)
fun cached(name: String) = File(cache, name).takeIf { it.isFile }
return Art(
portrait = chosen(cache, Slot.COVER) ?: own.portrait ?: cached("p.jpg") ?: exeArt(context, game, cover = true),
header = own.header ?: cached("header.jpg") ?: exeArt(context, game, cover = false),
header = chosen(cache, Slot.WIDE) ?: own.header ?: cached("header.jpg") ?: exeArt(context, game, cover = false),
hero = chosen(cache, Slot.BACKGROUND) ?: own.hero ?: cached("hero.jpg"),
logo = chosen(cache, Slot.LOGO) ?: own.logo ?: cached("logo.png"),
icon = chosen(cache, Slot.ICON) ?: own.icon ?: cached("icon-sgdb.png") ?: exeIcon(context, game),
)
}
/** Each choosable piece with its file and where it came from, for the game's editor; no network. */
fun describe(context: Context, game: AddedGames.Game): List<SlotState> {
val own = local(game)
val cache = cacheDir(context, game)
@@ -111,6 +96,7 @@ object AddedGameArt {
chosen(cache, slot)?.let { return@map SlotState(slot, it, chosenSource(context, game, slot).first.ifEmpty { FILE }, true) }
val auto: Pair<File, String>? = when (slot) {
Slot.COVER -> own.portrait?.let { it to FOLDER } ?: cached("p.jpg") ?: exeArt(context, game, cover = true)?.let { it to EXE }
Slot.WIDE -> own.header?.let { it to FOLDER } ?: cached("header.jpg") ?: exeArt(context, game, cover = false)?.let { it to EXE }
Slot.BACKGROUND -> own.hero?.let { it to FOLDER } ?: cached("hero.jpg")
Slot.LOGO -> own.logo?.let { it to FOLDER } ?: cached("logo.png")
Slot.ICON -> own.icon?.let { it to FOLDER } ?: File(cache, "icon-sgdb.png").takeIf { it.isFile }?.let { it to SGDB }
@@ -122,61 +108,98 @@ object AddedGameArt {
private val CHOOSABLE = setOf("png", "jpg", "jpeg", "webp")
/** Images in the game's folder (and its art folders) that could stand for a piece. */
internal fun folderImages(folder: File): List<File> =
listOf(folder, File(folder, "art"), File(folder, ".art")).flatMap { dir ->
dir.listFiles { f -> f.isFile && f.extension.lowercase() in CHOOSABLE }?.sortedBy { it.name.lowercase() }.orEmpty()
}.take(12)
/**
* What the chooser offers for [slot]: the game folder's images, Steam's official piece (when the
* game has a Steam appid and Steam has the piece), and SteamGridDB's best few (with a key).
* Blocking: it asks the network.
*/
fun options(context: Context, game: AddedGames.Game, slot: Slot): List<Option> {
fun choices(context: Context, game: AddedGames.Game, slot: Slot): Choices {
val out = ArrayList<Option>()
folderImages(game.folder).forEach { out.add(Option(FOLDER, it.path, it.path)) }
val cache = cacheDir(context, game)
val steamId = SteamMatch.resolve(context, game.folder.path, game.exe, game.name, legacy = File(cache, "steam-appid")).appId?.toString()
val remote = when (slot) { Slot.COVER -> "library_600x900.jpg"; Slot.BACKGROUND -> "library_hero.jpg"; Slot.LOGO -> "logo.png"; Slot.ICON -> null }
if (steamId != null && remote != null) {
val url = "$CDN$steamId/$remote".takeIf { exists(it) } ?: steamAssetUrl(steamId, remote)
if (url != null) out.add(Option(STEAM, url, url))
if (steamId != null) steamOption(steamId, slot)?.let { out.add(it) }
val (found, status) = sgdbChoices(context, File(cache, "sgdb-id"), steamId?.toIntOrNull(), game.name, slot)
out.addAll(found)
return Choices(out, status)
}
internal fun steamOption(steamId: String, slot: Slot): Option? {
val remote = when (slot) { Slot.COVER -> "library_600x900.jpg"; Slot.WIDE -> "header.jpg"; Slot.BACKGROUND -> "library_hero.jpg"; Slot.LOGO -> "logo.png"; Slot.ICON -> null } ?: return null
val url = "$CDN$steamId/$remote".takeIf { exists(it) } ?: steamAssetUrl(steamId, remote) ?: return null
return Option(STEAM, url, url)
}
internal fun sgdbChoices(context: Context, marker: File, steamId: Int?, name: String, slot: Slot): Pair<List<Option>, SteamGridDb.Status> {
val key = SteamGridDb.key(context)
if (key.isEmpty()) return emptyList<Option>() to SteamGridDb.Status.NO_KEY
marker.parentFile?.mkdirs()
return try {
val gridId = remembered(marker, SteamGridDb.keyChangedAt(context), retryMiss = true) {
SteamGridDb.gameId(key, steamId, name)?.toString()
}?.toIntOrNull() ?: return emptyList<Option>() to SteamGridDb.Status.NO_GAME
SteamGridDb.images(key, gridId, slot, if (slot == Slot.ICON) 3 else 5).map { (thumb, full) -> Option(SGDB, thumb, full) } to SteamGridDb.Status.OK
} catch (e: Exception) {
Log.w(TAG, "$name: SteamGridDB failed (${e.javaClass.simpleName})")
emptyList<Option>() to ((e as? SteamGridDb.Failed)?.status ?: SteamGridDb.Status.SERVER)
}
}
fun searchGames(context: Context, term: String): Found<SteamGridDb.Game> = sgdb(context) { SteamGridDb.search(it, term) }
fun gameImages(context: Context, id: Int, slot: Slot): Found<Option> = sgdb(context) { key ->
SteamGridDb.all(key, id, slot).map { (thumb, full) -> Option(SGDB, thumb, full) }
}
private fun <T> sgdb(context: Context, call: (String) -> List<T>): Found<T> {
val key = SteamGridDb.key(context)
if (key.isNotEmpty()) {
cache.mkdirs()
val gridId = remembered(File(cache, "sgdb-id")) {
runCatching { SteamGridDb.gameId(key, steamId?.toIntOrNull(), game.name) }.getOrNull()?.toString()
}?.toIntOrNull()
if (gridId != null) runCatching { SteamGridDb.images(key, gridId, slot, if (slot == Slot.ICON) 3 else 5) }.getOrNull()
?.forEach { (thumb, full) -> out.add(Option(SGDB, thumb, full)) }
if (key.isEmpty()) return Found(emptyList(), SteamGridDb.Status.NO_KEY)
return try {
Found(call(key), SteamGridDb.Status.OK)
} catch (e: Exception) {
Log.w(TAG, "SteamGridDB: ${e.javaClass.simpleName}")
Found(emptyList(), (e as? SteamGridDb.Failed)?.status ?: SteamGridDb.Status.SERVER)
}
return out
}
/** Sets [slot] to [option] (a file is copied, a URL fetched) and records where it came from. False when it could not be had. Blocking. */
fun choose(context: Context, game: AddedGames.Game, slot: Slot, option: Option): Boolean {
val bytes = if (option.full.startsWith("https://")) get(option.full) else runCatching { File(option.full).readBytes() }.getOrNull()
bytes ?: return false
val ext = option.full.substringAfterLast('.', "png").substringBefore('?').lowercase().takeIf { it in CHOOSABLE + "ico" } ?: "png"
val cache = cacheDir(context, game).apply { mkdirs() }
chosen(cache, slot)?.delete()
val dst = File(cache, "chosen-${slot.key}.${if (ext == "jpeg") "jpg" else ext}")
val tmp = File(cache, dst.name + ".tmp")
tmp.writeBytes(bytes)
if (!tmp.renameTo(dst)) return false
if (!store(cacheDir(context, game), slot, option)) return false
SessionPrefs.setAddedGameArtSource(context, game.folder.path, slot.key, option.group + "|" + option.full)
return true
}
/** Where the user's choice for [slot] came from and which image it was ("" both when automatic). */
internal fun store(cache: File, slot: Slot, option: Option): Boolean {
val fresh = storeNew(cache, slot, option) ?: return false
dropOlder(cache, slot, fresh)
return true
}
internal fun dropOlder(cache: File, slot: Slot, keep: File) {
cache.listFiles { f -> f != keep && CHOSEN.matchEntire(f.name)?.groupValues?.get(1) == slot.key }?.forEach { it.delete() }
}
private val lastStamp = java.util.concurrent.atomic.AtomicLong(0)
private fun nextStamp(): Long = lastStamp.updateAndGet { maxOf(it + 1, System.currentTimeMillis()) }
internal fun storeNew(cache: File, slot: Slot, option: Option): File? {
val bytes = if (option.full.startsWith("https://")) get(option.full) else runCatching { File(option.full).readBytes() }.getOrNull()
bytes ?: return null
val ext = option.full.substringAfterLast('.', "png").substringBefore('?').lowercase().takeIf { it in CHOOSABLE + "ico" } ?: "png"
cache.mkdirs()
val suffix = if (ext == "jpeg") "jpg" else ext
var dst = File(cache, "chosen-${slot.key}-" + "%013d".format(nextStamp()) + "." + suffix)
while (dst.exists()) dst = File(cache, "chosen-${slot.key}-" + "%013d".format(nextStamp()) + "." + suffix)
val tmp = File(cache, dst.name + ".tmp")
tmp.writeBytes(bytes)
return if (tmp.renameTo(dst)) dst else null
}
fun chosenSource(context: Context, game: AddedGames.Game, slot: Slot): Pair<String, String> {
val value = SessionPrefs.addedGameArtSource(context, game.folder.path, slot.key)
return value.substringBefore('|') to value.substringAfter('|', "")
}
/** [slot] back to what is found by itself. */
fun reset(context: Context, game: AddedGames.Game, slot: Slot) {
chosen(cacheDir(context, game), slot)?.delete()
SessionPrefs.setAddedGameArtSource(context, game.folder.path, slot.key, "")
@@ -191,7 +214,6 @@ object AddedGameArt {
try { c.responseCode == 200 } finally { c.disconnect() }
}.getOrDefault(false)
/** The first icon inside the game's .exe, kept as a PNG beside the cached art. */
private fun exeIcon(context: Context, game: AddedGames.Game): File? {
val cache = cacheDir(context, game)
val png = File(cache, "icon.png")
@@ -206,15 +228,11 @@ object AddedGameArt {
return if (tmp.renameTo(png)) png else null
}
/**
* Fetches the art that is missing: Steam first (the appid the game's files name, else a store
* search on its title whose result carries the same name), then SteamGridDB when a key is
* set. Each lookup runs once per game; a miss is retried after a week. Blocking: run it off
* the main thread. Returns true when anything new arrived.
*/
fun fetchMissing(context: Context, games: List<AddedGames.Game>): Boolean {
if (!SessionPrefs.addedGamesArt(context)) return false
val sgdbKey = SteamGridDb.key(context)
val keyChangedAt = SteamGridDb.keyChangedAt(context)
var sgdbDown = false
var changed = false
for (game in games) {
if (local(game).portrait != null) continue
@@ -227,31 +245,40 @@ object AddedGameArt {
val appId = match.appId?.takeIf { match.certainty == SteamMatch.Certainty.FILES }
val id = match.appId?.toString()
if (id != null && download(id, cache)) changed = true
if (File(cache, "p.jpg").isFile || sgdbKey.isEmpty()) continue
val gridId = remembered(File(cache, "sgdb-id")) {
runCatching { SteamGridDb.gameId(sgdbKey, appId ?: id?.toIntOrNull(), game.name) }.getOrNull()?.toString()
}?.toIntOrNull() ?: continue
if (runCatching { SteamGridDb.download(sgdbKey, gridId, cache) }.getOrDefault(false)) changed = true
if (File(cache, "p.jpg").isFile || sgdbKey.isEmpty() || sgdbDown) continue
val stored: String? = try {
remembered(File(cache, "sgdb-id"), keyChangedAt) {
SteamGridDb.gameId(sgdbKey, appId ?: id?.toIntOrNull(), game.name)?.toString()
}
} catch (e: Exception) {
Log.w(TAG, "${game.name}: SteamGridDB lookup failed (${e.javaClass.simpleName}); not asking again this pass")
sgdbDown = true
null
}
val gridId = stored?.toIntOrNull() ?: continue
val grabbed = try {
SteamGridDb.download(sgdbKey, gridId, cache)
} catch (e: Exception) {
sgdbDown = true
false
}
if (grabbed) changed = true
}
return changed
}
/**
* The game's Steam app, the one record its Windows components use too ([SteamMatch]): what its
* files name, else a store search on its title ([search]) whose result has the same name.
*/
internal fun steamMatch(context: Context, game: AddedGames.Game, search: (String) -> Int?): SteamMatch.Match {
val match = SteamMatch.resolve(context, game.folder.path, game.exe, game.name, legacy = File(cacheDir(context, game), "steam-appid"), search = search)
match.appId?.let { Log.i(TAG, "${game.name}: Steam app $it (${match.certainty.name.lowercase()})") }
return match
}
/** What [marker] remembers (an id, or "none" for a week), else [find]'s answer written to it. */
private fun remembered(marker: File, find: () -> String?): String? {
internal fun remembered(marker: File, notBefore: Long = 0L, retryMiss: Boolean = false, find: () -> String?): String? {
if (marker.isFile) {
val value = marker.readText().trim()
if (value != "none") return value
if (System.currentTimeMillis() - marker.lastModified() < RETRY_AFTER_MS) return null
val at = marker.lastModified()
if (!retryMiss && at >= notBefore && System.currentTimeMillis() - at < RETRY_AFTER_MS) return null
}
val found = find()
marker.writeText(found ?: "none")
@@ -262,11 +289,6 @@ object AddedGameArt {
private val EDITION = Regex("(?i)\\b(complete|definitive|legendary|goty|game of the year|deluxe|ultimate|remastered)( edition)?\\b")
/**
* How well a result's name [got] matches the game's [name]: 3 the same, 2 the same once
* edition words are dropped, 1 one starts with the other; 0 no match. Nothing is taken for
* being the first result.
*/
internal fun nameMatch(name: String, got: String): Int {
val want = normalize(name)
val wantShort = normalize(name.replace(EDITION, " ").trim().ifEmpty { name })
@@ -280,13 +302,11 @@ object AddedGameArt {
}
}
/** The id of the store result whose name matches [name] best, or null when none does. */
internal fun pickStoreResult(name: String, items: List<Pair<String, String>>): String? =
items.map { (id, got) -> id to nameMatch(name, got) }
.filter { it.second > 0 && it.first.isNotEmpty() }
.maxByOrNull { it.second }?.first
/** The Steam appid whose store name matches [name], or null for no match. */
private fun search(name: String): String? {
val term = name.replace(EDITION, " ").trim().ifEmpty { name }
val json = get(STORE_SEARCH + URLEncoder.encode(term, "UTF-8"))?.toString(Charsets.UTF_8) ?: return null
@@ -309,10 +329,6 @@ object AddedGameArt {
return got
}
/**
* Newer Steam games keep their art under hashed paths (store_item_assets/steam/apps/<id>/<hash>/…),
* where the fixed CDN names above 404. The store's item API names those paths; once per appid.
*/
private fun steamAssetUrl(id: String, remote: String): String? {
val assets = steamAssets.getOrPut(id) {
runCatching {
@@ -336,11 +352,6 @@ object AddedGameArt {
private val steamAssets = java.util.concurrent.ConcurrentHashMap<String, Map<String, String>>()
/**
* When nothing else gave a cover (or header), one made from the exe's icon: the icon centred on
* its own colour, darkened toward the bottom, so the game still has a face in the list and in
* Steam. Made once per game; real art found later takes over because it is looked at first.
*/
private fun exeArt(context: Context, game: AddedGames.Game, cover: Boolean): File? {
val cache = cacheDir(context, game)
val out = File(cache, if (cover) "p-exe.png" else "header-exe.png")
@@ -5,27 +5,16 @@ import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.stores.SteamLiveShortcuts
import java.io.File
/**
* The game editor's writes for an added game, Steam's non-Steam Properties in the app: name,
* target, Start in, launch options and art. Each goes through the same shortcut - the listing the
* shortcuts writer reads, and the running client - on the appid the game already has, so an edit
* never makes a second shortcut. Each call is quick (the game alone, [AddedGames.single]); the
* client and the listing follow on [AddedGamesPublisher], in order. Call off the main thread.
*/
object AddedGameEdits {
/** The added game in [folder] as it stands now, or null when it is not listed. */
fun game(context: Context, folder: String): AddedGames.Game? = AddedGames.single(context, folder)
/** The client told ([live]), then the listing rewritten, on [AddedGamesPublisher]. */
private fun publish(context: Context, live: () -> Unit) = AddedGamesPublisher.later(context, live)
/** [name] as the game's name; "" goes back to the name found by itself. */
fun setName(context: Context, folder: String, name: String): AddedGames.Game? {
SessionPrefs.setAddedGameName(context, folder, name)
return apply(context, folder)
}
/** [exe] as the game's target; false when the session cannot see it. */
fun setExe(context: Context, folder: String, exe: File): Boolean {
if (!exe.isFile || AddedGames.guestPath(context, exe) == null) return false
SessionPrefs.setAddedGameExe(context, folder, exe.path)
@@ -33,7 +22,6 @@ object AddedGameEdits {
return true
}
/** [dir] (a host folder) as the game's Start in; "" = the exe's own folder. False when the session cannot see it. */
fun setStartIn(context: Context, folder: String, dir: String): Boolean {
val guest = if (dir.isBlank()) "" else AddedGames.guestPath(context, File(dir.trim())) ?: return false
SessionPrefs.setAddedGameStartIn(context, folder, guest)
@@ -46,23 +34,36 @@ object AddedGameEdits {
return apply(context, folder)
}
/** [slot] set to [option], then handed to the shortcut. False when the image could not be had. */
fun setArt(context: Context, folder: String, slot: AddedGameArt.Slot, option: AddedGameArt.Option): Boolean {
val game = game(context, folder) ?: return false
if (!AddedGameArt.choose(context, game, slot, option)) return false
applyArt(context, game, slot)
return true
return synchronized(SteamGrid.lock) {
if (!AddedGameArt.choose(context, game, slot, option)) return@synchronized false
applyArt(context, game, slot)
true
}
}
/** [slot] back to automatic, then handed to the shortcut. */
fun resetArt(context: Context, folder: String, slot: AddedGameArt.Slot) {
val game = game(context, folder) ?: return
AddedGameArt.reset(context, game, slot)
applyArt(context, game, slot)
synchronized(SteamGrid.lock) {
AddedGameArt.reset(context, game, slot)
applyArt(context, game, slot)
}
}
fun logoPosition(context: Context, folder: String): LogoPosition.Value? =
game(context, folder)?.let { SteamGrid.position(SteamGrid.target(it, context)) }
fun setLogoPosition(context: Context, folder: String, value: LogoPosition.Value?): Boolean {
val game = game(context, folder) ?: return false
if (!SteamGrid.writePosition(context, SteamGrid.target(game, context), value)) return false
publish(context) { SteamLiveShortcuts.setLogoPosition(context, game.appId, value) }
return true
}
private fun applyArt(context: Context, game: AddedGames.Game, slot: AddedGameArt.Slot) {
val file = AddedGameArt.describe(context, game).first { it.slot == slot }.file
if (slot != AddedGameArt.Slot.ICON) SteamGrid.write(context, SteamGrid.target(game, context), slot, AddedGameArt.chosen(AddedGameArt.cacheDir(context, game), slot))
publish(context) {
if (slot == AddedGameArt.Slot.ICON) SteamLiveShortcuts.setArt(context, game.appId, null, null, iconPath = file?.absolutePath.orEmpty())
else SteamLiveShortcuts.setArt(context, game.appId, slot.assetType, file)
@@ -6,12 +6,12 @@ import com.droiddeck.launcher.runtime.LinuxRuntime
import org.json.JSONObject
import com.droiddeck.launcher.session.GameStorage
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreGameSidecar
import com.droiddeck.launcher.stores.StoreInstallRoot
import java.io.File
import java.util.zip.CRC32
/** Windows game folders shared with the Steam session. */
object AddedGames {
private const val TAG = "AddedGames"
private const val LIBRARY = "/mnt/droiddeck-sd"
@@ -19,38 +19,23 @@ object AddedGames {
class Game(
val folder: File, val name: String,
/** The game's own .exe: what the icon is read from and what the settings page lists. */
val exe: File,
/** The guest path the shortcut runs: [exe], or a store install's launcher .bat in front of it. */
val guestExe: String, val guestDir: String,
/** The client's 32-bit appid for this shortcut, as an unsigned value. */
val appId: Long,
/** What steam://rungameid/ takes for a shortcut. */
val gameId: Long,
val candidates: List<File>,
val steamAppId: Int? = null,
/** Where the game came from: a store's id, or [Library.ADDED] for a folder the user added. */
val source: String = Library.ADDED,
/** The store's own id for the game, from its sidecar; null for a plain added folder. */
val storeId: String? = null,
/** The exe is the picker's guess and nothing clearly won ([GameExePicker.Ranking.uncertain]). */
val exeUncertain: Boolean = false,
/** Steam's launch options for the shortcut; "" for none. */
val launchOptions: String = "",
/** Added from the Games tab (an entry of its own), not found in a Games folder. */
val single: Boolean = false,
) {
fun folderName(): String = folder.name
}
/** A Games folder an earlier build had, and where the session sees it. */
class Root(val host: File, val guest: String)
/**
* The Games folders earlier builds had, bound into the session at the guest paths they had
* then (/root/Games/<name>), so the games in them (entries now, [GamesFolderMigration]) keep
* their paths and shortcuts. Nothing adds one any more.
*/
fun roots(context: Context): List<Root> {
GamesFolderMigration.run(context)
return SessionPrefs.gameFolderBinds(context).map { (host, guest) -> Root(File(host), guest) }
@@ -61,14 +46,10 @@ object AddedGames {
"|.*installer.*|.*uninstall.*|.*updater?.*|.*config(ur.*)?|.*settings.*|.*editor.*|.*server.*|.*benchmark.*|.*helper.*|.*eac.*|.*easyanticheat.*|.*battleye.*)\\.exe$",
)
/** Under here the chosen Games folders are bound inside the session, one each. */
const val GUEST_DIR = "/root/Games"
/** Where a host path appears inside the session, or null when the session cannot see it. */
fun guestPath(context: Context, host: File): String? {
val path = host.absolutePath
// The Games folders are bound on their own, so a folder anywhere - an SD card, a USB
// drive - works without being inside one of the other binds.
for (root in roots(context)) {
val dir = root.host.absolutePath
if (path == dir) return root.guest
@@ -77,26 +58,19 @@ object AddedGames {
SessionPrefs.romsDir(context).takeIf { it.isNotEmpty() }?.let { roms ->
if (path.startsWith("$roms/")) return "/root/ROMs/" + path.removePrefix("$roms/")
}
// A store root on a card has a bind of its own, before the library rule: the same folder
// keeps the same guest path (and appid) whether or not that card is also the Steam library.
StoreInstallRoot.externalGuestPath(context, host)?.let { return it }
GameStorage.effective(context)?.let { lib ->
if (path.startsWith("${lib.path}/")) return "$LIBRARY/" + path.removePrefix("${lib.path}/")
}
// Inside the runtime's own tree (store installs on internal storage live at
// root/Games/Stores): the tree is the session's / and needs no bind.
val runtime = LinuxRuntime.rootDir(context).absolutePath
if (path.startsWith("$runtime/")) return "/" + path.removePrefix("$runtime/")
// Internal storage, bound at /root/Storage in every session: an exe picked anywhere on it.
val storage = android.os.Environment.getExternalStorageDirectory().absolutePath
if (path.startsWith("$storage/")) return "/root/Storage/" + path.removePrefix("$storage/")
return null
}
/** The .exe files a game folder offers, best first ([GameExePicker]); declared Store exes lead. */
fun candidates(folder: File): List<File> = rank(folder).exes
/** [GameExePicker]'s ranking, with a Store-declared executable taking precedence when present. */
fun rank(folder: File): GameExePicker.Ranking {
val key = "${folder.path}|${folder.lastModified()}"
val now = System.currentTimeMillis()
@@ -115,20 +89,11 @@ object AddedGames {
return result
}
/** A folder's ranking for a minute, so one add (rank, then the game built from it) walks it once. */
private val ranked = java.util.concurrent.ConcurrentHashMap<String, Pair<Long, GameExePicker.Ranking>>()
private const val RANK_FRESH_MS = 60_000L
/**
* Names identified from an exe (Steam manifest, GOG info, version resource), by the exe's path,
* size and time and its game folder's time (a GOG info file dropped in changes the latter).
*/
private val identified = java.util.concurrent.ConcurrentHashMap<String, String>()
/**
* The game's own title for [exe] when the disk names one ([GameIdentifier]: a Steam manifest,
* a GOG info file, the exe's version resource), else null (the folder name stands).
*/
internal fun identifiedName(folder: File, exe: File): String? {
val key = "${exe.path}|${exe.length()}|${exe.lastModified()}|${folder.lastModified()}"
identified[key]?.let { return it.ifEmpty { null } }
@@ -141,13 +106,6 @@ object AddedGames {
return name
}
/**
* The exe's version resource often spells the title the way a programmer typed it ("Insane2")
* while the folder has the real spacing ("Insane 2"). When both spell the same letters and
* digits, the one with more word breaks wins; a tie keeps mixed case over all one case
* ("DiRT 3" over "dirt 3"), else the exe's spelling. Different titles keep the exe's.
*/
/** "Watch_Dogs" → "Watch Dogs": an underscore in a version-resource title is a programmer's space. */
internal fun underscoresToSpaces(name: String): String =
name.replace('_', ' ').replace(Regex("\\s+"), " ").trim().ifEmpty { name }
@@ -163,7 +121,6 @@ object AddedGames {
else -> peName
}
}
/** The executables MicrosoftGame.config or appxmanifest.xml declares that exist inside [folder]. */
internal fun declaredExes(folder: File): List<File> {
val names = DECLARED.flatMap { (file, pattern) ->
runCatching { pattern.findAll(File(folder, file).readText()).map { it.groupValues[1] }.toList() }.getOrDefault(emptyList())
@@ -182,6 +139,10 @@ object AddedGames {
"appxmanifest.xml" to Regex("<Application\\b[^>]*?\\bExecutable\\s*=\\s*\"([^\"]+)\"", RegexOption.IGNORE_CASE),
)
/** The card's Steam libraries: the arm64 client's steamapps and the x86-64 client's own folder beside it. */
internal fun isSteamLibraryFolder(name: String): Boolean =
name.equals("steamapps", ignoreCase = true) || name == com.droiddeck.launcher.session.GameStorage.X64_CARD_DIR
fun scan(context: Context): List<Game> {
val out = ArrayList<Game>()
GamesFolderMigration.run(context)
@@ -194,31 +155,21 @@ object AddedGames {
if (!dir.isDirectory) { Log.w(TAG, "$dir is not a folder; skipped"); continue }
val steamInstalls = steamInstallDirs(dir)
for (folder in dir.listFiles { f -> f.isDirectory }?.sortedBy { it.name.lowercase() } ?: emptyList()) {
if (folder.name.lowercase() in steamInstalls || folder.name.equals("steamapps", ignoreCase = true)) continue
// Removed from the list by the user (the folder stays on disk); restored from the settings page.
if (folder.name.lowercase() in steamInstalls || isSteamLibraryFolder(folder.name)) continue
if (folder.path in removed) continue
// The store install root sits inside the Games storage; its games are scanned below, per store.
if (folder.name == "Games" && StoreInstallRoot.roots(context).any { it.absolutePath == folder.absolutePath }) continue
scanGame(context, folder, out)
}
}
// Games added as one picked exe (the Games tab's +), by that exe's folder.
for (entry in AddedExes.list(context)) {
val folder = File(entry.folder)
if (folder.path in removed || !folder.isDirectory) continue
scanGame(context, folder, out, single = entry)
}
// Games the Stores section installed: one folder each under <root>/<Store>/, with a sidecar.
// An unfinished one (sidecar still `installing`, or none at all) is neither a game nor Custom.
for (folder in StoreInstallRoot.gameFolders(context)) if (StoreInstallRoot.isFinished(folder)) scanGame(context, folder, out)
return out.distinctBy { it.folder.canonicalPath }
}
/**
* The game in [folderPath] alone, built by the same rules as [scan] (store sidecar, an entry
* from the Games tab, the chosen exe, the edits) without walking every other game: what the
* editor needs for one game. Null when that folder is not a game, or was removed.
*/
fun single(context: Context, folderPath: String): Game? {
val folder = File(folderPath)
if (!folder.isDirectory || folderPath in SessionPrefs.removedAddedGames(context)) return null
@@ -227,12 +178,6 @@ object AddedGames {
return out.firstOrNull()
}
/**
* Stops listing the added game in [folder] at once: marked removed and dropped from the
* library cache; the running client loses its shortcut ([appId], else the one stored for the
* folder) and the listing is rewritten afterwards ([AddedGamesPublisher]). Nothing on disk is
* deleted. Quick; call off the main thread (preferences and a small file).
*/
fun remove(context: Context, folder: File, appId: Long? = null, name: String = folder.name) {
val app = context.applicationContext
val started = android.os.SystemClock.elapsedRealtime()
@@ -243,13 +188,10 @@ object AddedGames {
AddedGamesPublisher.later(app) { if (id != null) com.droiddeck.launcher.stores.SteamLiveShortcuts.remove(app, id) }
}
/** What [addExe] did with a picked exe. */
sealed class AddResult {
/** A new game, or the existing one the exe already is, or the listed game whose exe it now is. */
class Added(val game: Game) : AddResult()
class Existing(val game: Game) : AddResult()
class Switched(val game: Game) : AddResult()
/** The session cannot see the exe's path. */
object Unreachable : AddResult()
object NotFound : AddResult()
}
@@ -258,11 +200,6 @@ object AddedGames {
private fun inside(path: String, folder: String) = path.startsWith(folder.trimEnd('/') + "/")
/**
* The game folder [exe] belongs to, without a scan: an entry from the Games tab (Games folders
* of earlier builds included), a removed game, a store install, or the first-level folder under
* the Steam library that holds it. Null when it is in none.
*/
private fun ownerFolder(context: Context, exe: File): File? {
GamesFolderMigration.run(context)
val path = canonical(exe)
@@ -270,7 +207,6 @@ object AddedGames {
AddedExes.list(context).map { File(it.folder) }.firstOrNull(::holds)?.let { return it }
SessionPrefs.removedAddedGames(context).map { File(it) }.firstOrNull(::holds)?.let { return it }
StoreInstallRoot.gameFolders(context).firstOrNull(::holds)?.let { return it }
// Games folders of earlier builds are entries now, found above.
val dirs = listOfNotNull(
GameStorage.effective(context)?.let { File(it.path, "steamapps/common") },
GameStorage.effective(context)?.let { File(it.path) },
@@ -279,17 +215,11 @@ object AddedGames {
val base = canonical(dir)
if (!inside(path, base)) continue
val first = path.removePrefix("$base/").substringBefore('/')
File(dir, first).takeIf { it.isDirectory && first != "steamapps" }?.let { return it }
File(dir, first).takeIf { it.isDirectory && !isSteamLibraryFolder(first) }?.let { return it }
}
return null
}
/**
* Adds the game [exe] is: the game itself when it is listed already, that game's exe switched
* when [exe] sits in a listed (or removed) game's folder, else a new game for [exe]'s folder.
* Only that one game is read; Steam (the running client, then the listing) follows on
* [AddedGamesPublisher]. Nothing is added when the session cannot see the path.
*/
fun addExe(context: Context, exe: File): AddResult {
val app = context.applicationContext
if (!exe.isFile) return AddResult.NotFound
@@ -309,15 +239,8 @@ object AddedGames {
return if (owner == null) AddResult.Added(game) else AddResult.Switched(game)
}
/** What [addFolder] did: the games it added, and how many of the folder's games were listed already. */
class FolderResult(val added: List<Game>, val already: Int)
/**
* "Add all games in this folder": every subfolder of [dir] with an .exe becomes a game with
* [GameExePicker]'s best one (marked uncertain where nothing clearly won), as an entry of its
* own, not a Games folder. A subfolder that is, holds or sits in a listed game counts as
* already there; a removed one comes back. Steam follows once, on [AddedGamesPublisher]. Blocking.
*/
fun addFolder(context: Context, dir: File, onGame: (Game) -> Unit = {}, onChecking: (String?) -> Unit = {}): FolderResult {
val app = context.applicationContext
val removed = SessionPrefs.removedAddedGames(app).toSet()
@@ -343,11 +266,6 @@ object AddedGames {
return FolderResult(added, already)
}
/**
* The game folders already listed, without building any game: the first-level folders of the
* Steam library (as [scan] walks them), the Games tab's entries (Games folders of earlier builds
* included) and the store installs. Canonical paths; removed games are left out.
*/
private fun listedFolders(context: Context, removed: Set<String>): List<String> {
val out = ArrayList<String>()
GamesFolderMigration.run(context)
@@ -357,7 +275,7 @@ object AddedGames {
)
for (dir in dirs.distinctBy { it.absolutePath }) {
for (folder in dir.listFiles { f -> f.isDirectory }.orEmpty()) {
if (folder.path in removed || folder.name.equals("steamapps", ignoreCase = true)) continue
if (folder.path in removed || isSteamLibraryFolder(folder.name)) continue
if (folder.name == "Games" && StoreInstallRoot.roots(context).any { it.absolutePath == folder.absolutePath }) continue
out.add(canonical(folder))
}
@@ -367,11 +285,6 @@ object AddedGames {
return out
}
/**
* The folders under a library's steamapps/common that a manifest beside it already claims.
* Steam lists those itself, so a shortcut would only add a non-Steam copy of the game. Steam
* may lowercase installdir on Android's case-insensitive storage, so names compare lowercased.
*/
internal fun steamInstallDirs(dir: File): Set<String> {
val steamapps = dir.parentFile?.takeIf { dir.name == "common" && it.name == "steamapps" } ?: return emptySet()
return steamapps.listFiles { f -> f.isFile && f.name.startsWith("appmanifest_") && f.name.endsWith(".acf") }
@@ -385,33 +298,27 @@ object AddedGames {
private val INSTALL_DIR = Regex("\"installdir\"\\s*\"([^\"]*)\"", RegexOption.IGNORE_CASE)
/** [single]: the entry a game added from the Games tab has; its exe stands in when no other is chosen and the user picked it. */
private fun scanGame(context: Context, folder: File, out: MutableList<Game>, single: AddedExes.Entry? = null) {
run {
// A store install carries a sidecar; every one of them is a shortcut.
val sidecar = StoreGameSidecar.read(folder)
sidecar?.takeIf { it.id.isNotEmpty() }?.let { SessionPrefs.followStoreGame(context, "${it.store.id}:${it.id}", folder.path) { old -> !File(old).exists() } }
val ranking = rank(folder)
val candidates = ranking.exes
val picked = SessionPrefs.addedGameExe(context, folder.path)
val chosen = picked.takeIf { it.isNotEmpty() }?.let { File(it) }?.takeIf { it.isFile }
?: single?.takeIf { it.picked }?.let { File(it.exe) }?.takeIf { it.isFile }
// A store install says which exe it is; the folder's guess only when that file is gone.
val found = chosen ?: sidecar?.exeFile(folder)?.takeIf { it.isFile } ?: candidates.firstOrNull() ?: return
// The store's launcher .bat stands in front of the exe when the game needs arguments or
// environment; it is what the shortcut runs. Dropped when the user picked another exe.
val launch = if (chosen == null) sidecar?.launcherFile(folder)?.takeIf { it.isFile } ?: found else found
val epicLauncher = sidecar?.takeIf { it.store == Store.EPIC && (chosen == null || canonical(chosen) == canonical(it.exeFile(folder))) }
?.launcherFile(folder)?.takeIf { it.isFile }
val launch = epicLauncher ?: if (chosen == null) sidecar?.launcherFile(folder)?.takeIf { it.isFile } ?: found else found
val foundGuest = guestPath(context, launch)
if (foundGuest == null) { Log.w(TAG, "${folder.name}: the session cannot see ${launch.path}"); return }
// A store install's title; else the title the disk gives (the shortcut's appid is kept
// per folder from its first scan, so a name found later does not move it); else the folder.
val autoName = sidecar?.title ?: identifiedName(folder, found) ?: folder.name
// Keyed by the pre-rename path so shortcut ids, and the prefixes and saves under them, stay put.
val crc = CRC32().apply { update(("\"${foundGuest.replaceFirst(Regex("^$LIBRARY/"), "$LEGACY_LIBRARY/")}\"" + autoName).toByteArray()) }.value
val appId = SessionPrefs.addedGameAppId(context, folder.path, crc or 0x80000000L)
val config = accountConfig(File(LinuxRuntime.rootDir(context), "root/.local/share/Steam"))
val config = accountConfig(SessionPrefs.activeSteamRoot(context))
val record = config?.let { shortcutRecord(it, appId) }
val rev = record?.optInt("rev", 0) ?: 0
// Name, Start in and launch options edited in Steam's own Properties become the app's.
if (sidecar == null && record != null) adoptSteamEdits(context, folder, record)
val name = SessionPrefs.addedGameName(context, folder.path).takeIf { sidecar == null && it.isNotEmpty() } ?: autoName
val adopted = guestPath(context, folder)
@@ -431,7 +338,7 @@ object AddedGames {
}
}
private fun accountConfig(root: File): File? {
internal fun accountConfig(root: File): File? {
val users = runCatching { File(root, "config/loginusers.vdf").readText() }.getOrDefault("")
val recent = Regex(""""(\d{5,})"\s*\{([^}]*)\}""").findAll(users)
.lastOrNull { Regex(""""MostRecent"\s*"1"""").containsMatchIn(it.groupValues[2]) }
@@ -444,6 +351,9 @@ object AddedGames {
JSONObject(File(config, ".droiddeck-routes.json").readText()).optInt(appId.toString()).takeIf { it > 0 }
}.getOrNull()
fun inSteam(context: Context, appId: Long): Boolean =
accountConfig(SessionPrefs.activeSteamRoot(context))?.let { shortcutRecord(it, appId) } != null
private fun shortcutRecord(config: File, appId: Long): JSONObject? = runCatching {
JSONObject(File(config, ".droiddeck-shortcuts.json").readText()).optJSONObject("games")?.optJSONObject(appId.toString())
}.getOrNull()
@@ -460,12 +370,6 @@ object AddedGames {
private fun unquote(value: String?): String = value.orEmpty().trim().trim('"')
/**
* What the user changed in Steam's Properties since the app last wrote the shortcut ([record]:
* what the app asked for, and what Steam had): a new name, launch options, or a Start in other
* than the exe's folder, taken over as the app's own. Each Steam value is taken once, so an app
* edit made after it is not undone before the shortcut is written again.
*/
internal fun adoptSteamEdits(context: Context, folder: File, record: JSONObject) {
val app = record.optJSONObject("app") ?: return
val steam = record.optJSONObject("steam") ?: return
@@ -489,8 +393,8 @@ object AddedGames {
}
}
/** The list the session hands the runtime's shortcuts writer; one file per session start. */
fun writeListing(context: Context, games: List<Game>): File {
fun writeListing(context: Context, games: List<Game>,
extra: List<JSONObject> = listOfNotNull(com.droiddeck.launcher.stores.epic.EpicClient.steamListing(context))): File {
val file = File(context.filesDir, "session/added-games.json").apply { parentFile?.mkdirs() }
val json = StringBuilder("[")
games.forEachIndexed { i, g ->
@@ -500,14 +404,17 @@ object AddedGames {
.append(",\"dir\":").append(quote(g.guestDir)).append(",\"appid\":").append(g.appId)
.append(",\"seen\":").append(SessionPrefs.addedGameExeSeen(context, g.folder.path))
if (g.launchOptions.isNotEmpty()) json.append(",\"launch\":").append(quote(g.launchOptions))
// The art, as the session sees it: the app's cache is bound at its own path, a file in
// the game's folder at the folder's guest path.
if (g.source != Library.ADDED) json.append(",\"store\":").append(quote(g.source))
val art = AddedGameArt.resolve(context, g)
val pieces = listOf("p" to art.portrait, "header" to art.header, "hero" to art.hero, "logo" to art.logo, "icon" to art.icon)
.mapNotNull { (k, f) -> f?.let { file -> artGuestPath(context, file)?.let { k to it } } }
if (pieces.isNotEmpty()) json.append(",\"art\":{").append(pieces.joinToString(",") { (k, v) -> quote(k) + ":" + quote(v) }).append('}')
json.append('}')
}
extra.forEachIndexed { i, entry ->
if (games.isNotEmpty() || i > 0) json.append(',')
json.append(entry.toString())
}
file.writeText(json.append(']').toString())
return file
}
@@ -5,23 +5,15 @@ import android.util.Log
import androidx.annotation.VisibleForTesting
import java.util.concurrent.Executors
/**
* The slow part of an added-game change, off the caller's path and in order: the running client
* told ([later]'s work), then the listing the shortcuts writer reads at the next session start and
* the library cache, both from one scan ([republish]). An add, an edit or a removal shows at once;
* this follows.
*/
object AddedGamesPublisher {
private const val TAG = "AddedGamesPublisher"
/** Tests publish at once, to read the listing right after a change. */
@VisibleForTesting
@Volatile
internal var now = false
private val worker = Executors.newSingleThreadExecutor { r -> Thread(r, "added-games-publish") }
/** [live] (a DevTools call or nothing), then [republish], on the worker. */
fun later(context: Context, live: () -> Unit = {}) {
val app = context.applicationContext
val work = Runnable {
@@ -31,10 +23,10 @@ object AddedGamesPublisher {
if (now) work.run() else worker.execute(work)
}
/** The listing and the library cache from one scan of the added games. Blocking. */
fun republish(context: Context) {
val games = AddedGames.scan(context)
AddedGames.writeListing(context, games)
LibraryCache.save(context, Library.launchableGames(context, added = games))
val arch = com.droiddeck.launcher.session.SessionPrefs.activeSteamClientArch(context)
LibraryCache.save(context, Library.launchableGames(context, added = games, arch = arch), arch == "x86_64")
}
}
@@ -16,29 +16,12 @@ import org.json.JSONObject
import java.io.File
import java.util.concurrent.PriorityBlockingQueue
/**
* Turns on, per game, the Windows components its source's list names ([WinCompSources]). Only the
* findings marked auto are turned on, only the ones that install here, and never one the user
* switched off: the user's switches are kept apart ([WinComponents.Selection.user]) and win.
*
* All the work is done by one background thread (low priority, so are the runtime helpers it
* starts), one job at a time from one queue: working out a game's list (kept, so a page opens on
* it) and downloading a component (each once, whichever games want it). The queue waits while a
* session runs; jobs nobody asked for (the start-up sweep, a game being launched) also wait on
* battery saver, a warm device or a metered network. Nothing here runs on the caller's thread.
*/
object AutoComponents {
private const val TAG = "AutoComponents"
/** The key a game's picks are kept under: the appid its prefix is named after. */
fun appKey(game: Library.SteamGame): String? =
if (game.library == Library.ADDED) Integer.toUnsignedString(game.appId) else game.appId.takeIf { it > 0 }?.toString()
/**
* What turns on by itself for [findings], given [catalog] and what is [installed]: the catalog
* name each auto finding installs as, with its reason. A component that needs a Windows
* installer, or is not in the catalog, stays out (the editor lists it as a suggestion).
*/
fun autoPicks(findings: List<WinCompSources.Finding>, catalog: Map<String, WinComponents.Component>): Map<String, WinComponents.AutoReason> {
val out = LinkedHashMap<String, WinComponents.AutoReason>()
for (f in findings) {
@@ -51,30 +34,22 @@ object AutoComponents {
return out
}
// ---- seams for tests ----
@VisibleForTesting internal var catalogOf: (Context, Boolean) -> List<WinComponents.Component>? =
{ c, network -> if (network) WinComponents.fetch(c) else WinComponents.cached(c) }
@VisibleForTesting internal var installedOf: (Context) -> Set<String> = { WinComponents.installedIds(it).toSet() }
@VisibleForTesting internal var installer: (Context, WinComponents.Component, Map<String, WinComponents.Component>, (WinComponents.Progress) -> Unit) -> String? =
{ c, comp, all, progress -> WinComponents.install(c, comp, all, progress) }
@VisibleForTesting internal var sessionRunning: () -> Boolean = { SessionState.running }
/** Battery saver, a warm device or a metered network: no work nobody asked for. */
@VisibleForTesting internal var unfavourable: (Context) -> Boolean = { c -> constrained(c) }
/** A usable network: a download waits for one. */
@VisibleForTesting internal var online: (Context) -> Boolean = { c -> isOnline(c) }
/** Tests run the queue on the caller's thread ([drain]) instead of the worker. */
@VisibleForTesting @Volatile internal var runNow = false
/** Bumped whenever a game's list or automatic components change, so an open page reads them again. */
val changes = MutableStateFlow(0)
/** The components downloading for an automatic pick right now, with their progress. */
val downloading = MutableStateFlow<Map<String, WinComponents.Progress>>(emptyMap())
/** Games (by [appKey]) whose list is queued or being worked out: a page waits on it. */
val pendingGames = MutableStateFlow<Set<String>>(emptySet())
/** Components queued for an automatic pick, not yet downloaded. */
val pendingComponents = MutableStateFlow<Set<String>>(emptySet())
private fun mark(job: Job, on: Boolean) {
@@ -84,10 +59,8 @@ object AutoComponents {
flow.value = if (on) flow.value + id else flow.value - id
}
/** One piece of work: a game's list ([game]) or one component's download ([component]). [asked]: the user is waiting on it. */
internal class Job(
val game: Library.SteamGame?, val component: String?, val asked: Boolean, val seq: Long, val pics: List<Int>? = null,
/** For a game Steam installed during the running session: may run in it, unless a game is running. */
val inSession: Boolean = false,
) {
val key get() = when {
@@ -95,24 +68,17 @@ object AutoComponents {
component != null -> "c:$component"
else -> "g:" + (game?.let { appKey(it) } ?: "")
}
/** Needs the network: a download, or Steam asked anonymously. */
val online get() = component != null || pics != null
}
private val seq = java.util.concurrent.atomic.AtomicLong()
/** What the user asked for first, then in the order queued. */
private val jobs = PriorityBlockingQueue<Job>(16, compareByDescending<Job> { it.asked }.thenBy { it.seq })
@Volatile private var draining = false
private val queuedKeys = java.util.Collections.synchronizedSet(HashSet<String>())
/** The games waiting on a component's download, to turn it on for when it lands. */
private val waiting = HashMap<String, MutableList<Library.SteamGame>>()
@Volatile private var worker: Thread? = null
private const val PAUSE_MS = 15_000L
/**
* [game]'s list worked out again on the worker, and its components queued: [asked] when the
* user is waiting (its page is open, or they installed or added it).
*/
fun queueGame(context: Context, game: Library.SteamGame, asked: Boolean) {
if (appKey(game) == null) return
enqueue(context, Job(game, null, asked, seq.incrementAndGet()))
@@ -121,10 +87,8 @@ object AutoComponents {
private fun queueDownload(context: Context, id: String, asked: Boolean, inSession: Boolean = false) =
enqueue(context, Job(null, id, asked, seq.incrementAndGet(), inSession = inSession))
/** Adds [job] unless the same one is queued (a game or a component, once each). */
private fun enqueue(context: Context, job: Job) {
if (!queuedKeys.add(job.key)) {
// Asked for now: ahead of what nobody asked for, so the open page does not wait behind the sweep.
if (job.asked && jobs.removeIf { it.key == job.key && !it.asked }) jobs.add(job)
if (runNow && !draining) drain(context)
return
@@ -135,12 +99,10 @@ object AutoComponents {
if (runNow) { if (!draining) drain(context) } else ensureWorker(context.applicationContext)
}
/** Whether [job] has to wait: always while a session runs; one nobody asked for also when it is unfavourable. */
internal fun mustWait(context: Context, job: Job): Boolean =
if (job.inSession && sessionRunning()) gameRunning() || (job.online && !online(context))
else sessionRunning() || (job.online && !online(context)) || (!job.asked && unfavourable(context))
/** A game is running in the session (Steam's reaper, or Proton waiting on its game). */
@VisibleForTesting internal var gameRunning: () -> Boolean = { gameProcessRunning() }
private fun gameProcessRunning(): Boolean = File("/proc").listFiles()?.any { p ->
@@ -148,17 +110,11 @@ object AutoComponents {
?.let { it.contains("SteamLaunch AppId=") || it.contains("proton waitforexitandrun") } == true
} == true
/**
* A game Steam installed during the running session: its list worked out and its components
* readied in the session (one job, low priority), so a launch right after finds them; it
* waits while a game runs. All other work stays paused until the session ends.
*/
fun queueInSession(context: Context, game: Library.SteamGame) {
if (appKey(game) == null) return
enqueue(context, Job(game, null, true, seq.incrementAndGet(), inSession = true))
}
/** Runs the queued jobs on this thread until the queue is empty or the next one has to wait. Tests. */
@VisibleForTesting
internal fun drain(context: Context) {
draining = true
@@ -173,7 +129,6 @@ object AutoComponents {
}
}
/** The first job in order that may run now, taken off the queue; null when none may. */
private fun next(context: Context): Job? = synchronized(jobs) {
val job = jobs.sortedWith(order).firstOrNull { !mustWait(context, it) } ?: return null
jobs.remove(job)
@@ -190,8 +145,6 @@ object AutoComponents {
worker = Thread({
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND)
while (true) {
// Something queued, then the first that may run (one for a game installed in the
// session goes ahead of paused work); else a look again in a while.
try { jobs.put(jobs.take()) } catch (e: InterruptedException) { return@Thread }
val job = next(app)
if (job == null) {
@@ -219,21 +172,14 @@ object AutoComponents {
}
}
/**
* [game]'s list (worked out again only when what it is read from changed), its automatic
* components that are downloaded turned on, and the rest queued for download.
*/
private fun apply(context: Context, game: Library.SteamGame, asked: Boolean, inSession: Boolean = false) {
val key = appKey(game) ?: return
val folder = game.gameFiles
if (game.library == Library.ADDED && game.source == Library.ADDED && folder != null && SteamMatch.get(context, folder.path) == null) {
AddedGames.single(context, folder.path)?.let { SteamMatch.resolve(context, folder.path, it.exe, it.name) }
}
// A GOG or Epic game installed before its sidecar kept the store's list: read it once from the store.
val store = com.droiddeck.launcher.stores.Store.byId(game.source)
if (folder != null && (store == com.droiddeck.launcher.stores.Store.GOG || store == com.droiddeck.launcher.stores.Store.EPIC)) StoreListBackfill.fill(context, folder)
// Its Steam app, when the client never saw it: Steam asked anonymously now if the user is
// waiting on this game (a pass over every game asks for all of them in one go, [sweep]).
if (asked && online(context)) steamAppOf(context, game)?.let { SteamAppInfo.lookup(context, listOf(it)) }
val findings = findings(context, game)
val catalog = (catalogOf(context, false) ?: catalogOf(context, true))?.associateBy { it.name } ?: return
@@ -249,7 +195,6 @@ object AutoComponents {
queueDownload(context, id, asked, inSession)
missing.add(id)
}
// In a session the launch waits a little for these (droiddeck-wincomponents reads the list).
if (inSession) Pending.set(context, key, missing)
val before = WinComponents.selection(context, key).auto
if (before.keys != kept.keys) {
@@ -259,7 +204,6 @@ object AutoComponents {
changes.value = changes.value + 1
}
/** One component downloaded and installed, then turned on for every game waiting on it. */
private fun download(context: Context, id: String, asked: Boolean, inSession: Boolean = false) {
val catalog = catalogOf(context, true)?.associateBy { it.name } ?: run { Pending.drop(context, id); return }
if (id !in installedOf(context)) {
@@ -279,10 +223,6 @@ object AutoComponents {
Pending.drop(context, id)
}
/**
* The components still being readied for games installed in the session, where the launch
* reads them (~/.config/droiddeck/wincomponents-pending.json in the runtime).
*/
internal object Pending {
private fun file(context: Context) = File(com.droiddeck.launcher.runtime.LinuxRuntime.rootDir(context), "root/.config/droiddeck/wincomponents-pending.json")
@@ -315,11 +255,9 @@ object AutoComponents {
}
}
// ---- the list, kept per game ----
private const val FINDINGS = "wincomp-findings.json"
/** What the list is read from, by modification time: a change means it is worked out again. */
internal fun signature(context: Context, game: Library.SteamGame): String {
val folder = game.gameFiles
val parts = listOfNotNull(
@@ -334,7 +272,6 @@ object AutoComponents {
return parts.joinToString("|")
}
/** [game]'s list: the one kept when nothing it is read from changed, else worked out again and kept. Worker only. */
internal fun findings(context: Context, game: Library.SteamGame): List<WinCompSources.Finding> {
val key = appKey(game) ?: return emptyList()
val sig = signature(context, game)
@@ -345,7 +282,6 @@ object AutoComponents {
return fresh
}
/** The list kept for [appKey], without working anything out; null before the first time. */
fun cachedFindings(context: Context, appKey: String): List<WinCompSources.Finding>? =
readFindings(context).optJSONObject(appKey)?.let { decode(it.optJSONArray("findings")) }
@@ -374,34 +310,21 @@ object AutoComponents {
WinCompSources.Finding(o.optString("component").takeIf { it.isNotEmpty() }, o.optString("original"), origin, o.optBoolean("auto"), args)
}
// ---- when ----
/** Before a launch from DroidDeck: nothing but a queued look (it waits for the session to end). */
fun beforeLaunch(context: Context, game: Library.SteamGame) = queueGame(context, game, asked = false)
/** After an install or an add: the user did it, so it does not wait on a metered network. */
fun refreshLater(context: Context, game: Library.SteamGame) = queueGame(context, game, asked = true)
/**
* Every game, whatever its source, at app start and after each session (Steam installs its
* games without a word to DroidDeck; older GOG and Epic installs get their list read once):
* only queued, one game at a time, behind anything asked for, under the same conditions. A
* game whose files did not change costs a look at their times.
*/
fun sweep(context: Context, games: List<Library.SteamGame>) {
// First every Steam app these games are that the client never saw, asked of Steam in one
// connection; then each game's list.
val apps = games.mapNotNull { steamAppOf(context, it) }.distinct()
if (apps.isNotEmpty() && SteamAppInfo.needLookup(context, apps).isNotEmpty()) enqueue(context, Job(null, null, false, seq.incrementAndGet(), pics = apps))
games.forEach { queueGame(context, it, asked = false) }
}
/** The Steam app whose list a game takes: a Steam game's own, or an added game's match. */
private fun steamAppOf(context: Context, game: Library.SteamGame): Int? =
if (game.library != Library.ADDED) game.appId.takeIf { it > 0 }
else game.gameFiles?.let { SteamMatch.get(context, it.path) }?.appId
/** The default network is there and validated (reaches the internet). */
fun isOnline(context: Context): Boolean {
val cm = context.getSystemService(ConnectivityManager::class.java) ?: return false
val caps = cm.getNetworkCapabilities(cm.activeNetwork ?: return false) ?: return false
@@ -409,8 +332,7 @@ object AutoComponents {
caps.hasCapability(android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED)
}
/** Battery saver, thermal status moderate or worse, or a metered network. */
private fun constrained(context: Context): Boolean {
fun constrained(context: Context): Boolean {
val power = context.getSystemService(PowerManager::class.java)
if (power?.isPowerSaveMode == true) return true
if (Build.VERSION.SDK_INT >= 29 && (power?.currentThermalStatus ?: 0) >= PowerManager.THERMAL_STATUS_MODERATE) return true
@@ -3,6 +3,7 @@ package com.droiddeck.launcher.frontend
import android.content.Context
import android.graphics.BitmapFactory
import android.util.Log
import com.droiddeck.launcher.core.DownloadSource
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
@@ -11,27 +12,12 @@ import java.net.HttpURLConnection
import java.net.URL
import java.net.URLEncoder
/**
* Box art for the emulators' games, found and kept on the device: the front end shows a disc image
* with nothing but its emulator's logo otherwise.
*
* In order: the name of the file against RetroArch's thumbnail collection for the system
* (libretro-thumbnails, named after the standard disc-dump names - "Need for Speed - Underground 2
* (USA)"), first exactly and then loosely (tags dropped, the same region preferred) against the
* collection's list of names; then the disc's own ID, read from the image - PS2 serials against
* xlenore/ps2-covers (what PCSX2 and ARMSX2 download from), GameCube/Wii IDs against GameTDB (what
* Dolphin uses). A PS3 disc image gives its own PS3_GAME/ICON0.PNG first: the libretro PS3
* collection is small. A cover is downloaded once into files/covers; a game none of them has is not
* asked about again for a few days.
*/
object CoverArt {
private const val TAG = "CoverArt"
private const val MISS_RETRY_MS = 3L * 24 * 3600 * 1000
private const val INDEX_TTL_MS = 14L * 24 * 3600 * 1000
/** Downloads in one pass, so a first visit to a big library does not hold the network for long. */
private const val MAX_PER_PASS = 40
/** The emulator's systems in libretro-thumbnails, in the order they are tried. */
private val collections = mapOf(
"rpcs3" to listOf("Sony_-_PlayStation_3"),
"armsx2" to listOf("Sony_-_PlayStation_2"),
@@ -44,14 +30,12 @@ object CoverArt {
private fun root(context: Context) = File(context.filesDir, "covers")
/** The file name, as the cover is kept under: a game keeps its art when its folder moves. */
private fun keyOf(rom: Library.Rom): String =
rom.hostPath.name.replace(Regex("[^A-Za-z0-9._() -]"), "_").take(150)
private fun coverFile(context: Context, rom: Library.Rom) = File(root(context), "${rom.emulatorId}/${keyOf(rom)}.img")
private fun missFile(context: Context, rom: Library.Rom) = File(root(context), "${rom.emulatorId}/${keyOf(rom)}.miss")
/** A cover already on the device for this game, or null. */
fun cached(context: Context, rom: Library.Rom): File? = coverFile(context, rom).takeIf { it.isFile && it.length() > 0 }
private fun wanted(context: Context, rom: Library.Rom): Boolean {
@@ -60,10 +44,6 @@ object CoverArt {
return !(miss.isFile && System.currentTimeMillis() - miss.lastModified() < MISS_RETRY_MS)
}
/**
* Looks for covers for the games that have none, on the calling (background) thread. True when
* at least one was found, so the caller rebuilds the list to show it.
*/
fun fetchMissing(context: Context, roms: List<Library.Rom>): Boolean {
var found = false
var tries = 0
@@ -71,7 +51,6 @@ object CoverArt {
if (!wanted(context, rom)) continue
if (++tries > MAX_PER_PASS) break
val target = coverFile(context, rom)
// A PS3 disc carries its own cover (PS3_GAME/ICON0.PNG, not encrypted): no download.
if (rom.emulatorId == "rpcs3" && try { copyPs3Icon(rom.hostPath, target) } catch (e: Exception) { false }) {
Log.i(TAG, "${rom.name}: ICON0.PNG from the disc")
missFile(context, rom).delete()
@@ -93,13 +72,10 @@ object CoverArt {
private fun findUrl(context: Context, rom: Library.Rom): String? {
val name = rom.hostPath.nameWithoutExtension.removeSuffix(".dec")
for (collection in collections.getValue(rom.emulatorId)) {
// Exactly the dump name first: no listing needed.
val exact = thumbnailUrl(collection, name)
if (exists(exact)) return exact
// Then loosely, against the collection's names.
nearest(name, names(context, collection))?.let { return thumbnailUrl(collection, it) }
}
// The disc's own ID.
return when (rom.emulatorId) {
"armsx2" -> ps2Serial(rom.hostPath)?.let { "https://raw.githubusercontent.com/xlenore/ps2-covers/main/covers/default/$it.jpg" }
"dolphin" -> nintendoDiscId(rom.hostPath)?.let { id ->
@@ -110,18 +86,15 @@ object CoverArt {
}?.takeIf { exists(it) }
}
/** libretro-thumbnails replaces these in a title's file name. */
private fun thumbName(title: String) = title.replace(Regex("[&*/:`<>?\\\\|\"]"), "_")
private fun thumbnailUrl(collection: String, title: String) =
"https://raw.githubusercontent.com/libretro-thumbnails/$collection/master/Named_Boxarts/" +
URLEncoder.encode("${thumbName(title)}.png", "UTF-8").replace("+", "%20")
// ---- loose matching ------------------------------------------------------------------------
private fun tags(name: String): List<String> = Regex("[(\\[]([^)\\]]*)[)\\]]").findAll(name).map { it.groupValues[1].lowercase() }.toList()
/** A title without its tags, articles and punctuation: "Legend of Zelda, The (USA)" = "legend of zelda". */
private fun base(name: String): String = name
.replace(Regex("\\s*[(\\[][^)\\]]*[)\\]]"), " ")
.lowercase()
@@ -139,7 +112,6 @@ object CoverArt {
return same.firstOrNull { region in tags(it) } ?: same.firstOrNull { "usa" in tags(it) } ?: same.first()
}
/** The titles in a collection's Named_Boxarts (no extension), kept for a fortnight. */
private fun names(context: Context, collection: String): List<String> {
val cache = File(root(context), "index-$collection.txt")
if (cache.isFile && System.currentTimeMillis() - cache.lastModified() < INDEX_TTL_MS) return cache.readLines()
@@ -161,12 +133,7 @@ object CoverArt {
private fun cachedOr(cache: File): List<String> = if (cache.isFile) cache.readLines() else emptyList()
// ---- disc IDs ------------------------------------------------------------------------------
/**
* A file's bytes from an ISO 9660 image by path ("PS3_GAME/ICON0.PNG"), or null. Primary volume
* names only (upper case, ";1" suffix), which is what PS2 and PS3 discs use.
*/
private fun isoFile(file: File, path: String, maxSize: Int): ByteArray? {
if (!file.name.endsWith(".iso", ignoreCase = true)) return null
RandomAccessFile(file, "r").use { f ->
@@ -208,14 +175,12 @@ object CoverArt {
return if (bounds.outWidth > 0 && tmp.renameTo(target)) true else { tmp.delete(); false }
}
/** "SLUS-21065" from a PS2 .iso's SYSTEM.CNF (BOOT2 = cdrom0:\SLUS_210.65;1), or null. */
private fun ps2Serial(file: File): String? {
val cnf = String(isoFile(file, "SYSTEM.CNF", 4096) ?: return null, Charsets.US_ASCII)
val m = Regex("BOOT2\\s*=\\s*cdrom0:\\\\?([A-Z]{4})_(\\d{3})\\.(\\d{2})").find(cnf) ?: return null
return "${m.groupValues[1]}-${m.groupValues[2]}${m.groupValues[3]}"
}
/** The six-character ID of a GameCube/Wii disc image (.iso/.gcm, or .wbfs), or null. */
private fun nintendoDiscId(file: File): String? {
val offset = when (file.extension.lowercase()) { "iso", "gcm" -> 0L; "wbfs" -> 0x200L; else -> return null }
RandomAccessFile(file, "r").use { f ->
@@ -225,7 +190,6 @@ object CoverArt {
}
}
// ---- network -------------------------------------------------------------------------------
private fun open(url: String, method: String = "GET"): HttpURLConnection =
(URL(url).openConnection() as HttpURLConnection).apply {
@@ -236,25 +200,62 @@ object CoverArt {
setRequestProperty("User-Agent", "DroidDeck")
}
private fun exists(url: String): Boolean = try {
val c = open(url, "HEAD"); val ok = c.responseCode == 200; c.disconnect(); ok
} catch (e: Exception) { false }
private fun <T> through(url: String, attempt: (String, Boolean) -> T?): T? {
for (source in DownloadSource.candidates(url)) {
val result = attempt(source, source == url)
if (result != null) {
DownloadSource.succeeded(source)
return result
}
}
DownloadSource.failed()
return null
}
private fun get(url: String): String? = try {
private fun exists(url: String): Boolean = through(url) { source, original -> head(source, original) } ?: false
private fun head(url: String, original: Boolean): Boolean? = try {
val c = open(url, "HEAD")
val code = c.responseCode
c.disconnect()
when {
code == 200 -> true
code == 404 || code == 410 -> if (original) false else null
else -> null
}
} catch (e: Exception) { null }
private fun get(url: String): String? = through(url) { source, original -> text(source, original) }
private fun text(url: String, original: Boolean): String? = try {
val c = open(url)
if (c.responseCode != 200) { c.disconnect(); null } else c.inputStream.bufferedReader().use { it.readText() }
val code = c.responseCode
when {
code == 200 -> c.inputStream.bufferedReader().use { it.readText() }.takeIf { it.isNotEmpty() }
code == 404 || code == 410 -> { c.disconnect(); if (original) "" else null }
else -> { c.disconnect(); null }
}
} catch (e: Exception) { Log.w(TAG, "$url: $e"); null }
/** Saves the image at [url] to [target] if it really is an image. */
private fun download(url: String, target: File): Boolean = try {
private fun download(url: String, target: File): Boolean = through(url) { source, original -> fetch(source, target, original) } ?: false
private fun fetch(url: String, target: File, original: Boolean): Boolean? = try {
val c = open(url)
if (c.responseCode != 200) { c.disconnect(); false } else {
target.parentFile?.mkdirs()
val tmp = File(target.path + ".part")
c.inputStream.use { input -> tmp.outputStream().use { input.copyTo(it) } }
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
BitmapFactory.decodeFile(tmp.path, bounds)
if (bounds.outWidth > 0 && tmp.renameTo(target)) true else { tmp.delete(); false }
when (val code = c.responseCode) {
200 -> {
target.parentFile?.mkdirs()
val tmp = File(target.path + ".part")
c.inputStream.use { input -> tmp.outputStream().use { input.copyTo(it) } }
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
BitmapFactory.decodeFile(tmp.path, bounds)
when {
bounds.outWidth > 0 && tmp.renameTo(target) -> true
original -> { tmp.delete(); false }
else -> { tmp.delete(); null }
}
}
404, 410 -> { c.disconnect(); if (original) false else null }
else -> { c.disconnect(); null }
}
} catch (e: Exception) { Log.w(TAG, "$url: $e"); false }
} catch (e: Exception) { Log.w(TAG, "$url: $e"); null }
}
@@ -3,49 +3,25 @@ package com.droiddeck.launcher.frontend
import java.io.File
import java.util.Locale
/**
* Best-effort detection of the Windows redistributables a game bundles, by scanning its install
* folder for the redist installers Steam/Epic/GOG ship (`_CommonRedist/`, `_Redist/`,
* `Redistributables/`, loose `vcredist_x64.exe`, …) and mapping them to entries in our component
* catalog (WinComponents, Bannerlator's components.json `name` keys). The result drives the
* Recommended part of a game's Windows components; the user still switches each one on.
* Bannerlator's detector, unchanged but for this note and its package.
*
* Wine-aware: keeps the redists that actually matter under Wine/DXVK (VC++ runtimes, .NET, PhysX,
* OpenAL, XACT, XNA, GFWL) and SKIPS the ones DXVK/VKD3D already provide (DX12 Agility SDK) or that
* aren't our components (anti-cheat, Rockstar/Social-Club/launcher setups). Never throws; returns an
* empty list on anything unexpected. Pure filesystem — no network, no Android deps (unit-testable).
*/
object DependencyDetector {
/** How a recommendation was found — drives the UI's "recommended vs optional" split. */
enum class Kind {
/** The game bundles the redist installer (in _CommonRedist etc.) — meant to be installed. */
BUNDLED,
/** A runtime DLL the game ships loose — usually already works from the game folder; install
* only if the game misbehaves. */
SHIPPED,
/** Steam installs it with the game from Steamworks Shared ([SteamRedists]). */
STEAM,
}
/** A recommended component: the catalog [componentName] to install, a display [label], the on-disk
* redist that triggered it ([reason]), and [kind] (bundled installer vs shipped DLL). */
data class Recommendation(val componentName: String, val label: String, val reason: String, val kind: Kind = Kind.BUNDLED)
private const val MAX_DEPTH = 5
private const val MAX_ENTRIES = 8000 // cost guard against giant game trees
private const val MAX_ENTRIES = 8000
/** A loose VC++ installer with no year in its path — resolved to "latest" only if no specific
* vcredistYYYY was found anywhere (else it's just a duplicate of the versioned one). */
private const val VCREDIST_LOOSE = "__vcredist_loose"
/** Directory names that are redist containers worth descending into. */
private val REDIST_DIR_RE = Regex(
"""(?i)^(_?commonredist|_?redist(ributables)?|__?installer|vcredist|vc_redist|directx|direct_x|physx|openal|dotnet(fx)?|\.net|xna|xact|redist|support)$"""
)
/** Skip whole subtrees that are never our components. */
private val SKIP_DIR_RE = Regex("""(?i)^(d3d12.*|.*agility.*|dxvk|vkd3d|battleye|easyanticheat|eac|anticheat)$""")
private val YEAR_TO_VCREDIST = mapOf(
@@ -70,7 +46,6 @@ object DependencyDetector {
"dotnetcoredesktop8" to ".NET Desktop 8",
)
/** Preferred install order (runtimes others may depend on first). */
private val INSTALL_ORDER = listOf(
"vcredist2005", "vcredist2008", "vcredist2010", "vcredist2012", "vcredist2013",
"vcredist2015", "vcredist2019", "vcredist2022",
@@ -79,13 +54,6 @@ object DependencyDetector {
"d3dx9", "d3dx11", "xact", "xact_x64", "xna31", "xna40", "physx", "oalinst", "XLiveRedist",
)
/**
* Detect from the .exe the user picked, resolving the game root first: walk up from the exe's
* folder (bounded) to the nearest ancestor that holds a redist container (`_CommonRedist`, …) at
* its top level, and scan from there — so a nested exe (`…/Game/bin/x.exe`) still finds a root-level
* `_CommonRedist`, without over-reaching into a shared `steamapps/common` (which has no redist
* container of its own). Falls back to scanning the exe's own folder.
*/
fun detectForExe(exeFile: File): List<Recommendation> {
val start = exeFile.absoluteFile.parentFile ?: return emptyList()
var d: File? = start
@@ -101,19 +69,14 @@ object DependencyDetector {
return detect(start)
}
/**
* Scan [gameDir] and return the recommended components, de-duplicated (first reason wins), in a
* stable install-friendly order. Empty when nothing is detected or [gameDir] isn't a directory.
*/
fun detect(gameDir: File): List<Recommendation> {
if (!gameDir.isDirectory) return emptyList()
val hits = LinkedHashMap<String, Recommendation>() // componentName -> first recommendation
val hits = LinkedHashMap<String, Recommendation>()
var visited = 0
fun consider(f: File, isDir: Boolean, insideRedist: Boolean) {
val name = f.name.lowercase(Locale.US)
val path = f.path.replace('\\', '/').lowercase(Locale.US)
// Bundled = in a redist container or an installer (exe/msi/cab); shipped = a loose runtime DLL.
val path = relativeReason(gameDir, f).lowercase(Locale.US)
val kind = if (insideRedist || isDir || name.endsWith(".exe") || name.endsWith(".msi") || name.endsWith(".cab"))
Kind.BUNDLED else Kind.SHIPPED
for (comp in classify(path, name, isDir)) {
@@ -121,7 +84,7 @@ object DependencyDetector {
if (existing == null) {
hits[comp] = Recommendation(comp, LABELS[comp] ?: comp, relativeReason(gameDir, f), kind)
} else if (existing.kind == Kind.SHIPPED && kind == Kind.BUNDLED) {
hits[comp] = existing.copy(kind = Kind.BUNDLED) // a bundled installer outranks a shipped DLL
hits[comp] = existing.copy(kind = Kind.BUNDLED)
}
}
}
@@ -134,17 +97,11 @@ object DependencyDetector {
val lower = f.name.lowercase(Locale.US)
if (f.isDirectory) {
if (SKIP_DIR_RE.matches(lower)) continue
// Unity's <Game>_Data/Managed holds the game's own Mono assemblies, not a .NET
// Framework to install.
if (lower == "managed" && dir.name.endsWith("_Data", ignoreCase = true)) continue
val isRedistDir = insideRedist || REDIST_DIR_RE.matches(lower)
if (isRedistDir) consider(f, isDir = true, insideRedist = true)
// Aggressive coverage: descend redist containers fully, and otherwise sweep the game
// tree a few levels deep so runtime libraries the game ships (in bin/, etc.) are seen.
if (isRedistDir || depth < 3) walk(f, depth + 1, isRedistDir)
} else {
// Classify installers + runtime libraries (.dll/.exe/.msi) anywhere in the scanned
// tree; other files are ignored. Recommendations are advisory, so cast a wide net.
val ext = lower.substringAfterLast('.', "")
if (insideRedist || ext == "dll" || ext == "exe" || ext == "msi") consider(f, isDir = false, insideRedist = insideRedist)
}
@@ -160,20 +117,13 @@ object DependencyDetector {
}
}
/**
* The catalog components one installer stands for, by its path ("_CommonRedist/vcredist/2015/
* vc_redist.x64.exe", "DXSETUP.exe"): what a store's install list names. A VC++ installer
* with no year is the latest; empty for one that is none of ours.
*/
fun componentsFor(path: String): List<String> {
val p = path.replace('\\', '/').lowercase(Locale.US)
return classify(p, p.substringAfterLast('/'), isDir = false).map { if (it == VCREDIST_LOOSE) "vcredist2022" else it }
}
/** The name the catalog component [name] goes by, for a reason line. */
fun label(name: String): String = LABELS[name] ?: name
/** A loose VC++ installer resolves to the latest 14.x only if no specific vcredistYYYY was found. */
private fun resolveLooseVcredist(hits: LinkedHashMap<String, Recommendation>) {
val loose = hits.remove(VCREDIST_LOOSE) ?: return
val hasSpecific = hits.keys.any { it in YEAR_TO_VCREDIST.values }
@@ -186,52 +136,38 @@ object DependencyDetector {
return if (p.startsWith("$g/")) p.substring(g.length + 1) else f.name
}
/**
* Map a redist file/dir (its full lowercased [path], leaf [name], and whether it [isDir]) to
* catalog component name(s). Empty for things we deliberately skip.
*/
private fun classify(path: String, name: String, isDir: Boolean): List<String> {
// Hard skips first — DX12 is provided by DXVK/VKD3D; these aren't our components.
if (path.contains("d3d12") || path.contains("agility") ||
path.contains("battleye") || path.contains("anticheat") || path.contains("easyanticheat") ||
name.contains("socialclub") || name.contains("social-club") ||
name.contains("rockstar") || name == "setup.exe"
) return emptyList()
// Visual C++ runtimes — most important. Year comes from the path context.
if (path.contains("vcredist") || path.contains("vc_redist") || name.startsWith("vcredist") || name.startsWith("vc_redist")) {
val year = YEAR_TO_VCREDIST.keys.firstOrNull { path.contains(it) }
return when {
year != null -> listOf(YEAR_TO_VCREDIST.getValue(year))
isDir -> emptyList() // bare "vcredist" container — wait for the year subfolder / installer
else -> listOf(VCREDIST_LOOSE) // loose installer, no year → resolved post-walk
isDir -> emptyList()
else -> listOf(VCREDIST_LOOSE)
}
}
// VC++ runtime DLL the game ships (msvcp140.dll → 2015-2022, msvcp120.dll → 2013, …).
VCRUNTIME_DLL.entries.firstOrNull { name.startsWith(it.key) }?.let { return listOf(it.value) }
if (path.contains("openal") || name.startsWith("oalinst") || name == "openal32.dll" || name == "wrap_oal.dll") return listOf("oalinst")
if (path.contains("physx") || name.startsWith("physx")) return listOf("physx")
if (path.contains("xact") || name.startsWith("xactengine") || name.startsWith("x3daudio")) return listOf("xact", "xact_x64")
// Games for Windows Live — the redist folder, OR a GFWL library the game ships (xlive.dll,
// Codemasters' dbxLive32/64, gfwl*): GFWL titles need XLiveRedist to run even when they don't
// bundle the redist itself.
if (path.contains("xliveredist") || name.contains("xlive") || name.startsWith("gfwl") || name.startsWith("dbxlive")) {
return listOf("XLiveRedist")
}
if (path.contains("xna")) return listOf(if (path.contains("3.1") || path.contains("3_1") || path.contains("v31")) "xna31" else "xna40")
// .NET — Framework and Core/Desktop runtimes.
if (path.contains("dotnet") || path.contains(".net") || name.startsWith("ndp") || name.startsWith("dotnetfx") || name.contains("windowsdesktop-runtime")) {
val comp = dotnetComponent("$path $name")
return when {
comp != null -> listOf(comp)
isDir -> emptyList() // bare ".NET" container — wait for the versioned installer
else -> listOf("dotnet48") // unlabeled .NET Framework installer file → sensible default
isDir -> emptyList()
else -> listOf("dotnet48")
}
}
// DirectX 11 helper DLLs the game ships (d3dx11_43.dll).
if (D3DX11_RE.matches(name)) return listOf("d3dx11")
// DirectX 9 helper DLLs / d3dcompiler / June-2010 DX redist (DXVK provides core d3d9 but NOT
// these helper DLLs).
if (name.startsWith("dxsetup") || name.startsWith("dxwebsetup") ||
D3DX9_RE.matches(name) || D3DCOMPILER_RE.matches(name) ||
(path.contains("directx") && name.endsWith(".exe"))
@@ -239,7 +175,6 @@ object DependencyDetector {
return emptyList()
}
/** Runtime DLL prefix → the VC++ redistributable that provides it (checked with startsWith). */
private val VCRUNTIME_DLL = linkedMapOf(
"vcruntime140" to "vcredist2022", "msvcp140" to "vcredist2022", "msvcr140" to "vcredist2022", "concrt140" to "vcredist2022",
"msvcp120" to "vcredist2013", "msvcr120" to "vcredist2013",
@@ -252,7 +187,6 @@ object DependencyDetector {
private val D3DX11_RE = Regex("""d3dx11_\d+\.dll""")
private val D3DCOMPILER_RE = Regex("""d3dcompiler_\d+\.dll""")
/** Resolve a .NET component from installer/path text, or null when no version is discernible. */
private fun dotnetComponent(s: String): String? = when {
s.contains("desktop-runtime-8") || Regex("""\b8\.\d""").containsMatchIn(s) -> "dotnetcoredesktop8"
s.contains("desktop-runtime-7") || Regex("""\b7\.\d""").containsMatchIn(s) -> "dotnetcoredesktop7"
@@ -264,6 +198,6 @@ object DependencyDetector {
s.contains("dotnetfx45") || s.contains("ndp45") || s.contains("4.5") -> "dotnet45"
s.contains("dotnetfx40") || s.contains("ndp40") || s.contains("4.0") -> "dotnet40"
s.contains("dotnetfx35") || s.contains("3.5") -> "dotnet35"
else -> null // no version signal (e.g. a bare "DotNet" container dir)
else -> null
}
}
@@ -2,26 +2,15 @@ package com.droiddeck.launcher.frontend
import java.io.File
/**
* Which `.exe` in a game folder is the game. A real install holds installers, crash handlers,
* anti-cheat stubs and launchers beside the game, and the game itself may sit several levels down
* (`Game/Binaries/Win64/Game-Win64-Shipping.exe`), so candidates are collected with a bounded walk
* and ranked rather than guessed by position.
*/
object GameExePicker {
/** How far below a game folder to look: Unreal puts the game three down, repacks deeper. */
const val MAX_DEPTH = 5
/** Folders and .exe files looked at per game before the walk stops, so a huge game stays quick. */
const val WALK_BUDGET = 400
/** Below this the pick is marked uncertain rather than trusted silently. */
const val CONFIDENT_SCORE = 60
/** The best and the runner-up this close together: nothing clearly won. */
const val AMBIGUOUS_MARGIN = 15
/** Folders that never hold the game itself. */
private val SKIP_DIRS = setOf(
"_commonredist", "commonredist", "redist", "redistributable", "redistributables",
"directx", "dotnet", "dotnetfx", "vcredist", "vc_redist", "prerequisites", "prereq",
@@ -32,7 +21,6 @@ object GameExePicker {
private val SKIP_DIR_PREFIX = listOf("dotnet", "vcredist", "$")
private val SKIP_PATHS = listOf("engine/extras", "engine/binaries/thirdparty")
/** Exe names that are never the game. */
private val JUNK_EXE_RE = Regex(
"""(?i)^(unins\w*|setup|install\w*|vc_?redist.*|vcredist.*|dxsetup|dxwebsetup|directx.*|""" +
"""oalinst|openal.*|dotnetfx.*|ndp\d.*|unitycrashhandler\d*|crashreport\w*|crashpad\w*|""" +
@@ -41,10 +29,8 @@ object GameExePicker {
"""activation\w*|register\w*|readme|report\w*|.*_debug|.*-debug)$""",
)
/** A folder's exes best first, with their scores, and whether the first clearly won. */
class Ranking(val exes: List<File>, val scores: List<Int>, val uncertain: Boolean)
/** Ranks the .exe files under [folder]; [keep] drops names the caller never offers. */
fun rank(folder: File, keep: (File) -> Boolean = { true }): Ranking {
val scored = collect(folder).filter { keep(it.first) }.map { (exe, depth) -> exe to score(exe, folder, depth) }
.sortedWith(compareByDescending<Pair<File, Int>> { it.second }.thenBy { it.first.path.lowercase() })
@@ -54,11 +40,6 @@ object GameExePicker {
return Ranking(scored.map { it.first }, scored.map { it.second }, uncertain)
}
/**
* Every plausible exe under [folder] with its depth (0 = in the folder itself): up to
* [MAX_DEPTH] folders down, nearest levels first, at most [WALK_BUDGET] entries, never through a
* symlink, past folders that never hold the game.
*/
private fun collect(folder: File): List<Pair<File, Int>> {
val found = ArrayList<Pair<File, Int>>()
var visited = 0
@@ -86,31 +67,23 @@ object GameExePicker {
return found
}
/**
* How likely [exe] is to be the game in [folder]. Relative only: the numbers rank candidates
* against each other and decide whether anything won clearly enough to trust.
*/
internal fun score(exe: File, folder: File, depth: Int): Int {
var score = 0
val base = exe.nameWithoutExtension
val exeKey = key(base)
val folderKey = key(folder.name)
// The exe named after its folder is the single strongest signal.
if (exeKey.isNotEmpty() && folderKey.isNotEmpty()) {
when {
exeKey == folderKey -> score += 100
folderKey.contains(exeKey) || exeKey.contains(folderKey) -> score += 60
}
}
// Unreal ships as <Game>-Win64-Shipping.exe under Binaries/Win64.
if (base.endsWith("-Win64-Shipping", true) || base.endsWith("-Win32-Shipping", true)) score += 70
val path = exe.absolutePath.lowercase()
if (path.contains("/binaries/win64") || path.contains("/binaries/win32")) score += 30
if (path.contains("/bin/") || path.contains("/bin64/")) score += 10
// A launcher is a real entry point, so it stays a candidate, just not the preferred one.
if (GameIdentifier.isLauncherExeName(base)) score -= 40
if (GameIdentifier.isJunkPeName(base)) score -= 30
// Shallower is more likely the entry point; big binaries beat small helpers.
score -= depth * 8
score += (exe.length() / (8L * 1024 * 1024)).toInt().coerceAtMost(30)
return score
@@ -125,6 +98,5 @@ object GameExePicker {
private fun isLink(f: File): Boolean = runCatching { java.nio.file.Files.isSymbolicLink(f.toPath()) }.getOrDefault(false)
/** Comparison key: letters and digits only, so "GTA IV" and "gta-iv" match. */
private fun key(s: String) = s.lowercase().filter { it.isLetterOrDigit() }
}
@@ -10,7 +10,6 @@ import java.io.File
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
/** Runs while DroidDeck's process is alive, including downloads in a background Steam session. */
object GameFileSync {
private val worker = Executors.newSingleThreadScheduledExecutor { task -> Thread(task, "game-file-sync").apply { isDaemon = true } }
private fun prefs(context: Context) = context.getSharedPreferences("game_files", Context.MODE_PRIVATE)
@@ -39,8 +38,7 @@ object GameFileSync {
@Synchronized private fun sync(context: Context, folder: File) {
check(LinuxRuntime.isInstalled(context)) { context.getString(R.string.game_sync_err_runtime) }
// A disconnected library must not be interpreted as a mass uninstall.
check(File(LinuxRuntime.rootDir(context), "root/.local/share/Steam/steamapps").isDirectory) { context.getString(R.string.game_sync_err_steam_library) }
check(File(SessionPrefs.activeSteamRoot(context), "steamapps").isDirectory) { context.getString(R.string.game_sync_err_steam_library) }
val storage = GameStorage.effective(context)
val storageSetting = SessionPrefs.gameStorage(context)
val previousStorage = prefs(context).getString("storagePath", null)
@@ -11,14 +11,12 @@ import org.json.JSONObject
import java.io.File
import java.io.InputStream
/** Small, portable launch files for frontends that scan ROM folders. No guest paths or commands. */
object GameFiles {
const val EXTENSION = "steam"
private const val MAX_BYTES = 128
private const val INVENTORY = ".droiddeck-sync.json"
private const val FORMAT = "droiddeck-game-files-v1"
/** Called on a worker: content providers and shared storage may block. */
fun readIntent(context: Context, request: Intent): String? = runCatching {
if (request.action != Intent.ACTION_VIEW) return null
val uri = request.data ?: return null
@@ -36,7 +34,6 @@ object GameFiles {
}.getOrNull()
fun read(stream: InputStream): String? {
// An untrusted URI must not make us buffer an arbitrary ROM or parse a command.
val bytes = ByteArray(MAX_BYTES + 1)
var count = 0
while (count < bytes.size) {
@@ -84,7 +81,6 @@ object GameFiles {
} else target
}
/** Cocoon matches icons in its data folder's images directory. Never replace custom art. */
private fun exportArt(context: Context, folder: File, game: Library.SteamGame) {
val art = listOfNotNull(game.icon, game.art).firstOrNull {
it.isFile && it.extension.lowercase() in listOf("jpg", "jpeg", "png", "webp")
@@ -100,7 +96,6 @@ object GameFiles {
if (!target.exists()) AtomicFile(target).writeBytes(art.readBytes())
}
/** Only remove files in our inventory whose content is still exactly what we wrote. */
@Synchronized fun sync(context: Context, folder: File, games: List<Library.SteamGame>) {
require(folder.isDirectory) { context.getString(R.string.gfiles_folder_unavailable) }
val inventory = child(context, folder, INVENTORY)
@@ -119,21 +114,17 @@ object GameFiles {
else exportTarget(context, folder, game, name)
val text = contents(game.gameIdString)
val existed = target.exists()
// Keep an edited export without letting it block the rest of the library sync.
val owned = inventoryOwns(context, folder, previous, target, game.gameIdString)
val file = if (owned && target.exists() &&
!matches(target, text)) target else exportNamed(context, folder, game, target.name)
if (!existed || owned) next.put(file.name, game.gameIdString)
file
}
// Write new exports before pruning; a failed write must not remove old launch files.
for (name in previous.keys()) {
val id = previous.getString(name)
if (!next.has(name) && name.endsWith(".$EXTENSION") && GameLaunchLink.validId(id)) {
val file = child(context, folder, name)
if (matches(file, contents(id))) {
// Android shared storage is often case insensitive. A case-only rename may
// still resolve to the live export, which must not be pruned as an old file.
val retained = exports.any { java.nio.file.Files.isSameFile(it.toPath(), file.toPath()) }
if (!retained) check(file.delete()) { context.getString(R.string.gfiles_remove_failed, name) }
}
@@ -2,65 +2,35 @@ package com.droiddeck.launcher.frontend
import java.io.File
/**
* Best-effort identification of a game from its on-disk footprint, given its .exe: a Steam appId
* and/or a display name, so an added game is not just named after its folder or exe. Pure
* filesystem and PE parsing, no network, never throws; it reports only what the disk tells.
*
* Signal chain (appId preferred; best name chosen across all hits):
* 1. steam_appid.txt → appId (Goldberg-style installs)
* 2. ColdClientLoader / emu ini → appId (AppId= key)
* 3. appmanifest_<id>.acf → appId + name (real Steam install under steamapps/)
* 4. goggame-<id>.info → name (+ gogId) (GOG installs)
* 5. PE VS_VERSIONINFO → name (ProductName / FileDescription)
* 6. cleaned folder / exe name → name (weak fallback)
*/
object GameIdentifier {
/** How far up the directory tree to look for marker files (games nest deep, e.g. Game/Binaries/Win64/x.exe). */
private const val MAX_PARENT_DEPTH = 6
enum class Source { STEAM_APPID_TXT, STEAM_MANIFEST_ACF, STEAM_EMU_INI, GOG_INFO, PE_VERSION, FILENAME, NONE }
enum class Confidence { HIGH, MEDIUM, LOW }
/**
* @param appId Steam appId if one was found on disk, else null.
* @param gogId GOG product id if a goggame info file was found, else null.
* @param name Best display name found locally; may be null when only an appId was
* found (caller resolves the name from the appId over the network).
* @param source Which signal produced the primary identity.
*/
data class GameIdentity(
val appId: Int? = null,
val gogId: String? = null,
val name: String? = null,
val source: Source = Source.NONE,
val confidence: Confidence = Confidence.LOW,
/** Which signal gave [name]: [Source.FILENAME] means only a cleaned folder or exe name. */
val nameSource: Source = Source.NONE,
) {
val hasStrongId: Boolean get() = appId != null
}
/** Identify the game owning [exeFile]. Never throws; returns an empty identity if nothing matched. */
fun identify(exeFile: File): GameIdentity {
val dirs = ancestorDirs(exeFile)
// Steam appId from a bare steam_appid.txt (in the exe dir, a steam_settings/ subdir, or an ancestor).
val steamAppId = dirs.firstNotNullOfOrNull { readSteamAppIdTxt(it) }
// AppId= out of a ColdClientLoader.ini / steam_emu.ini next to the game.
val emuAppId = dirs.firstNotNullOfOrNull { readEmuIniAppId(it) }
// Real Steam install: appmanifest_<id>.acf up under steamapps/ — carries both appId and name.
val acf = resolveSteamManifest(exeFile, dirs)
// GOG: goggame-<id>.info JSON with the real title.
val gog = dirs.firstNotNullOfOrNull { readGogInfo(it) }
// PE ProductName / FileDescription — but not when it merely names a launcher/loader
// ("Rockstar Games Launcher Redirector", "GSE", …), which must not become the game name.
val peName = PeVersionInfo.bestName(PeVersionInfo.read(exeFile))?.takeUnless { isJunkPeName(it) }
val appId = acf?.appId ?: steamAppId ?: emuAppId
// Best name, most-trustworthy first.
val nameHit: Pair<String, Source>? = when {
acf?.name != null -> acf.name to Source.STEAM_MANIFEST_ACF
gog?.name != null -> gog.name to Source.GOG_INFO
@@ -68,7 +38,6 @@ object GameIdentifier {
else -> cleanedFallbackName(exeFile)?.let { it to Source.FILENAME }
}
// Primary source = whatever gave us the appId (strongest), else the name source.
val primary: Source = when {
acf?.appId != null -> Source.STEAM_MANIFEST_ACF
steamAppId != null -> Source.STEAM_APPID_TXT
@@ -93,27 +62,19 @@ object GameIdentifier {
)
}
/**
* Make an identified title readable and filesystem-safe (a Steam shortcut's name also names
* files): "DARK SOULS: REMASTERED" becomes "DARK SOULS - REMASTERED". Strips trademark marks,
* turns title punctuation into safe equivalents, and collapses whitespace.
*/
fun normalizeName(raw: String): String {
var s = raw
s = s.replace("™", "").replace("®", "").replace("©", "")
s = s.replace(Regex("""(?i)\(\s*(tm|r|c)\s*\)"""), "") // "(TM)" / "(R)" / "(C)"
s = s.replace(":", " - ") // colon → dash separator (safe + readable)
s = s.replace(Regex("""[\\/]"""), " ") // path separators → space
s = s.replace(Regex("""["*?<>|]"""), "") // other illegal chars → drop
// Drop trailing generic descriptors a PE FileDescription tacks on ("DiRT 3 Executable" → "DiRT 3").
s = s.replace(Regex("""(?i)\(\s*(tm|r|c)\s*\)"""), "")
s = s.replace(":", " - ")
s = s.replace(Regex("""[\\/]"""), " ")
s = s.replace(Regex("""["*?<>|]"""), "")
s = s.replace(Regex("""(?i)[ \-]+(executable|application|launcher|redistributable|installer|setup)$"""), "")
s = s.replace(Regex("""\s+"""), " ").trim() // collapse whitespace
return s.trim(' ', '-').trim() // tidy stray leading/trailing dashes
s = s.replace(Regex("""\s+"""), " ").trim()
return s.trim(' ', '-').trim()
}
// ── directory walk ──
/** The exe's directory plus its ancestors, nearest-first, bounded by [MAX_PARENT_DEPTH]. */
private fun ancestorDirs(exeFile: File): List<File> {
val out = ArrayList<File>(MAX_PARENT_DEPTH + 1)
var d: File? = exeFile.absoluteFile.parentFile
@@ -126,12 +87,9 @@ object GameIdentifier {
return out
}
// ── signal readers (all best-effort; return null on anything unexpected) ──
/** steam_appid.txt directly in [dir] or in a steam_settings/ subdir. */
private fun readSteamAppIdTxt(dir: File): Int? {
for (candidate in arrayOf(File(dir, "steam_appid.txt"), File(dir, "steam_settings/steam_appid.txt"))) {
// Strip a leading UTF-8 BOM (trim() doesn't) before reading the digits.
val id = readSmallTextFile(candidate)?.removePrefix("\uFEFF")?.trim()
?.takeWhile { it.isDigit() }?.toIntOrNull()
if (id != null && id > 0) return id
@@ -139,12 +97,6 @@ object GameIdentifier {
return null
}
/**
* First `AppId=<n>` in a crack/emu config .ini next to the game (ColdClientLoader, steam_emu,
* flt, hlm, …). Known names are tried first, then any other `.ini` in the dir — only crack
* configs carry an `AppId=` key, game settings .ini files (settings.ini, graphics.ini) don't,
* so the broad scan is safe.
*/
private fun readEmuIniAppId(dir: File): Int? {
val re = Regex("""(?im)^\s*appid\s*=\s*(\d+)""")
val named = arrayOf(
@@ -166,13 +118,6 @@ object GameIdentifier {
private data class Acf(val appId: Int?, val name: String?)
/**
* Resolve the Steam appmanifest for this game: find the `common` ancestor on the exe's
* path, read the sibling `steamapps/appmanifest_*.acf`, and match by installdir == the
* game folder under common. No match, no manifest: a folder in steamapps/common that no
* manifest claims is exactly the kind added games list, and the library's one other game is
* not its name.
*/
private fun resolveSteamManifest(exeFile: File, dirs: List<File>): Acf? {
val commonIdx = dirs.indexOfFirst { it.name.equals("common", ignoreCase = true) }
if (commonIdx < 0) return null
@@ -183,7 +128,6 @@ object GameIdentifier {
} ?: return null
if (manifests.isEmpty()) return null
// The game's install folder is the child of `common` on our descent path.
val gameFolder = if (commonIdx > 0) dirs[commonIdx - 1].name else null
val match = manifests.firstOrNull { m ->
@@ -200,7 +144,6 @@ object GameIdentifier {
private data class Gog(val id: String?, val name: String?)
/** goggame-<id>.info JSON: pull `name` (and the id from the filename). */
private fun readGogInfo(dir: File): Gog? {
val files = dir.listFiles { f ->
f.isFile && f.name.startsWith("goggame-") && f.name.endsWith(".info")
@@ -214,75 +157,42 @@ object GameIdentifier {
return null
}
// ── filename fallback ──
private val REPACK_TOKENS = setOf(
"fitgirl", "dodi", "codex", "plaza", "skidrow", "rune", "empress", "flt", "tenoke",
"reloaded", "elamigos", "razor1911", "hoodlum", "gog", "repack", "multi", "goldberg",
// repack/site tags that show up in folder names
"ankergames", "steamrip", "onlinefix", "online-fix", "gload", "fitgirlrepacks",
)
private const val EXE_NOISE_WORDS =
"""win64|win32|shipping|wingdk|client|launcher|game|x64|x86|64bit|32bit"""
/**
* Does this exe basename look like engine/arch boilerplate rather than a title? Deliberately
* UNANCHORED — "GameConTest" is boilerplate-ish wherever the giveaway sits, and the answer
* decides whether we trust the folder name instead of the exe name.
*/
private val EXE_NOISE_RE = Regex("""(?i)[-_. ]*(?:$EXE_NOISE_WORDS)""")
/**
* Trailing noise to strip off a title. ANCHORED to the end and allowed to repeat, because these
* words are only junk as a suffix: "Witcher3-Win64-Shipping" -> "Witcher3", while
* "Game of Thrones" and "Client Simulator" must survive intact.
*
* Kept separate from [EXE_NOISE_RE] on purpose. One regex used to serve both jobs, so
* anchoring it to fix stripping ALSO narrowed the boilerplate test — and a game in
* "Game Controller Test/GameConTest.exe" stopped preferring its folder and became "GameConTest".
*/
private val EXE_SUFFIX_RE = Regex("""(?i)(?:[-_. ]*(?:$EXE_NOISE_WORDS))+$""")
private val VERSION_TOKEN_RE = Regex("""(?i)\b(v?\d+([._]\d+)+|build[-_ ]?\d+|update[-_ ]?\d+)\b""")
/**
* True when an exe basename is a launcher/loader rather than the game — its parent folder is the
* better title. Catches "PlayGTAV"/"Play - GTA IV"/"LaunchGame"/"MyLauncher"/"GSE" but NOT a real
* title that merely starts with those letters ("PlayerUnknown"). A "play|launch|start|run" prefix
* counts only when it's a whole token — followed by a separator or a camelCase uppercase boundary,
* not another lowercase letter (checked case-sensitively, so it survives the case-insensitive JUNK
* matching used elsewhere).
*/
/** True when an exe basename looks like a launcher/loader rather than the game itself. */
fun isLauncherExeName(base: String): Boolean {
val lower = base.lowercase()
if (lower == "gse" || lower.endsWith("launcher") || lower.endsWith("redirector")) return true
for (p in arrayOf("play", "launch", "start", "run")) {
if (!lower.startsWith(p)) continue
val rest = base.substring(p.length)
if (rest.isEmpty()) return true // exactly "Play"
if (rest.isEmpty()) return true
val c = rest[0]
if (!c.isLetterOrDigit() || c.isUpperCase()) return true // "Play - GTA IV" / camelCase "PlayGTAV"
// lowercase continuation ("Player…") → part of a longer word, not a launcher
if (!c.isLetterOrDigit() || c.isUpperCase()) return true
}
return false
}
/** A PE ProductName/FileDescription that describes a launcher/loader, not the game — reject it so it
* doesn't become the shortcut name (e.g. "Rockstar Games Launcher Redirector", "GSE", "Steam"). */
private val JUNK_NAME_RE = Regex(
"""(?i)^(gse|steam|steam client|goldberg.*|launcher|game|play|start|setup|.* launcher.*|.*launcher redirector|.* redirector|rockstar games launcher.*|epic games launcher|smartsteamemu|coldclient.*|steamemu.*)$"""
)
/** True when a PE name is a launcher/loader label rather than a real game title. */
fun isJunkPeName(name: String): Boolean {
val n = name.trim()
return n.isEmpty() || JUNK_NAME_RE.matches(n)
}
/**
* Prefer the game's install-folder name (usually the real title) over the exe basename when the
* exe is a UE-style shipping binary OR a launcher/loader; then strip repack/suffix noise.
*/
private fun cleanedFallbackName(exeFile: File): String? {
val exeBase = exeFile.nameWithoutExtension
val preferFolder = EXE_NOISE_RE.containsMatchIn(exeBase) ||
@@ -293,14 +203,12 @@ object GameIdentifier {
return cleanName(raw)
}
/** Strip bracketed tags, repack-group names, engine/arch suffixes and version tokens; tidy separators. */
private fun cleanName(raw: String): String? {
var s = raw
s = s.replace(Regex("""[\[(][^\])]*[\])]"""), " ") // (…) […]
s = s.replace(Regex("""[\[(][^\])]*[\])]"""), " ")
s = VERSION_TOKEN_RE.replace(s, " ")
s = EXE_SUFFIX_RE.replace(s, " ")
s = s.replace(Regex("""[._]+"""), " ")
// Drop standalone repack-group words.
s = s.split(Regex("""[\s-]+"""))
.filter { it.isNotBlank() && it.lowercase() !in REPACK_TOKENS }
.joinToString(" ")
@@ -308,20 +216,16 @@ object GameIdentifier {
return s.ifBlank { null }
}
// ── tiny text/format helpers (regex-based → no Android/org.json dependency, unit-testable) ──
/** Read a small text file (guards against accidentally slurping a huge file). */
private fun readSmallTextFile(f: File, maxBytes: Long = 1L * 1024 * 1024): String? = try {
if (f.isFile && f.length() in 1..maxBytes) f.readText(Charsets.UTF_8) else null
} catch (_: Throwable) {
null
}
/** Value of a Valve KeyValues (.acf/.vdf) `"key" "value"` pair, first match, case-insensitive key. */
private fun vdfValue(text: String, key: String): String? =
Regex(""""${Regex.escape(key)}"\s*"([^"]*)"""", RegexOption.IGNORE_CASE).find(text)?.groupValues?.getOrNull(1)?.trim()
/** Value of a JSON `"key": "value"` pair, first match. Sufficient for the flat goggame .info files. */
private fun jsonStringValue(text: String, key: String): String? =
Regex(""""${Regex.escape(key)}"\s*:\s*"([^"\\]*(?:\\.[^"\\]*)*)"""").find(text)
?.groupValues?.getOrNull(1)?.replace("\\\"", "\"")?.replace("\\\\", "\\")?.trim()
@@ -1,6 +1,5 @@
package com.droiddeck.launcher.frontend
/** External callers can select a game, never a guest path or arbitrary Steam command. */
object GameLaunchLink {
private val idPattern = Regex("[1-9][0-9]{0,19}")
private val linkPattern = Regex("droiddeck://game/([1-9][0-9]{0,19})")
@@ -5,12 +5,14 @@ import android.content.Context
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.Icon
import android.net.Uri
import android.widget.Toast
import com.droiddeck.launcher.MainActivity
import com.droiddeck.launcher.R
import java.io.File
object GameShortcuts {
fun launchIntent(context: Context, gameId: String) = Intent(Intent.ACTION_VIEW,
@@ -19,22 +21,35 @@ object GameShortcuts {
private fun info(context: Context, game: Library.SteamGame): ShortcutInfo {
val name = game.name.ifBlank { context.getString(R.string.app_name) }
val icon = listOfNotNull(game.icon, game.art).firstNotNullOfOrNull { file -> runCatching {
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
BitmapFactory.decodeFile(file.path, bounds)
if (bounds.outWidth <= 0 || bounds.outHeight <= 0) return@runCatching null
val options = BitmapFactory.Options().apply {
inSampleSize = 1
while (maxOf(bounds.outWidth, bounds.outHeight) / inSampleSize > 256) inSampleSize *= 2
}
BitmapFactory.decodeFile(file.path, options)?.let(Icon::createWithBitmap)
}.getOrNull() } ?: Icon.createWithResource(context, R.mipmap.ic_launcher)
val icon = iconBitmap(game)?.let(Icon::createWithBitmap) ?: Icon.createWithResource(context, R.mipmap.ic_launcher)
return ShortcutInfo.Builder(context, "game:${game.gameIdString}")
.setActivity(android.content.ComponentName(context, MainActivity::class.java))
.setShortLabel(name.take(40)).setLongLabel(name).setIcon(icon)
.setIntent(launchIntent(context, game.gameIdString)).build()
}
internal fun iconBitmap(game: Library.SteamGame): Bitmap? {
val icon = game.icon?.let(::decode)
if (icon != null && minOf(icon.width, icon.height) >= SHARP_ICON_PX) return icon
return game.art?.let(::decode)?.let(::square) ?: icon
}
private fun decode(file: File): Bitmap? = runCatching {
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
BitmapFactory.decodeFile(file.path, bounds)
if (bounds.outWidth <= 0 || bounds.outHeight <= 0) return@runCatching null
val options = BitmapFactory.Options().apply {
inSampleSize = 1
while (minOf(bounds.outWidth, bounds.outHeight) / (inSampleSize * 2) >= ICON_PX) inSampleSize *= 2
}
BitmapFactory.decodeFile(file.path, options)
}.getOrNull()
private fun square(bitmap: Bitmap): Bitmap {
val side = minOf(bitmap.width, bitmap.height)
return Bitmap.createBitmap(bitmap, (bitmap.width - side) / 2, (bitmap.height - side) / 2, side, side)
}
fun pin(activity: Activity, game: Library.SteamGame) {
val manager = activity.getSystemService(ShortcutManager::class.java)
val requested = runCatching { manager.isRequestPinShortcutSupported &&
@@ -42,7 +57,9 @@ object GameShortcuts {
if (!requested) Toast.makeText(activity, R.string.game_shortcut_unsupported, Toast.LENGTH_LONG).show()
}
/** Standard shortcut picker result, understood by launchers that import Android shortcuts. */
fun result(activity: Activity, game: Library.SteamGame): Intent =
activity.getSystemService(ShortcutManager::class.java).createShortcutResultIntent(info(activity, game))
private const val ICON_PX = 256
private const val SHARP_ICON_PX = 128
}
@@ -8,15 +8,6 @@ import com.droiddeck.launcher.stores.StoreInstallRoot
import java.io.File
import java.util.zip.CRC32
/**
* Games folders (the roots earlier builds let the user add in Steam settings) become what the
* Games tab's "Add all games in this folder" makes: one entry per game ([AddedExes]). Everything
* a game had is keyed by its own folder and stays as it was - the chosen exe, the shortcut appid,
* the name, Start in, launch options, art choices, a removal. The roots stay bound into the
* session at the same guest paths ([SessionPrefs.gameFolderBinds]), so every exe keeps its path
* inside the session and every Steam shortcut its target; they are no longer Steam libraries.
* Runs whenever saved roots remain (so twice is the same as once) and clears them after.
*/
object GamesFolderMigration {
private const val TAG = "GamesFolderMigration"
@@ -26,7 +17,6 @@ object GamesFolderMigration {
if (dirs.isEmpty()) return
val roots = legacyRoots(SessionPrefs.gameFolderBinds(context).map { it.first } + dirs)
.filter { root -> root.host.path in dirs }
// The binds first: a game's guest path (and with it the shortcut's appid) depends on them.
SessionPrefs.setGameFolderBinds(context, SessionPrefs.gameFolderBinds(context) + roots.map { it.host.path to it.guest })
val removed = SessionPrefs.removedAddedGames(context).toSet()
val entries = AddedExes.list(context).map { it.folder }.toMutableSet()
@@ -35,7 +25,7 @@ object GamesFolderMigration {
var n = 0
val steamInstalls = AddedGames.steamInstallDirs(root.host)
for (folder in root.host.listFiles { f -> f.isDirectory }?.sortedBy { it.name.lowercase() }.orEmpty()) {
if (folder.name.equals("steamapps", ignoreCase = true) || folder.name.lowercase() in steamInstalls) continue
if (AddedGames.isSteamLibraryFolder(folder.name) || folder.name.lowercase() in steamInstalls) continue
if (folder.absolutePath in storeRoots || StoreGameSidecar.read(folder) != null) continue
if (folder.path in entries) continue
val picked = SessionPrefs.addedGameExe(context, folder.path).takeIf { it.isNotEmpty() }?.let { File(it) }?.takeIf { it.isFile }
@@ -50,10 +40,6 @@ object GamesFolderMigration {
SessionPrefs.setGamesFoldersMigrated(context)
}
/**
* The guest paths earlier builds gave Games folders: /root/Games/<folder name>, with a short
* hash of the host path when two share a name. Kept exactly, so nothing moves in the session.
*/
internal fun legacyRoots(paths: List<String>): List<AddedGames.Root> {
val hosts = paths.distinct().map { File(it) }
val names = hosts.groupingBy { it.name.lowercase() }.eachCount()
@@ -8,40 +8,29 @@ import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.session.SecondaryLibrary
import java.io.File
/**
* What the front end lists: the Steam client's installed games (from its own appmanifests, both
* libraries) and each emulator's games (files in the ROMs folder, by extension, with a system
* folder as a hint). Read on a worker thread; nothing here is cached beyond one screen refresh.
*/
object Library {
/** [gameId] is what steam://rungameid/ takes: the appid for a Steam title, the shortcut id for an added game. */
/**
* [art] is the portrait capsule; [hero] the wide banner Steam shows above a game's page, when
* the client has cached one. [lastPlayed] is Steam's own "LastPlayed" (unix seconds), 0 = never.
*/
class SteamGame(
val appId: Int, val name: String, val art: File?, val library: String, val gameId: Long = appId.toLong(),
val hero: File? = null, val lastPlayed: Long = 0L,
val gameFiles: File? = null, val protonPrefix: File? = null,
val icon: File? = null,
/** Where the game came from: [SOURCE_STEAM], a store's id (gog, epic, amazon) or [ADDED] for a folder the user added. */
val source: String = SOURCE_STEAM,
/** The store's own id for a store install; null otherwise. */
val storeId: String? = null,
) {
/** Decimal form used by Steam links and Android shortcuts, including unsigned shortcut ids. */
fun withArt(art: File?, hero: File?, icon: File?) =
SteamGame(appId, name, art, library, gameId, hero, lastPlayed, gameFiles, protonPrefix, icon, source, storeId)
val gameIdString: String get() = java.lang.Long.toUnsignedString(gameId)
val profileKey: String get() = if (library == ADDED) java.lang.Integer.toUnsignedString(appId) else appId.toString()
}
/** The [SteamGame.library] of a game added to the library rather than installed by Steam, and the [SteamGame.source] of a plain added folder. */
const val ADDED = "added"
/** The [SteamGame.source] of a title Steam itself installed. */
const val EPIC_LAUNCHER = "epic-launcher"
const val EPIC_LAUNCHER_NAME = "Epic Games Launcher"
const val SOURCE_STEAM = "steam"
class Rom(val name: String, val hostPath: File, val guestPath: String, val emulatorId: String, val art: File? = null)
class Emulator(val id: String, val name: String, val system: String, val program: String, val installed: Boolean, val games: List<Rom>) {
/** The shown name of [system] when it is words rather than a platform name, else 0. */
val systemRes: Int get() = if (id == "retroarch") R.string.content_system_many else 0
/** The emulator's own icon, bundled (the runtime keeps them as theme SVGs the app cannot draw). */
val iconRes: Int get() = when (id) {
"rpcs3" -> R.drawable.emu_rpcs3; "armsx2" -> R.drawable.emu_pcsx2; "dolphin" -> R.drawable.emu_dolphin
"duckstation" -> R.drawable.emu_duckstation; "melonds" -> R.drawable.emu_melonds; "cemu" -> R.drawable.emu_cemu
@@ -49,34 +38,33 @@ object Library {
}
}
/** The client's own tools and runtimes live in steamapps beside the games; they are not titles. */
private val NOT_GAMES = setOf(
858280, // Proton 3.7
961940, // Proton 3.16
993090, // Lossless Scaling
1054830, // Proton 4.2
1070560, // Steam Linux Runtime 1.0
1113280, // Proton 4.11
1245040, // Proton 5.0
1391110, // Steam Linux Runtime 2.0
1420170, // Proton 5.13
1493710, // Proton Experimental
1580130, // Proton 6.3
1628350, // Steam Linux Runtime 3.0
1887720, // Proton 7
2180100, // Proton Hotfix
228980, // Steamworks Common Redistributables
2348590, // Proton 8
2805730, // Proton 9
3029110, // Lepton
3127680, // FEX
3658110, // Proton 10
4183110, // Steam Linux Runtime 4.0
4185400, // Steam Linux Runtime 4.0 for ARM64
4427310, // Proton Experimental for ARM64
4628710, // Proton 11 / Proton Next
4628740, // Proton 11 for ARM64
4690330, // Legacy Steam Runtime
858280,
961940,
993090,
1054830,
1070560,
1113280,
1245040,
1391110,
1420170,
1493710,
1580130,
1628350,
1887720,
2180100,
228980,
2348590,
2805730,
3029110,
3127680,
3658110,
4183110,
4185400,
4427310,
4628710,
4628740,
4690330,
)
private val STEAM_CAPSULES = listOf("library_capsule.jpg", "library_600x900.jpg")
private val STEAM_HEROES = listOf("library_hero.jpg")
@@ -86,16 +74,17 @@ object Library {
private val LAST_PLAYED = Regex("^\\s*\"LastPlayed\"\\s*\"(\\d+)\"", RegexOption.MULTILINE)
private val INSTALL_DIR = Regex("^\\s*\"installdir\"\\s*\"([^\"]*)\"", RegexOption.MULTILINE)
/** Steam's library roots visible to this launcher: its private default plus the selected library. */
private fun steamLibraries(context: Context): List<Pair<File, String>> {
val root = File(LinuxRuntime.rootDir(context), "root/.local/share/Steam")
internal fun steamLibraries(context: Context, arch: String = SessionPrefs.activeSteamClientArch(context)): List<Pair<File, String>> {
val root = SessionPrefs.steamRoot(context, arch)
// The session gives the x86-64 client only its card folder; the games folders stay the arm64 client's.
val card = GameStorage.effective(context)
if (arch == "x86_64") return listOfNotNull(root to "internal", card?.let { File(it.path, GameStorage.X64_CARD_DIR) to it.label })
return listOfNotNull(
root to "internal",
GameStorage.effective(context)?.let { File(it.path) to it.label },
card?.let { File(it.path) to it.label },
) + GameStorage.gamesFolderLibraries(context).map { it.host to it.label }
}
/** Proton keeps each game's prefix below compatdata/<appid>/pfx in a Steam library. */
fun protonPrefix(
context: Context, appId: Long, preferredLibrary: File? = null,
libraries: List<File> = steamLibraries(context).map { it.first },
@@ -112,10 +101,10 @@ object Library {
.firstOrNull { it.isDirectory }
}
fun steamGames(context: Context, strictRead: Boolean = false): List<SteamGame> {
val root = File(LinuxRuntime.rootDir(context), "root/.local/share/Steam")
fun steamGames(context: Context, strictRead: Boolean = false, arch: String = SessionPrefs.activeSteamClientArch(context)): List<SteamGame> {
val root = SessionPrefs.steamRoot(context, arch)
val cache = File(root, "appcache/librarycache")
val libraries = steamLibraries(context)
val libraries = steamLibraries(context, arch)
val roots = libraries.map { it.first }
val out = LinkedHashMap<Int, SteamGame>()
for ((library, label) in libraries) {
@@ -128,37 +117,47 @@ object Library {
val text = try { manifest.readText() } catch (e: Exception) { if (strictRead) throw e; return@forEach }
val name = NAME.find(text)?.groupValues?.get(1)?.trim().orEmpty()
val flags = STATE.find(text)?.groupValues?.get(1)?.toIntOrNull() ?: 0
// StateFlags 4 = fully installed; anything else is downloading, updating or broken.
if (name.isEmpty() || flags and 4 == 0) return@forEach
val art = steamCacheImage(cache, appId, STEAM_CAPSULES)
val hero = steamCacheImage(cache, appId, STEAM_HEROES)
val lastPlayed = LAST_PLAYED.find(text)?.groupValues?.get(1)?.toLongOrNull() ?: 0L
// App manifests are Valve KeyValues (VDF) files. installdir is one folder
// below steamapps/common; only expose it when the directory exists and the
// manifest value cannot escape that directory.
val installDir = INSTALL_DIR.find(text)?.groupValues?.get(1)?.trim()
?.takeIf { it.isNotEmpty() && it != "." && it != ".." && '/' !in it && '\\' !in it }
val gameFiles = installDir?.let { File(steamapps, "common/$it").takeIf(File::isDirectory) }
out[appId] = SteamGame(
out[appId] = SteamGameArt.withOverrides(context, SteamGame(
appId, name, art, label, hero = hero, lastPlayed = lastPlayed,
gameFiles = gameFiles,
protonPrefix = protonPrefix(context, appId.toLong(), library, roots),
icon = steamCacheImage(cache, appId, listOf("icon.jpg", "icon.png"))
?: File(cache, appId.toString()).listFiles()?.firstOrNull { it.isFile && STEAM_ICON.matches(it.name) },
)
))
}
}
return out.values.toList()
}
/** The same installed-game inventory used for links, shortcuts and file exports. */
/** [added]: the added games when the caller has just scanned them, so they are not scanned again. */
fun launchableGames(context: Context, strictRead: Boolean = false, added: List<AddedGames.Game>? = null): List<SteamGame> {
val roots = steamLibraries(context).map { it.first }
return (steamGames(context, strictRead) + (added ?: AddedGames.scan(context)).map { g -> addedGame(context, g, roots) }).distinctBy { it.gameId }
fun steamLibraryArt(context: Context): List<Pair<Int, File>> {
val cache = File(SessionPrefs.activeSteamRoot(context), "appcache/librarycache")
return cache.listFiles().orEmpty().mapNotNull { dir ->
val appId = dir.name.toIntOrNull()?.takeIf { it !in NOT_GAMES } ?: return@mapNotNull null
steamCacheImage(cache, appId, STEAM_CAPSULES)?.let { appId to it }
}
}
fun fromSteamClient(g: SteamGame): Boolean = g.library != ADDED && g.source == SOURCE_STEAM
fun launchableGames(
context: Context, strictRead: Boolean = false, added: List<AddedGames.Game>? = null,
arch: String = SessionPrefs.activeSteamClientArch(context),
): List<SteamGame> {
val roots = steamLibraries(context, arch).map { it.first }
val list = added ?: AddedGames.scan(context)
var steam = steamGames(context, strictRead, arch)
val targets = steam.filter(::fromSteamClient).map { SteamGrid.target(it, context) } + list.map { SteamGrid.target(it, context) }
if (runCatching { SteamGrid.sync(context, targets) }.getOrDefault(false)) steam = steamGames(context, strictRead, arch)
return (steam + list.map { g -> addedGame(context, g, roots) }).distinctBy { it.gameId }
}
/** One added game as the Games tab lists it; [roots] the Steam libraries, for its Proton prefix. */
fun addedGame(context: Context, g: AddedGames.Game, roots: List<File> = steamLibraries(context).map { it.first }): SteamGame =
AddedGameArt.resolve(context, g).let { art ->
SteamGame(
@@ -170,8 +169,7 @@ object Library {
)
}
/** Steam stores current library art inside hash-named folders under the app's cache dir. */
private fun steamCacheImage(cache: File, appId: Int, names: List<String>): File? {
internal fun steamCacheImage(cache: File, appId: Int, names: List<String>): File? {
val appDir = File(cache, appId.toString())
val dirs = listOf(appDir) + appDir.listFiles()
.orEmpty().filter { it.isDirectory }.sortedBy { it.name }
@@ -180,12 +178,9 @@ object Library {
.firstOrNull { it.isFile && it.length() > 0L }
}
/** [atPanel]: frames cheap enough to draw at the panel's own size (see [drawsAtPanel]). */
private class Spec(val id: String, val name: String, val system: String, val program: String, val folders: List<String>, val exts: Set<String>, val atPanel: Boolean = false)
private val specs = listOf(
Spec("rpcs3", "RPCS3", "PS3", "/opt/appimages/rpcs3.AppImage", listOf("ps3"), setOf("iso")),
// PS2: ARMSX2, the PCSX2 fork with ARM64 recompilers. Upstream PCSX2 interprets the PS2's
// CPUs on ARM64 (NFS Underground 2: 17 fps against ARMSX2's full 60), so it is not offered.
Spec("armsx2", "ARMSX2", "PS2", "/opt/appimages/armsx2.AppImage", listOf("ps2"), setOf("iso", "chd", "cso", "gz")),
Spec("dolphin", "Dolphin", "GameCube / Wii", "/opt/appimages/dolphin.AppImage", listOf("gc", "gamecube", "wii"), setOf("iso", "rvz", "gcz", "wbfs", "ciso")),
Spec("duckstation", "DuckStation", "PS1", "/opt/appimages/duckstation.AppImage", listOf("ps1", "psx"), setOf("cue", "chd", "pbp", "iso", "bin", "img", "ecm", "m3u")),
@@ -194,73 +189,47 @@ object Library {
Spec("ppsspp", "PPSSPP", "PSP", "/usr/bin/PPSSPPSDL", listOf("psp"), setOf("iso", "cso", "pbp", "chd")),
Spec("retroarch", "RetroArch", "many systems", "/usr/bin/retroarch", emptyList(), emptySet()),
)
/**
* A dump's file name as a title: its tags - (USA), (En,Fr,Es,Pt), [!], (v1.01) - identify the
* file and are dropped for reading ("Tomb Raider (USA) (En,Fr,Es,Pt)" -> "Tomb Raider"). The
* file keeps them, and the cover lookup still uses them.
*/
private fun displayTitle(name: String): String =
name.replace(Regex("\\s*[(\\[][^)\\]]*[)\\]]"), "").trim().ifEmpty { name }
/** Every system folder name the specs claim, so a loose-file scan of the root skips them. */
private val systemFolders: Set<String> by lazy { specs.flatMap { it.folders }.toSet() }
private val installedIds = mapOf(
"rpcs3" to "rpcs3", "armsx2" to "armsx2", "dolphin" to "dolphin", "duckstation" to "duckstation",
"melonds" to "melonds", "cemu" to "cemu", "ppsspp" to "emulators", "retroarch" to "emulators",
)
/** The emulator's name for a program path from the rail ("ARMSX2"), or null. */
fun nameForProgram(program: String?): String? =
if (program == com.droiddeck.launcher.runtime.FlatpakManager.LAUNCHER || program == com.droiddeck.launcher.runtime.AppImageManager.LAUNCHER ||
program == com.droiddeck.launcher.runtime.UserApps.SCRIPT_LAUNCHER) {
com.droiddeck.launcher.session.SessionState.programArgs.firstOrNull()?.let { flatpakNames[it] ?: it.substringAfterLast('.') }
} else specs.firstOrNull { it.program == program }?.name
} else com.droiddeck.launcher.session.NoSteamLaunch.nameFor(program, com.droiddeck.launcher.session.SessionState.programArgs)
?: specs.firstOrNull { it.program == program }?.name
/** Added apps' names by Flatpak id or directory, as launched: the session only knows that. */
val flatpakNames = java.util.concurrent.ConcurrentHashMap<String, String>()
/**
* Whether a program from the rail runs at the panel's own resolution rather than the session's
* 720p default. melonDS draws two 256x192 screens on the CPU: the panel's size costs it
* nothing, and 720p scaled up to the panel blurs the sharp pixels its screen layout is set
* up for (droiddeck-pad-defaults).
*/
fun drawsAtPanel(program: String?): Boolean = specs.any { it.program == program && it.atPanel }
/** Desktop catalog package that supplies this emulator. */
fun packageId(emulatorId: String): String? = installedIds[emulatorId]
/** Every emulator the app knows, installed or not, with the games its system folder holds. */
fun emulators(context: Context, installedPackage: (String) -> Boolean): List<Emulator> {
val romsRoot = SessionPrefs.romsDir(context).takeIf { it.isNotEmpty() }?.let(::File)?.takeIf { it.isDirectory }
return specs.map { spec ->
val games = ArrayList<Rom>()
if (romsRoot != null && spec.exts.isNotEmpty()) {
// The system's folder(s), matched without regard to case, and up to three folders
// inside them - a dump usually comes as a folder named for the game with the image
// inside it, and people sort those into folders of their own (ps2/games/<game>/).
// The root itself only one deep, for a file left loose there.
val systemDirs = romsRoot.listFiles { f -> f.isDirectory && f.name.lowercase() in spec.folders }.orEmpty().toList()
val dirs = LinkedHashSet<File>()
// A BIOS folder holds the console's firmware, not games (psx/bios/SCPH1001.BIN).
for (top in systemDirs) top.walkTopDown().maxDepth(3).onEnter { it.name.lowercase() !in firmwareFolders }
.filter { it.isDirectory }.forEach { dirs.add(it) }
dirs.add(romsRoot)
// ...and not into another system's folder: a PS3 .iso in ps3/ is not a PS2 game.
romsRoot.listFiles { f -> f.isDirectory && f.name.lowercase() !in systemFolders }?.forEach { dirs.add(it) }
for (dir in dirs) {
// A PS3 disc dump is a folder with PS3_GAME in it; RPCS3 boots the folder.
if (spec.id == "rpcs3" && File(dir, "PS3_GAME").isDirectory) {
val rel = dir.relativeTo(romsRoot).path
games.add(Rom(displayTitle(dir.name), dir, "/root/ROMs/$rel", spec.id,
// The dump carries its own art, as an installed package does.
art = File(dir, "PS3_GAME/ICON0.PNG").takeIf { it.isFile }))
continue
}
val files = dir.listFiles()?.sortedBy { it.name.lowercase() }.orEmpty()
// A disc sheet (.cue, .gdi) names its track files and a playlist (.m3u) its
// discs: the sheet is the game, what it names is part of it. A .bin beside a
// .cue of the same name is a track even when the sheet can't be read.
val parts = files.filter { it.isFile && it.extension.lowercase() in sheetExts }.flatMap(::sheetParts).toSet()
files.forEach { f ->
val ext = f.extension.lowercase()
@@ -273,7 +242,6 @@ object Library {
}
}
if (spec.id == "rpcs3") games.addAll(rpcs3Installed(context))
// A cover found for the game before (CoverArt) where it has no art of its own.
val withArt = games.map { g -> if (g.art != null) g else CoverArt.cached(context, g)?.let { Rom(g.name, g.hostPath, g.guestPath, g.emulatorId, it) } ?: g }
Emulator(spec.id, spec.name, spec.system, spec.program, installedPackage(installedIds.getValue(spec.id)), withArt)
}
@@ -282,27 +250,19 @@ object Library {
private val firmwareFolders = setOf("bios", "firmware")
private val sheetExts = setOf("cue", "gdi", "m3u")
/** The file names (lower case) a .cue, .gdi or .m3u refers to, from its own folder. */
private fun sheetParts(sheet: File): List<String> = runCatching {
if (sheet.length() > 64 * 1024) return emptyList()
sheet.readLines().mapNotNull { line ->
val t = line.trim()
when (sheet.extension.lowercase()) {
// FILE "Tekken 3 (USA) (Track 1).bin" BINARY
"cue" -> Regex("^FILE\\s+\"([^\"]+)\"", RegexOption.IGNORE_CASE).find(t)?.groupValues?.get(1)
?: Regex("^FILE\\s+(\\S+)", RegexOption.IGNORE_CASE).find(t)?.groupValues?.get(1)
// 1 0 4 2352 "track01.bin" 0 / 1 0 4 2352 track01.bin 0
"gdi" -> Regex("\"([^\"]+)\"").find(t)?.groupValues?.get(1) ?: t.split(Regex("\\s+")).getOrNull(4)
else -> t.takeIf { it.isNotEmpty() && !it.startsWith("#") }
}
}.map { File(it.replace('\\', '/')).name.lowercase() }
}.getOrDefault(emptyList())
/**
* What RPCS3 has installed on its own HDD - packages (PSN games) land in dev_hdd0/game/<ID>
* with a PARAM.SFO for the title and an EBOOT to boot - as RPCS3's own file list would show
* them. Not in the ROMs folder, so listed from RPCS3's home in the runtime.
*/
private fun rpcs3Installed(context: Context): List<Rom> {
val hdd = File(LinuxRuntime.rootDir(context), "root/.config/rpcs3/dev_hdd0/game")
return hdd.listFiles { f -> f.isDirectory }?.sortedBy { it.name }?.mapNotNull { dir ->
@@ -310,7 +270,6 @@ object Library {
val sfo = File(dir, "PARAM.SFO")
if (!eboot.isFile || !sfo.isFile) return@mapNotNull null
val fields = readSfo(sfo)
// Only games: patches, DLC and save data live here too, with their own categories.
if (fields["CATEGORY"]?.let { it == "HG" || it == "DG" || it == "GD" } == false) return@mapNotNull null
val title = fields["TITLE"]?.trim()?.takeIf { it.isNotEmpty() } ?: dir.name
Rom(title, eboot, "/root/.config/rpcs3/dev_hdd0/game/${dir.name}/USRDIR/EBOOT.BIN", "rpcs3",
@@ -318,7 +277,6 @@ object Library {
} ?: emptyList()
}
/** The string fields of a PARAM.SFO (the PSP/PS3 metadata file): a small binary table. */
private fun readSfo(file: File): Map<String, String> {
val out = HashMap<String, String>()
try {
@@ -335,7 +293,7 @@ object Library {
var keyEnd = keyStart
while (keyEnd < b.size && b[keyEnd] != 0.toByte()) keyEnd++
val key = String(b, keyStart, keyEnd - keyStart, Charsets.US_ASCII)
if (fmt == 0x0204 || fmt == 0x0004) { // utf8 string (null-terminated or not)
if (fmt == 0x0204 || fmt == 0x0004) {
val start = data + dataOff
val end = minOf(b.size, start + len)
var stop = start
@@ -344,38 +302,21 @@ object Library {
}
}
} catch (e: Exception) {
// unreadable metadata: the folder name stands in
}
return out
}
/** How the emulator is told which game to boot, on its command line. */
fun launchArgs(emulatorId: String, guestPath: String): List<String> = when (emulatorId) {
"rpcs3" -> listOf("--no-gui", guestPath)
// Straight into the game in its controller-driven full-screen UI (first-time setup there
// too), and gone when the game is quit from its pause menu (guide button): -batch.
"armsx2" -> listOf("-batch", "-bigpicture", "-fullscreen", "--", guestPath)
// DuckStation shares PCSX2's flags (src/duckstation-qt/qthost.cpp): the same launch and exit.
"duckstation" -> listOf("-batch", "-bigpicture", "-fullscreen", "--", guestPath)
// Dolphin: full screen, drawn inside its own main window, without its "stop the emulation?"
// question; no warning boxes either, which wait for a click a controller cannot give
// (they still go to Dolphin's log). The guide button is Dolphin's Toggle Fullscreen hotkey
// (droiddeck-pad-defaults), which shows Dolphin's window and its settings - so no -b, which
// hides that window; closing Dolphin ends the session. Under gamescope Dolphin does not
// always see its window as focused, and by default both its hotkeys and the game's
// controller then stop: HotkeysRequireFocus off, BackgroundInput on. -C sets a Dolphin.ini
// value for this run only, so Dolphin started from the desktop keeps its own settings.
"dolphin" -> listOf(
"-C", "Dolphin.Display.Fullscreen=True", "-C", "Dolphin.Display.RenderToMain=True",
"-C", "Dolphin.Interface.ConfirmStop=False", "-C", "Dolphin.Interface.UsePanicHandlers=False",
"-C", "Dolphin.General.HotkeysRequireFocus=False", "-C", "Dolphin.Input.BackgroundInput=True",
"-e", guestPath,
)
// Full screen (LaunchSettings.cpp -f); the settings seed keeps the Getting Started
// dialog away (droiddeck-pad-defaults).
"cemu" -> listOf("-f", "-g", guestPath)
// Full screen (CLI.cpp --fullscreen); the guide button leaves it for melonDS's menus and
// comes back (HK_FullscreenToggle, droiddeck-pad-defaults).
"melonds" -> listOf("-f", guestPath)
else -> listOf(guestPath)
}
@@ -6,19 +6,18 @@ import org.json.JSONArray
import org.json.JSONObject
import java.io.File
/**
* The last game list the front end showed, kept in the app's files so the next start draws the
* Steam wall on its first frame. Building the list reads every manifest and walks the added-games
* folders, which can take a second or more; the fresh list replaces this one when it is ready.
*/
object LibraryCache {
private const val TAG = "LibraryCache"
private fun file(context: Context) = File(context.filesDir, "library-cache.json")
fun load(context: Context): List<Library.SteamGame> = runCatching {
/** The cached games and whether they are the x86-64 client's; a cache written as a bare list is arm64's. */
@Synchronized
fun snapshot(context: Context): Pair<List<Library.SteamGame>, Boolean> = runCatching {
val f = file(context)
if (!f.isFile) return emptyList()
val array = JSONArray(f.readText())
if (!f.isFile) return Pair(emptyList(), false)
val text = f.readText().trim()
val (array, x64) = if (text.startsWith("{")) JSONObject(text).let { it.getJSONArray("games") to (it.optString("client") == "x86_64") }
else JSONArray(text) to false
List(array.length()) { i ->
val o = array.getJSONObject(i)
Library.SteamGame(
@@ -29,11 +28,18 @@ object LibraryCache {
source = o.optString("source").ifEmpty { if (o.getString("library") == Library.ADDED) Library.ADDED else Library.SOURCE_STEAM },
storeId = o.optString("storeId").ifEmpty { null },
)
}
}.getOrElse { e -> Log.w(TAG, "unreadable, ignored: ${e.message}"); emptyList() }
} to x64
}.getOrElse { e -> Log.w(TAG, "unreadable, ignored: ${e.message}"); Pair(emptyList(), false) }
fun save(context: Context, games: List<Library.SteamGame>) {
fun load(context: Context): List<Library.SteamGame> = snapshot(context).first
fun listedX64(context: Context): Boolean = snapshot(context).second
/** Without [x64] the list keeps the client already recorded. */
@Synchronized
fun save(context: Context, games: List<Library.SteamGame>, x64: Boolean? = null) {
runCatching {
val client = x64 ?: listedX64(context)
val array = JSONArray()
for (g in games) array.put(
JSONObject().put("appId", g.appId).put("name", g.name).put("library", g.library).put("gameId", g.gameId)
@@ -43,8 +49,12 @@ object LibraryCache {
)
val f = file(context)
val tmp = File(f.path + ".tmp")
tmp.writeText(array.toString())
tmp.renameTo(f)
tmp.writeText(JSONObject().put("client", if (client) "x86_64" else "arm64").put("games", array).toString())
if (!tmp.renameTo(f)) {
tmp.delete()
f.delete()
Log.w(TAG, "not saved: rename failed; cache dropped so the next start scans")
}
}.onFailure { e -> Log.w(TAG, "not saved: ${e.message}") }
}
@@ -0,0 +1,48 @@
package com.droiddeck.launcher.frontend
import org.json.JSONObject
/** Where Steam pins a game's logo on its hero image and how big it draws it; the same JSON Steam keeps in `<appid>.json`. */
object LogoPosition {
enum class Pin(val steam: String) {
UPPER_LEFT("UpperLeft"), UPPER_CENTER("UpperCenter"), CENTER("CenterCenter"), BOTTOM_LEFT("BottomLeft"), BOTTOM_CENTER("BottomCenter");
companion object {
fun of(steam: String): Pin? = values().firstOrNull { it.steam == steam }
}
}
data class Value(val pin: Pin, val widthPct: Int, val heightPct: Int)
const val MIN_PCT = 10
const val MAX_PCT = 100
/** What Steam draws when a game has no custom position. */
val DEFAULT = Value(Pin.BOTTOM_LEFT, 50, 50)
private const val VERSION = 1
private const val KEY = "logoPosition"
fun sized(pin: Pin, size: Int): Value = size.coerceIn(MIN_PCT, MAX_PCT).let { Value(pin, it, it) }
/** `{"nVersion":1,"logoPosition":{...}}`, with whatever else [into] already held kept as it was. */
fun toJson(value: Value, into: JSONObject = JSONObject()): JSONObject = into
.put("nVersion", VERSION)
.put(KEY, JSONObject().put("pinnedPosition", value.pin.steam).put("nWidthPct", value.widthPct).put("nHeightPct", value.heightPct))
fun parse(text: String): Value? = runCatching { parse(JSONObject(text)) }.getOrNull()
fun parse(json: JSONObject): Value? {
val inner = json.optJSONObject(KEY) ?: return null
val pin = Pin.of(inner.optString("pinnedPosition")) ?: return null
if (!inner.has("nWidthPct") || !inner.has("nHeightPct")) return null
return Value(pin, inner.optInt("nWidthPct").coerceIn(MIN_PCT, MAX_PCT), inner.optInt("nHeightPct").coerceIn(MIN_PCT, MAX_PCT))
}
/** [existing] without the logo position; null when nothing but the version number would be left. */
fun without(existing: JSONObject): JSONObject? {
existing.remove(KEY)
val rest = existing.keys().asSequence().filter { it != "nVersion" }.toList()
return if (rest.isEmpty()) null else existing
}
}
@@ -3,27 +3,11 @@ package com.droiddeck.launcher.frontend
import java.io.File
import java.io.RandomAccessFile
/**
* Reads the StringFileInfo strings (ProductName, FileDescription, …) out of the
* VS_VERSION_INFO resource embedded in a Windows PE executable (.exe).
*
* Walks the PE resource directory to the first RT_VERSION resource — the same
* navigation [com.droiddeck.launcher.files.PeIconExtractor] uses for icons — then parses the alignment-heavy
* VS_VERSIONINFO → StringFileInfo → StringTable → String tree by hand.
*
* Everything is best-effort: any malformed / oversized / unsupported input returns
* null (or an empty map). No allocation beyond the single resource blob.
*/
object PeVersionInfo {
private const val RT_VERSION = 16
private const val MAX_RES_SECTION = 64 * 1024 * 1024 // don't slurp absurd resource sections
private const val MAX_RES_SECTION = 64 * 1024 * 1024
/**
* Returns the StringFileInfo key→value map (e.g. `"ProductName" -> "Palworld"`),
* or null if the file has no readable version resource. Keys are the raw
* VS_VERSIONINFO names; look up [productName]/[fileDescription] for the common ones.
*/
fun read(file: File): Map<String, String>? = try {
RandomAccessFile(file, "r").use { raf ->
val blob = versionResourceBytes(raf) ?: return@use null
@@ -33,13 +17,6 @@ object PeVersionInfo {
null
}
/**
* Best display name from a version map. ProductName is usually the cleanest title, but is
* sometimes an internal abbreviation (e.g. God of War ships ProductName "GoW" while
* FileDescription is "God of War"). When ProductName is a single spaceless token and
* FileDescription is a normal multi-word title, prefer FileDescription. Trimmed; null if
* neither is meaningful.
*/
fun bestName(map: Map<String, String>?): String? {
if (map == null) return null
val product = map["ProductName"]?.trim()?.takeIf { it.isNotEmpty() }
@@ -52,15 +29,14 @@ object PeVersionInfo {
return product ?: desc
}
// ── locate the RT_VERSION resource blob (mirrors PeIconExtractor's navigation) ──
private fun versionResourceBytes(raf: RandomAccessFile): ByteArray? {
val dos = read(raf, 0, 0x40) ?: return null
if (u8(dos, 0) != 0x4D || u8(dos, 1) != 0x5A) return null // "MZ"
if (u8(dos, 0) != 0x4D || u8(dos, 1) != 0x5A) return null
val peOff = u32(dos, 0x3C).toInt()
val coff = read(raf, peOff.toLong(), 24) ?: return null
if (u8(coff, 0) != 0x50 || u8(coff, 1) != 0x45 || u8(coff, 2) != 0 || u8(coff, 3) != 0) return null // "PE\0\0"
if (u8(coff, 0) != 0x50 || u8(coff, 1) != 0x45 || u8(coff, 2) != 0 || u8(coff, 3) != 0) return null
val numSections = u16(coff, 6)
val sizeOpt = u16(coff, 20)
if (numSections <= 0 || numSections > 96 || sizeOpt <= 0) return null
@@ -68,8 +44,8 @@ object PeVersionInfo {
val optOff = peOff + 24L
val opt = read(raf, optOff, sizeOpt) ?: return null
val magic = u16(opt, 0)
val ddOff = if (magic == 0x20B) 112 else 96 // PE32+ vs PE32 data-directory offset
val resDirEntry = ddOff + 2 * 8 // data directory index 2 = resource table
val ddOff = if (magic == 0x20B) 112 else 96
val resDirEntry = ddOff + 2 * 8
if (resDirEntry + 8 > opt.size) return null
val resRVA = u32(opt, resDirEntry).toInt()
val resSize = u32(opt, resDirEntry + 4).toInt()
@@ -105,7 +81,6 @@ object PeVersionInfo {
return dataEntryBytes(res, verData, secVA)
}
/** Resolve the sub-directory under [dirIdx] whose id matches [wantId]; null if absent or not a directory. */
private fun findIdSubdir(res: ByteArray, dirIdx: Int, wantId: Int, resDirStart: Int): Int? {
if (dirIdx + 16 > res.size) return null
val total = u16(res, dirIdx + 12) + u16(res, dirIdx + 14)
@@ -123,7 +98,6 @@ object PeVersionInfo {
return null
}
/** Descend the first entry of each level until an IMAGE_RESOURCE_DATA_ENTRY; returns its index. */
private fun firstDataEntry(res: ByteArray, dirIdx: Int, resDirStart: Int): Int? {
if (dirIdx + 16 > res.size) return null
if (u16(res, dirIdx + 12) + u16(res, dirIdx + 14) == 0) return null
@@ -137,7 +111,6 @@ object PeVersionInfo {
}
}
/** Read the bytes an IMAGE_RESOURCE_DATA_ENTRY points at (its OffsetToData is an RVA). */
private fun dataEntryBytes(res: ByteArray, entryIdx: Int, secVA: Int): ByteArray? {
if (entryIdx < 0 || entryIdx + 8 > res.size) return null
val rva = u32(res, entryIdx).toInt()
@@ -147,17 +120,11 @@ object PeVersionInfo {
return res.copyOfRange(start, start + size)
}
// ── VS_VERSIONINFO tree parse ──
//
// Every node is: WORD wLength, WORD wValueLength, WORD wType, WCHAR szKey[],
// then (32-bit aligned) an optional Value, then (32-bit aligned) child nodes.
// We only care about: VS_VERSIONINFO → StringFileInfo → StringTable → String.
private fun parseStringFileInfo(b: ByteArray): Map<String, String> {
val out = LinkedHashMap<String, String>()
if (b.size < 6) return out
val rootLen = minOf(u16(b, 0), b.size)
// VS_VERSION_INFO header: skip szKey "VS_VERSION_INFO" then the FIXEDFILEINFO value.
val rootValLen = u16(b, 2)
var pos = align4(6 + (utf16zLen(b, 6) + 1) * 2)
pos = align4(pos + rootValLen)
@@ -173,7 +140,6 @@ object PeVersionInfo {
return out
}
/** Iterate the StringTable children inside a StringFileInfo block [start, end). */
private fun parseTables(b: ByteArray, start: Int, end: Int, out: MutableMap<String, String>) {
var p = align4(start + 6 + (utf16zLen(b, start + 6) + 1) * 2)
while (p + 6 <= end) {
@@ -185,7 +151,6 @@ object PeVersionInfo {
}
}
/** Iterate the String children inside a StringTable block [start, end). */
private fun parseStrings(b: ByteArray, start: Int, end: Int, out: MutableMap<String, String>) {
var q = align4(start + 6 + (utf16zLen(b, start + 6) + 1) * 2)
while (q + 6 <= end) {
@@ -202,11 +167,9 @@ object PeVersionInfo {
}
}
// ── little-endian / UTF-16 readers ──
private fun align4(x: Int): Int = (x + 3) and 3.inv()
/** Number of UTF-16 code units before the null terminator starting at [off]. */
private fun utf16zLen(b: ByteArray, off: Int): Int {
var i = off
while (i + 1 < b.size) {
@@ -216,7 +179,6 @@ object PeVersionInfo {
return (i - off) / 2
}
/** Read a null-terminated UTF-16LE string starting at [off], not reading past [limit]. */
private fun utf16z(b: ByteArray, off: Int, limit: Int = b.size): String {
val sb = StringBuilder()
var i = off
@@ -3,18 +3,9 @@ package com.droiddeck.launcher.frontend
import org.json.JSONObject
import java.io.File
/**
* Which catalog components a game's prefix already has from elsewhere - a real installer, the game,
* the user - so the list can say so instead of recommending them again. Bannerlator's detector,
* on DroidDeck's compatdata/<id>: only markers absent from a fresh Proton prefix count (OpenAL32,
* Mono, PhysX, Games for Windows Live); msvcp140, d3dx9 and the like ship with Proton and cannot
* tell. Files DroidDeck put there itself (droiddeck-wincomponents' state) are not counted.
* Pure filesystem reads; never throws.
*/
object PrefixInstalledDetector {
private const val STATE = ".droiddeck-wincomponents.json"
/** Detector names (oalinst, mono, physx, XLiveRedist) present in [compat]/pfx. */
fun detect(compat: File?): Set<String> = try {
val pfx = compat?.let { File(it, "pfx") }
if (pfx == null || !pfx.isDirectory) emptySet() else {
@@ -38,7 +29,6 @@ object PrefixInstalledDetector {
emptySet()
}
/** Windows names ignore case; the prefix's folder may spell them either way. */
private fun fileIgnoringCase(pfx: File, rel: String): Boolean {
val target = File(pfx, rel)
return target.parentFile?.listFiles()?.any { it.isFile && it.name.equals(target.name, ignoreCase = true) } == true
@@ -7,17 +7,10 @@ import com.droiddeck.launcher.session.GameStorage
import com.droiddeck.launcher.session.SessionState
import java.io.File
/**
* A Steam game whose download finishes during a session, noticed by its appmanifest (a new one,
* or StateFlags turning to 4, fully installed) with a light look every five seconds while the
* session runs, and handed to [AutoComponents] as a single job that may run in the session, so its
* Windows components are in place when the user plays it right after the download.
*/
object SessionInstalls {
private const val TAG = "SessionInstalls"
private const val POLL_MS = 5_000L
/** One appmanifest as read: the app, its name and folder, whether it is fully installed. */
class Manifest(val appId: Int, val name: String, val installDir: String, val installed: Boolean, val steamapps: File)
private val APPID = Regex("\"appid\"\\s*\"(\\d+)\"", RegexOption.IGNORE_CASE)
@@ -32,16 +25,18 @@ object SessionInstalls {
STATE.find(text)?.groupValues?.get(1)?.toIntOrNull() == 4, file.parentFile!!)
}.getOrNull()
/** The libraries the session's client installs to, as host folders. */
internal fun steamappsDirs(context: Context): List<File> = listOfNotNull(
File(LinuxRuntime.rootDir(context), "root/.local/share/Steam/steamapps"),
GameStorage.effective(context)?.let { File(it.path, "steamapps") },
internal fun steamappsDirs(context: Context): List<File> = (
if (com.droiddeck.launcher.session.SessionPrefs.activeSteamClientArch(context) == "x86_64")
listOfNotNull(
File(com.droiddeck.launcher.session.SessionPrefs.activeSteamRoot(context), "steamapps"),
GameStorage.x64CardLibrary(context)?.let { File(it, "steamapps") },
)
else listOfNotNull(
File(LinuxRuntime.rootDir(context), "root/.local/share/Steam/steamapps"),
GameStorage.effective(context)?.let { File(it.path, "steamapps") },
)
).filter { it.isDirectory }
/**
* Keeps what the libraries hold between looks: a manifest re-read only when its time changed.
* [poll] returns the apps that became fully installed since the last look (none on the first).
*/
internal class Watch(private val dirs: () -> List<File>) {
private val seen = HashMap<String, Pair<Long, Manifest?>>()
private var first = true
@@ -60,22 +55,16 @@ object SessionInstalls {
}
}
/** A finished install as the Games tab would list it. */
internal fun gameOf(m: Manifest): Library.SteamGame =
Library.SteamGame(m.appId, m.name.ifEmpty { m.appId.toString() }, null, m.steamapps.parentFile?.path ?: "steam",
gameFiles = File(m.steamapps, "common/${m.installDir}"), source = Library.SOURCE_STEAM)
/**
* Looks every five seconds while the session runs, on a thread of its own at background
* priority; each finished install becomes one job for its Windows components.
*/
fun watch(context: Context) {
val app = context.applicationContext
Thread({
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND)
val watch = Watch { steamappsDirs(app) }
try {
// The session is still starting when this is called: up to two minutes for it.
var waited = 0L
while (!SessionState.running && waited < 120_000L) { Thread.sleep(POLL_MS); waited += POLL_MS }
watch.poll()
@@ -3,7 +3,6 @@ package com.droiddeck.launcher.frontend
import android.content.Context
import android.util.AtomicFile
import android.util.Log
import com.droiddeck.launcher.runtime.LinuxRuntime
import org.json.JSONArray
import org.json.JSONObject
import java.io.DataInputStream
@@ -12,25 +11,16 @@ import java.io.RandomAccessFile
import java.nio.ByteBuffer
import java.nio.ByteOrder
/**
* Steam's own data for an app, from the client in the runtime: its app-info cache
* (Steam/appcache/appinfo.vdf, the PICS data the client keeps for every app it has seen). What is
* read is which Steamworks Shared depots (the VC++, DirectX... redistributables of app 228980) the
* app's depots point at, and its name. Kept per appid in the app's files, so it works offline; an
* app the client has not seen is looked for again after a week.
*/
object SteamAppInfo {
private const val TAG = "SteamAppInfo"
private const val CACHE = "steam-appinfo.json"
private const val RETRY_AFTER_MS = 7L * 24 * 3600 * 1000
/** The Steamworks Shared app the redistributable depots belong to. */
const val SHARED_APP = "228980"
class Info(val name: String, val sharedDepots: List<String>)
fun appinfoFile(context: Context) = File(LinuxRuntime.rootDir(context), "root/.local/share/Steam/appcache/appinfo.vdf")
fun appinfoFile(context: Context) = File(com.droiddeck.launcher.session.SessionPrefs.activeSteamRoot(context), "appcache/appinfo.vdf")
/** The shared depots for [appId]: from the cache, else the client's app-info file; null when unknown. */
@Synchronized
fun info(context: Context, appId: Int, appinfo: File = appinfoFile(context)): Info? {
val cacheFile = AtomicFile(File(context.filesDir, CACHE))
@@ -40,13 +30,10 @@ object SteamAppInfo {
val known = cache.optJSONObject(appId.toString())
known?.let { o ->
if (!o.optBoolean("miss")) return Info(o.optString("name"), o.optJSONArray("depots").strings())
// A miss is read again when the client has written its cache since (the user may have
// looked at the app in Steam), else a week later.
if (now - o.optLong("at") < RETRY_AFTER_MS && o.optLong("mtime") == mtime) return null
}
val read = runCatching { read(appinfo, appId) }.onFailure { Log.w(TAG, "appinfo: ${it.javaClass.simpleName}") }.getOrNull()
val entry = JSONObject().put("at", now).put("mtime", mtime)
// When Steam was last asked anonymously ([lookup]), kept across re-reads of the file.
known?.optLong("picsAt")?.takeIf { it > 0 }?.let { entry.put("picsAt", it) }
if (read == null) entry.put("miss", true) else entry.put("name", read.name).put("depots", JSONArray(read.sharedDepots))
cache.put(appId.toString(), entry)
@@ -55,14 +42,9 @@ object SteamAppInfo {
return read
}
/** Steam asked anonymously ([SteamPics]); a test stands in for it. */
@androidx.annotation.VisibleForTesting
internal var pics: (List<Int>) -> Map<Int, Info?>? = { SteamPics.lookup(it) }
/**
* Of [appIds], those neither the client's cache (appinfo.vdf, read first: free) nor an earlier
* answer has, and that Steam was not asked about anonymously in the past week.
*/
fun needLookup(context: Context, appIds: List<Int>): List<Int> {
val wanted = appIds.distinct().filter { it > 0 && info(context, it) == null }
if (wanted.isEmpty()) return emptyList()
@@ -71,11 +53,6 @@ object SteamAppInfo {
return wanted.filter { now - (cache.optJSONObject(it.toString())?.optLong("picsAt") ?: 0L) >= RETRY_AFTER_MS }
}
/**
* [appIds] the client never saw, asked of Steam anonymously in one connection ([SteamPics], in
* batches) and every answer kept like the client's: an app's depots and name, or a miss tried
* again in a week. False when nothing needed asking or Steam could not be reached. Blocking.
*/
@Synchronized
fun lookup(context: Context, appIds: List<Int>): Boolean {
val need = needLookup(context, appIds)
@@ -96,7 +73,6 @@ object SteamAppInfo {
return true
}
/** What is kept for [appId]: "hit", "miss" or "none" (part of a game's list fingerprint). */
fun state(context: Context, appId: Int): String = cache(context).optJSONObject(appId.toString())?.let { if (it.optBoolean("miss")) "miss" else "hit" } ?: "none"
private fun cache(context: Context): JSONObject =
@@ -104,10 +80,6 @@ object SteamAppInfo {
private fun JSONArray?.strings(): List<String> = if (this == null) emptyList() else (0 until length()).map { optString(it) }
/**
* [appId]'s entry in an appinfo.vdf (versions 27, 28 and 29, the last with its keys in a string
* table), or null when the file does not have it.
*/
internal fun read(file: File, appId: Int): Info? {
if (!file.isFile) return null
RandomAccessFile(file, "r").use { raf ->
@@ -181,7 +153,6 @@ object SteamAppInfo {
return String(bytes, Charsets.UTF_8)
}
/** One binary key-value map, to its end marker; keys by name, or by index into [strings]. */
private fun readMap(b: ByteBuffer, strings: List<String>?): Map<String, Any> {
val out = LinkedHashMap<String, Any>()
while (b.hasRemaining()) {
@@ -0,0 +1,91 @@
package com.droiddeck.launcher.frontend
import android.content.Context
import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import com.droiddeck.launcher.stores.StoreNet
import org.json.JSONArray
import org.json.JSONObject
import java.util.Locale
import java.util.concurrent.atomic.AtomicBoolean
object SteamFeatured {
class Game(val appId: Int, val name: String, val art: String)
private const val TAG = "SteamFeatured"
private const val PREFS = "steam_featured"
private const val KEY_GAMES = "games_v2"
private const val KEY_AT = "at"
private const val FRESH_MS = 6 * 60 * 60 * 1000L
private val SECTIONS = listOf("top_sellers", "new_releases", "specials")
private const val ASSET_ROOT = "https://shared.akamai.steamstatic.com/store_item_assets/"
private val HIDDEN_DESCRIPTORS = setOf(1, 3, 4)
var games by mutableStateOf<List<Game>>(emptyList())
private set
private val loading = AtomicBoolean(false)
fun load(context: Context) {
val prefs = context.applicationContext.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
if (games.isEmpty()) parse(prefs.getString(KEY_GAMES, null))?.let { games = it }
if (System.currentTimeMillis() - prefs.getLong(KEY_AT, 0L) in 0 until FRESH_MS && games.isNotEmpty()) return
if (!loading.compareAndSet(false, true)) return
Thread({
try {
val cc = Locale.getDefault().country.takeIf { it.length == 2 } ?: "US"
val body = StoreNet.get("https://store.steampowered.com/api/featuredcategories?cc=$cc&l=english") ?: return@Thread
val root = JSONObject(body)
val ids = LinkedHashSet<Int>()
for (section in SECTIONS) {
val items = root.optJSONObject(section)?.optJSONArray("items") ?: continue
for (i in 0 until items.length()) {
val o = items.optJSONObject(i) ?: continue
val id = o.optInt("id")
if (id > 0 && o.optInt("type", 0) == 0) ids += id
}
}
val list = withArt(ids, cc)
if (list.isEmpty()) return@Thread
prefs.edit().putString(KEY_GAMES, JSONArray(list.map { JSONObject().put("id", it.appId).put("name", it.name).put("art", it.art) }).toString())
.putLong(KEY_AT, System.currentTimeMillis()).apply()
android.os.Handler(android.os.Looper.getMainLooper()).post { games = list }
Log.i(TAG, "featured games: ${list.size}")
} catch (e: Exception) {
Log.w(TAG, "featured: ${e.javaClass.simpleName}")
} finally {
loading.set(false)
}
}, "steam-featured").start()
}
private fun withArt(ids: Collection<Int>, cc: String): List<Game> {
if (ids.isEmpty()) return emptyList()
val input = JSONObject()
.put("ids", JSONArray(ids.map { JSONObject().put("appid", it) }))
.put("context", JSONObject().put("language", "english").put("country_code", cc))
.put("data_request", JSONObject().put("include_assets", true))
val body = StoreNet.get(
"https://api.steampowered.com/IStoreBrowseService/GetItems/v1/?input_json=" + java.net.URLEncoder.encode(input.toString(), "UTF-8"),
) ?: return emptyList()
val items = JSONObject(body).optJSONObject("response")?.optJSONArray("store_items") ?: return emptyList()
return (0 until items.length()).mapNotNull { i ->
val o = items.optJSONObject(i) ?: return@mapNotNull null
val descriptors = o.optJSONArray("content_descriptorids")
if (descriptors != null && (0 until descriptors.length()).any { descriptors.optInt(it) in HIDDEN_DESCRIPTORS }) return@mapNotNull null
val assets = o.optJSONObject("assets") ?: return@mapNotNull null
val format = assets.optString("asset_url_format").takeIf { "\${FILENAME}" in it } ?: return@mapNotNull null
val file = listOf("library_capsule_2x", "library_capsule").firstNotNullOfOrNull { k -> assets.optString(k).takeIf { it.isNotEmpty() } }
?: return@mapNotNull null
Game(o.optInt("appid"), o.optString("name"), ASSET_ROOT + format.replace("\${FILENAME}", file))
}
}
private fun parse(text: String?): List<Game>? {
val a = runCatching { JSONArray(text ?: return null) }.getOrNull() ?: return null
return (0 until a.length()).mapNotNull { a.optJSONObject(it) }.map { Game(it.optInt("id"), it.optString("name"), it.optString("art")) }
.filter { it.appId > 0 && it.art.isNotEmpty() }.takeIf { it.isNotEmpty() }
}
}
@@ -0,0 +1,99 @@
package com.droiddeck.launcher.frontend
import android.content.Context
import com.droiddeck.launcher.stores.SteamLiveShortcuts
import com.droiddeck.launcher.session.SessionPrefs
import java.io.File
object SteamGameArt {
internal fun dir(context: Context, appId: Int) = File(context.filesDir, "steam-art/$appId")
private fun sourceFile(context: Context, appId: Int, slot: AddedGameArt.Slot) = File(dir(context, appId), "source-${slot.key}")
internal fun setSource(context: Context, appId: Int, slot: AddedGameArt.Slot, value: String) {
val file = sourceFile(context, appId, slot)
if (value.isEmpty()) file.delete() else file.apply { parentFile?.mkdirs() }.writeText(value)
}
fun chosenFile(context: Context, appId: Int, slot: AddedGameArt.Slot): File? = AddedGameArt.chosen(dir(context, appId), slot)
fun withOverrides(context: Context, game: Library.SteamGame): Library.SteamGame {
if (game.library == Library.ADDED || !dir(context, game.appId).isDirectory) return game
val cover = chosenFile(context, game.appId, AddedGameArt.Slot.COVER)
val hero = chosenFile(context, game.appId, AddedGameArt.Slot.BACKGROUND)
val icon = chosenFile(context, game.appId, AddedGameArt.Slot.ICON)
if (cover == null && hero == null && icon == null) return game
return Library.SteamGame(
game.appId, game.name, cover ?: game.art, game.library, game.gameId,
hero ?: game.hero, game.lastPlayed, game.gameFiles, game.protonPrefix, icon ?: game.icon, game.source, game.storeId,
)
}
fun chosenSource(context: Context, appId: Int, slot: AddedGameArt.Slot): Pair<String, String> {
val value = sourceFile(context, appId, slot).takeIf { it.isFile }?.readText()?.trim().orEmpty()
return value.substringBefore('|') to value.substringAfter('|', "")
}
fun describe(context: Context, game: Library.SteamGame): List<AddedGameArt.SlotState> {
val cache = File(SessionPrefs.activeSteamRoot(context), "appcache/librarycache")
val own = withOverrides(context, game)
return AddedGameArt.Slot.values().map { slot ->
chosenFile(context, game.appId, slot)?.let {
return@map AddedGameArt.SlotState(slot, it, chosenSource(context, game.appId, slot).first.ifEmpty { AddedGameArt.FILE }, true)
}
val auto = when (slot) {
AddedGameArt.Slot.COVER -> own.art
AddedGameArt.Slot.WIDE -> Library.steamCacheImage(cache, game.appId, listOf("library_header.jpg", "header.jpg"))
AddedGameArt.Slot.BACKGROUND -> own.hero
AddedGameArt.Slot.ICON -> own.icon
AddedGameArt.Slot.LOGO -> Library.steamCacheImage(cache, game.appId, listOf("logo.png"))
}
AddedGameArt.SlotState(slot, auto, if (auto != null) AddedGameArt.STEAM else AddedGameArt.AUTO, false)
}
}
fun choices(context: Context, game: Library.SteamGame, slot: AddedGameArt.Slot): AddedGameArt.Choices {
val out = ArrayList<AddedGameArt.Option>()
AddedGameArt.steamOption(game.appId.toString(), slot)?.let { out.add(it) }
val (found, status) = AddedGameArt.sgdbChoices(context, File(dir(context, game.appId), "sgdb-id"), game.appId, game.name, slot)
out.addAll(found)
return AddedGameArt.Choices(out, status)
}
fun choose(context: Context, game: Library.SteamGame, slot: AddedGameArt.Slot, option: AddedGameArt.Option): Boolean {
return synchronized(SteamGrid.lock) {
val folder = dir(context, game.appId)
val fresh = AddedGameArt.storeNew(folder, slot, option) ?: return@synchronized false
if (!push(context, game, slot, fresh)) {
fresh.delete()
return@synchronized false
}
AddedGameArt.dropOlder(folder, slot, fresh)
setSource(context, game.appId, slot, option.group + "|" + option.full)
true
}
}
fun reset(context: Context, game: Library.SteamGame, slot: AddedGameArt.Slot) {
synchronized(SteamGrid.lock) {
if (!push(context, game, slot, null)) return@synchronized
AddedGameArt.chosen(dir(context, game.appId), slot)?.delete()
setSource(context, game.appId, slot, "")
}
}
fun position(context: Context, game: Library.SteamGame): LogoPosition.Value? = SteamGrid.position(SteamGrid.target(game, context))
fun setPosition(context: Context, game: Library.SteamGame, value: LogoPosition.Value?): Boolean {
if (!SteamGrid.writePosition(context, SteamGrid.target(game, context), value)) return false
AddedGamesPublisher.later(context) { SteamLiveShortcuts.setLogoPosition(context, game.appId.toLong(), value) }
return true
}
private fun push(context: Context, game: Library.SteamGame, slot: AddedGameArt.Slot, file: File?): Boolean {
val type = slot.assetType ?: return true
if (!SteamGrid.write(context, SteamGrid.target(game, context), slot, file)) return false
AddedGamesPublisher.later(context) { SteamLiveShortcuts.setArt(context, game.appId.toLong(), type, file) }
return true
}
}
@@ -0,0 +1,209 @@
package com.droiddeck.launcher.frontend
import android.content.Context
import android.util.Log
import com.droiddeck.launcher.session.SessionPrefs
import org.json.JSONObject
import java.io.File
/** Steam keeps a game's custom art in `userdata/<account>/config/grid`; this is where DroidDeck's choices and Steam's meet. */
object SteamGrid {
private const val TAG = "SteamGrid"
/** Every change to a game's art, here or in a scan, takes this first so two of them never interleave. */
internal val lock = Any()
private const val MANIFEST = ".droiddeck-art.json"
private val SUFFIX = mapOf(AddedGameArt.Slot.COVER to "p", AddedGameArt.Slot.WIDE to "", AddedGameArt.Slot.BACKGROUND to "_hero", AddedGameArt.Slot.LOGO to "_logo")
private val EXTS = listOf("png", "jpg", "jpeg", "webp")
class Target(val artDir: File, val id: Long, val setSource: (AddedGameArt.Slot, String) -> Unit)
fun target(game: Library.SteamGame, context: Context) = Target(SteamGameArt.dir(context, game.appId), game.appId.toLong()) { slot, value ->
SteamGameArt.setSource(context, game.appId, slot, value)
}
fun target(game: AddedGames.Game, context: Context) = Target(AddedGameArt.cacheDir(context, game), game.appId and 0xFFFFFFFFL) { slot, value ->
SessionPrefs.setAddedGameArtSource(context, game.folder.path, slot.key, value)
}
internal fun dir(context: Context): File? {
return AddedGames.accountConfig(SessionPrefs.activeSteamRoot(context))?.let { File(it, "grid") }
}
private fun files(grid: File, id: Long, slot: AddedGameArt.Slot): List<File> =
EXTS.map { File(grid, "$id${SUFFIX[slot]}.$it") }.filter { it.isFile }
private fun signature(file: File?) = if (file == null) "" else "${file.length()}:${file.lastModified()}"
/** Each Steam client has its own grid folder, so what was last seen there is kept per client. */
private fun sigName(context: Context, key: String) =
if (SessionPrefs.activeSteamClientArch(context) == "x86_64") "grid-x86_64-$key" else "grid-$key"
private fun sigFile(context: Context, target: Target, slot: AddedGameArt.Slot) = File(target.artDir, sigName(context, slot.key))
private fun recorded(context: Context, target: Target, slot: AddedGameArt.Slot): String? =
sigFile(context, target, slot).takeIf { it.isFile }?.readText()
private fun record(context: Context, target: Target, slot: AddedGameArt.Slot, value: String) {
target.artDir.mkdirs()
sigFile(context, target, slot).writeText(value)
}
private fun writtenByDroidDeck(grid: File): Set<String> = runCatching {
val manifest = JSONObject(File(grid, MANIFEST).readText())
manifest.keys().asSequence().filter { name -> File(grid, name).let { it.isFile && it.length() == manifest.optLong(name, -1L) } }.toSet()
}.getOrDefault(emptySet())
/** Puts a choice (or, with null, the lack of one) where Steam reads it, whether or not Steam is running. */
fun write(context: Context, target: Target, slot: AddedGameArt.Slot, file: File?): Boolean = synchronized(lock) { place(context, target, slot, file) }
private fun place(context: Context, target: Target, slot: AddedGameArt.Slot, file: File?): Boolean {
val suffix = SUFFIX[slot] ?: return true
val grid = dir(context) ?: return true
if (file == null) {
if (files(grid, target.id, slot).map { it.delete() }.any { !it }) return false
record(context, target, slot, "")
return true
}
val ext = file.extension.lowercase().let { if (it == "jpeg") "jpg" else it }
if (ext !in EXTS) return false
grid.mkdirs()
val dst = File(grid, "${target.id}$suffix.$ext")
val tmp = File(grid, dst.name + ".tmp")
val placed = runCatching { file.copyTo(tmp, overwrite = true); if (tmp.renameTo(dst)) dst else null }.getOrNull()
if (placed == null) {
tmp.delete()
return false
}
if (files(grid, target.id, slot).filter { it != dst }.map { it.delete() }.any { !it }) return false
record(context, target, slot, signature(placed))
return true
}
private const val POSITION = "logo-position"
private const val POSITION_SIG = "json"
private fun positionFile(grid: File, id: Long) = File(grid, "$id.json")
private fun positionIn(file: File): LogoPosition.Value? = file.takeIf { it.isFile }?.let { runCatching { it.readText() }.getOrNull() }?.let(LogoPosition::parse)
/** The logo position chosen here (or taken over from Steam) for a game; null when Steam's own default applies. */
fun position(target: Target): LogoPosition.Value? = positionIn(File(target.artDir, POSITION))
private fun positionRecorded(context: Context, target: Target): String? =
File(target.artDir, sigName(context, POSITION_SIG)).takeIf { it.isFile }?.readText()
private fun recordPosition(context: Context, target: Target, value: String) {
target.artDir.mkdirs()
File(target.artDir, sigName(context, POSITION_SIG)).writeText(value)
}
private fun storePosition(target: Target, value: LogoPosition.Value?): Boolean {
val file = File(target.artDir, POSITION)
if (value == null) return !file.exists() || file.delete()
target.artDir.mkdirs()
return runCatching { file.writeText(LogoPosition.toJson(value).toString()) }.isSuccess
}
/** Sets (or with null, clears) a game's logo position here and in the client's grid folder, whether or not Steam is running. */
fun writePosition(context: Context, target: Target, value: LogoPosition.Value?): Boolean = synchronized(lock) {
placePosition(context, target, value) && storePosition(target, value)
}
/** Steam keeps the position in `<id>.json`; anything else that file holds is left as it was. */
private fun placePosition(context: Context, target: Target, value: LogoPosition.Value?): Boolean {
val grid = dir(context) ?: return true
val file = positionFile(grid, target.id)
val existing = if (file.isFile) runCatching { JSONObject(file.readText()) }.getOrNull() else null
if (file.isFile && existing == null) return value == null
val next = if (value == null) existing?.let(LogoPosition::without) else LogoPosition.toJson(value, existing ?: JSONObject())
if (next == null) {
if (file.isFile && !file.delete()) return false
recordPosition(context, target, "")
return true
}
grid.mkdirs()
val tmp = File(grid, file.name + ".tmp")
val placed = runCatching { tmp.writeText(next.toString()); tmp.renameTo(file) }.getOrDefault(false)
if (!placed) {
tmp.delete()
return false
}
recordPosition(context, target, signature(file))
return true
}
/** A `logoPosition` this version cannot read (a pin it does not know, say) is left alone rather than read as "removed". */
private fun hasUnknownPosition(file: File): Boolean {
val json = runCatching { JSONObject(file.readText()) }.getOrNull() ?: return false
return json.has("logoPosition") && LogoPosition.parse(json) == null
}
private fun reconcilePosition(context: Context, grid: File, target: Target) {
val file = positionFile(grid, target.id)
if (hasUnknownPosition(file)) return
val theirs = positionIn(file)
val now = if (theirs != null) signature(file) else ""
val before = positionRecorded(context, target)
val own = position(target)
when {
before == null && own != null && theirs == null -> placePosition(context, target, own)
before == null || now != before -> {
// Recorded only once the local side agrees, so a failed take-over is tried again on the next scan.
if (theirs != null) {
if (own == theirs || storePosition(target, theirs)) recordPosition(context, target, now)
} else {
if (own == null || storePosition(target, null)) recordPosition(context, target, "")
}
}
own != null && own != theirs -> placePosition(context, target, own)
}
}
/** Takes over what changed in Steam since the last look; returns true when a choice was added, replaced or dropped. */
fun sync(context: Context, targets: List<Target>): Boolean = synchronized(lock) { reconcile(context, targets) }
private fun reconcile(context: Context, targets: List<Target>): Boolean {
val grid = dir(context) ?: return false
val ours = writtenByDroidDeck(grid)
var changed = false
for (target in targets) for (slot in SUFFIX.keys) {
try {
val own = AddedGameArt.chosen(target.artDir, slot)
val present = files(grid, target.id, slot)
val theirs = present.filter { it.name !in ours }.maxByOrNull { it.lastModified() }
val now = signature(theirs)
val before = recorded(context, target, slot)
when {
before == null && own != null && theirs == null -> write(context, target, slot, own)
before == null || now != before -> {
if (theirs != null) {
if (own != null && own.length() == theirs.length() && own.readBytes().contentEquals(theirs.readBytes())) record(context, target, slot, now)
else if (AddedGameArt.store(target.artDir, slot, AddedGameArt.Option(AddedGameArt.STEAM, theirs.path, theirs.path))) {
target.setSource(slot, AddedGameArt.STEAM + "|steam-client")
record(context, target, slot, now)
changed = true
}
} else {
if (own != null && present.isEmpty()) { own.delete(); target.setSource(slot, ""); changed = true }
record(context, target, slot, "")
}
}
own != null && theirs != null && now == before && own.length() != theirs.length() -> write(context, target, slot, own)
own != null && present.isEmpty() -> write(context, target, slot, own)
}
} catch (e: Exception) {
Log.w(TAG, "${target.id} ${slot.key}: ${e.javaClass.simpleName}")
}
}
for (target in targets) {
try {
reconcilePosition(context, grid, target)
} catch (e: Exception) {
Log.w(TAG, "${target.id} logo position: ${e.javaClass.simpleName}")
}
}
if (changed) Log.i(TAG, "took over art changed in Steam")
return changed
}
}
@@ -11,37 +11,33 @@ import java.net.HttpURLConnection
import java.net.URL
import java.net.URLEncoder
/**
* SteamGridDB, the art source after Steam's store: a game by its Steam appid, else by a search
* whose result must carry the same name, then its best-rated grids, heroes, logos and icons.
*
* The API key: the user's own (Steam settings › Games), sealed with [CredentialCipher] in the
* app's files, else the one the build carries (`BuildConfig.SGDB_API_KEY`, from a CI secret), else
* none, and then this step is skipped. The key travels only in the Authorization header and is
* never logged.
*/
object SteamGridDb {
private const val TAG = "SteamGridDb"
private const val API = "https://www.steamgriddb.com/api/v2"
private const val QUERY = "nsfw=false&humor=false"
enum class Status { OK, NO_KEY, BAD_KEY, LIMIT, OFFLINE, SERVER, NO_GAME }
class Failed(val status: Status) : Exception(status.name)
class Game(val id: Int, val name: String, val verified: Boolean)
@VisibleForTesting
@Volatile
internal var keys: CredentialCipher.KeyProvider = CredentialCipher.Keystore
private fun keyFile(context: Context) = File(context.filesDir, "sgdb/key.json")
/** The key the build carries, or "" (a build without the secret, a fork's PR build). */
fun builtInKey(): String = runCatching {
Class.forName("com.droiddeck.launcher.BuildConfig").getField("SGDB_API_KEY").get(null) as? String
}.getOrNull().orEmpty()
/** The user's key wins, then the build's; "" when there is neither. */
fun pick(user: String, builtIn: String): String = user.trim().ifEmpty { builtIn.trim() }
fun key(context: Context): String = pick(userKey(context), builtInKey())
/** The key the user entered, or "". */
fun keyChangedAt(context: Context): Long = keyFile(context).lastModified()
@Synchronized
fun userKey(context: Context): String {
val f = keyFile(context)
@@ -56,7 +52,6 @@ object SteamGridDb {
}
}
/** Saves the user's key sealed (plain only where the Keystore does not work); "" clears it. */
@Synchronized
fun setUserKey(context: Context, key: String) {
val f = keyFile(context)
@@ -75,7 +70,6 @@ object SteamGridDb {
if (!tmp.renameTo(f)) { f.delete(); tmp.renameTo(f) }
}
/** The SteamGridDB game id for [steamAppId], else for a search on [name] that returns the same name; null when neither. */
fun gameId(key: String, steamAppId: Int?, name: String): Int? {
if (steamAppId != null) {
json(key, "$API/games/steam/$steamAppId")?.optJSONObject("data")?.optInt("id")?.takeIf { it > 0 }?.let { return it }
@@ -85,16 +79,11 @@ object SteamGridDb {
return match(name, (0 until results.length()).map { results.getJSONObject(it) }.map { it.optInt("id") to it.optString("name") })
}
/** The id of the first result whose name matches [name] ([AddedGameArt.nameMatch]); no blind first pick. */
internal fun match(name: String, results: List<Pair<Int, String>>): Int? =
results.map { (id, got) -> id to AddedGameArt.nameMatch(name, got) }
.filter { it.second > 0 && it.first > 0 }
.maxByOrNull { it.second }?.first
/**
* Fetches what is missing into [cache] under the names Steam's art uses there (p.jpg,
* header.jpg, hero.jpg, logo.png) and the icon as icon-sgdb.png. True when anything arrived.
*/
fun download(key: String, id: Int, cache: File): Boolean {
var got = false
for ((path, local) in listOf(
@@ -116,32 +105,74 @@ object SteamGridDb {
return got
}
/** The best-scored [limit] images for [slot] of game [id], as (thumbnail, full image) URLs. */
fun images(key: String, id: Int, slot: AddedGameArt.Slot, limit: Int): List<Pair<String, String>> {
val path = when (slot) {
fun search(key: String, term: String): List<Game> {
val q = term.trim()
if (q.isEmpty()) return emptyList()
val enc = URLEncoder.encode(q, "UTF-8").replace("+", "%20")
val data = json(key, "$API/search/autocomplete/$enc")?.optJSONArray("data") ?: return emptyList()
return games(data)
}
internal fun games(data: JSONArray): List<Game> =
(0 until data.length()).mapNotNull { data.optJSONObject(it) }
.filter { it.optInt("id") > 0 && it.optString("name").isNotEmpty() }
.map { Game(it.optInt("id"), it.optString("name"), it.optBoolean("verified")) }
internal fun path(id: Int, slot: AddedGameArt.Slot): String =
when (slot) {
AddedGameArt.Slot.COVER -> "grids/game/$id?dimensions=600x900&$QUERY"
AddedGameArt.Slot.WIDE -> "grids/game/$id?dimensions=460x215,920x430&$QUERY"
AddedGameArt.Slot.BACKGROUND -> "heroes/game/$id?$QUERY"
AddedGameArt.Slot.LOGO -> "logos/game/$id?$QUERY"
AddedGameArt.Slot.ICON -> "icons/game/$id?$QUERY"
}
val items = json(key, "$API/$path")?.optJSONArray("data") ?: return emptyList()
return ranked(items, pngOnly = slot == AddedGameArt.Slot.LOGO || slot == AddedGameArt.Slot.ICON).take(limit)
fun page(key: String, id: Int, slot: AddedGameArt.Slot): List<Pair<String, String>> {
val items = json(key, "$API/${path(id, slot)}")?.optJSONArray("data") ?: return emptyList()
return ranked(items, pngOnly = slot == AddedGameArt.Slot.LOGO || slot == AddedGameArt.Slot.ICON)
}
/** [items] best-scored first, as (thumbnail, full image) URLs; for a .png slot only PNGs. */
private const val PAGE_SIZE = 50
private const val MAX_PAGES = 4
fun all(key: String, id: Int, slot: AddedGameArt.Slot): List<Pair<String, String>> {
val raw = JSONArray()
for (n in 0 until MAX_PAGES) {
val url = "$API/${path(id, slot)}" + if (n > 0) "&page=$n" else ""
val data: JSONArray? = try {
json(key, url)?.optJSONArray("data")
} catch (e: Failed) {
if (n == 0) throw e
null
}
val items = data ?: break
for (i in 0 until items.length()) raw.put(items.get(i))
if (items.length() < PAGE_SIZE) break
}
return ranked(raw, pngOnly = slot == AddedGameArt.Slot.LOGO || slot == AddedGameArt.Slot.ICON)
}
fun images(key: String, id: Int, slot: AddedGameArt.Slot, limit: Int): List<Pair<String, String>> =
page(key, id, slot).take(limit)
internal fun ranked(items: JSONArray, pngOnly: Boolean): List<Pair<String, String>> =
(0 until items.length()).map { items.getJSONObject(it) }
.filter { it.optString("url").startsWith("https://") && (!pngOnly || it.optString("mime") == "image/png" || it.optString("url").endsWith(".png", true)) }
.sortedByDescending { it.optInt("score", 0) }
.map { (it.optString("thumb").takeIf { t -> t.startsWith("https://") } ?: it.optString("url")) to it.optString("url") }
/** The highest-scored image's URL; for a .png slot only a PNG. */
internal fun best(items: JSONArray, pngOnly: Boolean): String? =
(0 until items.length()).map { items.getJSONObject(it) }
.filter { it.optString("url").startsWith("https://") && (!pngOnly || it.optString("mime") == "image/png" || it.optString("url").endsWith(".png", true)) }
.maxByOrNull { it.optInt("score", 0) }?.optString("url")
/** A GET with the key in its Authorization header; the body as JSON, or null. Logs the path only. */
internal fun failure(code: Int): Status? = when {
code == 401 || code == 403 -> Status.BAD_KEY
code == 429 -> Status.LIMIT
code >= 500 -> Status.SERVER
else -> null
}
private fun json(key: String, url: String): JSONObject? {
val c = URL(url).openConnection() as HttpURLConnection
c.connectTimeout = 10_000
@@ -149,8 +180,19 @@ object SteamGridDb {
c.setRequestProperty("User-Agent", "DroidDeck-launcher")
c.setRequestProperty("Authorization", "Bearer $key")
return try {
if (c.responseCode != 200) { Log.w(TAG, "${URL(url).path}: HTTP ${c.responseCode}"); null }
else JSONObject(c.inputStream.use { it.readBytes() }.toString(Charsets.UTF_8)).takeIf { it.optBoolean("success", true) }
val code = c.responseCode
if (code == 200) {
JSONObject(c.inputStream.use { it.readBytes() }.toString(Charsets.UTF_8)).takeIf { it.optBoolean("success", true) }
} else {
Log.w(TAG, "${URL(url).path}: HTTP $code")
failure(code)?.let { throw Failed(it) }
null
}
} catch (e: Failed) {
throw e
} catch (e: java.io.IOException) {
Log.w(TAG, "${URL(url).path}: ${e.javaClass.simpleName}")
throw Failed(Status.OFFLINE)
} catch (e: Exception) {
Log.w(TAG, "${URL(url).path}: ${e.javaClass.simpleName}"); null
} finally {
@@ -5,20 +5,9 @@ import android.util.AtomicFile
import org.json.JSONObject
import java.io.File
/**
* Which Steam app a game added in DroidDeck is, kept once per game (by its folder) and used by
* everything that asks: its art ([AddedGameArt]) and its Windows components ([WinCompSources]),
* so the two never disagree.
*
* [Certainty.FILES]: the game's own files name the appid (steam_appid.txt, also under
* steam_settings/ or a parent folder; an emulator ini; Steam's appmanifest) - certain.
* [Certainty.NAME]: Steam's store search returned a game of the same name - a guess.
* [Certainty.NONE]: neither; a miss is tried again after a week.
*/
object SteamMatch {
enum class Certainty { FILES, NAME, NONE }
/** [source]: what gave the appid (GameIdentifier's source, or "store search"). */
class Match(val appId: Int?, val certainty: Certainty, val source: String, val checkedAt: Long)
private const val FILE = "steam-matches.json"
@@ -50,12 +39,6 @@ object SteamMatch {
try { out.write(json.toString().toByteArray()); atomic.finishWrite(out) } catch (e: Exception) { atomic.failWrite(out) }
}
/**
* The match for the game in [key] (its folder), [exe] its exe and [name] its title: its files
* first, always read again; else the one kept (a name match stays, a miss for a week); else
* [legacy], an appid an earlier build's art lookup kept; else [search] on the name, when given.
* Blocking ([search] asks the network).
*/
fun resolve(context: Context, key: String, exe: File, name: String, legacy: File? = null, search: ((String) -> Int?)? = null): Match {
val now = System.currentTimeMillis()
val identity = runCatching { GameIdentifier.identify(exe) }.getOrNull()
@@ -18,18 +18,9 @@ import java.util.zip.GZIPInputStream
import javax.net.ssl.SSLSocket
import javax.net.ssl.SSLSocketFactory
/**
* Steam's own app info (PICS) for apps the client in the runtime has never seen, asked of Steam
* anonymously: a connection to one of Steam's connection managers (the list from Steam's public
* ISteamDirectory/GetCMListForConnect, the WebSocket transport Steam's web clients use), an
* anonymous logon - no account, nothing of the user's Steam sign-in - a request for the apps'
* access tokens and then their product info, the app KeyValues read for their Steamworks Shared
* depots and name, and the connection closed. What steamcmd's anonymous `app_info_print` sees.
*/
object SteamPics {
private const val TAG = "SteamPics"
private const val CM_LIST = "https://api.steampowered.com/ISteamDirectory/GetCMListForConnect/v1/?cellid=0&format=json"
/** At most this many apps per request. */
const val BATCH = 25
private const val PROTO = 0x80000000.toInt()
private const val EMSG_MULTI = 1
@@ -41,13 +32,8 @@ object SteamPics {
private const val EMSG_INFO_REQUEST = 8903
private const val EMSG_INFO_RESPONSE = 8904
private const val PROTOCOL_VERSION = 65580
/** An anonymous user's SteamID: universe Public, account type AnonUser, instance 0. */
private const val ANON_STEAMID = (1L shl 56) or (10L shl 52)
/**
* [appIds]' info, one connection for all of them in batches of [BATCH]: each known app's
* info, null for an app Steam does not know. Null when Steam could not be reached. Blocking.
*/
fun lookup(appIds: List<Int>): Map<Int, SteamAppInfo.Info?>? {
if (appIds.isEmpty()) return emptyMap()
val host = runCatching { cmHost() }.getOrNull() ?: return null
@@ -56,7 +42,6 @@ object SteamPics {
ws.send(message(EMSG_LOGON, header(ANON_STEAMID, 0), Proto().varint(1, PROTOCOL_VERSION.toLong()).varint(7, 16).string(6, "english").bytes()))
var steamId = 0L
var session = 0
// The logon's answer (other messages before it are passed over).
loop@ while (true) for ((emsg, hdr, body) in messages(ws.receive() ?: return null)) if (emsg == EMSG_LOGON_RESPONSE) {
val h = fields(hdr); val b = fields(body)
if ((b[1]?.firstOrNull() as? Long ?: 0L) != 1L) { Log.w(TAG, "anonymous logon refused: ${b[1]}"); return null }
@@ -99,10 +84,8 @@ object SteamPics {
}
}
/** [appIds] in requests of at most [BATCH], each once. */
fun batches(appIds: List<Int>): List<List<Int>> = appIds.distinct().chunked(BATCH)
/** A connection manager for the WebSocket transport, least loaded first as Steam lists them. */
private fun cmHost(): String? {
val c = URL(CM_LIST).openConnection() as HttpURLConnection
c.connectTimeout = 10_000; c.readTimeout = 10_000
@@ -112,11 +95,9 @@ object SteamPics {
.firstOrNull { it.optString("type") == "websockets" }?.optString("endpoint")?.substringBefore(':')
}
// ---- the product info answer ----
class InfoResponse(val apps: Map<Int, SteamAppInfo.Info>, val unknown: List<Int>, val pending: Boolean)
/** A CMsgClientPICSProductInfoResponse: each app's KeyValues read, the unknown ones, whether more follows. */
@VisibleForTesting
internal fun parseInfoResponse(body: ByteArray): InfoResponse {
val f = fields(body)
@@ -132,17 +113,14 @@ object SteamPics {
return InfoResponse(apps, unknown, (f[6]?.firstOrNull() as? Long ?: 0L) != 0L)
}
/** An app's KeyValues text ("appinfo" { "common" {...} "depots" {...} }) to its name and shared depots. */
@VisibleForTesting
internal fun infoFromKeyValues(text: String): SteamAppInfo.Info? {
// The buffer ends in a NUL, kept or not by whoever handed it over.
val tree = runCatching { KeyValues(text.trimEnd('\u0000', ' ', '\n', '\r', '\t')).read() }.getOrNull() ?: return null
@Suppress("UNCHECKED_CAST")
val app = tree["appinfo"] as? Map<String, Any> ?: return null
return SteamAppInfo.infoOf(app)
}
/** Valve's text KeyValues: quoted keys and values, nested braces. */
private class KeyValues(private val s: String) {
private var i = 0
fun read(): Map<String, Any> = map(top = true)
@@ -182,7 +160,6 @@ object SteamPics {
}
}
// ---- messages ----
private fun header(steamId: Long, session: Int) = Proto().fixed64(1, steamId).varint(2, session.toLong()).bytes()
@@ -190,7 +167,6 @@ object SteamPics {
ByteBuffer.allocate(8 + header.size + body.size).order(ByteOrder.LITTLE_ENDIAN)
.putInt(emsg or PROTO).putInt(header.size).put(header).put(body).array()
/** One message off the wire as (emsg, header, body); a Multi unpacked (gunzipped when it says so) into the ones inside. */
@VisibleForTesting
internal fun messages(data: ByteArray): List<Triple<Int, ByteArray, ByteArray>> {
if (data.size < 8) return emptyList()
@@ -217,9 +193,7 @@ object SteamPics {
return out
}
// ---- protobuf, the little needed ----
/** Writes protobuf fields. */
@VisibleForTesting
internal class Proto {
private val out = ByteArrayOutputStream()
@@ -231,7 +205,6 @@ object SteamPics {
fun bytes(): ByteArray = out.toByteArray()
}
/** Reads protobuf fields: number to values (Long for varint and fixed, ByteArray for length-delimited). */
@VisibleForTesting
internal fun fields(b: ByteArray): Map<Int, List<Any>> {
val out = HashMap<Int, MutableList<Any>>()
@@ -258,16 +231,13 @@ object SteamPics {
return out
}
// ---- WebSocket over TLS, binary frames ----
private class Wss(host: String, timeoutMs: Int) : AutoCloseable {
// Made with the host name, so it goes out as SNI.
private val socket: SSLSocket = (SSLSocketFactory.getDefault().createSocket(host, 443) as SSLSocket).apply { soTimeout = timeoutMs }
private val out: OutputStream
private val input: DataInputStream
init {
// Host name checked against the certificate, as HTTPS does.
socket.sslParameters = socket.sslParameters.apply { endpointIdentificationAlgorithm = "HTTPS" }
socket.startHandshake()
out = socket.outputStream
@@ -305,7 +275,6 @@ object SteamPics {
out.write(frame.toByteArray()); out.flush()
}
/** The next binary message (fragments joined); null on close. */
fun receive(): ByteArray? {
val message = ByteArrayOutputStream()
while (true) {
@@ -2,14 +2,7 @@ package com.droiddeck.launcher.frontend
import java.io.File
/**
* What Steam itself installs alongside a game: the Steamworks Shared redistributables (app 228980)
* listed under "SharedDepots" in the game's appmanifest. Those live in Steamworks Shared rather than
* the game's folder, so the folder scan ([DependencyDetector]) cannot see them; this reads Steam's
* list instead. Depots are matched to Bannerlator's catalog names; unknown ones are left out.
*/
object SteamRedists {
/** Steamworks Shared depot -> (catalog component, what Steam calls it). 2015-2019 runtimes are one family. */
val DEPOTS = linkedMapOf(
"228981" to ("vcredist2005_dll" to "VC++ 2005"),
"228982" to ("vcredist2008_dll" to "VC++ 2008"),
@@ -25,7 +18,6 @@ object SteamRedists {
private val BLOCK = Regex("\"SharedDepots\"\\s*\\{([^}]*)\\}", RegexOption.IGNORE_CASE)
private val ENTRY = Regex("\"(\\d+)\"\\s+\"\\d+\"")
/** The game's appmanifest beside its install folder (steamapps/common/<dir> -> steamapps/). */
fun manifest(gameDir: File?, appId: Int): File? {
val steamapps = gameDir?.parentFile?.parentFile ?: return null
return File(steamapps, "appmanifest_$appId.acf").takeIf { it.isFile }
@@ -14,47 +14,28 @@ import com.droiddeck.launcher.stores.gog.GogDownloadManager
import org.json.JSONObject
import java.io.File
/**
* For a GOG or Epic game installed before its sidecar kept the store's list (GOG's build
* `dependencies`, Epic's manifest prerequisite): the list read once from the store's build
* manifest with the saved sign-in - the manifest alone, never a game file - and kept in the sidecar
* the way an install keeps it. Runs on the components' worker ([AutoComponents]). Without a
* sign-in or a network it is skipped quietly and tried again a week later.
*/
object StoreListBackfill {
private const val TAG = "StoreListBackfill"
private const val FILE = "store-list-backfill.json"
private const val RETRY_AFTER_MS = 7L * 24 * 3600 * 1000
/** The store's list for the game in a sidecar: GOG's dependency ids, or Epic's prerequisite {name, path}; null when it could not be had. */
fun interface Fetcher {
fun fetch(context: Context, sidecar: StoreGameSidecar): Map<String, String>?
}
/** The store itself, with the saved sign-in. */
internal val storeFetcher = Fetcher { context, sidecar -> fromStore(context, sidecar) }
@VisibleForTesting internal var fetcher: Fetcher = storeFetcher
/** Whether [sidecar] was written before the list was kept. */
/**
* Whether [sidecar] has no list read whole: written before the list was kept, or by a build
* that could have written an empty one for a manifest it did not read right.
*/
fun needs(sidecar: StoreGameSidecar): Boolean =
(sidecar.store == Store.GOG || sidecar.store == Store.EPIC) && WinCompSources.LIST_READ !in sidecar.extra
/**
* The list for the game in [folder], fetched and kept when its sidecar lacks one and it was not
* tried in the past week. True when the sidecar changed. Blocking (network).
*/
@Synchronized
fun fill(context: Context, folder: File): Boolean {
val sidecar = StoreGameSidecar.read(folder)?.takeIf { it.isInstalled && needs(it) } ?: return false
val tries = runCatching { JSONObject(AtomicFile(File(context.filesDir, FILE)).readFully().toString(Charsets.UTF_8)) }.getOrDefault(JSONObject())
val now = System.currentTimeMillis()
if (now - tries.optLong(folder.path, 0L) < RETRY_AFTER_MS) return false
// Offline is no try: it waits for a network rather than a week.
if (!AutoComponents.online(context)) return false
val list = runCatching { fetcher.fetch(context, sidecar) }.getOrNull()
if (list == null) {
@@ -65,7 +46,6 @@ object StoreListBackfill {
Log.i(TAG, "${sidecar.title}: the ${sidecar.store.label} list could not be had; again in a week")
return false
}
// Read fresh and written whole, as an install writes it: only the list is added.
val current = StoreGameSidecar.read(folder) ?: return false
current.copy(extra = current.extra + list + (WinCompSources.LIST_READ to "1")).write(folder)
Log.i(TAG, "backfill: ${sidecar.title} " + when (sidecar.store) {
@@ -7,35 +7,11 @@ import org.json.JSONObject
import java.io.File
import java.util.Locale
/**
* Which Windows components a game needs, from the list its source keeps - the source badge decides
* which list is trusted:
*
* - **Steam**: the Steamworks Shared redistributables Steam installs with the game (its
* appmanifest's SharedDepots, else the client's app info) - turned on by themselves.
* - **GOG**: the build manifest's `dependencies`, kept in the sidecar at install - on by themselves.
* - **Epic Games**: the manifest's prerequisite (usually UE4/UE5's prerequisite setup: VC++ and the
* DirectX June 2010 runtime), kept in the sidecar at install - on by themselves.
* - **Amazon Games**: fuel.json's PostInstall installers - on by themselves.
* - **Custom** whose files name a Steam appid ([SteamMatch], certain): Steam's list for that app -
* on by themselves. Matched by name only: the same list as Recommended. No match: the folder scan
* ([DependencyDetector]) as Recommended.
*
* Every store entry is mapped to catalog names here, in one place; one that maps to nothing is a
* suggestion under its own name. The folder scan is offered as Recommended for every game.
*/
object WinCompSources {
/** Whose list a finding came from; the reason line says it. */
enum class Origin { STEAM, GOG, EPIC, AMAZON, STEAM_NAME, FOLDER }
/**
* One thing a list names: [component] the catalog name it maps to (null: none, a suggestion),
* [original] what the list called it, [auto] whether it is turned on by itself, [args] what
* the reason line names (Steam: the label, the game, the appid).
*/
data class Finding(val component: String?, val original: String, val origin: Origin, val auto: Boolean, val args: List<String> = emptyList())
// ---- mapping tables: a store's names -> catalog components ----
private val VC_YEAR = mapOf(
"2005" to "vcredist2005", "2008" to "vcredist2008", "2010" to "vcredist2010", "2012" to "vcredist2012",
@@ -47,10 +23,6 @@ object WinCompSources {
"472" to "dotnet472", "48" to "dotnet48", "481" to "dotnet48",
)
/**
* A GOG dependency id ("MSVC2017_x64", "DirectX", "DOTNET48", "PhysX", "OpenAL", "XNA4") to its
* components; empty for one that is none of ours (ISI scripts, GOG's own helpers).
*/
fun gog(id: String): List<String> {
val key = id.uppercase(Locale.US).replace(Regex("[^A-Z0-9]"), "")
Regex("""^(?:MSVC|VCREDIST|VC)(\d{4})""").find(key)?.let { m -> return listOfNotNull(VC_YEAR[m.groupValues[1]]) }
@@ -67,40 +39,30 @@ object WinCompSources {
}
}
/** Unreal's prerequisite setup (UE4PrereqSetup_x64.exe, UEPrereqSetup_x64.exe): VC++ 2015-2022 and DirectX June 2010. */
private val UE_PREREQ = Regex("""(?i)ue\d?prereqsetup""")
/** An Epic prerequisite ([path] the installer it runs, [name] what it is called) to its components. */
fun epic(path: String, name: String): List<String> = when {
UE_PREREQ.containsMatchIn(path) || name.contains("UE4 Prerequisites", true) || name.contains("UE5 Prerequisites", true) ||
name.contains("Unreal Engine Prerequisites", true) -> listOf("vcredist2022", "d3dx9")
else -> DependencyDetector.componentsFor(path).ifEmpty { if (path.isEmpty()) gog(name) else emptyList() }
}
/** An installer a store runs after an install (Amazon's PostInstall "Command") to its components. */
fun installer(path: String): List<String> = if (UE_PREREQ.containsMatchIn(path)) listOf("vcredist2022", "d3dx9") else DependencyDetector.componentsFor(path)
/** Steamworks Shared depot ids to (component, Steam's label). */
fun steamDepots(ids: List<String>): List<Pair<String, String>> = ids.mapNotNull { SteamRedists.DEPOTS[it] }.distinctBy { it.first }
// ---- what each store left at install ----
/** Sidecar keys the store installs fill. */
const val GOG_DEPENDENCIES = "dependencies"
const val EPIC_PREREQ_PATH = "prereqPath"
const val EPIC_PREREQ_NAME = "prereqName"
/** Set beside the list when it was read whole (an empty list then means the store lists none). */
const val LIST_READ = "listRead"
/** fuel.json's PostInstall commands. */
fun amazonPostInstall(folder: File): List<String> = runCatching {
val arr = JSONObject(File(folder, "fuel.json").readText()).optJSONArray("PostInstall") ?: return emptyList()
(0 until arr.length()).mapNotNull { arr.optJSONObject(it)?.optString("Command")?.takeIf { c -> c.isNotEmpty() } }
}.getOrDefault(emptyList())
// ---- per game ----
/** Steam's list for [appId]: the local appmanifest's SharedDepots, else the client's app info. */
fun steamList(context: Context, gameDir: File?, appId: Int, gameName: String, auto: Boolean): List<Finding> {
val local = SteamRedists.detect(gameDir, appId)
val pairs = if (local.isNotEmpty()) local.map { it.componentName to it.label }
@@ -110,7 +72,6 @@ object WinCompSources {
}
}
/** Everything known for [game], blocking (files; the client's app info; no network). */
fun forGame(context: Context, game: Library.SteamGame): List<Finding> {
val folder = game.gameFiles
val out = ArrayList<Finding>()
@@ -141,8 +102,6 @@ object WinCompSources {
}
else -> {}
}
// A game added in DroidDeck (Custom, or a store's) that is a Steam app: Steam's list for it,
// on by itself when its files say so, else only recommended.
if (game.library == Library.ADDED && folder != null) SteamMatch.get(context, folder.path)?.let { match ->
val id = match.appId ?: return@let
when (match.certainty) {
@@ -151,7 +110,6 @@ object WinCompSources {
else -> {}
}
}
// The folder scan, for every game: what it bundles, as Recommended.
folder?.let { DependencyDetector.detect(it) }?.forEach { rec ->
out += Finding(rec.componentName, rec.reason, Origin.FOLDER, false, listOf(rec.reason, rec.kind.name))
}
@@ -0,0 +1,24 @@
package com.droiddeck.launcher.glasses
import android.content.Context
import com.droiddeck.launcher.R
import kotlin.math.abs
internal data class CalibrationDistanceResult(val accepted: Boolean, val message: String)
internal object CalibrationDistanceCheck {
fun evaluate(context: Context, expected: Float, corners: Int, points: List<SpatialVector>?): CalibrationDistanceResult {
require(expected.isFinite() && expected in 0.15f..1.5f)
if (corners != 24) return CalibrationDistanceResult(false,
context.getString(R.string.spatial_distance_corners, corners))
if (points?.size != 24 || points.any { !it.finite() }) return CalibrationDistanceResult(false,
context.getString(R.string.spatial_distance_depth))
val measured=(points.reduce { a,b -> a+b }*(1f/points.size)).length()
if (!measured.isFinite()) return CalibrationDistanceResult(false,context.getString(R.string.spatial_distance_invalid))
val accepted=abs(measured-expected)<0.05f
return CalibrationDistanceResult(accepted,
context.getString(R.string.spatial_distance_result,
SpatialMeasurements.validation(measured), SpatialMeasurements.validation(expected),
context.getString(if (accepted) R.string.spatial_distance_passed else R.string.spatial_distance_retry)))
}
}
@@ -0,0 +1,27 @@
package com.droiddeck.launcher.glasses
import org.json.JSONObject
internal data class CalibrationObservationData(
val objects: List<DoubleArray>, val left: List<DoubleArray>, val right: List<DoubleArray>, val head: SpatialPose,
) {
companion object {
fun parse(view: JSONObject): CalibrationObservationData {
fun points(name: String, dimensions: Int): List<DoubleArray> {
val rows=view.getJSONArray(name)
require(rows.length() in 12..24) { "Invalid saved board corner count" }
return (0 until rows.length()).map { row ->
val values=rows.getJSONArray(row)
require(values.length()==dimensions)
DoubleArray(dimensions) { values.getDouble(it).also { value -> require(value.isFinite()) } }
}
}
val objects=points("objects",3); val left=points("left",2); val right=points("right",2)
require(objects.size==left.size && objects.size==right.size)
val pose=view.getJSONArray("head")
require(pose.length()==7)
val head=SpatialPose.fromSdk(FloatArray(7) { pose.getDouble(it).toFloat() }) ?: error("Invalid saved head pose")
return CalibrationObservationData(objects,left,right,head)
}
}
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,29 @@
package com.droiddeck.launcher.glasses
data class GlassesResolution(val width: Int, val height: Int) {
val key: String get() = "${width}x$height"
}
enum class GlassesDisplayProfile { TWO_D, THREE_D, VR }
internal object GlassesDisplayModes {
fun profile(stereo: Boolean, vr: Boolean): GlassesDisplayProfile = when {
vr -> GlassesDisplayProfile.VR
stereo -> GlassesDisplayProfile.THREE_D
else -> GlassesDisplayProfile.TWO_D
}
fun choice(settings: GlassesSettings, profile: GlassesDisplayProfile): String = when (profile) {
GlassesDisplayProfile.TWO_D -> settings.resolution2d
GlassesDisplayProfile.THREE_D -> settings.resolution3d
GlassesDisplayProfile.VR -> settings.resolutionVr
}
fun selected(settings: GlassesSettings, state: GlassesState,
stereo: Boolean = state.sbs, vr: Boolean = state.vrActive): GlassesResolution? {
val selected = choice(settings, profile(stereo, vr)).ifEmpty {
if (vr) choice(settings, profile(stereo, false)) else ""
}
return state.resolutions.firstOrNull { it.key == selected }
}
}
@@ -0,0 +1,27 @@
package com.droiddeck.launcher.glasses
/** Hardware properties; display refresh is selected from Android's advertised modes. */
interface GlassesModel {
val name: String
val controllable: Boolean
val brightnessMax: Int
val volumeMax: Int
val diagonalFovDeg: Float
val sbs90: Boolean
val hasSunblock: Boolean get() = true
val resolutions: List<GlassesResolution> get() = emptyList()
fun mode(hz: Int, sbs: Boolean, resolution: GlassesResolution?): Int = mode(hz, sbs)
fun mode(hz: Int, sbs: Boolean): Int
fun modeForStep(step: Int): Int?
}
/** The command IDs here are the existing glasses controller's internal vocabulary.
* Each transport translates them to its own wire protocol; unsupported controls return false.
*/
internal interface GlassesLink {
val hasImu: Boolean
val badFrames: Long get() = 0L
val ready: Boolean get() = true
fun send(msgId: Int, payload: ByteArray = ByteArray(0)): Boolean
fun close()
}
@@ -0,0 +1,175 @@
package com.droiddeck.launcher.glasses
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.graphics.Color
import android.graphics.PorterDuff
import android.graphics.SurfaceTexture
import android.os.Build
import android.os.Looper
import android.view.Choreographer
import android.view.Surface
import android.view.View
import android.view.WindowManager
import android.view.inspector.WindowInspector
import kotlin.math.min
import kotlin.math.roundToInt
class GlassesMenuCapture(private val activity: Activity, private val onSize: (Int, Int) -> Unit,
private val onFailure: (Throwable) -> Unit = {}) : Choreographer.FrameCallback {
private var texture: SurfaceTexture? = null
private var surface: Surface? = null
private var running = false
private var framePending = false
private var lastFrameNs = Long.MIN_VALUE
private var failures = 0
private var bufferWidth = 0
private var bufferHeight = 0
private val basePosition = IntArray(2)
private val windowPosition = IntArray(2)
fun attach(texture: SurfaceTexture, surface: Surface) {
checkMainThread()
this.texture = texture
this.surface = surface
bufferWidth = 0
bufferHeight = 0
failures = 0
lastFrameNs = Long.MIN_VALUE
}
fun start() {
checkMainThread()
if (running || activity.isDestroyed || activity.isFinishing) return
running = true
failures = 0
lastFrameNs = Long.MIN_VALUE
schedule()
}
fun stop() {
checkMainThread()
running = false
if (framePending) Choreographer.getInstance().removeFrameCallback(this)
framePending = false
}
override fun doFrame(frameTimeNanos: Long) {
framePending = false
if (!running) return
if (activity.isDestroyed || activity.isFinishing) {
stop()
return
}
if (lastFrameNs == Long.MIN_VALUE || frameTimeNanos - lastFrameNs >= FRAME_INTERVAL_NS) {
lastFrameNs = frameTimeNanos
try {
capture()
failures = 0
} catch (error: Exception) {
if (++failures >= MAX_FAILURES) {
stop()
onFailure(error)
}
}
}
if (running) schedule()
}
private fun schedule() {
if (framePending) return
framePending = true
Choreographer.getInstance().postFrameCallback(this)
}
private fun capture() {
val target = surface ?: return
val input = texture ?: return
val decor = activity.window.decorView
if (!decor.isAttachedToWindow || !decor.isShown) return
val size = captureSize(decor.width, decor.height) ?: return
check(target.isValid)
if (bufferWidth != size.first || bufferHeight != size.second) {
input.setDefaultBufferSize(size.first, size.second)
onSize(size.first, size.second)
bufferWidth = size.first
bufferHeight = size.second
}
val candidates = if (Build.VERSION.SDK_INT >= 29) WindowInspector.getGlobalWindowViews() else emptyList()
val roots = captureRoots(activity, candidates)
val canvas = target.lockHardwareCanvas()
val initialSave = canvas.save()
try {
canvas.drawColor(Color.BLACK, PorterDuff.Mode.SRC)
val scale = min(canvas.width.toFloat() / decor.width, canvas.height.toFloat() / decor.height)
canvas.translate((canvas.width - decor.width * scale) / 2f, (canvas.height - decor.height * scale) / 2f)
canvas.scale(scale, scale)
canvas.clipRect(0, 0, decor.width, decor.height)
decor.getLocationOnScreen(basePosition)
for (root in roots) {
val params = root.layoutParams as? WindowManager.LayoutParams
if (root !== decor && params != null && params.flags and WindowManager.LayoutParams.FLAG_DIM_BEHIND != 0) {
canvas.drawColor(Color.argb((params.dimAmount.coerceIn(0f, 1f) * 255).roundToInt(), 0, 0, 0))
}
root.getLocationOnScreen(windowPosition)
val saved = canvas.save()
try {
canvas.translate((windowPosition[0] - basePosition[0]).toFloat(), (windowPosition[1] - basePosition[1]).toFloat())
root.draw(canvas)
} finally {
canvas.restoreToCount(saved)
}
}
} finally {
try {
canvas.restoreToCount(initialSave)
} finally {
target.unlockCanvasAndPost(canvas)
}
}
}
private fun checkMainThread() {
check(Looper.myLooper() == Looper.getMainLooper())
}
internal companion object {
private const val FRAME_INTERVAL_NS = 33_333_334L
private const val MAX_FAILURES = 3
private const val MAX_EDGE = 1920
internal fun captureSize(width: Int, height: Int): Pair<Int, Int>? {
if (width <= 0 || height <= 0) return null
val scale = min(1.0, MAX_EDGE.toDouble() / maxOf(width, height))
return (width * scale).roundToInt().coerceAtLeast(1) to (height * scale).roundToInt().coerceAtLeast(1)
}
internal fun captureRoots(activity: Activity, candidates: List<View>): List<View> {
val decor = activity.window.decorView
if (activity.window.attributes.flags and WindowManager.LayoutParams.FLAG_SECURE != 0 ||
!decor.isAttachedToWindow || !decor.isShown) return emptyList()
if (Build.VERSION.SDK_INT < 29) return listOf(decor)
val display = decor.display?.displayId ?: return listOf(decor)
val token = decor.applicationWindowToken
return listOf(decor) + candidates.filter { root ->
if (root === decor || !root.isAttachedToWindow || !root.isShown || root.display?.displayId != display) return@filter false
val params = root.layoutParams as? WindowManager.LayoutParams ?: return@filter false
if (params.flags and WindowManager.LayoutParams.FLAG_SECURE != 0 ||
params.type !in WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW..WindowManager.LayoutParams.LAST_SUB_WINDOW) return@filter false
ownsContext(root.context, activity) || (token != null && root.applicationWindowToken == token)
}.distinct()
}
private fun ownsContext(context: Context, activity: Activity): Boolean {
var current = context
repeat(16) {
if (current === activity) return true
val next = (current as? ContextWrapper)?.baseContext ?: return false
if (next === current) return false
current = next
}
return false
}
}
}
@@ -0,0 +1,219 @@
package com.droiddeck.launcher.glasses
import android.app.Activity
import android.app.Application
import android.app.Presentation
import android.os.Bundle
import android.util.Log
import android.view.Display
import android.view.WindowManager
import com.droiddeck.launcher.SessionActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
object GlassesMenuMirror {
private var controller: GlassesMenuController? = null
fun initialize(app: Application) {
if (controller != null) return
val pose = GlassesMenuPose()
val menus = GlassesMenuController(pose,
display = Glasses::glassesDisplay,
observe = { active -> if (active) Glasses.addPoseObserver(pose) else Glasses.removePoseObserver(pose) },
create = { activity, display, failure -> MenuPresentation(activity, display, pose, failure) })
controller = menus
app.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
override fun onActivityResumed(activity: Activity) = menus.resume(activity)
override fun onActivityPaused(activity: Activity) = menus.pause(activity)
override fun onActivityDestroyed(activity: Activity) = menus.pause(activity)
override fun onActivityCreated(activity: Activity, state: Bundle?) = Unit
override fun onActivityStarted(activity: Activity) = Unit
override fun onActivityStopped(activity: Activity) = Unit
override fun onActivitySaveInstanceState(activity: Activity, state: Bundle) = Unit
})
CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate).launch {
combine(Glasses.state, Glasses.settings) { state, settings -> state to settings }
.collect { (state, settings) -> menus.update(state, settings) }
}
}
fun setSessionMenuVisible(activity: Activity, visible: Boolean) {
controller?.setSessionMenuVisible(activity, visible)
}
fun recenter() { controller?.recenter() }
}
internal class GlassesMenuController(
private val pose: GlassesMenuPose,
private val display: () -> Display?,
private val observe: (Boolean) -> Unit,
private val create: (Activity, Display, (Throwable) -> Unit) -> Output,
private val schedule: (() -> Unit) -> Unit = { android.os.Handler(android.os.Looper.getMainLooper()).post(it) },
) {
interface Output {
fun configure(state: GlassesState, settings: GlassesSettings)
fun show()
fun close()
}
private var activity: Activity? = null
private val sessionMenus = java.util.WeakHashMap<Activity, Boolean>()
private var state = GlassesState()
private var settings = GlassesSettings()
private var output: Output? = null
private var outputDisplay = Display.INVALID_DISPLAY
private var failed = false
private var dismissalRetried = false
private var generation = 0
fun resume(owner: Activity) {
if (activity !== owner) close()
activity = owner
failed = false
dismissalRetried = false
reconcile()
}
fun pause(owner: Activity) {
if (activity !== owner) return
close()
activity = null
}
fun setSessionMenuVisible(owner: Activity, visible: Boolean) {
if (visible) sessionMenus[owner] = true else sessionMenus.remove(owner)
if (activity === owner) reconcile()
}
fun update(next: GlassesState, preferences: GlassesSettings) {
val deviceChanged = state.canControl != next.canControl || state.model != next.model
if (deviceChanged) pose.reset()
if (deviceChanged || state.displayId != next.displayId || state.width != next.width ||
state.height != next.height || state.sbs != next.sbs || settings != preferences) {
failed = false
dismissalRetried = false
}
state = next
settings = preferences
pose.configure(preferences)
reconcile()
}
fun recenter() = pose.recenter()
private fun reconcile() {
val owner = activity
val eligible = owner != null && !owner.isFinishing && !owner.isDestroyed && !owner.isInPictureInPictureMode &&
owner.window.attributes.flags and WindowManager.LayoutParams.FLAG_SECURE == 0 &&
owner !is com.droiddeck.launcher.QuestXrActivity &&
(owner !is SessionActivity || sessionMenus[owner] == true) &&
settings.gameOnGlasses && settings.screen3dof && state.usable && state.canControl && state.supportsScreen3dof
val target = if (eligible && !failed) display() else null
if (target == null || owner == null || owner.windowManager.defaultDisplay.displayId == target.displayId) {
close()
return
}
if (outputDisplay != target.displayId) close()
try {
output?.let { it.configure(state, settings); return }
val token = ++generation
val next = create(owner, target) { error ->
if (generation == token) {
close()
if (error is GlassesMenuDisplayStopped && !dismissalRetried) {
dismissalRetried = true
val retry = generation
schedule { if (generation == retry && activity === owner) reconcile() }
} else {
Log.w("GlassesMenu", "Tracked menus are unavailable for this display", error)
failed = true
}
}
}
if (generation != token) {
next.close()
return
}
output = next
outputDisplay = target.displayId
next.configure(state, settings)
if (generation != token) return
observe(true)
next.show()
} catch (error: RuntimeException) {
Log.w("GlassesMenu", "Could not show tracked menus", error)
failed = true
close()
}
}
private fun close() {
++generation
val previous = output
output = null
outputDisplay = Display.INVALID_DISPLAY
if (previous != null) {
observe(false)
try { previous.close() }
catch (error: RuntimeException) { Log.w("GlassesMenu", "Could not close tracked menus", error) }
}
}
}
internal class GlassesMenuDisplayStopped : IllegalStateException("The glasses menu display stopped")
private class MenuPresentation(
private val owner: Activity,
display: Display,
pose: GlassesMenuPose,
private val onFailure: (Throwable) -> Unit,
) : Presentation(owner, display), GlassesMenuController.Output {
private var closed = false
private val capture: GlassesMenuCapture = GlassesMenuCapture(owner,
onSize = { width, height -> surface.setSourceSize(width, height) }, onFailure = onFailure)
private val surface: GlassesMenuSurface = GlassesMenuSurface(context, pose, onFailure = onFailure) { texture, input ->
if (!closed) {
capture.attach(texture, input)
capture.start()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window?.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
setContentView(surface)
}
override fun configure(state: GlassesState, settings: GlassesSettings) {
surface.configure(settings, state)
val mode = display.mode
val hz = if (state.sbs) settings.sbsHz else settings.refreshHz
val best = display.supportedModes.filter {
it.physicalWidth == mode.physicalWidth && it.physicalHeight == mode.physicalHeight
}.minByOrNull { kotlin.math.abs(it.refreshRate - hz) }
if (best != null) window?.let { it.attributes = it.attributes.apply { preferredDisplayModeId = best.modeId } }
}
override fun close() {
if (closed) return
closed = true
capture.stop()
surface.close()
dismiss()
}
override fun onStop() {
capture.stop()
surface.close()
super.onStop()
if (!closed) {
closed = true
onFailure(GlassesMenuDisplayStopped())
}
}
}
@@ -0,0 +1,86 @@
package com.droiddeck.launcher.glasses
import com.droiddeck.launcher.gpu.HeadPose
class GlassesMenuPose internal constructor(
private val spatialEnabled: () -> Boolean = { SpatialTracking.enabled },
private val spatialView: (Long) -> SpatialPose? = { now ->
synchronized(SpatialTracking.session) {
if (SpatialTracking.session.valid(now)) SpatialTracking.session.view(true) else null
}
},
private val recenterSpatial: () -> Unit = { SpatialTracking.recenter(true) },
) : Glasses.PoseSink {
data class Frame(
val rotation: FloatArray,
val position: FloatArray,
val valid: Boolean,
val spatial: Boolean,
)
private var tracking = true
private var positional = false
private var reference: HeadPose.Reference? = null
private var latest: FloatArray? = null
private var latestTime = 0L
private var safe = identity()
@Synchronized fun configure(settings: GlassesSettings) {
if (tracking != settings.headTracking) {
reference = null
safe = identity()
}
tracking = settings.headTracking
positional = settings.positionalTracking
}
@Synchronized override fun onPose(x: Float, y: Float, z: Float, w: Float, timeNs: Long) {
val norm = x*x + y*y + z*z + w*w
if (!norm.isFinite() || norm !in 0.25f..2.25f || timeNs <= latestTime) return
val matrix = HeadPose.fromGlasses(x, y, z, w)
if (matrix.any { !it.isFinite() }) return
latest = matrix
latestTime = timeNs
}
@Synchronized fun snapshot(now: Long = System.nanoTime()): Frame {
if (!tracking) return identity()
val spatial = positional && spatialEnabled()
if (spatial) {
val pose = spatialView(now)
val normalized = pose?.rotation?.normalized()
if (pose != null && pose.position.finite() && normalized != null) {
safe = Frame(normalized.matrix(), pose.position.array(), true, true)
return copy(safe)
}
} else {
val matrix = latest
if (matrix != null && latestTime > 0 && now-latestTime in 0..MAX_AGE_NS) {
val anchor = reference ?: HeadPose.recenter(matrix, centerView = true).also { reference = it }
safe = Frame(HeadPose.relative(anchor, matrix), FloatArray(3), true, false)
return copy(safe)
}
}
return copy(safe).copy(valid = false, spatial = spatial)
}
@Synchronized fun recenter() {
if (positional && spatialEnabled()) recenterSpatial()
else reference = null
}
@Synchronized fun reset() {
reference = null
latest = null
latestTime = 0L
safe = identity()
}
private fun copy(frame: Frame) = frame.copy(rotation = frame.rotation.copyOf(), position = frame.position.copyOf())
private fun identity() = Frame(HeadPose.IDENTITY.copyOf(), FloatArray(3), false, false)
companion object {
internal const val MAX_AGE_NS = 100_000_000L
}
}
Loaded 100 of 1480 files, more files were not shown because too many files have changed in this diff. Show more