Skip to content

Run Windows games on Apple Silicon — free, open source, with an open compatibility database that tells you what actually works.

A program's cover and name survive a rename of its environment (highball#258)

Gauthier Piarrette65dbc2864e9f
diff --git a/Sources/HighballApp/AppState.swift b/Sources/HighballApp/AppState.swift
index 5aae15d..76cdb24 100644
--- a/Sources/HighballApp/AppState.swift
+++ b/Sources/HighballApp/AppState.swift
@@ -265,7 +265,7 @@ final class AppState {
var renaming: LibraryItem?
var renameText = ""
- func displayTitle(_ item: LibraryItem) -> String { customNames[item.id] ?? item.title }
+ func displayTitle(_ item: LibraryItem) -> String { NameStore.name(for: item.id, in: customNames) ?? item.title }
func beginRename(_ item: LibraryItem) {
renameText = displayTitle(item)
diff --git a/Sources/HighballApp/LibraryView.swift b/Sources/HighballApp/LibraryView.swift
index f6cd8d9..74010f5 100644
--- a/Sources/HighballApp/LibraryView.swift
+++ b/Sources/HighballApp/LibraryView.swift
@@ -353,7 +353,7 @@ struct LibraryTile: View {
Button(L("Reset cover")) { state.resetCover(for: item) }
}
Button(L("Rename…")) { state.beginRename(item) }
- if state.customNames[item.id] != nil {
+ if NameStore.name(for: item.id, in: state.customNames) != nil {
Button(L("Reset name")) { state.resetName(for: item) }
}
// A program someone added by hand leaves the library from its tile, not only from
diff --git a/Sources/HighballKit/Library.swift b/Sources/HighballKit/Library.swift
index 1e1a823..83c5a95 100644
--- a/Sources/HighballKit/Library.swift
+++ b/Sources/HighballKit/Library.swift
@@ -267,14 +267,23 @@ public struct CoverStore: Sendable {
id.replacingOccurrences(of: ":", with: "_").replacingOccurrences(of: "/", with: "_")
}
- /// The stored override for an item, if any.
+ static let extensions = ["png", "jpg", "jpeg", "heic", "webp"]
+
+ /// The stored override for an item, if any. A program's cover is found by its own id when
+ /// the environment it lives in was renamed since (PinID).
public func coverURL(for id: String) -> URL? {
let base = dir.appending(path: Self.filename(for: id))
- for ext in ["png", "jpg", "jpeg", "heic", "webp"] {
+ for ext in Self.extensions {
let url = base.appendingPathExtension(ext)
if FileManager.default.fileExists(atPath: url.path) { return url }
}
- return nil
+ guard let uuid = PinID.uuid(in: id) else { return nil }
+ let suffix = "_" + uuid
+ return ((try? FileManager.default.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil)) ?? [])
+ .filter { Self.extensions.contains($0.pathExtension.lowercased()) }
+ .filter { $0.deletingPathExtension().lastPathComponent.hasPrefix("pin_") && $0.deletingPathExtension().lastPathComponent.hasSuffix(suffix) }
+ .sorted { $0.lastPathComponent < $1.lastPathComponent }
+ .first
}
/// Copies the chosen image in (replacing any previous override).
@@ -354,9 +363,25 @@ public struct CoverStore: Sendable {
return data as Data
}
+ /// Removes the item's override, and for a program every copy kept under an earlier name of
+ /// its environment, so Reset cover really resets.
public func clearCover(for id: String) {
- guard let existing = coverURL(for: id) else { return }
- try? FileManager.default.removeItem(at: existing)
+ for _ in 0..<8 {
+ guard let existing = coverURL(for: id) else { return }
+ guard (try? FileManager.default.removeItem(at: existing)) != nil else { return }
+ }
+ }
+}
+
+/// A program's library id carries its environment's name ("pin:<bottle>:<uuid>"), so renaming or
+/// swapping environments changes it, and the cover and name chosen for the program went missing
+/// although the files were still there (highball#258, 2026-10-05). The uuid is the program's own
+/// and never changes, so covers and names fall back to it when the full id finds nothing.
+enum PinID {
+ static func uuid(in id: String) -> String? {
+ guard id.hasPrefix("pin:"), let last = id.split(separator: ":").last,
+ UUID(uuidString: String(last)) != nil else { return nil }
+ return String(last)
}
}
@@ -377,13 +402,26 @@ public struct NameStore: Sendable {
return (try? JSONDecoder().decode([String: String].self, from: d)) ?? [:]
}
- public func name(for id: String) -> String? { names()[id] }
+ public func name(for id: String) -> String? { Self.name(for: id, in: names()) }
+
+ /// The name for an id in a loaded table: the id itself, or for a program the name kept under
+ /// an earlier name of its environment (PinID).
+ public static func name(for id: String, in names: [String: String]) -> String? {
+ if let exact = names[id] { return exact }
+ guard let uuid = PinID.uuid(in: id) else { return nil }
+ return names.filter { $0.key.hasPrefix("pin:") && $0.key.hasSuffix(":" + uuid) }.sorted { $0.key < $1.key }.first?.value
+ }
/// Sets a name, or clears it when the name is empty once trimmed: a blank is a reset, not a
/// game called nothing.
public func setName(_ name: String?, for id: String) throws {
var all = names()
let trimmed = name?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
+ // A program's name kept under an earlier name of its environment goes, whether this sets
+ // a new name or resets it, or the reset would show the old one again.
+ if let uuid = PinID.uuid(in: id) {
+ for key in all.keys where key != id && key.hasPrefix("pin:") && key.hasSuffix(":" + uuid) { all.removeValue(forKey: key) }
+ }
if trimmed.isEmpty { all.removeValue(forKey: id) } else { all[id] = trimmed }
if all.isEmpty {
try? FileManager.default.removeItem(at: file)
diff --git a/Tests/HighballKitTests/PinIdentityTests.swift b/Tests/HighballKitTests/PinIdentityTests.swift
new file mode 100644
index 0000000..1c94099
--- /dev/null
+++ b/Tests/HighballKitTests/PinIdentityTests.swift
@@ -0,0 +1,44 @@
+import XCTest
+import AppKit
+@testable import HighballKit
+
+/// A program's cover and name survive a rename of its environment (highball#258): the library id
+/// carries the environment's name, the program's uuid does not change.
+final class PinIdentityTests: XCTestCase {
+ private var home: URL!
+ private let uuid = "6F9619FF-8B86-D011-B42D-00CF4FC964FF"
+
+ override func setUpWithError() throws {
+ home = FileManager.default.temporaryDirectory.appending(path: "hb-pinid-\(UUID().uuidString)", directoryHint: .isDirectory)
+ try FileManager.default.createDirectory(at: home, withIntermediateDirectories: true)
+ }
+ override func tearDownWithError() throws { try? FileManager.default.removeItem(at: home) }
+
+ private func png() throws -> URL {
+ let url = home.appending(path: "src.png")
+ let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: 20, pixelsHigh: 30, bitsPerSample: 8, samplesPerPixel: 4,
+ hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)!
+ try rep.representation(using: .png, properties: [:])!.write(to: url)
+ return url
+ }
+
+ func testACoverFollowsTheProgramAcrossAnEnvironmentRename() throws {
+ let store = CoverStore(paths: HighballPaths(home: home))
+ try store.setCover(for: "pin:Games:\(uuid)", from: try png())
+ XCTAssertNotNil(store.coverURL(for: "pin:Games_:\(uuid)"), "the environment was renamed, the program's cover stays")
+ XCTAssertNil(store.coverURL(for: "pin:Games_:\(UUID().uuidString)"), "another program does not get it")
+ XCTAssertNil(store.coverURL(for: "steam:400"), "only programs fall back")
+ store.clearCover(for: "pin:Games_:\(uuid)")
+ XCTAssertNil(store.coverURL(for: "pin:Games:\(uuid)"), "Reset cover removes the copy kept under the old name")
+ }
+
+ func testANameFollowsTheProgramAndAResetReallyResets() throws {
+ let store = NameStore(paths: HighballPaths(home: home))
+ try store.setName("Bloody Spell", for: "pin:Games:\(uuid)")
+ XCTAssertEqual(store.name(for: "pin:Sandbox:\(uuid)"), "Bloody Spell")
+ try store.setName(nil, for: "pin:Sandbox:\(uuid)")
+ XCTAssertNil(store.name(for: "pin:Sandbox:\(uuid)"), "a reset after the rename does not bring the old name back")
+ XCTAssertNil(store.name(for: "pin:Games:\(uuid)"))
+ XCTAssertNil(NameStore.name(for: "steam:400", in: ["pin:Games:\(uuid)": "x"]))
+ }
+}