Skip to content

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

The signing team is also read from the Apple IDs signed in to Xcode, so a Mac without a certificate yet can install (#87)

v0lumehi52e090a0ff1a
diff --git a/frontend/Sources/NSPortBuilder/ContentView.swift b/frontend/Sources/NSPortBuilder/ContentView.swift
index 5ec6e955..81b352e3 100644
--- a/frontend/Sources/NSPortBuilder/ContentView.swift
+++ b/frontend/Sources/NSPortBuilder/ContentView.swift
@@ -2226,11 +2226,9 @@ struct BuilderView: View {
let devices = [(value: "", label: m.t("dev.none"))] + m.devices.map { d in
(value: d.id, label: d.name + (d.reachable ? "" : " (" + m.t("dev.notConnected") + ")"))
}
- let teams = m.teams.map { t in
- (value: t.id, label: t.identity.replacingOccurrences(of: #"^(Apple Development|iPhone Developer): "#, with: "", options: .regularExpression)
- .replacingOccurrences(of: #" \([A-Z0-9]+\)$"#, with: "", options: .regularExpression) + " · " + t.id)
- }
- return group(m.t("dev.section"), footnote: m.installDevice.isEmpty ? "" : m.t("dev.note")) {
+ let teams = m.teams.map { (value: $0.id, label: $0.label(personalTeam: m.t("dev.personalTeam"))) }
+ let needsCertificate = m.teams.first { $0.id == m.signingTeam }.map { !$0.hasCertificate } ?? false
+ return group(m.t("dev.section"), footnote: m.installDevice.isEmpty ? "" : (needsCertificate ? m.t("dev.teamNoCert") + " " : "") + m.t("dev.note")) {
row(m.t("dev.target"), detail: m.devicesChecked && m.devices.isEmpty ? m.t("dev.noDevices") : "") {
if !m.devicesChecked { ProgressView().controlSize(.small) }
Button(m.t("dev.refresh")) { m.refreshDevices() }.buttonStyle(.plain).font(.manrope(13)).foregroundStyle(.secondary)
diff --git a/frontend/Sources/NSPortBuilder/Devices.swift b/frontend/Sources/NSPortBuilder/Devices.swift
index c57c27d9..e6655a6e 100644
--- a/frontend/Sources/NSPortBuilder/Devices.swift
+++ b/frontend/Sources/NSPortBuilder/Devices.swift
@@ -9,10 +9,33 @@ struct AppleDevice: Identifiable, Equatable {
let reachable: Bool // connected now, by cable or the local network
}
-/// A development signing identity in the keychain and the team it signs for.
+/// A team the launcher can sign for: from a development certificate in the keychain, from an account signed in to
+/// Xcode, or both (#87: signing in to Xcode alone creates no certificate; `xcodebuild -allowProvisioningUpdates` makes
+/// it at the first install).
struct SigningTeam: Identifiable, Equatable {
let id: String // the team ID: the certificate's organizational unit, not the code in its name
- let identity: String // "Apple Development: Name (ABCDE12345)"
+ let identity: String // "Apple Development: Name (ABCDE12345)"; empty when only an Xcode account knows the team
+ var name: String = "" // the team's name in Xcode; empty when only a certificate knows the team
+ var personal = false // a free Apple ID's "Personal team"
+
+ var hasCertificate: Bool { !identity.isEmpty }
+
+ /// The name in the team menu: the team name (or the certificate holder's), "Personal team" for a free one, and the ID.
+ func label(personalTeam: String) -> String {
+ var holder = name
+ if holder.isEmpty {
+ holder = identity.replacingOccurrences(of: #"^(Apple Development|iPhone Developer): "#, with: "", options: .regularExpression)
+ .replacingOccurrences(of: #" \([A-Z0-9]+\)$"#, with: "", options: .regularExpression)
+ }
+ return (holder.isEmpty ? id : holder + (personal ? " (" + personalTeam + ")" : "") + " · " + id)
+ }
+}
+
+/// A team of an Apple ID signed in to Xcode, as Xcode's preferences list it.
+struct XcodeTeam: Equatable {
+ let id: String
+ let name: String
+ let free: Bool
}
/// Xcode's own tools for devices and signing: `xcrun devicectl` and `security`. Nothing here signs or installs;
@@ -45,14 +68,64 @@ enum DeviceTools {
return devices.sorted { ($0.reachable ? 0 : 1, $0.name) < ($1.reachable ? 0 : 1, $1.name) }
}
- /// The development identities that can sign for a device, with their teams.
+ /// Every team the launcher can offer: the teams of the Apple IDs signed in to Xcode plus the teams of development
+ /// certificates in the keychain, one entry per team ID. A team with a certificate comes first (it can sign at once).
static func teams() -> [SigningTeam] {
+ merge(certificates: certificateTeams(), xcode: xcodeTeams())
+ }
+
+ /// Pure: union by team ID. The certificate gives the identity, Xcode gives the team's name and whether it is a free one.
+ static func merge(certificates: [SigningTeam], xcode: [XcodeTeam]) -> [SigningTeam] {
+ var result: [SigningTeam] = []
+ for c in certificates where !result.contains(where: { $0.id == c.id }) {
+ let known = xcode.first { $0.id == c.id }
+ result.append(SigningTeam(id: c.id, identity: c.identity, name: known?.name ?? c.name, personal: known?.free ?? c.personal))
+ }
+ for x in xcode where !result.contains(where: { $0.id == x.id }) {
+ result.append(SigningTeam(id: x.id, identity: "", name: x.name, personal: x.free))
+ }
+ return result
+ }
+
+ /// The teams of the accounts signed in to Xcode, from its preferences (read only; nothing of the account but the
+ /// teams is looked at).
+ static func xcodeTeams() -> [XcodeTeam] {
+ guard let plist = run("/usr/bin/defaults", ["export", "com.apple.dt.Xcode", "-"]) else { return [] }
+ return xcodeTeams(fromPlist: Data(plist.utf8))
+ }
+
+ /// Pure. Xcode keeps the teams under `IDEProvisioningTeamByIdentifier` (current: account identifier -> array of
+ /// teams) or `IDEProvisioningTeams` (older); every dictionary below those keys with a team ID is a team. A missing
+ /// key, an unreadable plist or another shape gives no teams.
+ static func xcodeTeams(fromPlist data: Data) -> [XcodeTeam] {
+ guard let root = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String: Any] else { return [] }
+ var found: [XcodeTeam] = []
+ func walk(_ value: Any) {
+ if let dict = value as? [String: Any] {
+ if let id = dict["teamID"] as? String, id.range(of: "^[A-Z0-9]{10}$", options: .regularExpression) != nil {
+ let type = (dict["teamType"] as? String ?? "").lowercased()
+ let flag = (dict["isFreeProvisioningTeam"] as? NSNumber)?.boolValue ?? false
+ let free = flag || type.contains("free") || type.contains("personal")
+ if !found.contains(where: { $0.id == id }) { found.append(XcodeTeam(id: id, name: dict["teamName"] as? String ?? "", free: free)) }
+ } else {
+ for key in dict.keys.sorted() { walk(dict[key]!) }
+ }
+ } else if let array = value as? [Any] {
+ array.forEach(walk)
+ }
+ }
+ for key in ["IDEProvisioningTeamByIdentifier", "IDEProvisioningTeams"] { if let value = root[key] { walk(value) } }
+ return found
+ }
+
+ /// The development identities that can sign for a device, with their teams (one per team ID).
+ static func certificateTeams() -> [SigningTeam] {
guard let list = run("/usr/bin/security", ["find-identity", "-v", "-p", "codesigning"]) else { return [] }
var seen = Set<String>()
return identities(list).compactMap { identity in
guard let pem = run("/usr/bin/security", ["find-certificate", "-c", identity, "-p"]),
let subject = run("/usr/bin/openssl", ["x509", "-noout", "-subject"], input: pem),
- let team = team(fromSubject: subject), seen.insert(team + identity).inserted else { return nil }
+ let team = team(fromSubject: subject), seen.insert(team).inserted else { return nil }
return SigningTeam(id: team, identity: identity)
}
}
diff --git a/frontend/Sources/NSPortBuilder/Localization.swift b/frontend/Sources/NSPortBuilder/Localization.swift
index 0764d70c..fd4ec5cc 100644
--- a/frontend/Sources/NSPortBuilder/Localization.swift
+++ b/frontend/Sources/NSPortBuilder/Localization.swift
@@ -66,7 +66,11 @@ enum L10n {
"dev.noDevices": ("\u{6ca1}\u{6709}\u{4e0e}\u{8fd9}\u{53f0} Mac \u{914d}\u{5bf9}\u{7684} iPhone \u{6216} iPad\u{ff08}\u{9700}\u{8981} Xcode\u{ff09}\u{3002}", "No iPhone or iPad paired with this Mac (needs Xcode)."),
"dev.refresh": ("\u{5237}\u{65b0}", "Refresh"),
"dev.team": ("\u{7b7e}\u{540d}\u{56e2}\u{961f}", "Signing team"),
- "dev.noTeam": ("\u{6ca1}\u{6709}\u{5f00}\u{53d1}\u{8bc1}\u{4e66}\u{3002}\u{5728} Xcode \u{4e2d}\u{767b}\u{5f55} Apple ID\u{ff08}\u{8bbe}\u{7f6e} \u{2192} \u{8d26}\u{6237}\u{ff09}\u{5373}\u{53ef}\u{83b7}\u{5f97}\u{3002}", "No development certificate. Sign in to Xcode with your Apple ID (Settings \u{2192} Accounts) to get one."),
+ "dev.noTeam": ("Xcode 里没有 Apple ID,也没有开发证书。请在 Xcode → 设置 → 账户中登录。",
+ "No Apple ID in Xcode and no development certificate. Sign in to Xcode → Settings → Accounts."),
+ "dev.teamNoCert": ("这个团队还没有开发证书:第一次安装时 Xcode 会自动创建(Mac 需要联网)。",
+ "This team has no development certificate yet: Xcode creates it at the first install (the Mac needs to be online)."),
+ "dev.personalTeam": ("个人团队", "Personal team"),
"dev.note": ("Xcode 会为这台设备签名并直接安装,游戏数据(romfs.img)复制到 App 的“文稿”中,与侧载相同;第一次复制大型游戏需要几分钟。第一次需要在设备上开启开发者模式,并信任开发者(设置 → 通用 → VPN 与设备管理)。", "Xcode signs the game for this device and installs it there, then copies the game data (romfs.img) into the app’s Documents, as for sideloading; the first copy of a large game takes a few minutes. The first time, turn on Developer Mode on the device and trust the developer (Settings → General → VPN & Device Management)."),
// ---- Saves: a game's save copied between this Mac and the device it is installed on (SaveTransfer.swift) ----
"saves.menu": ("存档…", "Saves…"),
diff --git a/frontend/Tests/NSPortBuilderTests/LibraryTests.swift b/frontend/Tests/NSPortBuilderTests/LibraryTests.swift
index aa256301..2be1fc4a 100644
--- a/frontend/Tests/NSPortBuilderTests/LibraryTests.swift
+++ b/frontend/Tests/NSPortBuilderTests/LibraryTests.swift
@@ -707,6 +707,15 @@ final class LibraryTests: XCTestCase {
let previousDevice = model.installDevice, previousTeam = model.signingTeam
model.installDevice = "00008150-AAAA"; model.signingTeam = "TEAM123456"
try capture("build-install", height: 1300, page: .builds)
+ // #87: signed in to Xcode, no certificate yet (a free personal team and a paid one), and neither account nor certificate
+ model.teams = [SigningTeam(id: "FGHIJ67890", identity: "", name: "Jo Doe", personal: true),
+ SigningTeam(id: "KLMNO11111", identity: "", name: "Studio Ltd")]
+ model.signingTeam = "FGHIJ67890"
+ try capture("build-install-xcode-team", height: 1300, page: .builds)
+ model.teams = []; model.signingTeam = ""
+ try capture("build-install-no-team", height: 1300, page: .builds)
+ model.teams = [SigningTeam(id: "TEAM123456", identity: "Apple Development: Jo Doe (ABCDE12345)")]
+ model.signingTeam = "TEAM123456"
model.installDevice = previousDevice; model.signingTeam = previousTeam; model.target = previousTarget
// The Free space sheet with a measured build folder (example sizes), two parts picked, then while a build runs
let measured = BuildFolderSurvey(folder: "/Volumes/Games/Hades/\(titleID)_build",
diff --git a/frontend/Tests/NSPortBuilderTests/XcodeTeamsTests.swift b/frontend/Tests/NSPortBuilderTests/XcodeTeamsTests.swift
new file mode 100644
index 00000000..6a8c2eef
--- /dev/null
+++ b/frontend/Tests/NSPortBuilderTests/XcodeTeamsTests.swift
@@ -0,0 +1,105 @@
+import XCTest
+@testable import NSPortBuilder
+
+/// #87: the signing teams of the Apple IDs signed in to Xcode, read from Xcode's preferences (sample plists, not this
+/// Mac's), merged with the teams of development certificates.
+final class XcodeTeamsTests: XCTestCase {
+ private func plist(_ body: String) -> Data {
+ Data("""
+ <?xml version="1.0" encoding="UTF-8"?>
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+ <plist version="1.0"><dict>\(body)</dict></plist>
+ """.utf8)
+ }
+
+ private func team(_ id: String, _ name: String, free: Bool? = nil, type: String? = nil) -> String {
+ "<dict>" + (free.map { "<key>isFreeProvisioningTeam</key><\($0)/>" } ?? "") + "<key>teamID</key><string>\(id)</string>"
+ + "<key>teamName</key><string>\(name)</string>" + (type.map { "<key>teamType</key><string>\($0)</string>" } ?? "") + "</dict>"
+ }
+
+ func testTheCurrentShapeAccountIdentifierToTeams() {
+ let data = plist("""
+ <key>IDEProvisioningTeamByIdentifier</key><dict>
+ <key>0A681446-AAAA</key><array>\(team("ABCDE12345", "Jo Doe", free: false, type: "Individual"))</array>
+ <key>5B7C0000-BBBB</key><array>\(team("FGHIJ67890", "Jo Doe", free: true, type: "Personal Team"))\(team("KLMNO11111", "Studio Ltd", free: false, type: "Company/Organization"))</array>
+ </dict>
+ <key>IDEProvisioningTeamManagerLastSelectedTeamID</key><string>ABCDE12345</string>
+ """)
+ let teams = DeviceTools.xcodeTeams(fromPlist: data)
+ XCTAssertEqual(Set(teams.map(\.id)), ["ABCDE12345", "FGHIJ67890", "KLMNO11111"])
+ XCTAssertEqual(teams.first { $0.id == "ABCDE12345" }, XcodeTeam(id: "ABCDE12345", name: "Jo Doe", free: false))
+ XCTAssertEqual(teams.first { $0.id == "FGHIJ67890" }?.free, true)
+ XCTAssertEqual(teams.first { $0.id == "KLMNO11111" }?.name, "Studio Ltd")
+ }
+
+ func testTheOlderShapeAndAFreeTeamWithoutTheFlag() {
+ let data = plist("""
+ <key>IDEProvisioningTeams</key><dict>
+ <key>[email protected]</key><array>\(team("ABCDE12345", "Jo Doe (Personal Team)", type: "Free"))</array>
+ </dict>
+ """)
+ XCTAssertEqual(DeviceTools.xcodeTeams(fromPlist: data), [XcodeTeam(id: "ABCDE12345", name: "Jo Doe (Personal Team)", free: true)])
+ let flat = plist("<key>IDEProvisioningTeams</key><array>\(team("ABCDE12345", "Jo Doe"))</array>")
+ XCTAssertEqual(DeviceTools.xcodeTeams(fromPlist: flat), [XcodeTeam(id: "ABCDE12345", name: "Jo Doe", free: false)], "a plain array, no flag: not free")
+ }
+
+ func testMissingKeysAndOtherShapesGiveNoTeams() {
+ XCTAssertEqual(DeviceTools.xcodeTeams(fromPlist: plist("<key>Other</key><string>x</string>")), [])
+ XCTAssertEqual(DeviceTools.xcodeTeams(fromPlist: plist("<key>IDEProvisioningTeamByIdentifier</key><dict/>")), [])
+ XCTAssertEqual(DeviceTools.xcodeTeams(fromPlist: plist("<key>IDEProvisioningTeamByIdentifier</key><string>unexpected</string>")), [])
+ XCTAssertEqual(DeviceTools.xcodeTeams(fromPlist: plist("<key>IDEProvisioningTeamByIdentifier</key><dict><key>A</key><array><string>x</string></array></dict>")), [])
+ XCTAssertEqual(DeviceTools.xcodeTeams(fromPlist: Data("not a plist".utf8)), [])
+ XCTAssertEqual(DeviceTools.xcodeTeams(fromPlist: Data()), [])
+ // a dictionary whose team ID is not a team ID (a stray key) is not a team; a team of another key in the file is not read
+ XCTAssertEqual(DeviceTools.xcodeTeams(fromPlist: plist("<key>IDEProvisioningTeamByIdentifier</key><dict><key>A</key><array>\(team("short", "x"))</array></dict>")), [])
+ XCTAssertEqual(DeviceTools.xcodeTeams(fromPlist: plist("<key>Elsewhere</key><array>\(team("ABCDE12345", "x"))</array>")), [])
+ }
+
+ func testTheSameTeamUnderTwoAccountsIsListedOnce() {
+ let data = plist("""
+ <key>IDEProvisioningTeamByIdentifier</key><dict>
+ <key>A</key><array>\(team("ABCDE12345", "Studio Ltd", free: false))</array>
+ <key>B</key><array>\(team("ABCDE12345", "Studio Ltd", free: false))</array>
+ </dict>
+ """)
+ XCTAssertEqual(DeviceTools.xcodeTeams(fromPlist: data).count, 1)
+ }
+
+ func testTheUnionOfAccountsAndCertificates() {
+ let certificates = [SigningTeam(id: "ABCDE12345", identity: "Apple Development: Jo Doe (XXXXXXXXXX)"),
+ SigningTeam(id: "ABCDE12345", identity: "Apple Development: Jo Doe (YYYYYYYYYY)"),
+ SigningTeam(id: "CERTONLY12", identity: "iPhone Developer: Lee Roe (ZZZZZZZZZZ)")]
+ let xcode = [XcodeTeam(id: "FGHIJ67890", name: "Jo Doe", free: true), XcodeTeam(id: "ABCDE12345", name: "Studio Ltd", free: false)]
+ let merged = DeviceTools.merge(certificates: certificates, xcode: xcode)
+ XCTAssertEqual(merged.map(\.id), ["ABCDE12345", "CERTONLY12", "FGHIJ67890"], "certificates first, one entry per team ID")
+ XCTAssertEqual(merged[0].name, "Studio Ltd")
+ XCTAssertTrue(merged[0].hasCertificate)
+ XCTAssertEqual(merged[0].identity, "Apple Development: Jo Doe (XXXXXXXXXX)")
+ XCTAssertTrue(merged[1].hasCertificate)
+ XCTAssertFalse(merged[2].hasCertificate, "signed in to Xcode, no certificate yet: Xcode makes it at the first install")
+ XCTAssertTrue(merged[2].personal)
+ XCTAssertEqual(DeviceTools.merge(certificates: [], xcode: []), [], "neither an account nor a certificate")
+ }
+
+ func testTheMenuLabels() {
+ XCTAssertEqual(SigningTeam(id: "FGHIJ67890", identity: "", name: "Jo Doe", personal: true).label(personalTeam: "Personal team"),
+ "Jo Doe (Personal team) · FGHIJ67890")
+ XCTAssertEqual(SigningTeam(id: "ABCDE12345", identity: "", name: "Studio Ltd").label(personalTeam: "Personal team"), "Studio Ltd · ABCDE12345")
+ XCTAssertEqual(SigningTeam(id: "TEAM123456", identity: "Apple Development: Jo Doe (ABCDE12345)").label(personalTeam: "Personal team"),
+ "Jo Doe · TEAM123456", "a team only a certificate knows keeps the holder's name from it")
+ XCTAssertEqual(SigningTeam(id: "TEAM123456", identity: "").label(personalTeam: "Personal team"), "TEAM123456")
+ }
+
+ func testTheNoTeamMessageNamesTheRealCause() {
+ for lang in [Lang.en, .zh] {
+ let m = BuildModel(); m.lang = lang
+ XCTAssertFalse(m.t("dev.noTeam").isEmpty)
+ XCTAssertNotEqual(m.t("dev.teamNoCert"), "dev.teamNoCert")
+ XCTAssertNotEqual(m.t("dev.personalTeam"), "dev.personalTeam")
+ }
+ let m = BuildModel(); m.lang = .en
+ XCTAssertTrue(m.t("dev.noTeam").contains("Settings → Accounts"))
+ XCTAssertTrue(m.t("dev.noTeam").contains("no development certificate"))
+ XCTAssertTrue(m.t("dev.teamNoCert").contains("first install"))
+ }
+}