fix: upload GOG cloud saves in Galaxy's gzip + metadata format (#1952)

fix: upload GOG cloud saves in Galaxy's gzip format
This commit is contained in:
Moroni Granja authored and GitHub committed 2026-11-05 18:56:21 -05:00
1 parent 5971b91ed4
commit cd452e2b60
7 files changed
+244 -22

No files matched your search

@@ -54,6 +54,22 @@ class CloudPlanTest {
assertTrue(CloudPlan.down(mapOf("s" to Local("a")), mapOf("s" to Remote(null, 2500)), base).transfer.isEmpty())
}
@Test fun gogRawObjectsThatStillMatchTheLocalBytesAreResentOnce() {
val base = mapOf("s" to Entry("a", "a", 1000))
val plan = CloudPlan.up(mapOf("s" to Local("a")), mapOf("s" to Remote("a", 1000)), base)
assertEquals(listOf("s"), plan.same)
assertEquals(listOf("s"), CloudPlan.upSend(plan, force = false, repairRawGog = true))
assertTrue(CloudPlan.upSend(plan, force = false, repairRawGog = false).isEmpty())
}
@Test fun aForcedGogUploadSendsUnchangedFilesEvenWithoutACloudHash() {
val base = mapOf("s" to Entry("a", "a", 1000))
val plan = CloudPlan.up(mapOf("s" to Local("a")), mapOf("s" to Remote(null, 1000)), base)
assertTrue(plan.transfer.isEmpty() && plan.conflicts.isEmpty() && plan.same.isEmpty())
assertEquals(listOf("s"), CloudPlan.upSend(plan, force = true, repairRawGog = true, allLocal = listOf("s")))
assertTrue(CloudPlan.upSend(plan, force = true, repairRawGog = false, allLocal = listOf("s")).isEmpty())
}
@Test fun theBaselineSurvivesItsJson() {
val b = mapOf("a/b.sav" to Entry("l", "c", 5), "x" to Entry("l2", null, -1))
assertEquals(b, CloudPlan.fromJson(CloudPlan.toJson(b)))
@@ -22,7 +22,7 @@ class CloudSavesTest {
val puts = ArrayList<String>()
override fun list() = files.map { (k, v) -> CloudFile(k, v.second, CloudSaves.md5(v.first)) }
override fun get(file: CloudFile) = files.getValue(file.name)
override fun put(files: Map<String, ByteArray>): Map<String, Long> =
override fun put(files: Map<String, ByteArray>, modifiedMs: Map<String, Long>): Map<String, Long> =
files.mapValues { (k, v) -> puts += k; this.files[k] = v to 99_000L; 99_000L }
}
@@ -56,6 +56,81 @@ class GogCloudKeysTest {
assertFalse(gzipGogPut(raw).contentEquals(gameGz))
assertArrayEquals(gameGz, gogDecodeCloudBody(gameGz, null))
}
@Test fun putUploadsTheGogGzipBodyWithEncodingAndCompressedHash() {
val raw = "savegame0".toByteArray()
val server = java.net.ServerSocket(0, 4, java.net.InetAddress.getByName("127.0.0.1"))
val requests = java.util.concurrent.CopyOnWriteArrayList<Request>()
val worker = Thread {
repeat(2) {
server.accept().use { client ->
client.soTimeout = 5_000
val input = client.getInputStream()
val line = readLine(input)
val parts = line.split(' ')
val headers = LinkedHashMap<String, String>()
while (true) {
val h = readLine(input)
if (h.isEmpty()) break
val colon = h.indexOf(':')
headers[h.substring(0, colon).trim().lowercase()] = h.substring(colon + 1).trim()
}
val length = headers["content-length"]?.toIntOrNull() ?: 0
val body = ByteArray(length)
var offset = 0
while (offset < length) {
val n = input.read(body, offset, length - offset)
if (n < 0) break
offset += n
}
requests += Request(parts[0], parts[1], headers, body)
val response = if (parts[0] == "PUT") {
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
} else {
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n" +
"Etag: \"${CloudSaves.md5(body)}\"\r\n" +
"Last-Modified: Tue, 20 Oct 2026 10:00:00 GMT\r\nConnection: close\r\n\r\n"
}
client.getOutputStream().apply { write(response.toByteArray()); flush() }
}
}
}.apply { isDaemon = true; start() }
try {
val cloud = GogCloud("user", "client", "token", "saves", "http://127.0.0.1:${server.localPort}/v1/user/client")
val source = java.io.File.createTempFile("save", ".sav").apply { writeBytes(raw) }
val result = try {
cloud.putFiles(mapOf("save.sav" to source), mapOf("save.sav" to 1_793_490_825_000L)).getValue("save.sav")
} finally {
source.delete()
}
assertTrue(result.modifiedMs > 0)
assertEquals(CloudSaves.md5(raw), result.md5)
val put = requests[0]
assertEquals("PUT", put.method)
assertEquals("/v1/user/client/saves%2Fsave.sav", put.path)
assertEquals("gzip", put.headers["content-encoding"])
assertEquals("2026-10-31T23:53:45+00:00", put.headers["x-object-meta-locallastmodified"])
assertArrayEquals(gzipGogPut(raw), put.body)
assertEquals(CloudSaves.md5(put.body), put.headers["etag"])
assertArrayEquals(raw, gogDecodeCloudBody(put.body, put.headers["content-encoding"]))
assertEquals("HEAD", requests[1].method)
} finally {
server.close()
worker.join(2_000)
}
}
}
private class Request(val method: String, val path: String, val headers: Map<String, String>, val body: ByteArray)
private fun readLine(input: java.io.InputStream): String {
val out = ByteArrayOutputStream()
while (true) {
val b = input.read()
if (b < 0 || b == '\n'.code) break
if (b != '\r'.code) out.write(b)
}
return out.toString("UTF-8")
}
private fun String.hex() = chunked(2).map { it.toInt(16).toByte() }.toByteArray()