mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 06:15:58 +00:00
feat(ea, ubisoft): cloud saves and a kept Ubisoft Denuvo token (#2824)
* feat(ubisoft): keep Denuvo game tokens per account and product until the game invalidates them * feat(ubisoft): sync cloud saves through Ubisoft's cloud save service * fix(xbox): turning cloud saves off passes the offline mode the game save runtime reads * fix(ubisoft): Ubisoft games list their shared prefix, so they get a saves and cloud tab * feat(ea): sync cloud saves through EA's cloud sync service * fix(ea): sync at EA launch and exit, keep one rule snapshot, and check the cloud copy under the write lock * fix(ubisoft): exact slot versions, scoped save export, and a Denuvo cache bound to its account * fix(ea): fresh rules per sync, checksums required, backup before restore, and sign-in shown on the saves tab * fix(ubisoft): launch-time sync on the shared prefix, saves scoped to the game, and Denuvo bound to each launch's account * fix(ea): back up only the saves a restore replaces, refuse truncated manifests, and need a master title id * fix(ubisoft): treat a slot whose bytes changed since the last sync as a conflict before uploading over it * fix(ea): one resolved save folder per sync, refuse case collisions, and walk each folder once * fix(ubisoft): sync only identified slots, keep live saves and backups safe, and tie a pending eviction to its account * fix(cloud): a forced download fetches every saved conflict and keeps the ones it could not write * test(ea): keep the cloud save cases in EaTest * fix(ubisoft): report only written slots, keep drift conflicts, read whole headers, and never rename a named eviction * fix(ea): keep real save folders in the inventory, back up any restored file, refuse duplicate or malformed cloud listings * fix(cloud): commit Ubisoft slots one at a time and stop on a newer cloud write, name skipped and blocked slots, keep unsent conflicts * fix(ubisoft): acknowledge an invalidation only once its marker is published and never serve a kept token after it * fix(cloud): a cloud request the launcher cannot write still starts the game and keeps the exit upload * fix(cloud): scope Ubisoft sync state to the account and product, compare alike slots, keep committed progress, and name partial slots * fix(ea): ask for sign-in after a rejected refresh, upload saves reached through folder aliases * fix(cloud): a launch that stops waiting for the cloud download claims it back, so no save is written once the game starts * fix(ubisoft): name the account in an invalidation marker and never hold the token lock across a pipe write * fix(ea): only invalid_grant asks for sign-in again, and case collisions count only among synced saves * test(epic): let the launcher's ask doubles take the claim flag * fix(ea): apply save rules in their order, match bare patterns inside the save folder, and resolve LocalLow and install folders * fix(ubisoft): stamp a fresh identifier into a new save that has none, so it can sync * fix(ubisoft): answer a burn ticket request as unavailable instead of leaving the game waiting * fix(cloud): read EA manifests with the XML parser, clear conflicts whose sides match, and bound the claim and stall tests
This commit is contained in:
1 parent
6ed281f78d
commit
123c431b8e
31 files changed
+1999
-132
No files matched your search
@@ -1,5 +1,8 @@
|
||||
package com.droiddeck.launcher.stores
|
||||
|
||||
import com.droiddeck.launcher.stores.ubisoft.MemorySlots
|
||||
import com.droiddeck.launcher.stores.ubisoft.UbisoftCloud
|
||||
import com.droiddeck.launcher.stores.ubisoft.UbisoftCloudTransport
|
||||
import java.io.File
|
||||
import java.util.concurrent.Executor
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
@@ -20,10 +23,18 @@ class CloudSavesTest {
|
||||
private class Memory : CloudTransport {
|
||||
val files = HashMap<String, Pair<ByteArray, Long>>()
|
||||
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)) }
|
||||
override fun get(file: CloudFile) = files.getValue(file.name)
|
||||
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 }
|
||||
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>()
|
||||
for ((k, v) in files) {
|
||||
if (sent.size >= raceAfter) throw CloudChanged(sent, files.keys.filter { it !in sent })
|
||||
puts += k; this.files[k] = v to 99_000L; sent[k] = 99_000L
|
||||
}
|
||||
return sent
|
||||
}
|
||||
}
|
||||
|
||||
private fun dir() = File(app.filesDir, "saves-${System.nanoTime()}").apply { mkdirs() }
|
||||
@@ -50,6 +61,72 @@ class CloudSavesTest {
|
||||
assertEquals("v1", File(kept, "slot0.sav").readText())
|
||||
}
|
||||
|
||||
@Test fun aFolderAliasNeverHidesTheRealSaveFromARestore() {
|
||||
val game = id; val dir = dir()
|
||||
File(dir, "SaveData").mkdirs(); File(dir, "SaveData/slot.sav").writeText("local")
|
||||
val link = runCatching { java.nio.file.Files.createSymbolicLink(File(dir, "Cache").toPath(), File(dir, "SaveData").toPath()) }
|
||||
org.junit.Assume.assumeTrue(link.isSuccess)
|
||||
val memory = Memory().apply { files["SaveData/slot.sav"] = "cloud".toByteArray() to 1000L }
|
||||
val cloud = object : CloudTransport by memory {
|
||||
override fun wants(name: String) = name.startsWith("SaveData/")
|
||||
}
|
||||
CloudSaves.downWith(app, Store.GOG, game, dir, cloud, force = true)
|
||||
assertEquals("cloud", File(dir, "SaveData/slot.sav").readText())
|
||||
val kept = File(app.filesDir, "stores/cloud-backups/gog-$game").listFiles { f -> f.name.startsWith("local-") }!!.single()
|
||||
assertEquals("local", File(kept, "SaveData/slot.sav").readText())
|
||||
}
|
||||
|
||||
@Test fun aSaveReachedOnlyThroughAFolderAliasStillGoesUp() {
|
||||
val game = id; val dir = dir()
|
||||
File(dir, "Actual").mkdirs(); File(dir, "Actual/slot.sav").writeText("played")
|
||||
val link = runCatching { java.nio.file.Files.createSymbolicLink(File(dir, "SaveData").toPath(), File(dir, "Actual").toPath()) }
|
||||
org.junit.Assume.assumeTrue(link.isSuccess)
|
||||
java.nio.file.Files.createSymbolicLink(File(dir, "Actual/loop").toPath(), dir.toPath())
|
||||
val memory = Memory()
|
||||
val cloud = object : CloudTransport by memory {
|
||||
override fun wants(name: String) = name.startsWith("SaveData/")
|
||||
}
|
||||
CloudSaves.downWith(app, Store.GOG, game, dir, cloud, force = false)
|
||||
val r = CloudSaves.upWith(app, Store.GOG, game, dir, cloud, force = false)
|
||||
assertEquals("uploaded", r.reason)
|
||||
assertEquals(listOf("SaveData/slot.sav"), memory.puts)
|
||||
assertEquals("played", String(memory.files.getValue("SaveData/slot.sav").first))
|
||||
}
|
||||
|
||||
@Test fun onlySelectedSavesCanCollideByCase() {
|
||||
val names = listOf("Logs/A.txt", "Logs/a.txt", "SaveGame/1")
|
||||
assertFalse(CloudSaves.caseCollision(names) { !it.startsWith("Logs/") })
|
||||
assertTrue(CloudSaves.caseCollision(names) { true })
|
||||
assertTrue(CloudSaves.caseCollision(listOf("SaveGame/1", "savegame/1")) { it.lowercase().startsWith("savegame/") })
|
||||
}
|
||||
|
||||
@Test fun aSavedConflictWhoseSidesNowMatchIsCleared() {
|
||||
val game = id; val dir = dir()
|
||||
val cloud = Memory().apply { files["slot0.sav"] = "v1".toByteArray() to 1000L }
|
||||
CloudSaves.downWith(app, Store.GOG, game, dir, cloud, force = false)
|
||||
cloud.files["slot0.sav"] = "v2".toByteArray() to 50_000L
|
||||
File(dir, "slot0.sav").writeText("v2")
|
||||
com.droiddeck.launcher.stores.gog.GogPrefs.get(app).edit().putString("cloud_conflicts_$game", "slot0.sav").apply()
|
||||
assertEquals("up-to-date", CloudSaves.downWith(app, Store.GOG, game, dir, cloud, force = false).reason)
|
||||
assertTrue(CloudSaves.conflicts(app, Store.GOG, game).isEmpty())
|
||||
com.droiddeck.launcher.stores.gog.GogPrefs.get(app).edit().putString("cloud_conflicts_$game", "slot0.sav").apply()
|
||||
CloudSaves.upWith(app, Store.GOG, game, dir, cloud, force = false)
|
||||
assertTrue(CloudSaves.conflicts(app, Store.GOG, game).isEmpty())
|
||||
}
|
||||
|
||||
@Test fun aDownloadTheLaunchStoppedWaitingForWritesNothing() {
|
||||
val game = id; val dir = dir()
|
||||
File(dir, "slot0.sav").writeText("local")
|
||||
val cloud = Memory().apply { files["slot0.sav"] = "v1".toByteArray() to 1000L }
|
||||
val r = CloudSaves.downWith(app, Store.GOG, game, dir, cloud, force = true, claim = { false })
|
||||
assertEquals("skipped", r.result); assertEquals("launch-moved-on", r.reason)
|
||||
assertEquals("local", File(dir, "slot0.sav").readText())
|
||||
assertFalse(File(app.filesDir, "stores/cloud-backups/gog-$game").exists())
|
||||
var claimed = 0
|
||||
assertEquals("ok", CloudSaves.downWith(app, Store.GOG, game, dir, cloud, force = true, claim = { claimed++; true }).result)
|
||||
assertEquals(1, claimed); assertEquals("v1", File(dir, "slot0.sav").readText())
|
||||
}
|
||||
|
||||
@Test fun aFileChangedOnBothSidesIsLeftForTheUser() {
|
||||
val game = id; val dir = dir()
|
||||
val cloud = Memory().apply { files["slot0.sav"] = "v1".toByteArray() to 1000L }
|
||||
@@ -65,6 +142,134 @@ class CloudSavesTest {
|
||||
assertTrue(CloudSaves.conflicts(app, Store.GOG, game).isEmpty())
|
||||
}
|
||||
|
||||
@Test fun aSlotTheGameRewritesDuringADownloadIsKeptAndNamedAsAConflict() {
|
||||
val game = id; val dir = dir()
|
||||
val cloud = Memory().apply { files["slot0.sav"] = "v1".toByteArray() to 1000L }
|
||||
CloudSaves.downWith(app, Store.GOG, game, dir, cloud, force = false)
|
||||
cloud.files["slot0.sav"] = "cloud v2".toByteArray() to 50_000L
|
||||
cloud.onGet = { File(dir, "slot0.sav").writeText("game wrote") }
|
||||
val r = CloudSaves.downWith(app, Store.GOG, game, dir, cloud, force = false)
|
||||
assertEquals("partial", r.result); assertEquals("changed-locally", r.reason); assertEquals(1, r.conflicts)
|
||||
assertTrue(r.detail.contains("slot0.sav"))
|
||||
assertEquals("game wrote", File(dir, "slot0.sav").readText())
|
||||
assertEquals(listOf("slot0.sav"), CloudSaves.conflicts(app, Store.GOG, game))
|
||||
}
|
||||
|
||||
@Test fun aCloudNameOutsideTheSaveFolderIsBlockedNotAConflict() {
|
||||
val game = id; val dir = dir()
|
||||
val cloud = Memory().apply { files["../escape.sav"] = "x".toByteArray() to 1000L }
|
||||
val r = CloudSaves.downWith(app, Store.GOG, game, dir, cloud, force = false)
|
||||
assertEquals("partial", r.result); assertEquals("blocked-path", r.reason); assertEquals(0, r.conflicts)
|
||||
assertTrue(r.detail.contains("../escape.sav"))
|
||||
assertFalse(File(dir.parentFile, "escape.sav").exists())
|
||||
assertTrue(CloudSaves.conflicts(app, Store.GOG, game).isEmpty())
|
||||
}
|
||||
|
||||
@Test fun anUploadStoppedByANewerCloudWriteKeepsTheRestAsConflicts() {
|
||||
val game = id; val dir = dir()
|
||||
val cloud = Memory().apply { files["a.sav"] = "a1".toByteArray() to 1000L; files["b.sav"] = "b1".toByteArray() to 1000L }
|
||||
CloudSaves.downWith(app, Store.GOG, game, dir, cloud, force = false)
|
||||
File(dir, "a.sav").writeText("a2"); File(dir, "b.sav").writeText("b2")
|
||||
cloud.raceAfter = 1
|
||||
val r = CloudSaves.upWith(app, Store.GOG, game, dir, cloud, force = false)
|
||||
assertEquals("partial", r.result); assertEquals("changed-in-cloud", r.reason); assertEquals(1, r.files)
|
||||
val unsent = listOf("a.sav", "b.sav").single { it !in cloud.puts }
|
||||
assertEquals(listOf(unsent), CloudSaves.conflicts(app, Store.GOG, game))
|
||||
}
|
||||
|
||||
@Test fun aForcedUploadKeepsConflictsForSlotsItDidNotSend() {
|
||||
val game = id; val dir = dir()
|
||||
val cloud = Memory().apply { files["slot0.sav"] = "v1".toByteArray() to 1000L; files["slot1.sav"] = "w1".toByteArray() to 1000L }
|
||||
CloudSaves.downWith(app, Store.GOG, game, dir, cloud, force = false)
|
||||
File(dir, "slot0.sav").writeText("local v2"); File(dir, "slot1.sav").delete()
|
||||
cloud.files["slot0.sav"] = "cloud v2".toByteArray() to 50_000L
|
||||
com.droiddeck.launcher.stores.gog.GogPrefs.get(app).edit().putString("cloud_conflicts_$game", "slot0.sav\nslot1.sav").apply()
|
||||
CloudSaves.upWith(app, Store.GOG, game, dir, cloud, force = true)
|
||||
assertEquals("local v2", String(cloud.files.getValue("slot0.sav").first))
|
||||
assertEquals(listOf("slot1.sav"), CloudSaves.conflicts(app, Store.GOG, game))
|
||||
}
|
||||
|
||||
private fun ubisoftDir(user: String, root: File = File(app.filesDir, "ubi-${System.nanoTime()}")) = File(root, "$user/77").apply { mkdirs() }
|
||||
|
||||
@Test fun aSlotBothSidesSavedAlikeIsNotAConflictWhenTheCloudNamesOnlyAVersion() {
|
||||
val game = id; val dir = ubisoftDir("u1")
|
||||
val slots = MemorySlots().apply { store("1", MemorySlots.save("a1")) }
|
||||
assertEquals("ok", CloudSaves.downWith(app, Store.UBISOFT, game, dir, UbisoftCloudTransport(slots, "u1"), force = false).result)
|
||||
File(dir, "1.save").writeBytes(MemorySlots.save("a2"))
|
||||
slots.store("1", MemorySlots.save("a2"))
|
||||
val down = CloudSaves.downWith(app, Store.UBISOFT, game, dir, UbisoftCloudTransport(slots, "u1"), force = false)
|
||||
assertEquals(0, down.conflicts)
|
||||
assertTrue(CloudSaves.conflicts(app, Store.UBISOFT, game, dir).isEmpty())
|
||||
val up = CloudSaves.upWith(app, Store.UBISOFT, game, dir, UbisoftCloudTransport(slots, "u1"), force = false)
|
||||
assertEquals("up-to-date", up.reason); assertEquals(0, up.conflicts)
|
||||
File(dir, "1.save").writeBytes(MemorySlots.save("a3 local"))
|
||||
slots.store("1", MemorySlots.save("a3 cloud"))
|
||||
assertEquals(1, CloudSaves.downWith(app, Store.UBISOFT, game, dir, UbisoftCloudTransport(slots, "u1"), force = false).conflicts)
|
||||
}
|
||||
|
||||
@Test fun aNewUbisoftSaveWithAnEmptyIdentifierIsStampedAndUploaded() {
|
||||
val game = id; val dir = ubisoftDir("u1")
|
||||
val slots = MemorySlots().apply { store("1", MemorySlots.save("a1")) }
|
||||
CloudSaves.downWith(app, Store.UBISOFT, game, dir, UbisoftCloudTransport(slots, "u1"), force = false)
|
||||
File(dir, "2.save").writeBytes("UBISAVE0".toByteArray() + ByteArray(32) + "fresh slot".toByteArray())
|
||||
File(dir, "3.save").writeBytes("UBISAVE0".toByteArray() + "not-an-identifier-but-not-empty!".toByteArray())
|
||||
val up = CloudSaves.upWith(app, Store.UBISOFT, game, dir, UbisoftCloudTransport(slots, "u1"), force = false)
|
||||
assertEquals("uploaded", up.reason)
|
||||
val stamped = UbisoftCloud.mark(File(dir, "2.save").readBytes())
|
||||
assertTrue(stamped != null)
|
||||
assertEquals("fresh slot", String(File(dir, "2.save").readBytes().copyOfRange(40, 50)))
|
||||
assertEquals(stamped, UbisoftCloud.mark(slots.data.getValue("2")))
|
||||
assertFalse("3" in slots.data)
|
||||
assertEquals("not-an-identifier-but-not-empty!", String(File(dir, "3.save").readBytes().copyOfRange(8, 40)))
|
||||
}
|
||||
|
||||
@Test fun aSlotCommittedBeforeALaterOneFailsReachesTheBaseline() {
|
||||
val game = id; val dir = ubisoftDir("u1")
|
||||
val slots = MemorySlots().apply { store("1", MemorySlots.save("a1")); store("2", MemorySlots.save("b1")) }
|
||||
CloudSaves.downWith(app, Store.UBISOFT, game, dir, UbisoftCloudTransport(slots, "u1"), force = false)
|
||||
File(dir, "1.save").writeBytes(MemorySlots.save("a2")); File(dir, "2.save").writeBytes(MemorySlots.save("b2"))
|
||||
slots.uploadsLeft = 1
|
||||
assertTrue(runCatching { CloudSaves.upWith(app, Store.UBISOFT, game, dir, UbisoftCloudTransport(slots, "u1"), force = false) }
|
||||
.exceptionOrNull() is java.io.IOException)
|
||||
val base = CloudSaves.baseline(app, Store.UBISOFT, CloudSaves.scope(Store.UBISOFT, game, dir))!!
|
||||
val sent = listOf("1", "2").single { slots.data.getValue(it).contentEquals(MemorySlots.save(if (it == "1") "a2" else "b2")) }
|
||||
val kept = if (sent == "1") "2" else "1"
|
||||
assertEquals(CloudSaves.md5(File(dir, "$sent.save")), base.getValue("$sent.save").localMd5)
|
||||
assertEquals(CloudSaves.md5(MemorySlots.save(if (kept == "1") "a1" else "b1")), base.getValue("$kept.save").localMd5)
|
||||
slots.uploadsLeft = Int.MAX_VALUE
|
||||
val r = CloudSaves.upWith(app, Store.UBISOFT, game, dir, UbisoftCloudTransport(slots, "u1"), force = false)
|
||||
assertEquals("ok", r.result); assertEquals(1, r.files); assertEquals(0, r.conflicts)
|
||||
}
|
||||
|
||||
@Test fun ubisoftConflictsBelongToTheAccountAndProductThatHadThem() {
|
||||
val game = id; val root = File(app.filesDir, "ubi-${System.nanoTime()}")
|
||||
val a = ubisoftDir("uA", root); val b = ubisoftDir("uB", root)
|
||||
val cloudB = Memory().apply { files["slot0.save"] = "b1".toByteArray() to 1000L }
|
||||
CloudSaves.downWith(app, Store.UBISOFT, game, b, cloudB, force = false)
|
||||
File(b, "slot0.save").writeText("b local")
|
||||
val cloudA = Memory().apply { files["slot0.save"] = "a1".toByteArray() to 1000L }
|
||||
CloudSaves.downWith(app, Store.UBISOFT, game, a, cloudA, force = false)
|
||||
File(a, "slot0.save").writeText("a local")
|
||||
cloudA.files["slot0.save"] = "a2".toByteArray() to 50_000L
|
||||
assertEquals(1, CloudSaves.downWith(app, Store.UBISOFT, game, a, cloudA, force = false).conflicts)
|
||||
assertEquals(listOf("slot0.save"), CloudSaves.conflicts(app, Store.UBISOFT, game, a))
|
||||
assertTrue(CloudSaves.conflicts(app, Store.UBISOFT, game, b).isEmpty())
|
||||
CloudSaves.downWith(app, Store.UBISOFT, game, b, cloudB, force = true)
|
||||
assertEquals("b local", File(b, "slot0.save").readText())
|
||||
com.droiddeck.launcher.stores.epic.EpicPrefs.get(app).edit().putString("cloud_conflicts_$game", "slot0.save").apply()
|
||||
assertTrue(CloudSaves.conflicts(app, Store.UBISOFT, game, ubisoftDir("uC", root)).isEmpty())
|
||||
assertTrue(CloudSaves.conflicts(app, Store.UBISOFT, game).isEmpty())
|
||||
}
|
||||
|
||||
@Test fun aUbisoftGameAddedFromAnotherFolderIsFoundForCloudSaves() {
|
||||
val game = id
|
||||
val folder = File(app.filesDir, "Elsewhere/Anno-$game").apply { mkdirs() }
|
||||
File(folder, "Anno.exe").writeText("MZ")
|
||||
StoreGameSidecar(Store.UBISOFT, game, "Anno", "Anno.exe", extra = mapOf("productId" to "77")).write(folder)
|
||||
com.droiddeck.launcher.session.SessionPrefs.followStoreGame(app, "ubisoft:$game", folder.path) { true }
|
||||
assertEquals("not-signed-in", CloudSaves.status(app, Store.UBISOFT, game).reason)
|
||||
}
|
||||
|
||||
@Test fun aDirtyMarkStaysUntilAnUploadGoesThroughOrIsSkippedForGood() {
|
||||
val gameId = "1207664643"
|
||||
val folder = File(StoreInstallRoot.storeDir(StoreInstallRoot.internalRoot(app), Store.GOG), "ELDERBORN-mark").apply { mkdirs() }
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.droiddeck.launcher.stores.ea
|
||||
|
||||
import com.droiddeck.launcher.stores.CatalogItem
|
||||
import com.droiddeck.launcher.stores.CloudSaves
|
||||
import com.droiddeck.launcher.stores.Store
|
||||
import com.droiddeck.launcher.stores.StoreGameSidecar
|
||||
import com.droiddeck.launcher.stores.StoreLaunch
|
||||
@@ -413,4 +414,190 @@ class EaTest {
|
||||
assertTrue(!EaAuth.storeRefreshed(app, "late", 3600, "r3"))
|
||||
assertNull(EaAuth.load(app))
|
||||
}
|
||||
|
||||
private fun tokenServer(status: String, error: String = "invalid_grant"): java.net.ServerSocket {
|
||||
val server = java.net.ServerSocket(0)
|
||||
Thread {
|
||||
runCatching {
|
||||
server.accept().use { socket ->
|
||||
val input = socket.getInputStream()
|
||||
val head = StringBuilder()
|
||||
while (!head.endsWith("\r\n\r\n")) head.append(input.read().takeIf { it >= 0 }?.toChar() ?: break)
|
||||
val length = Regex("(?i)content-length: *(\\d+)").find(head)?.groupValues?.get(1)?.toInt() ?: 0
|
||||
repeat(length) { input.read() }
|
||||
val body = """{"error":"$error"}""".toByteArray()
|
||||
socket.getOutputStream().apply {
|
||||
write("HTTP/1.1 $status\r\nContent-Type: application/json\r\nContent-Length: ${body.size}\r\nConnection: close\r\n\r\n".toByteArray())
|
||||
write(body)
|
||||
flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
}.apply { isDaemon = true; start() }
|
||||
return server
|
||||
}
|
||||
|
||||
@Test fun aRejectedRefreshAsksForSignInWhileAnUnreachableOneKeepsTheSessionForARetry() {
|
||||
val app = org.robolectric.RuntimeEnvironment.getApplication()
|
||||
val real = EaAuth.tokenUrl
|
||||
try {
|
||||
com.droiddeck.launcher.stores.StoreAccounts.write(app, Store.EA,
|
||||
JSONObject().put("access_token", "old").put("refresh_token", "r").put("expires_at", 1L).put("user_id", "1"))
|
||||
EaAuth.tokenUrl = "http://127.0.0.1:1/"
|
||||
assertNull(EaAuth.accessToken(app))
|
||||
assertTrue(CloudSaves.status(app, Store.EA, "x").signedIn)
|
||||
tokenServer("503 Service Unavailable", "temporarily_unavailable").use { server ->
|
||||
EaAuth.tokenUrl = "http://127.0.0.1:${server.localPort}/"
|
||||
assertNull(EaAuth.accessToken(app))
|
||||
}
|
||||
assertTrue(CloudSaves.status(app, Store.EA, "x").signedIn)
|
||||
tokenServer("400 Bad Request", "invalid_client").use { server ->
|
||||
EaAuth.tokenUrl = "http://127.0.0.1:${server.localPort}/"
|
||||
assertNull(EaAuth.accessToken(app))
|
||||
}
|
||||
assertTrue(CloudSaves.status(app, Store.EA, "x").signedIn)
|
||||
tokenServer("400 Bad Request").use { server ->
|
||||
EaAuth.tokenUrl = "http://127.0.0.1:${server.localPort}/"
|
||||
assertNull(EaAuth.accessToken(app))
|
||||
}
|
||||
assertFalse(CloudSaves.status(app, Store.EA, "x").signedIn)
|
||||
assertTrue(EaAuth.storeRefreshed(app, "new", 3600, "r2"))
|
||||
assertTrue(CloudSaves.status(app, Store.EA, "x").signedIn)
|
||||
} finally {
|
||||
EaAuth.tokenUrl = real
|
||||
EaAuth.signOut(app)
|
||||
}
|
||||
}
|
||||
|
||||
private val heat = """<saveFileCriteria>
|
||||
<include order="0">%Documents%/Need for Speed Heat/SaveGame/savegame/</include>
|
||||
<include order="1">%Documents%/Need for Speed Heat/SaveGame/wraps/</include>
|
||||
<include order="2">%Documents%/Need for Speed Heat/settings/PROFILEOPTIONS</include>
|
||||
</saveFileCriteria>"""
|
||||
|
||||
@Test fun rulesFindTheFolderTheyShareAndPickOnlyTheirFiles() {
|
||||
val rules = EaSaveRules.parse(heat)!!
|
||||
assertEquals("%Documents%/Need for Speed Heat/", rules.base())
|
||||
assertEquals("%DOCUMENTS%/Need for Speed Heat/", EaSaveRules.template(rules.base()!!))
|
||||
assertTrue(rules.matches("%Documents%/Need for Speed Heat/SaveGame/savegame/1"))
|
||||
assertTrue(rules.matches("%documents%\\need for speed heat\\settings\\PROFILEOPTIONS"))
|
||||
assertFalse(rules.matches("%Documents%/Need for Speed Heat/settings/PROFILEOPTIONS.bak"))
|
||||
assertFalse(rules.matches("%Documents%/Need for Speed Heat/Logs/crash.txt"))
|
||||
}
|
||||
|
||||
@Test fun globsStayInsideTheirFolderAndExcludesWin() {
|
||||
val bf = EaSaveRules.parse("""<saveFileCriteria><include order="0">%Documents%/Battlefield 6/settings/*.xml</include>
|
||||
<include order="2">%Documents%/Battlefield 6/settings/*.sav</include></saveFileCriteria>""")!!
|
||||
assertEquals("%Documents%/Battlefield 6/settings/", bf.base())
|
||||
assertTrue(bf.matches("%Documents%/Battlefield 6/settings/a.sav"))
|
||||
assertFalse(bf.matches("%Documents%/Battlefield 6/settings/sub/a.sav"))
|
||||
val le = EaSaveRules.parse("""<saveFileCriteria>
|
||||
<exclude order="0">%Documents%/BioWare/Mass Effect Legendary Edition/Config/</exclude>
|
||||
<include order="1">%Documents%/BioWare/Mass Effect Legendary Edition/</include></saveFileCriteria>""")!!
|
||||
assertTrue(le.matches("%Documents%/BioWare/Mass Effect Legendary Edition/Save/ME1/x.pcsav"))
|
||||
assertFalse(le.matches("%Documents%/BioWare/Mass Effect Legendary Edition/Config/GamerSettings.ini"))
|
||||
assertNull(EaSaveRules.parse("<saveFileCriteria></saveFileCriteria>"))
|
||||
assertNull(EaSaveRules.parse("""<saveFileCriteria><include order="0">C:/Games/x/</include></saveFileCriteria>""")!!.base())
|
||||
}
|
||||
|
||||
@Test fun rulesApplyInTheirOrderAndTheFirstMatchDecides() {
|
||||
val rules = EaSaveRules.parse("""<saveFileCriteria>
|
||||
<exclude order="1">%Documents%/Game/Config/</exclude>
|
||||
<include order="0">%Documents%/Game/Config/keys.ini</include>
|
||||
<include order="2">%Documents%/Game/</include></saveFileCriteria>""")!!
|
||||
assertTrue(rules.matches("%Documents%/Game/Config/keys.ini"))
|
||||
assertFalse(rules.matches("%Documents%/Game/Config/video.ini"))
|
||||
assertTrue(rules.matches("%Documents%/Game/Save/1.sav"))
|
||||
}
|
||||
|
||||
@Test fun aBarePatternMatchesFileNamesInsideTheSaveFolder() {
|
||||
val dig = EaSaveRules.parse("""<saveFileCriteria>
|
||||
<exclude order="0">%Documents%/My Games/SteamWorld Dig/controls.cfg</exclude>
|
||||
<exclude order="2">%Documents%/My Games/SteamWorld Dig/settings.xml</exclude>
|
||||
<include order="3">*.xml</include>
|
||||
<include order="4">%Documents%/My Games/SteamWorld Dig/profile.dat</include></saveFileCriteria>""")!!
|
||||
assertEquals("%Documents%/My Games/SteamWorld Dig/", dig.base())
|
||||
assertTrue(dig.matches("%Documents%/My Games/SteamWorld Dig/profile1.xml"))
|
||||
assertFalse(dig.matches("%Documents%/My Games/SteamWorld Dig/settings.xml"))
|
||||
assertFalse(dig.matches("%Documents%/Other Game/profile1.xml"))
|
||||
val witness = EaSaveRules.parse("""<saveFileCriteria><include order="0">%AppData%/The Witness/*</include><include order="1">*.png</include></saveFileCriteria>""")!!
|
||||
assertEquals("%AppData%/The Witness/", witness.base())
|
||||
assertTrue(witness.matches("%AppData%/The Witness/shot.png"))
|
||||
}
|
||||
|
||||
@Test fun localLowAndInstallFoldersResolveToTheirTemplates() {
|
||||
val fe = EaSaveRules.parse("""<saveFileCriteria><exclude order="0">%LocalLowAppData%/Zoink Games/Fe/SystemSettings.sav</exclude>
|
||||
<include order="1">%LocalLowAppData%/Zoink Games/Fe/Save1.sav</include></saveFileCriteria>""")!!
|
||||
assertEquals("%APPLICATION_DATA_LOCAL_LOW%/Zoink Games/Fe/", EaSaveRules.template(fe.base()!!))
|
||||
val castle = EaSaveRules.parse("""<saveFileCriteria><include order="0">%InstallDir%/x86/Lost_Castle_Data/lc_game_data</include>
|
||||
<include order="1">%InstallDir%/x64/Lost_Castle_Data/lc_game_data</include></saveFileCriteria>""")!!
|
||||
assertEquals("%INSTALL%/", EaSaveRules.template(castle.base()!!))
|
||||
assertTrue(castle.matches("%InstallDir%/x64/Lost_Castle_Data/lc_game_data"))
|
||||
assertFalse(castle.matches("%InstallDir%/x64/Lost_Castle_Data/other"))
|
||||
}
|
||||
|
||||
@Test fun aManifestWithSingleQuotedAttributesKeepsEveryField() {
|
||||
val m = EaCloud.parse("""<manifest xmlns='http://origin.com/cloudsaves/manifest' lineage='{1}'>
|
||||
<file size='12' href='content/12-ab' md5='q+w=='><localName created='2022-10-17T23:51:04ZZ' lastModified='2022-10-18T00:00:00ZZ'>%Documents%/A & B/1</localName></file>
|
||||
<snapshot>s1</snapshot></manifest>""")
|
||||
val f = m.files.single()
|
||||
assertEquals(12L, f.size); assertEquals("content/12-ab", f.href); assertEquals("q+w==", f.md5)
|
||||
assertEquals("2022-10-17T23:51:04ZZ", f.created); assertEquals("2022-10-18T00:00:00ZZ", f.modified)
|
||||
assertEquals("%Documents%/A & B/1", f.localName)
|
||||
assertEquals("{1}", m.lineage); assertEquals(listOf("s1"), m.snapshots)
|
||||
assertTrue(runCatching { EaCloud.parse("<manifest><file size='1'>") }.isFailure)
|
||||
}
|
||||
|
||||
@Test fun aManifestKeepsItsFilesSnapshotsAndEaTimestampsThroughAWrite() {
|
||||
val xml = """<manifest xmlns="http://origin.com/cloudsaves/manifest" lineage="{00000000-0000-0000-0000-000000000000}">
|
||||
<file size="115352" href="content/115352-c7996be80a6e38d1f8ef28866b114f8b" md5="x5lr6ApuONH47yiGaxFPiw==">
|
||||
<localName created="2022-10-17T23:51:04ZZ" lastModified="2022-10-17T23:51:04ZZ">%Documents%/Need for Speed Heat/SaveGame/savegame/1</localName>
|
||||
</file>
|
||||
<snapshot>80cmnqT/rIKQRmciINSVhQ==</snapshot>
|
||||
</manifest>"""
|
||||
val m = EaCloud.parse(xml)
|
||||
assertEquals(1, m.files.size)
|
||||
assertEquals("c7996be80a6e38d1f8ef28866b114f8b", EaCloud.hex(m.files[0].md5))
|
||||
assertEquals(1666050664000L, EaCloud.parseTime(m.files[0].modified))
|
||||
assertEquals("2022-10-17T23:51:04ZZ", EaCloud.stamp(1666050664123L))
|
||||
val again = EaCloud.parse(EaCloud.write(m))
|
||||
assertEquals(m.files.single().localName, again.files.single().localName)
|
||||
assertEquals(m.files.single().href, again.files.single().href)
|
||||
assertEquals(m.files.single().created, again.files.single().created)
|
||||
assertEquals(m.files.single().modified, again.files.single().modified)
|
||||
assertEquals(m.files.single().size, again.files.single().size)
|
||||
assertEquals(m.files.single().md5, again.files.single().md5)
|
||||
assertEquals(listOf("80cmnqT/rIKQRmciINSVhQ=="), again.snapshots)
|
||||
assertEquals(m.lineage, again.lineage)
|
||||
}
|
||||
|
||||
@Test fun commentedRulesAreIgnoredAndNamesKeepTheirSpaces() {
|
||||
val rules = EaSaveRules.parse("""<saveFileCriteria>
|
||||
<!-- <exclude order="0">%Documents%/Game/Config/</exclude> -->
|
||||
<include order="1"> %Documents%/Game/ </include></saveFileCriteria>""")!!
|
||||
assertEquals(listOf("%Documents%/Game/"), rules.includes)
|
||||
assertTrue(rules.excludes.isEmpty())
|
||||
assertTrue(rules.matches("%Documents%/Game/Config/a.ini"))
|
||||
assertEquals("%Documents%/Game/slot.sav ", EaSaveRules.normalize("%Documents%\\Game\\slot.sav "))
|
||||
}
|
||||
|
||||
@Test fun aTruncatedManifestIsRefused() {
|
||||
val cut = runCatching { EaCloud.parse("""<manifest lineage="{1}"><file size="1" href="content/1-a" md5="x5lr6ApuONH47yiGaxFPiw==">""") }
|
||||
assertTrue(cut.isFailure)
|
||||
val open = runCatching { EaCloud.parse("""<manifest lineage="{1}"><file size="1" href="content/1-a" md5="x5lr6ApuONH47yiGaxFPiw=="><localName>%Documents%/a</localName></manifest>""") }
|
||||
assertTrue(open.isFailure)
|
||||
}
|
||||
|
||||
@Test fun snapshotEntitiesAreDecodedOnceAndWrittenBackTheSame() {
|
||||
val m = EaCloud.parse("""<manifest lineage="{1}"><snapshot>abc+def</snapshot></manifest>""")
|
||||
assertEquals(listOf("abc+def"), m.snapshots)
|
||||
assertEquals(listOf("abc+def"), EaCloud.parse(EaCloud.write(m)).snapshots)
|
||||
}
|
||||
|
||||
@Test fun cloudKeysFollowTheMultiplayerId() {
|
||||
assertEquals(listOf("195133_195133"), EaCloud.keys("195133", "195133"))
|
||||
assertEquals(listOf("52209", "52209_52209"), EaCloud.keys("52209", ""))
|
||||
assertEquals("https://x/y?a=1&b=2", EaCloud.manifestUrl("<sync><manifest>https://x/y?a=1&b=2</manifest></sync>"))
|
||||
assertTrue(CloudSaves.supports(Store.EA))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.droiddeck.launcher.stores.ubisoft
|
||||
|
||||
import com.droiddeck.launcher.stores.CloudSaves
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import java.time.Instant
|
||||
|
||||
internal class MemorySlots : UbisoftSlots {
|
||||
private var record = JSONObject().put("entityId", "e1").put("name", UbisoftCloud.entityName(77L)).put("revision", 1)
|
||||
.put("obj", JSONObject().put("slots", JSONArray()))
|
||||
private var clock = 1_700_000_000_000L
|
||||
val data = HashMap<String, ByteArray>()
|
||||
var uploadsLeft = Int.MAX_VALUE
|
||||
var afterUpdate: (JSONArray) -> Unit = {}
|
||||
|
||||
override fun entity() = UbisoftCloud.Entity(JSONObject(record.toString()))
|
||||
|
||||
override fun upload(slot: String, data: ByteArray) {
|
||||
if (uploadsLeft-- <= 0) throw java.io.IOException("upload failed")
|
||||
this.data[slot] = data
|
||||
}
|
||||
|
||||
override fun update(entity: UbisoftCloud.Entity, slots: JSONArray) {
|
||||
afterUpdate(slots)
|
||||
record = JSONObject(record.toString()).put("revision", record.getInt("revision") + 1).put("obj", JSONObject().put("slots", slots))
|
||||
}
|
||||
|
||||
override fun download(slot: String) = data[slot]
|
||||
|
||||
fun store(slot: String, bytes: ByteArray) {
|
||||
data[slot] = bytes
|
||||
val slots = JSONArray(record.getJSONObject("obj").getJSONArray("slots").toString())
|
||||
val entry = (0 until slots.length()).map { slots.getJSONObject(it) }.firstOrNull { it.optString("filename") == slot }
|
||||
?: JSONObject().put("filename", slot).put("id", slot.toInt()).also { slots.put(it) }
|
||||
clock += 60_000L
|
||||
entry.put("hash", UbisoftCloud.mark(bytes)).put("size", bytes.size).put("lastModified", Instant.ofEpochMilli(clock).toString())
|
||||
record = JSONObject(record.toString()).put("revision", record.getInt("revision") + 1).put("obj", JSONObject().put("slots", slots))
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun save(tag: String): ByteArray = ("UBISAVE0" + CloudSaves.md5(tag.toByteArray()) + tag).toByteArray()
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import com.droiddeck.launcher.stores.Store
|
||||
import com.droiddeck.launcher.stores.StoreGameSidecar
|
||||
import com.droiddeck.launcher.stores.StoreLaunch
|
||||
import org.json.JSONArray
|
||||
import java.io.File
|
||||
import org.json.JSONObject
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
@@ -98,4 +99,74 @@ class UbisoftTest {
|
||||
assertArrayEquals(bytes, UbisoftActivation.unhex("00017fff802a"))
|
||||
assertNull(UbisoftActivation.unhex("abc"))
|
||||
}
|
||||
|
||||
@Test fun aSaveNamesItsUbisoftIdentifierInItsHeader() {
|
||||
val mark = "4ef5bf634b40daa577aaeaa52c2283a9"
|
||||
val save = byteArrayOf(0x24, 2, 0, 0, 0, 0, 0, 0) + mark.toByteArray() + ByteArray(64) { it.toByte() }
|
||||
assertEquals(mark, UbisoftCloud.mark(save))
|
||||
assertNull(UbisoftCloud.mark(ByteArray(8) + ByteArray(32) + ByteArray(16)))
|
||||
assertNull(UbisoftCloud.mark(ByteArray(39)))
|
||||
}
|
||||
|
||||
@Test fun cloudSaveBodiesSurviveTheZlibHop() {
|
||||
val save = ByteArray(200_000) { (it % 251).toByte() }
|
||||
assertArrayEquals(save, UbisoftCloud.inflate(UbisoftCloud.deflate(save)))
|
||||
assertEquals(0x78, UbisoftCloud.deflate(save)[0].toInt())
|
||||
val packed = UbisoftCloud.deflate(save)
|
||||
val cut = runCatching { UbisoftCloud.inflate(packed.copyOf(packed.size / 2)) }
|
||||
assertTrue(cut.isFailure)
|
||||
}
|
||||
|
||||
@Test fun aSlotVersionChangesWithItsSizeTimeOrIdentifier() {
|
||||
val a = UbisoftCloud.Slot(1, "1", "4ef5bf634b40daa577aaeaa52c2283a9", 2414, 1_689_224_156_000)
|
||||
assertEquals("2414:1689224156000:4ef5bf634b40daa577aaeaa52c2283a9", UbisoftCloud.version(a))
|
||||
assertTrue(UbisoftCloud.version(a) != UbisoftCloud.version(UbisoftCloud.Slot(1, "1", a.hash, 2414, a.modifiedMs + 1)))
|
||||
}
|
||||
|
||||
@Test fun aSlotCommitCountsOnlyWhenNoOtherSlotMovedAndOursLanded() {
|
||||
fun entity(revision: Int, vararg slots: Triple<String, String, Long>) = UbisoftCloud.Entity(JSONObject().put("revision", revision)
|
||||
.put("obj", JSONObject().put("slots", JSONArray().apply {
|
||||
for ((name, hash, size) in slots) put(JSONObject().put("filename", name).put("hash", hash).put("size", size).put("lastModified", "2026-01-01T00:00:00Z"))
|
||||
})))
|
||||
val a = "4ef5bf634b40daa577aaeaa52c2283a9"; val b = "0123456789abcdef0123456789abcdef"; val c = "fedcba9876543210fedcba9876543210"
|
||||
val before = entity(4, Triple("1", a, 10), Triple("2", b, 20))
|
||||
assertEquals(c, UbisoftCloudTransport.committedSlot(before, entity(5, Triple("1", c, 11), Triple("2", b, 20)), "1", c, 11)?.hash)
|
||||
assertNull(UbisoftCloudTransport.committedSlot(before, entity(6, Triple("1", c, 11), Triple("2", a, 21)), "1", c, 11))
|
||||
assertNull(UbisoftCloudTransport.committedSlot(before, entity(6, Triple("1", b, 30), Triple("2", b, 20)), "1", c, 11))
|
||||
assertNull(UbisoftCloudTransport.committedSlot(before, entity(6, Triple("1", c, 11)), "1", c, 11))
|
||||
}
|
||||
|
||||
@Test fun aSlotTheCloudRewritesBeforeItIsVerifiedStaysUnsent() {
|
||||
val slots = MemorySlots().apply { store("1", MemorySlots.save("a1")) }
|
||||
val transport = UbisoftCloudTransport(slots, "u1")
|
||||
transport.list()
|
||||
slots.afterUpdate = { list -> list.getJSONObject(0).put("hash", UbisoftCloud.mark(MemorySlots.save("other device"))) }
|
||||
val changed = runCatching { transport.put(mapOf("1.save" to MemorySlots.save("a2"))) }.exceptionOrNull() as com.droiddeck.launcher.stores.CloudChanged
|
||||
assertTrue(changed.sent.isEmpty())
|
||||
assertEquals(listOf("1.save"), changed.unsent)
|
||||
assertNull(transport.committed("1.save"))
|
||||
}
|
||||
|
||||
@Test fun ubisoftCloudSavesAreSupported() {
|
||||
assertTrue(com.droiddeck.launcher.stores.CloudSaves.supports(Store.UBISOFT))
|
||||
assertTrue(!com.droiddeck.launcher.stores.CloudSaves.supports(Store.AMAZON))
|
||||
}
|
||||
|
||||
@Test fun keptGameTokensBelongToOneAccountAndProductUntilDropped() {
|
||||
val root = kotlin.io.path.createTempDirectory().toFile()
|
||||
val dir = File(root, "cache")
|
||||
val cache = UbisoftActivation.TokenCache(dir)
|
||||
assertNull(cache.get("u-1", 109))
|
||||
assertTrue(cache.put("u-1", 109, byteArrayOf(1, 2, 3)))
|
||||
assertArrayEquals(byteArrayOf(1, 2, 3), cache.get("u-1", 109))
|
||||
assertNull(cache.get("u-1", 82))
|
||||
assertNull(cache.get("u-2", 109))
|
||||
assertTrue(cache.put("../u-1", 82, byteArrayOf(4)))
|
||||
assertEquals(listOf("cache"), root.list()!!.toList())
|
||||
assertTrue(dir.listFiles()!!.all { it.parentFile == dir })
|
||||
assertTrue(cache.drop("u-1", 109))
|
||||
assertNull(cache.get("u-1", 109))
|
||||
assertTrue(cache.drop("u-1", 109))
|
||||
root.deleteRecursively()
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.droiddeck.launcher.ui
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.stores.CloudSaves
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
@@ -25,6 +26,15 @@ class CloudMessageTest {
|
||||
assertEquals(ctx.getString(R.string.cloud_not_synced), msg("failed", 0, 0, "SocketTimeoutException"))
|
||||
}
|
||||
|
||||
@Test fun aPartialSyncNamesTheSlotsItLeft() {
|
||||
fun partial(conflicts: Int, detail: String) = cloudMessage(ctx, CloudSaves.Result("partial", 0, 0L, "blocked-path", conflicts, detail))
|
||||
assertEquals(ctx.getString(R.string.cloud_partial, "outside the save folder: ../x.save"), partial(0, "outside the save folder: ../x.save"))
|
||||
assertEquals(ctx.getString(R.string.cloud_conflict_files_detail, 2, "changed locally: 1.save, 2.save"), partial(2, "changed locally: 1.save, 2.save"))
|
||||
assertTrue(partial(1, "changed locally: 1.save").contains("1.save"))
|
||||
assertEquals(ctx.getString(R.string.cloud_conflict_files, 1), partial(1, ""))
|
||||
assertEquals(ctx.getString(R.string.cloud_not_synced), partial(0, ""))
|
||||
}
|
||||
|
||||
@Test fun okResultsSayWhatHappened() {
|
||||
assertEquals(ctx.getString(R.string.cloud_up_to_date), msg("ok", 0, 0, "up-to-date"))
|
||||
assertEquals(ctx.getString(R.string.cloud_done, 3), msg("ok", 3, 0, "uploaded"))
|
||||
|
||||
Reference in new issue
Block a user