fix(gog): skip saves GOG Cloud marks deleted (#3025)

* fix(gog): skip saves GOG Cloud marks deleted

GOG keeps a deleted cloud save in the listing as an empty gzip object
(hash aadd86936a80ee8a369579c3926f1b3c). The listing was read as plain
names without hashes, so every deleted save was downloaded again as an
empty file. Ask for the JSON listing, keep each object's hash, treat
deletion markers as absent, and only send a marked save back once it
changed locally. Local files are never removed.

* docs: credit gogdl and Lutris for the GOG cloud deletion marker
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-24 16:16:31 -05:00
1 parent ae47876386
commit 8bb1895702
7 files changed
+89 -10

No files matched your search

@@ -36,13 +36,14 @@ object CloudPlan {
return Plan(transfer, conflicts, same)
}
fun up(local: Map<String, Local>, remote: Map<String, Remote>, baseline: Map<String, Entry>?): Plan {
fun up(local: Map<String, Local>, remote: Map<String, Remote>, baseline: Map<String, Entry>?, deleted: Set<String> = emptySet()): Plan {
if (baseline == null) return Plan(emptyList(), emptyList(), emptyList(), refused = "no-baseline")
val transfer = ArrayList<String>(); val conflicts = ArrayList<String>(); val same = ArrayList<String>()
for ((name, l) in local) {
val r = remote[name]
val b = baseline[name]
when {
r == null && name in deleted && !localChanged(l, b) -> Unit
r == null -> transfer += name
same(l, r) -> same += name
localChanged(l, b) && cloudChanged(r, b) -> conflicts += name
@@ -430,17 +430,20 @@ object CloudSaves {
internal fun md5(data: ByteArray): String = MessageDigest.getInstance("MD5").digest(data).joinToString("") { "%02x".format(it) }
private fun remote(cloud: CloudTransport, wanted: (String) -> Boolean): Map<String, CloudFile> {
private class Listing(val live: Map<String, CloudFile>, val deleted: Set<String>)
private fun remote(cloud: CloudTransport, wanted: (String) -> Boolean): Listing {
val files = cloud.list()
val pool = Executors.newFixedThreadPool(PARALLEL, com.droiddeck.launcher.stores.download.DownloadQueue.workerFactory("cloud"))
for (f in files) if (wanted(f.name)) pool.execute { runCatching { cloud.details(f) } }
pool.shutdown(); pool.awaitTermination(2, TimeUnit.MINUTES)
return files.associateBy { it.name }
val (gone, live) = files.partition(cloud::deleted)
return Listing(live.associateBy { it.name }, gone.mapTo(HashSet()) { it.name })
}
internal fun downWith(context: Context, store: Store, id: String, dir: File, cloud: CloudTransport, force: Boolean, claim: () -> Boolean = { true }): Result {
val files = localFiles(dir).filterKeys(cloud::wants)
val remote = remote(cloud) { it in files }
val remote = remote(cloud) { it in files }.live
val local = files.mapValues { CloudPlan.Local(md5(it.value)) }
val key = scope(store, id, dir)
val base = baseline(context, store, key)
@@ -498,9 +501,10 @@ object CloudSaves {
val base = baseline(context, store, key) ?: return Result("skipped", 0, 0, "no-baseline")
val files = localFiles(dir).filterKeys(cloud::wants).let { all -> if (cloud is UbisoftCloudTransport) all.filterValues(::markUbisoftSave) else all }
if (files.isEmpty()) return Result("skipped", 0, 0, "nothing-local")
val remote = remote(cloud) { it in files }
val listing = remote(cloud) { it in files }
val remote = listing.live
val local = files.mapValues { CloudPlan.Local(md5(it.value)) }
val planned = CloudPlan.up(local, remote.mapValues { CloudPlan.Remote(it.value.md5, it.value.modifiedMs) }, base)
val planned = CloudPlan.up(local, remote.mapValues { CloudPlan.Remote(it.value.md5, it.value.modifiedMs) }, base, listing.deleted)
val alike = alike(cloud, planned.conflicts, local, remote)
val plan = settled(planned, alike)
val saved = savedConflicts(context, store, key).filter { it !in alike && it !in plan.same }
@@ -547,7 +551,7 @@ object CloudSaves {
send = send.filter { it in stamped }
for ((name, t) in stamped) if (t > 0) files[name]?.setLastModified(t)
}
val after = if (send.all { cloud.committed(it) != null }) emptyMap() else remote(cloud) { it in send && cloud.committed(it) == null }
val after = if (send.all { cloud.committed(it) != null }) emptyMap() else remote(cloud) { it in send && cloud.committed(it) == null }.live
for (name in send) {
val r = cloud.committed(name) ?: after[name]
next[name] = CloudPlan.Entry(sentMd5.getValue(name), r?.md5, r?.modifiedMs ?: stamped[name] ?: -1L)
@@ -31,6 +31,7 @@ interface CloudTransport {
val contentDigests: Boolean get() = true
fun committed(name: String): CloudFile? = null
fun wants(name: String): Boolean = true
fun deleted(file: CloudFile): Boolean = false
fun list(): List<CloudFile>
fun details(file: CloudFile) {}
fun get(file: CloudFile): Pair<ByteArray, Long>
@@ -252,6 +253,8 @@ private fun gzipFile(src: File, dst: File): GzipFile {
}
}
internal const val GOG_DELETED_MD5 = "aadd86936a80ee8a369579c3926f1b3c"
internal const val GOG_CLOUD_UA = "GOGGalaxyCommunicationService/2.0.13.27 (Windows_32bit) dont_sync_marker/true installation_source/gog"
/** GOG Galaxy parses `X-Object-Meta-LocalLastModified`; objects without it fail as "invalid data". */
@@ -276,8 +279,11 @@ class GogCloud(
private fun cloudKey(name: String) = key((prefix ?: "") + name)
override fun deleted(file: CloudFile): Boolean = file.md5.equals(GOG_DELETED_MD5, ignoreCase = true)
override fun list(): List<CloudFile> {
val c = open(base, "GET", token, ua)
c.setRequestProperty("Accept", "application/json")
val code = c.responseCode
if (code == 404) { c.disconnect(); return emptyList() }
if (code !in 200..299) {
@@ -300,7 +306,7 @@ class GogCloud(
lm > 0 -> lm * 1000
iso > 0 -> iso
else -> -1L
})
}, o.optString("hash").trim().ifEmpty { null })
}
} else {
text.lines().map { it.trim() }.filter { it.isNotEmpty() }.map { CloudFile(it, -1L) }
@@ -48,6 +48,15 @@ class CloudPlanTest {
assertEquals(listOf("s"), CloudPlan.up(mapOf("s" to Local("c")), mapOf("s" to Remote("c", 9000)), base).same)
}
@Test fun aSaveDeletedInTheCloudGoesUpOnlyOnceItChangesLocally() {
val base = mapOf("s" to Entry("a", "a", 1000), "t" to Entry("x", "x", 1000))
val gone = setOf("s")
val idle = CloudPlan.up(mapOf("s" to Local("a"), "t" to Local("y"), "n" to Local("new")), mapOf("t" to Remote("x", 1000)), base, gone)
assertEquals(setOf("t", "n"), idle.transfer.toSet())
assertEquals(listOf("s"), CloudPlan.up(mapOf("s" to Local("b")), emptyMap(), base, gone).transfer)
assertEquals(listOf("s"), CloudPlan.up(mapOf("s" to Local("a")), emptyMap(), base).transfer)
}
@Test fun timesAloneDecideWhenTheServiceGivesNoHash() {
val base = mapOf("s" to Entry("a", null, 1000))
assertEquals(listOf("s"), CloudPlan.down(mapOf("s" to Local("a")), mapOf("s" to Remote(null, 60_000)), base).transfer)
@@ -25,7 +25,9 @@ class CloudSavesTest {
val puts = ArrayList<String>()
var onGet: (CloudFile) -> Unit = {}
var raceAfter = Int.MAX_VALUE
override fun list() = files.map { (k, v) -> CloudFile(k, v.second, CloudSaves.md5(v.first)) }
val gone = HashSet<String>()
override fun deleted(file: CloudFile) = file.md5 == GOG_DELETED_MD5
override fun list() = files.map { (k, v) -> CloudFile(k, v.second, CloudSaves.md5(v.first)) } + gone.map { CloudFile(it, 5000L, GOG_DELETED_MD5) }
override fun get(file: CloudFile) = files.getValue(file.name).also { onGet(file) }
override fun put(files: Map<String, ByteArray>, modifiedMs: Map<String, Long>): Map<String, Long> {
val sent = LinkedHashMap<String, Long>()
@@ -40,6 +42,40 @@ class CloudSavesTest {
private fun dir() = File(app.filesDir, "saves-${System.nanoTime()}").apply { mkdirs() }
private val id get() = "g${System.nanoTime()}"
@Test fun savesTheCloudMarksDeletedAreNotDownloaded() {
val dir = dir()
val cloud = Memory().apply {
files["save1.ess"] = "kept".toByteArray() to 1000L
gone += listOf("save2.ess", "save3.ess", "save3.skse")
}
val r = CloudSaves.downWith(app, Store.GOG, id, dir, cloud, force = true)
assertEquals("ok", r.result); assertEquals(1, r.files)
assertEquals(listOf("save1.ess"), dir.list()!!.sorted())
}
@Test fun aLocalSaveTheCloudMarksDeletedIsLeftAlone() {
val dir = dir(); File(dir, "save2.ess").writeText("mine")
val cloud = Memory().apply { files["save1.ess"] = "kept".toByteArray() to 1000L; gone += "save2.ess" }
assertEquals("ok", CloudSaves.downWith(app, Store.GOG, id, dir, cloud, force = false).result)
assertEquals("mine", File(dir, "save2.ess").readText())
assertEquals("kept", File(dir, "save1.ess").readText())
}
@Test fun anUnchangedSaveDeletedInTheCloudIsNotSentBackButANewerOneIs() {
val game = id; val dir = dir()
val cloud = Memory().apply { files["save1.ess"] = "a".toByteArray() to 1000L; files["save2.ess"] = "b".toByteArray() to 1000L }
assertEquals("ok", CloudSaves.downWith(app, Store.GOG, game, dir, cloud, force = false).result)
cloud.files.remove("save2.ess"); cloud.gone += "save2.ess"
val idle = CloudSaves.upWith(app, Store.GOG, game, dir, cloud, force = false)
assertEquals("up-to-date", idle.reason)
assertTrue(cloud.puts.isEmpty())
File(dir, "save2.ess").writeText("b played again")
val r = CloudSaves.upWith(app, Store.GOG, game, dir, cloud, force = false)
assertEquals(listOf("save2.ess"), cloud.puts)
assertEquals("b played again", String(cloud.files.getValue("save2.ess").first))
assertEquals(1, r.files)
}
@Test fun anUploadBeforeAnyDownloadIsRefused() {
val dir = dir(); File(dir, "slot0.sav").writeText("fresh profile")
val cloud = Memory().apply { files["slot0.sav"] = "real save".toByteArray() to 1000L }
@@ -8,7 +8,12 @@ import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class GogCloudKeysTest {
@Test fun defaultLocationIsStrippedLikeAnyNamedLocation() {
val files = listOf(CloudFile("__default/savegame0.sav", 1L), CloudFile("saves/slot0.sav", 2L))
@@ -183,6 +188,24 @@ class GogCloudKeysTest {
requests.forEach { assertEquals("Bearer token", it.headers["authorization"]) }
}
@Test fun listAsksForJsonAndCarriesGalaxyDeletionMarks() {
val body = "[{\"name\":\"saves/save1.ess\",\"hash\":\"0123456789abcdef0123456789abcdef\",\"last_modified\":\"2026-11-17T22:58:55.038000+00:00\"}," +
"{\"name\":\"saves/save2.ess\",\"hash\":\"$GOG_DELETED_MD5\",\"last_modified\":\"2026-11-17T22:59:00+00:00\"}]"
lateinit var files: List<CloudFile>
lateinit var cloud: GogCloud
val requests = serve(1, { "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: ${body.length}\r\nConnection: close\r\n\r\n$body" }) { url ->
cloud = GogCloud("user", "client", "token", "saves", url)
files = cloud.list()
}
assertEquals("application/json", requests.single().headers["accept"])
assertEquals(listOf("save1.ess", "save2.ess"), files.map { it.name })
assertEquals("0123456789abcdef0123456789abcdef", files[0].md5)
assertEquals(1_794_956_335_038L, files[0].modifiedMs)
assertFalse(cloud.deleted(files[0]))
assertTrue(cloud.deleted(files[1]))
assertFalse(cloud.deleted(CloudFile("save3.ess", 1L)))
}
@Test fun rejectedListKeepsTheServerErrorInTheDetail() {
val body = "{\"error\":\"invalid_version\",\"error_description\":\"not supported with this version\"}"
val failure = runCatching {