Run a virtual iPhone on an Apple Silicon Mac.
Swift
Format sources with swiftformat for Swift 6.2
Lakr9e560b3ada18
diff --git a/VPhoneDaemon/Daemon/GuestAPI+Device.swift b/VPhoneDaemon/Daemon/GuestAPI+Device.swiftindex 9826bf07..dc9c203b 100644--- a/VPhoneDaemon/Daemon/GuestAPI+Device.swift+++ b/VPhoneDaemon/Daemon/GuestAPI+Device.swift@@ -27,7 +27,7 @@ extension GuestAPI {case "network.ipv4.set":return try networkIPv4(interface: optionalString(params, "interface") ?? "en0", set: params)case "network.resolve":- let port = (params["port"] as? NSNumber).map { $0.intValue }+ let port = (params["port"] as? NSNumber).map(\.intValue)let family: Int32 = switch optionalString(params, "family") {case "ipv4": AF_INETcase "ipv6": AF_INET6@@ -303,4 +303,3 @@ extension GuestAPI {return outcome}}-diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DyldSharedCache/DyldSharedCacheChunkSet.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DyldSharedCache/DyldSharedCacheChunkSet.swiftindex 987205cd..958503a2 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DyldSharedCache/DyldSharedCacheChunkSet.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DyldSharedCache/DyldSharedCacheChunkSet.swift@@ -484,9 +484,9 @@ public final class DyldSharedCacheChunkSet {.filter { $0.hasPrefix(prefix) }.filter { !$0.hasSuffix(".symbols") && !$0.hasSuffix(".map") }- /// Base file first, then numerically by sub-cache index. Files whose- /// suffix is not a bare number (`.75.dylddata`, `.atlas`) sort after,- /// by name — they still carry mappings and must not be dropped.+ // Base file first, then numerically by sub-cache index. Files whose+ // suffix is not a bare number (`.75.dylddata`, `.atlas`) sort after,+ // by name — they still carry mappings and must not be dropped.func sortKey(_ name: String) -> (Int, Int, String) {if name == prefix {return (0, -1, name)diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareDeviceTreePatchSet.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareDeviceTreePatchSet.swiftindex 057d85e2..dad52640 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareDeviceTreePatchSet.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareDeviceTreePatchSet.swift@@ -167,6 +167,7 @@ public enum FirmwareDeviceTreePatchSet {),// MARK: iPad Guest+//// Written only to an iPad guest's installed tree (an iPhone guest// never produces these records). See DeviceTreeGuestDevicePatches.swift.diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePipelineComponents.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePipelineComponents.swiftindex 10888fd4..ec0ed6ec 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePipelineComponents.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePipelineComponents.swift@@ -37,8 +37,8 @@ extension FirmwarePipeline {) -> [ComponentDescriptor] {var components: [ComponentDescriptor] = []- /// Whether the plan kept a bundled set. True when there is no plan, so a- /// directly built pipeline runs every patcher its variant names.+ // Whether the plan kept a bundled set. True when there is no plan, so a+ // directly built pipeline runs every patcher its variant names.func includesSet(_ identifier: String) -> Bool {plan?.includesPatchSet(identifier) ?? true}@@ -55,9 +55,9 @@ extension FirmwarePipeline {let hasGuestTree = guestDevice.isPad && FileManager.default.fileExists(atPath: guestTreeURL.path)let boardTreeURL = restoreDir.appending(path: guestDevice.boardDeviceTreePath)- /// Whether the plan turned a patch on. Without a plan, fall back to the- /// release the patch is pinned to, which is the same answer the standard- /// preset gives.+ // Whether the plan turned a patch on. Without a plan, fall back to the+ // release the patch is pinned to, which is the same answer the standard+ // preset gives.func isEnabled(_ identifier: String, fallback: Bool) -> Bool {plan?.isEnabled(identifier) ?? fallback}@@ -270,13 +270,13 @@ extension FirmwarePipeline {inRestoreDir: true,searchPatterns: [FirmwareManifest.guestDeviceTreePath],patcherFactories: includeDeviceTree ? [{ data, verbose in- let p = DeviceTreePatcher(+ let p = try DeviceTreePatcher(data: data,verbose: verbose,includeIdentityPatches: dtIncludeIdentity,device: guestDevice,role: .installed,- sourceTree: try self.loader.load(from: boardTreeURL),+ sourceTree: self.loader.load(from: boardTreeURL),)p.gate = gatereturn pdiff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/DeviceTreeGuestDeviceTests.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/DeviceTreeGuestDeviceTests.swiftindex a2f103f7..6d3097c3 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/DeviceTreeGuestDeviceTests.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/DeviceTreeGuestDeviceTests.swift@@ -290,7 +290,7 @@ struct DeviceTreeGuestDeviceTests {}private extension Data {- mutating func append<T: FixedWidthInteger>(littleEndian value: T) {+ mutating func append(littleEndian value: some FixedWidthInteger) {var little = value.littleEndianSwift.withUnsafeBytes(of: &little) { append(contentsOf: $0) }}diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCommand.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCommand.swiftindex 884b238f..dcb2ccff 100644--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCommand.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCommand.swift@@ -274,8 +274,8 @@ struct VPhoneVirtualMachineConfigCommand: ParsableCommand {}private func networkEdit(vmName: String) throws -> VPhoneNetworkEdit {- var edit = VPhoneNetworkEdit(- mode: try network.map(Self.parseMode),+ var edit = try VPhoneNetworkEdit(+ mode: network.map(Self.parseMode),bridgeInterface: bridgeInterface,removeForwards: removeForward,clearForwards: clearForwards,diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneHostHotKeys.swift b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneHostHotKeys.swiftindex 6011e52a..5772e355 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneHostHotKeys.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/VPhoneHostHotKeys.swift@@ -39,7 +39,7 @@ final class VPhoneHostHotKeys {let setEnabled: SetEnabled}- nonisolated(unsafe) private static let skyLight: SkyLight? = {+ private nonisolated(unsafe) static let skyLight: SkyLight? = {guard let handle = dlopen("/System/Library/PrivateFrameworks/SkyLight.framework/SkyLight", RTLD_NOW),let get = dlsym(handle, "CGSGetSymbolicHotKeyValue"),let isEnabled = dlsym(handle, "CGSIsSymbolicHotKeyEnabled"),@@ -53,10 +53,10 @@ final class VPhoneHostHotKeys {}()/// The hot keys this process turned off. Read at exit, from `atexit`.- nonisolated(unsafe) private static var suspended: [Int32] = []+ private nonisolated(unsafe) static var suspended: [Int32] = []- nonisolated private static let spaceKeyCode: UInt16 = 0x31- nonisolated private static let functionModifier: UInt32 = 0x80_0000+ private nonisolated static let spaceKeyCode: UInt16 = 0x31+ private nonisolated static let functionModifier: UInt32 = 0x800000/// Symbolic hot key identifiers run well below this.private static let identifierLimit: Int32 = 512@@ -91,7 +91,7 @@ final class VPhoneHostHotKeys {Self.resumeSuspended()}- nonisolated private static func resumeSuspended() {+ private nonisolated static func resumeSuspended() {guard !suspended.isEmpty, let skyLight else { return }for identifier in suspended {_ = skyLight.setEnabled(identifier, true)@@ -120,17 +120,17 @@ final class VPhoneHostHotKeys {}/// Shift, Control, Option, Command, fn (`NSEvent.ModifierFlags`' bits).- nonisolated private static let deviceIndependentModifiers: UInt32 = 0xFF_0000+ private nonisolated static let deviceIndependentModifiers: UInt32 = 0xFF0000// MARK: - Crash Recovery- nonisolated private static var recordURL: URL {+ private nonisolated static var recordURL: URL {FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0].appendingPathComponent("vphone", isDirectory: true).appendingPathComponent("suspended-hotkeys.plist")}- nonisolated private static func record(_ identifiers: [Int32]) {+ private nonisolated static func record(_ identifiers: [Int32]) {let url = recordURLif identifiers.isEmpty {try? FileManager.default.removeItem(at: url)diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneHostKeyEvents.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneHostKeyEvents.swiftindex b731c76a..342c7991 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneHostKeyEvents.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneHostKeyEvents.swift@@ -101,17 +101,17 @@ enum VPhoneHostKeyEvents {private static func send(_ type: NSEvent.EventType, keyCode: UInt16, flags: NSEvent.ModifierFlags, in window: NSWindow) {guard let view = window.firstResponder as? VPhoneVirtualMachineView ?? window.contentView?.firstVMView,let event = NSEvent.keyEvent(- with: type,- location: .zero,- modifierFlags: flags,- timestamp: ProcessInfo.processInfo.systemUptime,- windowNumber: window.windowNumber,- context: nil,- characters: "",- charactersIgnoringModifiers: "",- isARepeat: false,- keyCode: keyCode,- ) else { return }+ with: type,+ location: .zero,+ modifierFlags: flags,+ timestamp: ProcessInfo.processInfo.systemUptime,+ windowNumber: window.windowNumber,+ context: nil,+ characters: "",+ charactersIgnoringModifiers: "",+ isARepeat: false,+ keyCode: keyCode,+ ) else { return }if (NSApp as? VPhoneApplication)?.takeKeyEvent(event, view: view) == true {return}@@ -126,9 +126,13 @@ enum VPhoneHostKeyEvents {private extension NSView {/// The VM view inside this view, when the window's first responder is not it.var firstVMView: VPhoneVirtualMachineView? {- if let view = self as? VPhoneVirtualMachineView { return view }+ if let view = self as? VPhoneVirtualMachineView {+ return view+ }for child in subviews {- if let view = child.firstVMView { return view }+ if let view = child.firstVMView {+ return view+ }}return nil}diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swiftindex c630adac..01d67915 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift@@ -318,11 +318,10 @@ class VPhoneVirtualMachineAppDelegate: NSObject, NSApplicationDelegate {}guard !plan.portForwards.isEmpty else { return }- let destination: VPhonePortForwarder.Destination- if case .tunnel = plan.attachment, let network = vm.tunnelNetwork {- destination = .tunnel(network)+ let destination: VPhonePortForwarder.Destination = if case .tunnel = plan.attachment, let network = vm.tunnelNetwork {+ .tunnel(network)} else {- destination = .direct(plan.forwardingAddress)+ .direct(plan.forwardingAddress)}let forwarder = VPhonePortForwarder(forwards: plan.portForwards, destination: destination)for failure in forwarder.start() {@@ -405,7 +404,9 @@ class VPhoneVirtualMachineAppDelegate: NSObject, NSApplicationDelegate {menuController?.stopBatteryMonitoring()windowController?.closeForRestart()// Tool windows belong to the old guest connection too.- for window in NSApp.windows { window.close() }+ for window in NSApp.windows {+ window.close()+ }NSApp.mainMenu = nilwindowController = nilmenuController = nildiff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachine.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachine.swiftindex 284eb267..b711f672 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachine.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachine.swift@@ -435,9 +435,8 @@ class VPhoneVirtualMachine: NSObject, VZVirtualMachineDelegate {print("[vphone] Network link \(up ? "up" : "down")")}-static func describe(_ plan: VPhoneNetworkPlan) -> String {- var parts: [String] = switch plan.attachment {+ var parts = switch plan.attachment {case .none: ["none"]case .sharedNAT: ["nat"]case let .bridged(interface): ["bridged(\(interface))"]diff --git a/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneTreeFingerprint.swift b/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneTreeFingerprint.swiftindex dab70c41..dc6f484e 100644--- a/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneTreeFingerprint.swift+++ b/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneTreeFingerprint.swift@@ -62,9 +62,9 @@ public struct VPhoneTreeFingerprint: Codable, Equatable, Sendable {var paths: [String] = []- /// An explicit walk rather than FileManager's enumerator, which follows- /// its own rules about symlinks and packages. This has to see exactly- /// what is on disk, in a stable order.+ // An explicit walk rather than FileManager's enumerator, which follows+ // its own rules about symlinks and packages. This has to see exactly+ // what is on disk, in a stable order.func walk(_ directory: String) throws {let names = try FileManager.default.contentsOfDirectory(atPath: directory)for name in names.sorted() {diff --git a/VPhoneKit/VPhoneCoreKit/Network/VPhoneUDPForwarder.swift b/VPhoneKit/VPhoneCoreKit/Network/VPhoneUDPForwarder.swiftindex 4dd9c5d6..bcad73ff 100644--- a/VPhoneKit/VPhoneCoreKit/Network/VPhoneUDPForwarder.swift+++ b/VPhoneKit/VPhoneCoreKit/Network/VPhoneUDPForwarder.swift@@ -27,10 +27,10 @@ enum VPhoneHostResolver {}}-extension VPhoneIPv4Address {+public extension VPhoneIPv4Address {/// Parse `a.b.c.d`, or nil for anything else (including IPv6, which this/// stack deliberately does not carry).- public init?(dotted: String) {+ init?(dotted: String) {let parts = dotted.split(separator: ".", omittingEmptySubsequences: false)guard parts.count == 4 else { return nil }var octets: [UInt8] = []diff --git a/VPhoneKit/VPhoneCoreKit/Support/VPhoneNetworking.swift b/VPhoneKit/VPhoneCoreKit/Support/VPhoneNetworking.swiftindex 2414f25e..4b135d64 100644--- a/VPhoneKit/VPhoneCoreKit/Support/VPhoneNetworking.swift+++ b/VPhoneKit/VPhoneCoreKit/Support/VPhoneNetworking.swift@@ -549,7 +549,7 @@ public enum VPhoneNetworking {forwards.removeAll { forward inforward.description == name|| String(forward.hostPort) == name- || "\(forward.transport.rawValue):\(forward.hostPort)" == name+ || name == "\(forward.transport.rawValue):\(forward.hostPort)"|| (try? parsePortForward(name)) == forward}if forwards.count == before {@@ -561,13 +561,13 @@ public enum VPhoneNetworking {forwards.append(forward)}- let merged = NetworkConfig(+ let merged = try NetworkConfig(mode: newMode,macAddress: mac,bridgeInterface: newBridge,ipv4: ipv4,portForwards: forwards.isEmpty ? nil : forwards,- localHostName: try edit.localHostName.map { try $0.map(validLocalHostName) } ?? current.localHostName,+ localHostName: edit.localHostName.map { try $0.map(validLocalHostName) } ?? current.localHostName,// On is the default, so it is stored as an absent key.resolvesMacName: edit.resolvesMacName.map { $0 ? nil : false } ?? current.resolvesMacName,)@@ -723,8 +723,7 @@ public enum VPhoneNetworking {case .off:return VPhoneNetworkPlan(attachment: .none, macAddress: nil, guestIPv4: nil, portForwards: [], localHostName: localHostName,- resolvesMacName: false, macAddressForGuest: nil, forwardingAddress: nil,- )+ resolvesMacName: false, macAddressForGuest: nil, forwardingAddress: nil)case .nat:guard let resolved else {diff --git a/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneVirtualMachineManifest.swift b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneVirtualMachineManifest.swiftindex 0db4ece3..d8157062 100644--- a/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneVirtualMachineManifest.swift+++ b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneVirtualMachineManifest.swift@@ -95,7 +95,9 @@ public struct VPhoneVirtualMachineManifest: Codable, Sendable {/// Missing in older manifests, which keep the original USB keyboard.public let hardwareKeyboardEnabled: Bool?- public var usesHardwareKeyboard: Bool { hardwareKeyboardEnabled ?? true }+ public var usesHardwareKeyboard: Bool {+ hardwareKeyboardEnabled ?? true+ }// MARK: - Displaydiff --git a/VPhoneKit/VPhoneCoreKitTests/Support/MacNameTests.swift b/VPhoneKit/VPhoneCoreKitTests/Support/MacNameTests.swiftindex c4135ba3..e40aa398 100644--- a/VPhoneKit/VPhoneCoreKitTests/Support/MacNameTests.swift+++ b/VPhoneKit/VPhoneCoreKitTests/Support/MacNameTests.swift@@ -15,7 +15,7 @@ struct MacNameTests {@Test func `the Mac's name can be turned off and stays off`() throws {let off = try VPhoneNetworking.merge(into: .default, mode: nil, bridgeInterface: nil, resolvesMacName: false)#expect(off.resolvesMacName == false)- #expect(VPhoneNetworking.macStaticNames(plan: try VPhoneNetworking.plan(off, host: host), macName: "Lab-Mac").isEmpty)+ #expect(try VPhoneNetworking.macStaticNames(plan: VPhoneNetworking.plan(off, host: host), macName: "Lab-Mac").isEmpty)let tunnel = try VPhoneNetworking.merge(into: off, mode: .tunnel, bridgeInterface: nil)#expect(tunnel.resolvesMacName == false)let on = try VPhoneNetworking.merge(into: tunnel, mode: nil, bridgeInterface: nil, resolvesMacName: true)@@ -31,6 +31,6 @@ struct MacNameTests {#expect(decoded.resolvesMacName == nil)let encoder = PropertyListEncoder()encoder.outputFormat = .xml- #expect(!String(decoding: try encoder.encode(decoded), as: UTF8.self).contains("resolvesMacName"))+ #expect(try !String(decoding: encoder.encode(decoded), as: UTF8.self).contains("resolvesMacName"))}}diff --git a/VPhoneKit/VPhoneCoreKitTests/Support/NetworkPlanTests.swift b/VPhoneKit/VPhoneCoreKitTests/Support/NetworkPlanTests.swiftindex f856ede2..216a8423 100644--- a/VPhoneKit/VPhoneCoreKitTests/Support/NetworkPlanTests.swift+++ b/VPhoneKit/VPhoneCoreKitTests/Support/NetworkPlanTests.swift@@ -350,7 +350,7 @@ struct NetworkPlanTests {@Test func `the Mac's name can be turned off`() throws {let off = try VPhoneNetworking.merge(into: .default, edit: VPhoneNetworkEdit(resolvesMacName: false), host: host)#expect(off.resolvesMacName == false)- #expect(VPhoneNetworking.macStaticNames(plan: try VPhoneNetworking.plan(off, host: host), macName: "Lab-Mac").isEmpty)+ #expect(try VPhoneNetworking.macStaticNames(plan: VPhoneNetworking.plan(off, host: host), macName: "Lab-Mac").isEmpty)let on = try VPhoneNetworking.merge(into: off, edit: VPhoneNetworkEdit(resolvesMacName: true), host: host)#expect(on.resolvesMacName == nil)#expect(try VPhoneNetworking.plan(on, host: host).resolvesMacName)@@ -389,7 +389,7 @@ struct NetworkPlanTests {let encoder = PropertyListEncoder()encoder.outputFormat = .xml- let plain = String(decoding: try encoder.encode(decoded), as: UTF8.self)+ let plain = try String(decoding: encoder.encode(decoded), as: UTF8.self)#expect(!plain.contains("ipv4"))#expect(!plain.contains("portForwards"))diff --git a/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/GuestDeviceTests.swift b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/GuestDeviceTests.swiftindex 53e40d1a..aa048e0d 100644--- a/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/GuestDeviceTests.swift+++ b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/GuestDeviceTests.swift@@ -26,7 +26,7 @@ struct GuestDeviceTests {}@Test- func `a VM made before iPad guests is an iPhone17,3`() throws {+ func `a VM made before iPad guests is an iPhone17,3`() {let manifest = VPhoneVirtualMachineManifest.newVM()#expect(manifest.guestProductType == nil)#expect(manifest.guestDevice == .iPhone17_3)diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadCreationPipeline.swift b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadCreationPipeline.swiftindex 40d55429..b020846d 100644--- a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadCreationPipeline.swift+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadCreationPipeline.swift@@ -25,7 +25,7 @@ final class VPhoneLaunchpadCreationPipeline {var cloudOSSource: String/// The model `fw prepare --device` picks from an IPSW that covers/// several, such as the 13-inch iPad; nil takes the first.- var device: String? = nil+ var device: String?var cpuCount: Intvar memoryMB: Intvar diskSizeGB: Int