From 58968c9286973e341e1f61f4c862ea3b04d9554b Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Thu, 29 Oct 2026 23:21:38 -0400 Subject: [PATCH] fix: itch.io installs unpack a game archive nested inside the download (#1800) --- .../launcher/stores/itch/ItchArchives.kt | 45 ++++++++ .../launcher/stores/itch/ItchInstaller.kt | 107 ++++++++++-------- .../launcher/stores/itch/ItchArchivesTest.kt | 70 ++++++++++++ 3 files changed, 172 insertions(+), 50 deletions(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/itch/ItchArchives.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/itch/ItchArchivesTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchArchives.kt b/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchArchives.kt new file mode 100644 index 0000000000..fd08c9d6bd --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchArchives.kt @@ -0,0 +1,45 @@ +package com.droiddeck.launcher.stores.itch + +import java.io.File +import java.io.FileInputStream +import java.nio.file.Files + +internal object ItchArchives { + private val PACKED = setOf("zip", "7z", "rar", "tar", "gz", "bz2", "xz") + + fun kind(file: File, filename: String): String { + val extension = filename.substringAfterLast('.', "").lowercase() + if (extension == "exe") return "exe" + return signature(file) ?: extension + } + + private fun signature(file: File): String? { + val head = runCatching { FileInputStream(file).use { input -> ByteArray(300).also { input.read(it) } } }.getOrNull() + ?: return null + fun at(i: Int, vararg bytes: Int) = bytes.indices.all { head.getOrNull(i + it) == bytes[it].toByte() } + return when { + at(0, 0x50, 0x4B) -> "zip" + at(0, 0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C) -> "7z" + at(0, 0x52, 0x61, 0x72, 0x21) -> "rar" + at(0, 0x1F, 0x8B) -> "gz" + at(0, 0x42, 0x5A, 0x68) -> "bz2" + at(0, 0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00) -> "xz" + at(257, 0x75, 0x73, 0x74, 0x61, 0x72) -> "tar" + else -> null + } + } + + fun inner(folder: File): File? { + val found = ArrayList() + fun walk(dir: File, depth: Int) { + val files = dir.listFiles() ?: return + for (file in files) { + if (Files.isSymbolicLink(file.toPath())) continue + if (file.isFile && signature(file) in PACKED) found.add(file) + else if (file.isDirectory && depth < 2 && !file.name.startsWith(".")) walk(file, depth + 1) + } + } + walk(folder, 0) + return found.maxByOrNull { it.length() } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchInstaller.kt b/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchInstaller.kt index f084e503d6..9aa36acd0f 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchInstaller.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchInstaller.kt @@ -13,6 +13,7 @@ import com.droiddeck.launcher.stores.StoreInstalls import com.droiddeck.launcher.stores.StoreNet import com.droiddeck.launcher.stores.download.DownloadQueue import com.droiddeck.launcher.stores.download.DownloadStage +import org.apache.commons.compress.MemoryLimitException import org.apache.commons.compress.archivers.sevenz.SevenZFile import org.apache.commons.compress.archivers.tar.TarArchiveInputStream import org.apache.commons.compress.archivers.zip.ZipFile @@ -25,6 +26,7 @@ import java.io.FileInputStream import java.io.FileOutputStream import java.net.HttpURLConnection import java.net.URL +import java.nio.file.Files import java.util.concurrent.atomic.AtomicBoolean internal class ItchInstaller( @@ -184,17 +186,20 @@ internal class ItchInstaller( work.mkdirs() if (resuming) clearPartialInstall() val exe = try { - when (val kind = sniff(archive, filename)) { - "zip" -> unzip(archive, handle) - "7z" -> sevenZip(archive, handle) - "rar" -> unrar(archive, handle) - "exe" -> copy(archive, File(work, filename.ifBlank { "game.exe" })) - "tar" -> untar(archive, null, handle) - "gz", "bz2", "xz" -> untar(archive, kind, handle) ?: single(archive, kind, handle) - else -> copy(archive, File(work, filename.ifBlank { "game" })) - } + unpack(archive, filename, handle) val hint = filename.substringBeforeLast('.') - StoreExe.pick(work, hint.ifBlank { null }, item.title) + var picked = StoreExe.pick(work, hint.ifBlank { null }, item.title) + var passes = 0 + while (picked.isBlank() && passes < 2 && !cancelled.get()) { + val inner = ItchArchives.inner(work) ?: break + handle.log("itch.io: no exe yet, unpacking ${inner.name}") + unpack(inner, inner.name, handle) + if (cancelled.get()) break + inner.delete() + picked = StoreExe.pick(work, inner.name.substringBeforeLast('.').takeIf { it.endsWith(".exe", ignoreCase = true) }, item.title) + passes++ + } + picked } catch (t: Throwable) { if (updating) ItchStaging.delete(work) throw t @@ -221,21 +226,20 @@ internal class ItchInstaller( return exe } - private fun sniff(file: File, filename: String): String { - val extension = filename.substringAfterLast('.', "").lowercase() - if (extension == "exe") return "exe" - val head = runCatching { FileInputStream(file).use { input -> ByteArray(300).also { input.read(it) } } }.getOrNull() - ?: return extension - fun at(i: Int, vararg bytes: Int) = bytes.indices.all { head.getOrNull(i + it) == bytes[it].toByte() } - return when { - at(0, 0x50, 0x4B) -> "zip" - at(0, 0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C) -> "7z" - at(0, 0x52, 0x61, 0x72, 0x21) -> "rar" - at(0, 0x1F, 0x8B) -> "gz" - at(0, 0x42, 0x5A, 0x68) -> "bz2" - at(0, 0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00) -> "xz" - at(257, 0x75, 0x73, 0x74, 0x61, 0x72) -> "tar" - else -> extension + private fun unpack(archive: File, filename: String, handle: DownloadQueue.JobHandle) { + when (val kind = ItchArchives.kind(archive, filename)) { + "zip" -> unzip(archive, handle) + "7z" -> try { + sevenZip(archive, handle) + } catch (e: MemoryLimitException) { + handle.log("itch.io: ${archive.name} needs more memory than the app has, unpacking it in the runtime") + unrar(archive, handle, kind) + } + "rar" -> unrar(archive, handle, kind) + "exe" -> copy(archive, File(work, filename.ifBlank { "game.exe" })) + "tar" -> untar(archive, null, handle) + "gz", "bz2", "xz" -> untar(archive, kind, handle) ?: single(archive, kind, handle) + else -> copy(archive, File(work, filename.ifBlank { "game" })) } } @@ -247,12 +251,12 @@ internal class ItchInstaller( } // No JVM library reads RAR5; the runtime's bsdtar (libarchive) does. - private fun unrar(archive: File, handle: DownloadQueue.JobHandle) { - if (!LinuxRuntime.isInstalled(app)) throw IllegalStateException(app.getString(R.string.stores_itch_archive, "rar")) + private fun unrar(archive: File, handle: DownloadQueue.JobHandle, kind: String) { + if (!LinuxRuntime.isInstalled(app)) throw IllegalStateException(app.getString(R.string.stores_itch_archive, kind)) val binds = if (listOf(app.filesDir, app.cacheDir).any { work.absolutePath.startsWith(it.absolutePath + "/") }) null else listOf(work.absolutePath) fun bsdtar(args: List, onLine: (String) -> Unit) = - GuestCommand.run(app, listOf("/usr/bin/bsdtar") + args, logName = "itch-unrar", extraBinds = binds, fastPath = true) { line -> + GuestCommand.run(app, listOf("/usr/bin/bsdtar") + args, logName = "itch-unrar", extraBinds = binds, fastPath = true, cancelled = cancelled) { line -> if (!line.startsWith("proot ")) onLine(line) } var entries = 0L @@ -276,28 +280,31 @@ internal class ItchInstaller( val total = archive.length() var written = 0L val buffer = ByteArray(64 * 1024) - SevenZFile.builder().setFile(archive).get().use { seven -> - while (true) { - if (cancelled.get()) return - val entry = seven.nextEntry ?: break - val file = target(entry.name) - if (file == null) { - handle.log("itch.io: skipped an entry outside the game folder") - continue - } - if (entry.isDirectory) { - file.mkdirs() - continue - } - file.parentFile?.mkdirs() - FileOutputStream(file).use { out -> - while (true) { - if (cancelled.get()) return - val read = seven.read(buffer, 0, buffer.size) - if (read < 0) break - out.write(buffer, 0, read) - written += read - handle.stageProgress(written, total) + val limitKb = (Runtime.getRuntime().maxMemory() / 4 / 1024).coerceIn(32L * 1024, Int.MAX_VALUE.toLong()).toInt() + Files.newByteChannel(archive.toPath()).use { channel -> + SevenZFile.builder().setSeekableByteChannel(channel).setMaxMemoryLimitKb(limitKb).get().use { seven -> + while (true) { + if (cancelled.get()) return + val entry = seven.nextEntry ?: break + val file = target(entry.name) + if (file == null) { + handle.log("itch.io: skipped an entry outside the game folder") + continue + } + if (entry.isDirectory) { + file.mkdirs() + continue + } + file.parentFile?.mkdirs() + FileOutputStream(file).use { out -> + while (true) { + if (cancelled.get()) return + val read = seven.read(buffer, 0, buffer.size) + if (read < 0) break + out.write(buffer, 0, read) + written += read + handle.stageProgress(written, total) + } } } } diff --git a/app/src/test/java/com/droiddeck/launcher/stores/itch/ItchArchivesTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/itch/ItchArchivesTest.kt new file mode 100644 index 0000000000..e8ab5f10fa --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/itch/ItchArchivesTest.kt @@ -0,0 +1,70 @@ +package com.droiddeck.launcher.stores.itch + +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Before +import org.junit.Test +import java.io.File +import java.nio.file.Files + +class ItchArchivesTest { + private lateinit var dir: File + + @Before fun setUp() { + dir = Files.createTempDirectory("itch-archives").toFile() + } + + @After fun tearDown() { + Files.walk(dir.toPath()).sorted(Comparator.reverseOrder()).forEach { Files.deleteIfExists(it) } + } + + private fun file(name: String, vararg head: Int, size: Int = 64): File { + val f = File(dir, name) + f.parentFile?.mkdirs() + val bytes = ByteArray(maxOf(size, head.size)) + head.forEachIndexed { i, b -> bytes[i] = b.toByte() } + f.writeBytes(bytes) + return f + } + + @Test fun kindsComeFromTheContentNotTheName() { + assertEquals("7z", ItchArchives.kind(file("data.bin", 0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C), "data.bin")) + assertEquals("zip", ItchArchives.kind(file("a.7z", 0x50, 0x4B, 3, 4), "a.7z")) + assertEquals("rar", ItchArchives.kind(file("b", 0x52, 0x61, 0x72, 0x21), "b")) + assertEquals("exe", ItchArchives.kind(file("Game.exe", 0x50, 0x4B), "Game.exe")) + } + + @Test fun theLargestArchiveInsideIsUnpackedNext() { + file("readme.txt", 0x48, 0x69) + file("VotV.7z", 0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C, size = 4096) + file("extras/art.zip", 0x50, 0x4B, 3, 4, size = 128) + assertEquals("VotV.7z", ItchArchives.inner(dir)?.name) + } + + @Test fun archivesDeeperThanTwoFoldersAreLeftAlone() { + file("a/b/c/deep.zip", 0x50, 0x4B, 3, 4) + assertNull(ItchArchives.inner(dir)) + } + + @Test fun nothingPackedMeansNothingToUnpack() { + file("game.pak", 0x00, 0x01) + file("notes.txt", 0x48, 0x69) + assertNull(ItchArchives.inner(dir)) + } + + @Test fun aNamedArchiveWithoutASignatureIsSkipped() { + file("extras.zip", 0x48, 0x69, size = 8192) + file("game.7z", 0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C, size = 128) + assertEquals("game.7z", ItchArchives.inner(dir)?.name) + } + + @Test fun symlinksAreNotFollowed() { + val root = File(dir, "root").apply { mkdirs() } + val outside = File(dir, "outside").apply { mkdirs() } + File(outside, "keep.zip").writeBytes(byteArrayOf(0x50, 0x4B, 3, 4)) + Files.createSymbolicLink(File(root, "link").toPath(), outside.toPath()) + Files.createSymbolicLink(File(root, "linked.zip").toPath(), File(outside, "keep.zip").toPath()) + assertNull(ItchArchives.inner(root)) + } +}