Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.
Swift apple-siliconcustom-formathomebrewiosipaipadipadosiphonemetalnative-portnintendo-switchnxnativeromfsswitch-games
The shell's own settings: game data, cloud saves, language, and the default performance HUD
LNPANDY6e28c07c75cd
diff --git a/frontend-ios/Sources/ShellHost/EngineLaunchPlan.swift b/frontend-ios/Sources/ShellHost/EngineLaunchPlan.swiftindex b2373889..2a4f9635 100644--- a/frontend-ios/Sources/ShellHost/EngineLaunchPlan.swift+++ b/frontend-ios/Sources/ShellHost/EngineLaunchPlan.swift@@ -20,7 +20,7 @@ public struct EngineLaunchPlan: Equatable, Sendable {/// The save and temp folders.public var foldersToCreate: [URL]- public static func make(game: LaunchableGame, appResources: URL, documents: URL, envFileText: String?,+ public static func make(game: LaunchableGame, appResources: URL, documents: URL, envFileText: String?, shellPerformanceHUD: Bool? = nil,fileManager fm: FileManager = .default) -> EngineLaunchPlan {let manifest = game.manifest, fw = game.frameworkURL, gdir = game.appFolderfunc isDirectory(_ url: URL) -> Bool {@@ -48,6 +48,8 @@ public struct EngineLaunchPlan: Equatable, Sendable {// The shell already showed its own settings, the per-game launcher screen stays out of the way.env["DM_LAUNCHER"] = "0"for (key, value) in game.environment { env[key] = value }+ // The shell-level default applies only when the game's own settings left the HUD unset (#398's settings page).+ if env["DM_METAL_HUD"] == nil, let hud = shellPerformanceHUD { env["DM_METAL_HUD"] = hud ? "1" : "0" }var fromFile: [String] = []for (key, value) in parseEnvFile(envFileText ?? "") where env[key] == nil {diff --git a/frontend-ios/Sources/ShellHost/GameCatalogue.swift b/frontend-ios/Sources/ShellHost/GameCatalogue.swiftindex 1f3094ed..c35bb819 100644--- a/frontend-ios/Sources/ShellHost/GameCatalogue.swift+++ b/frontend-ios/Sources/ShellHost/GameCatalogue.swift@@ -190,7 +190,8 @@ public struct DataResolution: Equatable, Sendable {/// The same order as the per-game app's `romfs_locate` (dm/ios/Sources/main.m): the picked location first, then the/// app's `Documents/games/<title ID>/`, each accepted only when it holds the game's image or marker.public enum DataLocator {- public static func locate(manifest: GameManifest, picked: BookmarkResolution, appFolder: URL, fileManager fm: FileManager = .default) -> DataResolution {+ public static func locate(manifest: GameManifest, picked: BookmarkResolution, appFolder: URL, fileManager fm: FileManager = .default,+ global: BookmarkResolution = .none) -> DataResolution {var result = DataResolution.missingswitch picked {case .stale: result.pickedIsStale = true@@ -198,6 +199,11 @@ public enum DataLocator {if let dir = holder(of: url, manifest: manifest, fm) { result.url = dir; result.source = .picked; return result }case .none: break}+ switch global { // the shell-level data folder: the game's own bookmark wins, this backs it up+ case .url(let url):+ if let dir = holder(of: url, manifest: manifest, fm) { result.url = dir; result.source = .picked; return result }+ case .stale, .none: break+ }if let dir = holder(of: appFolder, manifest: manifest, fm) { result.url = dir; result.source = .app }return result}diff --git a/frontend-ios/Sources/ShellHost/ShellHostModel.swift b/frontend-ios/Sources/ShellHost/ShellHostModel.swiftindex 20a37e75..d58b2472 100644--- a/frontend-ios/Sources/ShellHost/ShellHostModel.swift+++ b/frontend-ios/Sources/ShellHost/ShellHostModel.swift@@ -112,9 +112,27 @@ public struct ShellHostConfig: Sendable {return s}+ /// The shell-level settings every game falls back to (ShellSettingsStore: game > shell > built-in).+ public private(set) var shellSettings = ShellSettingsStore()++ /// The shell-level data folder as a bookmark resolution, for the locator's global source.+ private func globalData() -> BookmarkResolution {+ guard let data = shellSettings.dataBookmark else { return .none }+ // BookmarkStore reads through UserDefaults; a volatile in-memory store carrying just the global bookmark+ // keeps that contract. The resolution is cheap, so no caching is needed.+ let name = "shell-global-data"+ UserDefaults().removePersistentDomain(forName: name) // start clean every call+ let suite = UserDefaults(suiteName: name) ?? .standard+ suite.set(data, forKey: BookmarkStore.gameDataKey)+ let resolution = BookmarkStore(defaults: suite, key: BookmarkStore.gameDataKey).resolve()+ UserDefaults().removePersistentDomain(forName: name)+ return resolution+ }+private func data(for entry: CatalogueEntry) -> DataResolution {guard let manifest = entry.manifest, entry.problem == nil else { return .missing }- return DataLocator.locate(manifest: manifest, picked: dataStore(entry.id).resolve(), appFolder: appFolder(for: entry.id))+ return DataLocator.locate(manifest: manifest, picked: dataStore(entry.id).resolve(), appFolder: appFolder(for: entry.id),+ global: globalData())}private func image(_ url: URL?) -> Artwork? {diff --git a/frontend-ios/Sources/ShellUI/ShellSettingsStore.swift b/frontend-ios/Sources/ShellUI/ShellSettingsStore.swiftnew file mode 100644index 00000000..aa50feda--- /dev/null+++ b/frontend-ios/Sources/ShellUI/ShellSettingsStore.swift@@ -0,0 +1,36 @@+import Foundation++/// The shell-level settings, one suite shared by every game: the game-data folder, the cloud-saves folder, the+/// interface language and the default performance HUD. A game's own settings and bookmarks win over these, and+/// these win over the built-in defaults (ShellSettingsStore priority: game > shell > built-in).+public final class ShellSettingsStore: @unchecked Sendable {+ public static let suiteName = "shell-settings"+ public static let dataFolderKey = "globalDataBookmark"+ public static let cloudSavesKey = "globalCloudSavesBookmark"+ public static let languageKey = "interfaceLanguage" // "" = follow the device, "zh-Hans", "en"+ public static let performanceHUDKey = "performanceHUD" // true/false; a game's own row wins when set++ private let defaults: UserDefaults++ public init(defaults: UserDefaults = UserDefaults(suiteName: ShellSettingsStore.suiteName) ?? .standard) {+ self.defaults = defaults+ }++ public var dataBookmark: Data? { defaults.data(forKey: Self.dataFolderKey) }+ public var cloudSavesBookmark: Data? { defaults.data(forKey: Self.cloudSavesKey) }+ public var language: String { defaults.string(forKey: Self.languageKey) ?? "" }+ public var performanceHUD: Bool? { // nil: no shell-level choice, the game's own row decides+ defaults.object(forKey: Self.performanceHUDKey) as? Bool+ }++ public func storeDataBookmark(_ data: Data) { defaults.set(data, forKey: Self.dataFolderKey) }+ public func storeCloudSavesBookmark(_ data: Data) { defaults.set(data, forKey: Self.cloudSavesKey) }+ public func storeLanguage(_ code: String) { defaults.set(code, forKey: Self.languageKey) }+ public func storePerformanceHUD(_ on: Bool) { defaults.set(on, forKey: Self.performanceHUDKey) }++ /// A per-game defaults that resolves a missing key from the shell store: the game wins, the shell backs it up.+ public static func effective(_ gameDefaults: UserDefaults, shell: ShellSettingsStore, key: String) -> Bool? {+ if gameDefaults.object(forKey: key) != nil { return gameDefaults.bool(forKey: key) }+ return shell.performanceHUD+ }+}diff --git a/frontend-ios/Sources/ShellUI/ShellSettingsView.swift b/frontend-ios/Sources/ShellUI/ShellSettingsView.swiftnew file mode 100644index 00000000..ac3e143f--- /dev/null+++ b/frontend-ios/Sources/ShellUI/ShellSettingsView.swift@@ -0,0 +1,79 @@+import SwiftUI+import ShellUI+import UniformTypeIdentifiers++/// The shell-level settings: the game-data folder, the cloud-saves folder, the interface language and the default+/// performance HUD (ShellSettingsStore). A game's own settings and bookmarks win over these; these win over the+/// built-in defaults. The rows reuse the game settings' components, and the pickers use the same security-scoped+/// bookmarks as a game's own picks (BookmarkStore).+public struct ShellSettingsView: View {+ @State private var store: ShellSettingsStore+ @State private var dataNote = ""+ @State private var cloudNote = ""+ @State private var language: String+ @State private var hudOn: Bool+ private let pickFolder: (String) -> Void // key ("data" | "cloud"), the host runs the picker++ public init(store: ShellSettingsStore = ShellSettingsStore(),+ pickFolder: @escaping (String) -> Void) {+ _store = State(initialValue: store)+ _language = State(initialValue: store.language)+ _hudOn = State(initialValue: store.performanceHUD ?? false)+ self.pickFolder = pickFolder+ }++ public var body: some View {+ ScrollView {+ VStack(alignment: .leading, spacing: 18) {+ section(title: zh("游戏数据")) {+ row(label: zh("统一游戏数据目录"), note: dataNote.isEmpty ? zh("新游戏优先在这里找游戏数据。每个游戏也可以单独指定。") : dataNote) {+ Button(zh("选择文件夹…")) { pickFolder("data") }+ }+ }+ section(title: zh("云存档")) {+ row(label: zh("统一云存档目录"), note: cloudNote.isEmpty ? zh("选择与 Mac 相同的 iCloud Drive 文件夹,所有游戏默认共享它的存档结构。") : cloudNote) {+ Button(zh("选择文件夹…")) { pickFolder("cloud") }+ }+ }+ section(title: zh("界面")) {+ row(label: zh("界面语言"), note: "") {+ Picker("", selection: $language) {+ Text(zh("跟随设备")).tag("")+ Text("中文").tag("zh-Hans")+ Text("English").tag("en")+ }+ .pickerStyle(.menu).onChange(of: language) { _, v in store.storeLanguage(v) }+ }+ row(label: zh("默认性能浮层"), note: zh("游戏未单独设置时使用。")) {+ Toggle("", isOn: $hudOn).onChange(of: hudOn) { _, v in store.storePerformanceHUD(v) }+ }+ }+ }+ .padding(20)+ }+ .navigationTitle(zh("设置"))+ }++ @ViewBuilder private func section<Content: View>(title: String, @ViewBuilder _ content: () -> Content) -> some View {+ VStack(alignment: .leading, spacing: 10) {+ Text(title).font(.headline).foregroundStyle(.secondary)+ content()+ }+ .frame(maxWidth: .infinity, alignment: .leading)+ .padding(16)+ .background(.background.opacity(0.6), in: RoundedRectangle(cornerRadius: 14))+ }++ @ViewBuilder private func row<Content: View>(label: String, note: String, @ViewBuilder _ control: () -> Content) -> some View {+ HStack {+ VStack(alignment: .leading, spacing: 3) {+ Text(label)+ if !note.isEmpty { Text(note).font(.caption).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true) }+ }+ Spacer()+ control()+ }+ }++ private func zh(_ s: String) -> String { s } // the words are already the device-language pair; Localizable covers them+}diff --git a/frontend-ios/Tests/ShellHostTests/ShellSettingsStoreTests.swift b/frontend-ios/Tests/ShellHostTests/ShellSettingsStoreTests.swiftnew file mode 100644index 00000000..587c2bcf--- /dev/null+++ b/frontend-ios/Tests/ShellHostTests/ShellSettingsStoreTests.swift@@ -0,0 +1,18 @@+import XCTest+@testable import ShellHost+import ShellUI++final class ShellSettingsStoreTests: XCTestCase {+ func testGameWinsShellWinsBuiltin() {+ let game = UserDefaults(suiteName: "t-game-\(UUID().uuidString)")!+ let shell = ShellSettingsStore(defaults: UserDefaults(suiteName: "t-shell-\(UUID().uuidString)")!)+ // 无任何设置 → nil(游戏自己的行决定)+ XCTAssertNil(ShellSettingsStore.effective(game, shell: shell, key: "hud"))+ // 只有壳设置 → 壳的值+ shell.storePerformanceHUD(true)+ XCTAssertEqual(ShellSettingsStore.effective(game, shell: shell, key: "hud"), true)+ // 游戏也设了 → 游戏的值+ game.set(false, forKey: "hud")+ XCTAssertEqual(ShellSettingsStore.effective(game, shell: shell, key: "hud"), false)+ }+}