diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt b/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt index 80b8091bee..f4952db752 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt @@ -5,6 +5,7 @@ import com.droiddeck.launcher.R import com.droiddeck.launcher.runtime.LinuxRuntime import com.droiddeck.launcher.session.GameStorage import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.session.SecondaryLibrary import java.io.File /** @@ -84,7 +85,11 @@ object Library { /** Proton keeps each game's prefix below compatdata//pfx in a Steam library. */ fun protonPrefix(context: Context, appId: Long, preferredLibrary: File? = null): File? { val ids = listOf(appId.toString(), java.lang.Integer.toString(appId.toInt())).distinct() - val roots = (listOfNotNull(preferredLibrary) + steamLibraries(context).map { it.first }) + val secondary = GameStorage.effective(context)?.let { File(it.path) } + fun prefixRoot(root: File): File = if (secondary != null && root.canonicalPath == secondary.canonicalPath) + SecondaryLibrary.privateRoot(context.filesDir, root).takeIf { File(it, "steamapps/compatdata").isDirectory } ?: root + else root + val roots = (listOfNotNull(preferredLibrary) + steamLibraries(context).map { it.first }).map(::prefixRoot) .distinctBy { runCatching { it.canonicalPath }.getOrDefault(it.absolutePath) } return roots.asSequence() .flatMap { root -> ids.asSequence().map { id -> File(root, "steamapps/compatdata/$id/pfx") } } diff --git a/app/src/main/java/com/droiddeck/launcher/session/SecondaryLibrary.kt b/app/src/main/java/com/droiddeck/launcher/session/SecondaryLibrary.kt new file mode 100644 index 0000000000..78760a5fba --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/session/SecondaryLibrary.kt @@ -0,0 +1,94 @@ +package com.droiddeck.launcher.session + +import java.io.File +import java.nio.file.Files +import java.nio.file.LinkOption.NOFOLLOW_LINKS +import java.nio.file.StandardCopyOption.COPY_ATTRIBUTES +import java.security.MessageDigest +import java.util.Locale + +/** Android shared storage cannot create the links in Wine prefixes or native Steam depots. */ +object SecondaryLibrary { + private val bootstrapTools = linkedMapOf( + "4185400" to "SteamLinuxRuntime_4-arm64", + "4427310" to "Proton Experimental (ARM64)", + "4628740" to "Proton 11.0 (ARM64)", + ) + + fun privateRoot(files: File, library: File): File { + val digest = MessageDigest.getInstance("SHA-256").digest(library.canonicalPath.toByteArray()) + .joinToString("") { "%02x".format(it.toInt() and 255) } + return File(files, "steam-libraries/$digest") + } + + /** Parent binds come first; the more specific private directories override them in PRoot. */ + fun binds(files: File, library: File): List { + val private = privateRoot(files, library) + val apps = File(library, "steamapps") + val tools = LinkedHashMap(bootstrapTools) + // Include other installed Proton/runtime versions, without routing ordinary game depots. + apps.listFiles()?.filter { it.name.matches(Regex("appmanifest_\\d+\\.acf")) }?.forEach { manifest -> + val text = manifest.readText() + val id = Regex("\"appid\"\\s+\"(\\d+)\"").find(text)?.groupValues?.get(1) + val dir = Regex("\"installdir\"\\s+\"([^\"]+)\"").find(text)?.groupValues?.get(1) + if (id != null && dir != null && File(dir).name == dir && + (dir.startsWith("Proton ") || dir.startsWith("SteamLinuxRuntime"))) tools[id] = dir + } + val overrides = ArrayList>() + for (name in listOf("compatdata", "shadercache")) { + val target = File(private, "steamapps/$name") + migrate(File(apps, name), target) + overrides.add(target to "steamapps/$name") + } + for ((id, dir) in tools) { + for (relative in listOf("steamapps/common/$dir", "steamapps/downloading/$id")) { + val target = File(private, relative) + migrate(File(library, relative), target) + // FUSE discarded the executable bits. Steam will restore the depot's exact modes + // on verification; its launchers must already be executable when importing it. + target.walkTopDown().filter { it.isFile }.forEach { it.setExecutable(true, true) } + overrides.add(target to relative) + // Steam normalizes depot install directories to lowercase on Android's + // case-insensitive shared storage. PRoot binds are case-sensitive, so both + // spellings must reach the same private directory during the commit. + val lower = relative.lowercase(Locale.ROOT) + if (lower != relative) overrides.add(target to lower) + } + } + return listOf("/mnt/droiddeck-sd", "/mnt/bannerlator-sd").flatMap { guest -> + listOf("${library.path}:$guest") + overrides.map { (host, relative) -> "${host.path}:$guest/$relative" } + } + } + + /** Copy once into a staging directory; retain the source and never merge over a live prefix. */ + private fun migrate(source: File, target: File) { + val migrated = File(target.parentFile, ".${target.name}.migrated") + if (target.isDirectory) { + if (!migrated.isFile) migrated.writeText(source.canonicalPath) + return + } + check(target.parentFile!!.isDirectory || target.parentFile!!.mkdirs()) { "cannot create ${target.parent}" } + if (!source.isDirectory || migrated.isFile) { + check(target.mkdirs()) { "cannot create $target" } + if (!migrated.isFile) migrated.writeText(source.canonicalPath) + return + } + val stage = File(target.parentFile, "${target.name}.migrating") + check(!stage.exists() || stage.deleteRecursively()) { "cannot clear $stage" } + check(stage.mkdirs()) { "cannot create $stage" } + try { + Files.walk(source.toPath()).use { paths -> + paths.forEach { path -> + val destination = stage.toPath().resolve(source.toPath().relativize(path)) + if (Files.isDirectory(path, NOFOLLOW_LINKS)) Files.createDirectories(destination) + else Files.copy(path, destination, NOFOLLOW_LINKS, COPY_ATTRIBUTES) + } + } + check(stage.renameTo(target)) { "cannot finish migration to $target" } + migrated.writeText(source.canonicalPath) + } catch (e: Exception) { + stage.deleteRecursively() + throw e + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index 17eb197dd1..f9d798a7ff 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -736,11 +736,14 @@ class SessionService : Service() { val problem = GameStorage.prepare(library.path) if (problem == null) { File(LinuxRuntime.rootDir(this), "mnt/droiddeck-sd").mkdirs() - binds.add("${library.path}:/mnt/droiddeck-sd") // Links, prefixes and Steam entries made before the rename still name the old path. File(LinuxRuntime.rootDir(this), "mnt/bannerlator-sd").mkdirs() - binds.add("${library.path}:/mnt/bannerlator-sd") - Log.i(TAG, "game storage: ${library.path} -> /mnt/droiddeck-sd (\"${library.label}\")") + try { + binds.addAll(SecondaryLibrary.binds(filesDir, File(library.path))) + Log.i(TAG, "game storage: ${library.path} -> /mnt/droiddeck-sd (\"${library.label}\"); prefixes and native tools private") + } catch (e: Exception) { + Log.w(TAG, "game storage: private directories could not be prepared; internal only this session", e) + } } else { Log.w(TAG, "game storage: $problem; internal only this session") } diff --git a/app/src/test/java/com/droiddeck/launcher/session/SecondaryLibraryTest.kt b/app/src/test/java/com/droiddeck/launcher/session/SecondaryLibraryTest.kt new file mode 100644 index 0000000000..cdd553cffa --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/session/SecondaryLibraryTest.kt @@ -0,0 +1,76 @@ +package com.droiddeck.launcher.session + +import java.io.File +import java.nio.file.Files +import org.junit.Assert.* +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder + +class SecondaryLibraryTest { + @get:Rule val tmp = TemporaryFolder() + + @Test fun gamesStaySharedButPrefixesAndNativeDownloadsArePrivate() { + val files = tmp.newFolder("files") + val library = tmp.newFolder("card") + File(library, "steamapps/common/Game").mkdirs() + val binds = SecondaryLibrary.binds(files, library) + val private = SecondaryLibrary.privateRoot(files, library) + for (guest in listOf("/mnt/droiddeck-sd", "/mnt/bannerlator-sd")) { + assertTrue(binds.contains("${library.path}:$guest")) + assertTrue(binds.contains("$private/steamapps/compatdata:$guest/steamapps/compatdata")) + assertTrue(binds.contains("$private/steamapps/downloading/4185400:$guest/steamapps/downloading/4185400")) + assertTrue(binds.contains("$private/steamapps/common/SteamLinuxRuntime_4-arm64:$guest/steamapps/common/steamlinuxruntime_4-arm64")) + assertFalse(binds.any { it.endsWith(":$guest/steamapps/common/Game") }) + } + } + + @Test fun migrationPreservesSavesLinksAndOriginalAndNeverOverwritesNewSaves() { + val files = tmp.newFolder("files") + val library = tmp.newFolder("card") + val save = File(library, "steamapps/compatdata/42/pfx/save.dat") + save.parentFile!!.mkdirs() + save.writeText("original") + Files.createSymbolicLink(File(save.parentFile, "link").toPath(), File("save.dat").toPath()) + SecondaryLibrary.binds(files, library) + val copied = File(SecondaryLibrary.privateRoot(files, library), "steamapps/compatdata/42/pfx/save.dat") + assertEquals("original", copied.readText()) + assertTrue(Files.isSymbolicLink(File(copied.parentFile, "link").toPath())) + copied.writeText("new save") + SecondaryLibrary.binds(files, library) + assertEquals("new save", copied.readText()) + assertEquals("original", save.readText()) + } + + @Test fun changingLibraryUsesADifferentPrivatePrefixDirectory() { + val files = tmp.newFolder("files") + assertNotEquals(SecondaryLibrary.privateRoot(files, tmp.newFolder("one")), + SecondaryLibrary.privateRoot(files, tmp.newFolder("two"))) + } + + @Test fun installedNativeToolVersionsAlsoGetPrivateStaging() { + val files = tmp.newFolder("files") + val library = tmp.newFolder("card") + val manifest = File(library, "steamapps/appmanifest_123.acf") + manifest.parentFile!!.mkdirs() + manifest.writeText("\"AppState\" { \"appid\" \"123\" \"installdir\" \"Proton 99.0 (ARM64)\" }") + assertTrue(SecondaryLibrary.binds(files, library).any { + it.endsWith(":/mnt/droiddeck-sd/steamapps/downloading/123") + }) + } + + @Test fun deletingAStagedDepotDoesNotRestoreTheOldSharedDownload() { + val files = tmp.newFolder("files") + val library = tmp.newFolder("card") + val stale = File(library, "steamapps/downloading/4185400/stale") + stale.parentFile!!.mkdirs() + stale.writeText("old download") + SecondaryLibrary.binds(files, library) + val target = File(SecondaryLibrary.privateRoot(files, library), "steamapps/downloading/4185400") + assertTrue(File(target, "stale").isFile) + target.deleteRecursively() + SecondaryLibrary.binds(files, library) + assertFalse(File(target, "stale").exists()) + assertTrue(stale.isFile) + } +} diff --git a/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-compat b/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-compat index fb70b40e23..1a38c20b88 100755 --- a/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-compat +++ b/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-compat @@ -254,7 +254,8 @@ fi depot= best=0 for name in %s; do - candidate="$steam_root/steamapps/common/$name" + for library in "$steam_root" /mnt/droiddeck-sd; do + candidate="$library/steamapps/common/$name" [ -d "$candidate/files/bin-arm64" ] && [ -x "$candidate/proton" ] || continue build=0 read -r build _ 2>/dev/null < "$candidate/version" @@ -263,6 +264,7 @@ for name in %s; do depot=$candidate best=$build fi + done done if [ -n "$depot" ]; then bl_components "$depot" @@ -595,10 +597,10 @@ def depot_build(depot): def find_source(steam_root): """The newest ARM64 Proton depot the client has downloaded (the one the launcher runs), or None.""" - common = os.path.join(steam_root, "steamapps", "common") # bin-arm64 is what makes it the native build rather than the x86_64 one. - found = [os.path.join(common, name) for name in SOURCES - if os.path.isdir(os.path.join(common, name, "files", "bin-arm64"))] + found = [os.path.join(root, "steamapps", "common", name) + for root in (steam_root, "/mnt/droiddeck-sd") for name in SOURCES + if os.path.isdir(os.path.join(root, "steamapps", "common", name, "files", "bin-arm64"))] return max(found, key=depot_build) if found else None