Skip to content

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

environment setting: keep game files inside this environment (discussion #157). Documents becomes a real folder under drive_c instead of wineboot's link to ~/Documents; off again keeps the inside folder as 'Documents (environment)', nothing deleted silently; the delete confirmation says when saves are inside; CLI bottle set keepfiles on|off; survives bottle repair

Gauthier Piarrette43151f2c451c
diff --git a/Sources/HighballApp/AppState.swift b/Sources/HighballApp/AppState.swift
index 5b06793..94dcdd8 100644
--- a/Sources/HighballApp/AppState.swift
+++ b/Sources/HighballApp/AppState.swift
@@ -1552,6 +1552,19 @@ final class AppState {
do { try bottleStore.update(bottle); refresh() } catch { fail(error, bottle: bottle) }
}
+ /// Discussion #157: the environment's Documents becomes a real folder inside it (or the link
+ /// to the Mac's Documents again). The prefix changes first, the setting records it; a failure
+ /// leaves the setting as it was, so the switch never claims a shape the prefix does not have.
+ func setKeepFilesInside(_ on: Bool, for bottle: Bottle) {
+ var copy = bottles.first { $0.name == bottle.name } ?? bottle
+ guard copy.settings.keepFilesInside != on else { return }
+ do {
+ for line in try UserFolders.set(inside: on, driveC: copy.driveC) { appendLog(line) }
+ copy.settings.keepFilesInside = on
+ update(copy)
+ } catch { fail(error, bottle: copy) }
+ }
+
func deleteBottle(_ name: String) {
// The row and its Delete item stay on screen for the whole operation, so without this a
// second click started a second delete and the loser reported "missing" for a delete that
diff --git a/Sources/HighballApp/BottleView.swift b/Sources/HighballApp/BottleView.swift
index 306b0f7..a0e14af 100644
--- a/Sources/HighballApp/BottleView.swift
+++ b/Sources/HighballApp/BottleView.swift
@@ -435,6 +435,11 @@ struct BottleSettingsSheet: View {
.font(.caption).foregroundStyle(.secondary)
Text(L("Games run with the environment’s sync (msync is fastest). Opening the Steam window restarts Windows processes with sync off — its interface needs it."))
.font(.caption).foregroundStyle(.secondary)
+ Toggle(L("Keep game files inside this environment"), isOn: Binding(
+ get: { (state.bottles.first { $0.name == bottle.name } ?? bottle).settings.keepFilesInside },
+ set: { on in state.setKeepFilesInside(on, for: bottle) }))
+ Text(L("On, the Windows Documents folder lives inside this environment, so games that save there (FromSoftware, Bandai Namco, the Sims and others) no longer write into your Mac's Documents. Files already in your Mac's Documents stay there and the game starts fresh here. Off again keeps the environment's folder as “Documents (environment)”. Saves kept inside go with the environment when you delete it."))
+ .font(.caption).foregroundStyle(.secondary)
}
Section(L("Advanced")) {
let offered = state.offeredEngines(for: state.bottles.first { $0.name == bottle.name } ?? bottle)
@@ -495,7 +500,9 @@ struct BottleSettingsSheet: View {
.formStyle(.grouped)
}
.frame(width: 560, height: 560)
- .confirmationDialog(L("Delete this environment? Its Windows drive and everything installed in it are removed."),
+ .confirmationDialog(UserFolders.hasFilesInside(driveC: bottle.driveC)
+ ? L("Delete this environment? Its Windows drive and everything installed in it are removed, including the game saves kept in its Documents folder.")
+ : L("Delete this environment? Its Windows drive and everything installed in it are removed."),
isPresented: $confirmDelete, titleVisibility: .visible) {
Button(L("Delete"), role: .destructive) { dismiss(); state.deleteBottle(bottle.name) }
Button(L("Cancel"), role: .cancel) {}
diff --git a/Sources/HighballApp/L10n.swift b/Sources/HighballApp/L10n.swift
index 1b1e217..4fe85ce 100644
--- a/Sources/HighballApp/L10n.swift
+++ b/Sources/HighballApp/L10n.swift
@@ -468,6 +468,9 @@ enum L10n {
"Creating the %@ environment — first boot takes about 90 seconds": "Création de l'environnement %@ — le premier démarrage prend environ 90 secondes",
"Delete environment…": "Supprimer l'environnement…",
"Delete this environment? Its Windows drive and everything installed in it are removed.": "Supprimer cet environnement ? Son disque Windows et tout ce qui y est installé seront supprimés.",
+ "Delete this environment? Its Windows drive and everything installed in it are removed, including the game saves kept in its Documents folder.": "Supprimer cet environnement ? Son disque Windows et tout ce qui y est installé seront supprimés, y compris les sauvegardes gardées dans son dossier Documents.",
+ "Keep game files inside this environment": "Garder les fichiers des jeux dans cet environnement",
+ "On, the Windows Documents folder lives inside this environment, so games that save there (FromSoftware, Bandai Namco, the Sims and others) no longer write into your Mac's Documents. Files already in your Mac's Documents stay there and the game starts fresh here. Off again keeps the environment's folder as “Documents (environment)”. Saves kept inside go with the environment when you delete it.": "Activé, le dossier Documents de Windows vit dans cet environnement, donc les jeux qui y sauvegardent (FromSoftware, Bandai Namco, les Sims et d'autres) n'écrivent plus dans le dossier Documents de votre Mac. Les fichiers déjà dans le Documents du Mac y restent et le jeu repart de zéro ici. Désactivé, le dossier de l'environnement est gardé sous le nom « Documents (environment) ». Les sauvegardes gardées à l'intérieur partent avec l'environnement quand vous le supprimez.",
"Deleting the %@ environment": "Suppression de l'environnement %@",
"Duplicating the %@ environment": "Duplication de l'environnement %@",
"Environment default": "Réglage de l'environnement",
diff --git a/Sources/HighballKit/Bottle.swift b/Sources/HighballKit/Bottle.swift
index 151bd31..9c7266f 100644
--- a/Sources/HighballKit/Bottle.swift
+++ b/Sources/HighballKit/Bottle.swift
@@ -369,12 +369,18 @@ public struct BottleSettings: Codable, Sendable {
/// inherits Steam's environment from before the setting changed and never sees it
/// (issues #22/#25). Launches re-mirror when this differs from dllOverrides.
public var dllOverridesSynced: String?
+ /// Game files stay inside the environment: the user's Documents is a real folder under
+ /// drive_c instead of a link to the macOS Documents (which is what wineboot makes, and why
+ /// Dark Souls' or the Sims' save folders land in ~/Documents, discussion #157). Off keeps the
+ /// link. Applied to the prefix by `UserFolders`; the saves go with the environment when it
+ /// is deleted, and the delete confirmation says so.
+ public var keepFilesInside: Bool = false
public var environment: [String: String] = [:]
public var pins: [Pin] = []
public var recipes: [String] = []
public var created: Date = Date()
- enum CodingKeys: String, CodingKey { case formatVersion, name, engineID, renderer, rendererExplicit, windowsVersion, sync, metalHUD, advertiseAVX, dxvkAsync, fpsCap, frameGen, frameGenAdaptive, frameGenFlowScale, frameGenPerformance, frameGenForceVsync, commandIsControl, commandIsControlSynced, dpiScale, dllOverrides, dxvkAppConfig, dllOverridesSynced, environment, pins, recipes, created }
+ enum CodingKeys: String, CodingKey { case formatVersion, name, engineID, renderer, rendererExplicit, windowsVersion, sync, metalHUD, advertiseAVX, dxvkAsync, fpsCap, frameGen, frameGenAdaptive, frameGenFlowScale, frameGenPerformance, frameGenForceVsync, commandIsControl, commandIsControlSynced, dpiScale, dllOverrides, dxvkAppConfig, dllOverridesSynced, keepFilesInside, environment, pins, recipes, created }
public init(name: String, engineID: String) {
self.name = name
@@ -412,6 +418,7 @@ public struct BottleSettings: Codable, Sendable {
}
dllOverrides = try c.decodeIfPresent(String.self, forKey: .dllOverrides) ?? ""
dllOverridesSynced = try c.decodeIfPresent(String.self, forKey: .dllOverridesSynced)
+ keepFilesInside = try c.decodeIfPresent(Bool.self, forKey: .keepFilesInside) ?? false
commandIsControl = try c.decodeIfPresent(Bool.self, forKey: .commandIsControl) ?? true
commandIsControlSynced = try c.decodeIfPresent(Bool.self, forKey: .commandIsControlSynced)
environment = try c.decodeIfPresent([String: String].self, forKey: .environment) ?? [:]
diff --git a/Sources/HighballKit/UserFolders.swift b/Sources/HighballKit/UserFolders.swift
new file mode 100644
index 0000000..9107e09
--- /dev/null
+++ b/Sources/HighballKit/UserFolders.swift
@@ -0,0 +1,97 @@
+import Foundation
+
+/// The user folders Wine links to the Mac's own. wineboot makes `drive_c/users/<name>/Documents`
+/// (and Desktop, Downloads, Music, Pictures, Videos) symlinks to ~/Documents and so on, so a game
+/// that saves under Documents writes into the macOS folder and its saves outlive the environment.
+/// Two people asked for the opposite (discussion #157): saves inside the environment, nothing
+/// in ~/Documents. This swaps Documents between the two shapes, and nothing else:
+///
+/// - inside: the link is removed and a real folder takes its place. A folder kept by an earlier
+/// switch back (`Documents (environment)`) is restored instead of starting empty. Files that
+/// live in ~/Documents stay there; the game starts fresh inside, which the caption says.
+/// - linked: a real folder is renamed to `Documents (environment)` (or removed when empty) and
+/// the link to ~/Documents comes back. Nothing is deleted silently.
+///
+/// wineboot only creates the link when the path does not exist, so a real folder survives the
+/// Windows setup re-running on an engine move or a repair.
+public enum UserFolders {
+ public static let folder = "Documents"
+ public static let keptName = "Documents (environment)"
+
+ /// What the folder is right now, so the UI can say it and a switch can be a no-op.
+ public enum Shape: Equatable, Sendable { case linked(to: String), inside, missing }
+
+ /// Every real user directory under drive_c/users: Wine's own `Public` is skipped, and so is
+ /// anything that is not a directory.
+ public static func userDirectories(driveC: URL) -> [URL] {
+ let users = driveC.appending(path: "users", directoryHint: .isDirectory)
+ let entries = (try? FileManager.default.contentsOfDirectory(at: users, includingPropertiesForKeys: [.isDirectoryKey], options: [.skipsHiddenFiles])) ?? []
+ return entries.filter { url in
+ url.lastPathComponent != "Public"
+ && ((try? url.resourceValues(forKeys: [.isDirectoryKey]).isDirectory) ?? false)
+ }.sorted { $0.lastPathComponent < $1.lastPathComponent }
+ }
+
+ public static func shape(userDirectory: URL) -> Shape {
+ let path = userDirectory.appending(path: folder).path
+ if let target = try? FileManager.default.destinationOfSymbolicLink(atPath: path) { return .linked(to: target) }
+ var isDir: ObjCBool = false
+ if FileManager.default.fileExists(atPath: path, isDirectory: &isDir), isDir.boolValue { return .inside }
+ return .missing
+ }
+
+ /// Puts Documents in the asked shape in one user directory. `host` is the macOS Documents the
+ /// link points at when going back (the same one wineboot would pick). Returns one line per
+ /// change for the log, none when nothing had to move.
+ @discardableResult
+ public static func set(inside: Bool, userDirectory: URL, host: URL) throws -> [String] {
+ let fm = FileManager.default
+ let doc = userDirectory.appending(path: folder, directoryHint: .isDirectory)
+ let kept = userDirectory.appending(path: keptName, directoryHint: .isDirectory)
+ var notes: [String] = []
+ switch (shape(userDirectory: userDirectory), inside) {
+ case (.inside, true), (.linked, false):
+ return []
+ case (.linked, true), (.missing, true):
+ if fm.fileExists(atPath: doc.path) || (try? fm.destinationOfSymbolicLink(atPath: doc.path)) != nil {
+ try fm.removeItem(at: doc)
+ }
+ if fm.fileExists(atPath: kept.path) {
+ try fm.moveItem(at: kept, to: doc)
+ notes.append("\(userDirectory.lastPathComponent): Documents is inside the environment again, the folder kept from before is back")
+ } else {
+ try fm.createDirectory(at: doc, withIntermediateDirectories: true)
+ notes.append("\(userDirectory.lastPathComponent): Documents is a folder inside the environment now; files already in \(host.path) stay there")
+ }
+ case (.inside, false), (.missing, false):
+ if fm.fileExists(atPath: doc.path) {
+ let contents = (try? fm.contentsOfDirectory(atPath: doc.path)) ?? []
+ if contents.isEmpty {
+ try fm.removeItem(at: doc)
+ } else {
+ if fm.fileExists(atPath: kept.path) { try fm.removeItem(at: kept) }
+ try fm.moveItem(at: doc, to: kept)
+ notes.append("\(userDirectory.lastPathComponent): the environment's Documents is kept as \(keptName)")
+ }
+ }
+ try fm.createSymbolicLink(at: doc, withDestinationURL: host)
+ notes.append("\(userDirectory.lastPathComponent): Documents links to \(host.path) again")
+ }
+ return notes
+ }
+
+ /// The same for every user directory of a prefix.
+ @discardableResult
+ public static func set(inside: Bool, driveC: URL, host: URL = FileManager.default.homeDirectoryForCurrentUser.appending(path: "Documents")) throws -> [String] {
+ try userDirectories(driveC: driveC).flatMap { try set(inside: inside, userDirectory: $0, host: host) }
+ }
+
+ /// True when a user directory holds a Documents folder inside with something in it: what a
+ /// deletion would take with it.
+ public static func hasFilesInside(driveC: URL) -> Bool {
+ userDirectories(driveC: driveC).contains { dir in
+ guard shape(userDirectory: dir) == .inside else { return false }
+ return !((try? FileManager.default.contentsOfDirectory(atPath: dir.appending(path: folder).path)) ?? []).isEmpty
+ }
+ }
+}
diff --git a/Sources/highball/main.swift b/Sources/highball/main.swift
index c603727..97a31bb 100644
--- a/Sources/highball/main.swift
+++ b/Sources/highball/main.swift
@@ -138,7 +138,7 @@ struct Bottle: AsyncParsableCommand {
}
struct Set: AsyncParsableCommand {
- static let configuration = CommandConfiguration(abstract: "Change a bottle setting: engine, renderer, winver, sync, hud, avx, dpi, dxvkasync, framegen, framegenadaptive, framegenflow, framegenperformance, framegenforcevsync, dlloverrides, env KEY=VALUE (empty VALUE removes)")
+ static let configuration = CommandConfiguration(abstract: "Change a bottle setting: engine, renderer, winver, sync, hud, avx, dpi, dxvkasync, framegen, framegenadaptive, framegenflow, framegenperformance, framegenforcevsync, dlloverrides, keepfiles on|off, env KEY=VALUE (empty VALUE removes)")
@Argument var name: String
@Argument var setting: String
@Argument var value: String
@@ -176,6 +176,11 @@ struct Bottle: AsyncParsableCommand {
b.settings.frameGenFlowScale = f
case "dlloverrides":
b.settings.dllOverrides = value
+ case "keepfiles":
+ // Discussion #157: Documents inside the environment (on) or linked to ~/Documents (off).
+ let on = (value == "1" || value == "true" || value == "on")
+ for line in try UserFolders.set(inside: on, driveC: b.driveC) { print(line) }
+ b.settings.keepFilesInside = on
case "dpi":
guard let scale = Int(value) else { fail("dpi expects a number (96..240; 96 = 100%, 192 = 200%)") }
b.settings.dpiScale = scale
diff --git a/Tests/HighballKitTests/UserFoldersTests.swift b/Tests/HighballKitTests/UserFoldersTests.swift
new file mode 100644
index 0000000..3f3fabc
--- /dev/null
+++ b/Tests/HighballKitTests/UserFoldersTests.swift
@@ -0,0 +1,97 @@
+import XCTest
+@testable import HighballKit
+
+/// Discussion #157: game saves inside the environment instead of ~/Documents. The switch must
+/// never lose a file in either direction, must be a no-op when the shape already matches, and
+/// must restore what an earlier switch back set aside.
+final class UserFoldersTests: XCTestCase {
+ private var root: URL!
+ private var driveC: URL!
+ private var host: URL!
+ private var user: URL!
+
+ override func setUpWithError() throws {
+ root = FileManager.default.temporaryDirectory.appending(path: "userfolders-\(UUID().uuidString)")
+ driveC = root.appending(path: "drive_c")
+ host = root.appending(path: "host-Documents")
+ user = driveC.appending(path: "users/tester")
+ try FileManager.default.createDirectory(at: user, withIntermediateDirectories: true)
+ try FileManager.default.createDirectory(at: driveC.appending(path: "users/Public"), withIntermediateDirectories: true)
+ try FileManager.default.createDirectory(at: host, withIntermediateDirectories: true)
+ // What wineboot leaves: Documents is a link to the Mac's folder.
+ try FileManager.default.createSymbolicLink(at: user.appending(path: "Documents"), withDestinationURL: host)
+ }
+
+ override func tearDownWithError() throws {
+ try? FileManager.default.removeItem(at: root)
+ }
+
+ private func write(_ name: String, in dir: URL) throws {
+ try Data("x".utf8).write(to: dir.appending(path: name))
+ }
+
+ func testStartsLinkedAndOnlyRealUsersCount() {
+ XCTAssertEqual(UserFolders.shape(userDirectory: user), .linked(to: host.path))
+ XCTAssertEqual(UserFolders.userDirectories(driveC: driveC).map(\.lastPathComponent), ["tester"], "Public is Wine's, never touched")
+ XCTAssertFalse(UserFolders.hasFilesInside(driveC: driveC))
+ }
+
+ func testInsideReplacesTheLinkAndLeavesHostFilesWhereTheyAre() throws {
+ try write("FromSoftware.txt", in: host)
+ let notes = try UserFolders.set(inside: true, driveC: driveC, host: host)
+ XCTAssertEqual(notes.count, 1)
+ XCTAssertEqual(UserFolders.shape(userDirectory: user), .inside)
+ XCTAssertNil(try? FileManager.default.destinationOfSymbolicLink(atPath: user.appending(path: "Documents").path))
+ XCTAssertTrue(FileManager.default.fileExists(atPath: host.appending(path: "FromSoftware.txt").path), "the Mac's Documents is not moved")
+ XCTAssertEqual(try FileManager.default.contentsOfDirectory(atPath: user.appending(path: "Documents").path), [], "starts empty inside")
+ XCTAssertEqual(try UserFolders.set(inside: true, driveC: driveC, host: host), [], "already inside: nothing to do")
+ }
+
+ func testBackToLinkedKeepsTheInsideFolderWhenItHasFiles() throws {
+ try UserFolders.set(inside: true, driveC: driveC, host: host)
+ try write("save.es3", in: user.appending(path: "Documents"))
+ XCTAssertTrue(UserFolders.hasFilesInside(driveC: driveC))
+ let notes = try UserFolders.set(inside: false, driveC: driveC, host: host)
+ XCTAssertEqual(notes.count, 2)
+ XCTAssertEqual(UserFolders.shape(userDirectory: user), .linked(to: host.path))
+ XCTAssertTrue(FileManager.default.fileExists(atPath: user.appending(path: "Documents (environment)/save.es3").path), "nothing is deleted silently")
+ XCTAssertFalse(UserFolders.hasFilesInside(driveC: driveC))
+ }
+
+ func testBackToLinkedDropsAnEmptyInsideFolder() throws {
+ try UserFolders.set(inside: true, driveC: driveC, host: host)
+ try UserFolders.set(inside: false, driveC: driveC, host: host)
+ XCTAssertFalse(FileManager.default.fileExists(atPath: user.appending(path: "Documents (environment)").path))
+ XCTAssertEqual(UserFolders.shape(userDirectory: user), .linked(to: host.path))
+ XCTAssertEqual(try UserFolders.set(inside: false, driveC: driveC, host: host), [], "already linked: nothing to do")
+ }
+
+ func testInsideAgainRestoresTheKeptFolder() throws {
+ try UserFolders.set(inside: true, driveC: driveC, host: host)
+ try write("save.es3", in: user.appending(path: "Documents"))
+ try UserFolders.set(inside: false, driveC: driveC, host: host)
+ let notes = try UserFolders.set(inside: true, driveC: driveC, host: host)
+ XCTAssertEqual(notes.count, 1)
+ XCTAssertTrue(FileManager.default.fileExists(atPath: user.appending(path: "Documents/save.es3").path), "the kept folder is Documents again")
+ XCTAssertFalse(FileManager.default.fileExists(atPath: user.appending(path: "Documents (environment)").path))
+ }
+
+ func testAMissingFolderIsCreatedOrLinkedAsAsked() throws {
+ try FileManager.default.removeItem(at: user.appending(path: "Documents"))
+ XCTAssertEqual(UserFolders.shape(userDirectory: user), .missing)
+ try UserFolders.set(inside: false, driveC: driveC, host: host)
+ XCTAssertEqual(UserFolders.shape(userDirectory: user), .linked(to: host.path))
+ try FileManager.default.removeItem(at: user.appending(path: "Documents"))
+ try UserFolders.set(inside: true, driveC: driveC, host: host)
+ XCTAssertEqual(UserFolders.shape(userDirectory: user), .inside)
+ }
+
+ func testSettingDecodesWithoutTheKeyAndRoundTrips() throws {
+ let json = #"{"name":"g","engineID":"e"}"#
+ let s = try JSONDecoder().decode(BottleSettings.self, from: Data(json.utf8))
+ XCTAssertFalse(s.keepFilesInside, "existing environments keep the link")
+ var on = s; on.keepFilesInside = true
+ let back = try JSONDecoder().decode(BottleSettings.self, from: JSONEncoder().encode(on))
+ XCTAssertTrue(back.keepFilesInside)
+ }
+}