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
Prepare archived iOS shaders atomically before launching
MasterFlowerf3ed48e727f1
diff --git a/frontend-ios/Sources/ShellHost/EngineLaunchPlan.swift b/frontend-ios/Sources/ShellHost/EngineLaunchPlan.swiftindex 851404bd..0e78c74a 100644--- a/frontend-ios/Sources/ShellHost/EngineLaunchPlan.swift+++ b/frontend-ios/Sources/ShellHost/EngineLaunchPlan.swift@@ -42,7 +42,8 @@ public struct EngineLaunchPlan: Equatable, Sendable {env["DM_TEMP"] = temp.pathenv["DM_NVN_SHMAP"] = fw.appendingPathComponent(manifest.shaderMap).pathlet ownMSL = gdir.appendingPathComponent("msl", isDirectory: true)- env["DM_MSL_DIR"] = (isDirectory(ownMSL) ? ownMSL : fw.appendingPathComponent("msl", isDirectory: true)).path+ let bundledMSL = game.shaderResourcesURL ?? fw.appendingPathComponent("msl", isDirectory: true)+ env["DM_MSL_DIR"] = (isDirectory(ownMSL) ? ownMSL : bundledMSL).pathlet ownPSO = gdir.appendingPathComponent("pso", isDirectory: true), fwPSO = fw.appendingPathComponent("pso", isDirectory: true)if isDirectory(ownPSO) { env["DM_PSO_BUNDLE"] = ownPSO.path } else if isDirectory(fwPSO) { env["DM_PSO_BUNDLE"] = fwPSO.path }let dataAOC = game.dataURL.appendingPathComponent("aoc", isDirectory: true), ownAOC = gdir.appendingPathComponent("aoc", isDirectory: true)diff --git a/frontend-ios/Sources/ShellHost/GameLauncher.swift b/frontend-ios/Sources/ShellHost/GameLauncher.swiftindex 279a663d..68c52478 100644--- a/frontend-ios/Sources/ShellHost/GameLauncher.swift+++ b/frontend-ios/Sources/ShellHost/GameLauncher.swift@@ -14,6 +14,8 @@ public struct LaunchableGame: Equatable, Sendable {public var dataURL: URL/// The app's `Documents/games/<title ID>/`: save, temp, log.txt.public var appFolder: URL+ /// The exported framework's shaders, unpacked for this launch when the player has no override.+ public var shaderResourcesURL: URL? = nil/// Every `DM_*` switch with the player's value or the tested default, as `launcher.<VAR>` would set it.public var environment: [String: String]/// The picked cloud-save folder, when cloud saves are on.diff --git a/frontend-ios/Sources/ShellHost/ShaderResources.swift b/frontend-ios/Sources/ShellHost/ShaderResources.swiftnew file mode 100644index 00000000..257b83d8--- /dev/null+++ b/frontend-ios/Sources/ShellHost/ShaderResources.swift@@ -0,0 +1,63 @@+import AppleArchive+import Foundation+import System++/// The export packs the shader tree into one archive per framework to keep the IPA within sideloaders' ZIP limits.+actor ShaderResources {+ static let shared = ShaderResources()++ func prepare(_ game: LaunchableGame) throws -> URL? {+ let fm = FileManager.default+ var cache = game.appFolder.appendingPathComponent(".shader-resources", isDirectory: true)+ let directories = ((try? fm.contentsOfDirectory(at: cache, includingPropertiesForKeys: [.isDirectoryKey])) ?? [])+ .filter { (try? $0.resourceValues(forKeys: [.isDirectoryKey]).isDirectory) == true }+ // Only this actor extracts into this cache. A partial directory present on entry survived an interrupted launch.+ for partial in directories where Self.hasUUIDName(partial.lastPathComponent, prefix: ".partial-") {+ try? fm.removeItem(at: partial)+ }++ let override = game.appFolder.appendingPathComponent("msl", isDirectory: true)+ if (try? override.resourceValues(forKeys: [.isDirectoryKey]).isDirectory) == true { return nil }++ let files = try fm.contentsOfDirectory(at: game.frameworkURL, includingPropertiesForKeys: nil)+ let archives = files.filter { $0.pathExtension == "aar" && Self.hasUUIDName($0.deletingPathExtension().lastPathComponent, prefix: "msl-") }+ guard let archive = archives.first else { return nil } // Older IPAs and direct installs have loose shaders.+ guard archives.count == 1 else { throw CocoaError(.fileReadCorruptFile) }++ let destination = cache.appendingPathComponent(archive.deletingPathExtension().lastPathComponent, isDirectory: true)+ if (try? destination.resourceValues(forKeys: [.isDirectoryKey]).isDirectory) != true {+ try fm.createDirectory(at: cache, withIntermediateDirectories: true)+ var attributes = URLResourceValues()+ attributes.isExcludedFromBackup = true+ try cache.setResourceValues(attributes)++ let temporary = cache.appendingPathComponent(".partial-" + UUID().uuidString, isDirectory: true)+ try fm.createDirectory(at: temporary, withIntermediateDirectories: false)+ defer { try? fm.removeItem(at: temporary) }+ try extract(archive, to: temporary)+ try fm.moveItem(at: temporary, to: destination)+ }+ // A new archive UUID comes from a new installed app. No running game can still use the old app's archive.+ for old in directories where old.lastPathComponent != destination.lastPathComponent &&+ Self.hasUUIDName(old.lastPathComponent, prefix: "msl-") {+ try? fm.removeItem(at: old)+ }+ return destination+ }++ private static func hasUUIDName(_ name: String, prefix: String) -> Bool {+ name.hasPrefix(prefix) && UUID(uuidString: String(name.dropFirst(prefix.count))) != nil+ }++ private func extract(_ archive: URL, to directory: URL) throws {+ try ArchiveByteStream.withFileStream(path: FilePath(archive.path), mode: .readOnly, options: [], permissions: []) { file in+ try ArchiveByteStream.withDecompressionStream(readingFrom: file) { decompressed in+ try ArchiveStream.withDecodeStream(readingFrom: decompressed) { input in+ try ArchiveStream.withExtractStream(extractingTo: FilePath(directory.path), flags: [.ignoreOperationNotPermitted]) { output in+ _ = try ArchiveStream.process(readingFrom: input, writingTo: output)+ }+ }+ }+ }+ }+}diff --git a/frontend-ios/Sources/ShellHost/ShellHostModel.swift b/frontend-ios/Sources/ShellHost/ShellHostModel.swiftindex 02898cb0..6c4e6a8b 100644--- a/frontend-ios/Sources/ShellHost/ShellHostModel.swift+++ b/frontend-ios/Sources/ShellHost/ShellHostModel.swift@@ -64,6 +64,7 @@ public struct ShellHostConfig: Sendable {@ObservationIgnored private let config: ShellHostConfig@ObservationIgnored private let launcher: any GameLauncher+ @ObservationIgnored private var launchingGame = false@ObservationIgnored private let picker: any FolderPicker@ObservationIgnored private let adapter = GamepadAdapter()@ObservationIgnored private var dataStores: [String: BookmarkStore] = [:]@@ -445,8 +446,10 @@ public struct ShellHostConfig: Sendable {}func playNow(_ id: String) async {- guard let entry = entry(id), let manifest = entry.manifest, let profile = entry.profile, let framework = entry.frameworkURL,+ guard !launchingGame, let entry = entry(id), let manifest = entry.manifest, let profile = entry.profile, let framework = entry.frameworkURL,details.first(where: { $0.id == id })?.game.state == .playable, let dataURL = data(for: entry).url else { return }+ launchingGame = true+ defer { launchingGame = false }var cloud: URL?switch cloudSaves(id) {case .own(let url), .shell(let url): cloud = url@@ -455,11 +458,13 @@ public struct ShellHostConfig: Sendable {// Remembered when Play is triggered, even if the launcher then fails (the stub). The order changes at the next load.LastPlayedStore(defaults: config.defaults(id)).record(config.now())refresh(id)- let game = LaunchableGame(+ var game = LaunchableGame(titleID: id, name: entry.title, manifest: manifest, directory: entry.directory, frameworkURL: framework, dataURL: dataURL,appFolder: appFolder(for: id), environment: launchEnvironment(profile: profile, id: id), cloudSavesURL: cloud)- do { try await launcher.launch(game) }- catch { notice = error.localizedDescription }+ do {+ game.shaderResourcesURL = try await ShaderResources.shared.prepare(game)+ try await launcher.launch(game)+ } catch { notice = error.localizedDescription }}/// A game's values for the engine: its own choices, then the shell's defaults. A game whose settings list has no