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
refactor(steam): read libraryfolders.vdf with one parser that unescapes paths
raymondchins36037f6aace0
diff --git a/app/Sources/BridgeplayApp/Model/BrandMigration.swift b/app/Sources/BridgeplayApp/Model/BrandMigration.swiftindex d9e4b6c..cb0d201 100644--- a/app/Sources/BridgeplayApp/Model/BrandMigration.swift+++ b/app/Sources/BridgeplayApp/Model/BrandMigration.swift@@ -441,13 +441,7 @@ struct BrandMigration: Sendable {func libraries() -> [URL] {let vdf = steamUserData.appending(path: "steamapps/libraryfolders.vdf")let text = (try? String(contentsOf: vdf, encoding: .utf8)) ?? ""- var roots = [steamUserData]- for line in text.components(separatedBy: "\n") {- guard let (key, value, _) = Self.keyValue(line), key.caseInsensitiveCompare("path") == .orderedSame- else { continue }- let path = value.replacingOccurrences(of: "\\\\", with: "\\").replacingOccurrences(of: "\\\"", with: "\"")- roots.append(URL(filePath: path))- }+ let roots = [steamUserData] + PrefixStore.libraryPaths(in: text).map { URL(filePath: $0) }var seen: Set<String> = []return roots.filter { seen.insert($0.path(percentEncoded: false).trimmingSuffix("/")).inserted }}diff --git a/app/Sources/BridgeplayApp/Model/PrefixStore.swift b/app/Sources/BridgeplayApp/Model/PrefixStore.swiftindex 2c8f4c5..ff83720 100644--- a/app/Sources/BridgeplayApp/Model/PrefixStore.swift+++ b/app/Sources/BridgeplayApp/Model/PrefixStore.swift@@ -67,14 +67,7 @@ enum PrefixStore {return [library(at: SupportPaths.Steam.userData)]}- var roots: [URL] = []- for line in text.split(separator: "\n") {- let fields = line.split(separator: "\"").map(String.init)- .filter { !$0.trimmingCharacters(in: .whitespaces).isEmpty }- guard fields.count >= 2, fields[0] == "path" else { continue }- roots.append(URL(filePath: fields[1]))- }-+ var roots = libraryPaths(in: text).map { URL(filePath: $0) }if roots.isEmpty { roots = [SupportPaths.Steam.userData] }var seen: Set<String> = []@@ -84,6 +77,17 @@ enum PrefixStore {}}+ // Every "path" in libraryfolders.vdf, with Steam's \\ and \" escapes undone. The one+ // reader for this file, so Uninstall, Prefixes and the 3.0 migration see the same paths.+ static func libraryPaths(in text: String) -> [String] {+ text.components(separatedBy: "\n").compactMap { line in+ guard let (key, value, _) = BrandMigration.keyValue(line),+ key.caseInsensitiveCompare("path") == .orderedSame+ else { return nil }+ return value.replacingOccurrences(of: "\\\\", with: "\\").replacingOccurrences(of: "\\\"", with: "\"")+ }+ }+static func library(at root: URL) -> SteamLibrary {SteamLibrary(root: root, volume: volumeName(of: root))}diff --git a/app/Tests/BridgeplayAppTests/PrefixStoreTests.swift b/app/Tests/BridgeplayAppTests/PrefixStoreTests.swiftindex d305b7b..b3e0c9b 100644--- a/app/Tests/BridgeplayAppTests/PrefixStoreTests.swift+++ b/app/Tests/BridgeplayAppTests/PrefixStoreTests.swift@@ -50,6 +50,13 @@ struct PrefixStoreTests {#expect(libraries.last?.compatdata.lastPathComponent == "compatdata")}+ // The migration unescaped these and Prefixes/Uninstall did not, so the two disagreed.+ @Test("Library paths come back with Steam's escapes undone")+ func libraryPathsAreUnescaped() {+ let text = "\t\t\"path\"\t\t\"/Volumes/A \\\"B\\\"/C\\\\D\"\n\t\t\"Path\"\t\t\"/Volumes/E\"\n\t\t\"label\"\t\t\"path\"\n"+ #expect(PrefixStore.libraryPaths(in: text) == ["/Volumes/A \"B\"/C\\D", "/Volumes/E"])+ }+@Test("A missing or empty file still yields the default library")func fallsBackToTheDefaultLibrary() throws {let dir = try scratchDirectory("pfx")