Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

An app whose launcher does not read the saved choices still gets them: DMSettings in Info.plist says whether it does

v0lumehid270a26d7f8c
diff --git a/frontend/Sources/NSPortBuilder/LauncherView.swift b/frontend/Sources/NSPortBuilder/LauncherView.swift
index 14505af7..2b84a00a 100644
--- a/frontend/Sources/NSPortBuilder/LauncherView.swift
+++ b/frontend/Sources/NSPortBuilder/LauncherView.swift
@@ -643,7 +643,9 @@ struct ContentView: View {
library.migrateLaunchOptions()
let settings = LaunchSetting.forMac(app: game.appPath, titleID: game.titleID, repo: m.repoPath, labels: m.t) ?? []
let mods = GameMod.list(in: GameMod.folder(for: game, library: library.folder), titleID: game.titleID)
- let environment = game.startEnvironment(controls: controls.environment, settings: settings, mods: mods)
+ let environment = game.startEnvironment(controls: controls.environment, settings: settings, mods: mods,
+ store: SavedChoices(app: game.appPath, titleID: game.titleID),
+ appReadsStore: SavedChoices.appReadsStore(app: game.appPath))
if !environment.isEmpty { configuration.environment = environment }
NSWorkspace.shared.openApplication(at: URL(fileURLWithPath: game.appPath), configuration: configuration) { _, error in
DispatchQueue.main.async {
diff --git a/frontend/Sources/NSPortBuilder/SavedChoices.swift b/frontend/Sources/NSPortBuilder/SavedChoices.swift
index b3aca9b5..8f802bc7 100644
--- a/frontend/Sources/NSPortBuilder/SavedChoices.swift
+++ b/frontend/Sources/NSPortBuilder/SavedChoices.swift
@@ -119,12 +119,32 @@ extension LaunchSetting {
}
}
+extension SavedChoices {
+ /// Whether the app's launcher reads the store itself. seedds/nxnative's pack_app.py writes `DMSettings` (the rules launcher.c checks
+ /// a saved choice against) into Info.plist only together with that reading (#431), so its presence means "the store applies" and an
+ /// app without it has an older launcher.c that ignores the preferences domain. Those apps are given the choices in the environment.
+ static func appReadsStore(app: String) -> Bool {
+ let plist = URL(fileURLWithPath: app).appendingPathComponent("Contents/Info.plist")
+ guard let data = try? Data(contentsOf: plist),
+ let info = try? PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any] else { return false }
+ return info["DMSettings"] is [Any]
+ }
+}
+
extension LibraryGame {
- /// The environment a Mac game is started with: launch overrides only, never written to the store. The rows of launcher.json are
- /// saved choices the app reads itself; what is here is the Controls page's layout, what only this launcher has (Display mode),
- /// the mods, and DM_LAUNCHER=0 (this launcher is the settings window, so the app's own is skipped; saved choices still apply).
- func startEnvironment(controls: [String: String], settings: [LaunchSetting], mods: [GameMod]) -> [String: String] {
+ /// The environment a Mac game is started with: launch overrides, never written to the store. The rows of launcher.json are saved
+ /// choices; an app whose launcher reads the store (`appReadsStore`) gets none of them here, since a value in the environment would
+ /// override a choice made elsewhere. An older app does not read it, so it is given the saved non-default choices as variables, as
+ /// this launcher always did. The rest is the Controls page's layout, what only this launcher has (Display mode), the mods, and
+ /// DM_LAUNCHER=0 (this launcher is the settings window, so the app's own is skipped; saved choices still apply).
+ func startEnvironment(controls: [String: String], settings: [LaunchSetting], mods: [GameMod],
+ store: SavedChoices? = nil, appReadsStore: Bool = true) -> [String: String] {
var environment = controls
+ if !appReadsStore, let store {
+ for s in settings where s.storeBacked {
+ if let v = store.value(s), !s.isDefault(v) { environment[s.env] = v }
+ }
+ }
environment.merge(launchEnvironment(settings)) { _, own in own }
environment.merge(modEnvironment(mods)) { _, mods in mods }
environment["DM_LAUNCHER"] = "0"
diff --git a/frontend/Tests/NSPortBuilderTests/SavedChoicesTests.swift b/frontend/Tests/NSPortBuilderTests/SavedChoicesTests.swift
index 9cc990d6..5c66b4cc 100644
--- a/frontend/Tests/NSPortBuilderTests/SavedChoicesTests.swift
+++ b/frontend/Tests/NSPortBuilderTests/SavedChoicesTests.swift
@@ -26,7 +26,7 @@ final class SavedChoicesTests: XCTestCase {
private let scale = #"{"env": "DM_RENDER_SCALE", "kind": "range", "section": "Game", "label": "Render scale", "note": "", "default": "1.0", "min": 0.5, "max": 1.0, "step": 0.05}"#
private let hold = #"{"env": "DM_FPS_HOLD", "kind": "toggle", "section": "Performance", "label": "Hold", "note": "", "default": "on", "on": "on", "off": "off"}"#
- private func app(_ rows: [String], schema: Int? = 1, bundle: Bool = true) throws -> String {
+ private func app(_ rows: [String], schema: Int? = 1, bundle: Bool = true, dmSettings: Bool = false) throws -> String {
let app = temporary.appendingPathComponent("Game.app")
let dm = app.appendingPathComponent("Contents/Resources/dm")
try FileManager.default.createDirectory(at: dm, withIntermediateDirectories: true)
@@ -34,6 +34,7 @@ final class SavedChoicesTests: XCTestCase {
try Data(#"{\#(head)"game": "\#(titleID)", "settings": [\#(rows.joined(separator: ","))]}"#.utf8).write(to: dm.appendingPathComponent("launcher.json"))
var info: [String: Any] = ["DMTitleID": titleID]
if bundle { info["CFBundleIdentifier"] = bundleID }
+ if dmSettings { info["DMSettings"] = Self.rules(rows) }
try PropertyListSerialization.data(fromPropertyList: info, format: .xml, options: 0).write(to: app.appendingPathComponent("Contents/Info.plist"))
return app.path
}
@@ -174,4 +175,70 @@ final class SavedChoicesTests: XCTestCase {
XCTAssertEqual(env, ["DM_LAUNCHER": "0", "DM_FULLSCREEN": "1"], "a launcher-only setting is an override, never written to the store")
XCTAssertNil(store.raw("DM_FULLSCREEN"))
}
+
+ /// DMSettings as seedds/nxnative's dm/mac/pack_app.py settings_rules writes it.
+ private static func rules(_ rows: [String]) -> [[String: Any]] {
+ rows.compactMap { row in
+ guard let s = try? JSONDecoder().decode(LaunchSetting.self, from: Data(row.utf8)) else { return nil }
+ switch s.kind {
+ case "range": return ["env": s.env, "min": s.min ?? 0, "max": s.max ?? 0]
+ case "choice": return ["env": s.env, "values": (s.options ?? []).map { $0[0] }]
+ default: return ["env": s.env, "values": [s.on ?? "1", s.off ?? "0"]]
+ }
+ }
+ }
+
+ /// dm/mac/launcher.c's saved_accepted, line for line: one of the rule's values, else a number from min to max.
+ private static func launcherCAccepts(_ rule: [String: Any], _ v: String) -> Bool {
+ if let values = rule["values"] as? [String] { return values.contains(v) }
+ guard let lo = rule["min"] as? Double, let hi = rule["max"] as? Double else { return false }
+ return v.withCString { c -> Bool in
+ var end: UnsafeMutablePointer<CChar>?
+ let x = strtod(c, &end)
+ let space = isspace(Int32(UInt8(bitPattern: c.pointee))) != 0
+ return c.pointee != 0 && !space && strpbrk(c, "xX") == nil && end?.pointee == 0 && x == x && x >= lo && x <= hi
+ }
+ }
+
+ func testWhatIsWrittenPassesTheRulesLauncherCChecks() throws {
+ let wide = #"{"env": "DM_RENDER_SCALE", "kind": "range", "section": "Picture", "label": "Render scale", "note": "", "default": "1.0", "min": 0.5, "max": 1.35, "step": 0.05}"#
+ let rows = [fps, wide, hold]
+ let path = try app(rows, dmSettings: true)
+ let list = try XCTUnwrap(LaunchSetting.read(app: path))
+ let info = try XCTUnwrap(PropertyListSerialization.propertyList(from: Data(contentsOf: URL(fileURLWithPath: path + "/Contents/Info.plist")), format: nil) as? [String: Any])
+ let rules = try XCTUnwrap(info["DMSettings"] as? [[String: Any]])
+ let store = SavedChoices(domain: bundleID)
+ // What the sheet writes: a slider's "%.2f" at every step, the options, on and off
+ var written: [(LaunchSetting, String)] = []
+ for n in stride(from: 0.5, through: 1.35, by: 0.05) { written.append((list[1], String(format: "%.2f", n))) }
+ written += [(list[0], "60"), (list[2], "off")]
+ for (s, v) in written where !s.isDefault(v) {
+ store.set(s, v)
+ let stored = try XCTUnwrap(store.raw(s.env) as? String, "\(s.env)=\(v) is kept")
+ let rule = try XCTUnwrap(rules.first { $0["env"] as? String == s.env })
+ XCTAssertTrue(Self.launcherCAccepts(rule, stored), "launcher.c accepts \(s.env)=\(stored)")
+ XCTAssertTrue(s.accepts(stored))
+ }
+ // The two readers agree on what they refuse
+ let rule = try XCTUnwrap(rules.first { $0["env"] as? String == "DM_RENDER_SCALE" })
+ for v in ["0x1", " 0.8", "nan", "", "0.4", "1.36", "1e0x", "1", "1.", "+1", "0.5", "1.35", "1e0", "inf"] {
+ XCTAssertEqual(Self.launcherCAccepts(rule, v), list[1].accepts(v), "range value '\(v)'")
+ }
+ }
+
+ func testAppReadsTheStoreOnlyWithDMSettings() throws {
+ let withRules = try app([fps, scale, hold], dmSettings: true)
+ XCTAssertTrue(SavedChoices.appReadsStore(app: withRules))
+ let older = try app([fps, scale, hold])
+ XCTAssertFalse(SavedChoices.appReadsStore(app: older))
+ let store = SavedChoices(domain: bundleID)
+ let list = try XCTUnwrap(LaunchSetting.read(app: older))
+ store.set(list[0], "60"); store.set(list[1], "0.75")
+ let game = LibraryGame(titleID: titleID, name: "Z", appPath: older)
+ XCTAssertEqual(game.startEnvironment(controls: [:], settings: list, mods: [], store: store, appReadsStore: true),
+ ["DM_LAUNCHER": "0"], "the app reads the store itself: nothing for the rows")
+ XCTAssertEqual(game.startEnvironment(controls: [:], settings: list, mods: [], store: store, appReadsStore: false),
+ ["DM_LAUNCHER": "0", "DM_FPS_TARGET": "60", "DM_RENDER_SCALE": "0.75"],
+ "an older launcher.c: the saved non-default choices go in the environment, defaults stay out")
+ }
}