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 <danielhimebauch@gmail.com>
Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-10-25 13:20:29 -04:00
1 parent 27e390a846
commit ae193e9b8d
7 files changed
+245 -34

No files matched your search

@@ -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<Pair<String, String>>, 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
@@ -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)
@@ -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<ByteArray, Long> {
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<String, ByteArray>): Map<String, Long> = files.mapValues { (name, data) ->