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
Name non-Steam shortcut prefixes from shortcuts.vdf
NotProtonNot85c28a9778e9
diff --git a/app/Sources/NotProtonApp/Model/PrefixStore.swift b/app/Sources/NotProtonApp/Model/PrefixStore.swiftindex dd5b723..5a70148 100644--- a/app/Sources/NotProtonApp/Model/PrefixStore.swift+++ b/app/Sources/NotProtonApp/Model/PrefixStore.swift@@ -98,7 +98,11 @@ enum PrefixStore {}// Every appid under compatdata that actually has a prefix.- static func all(libraries: [SteamLibrary] = PrefixStore.libraries()) -> [WinePrefix] {+ static func all(+ libraries: [SteamLibrary] = PrefixStore.libraries(),+ userdata: URL = SupportPaths.Steam.userData.appending(path: "userdata")+ ) -> [WinePrefix] {+ var shortcuts: [String: String]?var found: [WinePrefix] = []for library in libraries {let entries = (try? FileManager.default.contentsOfDirectory(@@ -115,10 +119,16 @@ enum PrefixStore {isDirectory.boolValueelse { continue }+ var name = appName(appID: appID, in: library)+ if name == nil, let id = UInt32(appID), id & 0x8000_0000 != 0 {+ if shortcuts == nil { shortcuts = shortcutNames(userdata: userdata) }+ name = shortcuts?[appID]+ }+found.append(WinePrefix(appID: appID,- name: appName(appID: appID, in: library),+ name: name,library: library,lastUsed: (try? pfx.resourceValues(forKeys: [.contentModificationDateKey]))?.contentModificationDate@@ -136,6 +146,74 @@ enum PrefixStore {manifestValue("name", appID: appID, in: library)}+ static func shortcutNames(userdata: URL) -> [String: String] {+ let accounts = (try? FileManager.default.contentsOfDirectory(+ at: userdata, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles]+ )) ?? []+ var names: [String: String] = [:]+ for account in accounts.sorted(by: { $0.lastPathComponent < $1.lastPathComponent }) {+ let vdf = account.appending(path: "config/shortcuts.vdf")+ guard let data = try? Data(contentsOf: vdf) else { continue }+ names.merge(shortcutNames(vdf: data)) { first, _ in first }+ }+ return names+ }++ static func shortcutNames(vdf data: Data) -> [String: String] {+ let bytes = [UInt8](data)+ var names: [String: String] = [:]+ var offset = 0++ func cString() -> String? {+ guard let end = bytes[offset...].firstIndex(of: 0) else { return nil }+ defer { offset = end + 1 }+ return String(decoding: bytes[offset..<end], as: UTF8.self)+ }++ func skip(_ count: Int) -> Bool {+ guard bytes.count - offset >= count else { return false }+ offset += count+ return true+ }++ // Binary KeyValues: type byte, key, value; 0x08 closes a map.+ func map(depth: Int) -> Bool {+ guard depth < 16 else { return false }+ var appID: UInt32?+ var appName: String?+ while offset < bytes.count {+ let type = bytes[offset]+ offset += 1+ if type == 0x08 { break }+ guard let key = cString()?.lowercased() else { return false }+ switch type {+ case 0x00:+ guard map(depth: depth + 1) else { return false }+ case 0x01:+ guard let value = cString() else { return false }+ if key == "appname" { appName = value }+ case 0x02:+ guard bytes.count - offset >= 4 else { return false }+ if key == "appid" {+ appID = bytes[offset..<offset + 4].reversed().reduce(0) { $0 << 8 | UInt32($1) }+ }+ offset += 4+ case 0x03, 0x04, 0x06:+ guard skip(4) else { return false }+ case 0x07, 0x0a:+ guard skip(8) else { return false }+ default:+ return false+ }+ }+ if let appID, let appName, !appName.isEmpty { names[String(appID)] = appName }+ return true+ }++ _ = map(depth: 0)+ return names+ }+static func installDirectory(of prefix: WinePrefix) -> URL? {guard let folder = manifestValue("installdir", appID: prefix.appID, in: prefix.library)else { return nil }diff --git a/app/Sources/NotProtonApp/Support/TextWidth.swift b/app/Sources/NotProtonApp/Support/TextWidth.swiftindex 880762e..2c72105 100644--- a/app/Sources/NotProtonApp/Support/TextWidth.swift+++ b/app/Sources/NotProtonApp/Support/TextWidth.swift@@ -3,6 +3,7 @@ import AppKitenum TextWidth {static let cellPadding: CGFloat = 24+ static let maxTitle: CGFloat = 320static func of(_ text: String, size: CGFloat = NSFont.systemFontSize) -> CGFloat {let font = NSFont.systemFont(ofSize: size)diff --git a/app/Sources/NotProtonApp/Views/BackupsView.swift b/app/Sources/NotProtonApp/Views/BackupsView.swiftindex eafcbaf..18c47dc 100644--- a/app/Sources/NotProtonApp/Views/BackupsView.swift+++ b/app/Sources/NotProtonApp/Views/BackupsView.swift@@ -125,7 +125,7 @@ struct BackupsView: View {}private var gameWidth: CGFloat {- TextWidth.widest(model.backups.map(\.title)) ?? 240+ min(TextWidth.widest(model.backups.map(\.title)) ?? 240, TextWidth.maxTitle + TextWidth.cellPadding)}private var libraryWidth: CGFloat {diff --git a/app/Sources/NotProtonApp/Views/PrefixesView.swift b/app/Sources/NotProtonApp/Views/PrefixesView.swiftindex ec3284e..97265e9 100644--- a/app/Sources/NotProtonApp/Views/PrefixesView.swift+++ b/app/Sources/NotProtonApp/Views/PrefixesView.swift@@ -210,7 +210,7 @@ struct PrefixesView: View {private var gameWidth: CGFloat {let widths = model.prefixes.map { prefix -> CGFloat in- var width = TextWidth.of(prefix.title)+ var width = min(TextWidth.of(prefix.title), TextWidth.maxTitle)if model.isStale(prefix) { width += 22 }if prefix.name == nil {width += TextWidth.of(String(localized: "No longer installed"), size: NSFont.smallSystemFontSize) + 6@@ -267,11 +267,12 @@ struct PrefixesView: View {.disabled(model.isBusy || model.tools.isEmpty).accessibilityLabel("Needs rebuilding")}- Text(prefix.title).help(prefix.title)+ Text(prefix.title).lineLimit(1).help(prefix.title)if prefix.name == nil {Text("No longer installed").font(.caption).foregroundStyle(.secondary)+ .fixedSize()}if model.busy.contains(prefix.id) {ProgressView().controlSize(.small)diff --git a/app/Tests/NotProtonAppTests/PrefixStoreTests.swift b/app/Tests/NotProtonAppTests/PrefixStoreTests.swiftindex 1ff68d5..d7c6d9b 100644--- a/app/Tests/NotProtonAppTests/PrefixStoreTests.swift+++ b/app/Tests/NotProtonAppTests/PrefixStoreTests.swift@@ -114,6 +114,45 @@ struct PrefixStoreTests {#expect(orphan?.title == "App 1649240")}+ @Test("A non-Steam shortcut prefix is named from shortcuts.vdf")+ func readsTheShortcutName() throws {+ let dir = try scratchDirectory("pfx")+ defer { try? FileManager.default.removeItem(at: dir) }+ let library = SteamLibrary(root: dir.appending(path: "library"))+ let userdata = dir.appending(path: "userdata")++ func entry(_ index: String, appID: UInt32, name: String) -> [UInt8] {+ var out: [UInt8] = [0x00] + Array(index.utf8) + [0]+ out += [0x02] + Array("appid".utf8) + [0]+ out += withUnsafeBytes(of: appID.littleEndian, Array.init)+ out += [0x01] + Array("AppName".utf8) + [0] + Array(name.utf8) + [0]+ out += [0x07] + Array("LastPlayTime".utf8) + [0] + [UInt8](repeating: 1, count: 8)+ out += [0x00] + Array("tags".utf8) + [0, 0x08]+ return out + [0x08]+ }+ var vdf: [UInt8] = [0x00] + Array("shortcuts".utf8) + [0]+ vdf += entry("0", appID: 0xf281_5b63, name: "Grand Theft Auto V Enhanced")+ vdf += entry("1", appID: 0x9ea9_860b, name: "Postal2")+ vdf += [0x08, 0x08]+ let file = userdata.appending(path: "54303850/config/shortcuts.vdf")+ try FileManager.default.createDirectory(+ at: file.deletingLastPathComponent(), withIntermediateDirectories: true)+ try Data(vdf).write(to: file)++ for appID in ["4068563811", "2661910027", "3000000000"] {+ try FileManager.default.createDirectory(+ at: library.compatdata.appending(path: "\(appID)/pfx"), withIntermediateDirectories: true)+ }++ let prefixes = PrefixStore.all(libraries: [library], userdata: userdata)+ func title(_ appID: String) -> String? { prefixes.first { $0.appID == appID }?.title }++ #expect(title("4068563811") == "Grand Theft Auto V Enhanced")+ #expect(title("2661910027") == "Postal2")+ #expect(title("3000000000") == "App 3000000000")+ #expect(PrefixStore.shortcutNames(vdf: Data(vdf.prefix(40))).isEmpty)+ }+@Test("Sizing a prefix stops at a symlink instead of walking through it")func doesNotFollowLinksOutOfThePrefix() throws {let dir = try scratchDirectory("pfx")