Run Windows games on Apple Silicon — free, open source, with an open compatibility database that tells you what actually works.
Swift apple-silicond3dmetaldxmtdxvkfexgame-porting-toolkitgamingmac-gamingmacosmetalmoltenvksteamswiftuivkd3d-protonwhisky-alternativewindows-gameswinewine-wrapper
GOG saves: a shared name prefix, a game with no client id and an old cloud file no longer trip the copies
Gabe42b75e4348ac
diff --git a/Sources/HighballApp/AppState.swift b/Sources/HighballApp/AppState.swiftindex 43e818c..339ce12 100644--- a/Sources/HighballApp/AppState.swift+++ b/Sources/HighballApp/AppState.swift@@ -3731,12 +3731,9 @@ final class AppState {try await gogRunSetup(game, title: item.title, in: bottle, language: GOGStore.language(for: info?.languages ?? []))}let saveFolders = useSaves ? (await gogSaveLocations(game, in: bottle) ?? []) : []- var inStep = Set<String>() // locations whose saves came down: their upload needs no copy of the cloud's firstfor (location, folder) in saveFolders {stage = L("Bringing your saves down from GOG")- if try await gogSync(game, title: item.title, location: location, folder: folder, direction: .download, beforePlay: true) {- inStep.insert(location.name)- }+ try await gogSync(game, title: item.title, location: location, folder: folder, direction: .download, beforePlay: true)}// Stop during the steps before the game (setup, saves) ends the launch there: the// strip already reads "Stopped", so nothing may start after it.@@ -3816,10 +3813,9 @@ final class AppState {sessionEndActions[session.id] = { [weak self] _ inGOGComet(store: store).stop(comet)guard let self, !saveFolders.isEmpty else { return }- Task { @MainActor [inStep] in+ Task { @MainActor infor (location, folder) in saveFolders {- _ = try? await self.gogSync(game, title: item.title, location: location, folder: folder, direction: .upload,- beforePlay: false, cloudInStep: inStep.contains(location.name))+ try? await self.gogSync(game, title: item.title, location: location, folder: folder, direction: .upload, beforePlay: false)}}}@@ -3873,7 +3869,7 @@ final class AppState {let running = comet.isRunning // read before the log, so the last lines are never missedlet unlocks = reader.newUnlocks().filter { shown.insert($0.key).inserted }if !unlocks.isEmpty {- if clientID == nil { clientID = GOGSaves(store: store).clientID(for: game) }+ if clientID == nil { clientID = try? GOGSaves(store: store).clientID(for: game) }let database = clientID.flatMap { $0 }.flatMap { GOGComet(store: store).achievementDatabase(clientID: $0) }for unlock in unlocks {let achievement = GOGAchievements.details(key: unlock.key, name: unlock.name, database: database)@@ -3924,18 +3920,12 @@ final class AppState {}/// The game's cloud-save folders in this environment: empty for a game GOG keeps no saves- /// for, nil when GOG could not be asked (offline, no client id), as against none: a game without- /// cloud saves.+ /// for (no client id, or cloud saves off), nil when GOG could not be asked (offline).private func gogSaveLocations(_ game: GOGStore.InstalledGame, in bottle: Bottle) async -> [(GOGSaves.Location, URL)]? {let saves = GOGSaves(store: gogStore)// Both can reach GOG (an offline install's client id comes from its build manifest).- // Kept apart: GOG unreachable (nil) and a game GOG keeps no saves for (.some(nil)).- let asked = await Task.detached { () -> [GOGSaves.Location]?? in- guard let client = saves.clientID(for: game) else { return nil }- do { return .some(try saves.locations(clientID: client)) } catch { return nil }- }.value- guard let answer = asked else { return nil }- return (answer ?? []).compactMap { location in+ guard let locations = await Task.detached(operation: { try? saves.cloudLocations(for: game) }).value else { return nil }+ return locations.compactMap { location inguard let folder = GOGSaves.resolve(location.template, game: game, driveC: bottle.driveC) else {appendLog("\(game.title): cloud saves at \(location.template) have no folder here, skipping that one")return nil@@ -3954,23 +3944,20 @@ final class AppState {/// One sync, asking the player on a conflict. Before Play the launch waits for the answer;/// after the game it is asked whenever the player is back.- /// Whether the Mac's saves and the cloud's ended up in step.- @discardableResultprivate func gogSync(_ game: GOGStore.InstalledGame, title: String, location: GOGSaves.Location, folder: URL,- direction: GOGSaves.Direction, beforePlay: Bool, cloudInStep: Bool = false) async throws -> Bool {+ direction: GOGSaves.Direction, beforePlay: Bool) async throws {let saves = GOGSaves(store: gogStore)- let outcome = await Task.detached { saves.sync(game, location: location, folder: folder, direction: direction, cloudInStep: cloudInStep) { line in+ let outcome = await Task.detached { saves.sync(game, location: location, folder: folder, direction: direction) { line inTask { @MainActor in self.appendLog(line) }} }.valueswitch outcome {case .synced:- return true+ returncase .refused:// Newer saves on the other side; the next sync in that direction takes them.- return false+ returncase let .failed(why):appendLog("\(title): cloud saves did not sync (\(why)); \(beforePlay ? "playing with the saves on this Mac" : "they stay on this Mac until the next sync")")- return falsecase .conflict:let choice: GOGSaveConflict.Choice = await withCheckedContinuation { continuation in// One question at a time: an earlier one still open (another game's) is answered@@ -3987,7 +3974,7 @@ final class AppState {case .keepMac: forced = .forceUploadcase .skip:appendLog("\(title): cloud saves left as they are this time")- return false+ return}let again = await Task.detached { saves.sync(game, location: location, folder: folder, direction: forced) }.valueswitch again {@@ -3995,7 +3982,6 @@ final class AppState {case let .failed(why): appendLog("\(title): cloud saves did not sync (\(why))")case .conflict, .refused: appendLog("\(title): cloud saves did not sync")}- return again == .synced}}@@ -4024,9 +4010,10 @@ final class AppState {runBusy(String(format: L("Removing %@"), title),done: DoneState(title: String(format: L("%@ removed"), title), ctaTitle: nil, cta: nil),stop: .killBottle(bottle, label: L("Stop"))) { [self] in- // Saves the game keeps inside its own folder go with the folder: copied first, before- // anything runs, and a failed copy stops the removal. Without GOG's answer of where- // the saves are, nothing is removed.+ // GOG's cloud-save locations inside the game's own folder go with the folder: copied+ // first, before anything runs, and a failed copy stops the removal. Without GOG's+ // answer of where the saves are, nothing is removed; a game with no client id or with+ // cloud saves off has none to copy. Nothing else in the folder is copied.guard let locations = await gogSaveLocations(game, in: bottle) else {throw HighballError.failed(String(format: L("Highball could not ask GOG where %@ keeps its saves, so it left the game in place. Try again once GOG can be reached."), title))}diff --git a/Sources/HighballKit/GOGSaves.swift b/Sources/HighballKit/GOGSaves.swiftindex 6c20c6c..c034569 100644--- a/Sources/HighballKit/GOGSaves.swift+++ b/Sources/HighballKit/GOGSaves.swift@@ -17,12 +17,20 @@ public struct GOGSaves: Sendable {public let name: String/// GOG's template, e.g. "<?SAVED_GAMES?>/Remedy/Control".public let template: String+ /// Another of the game's locations has a name that starts with this one's (saves and+ /// saves_profiles). gogdl picks a location's cloud files by that prefix alone (saves.py,+ /// is_in_our_dir), so it also lists the other one's, asks for them under this name, and+ /// logs "Downloading file failed" for each, naming no file: here that line says nothing+ /// about this location's own files.+ public var sharesItsPrefix = false}/// The client id cloud storage is keyed by: the info file's, else the manifest gogdl kept/// (Control's info file has none; its manifest does), else GOG's latest build manifest for an/// install with neither (an offline installer, a gogdl install from elsewhere), remembered.- public func clientID(for game: GOGStore.InstalledGame) -> String? {+ /// Nil when the game has none: GOG's own manifest names none (Beyond Good & Evil's), and gogdl+ /// cannot sync such a game either. Throws when GOG could not be asked, which says nothing.+ public func clientID(for game: GOGStore.InstalledGame) throws -> String? {if let id = game.clientID, !id.isEmpty { return id }if let data = try? Data(contentsOf: store.installManifest(game)),let obj = try? JSONSerialization.jsonObject(with: data) as? [String: Any],@@ -30,26 +38,51 @@ public struct GOGSaves: Sendable {let cacheFile = store.dir.appending(path: "client-ids.json")var cache = (try? JSONDecoder().decode([String: String].self, from: Data(contentsOf: cacheFile))) ?? [:]if let id = cache[game.id] { return id }- guard let id = try? Self.publishedClientID(productID: game.id) else { return nil }+ guard let id = try Self.publishedClientID(productID: game.id) else { return nil }cache[game.id] = idtry? JSONEncoder().encode(cache).write(to: cacheFile, options: .atomic)return id}- /// The client id in the newest Windows build's manifest. The build list and manifests are- /// public; a v2 manifest is zlib-compressed JSON.+ /// The client id in the newest Windows build's manifest, nil when GOG has none for the game+ /// (no Windows build, or a manifest that names none). The build list and manifests are public;+ /// a v2 manifest is zlib-compressed JSON. Throws when either cannot be fetched or read.static func publishedClientID(productID: String) throws -> String? {let list = URL(string: "https://content-system.gog.com/products/\(productID)/os/windows/builds?generation=2")!- guard let builds = try JSONSerialization.jsonObject(with: GOGStore.fetch(URLRequest(url: list))) as? [String: Any],- let link = ((builds["items"] as? [[String: Any]])?.first?["link"] as? String).flatMap(URL.init(string:)) else { return nil }- return clientID(inManifest: try GOGStore.fetch(URLRequest(url: link)))+ guard let link = try newestBuild(inBuildList: GOGStore.fetch(URLRequest(url: list))) else { return nil }+ return try clientID(inPublishedManifest: GOGStore.fetch(URLRequest(url: link)))}- static func clientID(inManifest data: Data) -> String? {+ /// The newest build's manifest address, nil for a game with no build for Windows.+ static func newestBuild(inBuildList data: Data) throws -> URL? {+ guard let builds = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any],+ let items = builds["items"] as? [[String: Any]] else { throw HighballError.invalid("GOG's build list could not be read") }+ guard let newest = items.first else { return nil }+ guard let link = (newest["link"] as? String).flatMap(URL.init(string:)) else { throw HighballError.invalid("GOG's build list names no manifest") }+ return link+ }++ /// A manifest GOG served: its client id, nil when it names none. Throws when it cannot be read,+ /// as that is no answer.+ static func clientID(inPublishedManifest data: Data) throws -> String? {+ guard let obj = manifestObject(data) else { throw HighballError.invalid("GOG's build manifest could not be read") }+ return obj["clientId"] as? String+ }++ static func clientID(inManifest data: Data) -> String? { manifestObject(data)?["clientId"] as? String }++ private static func manifestObject(_ data: Data) -> [String: Any]? {// zlib = a 2-byte header, raw DEFLATE, then a checksum; Foundation's .zlib reads raw DEFLATE.let json = data.first == 0x78 ? (try? (data.dropFirst(2) as NSData).decompressed(using: .zlib) as Data) : dataguard let json, let obj = try? JSONSerialization.jsonObject(with: json) as? [String: Any] else { return nil }- return obj["clientId"] as? String+ return obj+ }++ /// Where the game keeps its cloud saves: none for a game with no client id or with cloud saves+ /// off, both answers from GOG. Throws when GOG could not be asked: then nobody can say.+ public func cloudLocations(for game: GOGStore.InstalledGame) throws -> [Location] {+ guard let client = try clientID(for: game) else { return [] }+ return try locations(clientID: client) ?? []}/// Where the game keeps its saves, nil when it has no cloud saves. An empty list from GOG means@@ -64,10 +97,14 @@ public struct GOGSaves: Sendable {guard let obj = try? JSONSerialization.jsonObject(with: data) as? [String: Any],let windows = (obj["content"] as? [String: Any])?["Windows"] as? [String: Any],let cloud = windows["cloudStorage"] as? [String: Any], cloud["enabled"] as? Bool == true else { return nil }- let list = (cloud["locations"] as? [[String: Any]] ?? []).compactMap { l -> Location? in+ let named = (cloud["locations"] as? [[String: Any]] ?? []).compactMap { l -> Location? inguard let name = l["name"] as? String, let location = l["location"] as? String else { return nil }return Location(name: name, template: location)}+ let list = named.map { l in+ Location(name: l.name, template: l.template,+ sharesItsPrefix: named.contains { $0.name != l.name && $0.name.hasPrefix(l.name) })+ }return list.isEmpty? [Location(name: "__default", template: "<?APPLICATION_DATA_LOCAL?>/GOG.com/Galaxy/Applications/\(clientID)/Storage/Shared/Files")]: list@@ -204,7 +241,7 @@ public struct GOGSaves: Sendable {let failed = OSAllocatedUnfairLock(initialState: false)let status = try store.withAuthFile {try store.runStreaming(Self.arguments(folder: partial, id: game.id, location: location, timestamp: "0", direction: .forceDownload)) { line in- if Self.isFailure(line) { failed.withLock { $0 = true } }+ if Self.isFailure(line, location: location) { failed.withLock { $0 = true } }}}guard status == 0, !failed.withLock({ $0 }) else {@@ -213,8 +250,14 @@ public struct GOGSaves: Sendable {}}- /// gogdl's lines for a file it could not move, or an error it logs and carries on past.- static func isFailure(_ line: String) -> Bool { line.contains("Downloading file failed") || line.contains(" ERROR: ") }+ /// gogdl's lines for a file it could not move, or an error it logs and carries on past. Its+ /// "Downloading file failed" names no file, so for a location whose name another one's starts+ /// with it may be one of the other location's files, asked for at the wrong path, and does+ /// not count (the other location's own sync fetches those).+ static func isFailure(_ line: String, location: Location) -> Bool {+ if line.contains("Downloading file failed") { return !location.sharesItsPrefix }+ return line.contains(" ERROR: ")+ }/// Fills a backup under a ".partial" name and gives it its own name only once complete, so a/// failed copy never sits among the real ones; then keeps the newest `keep`, never this one.@@ -297,20 +340,18 @@ public struct GOGSaves: Sendable {static func isRefusal(_ line: String) -> Bool { line.contains("Refused to") }/// Syncs one location. The new timestamp is kept only when something was decided.- /// `cloudInStep`: this location's saves came down from the cloud before the session, so an- /// upload after it needs no copy of the cloud's first.public func sync(_ game: GOGStore.InstalledGame, location: Location, folder: URL, direction: Direction,- cloudInStep: Bool = false, onLine: (@Sendable (String) -> Void)? = nil) -> Outcome {+ onLine: (@Sendable (String) -> Void)? = nil) -> Outcome {try? FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)let ts = timestamps()[key(game, location)] ?? "0"// Whatever a sync could replace or delete is copied first, and a failed copy stops it. An- // upload after a session whose saves came down first needs none: the cloud then held- // nothing the Mac did not, so it only takes what the game changed.+ // upload copies the cloud even after a session whose saves came down first: a download+ // brings only cloud files newer than the last sync, so one older than it and missing on+ // the Mac stays in the cloud alone, and the upload deletes it.do {switch direction {case .download, .forceDownload: try backUpLocal(game, location: location, folder: folder)- case .forceUpload: try backUpCloud(game, location: location)- case .upload: if !cloudInStep { try backUpCloud(game, location: location) }+ case .upload, .forceUpload: try backUpCloud(game, location: location)}} catch {return .failed("the saves could not be backed up first (\(error.localizedDescription)), so nothing was synced")@@ -325,7 +366,7 @@ public struct GOGSaves: Sendable {status = try store.withAuthFile { try store.runStreaming(Self.arguments(folder: folder, id: game.id, location: location, timestamp: ts, direction: direction)) { line inif Self.isConflict(line) { conflict.withLock { $0 = true } }if Self.isRefusal(line) { refused.withLock { $0 = true } }- if Self.isFailure(line) { failed.withLock { $0 = true } }+ if Self.isFailure(line, location: location) { failed.withLock { $0 = true } }if let (value, rest) = Self.timestamp(in: line) {stdout.withLock { $0 = value }if !rest.isEmpty { onLine?(rest) }diff --git a/Sources/HighballKit/Uninstall.swift b/Sources/HighballKit/Uninstall.swiftindex c33c58f..740aa88 100644--- a/Sources/HighballKit/Uninstall.swift+++ b/Sources/HighballKit/Uninstall.swift@@ -67,7 +67,7 @@ public enum Uninstall {case .epic:return "The Epic tools do the uninstalling, so their library stays right.\(size)"case .gog:- return "Highball deletes the game's folder from this environment. Saves synced to GOG's cloud stay there, saves the game keeps inside its own folder are copied to Highball's save-backups first, and the game stays in your GOG library to install again.\(size)"+ return "Highball deletes the game's folder from this environment. Saves synced to GOG's cloud stay there, and any of those the game keeps inside its own folder are copied to Highball's save-backups first. Other saves in the game's folder, ones GOG does not sync, are deleted with it. The game stays in your GOG library to install again.\(size)"case .windowsUninstaller:return "Highball opens Windows' Add or Remove Programs in this environment, where you pick it from the list. A game with no uninstaller of its own will not be in that list, and then the only way to remove it is to delete the environment."case let .none(reason):diff --git a/Sources/highball/main.swift b/Sources/highball/main.swiftindex 76f3db8..745051c 100644--- a/Sources/highball/main.swift+++ b/Sources/highball/main.swift@@ -837,14 +837,9 @@ struct GOG: AsyncParsableCommand {let saves = GOGSaves(store: store)let settings = GOGSettings.load(store) // the game page's choices hold here toovar folders: [(GOGSaves.Location, URL)] = []- var inStep = Set<String>() // locations whose saves came down: their upload needs no copy of the cloud's first- if !noSaves, !settings.cloudSavesOff.contains(id), store.isAuthenticated, let client = saves.clientID(for: game), let locations = try? saves.locations(clientID: client) {+ if !noSaves, !settings.cloudSavesOff.contains(id), store.isAuthenticated, let locations = try? saves.cloudLocations(for: game) {folders = locations.compactMap { l in GOGSaves.resolve(l.template, game: game, driveC: b.driveC).map { (l, $0) } }- for (l, f) in folders {- let outcome = saves.sync(game, location: l, folder: f, direction: .download)- if outcome == .synced { inStep.insert(l.name) }- print("saves \(l.name) down: \(Self.describe(outcome))")- }+ for (l, f) in folders { print("saves \(l.name) down: \(Self.describe(saves.sync(game, location: l, folder: f, direction: .download)))") }}var comet: Process?if !noComet, settings.cometOff[id] == nil, store.isAuthenticated {@@ -878,10 +873,7 @@ struct GOG: AsyncParsableCommand {}}GOGComet(store: store).stop(comet)- for (l, f) in folders {- let outcome = saves.sync(game, location: l, folder: f, direction: .upload, cloudInStep: inStep.contains(l.name))- print("saves \(l.name) up: \(Self.describe(outcome))")- }+ for (l, f) in folders { print("saves \(l.name) up: \(Self.describe(saves.sync(game, location: l, folder: f, direction: .upload)))") }}static func describe(_ outcome: GOGSaves.Outcome) -> String {diff --git a/Tests/HighballKitTests/GOGSaveSafetyTests.swift b/Tests/HighballKitTests/GOGSaveSafetyTests.swiftindex da5fe6f..0bdefc0 100644--- a/Tests/HighballKitTests/GOGSaveSafetyTests.swift+++ b/Tests/HighballKitTests/GOGSaveSafetyTests.swift@@ -79,20 +79,23 @@ final class GOGSaveSafetyTests: XCTestCase {XCTAssertEqual(saves.backupsFolder(a, location), saves.backupsFolder(a, location))}- /// gogdl exits 0 when a file failed to come down; its log line fails the cloud copy instead.+ /// gogdl exits 0 when a file failed to come down; its log line fails the cloud copy instead,+ /// except for a location whose name another one's starts with: gogdl lists the other's files+ /// for it too and fails each at the wrong path, in a line that names no file.func testAFileGogdlFailedToFetchFailsTheCopy() {- XCTAssertTrue(GOGSaves.isFailure("[SAVES] ERROR: Downloading file failed"))- XCTAssertFalse(GOGSaves.isFailure("[SAVES] INFO: Files in cloud: 3"))+ let own = GOGSaves.Location(name: "saves_profiles", template: "")+ XCTAssertTrue(GOGSaves.isFailure("[SAVES] ERROR: Downloading file failed", location: own))+ XCTAssertFalse(GOGSaves.isFailure("[SAVES] INFO: Files in cloud: 3", location: own))+ let prefix = GOGSaves.Location(name: "saves", template: "", sharesItsPrefix: true)+ XCTAssertFalse(GOGSaves.isFailure("[SAVES] ERROR: Downloading file failed", location: prefix))+ XCTAssertTrue(GOGSaves.isFailure("[SAVES] ERROR: There was an error uploading a file ", location: prefix))}- /// An upload copies GOG's cloud first unless the saves came down before the session: with- /// gogdl absent here, the copy is what fails in the first case, and the sync in the second.- func testAnUploadCopiesTheCloudFirstUnlessTheSavesCameDown() throws {+ /// An upload always copies GOG's cloud first: with gogdl absent here, the copy is what fails.+ func testAnUploadCopiesTheCloudFirst() throws {let (saves, game, location, folder) = try fixture()- guard case let .failed(first) = saves.sync(game, location: location, folder: folder, direction: .upload) else { return XCTFail() }- XCTAssertTrue(first.contains("backed up"))- guard case let .failed(second) = saves.sync(game, location: location, folder: folder, direction: .upload, cloudInStep: true) else { return XCTFail() }- XCTAssertFalse(second.contains("backed up"))+ guard case let .failed(why) = saves.sync(game, location: location, folder: folder, direction: .upload) else { return XCTFail() }+ XCTAssertTrue(why.contains("backed up"))}/// A save folder years old: its copy is dated now, so the newest copies are still the ones kept.@@ -110,4 +113,165 @@ final class GOGSaveSafetyTests: XCTestCase {XCTAssertTrue(GOGSaves.isRefusal("[SAVES] WARNING: Refused to download files, newer files locally"))XCTAssertFalse(GOGSaves.isRefusal("[SAVES] INFO: Done"))}++ // MARK: Against a stand-in gogdl++ /// A stand-in for gogdl at the path GOGStore runs, keeping its cloud in a folder: listing by+ /// name prefix, downloads of newer cloud files only, an upload deleting cloud files the Mac+ /// lacks, refusals, conflicts, a timestamp glued to the next line, as gogdl 1.3.1 does.+ private func fakeGogdl(for saves: GOGSaves) throws -> URL {+ let cloud = root.appending(path: "cloud", directoryHint: .isDirectory)+ let binary = saves.store.gogdlBinary+ try FileManager.default.createDirectory(at: binary.deletingLastPathComponent(), withIntermediateDirectories: true)+ try FileManager.default.createDirectory(at: cloud, withIntermediateDirectories: true)+ try #"""+ #!/bin/sh+ # gogdl 1.3.1's save-sync (gogdl/saves.py), with a folder standing in for GOG's cloud: a cloud+ # file's modification date is its last_modified.+ cloud="\#(cloud.path)"+ [ "$1" = --auth-config-path ] && shift 2+ [ "$1" = save-sync ] || exit 2+ dir="$2"; ts=0; tsArg=0; name=__default; prefered=""+ shift 3+ while [ $# -gt 0 ]; do+ case "$1" in+ --ts) tsArg="$2"; ts="${2%%.*}"; shift 2 ;;+ --name) name="$2"; shift 2 ;;+ --os) shift 2 ;;+ --skip-download) prefered=upload; shift ;;+ --skip-upload) prefered=download; shift ;;+ --force-upload) prefered=forceupload; shift ;;+ --force-download) prefered=forcedownload; shift ;;+ *) shift ;;+ esac+ done+ mkdir -p "$dir" "$cloud"+ mtime() { stat -c %Y "$1" 2>/dev/null || stat -f %m "$1"; }+ localFiles() { (cd "$dir" && find . -type f | sed 's|^\./||'); }+ # is_in_our_dir: startswith(name), so "saves" lists "saves_profiles/..." too.+ cloudNames() { (cd "$cloud" && find . -type f | sed 's|^\./||' | while IFS= read -r n; do case "$n" in "$name"*) echo "$n" ;; esac; done); }+ # name.replace(f"{dirname}/", "", 1): the first occurrence, wherever it is.+ rel() { case "$1" in *"$name/"*) echo "${1%%"$name/"*}${1#*"$name/"}" ;; *) echo "$1" ;; esac; }+ # GOG answers an error for a path it has no file at; a file named *.unserved stands for one it+ # fails to serve.+ download() {+ case "$1" in *.unserved) echo "[SAVES] ERROR: Downloading file failed"; return ;; esac+ [ -f "$cloud/$name/$1" ] || { echo "[SAVES] ERROR: Downloading file failed"; return; }+ mkdir -p "$(dirname "$dir/$1")"; cp -p "$cloud/$name/$1" "$dir/$1"+ }+ upload() { mkdir -p "$(dirname "$cloud/$name/$1")"; cp -p "$dir/$1" "$cloud/$name/$1"; }+ lines() { printf '%s\n' "$1" | while IFS= read -r r; do [ -n "$r" ] && echo "$r"; done; }+ finish() { printf '%s' "$(date +%s)"; echo "[SAVES] INFO: Done"; exit 0; }+ localList=$(localFiles); remote=$(cloudNames)+ echo "[SAVES] INFO: Local files: $(lines "$localList" | wc -l | tr -d ' ')"+ if [ -n "$localList" ] && [ -z "$remote" ]; then+ echo "[SAVES] INFO: No files in cloud, uploading"+ lines "$localList" | while IFS= read -r r; do upload "$r"; done; finish+ elif [ -z "$localList" ] && [ -n "$remote" ]; then+ echo "[SAVES] INFO: No files locally, downloading"+ lines "$remote" | while IFS= read -r n; do download "$(rel "$n")"; done; finish+ fi+ newerLocal=$(lines "$localList" | while IFS= read -r r; do [ "$(mtime "$dir/$r")" -gt "$ts" ] && echo "$r"; done)+ newerCloud=$(lines "$remote" | while IFS= read -r n; do [ "$(mtime "$cloud/$n")" -gt "$ts" ] && rel "$n"; done)+ if [ -z "$newerLocal" ] && [ -n "$newerCloud" ]; then action=download+ elif [ -n "$newerLocal" ] && [ -z "$newerCloud" ]; then action=upload+ elif [ -z "$newerLocal" ] && [ -z "$newerCloud" ]; then action=none+ else action=conflict; fi+ case "$prefered" in+ forceupload) echo "[SAVES] WARNING: Forcing upload"; newerLocal="$localList"; action=upload ;;+ forcedownload) echo "[SAVES] WARNING: Forcing download"; newerCloud=$(lines "$remote" | while IFS= read -r n; do rel "$n"; done); action=download ;;+ upload) [ "$action" = download ] && { echo "[SAVES] WARNING: Refused to upload files, newer files in the cloud"; echo "$tsArg"; exit 0; } ;;+ download) [ "$action" = upload ] && { echo "[SAVES] WARNING: Refused to download files, newer files locally"; echo "$tsArg"; exit 0; } ;;+ esac+ case "$action" in+ upload)+ lines "$newerLocal" | while IFS= read -r r; do upload "$r"; done+ lines "$remote" | while IFS= read -r n; do r=$(rel "$n"); [ -f "$dir/$r" ] || { echo "[SAVES] INFO: DELETING IN CLOUD $r"; rm -f "$cloud/$name/$r"; }; done ;;+ download)+ lines "$newerCloud" | while IFS= read -r r; do download "$r"; done+ lines "$localList" | while IFS= read -r r; do [ -f "$cloud/$name/$r" ] || { echo "[SAVES] INFO: DELETING LOCALLY $r"; rm -f "$dir/$r"; }; done ;;+ conflict) echo "[SAVES] WARNING: Files in conflict force downloading or uploading of files" ;;+ none) echo "[SAVES] INFO: Nothing to do" ;;+ esac+ finish+ """#.write(to: binary, atomically: true, encoding: .utf8)+ try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: binary.path)+ return cloud+ }++ private func write(_ text: String, to file: URL, at date: Date) throws {+ try FileManager.default.createDirectory(at: file.deletingLastPathComponent(), withIntermediateDirectories: true)+ try Data(text.utf8).write(to: file)+ try FileManager.default.setAttributes([.modificationDate: date], ofItemAtPath: file.path)+ }++ private func setLastSync(_ saves: GOGSaves, _ game: GOGStore.InstalledGame, _ location: GOGSaves.Location, _ date: Date) throws {+ var all = saves.timestamps()+ all[saves.key(game, location)] = String(Int(date.timeIntervalSince1970))+ try FileManager.default.createDirectory(at: saves.timestampsFile.deletingLastPathComponent(), withIntermediateDirectories: true)+ try JSONEncoder().encode(all).write(to: saves.timestampsFile)+ }++ /// Owner review on #589: with `saves` and `saves_profiles` (Crysis's layout), gogdl lists+ /// saves_profiles' files for `saves` too and fails each at the wrong path. That fails neither+ /// `saves`' sync nor its cloud copy, so the timestamp is kept and the next sync does not wedge;+ /// a file that really fails to come down still fails a location that shares no prefix.+ func testTwoLocationsSharingAPrefixSyncAgainstGogdl() throws {+ let (saves, game, _, folder) = try fixture()+ let cloud = try fakeGogdl(for: saves)+ let json = #"{"content":{"Windows":{"cloudStorage":{"enabled":true,"locations":[{"name":"saves","location":"<?INSTALL?>/saves"},{"name":"saves_profiles","location":"<?INSTALL?>/saves_profiles"}]}}}}"#+ let locations = try XCTUnwrap(GOGSaves.parseLocations(Data(json.utf8), clientID: "c"))+ XCTAssertEqual(locations.map(\.sharesItsPrefix), [true, false])+ let (ownSaves, profiles) = (locations[0], locations[1])+ let profilesFolder = root.appending(path: "Game/saves_profiles")+ let now = Date()+ try FileManager.default.setAttributes([.modificationDate: now.addingTimeInterval(-3000)], ofItemAtPath: folder.appending(path: "slot1/save.dat").path)+ try write("from the cloud", to: cloud.appending(path: "saves/slot1/save.dat"), at: now.addingTimeInterval(-1000))+ try write("profile", to: cloud.appending(path: "saves_profiles/profile.dat"), at: now.addingTimeInterval(-1000))+ try setLastSync(saves, game, ownSaves, now.addingTimeInterval(-2000))+ let before = saves.timestamps()[saves.key(game, ownSaves)]++ XCTAssertEqual(saves.sync(game, location: ownSaves, folder: folder, direction: .download), .synced)+ XCTAssertEqual(try String(contentsOf: folder.appending(path: "slot1/save.dat"), encoding: .utf8), "from the cloud")+ XCTAssertNotEqual(saves.timestamps()[saves.key(game, ownSaves)], before, "the sync's timestamp is kept")+ XCTAssertEqual(saves.sync(game, location: profiles, folder: profilesFolder, direction: .download), .synced)+ XCTAssertEqual(try String(contentsOf: profilesFolder.appending(path: "profile.dat"), encoding: .utf8), "profile")++ let copy = try saves.backUpCloud(game, location: ownSaves)+ XCTAssertEqual(try String(contentsOf: copy.appending(path: "slot1/save.dat"), encoding: .utf8), "from the cloud")+ XCTAssertFalse(FileManager.default.fileExists(atPath: copy.appending(path: "saves_profiles").path), "nothing of the other location's")+ XCTAssertEqual(saves.sync(game, location: ownSaves, folder: folder, direction: .forceUpload), .synced, "keeping this Mac's saves works")++ // GOG failing to serve one of saves_profiles' own files still fails its copy and its sync.+ try write("lost", to: cloud.appending(path: "saves_profiles/broken.unserved"), at: now.addingTimeInterval(600))+ XCTAssertThrowsError(try saves.backUpCloud(game, location: profiles))+ let names = (try? FileManager.default.contentsOfDirectory(atPath: saves.backupsFolder(game, profiles).path)) ?? []+ XCTAssertFalse(names.contains { $0.hasSuffix(".partial") }, "no half copy")+ let kept = saves.timestamps()[saves.key(game, profiles)]+ guard case .failed = saves.sync(game, location: profiles, folder: profilesFolder, direction: .download) else { return XCTFail() }+ XCTAssertEqual(saves.timestamps()[saves.key(game, profiles)], kept, "a failed sync keeps no timestamp")+ }++ /// Owner review on #589: a cloud file older than the last sync and missing on the Mac does not+ /// come down before Play, and the upload after the session deletes it from the cloud; the copy+ /// of the cloud taken before that upload still holds it.+ func testAnUploadAfterADownloadKeepsACopyOfACloudFileTheMacLacks() throws {+ let (saves, game, location, folder) = try fixture()+ let cloud = try fakeGogdl(for: saves)+ let now = Date()+ let save = folder.appending(path: "slot1/save.dat")+ try FileManager.default.setAttributes([.modificationDate: now.addingTimeInterval(-3000)], ofItemAtPath: save.path)+ try write("progress", to: cloud.appending(path: "saves/slot1/save.dat"), at: now.addingTimeInterval(-3000))+ try write("old", to: cloud.appending(path: "saves/old.dat"), at: now.addingTimeInterval(-3000))+ try setLastSync(saves, game, location, now.addingTimeInterval(-2000))++ XCTAssertEqual(saves.sync(game, location: location, folder: folder, direction: .download), .synced)+ XCTAssertFalse(FileManager.default.fileExists(atPath: folder.appending(path: "old.dat").path), "gogdl brings only newer files")+ try write("more progress", to: save, at: now.addingTimeInterval(600)) // the session+ XCTAssertEqual(saves.sync(game, location: location, folder: folder, direction: .upload), .synced)+ XCTAssertFalse(FileManager.default.fileExists(atPath: cloud.appending(path: "saves/old.dat").path), "gogdl deleted it from the cloud")+ XCTAssertEqual(try String(contentsOf: cloud.appending(path: "saves/slot1/save.dat"), encoding: .utf8), "more progress")+ let copy = try XCTUnwrap(saves.backups(in: saves.backupsFolder(game, location)).first { $0.lastPathComponent.contains("-cloud") })+ XCTAssertEqual(try String(contentsOf: copy.appending(path: "old.dat"), encoding: .utf8), "old")+ }}diff --git a/Tests/HighballKitTests/GOGStoreTests.swift b/Tests/HighballKitTests/GOGStoreTests.swiftindex 3042335..cc8fa70 100644--- a/Tests/HighballKitTests/GOGStoreTests.swift+++ b/Tests/HighballKitTests/GOGStoreTests.swift@@ -213,6 +213,9 @@ final class GOGStoreTests: XCTestCase {"<?APPLICATION_DATA_LOCAL?>/GOG.com/Galaxy/Applications/533/Storage/Shared/Files")let off = #"{"content":{"Windows":{"cloudStorage":{"enabled":false}}}}"#XCTAssertNil(GOGSaves.parseLocations(Data(off.utf8), clientID: "c"))+ // gogdl lists a location's cloud files by name prefix, so `saves` also sees saves_profiles'.+ let shared = #"{"content":{"Windows":{"cloudStorage":{"enabled":true,"locations":[{"name":"saves_profiles","location":"<?INSTALL?>/p"},{"name":"saves","location":"<?INSTALL?>/s"},{"name":"config","location":"<?INSTALL?>/c"}]}}}}"#+ XCTAssertEqual(GOGSaves.parseLocations(Data(shared.utf8), clientID: "c")?.map(\.sharesItsPrefix), [false, true, false])}func testClientIDFromCompressedBuildManifest() throws {@@ -224,6 +227,19 @@ final class GOGStoreTests: XCTestCase {XCTAssertNil(GOGSaves.clientID(inManifest: Data([0x78, 0x9c, 0x00])))}+ /// Owner review on #589: a game whose build manifest names no client id (Beyond Good & Evil's)+ /// has no cloud saves, an answer, where GOG out of reach or answering something unreadable is+ /// none: only the second may keep an uninstall waiting.+ func testAGameWithoutAClientIDIsAnAnswerNotAFailure() throws {+ XCTAssertNil(try GOGSaves.clientID(inPublishedManifest: Data(#"{"baseProductId":"1207658746"}"#.utf8)))+ XCTAssertEqual(try GOGSaves.clientID(inPublishedManifest: Data(#"{"clientId":"5353"}"#.utf8)), "5353")+ XCTAssertThrowsError(try GOGSaves.clientID(inPublishedManifest: Data([0x78, 0x9c, 0x00])))+ XCTAssertNil(try GOGSaves.newestBuild(inBuildList: Data(#"{"total_count":0,"items":[]}"#.utf8)), "no build for Windows")+ XCTAssertEqual(try GOGSaves.newestBuild(inBuildList: Data(#"{"items":[{"link":"https://cdn.gog.com/a"},{"link":"https://cdn.gog.com/b"}]}"#.utf8)),+ URL(string: "https://cdn.gog.com/a"))+ XCTAssertThrowsError(try GOGSaves.newestBuild(inBuildList: Data("<html>".utf8)))+ }+func testSaveFoldersResolveInsideTheEnvironment() throws {let driveC = FileManager.default.temporaryDirectory.appending(path: "gogsaves-\(UUID().uuidString)/drive_c")defer { try? FileManager.default.removeItem(at: driveC.deletingLastPathComponent()) }diff --git a/Tests/HighballKitTests/UninstallTests.swift b/Tests/HighballKitTests/UninstallTests.swiftindex 318a321..034a167 100644--- a/Tests/HighballKitTests/UninstallTests.swift+++ b/Tests/HighballKitTests/UninstallTests.swift@@ -55,6 +55,14 @@ final class UninstallTests: XCTestCase {XCTAssertTrue(text.contains("delete the environment"), text)}+ /// Owner review on #589: only GOG's cloud-save locations are copied before the folder goes, so+ /// a game with cloud saves off that saves beside its exe loses those saves, and the text says so.+ func testTheGOGMessagePromisesACopyOnlyOfWhatGOGSyncs() {+ let text = Uninstall.confirmation(route: .gog(id: "1"), sizeOnDisk: 0)+ XCTAssertTrue(text.contains("ones GOG does not sync, are deleted with it"), text)+ XCTAssertFalse(text.contains("saves the game keeps inside its own folder are copied"), text)+ }+func testTheReasonIsWhatAnUnroutableGameShows() {let route = Uninstall.route(for: item(.steam))guard case let .none(reason) = route else { return XCTFail("expected none") }