feat: add GOG maintenance

This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-28 16:10:17 -04:00
1 parent 52fa41767c
commit 198f667bf0
54 files changed
+11840 -26

No files matched your search

@@ -74,6 +74,7 @@ object SessionFiles {
"usr/local/bin/droiddeck-steam-shortcuts" to "usr/local/bin/droiddeck-steam-shortcuts",
"usr/local/bin/droiddeck-xbox" to "usr/local/bin/droiddeck-xbox",
"usr/local/bin/droiddeck-store-launch" to "usr/local/bin/droiddeck-store-launch",
"usr/local/bin/droiddeck-gog-maintenance" to "usr/local/bin/droiddeck-gog-maintenance",
"usr/local/bin/droiddeck-battlenet" to "usr/local/bin/droiddeck-battlenet",
"usr/local/bin/droiddeck-epic" to "usr/local/bin/droiddeck-epic",
"usr/local/bin/droiddeck-epic-steam" to "usr/local/bin/droiddeck-epic-steam",
@@ -254,6 +254,7 @@ object StoreInstallRoot {
val sidecar = StoreGameSidecar.read(folder)
when {
sidecar == null -> Unfinished(store, null, folder)
store == Store.GOG && sidecar.state == StoreGameSidecar.STATE_UPDATING -> null
!sidecar.isInstalled -> Unfinished(store, sidecar.id, folder)
else -> null
}
@@ -81,7 +81,7 @@ object StoresState {
runCatching { com.droiddeck.launcher.stores.battlenet.BattleNetClient.syncAccount(app) }
val found = Store.entries.mapNotNull { store -> StoreAccounts.displayName(app, store)?.let { store to it } }
val onDisk = StoreInstallRoot.gameFolders(app).mapNotNull { folder ->
StoreGameSidecar.read(folder)?.takeIf { it.isInstalled }?.let { InstalledStoreGame(it, folder) }
StoreGameSidecar.read(folder)?.takeIf { it.isInstalled || it.store == Store.GOG && it.state == StoreGameSidecar.STATE_UPDATING }?.let { InstalledStoreGame(it, folder) }
} + runCatching { com.droiddeck.launcher.stores.battlenet.BattleNetBackend.installedGames(app) }.getOrDefault(emptyList())
val partial = StoreInstallRoot.unfinished(app)
val version = if (StoresNative.available) StoresNative.version ?: "" else ""
@@ -95,6 +95,7 @@ object StoresState {
engine = version
accountsLoaded = true
}
com.droiddeck.launcher.stores.gog.GogMaintenance.checkInstalled(app, onDisk)
}
}
}
@@ -31,6 +31,9 @@ data class DownloadEntry(
val stageItemsTotal: Int = 0,
val diskBytes: Long = 0L,
val stagesPassed: Int = 0,
val interruptible: Boolean = true,
val reusedBytes: Long = 0L,
val appliedBytes: Long = 0L,
) {
fun passed(s: DownloadStage): Boolean = state == DownloadState.INSTALLED || (stagesPassed and (1 shl s.ordinal)) != 0
@@ -21,6 +21,11 @@ object DownloadQueue {
fun diskSize(bytes: Long) = update(key) { if (bytes > 0) it.copy(diskBytes = bytes) else it }
fun progress(bytesDone: Long, bytesTotal: Long, detail: String? = null, @Suppress("UNUSED_PARAMETER") speedBps: Long = -1L) = measure(key, bytesDone, bytesTotal, detail?.let(::stripSpeed))
fun log(line: String) = StoresState.logLine(line)
fun interruptible(value: Boolean) = update(key) {
check(value || !cancelled.get()) { "The operation was paused before installer application" }
it.copy(interruptible = value)
}
fun maintenance(reusedBytes: Long, appliedBytes: Long) = update(key) { it.copy(reusedBytes = reusedBytes, appliedBytes = appliedBytes) }
}
private fun stripSpeed(s: String): String = s.replace(Regex("\\s+\\S+ [KMG]B/s\\s*$"), "").trimEnd()
@@ -166,12 +171,13 @@ object DownloadQueue {
fun pause(key: String) {
val item = synchronized(lock) { items[key] } ?: return
synchronized(lock) {
if (!item.entry.interruptible) return
if (item.entry.state == DownloadState.QUEUED) { item.entry = item.entry.copy(state = DownloadState.PAUSED, queuePosition = 0); renumber(); publishLocked(); return }
if (item.entry.state != DownloadState.RUNNING) return
item.pauseRequested = true
item.cancelled.set(true)
item.job?.cancel(deleteFiles = false)
}
item.cancelled.set(true)
item.job?.cancel(deleteFiles = false)
StoresState.logLine("paused \"${item.entry.name}\"")
}
@@ -204,17 +210,19 @@ object DownloadQueue {
val wasRunning: Boolean
synchronized(lock) {
item = items[key] ?: return
if (!item.entry.interruptible) return
wasRunning = item.entry.state == DownloadState.RUNNING
if (wasRunning) {
item.cancelled.set(true)
item.job?.cancel(deleteFiles = true)
}
if (!wasRunning) {
item.entry = item.entry.copy(state = DownloadState.CANCELLED, queuePosition = 0, finishedAt = System.currentTimeMillis())
renumber()
publishLocked()
}
}
if (wasRunning) {
item.cancelled.set(true)
item.job?.cancel(deleteFiles = true)
} else {
if (!wasRunning) {
Thread({ runCatching { item.factory?.invoke()?.cancel(deleteFiles = true) } }, "store-dl-cancel").start()
}
StoresState.logLine("cancelled \"${item.entry.name}\"")
@@ -45,7 +45,7 @@ object SavedQueue {
val f = File(context.filesDir, FILE)
val tmp = File(context.filesDir, "$FILE.tmp")
tmp.writeText(out.toString())
tmp.renameTo(f)
java.nio.file.Files.move(tmp.toPath(), f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING)
}.onFailure { Log.w(TAG, "could not save the queue", it) }
}
}
@@ -68,7 +68,12 @@ object SavedQueue {
for ((item, root, paused) in back) {
if (DownloadQueue.entry(key(item)) != null) continue
if (paused) DownloadQueue.holdPaused(key(item))
StoresState.install(app, item, root)
if (item.store == Store.GOG && item.extra.containsKey(com.droiddeck.launcher.stores.gog.GogMaintenance.OPERATION)) {
Thread({
runCatching { com.droiddeck.launcher.stores.gog.GogMaintenance.restore(app, item) }
.onFailure { DownloadQueue.recordFailed(app, DownloadEntry(item.store, item.id, item.title), it.message ?: "GOG recovery failed") }
}, "gog-restore").start()
} else StoresState.install(app, item, root)
}
}
}, "store-queue-restore").start()
@@ -33,6 +33,7 @@ object GogAuth {
@Synchronized
private fun saveAccount(context: Context, accessToken: String, refreshToken: String?, userId: String?) {
GogMaintenance.reset()
var username = ""
runCatching {
val body = StoreNet.get("https://embed.gog.com/userData.json", bearer = accessToken, userAgent = GALAXY_UA)
@@ -28,6 +28,7 @@ object GogBackend : StoreBackend {
}
override fun signOut(context: Context) {
GogMaintenance.reset()
GogPrefs.get(context).edit().remove("library_cache").remove("library_cache_v2").remove("library_synced_at").apply()
}
@@ -53,7 +54,11 @@ object GogBackend : StoreBackend {
StoresState.post {
StoresState.status.remove(store)
when (result) {
is GogLibrary.SyncResult.Ok -> { StoresState.library[store] = com.droiddeck.launcher.stores.mergeItems(StoresState.library[store], result.games.map { GogLibrary.toCatalogItem(app, it) }); StoresState.problems.remove(store) }
is GogLibrary.SyncResult.Ok -> {
StoresState.library[store] = com.droiddeck.launcher.stores.mergeItems(StoresState.library[store], result.games.map { GogLibrary.toCatalogItem(app, it) })
StoresState.problems.remove(store)
GogMaintenance.checkInstalled(app, StoresState.installed)
}
is GogLibrary.SyncResult.Failed -> StoresState.problems[store] = result.message
GogLibrary.SyncResult.NotLoggedIn -> StoresState.problems[store] = StoresState.notSignedInLine(store)
GogLibrary.SyncResult.Busy -> {}
@@ -0,0 +1,187 @@
package com.droiddeck.launcher.stores.gog
import android.content.Context
import com.droiddeck.launcher.stores.StoreLanguage
import com.droiddeck.launcher.stores.StoreNet
import org.json.JSONArray
import org.json.JSONObject
import java.net.HttpURLConnection
import java.net.URL
import java.util.zip.GZIPInputStream
import java.util.zip.InflaterInputStream
internal object GogContent {
const val SYSTEM = "https://content-system.gog.com"
const val CDN = "https://gog-cdn-fastly.gog.com/content-system"
fun token(context: Context) = GogAuth.validToken(context) ?: error("Sign in to GOG and retry")
fun builds(id: String, token: String): JSONObject {
val result = json("$SYSTEM/products/$id/os/windows/builds?generation=2", token)
if (result.optJSONArray("items")?.length() != 0) return result
return json("$SYSTEM/products/$id/os/windows/builds?generation=1", token)
}
fun product(id: String, token: String): JSONObject = json("https://api.gog.com/products/$id?expand=downloads", token)
fun downloadFilename(url: String): String = java.net.URLDecoder.decode(URL(url).path.substringAfterLast('/').replace("+", "%2B"), "UTF-8")
fun resolveDownload(download: GogOfflineDownload, token: String): GogOfflineDownload = download.copy(parts = download.parts.map { part ->
val resolved = GogDownloadManager.resolveRedirect(part.url, token) ?: error("The GOG download link is unavailable")
val connection = URL(resolved).openConnection() as HttpURLConnection
try {
connection.requestMethod = "HEAD"
connection.connectTimeout = 20_000; connection.readTimeout = 30_000
check(connection.responseCode in 200..299) { "GOG download metadata failed (HTTP ${connection.responseCode})" }
val disposition = connection.getHeaderField("Content-Disposition").orEmpty()
val supplied = Regex("filename=\"?([^\";]+)", RegexOption.IGNORE_CASE).find(disposition)?.groupValues?.get(1).orEmpty()
val remote = downloadFilename(resolved).takeIf { it.endsWith(".exe", true) || it.endsWith(".bin", true) }.orEmpty()
val name = part.name.ifBlank { supplied.ifBlank { remote } }
check(name.isNotBlank()) { "GOG did not provide the installer part filename" }
part.copy(name = name, size = connection.contentLengthLong.coerceAtLeast(0))
} finally { connection.disconnect() }
})
fun json(url: String, token: String? = null): JSONObject {
require(URL(url).protocol == "https") { "GOG metadata requires HTTPS" }
val connection = URL(url).openConnection() as HttpURLConnection
try {
connection.connectTimeout = 20_000; connection.readTimeout = 30_000
connection.setRequestProperty("User-Agent", GogAuth.GALAXY_UA)
if (token != null && URL(url).host.let { it == "gog.com" || it.endsWith(".gog.com") }) connection.setRequestProperty("Authorization", "Bearer $token")
check(connection.responseCode in 200..299) { "GOG metadata request failed (HTTP ${connection.responseCode})" }
val raw = connection.inputStream.readBytes()
val text = when {
raw.size > 1 && raw[0] == 0x78.toByte() -> InflaterInputStream(raw.inputStream()).bufferedReader().use { it.readText() }
raw.size > 1 && raw[0] == 0x1f.toByte() && raw[1] == 0x8b.toByte() -> GZIPInputStream(raw.inputStream()).bufferedReader().use { it.readText() }
else -> raw.toString(Charsets.UTF_8)
}
return JSONObject(text)
} finally { connection.disconnect() }
}
fun snapshot(old: GogInstallRecord, build: GogBuild, token: String,
metadata: (String, String?) -> JSONObject = ::json): GogInstallRecord {
check(build.link.isNotBlank()) { "GOG build manifest is unavailable" }
val manifest = metadata(build.link, token)
val files = JSONArray()
val depots = if (build.generation == 1) manifest.optJSONObject("product")?.optJSONArray("depots") ?: manifest.optJSONArray("depot") else manifest.optJSONArray("depots")
check(depots != null) { "GOG build has no depot list" }
val resolvedLanguage = StoreLanguage.pick(StoreLanguage.codesFromDepots(depots), old.language)
val selectedBitness = if (depots.objects().any { it.optJSONArray("osBitness")?.strings()?.contains("64") == true }) "64" else "32"
val owned = if (old.products.any { it != old.id }) StoreNet.get("https://embed.gog.com/user/data/games", bearer = token)?.let { JSONObject(it).optJSONArray("owned")?.strings()?.toSet() } else emptySet()
val products = old.products.filter { it == old.id || owned?.contains(it) == true }
check(old.products.all { it == old.id || owned?.contains(it) == true }) { "Could not confirm ownership of installed GOG DLC" }
for (depot in depots.objects()) {
if (depot.has("redist")) continue
val ids = depot.optJSONArray("gameIDs")?.strings() ?: listOf(depot.optString("productId", old.id))
val product = ids.firstOrNull { it in products } ?: continue
val languages = depot.optJSONArray("languages")?.strings().orEmpty()
if (!languageSelected(languages, resolvedLanguage)) continue
val bitness = depot.optJSONArray("osBitness")?.strings().orEmpty()
if (bitness.isNotEmpty() && selectedBitness !in bitness) continue
val hash = depot.optString("manifest")
val content = when {
hash.isNotEmpty() && build.generation == 2 -> metadata("$CDN/v2/meta/${cdnPath(hash)}", null)
hash.isNotEmpty() -> {
val timestamp = manifest.getJSONObject("product").getString("timestamp")
metadata("$CDN/v1/manifests/$product/windows/$timestamp/$hash", token)
}
depot.has("files") -> JSONObject().put("depot", depot)
else -> error("GOG depot manifest is missing")
}
val entries = content.getJSONObject("depot").optJSONArray(if (build.generation == 2) "items" else "files") ?: error("GOG depot files are missing")
for (entry in entries.objects()) {
val type = entry.optString("type")
check(type != "DepotLink") { "This GOG build contains unsupported file links" }
val support = depot.optBoolean("support") || entry.optBoolean("support") || entry.optJSONArray("flags")?.strings()?.contains("support") == true
val relative = entry.getString("path").replace('\\', '/').trimEnd('/').let { if (build.generation == 1) it.trimStart('/') else it }
val path = if (support) (if (build.generation == 1) "support/$relative" else "support/$product/$relative") else relative
val directory = type == "DepotDirectory" || entry.has("directory")
val chunks = entry.optJSONArray("chunks") ?: JSONArray()
val size = if (build.generation == 2) chunks.objects().sumOf { it.optLong("size") } else entry.optLong("size")
files.put(JSONObject().put("path", path).put("size", size).put("directory", directory)
.put("md5", entry.optString("md5").ifBlank { entry.optString("hash") }.takeIf { it.matches(Regex("[a-fA-F0-9]{32}")) }.orEmpty())
.put("sha256", entry.optString("sha256")).put("chunks", chunks).put("product", product)
.put("offset", entry.optLong("offset")).put("remotePath", entry.optString("url").substringBefore('?')))
}
}
check(files.length() > 0) { "GOG build contains no matching files" }
return old.copy(source = "depot", generation = build.generation, build = build.id, version = build.version,
branch = build.branch, language = resolvedLanguage, manifest = sanitize(manifest), files = files, products = products, verified = true)
}
fun languageSelected(list: List<String>, selected: String): Boolean = list.any { it.equals("Neutral", true) } || StoreLanguage.depotMatchesResolved(list.toTypedArray(), selected)
fun cdnPath(hash: String): String {
require(hash.matches(Regex("[a-fA-F0-9]{32,64}"))) { "Invalid GOG content hash" }
return "${hash.take(2)}/${hash.substring(2, 4)}/$hash"
}
fun links(record: GogInstallRecord, token: String, patches: Boolean = false): JSONObject {
val out = JSONObject()
for (id in record.products) {
val path = if (record.generation == 1) "/windows/${record.manifest.optJSONObject("product")?.optString("timestamp")}/" else "/"
val query = "_version=2&" + (if (record.generation == 1) "type=depot" else "generation=2") + "&path=${java.net.URLEncoder.encode(path, "UTF-8")}" + if (patches) "&root=%2Fpatches%2Fstore" else ""
val response = json("$SYSTEM/products/$id/secure_link?$query", token)
val bases = response.optJSONArray("urls")?.objects().orEmpty().mapNotNull { item ->
val params = item.optJSONObject("parameters")
var url = item.optString("url_format")
params?.keys()?.forEach { key -> url = url.replace("{$key}", params.getString(key)) }
url.takeIf { it.startsWith("https://") }
}
check(bases.isNotEmpty()) { "GOG refused a download link" }
out.put(id, JSONArray(bases))
}
return out
}
fun patches(old: GogInstallRecord, target: GogInstallRecord, token: String): JSONArray {
if (old.generation != 2 || target.generation != 2 || old.build.isBlank() || old.build == target.build) return JSONArray()
return runCatching {
val index = json("$SYSTEM/products/${old.id}/patches?_version=4&from_build_id=${old.build}&to_build_id=${target.build}", token)
val meta = json(index.getString("link"), token)
if (meta.optString("algorithm") != "xdelta3") return@runCatching JSONArray()
val result = JSONArray()
for (depot in meta.getJSONArray("depots").objects()) {
if (depot.optString("productId") !in target.products || !languageSelected(depot.optJSONArray("languages")?.strings().orEmpty(), target.language)) continue
val data = json("$CDN/v2/patches/meta/${cdnPath(depot.getString("manifest"))}")
for (item in data.getJSONObject("depot").getJSONArray("items").objects()) if (item.optString("type") == "DepotDiff")
result.put(JSONObject(item.toString()).put("product", depot.getString("productId")))
}
result
}.getOrElse { JSONArray() }
}
fun setupFiles(record: GogInstallRecord, token: String): Pair<JSONArray, JSONObject>? {
if (!record.manifest.optBoolean("scriptInterpreter")) return null
if (record.files.objects().none { file -> record.products.any { file.optString("path").substringAfterLast('/') == "goggame-$it.script" } }) return null
val index = json("$SYSTEM/dependencies/repository?generation=2")
val repository = json(index.getString("repository_manifest"))
val depot = repository.getJSONArray("depots").objects().firstOrNull { it.optString("dependencyId") == "ISI" }
?: error("GOG setup interpreter is unavailable")
val content = json("$CDN/v2/dependencies/meta/${cdnPath(depot.getString("manifest"))}")
val files = JSONArray()
for (entry in content.getJSONObject("depot").getJSONArray("items").objects()) {
if (entry.optString("type") != "DepotFile") continue
val chunks = entry.getJSONArray("chunks")
files.put(JSONObject().put("path", "support/" + entry.getString("path").replace('\\', '/'))
.put("size", chunks.objects().sumOf { it.getLong("size") }).put("md5", entry.optString("md5"))
.put("chunks", chunks).put("product", "redist"))
}
val response = json("$SYSTEM/open_link?generation=2&_version=2&path=%2Fdependencies%2Fstore%2F", token)
val bases = GogDownloadManager.parseCdnUrls(response.toString())
check(bases.isNotEmpty()) { "GOG setup download links are unavailable" }
return files to JSONObject().put("redist", JSONArray(bases))
}
fun sanitize(value: JSONObject): JSONObject {
val result = JSONObject()
for (key in value.keys()) {
if (key.lowercase() in setOf("clientsecret", "access_token", "refresh_token", "link", "urls", "url")) continue
val v = value.get(key)
result.put(key, when (v) { is JSONObject -> sanitize(v); is JSONArray -> JSONArray((0 until v.length()).map { i -> v.get(i).let { if (it is JSONObject) sanitize(it) else it } }); else -> v })
}
return result
}
}
@@ -106,7 +106,7 @@ public final class GogDownloadManager {
String builds1Json = httpGet(builds1Url, null);
if (builds1Json == null) builds1Json = httpGet(builds1Url, token);
if (builds1Json == null) throw new InstallException("No builds available for this game");
Gen1Outcome one = runGen1(ctx, game, token, builds1Json, installPath, cb, cancelled);
Gen1Outcome one = runGen1(ctx, game, token, builds1Json, installPath, cb, cancelled, lang);
if (one.result != null || cancelled.get()) return one.result;
if (!"NO_CS_BUILDS".equals(one.error)) throw new InstallException("Download failed: " + one.error);
cb.onProgress("No Galaxy builds; fetching the installer…", 12);
@@ -250,6 +250,11 @@ public final class GogDownloadManager {
if (anyFailed.get()) { out.error = "one or more chunks failed to download"; return out; }
cb.onProgress("Finishing…", 96);
try {
GogMaintenance.captureDepot(game.gameId, installPath, gm.buildId, buildsJson, manifest, new JSONArray(depotJsons), resolved);
} catch (Exception e) {
cb.onLog("gog: maintenance inventory not recorded: " + e.getMessage());
}
writeFile(new File(installPath, MARKER), ("{\"gameId\":\"" + game.gameId + "\",\"buildId\":\"" + gm.buildId + "\"}").getBytes("UTF-8"));
deleteCounted(new File(installPath, ".gog_chunks"), cb);
String clientId = manifest.optString("clientId", null);
@@ -279,7 +284,7 @@ public final class GogDownloadManager {
String buildId = null, manifestUrl = null;
for (int i = 0; i < items.length(); i++) {
JSONObject item = items.getJSONObject(i);
if ("windows".equals(item.optString("os"))) {
if ("windows".equals(item.optString("os")) && (item.isNull("branch") || item.optString("branch").isEmpty())) {
buildId = item.optString("build_id");
manifestUrl = item.optString("link");
if (manifestUrl == null || manifestUrl.isEmpty()) manifestUrl = item.optString("meta_url");
@@ -328,7 +333,7 @@ public final class GogDownloadManager {
private static final class Gen1Outcome { Result result; String error; }
private static Gen1Outcome runGen1(Context ctx, GogGame game, String token, String buildsJson, File installPath, Callback cb, AtomicBoolean cancelled) {
private static Gen1Outcome runGen1(Context ctx, GogGame game, String token, String buildsJson, File installPath, Callback cb, AtomicBoolean cancelled, String language) {
Gen1Outcome out = new Gen1Outcome();
try {
JSONObject builds = new JSONObject(buildsJson);
@@ -346,6 +351,15 @@ public final class GogDownloadManager {
String manifestStr = decompressBytes(raw);
if (manifestStr == null) { out.error = "manifest decompress failed"; return out; }
JSONObject manifest = new JSONObject(manifestStr);
if (manifest.has("product")) {
try {
out.result = GogMaintenance.installContent(ctx, game, installPath, builds, language, cb, cancelled);
if (out.result == null && !cancelled.get()) out.error = "no usable public Windows build";
} catch (Exception e) {
out.error = e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage();
}
return out;
}
JSONArray depots = manifest.optJSONArray("depot");
if (depots == null) { out.error = "no depot array"; return out; }
List<Gen1File> files = new ArrayList<>();
@@ -472,6 +486,7 @@ public final class GogDownloadManager {
String exe = pickExe(installPath, null, game.title);
if (exe.isEmpty()) throw new InstallException("The installer was extracted but no game executable was found");
cb.onLog("gog: installer extracted natively; game exe " + exe);
GogMaintenance.captureOffline(game.gameId, installPath, downloads);
return new Result(exe, "", total);
} catch (InstallerException e) {
throw e;
@@ -576,13 +591,13 @@ public final class GogDownloadManager {
return cleaned.isEmpty() ? "game" : cleaned;
}
private static String resolveRedirect(String url, String token) {
static String resolveRedirect(String url, String token) {
try {
if (url.startsWith("/")) url = "https://www.gog.com" + url;
for (int hop = 0; hop < 5; hop++) {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setConnectTimeout(TIMEOUT); conn.setReadTimeout(TIMEOUT); conn.setInstanceFollowRedirects(false);
if (token != null) conn.setRequestProperty("Authorization", "Bearer " + token);
if (token != null && (new URL(url).getHost().equals("gog.com") || new URL(url).getHost().endsWith(".gog.com"))) conn.setRequestProperty("Authorization", "Bearer " + token);
int code = conn.getResponseCode();
String location = conn.getHeaderField("Location");
if (code == 301 || code == 302 || code == 303 || code == 307 || code == 308) {
@@ -686,6 +701,7 @@ public final class GogDownloadManager {
cb.onBytes(shown, planned, speedBps.get());
}
@Override public void onLog(String line) { cb.onLog(line); }
@Override public void onMaintenance(long networkBytes, long reusedBytes, long appliedBytes) {}
@Override public void onComplete(boolean success, boolean wasCancelled, boolean linkExpiry, String error, long bytesWritten, int filesDone) {
okRef.set(success); linkExpiryRef.set(linkExpiry); errRef.set(error == null ? "" : error); latch.countDown();
}
@@ -0,0 +1,58 @@
package com.droiddeck.launcher.stores.gog
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
import java.io.RandomAccessFile
import java.security.MessageDigest
internal object GogEstimate {
data class Size(val download: Long, val required: Long)
private data class Chunk(val file: File, val offset: Long)
fun range(file: File, offset: Long, size: Long, md5: String): Boolean {
if (!file.isFile || offset < 0 || size < 0 || file.length() < offset + size || !md5.matches(Regex("[a-fA-F0-9]{32}"))) return false
return runCatching {
RandomAccessFile(file, "r").use { input ->
input.seek(offset)
val digest = MessageDigest.getInstance("MD5"); val buffer = ByteArray(64 * 1024); var left = size
while (left > 0) { val n = input.read(buffer, 0, minOf(left, buffer.size.toLong()).toInt()); check(n > 0); digest.update(buffer, 0, n); left -= n }
digest.digest().joinToString("") { "%02x".format(it) }.equals(md5, true)
}
}.getOrDefault(false)
}
fun calculate(folder: File, old: GogInstallRecord, target: GogInstallRecord, patches: JSONArray, reuseChunks: Boolean = true): Size {
val reusable = HashMap<Pair<String, Long>, MutableList<Chunk>>()
for (file in old.files.objects()) {
val source = GogFiles.inside(folder, file.getString("path")); var offset = 0L
for (chunk in file.optJSONArray("chunks")?.objects().orEmpty()) {
reusable.getOrPut(chunk.optString("md5").lowercase() to chunk.optLong("size")) { ArrayList() }.add(Chunk(source, offset))
offset += chunk.optLong("size")
}
}
var download = 0L; var required = 0L
for (file in target.files.objects()) {
if (file.optBoolean("directory") || GogFiles.matches(folder, file)) continue
val chunks = file.optJSONArray("chunks")?.objects().orEmpty()
val stamp = MessageDigest.getInstance("MD5").digest(file.getString("path").toByteArray()).joinToString("") { "%02x".format(it) }
val stage = File(GogFiles.dir(folder), "stage/$stamp")
var offset = 0L
var cost = if (chunks.isEmpty()) file.getLong("size") else chunks.sumOf { chunk ->
val size = chunk.getLong("size"); val hash = chunk.getString("md5")
val exists = reuseChunks && (range(stage, offset, size, hash) || reusable[hash.lowercase() to size].orEmpty().any { range(it.file, it.offset, size, hash) })
offset += size
if (exists) 0L else chunk.optLong("compressedSize")
}
val patch = patches.objects().firstOrNull { it.optString("path_target").replace('\\', '/') == file.getString("path") }
if (reuseChunks && patch != null) {
val source = GogFiles.inside(folder, patch.getString("path_source").replace('\\', '/'))
val delta = patch.getJSONArray("chunks").objects().sumOf { it.optLong("compressedSize") }
if (delta < cost && source.isFile && GogFiles.hash(source).equals(patch.getString("md5_source"), true)) cost = delta
}
download = Math.addExact(download, cost)
required = Math.addExact(required, maxOf(0, file.getLong("size") - stage.length()) + cost)
}
return Size(download, required)
}
}
@@ -0,0 +1,115 @@
package com.droiddeck.launcher.stores.gog
import com.droiddeck.launcher.stores.download.DownloadQueue
import com.droiddeck.launcher.stores.download.DownloadStage
import org.json.JSONObject
import java.io.File
import java.io.FileOutputStream
import java.net.HttpURLConnection
import java.net.URL
import java.util.concurrent.atomic.AtomicBoolean
import java.util.zip.InflaterInputStream
internal object GogFallback {
fun transfer(folder: File, plan: JSONObject, handle: DownloadQueue.JobHandle?, stop: AtomicBoolean,
callback: GogDownloadManager.Callback? = null) {
val stageRoot = File(GogFiles.dir(folder), "stage").apply { mkdirs() }
val files = plan.getJSONArray("files")
GogFiles.validate(files, folder)
var wire = 0L
fun progress(n: Int) { wire += n; handle?.progress(wire, plan.optLong("downloadBytes").coerceAtLeast(wire)); callback?.onBytes(wire, plan.optLong("downloadBytes").coerceAtLeast(wire), 0) }
for ((index, item) in files.objects().withIndex()) {
if (stop.get()) return
val destination = GogFiles.inside(folder, item.getString("path"))
if (item.optBoolean("directory")) { java.nio.file.Files.createDirectories(destination.toPath()); continue }
if (GogFiles.matches(folder, item)) continue
val stage = File(stageRoot, java.security.MessageDigest.getInstance("MD5").digest(item.getString("path").toByteArray()).joinToString("") { "%02x".format(it) })
val chunks = item.optJSONArray("chunks")?.objects().orEmpty()
FileOutputStream(stage).use { output ->
if (chunks.isNotEmpty()) {
for (chunk in chunks) {
if (stop.get()) return
val packed = File(stageRoot, "${stage.name}.packed")
val urls = endpoints(plan.getJSONObject("links"), item.getString("product"), GogContent.cdnPath(chunk.getString("compressedMd5")))
download(urls, packed, chunk.getLong("compressedSize"), stop, ::progress)
check(GogFiles.hash(packed).equals(chunk.getString("compressedMd5"), true)) { "GOG compressed chunk checksum mismatch" }
val digest = java.security.MessageDigest.getInstance("MD5")
var count = 0L
packed.inputStream().buffered().use { raw ->
raw.mark(2); val first = raw.read(); raw.reset()
val stored = chunk.getLong("size") == chunk.getLong("compressedSize") && chunk.getString("md5").equals(chunk.getString("compressedMd5"), true)
val input = if (!stored && first == 0x78) InflaterInputStream(raw) else raw
val buffer = ByteArray(64 * 1024)
while (true) {
if (stop.get()) return
val n = input.read(buffer); if (n < 0) break
count += n; check(count <= chunk.getLong("size")) { "GOG chunk exceeds expected size" }
digest.update(buffer, 0, n); output.write(buffer, 0, n)
}
}
check(count == chunk.getLong("size") && digest.digest().joinToString("") { "%02x".format(it) }.equals(chunk.getString("md5"), true)) { "GOG chunk checksum mismatch" }
packed.delete()
}
} else if (item.getLong("size") > 0) {
val remote = item.getString("remotePath")
val urls = if (remote.startsWith("https://")) listOf(remote) else endpoints(plan.getJSONObject("links"), item.getString("product"), remote.trimStart('/'))
val range = item.getLong("offset")
val raw = File(stageRoot, "${stage.name}.range")
download(urls, raw, item.getLong("size"), stop, ::progress, range)
raw.inputStream().use { it.copyTo(output) }; raw.delete()
}
output.fd.sync()
}
if (stop.get()) return
check(stage.length() == item.getLong("size")) { "GOG file size mismatch" }
if (item.optString("md5").isNotBlank()) check(GogFiles.hash(stage).equals(item.getString("md5"), true)) { "GOG file checksum mismatch" }
if (item.optString("sha256").isNotBlank()) check(GogFiles.hash(stage, "SHA-256").equals(item.getString("sha256"), true)) { "GOG file checksum mismatch" }
destination.parentFile!!.mkdirs()
GogFiles.inside(folder, item.getString("path"))
GogFiles.replace(stage, destination)
handle?.stage(DownloadStage.INSTALL); handle?.stageProgress((index + 1).toLong(), files.length().toLong())
callback?.onProgress("Downloading: ${destination.name}", ((index + 1) * 100 / files.length()))
}
val targetPaths = files.objects().mapTo(HashSet()) { it.getString("path").lowercase(java.util.Locale.ROOT) }
for (old in plan.optJSONArray("oldFiles")?.objects().orEmpty()) {
if (stop.get()) return
if (old.getString("path").lowercase(java.util.Locale.ROOT) !in targetPaths && GogFiles.matches(folder, old))
check(GogFiles.inside(folder, old.getString("path")).delete()) { "Could not remove obsolete GOG file" }
}
}
fun endpoints(links: JSONObject, product: String, path: String): List<String> = links.getJSONArray(product).strings().map { base ->
if (base.contains("{GALAXY_PATH}")) base.replace("{GALAXY_PATH}", path)
else base.substringBefore('?').trimEnd('/') + "/" + path + if ('?' in base) "?" + base.substringAfter('?') else ""
}
fun download(urls: List<String>, destination: File, size: Long, stop: AtomicBoolean, progress: (Int) -> Unit, range: Long? = null) {
var failure: Throwable? = null
for (attempt in 0..2) for (url in urls) {
if (stop.get()) return
val connection = URL(url).openConnection() as HttpURLConnection
try {
check(URL(url).protocol == "https" || URL(url).host in setOf("127.0.0.1", "localhost"))
connection.connectTimeout = 20_000; connection.readTimeout = 30_000
if (range != null) connection.setRequestProperty("Range", "bytes=$range-${range + size - 1}")
check(connection.responseCode == if (range == null) 200 else 206) { "GOG download failed (HTTP ${connection.responseCode})" }
FileOutputStream(destination).use { output ->
connection.inputStream.use { input ->
val buffer = ByteArray(64 * 1024); var count = 0L
while (true) {
if (stop.get()) return
val n = input.read(buffer); if (n < 0) break
count += n; check(size <= 0 || count <= size) { "GOG response exceeds expected size" }
output.write(buffer, 0, n); progress(n)
}
check(size <= 0 || count == size) { "Incomplete GOG download" }
}
output.fd.sync()
}
return
} catch (e: Exception) { failure = e }
finally { connection.disconnect() }
}
throw failure ?: IllegalStateException("No GOG download URL")
}
}
@@ -0,0 +1,176 @@
package com.droiddeck.launcher.stores.gog
import com.droiddeck.launcher.stores.StoreGameSidecar
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
import java.io.FileOutputStream
import java.nio.file.Files
import java.nio.file.LinkOption
import java.nio.file.StandardCopyOption
import java.security.MessageDigest
import java.util.Locale
internal enum class GogOperation { UPDATE, REPAIR, PATCH, REFRESH }
internal data class GogInstallRecord(
val id: String,
val source: String,
val generation: Int = 0,
val build: String = "",
val version: String = "",
val branch: String = "",
val language: String = "en-US",
val products: List<String> = listOf(id),
val manifest: JSONObject = JSONObject(),
val files: JSONArray = JSONArray(),
val verified: Boolean = true,
) {
fun json() = JSONObject().put("schema", 1).put("id", id).put("source", source)
.put("generation", generation).put("build", build).put("version", version).put("branch", branch)
.put("language", language).put("products", JSONArray(products)).put("manifest", manifest)
.put("files", files).put("verified", verified)
fun write(folder: File) = GogFiles.write(File(GogFiles.dir(folder), "installed.json"), json())
companion object {
fun parse(o: JSONObject): GogInstallRecord {
require(o.optInt("schema") == 1) { "Unsupported GOG installation record" }
return GogInstallRecord(o.getString("id"), o.getString("source"), o.optInt("generation"),
o.optString("build"), o.optString("version"), o.optString("branch"), o.optString("language", "en-US"),
o.optJSONArray("products")?.strings().orEmpty().ifEmpty { listOf(o.getString("id")) },
o.optJSONObject("manifest") ?: JSONObject(), o.optJSONArray("files") ?: JSONArray(), o.optBoolean("verified", true))
}
fun read(folder: File): GogInstallRecord? = GogFiles.read(File(GogFiles.dir(folder), "installed.json"))?.let(::parse)
fun authenticBuild(folder: File, id: String): String = GogFiles.read(File(folder, "goggame-$id.id"))?.optString("buildId").orEmpty()
.ifBlank { GogFiles.read(File(folder, "goggame-$id.info"))?.optString("buildId").orEmpty() }
fun migrate(folder: File, sidecar: StoreGameSidecar): GogInstallRecord {
read(folder)?.let { require(it.id == sidecar.id); return it }
val legacy = GogFiles.read(File(folder, "_gog_manifest.json"))
val info = GogFiles.read(File(folder, "goggame-${sidecar.id}.info"))
val build = authenticBuild(folder, sidecar.id).ifBlank { legacy?.optString("buildId").orEmpty().ifBlank { sidecar.installVersion } }
val ids = folder.listFiles().orEmpty().mapNotNull { Regex("goggame-([0-9]+)\\.info").matchEntire(it.name)?.groupValues?.get(1) }
return GogInstallRecord(sidecar.id, if (legacy != null || sidecar.installVersion.isNotBlank()) "depot" else "offline",
legacy?.optInt("generation", if (build.isNotBlank()) 2 else 0) ?: if (build.isNotBlank()) 2 else 0,
build, info?.optString("versionName").orEmpty(), language = sidecar.extra[com.droiddeck.launcher.stores.StoreLanguage.EXTRA] ?: "en-US",
products = (listOf(sidecar.id) + ids).distinct()).also { it.write(folder) }
}
}
}
internal object GogFiles {
fun dir(folder: File): File = inside(folder, ".droiddeck-gog").apply { mkdirs() }
fun journal(folder: File): File = File(dir(folder), "operation.json")
fun inside(root: File, raw: String): File {
val path = raw.replace('\\', '/')
require(path.isNotBlank() && !path.startsWith('/') && ':' !in path && '\u0000' !in path &&
path.split('/').none { it.isEmpty() || it == "." || it == ".." }) { "Invalid GOG path: $raw" }
val base = root.canonicalFile
var out = base
for (part in path.split('/')) {
out = File(out, part)
require(!Files.isSymbolicLink(out.toPath())) { "GOG path is a symbolic link: $raw" }
}
require(out.canonicalFile.toPath().startsWith(base.toPath())) { "GOG path leaves installation" }
return out
}
fun read(file: File): JSONObject? {
require(!Files.isSymbolicLink(file.toPath())) { "GOG metadata is a symbolic link" }
return if (!file.exists()) null else JSONObject(file.readText())
}
fun write(file: File, value: JSONObject) {
require(!Files.isSymbolicLink(file.toPath()))
file.parentFile!!.mkdirs()
val tmp = File.createTempFile(".gog-", ".stage", file.parentFile)
try {
FileOutputStream(tmp).use { it.write(value.toString().toByteArray(Charsets.UTF_8)); it.fd.sync() }
replace(tmp, file)
} finally { tmp.delete() }
}
fun replace(from: File, to: File) {
require(!Files.isSymbolicLink(to.toPath()))
try { Files.move(from.toPath(), to.toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING) }
catch (_: java.nio.file.AtomicMoveNotSupportedException) { Files.move(from.toPath(), to.toPath(), StandardCopyOption.REPLACE_EXISTING) }
}
fun hash(file: File, algorithm: String = "MD5"): String {
val digest = MessageDigest.getInstance(algorithm)
file.inputStream().use { input -> val buffer = ByteArray(128 * 1024); while (true) { val n = input.read(buffer); if (n < 0) break; digest.update(buffer, 0, n) } }
return digest.digest().joinToString("") { "%02x".format(it) }
}
fun matches(folder: File, file: JSONObject): Boolean {
val f = inside(folder, file.getString("path"))
if (!Files.isRegularFile(f.toPath(), LinkOption.NOFOLLOW_LINKS) || f.length() != file.getLong("size")) return false
val md5 = file.optString("md5")
if (md5.matches(Regex("[a-fA-F0-9]{32}"))) return hash(f).equals(md5, true)
val sha = file.optString("sha256")
if (sha.isNotBlank()) return hash(f, "SHA-256").equals(sha, true)
val chunks = file.optJSONArray("chunks")?.objects().orEmpty()
var offset = 0L
return chunks.isNotEmpty() && chunks.all { chunk ->
val result = GogEstimate.range(f, offset, chunk.getLong("size"), chunk.getString("md5"))
offset += chunk.getLong("size"); result
} || f.length() == 0L
}
fun validate(files: JSONArray, folder: File) {
val paths = HashSet<String>()
val components = HashMap<String, String>()
for (file in files.objects()) {
val path = file.getString("path").replace('\\', '/')
inside(folder, path)
require(!path.startsWith(".droiddeck-", true) && path != StoreGameSidecar.FILE_NAME) { "GOG content overlaps launcher metadata" }
require(paths.add(path.lowercase(Locale.ROOT))) { "GOG paths differ only by case: $path" }
var parent = folder
var relative = ""
for (part in path.split('/')) {
relative = if (relative.isEmpty()) part else "$relative/$part"
val previous = components.putIfAbsent(relative.lowercase(Locale.ROOT), relative)
require(previous == null || previous == relative) { "GOG directory paths differ only by case: $path" }
require(parent.list()?.none { it != part && it.equals(part, true) } != false) { "An existing path differs only by case: $path" }
parent = File(parent, part)
}
}
}
}
internal fun JSONArray.objects(): List<JSONObject> = (0 until length()).map { getJSONObject(it) }
internal fun JSONArray.strings(): List<String> = (0 until length()).map { optString(it) }
internal data class GogBuild(val id: String, val version: String, val generation: Int, val branch: String, val link: String) {
companion object {
fun select(builds: JSONObject, pinned: String = "", branch: String = ""): GogBuild? {
val items = builds.optJSONArray("items")?.objects().orEmpty().filter { it.optString("os", "windows") == "windows" }
val item = if (pinned.isNotBlank()) items.firstOrNull { it.optString("build_id") == pinned || it.optString("legacy_build_id") == pinned }
else items.firstOrNull { it.optString("branch").let { b -> (if (b == "null") "" else b) == branch } }
return item?.let { GogBuild(it.optString("build_id"), it.optString("version_name"), it.optInt("generation", 2),
it.optString("branch").let { b -> if (b == "null") "" else b },
it.optString("link").ifBlank { it.optString("meta_url").ifBlank { it.optJSONArray("urls")?.optJSONObject(0)?.optString("url").orEmpty() } }) }
}
}
}
internal data class GogOfflinePart(val id: String, val name: String, val url: String, val size: Long, val md5: String)
internal data class GogOfflineDownload(val id: String, val name: String, val version: String, val language: String, val parts: List<GogOfflinePart>) {
val size: Long get() = parts.sumOf { it.size }
companion object {
fun parse(downloads: JSONObject, kind: String): List<GogOfflineDownload> = downloads.optJSONArray(kind)?.objects().orEmpty()
.filter { it.optString("os") == "windows" }.mapNotNull { entry ->
val files = entry.optJSONArray("files")?.objects().orEmpty()
val parts = files.mapNotNull { f ->
val url = f.optString("downlink").ifBlank { f.optString("manualUrl") }
if (url.isBlank()) null else GogOfflinePart(f.optString("id"), f.optString("filename"), url, f.optLong("size"), f.optString("md5"))
}
if (parts.isEmpty()) null else GogOfflineDownload(entry.optString("id").ifBlank { parts.first().id },
entry.optString("name", "GOG download"), entry.optString("version"), entry.optString("language"), parts)
}
}
}
@@ -0,0 +1,415 @@
package com.droiddeck.launcher.stores.gog
import android.content.Context
import androidx.compose.runtime.mutableStateMapOf
import com.droiddeck.launcher.agent.AgentGuest
import com.droiddeck.launcher.frontend.AddedGames
import com.droiddeck.launcher.runtime.GuestCommand
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.session.SessionState
import com.droiddeck.launcher.stores.*
import com.droiddeck.launcher.stores.download.*
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
import java.io.RandomAccessFile
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
internal object GogMaintenance {
enum class State { UNKNOWN, CHECKING, CURRENT, AVAILABLE, FAILED }
data class Check(val state: State, val version: String = "", val reason: String = "")
data class Proposal(val folder: File, val before: GogInstallRecord, val target: GogInstallRecord,
val operation: GogOperation, val downloadBytes: Long, val diskBytes: Long, val patch: GogOfflineDownload? = null, val deltas: JSONArray = JSONArray(), val fullFiles: Boolean = false)
val checks = mutableStateMapOf<String, Check>()
private val workers = Executors.newFixedThreadPool(2)
private val checkedAt = java.util.concurrent.ConcurrentHashMap<String, Long>()
private val inFlight = java.util.concurrent.ConcurrentHashMap.newKeySet<String>()
@Volatile private var account = ""
private val revision = java.util.concurrent.atomic.AtomicLong()
private const val HELPER = "/usr/local/bin/droiddeck-gog-maintenance"
const val OPERATION = "gogOperation"
const val FOLDER = "gogFolder"
fun reset() {
revision.incrementAndGet()
account = ""; checkedAt.clear()
StoresState.post { checks.clear() }
}
fun checkInstalled(context: Context, games: List<InstalledStoreGame>) {
val user = StoreAccounts.displayName(context, Store.GOG).orEmpty()
if (user != account) { reset(); account = user }
if (user.isBlank()) return
games.filter { it.sidecar.store == Store.GOG && it.sidecar.isInstalled }.forEach { check(context, it) }
}
fun check(context: Context, game: InstalledStoreGame, force: Boolean = false) {
val key = game.folder.absolutePath
if (!force && System.currentTimeMillis() - (checkedAt[key] ?: 0) < 30 * 60_000) return
if (!inFlight.add(key)) return
val owner = revision.get()
StoresState.post { checks[game.sidecar.id] = Check(State.CHECKING) }
workers.execute {
val result = runCatching {
var old = GogInstallRecord.migrate(game.folder, game.sidecar)
if (!old.verified || GogFiles.journal(game.folder).exists()) Check(State.UNKNOWN, reason = "Finish or recover the GOG maintenance operation first")
else {
val token = GogContent.token(context)
if (old.source == "depot") {
val builds = GogContent.builds(old.id, token)
if (old.files.length() == 0 && old.build.isNotBlank()) GogBuild.select(builds, pinned = old.build)?.let { old = old.copy(branch = it.branch, generation = it.generation) }
val latest = GogBuild.select(builds, branch = old.branch)
when { latest == null -> Check(State.UNKNOWN, reason = "No build is available on the installed branch")
old.build.isBlank() -> Check(State.UNKNOWN, latest.version, "The installed build is unknown")
latest.id == old.build -> Check(State.CURRENT, old.version)
else -> Check(State.AVAILABLE, latest.version) }
} else {
val downloads = GogContent.product(old.id, token).getJSONObject("downloads")
val installer = offlineInstaller(downloads, old.language)
if (old.version.isBlank()) Check(State.UNKNOWN, installer.version, "The installed version is unknown")
else Check(if (old.version == installer.version) State.CURRENT else State.AVAILABLE, installer.version)
}
}
}.getOrElse { Check(State.FAILED, reason = it.message ?: "GOG update check failed") }
if (revision.get() == owner && (result.state == State.CURRENT || result.state == State.AVAILABLE)) checkedAt[key] = System.currentTimeMillis()
inFlight.remove(key)
StoresState.post { if (revision.get() == owner) checks[game.sidecar.id] = result }
}
}
fun offlinePatches(context: Context, game: InstalledStoreGame): List<GogOfflineDownload> =
GogOfflineDownload.parse(GogContent.product(game.sidecar.id, GogContent.token(context)).getJSONObject("downloads"), "patches")
internal fun offlineInstaller(downloads: JSONObject, language: String): GogOfflineDownload {
val installers = GogOfflineDownload.parse(downloads, "installers")
return installers.firstOrNull { it.language.equals(language, true) }
?: installers.firstOrNull { !it.language.equals("Neutral", true) && StoreLanguage.depotAccepted(arrayOf(it.language), language) }
?: installers.firstOrNull { it.language.equals("Neutral", true) }
?: error("No Windows offline installer is available for the installed language ($language)")
}
fun prepare(context: Context, game: InstalledStoreGame, operation: GogOperation, patch: GogOfflineDownload? = null, resume: Boolean = false): Proposal {
val folder = game.folder.canonicalFile
var old = GogInstallRecord.migrate(folder, game.sidecar)
val journal = GogFiles.read(GogFiles.journal(folder))
require(journal == null || journal.optString("folder") == folder.path) { "GOG recovery folder does not match" }
if (journal != null && journal.optString("phase") in setOf("patch-starting", "patch-applying", "patch-uncertain") && operation != GogOperation.REFRESH) error("The patch was interrupted. Inspect the installer result or use a full offline refresh; it will not be rerun automatically.")
require(journal == null || operation == GogOperation.REFRESH || journal.optString("operation") == operation.name) { "Resume the existing GOG operation or select a full offline refresh" }
val token = GogContent.token(context)
if (operation == GogOperation.PATCH) {
if (old.source == "depot" && old.manifest.length() == 0 && old.build.isNotBlank()) {
val exact = GogBuild.select(GogContent.builds(old.id, token), pinned = old.build)
?: error("The installed GOG setup manifest is unavailable; use a full offline refresh")
old = GogContent.snapshot(old, exact, token)
}
check(patch != null) { "Select a GOG patch" }
if (resume) require(journal?.optString("downloadVersion") == patch.version) { "The selected patch changed; select it again in Offline patches" }
val selected = GogContent.resolveDownload(patch, token)
return Proposal(folder, old, old.copy(source = "offline", version = selected.version, verified = false), operation, selected.size,
selected.size + folder.walkTopDown().onEnter { !java.nio.file.Files.isSymbolicLink(it.toPath()) && it.name != ".droiddeck-gog" }.filter { it.isFile }.sumOf { it.length() }, selected)
}
if (operation == GogOperation.REFRESH || old.source != "depot") {
require(operation != GogOperation.REPAIR) { "This offline installation has no depot manifest. Use a full offline refresh to restore its files." }
val download = GogContent.resolveDownload(offlineInstaller(GogContent.product(old.id, token).getJSONObject("downloads"), old.language), token)
if (resume) require(journal?.optString("downloadId") == download.id && journal.optString("downloadVersion") == download.version) { "The previous offline installer is no longer available; select a new full offline refresh" }
if (old.products.any { it != old.id }) {
val pinned = journal?.optJSONObject("target")?.optString("build").orEmpty()
val build = GogBuild.select(GogContent.builds(old.id, token), pinned = pinned, branch = old.branch)
?: error("No depot build is available to preserve installed DLC during this refresh. Use Update instead.")
val target = GogContent.snapshot(old, build, token)
GogFiles.validate(target.files, folder)
val native = GogNative.isAvailable()
val remaining = GogEstimate.calculate(folder, old, target, JSONArray(), reuseChunks = native)
return Proposal(folder, old, target, GogOperation.REFRESH, Math.addExact(download.size, remaining.download),
Math.addExact(Math.multiplyExact(download.size, 3), remaining.required), download, fullFiles = !native)
}
val target = old.copy(source = "offline", build = "", generation = 0, version = download.version, manifest = JSONObject(), files = JSONArray(), verified = true)
return Proposal(folder, old, target, GogOperation.REFRESH, download.size, download.size * 3, download)
}
require(old.verified || operation != GogOperation.REPAIR) { "The patched installation must be reconciled before repair" }
val pinned = journal?.optJSONObject("target")?.let(GogInstallRecord::parse)
val builds = GogContent.builds(old.id, token)
if (old.files.length() == 0 && old.build.isNotBlank()) GogBuild.select(builds, pinned = old.build)?.let { old = old.copy(branch = it.branch, generation = it.generation) }
val build = GogBuild.select(builds,
pinned = pinned?.build ?: if (operation == GogOperation.REPAIR) old.build.ifBlank { error("The installed GOG build is unknown; update it first") } else "",
branch = pinned?.branch ?: old.branch) ?: error("The requested GOG build is no longer available")
val target = GogContent.snapshot(old, build, token)
GogFiles.validate(target.files, folder)
var before = journal?.optJSONObject("before")?.let(GogInstallRecord::parse) ?: old
if (before.files.length() == 0 && before.build.isNotBlank()) {
val historical = GogBuild.select(builds, pinned = before.build)
if (historical != null) before = runCatching { GogContent.snapshot(before, historical, token) }.getOrDefault(before)
}
val deltas = GogContent.patches(before, target, token)
val native = GogNative.isAvailable()
val size = GogEstimate.calculate(folder, before, target, deltas, reuseChunks = native)
return Proposal(folder, before, target, operation, size.download, size.required, deltas = deltas, fullFiles = !native)
}
fun resume(context: Context, game: InstalledStoreGame): Proposal {
val journal = GogFiles.read(GogFiles.journal(game.folder)) ?: error("GOG operation journal is missing")
val operation = GogOperation.valueOf(journal.getString("operation"))
val patch = if (operation == GogOperation.PATCH) offlinePatches(context, game)
.firstOrNull { it.id == journal.optString("downloadId") } ?: error("The selected GOG patch is no longer available") else null
return prepare(context, game, operation, patch, resume = true)
}
fun recoverPatch(context: Context, game: InstalledStoreGame) {
val folder = game.folder.canonicalFile
check(!DownloadQueue.isActive(game.key)) { "Wait for the GOG operation to finish" }
val journal = GogFiles.read(GogFiles.journal(folder)) ?: error("GOG patch journal is missing")
require(journal.optString("folder") == folder.path && journal.optString("operation") == GogOperation.PATCH.name)
require(journal.optString("phase") in setOf("patch-starting", "patch-applying", "patch-uncertain"))
RandomAccessFile(GogFiles.inside(folder, ".droiddeck-gog/lease"), "rw").use { lease ->
val lock = runCatching { lease.channel.tryLock() }.getOrNull() ?: error("Close the patch installer before verifying its result")
lock.use {
ensureIdle(context, folder)
val before = GogInstallRecord.parse(journal.getJSONObject("before"))
val buildId = GogInstallRecord.authenticBuild(folder, before.id).ifBlank { before.build }
check(buildId.isNotBlank()) { "The resulting build is unknown. Use a full offline refresh." }
val token = GogContent.token(context)
val build = GogBuild.select(GogContent.builds(before.id, token), pinned = buildId)
?: error("The resulting build manifest is unavailable. Use a full offline refresh.")
val target = GogContent.snapshot(before, build, token)
GogFiles.validate(target.files, folder)
check(target.files.objects().all { it.optBoolean("directory") || GogFiles.matches(folder, it) }) {
"The files do not match the reported build. Use a full offline refresh to recover."
}
val sidecar = StoreGameSidecar.parse(journal.getJSONObject("sidecar").toString()) ?: error("The original GOG settings are missing")
target.write(folder)
StoreInstalls.complete(context, folder, sidecar.copy(installVersion = target.build, state = StoreGameSidecar.STATE_INSTALLED))
check(GogFiles.journal(folder).delete()) { "Could not finish the GOG recovery record" }
}
}
checkedAt.remove(folder.path)
DownloadQueue.dismiss(game.key)
StoresState.notifyLibraryChanged(context)
StoresState.post { checks.remove(game.sidecar.id) }
}
fun enqueue(context: Context, game: InstalledStoreGame, proposal: Proposal) {
val app = context.applicationContext
require(game.folder.canonicalFile == proposal.folder)
check(!DownloadQueue.isActive("gog:${game.sidecar.id}")) { "GOG maintenance is already queued" }
check(!StoreInstallRoot.insufficientSpace(proposal.diskBytes, StoreInstallRoot.availableBytes(proposal.folder))) { "Not enough free space for GOG maintenance" }
val journal = JSONObject().put("schema", 1).put("folder", proposal.folder.path).put("operation", proposal.operation.name)
.put("before", proposal.before.json()).put("target", proposal.target.json()).put("sidecar", game.sidecar.toJson()).put("phase", "download")
proposal.patch?.let { journal.put("downloadId", it.id).put("downloadVersion", it.version) }
GogFiles.write(GogFiles.journal(proposal.folder), journal)
val item = CatalogItem(Store.GOG, game.sidecar.id, game.sidecar.title, game.sidecar.hero, tallImageUrl = game.sidecar.cover,
extra = mapOf(OPERATION to proposal.operation.name, FOLDER to proposal.folder.path))
SavedQueue.remember(app, item, null)
enqueueJob(app, item, proposal)
}
private fun enqueueJob(context: Context, item: CatalogItem, proposal: Proposal) {
DownloadQueue.enqueue(context, DownloadEntry(Store.GOG, item.id, item.title, cover = item.tallImageUrl,
bytesTotal = proposal.downloadBytes, location = StoreInstallRoot.labelFor(context, proposal.folder))) { Job(context, proposal) }
}
fun restore(context: Context, item: CatalogItem) {
val folder = File(item.extra[FOLDER] ?: error("Missing GOG recovery folder")).canonicalFile
require(StoreInstallRoot.gameFolders(context).any { it.canonicalFile == folder }) { "GOG recovery folder is unavailable" }
val sidecar = StoreGameSidecar.read(folder) ?: error("GOG installation record is missing")
require(sidecar.store == Store.GOG && sidecar.id == item.id)
val journal = GogFiles.read(GogFiles.journal(folder)) ?: error("GOG operation journal is missing")
val operation = GogOperation.valueOf(journal.getString("operation"))
if (journal.optString("phase") in setOf("patch-starting", "patch-applying", "patch-uncertain")) {
DownloadQueue.recordFailed(context, DownloadEntry(Store.GOG, item.id, item.title), "The patch result needs recovery; open Offline patches")
return
}
val game = InstalledStoreGame(sidecar, folder)
SavedQueue.remember(context, item, null)
enqueueJob(context, item, resume(context, game))
}
private class Job(val app: Context, val proposal: Proposal) : DownloadQueue.DownloadJob {
val cancelled = AtomicBoolean(false)
@Volatile var applyingPatch = false
@Volatile private var running = false
@Volatile private var deleteCaches = false
override fun cancel(deleteFiles: Boolean) {
if (applyingPatch) return
deleteCaches = deleteCaches || deleteFiles
cancelled.set(true)
if (!running && deleteCaches) cleanCaches()
}
override fun run(handle: DownloadQueue.JobHandle): String? {
running = true
try { return perform(handle) }
finally {
running = false; applyingPatch = false; handle.interruptible(true)
if (deleteCaches) cleanCaches()
}
}
private fun cleanCaches() {
for (name in listOf("stage", "downloads", "extracted")) {
val root = GogFiles.inside(proposal.folder, ".droiddeck-gog/$name")
if (root.exists()) java.nio.file.Files.walk(root.toPath()).use { paths ->
paths.sorted(java.util.Comparator.reverseOrder()).forEach { java.nio.file.Files.deleteIfExists(it) }
}
}
}
private fun perform(handle: DownloadQueue.JobHandle): String? {
val folder = proposal.folder
val journal = GogFiles.read(GogFiles.journal(folder)) ?: error("GOG operation journal is missing")
val sidecar = StoreGameSidecar.parse(journal.getJSONObject("sidecar").toString()) ?: error("GOG previous installation is missing")
if (proposal.operation == GogOperation.PATCH) {
val result = GogOffline.apply(app, proposal, handle, cancelled, onApplying = { applyingPatch = true; handle.interruptible(false) })
applyingPatch = false; handle.interruptible(true)
if (!result || cancelled.get()) return null
} else {
RandomAccessFile(GogFiles.inside(folder, ".droiddeck-gog/lease"), "rw").use { lease ->
val lock = runCatching { lease.channel.tryLock() }.getOrNull() ?: error("Close the GOG game before updating it")
lock.use {
ensureIdle(app, folder)
sidecar.copy(state = StoreGameSidecar.STATE_UPDATING).write(folder)
GogFiles.write(GogFiles.journal(folder), journal.put("phase", "applying"))
if (proposal.operation == GogOperation.REFRESH) {
GogOffline.refresh(app, proposal, handle, cancelled)
if (!cancelled.get() && proposal.target.source == "depot") transfer(app, proposal, handle, cancelled)
} else transfer(app, proposal, handle, cancelled)
if (cancelled.get()) return null
}
}
}
if (cancelled.get()) return null
val target = GogInstallRecord.read(folder)?.takeIf { proposal.operation == GogOperation.PATCH || proposal.operation == GogOperation.REFRESH && proposal.target.source != "depot" } ?: proposal.target
check(target.verified) { "The patch completed, but its resulting version could not be verified. Inspect the game or use a full offline refresh." }
target.write(folder)
StoreInstalls.complete(app, folder, sidecar.copy(installVersion = target.build.ifBlank { target.version }, state = StoreGameSidecar.STATE_INSTALLED))
check(GogFiles.journal(folder).delete()) { "Could not finish the GOG recovery record" }
runCatching { cleanCaches() }.onFailure { handle.log("gog: completed; could not remove expendable cache: ${it.message}") }
checkedAt.remove(folder.path)
StoresState.post { checks.remove(sidecar.id) }
return folder.path
}
}
private fun ensureIdle(context: Context, folder: File) {
val game = AddedGames.single(context, folder.path) ?: return
check(SessionState.gameId != (game.appId and 0xffffffffL).toString()) { "Close the GOG game before maintenance" }
if (SessionState.running) {
val id = (game.steamAppId?.toLong() ?: game.appId) and 0xffffffffL
val guest = AddedGames.guestPath(context, folder) ?: error("The GOG folder is unavailable to Linux")
if (SessionState.suspended || AgentGuest.hello(context) == null) {
val root = AddedGames.guestPath(context, SessionPrefs.activeSteamRoot(context)) ?: error("The Steam folder is unavailable to Linux")
val rc = GuestCommand.run(context, listOf("/usr/bin/env", "STEAM_COMPAT_CLIENT_INSTALL_PATH=$root", HELPER, "idle", guest, id.toString(), game.guestExe),
extraBinds = StoreInstallRoot.externalBindSpecs(context), logName = "gog-idle", onLine = StoresState::logLine)
check(rc == 0) { "Close the GOG game before maintenance" }
return
}
val request = JSONObject().put("kind", "exec").put("argv", JSONArray(listOf(HELPER, "idle", guest, id.toString(), game.guestExe))).put("timeout", 15)
val result = AgentGuest.call(context, request, 20_000, trusted = true)
check(result.optBoolean("ok") && result.optInt("exitCode", -1) == 0) { "Close the GOG game before maintenance" }
}
}
private fun transfer(context: Context, proposal: Proposal, handle: DownloadQueue.JobHandle?, stop: AtomicBoolean, callback: GogDownloadManager.Callback? = null) {
val token = GogContent.token(context)
var patches = proposal.deltas
var previousWire = 0L
for (attempt in 0..4) {
if (stop.get()) return
val links = GogContent.links(proposal.target, GogContent.token(context))
val patchLinks = if (patches.length() > 0) runCatching { GogContent.links(proposal.target, token, patches = true) }.getOrElse { patches = JSONArray(); JSONObject() } else JSONObject()
val plan = JSONObject().put("files", proposal.target.files).put("oldFiles", proposal.before.files)
.put("patches", patches).put("links", links).put("patchLinks", patchLinks).put("downloadBytes", proposal.downloadBytes)
val optimized = GogNative.isAvailable()
val remaining = GogEstimate.calculate(proposal.folder, proposal.before, proposal.target, patches, reuseChunks = optimized).required
check(!StoreInstallRoot.insufficientSpace(remaining, StoreInstallRoot.availableBytes(proposal.folder))) { "Not enough free space for remaining GOG maintenance" }
handle?.diskSize(remaining)
if (!optimized) {
try { GogFallback.transfer(proposal.folder, plan, handle, stop, callback); return }
catch (e: Exception) {
if (stop.get()) return
if (attempt == 4 || Regex("HTTP (401|403|404|500)").find(e.message.orEmpty()) == null) throw e
handle?.log("gog: refreshing file-download links; verified replacements are retained")
continue
}
}
val latch = CountDownLatch(1)
var runWire = 0L
var success = false; var expired = false; var failure = "GOG download failed"
val listener = object : GogNative.Listener {
override fun onProgress(bytesDone: Long, bytesTotal: Long, filesDone: Int, filesTotal: Int, file: String, fileBytes: Long, verified: Boolean) {
handle?.stage(if (verified) DownloadStage.VERIFY else DownloadStage.INSTALL)
handle?.stageProgress(filesDone.toLong(), filesTotal.toLong(), filesDone, filesTotal)
callback?.onStage(if (verified) "verify" else "install", filesDone.toLong(), filesTotal.toLong(), filesDone, filesTotal)
}
override fun onBytes(bytesDone: Long, bytesTotal: Long) {
runWire = bytesDone
handle?.progress(previousWire + bytesDone, (previousWire + bytesTotal).coerceAtLeast(previousWire + bytesDone))
callback?.onBytes(previousWire + bytesDone, (previousWire + bytesTotal).coerceAtLeast(previousWire + bytesDone), 0)
}
override fun onLog(line: String) { handle?.log(line); callback?.onLog(line) }
override fun onMaintenance(networkBytes: Long, reusedBytes: Long, appliedBytes: Long) { handle?.maintenance(reusedBytes, appliedBytes) }
override fun onComplete(ok: Boolean, cancelled: Boolean, linkExpiry: Boolean, error: String, bytesWritten: Long, filesDone: Int) {
success = ok; expired = linkExpiry; failure = error; latch.countDown()
}
}
val native = GogNative.start(2, arrayOf(plan.toString()), emptyArray(), proposal.folder.path, emptyArray(), "", 1, 1, false, "gog maintenance", listener)
check(native != 0L) { "GOG native engine could not start" }
try { while (!latch.await(200, TimeUnit.MILLISECONDS)) if (stop.get()) GogNative.cancel(native) }
finally { GogNative.release(native) }
if (stop.get()) return
if (success) {
for (file in proposal.target.files.objects()) if (!file.optBoolean("directory") && file.optString("md5").isBlank() && file.optString("sha256").isBlank())
file.put("sha256", GogFiles.hash(GogFiles.inside(proposal.folder, file.getString("path")), "SHA-256"))
return
}
if (!expired || attempt == 4) error(failure)
previousWire += runWire
handle?.log("gog: refreshing expired download links; validated files and chunks are retained")
}
}
@JvmStatic
fun installContent(context: Context, game: GogGame, folder: File, builds: JSONObject, language: String,
callback: GogDownloadManager.Callback, cancelled: AtomicBoolean): GogDownloadManager.Result? {
val build = GogBuild.select(builds) ?: return null
folder.mkdirs()
val old = GogInstallRecord(game.gameId, "depot", language = language)
val target = GogContent.snapshot(old, build, GogContent.token(context))
GogFiles.validate(target.files, folder)
val bytes = target.files.objects().sumOf { it.optJSONArray("chunks")?.objects()?.sumOf { c -> c.optLong("compressedSize") }?.takeIf { n -> n > 0 } ?: it.optLong("size") }
val disk = target.files.objects().sumOf { it.optLong("size") }
check(!StoreInstallRoot.insufficientSpace(disk, StoreInstallRoot.availableBytes(folder))) { "Not enough free space for GOG installation" }
callback.onSizes(bytes, disk)
transfer(context, Proposal(folder, old, target, GogOperation.UPDATE, bytes, disk), null, cancelled, callback)
if (cancelled.get()) return null
target.write(folder)
val result = GogDownloadManager.Result(GogDownloadManager.pickExe(folder, null, game.title), build.id, disk)
result.language = target.language
result.dependencies = target.manifest.optJSONArray("dependencies")?.strings().orEmpty()
result.dependenciesRead = true
return result
}
@JvmStatic
fun captureDepot(id: String, folder: File, build: String, buildsJson: String, manifest: JSONObject, depots: JSONArray, language: String) {
val selected = GogBuild.select(JSONObject(buildsJson), pinned = build)
val files = JSONArray()
for (depot in depots.strings()) for (item in JSONObject(depot).getJSONObject("depot").getJSONArray("items").objects()) {
val chunks = item.optJSONArray("chunks") ?: continue
files.put(JSONObject().put("path", item.getString("path").replace('\\', '/')).put("size", chunks.objects().sumOf { it.optLong("size") })
.put("md5", item.optString("md5")).put("chunks", chunks).put("product", id))
}
GogFiles.validate(files, folder)
GogInstallRecord(id, "depot", 2, build, selected?.version.orEmpty(), selected?.branch.orEmpty(), language,
manifest = GogContent.sanitize(manifest), files = files).write(folder)
}
@JvmStatic
fun captureOffline(id: String, folder: File, downloads: JSONObject) {
val selected = GogOfflineDownload.parse(downloads, "installers").firstOrNull() ?: return
GogInstallRecord(id, "offline", version = selected.version, language = selected.language).write(folder)
}
}
@@ -15,6 +15,8 @@ object GogNative {
fun onBytes(bytesDone: Long, bytesTotal: Long)
fun onMaintenance(networkBytes: Long, reusedBytes: Long, appliedBytes: Long)
fun onLog(line: String)
fun onComplete(success: Boolean, cancelled: Boolean, linkExpiry: Boolean, error: String, bytesWritten: Long, filesDone: Int)
@@ -26,7 +28,7 @@ object GogNative {
fun isAvailable(): Boolean {
available?.let { return it }
val ok = try {
StoresNative.ensureLoaded() && nativeProbe() == 1
StoresNative.ensureLoaded() && nativeProbe() == 2
} catch (t: Throwable) {
Log.w(TAG, "native GOG engine unavailable: ${t.javaClass.simpleName}: ${t.message}")
false
@@ -0,0 +1,200 @@
package com.droiddeck.launcher.stores.gog
import android.content.Context
import com.droiddeck.launcher.agent.AgentGuest
import com.droiddeck.launcher.frontend.AddedGames
import com.droiddeck.launcher.runtime.GuestCommand
import com.droiddeck.launcher.session.SessionService
import com.droiddeck.launcher.session.SessionState
import com.droiddeck.launcher.stores.StoreGameSidecar
import com.droiddeck.launcher.stores.StoreInstallRoot
import com.droiddeck.launcher.stores.download.DownloadQueue
import com.droiddeck.launcher.stores.download.DownloadStage
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
import java.util.concurrent.atomic.AtomicBoolean
internal object GogOffline {
private const val HELPER = "/usr/local/bin/droiddeck-gog-maintenance"
internal data class Layout(val source: File, val prefix: String)
internal fun layout(extracted: File, installed: File, id: String, executable: String): Layout {
val nested = executable.startsWith("app/", true) && !File(installed, "goggame-$id.info").isFile
return Layout(extracted, if (nested) "app/" else "")
}
internal fun validateDepotInstaller(extracted: File, target: GogInstallRecord) {
if (target.source != "depot") return
val builds = listOf(extracted, File(extracted, "app")).map { GogInstallRecord.authenticBuild(it, target.id) }.filter { it.isNotBlank() }
require(builds.isNotEmpty() && builds.all { it == target.build } && target.build.isNotBlank()) {
"The offline installer does not identify the selected depot build. Use Update to preserve installed DLC."
}
}
private fun download(context: Context, proposal: GogMaintenance.Proposal, handle: DownloadQueue.JobHandle, stop: AtomicBoolean): File {
val patch = proposal.patch ?: error("GOG download selection is missing")
val root = File(GogFiles.dir(proposal.folder), "downloads").apply { mkdirs() }
var first: File? = null; var bytes = 0L
for ((index, part) in patch.parts.withIndex()) {
if (stop.get()) return first ?: File(root, "cancelled")
val resolved = GogDownloadManager.resolveRedirect(part.url, GogContent.token(context)) ?: error("The GOG download link is unavailable")
val serverName = GogContent.downloadFilename(resolved).takeIf { it.endsWith(".exe", true) || it.endsWith(".bin", true) }
val name = part.name.ifBlank { serverName ?: error("GOG did not provide the installer part filename") }
require('/' !in name && '\\' !in name && name !in setOf(".", "..")) { "Invalid GOG installer filename" }
val file = GogFiles.inside(root, name)
val receipt = File(root, "part-$index.json")
val saved = GogFiles.read(receipt)
val complete = file.isFile && (part.size <= 0 || file.length() == part.size) && when {
part.md5.isNotBlank() -> GogFiles.hash(file).equals(part.md5, true)
saved?.optString("id") == part.id && saved.optString("filename") == name -> GogFiles.hash(file, "SHA-256") == saved.optString("sha256")
else -> false
}
if (!complete) {
val staging = GogFiles.inside(root, "$name.partial")
GogFallback.download(listOf(resolved), staging, part.size, stop, { n -> bytes += n; handle.progress(bytes, proposal.downloadBytes.coerceAtLeast(bytes)) })
if (stop.get()) return first ?: file
if (part.md5.isNotBlank()) check(GogFiles.hash(staging).equals(part.md5, true)) { "GOG installer checksum mismatch" }
GogFiles.replace(staging, file)
GogFiles.write(receipt, JSONObject().put("id", part.id).put("filename", name).put("sha256", GogFiles.hash(file, "SHA-256")))
}
if (index == 0) { check(file.extension.equals("exe", true)) { "GOG patch has no setup executable" }; first = file }
}
return first ?: error("GOG download has no files")
}
fun refresh(context: Context, proposal: GogMaintenance.Proposal, handle: DownloadQueue.JobHandle, stop: AtomicBoolean) {
val installer = download(context, proposal, handle, stop)
if (stop.get()) return
val extracted = File(GogFiles.dir(proposal.folder), "extracted").apply { mkdirs() }
val source = AddedGames.guestPath(context, installer) ?: error("The GOG installer is not visible to Linux")
val target = AddedGames.guestPath(context, extracted) ?: error("The GOG staging folder is not visible to Linux")
handle.stage(DownloadStage.INSTALL, "Extracting the full offline installer")
val rc = GuestCommand.run(context, listOf("/usr/local/bin/droiddeck-innoextract", "--gog", "--silent", "-d", target, source),
extraBinds = StoreInstallRoot.externalBindSpecs(context), cancelled = stop, logName = "gog-refresh", onLine = handle::log)
if (stop.get()) return
check(rc == 0) { "GOG installer extraction failed (exit $rc)" }
applyRefresh(proposal, extracted, handle, stop)
}
internal fun applyRefresh(proposal: GogMaintenance.Proposal, extracted: File, handle: DownloadQueue.JobHandle?, stop: AtomicBoolean) {
validateDepotInstaller(extracted, proposal.target)
val sidecar = StoreGameSidecar.read(proposal.folder) ?: error("The original GOG settings are missing")
val layout = layout(extracted, proposal.folder, proposal.before.id, sidecar.exe)
val files = layout.source.walkTopDown().onEnter { directory ->
check(!java.nio.file.Files.isSymbolicLink(directory.toPath())) { "GOG installer contains a symbolic link" }
directory.parentFile != extracted || directory.name !in setOf("tmp", "commonappdata", "userappdata", "localappdata")
}.filter { it != layout.source && it != File(extracted, "app") }.map { file ->
check(!java.nio.file.Files.isSymbolicLink(file.toPath()))
val relative = file.relativeTo(layout.source).invariantSeparatorsPath.removePrefix("app/")
JSONObject().put("path", layout.prefix + relative)
.put("size", if (file.isDirectory) 0 else file.length()).put("directory", file.isDirectory)
.put("md5", if (file.isDirectory) "" else GogFiles.hash(file)).put("product", proposal.before.id) to file
}.toList().groupBy { it.first.getString("path") }.map { (_, entries) ->
check(entries.size == 1 || entries.all { it.first.getBoolean("directory") }) { "GOG installer has conflicting file locations" }
entries.first()
}
check(files.any { !it.first.getBoolean("directory") }) { "GOG installer contained no game files" }
val inventory = JSONArray(files.map { it.first })
GogFiles.validate(inventory, proposal.folder)
val executable = sidecar.exe.replace('\\', '/')
check(files.any { !it.first.getBoolean("directory") && it.first.getString("path").equals(executable, true) } ||
proposal.before.verified && proposal.before.files.length() > 0 &&
proposal.before.files.objects().none { it.getString("path").equals(executable, true) } && GogFiles.inside(proposal.folder, executable).isFile) {
"The offline installer did not extract the game's executable. No installed files were changed."
}
val installed = proposal.target.copy(files = inventory)
GogFiles.write(File(GogFiles.dir(proposal.folder), "refresh-target.json"), installed.json())
for ((index, entry) in files.withIndex()) {
if (stop.get()) return
val (file, sourceFile) = entry
val destination = GogFiles.inside(proposal.folder, file.getString("path"))
if (file.getBoolean("directory")) java.nio.file.Files.createDirectories(destination.toPath()) else {
destination.parentFile!!.mkdirs()
GogFiles.replace(sourceFile, destination)
}
handle?.stageProgress((index + 1).toLong(), files.size.toLong())
}
if (proposal.target.source != "depot") {
val targetPaths = files.mapTo(HashSet()) { it.first.getString("path").lowercase(java.util.Locale.ROOT) }
for (old in proposal.before.files.objects()) if (old.getString("path").lowercase(java.util.Locale.ROOT) !in targetPaths && GogFiles.matches(proposal.folder, old))
check(GogFiles.inside(proposal.folder, old.getString("path")).delete())
installed.write(proposal.folder)
}
}
fun apply(context: Context, proposal: GogMaintenance.Proposal, handle: DownloadQueue.JobHandle, stop: AtomicBoolean, onApplying: () -> Unit): Boolean {
check(GogFiles.read(GogFiles.journal(proposal.folder))?.optString("phase") in setOf("download", "patch-ready")) {
"The patch result needs recovery. Inspect the installer or use a full offline refresh; the patch will not be run again."
}
val installer = download(context, proposal, handle, stop)
if (stop.get()) return false
val setup = GogContent.setupFiles(proposal.before, GogContent.token(context))
if (setup != null) {
GogFallback.transfer(proposal.folder, JSONObject().put("files", setup.first).put("links", setup.second)
.put("downloadBytes", setup.first.objects().sumOf { it.getJSONArray("chunks").objects().sumOf { chunk -> chunk.getLong("compressedSize") } }), handle, stop)
if (stop.get()) return false
}
val added = AddedGames.single(context, proposal.folder.path) ?: error("The GOG game shortcut is unavailable")
val id = (added.steamAppId?.toLong() ?: added.appId) and 0xffffffffL
val folder = AddedGames.guestPath(context, proposal.folder) ?: error("The GOG folder is unavailable to Linux")
val executable = AddedGames.guestPath(context, installer) ?: error("The GOG patch is unavailable to Linux")
context.startActivity(android.content.Intent(context, com.droiddeck.launcher.SessionActivity::class.java)
.putExtra(SessionService.EXTRA_MODE, if (SessionState.running) SessionState.mode else SessionService.MODE_DESKTOP)
.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK or android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP or android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP))
val deadline = System.currentTimeMillis() + 120_000
while (AgentGuest.hello(context) == null && System.currentTimeMillis() < deadline) { if (stop.get()) return false; Thread.sleep(250) }
check(AgentGuest.hello(context) != null) { "The Linux session did not become ready; open it and retry" }
val journal = GogFiles.read(GogFiles.journal(proposal.folder)) ?: error("GOG patch journal is missing")
journal.put("phase", "patch-ready")
File(GogFiles.dir(proposal.folder), "patch-status.json").delete()
GogFiles.write(GogFiles.journal(proposal.folder), journal)
val request = JSONObject().put("kind", "exec").put("timeout", 15)
.put("env", JSONObject().put("DROIDDECK_GOG_DESKTOP", if (SessionState.mode == SessionService.MODE_DESKTOP) "1" else "0"))
.put("argv", JSONArray(listOf(HELPER, "start", folder, id.toString(), executable, added.guestExe)))
if (stop.get()) return false
onApplying()
val response = AgentGuest.call(context, request, 20_000, trusted = true)
check(response.optBoolean("ok") && response.optInt("exitCode", -1) == 0) { "The GOG patch installer could not start; inspect the GOG log" }
handle.stage(DownloadStage.INSTALL, "Use the Linux session to finish or cancel the patch installer")
var nextStatus = System.currentTimeMillis() + 5_000
while (true) {
val status = GogFiles.read(File(GogFiles.dir(proposal.folder), "patch-status.json"))
if (status?.optString("state") in setOf("done", "failed", "uncertain")) {
check(status?.optString("state") == "done") { status?.optString("reason") ?: "GOG patch failed" }
break
}
check(SessionState.running) { "The patch session ended. Its result needs recovery; the patch will not be rerun automatically." }
check(!SessionState.suspended) { "The patch session was suspended. Resume it and verify recovery before retrying." }
if (System.currentTimeMillis() >= nextStatus) {
val query = JSONObject().put("kind", "exec").put("timeout", 5).put("argv", JSONArray(listOf(HELPER, "status", folder)))
val answer = AgentGuest.call(context, query, 10_000, trusted = true)
check(answer.optBoolean("ok") && answer.optInt("exitCode", -1) == 0) { "The patch helper could not be reached. Resume the Linux session and verify recovery." }
nextStatus = System.currentTimeMillis() + 5_000
}
Thread.sleep(500)
}
val info = GogFiles.read(File(proposal.folder, "goggame-${proposal.before.id}.info"))
val version = info?.optString("versionName").orEmpty()
val build = GogInstallRecord.authenticBuild(proposal.folder, proposal.before.id)
var record = proposal.target.copy(build = build, version = version, files = JSONArray(), manifest = JSONObject(), verified = false)
record.write(proposal.folder)
if (build.isNotBlank()) {
record = runCatching {
val token = GogContent.token(context)
val target = GogBuild.select(GogContent.builds(record.id, token), pinned = build)
val snapshot = target?.let { GogContent.snapshot(record, it, token) }
snapshot?.takeIf { it.files.objects().all { f -> f.optBoolean("directory") || GogFiles.matches(proposal.folder, f) } } ?: record
}.getOrDefault(record)
}
record.write(proposal.folder)
if (!record.verified) {
val before = StoreGameSidecar.read(proposal.folder) ?: error("The GOG installation record is missing")
before.copy(installVersion = "", state = StoreGameSidecar.STATE_UPDATING).write(proposal.folder)
val operation = GogFiles.read(GogFiles.journal(proposal.folder))!!
GogFiles.write(GogFiles.journal(proposal.folder), operation.put("phase", "patch-uncertain"))
}
return true
}
}
@@ -351,7 +351,7 @@ internal fun GameActions(
val gone = storeKey?.let { StoresState.uninstalling[it] }
val xboxBusy = store == Store.XBOX && id != null && XboxContent.busy(id)
PrimaryButton(
stringResource(R.string.stores_play), enabled = !s.busy && gone == null && download == null && !xboxBusy,
stringResource(R.string.stores_play), enabled = !s.busy && gone == null && download == null && !xboxBusy && (store != Store.GOG || installed?.sidecar?.isInstalled != false),
main = true, icon = Icons.Filled.PlayArrow, modifier = leadOn,
) {
if (game != null) a.onSteamGame(game) else if (store != null && id != null) launchStoreGame(ctx, store, id, s, a)
@@ -363,6 +363,7 @@ internal fun GameActions(
EditArtButton(game)
}
if (store == Store.ITCH && installed != null) ItchUpdateButton(installed, download == null && StoresState.uninstalling[installed.key] == null)
if (store == Store.GOG && installed != null) GogMaintenanceActions(installed)
if (store == Store.XBOX && id != null) XboxInstalledStream(id, game?.name ?: item?.title.orEmpty(), item)
if (store == Store.XBOX && installed != null && id != null) {
SecondaryButton(stringResource(R.string.stores_xbox_update), enabled = download == null && !XboxRuntime.isActive(id)) {
@@ -0,0 +1,188 @@
package com.droiddeck.launcher.ui
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.background
import androidx.compose.material3.Text
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.Alignment
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import com.droiddeck.launcher.R
import com.droiddeck.launcher.stores.InstalledStoreGame
import com.droiddeck.launcher.stores.formatBytes
import com.droiddeck.launcher.stores.StoreLanguage
import com.droiddeck.launcher.stores.download.DownloadQueue
import com.droiddeck.launcher.stores.download.DownloadState
import com.droiddeck.launcher.stores.gog.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@Composable
internal fun GogMaintenanceActions(game: InstalledStoreGame) {
val ctx = LocalContext.current
val colors = MaterialTheme.colorScheme
val scope = rememberCoroutineScope()
var menu by remember { mutableStateOf(false) }
var patches by remember { mutableStateOf<List<GogOfflineDownload>?>(null) }
var proposal by remember { mutableStateOf<GogMaintenance.Proposal?>(null) }
var error by remember { mutableStateOf<String?>(null) }
var busy by remember { mutableStateOf(false) }
var recoverablePatch by remember { mutableStateOf(false) }
val key = "gog:${game.sidecar.id}"
val download = com.droiddeck.launcher.stores.StoresState.download(key)
val active = download?.isActive == true
val check = GogMaintenance.checks[game.sidecar.id]
LaunchedEffect(game.folder.path) { GogMaintenance.check(ctx, game) }
LaunchedEffect(menu, active) {
if (menu) {
recoverablePatch = false
val result = withContext(Dispatchers.IO) { runCatching {
GogFiles.read(GogFiles.journal(game.folder))?.let {
it.optString("operation") == GogOperation.PATCH.name && it.optString("phase") in setOf("patch-starting", "patch-applying", "patch-uncertain")
} == true
} }
result.onSuccess { recoverablePatch = it }.onFailure { error = it.message }
}
}
fun prepare(operation: GogOperation, patch: GogOfflineDownload? = null) {
menu = false; patches = null; busy = true; error = null
scope.launch {
runCatching { withContext(Dispatchers.IO) { GogMaintenance.prepare(ctx, game, operation, patch) } }
.onSuccess { proposal = it }.onFailure { error = it.message }
busy = false
}
}
if (active) {
ActionChip(downloadLabel(download!!), ok = false)
if (download.interruptible) {
SecondaryButton(stringResource(if (download.state == DownloadState.PAUSED) R.string.stores_dl_resume else R.string.stores_dl_pause)) {
if (download.state == DownloadState.PAUSED) DownloadQueue.resume(ctx, key) else DownloadQueue.pause(key)
}
} else Text(stringResource(R.string.gog_patch_in_session), color = MaterialTheme.colorScheme.onSurfaceVariant)
} else if (!game.sidecar.isInstalled || GogFiles.journal(game.folder).exists()) {
SecondaryButton(stringResource(R.string.gog_resume), modifier = Modifier.testTag("gog-resume")) {
busy = true
scope.launch {
runCatching {
withContext(Dispatchers.IO) {
GogMaintenance.resume(ctx, game)
}
}.onSuccess { proposal = it }.onFailure { error = it.message }
busy = false
}
}
} else if (check?.state == GogMaintenance.State.AVAILABLE) {
SecondaryButton(stringResource(R.string.gog_update, check.version.ifBlank { game.sidecar.title }), enabled = !busy, modifier = Modifier.testTag("gog-update")) { prepare(GogOperation.UPDATE) }
}
androidx.compose.foundation.layout.Box {
SecondaryButton(stringResource(if (busy) R.string.common_checking else R.string.gog_maintenance), enabled = !busy && !active, modifier = Modifier.testTag("gog-maintenance")) { menu = !menu }
AnchoredMenu(menu, { menu = false }, title = stringResource(R.string.gog_maintenance), compact = true) { first ->
MenuItem(stringResource(R.string.common_update), checked = false) { prepare(GogOperation.UPDATE) }
MenuItem(stringResource(R.string.gog_check), checked = false, focusRequester = first) { menu = false; GogMaintenance.check(ctx, game, true) }
MenuItem(stringResource(R.string.gog_repair), checked = false, modifier = Modifier.testTag("gog-repair")) { prepare(GogOperation.REPAIR) }
MenuItem(stringResource(R.string.gog_patches), checked = false, modifier = Modifier.testTag("gog-patches")) {
menu = false; busy = true
scope.launch {
runCatching { withContext(Dispatchers.IO) { GogMaintenance.offlinePatches(ctx, game) } }
.onSuccess { patches = it }.onFailure { error = it.message }
busy = false
}
}
MenuItem(stringResource(R.string.gog_refresh), checked = false) { prepare(GogOperation.REFRESH) }
if (recoverablePatch) MenuItem(stringResource(R.string.gog_recover), checked = false, modifier = Modifier.testTag("gog-recover")) {
menu = false; busy = true
scope.launch {
runCatching { withContext(Dispatchers.IO) { GogMaintenance.recoverPatch(ctx, game) } }.onFailure { error = it.message }
busy = false
}
}
}
}
if (check?.state == GogMaintenance.State.FAILED || check?.state == GogMaintenance.State.UNKNOWN) {
if (check.reason.isNotBlank()) Text(check.reason, fontSize = 12.sp, color = colors.onSurfaceVariant)
}
patches?.let { list ->
val shown = rememberShown { patches = null }
val close = { shown.targetState = false }
AppDialog(shown, close, "gogPatches", wide = false, maxWidth = 460.dp) {
DialogTitleRow(stringResource(R.string.gog_patches), close)
Text(game.sidecar.title, fontSize = 12.sp, lineHeight = 16.sp, color = colors.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis)
Text(stringResource(R.string.gog_patch_requirements), fontSize = 12.sp, lineHeight = 18.sp, color = colors.onSurfaceVariant)
if (list.isEmpty()) Text(stringResource(R.string.gog_no_patches), fontSize = 13.sp, color = colors.onSurfaceVariant)
Column(verticalArrangement = Arrangement.spacedBy(6.dp)) {
list.forEach { patch ->
ListRow(onClick = { close(); prepare(GogOperation.PATCH, patch) }) {
Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(3.dp)) {
Text(patch.name, fontSize = 13.sp, lineHeight = 18.sp, fontWeight = FontWeight.Medium, color = colors.onBackground, maxLines = 2, overflow = TextOverflow.Ellipsis)
Text(listOf(StoreLanguage.displayName(patch.language).ifBlank { patch.language }, patch.version, formatBytes(patch.size)).filter { it.isNotBlank() }.joinToString(" · "),
fontSize = 11.sp, lineHeight = 15.sp, color = colors.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis)
}
Text(stringResource(R.string.gog_select_patch), fontSize = 12.sp, color = LocalPalette.current.signal)
}
}
}
SecondaryButton(stringResource(R.string.gog_refresh)) { prepare(GogOperation.REFRESH) }
}
}
proposal?.let { selected ->
val shown = rememberShown { proposal = null }
val close = { shown.targetState = false }
val action = when (selected.operation) {
GogOperation.UPDATE -> R.string.common_update
GogOperation.REPAIR -> R.string.gog_repair_action
GogOperation.PATCH -> R.string.gog_open_patch
GogOperation.REFRESH -> R.string.gog_refresh_action
}
AppDialog(shown, close, "gogMaintenance", wide = false, maxWidth = 460.dp) {
DialogTitleRow(stringResource(action), close)
Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {
Text(listOf(game.sidecar.title, selected.before.version).filter { it.isNotBlank() }.joinToString(" · "), fontSize = 12.sp, lineHeight = 16.sp, color = colors.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis)
if (selected.operation != GogOperation.REPAIR) Text(selected.patch?.name ?: selected.target.version.ifBlank { selected.target.build }, fontSize = 15.sp, lineHeight = 20.sp, fontWeight = FontWeight.SemiBold,
color = colors.onBackground, maxLines = 2, overflow = TextOverflow.Ellipsis)
}
Row(horizontalArrangement = Arrangement.spacedBy(16.dp), modifier = Modifier.fillMaxWidth().clip(Shape12).background(colors.surface.copy(alpha = 0.65f)).padding(12.dp)) {
GogMetric(stringResource(R.string.gog_download_label), formatBytes(selected.downloadBytes), Modifier.weight(1f))
GogMetric(stringResource(if (selected.operation == GogOperation.PATCH || selected.operation == GogOperation.REFRESH) R.string.gog_space_estimate_label else R.string.gog_space_label),
formatBytes(selected.diskBytes), Modifier.weight(1f))
}
Column(verticalArrangement = Arrangement.spacedBy(6.dp)) {
Text(stringResource(R.string.gog_managed_files), fontSize = 12.sp, lineHeight = 18.sp, color = colors.onSurfaceVariant)
if (selected.fullFiles) Text(stringResource(R.string.gog_full_files), fontSize = 12.sp, lineHeight = 18.sp, color = colors.onSurfaceVariant)
if (selected.operation == GogOperation.PATCH) Text(stringResource(R.string.gog_patch_installer_note), fontSize = 12.sp, lineHeight = 18.sp, color = colors.onSurfaceVariant)
}
Row(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically) {
PrimaryButton(stringResource(action), enabled = !busy, modifier = Modifier.testTag("gog-apply")) {
busy = true
close()
scope.launch {
runCatching { withContext(Dispatchers.IO) { GogMaintenance.enqueue(ctx, game, selected) } }.onFailure { error = it.message }
busy = false
}
}
SecondaryButton(stringResource(R.string.common_cancel), onClick = close)
}
}
}
error?.let { Text(it, fontSize = 12.sp, color = colors.error) }
}
@Composable
private fun GogMetric(label: String, value: String, modifier: Modifier) {
Column(modifier, verticalArrangement = Arrangement.spacedBy(4.dp)) {
Text(label, fontSize = 11.sp, lineHeight = 15.sp, color = MaterialTheme.colorScheme.onSurfaceVariant)
Text(value, fontSize = 15.sp, lineHeight = 20.sp, fontWeight = FontWeight.SemiBold, color = MaterialTheme.colorScheme.onBackground)
}
}
@@ -92,6 +92,7 @@ import com.droiddeck.launcher.stores.formatBytes
import com.droiddeck.launcher.stores.itch.ItchCollection
import com.droiddeck.launcher.stores.itch.ItchCollections
import com.droiddeck.launcher.stores.itch.ItchUpdates
import com.droiddeck.launcher.stores.gog.GogMaintenance
import com.droiddeck.launcher.stores.retainOrder
import com.droiddeck.launcher.stores.xbox.XboxLibraryFilter
import com.droiddeck.launcher.stores.sortGames
@@ -586,13 +587,16 @@ internal fun LibraryMarker(e: LibraryEntry, download: DownloadEntry?) {
download != null -> Text("${download.percent}%", fontSize = 10.sp, fontWeight = FontWeight.Bold, color = pal.signal, maxLines = 1)
e.item != null && StoresState.isUnfinished(e.item) -> Text(stringResource(R.string.stores_resume), fontSize = 10.sp, fontWeight = FontWeight.Bold, color = pal.signal, maxLines = 1)
else -> Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(3.dp)) {
if (e.installed && e.storeKey?.let { it in ItchUpdates.available } == true) Text(stringResource(R.string.common_update), fontSize = 10.sp, fontWeight = FontWeight.Bold, color = pal.signal, maxLines = 1)
if (e.installed && libraryUpdateAvailable(e)) Text(stringResource(R.string.common_update), fontSize = 10.sp, fontWeight = FontWeight.Bold, color = pal.signal, maxLines = 1)
if (size != null && size > 0) Text(formatBytes(size), fontSize = 11.sp, fontWeight = FontWeight.Bold, color = Color.White, maxLines = 1)
if (!e.installed) Icon(Icons.Outlined.Download, stringResource(R.string.stores_section_not_installed), tint = Color.White, modifier = Modifier.size(12.dp))
}
}
}
private fun libraryUpdateAvailable(e: LibraryEntry): Boolean = e.storeKey?.let { it in ItchUpdates.available } == true ||
e.source == Store.GOG.id && (e.item?.id ?: e.game?.storeId)?.let { GogMaintenance.checks[it]?.state } == GogMaintenance.State.AVAILABLE
@Composable
private fun LibraryRow(e: LibraryEntry, first: Boolean, navigation: Modifier, choose: (() -> Unit)?, onOpen: (String) -> Unit) {
val colors = MaterialTheme.colorScheme
@@ -613,6 +617,7 @@ private fun LibraryRow(e: LibraryEntry, first: Boolean, navigation: Modifier, ch
val size = if (e.installed && folder != null) diskSize(inst, folder) else e.item?.let { StoreSizes.size(it) }?.takeIf { it > 0 }
val status = when {
download != null -> downloadLabel(download)
e.installed && libraryUpdateAvailable(e) -> stringResource(R.string.common_update)
!e.installed -> stringResource(R.string.stores_section_not_installed)
e.game != null -> playedSpan(e.game.lastPlayed)?.replaceFirstChar { it.uppercase() } ?: stringResource(R.string.games_never_played)
else -> stringResource(R.string.stores_installed_chip)
@@ -360,8 +360,10 @@ private fun DownloadCard(d: DownloadEntry, s: FrontEndState, a: FrontEndActions)
Row(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically) {
when (d.state) {
DownloadState.RUNNING, DownloadState.QUEUED -> {
RoundAction(Icons.Filled.Pause, stringResource(R.string.stores_dl_pause)) { DownloadQueue.pause(d.key) }
CancelAction { DownloadQueue.cancel(ctx, d.key) }
if (d.interruptible) {
RoundAction(Icons.Filled.Pause, stringResource(R.string.stores_dl_pause)) { DownloadQueue.pause(d.key) }
CancelAction { DownloadQueue.cancel(ctx, d.key) }
}
}
DownloadState.PAUSED -> {
RoundAction(Icons.Filled.PlayArrow, stringResource(R.string.stores_dl_resume), primary = true) { DownloadQueue.resume(ctx, d.key) }
@@ -1380,6 +1380,9 @@ private fun CardMarker(item: CatalogItem, installed: Boolean, download: com.droi
val pal = LocalPalette.current
val small = 10.sp
when {
item.store == Store.GOG && download != null -> Text("${download.percent}%", fontSize = small, fontWeight = FontWeight.Bold, color = pal.signal, maxLines = 1)
item.store == Store.GOG && installed && com.droiddeck.launcher.stores.gog.GogMaintenance.checks[item.id]?.state == com.droiddeck.launcher.stores.gog.GogMaintenance.State.AVAILABLE ->
Text(stringResource(R.string.common_update), fontSize = small, fontWeight = FontWeight.Bold, color = pal.signal, maxLines = 1)
installed -> Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(3.dp)) {
val ctx = LocalContext.current
val game = StoresState.installedGame(item.store, item.id)
+21
View File
@@ -1,5 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="gog_maintenance">Manage GOG installation</string>
<string name="gog_check">Check for updates</string>
<string name="gog_repair">Verify/Repair</string>
<string name="gog_patches">Offline patches</string>
<string name="gog_refresh">Download full offline installer</string>
<string name="gog_update">Update to %1$s</string>
<string name="gog_resume">Resume GOG maintenance</string>
<string name="gog_open_patch">Open patch installer</string>
<string name="gog_patch_in_session">Finish or cancel the patch installer in the Linux session.</string>
<string name="gog_patch_requirements">Choose a patch for your installed version and language. Supported source versions may not be listed.</string>
<string name="gog_no_patches">GOG has no standalone Windows patches for this game.</string>
<string name="gog_managed_files">Modified game files may be replaced.</string>
<string name="gog_patch_installer_note">The installer opens in your Linux session.</string>
<string name="gog_download_label">Download</string>
<string name="gog_space_label">Extra space</string>
<string name="gog_space_estimate_label">Space estimate</string>
<string name="gog_repair_action">Repair files</string>
<string name="gog_refresh_action">Refresh installation</string>
<string name="gog_select_patch">Select</string>
<string name="glasses_experimental">Luma Ultra cameras — Experimental</string>
<string name="glasses_joycons">Joy-Con position tracking</string>
<string name="glasses_joycons_hint">Use the glasses cameras to position your paired Joy-Cons. Rotation and button inputs come from the Joy-Cons. Position is estimated when your wrists are hidden. Replaces bare-hand controllers and pauses phone-camera tracking. Point the Joy-Cons forward when connecting or recentering.</string>
@@ -3325,4 +3344,6 @@
<string name="integration_shortcuts_service">DroidDeck Home button</string>
<string name="integration_shortcuts_service_description">Pass Home button presses to Steam Guide while a Steam session is focused and DroidDeck is the default Home app. Does not read screen content.</string>
<string name="integration_brightness_needed">Turn on Brightness in Setup → Steam integration</string>
<string name="gog_full_files">Native optimizations are unavailable. The download includes complete replacement files.</string>
<string name="gog_recover">Verify patch result</string>
</resources>
+3
View File
@@ -1,3 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
@@ -186,6 +188,7 @@ dependencies = [
name = "droiddeckstores"
version = "0.1.0"
dependencies = [
"cc",
"flate2",
"futures-util",
"jni",
+3
View File
@@ -21,6 +21,9 @@ serde_json = { version = "1", features = ["preserve_order"] }
sha1 = "0.10"
sha2 = "0.10"
[build-dependencies]
cc = "1"
[profile.release]
codegen-units = 1
lto = "thin"
+11 -2
View File
@@ -77,14 +77,14 @@ after it (marker files, exe resolution, DLC markers, redist orchestration).
| `nativeCancel(handle: Long)` | `(J)V` | `Java_com_droiddeck_launcher_stores_gog_GogNative_nativeCancel` |
| `nativeRelease(handle: Long)` | `(J)V` | `Java_com_droiddeck_launcher_stores_gog_GogNative_nativeRelease` |
`nativeProbe()` always returns `1`; it exists so `isAvailable()` can catch `UnsatisfiedLinkError`
`nativeProbe()` returns `2`; it exists so `isAvailable()` can catch `UnsatisfiedLinkError`
once at startup instead of mid-download.
`nativeStart` parameters, in order:
| # | Parameter | Meaning |
|---|---|---|
| 1 | `kind: Int` | `0` = `KIND_GEN2_CHUNKS`, `1` = `KIND_GEN1_RANGES` (anything else → gen2) |
| 1 | `kind: Int` | `0` = `KIND_GEN2_CHUNKS`, `1` = `KIND_GEN1_RANGES`, `2` = GOG maintenance (anything else → gen2) |
| 2 | `depotManifests: Array<String>` | gen2: the inflated depot-manifest JSON strings, in fetch order, already filtered by product + language; gen1: the inflated build manifest (one element) |
| 3 | `cdnBases: Array<String>` | gen2: **every** CDN base of the secure-link answer — one per `urls[]` entry, each resolved the way `parseCdnUrl` resolves `urls[0]` today (query string intact), in GOG's order — or the one unauthenticated dependency store base (an array of one); gen1: `emptyArray()` (file URLs are in the manifest). Blank entries are ignored and a base whose host already appeared is dropped (the first wins). The fetch core spreads the window across the distinct hosts with a per-host cap of `max(6, ceil(maxWorkers / hosts))`, rotating a failed chunk to another CDN; one base behaves exactly like the old single `cdnBase`. The start log line lists them as `hosts=a,b,c`. |
| 4 | `installDir: String` | absolute install directory |
@@ -113,6 +113,15 @@ On-disk protocol (what a resumed run, by either engine, relies on): a file is as
`<file>.bhtmp`, then renamed over `<file>` once its size and MD5 match the manifest; an existing
`<file>` whose size+MD5 match is skipped. The engine writes nothing else.
Maintenance (`kind=2`) instead takes a single normalized plan in `depotManifests[0]`:
`files`, `oldFiles`, optional exact-build `patches`, per-product `links` and `patchLinks`,
and estimated `downloadBytes`. Credentials and these temporary links stay in memory.
It stages replacements under `.droiddeck-gog/stage`, revalidates completed chunks on
every run, and verifies each file before atomic replacement. `onBytes` counts actual
network bytes, including failed attempts. `onMaintenance(networkBytes, reusedBytes,
appliedBytes)` (`(JJJ)V`) reports the three separate cumulative counters. Existing
gen1/gen2 first-install progress semantics are unchanged.
## `com.droiddeck.launcher.stores.epic.EpicNative`
Everything Epic runs in the Rust `epic` module: the web-service calls (sign-in, refresh, exchange
+21
View File
@@ -0,0 +1,21 @@
fn main() {
cc::Build::new()
.include("vendor/xdelta3")
.file("vendor/xdelta3/xdelta3.c")
.file("src/gog/delta.c")
.define("SIZEOF_SIZE_T", "8")
.define("SIZEOF_UNSIGNED_INT", "4")
.define("SIZEOF_UNSIGNED_LONG", "8")
.define("SIZEOF_UNSIGNED_LONG_LONG", "8")
.define("XD3_USE_LARGEFILE64", "1")
.define("XD3_ENCODER", "0")
.define("SECONDARY_DJW", "0")
.define("SECONDARY_FGK", "0")
.define("SECONDARY_LZMA", "0")
.define("SHELL_TESTS", "0")
.define("XD3_MAIN", "0")
.warnings(false)
.compile("gogdelta");
println!("cargo:rerun-if-changed=vendor/xdelta3");
println!("cargo:rerun-if-changed=src/gog/delta.c");
}
+1
View File
@@ -2,6 +2,7 @@
pub mod engine;
pub mod jni;
pub mod plan;
pub mod maintenance;
use std::fs::File;
use std::io::Read;
+78
View File
@@ -0,0 +1,78 @@
#include "xdelta3.h"
typedef struct { size_t used; } dd_budget;
typedef struct { size_t size; } dd_allocation;
static void *dd_alloc(void *opaque, size_t count, usize_t size) {
dd_budget *budget = opaque;
if (size && count > (62u * 1024u * 1024u) / size) return NULL;
size_t n = count * size;
if (n > 62u * 1024u * 1024u - budget->used) return NULL;
dd_allocation *allocation = malloc(sizeof(*allocation) + n);
if (!allocation) return NULL;
allocation->size = n;
budget->used += n;
return allocation + 1;
}
static void dd_free(void *opaque, void *ptr) {
if (!ptr) return;
dd_allocation *allocation = (dd_allocation *)ptr - 1;
((dd_budget *)opaque)->used -= allocation->size;
free(allocation);
}
int dd_gog_delta(const char *source, const char *patch, const char *target,
int (*cancel)(void *), void *opaque) {
FILE *old = NULL, *delta = NULL, *out = NULL;
xd3_stream stream = {0};
xd3_source src = {0};
xd3_config config = {0};
dd_budget budget = {0};
unsigned char *input = NULL, *block = NULL;
int result = -1, configured = 0;
old = fopen(source, "rb"); delta = fopen(patch, "rb"); out = fopen(target, "wb");
if (!old || !delta || !out) goto done;
input = malloc(64 * 1024); block = malloc(1024 * 1024);
if (!input || !block) goto done;
config.winsize = 64 * 1024;
config.alloc = dd_alloc; config.freef = dd_free; config.opaque = &budget;
if (xd3_config_stream(&stream, &config)) goto done;
configured = 1;
src.blksize = 1024 * 1024; src.curblk = block; src.curblkno = 0;
src.onblk = fread(block, 1, src.blksize, old);
if (xd3_set_source(&stream, &src)) goto done;
for (;;) {
size_t n = fread(input, 1, 64 * 1024, delta);
if (ferror(delta)) goto done;
if (feof(delta)) xd3_set_flags(&stream, XD3_FLUSH);
xd3_avail_input(&stream, input, n);
int code;
while ((code = xd3_decode_input(&stream)) != XD3_INPUT) {
if (cancel && cancel(opaque)) { result = 130; goto done; }
switch (code) {
case XD3_OUTPUT:
if (fwrite(stream.next_out, 1, stream.avail_out, out) != stream.avail_out) goto done;
xd3_consume_output(&stream); break;
case XD3_GETSRCBLK:
if (fseeko(old, (off_t)(src.getblkno * src.blksize), SEEK_SET)) goto done;
src.curblkno = src.getblkno;
src.onblk = fread(block, 1, src.blksize, old);
if (ferror(old)) goto done;
break;
case XD3_GOTHEADER: case XD3_WINSTART: case XD3_WINFINISH: break;
default: goto done;
}
}
if (feof(delta)) break;
}
if (xd3_close_stream(&stream) || fflush(out)) goto done;
result = 0;
done:
if (configured) xd3_free_stream(&stream);
if (old) fclose(old);
if (delta) fclose(delta);
if (out && fclose(out)) result = -1;
free(input); free(block);
return result;
}
+6 -1
View File
@@ -32,11 +32,14 @@ pub enum PlanKind {
#[default]
Gen2Chunks,
Gen1Ranges,
Maintenance,
}
impl PlanKind {
pub fn from_i32(v: i32) -> Self {
if v == 1 {
if v == 2 {
PlanKind::Maintenance
} else if v == 1 {
PlanKind::Gen1Ranges
} else {
PlanKind::Gen2Chunks
@@ -72,6 +75,7 @@ pub trait GogEvents: Sync {
);
fn on_log(&self, line: &str);
fn on_bytes(&self, _bytes_done: u64, _bytes_total: u64) {}
fn on_maintenance(&self, _network_bytes: u64, _reused_bytes: u64, _applied_bytes: u64) {}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
@@ -672,6 +676,7 @@ pub fn run(req: &GogRequest, cancel: &AtomicBool, events: &dyn GogEvents) -> Gog
match req.kind {
PlanKind::Gen2Chunks => run_gen2(req, cancel, events),
PlanKind::Gen1Ranges => run_gen1(req, cancel, events),
PlanKind::Maintenance => super::maintenance::run(req, cancel, events),
}
}
+10 -1
View File
@@ -153,6 +153,15 @@ impl GogEvents for JniEvents {
let _ = env.delete_local_ref(text);
});
}
fn on_maintenance(&self, network_bytes: u64, reused_bytes: u64, applied_bytes: u64) {
self.with_env(|env| {
let _ = env.call_method(self.listener.as_obj(), "onMaintenance", "(JJJ)V", &[
JValue::Long(network_bytes as jlong), JValue::Long(reused_bytes as jlong), JValue::Long(applied_bytes as jlong),
]);
Self::clear_exception(env);
});
}
}
fn jstring_to_string(env: &mut JNIEnv, value: &JString) -> Option<String> {
@@ -197,7 +206,7 @@ pub extern "system" fn Java_com_droiddeck_launcher_stores_gog_GogNative_nativePr
_env: JNIEnv,
_class: JClass,
) -> jint {
1
2
}
#[no_mangle]
@@ -0,0 +1,523 @@
use std::collections::{HashMap, HashSet};
use std::ffi::CString;
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Component, Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Mutex;
use serde_json::Value;
use super::engine::{GogEvents, GogRequest, GogRunResult, http_status_in, is_link_expiry_status};
use super::{md5_hex, md5_hex_file};
static DELTA_LOCK: Mutex<()> = Mutex::new(());
unsafe extern "C" {
fn dd_gog_delta(source: *const libc::c_char, patch: *const libc::c_char, target: *const libc::c_char,
cancel: extern "C" fn(*mut libc::c_void) -> libc::c_int, opaque: *mut libc::c_void) -> libc::c_int;
}
extern "C" fn cancelled(opaque: *mut libc::c_void) -> libc::c_int {
unsafe { (*(opaque as *const AtomicBool)).load(Ordering::Relaxed) as libc::c_int }
}
fn http_client(read_timeout: std::time::Duration) -> Result<reqwest::Client, String> {
reqwest::Client::builder()
.connect_timeout(std::time::Duration::from_secs(20))
.read_timeout(read_timeout)
.build().map_err(|e| e.to_string())
}
pub fn inside(root: &Path, relative: &str) -> Result<PathBuf, String> {
if relative.is_empty() || relative.contains(['\\', ':', '\0']) || relative.split('/').any(|p| p.is_empty() || p == "." || p == "..") { return Err("invalid GOG path".into()); }
let mut out = root.to_path_buf();
for component in Path::new(relative).components() {
let Component::Normal(part) = component else { return Err("GOG path leaves installation".into()) };
out.push(part);
if fs::symlink_metadata(&out).map(|m| m.file_type().is_symlink()).unwrap_or(false) {
return Err("GOG path is a symbolic link".into());
}
}
Ok(out)
}
fn text<'a>(v: &'a Value, key: &str) -> &'a str { v.get(key).and_then(Value::as_str).unwrap_or("") }
fn num(v: &Value, key: &str) -> u64 { v.get(key).and_then(Value::as_u64).unwrap_or(0) }
fn array<'a>(v: &'a Value, key: &str) -> &'a [Value] { v.get(key).and_then(Value::as_array).map(Vec::as_slice).unwrap_or(&[]) }
fn valid_md5(s: &str) -> bool { s.len() == 32 && s.bytes().all(|c| c.is_ascii_hexdigit()) }
fn matches(path: &Path, file: &Value) -> bool {
if !fs::metadata(path).map(|m| m.is_file() && m.len() == num(file, "size")).unwrap_or(false) { return false; }
let md5 = text(file, "md5");
if valid_md5(md5) { return md5_hex_file(path).map(|h| h.eq_ignore_ascii_case(md5)).unwrap_or(false); }
let sha = text(file, "sha256");
if sha.len() == 64 {
use sha2::{Digest, Sha256};
let Ok(mut input) = File::open(path) else { return false; };
let mut digest = Sha256::new(); let mut buffer = [0u8; 64 * 1024];
loop { let Ok(n) = input.read(&mut buffer) else { return false; }; if n == 0 { break; } digest.update(&buffer[..n]); }
return super::to_hex(&digest.finalize()).eq_ignore_ascii_case(sha);
}
let chunks = array(file, "chunks");
if chunks.is_empty() { return num(file, "size") == 0; }
let Ok(mut input) = File::open(path) else { return false; };
chunks.iter().all(|chunk| {
let size = num(chunk, "size");
valid_md5(text(chunk, "md5")) && hash_range(&mut input, size).as_deref() == Some(text(chunk, "md5"))
})
}
fn hash_range(input: &mut File, size: u64) -> Option<String> {
let mut digest = crate::md5_small::Md5::new();
let mut remaining = size;
let mut buffer = [0u8; 64 * 1024];
while remaining > 0 {
let want = remaining.min(buffer.len() as u64) as usize;
input.read_exact(&mut buffer[..want]).ok()?;
digest.update(&buffer[..want]); remaining -= want as u64;
}
Some(super::to_hex(&digest.finalize()))
}
#[derive(Clone)]
struct Reuse { path: PathBuf, offset: u64, size: u64 }
type ReuseMap = HashMap<(String, u64), Vec<Reuse>>;
fn reuse_map(root: &Path, files: &[Value]) -> Result<ReuseMap, String> {
let mut result: ReuseMap = HashMap::new();
for file in files {
if text(file, "path").to_lowercase().starts_with(".droiddeck-") { return Err("GOG content overlaps launcher metadata".into()); }
let path = inside(root, text(file, "path"))?;
let mut offset: u64 = 0;
for chunk in array(file, "chunks") {
let size = num(chunk, "size");
if valid_md5(text(chunk, "md5")) {
result.entry((text(chunk, "md5").to_ascii_lowercase(), size)).or_default()
.push(Reuse { path: path.clone(), offset, size });
}
offset = offset.checked_add(size).ok_or("GOG file size overflow")?;
}
}
Ok(result)
}
fn reusable(chunk: &Value, map: &ReuseMap) -> Option<Reuse> {
let key = (text(chunk, "md5").to_ascii_lowercase(), num(chunk, "size"));
map.get(&key)?.iter().find(|old| {
let Ok(mut file) = File::open(&old.path) else { return false; };
file.seek(SeekFrom::Start(old.offset)).is_ok() && hash_range(&mut file, old.size).map(|h| h.eq_ignore_ascii_case(&key.0)).unwrap_or(false)
}).cloned()
}
struct Transfer<'a> {
runtime: tokio::runtime::Runtime,
client: reqwest::Client,
cancel: &'a AtomicBool,
events: &'a dyn GogEvents,
wire: u64,
total: u64,
}
impl Transfer<'_> {
fn fetch(&mut self, urls: &[String], range: Option<(u64, u64)>, dest: &Path, limit: u64) -> Result<(), String> {
let mut failure = "GOG download has no URLs".to_string();
for _ in 0..3 {
for url in urls {
if self.cancel.load(Ordering::Relaxed) { return Err("cancelled".into()); }
let result = self.runtime.block_on(async {
let mut request = self.client.get(url).header("User-Agent", "GOG Galaxy");
if let Some((start, end)) = range { request = request.header("Range", format!("bytes={start}-{end}")); }
let mut response = request.send().await.map_err(|_| "GOG connection failed".to_string())?;
let status = response.status().as_u16();
if status != if range.is_some() { 206 } else { 200 } { return Err(format!("HTTP {status}")); }
let mut output = File::create(dest).map_err(|e| e.to_string())?;
let mut size = 0u64;
while let Some(bytes) = response.chunk().await.map_err(|_| "GOG connection interrupted".to_string())? {
if self.cancel.load(Ordering::Relaxed) { return Err("cancelled".into()); }
size += bytes.len() as u64;
if limit > 0 && size > limit { return Err("GOG response exceeds expected size".into()); }
self.wire += bytes.len() as u64;
self.events.on_bytes(self.wire, self.total.max(self.wire));
output.write_all(&bytes).map_err(|e| e.to_string())?;
}
if limit > 0 && size != limit { return Err("GOG download is incomplete".into()); }
output.sync_all().map_err(|e| e.to_string())
});
match result { Ok(()) => return Ok(()), Err(e) => failure = e }
if http_status_in(&failure).map(is_link_expiry_status).unwrap_or(false) { return Err(failure); }
}
}
Err(failure)
}
}
fn urls(links: &Value, product: &str, suffix: &str) -> Vec<String> {
links.get(product).and_then(Value::as_array).map(|bases| bases.iter().filter_map(Value::as_str).map(|base| {
if base.contains("{GALAXY_PATH}") { base.replace("{GALAXY_PATH}", suffix) }
else if let Some((path, query)) = base.split_once('?') { format!("{}{suffix}?{query}", path.trim_end_matches('/').to_string() + "/") }
else { format!("{}{suffix}", base.trim_end_matches('/').to_string() + "/") }
}).collect()).unwrap_or_default()
}
fn chunk_path(chunk: &Value) -> Result<String, String> {
let hash = text(chunk, "compressedMd5");
if !valid_md5(hash) { return Err("GOG chunk has no valid compressed hash".into()); }
Ok(super::plan::build_cdn_path(hash))
}
fn download_chunk(transfer: &mut Transfer, links: &Value, product: &str, chunk: &Value, scratch: &Path, output: &mut File) -> Result<(), String> {
transfer.fetch(&urls(links, product, &chunk_path(chunk)?), None, scratch, num(chunk, "compressedSize"))?;
if !md5_hex_file(scratch).map(|h| h.eq_ignore_ascii_case(text(chunk, "compressedMd5"))).unwrap_or(false) { return Err("GOG compressed chunk checksum mismatch".into()); }
let mut raw = File::open(scratch).map_err(|e| e.to_string())?;
let mut head = [0u8; 1]; raw.read_exact(&mut head).map_err(|e| e.to_string())?;
raw.seek(SeekFrom::Start(0)).map_err(|e| e.to_string())?;
let stored = num(chunk, "size") == num(chunk, "compressedSize") && text(chunk, "md5").eq_ignore_ascii_case(text(chunk, "compressedMd5"));
let input: Box<dyn Read> = if !stored && head[0] == 0x78 { Box::new(flate2::read::ZlibDecoder::new(raw)) } else { Box::new(raw) };
let mut input = input.take(num(chunk, "size") + 1);
let mut digest = crate::md5_small::Md5::new();
let mut size = 0u64;
let mut buffer = [0u8; 64 * 1024];
loop {
let n = input.read(&mut buffer).map_err(|_| "GOG chunk decompression failed")?;
if n == 0 { break; }
if transfer.cancel.load(Ordering::Relaxed) { return Err("cancelled".into()); }
digest.update(&buffer[..n]); size += n as u64;
output.write_all(&buffer[..n]).map_err(|e| e.to_string())?;
}
if size != num(chunk, "size") || !super::to_hex(&digest.finalize()).eq_ignore_ascii_case(text(chunk, "md5")) { return Err("GOG chunk checksum mismatch".into()); }
Ok(())
}
fn apply_delta(root: &Path, file: &Value, patch: &Value, plan: &Value, stage: &Path, transfer: &mut Transfer) -> Result<(), String> {
let _guard = DELTA_LOCK.lock().map_err(|_| "GOG delta lock poisoned")?;
let source = inside(root, &text(patch, "path_source").replace('\\', "/"))?;
if !md5_hex_file(&source).map(|h| h.eq_ignore_ascii_case(text(patch, "md5_source"))).unwrap_or(false) { return Err("GOG delta source changed".into()); }
let payload = stage.with_extension("delta");
let packed = stage.with_extension("packed");
let mut output = File::create(&payload).map_err(|e| e.to_string())?;
for chunk in array(patch, "chunks") { download_chunk(transfer, &plan["patchLinks"], text(patch, "product"), chunk, &packed, &mut output)?; }
drop(output);
if !md5_hex_file(&payload).map(|h| h.eq_ignore_ascii_case(text(patch, "md5"))).unwrap_or(false) { return Err("GOG delta checksum mismatch".into()); }
let cpath = |p: &Path| CString::new(p.as_os_str().as_encoded_bytes()).map_err(|_| "invalid path");
let (s, p, t) = (cpath(&source)?, cpath(&payload)?, cpath(stage)?);
let code = unsafe { dd_gog_delta(s.as_ptr(), p.as_ptr(), t.as_ptr(), cancelled, transfer.cancel as *const AtomicBool as *mut libc::c_void) };
let _ = fs::remove_file(payload); let _ = fs::remove_file(packed);
if code != 0 || !matches(stage, file) || !md5_hex_file(stage).map(|h| h.eq_ignore_ascii_case(text(patch, "md5_target"))).unwrap_or(false) { return Err("GOG delta reconstruction failed".into()); }
Ok(())
}
pub fn run(req: &GogRequest, cancel: &AtomicBool, events: &dyn GogEvents) -> GogRunResult {
let mut result = GogRunResult::default();
let work = || -> Result<(u64, u32), String> {
let plan: Value = serde_json::from_str(req.depot_manifests.first().ok_or("missing GOG maintenance plan")?).map_err(|_| "invalid GOG maintenance plan")?;
let root = fs::canonicalize(&req.install_dir).map_err(|e| e.to_string())?;
let files = array(&plan, "files");
let mut seen = HashSet::new();
let mut components = HashMap::new();
for file in files {
let name = text(file, "path"); inside(&root, name)?;
if name.to_lowercase().starts_with(".droiddeck-") || !seen.insert(name.to_lowercase()) { return Err("ambiguous GOG path".into()); }
let mut parent = root.clone();
let mut relative = PathBuf::new();
for part in name.split('/') {
relative.push(part);
let key = relative.to_string_lossy().to_lowercase();
if let Some(previous) = components.insert(key, relative.clone()) {
if previous != relative { return Err("ambiguous GOG directory".into()); }
}
if let Ok(entries) = fs::read_dir(&parent) {
for entry in entries.flatten() {
let actual = entry.file_name().to_string_lossy().into_owned();
if actual != part && actual.to_lowercase() == part.to_lowercase() { return Err("existing path differs only by case".into()); }
}
}
parent.push(part);
}
}
let map = reuse_map(&root, array(&plan, "oldFiles"))?;
let stages = inside(&root, ".droiddeck-gog/stage")?;
fs::create_dir_all(&stages).map_err(|e| e.to_string())?;
let mut transfer = Transfer {
runtime: tokio::runtime::Builder::new_current_thread().enable_all().build().map_err(|e| e.to_string())?,
client: http_client(std::time::Duration::from_secs(60))?,
cancel, events, wire: 0, total: num(&plan, "downloadBytes"),
};
let mut done = 0u32; let mut reused = 0u64; let mut applied = 0u64;
for file in files {
if cancel.load(Ordering::Relaxed) { return Err("cancelled".into()); }
let path = inside(&root, text(file, "path"))?;
if file.get("directory").and_then(Value::as_bool).unwrap_or(false) {
fs::create_dir_all(&path).map_err(|e| e.to_string())?; done += 1; continue;
}
if matches(&path, file) { done += 1; events.on_file_done(text(file, "path"), num(file, "size"), true, done, files.len() as u32, transfer.wire, transfer.total); continue; }
let stage = stages.join(md5_hex(text(file, "path").as_bytes()));
if !matches(&stage, file) {
let mut start = 0;
let missing_bytes: u64 = array(file, "chunks").iter().map(|chunk| {
let valid = File::open(&stage).ok().map(|mut input| input.seek(SeekFrom::Start(start)).is_ok() &&
hash_range(&mut input, num(chunk, "size")).map(|h| h.eq_ignore_ascii_case(text(chunk, "md5"))).unwrap_or(false)).unwrap_or(false);
start += num(chunk, "size");
if valid || reusable(chunk, &map).is_some() { 0 } else { num(chunk, "compressedSize") }
}).sum();
let patch = array(&plan, "patches").iter().find(|p| text(p, "path_target").replace('\\', "/") == text(file, "path") &&
array(p, "chunks").iter().map(|c| num(c, "compressedSize")).sum::<u64>() < missing_bytes);
let delta_ok = if let Some(patch) = patch {
match apply_delta(&root, file, patch, &plan, &stage, &mut transfer) {
Ok(()) => true,
Err(e) => { if cancel.load(Ordering::Relaxed) || http_status_in(&e).map(is_link_expiry_status).unwrap_or(false) { return Err(e); } events.on_log(&format!("gog: delta fallback file={} reason={e}", text(file, "path"))); false }
}
} else { false };
if !delta_ok {
let chunks = array(file, "chunks");
if chunks.is_empty() && num(file, "size") > 0 {
let offset = num(file, "offset");
let remote = text(file, "remotePath");
let endpoints = if remote.starts_with("https://") { vec![remote.to_string()] } else { urls(&plan["links"], text(file, "product"), remote.trim_start_matches('/')) };
transfer.fetch(&endpoints, Some((offset, offset + num(file, "size") - 1)), &stage, num(file, "size"))?;
} else {
let mut output = OpenOptions::new().create(true).truncate(false).read(true).write(true).open(&stage).map_err(|e| e.to_string())?;
let mut offset = 0u64;
for chunk in chunks {
if cancel.load(Ordering::Relaxed) { return Err("cancelled".into()); }
output.seek(SeekFrom::Start(offset)).map_err(|e| e.to_string())?;
let valid_stage = hash_range(&mut output, num(chunk, "size")).map(|h| h.eq_ignore_ascii_case(text(chunk, "md5"))).unwrap_or(false);
output.seek(SeekFrom::Start(offset)).map_err(|e| e.to_string())?;
if valid_stage { reused += num(chunk, "size"); }
else if let Some(old) = reusable(chunk, &map) {
let mut source = File::open(old.path).map_err(|e| e.to_string())?;
source.seek(SeekFrom::Start(old.offset)).map_err(|e| e.to_string())?;
std::io::copy(&mut source.take(old.size), &mut output).map_err(|e| e.to_string())?;
reused += old.size;
} else {
download_chunk(&mut transfer, &plan["links"], text(file, "product"), chunk, &stage.with_extension("packed"), &mut output)?;
}
offset += num(chunk, "size");
}
output.set_len(num(file, "size")).map_err(|e| e.to_string())?;
output.sync_all().map_err(|e| e.to_string())?;
}
}
}
let unhashed_range = array(file, "chunks").is_empty() && !valid_md5(text(file, "md5")) && text(file, "sha256").is_empty();
if !matches(&stage, file) && !(unhashed_range && fs::metadata(&stage).map(|m| m.len() == num(file, "size")).unwrap_or(false)) { return Err(format!("GOG target verification failed: {}", text(file, "path"))); }
if cancel.load(Ordering::Relaxed) { return Err("cancelled".into()); }
inside(&root, text(file, "path"))?;
fs::create_dir_all(path.parent().ok_or("GOG file has no parent")?).map_err(|e| e.to_string())?;
File::open(&stage).and_then(|f| f.sync_all()).map_err(|e| e.to_string())?;
fs::rename(&stage, &path).map_err(|e| e.to_string())?;
applied += num(file, "size"); done += 1;
events.on_file_done(text(file, "path"), num(file, "size"), false, done, files.len() as u32, transfer.wire, transfer.total);
events.on_maintenance(transfer.wire, reused, applied);
events.on_log(&format!("gog: maintenance wire={} reused={reused} applied={applied} files={done}", transfer.wire));
}
for old in array(&plan, "oldFiles") {
if cancel.load(Ordering::Relaxed) { return Err("cancelled".into()); }
if !seen.contains(&text(old, "path").to_lowercase()) {
let path = inside(&root, text(old, "path"))?;
if matches(&path, old) { fs::remove_file(path).map_err(|e| e.to_string())?; }
}
}
events.on_log(&format!("gog: maintenance complete wire={} reused={reused} applied={applied}", transfer.wire));
Ok((transfer.wire, done))
};
match work() {
Ok((bytes, files)) => { result.success = true; result.bytes_written = bytes; result.files_done = files; }
Err(error) => { result.cancelled = cancel.load(Ordering::Relaxed); result.link_expiry = http_status_in(&error).map(is_link_expiry_status).unwrap_or(false); result.error = error; }
}
result
}
#[cfg(test)]
mod tests {
use super::*;
use flate2::{write::ZlibEncoder, Compression};
use serde_json::json;
use std::net::TcpListener;
use std::sync::{Arc, atomic::AtomicU64};
struct Events;
impl GogEvents for Events {
fn on_file_done(&self, _: &str, _: u64, _: bool, _: u32, _: u32, _: u64, _: u64) {}
fn on_log(&self, _: &str) {}
}
struct Temp(PathBuf);
impl Temp {
fn new() -> Self {
let path = std::env::temp_dir().join(format!("gog-maintenance-{}-{}", std::process::id(), std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos()));
fs::create_dir_all(&path).unwrap(); Self(path)
}
}
impl Drop for Temp { fn drop(&mut self) { let _ = fs::remove_dir_all(&self.0); } }
struct Server { base: String, bytes: Arc<AtomicU64>, stop: Arc<AtomicBool>, thread: Option<std::thread::JoinHandle<()>> }
impl Server {
fn new(payloads: HashMap<String, Vec<u8>>, range: bool) -> Self {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let base = format!("http://{}", listener.local_addr().unwrap());
listener.set_nonblocking(true).unwrap();
let stop = Arc::new(AtomicBool::new(false)); let bytes = Arc::new(AtomicU64::new(0));
let (stopped, count) = (stop.clone(), bytes.clone());
let thread = std::thread::spawn(move || {
while !stopped.load(Ordering::Relaxed) {
if let Ok((mut stream, _)) = listener.accept() {
stream.set_read_timeout(Some(std::time::Duration::from_secs(2))).unwrap();
let mut request = Vec::new(); let mut one = [0u8];
while stream.read(&mut one).unwrap_or(0) > 0 { request.push(one[0]); if request.ends_with(b"\r\n\r\n") { break; } }
let request = String::from_utf8_lossy(&request);
let path = request.split_whitespace().nth(1).unwrap_or("");
if let Some(body) = payloads.get(path) {
let status = if range { "206 Partial Content" } else { "200 OK" };
let _ = write!(stream, "HTTP/1.1 {status}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", body.len());
if stream.write_all(body).is_ok() { count.fetch_add(body.len() as u64, Ordering::Relaxed); }
} else { let _ = stream.write_all(b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"); }
} else { std::thread::sleep(std::time::Duration::from_millis(2)); }
}
});
Self { base, bytes, stop, thread: Some(thread) }
}
}
impl Drop for Server { fn drop(&mut self) { self.stop.store(true, Ordering::Relaxed); if let Some(thread) = self.thread.take() { thread.join().unwrap(); } } }
#[test]
fn continuing_download_may_exceed_the_read_timeout_in_total() {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let url = format!("http://{}", listener.local_addr().unwrap());
let server = std::thread::spawn(move || {
let (mut stream, _) = listener.accept().unwrap();
stream.set_read_timeout(Some(std::time::Duration::from_secs(3))).unwrap();
let mut request = Vec::new(); let mut byte = [0u8];
while stream.read(&mut byte).unwrap() > 0 { request.push(byte[0]); if request.ends_with(b"\r\n\r\n") { break; } }
stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\n").unwrap();
for byte in b"abcd" {
stream.write_all(&[*byte]).unwrap(); stream.flush().unwrap();
std::thread::sleep(std::time::Duration::from_millis(400));
}
});
let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
let client = http_client(std::time::Duration::from_secs(1)).unwrap();
let body = runtime.block_on(async { client.get(url).send().await.unwrap().bytes().await.unwrap() });
assert_eq!(&body[..], b"abcd");
server.join().unwrap();
}
fn chunk(data: &[u8]) -> (Value, String, Vec<u8>) {
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default()); encoder.write_all(data).unwrap();
let packed = encoder.finish().unwrap(); let hash = md5_hex(&packed);
(json!({"md5": md5_hex(data), "size": data.len(), "compressedMd5": hash, "compressedSize": packed.len()}),
format!("/{}", super::super::plan::build_cdn_path(&hash)), packed)
}
fn execute(root: &Path, files: Value, old: Value, server: &Server) -> GogRunResult {
let plan = json!({"files": files, "oldFiles": old, "patches": [], "links": {"1": [server.base]}, "downloadBytes": 4096});
run(&GogRequest { install_dir: root.to_string_lossy().into_owned(), depot_manifests: vec![plan.to_string()], ..Default::default() }, &AtomicBool::new(false), &Events)
}
#[test]
fn reuses_moved_and_repeated_chunks_and_downloads_only_missing_bytes() {
let temp = Temp::new();
let (a, _, _) = chunk(b"aaaa"); let (b, path, packed) = chunk(b"bbbb");
fs::write(temp.0.join("archive"), b"aaaaaaaa").unwrap();
let server = Server::new(HashMap::from([(path, packed.clone())]), false);
let old = json!([{"path":"archive", "size":8, "md5":md5_hex(b"aaaaaaaa"), "chunks":[a,a]}]);
let files = json!([{"path":"archive", "size":12, "md5":md5_hex(b"bbbbaaaaaaaa"), "chunks":[b,a,a], "product":"1"}]);
let result = execute(&temp.0, files, old, &server);
assert!(result.success, "{}", result.error);
assert_eq!(fs::read(temp.0.join("archive")).unwrap(), b"bbbbaaaaaaaa");
assert_eq!(server.bytes.load(Ordering::Relaxed), packed.len() as u64);
assert_eq!(result.bytes_written, packed.len() as u64);
}
#[test]
fn revalidates_partial_staging_and_preserves_original_until_verified() {
let temp = Temp::new(); let (a, _, _) = chunk(b"aaaa"); let (b, path, packed) = chunk(b"bbbb");
let stages = temp.0.join(".droiddeck-gog/stage"); fs::create_dir_all(&stages).unwrap();
fs::write(temp.0.join("archive"), b"original").unwrap();
fs::write(stages.join(md5_hex(b"archive")), b"aaaabadd").unwrap();
let server = Server::new(HashMap::from([(path, packed.clone())]), false);
let files = json!([{"path":"archive", "size":8, "md5":md5_hex(b"aaaabbbb"), "chunks":[a,b], "product":"1"}]);
let result = execute(&temp.0, files, json!([]), &server);
assert!(result.success, "{}", result.error);
assert_eq!(server.bytes.load(Ordering::Relaxed), packed.len() as u64);
assert_eq!(fs::read(temp.0.join("archive")).unwrap(), b"aaaabbbb");
}
#[test]
fn unchanged_files_need_no_network_and_obsolete_modified_files_survive() {
let temp = Temp::new(); let (a, _, _) = chunk(b"aaaa");
fs::write(temp.0.join("current"), b"aaaa").unwrap();
fs::write(temp.0.join("obsolete"), b"old").unwrap(); fs::write(temp.0.join("modded"), b"mod").unwrap();
let server = Server::new(HashMap::new(), false);
let files = json!([{"path":"current", "size":4, "md5":md5_hex(b"aaaa"), "chunks":[a], "product":"1"}, {"path":"empty", "size":0, "chunks":[], "product":"1"}]);
let old = json!([{"path":"obsolete", "size":3, "md5":md5_hex(b"old")}, {"path":"modded", "size":3, "md5":md5_hex(b"old")}]);
let result = execute(&temp.0, files, old, &server);
assert!(result.success, "{}", result.error);
assert_eq!(server.bytes.load(Ordering::Relaxed), 0);
assert!(!temp.0.join("obsolete").exists()); assert!(temp.0.join("modded").exists());
assert_eq!(fs::metadata(temp.0.join("empty")).unwrap().len(), 0);
}
#[test]
fn gen1_same_size_corruption_is_replaced_using_hash_and_range() {
let temp = Temp::new(); fs::write(temp.0.join("game"), b"bad!").unwrap();
let server = Server::new(HashMap::from([("/range".to_string(), b"good".to_vec())]), true);
let files = json!([{"path":"game", "size":4, "md5":md5_hex(b"good"), "chunks":[], "product":"1", "remotePath":"range", "offset":10}]);
let result = execute(&temp.0, files, json!([]), &server);
assert!(result.success, "{}", result.error); assert_eq!(fs::read(temp.0.join("game")).unwrap(), b"good");
assert_eq!(server.bytes.load(Ordering::Relaxed), 4);
}
#[test]
fn bad_chunks_never_replace_original_and_expired_links_are_reported() {
let temp = Temp::new(); fs::write(temp.0.join("game"), b"old!").unwrap();
let (a, path, mut packed) = chunk(b"new!"); packed[0] ^= 1;
let server = Server::new(HashMap::from([(path, packed)]), false);
let files = json!([{"path":"game", "size":4, "md5":md5_hex(b"new!"), "chunks":[a], "product":"1"}]);
let result = execute(&temp.0, files.clone(), json!([]), &server);
assert!(!result.success); assert_eq!(fs::read(temp.0.join("game")).unwrap(), b"old!");
let missing = Server::new(HashMap::new(), false);
let result = execute(&temp.0, files, json!([]), &missing);
assert!(result.link_expiry, "{}", result.error);
}
#[test]
fn rejects_traversal_symlinks_and_case_collisions_before_fetching() {
let temp = Temp::new(); let outside = Temp::new();
std::os::unix::fs::symlink(&outside.0, temp.0.join("linked")).unwrap();
for path in ["../escape", "/absolute", "a/./b", "a//b", "linked/game"] { assert!(inside(&temp.0, path).is_err(), "{path}"); }
let server = Server::new(HashMap::new(), false);
let result = execute(&temp.0, json!([{"path":"Game"}, {"path":"game"}]), json!([]), &server);
assert!(!result.success); assert_eq!(server.bytes.load(Ordering::Relaxed), 0);
}
#[test]
fn valid_xdelta_downloads_only_delta_and_corrupt_source_falls_back() {
let old = b"the old archive contents\n".repeat(4096);
let mut new = b"the new archive contents\n".to_vec(); new.extend_from_slice(&old); new.extend_from_slice(b"END\n");
let delta = include_bytes!("fixtures/update.vcdiff");
let (patch_chunk, patch_url, packed_delta) = chunk(delta);
let (target_chunk, target_url, packed_target) = chunk(&new);
for mode in 0..3 {
let corrupt = mode == 1;
let temp = Temp::new();
fs::write(temp.0.join("game"), if corrupt { vec![b'x'; old.len()] } else { old.clone() }).unwrap();
let (delta_chunk, delta_url, delta_body) = if mode == 2 { chunk(b"unsupported VCDIFF") } else { (patch_chunk.clone(), patch_url.clone(), packed_delta.clone()) };
let server = Server::new(HashMap::from([(delta_url, delta_body.clone()), (target_url.clone(), packed_target.clone())]), false);
let plan = json!({"files":[{"path":"game", "product":"1", "size":new.len(), "md5":md5_hex(&new), "chunks":[target_chunk]}],
"oldFiles":[{"path":"game", "size":old.len(), "md5":md5_hex(&old), "chunks":[]}],
"patches":[{"path_source":"game", "path_target":"game", "product":"1", "md5_source":md5_hex(&old), "md5_target":md5_hex(&new), "md5":if mode == 2 { md5_hex(b"unsupported VCDIFF") } else { md5_hex(delta) }, "chunks":[delta_chunk]}],
"links":{"1":[server.base]}, "patchLinks":{"1":[server.base]}, "downloadBytes":packed_target.len()});
let result = run(&GogRequest {install_dir:temp.0.to_string_lossy().into_owned(), depot_manifests:vec![plan.to_string()], ..Default::default()}, &AtomicBool::new(false), &Events);
assert!(result.success, "{}", result.error);
assert_eq!(fs::read(temp.0.join("game")).unwrap(), new);
assert_eq!(server.bytes.load(Ordering::Relaxed), match mode { 1 => packed_target.len(), 2 => delta_body.len() + packed_target.len(), _ => packed_delta.len() } as u64);
}
}
#[test]
fn stored_chunks_starting_with_x_are_not_mistaken_for_zlib() {
let temp = Temp::new(); let raw = b"xyz stored data";
let hash = md5_hex(raw);
let path = format!("/{}", super::super::plan::build_cdn_path(&hash));
let server = Server::new(HashMap::from([(path, raw.to_vec())]), false);
let result = execute(&temp.0, json!([{"path":"game", "product":"1", "size":raw.len(), "md5":hash,
"chunks":[{"md5":hash,"compressedMd5":hash,"size":raw.len(),"compressedSize":raw.len()}]}]), json!([]), &server);
assert!(result.success, "{}", result.error);
assert_eq!(fs::read(temp.0.join("game")).unwrap(), raw);
}
}
+176
View File
@@ -0,0 +1,176 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
@@ -0,0 +1,7 @@
Xdelta3 decoder from jmacd/xdelta revision 0525275fe4b553a10f38e455d30c60dc6ed9b45d (release3_0_apl), the
Apache-2.0 revision used by heroic-gogdl. Upstream sources are unmodified.
https://github.com/jmacd/xdelta/tree/0525275/xdelta3
DroidDeck's streaming wrapper is src/gog/delta.c. Encoding and secondary
compression are disabled, matching heroic-gogdl's decoder baseline. Unsupported
streams fall back to verified Galaxy chunks. No Python runtime is required.
+171
View File
@@ -0,0 +1,171 @@
/* xdelta3 - delta compression tools and library
Copyright 2016 Joshua MacDonald
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/******************************************************************
SOFT string matcher
******************************************************************/
#if XD3_BUILD_SOFT
#define TEMPLATE soft
#define LLOOK stream->smatcher.large_look
#define LSTEP stream->smatcher.large_step
#define SLOOK stream->smatcher.small_look
#define SCHAIN stream->smatcher.small_chain
#define SLCHAIN stream->smatcher.small_lchain
#define MAXLAZY stream->smatcher.max_lazy
#define LONGENOUGH stream->smatcher.long_enough
#define SOFTCFG 1
#include "xdelta3.c"
#undef SOFTCFG
#undef TEMPLATE
#undef LLOOK
#undef SLOOK
#undef LSTEP
#undef SCHAIN
#undef SLCHAIN
#undef MAXLAZY
#undef LONGENOUGH
#endif
#define SOFTCFG 0
/************************************************************
FASTEST string matcher
**********************************************************/
#if XD3_BUILD_FASTEST
#define TEMPLATE fastest
#define LLOOK 9
#define LSTEP 26
#define SLOOK 4U
#define SCHAIN 1
#define SLCHAIN 1
#define MAXLAZY 6
#define LONGENOUGH 6
#include "xdelta3.c"
#undef TEMPLATE
#undef LLOOK
#undef SLOOK
#undef LSTEP
#undef SCHAIN
#undef SLCHAIN
#undef MAXLAZY
#undef LONGENOUGH
#endif
/************************************************************
FASTER string matcher
**********************************************************/
#if XD3_BUILD_FASTER
#define TEMPLATE faster
#define LLOOK 9
#define LSTEP 15
#define SLOOK 4U
#define SCHAIN 1
#define SLCHAIN 1
#define MAXLAZY 18
#define LONGENOUGH 18
#include "xdelta3.c"
#undef TEMPLATE
#undef LLOOK
#undef SLOOK
#undef LSTEP
#undef SCHAIN
#undef SLCHAIN
#undef MAXLAZY
#undef LONGENOUGH
#endif
/******************************************************
FAST string matcher
********************************************************/
#if XD3_BUILD_FAST
#define TEMPLATE fast
#define LLOOK 9
#define LSTEP 8
#define SLOOK 4U
#define SCHAIN 4
#define SLCHAIN 1
#define MAXLAZY 18
#define LONGENOUGH 35
#include "xdelta3.c"
#undef TEMPLATE
#undef LLOOK
#undef SLOOK
#undef LSTEP
#undef SCHAIN
#undef SLCHAIN
#undef MAXLAZY
#undef LONGENOUGH
#endif
/**************************************************
SLOW string matcher
**************************************************************/
#if XD3_BUILD_SLOW
#define TEMPLATE slow
#define LLOOK 9
#define LSTEP 2
#define SLOOK 4U
#define SCHAIN 44
#define SLCHAIN 13
#define MAXLAZY 90
#define LONGENOUGH 70
#include "xdelta3.c"
#undef TEMPLATE
#undef LLOOK
#undef SLOOK
#undef LSTEP
#undef SCHAIN
#undef SLCHAIN
#undef MAXLAZY
#undef LONGENOUGH
#endif
/********************************************************
DEFAULT string matcher
************************************************************/
#if XD3_BUILD_DEFAULT
#define TEMPLATE default
#define LLOOK 9
#define LSTEP 3
#define SLOOK 4U
#define SCHAIN 8
#define SLCHAIN 2
#define MAXLAZY 36
#define LONGENOUGH 70
#include "xdelta3.c"
#undef TEMPLATE
#undef LLOOK
#undef SLOOK
#undef LSTEP
#undef SCHAIN
#undef SLCHAIN
#undef MAXLAZY
#undef LONGENOUGH
#endif
File diff suppressed because it is too large. Load diff
+167
View File
@@ -0,0 +1,167 @@
/* xdelta3 - delta compression tools and library
Copyright 2016 Joshua MacDonald
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef _XDELTA3_HASH_H_
#define _XDELTA3_HASH_H_
#if XD3_DEBUG
#define SMALL_HASH_DEBUG1(s,inp) \
uint32_t debug_state; \
uint32_t debug_hval = xd3_checksum_hash (& (s)->small_hash, \
xd3_scksum (&debug_state, (inp), (s)->smatcher.small_look))
#define SMALL_HASH_DEBUG2(s,inp) \
XD3_ASSERT (debug_hval == xd3_checksum_hash (& (s)->small_hash, \
xd3_scksum (&debug_state, (inp), (s)->smatcher.small_look)))
#else
#define SMALL_HASH_DEBUG1(s,inp)
#define SMALL_HASH_DEBUG2(s,inp)
#endif /* XD3_DEBUG */
/* This is a good hash multiplier for 32-bit LCGs: see "linear
* congruential generators of different sizes and good lattice
* structure" */
static const uint32_t hash_multiplier = 1597334677U;
/***********************************************************************
Permute stuff
***********************************************************************/
#if HASH_PERMUTE == 0
#define PERMUTE(x) (x)
#else
#define PERMUTE(x) (__single_hash[(uint32_t)x])
extern const uint16_t __single_hash[256];
#endif
/* Update the checksum state. */
#if ADLER_LARGE_CKSUM
inline uint32_t
xd3_large_cksum_update (uint32_t cksum,
const uint8_t *base,
usize_t look) {
uint32_t old_c = PERMUTE(base[0]);
uint32_t new_c = PERMUTE(base[look]);
uint32_t low = ((cksum & 0xffff) - old_c + new_c) & 0xffff;
uint32_t high = ((cksum >> 16) - (old_c * look) + low) & 0xffff;
return (high << 16) | low;
}
#else
/* TODO: revisit this topic */
#endif
#if UNALIGNED_OK
#define UNALIGNED_READ32(dest,src) (*(dest)) = (*(uint32_t*)(src))
#else
#define UNALIGNED_READ32(dest,src) memcpy((dest), (src), 4);
#endif
/* TODO: small cksum is hard-coded for 4 bytes (i.e., "look" is unused) */
static inline uint32_t
xd3_scksum (uint32_t *state,
const uint8_t *base,
const usize_t look)
{
UNALIGNED_READ32(state, base);
return (*state) * hash_multiplier;
}
static inline uint32_t
xd3_small_cksum_update (uint32_t *state,
const uint8_t *base,
usize_t look)
{
UNALIGNED_READ32(state, base+1);
return (*state) * hash_multiplier;
}
/***********************************************************************
Ctable stuff
***********************************************************************/
static inline usize_t
xd3_checksum_hash (const xd3_hash_cfg *cfg, const usize_t cksum)
{
return (cksum >> cfg->shift) ^ (cksum & cfg->mask);
}
/***********************************************************************
Cksum function
***********************************************************************/
#if ADLER_LARGE_CKSUM
static inline uint32_t
xd3_lcksum (const uint8_t *seg, const usize_t ln)
{
usize_t i = 0;
uint32_t low = 0;
uint32_t high = 0;
for (; i < ln; i += 1)
{
low += PERMUTE(*seg++);
high += low;
}
return ((high & 0xffff) << 16) | (low & 0xffff);
}
#else
static inline uint32_t
xd3_lcksum (const uint8_t *seg, const usize_t ln)
{
usize_t i, j;
uint32_t h = 0;
for (i = 0, j = ln - 1; i < ln; ++i, --j) {
h += PERMUTE(seg[i]) * hash_multiplier_powers[j];
}
return h;
}
#endif
#if XD3_ENCODER
static usize_t
xd3_size_log2 (usize_t slots)
{
int bits = 28; /* This should not be an unreasonable limit. */
int i;
for (i = 3; i <= bits; i += 1)
{
if (slots < (1U << i))
{
/* TODO: this is compaction=1 in checksum_test.cc and maybe should
* not be fixed at -1. */
bits = i - 1;
break;
}
}
return bits;
}
static void
xd3_size_hashtable (xd3_stream *stream,
usize_t slots,
xd3_hash_cfg *cfg)
{
int bits = xd3_size_log2 (slots);
/* TODO: there's a 32-bit assumption here */
cfg->size = (1 << bits);
cfg->mask = (cfg->size - 1);
cfg->shift = 32 - bits;
}
#endif
#endif
@@ -0,0 +1,370 @@
/* xdelta3 - delta compression tools and library
Copyright 2016 Joshua MacDonald
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef XDELTA3_INTERNAL_H__
#define XDELTA3_INTERNAL_H__
#include "xdelta3.h"
typedef struct _main_file main_file;
typedef struct _main_extcomp main_extcomp;
void main_buffree (void *ptr);
void* main_bufalloc (size_t size);
void main_file_init (main_file *xfile);
int main_file_close (main_file *xfile);
void main_file_cleanup (main_file *xfile);
int main_file_isopen (main_file *xfile);
int main_file_open (main_file *xfile, const char* name, int mode);
int main_file_exists (main_file *xfile);
int xd3_whole_append_window (xd3_stream *stream);
int xd3_main_cmdline (int argc, char **argv);
int main_file_read (main_file *ifile,
uint8_t *buf,
size_t size,
size_t *nread,
const char *msg);
int main_file_write (main_file *ofile, uint8_t *buf,
usize_t size, const char *msg);
int test_compare_files (const char* f0, const char* f1);
usize_t xd3_bytes_on_srcblk (xd3_source *src, xoff_t blkno);
xoff_t xd3_source_eof(const xd3_source *src);
uint32_t xd3_large_cksum_update (uint32_t cksum,
const uint8_t *base,
usize_t look);
int xd3_emit_byte (xd3_stream *stream,
xd3_output **outputp,
uint8_t code);
int xd3_emit_bytes (xd3_stream *stream,
xd3_output **outputp,
const uint8_t *base,
usize_t size);
xd3_output* xd3_alloc_output (xd3_stream *stream,
xd3_output *old_output);
int xd3_encode_init_full (xd3_stream *stream);
size_t xd3_pow2_roundup (size_t x);
int xd3_process_stream (int is_encode,
xd3_stream *stream,
int (*func) (xd3_stream *),
int close_stream,
const uint8_t *input,
usize_t input_size,
uint8_t *output,
usize_t *output_size,
usize_t output_size_max);
#if PYTHON_MODULE || SWIG_MODULE || NOT_MAIN
int xd3_main_cmdline (int argc, char **argv);
#endif
/* main_file->mode values */
typedef enum
{
XO_READ = 0,
XO_WRITE = 1
} main_file_modes;
#ifndef XD3_POSIX
#define XD3_POSIX 0
#endif
#ifndef XD3_STDIO
#define XD3_STDIO 0
#endif
#ifndef XD3_WIN32
#define XD3_WIN32 0
#endif
#ifndef NOT_MAIN
#define NOT_MAIN 0
#endif
/* If none are set, default to posix. */
#if (XD3_POSIX + XD3_STDIO + XD3_WIN32) == 0
#undef XD3_POSIX
#define XD3_POSIX 1
#endif
struct _main_file
{
#if XD3_WIN32
HANDLE file;
#elif XD3_STDIO
FILE *file;
#elif XD3_POSIX
int file;
#endif
int mode; /* XO_READ and XO_WRITE */
const char *filename; /* File name or /dev/stdin,
* /dev/stdout, /dev/stderr. */
char *filename_copy; /* File name or /dev/stdin,
* /dev/stdout, /dev/stderr. */
const char *realname; /* File name or /dev/stdin,
* /dev/stdout, /dev/stderr. */
const main_extcomp *compressor; /* External compression struct. */
int flags; /* RD_FIRST, RD_NONEXTERNAL, ... */
xoff_t nread; /* for input position */
xoff_t nwrite; /* for output position */
uint8_t *snprintf_buf; /* internal snprintf() use */
int size_known; /* Set by main_set_souze */
xoff_t source_position; /* for avoiding seek in getblk_func */
int seek_failed; /* after seek fails once, try FIFO */
};
#ifdef _WIN32
#define vsnprintf_func _vsnprintf
#define snprintf_func _snprintf
#else
#define vsnprintf_func vsnprintf
#define snprintf_func snprintf
#endif
#define short_sprintf(sb,fmt,...) \
snprintf_func((sb).buf,sizeof((sb).buf),fmt,__VA_ARGS__)
/* Type used for short snprintf calls. */
typedef struct {
char buf[48];
} shortbuf;
/* Prior to SVN 303 this function was only defined in DJGPP and WIN32
* environments and other platforms would use the builtin snprintf()
* with an arrangement of macros below. In OS X 10.6, Apply made
* snprintf() a macro, which defeated those macros (since snprintf
* would be evaluated before its argument macros were expanded,
* therefore always define xsnprintf_func. */
#undef PRINTF_ATTRIBUTE
#ifdef __GNUC__
/* Let's just assume no one uses gcc 2.x! */
#define PRINTF_ATTRIBUTE(x,y) __attribute__ ((__format__ (__printf__, x, y)))
#else
#define PRINTF_ATTRIBUTE(x,y)
#endif
/* Underlying xprintf() */
int xsnprintf_func (char *str, int n, const char *fmt, ...)
PRINTF_ATTRIBUTE(3,4);
/* XPR(NT "", ...) (used by main) prefixes an "xdelta3: " to the output. */
void xprintf(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
#define XPR xprintf
#define NT "xdelta3: "
#define NTR ""
#ifndef UINT32_MAX
#define UINT32_MAX 4294967295U
#endif
#ifndef UINT64_MAX
#define UINT64_MAX 18446744073709551615ULL
#endif
#define UINT32_OFLOW_MASK 0xfe000000U
#define UINT64_OFLOW_MASK 0xfe00000000000000ULL
/*********************************************************************
Integer encoder/decoder functions
**********************************************************************/
/* Consume N bytes of input, only used by the decoder. */
#define DECODE_INPUT(n) \
do { \
stream->total_in += (xoff_t) (n); \
stream->avail_in -= (n); \
stream->next_in += (n); \
} while (0)
#define DECODE_INTEGER_TYPE(PART,OFLOW) \
while (stream->avail_in != 0) \
{ \
usize_t next = stream->next_in[0]; \
\
DECODE_INPUT(1); \
\
if (PART & OFLOW) \
{ \
stream->msg = "overflow in decode_integer"; \
return XD3_INVALID_INPUT; \
} \
\
PART = (PART << 7) | (next & 127); \
\
if ((next & 128) == 0) \
{ \
(*val) = PART; \
PART = 0; \
return 0; \
} \
} \
\
stream->msg = "further input required"; \
return XD3_INPUT
#define READ_INTEGER_TYPE(TYPE, OFLOW) \
TYPE val = 0; \
const uint8_t *inp = (*inpp); \
usize_t next; \
\
do \
{ \
if (inp == maxp) \
{ \
stream->msg = "end-of-input in read_integer"; \
return XD3_INVALID_INPUT; \
} \
\
if (val & OFLOW) \
{ \
stream->msg = "overflow in read_intger"; \
return XD3_INVALID_INPUT; \
} \
\
next = (*inp++); \
val = (val << 7) | (next & 127); \
} \
while (next & 128); \
\
(*valp) = val; \
(*inpp) = inp; \
\
return 0
#define EMIT_INTEGER_TYPE() \
/* max 64-bit value in base-7 encoding is 9.1 bytes */ \
uint8_t buf[10]; \
usize_t bufi = 10; \
\
/* This loop performs division and turns on all MSBs. */ \
do \
{ \
buf[--bufi] = (num & 127) | 128; \
num >>= 7U; \
} \
while (num != 0); \
\
/* Turn off MSB of the last byte. */ \
buf[9] &= 127; \
\
return xd3_emit_bytes (stream, output, buf + bufi, 10 - bufi)
#define IF_SIZEOF32(x) if (num < (1U << (7 * (x)))) return (x);
#define IF_SIZEOF64(x) if (num < (1ULL << (7 * (x)))) return (x);
#if USE_UINT32
static inline uint32_t
xd3_sizeof_uint32_t (uint32_t num)
{
IF_SIZEOF32(1);
IF_SIZEOF32(2);
IF_SIZEOF32(3);
IF_SIZEOF32(4);
return 5;
}
static inline int
xd3_decode_uint32_t (xd3_stream *stream, uint32_t *val)
{ DECODE_INTEGER_TYPE (stream->dec_32part, UINT32_OFLOW_MASK); }
static inline int
xd3_read_uint32_t (xd3_stream *stream, const uint8_t **inpp,
const uint8_t *maxp, uint32_t *valp)
{ READ_INTEGER_TYPE (uint32_t, UINT32_OFLOW_MASK); }
#if XD3_ENCODER
static inline int
xd3_emit_uint32_t (xd3_stream *stream, xd3_output **output, uint32_t num)
{ EMIT_INTEGER_TYPE (); }
#endif
#endif
#if USE_UINT64
static inline int
xd3_decode_uint64_t (xd3_stream *stream, uint64_t *val)
{ DECODE_INTEGER_TYPE (stream->dec_64part, UINT64_OFLOW_MASK); }
#if XD3_ENCODER
static inline int
xd3_emit_uint64_t (xd3_stream *stream, xd3_output **output, uint64_t num)
{ EMIT_INTEGER_TYPE (); }
#endif
/* These are tested but not used */
#if REGRESSION_TEST
static int
xd3_read_uint64_t (xd3_stream *stream, const uint8_t **inpp,
const uint8_t *maxp, uint64_t *valp)
{ READ_INTEGER_TYPE (uint64_t, UINT64_OFLOW_MASK); }
static uint32_t
xd3_sizeof_uint64_t (uint64_t num)
{
IF_SIZEOF64(1);
IF_SIZEOF64(2);
IF_SIZEOF64(3);
IF_SIZEOF64(4);
IF_SIZEOF64(5);
IF_SIZEOF64(6);
IF_SIZEOF64(7);
IF_SIZEOF64(8);
IF_SIZEOF64(9);
return 10;
}
#endif
#endif
#if SIZEOF_USIZE_T == 4
#define USIZE_T_MAX UINT32_MAX
#define USIZE_T_MAXBLKSZ 0x80000000U
#define xd3_decode_size xd3_decode_uint32_t
#define xd3_emit_size xd3_emit_uint32_t
#define xd3_sizeof_size xd3_sizeof_uint32_t
#define xd3_read_size xd3_read_uint32_t
#elif SIZEOF_USIZE_T == 8
#define USIZE_T_MAX UINT64_MAX
#define USIZE_T_MAXBLKSZ 0x8000000000000000ULL
#define xd3_decode_size xd3_decode_uint64_t
#define xd3_emit_size xd3_emit_uint64_t
#define xd3_sizeof_size xd3_sizeof_uint64_t
#define xd3_read_size xd3_read_uint64_t
#endif
#if SIZEOF_XOFF_T == 4
#define XOFF_T_MAX UINT32_MAX
#define xd3_emit_offset xd3_emit_uint32_t
static inline int
xd3_decode_offset (xd3_stream *stream, xoff_t *val)
{
return xd3_decode_uint32_t (stream, (uint32_t*) val);
}
#elif SIZEOF_XOFF_T == 8
#define XOFF_T_MAX UINT64_MAX
#define xd3_emit_offset xd3_emit_uint64_t
static inline int
xd3_decode_offset (xd3_stream *stream, xoff_t *val)
{
return xd3_decode_uint64_t (stream, (uint64_t*) val);
}
#endif
#define USIZE_T_OVERFLOW(a,b) ((USIZE_T_MAX - (usize_t) (a)) < (usize_t) (b))
#define XOFF_T_OVERFLOW(a,b) ((XOFF_T_MAX - (xoff_t) (a)) < (xoff_t) (b))
#define MAX_LRU_SIZE 32U
#define XD3_MINSRCWINSZ (XD3_ALLOCSIZE * MAX_LRU_SIZE)
#define XD3_MAXSRCWINSZ (1ULL << 31)
#endif // XDELTA3_INTERNAL_H__
+127
View File
@@ -0,0 +1,127 @@
/* xdelta3 - delta compression tools and library
Copyright 2016 Joshua MacDonald
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef __XDELTA3_LIST__
#define __XDELTA3_LIST__
#define XD3_MAKELIST(LTYPE,ETYPE,LNAME) \
\
static inline ETYPE* \
LTYPE ## _entry (LTYPE* l) \
{ \
return (ETYPE*) ((char*) l - (ptrdiff_t) &((ETYPE*) 0)->LNAME); \
} \
\
static inline void \
LTYPE ## _init (LTYPE *l) \
{ \
l->next = l; \
l->prev = l; \
} \
\
static inline void \
LTYPE ## _add (LTYPE *prev, LTYPE *next, LTYPE *ins) \
{ \
next->prev = ins; \
prev->next = ins; \
ins->next = next; \
ins->prev = prev; \
} \
\
static inline void \
LTYPE ## _push_back (LTYPE *l, ETYPE *i) \
{ \
LTYPE ## _add (l->prev, l, & i->LNAME); \
} \
\
static inline void \
LTYPE ## _del (LTYPE *next, \
LTYPE *prev) \
{ \
next->prev = prev; \
prev->next = next; \
} \
\
static inline ETYPE* \
LTYPE ## _remove (ETYPE *f) \
{ \
LTYPE *i = f->LNAME.next; \
LTYPE ## _del (f->LNAME.next, f->LNAME.prev); \
return LTYPE ## _entry (i); \
} \
\
static inline ETYPE* \
LTYPE ## _pop_back (LTYPE *l) \
{ \
LTYPE *i = l->prev; \
LTYPE ## _del (i->next, i->prev); \
return LTYPE ## _entry (i); \
} \
\
static inline ETYPE* \
LTYPE ## _pop_front (LTYPE *l) \
{ \
LTYPE *i = l->next; \
LTYPE ## _del (i->next, i->prev); \
return LTYPE ## _entry (i); \
} \
\
static inline int \
LTYPE ## _empty (LTYPE *l) \
{ \
return l == l->next; \
} \
\
static inline ETYPE* \
LTYPE ## _front (LTYPE *f) \
{ \
return LTYPE ## _entry (f->next); \
} \
\
static inline ETYPE* \
LTYPE ## _back (LTYPE *f) \
{ \
return LTYPE ## _entry (f->prev); \
} \
\
static inline int \
LTYPE ## _end (LTYPE *f, ETYPE *i) \
{ \
return f == & i->LNAME; \
} \
\
static inline ETYPE* \
LTYPE ## _next (ETYPE *f) \
{ \
return LTYPE ## _entry (f->LNAME.next); \
} \
\
static inline usize_t \
LTYPE ## _length (LTYPE *l) \
{ \
LTYPE *p; \
int c = 0; \
\
for (p = l->next; p != l; p = p->next) \
{ \
c += 1; \
} \
\
return c; \
} \
\
typedef int unused_ ## LTYPE
#endif
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,402 @@
package com.droiddeck.launcher.stores.gog
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreGameSidecar
import com.droiddeck.launcher.stores.StoreLanguage
import com.droiddeck.launcher.stores.CatalogItem
import com.droiddeck.launcher.stores.StoreInstallRoot
import com.droiddeck.launcher.stores.download.DownloadQueue
import com.droiddeck.launcher.stores.download.DownloadState
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import java.io.File
import java.nio.file.Files
import java.util.concurrent.atomic.AtomicBoolean
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class GogMaintenanceTest {
@get:Rule val temporary = TemporaryFolder()
@Test fun selectsPublicBranchAndTreatsBuildIdsAsOpaque() {
val builds = JSONObject("""{"items":[
{"os":"windows","branch":"beta","build_id":"99","generation":2,"link":"https://example.invalid/beta"},
{"os":"windows","branch":null,"build_id":"0002","generation":2,"version_name":"RC-final","link":"https://example.invalid/current"},
{"os":"windows","branch":null,"build_id":"10","generation":2,"link":"https://example.invalid/old"}]}""")
assertEquals("0002", GogBuild.select(builds)!!.id)
assertEquals("99", GogBuild.select(builds, branch = "beta")!!.id)
assertEquals("10", GogBuild.select(builds, pinned = "10")!!.id)
assertNull(GogBuild.select(builds, pinned = "missing"))
assertNull(GogBuild.select(builds, branch = "private"))
}
@Test fun migrationRetainsKnownBuildLanguageAndInstalledProducts() {
val folder = temporary.newFolder()
File(folder, "_gog_manifest.json").writeText("""{"gameId":"1","buildId":"000123"}""")
File(folder, "goggame-2.info").writeText("{}")
val sidecar = StoreGameSidecar(Store.GOG, "1", "Game", "game.exe", installVersion = "000123",
args = listOf("--custom"), env = mapOf("DD_TEST" to "1"), cloud = false, achievements = false,
extra = mapOf(StoreLanguage.EXTRA to "pl-PL"))
val record = GogInstallRecord.migrate(folder, sidecar)
assertEquals("000123", record.build)
assertEquals("pl-PL", record.language)
assertEquals(listOf("1", "2"), record.products)
assertEquals(record.json().toString(), GogInstallRecord.read(folder)!!.json().toString())
assertEquals(listOf("--custom"), sidecar.args)
assertFalse(sidecar.cloud)
}
@Test fun missingManifestNeverInventsLatestBuild() {
val folder = temporary.newFolder()
val record = GogInstallRecord.migrate(folder, StoreGameSidecar(Store.GOG, "1", "Game", "game.exe"))
assertEquals("offline", record.source)
assertEquals("", record.build)
assertEquals(0, record.files.length())
}
@Test fun offlineRefreshWithDlcRequiresAnAuthenticMatchingDepotBuild() {
val extracted = temporary.newFolder()
val app = File(extracted, "app").apply { mkdirs() }
val target = GogInstallRecord("1", "depot", build = "0000123", products = listOf("1", "2"))
assertThrows(IllegalArgumentException::class.java) { GogOffline.validateDepotInstaller(extracted, target) }
File(app, "goggame-1.info").writeText("""{"buildId":"0000999"}""")
assertThrows(IllegalArgumentException::class.java) { GogOffline.validateDepotInstaller(extracted, target) }
File(app, "goggame-1.info").writeText("""{"buildId":"0000123"}""")
GogOffline.validateDepotInstaller(extracted, target)
File(app, "goggame-1.info").delete()
GogOffline.validateDepotInstaller(extracted, target.copy(source = "offline", build = ""))
}
@Test fun offlineRefreshRetainsTheExistingExecutableLayout() {
val installed = temporary.newFolder()
val extracted = temporary.newFolder()
val app = File(extracted, "app").apply { mkdirs() }
File(app, "game.exe").writeText("replacement")
File(installed, "goggame-1.info").writeText("{}")
assertEquals(extracted, GogOffline.layout(extracted, installed, "1", "game.exe").source)
assertEquals("", GogOffline.layout(extracted, installed, "1", "game.exe").prefix)
File(installed, "goggame-1.info").delete()
assertEquals(extracted, GogOffline.layout(extracted, installed, "1", "app/game.exe").source)
val flat = temporary.newFolder()
val legacy = GogOffline.layout(flat, installed, "1", "app/game.exe")
assertEquals(flat, legacy.source)
assertEquals("app/", legacy.prefix)
}
@Test fun offlineInstallerMatchesRegionalLanguageWithoutChoosingAnotherLanguage() {
val downloads = JSONObject("""{"installers":[
{"id":"de","os":"windows","language":"de","files":[{"downlink":"https://example.invalid/de"}]},
{"id":"en","os":"windows","language":"en","files":[{"downlink":"https://example.invalid/en"}]}]}""")
assertEquals("en", GogMaintenance.offlineInstaller(downloads, "en-US").id)
assertThrows(IllegalStateException::class.java) { GogMaintenance.offlineInstaller(downloads, "pl-PL") }
downloads.getJSONArray("installers").put(0, JSONObject("""{"id":"neutral","os":"windows","language":"Neutral","files":[{"downlink":"https://example.invalid/neutral"}]}"""))
assertEquals("en", GogMaintenance.offlineInstaller(downloads, "en-US").id)
assertEquals("neutral", GogMaintenance.offlineInstaller(downloads, "pl-PL").id)
}
@Test fun incompleteOfflineExtractionCannotReplaceOrRemoveInstalledFiles() {
val folder = temporary.newFolder()
StoreGameSidecar(Store.GOG, "1", "Game", "game.exe").write(folder)
val game = File(folder, "game.exe").apply { writeText("installed game") }
val old = GogInstallRecord("1", "depot", files = JSONArray().put(JSONObject().put("path", game.name)
.put("size", game.length()).put("md5", GogFiles.hash(game))))
old.write(folder)
val extracted = temporary.newFolder()
File(extracted, "goggame-1.ico").writeText("ancillary file")
assertThrows(IllegalStateException::class.java) {
GogOffline.applyRefresh(GogMaintenance.Proposal(folder, old, old.copy(source = "offline"), GogOperation.REFRESH, 0, 0), extracted, null, AtomicBoolean(false))
}
assertEquals("installed game", game.readText())
assertFalse(File(folder, "goggame-1.ico").exists())
assertEquals("depot", GogInstallRecord.read(folder)!!.source)
val migrated = old.copy(files = JSONArray())
migrated.write(folder)
assertThrows(IllegalStateException::class.java) {
GogOffline.applyRefresh(GogMaintenance.Proposal(folder, migrated, migrated.copy(source = "offline"), GogOperation.REFRESH, 0, 0), extracted, null, AtomicBoolean(false))
}
assertEquals("installed game", game.readText())
assertFalse(File(folder, "goggame-1.ico").exists())
assertEquals("depot", GogInstallRecord.read(folder)!!.source)
assertEquals(0, GogInstallRecord.read(folder)!!.files.length())
}
@Test fun offlineRefreshCombinesRootPayloadAndAppPlaceholdersInBothLayouts() {
for (nested in listOf(false, true)) {
val folder = temporary.newFolder()
val executable = if (nested) "app/game.exe" else "game.exe"
StoreGameSidecar(Store.GOG, "1", "Game", executable).write(folder)
val game = File(folder, executable).apply { parentFile!!.mkdirs(); writeText("old game") }
val old = GogInstallRecord("1", "offline", files = JSONArray().put(JSONObject().put("path", executable)
.put("size", game.length()).put("md5", GogFiles.hash(game))))
old.write(folder)
val extracted = temporary.newFolder()
File(extracted, "game.exe").writeText("complete new game")
File(extracted, "Data").mkdirs()
File(extracted, "app/Data/SaveGame").mkdirs()
File(extracted, "app/Data/SaveGame/dummy.txt").writeBytes(byteArrayOf())
File(extracted, "tmp").mkdirs()
File(extracted, "tmp/installer.exe").writeText("installer support")
GogOffline.applyRefresh(GogMaintenance.Proposal(folder, old, old, GogOperation.REFRESH, 0, 0), extracted, null, AtomicBoolean(false))
assertEquals("complete new game", game.readText())
assertTrue(File(folder, (if (nested) "app/" else "") + "Data/SaveGame/dummy.txt").isFile)
assertFalse(File(folder, "tmp").exists())
assertTrue(GogInstallRecord.read(folder)!!.files.objects().any { it.getString("path") == executable })
}
}
@Test fun offlineRefreshDefersDlcCleanupAndRecordPublicationUntilDepotCompletion() {
val folder = temporary.newFolder()
StoreGameSidecar(Store.GOG, "1", "Game", "base.exe").write(folder)
val base = File(folder, "base.exe").apply { writeText("old base") }
val dlc = File(folder, "dlc.bin").apply { writeText("old DLC") }
val obsolete = File(folder, "old.bin").apply { writeText("obsolete") }
val save = File(folder, "save.dat").apply { writeText("player progress") }
fun entry(file: File, product: String) = JSONObject().put("path", file.name).put("size", file.length())
.put("md5", GogFiles.hash(file)).put("product", product).put("offset", 0).put("remotePath", file.name)
val old = GogInstallRecord("1", "depot", build = "old", products = listOf("1", "2"),
files = JSONArray().put(entry(base, "1")).put(entry(dlc, "2")).put(entry(obsolete, "1")))
old.write(folder)
val recordFile = File(GogFiles.dir(folder), "installed.json")
val record = recordFile.readText()
val extracted = temporary.newFolder()
val app = File(extracted, "app").apply { mkdirs() }
File(app, "goggame-1.info").writeText("""{"buildId":"new"}""")
val replacement = File(app, "base.exe").apply { writeText("new base") }
val expectedDlc = File(temporary.newFolder(), "dlc.bin").apply { writeText("new DLC") }
val target = old.copy(build = "new", files = JSONArray().put(entry(replacement, "1")).put(entry(expectedDlc, "2")))
GogOffline.applyRefresh(GogMaintenance.Proposal(folder, old, target, GogOperation.REFRESH, 0, 0), extracted, null, AtomicBoolean(false))
assertEquals("new base", base.readText())
assertEquals("old DLC", dlc.readText())
assertTrue(obsolete.isFile)
assertEquals(record, recordFile.readText())
val server = java.net.ServerSocket(0, 1, java.net.InetAddress.getByName("127.0.0.1"))
val worker = Thread {
server.accept().use { client ->
val reader = client.getInputStream().bufferedReader()
while (!reader.readLine().isNullOrEmpty()) Unit
val content = expectedDlc.readBytes()
client.getOutputStream().apply {
write("HTTP/1.1 206 Partial Content\r\nContent-Length: ${content.size}\r\nConnection: close\r\n\r\n".toByteArray())
write(content); flush()
}
}
}.apply { isDaemon = true; start() }
try {
GogFallback.transfer(folder, JSONObject().put("files", target.files).put("oldFiles", old.files)
.put("links", JSONObject().put("2", JSONArray().put("http://127.0.0.1:${server.localPort}"))), null, AtomicBoolean(false))
assertEquals("new DLC", dlc.readText())
assertFalse(obsolete.exists())
assertEquals("player progress", save.readText())
assertEquals(record, recordFile.readText())
} finally { server.close(); worker.join(2000) }
}
@Test fun authenticIdentityOverridesAStaleLegacyMarkerWithoutTreatingSchemaAsVersion() {
val folder = temporary.newFolder()
File(folder, "_gog_manifest.json").writeText("""{"buildId":"old"}""")
File(folder, "goggame-1.id").writeText("""{"buildId":"patched"}""")
File(folder, "goggame-1.info").writeText("""{"version":1}""")
val record = GogInstallRecord.migrate(folder, StoreGameSidecar(Store.GOG, "1", "Game", "game.exe", installVersion = "old"))
assertEquals("patched", record.build)
assertEquals("", record.version)
}
@Test fun authenticGenerationOneResolvesProductDepotsAndRetainsHashesAndBuild() {
fun fixture(name: String) = JSONObject(javaClass.classLoader!!.getResource("gog/$name")!!.readText())
val build = GogBuild("3301", "", 1, "", "https://example.invalid/repository.json")
val requested = ArrayList<String>()
val snapshot = GogContent.snapshot(GogInstallRecord("1207658940", "depot"), build, "unused") { url, _ ->
requested.add(url)
when {
url == build.link -> fixture("rayman-gen1-repository.json")
url.endsWith("a01f6fb8-55a8-4f33-bc5b-5f5d2f591775.json") -> fixture("rayman-gen1-neutral.json")
url.endsWith("ec88140c-2589-41a0-bbf3-8fc1f215fe5a.json") -> fixture("rayman-gen1-english-excerpt.json")
else -> error("Unexpected metadata: $url")
}
}
assertEquals("3301", snapshot.build)
assertEquals(1, snapshot.generation)
assertEquals(4, snapshot.files.length())
val support = snapshot.files.getJSONObject(0)
assertEquals("support/1207658940/galaxy_rayman2_2.1.0.39.exe", support.getString("path"))
assertEquals("ee2c247373733f29782395f1f92622f6", support.getString("md5"))
val game = snapshot.files.getJSONObject(1)
assertEquals("Data/World/Levels/Raycap/Raycap.snd", game.getString("path"))
assertEquals(384206096L, game.getLong("offset"))
assertEquals("1207658940/main.bin", game.getString("remotePath"))
assertTrue(requested.drop(1).all { "/1207658940/windows/31847977/" in it })
GogFiles.validate(snapshot.files, temporary.newFolder())
}
@Test fun multipartPatchNamesVersionsLanguageAndChecksumsSurviveParsing() {
val downloads = JSONObject("""{"patches":[
{"id":"patch-1","name":"Patch A to B","version":"B","language":"pl","os":"windows","files":[
{"id":"part0","filename":"patch_original.exe","downlink":"https://example.invalid/0","size":3,"md5":"abc"},
{"id":"part1","filename":"patch_original-1.bin","downlink":"https://example.invalid/1","size":4}]},
{"id":"linux","os":"linux","files":[{"downlink":"https://example.invalid/linux"}]}]}""")
val patch = GogOfflineDownload.parse(downloads, "patches").single()
assertEquals("Patch A to B", patch.name)
assertEquals("B", patch.version)
assertEquals("pl", patch.language)
assertEquals(7, patch.size)
assertEquals(listOf("patch_original.exe", "patch_original-1.bin"), patch.parts.map { it.name })
assertEquals("abc", patch.parts.first().md5)
assertEquals("patch_(52404)+special.exe", GogContent.downloadFilename("https://example.invalid/patch_%2852404%29+special.exe?token=temporary"))
}
@Test fun pathsRejectTraversalSymlinksCaseCollisionsAndMetadataOverlap() {
val folder = temporary.newFolder()
for (path in listOf("../escape", "/absolute", "C:/file", "a/./file", "a//file")) {
assertThrows(IllegalArgumentException::class.java) { GogFiles.inside(folder, path) }
}
val files = JSONArray("""[{"path":"Game.exe"},{"path":"game.exe"}]""")
assertThrows(IllegalArgumentException::class.java) { GogFiles.validate(files, folder) }
assertThrows(IllegalArgumentException::class.java) { GogFiles.validate(JSONArray("""[{"path":"Data/a"},{"path":"data/b"}]"""), folder) }
File(folder, "Game.bin").writeText("original")
assertThrows(IllegalArgumentException::class.java) { GogFiles.validate(JSONArray("""[{"path":"game.bin"}]"""), folder) }
assertThrows(IllegalArgumentException::class.java) { GogFiles.validate(JSONArray("""[{"path":".droiddeck-store.json"}]"""), folder) }
val outside = temporary.newFolder()
try { Files.createSymbolicLink(File(folder, "link").toPath(), outside.toPath()) }
catch (_: java.nio.file.FileSystemException) { return }
assertThrows(IllegalArgumentException::class.java) { GogFiles.inside(folder, "link/game.exe") }
}
@Test fun sameSizeCorruptionFailsVerificationAndLanguageVariantsRemainDistinct() {
val folder = temporary.newFolder()
val file = File(folder, "game.bin").apply { writeText("original") }
val expected = JSONObject().put("path", file.name).put("size", file.length()).put("md5", GogFiles.hash(file))
file.writeText("modified")
assertFalse(GogFiles.matches(folder, expected))
assertTrue(GogContent.languageSelected(listOf("*"), "zh-Hant"))
assertFalse(GogContent.languageSelected(listOf("zh-Hans"), "zh-Hant"))
assertTrue(GogContent.languageSelected(listOf("Polish"), "pl"))
}
@Test fun persistedManifestsContainNoCredentialsOrTemporaryLinks() {
val input = JSONObject("""{"buildId":"1","clientSecret":"secret","urls":[{"url":"https://example.invalid/?token=secret"}],
"products":[{"productId":"2","clientSecret":"nested"}],"dependencies":["ISI"]}""")
val text = GogContent.sanitize(input).toString()
assertFalse(text.contains("secret"))
assertFalse(text.contains("https://"))
assertTrue(text.contains("ISI"))
}
@Test fun interruptedExternalPatchRestoresRecoveryWithoutExecutingAgain() {
val app = RuntimeEnvironment.getApplication()
val folder = File(StoreInstallRoot.storeDir(StoreInstallRoot.internalRoot(app), Store.GOG), "Patch recovery").apply { mkdirs() }
try {
val sidecar = StoreGameSidecar(Store.GOG, "recovery-test", "Patch recovery", "game.exe",
args = listOf("--custom"), env = mapOf("SETTING" to "value"), cloud = false,
achievements = false, state = StoreGameSidecar.STATE_UPDATING)
sidecar.write(folder)
val previous = sidecar.toJson().toString()
GogFiles.write(GogFiles.journal(folder), JSONObject().put("operation", "PATCH").put("phase", "patch-applying"))
val item = CatalogItem(Store.GOG, sidecar.id, sidecar.title, imageUrl = null, extra = mapOf(
GogMaintenance.OPERATION to "PATCH", GogMaintenance.FOLDER to folder.path))
GogMaintenance.restore(app, item)
assertEquals(DownloadState.FAILED, DownloadQueue.entry(item.key)?.state)
assertTrue(DownloadQueue.entry(item.key)!!.error!!.contains("recovery"))
assertEquals(previous, StoreGameSidecar.read(folder)!!.toJson().toString())
assertEquals("patch-applying", GogFiles.read(GogFiles.journal(folder))!!.getString("phase"))
DownloadQueue.clear(app, item.key)
} finally { folder.deleteRecursively() }
}
@Test fun signedOutCheckRemainsAFailureInsteadOfCurrent() {
val app = RuntimeEnvironment.getApplication()
com.droiddeck.launcher.stores.StoreAccounts.clear(app, Store.GOG)
GogMaintenance.reset()
val folder = temporary.newFolder()
val game = com.droiddeck.launcher.stores.InstalledStoreGame(
StoreGameSidecar(Store.GOG, "signed-out-check", "Check", "game.exe", installVersion = "opaque-build"), folder)
GogMaintenance.check(app, game, force = true)
val deadline = System.nanoTime() + java.util.concurrent.TimeUnit.SECONDS.toNanos(5)
while (System.nanoTime() < deadline) {
org.robolectric.shadows.ShadowLooper.idleMainLooper()
if (GogMaintenance.checks[game.sidecar.id]?.state == GogMaintenance.State.FAILED) break
Thread.sleep(10)
}
assertEquals(GogMaintenance.State.FAILED, GogMaintenance.checks[game.sidecar.id]?.state)
assertTrue(GogMaintenance.checks[game.sidecar.id]!!.reason.contains("Sign in"))
}
@Test fun pauseBeforeInstallerClaimCannotStartExternalApplication() {
val app = RuntimeEnvironment.getApplication()
val ready = java.util.concurrent.CountDownLatch(1)
val proceed = java.util.concurrent.CountDownLatch(1)
val finished = java.util.concurrent.CountDownLatch(1)
val applied = AtomicBoolean(false)
val item = com.droiddeck.launcher.stores.download.DownloadEntry(Store.GOG, "installer-claim", "Installer claim")
DownloadQueue.enqueue(app, item) {
object : DownloadQueue.DownloadJob {
override fun run(handle: DownloadQueue.JobHandle): String? {
ready.countDown()
try {
check(proceed.await(5, java.util.concurrent.TimeUnit.SECONDS))
handle.interruptible(false)
applied.set(true)
return null
} finally { finished.countDown() }
}
override fun cancel(deleteFiles: Boolean) {}
}
}
try {
assertTrue(ready.await(5, java.util.concurrent.TimeUnit.SECONDS))
DownloadQueue.pause(item.key)
proceed.countDown()
assertTrue(finished.await(5, java.util.concurrent.TimeUnit.SECONDS))
assertFalse(applied.get())
} finally {
proceed.countDown()
val deadline = System.nanoTime() + java.util.concurrent.TimeUnit.SECONDS.toNanos(5)
while (DownloadQueue.entry(item.key)?.state == DownloadState.RUNNING && System.nanoTime() < deadline) Thread.sleep(10)
DownloadQueue.clear(app, item.key)
}
}
@Test fun fileFallbackVerifiesRemoteBytesAndPreservesOriginalOnCorruption() {
val folder = temporary.newFolder()
val content = "verified remote content".toByteArray()
val original = File(folder, "game.bin").apply { writeText("local data") }
val server = java.net.ServerSocket(0, 10, java.net.InetAddress.getByName("127.0.0.1"))
var reply = content
var transferred = 0
val worker = Thread {
while (!server.isClosed) {
val client = try { server.accept() } catch (_: java.io.IOException) { break }
client.use {
val reader = it.getInputStream().bufferedReader()
while (!reader.readLine().isNullOrEmpty()) Unit
it.getOutputStream().apply {
write("HTTP/1.1 206 Partial Content\r\nContent-Length: ${reply.size}\r\nConnection: close\r\n\r\n".toByteArray())
write(reply); flush(); transferred += reply.size
}
}
}
}.apply { isDaemon = true; start() }
try {
val expected = File(folder, "expected").apply { writeBytes(content) }
val file = JSONObject().put("path", "game.bin").put("size", content.size).put("md5", GogFiles.hash(expected))
.put("product", "1").put("offset", 0).put("remotePath", "data")
val plan = JSONObject().put("files", JSONArray().put(file)).put("links", JSONObject().put("1", JSONArray().put("http://127.0.0.1:${server.localPort}")))
reply = ByteArray(content.size) { 65 }
assertThrows(IllegalStateException::class.java) { GogFallback.transfer(folder, plan, null, AtomicBoolean(false)) }
assertEquals("local data", original.readText())
reply = content
GogFallback.transfer(folder, plan, null, AtomicBoolean(false))
assertArrayEquals(content, original.readBytes())
val before = transferred
GogFallback.transfer(folder, plan, null, AtomicBoolean(false))
assertEquals(before, transferred)
} finally { server.close(); worker.join(2000) }
}
}
+8
View File
@@ -0,0 +1,8 @@
Generation 1 fixtures from GOG's public Rayman 2 repository, timestamp 31847977,
retrieved 2026-10-28. The repository and Neutral depot are complete. The English
depot is an excerpt containing its first three original file entries.
https://gog-cdn-fastly.gog.com/content-system/v1/manifests/1207658940/windows/31847977/repository.json
The fixtures retain provider paths, support flags, range offsets, and original
hexadecimal hashes. They contain no game payload, credentials, or signed links.
@@ -0,0 +1,29 @@
{
"version": 1,
"depot": {
"name": "Rayman 2 - The Great Escape",
"files": [
{
"offset": 384206096,
"hash": "a08070ecde7f746779259f7ce5775747",
"url": "1207658940/main.bin",
"path": "/Data/World/Levels/Raycap/Raycap.snd",
"size": 32
},
{
"offset": 114255832,
"hash": "67165fe729ffda351a04e8bd92e05531",
"url": "1207658940/main.bin",
"path": "/Data/World/Sound/Bnk_29.bnm",
"size": 76
},
{
"offset": 97474601,
"hash": "2d2e0b308cae5ff44a620fd32bf6cb4d",
"url": "1207658940/main.bin",
"path": "/Data/World/Sound/Bnk_43.bnm",
"size": 76
}
]
}
}
@@ -0,0 +1,16 @@
{
"version": 1,
"depot": {
"name": "Rayman 2 - The Great Escape",
"files": [
{
"offset": 0,
"path": "1207658940/galaxy_rayman2_2.1.0.39.exe",
"size": 1258944,
"support": true,
"hash": "ee2c247373733f29782395f1f92622f6",
"url": "1207658940/main.bin"
}
]
}
}
@@ -0,0 +1,65 @@
{
"product": {
"timestamp": 31847977,
"depots": [
{
"languages": [
"Neutral"
],
"size": "1258944",
"gameIDs": [
"1207658940"
],
"systems": [
"Windows"
],
"manifest": "a01f6fb8-55a8-4f33-bc5b-5f5d2f591775.json"
},
{
"languages": [
"English"
],
"size": "505011865",
"gameIDs": [
"1207658940"
],
"systems": [
"Windows"
],
"manifest": "ec88140c-2589-41a0-bbf3-8fc1f215fe5a.json"
},
{
"redist": "nGlide",
"size": "0",
"targetDir": "/"
}
],
"support_commands": [
{
"languages": [
"Neutral"
],
"executable": "/galaxy_rayman2_2.1.0.39.exe",
"gameID": "1207658940",
"argument": "",
"systems": [
"Windows"
]
}
],
"installDirectory": "Rayman 2",
"rootGameID": "1207658940",
"gameIDs": [
{
"gameID": "1207658940",
"name": {
"en": "Rayman 2 - The Great Escape"
},
"dependencies": [],
"standalone": true
}
],
"projectName": "Rayman 2 - The Great Escape"
},
"version": 1
}