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
Launcher: the release manifest and the engine store, the core of engine updates
LNPANDYaf8c69000c60
diff --git a/frontend/Sources/NSPortBuilder/EngineManifest.swift b/frontend/Sources/NSPortBuilder/EngineManifest.swiftnew file mode 100644index 00000000..4b86b7c0--- /dev/null+++ b/frontend/Sources/NSPortBuilder/EngineManifest.swift@@ -0,0 +1,162 @@+import CryptoKit+import Foundation++/// What a release says about the engine it carries (and about the app itself), and how the launcher+/// decides whether to install it.+///+/// The player no longer fetches the repository: the release ships an engine tree — the repository minus+/// the launcher sources (`dm/`, `scripts/`, `docs/`, `ref/`, `.upstream-ref`, `dm/fork_patches.txt`) —+/// and the launcher unpacks it next to its own Application Support folder. That folder then *is* the+/// checkout the build reads (`BuildModel.repoPath`), so `isRepo`, the build script and the RomFS tool+/// keep working unchanged.+///+/// Two channels, one manifest: `stable` is the engine that shipped with this release; `rolling` is the+/// newest one the nightly sync built, opt-in and labelled experimental in Settings.+enum EngineManifest {+ /// One downloadable file: the release asset's name and the SHA-256 we verify before using it.+ struct Asset: Decodable, Equatable {+ let asset: String+ let sha256: String+ let size: Int?+ }++ /// The manifest's shape (`latest.json`). Unknown keys are ignored so an older launcher can read a+ /// newer manifest as long as what it needs is still there.+ struct Manifest: Decodable, Equatable {+ /// The release version this manifest describes, e.g. "0.8.0".+ let version: String+ /// The seedds/nxnative commit the engine tree was built from (what `.upstream-ref` holds).+ let pin: String+ let engine: Asset+ /// The app itself, so one manifest drives both updates.+ let app: Asset?+ /// Where the files live, in the order they are tried. Empty: the release page on GitHub.+ var sources: [String] = []+ /// When the nightly sync built this engine, for the rolling channel ("2026-10-31T02:00:00Z").+ var built: String?+ var channel: String?++ // Written out because a default value does not make a key optional to the synthesized decoder:+ // a manifest without `sources` is valid and means "the release page".+ private enum CodingKeys: String, CodingKey { case version, pin, engine, app, sources, built, channel }++ init(from decoder: Decoder) throws {+ let c = try decoder.container(keyedBy: CodingKeys.self)+ version = try c.decode(String.self, forKey: .version)+ pin = try c.decode(String.self, forKey: .pin)+ engine = try c.decode(Asset.self, forKey: .engine)+ app = try c.decodeIfPresent(Asset.self, forKey: .app)+ sources = try c.decodeIfPresent([String].self, forKey: .sources) ?? []+ built = try c.decodeIfPresent(String.self, forKey: .built)+ channel = try c.decodeIfPresent(String.self, forKey: .channel)+ }++ init(version: String, pin: String, engine: Asset, app: Asset?, sources: [String] = [], built: String? = nil, channel: String? = nil) {+ self.version = version; self.pin = pin; self.engine = engine; self.app = app+ self.sources = sources; self.built = built; self.channel = channel+ }+ }++ enum Problem: Error, Equatable {+ case notJSON+ case missingVersion+ case missingPin+ case badDigest(String) // not 64 lowercase hex characters+ case noSources+ }++ /// Decode and validate. A manifest the launcher cannot act on fails here rather than half-way+ /// through a download.+ static func decode(_ data: Data) throws -> Manifest {+ let m: Manifest+ do { m = try JSONDecoder().decode(Manifest.self, from: data) }+ catch { throw Problem.notJSON }+ guard !m.version.trimmingCharacters(in: .whitespaces).isEmpty else { throw Problem.missingVersion }+ guard m.pin.count >= 7 else { throw Problem.missingPin }+ for a in [m.engine, m.app].compactMap({ $0 }) where !isDigest(a.sha256) { throw Problem.badDigest(a.sha256) }+ return m+ }++ static func isDigest(_ s: String) -> Bool {+ s.count == 64 && s.allSatisfy { $0.isHexDigit && !$0.isUppercase }+ }++ // ---- Authenticity ----++ /// What we can say about a manifest's signature. Integrity (a SHA-256 that matches) only proves the+ /// download is what the manifest lists — it says nothing when whoever served the manifest also served+ /// the file. With a mirror in the picture that matters, so a release may carry a detached Ed25519+ /// signature and the app refuses an update it cannot authenticate once a key is built in.+ enum Signature: Equatable {+ case valid+ case invalid+ case absent+ /// No public key in this build yet: the check cannot run. The caller decides what that means —+ /// today it accepts the manifest, as it does without signatures at all.+ case noKey+ }++ /// `publicKey` is the base64 of the 32-byte Ed25519 key, compiled in; empty means "not configured".+ /// `signature` is the base64 of the 64-byte detached signature over the manifest's exact bytes.+ static func verify(manifest: Data, signature: String?, publicKey: String) -> Signature {+ guard !publicKey.isEmpty else { return .noKey }+ guard let signature, let sig = Data(base64Encoded: signature),+ let key = try? Curve25519.Signing.PublicKey(rawRepresentation: Data(base64Encoded: publicKey) ?? Data()) else {+ return .absent+ }+ return key.isValidSignature(sig, for: manifest) ? .valid : .invalid+ }++ /// Whether this result allows an install. Once a key is configured, only a valid signature installs —+ /// by hand or automatically; while no key exists the check cannot run and the launcher behaves as it+ /// did before signatures (`.noKey`), which is what a build from the repository sees.+ static func allowsInstall(_ signature: Signature) -> Bool {+ switch signature {+ case .valid, .noKey: return true+ case .absent, .invalid: return false+ }+ }+}++/// Where engine trees live on disk: one folder per pin, beside the app's other state, so an app update+/// and an engine update cannot half-replace each other, and going back is just selecting the older pin.+struct EngineStore {+ let root: URL++ init(root: URL = EngineStore.defaultRoot) { self.root = root }++ /// `~/Library/Application Support/NX2Apple/engine/<pin>/`. The folder is a full checkout for the+ /// build (`dm/`, `scripts/`, ...), so `repoPath` can point at it.+ static var defaultRoot: URL {+ let base = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first+ ?? URL(fileURLWithPath: NSHomeDirectory() + "/Library/Application Support")+ return base.appendingPathComponent("NX2Apple/engine", isDirectory: true)+ }++ func directory(for pin: String) -> URL { root.appendingPathComponent(pin, isDirectory: true) }++ /// A tree is usable when it holds what the launcher runs by path (`BuildModel.requiredTools`).+ /// Unpacking into a temporary folder and renaming makes a half-written tree unreadable rather than+ /// silently incomplete, and this is the check that backs it up.+ func isUsable(_ pin: String) -> Bool {+ let dir = directory(for: pin)+ for f in ["scripts/build_ipa_from_nsp.py", "dm/tools/romfs_image.py", ".upstream-ref"] {+ guard FileManager.default.fileExists(atPath: dir.appendingPathComponent(f).path) else { return false }+ }+ return true+ }++ /// Remove every other pin's tree. Called after a successful switch, so a player keeps the one they+ /// are on; `keeping` also holds the previous one while a build might still need it.+ @discardableResult+ func prune(keeping pins: Set<String>) -> [String] {+ let fm = FileManager.default+ guard let entries = try? fm.contentsOfDirectory(at: root, includingPropertiesForKeys: [.isDirectoryKey]) else { return [] }+ var removed: [String] = []+ for url in entries where (try? url.resourceValues(forKeys: [.isDirectoryKey]).isDirectory) == true {+ guard !pins.contains(url.lastPathComponent) else { continue }+ if (try? fm.removeItem(at: url)) != nil { removed.append(url.lastPathComponent) }+ }+ return removed.sorted()+ }+}diff --git a/frontend/Tests/NSPortBuilderTests/EngineManifestTests.swift b/frontend/Tests/NSPortBuilderTests/EngineManifestTests.swiftnew file mode 100644index 00000000..ace9ee0b--- /dev/null+++ b/frontend/Tests/NSPortBuilderTests/EngineManifestTests.swift@@ -0,0 +1,123 @@+import CryptoKit+import XCTest+@testable import NSPortBuilder++/// The release manifest and the engine store (EngineManifest.swift). Everything is driven from bytes and+/// temporary folders here: no network, no release.+final class EngineManifestTests: XCTestCase {+ private lazy var digest = String(repeating: "a", count: 64)++ private func json(version: String = "0.8.0", pin: String = "ee6648cb2bb4", engine: String? = nil, app: String? = nil, sources: [String] = []) -> Data {+ let e = engine ?? "{\"asset\":\"engine.zip\",\"sha256\":\"\(digest)\"}"+ let a = app ?? "{\"asset\":\"app.zip\",\"sha256\":\"\(digest)\"}"+ return Data("""+ {"version":"\(version)","pin":"\(pin)","engine":\(e),"app":\(a),"sources":\(sources)}+ """.utf8)+ }++ // MARK: Decoding++ func testAWellFormedManifestDecodes() throws {+ let m = try EngineManifest.decode(json(sources: ["https://example.invalid/v0.8.0/"]))+ XCTAssertEqual(m.version, "0.8.0")+ XCTAssertEqual(m.pin, "ee6648cb2bb4")+ XCTAssertEqual(m.engine.asset, "engine.zip")+ XCTAssertEqual(m.app?.asset, "app.zip")+ XCTAssertEqual(m.sources, ["https://example.invalid/v0.8.0/"])+ }++ /// An older launcher reads a newer manifest: extra keys are ignored rather than rejected.+ func testUnknownKeysAreIgnored() throws {+ let data = Data("""+ {"version":"1.0.0","pin":"ee6648cb2bb4","engine":{"asset":"e.zip","sha256":"\(digest)","future":true},"runtime":"arm64"}+ """.utf8)+ XCTAssertNoThrow(try EngineManifest.decode(data))+ }++ func testTheAppAssetIsOptional() throws {+ let data = Data("""+ {"version":"0.8.0","pin":"ee6648cb2bb4","engine":{"asset":"engine.zip","sha256":"\(digest)"}}+ """.utf8)+ XCTAssertNil(try EngineManifest.decode(data).app)+ }++ func testAManifestTheLauncherCannotActOnIsRejected() {+ XCTAssertThrowsError(try EngineManifest.decode(Data("not json".utf8))) { XCTAssertEqual($0 as? EngineManifest.Problem, .notJSON) }+ XCTAssertThrowsError(try EngineManifest.decode(json(version: " "))) { XCTAssertEqual($0 as? EngineManifest.Problem, .missingVersion) }+ XCTAssertThrowsError(try EngineManifest.decode(json(pin: "abc"))) { XCTAssertEqual($0 as? EngineManifest.Problem, .missingPin) }+ }++ func testADigestThatIsNotLowercaseHexIsRejected() {+ for bad in [String(repeating: "A", count: 64), String(repeating: "a", count: 63), "sha256:" + digest] {+ XCTAssertThrowsError(try EngineManifest.decode(json(engine: "{\"asset\":\"e.zip\",\"sha256\":\"\(bad)\"}"))) {+ XCTAssertEqual($0 as? EngineManifest.Problem, .badDigest(bad), bad)+ }+ }+ }++ // MARK: Signatures++ private func signed(_ data: Data) throws -> (signature: String, publicKey: String) {+ let key = Curve25519.Signing.PrivateKey()+ let sig = try key.signature(for: data)+ return (sig.base64EncodedString(), key.publicKey.rawRepresentation.base64EncodedString())+ }++ func testAValidSignatureVerifiesAndATamperedManifestDoesNot() throws {+ let data = json()+ let (sig, pub) = try signed(data)+ XCTAssertEqual(EngineManifest.verify(manifest: data, signature: sig, publicKey: pub), .valid)+ let tampered = Data(String(decoding: data, as: UTF8.self).replacingOccurrences(of: "0.8.0", with: "9.9.9").utf8)+ XCTAssertEqual(EngineManifest.verify(manifest: tampered, signature: sig, publicKey: pub), .invalid)+ }++ func testAnAbsentSignatureIsNotValid() throws {+ let (_, pub) = try signed(json())+ XCTAssertEqual(EngineManifest.verify(manifest: json(), signature: nil, publicKey: pub), .absent)+ XCTAssertEqual(EngineManifest.verify(manifest: json(), signature: "not base64!!", publicKey: pub), .absent)+ }++ func testABuildWithoutAKeyCannotCheckAndSaysSo() {+ XCTAssertEqual(EngineManifest.verify(manifest: json(), signature: nil, publicKey: ""), .noKey)+ }++ /// The rule that decides an install: once a key is configured only a valid signature installs; with no+ /// key the check cannot run, which is what a repository build sees.+ func testWhatAnInstallAccepts() {+ XCTAssertTrue(EngineManifest.allowsInstall(.valid))+ XCTAssertFalse(EngineManifest.allowsInstall(.invalid), "a configured key refuses a bad signature, by hand too")+ XCTAssertFalse(EngineManifest.allowsInstall(.absent), "a configured key refuses an unsigned manifest")+ XCTAssertTrue(EngineManifest.allowsInstall(.noKey), "no key configured: today's behaviour")+ }++ // MARK: The store++ func testTheStoreKeepsOneTreePerPinAndPrunesTheRest() throws {+ let root = FileManager.default.temporaryDirectory.appendingPathComponent("engine-tests-" + UUID().uuidString)+ defer { try? FileManager.default.removeItem(at: root) }+ let store = EngineStore(root: root)+ let fm = FileManager.default+ for pin in ["aaaaaaa", "bbbbbbb", "ccccccc"] {+ let dir = store.directory(for: pin)+ try fm.createDirectory(at: dir.appendingPathComponent("scripts"), withIntermediateDirectories: true)+ try fm.createDirectory(at: dir.appendingPathComponent("dm/tools"), withIntermediateDirectories: true)+ try Data([0]).write(to: dir.appendingPathComponent("scripts/build_ipa_from_nsp.py"))+ try Data([0]).write(to: dir.appendingPathComponent("dm/tools/romfs_image.py"))+ try Data([0]).write(to: dir.appendingPathComponent(".upstream-ref"))+ }+ XCTAssertTrue(store.isUsable("aaaaaaa"))+ XCTAssertEqual(store.prune(keeping: ["aaaaaaa"]), ["bbbbbbb", "ccccccc"])+ XCTAssertTrue(store.isUsable("aaaaaaa"))+ XCTAssertFalse(fm.fileExists(atPath: store.directory(for: "bbbbbbb").path))+ }++ func testAPartiallyUnpackedTreeIsNotUsable() throws {+ let root = FileManager.default.temporaryDirectory.appendingPathComponent("engine-tests-" + UUID().uuidString)+ defer { try? FileManager.default.removeItem(at: root) }+ let store = EngineStore(root: root)+ let dir = store.directory(for: "ddddddd")+ try FileManager.default.createDirectory(at: dir.appendingPathComponent("scripts"), withIntermediateDirectories: true)+ try Data([0]).write(to: dir.appendingPathComponent("scripts/build_ipa_from_nsp.py"))+ XCTAssertFalse(store.isUsable("ddddddd"), "no dm/tools/romfs_image.py and no .upstream-ref")+ }+}