From ae193e9b8d72046b41ce2b784422853e4a9e03cc Mon Sep 17 00:00:00 2001 From: Hector Osuna <55816095+FanGoH@users.noreply.github.com> Date: Sun, 25 Oct 2026 11:20:29 -0600 Subject: [PATCH] fix: find GOG Galaxy SDK cloud saves when remote-config lists no locations (#970) * fix: find GOG Galaxy SDK cloud saves when remote-config lists no locations cloudStorage.enabled with an empty locations list is Galaxy SDK IStorage under __default. DroidDeck treated that as no cloud. Use the SDK folder, strip/prefix the __default namespace, and inflate gzip bodies GOG stores on download. An empty cached template is fetched again so devices that already recorded "no cloud" recover. * fix: keep an empty GOG namespace, expire cached none, close gzip An explicit empty cloud_namespace is Auto Cloud and must not become __default just because the path template looks like Galaxy storage. A cached none is retried after a day so a later enablement is picked up. GZIPInputStream is closed after a compressed download. * fix: peel GOG cloud gzip only when the service or PUT compressor says so GET asks for identity encoding so the body is what GOG stored. A Content-Encoding of gzip is inflated once. Otherwise a gzip body is inflated only when it round-trips as GOG's PUT gzip (deflate 6, mtime 0), so a gzip-format save is not rewritten. * Reject corrupt GOG cloud downloads --------- Co-authored-by: xXJsonDeruloXx Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> --- .../launcher/stores/CloudSavePaths.kt | 20 +++++ .../droiddeck/launcher/stores/CloudSaves.kt | 71 +++++++++++------ .../launcher/stores/CloudTransport.kt | 77 ++++++++++++++++--- .../launcher/stores/CloudSavePathsTest.kt | 30 ++++++++ .../launcher/stores/CloudSavesTest.kt | 13 +++- .../launcher/stores/GogCloudKeysTest.kt | 61 +++++++++++++++ docs/development/stores.md | 7 +- 7 files changed, 245 insertions(+), 34 deletions(-) create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/GogCloudKeysTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/stores/CloudSavePaths.kt b/app/src/main/java/com/droiddeck/launcher/stores/CloudSavePaths.kt index 194448f0c8..9dfc6b6658 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/CloudSavePaths.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/CloudSavePaths.kt @@ -3,8 +3,28 @@ package com.droiddeck.launcher.stores import java.io.File object CloudSavePaths { + const val GOG_DEFAULT_NAMESPACE = "__default" + fun profile(prefix: File): File = File(prefix, "drive_c/users/steamuser") + fun gogGalaxySdkTemplate(clientId: String) = + "%LocalAppData%/GOG.com/Galaxy/Applications/$clientId/Storage/Shared/Files" + + class GogCloudLocation(val name: String, val template: String) + + /** + * cloudStorage.enabled with an empty locations list is Galaxy SDK IStorage, whose objects the + * API names under a location called `__default`. + */ + fun gogSaveFromRemote(enabled: Boolean, locations: List>, clientId: String): GogCloudLocation? { + if (!enabled) return null + val named = locations.filter { it.second.isNotEmpty() } + val pick = named.firstOrNull { it.first.equals("saves", true) } ?: named.firstOrNull() + if (pick != null) return GogCloudLocation(pick.first, pick.second) + if (clientId.isEmpty()) return null + return GogCloudLocation(GOG_DEFAULT_NAMESPACE, gogGalaxySdkTemplate(clientId)) + } + fun gog(template: String, prefix: File, install: File): File? { val norm = template.replace('\\', '/').trim().trimStart('/') val match = Regex("^(<\\?[^?>]+\\?>|%[^%]+%)").find(norm) ?: return null diff --git a/app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt b/app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt index 9aaa8e98a1..659ae7005c 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt @@ -1,6 +1,7 @@ package com.droiddeck.launcher.stores import android.content.Context +import android.content.SharedPreferences import android.util.Log import com.droiddeck.launcher.frontend.AddedGames import com.droiddeck.launcher.frontend.Library @@ -48,11 +49,38 @@ object CloudSaves { private fun markSynced(context: Context, store: Store, id: String) = prefs(context, store).edit().putLong("cloud_sync_$id", System.currentTimeMillis()).apply() + private const val TEMPLATE_NONE = "none" + internal const val TEMPLATE_NONE_TTL_MS = 24 * 60 * 60 * 1000L + + internal fun noneTemplateIsCurrent(storedAtMs: Long, nowMs: Long) = + storedAtMs > 0L && nowMs - storedAtMs < TEMPLATE_NONE_TTL_MS + + private fun rememberNoCloud(p: SharedPreferences, id: String) { + p.edit().putString("cloud_template_$id", TEMPLATE_NONE) + .putLong("cloud_template_at_$id", System.currentTimeMillis()).apply() + } + fun template(context: Context, store: Store, id: String, folder: File, sidecar: StoreGameSidecar): String? { val p = prefs(context, store) - if (p.contains("cloud_template_$id")) return p.getString("cloud_template_$id", "") + var retryNone = false + if (p.contains("cloud_template_$id")) { + val cached = p.getString("cloud_template_$id", "") ?: "" + if (cached == TEMPLATE_NONE) { + if (noneTemplateIsCurrent(p.getLong("cloud_template_at_$id", 0L), System.currentTimeMillis())) return "" + retryNone = true + } else if (cached.isNotEmpty()) return cached + } + var gogLocation = "" val fetched = when (store) { - Store.GOG -> gogTemplate(context, id, folder) + Store.GOG -> { + val config = gogConfig(context, id, folder) + if (config == null) { + if (retryNone) { rememberNoCloud(p, id); return "" } + return null + } + gogLocation = config.name + config.location + } Store.EPIC -> { val token = EpicCredentialStore.getValidAccessToken(context) ?: return null val ns = sidecar.extra["namespace"].orEmpty() @@ -61,7 +89,14 @@ object CloudSaves { } Store.AMAZON, Store.ITCH, Store.XBOX, Store.BATTLENET -> "" } ?: return null - p.edit().putString("cloud_template_$id", fetched).apply() + p.edit() + .putString("cloud_template_$id", fetched.ifEmpty { TEMPLATE_NONE }) + .apply { + if (fetched.isEmpty()) putLong("cloud_template_at_$id", System.currentTimeMillis()) + else remove("cloud_template_at_$id") + if (store == Store.GOG && gogLocation.isNotEmpty()) putString("cloud_location_$id", gogLocation) + } + .apply() Log.i(TAG, "cloud ${store.id} $id template ${if (fetched.isEmpty()) "none" else "found"}") return fetched } @@ -81,29 +116,21 @@ object CloudSaves { if (c.responseCode !in 200..299) { c.disconnect(); return null } val cloud = JSONObject(c.inputStream.bufferedReader().readText()).optJSONObject("content")?.optJSONObject("Windows")?.optJSONObject("cloudStorage") c.disconnect() - if (cloud?.optBoolean("enabled", false) != true) return GogConfig("", "") - val locations = cloud.optJSONArray("locations") ?: return GogConfig("", "") - val all = (0 until locations.length()).mapNotNull { locations.optJSONObject(it) } - val chosen = all.firstOrNull { it.optString("name").equals("saves", true) } ?: all.firstOrNull() - val location = chosen?.optString("location").orEmpty() - val name = chosen?.optString("name").orEmpty() - if (location.isEmpty()) GogConfig("", "") else GogConfig(name, location) + val locations = cloud?.optJSONArray("locations") + val picked = CloudSavePaths.gogSaveFromRemote( + enabled = cloud?.optBoolean("enabled", false) == true, + locations = (0 until (locations?.length() ?: 0)).mapNotNull { i -> + locations!!.optJSONObject(i)?.let { it.optString("name") to it.optString("location") } + }, + clientId = clientId, + ) + if (picked == null) GogConfig("", "") else GogConfig(picked.name, picked.template) } catch (e: Exception) { Log.w(TAG, "gog remote-config: ${e.javaClass.simpleName}") null } } - private fun gogTemplate(context: Context, id: String, folder: File): String? { - val p = GogPrefs.get(context) - if (p.contains("cloud_template_$id")) return p.getString("cloud_template_$id", "") - val config = gogConfig(context, id, folder) ?: return null - p.edit().putString("cloud_template_$id", config.location).apply { - if (config.name.isNotEmpty()) putString("cloud_location_$id", config.name) - }.apply() - return config.location - } - /** * The selected cloud location's name: GOG prefixes every cloud file with it, and the local folder * is the location itself. Older builds had no name cached; fetch it once for those. @@ -111,7 +138,7 @@ object CloudSaves { private fun gogLocationName(context: Context, id: String, folder: File): String? { val p = GogPrefs.get(context) p.getString("cloud_location_$id", null)?.ifEmpty { null }?.let { return it } - if (p.getString("cloud_template_$id", null)?.isEmpty() == true) return null + if (p.getString("cloud_template_$id", null)?.let { it.isEmpty() || it == TEMPLATE_NONE } == true) return null val config = gogConfig(context, id, folder) ?: return null if (config.name.isNotEmpty()) p.edit().putString("cloud_location_$id", config.name).apply() return config.name.ifEmpty { null } @@ -296,7 +323,7 @@ object CloudSaves { } } } catch (e: NoCloudSaves) { - prefs(context, store).edit().putString("cloud_template_$id", "").apply() + rememberNoCloud(prefs(context, store), id) Result("skipped", 0, 0, "no-cloud-saves") } catch (e: Exception) { Result("failed", 0, 0, e.javaClass.simpleName) diff --git a/app/src/main/java/com/droiddeck/launcher/stores/CloudTransport.kt b/app/src/main/java/com/droiddeck/launcher/stores/CloudTransport.kt index 2b2be1bc48..9c32bd4c1a 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/CloudTransport.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/CloudTransport.kt @@ -7,6 +7,8 @@ import java.net.URLEncoder import java.text.SimpleDateFormat import java.util.Locale import java.util.TimeZone +import java.util.zip.CRC32 +import java.util.zip.Deflater import org.json.JSONArray import org.json.JSONObject @@ -38,15 +40,60 @@ private fun httpDate(s: String?): Long = try { SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US).apply { timeZone = TimeZone.getTimeZone("GMT") }.parse(s!!.trim())?.time ?: -1L } catch (e: Exception) { -1L } +internal fun isGzip(data: ByteArray) = + data.size >= 10 && data[0] == 0x1f.toByte() && data[1] == 0x8b.toByte() && data[2] == 0x08.toByte() + +internal fun gunzip(data: ByteArray): ByteArray? = try { + java.util.zip.GZIPInputStream(data.inputStream()).use { it.readBytes() } +} catch (e: Exception) { null } + +/** Same gzip GOG uses on PUT: deflate level 6, mtime 0, OS Unix. */ +internal fun gzipGogPut(data: ByteArray): ByteArray { + val deflater = Deflater(6, true) + val raw = ByteArrayOutputStream() + try { + deflater.setInput(data) + deflater.finish() + val buf = ByteArray(512) + while (!deflater.finished()) { + val n = deflater.deflate(buf) + if (n > 0) raw.write(buf, 0, n) + } + } finally { + deflater.end() + } + val crc = CRC32().also { it.update(data) } + return ByteArrayOutputStream(18 + raw.size()).apply { + write(byteArrayOf(0x1f, 0x8b.toByte(), 8, 0, 0, 0, 0, 0, 0, 3)) + write(raw.toByteArray()) + writeIntLe(crc.value.toInt()) + writeIntLe(data.size) + }.toByteArray() +} + +private fun ByteArrayOutputStream.writeIntLe(v: Int) { + write(v and 0xff) + write(v ushr 8 and 0xff) + write(v ushr 16 and 0xff) + write(v ushr 24 and 0xff) +} + +internal fun gogDecodeCloudBody(data: ByteArray, contentEncoding: String?): ByteArray { + val encodings = contentEncoding.orEmpty().split(',').map { it.trim().lowercase() } + if ("gzip" in encodings || "x-gzip" in encodings) { + if (!isGzip(data)) return data + return gunzip(data) ?: throw java.io.IOException("Invalid GOG cloud gzip body") + } + if (!isGzip(data)) return data + val inflated = gunzip(data) ?: return data + return if (gzipGogPut(inflated).contentEquals(data)) inflated else data +} + internal fun isoDate(s: String?): Long { - if (s == null || s.length < 19) return -1L - return try { - java.util.Calendar.getInstance(TimeZone.getTimeZone("UTC")).apply { - clear() - set(s.substring(0, 4).toInt(), s.substring(5, 7).toInt() - 1, s.substring(8, 10).toInt(), - s.substring(11, 13).toInt(), s.substring(14, 16).toInt(), s.substring(17, 19).toInt()) - }.timeInMillis - } catch (e: Exception) { -1L } + if (s == null) return -1L + return runCatching { java.time.OffsetDateTime.parse(s).toInstant().toEpochMilli() } + .recoverCatching { java.time.LocalDateTime.parse(s).toInstant(java.time.ZoneOffset.UTC).toEpochMilli() } + .getOrDefault(-1L) } class NoCloudSaves : Exception("no cloud saves") @@ -106,7 +153,13 @@ class GogCloud( (0 until a.length()).mapNotNull { a.optJSONObject(it) }.mapNotNull { o -> val n = o.optString("name").ifEmpty { return@mapNotNull null } val lm = o.optLong("last_modified", 0L) - CloudFile(n, if (lm > 1_000_000_000_000L) lm else if (lm > 0) lm * 1000 else -1L) + val iso = isoDate(o.optString("last_modified").takeIf { it.isNotEmpty() }) + CloudFile(n, when { + lm > 1_000_000_000_000L -> lm + lm > 0 -> lm * 1000 + iso > 0 -> iso + else -> -1L + }) } } else { text.lines().map { it.trim() }.filter { it.isNotEmpty() }.map { CloudFile(it, -1L) } @@ -136,9 +189,13 @@ class GogCloud( override fun get(file: CloudFile): Pair { val c = open("$base/${cloudKey(file.name)}", "GET", token, ua) + c.setRequestProperty("Accept-Encoding", "identity") if (c.responseCode !in 200..299) { val code = c.responseCode; c.disconnect(); throw java.io.IOException("GOG cloud get HTTP $code") } val modified = httpDate(c.getHeaderField("Last-Modified")) - return c.body().also { c.disconnect() } to modified + val encoding = c.getHeaderField("Content-Encoding") + val data = c.body() + c.disconnect() + return gogDecodeCloudBody(data, encoding) to modified } override fun put(files: Map): Map = files.mapValues { (name, data) -> diff --git a/app/src/test/java/com/droiddeck/launcher/stores/CloudSavePathsTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/CloudSavePathsTest.kt index a88c8b9548..675c1e009e 100644 --- a/app/src/test/java/com/droiddeck/launcher/stores/CloudSavePathsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/stores/CloudSavePathsTest.kt @@ -47,4 +47,34 @@ class CloudSavePathsTest { File(profile, "AppData/Local/My-Game").mkdirs() assertEquals(File(profile, "AppData/Local/My-Game").path, CloudSavePaths.epic("{AppData}/MyGame", prefix, install, "acc", "app")!!.path) } + + @Test fun gogGalaxySdkEmptyLocationsUseDefaultLocation() { + val loc = CloudSavePaths.gogSaveFromRemote(true, emptyList(), "1")!! + assertEquals( + "%LocalAppData%/GOG.com/Galaxy/Applications/1/Storage/Shared/Files", + loc.template, + ) + assertEquals(CloudSavePaths.GOG_DEFAULT_NAMESPACE, loc.name) + val dir = CloudSavePaths.gog(loc.template, prefix, install) + assertEquals( + File(profile, "AppData/Local/GOG.com/Galaxy/Applications/1/Storage/Shared/Files").path, + dir!!.path, + ) + } + + @Test fun gogCloudDisabledOrUnknownClientIsNone() { + assertNull(CloudSavePaths.gogSaveFromRemote(false, emptyList(), "1")) + assertNull(CloudSavePaths.gogSaveFromRemote(true, emptyList(), "")) + assertNull(CloudSavePaths.gogSaveFromRemote(true, listOf("saves" to ""), "")) + } + + @Test fun gogNamedLocationKeepsItsNameAndPreferSaves() { + val loc = CloudSavePaths.gogSaveFromRemote( + true, + listOf("other" to "/A", "Saves" to "/Hyperstrange/ELDERBORN"), + "1", + )!! + assertEquals("/Hyperstrange/ELDERBORN", loc.template) + assertEquals("Saves", loc.name) + } } diff --git a/app/src/test/java/com/droiddeck/launcher/stores/CloudSavesTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/CloudSavesTest.kt index 0c30447430..0b80032627 100644 --- a/app/src/test/java/com/droiddeck/launcher/stores/CloudSavesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/stores/CloudSavesTest.kt @@ -4,6 +4,7 @@ import java.io.File import java.util.concurrent.Executor import org.junit.Assert.assertArrayEquals import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test import org.junit.runner.RunWith @@ -77,12 +78,22 @@ class CloudSavesTest { assertTrue(CloudSaves.dirty(app).contains(Store.GOG to gameId)) CloudSaves.uploadAllDirty(app, "recovery", running = true) assertTrue(CloudSaves.dirty(app).contains(Store.GOG to gameId)) - com.droiddeck.launcher.stores.gog.GogPrefs.get(app).edit().putString("cloud_template_$gameId", "").apply() + com.droiddeck.launcher.stores.gog.GogPrefs.get(app).edit() + .putString("cloud_template_$gameId", "none") + .putLong("cloud_template_at_$gameId", System.currentTimeMillis()) + .apply() assertEquals("no-cloud-saves", CloudSaves.uploadDirty(app, Store.GOG, gameId, "exit")!!.reason) assertTrue(CloudSaves.dirty(app).none { it.second == gameId }) assertEquals(null, CloudSaves.uploadDirty(app, Store.GOG, gameId, "session-end")) } + @Test fun aNegativeCloudTemplateIsRetriedAfterADay() { + val now = 1_000_000L + assertTrue(CloudSaves.noneTemplateIsCurrent(now, now + 60_000L)) + assertFalse(CloudSaves.noneTemplateIsCurrent(now, now + CloudSaves.TEMPLATE_NONE_TTL_MS)) + assertFalse(CloudSaves.noneTemplateIsCurrent(0L, now)) + } + @Test fun aFirstLaunchWithoutAPrefixIsDeferredNotBlocked() { val deferred = ArrayList() val folder = File(StoreInstallRoot.storeDir(StoreInstallRoot.internalRoot(app), Store.GOG), "ELDERBORN").apply { mkdirs() } diff --git a/app/src/test/java/com/droiddeck/launcher/stores/GogCloudKeysTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/GogCloudKeysTest.kt new file mode 100644 index 0000000000..df7ba8f2ba --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/GogCloudKeysTest.kt @@ -0,0 +1,61 @@ +package com.droiddeck.launcher.stores + +import java.io.ByteArrayOutputStream +import java.util.zip.GZIPOutputStream +import org.junit.Assert.assertArrayEquals +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test + +class GogCloudKeysTest { + @Test fun defaultLocationIsStrippedLikeAnyNamedLocation() { + val files = listOf(CloudFile("__default/savegame0.sav", 1L), CloudFile("saves/slot0.sav", 2L)) + val (chosen, relative) = gogRelativeNames(files, "__default") + assertEquals("__default", chosen) + assertEquals(listOf("savegame0.sav"), relative.map { it.name }) + } + + @Test fun gzipGogPutMatchesPythonGzipCompressLevel6Mtime0() { + val raw = "savegame0".toByteArray() + val expected = "1f8b08000000000000032b4e2c4b4d4fcc4d35000065fc333209000000".hex() + assertArrayEquals(expected, gzipGogPut(raw)) + assertArrayEquals(raw, gunzip(expected)) + } + + @Test fun contentEncodingGzipIsPeeledOnce() { + val raw = "savegame0".toByteArray() + val gz = ByteArrayOutputStream().also { GZIPOutputStream(it).use { z -> z.write(raw) } }.toByteArray() + assertArrayEquals(raw, gogDecodeCloudBody(gz, "gzip")) + assertArrayEquals(raw, gogDecodeCloudBody(raw, "gzip")) + } + + @Test fun corruptDeclaredGzipIsRejected() { + val corrupt = gzipGogPut("savegame0".toByteArray()).dropLast(2).toByteArray() + assertTrue(isGzip(corrupt)) + assertTrue(runCatching { gogDecodeCloudBody(corrupt, "gzip") }.isFailure) + } + + @Test fun isoDateHonorsOffsetAndFractionalSeconds() { + val expected = java.time.Instant.parse("2024-01-02T01:04:05.123Z").toEpochMilli() + assertEquals(expected, isoDate("2024-01-02T03:04:05.123+02:00")) + assertEquals(expected, isoDate("2024-01-02T01:04:05.123")) + } + + @Test fun gogPutWrappedBodyWithoutEncodingIsPeeled() { + val raw = "savegame0".toByteArray() + assertArrayEquals(raw, gogDecodeCloudBody(gzipGogPut(raw), null)) + assertArrayEquals(raw, gogDecodeCloudBody(raw, null)) + } + + @Test fun gzipThatIsTheSaveIsLeftIntact() { + val raw = "savegame0".toByteArray() + val gameGz = ByteArrayOutputStream().also { GZIPOutputStream(it).use { z -> z.write(raw) } }.toByteArray() + assertTrue(isGzip(gameGz)) + assertFalse(gzipGogPut(raw).contentEquals(gameGz)) + assertArrayEquals(gameGz, gogDecodeCloudBody(gameGz, null)) + } +} + +private fun String.hex() = chunked(2).map { it.toInt(16).toByte() }.toByteArray() diff --git a/docs/development/stores.md b/docs/development/stores.md index 97b119238b..cac5f58d8c 100644 --- a/docs/development/stores.md +++ b/docs/development/stores.md @@ -268,7 +268,12 @@ the prefix's user folder appears, within a minute. Every path logs `CloudSaveFolder`, e.g. `{AppData}/Game/Saved/SaveGames`. `CloudSavePaths` expands it inside the game's prefix (`compatdata//pfx/drive_c/users/steamuser`) or its install folder, case-insensitively, and refuses a path that leaves its boundary. A game whose store gives none - has no cloud saves. + has no cloud saves; a cached none is retried after a day. +- **Galaxy SDK saves:** `cloudStorage.enabled` with an empty `locations` list is Galaxy SDK + IStorage, whose objects live under a location named `__default` in + `%LocalAppData%/GOG.com/Galaxy/Applications//Storage/Shared/Files`. It goes through the + same location-qualified handling below. GET peels gzip when `Content-Encoding` is gzip, or when + the object round-trips as GOG's PUT gzip (deflate 6, mtime 0); a gzip-format save is left intact. - **GOG names are location-qualified:** the cloud API lists `saves/user1.dat`, where `saves` is the `name` of the selected `cloudStorage.locations` entry and the folder is that entry's `location` (`.../Hollow Knight Silksong/default`). `GogCloud` strips the location segment for the local path