Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit
Swift apple-silicond3dmetaldxmtdxvkgame-porting-toolkitgptkmac-gamingmacosmetalfxrosetta-2steamsteam-playwinewinecx
fix(prefix): keep Program Files and ProgramData when a prefix is rebuilt
raymondchins4764eb065ffa
diff --git a/app/Sources/BridgeplayApp/Model/PrefixRebuild.swift b/app/Sources/BridgeplayApp/Model/PrefixRebuild.swiftindex 6edd005..fccec72 100644--- a/app/Sources/BridgeplayApp/Model/PrefixRebuild.swift+++ b/app/Sources/BridgeplayApp/Model/PrefixRebuild.swift@@ -95,6 +95,12 @@ extension PrefixTools {detail: String(localized: "\(first) could not be copied out of the prefix. The prefix has been left as it was and nothing has been lost. Check free space and permissions, then try again."))}+ // A miss here keeps the old prefix as the backup instead of blocking the rebuild.+ carry.installedFolders()+ let discard = !keepBackup && carry.lost.isEmpty+ if !carry.lost.isEmpty {+ AppLog.note("rebuild \(prefix.appID) kept backup, did not carry: \(carry.lost.joined(separator: ", "))")+ }let rerun = InstallScriptMarkers.clear(userReg: fresh.appending(path: "user.reg"),@@ -117,7 +123,7 @@ extension PrefixTools {}if let record { try writeBuildRecord(record, for: prefix) }guard parked else { return nil }- guard keepBackup else {+ guard !discard else {try? fm.removeItem(at: previous)return nil}@@ -248,6 +254,32 @@ private final class Carry {}}+ // Saves and launchers also live here. Fresh files win; only what the template lacks comes over.+ func installedFolders() {+ for name in ["Program Files", "Program Files (x86)", "ProgramData"] {+ let source = old.appending(path: "drive_c/\(name)")+ let values = try? source.resourceValues(forKeys: [.isSymbolicLinkKey])+ guard values?.isSymbolicLink != true else { continue }+ fill(fresh.appending(path: "drive_c/\(name)"), from: source)+ }+ }++ private func fill(_ landing: URL, from source: URL) {+ for entry in realEntries(in: source) {+ let target = landing.appending(path: entry.lastPathComponent)+ if isBrokenLink(target) { try? fm.removeItem(at: target) }+ var isDirectory: ObjCBool = false+ if !fm.fileExists(atPath: target.path(percentEncoded: false), isDirectory: &isDirectory) {+ attempt(entry) { try fm.copyItem(at: entry, to: target) }+ } else if escapes(target) || isDirectory.boolValue != directoryExists(entry) {+ park(entry, beside: target)+ } else if isDirectory.boolValue {+ fill(target, from: entry)+ }+ }+ carryLinks(from: source, to: landing)+ }+private func attempt(_ source: URL, _ work: () throws -> Void) {do { try work() } catch { lost.append(source.path(percentEncoded: false)) }}diff --git a/app/Tests/BridgeplayAppTests/PrefixToolsTests.swift b/app/Tests/BridgeplayAppTests/PrefixToolsTests.swiftindex 55de58e..e8f349f 100644--- a/app/Tests/BridgeplayAppTests/PrefixToolsTests.swift+++ b/app/Tests/BridgeplayAppTests/PrefixToolsTests.swift@@ -687,7 +687,8 @@ struct PrefixToolsTests {// Replaced, both the file the arch is read from and what an installer left beside it.#expect(text("drive_c/windows/system32/ntdll.dll") == "fresh")#expect(text("drive_c/windows/vcruntime.dll") == nil)- #expect(text("drive_c/Program Files/redist/thing.dll") == nil)+ // Program Files can hold saves, so what the template lacks comes over.+ #expect(text("drive_c/Program Files/redist/thing.dll") == "installed")// Inside the prefix, so the folder Steam syncs is the folder the game reads. The home// folder the template used to point at keeps what is in it and is no longer reached.@@ -1022,9 +1023,9 @@ struct PrefixToolsTests {#expect(try Data(contentsOf: driveC.appending(path: "Games/Quake/quake.exe")) == Data("exe".utf8))#expect(try Data(contentsOf: driveC.appending(path: "notes.txt")) == Data("notes".utf8))- // A name the template does make is the template's, arch and all.+ // windows is the template's, arch and all.#expect(!fm.fileExists(- atPath: driveC.appending(path: "Program Files/redist/thing.dll").path(percentEncoded: false)))+ atPath: driveC.appending(path: "windows/vcruntime.dll").path(percentEncoded: false)))}// Emptying the prefix first meant a wineboot that failed left the game with nothing at@@ -1083,6 +1084,67 @@ struct PrefixToolsTests {#expect(PrefixStore.backups(of: prefix).isEmpty)}+ // Ubisoft Connect keeps saves under Program Files (x86), and the discard path deleted them.+ @Test("A discard rebuild carries Program Files and ProgramData, fresh files win")+ func discardRebuildCarriesProgramFiles() throws {+ let fm = FileManager.default+ let dir = fm.temporaryDirectory.appending(path: "np-progfiles-\(UUID().uuidString)")+ defer { try? fm.removeItem(at: dir) }+ let home = dir.appending(path: "home/Documents")+ try fm.createDirectory(at: home, withIntermediateDirectories: true)++ let prefix = try usedPrefix(in: dir, home: home)+ let drive = prefix.pfx.appending(path: "drive_c")+ for (path, body) in [+ ("Program Files (x86)/Ubisoft/savegames/1.save", "ubi"),+ ("ProgramData/Foo/x.dat", "foo"),+ ("ProgramData/Shared/both.ini", "old"),+ ] {+ let file = drive.appending(path: path)+ try fm.createDirectory(+ at: file.deletingLastPathComponent(), withIntermediateDirectories: true)+ try Data(body.utf8).write(to: file)+ }+ let runner = try fakeRunner(in: dir, home: home, extra: """+ mkdir -p "$WINEPREFIX/drive_c/ProgramData/Shared"; \+ printf fresh > "$WINEPREFIX/drive_c/ProgramData/Shared/both.ini"+ """)++ #expect(try PrefixTools.recreate(prefix, runner: runner, keepBackup: false) == nil)++ func text(_ path: String) -> String? {+ try? String(contentsOf: prefix.pfx.appending(path: "drive_c/\(path)"), encoding: .utf8)+ }+ #expect(text("Program Files (x86)/Ubisoft/savegames/1.save") == "ubi")+ #expect(text("ProgramData/Foo/x.dat") == "foo")+ #expect(text("ProgramData/Shared/both.ini") == "fresh")+ #expect(text("windows/system32/ntdll.dll") == "fresh")+ #expect(PrefixStore.backups(of: prefix).isEmpty)+ }++ @Test("A discard rebuild keeps the old prefix when Program Files cannot be carried")+ func discardRebuildKeepsBackupOnMiss() throws {+ let fm = FileManager.default+ let dir = fm.temporaryDirectory.appending(path: "np-progfiles-miss-\(UUID().uuidString)")+ defer { try? fm.removeItem(at: dir) }+ let home = dir.appending(path: "home/Documents")+ try fm.createDirectory(at: home, withIntermediateDirectories: true)++ let prefix = try usedPrefix(in: dir, home: home)+ let save = prefix.pfx.appending(path: "drive_c/Program Files/redist/thing.dll")+ try fm.setAttributes([.posixPermissions: 0], ofItemAtPath: save.path(percentEncoded: false))+ defer {+ try? fm.setAttributes(+ [.posixPermissions: 0o644], ofItemAtPath: save.path(percentEncoded: false))+ }++ let kept = try #require(try PrefixTools.recreate(+ prefix, runner: try fakeRunner(in: dir, home: home), keepBackup: false))+ #expect(PrefixStore.backups(of: prefix).count == 1)+ #expect(fm.fileExists(atPath: kept.appending(path: "drive_c/Program Files/redist/thing.dll")+ .path(percentEncoded: false)))+ }+// Steam lays a prefix down in stages, so one caught early holds no profile. That is nothing// to carry, not something lost, and refusing it blocks the prefix most likely to need it.@Test("A prefix with no profile yet still rebuilds")