diff --git a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt index 284970c9c6..a8f165bc42 100644 --- a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt @@ -1513,7 +1513,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet onComponents = { focusContent -> openComponents(focusContent) }, onGameComponents = { game -> openComponents(gameKey = game.profileKey) }, onComponentsTab = { tab -> openComponents(focusContent = true, tab = tab) }, - onSaveImport = { sg -> importSaves(sg.name) { GameSaves.game(sg) } }, + onSaveImport = { sg -> importSaves(sg.name) { GameSaves.game(this@FrontEndActivity, sg) } }, onAddonImport = { sg, target -> importAddons(sg.name, target) }, onAddonDelete = { target, name -> saveAction(getString(R.string.main_addons_deleting, name)) { @@ -1521,7 +1521,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet getString(R.string.main_addons_deleted, name) } }, - onSaveExport = { sg, layout -> exportSaves(sg.name, layout) { GameSaves.game(sg) } }, + onSaveExport = { sg, layout -> exportSaves(sg.name, layout) { GameSaves.game(this@FrontEndActivity, sg) } }, onPerformance = { refreshCores(); showComponents = false; showMapping = false; showPerformance = true }, performancePanel = { PerformanceHost(embedded = true) }, onRoms = { showRoms = true }, diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt b/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt index 7cc8051774..26c9c99813 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt @@ -293,6 +293,7 @@ object Library { g.steamAppId ?: g.appId.toInt(), g.name, art.portrait ?: art.header, ADDED, g.gameId, hero = art.hero ?: art.header, lastPlayed = maxOf(lastPlayed, recordedPlay(context, g.gameId)), gameFiles = g.folder, protonPrefix = if (g.source == com.droiddeck.launcher.stores.Store.BATTLENET.id) File(com.droiddeck.launcher.runtime.BattleNet.prefix(context), "pfx").takeIf { it.isDirectory } + else if (g.source == com.droiddeck.launcher.stores.Store.UBISOFT.id) File(com.droiddeck.launcher.stores.ubisoft.UbisoftConnect.prefix(context), "pfx").takeIf { it.isDirectory } else protonPrefix(context, g.steamAppId?.toLong() ?: g.appId, libraries = roots), icon = art.icon?.takeIf { it.extension.lowercase() != "ico" }, source = g.source, storeId = g.storeId, diff --git a/app/src/main/java/com/droiddeck/launcher/session/GameSaves.kt b/app/src/main/java/com/droiddeck/launcher/session/GameSaves.kt index 3a31da0113..a1491ff85a 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/GameSaves.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/GameSaves.kt @@ -25,7 +25,7 @@ object GameSaves { fun label(context: Context): String = context.getString(R.string.saves_layout_zip, app) } - data class Game(val game: Library.SteamGame, val prefix: File?, val saves: List) { + data class Game(val game: Library.SteamGame, val prefix: File?, val saves: List, val root: String? = null) { val launched get() = prefix?.let { File(it, "drive_c").isDirectory } == true } @@ -36,7 +36,28 @@ object GameSaves { fun savesDir(): File = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "DroidDeck/Saves") private fun backupsDir(): File = File(savesDir(), "backups") - fun game(game: Library.SteamGame): Game = Game(game, game.protonPrefix, game.protonPrefix?.let { locate(it, game) } ?: emptyList()) + fun game(context: Context, game: Library.SteamGame): Game { + val root = if (ubisoft(game)) ubisoftRoot(context, game) else null + val saves = game.protonPrefix?.let { prefix -> if (ubisoft(game)) root?.let { ubisoftSaves(prefix, it) }.orEmpty() else locate(prefix, game) }.orEmpty() + return Game(game, game.protonPrefix, saves, root) + } + + fun saves(context: Context, prefix: File, game: Library.SteamGame): List = + if (ubisoft(game)) ubisoftRoot(context, game)?.let { ubisoftSaves(prefix, it) }.orEmpty() else locate(prefix, game) + + private fun ubisoftRoot(context: Context, game: Library.SteamGame): String? { + val product = game.gameFiles?.let { com.droiddeck.launcher.stores.StoreGameSidecar.read(it) }?.extra?.get("productId")?.toLongOrNull() ?: return null + val user = com.droiddeck.launcher.stores.ubisoft.UbisoftAuth.load(context)?.userId + if (user.isNullOrEmpty() || user.contains('/') || user.contains("..")) return null + return "Saved Games/Ubisoft/$user/$product" + } + + private fun ubisoft(game: Library.SteamGame) = game.source == com.droiddeck.launcher.stores.Store.UBISOFT.id + + private fun ubisoftSaves(prefix: File, rel: String): List { + val files = File(profile(prefix), rel).walkTopDown().filter { it.isFile }.toList() + return if (files.isEmpty()) emptyList() else listOf(SaveDir(rel, files.size, files.sumOf { it.length() })) + } private val SAVE_ROOTS = listOf("Documents/My Games", "Saved Games", "AppData/Roaming", "Documents", "AppData/Local", "AppData/LocalLow") @@ -109,9 +130,11 @@ object GameSaves { fun export(context: Context, game: Game, layout: Layout, dir: File): Pair { val prefix = game.prefix ?: error(context.getString(R.string.saves_not_launched, game.game.name)) + val roots = game.saves.map { it.relPath } + if (roots.isEmpty() && ubisoft(game.game)) error(context.getString(R.string.saves_none_found, game.game.name)) dir.mkdirs() val out = File(dir, "${safe(game.game.name)}_${stamp()}.zip") - val count = zipProfile(profile(prefix), game.saves.map { it.relPath }.ifEmpty { null }, layout, out) + val count = zipProfile(profile(prefix), roots.ifEmpty { null }, layout, out) if (count == 0) { out.delete(); error(context.getString(R.string.saves_none_found, game.game.name)) } return out to count } @@ -142,12 +165,21 @@ object GameSaves { if (!driveC.isDirectory) error(context.getString(R.string.saves_not_launched, game.game.name)) if (!looksLikeSaveZip(zip)) error(context.getString(R.string.saves_not_save_zip, zip.name)) + val scoped = if (ubisoft(game.game)) game.root ?: error(context.getString(R.string.saves_none_found, game.game.name)) else null + if (scoped != null) { + val base = File(profile(prefix), scoped).canonicalFile + val eligible = ZipInputStream(BufferedInputStream(zip.inputStream())).use { zis -> + generateSequence { zis.nextEntry }.any { e -> !e.isDirectory && remap(e.name)?.let { File(driveC, it).canonicalFile.path.startsWith(base.path + File.separator) } == true } + } + if (!eligible) error(context.getString(R.string.saves_not_save_zip, zip.name)) + } backupsDir().mkdirs() - val backup = File(backupsDir(), "${safe(game.game.name)}_${stamp()}.zip") - val backedUp = zipProfile(profile(prefix), null, Layout.GAMEHUB, backup) + val backup = File(backupsDir(), "${safe(game.game.name)}_${stamp()}_${System.currentTimeMillis()}.zip") + val backedUp = if (scoped != null && File(profile(prefix), scoped).walkTopDown().none { it.isFile }) 0 + else zipProfile(profile(prefix), scoped?.let(::listOf), Layout.GAMEHUB, backup) if (backedUp == 0) backup.delete() - val base = driveC.canonicalFile + val base = (if (scoped != null) File(profile(prefix), scoped) else driveC).canonicalFile var written = 0 ZipInputStream(BufferedInputStream(zip.inputStream())).use { zis -> var e = zis.nextEntry diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt index d0da893c7d..ed23e31bc3 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -1484,6 +1484,8 @@ object SessionPrefs { (id and 0xFFFFFFFFL) to key.removePrefix("addedAppId:") }.toMap() + fun storeGameFolder(context: Context, key: String): String? = prefs(context).getString("storeFolder:$key", null) + fun followStoreGame(context: Context, key: String, folderPath: String, gone: (String) -> Boolean) = synchronized(addedGameLock) { val p = prefs(context) val previous = p.getString("storeFolder:$key", 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 45b1be5429..c2fd9890d5 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt @@ -5,11 +5,20 @@ import android.content.SharedPreferences import android.util.Log import com.droiddeck.launcher.frontend.AddedGames import com.droiddeck.launcher.frontend.Library +import com.droiddeck.launcher.stores.ea.EaAuth +import com.droiddeck.launcher.stores.ea.EaCloud +import com.droiddeck.launcher.stores.ea.EaLibrary +import com.droiddeck.launcher.stores.ea.EaPrefs +import com.droiddeck.launcher.stores.ea.EaSaveRules import com.droiddeck.launcher.stores.epic.EpicApiClient import com.droiddeck.launcher.stores.epic.EpicCredentialStore import com.droiddeck.launcher.stores.epic.EpicPrefs import com.droiddeck.launcher.stores.gog.GogAuth import com.droiddeck.launcher.stores.gog.GogPrefs +import com.droiddeck.launcher.stores.ubisoft.UbisoftAuth +import com.droiddeck.launcher.stores.ubisoft.UbisoftCloud +import com.droiddeck.launcher.stores.ubisoft.UbisoftCloudTransport +import com.droiddeck.launcher.stores.ubisoft.UbisoftConnect import java.io.File import java.net.HttpURLConnection import java.net.URL @@ -32,19 +41,27 @@ object CloudSaves { if (detail.isNotEmpty()) " detail=\"$detail\"" else "" } - class Status(val supported: Boolean, val folder: File?, val lastSync: Long, val reason: String, val conflicts: List = emptyList()) + class Status(val supported: Boolean, val folder: File?, val lastSync: Long, val reason: String, val conflicts: List = emptyList(), val signedIn: Boolean = true) - private fun game(context: Context, store: Store, id: String): Pair? = - StoreInstallRoot.gameFolders(context).firstNotNullOfOrNull { f -> - StoreGameSidecar.read(f)?.takeIf { it.store == store && it.id == id && it.isInstalled }?.let { f to it } - } + private fun game(context: Context, store: Store, id: String): Pair? { + fun match(f: File) = StoreGameSidecar.read(f)?.takeIf { it.store == store && it.id == id && it.isInstalled }?.let { f to it } + return StoreInstallRoot.gameFolders(context).firstNotNullOfOrNull(::match) + ?: if (store != Store.UBISOFT) null + else com.droiddeck.launcher.session.SessionPrefs.storeGameFolder(context, "${store.id}:$id")?.let { match(File(it)) } + } + + fun supports(store: Store?): Boolean = store == Store.GOG || store == Store.EPIC || store == Store.EA || store == Store.UBISOFT fun prefix(context: Context, folder: File): File? { val appId = AddedGames.scan(context).firstOrNull { it.folder.absolutePath == folder.absolutePath }?.appId ?: return null return Library.protonPrefix(context, appId and 0xFFFFFFFFL) } - private fun prefs(context: Context, store: Store) = if (store == Store.GOG) GogPrefs.get(context) else EpicPrefs.get(context) + private fun prefs(context: Context, store: Store) = when (store) { + Store.GOG -> GogPrefs.get(context) + Store.EA -> EaPrefs.get(context) + else -> EpicPrefs.get(context) + } fun lastSync(context: Context, store: Store, id: String): Long = prefs(context, store).getLong("cloud_sync_$id", 0L) @@ -52,6 +69,7 @@ object CloudSaves { private const val TEMPLATE_NONE = "none" + private const val UBISOFT_TEMPLATE = "ubisoft" internal const val TEMPLATE_NONE_TTL_MS = 24 * 60 * 60 * 1000L internal fun noneTemplateIsCurrent(storedAtMs: Long, nowMs: Long) = @@ -64,6 +82,10 @@ object CloudSaves { fun template(context: Context, store: Store, id: String, folder: File, sidecar: StoreGameSidecar): String? { val p = prefs(context, store) + if (store == Store.EA) eaRules(context, id)?.let { fresh -> + if (fresh.isEmpty()) rememberNoCloud(p, id) else p.edit().putString("cloud_template_$id", fresh).remove("cloud_template_at_$id").apply() + return fresh + } var retryNone = false if (p.contains("cloud_template_$id")) { val cached = p.getString("cloud_template_$id", "") ?: "" @@ -89,7 +111,9 @@ object CloudSaves { val item = sidecar.extra["catalogItemId"].orEmpty() if (ns.isEmpty() || item.isEmpty()) "" else EpicApiClient.getCustomAttribute(token, ns, item, "CloudSaveFolder") } - Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET, Store.EA, Store.UBISOFT, Store.ROCKSTAR -> "" + Store.UBISOFT -> UBISOFT_TEMPLATE + Store.EA -> eaRules(context, id) ?: return null + Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET, Store.ROCKSTAR -> "" } ?: return null p.edit() .putString("cloud_template_$id", fetched.ifEmpty { TEMPLATE_NONE }) @@ -146,18 +170,25 @@ object CloudSaves { val (folder, sidecar) = game(context, store, id) ?: return null to "not-installed" val template = template(context, store, id, folder, sidecar) ?: return null to "store-unreachable" if (template.isEmpty()) return null to "no-cloud-saves" + if (store == Store.UBISOFT) { + val user = UbisoftAuth.load(context)?.userId?.takeIf { it.isNotEmpty() } ?: return null to "not-signed-in" + val product = sidecar.extra["productId"]?.toLongOrNull() ?: return null to "unresolved" + return File(UbisoftConnect.prefix(context), "pfx/drive_c/users/steamuser/Saved Games/Ubisoft/$user/$product") to "ok" + } val pfx = prefix(context, folder) ?: return null to "no-prefix" val dir = when (store) { Store.GOG -> CloudSavePaths.gog(template, pfx, folder) Store.EPIC -> CloudSavePaths.epic(template, pfx, folder, EpicCredentialStore.load(context)?.accountId.orEmpty(), id) - Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET, Store.EA, Store.UBISOFT, Store.ROCKSTAR -> null + Store.EA -> if (sidecar.extra["masterTitleId"].isNullOrEmpty()) null + else EaSaveRules.parse(template)?.base()?.let(EaSaveRules::template)?.let { CloudSavePaths.gog(it, pfx, folder) } + Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET, Store.UBISOFT, Store.ROCKSTAR -> null } return dir to (if (dir == null) "unresolved" else "ok") } fun status(context: Context, store: Store, id: String): Status { val (dir, reason) = saveFolder(context, store, id) - return Status(reason != "no-cloud-saves" && reason != "not-installed", dir, lastSync(context, store, id), reason, conflicts(context, store, id)) + return Status(reason != "no-cloud-saves" && reason != "not-installed", dir, lastSync(context, store, id), reason, conflicts(context, store, id, dir), signedIn(context, store)) } @@ -175,7 +206,33 @@ object CloudSaves { val account = EpicCredentialStore.load(context)?.accountId.orEmpty() if (token == null || account.isEmpty()) null else EpicCloud(account, id, token) } - Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET, Store.EA, Store.UBISOFT, Store.ROCKSTAR -> null + Store.UBISOFT -> { + val session = UbisoftAuth.current(context) + val product = StoreGameSidecar.read(folder)?.extra?.get("productId")?.toLongOrNull() + if (session == null || product == null) null else UbisoftCloudTransport(UbisoftCloud(session, product), session.userId) + } + Store.EA -> { + val sidecar = StoreGameSidecar.read(folder) + val rules = sidecar?.let { template(context, store, id, folder, it) }?.let(EaSaveRules::parse) + val base = rules?.base() + val token = EaAuth.accessToken(context) + val uid = EaAuth.load(context)?.userId.orEmpty() + val master = sidecar?.extra?.get("masterTitleId").orEmpty() + if (rules == null || base == null || token == null || uid.isEmpty() || master.isEmpty()) null + else EaCloud(token, uid, EaCloud.keys(master, sidecar?.extra?.get("multiplayerId").orEmpty()), base, rules, saveFolder(context, store, id).first) + } + Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET, Store.ROCKSTAR -> null + } + + private fun signedIn(context: Context, store: Store): Boolean = when (store) { + Store.EA -> EaAuth.signedIn(context) + else -> true + } + + private fun eaRules(context: Context, id: String): String? { + val games = EaLibrary.cached(context) + if (games.isEmpty()) return null + return games.firstOrNull { it.offerId == id }?.cloudSaves.orEmpty() } private fun gogScopedToken(context: Context, id: String, clientId: String): String? { @@ -184,11 +241,15 @@ object CloudSaves { } - private fun baseline(context: Context, store: Store, id: String): Map? = - CloudPlan.fromJson(prefs(context, store).getString("cloud_baseline_$id", null)) + /** Ubisoft saves belong to an account and product, so their sync state is kept per save folder. */ + internal fun scope(store: Store, id: String, dir: File): String = + if (store == Store.UBISOFT) "$id:${dir.parentFile?.name.orEmpty()}:${dir.name}" else id - private fun saveBaseline(context: Context, store: Store, id: String, b: Map) = - prefs(context, store).edit().putString("cloud_baseline_$id", CloudPlan.toJson(b)).apply() + internal fun baseline(context: Context, store: Store, key: String): Map? = + CloudPlan.fromJson(prefs(context, store).getString("cloud_baseline_$key", null)) + + private fun saveBaseline(context: Context, store: Store, key: String, b: Map) = + prefs(context, store).edit().putString("cloud_baseline_$key", CloudPlan.toJson(b)).apply() internal class Migration(val baseline: Map, val moved: Int, val cleaned: Int, val failed: Int) @@ -260,30 +321,45 @@ object CloudSaves { if (result == null || result.failed == 0) p.edit().putInt("cloud_layout_$id", CLOUD_LAYOUT).apply() } - fun conflicts(context: Context, store: Store, id: String): List = - prefs(context, store).getString("cloud_conflicts_$id", "").orEmpty().split('\n').filter { it.isNotEmpty() } + fun conflicts(context: Context, store: Store, id: String, dir: File? = null): List = + if (store == Store.UBISOFT && dir == null) emptyList() else savedConflicts(context, store, dir?.let { scope(store, id, it) } ?: id) - private fun saveConflicts(context: Context, store: Store, id: String, names: Collection) = - prefs(context, store).edit().putString("cloud_conflicts_$id", names.joinToString("\n")).apply() + private fun savedConflicts(context: Context, store: Store, key: String): List = + prefs(context, store).getString("cloud_conflicts_$key", "").orEmpty().split('\n').filter { it.isNotEmpty() } - fun download(context: Context, store: Store, id: String, force: Boolean = false): Result = - sync(context, store, id, "down") { dir, cloud -> downWith(context, store, id, dir, cloud, force) } + private fun saveConflicts(context: Context, store: Store, key: String, names: Collection) = + prefs(context, store).edit().putString("cloud_conflicts_$key", names.joinToString("\n")).apply() + + private fun alike(cloud: CloudTransport, conflicts: List, local: Map, remote: Map): List = + if (cloud.contentDigests) emptyList() + else conflicts.filter { name -> + val r = remote[name] ?: return@filter false + val l = local[name] ?: return@filter false + runCatching { md5(cloud.get(r).first) }.getOrNull()?.equals(l.md5, ignoreCase = true) == true + } + + private fun settled(plan: CloudPlan.Plan, alike: List): CloudPlan.Plan = + if (alike.isEmpty()) plan else CloudPlan.Plan(plan.transfer, plan.conflicts - alike.toSet(), plan.same + alike, plan.refused) + + fun download(context: Context, store: Store, id: String, force: Boolean = false, claim: () -> Boolean = { true }): Result = + sync(context, store, id, "down") { dir, cloud -> downWith(context, store, id, dir, cloud, force, claim) } fun upload(context: Context, store: Store, id: String, force: Boolean = false): Result = sync(context, store, id, "up") { dir, cloud -> upWith(context, store, id, dir, cloud, force) } - fun downloadOrDefer(context: Context, store: Store, id: String): Result = - downloadOrDefer(context, store, id, Executor { Thread(it, "cloud-deferred").start() }) + fun downloadOrDefer(context: Context, store: Store, id: String, claim: () -> Boolean): Result = + downloadOrDefer(context, store, id, Executor { Thread(it, "cloud-deferred").start() }, claim) - internal fun downloadOrDefer(context: Context, store: Store, id: String, executor: Executor): Result { + internal fun downloadOrDefer(context: Context, store: Store, id: String, executor: Executor, claim: () -> Boolean = { true }): Result { val app = context.applicationContext val g = game(app, store, id) ?: return done(store, id, "down", Result("skipped", 0, 0, "not-installed")) if (!g.second.cloud) return done(store, id, "down", Result("skipped", 0, 0, "off")) - if (prefix(app, g.first)?.let { CloudSavePaths.profile(it).isDirectory } == true) return download(app, store, id) + val ready = { (if (store == Store.UBISOFT) File(UbisoftConnect.prefix(app), "pfx") else prefix(app, g.first))?.let { CloudSavePaths.profile(it).isDirectory } == true } + if (ready()) return download(app, store, id, claim = claim) executor.execute { val end = System.currentTimeMillis() + PREFIX_WAIT_MS while (System.currentTimeMillis() < end) { - if (prefix(app, g.first)?.let { CloudSavePaths.profile(it).isDirectory } == true) { download(app, store, id); return@execute } + if (ready()) { download(app, store, id); return@execute } Thread.sleep(2000) } done(store, id, "down", Result("skipped", 0, 0, "no-prefix")) @@ -304,8 +380,12 @@ object CloudSaves { if (dir != null && store == Store.GOG) migrateGogNested(context, id, g.first, dir) val cloud = if (dir != null) transport(context, store, id, g.first) else null when { + cloud is UbisoftCloudTransport && dir?.parentFile?.name != cloud.user -> Result("skipped", 0, 0, "account-changed") dir == null -> Result("skipped", 0, 0, reason) + cloud == null && store == Store.EA && signedIn(context, store) -> Result("failed", 0, 0, "sign-in-unreachable") cloud == null -> Result("skipped", 0, 0, "not-signed-in") + cloud is EaCloud && cloud.localDir?.canonicalPath != dir.canonicalPath -> Result("skipped", 0, 0, "rules-changed") + cloud is EaCloud && caseCollision(localFiles(dir).keys, cloud::wants) -> Result("failed", 0, 0, "case-collision") else -> body(dir, cloud).also { if (it.result == "ok") markSynced(context, store, id) } } } @@ -319,15 +399,32 @@ object CloudSaves { return done(store, id, way, result) } + internal fun caseCollision(names: Collection, wants: (String) -> Boolean): Boolean = + names.filter(wants).groupBy { it.lowercase() }.any { it.value.size > 1 } + private fun done(store: Store, id: String, way: String, r: Result): Result { Log.i(TAG, "cloud ${store.id} $id $way ${r.line}") StoresState.logLine("cloud ${store.id} $id $way ${r.line}") return r } - private fun localFiles(dir: File): Map = - if (!dir.isDirectory) emptyMap() - else dir.walkTopDown().filter { it.isFile }.associateBy { it.relativeTo(dir).path.replace(File.separatorChar, '/') } + private fun localFiles(dir: File): Map { + if (!dir.isDirectory) return emptyMap() + val root = runCatching { dir.toPath().toRealPath() }.getOrNull() ?: return emptyMap() + val found = LinkedHashMap() + fun walk(d: File, prefix: String, ancestry: Set) { + for (child in d.listFiles().orEmpty()) { + val real = runCatching { child.toPath().toRealPath() }.getOrNull() ?: continue + if (!real.startsWith(root)) continue + val name = prefix + child.name + if (child.isDirectory) { + if (real !in ancestry) walk(child, "$name/", ancestry + real) + } else if (child.isFile) found[name] = child + } + } + walk(dir, "", setOf(root)) + return found + } internal fun md5(f: File): String = md5(f.readBytes()) @@ -341,46 +438,85 @@ object CloudSaves { return files.associateBy { it.name } } - internal fun downWith(context: Context, store: Store, id: String, dir: File, cloud: CloudTransport, force: Boolean): Result { - val files = localFiles(dir) + 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 local = files.mapValues { CloudPlan.Local(md5(it.value)) } - val base = baseline(context, store, id) - val plan = CloudPlan.down(local, remote.mapValues { CloudPlan.Remote(it.value.md5, it.value.modifiedMs) }, base) - val take = if (force) plan.transfer + plan.conflicts else plan.transfer - if (take.isNotEmpty() && files.isNotEmpty()) backup(context, store, id, dir, "local") + val key = scope(store, id, dir) + val base = baseline(context, store, key) + val planned = CloudPlan.down(local, remote.mapValues { CloudPlan.Remote(it.value.md5, it.value.modifiedMs) }, base) + val alike = alike(cloud, planned.conflicts, local, remote) + val plan = settled(planned, alike) + val saved = savedConflicts(context, store, key).filter { it in remote && it !in alike && it !in plan.same } + val take = if (force) (plan.transfer + plan.conflicts + saved).distinct() else plan.transfer val next = HashMap(base.orEmpty()) var bytes = 0L + val written = HashSet() + val changed = ArrayList() + val blocked = ArrayList() + val fetched = LinkedHashMap>>() for (name in take) { val f = remote[name] ?: continue - val (data, modified) = cloud.get(f) + if (!File(dir, name).canonicalPath.startsWith(dir.canonicalPath + File.separator)) { blocked += name; continue } + fetched[name] = f to cloud.get(f) + } + if (fetched.isNotEmpty() && !claim()) return Result("skipped", 0, 0, "launch-moved-on") + val replaced = fetched.keys.mapNotNull { name -> files[name] ?: File(dir, name).takeIf { it.isFile } } + if (!backupReplaced(context, store, id, dir, replaced)) return Result("failed", 0, 0, "backup-failed") + for ((name, got) in fetched) { + val (f, content) = got + val (data, modified) = content val dest = File(dir, name) - if (!dest.canonicalPath.startsWith(dir.canonicalPath + File.separator)) continue + if ((if (dest.isFile) md5(dest) else null) != local[name]?.md5) { changed += name; continue } dest.parentFile?.mkdirs() dest.writeBytes(data) if (modified > 0) dest.setLastModified(modified) bytes += data.size + written += name next[name] = CloudPlan.Entry(md5(data), f.md5, if (modified > 0) modified else f.modifiedMs) } for (name in plan.same) remote[name]?.let { r -> next[name] = CloudPlan.Entry(local.getValue(name).md5, r.md5, r.modifiedMs) } - saveBaseline(context, store, id, next) - val left = if (force) emptyList() else plan.conflicts - saveConflicts(context, store, id, left) - return Result("ok", take.size, bytes, if (take.isEmpty()) "up-to-date" else "downloaded", left.size) + saveBaseline(context, store, key, next) + val left = (if (force) take.filter { it !in written } else plan.conflicts + saved.filter { it !in written } + changed) + .filter { it !in blocked }.distinct() + saveConflicts(context, store, key, left) + val reason = when { + take.isEmpty() -> "up-to-date" + changed.isNotEmpty() -> "changed-locally" + blocked.isNotEmpty() -> "blocked-path" + else -> "downloaded" + } + val detail = listOfNotNull( + changed.takeIf { it.isNotEmpty() }?.joinToString(prefix = "changed locally: "), + blocked.takeIf { it.isNotEmpty() }?.joinToString(prefix = "outside the save folder: "), + ).joinToString("; ") + return Result(if (written.size < take.size) "partial" else "ok", written.size, bytes, reason, left.size, detail) } internal fun upWith(context: Context, store: Store, id: String, dir: File, cloud: CloudTransport, force: Boolean): Result { - val base = baseline(context, store, id) ?: return Result("skipped", 0, 0, "no-baseline") - val files = localFiles(dir) + val key = scope(store, id, dir) + 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 local = files.mapValues { CloudPlan.Local(md5(it.value)) } - val plan = CloudPlan.up(local, remote.mapValues { CloudPlan.Remote(it.value.md5, it.value.modifiedMs) }, base) - val send = CloudPlan.upSend(plan, force, cloud is GogCloud, files.keys) + val planned = CloudPlan.up(local, remote.mapValues { CloudPlan.Remote(it.value.md5, it.value.modifiedMs) }, base) + 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 } + var send = CloudPlan.upSend(plan, force, cloud is GogCloud, files.keys) val next = HashMap(base) for (name in plan.same) remote[name]?.let { r -> next[name] = CloudPlan.Entry(local.getValue(name).md5, r.md5, r.modifiedMs) } + var drifted = emptyList() + var raced = emptyList() + if (send.isNotEmpty()) { + val copies = backupCloud(context, store, id, cloud, send.mapNotNull { remote[it] }) + if (cloud is UbisoftCloudTransport && !force) { + drifted = send.filter { name -> val synced = base[name]?.localMd5; synced != null && copies[name]?.let { it != synced } == true } + send = send - drifted.toSet() + } + } if (send.isNotEmpty()) { - backupCloud(context, store, id, cloud, send.mapNotNull { remote[it] }) val stamped = HashMap() val sentMd5 = HashMap() if (cloud is GogCloud) { @@ -394,19 +530,40 @@ object CloudSaves { } else { val data = send.associateWith { files.getValue(it).readBytes() } for ((name, bytes) in data) sentMd5[name] = md5(bytes) - stamped += cloud.put(data, send.associateWith { files.getValue(it).lastModified() }) + stamped += try { + cloud.put(data, send.associateWith { files.getValue(it).lastModified() }) + } catch (e: CloudChanged) { + raced = e.unsent + e.sent + } catch (e: Exception) { + val kept = send.mapNotNull { name -> cloud.committed(name)?.let { name to it } } + if (kept.isNotEmpty()) { + for ((name, r) in kept) next[name] = CloudPlan.Entry(sentMd5.getValue(name), r.md5, r.modifiedMs) + saveBaseline(context, store, key, next) + saveConflicts(context, store, key, saved - kept.map { it.first }.toSet()) + } + throw e + } + send = send.filter { it in stamped } for ((name, t) in stamped) if (t > 0) files[name]?.setLastModified(t) } - val after = remote(cloud) { it in send } + val after = if (send.all { cloud.committed(it) != null }) emptyMap() else remote(cloud) { it in send && cloud.committed(it) == null } for (name in send) { - val r = after[name] + val r = cloud.committed(name) ?: after[name] next[name] = CloudPlan.Entry(sentMd5.getValue(name), r?.md5, r?.modifiedMs ?: stamped[name] ?: -1L) } } - saveBaseline(context, store, id, next) - val left = if (force) emptyList() else plan.conflicts - saveConflicts(context, store, id, left) - return Result("ok", send.size, send.sumOf { files.getValue(it).length() }, if (send.isEmpty()) "up-to-date" else "uploaded", left.size) + saveBaseline(context, store, key, next) + val left = (if (force) (plan.conflicts + saved).filter { it !in send } + raced + else plan.conflicts + drifted + raced + saved.filter { it !in send && it in files }).distinct() + saveConflicts(context, store, key, left) + val reason = when { + raced.isNotEmpty() -> "changed-in-cloud" + send.isEmpty() -> "up-to-date" + else -> "uploaded" + } + return Result(if (raced.isEmpty()) "ok" else "partial", send.size, send.sumOf { files.getValue(it).length() }, reason, left.size, + if (raced.isEmpty()) "" else raced.joinToString(prefix = "changed in the cloud: ")) } @@ -441,23 +598,59 @@ object CloudSaves { private fun backupRoot(context: Context, store: Store, id: String) = File(context.filesDir, "stores/cloud-backups/${store.id}-${id.replace(Regex("[^A-Za-z0-9._-]"), "_")}") - private fun backup(context: Context, store: Store, id: String, dir: File, kind: String) { - val root = backupRoot(context, store, id) - runCatching { dir.copyRecursively(File(root, "$kind-${System.currentTimeMillis()}"), overwrite = true) } - prune(root, kind) + private fun markUbisoftSave(file: File): Boolean { + val head = ByteArray(40) + var filled = 0 + file.inputStream().use { input -> + while (filled < head.size) { + val n = input.read(head, filled, head.size - filled) + if (n < 0) break + filled += n + } + } + if (filled < head.size) return false + if (UbisoftCloud.mark(head) != null) return true + if (head.copyOfRange(8, 40).any { it != 0.toByte() }) return false + java.io.RandomAccessFile(file, "rw").use { it.seek(8); it.write(UbisoftCloud.freshMark().toByteArray(Charsets.ISO_8859_1)) } + return true } - private fun backupCloud(context: Context, store: Store, id: String, cloud: CloudTransport, files: List) { - if (files.isEmpty()) return + private fun backupCloud(context: Context, store: Store, id: String, cloud: CloudTransport, files: List): Map { + if (files.isEmpty()) return emptyMap() val root = backupRoot(context, store, id) val target = File(root, "cloud-${System.currentTimeMillis()}") + val copies = HashMap() for (f in files) { val dest = File(target, f.name) if (!dest.canonicalPath.startsWith(target.canonicalPath + File.separator)) continue dest.parentFile?.mkdirs() - dest.writeBytes(cloud.get(f).first) + val data = cloud.get(f).first + dest.writeBytes(data) + copies[f.name] = md5(data) } prune(root, "cloud") + return copies + } + + private fun backupReplaced(context: Context, store: Store, id: String, dir: File, replaced: List): Boolean { + if (replaced.isEmpty()) return true + val base = runCatching { dir.canonicalPath + File.separator }.getOrNull() ?: return false + val root = backupRoot(context, store, id) + val stamp = "${System.currentTimeMillis()}-${java.util.UUID.randomUUID().toString().take(8)}" + val staging = File(root, "staging-$stamp") + val copied = runCatching { + for (f in replaced) { + check(f.canonicalPath.startsWith(base)) + f.copyTo(File(staging, f.relativeTo(dir).path), overwrite = true) + } + check(staging.renameTo(File(root, "local-$stamp"))) + }.isSuccess + if (!copied) { + staging.deleteRecursively() + return false + } + prune(root, "local") + return true } private fun prune(root: File, kind: String) { 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 2971bb61ca..9e0ad99196 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/CloudTransport.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/CloudTransport.kt @@ -27,6 +27,10 @@ class CloudFile(val name: String, var modifiedMs: Long, var md5: String? = null, class CloudPut(val modifiedMs: Long, val md5: String?) interface CloudTransport { + /** False when [CloudFile.md5] is a version the service assigns rather than a digest of the content. */ + val contentDigests: Boolean get() = true + fun committed(name: String): CloudFile? = null + fun wants(name: String): Boolean = true fun list(): List fun details(file: CloudFile) {} fun get(file: CloudFile): Pair @@ -182,6 +186,8 @@ internal fun isoDate(s: String?): Long { class NoCloudSaves : Exception("no cloud saves") +class CloudChanged(val sent: Map, val unsent: List) : Exception("cloud saves changed since they were read") + internal fun splitGogName(name: String): Pair { val slash = name.indexOf('/') return if (slash <= 0 || slash == name.length - 1) null to name diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt index 9ec0dad5fd..cc231dfcb8 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt @@ -46,11 +46,18 @@ object StoreLaunchRequests { } private fun answer(app: Context, file: File) { + val resp = File(dir(app), "resp").apply { mkdirs() } + val rid = file.name.removeSuffix(".json") + val taken = File(resp, "$rid.taken") + val writing = File(resp, "$rid.writing") + if (!file.renameTo(taken)) return val request = try { - JSONObject(file.readText()).also { file.delete() } + JSONObject(taken.readText()) } catch (e: Exception) { + taken.delete() return } + val claim = { taken.renameTo(writing) } val reply = when (request.optString("op")) { "xbox-released" -> { com.droiddeck.launcher.stores.xbox.XboxRuntime.released(request.optString("lease")) @@ -108,7 +115,7 @@ object StoreLaunchRequests { else { lastLaunched = store to id CloudSaves.markDirty(app, store, id) - CloudSaves.downloadOrDefer(app, store, id).let { JSONObject().put("ok", it.ok).put("result", it.result).put("files", it.files).put("reason", it.reason) } + CloudSaves.downloadOrDefer(app, store, id, claim).let { JSONObject().put("ok", it.ok).put("result", it.result).put("files", it.files).put("reason", it.reason) } } } "cloud-up" -> { @@ -167,12 +174,14 @@ object StoreLaunchRequests { else -> JSONObject().put("ok", false).put("reason", "unknown-op") } try { - val resp = File(dir(app), "resp").apply { mkdirs() } val tmp = File(resp, file.name + ".tmp") tmp.writeText(reply.toString()) tmp.renameTo(File(resp, file.name)) } catch (e: Exception) { Log.w(TAG, "could not answer ${file.name}: ${e.message}") + } finally { + taken.delete() + writing.delete() } } diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ea/EaAuth.kt b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaAuth.kt index 67fb8e655e..a600c905bc 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/ea/EaAuth.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaAuth.kt @@ -17,7 +17,7 @@ object EaAuth { const val CLIENT_ID = "JUNO_PC_CLIENT" private const val CLIENT_SECRET = "4mRLtYMb6vq9qglomWEaT4ChxsXWcyqbQpuBNfMPOYOiDmYYQmjuaBsF2Zp0RyVeWkfqhE9TuGgAw7te" const val AUTH_URL = "https://accounts.ea.com/connect/auth" - private const val TOKEN_URL = "https://accounts.ea.com/connect/token" + internal var tokenUrl = "https://accounts.ea.com/connect/token" const val REDIRECT = "qrc:///html/login_successful.html" private const val REFRESH_MARGIN_MS = 5 * 60 * 1000L private val refreshLock = Any() @@ -41,8 +41,11 @@ object EaAuth { val personaId: String, val displayName: String, val eaId: String, + val rejected: Boolean = false, ) + private class Rejected(message: String) : IllegalStateException(message) + class Pending(val url: String, val verifier: String) internal fun fromJson(o: JSONObject): Account? { @@ -55,6 +58,7 @@ object EaAuth { personaId = o.optString("persona_id"), displayName = o.optString("display_name"), eaId = o.optString("ea_id"), + rejected = o.optBoolean("refresh_rejected"), ) } @@ -122,14 +126,15 @@ object EaAuth { private fun tokenCall(values: List>): JSONObject { val reply = EaHttp.request( - TOKEN_URL, "POST", + tokenUrl, "POST", mapOf("Content-Type" to "application/x-www-form-urlencoded"), EaHttp.form(values + listOf("client_id" to CLIENT_ID, "client_secret" to CLIENT_SECRET)).toByteArray(), follow = false, ) val json = runCatching { JSONObject(reply.text) }.getOrNull() if (!reply.ok || json == null || json.optString("access_token").isEmpty()) { - throw IllegalStateException("EA token request failed (${reply.status}): ${json?.optString("error_description")?.ifEmpty { json.optString("error") } ?: ""}") + val message = "EA token request failed (${reply.status}): ${json?.optString("error_description")?.ifEmpty { json.optString("error") } ?: ""}" + throw if (json?.optString("error") == "invalid_grant") Rejected(message) else IllegalStateException(message) } return json } @@ -162,6 +167,7 @@ object EaAuth { tokenCall(listOf("grant_type" to "refresh_token", "refresh_token" to a.refreshToken)) } catch (e: Exception) { Log.w(TAG, "refresh: ${e.message}") + if (e is Rejected) markRejected(context, a.refreshToken) return null } val access = token.getString("access_token") @@ -176,11 +182,24 @@ object EaAuth { val record = StoreAccounts.read(app, Store.EA) ?: return false record.put("access_token", access).put("expires_at", System.currentTimeMillis() + expiresIn * 1000) refresh.takeIf { it.isNotEmpty() }?.let { record.put("refresh_token", it) } + record.remove("refresh_rejected") player?.let { record.put("user_id", it.userId).put("persona_id", it.personaId).put("display_name", it.displayName).put("ea_id", it.eaId) } StoreAccounts.write(app, Store.EA, record) true } + private fun markRejected(context: Context, refresh: String) = synchronized(refreshLock) { + val app = context.applicationContext + val record = StoreAccounts.read(app, Store.EA) ?: return@synchronized + if (record.optString("refresh_token") != refresh) return@synchronized + StoreAccounts.write(app, Store.EA, record.put("refresh_rejected", true)) + } + + fun signedIn(context: Context): Boolean { + val a = load(context) ?: return false + return a.userId.isNotEmpty() && !(a.rejected && a.expiresAt <= System.currentTimeMillis()) + } + fun accessToken(context: Context): String? { val current = load(context) ?: return null if (fresh(current)) return current.accessToken diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ea/EaCloud.kt b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaCloud.kt new file mode 100644 index 0000000000..6c5242474e --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaCloud.kt @@ -0,0 +1,265 @@ +package com.droiddeck.launcher.stores.ea + +import android.util.Base64 +import android.util.Xml +import com.droiddeck.launcher.stores.CloudFile +import com.droiddeck.launcher.stores.CloudTransport +import java.io.File +import java.io.StringReader +import java.security.MessageDigest +import java.time.Instant +import org.xmlpull.v1.XmlPullParser + +internal class EaCloud( + private val token: String, + private val uid: String, + private val keys: List, + private val base: String, + private val rules: EaSaveRules, + val localDir: File? = null, +) : CloudTransport { + class Entry(val size: Long, val href: String, val md5: String, val created: String, val modified: String, val localName: String) + + class Manifest(val lineage: String, val files: List, val snapshots: List) + + private class Signed(val url: String, val headers: Map) + + private class Request(val verb: String, val resource: String, val md5: String? = null, val type: String? = null) + + private var key: String? = null + private var found = false + private var listed: Map? = null + private val committed = HashMap() + + override fun wants(name: String): Boolean = rules.matches(base + name) + + override fun committed(name: String): CloudFile? = committed[name] + + override fun list(): List { + val manifest = pick() + val files = manifest?.files.orEmpty().mapNotNull { e -> + val path = EaSaveRules.normalize(e.localName) + if (!path.startsWith(base, ignoreCase = true) || !rules.matches(path)) return@mapNotNull null + val md5 = hex(e.md5) ?: error("EA listed ${path.substring(base.length)} without a valid checksum") + CloudFile(localName(path.substring(base.length)), parseTime(e.modified), md5, readLink = e.href) + } + val twins = files.groupBy { it.name.lowercase() }.values.firstOrNull { it.size > 1 } + check(twins == null) { "EA lists more than one save named ${twins!!.first().name} in different cases" } + listed = files.associate { it.name.lowercase() to it.md5 } + return files + } + + private fun localName(name: String): String { + var dir = localDir ?: return name + val parts = name.split('/') + val out = ArrayList() + for (part in parts) { + val match = dir.listFiles()?.filter { it.name.equals(part, ignoreCase = true) }.orEmpty() + check(match.size <= 1) { "The save folder holds more than one file named $part in different cases" } + val pick = match.singleOrNull() + if (pick == null) return (out + parts.drop(out.size)).joinToString("/") + out += pick.name + dir = pick + } + return out.joinToString("/") + } + + override fun get(file: CloudFile): Pair { + val href = file.readLink ?: error("EA has no data for ${file.name}") + return locked("read", key ?: keys.first()) { lock, _ -> + val signed = authorize(lock, listOf(Request("GET", href))).single() + val reply = EaHttp.request(signed.url, headers = signed.headers) + check(reply.ok) { "EA cloud save download answered ${reply.status}" } + val want = file.md5 ?: error("EA listed ${file.name} without a valid checksum") + check(hex(b64(md5(reply.body))) == want) { "EA cloud save ${file.name} failed its checksum" } + reply.body to file.modifiedMs + } + } + + override fun put(files: Map, modifiedMs: Map): Map { + if (key == null) pick() + check(found || keys.size == 1) { "EA has no cloud save record for this game yet; play it once in the EA app" } + val target = key ?: keys.first() + val seen = listed + return locked("write", target) { lock, body -> + val locked = manifestUrl(body)?.let { fetchManifest(it) } + check(locked != null || !found) { "EA's cloud save record disappeared" } + val current = locked ?: Manifest("{00000000-0000-0000-0000-000000000000}", emptyList(), emptyList()) + if (seen != null) for (name in files.keys) { + val entry = current.files.firstOrNull { EaSaveRules.normalize(it.localName).equals(base + name, ignoreCase = true) } + val now = entry?.let { hex(it.md5) ?: error("EA lists $name without a valid checksum") } + check(now == seen[name.lowercase()]) { "EA's cloud saves changed since they were read" } + } + val now = System.currentTimeMillis() + val added = files.map { (name, data) -> + val sum = md5(data) + val stamp = stamp(modifiedMs[name]?.takeIf { it > 0 } ?: now) + val old = current.files.firstOrNull { EaSaveRules.normalize(it.localName).equals(base + name, ignoreCase = true) } + Entry(data.size.toLong(), "content/${data.size}-${hexOf(sum)}", b64(sum), old?.created ?: stamp, stamp, base + name) to data + } + val names = added.map { it.first.localName.lowercase() }.toSet() + val next = Manifest(current.lineage, current.files.filter { EaSaveRules.normalize(it.localName).lowercase() !in names } + added.map { it.first }, current.snapshots) + val xml = write(next).toByteArray() + val xmlMd5 = b64(md5(xml)) + val requests = added.map { Request("PUT", it.first.href, it.first.md5, "application/octet-stream") } + Request("PUT", "manifest.xml", xmlMd5, "text/xml") + val signed = authorize(lock, requests) + for ((i, pair) in added.withIndex()) upload(signed[i], pair.second) + upload(signed.last(), xml) + found = true + for ((entry, _) in added) { + val name = entry.localName.substring(base.length) + committed[name] = CloudFile(name, parseTime(entry.modified), hex(entry.md5), readLink = entry.href) + } + listed = null + files.mapValues { (name, _) -> modifiedMs[name]?.takeIf { it > 0 } ?: now } + } + } + + private fun pick(): Manifest? { + for (candidate in keys) { + val manifest = locked("read", candidate, missing = { null }) { _, body -> manifestUrl(body)?.let { fetchManifest(it) } } + if (manifest != null) { + key = candidate + found = true + return manifest + } + } + key = keys.first() + found = false + return null + } + + private fun upload(signed: Signed, data: ByteArray) { + val reply = EaHttp.request(signed.url, "PUT", signed.headers, data) + check(reply.ok) { "EA cloud save upload answered ${reply.status}" } + } + + private fun fetchManifest(url: String): Manifest? { + val reply = EaHttp.request(url) + if (reply.status == 404) return null + check(reply.ok) { "EA cloud save manifest answered ${reply.status}" } + return parse(reply.text) + } + + private fun locked(kind: String, key: String, missing: (() -> T)? = null, body: (String, String) -> T): T { + val reply = EaHttp.request("$HOST/lock/$kind/$uid/$key", "POST", mapOf(AUTH to token, "Content-Type" to "application/xml"), ByteArray(0)) + if (reply.status == 404 && missing != null) return missing() + check(reply.ok) { "EA cloud saves answered ${reply.status}" } + val lock = reply.header(LOCK) ?: error("EA did not grant a cloud save lock") + try { + return body(lock, reply.text) + } finally { + runCatching { EaHttp.request("$HOST/lock/delete/$uid", "DELETE", mapOf(AUTH to token, LOCK to lock)) } + } + } + + private fun authorize(lock: String, requests: List): List { + val body = buildString { + append("") + for ((i, r) in requests.withIndex()) { + append("").append(r.verb).append("").append(escape(r.resource)).append("") + r.md5?.let { append("").append(it).append("") } + r.type?.let { append("").append(it).append("") } + append("") + } + append("") + } + val reply = EaHttp.request("$HOST/lock/authorize/$uid", "PUT", mapOf(AUTH to token, LOCK to lock, "Content-Type" to "application/xml"), body.toByteArray()) + check(reply.ok) { "EA cloud saves refused the transfer (${reply.status})" } + return signedRequests(reply.text, requests.size) + } + + companion object { + private const val HOST = "https://cloudsync.juno.ea.com" + private const val AUTH = "X-Origin-AuthToken" + private const val LOCK = "X-Origin-Sync-Lock" + private val REQUEST = Regex("]*\\bid=\"(\\d+)\"[^>]*>(.*?)", RegexOption.DOT_MATCHES_ALL) + private val HEADER = Regex("]*)/?>") + + fun keys(masterTitleId: String, multiplayerId: String): List = + if (multiplayerId.isNotEmpty()) listOf("${masterTitleId}_$multiplayerId") else listOf(masterTitleId, "${masterTitleId}_$masterTitleId") + + fun manifestUrl(lockBody: String): String? = Regex("([^<]+)").find(lockBody)?.groupValues?.get(1)?.let(::unescape) + + fun parse(xml: String): Manifest { + val p = Xml.newPullParser().apply { setInput(StringReader(xml)) } + var root: String? = null + var lineage: String? = null + var depth = 0 + var closed = false + val files = ArrayList() + val snapshots = ArrayList() + var file: Array? = null + var local: Entry? = null + runCatching { + while (p.next() != XmlPullParser.END_DOCUMENT) when (p.eventType) { + XmlPullParser.START_TAG -> { + depth++ + val at = { name: String -> p.getAttributeValue(null, name).orEmpty() } + when { + root == null -> { root = p.name; lineage = p.getAttributeValue(null, "lineage") } + depth == 2 && p.name == "file" -> { file = arrayOf(at("size"), at("href"), at("md5")); local = null } + depth == 3 && p.name == "localName" && file != null -> { + val f = file!! + val created = at("created") + val modified = at("lastModified") + local = Entry(f[0].toLongOrNull() ?: 0, f[1], f[2], created, modified, p.nextText()) + depth-- + } + depth == 2 && p.name == "snapshot" -> { snapshots += p.nextText(); depth-- } + } + } + XmlPullParser.END_TAG -> { + if (depth == 2 && p.name == "file") { local?.let(files::add); file = null } + depth-- + if (depth == 0) closed = true + } + } + }.onFailure { error("EA sent an incomplete cloud save manifest") } + check(root == "manifest" && depth == 0 && closed) { "EA sent an incomplete cloud save manifest" } + return Manifest(lineage ?: "{00000000-0000-0000-0000-000000000000}", files, snapshots) + } + + fun write(m: Manifest): String = buildString { + append("\n") + for (f in m.files) { + append(" \n") + append(" ") + append(escape(f.localName)).append("\n \n") + } + for (s in m.snapshots) append(" ").append(escape(s)).append("\n") + append("\n") + } + + private fun signedRequests(xml: String, count: Int): List { + val byId = REQUEST.findAll(xml).associate { m -> + val inner = m.groupValues[2] + val url = Regex("([^<]+)").find(inner)?.groupValues?.get(1)?.let(::unescape) ?: error("EA sent no transfer URL") + val headers = HEADER.findAll(inner).mapNotNull { h -> + val k = attr(h.groupValues[1], "key") ?: return@mapNotNull null + k to (attr(h.groupValues[1], "value") ?: "") + }.toMap() + m.groupValues[1].toInt() to Signed(url, headers) + } + return (0 until count).map { byId[it] ?: error("EA signed ${byId.size} of $count cloud save transfers") } + } + + fun stamp(ms: Long): String = Instant.ofEpochMilli(ms / 1000 * 1000).toString() + "Z" + + fun parseTime(s: String): Long = runCatching { Instant.parse(s.trimEnd('Z') + "Z").toEpochMilli() }.getOrDefault(-1L) + + private fun attr(attrs: String, name: String): String? = Regex("\\b$name=\"([^\"]*)\"").find(attrs)?.groupValues?.get(1)?.let(::unescape) + + private fun md5(data: ByteArray) = MessageDigest.getInstance("MD5").digest(data) + + private fun b64(sum: ByteArray) = Base64.encodeToString(sum, Base64.NO_WRAP) + + private fun hexOf(sum: ByteArray) = sum.joinToString("") { "%02x".format(it) } + + fun hex(b64: String): String? = runCatching { hexOf(Base64.decode(b64, Base64.DEFAULT)) }.getOrNull()?.takeIf { it.length == 32 } + + private fun escape(s: String) = s.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """) + + private fun unescape(s: String) = EaSaveRules.unescape(s) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ea/EaSaveRules.kt b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaSaveRules.kt new file mode 100644 index 0000000000..b103473eb8 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaSaveRules.kt @@ -0,0 +1,84 @@ +package com.droiddeck.launcher.stores.ea + +internal class EaSaveRules(private val rules: List>) { + val includes get() = rules.filter { it.first }.map { it.second } + val excludes get() = rules.filterNot { it.first }.map { it.second } + + fun base(): String? { + val dirs = includes.filterNot(::bare).map { pattern -> + val wild = pattern.indexOfFirst { it == '*' || it == '?' } + val fixed = if (wild < 0) pattern else pattern.substring(0, wild) + if (fixed.endsWith('/')) fixed else fixed.substringBeforeLast('/', "") + "/" + } + val parts = dirs.map { it.trimEnd('/').split('/') } + if (parts.isEmpty()) return null + val common = ArrayList() + for (i in parts.minOf { it.size }.let { 0 until it }) { + val seg = parts[0][i] + if (parts.all { it[i].equals(seg, ignoreCase = true) }) common += seg else break + } + if (common.isEmpty() || !VARIABLE.matches(common[0])) return null + return common.joinToString("/") + "/" + } + + fun matches(path: String): Boolean { + val p = normalize(path) + val inside = base()?.let { p.startsWith(it, ignoreCase = true) } ?: false + return rules.firstOrNull { (_, pattern) -> if (bare(pattern)) inside && hit(pattern, p.substringAfterLast('/')) else hit(pattern, p) }?.first ?: false + } + + private fun bare(pattern: String) = !pattern.startsWith('%') && '/' !in pattern + + private fun hit(pattern: String, path: String): Boolean = when { + pattern.endsWith('/') -> path.startsWith(pattern, ignoreCase = true) + pattern.any { it == '*' || it == '?' } -> glob(pattern).matches(path) + else -> path.equals(pattern, ignoreCase = true) + } + + private fun glob(pattern: String): Regex = Regex( + pattern.split('*').joinToString("[^/]*") { part -> part.split('?').joinToString("[^/]") { Regex.escape(it) } }, + RegexOption.IGNORE_CASE, + ) + + companion object { + private val VARIABLE = Regex("%[A-Za-z]+%") + private val RULE = Regex("<(include|exclude)\\b([^>]*)>([^<]*)", RegexOption.IGNORE_CASE) + private val ORDER = Regex("\\border\\s*=\\s*[\"']?(\\d+)") + private val TOKENS = mapOf( + "documents" to "%DOCUMENTS%", "savedgames" to "%SAVED_GAMES%", "appdata" to "%APPDATA%", + "localappdata" to "%LOCALAPPDATA%", "programdata" to "%PROGRAMDATA%", "userprofile" to "%USERPROFILE%", + "locallowappdata" to "%APPLICATION_DATA_LOCAL_LOW%", "installdir" to "%INSTALL%", + ) + + private val COMMENT = Regex("", RegexOption.DOT_MATCHES_ALL) + private val ENTITY = Regex("&(#x[0-9a-fA-F]+|#[0-9]+|lt|gt|quot|apos|amp);") + + fun normalize(path: String) = path.replace('\\', '/') + + fun parse(xml: String): EaSaveRules? { + val rules = RULE.findAll(COMMENT.replace(xml, "")).mapIndexed { index, rule -> + val order = ORDER.find(rule.groupValues[2])?.groupValues?.get(1)?.toIntOrNull() ?: index + Triple(order, rule.groupValues[1].equals("include", true), normalize(unescape(rule.groupValues[3]).trim())) + }.filter { it.third.isNotEmpty() }.sortedBy { it.first }.map { it.second to it.third }.toList() + if (rules.none { it.first }) return null + return EaSaveRules(rules) + } + + fun template(base: String): String? { + val variable = VARIABLE.find(base)?.takeIf { it.range.first == 0 } ?: return null + val token = TOKENS[variable.value.trim('%').lowercase()] ?: return null + return token + base.substring(variable.value.length) + } + + fun unescape(s: String): String = ENTITY.replace(s) { m -> + when (val e = m.groupValues[1]) { + "lt" -> "<" + "gt" -> ">" + "quot" -> "\"" + "apos" -> "'" + "amp" -> "&" + else -> runCatching { String(Character.toChars(if (e.startsWith("#x")) e.substring(2).toInt(16) else e.substring(1).toInt())) }.getOrDefault(m.value) + } + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftActivation.kt b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftActivation.kt index d11fde4210..06075ca15d 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftActivation.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftActivation.kt @@ -15,23 +15,44 @@ object UbisoftActivation { private const val POLL_MS = 250L private const val IDLE_MS = 60_000L private val watching = HashMap() + private val owners = HashMap() + + private const val INVALIDATE = "denuvo-invalidate" fun bridge(context: Context, product: Long) = File(LinuxRuntime.rootDir(context), "root/.cache/droiddeck/ubisoft/bridge-$product") /** Starts answering the product's activations; stale requests are cleared before this returns. */ - fun watch(app: Context, product: Long) { + fun watch(app: Context, product: Long, owner: String?) { val dir = bridge(app, product) synchronized(watching) { val running = watching.containsKey(product) watching[product] = System.currentTimeMillis() + if (owner.isNullOrEmpty()) owners.remove(product) else owners[product] = owner if (running) return dir.mkdirs() - dir.listFiles()?.forEach { it.delete() } + dir.listFiles()?.forEach { if (it.name != INVALIDATE) it.delete() } } Thread({ var idleSince = 0L while (true) { - runCatching { dir.listFiles { f -> f.name.startsWith("denuvo-") && f.name.endsWith(".req") }?.forEach { answer(app, product, it) } } + runCatching { + val marker = File(dir, INVALIDATE) + val (owner, requests) = synchronized(watching) { + owners[product] to dir.listFiles { f -> f.name.startsWith("denuvo-") && f.name.endsWith(".req") }.orEmpty() + } + if (marker.exists()) { + val named = runCatching { marker.readText().trim() }.getOrNull()?.takeIf { it.isNotEmpty() } + val issuer = named ?: owner + if (invalidate(app, product, issuer)) marker.delete() + else if (named == null && issuer != null) runCatching { + val part = File(dir, "$INVALIDATE.part") + part.writeText(issuer) + check(part.renameTo(marker)) + } + } + val pending = marker.exists() + requests.forEach { answer(app, product, it, owner, pending) } + } .onFailure { StoresState.logLine("Ubisoft: activation for $product: ${it.javaClass.simpleName}") } val now = System.currentTimeMillis() if (SessionState.running) idleSince = 0L @@ -44,18 +65,31 @@ object UbisoftActivation { }, "ubisoft-activation-$product").start() } - private fun answer(app: Context, product: Long, request: File) { + private fun answer(app: Context, product: Long, request: File, owner: String?, pending: Boolean) { val challenge = runCatching { request.readBytes() }.getOrNull() ?: return request.delete() - val reply = File(request.parentFile, request.name.removeSuffix(".req") + ".rsp") - val token = activate(app, product, challenge) + val reply = File(request.parentFile, request.name.removeSuffix(".req").removeSuffix(".cache") + ".rsp") + val token = activate(app, product, challenge, owner, cached = request.name.endsWith(".cache.req") && !pending) val part = File(reply.path + ".part") part.writeBytes(token ?: ByteArray(0)) part.renameTo(reply) } - private fun activate(app: Context, product: Long, challenge: ByteArray): ByteArray? { + private fun invalidate(app: Context, product: Long, owner: String?): Boolean { + val user = owner ?: return false + val gone = TokenCache(cacheDir(app)).drop(user, product) + if (gone) StoresState.logLine("Ubisoft: activation for $product: kept game token dropped") + return gone + } + + private fun activate(app: Context, product: Long, challenge: ByteArray, owner: String?, cached: Boolean): ByteArray? { val s = UbisoftAuth.current(app) ?: return null.also { StoresState.logLine("Ubisoft: activation for $product: signed out") } + if (owner == null || s.userId != owner) return null.also { StoresState.logLine("Ubisoft: activation for $product: the signed-in account changed") } + val cache = TokenCache(cacheDir(app)) + if (cached) cache.get(s.userId, product)?.let { + StoresState.logLine("Ubisoft: activation for $product: kept ${it.size} B game token") + return it + } val answer = UbisoftNative.call("denuvo", JSONObject().put("ticket", s.ticket).put("sessionId", s.sessionId) .put("productId", product).put("challenge", hex(challenge))) answer?.optString("error")?.takeIf { it.isNotEmpty() }?.let { @@ -64,9 +98,28 @@ object UbisoftActivation { } val token = answer?.optString("token")?.takeIf { it.isNotEmpty() }?.let(::unhex) StoresState.logLine("Ubisoft: activation for $product: ${token?.size ?: 0} B game token") + if (token != null && !cache.put(s.userId, product, token)) StoresState.logLine("Ubisoft: activation for $product: game token not kept") return token } + private fun cacheDir(app: Context) = File(app.filesDir, "ubisoft/denuvo") + + internal class TokenCache(private val dir: File) { + private fun file(user: String, product: Long) = File(dir, "${user.filter { it.isLetterOrDigit() || it == '-' }}-$product.token") + + fun get(user: String, product: Long): ByteArray? = runCatching { file(user, product).readBytes() }.getOrNull()?.takeIf { it.isNotEmpty() } + + fun put(user: String, product: Long, token: ByteArray): Boolean = runCatching { + dir.mkdirs() + val target = file(user, product) + val part = File(target.path + ".part") + part.writeBytes(token) + check(part.renameTo(target)) + }.isSuccess + + fun drop(user: String, product: Long): Boolean = file(user, product).let { it.delete() || !it.exists() } + } + internal fun hex(b: ByteArray): String = b.joinToString("") { "%02x".format(it) } internal fun unhex(s: String): ByteArray? = diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftCloud.kt b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftCloud.kt new file mode 100644 index 0000000000..26001da37a --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftCloud.kt @@ -0,0 +1,220 @@ +package com.droiddeck.launcher.stores.ubisoft + +import android.os.Build +import android.util.Base64 +import com.droiddeck.launcher.stores.CloudChanged +import com.droiddeck.launcher.stores.CloudFile +import com.droiddeck.launcher.stores.CloudTransport +import org.json.JSONArray +import org.json.JSONObject +import java.io.ByteArrayOutputStream +import java.net.HttpURLConnection +import java.net.URL +import java.security.MessageDigest +import java.time.Instant +import java.util.zip.Deflater +import java.util.zip.Inflater + +internal interface UbisoftSlots { + fun entity(): UbisoftCloud.Entity? + fun upload(slot: String, data: ByteArray) + fun update(entity: UbisoftCloud.Entity, slots: JSONArray) + fun download(slot: String): ByteArray? +} + +internal class UbisoftCloud(private val session: UbisoftAuth.Session, val product: Long) : UbisoftSlots { + class Slot(val id: Int, val filename: String, val hash: String, val size: Long, val modifiedMs: Long) + + class Entity(val json: JSONObject) { + val id: String get() = json.optString("entityId") + val revision: Int get() = json.optInt("revision") + val slots: List get() { + val list = json.optJSONObject("obj")?.optJSONArray("slots") ?: JSONArray() + return (0 until list.length()).mapNotNull { list.optJSONObject(it) }.map { + Slot(it.optInt("id"), it.optString("filename"), it.optString("hash"), it.optLong("size"), UbisoftAuth.parseTime(it.optString("lastModified"))) + } + } + } + + private val authorization get() = "ubi_v1 t=${session.ticket}" + + override fun entity(): Entity? { + val url = "https://public-ubiservices.ubi.com/v1/profiles/${session.profileId}/entities?name=${entityName(product)}" + val reply = UbisoftAuth.request(url, authorization = authorization, sessionId = session.sessionId) + check(reply.ok) { "Ubisoft cloud saves answered ${reply.status}" } + val list = JSONObject(reply.body).optJSONArray("entities") ?: return null + return (0 until list.length()).mapNotNull { list.optJSONObject(it) }.firstOrNull { it.optString("name") == entityName(product) }?.let(::Entity) + } + + override fun upload(slot: String, data: ByteArray) { + val packed = deflate(data) + val md5 = Base64.encodeToString(MessageDigest.getInstance("MD5").digest(packed), Base64.NO_WRAP) + val signed = signed("put", JSONArray().put(JSONObject().put("name", slot).put("size", packed.size).put("md5", md5))).single() + val c = URL(signed.getString("url")).openConnection() as HttpURLConnection + c.connectTimeout = 15_000 + c.readTimeout = 60_000 + c.requestMethod = "PUT" + c.doOutput = true + c.setFixedLengthStreamingMode(packed.size) + c.setRequestProperty("Content-MD5", md5) + try { + c.outputStream.use { it.write(packed) } + check(c.responseCode in 200..299) { "Ubisoft cloud save upload answered ${c.responseCode}" } + } finally { + c.disconnect() + } + } + + override fun update(entity: Entity, slots: JSONArray) { + val obj = JSONObject(entity.json.optJSONObject("obj")?.toString() ?: "{}") + .put("slots", slots).put("timestamp", (System.currentTimeMillis() / 1000).toString()).put("pc_name", Build.MODEL ?: "Android") + val body = JSONObject().put("revision", entity.revision).put("name", entity.json.optString("name")).put("type", "cloudSave") + .put("spaceId", entity.json.optString("spaceId")).put("tags", entity.json.optJSONArray("tags") ?: JSONArray()).put("obj", obj) + val reply = UbisoftAuth.request("https://public-ubiservices.ubi.com/v1/profiles/entities/${entity.id}", "PUT", + authorization, session.sessionId, body.toString()) + check(reply.ok) { "Ubisoft cloud save record answered ${reply.status}" } + } + + override fun download(slot: String): ByteArray? { + val signed = signed("get", JSONArray().put(JSONObject().put("name", slot))).single() + val c = URL(signed.getString("url")).openConnection() as HttpURLConnection + c.connectTimeout = 15_000 + c.readTimeout = 60_000 + return try { + when (c.responseCode) { + 404 -> null + in 200..299 -> c.inputStream.use(::inflate) + else -> error("Ubisoft cloud save download answered ${c.responseCode}") + } + } finally { + c.disconnect() + } + } + + private fun signed(method: String, items: JSONArray): List { + val answer = UbisoftNative.call("cloud-urls", JSONObject().put("ticket", session.ticket).put("sessionId", session.sessionId) + .put("productId", product).put("method", method).put("items", items)) ?: error("Ubisoft cloud saves are unavailable") + answer.optString("error").takeIf { it.isNotEmpty() }?.let { error(it) } + val requests = answer.getJSONArray("requests") + return (0 until requests.length()).map { requests.getJSONObject(it) } + } + + companion object { + private const val MAX_SAVE = 256L * 1024 * 1024 + + fun entityName(product: Long) = "upcCloudSaveUplayId_$product" + + private val MARK = Regex("[0-9a-f]{32}") + + internal fun freshMark(): String = java.util.UUID.randomUUID().toString().replace("-", "") + + internal fun mark(save: ByteArray): String? = + if (save.size < 40) null else String(save, 8, 32, Charsets.ISO_8859_1).takeIf { MARK.matches(it) } + + internal fun deflate(data: ByteArray): ByteArray { + val deflater = Deflater(Deflater.BEST_COMPRESSION) + deflater.setInput(data) + deflater.finish() + val out = ByteArrayOutputStream(data.size / 2 + 64) + val buffer = ByteArray(64 * 1024) + try { + while (!deflater.finished()) out.write(buffer, 0, deflater.deflate(buffer)) + } finally { + deflater.end() + } + return out.toByteArray() + } + + internal fun inflate(data: ByteArray): ByteArray = inflate(data.inputStream()) + + internal fun inflate(input: java.io.InputStream): ByteArray { + val inflater = Inflater() + val out = ByteArrayOutputStream(64 * 1024) + val buffer = ByteArray(64 * 1024) + val chunk = ByteArray(64 * 1024) + try { + while (!inflater.finished()) { + if (inflater.needsInput()) { + val read = input.read(chunk) + if (read < 0) error("Ubisoft cloud save is truncated") + inflater.setInput(chunk, 0, read) + continue + } + if (inflater.needsDictionary()) error("Ubisoft cloud save is truncated") + val n = inflater.inflate(buffer) + check(out.size().toLong() + n <= MAX_SAVE) { "Ubisoft cloud save is too large" } + out.write(buffer, 0, n) + } + } finally { + inflater.end() + } + return out.toByteArray() + } + + internal fun version(slot: Slot): String = "${slot.size}:${slot.modifiedMs}:${slot.hash}" + } +} + +internal class UbisoftCloudTransport(private val cloud: UbisoftSlots, val user: String) : CloudTransport { + private var listed: UbisoftCloud.Entity? = null + private val committed = HashMap() + + override val contentDigests: Boolean get() = false + + override fun committed(name: String): CloudFile? = committed[name] + + override fun wants(name: String): Boolean = name.endsWith(SUFFIX) && !name.contains('/') + + override fun list(): List { + val entity = cloud.entity() + listed = entity + return entity?.slots.orEmpty().map { CloudFile(it.filename + SUFFIX, it.modifiedMs, UbisoftCloud.version(it)) } + } + + override fun get(file: CloudFile): Pair { + val data = cloud.download(file.name.removeSuffix(SUFFIX)) ?: error("Ubisoft has no data for ${file.name}") + return data to file.modifiedMs + } + + override fun put(files: Map, modifiedMs: Map): Map { + var expected = listed ?: cloud.entity() ?: error("Ubisoft has no cloud save record for this game yet; play it once in Ubisoft Connect") + val marks = files.mapValues { (name, data) -> + check(name.endsWith(SUFFIX) && !name.contains('/')) { "$name is not a Ubisoft save slot" } + UbisoftCloud.mark(data) ?: error("$name has no save identifier for Ubisoft Connect") + } + val sent = LinkedHashMap() + try { + for ((name, data) in files) { + val slot = name.removeSuffix(SUFFIX) + val live = cloud.entity() ?: error("Ubisoft's cloud save record disappeared") + if (live.revision != expected.revision) throw CloudChanged(sent, files.keys.filter { it !in sent }) + val now = System.currentTimeMillis() + cloud.upload(slot, data) + val slots = JSONArray(live.json.optJSONObject("obj")?.optJSONArray("slots")?.toString() ?: "[]") + val entry = (0 until slots.length()).map { slots.getJSONObject(it) }.firstOrNull { it.optString("filename") == slot } + ?: JSONObject().put("filename", slot).also { if (slot.toIntOrNull() != null) it.put("id", slot.toInt()); slots.put(it) } + entry.put("hash", marks.getValue(name)).put("size", data.size).put("lastModified", Instant.ofEpochMilli(now).toString()) + cloud.update(live, slots) + val after = cloud.entity() + val mine = after?.let { committedSlot(live, it, slot, marks.getValue(name), data.size.toLong()) } + ?: throw CloudChanged(sent, files.keys.filter { it !in sent }) + sent[name] = now + committed[name] = CloudFile(name, mine.modifiedMs, UbisoftCloud.version(mine)) + expected = after + } + } finally { + listed = null + } + return sent + } + + companion object { + const val SUFFIX = ".save" + + internal fun committedSlot(before: UbisoftCloud.Entity, after: UbisoftCloud.Entity, filename: String, hash: String, size: Long): UbisoftCloud.Slot? { + fun others(e: UbisoftCloud.Entity) = e.slots.filter { it.filename != filename }.associate { it.filename to UbisoftCloud.version(it) } + if (others(before) != others(after)) return null + return after.slots.firstOrNull { it.filename == filename && it.hash == hash && it.size == size } + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftLaunch.kt b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftLaunch.kt index 279d3b3e43..4f284f77b4 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftLaunch.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftLaunch.kt @@ -58,7 +58,7 @@ object UbisoftLaunch { } } ?: cached setup?.let { StoresState.logLine("Ubisoft: setup for $product: ${it.toString().take(1500)}") } - UbisoftActivation.watch(app, product) + UbisoftActivation.watch(app, product, s.userId) return JSONObject().put("ok", true) .put("productId", product) .put("ticket", native.optString("ticket")) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndGames.kt b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndGames.kt index a276eb91e5..ab0a80940b 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndGames.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndGames.kt @@ -82,6 +82,12 @@ internal fun cloudMessage(context: android.content.Context, r: com.droiddeck.lau r.files == 0 -> context.getString(R.string.cloud_up_to_date) else -> context.getString(R.string.cloud_done, r.files) } + "partial" -> when { + r.conflicts > 0 && r.detail.isNotEmpty() -> context.getString(R.string.cloud_conflict_files_detail, r.conflicts, r.detail) + r.conflicts > 0 -> context.getString(R.string.cloud_conflict_files, r.conflicts) + r.detail.isNotEmpty() -> context.getString(R.string.cloud_partial, r.detail) + else -> context.getString(R.string.cloud_not_synced) + } "skipped" -> context.getString(when (r.reason) { "not-signed-in" -> R.string.cloud_signed_out "no-cloud-saves" -> R.string.cloud_none diff --git a/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt b/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt index b99a4331bb..edd95faab4 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt @@ -258,7 +258,7 @@ internal fun GameDetail( stringResource(R.string.game_tab_achievements_count, stringResource(R.string.achievements_title), achievementState.done, list.size) } ?: stringResource(R.string.achievements_title) GameTab.LAUNCH -> stringResource(R.string.game_tab_launch) - GameTab.SAVES -> stringResource(if (Store.byId(source).let { it == Store.GOG || it == Store.EPIC }) R.string.game_tab_saves_cloud else R.string.games_saves) + GameTab.SAVES -> stringResource(if (CloudSaves.supports(Store.byId(source))) R.string.game_tab_saves_cloud else R.string.games_saves) GameTab.ADDONS -> stringResource(R.string.game_tab_addons) GameTab.INSTALL -> stringResource(R.string.game_tab_install) } @@ -892,8 +892,10 @@ private fun ShortcutFields(game: Library.SteamGame, folder: File) { @Composable private fun SavesTab(g: Library.SteamGame, prefix: File, a: FrontEndActions) { - val saves by produceState?>(null, g.gameId) { - value = withContext(Dispatchers.IO) { runCatching { GameSaves.locate(prefix, g) }.getOrDefault(emptyList()) } + val context = LocalContext.current.applicationContext + var synced by remember(g.gameId) { mutableStateOf(0) } + val saves by produceState?>(null, g.gameId, synced) { + value = withContext(Dispatchers.IO) { runCatching { GameSaves.saves(context, prefix, g) }.getOrDefault(emptyList()) } } val found = saves val summary = when { @@ -911,9 +913,9 @@ private fun SavesTab(g: Library.SteamGame, prefix: File, a: FrontEndActions) { } Column(Modifier.fillMaxWidth().tabPanel(), verticalArrangement = Arrangement.spacedBy(2.dp)) { Text(summary, fontSize = 13.sp, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.padding(bottom = 6.dp).readable().padding(4.dp)) - if (Store.byId(g.source).let { it == Store.GOG || it == Store.EPIC }) { + if (CloudSaves.supports(Store.byId(g.source))) { SectionLabel(stringResource(R.string.store_cloud_saves)) - CloudRows(g) + CloudRows(g) { synced++ } SectionLabel(stringResource(R.string.games_saves)) } MenuItem(stringResource(R.string.games_import_saves), checked = false) { a.onSaveImport(g) } @@ -969,14 +971,16 @@ private fun AddonRow(name: String, target: WowAddons.Target, a: FrontEndActions) } @Composable -private fun CloudRows(g: Library.SteamGame) { - val store = Store.byId(g.source)?.takeIf { it != Store.AMAZON && it != Store.EA } ?: return +private fun CloudRows(g: Library.SteamGame, onSynced: () -> Unit = {}) { + val store = Store.byId(g.source)?.takeIf { it != Store.AMAZON } ?: return val id = g.storeId ?: return val context = LocalContext.current.applicationContext var busy by remember(g.gameId) { mutableStateOf(null) } var tick by remember(g.gameId) { mutableStateOf(0) } + var refreshing by remember(g.gameId) { mutableStateOf(false) } val status by produceState(null, g.gameId, tick) { value = withContext(Dispatchers.IO) { runCatching { CloudSaves.status(context, store, id) }.getOrNull() } + refreshing = false } val folder = g.gameFiles var enabled by remember(g.gameId) { mutableStateOf(folder?.let { StoreGameSidecar.read(it)?.cloud } ?: true) } @@ -988,7 +992,7 @@ private fun CloudRows(g: Library.SteamGame) { busy = if (up) "up" else "down" Thread({ val r = if (up) CloudSaves.upload(context, store, id, force) else CloudSaves.download(context, store, id, force) - StoresState.post { busy = null; tick++; android.widget.Toast.makeText(context, cloudMessage(context, r), android.widget.Toast.LENGTH_SHORT).show() } + StoresState.post { refreshing = true; busy = null; tick++; onSynced(); android.widget.Toast.makeText(context, cloudMessage(context, r), android.widget.Toast.LENGTH_SHORT).show() } }, "cloud-manual").start() } MenuItem(stringResource(R.string.store_cloud_saves), checked = enabled && supported, enabled = supported, @@ -1000,11 +1004,13 @@ private fun CloudRows(g: Library.SteamGame) { StoresState.post { enabled = written?.cloud ?: enabled } }, "cloud-switch").start() } - val ready = st != null && supported && enabled && busy == null + val ready = st != null && supported && enabled && busy == null && !refreshing && st.folder != null && st.signedIn val pending = stringResource(R.string.cloud_pending) val summary = when { st == null || busy != null -> pending !supported -> stringResource(R.string.cloud_none) + !st.signedIn || st.reason == "not-signed-in" -> stringResource(R.string.cloud_signed_out) + st.folder == null -> stringResource(R.string.cloud_not_synced) conflict -> stringResource(R.string.cloud_conflict_files, st.conflicts.size) st.lastSync > 0 -> stringResource(R.string.cloud_last_sync, android.text.format.DateUtils.getRelativeTimeSpanString(st.lastSync).toString()) else -> stringResource(R.string.cloud_never) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 44e51c682c..e697c0bb00 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2898,6 +2898,8 @@ Already up to date Sign in to the store to sync saves Saves were not synced + Some saves were not synced · %1$s + Cloud conflict · %1$d files · %2$s installed in your library store diff --git a/app/src/main/rust/stores/src/ubisoft/ffi.rs b/app/src/main/rust/stores/src/ubisoft/ffi.rs index 30df1ccd4f..9cfbb97801 100644 --- a/app/src/main/rust/stores/src/ubisoft/ffi.rs +++ b/app/src/main/rust/stores/src/ubisoft/ffi.rs @@ -75,6 +75,33 @@ fn denuvo(ticket: &str, session: &str, product: u32, challenge: &str) -> Result< }) } +fn cloud_urls(request: &serde_json::Value) -> Result { + let s = |k: &str| request.get(k).and_then(|v| v.as_str()).unwrap_or("").to_string(); + let product = request.get("productId").and_then(|v| v.as_u64()).unwrap_or(0) as u32; + let method = match s("method").as_str() { + "put" => super::service::CLOUD_PUT, + "get" => super::service::CLOUD_GET, + other => return Err(format!("unknown cloud save method {other}")), + }; + let items: Vec = request.get("items").and_then(|v| v.as_array()).ok_or("no items")?.iter().map(|i| super::service::CloudItem { + name: i.get("name").and_then(|v| v.as_str()).unwrap_or("").to_string(), + size: i.get("size").and_then(|v| v.as_u64()).unwrap_or(0), + md5_base64: i.get("md5").and_then(|v| v.as_str()).unwrap_or("").to_string(), + }).collect(); + if items.is_empty() || items.iter().any(|i| i.name.is_empty()) { + return Err("cloud save items need names".into()); + } + let (ticket, session) = (s("ticket"), s("sessionId")); + let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().map_err(|e| e.to_string())?; + rt.block_on(async { + let mut d = super::demux::Demux::connect(&ticket).await?; + let (mut ownership, _) = super::service::Ownership::open(&mut d, &ticket, &session).await?; + let token = ownership.token(&mut d, product).await?; + let signed = super::service::cloudsave_urls(&mut d, &token, product, method, &items).await?; + Ok(json!({ "requests": signed.iter().map(|r| json!({ "url": r.url, "header": r.header })).collect::>() })) + }) +} + fn call(op: &str, request: &str) -> String { let parsed: serde_json::Value = serde_json::from_str(request).unwrap_or(serde_json::Value::Null); let s = |k: &str| parsed.get(k).and_then(|v| v.as_str()).unwrap_or("").to_string(); @@ -86,6 +113,7 @@ fn call(op: &str, request: &str) -> String { .and_then(|r| install::chunks(&r)) .map(|m| json!({ "chunks": install::chunks_json(&m.chunks), "setup": install::setup_json(&m) })) .unwrap_or_else(|e| json!({ "error": e })), + "cloud-urls" => cloud_urls(&parsed).unwrap_or_else(|e| json!({ "error": e })), "launch" => launch(&s("ticket"), &s("sessionId"), parsed.get("productId").and_then(|v| v.as_u64()).unwrap_or(0) as u32) .unwrap_or_else(|e| json!({ "error": e })), _ => json!({ "error": format!("unknown operation {op}") }), diff --git a/app/src/main/rust/stores/src/ubisoft/service.rs b/app/src/main/rust/stores/src/ubisoft/service.rs index bfdddc29a9..de8e724d09 100644 --- a/app/src/main/rust/stores/src/ubisoft/service.rs +++ b/app/src/main/rust/stores/src/ubisoft/service.rs @@ -204,3 +204,48 @@ pub async fn denuvo_token(d: &mut Demux, ownership_token: &str, challenge: &[u8] let t = pb::fields(pb::sub(&rsp, 3).ok_or("no game token answer")?)?; pb::sub(&t, 1).filter(|b| !b.is_empty()).map(<[u8]>::to_vec).ok_or_else(|| "Ubisoft sent an empty game token".into()) } + +pub struct CloudItem { + pub name: String, + pub size: u64, + pub md5_base64: String, +} + +pub struct SignedRequest { + pub header: String, + pub url: String, +} + +pub const CLOUD_PUT: u64 = 1; +pub const CLOUD_GET: u64 = 2; + +pub async fn cloudsave_urls(d: &mut Demux, ownership_token: &str, product_id: u32, method: u64, items: &[CloudItem]) -> Result, String> { + let conn = d.open("cloudsave_service").await?; + let mut req = Msg::new().text(1, ownership_token).uint(2, product_id as u64).uint(3, method); + for item in items { + let mut entry = Msg::new().text(1, &item.name); + if method == CLOUD_PUT { + entry = entry.msg(2, Msg::new().uint(1, item.size).text(2, &item.md5_base64)); + } + req = req.msg(4, entry); + } + let raw = d.call(conn, Msg::new().msg(1, Msg::new().uint(1, 1).msg(2, req))).await?; + let rsp = pb::fields(pb::sub(&pb::fields(&raw)?, 1).ok_or("no cloud save answer")?)?; + let urls = pb::fields(pb::sub(&rsp, 2).ok_or("no cloud save URLs")?)?; + let status = pb::num(&urls, 1).unwrap_or(0); + if status != 1 { + return Err(format!("Ubisoft's cloud save service refused the request (status {status})")); + } + let mut out = Vec::new(); + for r in pb::subs(&urls, 2) { + let r = pb::fields(r)?; + out.push(SignedRequest { + header: pb::string(&r, 1).unwrap_or_default(), + url: pb::string(&r, 2).ok_or("cloud save URL missing")?, + }); + } + if out.len() != items.len() { + return Err(format!("Ubisoft signed {} of {} cloud save requests", out.len(), items.len())); + } + Ok(out) +} 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 9f1dcd2b0d..f3944a8034 100644 --- a/app/src/test/java/com/droiddeck/launcher/stores/CloudSavesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/stores/CloudSavesTest.kt @@ -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>() val puts = ArrayList() + 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, modifiedMs: Map): Map = - 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, modifiedMs: Map): Map { + val sent = LinkedHashMap() + 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() } diff --git a/app/src/test/java/com/droiddeck/launcher/stores/ea/EaTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/ea/EaTest.kt index b9deb11a11..2a3ed5ce63 100644 --- a/app/src/test/java/com/droiddeck/launcher/stores/ea/EaTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/stores/ea/EaTest.kt @@ -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 = """ +%Documents%/Need for Speed Heat/SaveGame/savegame/ +%Documents%/Need for Speed Heat/SaveGame/wraps/ +%Documents%/Need for Speed Heat/settings/PROFILEOPTIONS +""" + + @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("""%Documents%/Battlefield 6/settings/*.xml + %Documents%/Battlefield 6/settings/*.sav""")!! + 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(""" + %Documents%/BioWare/Mass Effect Legendary Edition/Config/ + %Documents%/BioWare/Mass Effect Legendary Edition/""")!! + 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("")) + assertNull(EaSaveRules.parse("""C:/Games/x/""")!!.base()) + } + + @Test fun rulesApplyInTheirOrderAndTheFirstMatchDecides() { + val rules = EaSaveRules.parse(""" +%Documents%/Game/Config/ +%Documents%/Game/Config/keys.ini +%Documents%/Game/""")!! + 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(""" +%Documents%/My Games/SteamWorld Dig/controls.cfg +%Documents%/My Games/SteamWorld Dig/settings.xml +*.xml +%Documents%/My Games/SteamWorld Dig/profile.dat""")!! + 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("""%AppData%/The Witness/**.png""")!! + assertEquals("%AppData%/The Witness/", witness.base()) + assertTrue(witness.matches("%AppData%/The Witness/shot.png")) + } + + @Test fun localLowAndInstallFoldersResolveToTheirTemplates() { + val fe = EaSaveRules.parse("""%LocalLowAppData%/Zoink Games/Fe/SystemSettings.sav +%LocalLowAppData%/Zoink Games/Fe/Save1.sav""")!! + assertEquals("%APPLICATION_DATA_LOCAL_LOW%/Zoink Games/Fe/", EaSaveRules.template(fe.base()!!)) + val castle = EaSaveRules.parse("""%InstallDir%/x86/Lost_Castle_Data/lc_game_data +%InstallDir%/x64/Lost_Castle_Data/lc_game_data""")!! + 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(""" + %Documents%/A & B/1 + s1""") + 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("") }.isFailure) + } + + @Test fun aManifestKeepsItsFilesSnapshotsAndEaTimestampsThroughAWrite() { + val xml = """ + + %Documents%/Need for Speed Heat/SaveGame/savegame/1 + + 80cmnqT/rIKQRmciINSVhQ== +""" + 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(""" + + %Documents%/Game/ """)!! + 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("""""") } + assertTrue(cut.isFailure) + val open = runCatching { EaCloud.parse("""%Documents%/a""") } + assertTrue(open.isFailure) + } + + @Test fun snapshotEntitiesAreDecodedOnceAndWrittenBackTheSame() { + val m = EaCloud.parse("""abc+def""") + 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("https://x/y?a=1&b=2")) + assertTrue(CloudSaves.supports(Store.EA)) + } } diff --git a/app/src/test/java/com/droiddeck/launcher/stores/ubisoft/MemorySlots.kt b/app/src/test/java/com/droiddeck/launcher/stores/ubisoft/MemorySlots.kt new file mode 100644 index 0000000000..e32909ee71 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/ubisoft/MemorySlots.kt @@ -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() + 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() + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/ubisoft/UbisoftTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/ubisoft/UbisoftTest.kt index aecf4ca779..b0aea68660 100644 --- a/app/src/test/java/com/droiddeck/launcher/stores/ubisoft/UbisoftTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/stores/ubisoft/UbisoftTest.kt @@ -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) = 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() + } } diff --git a/app/src/test/java/com/droiddeck/launcher/ui/CloudMessageTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/CloudMessageTest.kt index 2bf7360edf..0ada7eba29 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/CloudMessageTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/CloudMessageTest.kt @@ -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")) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch b/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch index 1a5bc27114..5031cb93dc 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch @@ -116,7 +116,7 @@ def through_client(folder, sidecar, timeout=CLIENT_TIMEOUT): return str(answer.get("reason") or "failed") -def ask(channel, request, timeout=TIMEOUT): +def ask(channel, request, timeout=TIMEOUT, claim=False): req_dir, resp_dir = channel / "req", channel / "resp" req_dir.mkdir(parents=True, exist_ok=True) resp_dir.mkdir(parents=True, exist_ok=True) @@ -125,19 +125,33 @@ def ask(channel, request, timeout=TIMEOUT): tmp.write_text(json.dumps(request)) os.rename(tmp, req_dir / (rid + ".json")) answer = resp_dir / (rid + ".json") - deadline = time.monotonic() + timeout - while time.monotonic() < deadline: + + def wait(seconds): + deadline = time.monotonic() + seconds + while time.monotonic() < deadline: + try: + value = json.loads(answer.read_text()) + answer.unlink() + return value + except (OSError, ValueError): + time.sleep(0.05) + return None + + value = wait(timeout) + if value is not None: + return value + for pending in (req_dir / (rid + ".json"), resp_dir / (rid + ".taken")): + abandoned = pending.with_name(rid + ".abandoned") try: - value = json.loads(answer.read_text()) - answer.unlink() - return value - except (OSError, ValueError): - time.sleep(0.05) - try: - (req_dir / (rid + ".json")).unlink() - except OSError: - pass - return None + os.rename(pending, abandoned) + except OSError: + continue + try: + abandoned.unlink() + except OSError: + pass + return None + return wait(CLAIMED_TIMEOUT) if claim else None def windows_path(unix): @@ -255,7 +269,8 @@ def create_prefix(compat_data, proton, timeout=120): CLOUD_TIMEOUT = 15.0 -CLOUD_STORES = ("gog", "epic") +CLAIMED_TIMEOUT = 120.0 +CLOUD_STORES = ("gog", "epic", "ea", "ubisoft") STORE_GAME = 10 RAN = 11 RAN_FAILED = 12 @@ -304,11 +319,14 @@ def exited(argv, code=0): if found is not None and launch_dir and str(found[1].get("store")) == "ea": ask(Path(launch_dir) / "stores", {"op": "ea-exited", "store": "ea", "id": str(found[1].get("id"))}, timeout=1.0) log("ea %s exited" % found[1].get("id")) - return 0 if found is None or not launch_dir or not cloud_on(found[1]): return 0 store, game = str(found[1].get("store")), str(found[1].get("id")) - ask(Path(launch_dir) / "stores", {"op": "cloud-up", "store": store, "id": game}, timeout=1.0) + try: + ask(Path(launch_dir) / "stores", {"op": "cloud-up", "store": store, "id": game}, timeout=1.0) + except OSError as error: + log("cloud %s %s up not requested (%s)" % (store, game, type(error).__name__)) + return 0 log("cloud %s %s up requested" % (store, game)) return 0 @@ -1601,7 +1619,7 @@ def main(argv): compat_data = os.environ.get("STEAM_COMPAT_DATA_PATH", "").rstrip("/") prefix_id = os.path.basename(compat_data) if compat_data else "" launch_dir = os.environ.get("DD_LAUNCH_DIR", "") - if launch_dir and cloud_on(sidecar): + if launch_dir and cloud_on(sidecar) and store != "ea": made = create_prefix(compat_data, os.environ.get("DD_STORE_PROTON", "")) if made != "present": log("cloud %s %s prefix=%s" % (store, game, made)) @@ -1670,13 +1688,16 @@ def main(argv): log("epic prefix=%s %s" % (prefix_id or "?", provision_prefix(compat_data))) if store == "ea": prepare_ea(folder, sidecar, compat_data, launch_dir) - return STORE_GAME if not cloud_on(sidecar): if store in CLOUD_STORES: log("cloud %s %s down result=skipped reason=off" % (store, game)) - return 0 + return STORE_GAME if store == "ea" else 0 if launch_dir: - answer = ask(Path(launch_dir) / "stores", {"op": "cloud-down", "store": store, "id": game}, timeout=CLOUD_TIMEOUT) + try: + answer = ask(Path(launch_dir) / "stores", {"op": "cloud-down", "store": store, "id": game}, timeout=CLOUD_TIMEOUT, claim=True) + except OSError as error: + log("cloud %s %s down result=failed reason=%s" % (store, game, type(error).__name__)) + return STORE_GAME if answer is None: log("cloud %s %s down result=timeout" % (store, game)) else: diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-xbox b/tools/linuxfs/overlay/usr/local/bin/droiddeck-xbox index ca7032cdc9..143aa07c27 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-xbox +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-xbox @@ -518,7 +518,7 @@ def launch(command, env): context.chmod(0o600) owned_env = dict(env, DROIDDECK_XBOX_ACTIVE='1', DROIDDECK_XBOX_COMMAND_FILE=str(context), XODUS_SOCKET=reply['socket'], XODUS_BRIDGE_PORT=str(port), XODUS_BRIDGE_KEY=key, - XODUS_LOG='off', RUST_LOG='off', XODUS_SAVE_SYNC='auto' if sidecar.get('cloud', True) else 'off') + XODUS_LOG='off', RUST_LOG='off', XODUS_SAVE_SYNC='auto' if sidecar.get('cloud', True) else 'none') for name in ('DROIDDECK_XBOX_CREDENTIAL_SOCKET', 'DROIDDECK_XBOX_CREDENTIAL_PORT', 'DROIDDECK_XBOX_CREDENTIAL_KEY'): owned_env.pop(name, None) if reply.get('playfab'): diff --git a/tools/tests/test_droiddeck_epic.py b/tools/tests/test_droiddeck_epic.py index 6849db9c15..e78de6280c 100644 --- a/tools/tests/test_droiddeck_epic.py +++ b/tools/tests/test_droiddeck_epic.py @@ -1318,7 +1318,7 @@ class StoreLaunchClientTest(unittest.TestCase): (self.folder / ".droiddeck-store.json").write_text(json.dumps(sidecar)) with mock.patch.dict(os.environ, {"DD_LAUNCH_DIR": str(self.tmp / "launch"), "STEAM_COMPAT_DATA_PATH": ""}), \ mock.patch.dict(S, {"client_wanted": lambda: calls.append("wanted") or True, - "ask": lambda channel, request, timeout=0: calls.append(request["op"]) or {"ok": True}}): + "ask": lambda channel, request, timeout=0, claim=False: calls.append(request["op"]) or {"ok": True}}): S["main"]([str(self.folder / ".droiddeck-launch.bat")]) self.assertNotIn("wanted", calls) self.assertNotIn("epic-code", calls) @@ -1341,7 +1341,7 @@ class StoreLaunchClientTest(unittest.TestCase): with mock.patch.dict(os.environ, env), \ mock.patch.dict(S, {"client_wanted": lambda: True, "through_client": lambda folder, sidecar, timeout=0: calls.append("client") or "timeout", - "ask": lambda channel, request, timeout=0: calls.append(request["op"]) or {"ok": True, "code": True}}): + "ask": lambda channel, request, timeout=0, claim=False: calls.append(request["op"]) or {"ok": True, "code": True}}): S["main"]([str(self.folder / ".droiddeck-launch.bat")]) self.assertEqual(["client", "epic-code", "cloud-down"], calls) @@ -1356,7 +1356,7 @@ class StoreLaunchClientTest(unittest.TestCase): (self.folder / ".droiddeck-epic-code").write_text("stale") with mock.patch.dict(os.environ, {"DD_LAUNCH_DIR": str(self.tmp / "launch"), "STEAM_COMPAT_DATA_PATH": ""}), \ mock.patch.dict(S, {"client_wanted": lambda: True, "through_client": through, - "ask": lambda channel, request, timeout=0: calls.append(request["op"]) or {"ok": True}}): + "ask": lambda channel, request, timeout=0, claim=False: calls.append(request["op"]) or {"ok": True}}): S["main"]([str(self.folder / ".droiddeck-launch.bat")]) self.assertEqual(["client", "cloud-down"], calls) self.assertFalse((self.folder / ".droiddeck-epic-code").exists()) @@ -1368,7 +1368,7 @@ class StoreLaunchClientTest(unittest.TestCase): with mock.patch.dict(os.environ, {"DD_LAUNCH_DIR": str(self.tmp / "launch"), "DROIDDECK_EPIC_HANDLED": "1", "STEAM_COMPAT_DATA_PATH": ""}), \ mock.patch.dict(S, {"client_wanted": lambda: calls.append("wanted") or True, - "ask": lambda channel, request, timeout=0: calls.append(request["op"]) or {"ok": True}}): + "ask": lambda channel, request, timeout=0, claim=False: calls.append(request["op"]) or {"ok": True}}): S["main"]([str(self.folder / ".droiddeck-launch.bat")]) self.assertEqual(["cloud-down"], calls) self.assertTrue((self.folder / ".droiddeck-epic-args").exists()) diff --git a/tools/tests/test_store_launch.py b/tools/tests/test_store_launch.py index 1ce1659ea7..84ce7dcfa6 100644 --- a/tools/tests/test_store_launch.py +++ b/tools/tests/test_store_launch.py @@ -106,6 +106,62 @@ class Asking(unittest.TestCase): self.assertIsNone(store.ask(channel, {'op': 'epic-code'}, timeout=0.2)) self.assertEqual([], list((channel / 'req').iterdir())) + def test_a_cloud_download_the_launcher_stops_waiting_for_loses_its_claim(self): + with tempfile.TemporaryDirectory() as root: + channel = Path(root) / 'stores' + taken = [] + handed = threading.Event() + + def app(): + deadline = time.monotonic() + 5 + while not taken and time.monotonic() < deadline: + for request in (channel / 'req').glob('*.json') if (channel / 'req').is_dir() else []: + target = channel / 'resp' / (request.stem + '.taken') + os.rename(request, target) + taken.append(target) + handed.set() + time.sleep(0.02) + + t = threading.Thread(target=app, daemon=True) + t.start() + try: + self.assertIsNone(store.ask(channel, {'op': 'cloud-down'}, timeout=2.0, claim=True)) + finally: + t.join(5) + self.assertTrue(handed.is_set(), 'the app never took the request') + with self.assertRaises(OSError): + os.rename(taken[0], taken[0].with_suffix('.writing')) + + def test_a_claimed_cloud_download_is_waited_for(self): + with tempfile.TemporaryDirectory() as root: + channel = Path(root) / 'stores' + handed = threading.Event() + + def app(): + deadline = time.monotonic() + 5 + while time.monotonic() < deadline: + names = list((channel / 'req').glob('*.json')) if (channel / 'req').is_dir() else [] + if names: + handed.set() + writing = channel / 'resp' / (names[0].stem + '.writing') + os.rename(names[0], writing) + time.sleep(0.6) + tmp = channel / 'resp' / (names[0].name + '.tmp') + tmp.write_text(json.dumps({'ok': True, 'result': 'ok'})) + os.rename(tmp, channel / 'resp' / names[0].name) + writing.unlink() + return + time.sleep(0.02) + + t = threading.Thread(target=app, daemon=True) + t.start() + try: + answer = store.ask(channel, {'op': 'cloud-down'}, timeout=0.2, claim=True) + finally: + t.join(5) + self.assertTrue(handed.is_set(), 'the app never took the request') + self.assertEqual({'ok': True, 'result': 'ok'}, answer) + def test_a_stale_code_is_dropped_before_asking(self): with tempfile.TemporaryDirectory() as root: folder = game(root) @@ -214,7 +270,7 @@ class CloudHooks(unittest.TestCase): try: original = store.ask asked = [] - store.ask = lambda channel, request, timeout=5.0: asked.append((request, timeout)) or {'ok': True, 'files': 2, 'reason': 'ok'} + store.ask = lambda channel, request, timeout=5.0, claim=False: asked.append((request, timeout)) or {'ok': True, 'files': 2, 'reason': 'ok'} self.assertEqual(store.STORE_GAME, store.main([bat])) self.assertEqual(0, store.main(['--exited', bat])) finally: @@ -225,6 +281,55 @@ class CloudHooks(unittest.TestCase): self.assertEqual(store.CLOUD_TIMEOUT, asked[1][1]) self.assertEqual('cloud-up', asked[2][0]['op']) + def test_a_cloud_request_that_cannot_be_written_still_starts_the_game_and_uploads_on_exit(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='ubisoft', ident='space-1') + sc = json.loads((folder / '.droiddeck-store.json').read_text()); sc['extra'] = {'productId': '109'} + (folder / '.droiddeck-store.json').write_text(json.dumps(sc)) + bat = str(folder / '.droiddeck-launch.bat') + asked = [] + + def ask(channel, request, timeout=5.0, claim=False): + asked.append(request['op']) + if request['op'] in ('cloud-down', 'cloud-up'): + raise PermissionError(13, 'read-only') + return {'ok': True} + + lines = [] + with patch.dict(os.environ, {'DD_LAUNCH_DIR': str(Path(root) / 'session')}), patch.object(store, 'ask', side_effect=ask), patch.object(store, 'prepare_ubisoft', return_value='ready'), patch.object(store, 'create_prefix', return_value='present'), patch.object(store, 'log', side_effect=lines.append): + self.assertEqual(store.STORE_GAME, store.main([bat])) + self.assertEqual(0, store.main(['--exited', bat])) + self.assertEqual(['cloud-down', 'cloud-up'], [op for op in asked if op.startswith('cloud-')]) + self.assertIn('cloud ubisoft space-1 down result=failed reason=PermissionError', lines) + self.assertIn('cloud ubisoft space-1 up not requested (PermissionError)', lines) + def test_an_ea_game_with_cloud_saves_asks_after_its_preparation_and_exit(self): + ident = 'Origin.OFR.50.0002694' + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='ea', ident=ident) + bat = str(folder / '.droiddeck-launch.bat') + asked = [] + env = {'DD_LAUNCH_DIR': str(Path(root) / 'session')} + record = lambda channel, request, timeout=5.0, claim=False: asked.append(request) or {'result': 'ok'} + with patch.dict(os.environ, env), \ + patch.object(store, 'prepare_ea', side_effect=lambda *a: asked.append({'op': 'prepared'})), \ + patch.object(store, 'create_prefix', side_effect=AssertionError('EA prepares its own prefix')), \ + patch.object(store, 'ask', side_effect=record): + self.assertEqual(store.STORE_GAME, store.main([bat])) + self.assertEqual(['prepared', 'cloud-down'], [r['op'] for r in asked]) + self.assertEqual({'op': 'cloud-down', 'store': 'ea', 'id': ident}, asked[1]) + self.assertEqual(0, store.main(['--exited', bat])) + self.assertEqual(['ea-exited', 'cloud-up'], [r['op'] for r in asked[2:]]) + self.assertEqual({'op': 'cloud-up', 'store': 'ea', 'id': ident}, asked[3]) + sidecar = json.loads((folder / store.SIDECAR).read_text()) + sidecar['cloud'] = False + (folder / store.SIDECAR).write_text(json.dumps(sidecar)) + asked.clear() + with patch.dict(os.environ, env), patch.object(store, 'prepare_ea'), patch.object(store, 'ask', side_effect=record): + self.assertEqual(store.STORE_GAME, store.main([bat])) + self.assertEqual([], asked) + self.assertEqual(0, store.main(['--exited', bat])) + self.assertEqual(['ea-exited'], [r['op'] for r in asked]) + def test_a_first_launch_creates_the_prefix_before_the_cloud_download(self): with tempfile.TemporaryDirectory() as root: folder = game(root, store_id='epic', ident='fa4240e5') @@ -244,7 +349,7 @@ class CloudHooks(unittest.TestCase): env = {'DD_LAUNCH_DIR': root, 'STEAM_COMPAT_DATA_PATH': str(compat), 'DD_STORE_PROTON': str(proton)} with patch.dict(os.environ, env), patch.object(store.subprocess, 'run', side_effect=run), \ - patch.object(store, 'ask', side_effect=lambda channel, request, timeout=5.0: order.append(request) or {'result': 'ok'}): + patch.object(store, 'ask', side_effect=lambda channel, request, timeout=5.0, claim=False: order.append(request) or {'result': 'ok'}): self.assertEqual(store.STORE_GAME, store.main([str(folder / store.LAUNCHER)])) self.assertEqual([str(proton), 'run', 'cmd.exe', '/d', '/c', 'exit'], order[0]) self.assertEqual({'op': 'cloud-down', 'store': 'epic', 'id': 'fa4240e5'}, order[-1]) diff --git a/tools/tests/test_store_launch_ea.py b/tools/tests/test_store_launch_ea.py index 0f73f92f8a..1dd912f0ab 100644 --- a/tools/tests/test_store_launch_ea.py +++ b/tools/tests/test_store_launch_ea.py @@ -147,7 +147,7 @@ class EaHook(unittest.TestCase): folder.mkdir() asked = {} - def fake_ask(channel, request, timeout=5.0): + def fake_ask(channel, request, timeout=5.0, claim=False): asked.update(request) return {'ok': True, 'lsx': True, 'license': 'ok v4', 'licenses': [{'path': 'ProgramData/L/x.dlf', 'data': base64.b64encode(b'z').decode()}]} diff --git a/tools/ubisoft-launcher/src/bridge.rs b/tools/ubisoft-launcher/src/bridge.rs new file mode 100644 index 0000000000..361acb743f --- /dev/null +++ b/tools/ubisoft-launcher/src/bridge.rs @@ -0,0 +1,161 @@ +//! The file bridge DroidDeck answers Denuvo activations through. + +use std::path::Path; +use std::sync::{Condvar, Mutex}; +use std::thread; +use std::time::Duration; + +pub const INVALIDATE: &str = "denuvo-invalidate"; + +pub fn publish_invalidation(dir: &Path, account: &str, tries: u32, pause: Duration) -> bool { + let marker = dir.join(INVALIDATE); + let part = dir.join(format!("{INVALIDATE}.upc")); + for attempt in 0..tries { + if marker.is_file() { + return true; + } + if std::fs::write(&part, account.as_bytes()).is_ok() && std::fs::rename(&part, &marker).is_ok() { + return true; + } + let _ = std::fs::remove_file(&part); + if attempt + 1 < tries { + thread::sleep(pause); + } + } + false +} + +#[derive(Default)] +pub struct Generation { + state: Mutex<(u64, usize)>, + idle: Condvar, +} + +impl Generation { + pub fn current(&self) -> u64 { + self.state.lock().unwrap().0 + } + + pub fn invalidate(&self, wait: Duration, acknowledge: impl FnOnce() -> T) -> T { + let mut state = self.state.lock().unwrap(); + state.0 += 1; + let _ = self.idle.wait_timeout_while(state, wait, |s| s.1 > 0).unwrap(); + acknowledge() + } + + pub fn serve(&self, seen: u64, reply: impl FnOnce() -> T) -> Option { + { + let mut state = self.state.lock().unwrap(); + if state.0 != seen { + return None; + } + state.1 += 1; + } + let out = reply(); + self.state.lock().unwrap().1 -= 1; + self.idle.notify_all(); + Some(out) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::Arc; + use std::time::Instant; + + fn dir(name: &str) -> std::path::PathBuf { + let d = std::env::temp_dir().join(format!("upc-bridge-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + d + } + + #[test] + fn an_invalidation_is_published_as_the_marker() { + let d = dir("publish"); + assert!(publish_invalidation(&d, "user-a", 1, Duration::ZERO)); + assert_eq!(std::fs::read(d.join(INVALIDATE)).unwrap(), b"user-a"); + assert!(!d.join("denuvo-invalidate.upc").exists()); + } + + #[test] + fn a_marker_naming_its_account_is_kept() { + let d = dir("named"); + std::fs::write(d.join(INVALIDATE), b"user-a").unwrap(); + assert!(publish_invalidation(&d, "user-b", 1, Duration::ZERO)); + assert_eq!(std::fs::read(d.join(INVALIDATE)).unwrap(), b"user-a"); + } + + #[test] + fn a_marker_that_cannot_be_written_is_reported_after_every_try() { + let d = dir("missing").join("gone"); + let started = Instant::now(); + assert!(!publish_invalidation(&d, "user-a", 3, Duration::from_millis(20))); + assert!(started.elapsed() >= Duration::from_millis(40)); + assert!(!d.join(INVALIDATE).exists()); + } + + #[test] + fn a_marker_written_while_retrying_counts() { + let d = dir("late").join("bridge"); + let later = d.clone(); + let maker = thread::spawn(move || { + thread::sleep(Duration::from_millis(30)); + std::fs::create_dir_all(later).unwrap(); + }); + assert!(publish_invalidation(&d, "user-a", 50, Duration::from_millis(10))); + maker.join().unwrap(); + assert!(d.join(INVALIDATE).is_file()); + } + + #[test] + fn a_kept_token_asked_for_before_an_invalidation_is_not_served_after_it() { + let g = Generation::default(); + let seen = g.current(); + assert_eq!(g.invalidate(Duration::from_secs(1), || "acknowledged"), "acknowledged"); + assert_eq!(g.serve(seen, || "token"), None); + assert_eq!(g.serve(g.current(), || "token"), Some("token")); + } + + #[test] + fn a_reply_being_served_holds_the_acknowledgement_back() { + let g = Arc::new(Generation::default()); + let order = Arc::new(Mutex::new(Vec::new())); + let seen = g.current(); + let (g2, o2) = (g.clone(), order.clone()); + let served = g.serve(seen, || { + let ack = thread::spawn(move || g2.invalidate(Duration::from_secs(5), || o2.lock().unwrap().push("acknowledged"))); + thread::sleep(Duration::from_millis(50)); + order.lock().unwrap().push("token"); + ack + }); + served.unwrap().join().unwrap(); + assert_eq!(*order.lock().unwrap(), vec!["token", "acknowledged"]); + } + + #[test] + fn a_reply_that_stalls_never_blocks_other_connections_or_the_acknowledgement_for_long() { + let g = Arc::new(Generation::default()); + let seen = g.current(); + let (entered, inside) = std::sync::mpsc::channel(); + let (release, held) = std::sync::mpsc::channel::<()>(); + let stalled = { + let g = g.clone(); + thread::spawn(move || g.serve(seen, move || { + entered.send(()).unwrap(); + let _ = held.recv(); + })) + }; + inside.recv().unwrap(); + let started = Instant::now(); + assert_eq!(g.current(), seen); + assert_eq!(g.serve(seen, || "other"), Some("other")); + assert!(started.elapsed() < Duration::from_millis(200)); + assert_eq!(g.invalidate(Duration::from_millis(100), || "acknowledged"), "acknowledged"); + assert!(started.elapsed() < Duration::from_millis(450)); + assert_eq!(g.serve(seen, || "late"), None); + release.send(()).unwrap(); + assert!(stalled.join().unwrap().is_some()); + } +} diff --git a/tools/ubisoft-launcher/src/launcher.rs b/tools/ubisoft-launcher/src/launcher.rs index 343708dba0..d993ebe9f9 100644 --- a/tools/ubisoft-launcher/src/launcher.rs +++ b/tools/ubisoft-launcher/src/launcher.rs @@ -338,6 +338,8 @@ pub fn announced(body: &[u8]) -> Option<(Option, Option)> { pub enum AuxReq<'a> { Init(u64), Activate(u64, &'a [u8]), + Invalidate(u64), + BurnTicket(u64), Other(u64), } @@ -348,8 +350,14 @@ pub fn aux_request(body: &[u8]) -> AuxReq<'_> { let Some(activate) = pb::sub(&f, 3) else { return if pb::has(&f, 2) { AuxReq::Init(rid) } else { AuxReq::Other(rid) }; }; - let token = pb::fields(activate) - .and_then(|a| pb::sub(&a, 2).and_then(pb::fields)) + let Some(a) = pb::fields(activate) else { return AuxReq::Other(rid) }; + if pb::has(&a, 1) && !pb::has(&a, 2) { + return AuxReq::Invalidate(rid); + } + if pb::has(&a, 3) && !pb::has(&a, 2) { + return AuxReq::BurnTicket(rid); + } + let token = pb::sub(&a, 2).and_then(pb::fields) .and_then(|t| t.iter().find_map(|(n, v)| match v { Value::Bytes(b) if *n == 2 => Some(*b), _ => None })); match token { Some(t) => AuxReq::Activate(rid, t), @@ -365,6 +373,15 @@ pub fn aux_init(rid: u64, pid: u32) -> Vec { Msg::new().uint(1, rid).msg(2, init).0 } +pub fn aux_invalidated(rid: u64) -> Vec { + Msg::new().uint(1, rid).msg(3, Msg::new()).0 +} + +pub fn aux_no_burn_ticket(rid: u64) -> Vec { + let answer = Msg::new().uint(2, 3).uint(3, 3); + Msg::new().uint(1, rid).msg(3, Msg::new().msg(3, answer)).0 +} + pub fn aux_token(rid: u64, game_token: &[u8]) -> Vec { let answer = Msg::new().uint(2, 1).uint(3, 0).bytes(4, game_token).bytes(5, b""); Msg::new().uint(1, rid).msg(3, Msg::new().msg(2, answer)).0 @@ -476,6 +493,30 @@ mod tests { assert!(matches!(aux_request(&Msg::new().uint(1, 1).msg(2, Msg::new().uint(1, 109)).0), AuxReq::Init(1))); } + #[test] + fn an_invalidation_is_recognised_and_acknowledged_with_an_empty_activation() { + let req = Msg::new().uint(1, 5).msg(3, Msg::new().msg(1, Msg::new())); + assert!(matches!(aux_request(&req.0), AuxReq::Invalidate(5))); + let rsp = pb::fields(&aux_invalidated(5)).unwrap().into_iter().map(|(n, _)| n).collect::>(); + assert_eq!(rsp, vec![1, 3]); + assert_eq!(pb::sub(&pb::fields(&aux_invalidated(5)).unwrap(), 3), Some(&b""[..])); + let burn = Msg::new().uint(1, 6).msg(3, Msg::new().msg(3, Msg::new().bytes(2, b"t"))); + assert!(matches!(aux_request(&burn.0), AuxReq::BurnTicket(6))); + } + + #[test] + fn a_burn_ticket_request_is_answered_with_a_connection_error() { + let rsp = aux_no_burn_ticket(6); + let top = pb::fields(&rsp).unwrap(); + assert_eq!(pb::num(&top, 1), Some(6)); + let activate = pb::fields(pb::sub(&top, 3).unwrap()).unwrap(); + assert!(!pb::has(&activate, 2)); + let burn = pb::fields(pb::sub(&activate, 3).unwrap()).unwrap(); + assert_eq!(pb::num(&burn, 2), Some(3)); + assert_eq!(pb::num(&burn, 3), Some(3)); + assert!(!pb::has(&burn, 4)); + } + #[test] fn orbit_init_answers_with_the_save_folder() { let (rsp, what) = config().orbit(&[0x08, 0x01, 0x62, 0x03, 0x08, 0xcd, 0x01]).unwrap(); diff --git a/tools/ubisoft-launcher/src/main.rs b/tools/ubisoft-launcher/src/main.rs index 0b7126a919..e4442d7033 100644 --- a/tools/ubisoft-launcher/src/main.rs +++ b/tools/ubisoft-launcher/src/main.rs @@ -4,6 +4,7 @@ #![cfg_attr(windows, windows_subsystem = "windows")] +mod bridge; mod launcher; mod pb; #[cfg(windows)] @@ -22,12 +23,13 @@ fn main() { #[cfg(windows)] mod service { + use crate::bridge::{self, Generation}; use crate::launcher::{self, AuxReq, Config, SessionReq}; use crate::win::{self, Handle}; use std::fs::OpenOptions; use std::io::Write; - use std::path::PathBuf; - use std::sync::atomic::{AtomicU32, Ordering}; + use std::path::{Path, PathBuf}; + use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; use std::sync::{Arc, Mutex}; use std::thread; use std::time::{Duration, Instant}; @@ -36,6 +38,9 @@ mod service { const ANNOUNCE_WAIT: Duration = Duration::from_secs(120); const RESTART_WAIT: Duration = Duration::from_secs(15); const DENUVO_WAIT: Duration = Duration::from_secs(90); + const SERVE_WAIT: Duration = Duration::from_secs(10); + const MARKER_TRIES: u32 = 25; + const MARKER_PAUSE: Duration = Duration::from_millis(200); struct State { cfg: Config, @@ -43,6 +48,8 @@ mod service { log: Mutex>, event: Handle, game: AtomicU32, + stale_token: AtomicBool, + generation: Generation, started: Instant, } @@ -175,7 +182,8 @@ mod service { state.say("session: closed"); } - fn denuvo(state: &State, rid: u64, challenge: &[u8]) -> Option> { + /// The game token for an activation, with the invalidation count it was asked under when it may be a kept one. + fn denuvo(state: &State, rid: u64, challenge: &[u8], fresh: bool) -> Option<(Vec, Option)> { if state.cfg.bridge.is_empty() { return None; } @@ -185,12 +193,20 @@ mod service { let _ = std::fs::remove_file(&rsp); let part = dir.join(format!("denuvo-{rid}.req.part")); std::fs::write(&part, challenge).ok()?; - std::fs::rename(&part, dir.join(format!("denuvo-{rid}.req"))).ok()?; + let seen = state.generation.current(); + let stale = state.stale_token.swap(false, Ordering::SeqCst) || fresh; + let kind = if stale { "req" } else { "cache.req" }; + if std::fs::rename(&part, dir.join(format!("denuvo-{rid}.{kind}"))).is_err() { + if stale { + state.stale_token.store(true, Ordering::SeqCst); + } + return None; + } let deadline = Instant::now() + DENUVO_WAIT; while Instant::now() < deadline { if let Ok(token) = std::fs::read(&rsp) { let _ = std::fs::remove_file(&rsp); - return (!token.is_empty()).then_some(token); + return (!token.is_empty()).then_some((token, (!stale).then_some(seen))); } thread::sleep(Duration::from_millis(200)); } @@ -207,14 +223,48 @@ mod service { } AuxReq::Activate(rid, challenge) => { state.say(&format!("aux: activation {rid} with a {} B challenge", challenge.len())); - match denuvo(state, rid, challenge) { - Some(token) => { - send(&pipe, &launcher::aux_token(rid, &token)); + let mut fresh = false; + loop { + let Some((token, seen)) = denuvo(state, rid, challenge, fresh) else { + state.say(&format!("aux: no game token for activation {rid}")); + break; + }; + let answer = || send(&pipe, &launcher::aux_token(rid, &token)); + let served = match seen { + Some(g) => state.generation.serve(g, answer).is_some(), + None => { + answer(); + true + } + }; + if served { state.say(&format!("aux: activation {rid} answered ({} B)", token.len())); + break; } - None => state.say(&format!("aux: no game token for activation {rid}")), + state.say(&format!("aux: kept token for activation {rid} came back after an invalidation; asking afresh")); + fresh = true; } } + AuxReq::Invalidate(rid) => { + state.stale_token.store(true, Ordering::SeqCst); + let published = state.generation.invalidate(SERVE_WAIT, || { + let published = state.cfg.bridge.is_empty() + || bridge::publish_invalidation(Path::new(&state.cfg.bridge), &state.cfg.user_id, MARKER_TRIES, MARKER_PAUSE); + if published { + send(&pipe, &launcher::aux_invalidated(rid)); + } + published + }); + if published { + state.say(&format!("aux: token invalidated by request {rid}")); + } else { + state.say(&format!("aux: invalidation {rid} not acknowledged; the kept token could not be marked stale")); + } + } + AuxReq::BurnTicket(rid) => { + send(&pipe, &launcher::aux_no_burn_ticket(rid)); + state.say(&format!("aux: burn ticket {rid} answered as unavailable")); + } AuxReq::Other(rid) => state.say(&format!("aux: request {rid} not handled ({} B)", req.len())), } } @@ -304,6 +354,8 @@ mod service { log: Mutex::new(log), event: win::inheritable_event(), game: AtomicU32::new(0), + stale_token: AtomicBool::new(false), + generation: Generation::default(), started: Instant::now(), }); state.say(&format!("product {} on {}*", state.cfg.product, state.prefix));