Skip to content

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

The self-updater does not install a release this macOS cannot open

v0lumehi752b5d7506d5
diff --git a/frontend/Sources/NSPortBuilder/AppUpdate.swift b/frontend/Sources/NSPortBuilder/AppUpdate.swift
index 9a5a0442..ce917d00 100644
--- a/frontend/Sources/NSPortBuilder/AppUpdate.swift
+++ b/frontend/Sources/NSPortBuilder/AppUpdate.swift
@@ -76,6 +76,36 @@ enum AppUpdate {
return nil
}
+ // ---- What the running macOS can open ----
+
+ /// "26.6" or "26.6.1" as an OS version; nil for anything else.
+ static func parseOSVersion(_ s: String) -> OperatingSystemVersion? {
+ let n = s.trimmingCharacters(in: .whitespaces).split(separator: ".", omittingEmptySubsequences: false).map { Int($0) }
+ guard (1...3).contains(n.count), n.allSatisfy({ $0 != nil && $0! >= 0 }) else { return nil }
+ return OperatingSystemVersion(majorVersion: n[0]!, minorVersion: n.count > 1 ? n[1]! : 0, patchVersion: n.count > 2 ? n[2]! : 0)
+ }
+
+ /// `a` is older than `b`, field by field (major, minor, patch).
+ static func isOlder(_ a: OperatingSystemVersion, than b: OperatingSystemVersion) -> Bool {
+ (a.majorVersion, a.minorVersion, a.patchVersion) < (b.majorVersion, b.minorVersion, b.patchVersion)
+ }
+
+ static func describe(_ v: OperatingSystemVersion) -> String {
+ v.patchVersion == 0 ? "\(v.majorVersion).\(v.minorVersion)" : "\(v.majorVersion).\(v.minorVersion).\(v.patchVersion)"
+ }
+
+ /// The staged app's `LSMinimumSystemVersion`, as written ("26.6"), when it is above `running`: the release cannot be
+ /// opened on this Mac, so it must not replace the launcher that can. nil when it opens here, or names no minimum
+ /// (macOS then applies none) or one that cannot be read (the release workflow writes it; an unreadable value is
+ /// not a reason to keep a Mac from updating).
+ static func minimumOSAbove(_ running: OperatingSystemVersion, app: URL) -> String? {
+ guard let data = try? Data(contentsOf: app.appendingPathComponent("Contents/Info.plist")),
+ let info = try? PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any],
+ let text = info["LSMinimumSystemVersion"] as? String, let min = parseOSVersion(text)
+ else { return nil }
+ return isOlder(running, than: min) ? describe(min) : nil
+ }
+
// ---- What may replace itself ----
enum Installability: Equatable {
diff --git a/frontend/Sources/NSPortBuilder/ContentView.swift b/frontend/Sources/NSPortBuilder/ContentView.swift
index 0c64772b..8396f132 100644
--- a/frontend/Sources/NSPortBuilder/ContentView.swift
+++ b/frontend/Sources/NSPortBuilder/ContentView.swift
@@ -608,6 +608,8 @@ final class BuildModel: ObservableObject {
/// updates, so a user learns what changed before deciding, not after downloading.
@Published var showUpdateSheet = false
@Published var appStatus = ""
+ /// Set when the last update attempt was refused (a release this macOS cannot open, a bad download); the update sheet shows it.
+ @Published var appUpdateBlocked: String?
/// Download a new release by itself and install it when the app quits (Settings; on by default). Off, the
/// Update button in Settings works as before: download, swap in, relaunch.
@Published var autoUpdateApp = UserDefaults.standard.object(forKey: "autoUpdateApp") as? Bool ?? true {
@@ -716,6 +718,7 @@ final class BuildModel: ObservableObject {
func stageAppUpdate(_ release: AppUpdate.Release, automatic: Bool, then: ((AppUpdate.Staged) -> Void)? = nil) {
guard !appBusy, appInstallability == .ok, let url = URL(string: release.url) else { return }
appBusy = true
+ appUpdateBlocked = nil
appStatus = String(format: t(automatic ? "app.staging" : "app.downloading"), release.tag)
let expectedID = Bundle.main.bundleIdentifier ?? ""
DispatchQueue.global().async {
@@ -729,16 +732,35 @@ final class BuildModel: ObservableObject {
self.installAtQuit()
then?(staged)
case .failure(let why):
- self.appStatus = String(format: self.t(why.rawValue), release.tag)
+ if case .needsNewerMacOS(let min) = why {
+ // Nothing was installed: the running version stays, and says why
+ let now = AppUpdate.describe(ProcessInfo.processInfo.operatingSystemVersion)
+ self.appStatus = String(format: self.t(why.key), release.tag, min, now)
+ } else {
+ self.appStatus = String(format: self.t(why.key), release.tag)
+ }
+ self.appUpdateBlocked = self.appStatus
}
}
}
}
/// Why staging stopped: each case is the localization key of its message (a format with the release's tag).
- enum StageFailure: String, Error {
- case network = "app.stageNetFail", noDigest = "app.noDigest", mismatch = "app.digestMismatch"
- case unpack = "app.unpackFail", notOurApp = "app.notOurApp"
+ enum StageFailure: Error, Equatable {
+ case network, noDigest, mismatch, unpack, notOurApp
+ /// The release's LSMinimumSystemVersion is above the running macOS (the string is that minimum).
+ case needsNewerMacOS(String)
+
+ var key: String {
+ switch self {
+ case .network: return "app.stageNetFail"
+ case .noDigest: return "app.noDigest"
+ case .mismatch: return "app.digestMismatch"
+ case .unpack: return "app.unpackFail"
+ case .notOurApp: return "app.notOurApp"
+ case .needsNewerMacOS: return "app.needsNewerMacOS"
+ }
+ }
}
/// The background part of staging, in a folder of its own in the temporary directory (emptied first).
@@ -761,7 +783,8 @@ final class BuildModel: ObservableObject {
/// A downloaded zip to a staged app: its digest first, then unzip (ditto, as the release was packed), then the app inside.
nonisolated static func stage(zip: URL, release: AppUpdate.Release, in root: URL, expectedID: String,
- requireDigest: Bool) -> Result<AppUpdate.Staged, StageFailure> {
+ requireDigest: Bool,
+ runningOS: OperatingSystemVersion = ProcessInfo.processInfo.operatingSystemVersion) -> Result<AppUpdate.Staged, StageFailure> {
let fm = FileManager.default
switch AppUpdate.checkDigest(zip, expected: release.digest) {
case .match: break
@@ -785,6 +808,11 @@ final class BuildModel: ObservableObject {
try? fm.removeItem(at: dir)
return .failure(.notOurApp)
}
+ // Not a release this Mac cannot open: it would replace a launcher that works with an app that will not start
+ if let min = AppUpdate.minimumOSAbove(runningOS, app: app) {
+ try? fm.removeItem(at: dir)
+ return .failure(.needsNewerMacOS(min))
+ }
return .success(AppUpdate.Staged(release: release, app: app))
}
diff --git a/frontend/Sources/NSPortBuilder/Localization.swift b/frontend/Sources/NSPortBuilder/Localization.swift
index 80140f8d..03617327 100644
--- a/frontend/Sources/NSPortBuilder/Localization.swift
+++ b/frontend/Sources/NSPortBuilder/Localization.swift
@@ -378,6 +378,7 @@ enum L10n {
"app.digestMismatch": ("%@ 的下载与校验值不符,已丢弃", "The download of %@ did not match its checksum and was discarded"),
"app.unpackFail": ("%@ 的下载无法解压,已丢弃", "The download of %@ could not be unpacked and was discarded"),
"app.notOurApp": ("%@ 的下载不是 NX2Apple,已丢弃", "The download of %@ is not an NX2Apple app and was discarded"),
+ "app.needsNewerMacOS": ("NX2Apple %@ 需要 macOS %@,这台 Mac 是 %@。没有安装,继续使用当前版本", "NX2Apple %@ needs macOS %@ and this Mac has %@. It was not installed; you keep the current version"),
"app.devBuild": ("%@ 已发布。这个启动器是从仓库构建的,不会替换自己", "%@ is out. This launcher was built from the repository and does not replace itself"),
"app.readOnly": ("%@ 已发布,但 App 无法在当前位置替换自己——请把它移到「应用程序」文件夹", "%@ is out, but the app cannot replace itself where it is — move it to Applications"),
"app.auto": ("自动安装更新", "Install updates automatically"),
diff --git a/frontend/Sources/NSPortBuilder/UpdateSheet.swift b/frontend/Sources/NSPortBuilder/UpdateSheet.swift
index 719be73c..e5716e47 100644
--- a/frontend/Sources/NSPortBuilder/UpdateSheet.swift
+++ b/frontend/Sources/NSPortBuilder/UpdateSheet.swift
@@ -41,6 +41,11 @@ struct UpdateSheet: View {
}
.frame(maxHeight: 340)
+ if let blocked = m.appUpdateBlocked {
+ Text(blocked).font(.manrope(12.5)).foregroundStyle(.orange)
+ .fixedSize(horizontal: false, vertical: true)
+ .padding(.horizontal, 28).padding(.bottom, 12)
+ }
Divider().opacity(0.3)
HStack(spacing: 12) {
if let u = update {
@@ -54,8 +59,8 @@ struct UpdateSheet: View {
Button {
// Automatic mode has already downloaded and staged the release, so the action is
// to swap it in now; otherwise this downloads, verifies and installs as before.
- if m.appStaged != nil { m.installStagedUpdate(relaunch: true) } else { m.performAppUpdate() }
- dismiss()
+ // The sheet stays up while it downloads, so a refusal (a macOS too old for the release) is seen here.
+ if m.appStaged != nil { m.installStagedUpdate(relaunch: true); dismiss() } else { m.performAppUpdate() }
} label: {
Text(m.t(m.appStaged != nil ? "app.restartNow" : "update.now"))
.font(.manrope(13.5, .bold)).foregroundStyle(.black)
diff --git a/frontend/Tests/NSPortBuilderTests/AppUpdateTests.swift b/frontend/Tests/NSPortBuilderTests/AppUpdateTests.swift
index 9056b5bd..a63adc8c 100644
--- a/frontend/Tests/NSPortBuilderTests/AppUpdateTests.swift
+++ b/frontend/Tests/NSPortBuilderTests/AppUpdateTests.swift
@@ -19,15 +19,16 @@ final class AppUpdateTests: XCTestCase {
/// A launcher bundle as the release workflow assembles it.
@discardableResult
private func makeApp(_ name: String = "NX2Apple", in dir: URL? = nil, id: String? = nil, version: String = "0.4.8",
- executable: Bool = true, marker: String = "new") throws -> URL {
+ executable: Bool = true, marker: String = "new", minOS: String? = nil) throws -> URL {
let app = (dir ?? temporary).appendingPathComponent(name + ".app")
let macOS = app.appendingPathComponent("Contents/MacOS")
try FileManager.default.createDirectory(at: macOS, withIntermediateDirectories: true)
let exe = macOS.appendingPathComponent("NSPortBuilder")
try Data(marker.utf8).write(to: exe)
try FileManager.default.setAttributes([.posixPermissions: executable ? 0o755 : 0o644], ofItemAtPath: exe.path)
- let info: [String: Any] = ["CFBundleIdentifier": id ?? bundleID, "CFBundleExecutable": "NSPortBuilder",
+ var info: [String: Any] = ["CFBundleIdentifier": id ?? bundleID, "CFBundleExecutable": "NSPortBuilder",
"CFBundleShortVersionString": version, "CFBundlePackageType": "APPL"]
+ if let minOS { info["LSMinimumSystemVersion"] = minOS }
try PropertyListSerialization.data(fromPropertyList: info, format: .xml, options: 0)
.write(to: app.appendingPathComponent("Contents/Info.plist"))
return app
@@ -90,6 +91,52 @@ final class AppUpdateTests: XCTestCase {
XCTAssertEqual(AppUpdate.bundleProblem(temporary.appendingPathComponent("Missing.app"), expectedID: bundleID, tag: "v0.4.8"), .noInfo)
}
+ private func os(_ major: Int, _ minor: Int, _ patch: Int = 0) -> OperatingSystemVersion {
+ OperatingSystemVersion(majorVersion: major, minorVersion: minor, patchVersion: patch)
+ }
+
+ func testOSVersionComparison() throws {
+ XCTAssertEqual(AppUpdate.parseOSVersion("26.6")?.minorVersion, 6)
+ XCTAssertEqual(AppUpdate.parseOSVersion("26")?.minorVersion, 0)
+ XCTAssertEqual(AppUpdate.parseOSVersion("26.6.1")?.patchVersion, 1)
+ XCTAssertNil(AppUpdate.parseOSVersion(""))
+ XCTAssertNil(AppUpdate.parseOSVersion("26.x"))
+ XCTAssertNil(AppUpdate.parseOSVersion("1.2.3.4"))
+ XCTAssertTrue(AppUpdate.isOlder(os(26, 5, 9), than: os(26, 6)))
+ XCTAssertTrue(AppUpdate.isOlder(os(15, 9), than: os(26, 0)))
+ XCTAssertTrue(AppUpdate.isOlder(os(26, 6), than: os(26, 6, 1)))
+ XCTAssertFalse(AppUpdate.isOlder(os(26, 6), than: os(26, 6)))
+ XCTAssertFalse(AppUpdate.isOlder(os(27, 0), than: os(26, 6))) // minor 0 < 6 must not count when the major is higher
+ XCTAssertEqual(AppUpdate.describe(os(26, 6)), "26.6")
+ XCTAssertEqual(AppUpdate.describe(os(26, 6, 1)), "26.6.1")
+ }
+
+ func testMinimumOSDecidesWhetherAReleaseMayBeInstalled() throws {
+ let needs266 = try makeApp("Needs", minOS: "26.6")
+ XCTAssertEqual(AppUpdate.minimumOSAbove(os(15, 5), app: needs266), "26.6")
+ XCTAssertEqual(AppUpdate.minimumOSAbove(os(26, 5, 3), app: needs266), "26.6")
+ XCTAssertNil(AppUpdate.minimumOSAbove(os(26, 6), app: needs266))
+ XCTAssertNil(AppUpdate.minimumOSAbove(os(27, 0), app: needs266))
+ XCTAssertNil(AppUpdate.minimumOSAbove(os(13, 0), app: try makeApp("None"))) // no minimum written
+ XCTAssertNil(AppUpdate.minimumOSAbove(os(13, 0), app: try makeApp("Junk", minOS: "later"))) // unreadable: not a reason to refuse
+ }
+
+ func testStagingRefusesAReleaseThisMacCannotOpen() throws {
+ let source = temporary.appendingPathComponent("source-os", isDirectory: true)
+ let archive = try zip(try makeApp(in: source, minOS: "26.6"))
+ let release = AppUpdate.Release(tag: "v0.4.8", url: "https://example.invalid/a.zip", digest: try digest(archive))
+ func attempt(_ running: OperatingSystemVersion) throws -> Result<AppUpdate.Staged, BuildModel.StageFailure> {
+ let copy = temporary.appendingPathComponent("copy-" + UUID().uuidString + ".zip")
+ try FileManager.default.copyItem(at: archive, to: copy)
+ let root = temporary.appendingPathComponent("stage-" + UUID().uuidString, isDirectory: true)
+ try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
+ return BuildModel.stage(zip: copy, release: release, in: root, expectedID: bundleID, requireDigest: true, runningOS: running)
+ }
+ XCTAssertEqual(try attempt(os(15, 5)).failure, .needsNewerMacOS("26.6"))
+ XCTAssertEqual(BuildModel.StageFailure.needsNewerMacOS("26.6").key, "app.needsNewerMacOS")
+ XCTAssertNil(try attempt(os(26, 6)).failure)
+ }
+
func testStagingVerifiesDigestAndBundle() throws {
let source = temporary.appendingPathComponent("source", isDirectory: true)
let archive = try zip(try makeApp(in: source))