#if DEBUG && PARCEL_REALITY import AppKit import Darwin import Foundation @MainActor enum AppRealityDriverHost { static func storage(environment: [String: String], arguments: [String]) throws -> AppIsolatedStorage? { let requested = environment["PARCEL_REALITY_DRIVER_SOCKET"] != nil || arguments.contains("--reality-driver-host") guard requested else { return nil } guard arguments == ["--reality-driver-host"], environment["PARCEL_REALITY"] == "1", let path = environment["PARCEL_REALITY_DRIVER_SOCKET"], path.hasPrefix("/"), !path.utf8.contains(0), path.utf8.count < 104 else { throw CocoaError(.fileReadInvalidFileName) } let storage = try AppIsolatedStorage( token: environment["PARCEL_REALITY_ISOLATION_TOKEN"], path: environment["PARCEL_REALITY_STATE_DIRECTORY"]) let socket = URL(fileURLWithPath: path).standardizedFileURL guard socket.resolvingSymlinksInPath() == socket, socket.deletingLastPathComponent() == storage.directory.deletingLastPathComponent(), socket.lastPathComponent == "driver.sock" else { throw CocoaError(.fileReadInvalidFileName) } return storage } static func runIfRequested(environment: [String: String], arguments: [String]) -> Bool { do { guard try storage(environment: environment, arguments: arguments) != nil else { return false } guard let path = environment["PARCEL_REALITY_DRIVER_SOCKET"] else { throw CocoaError(.fileReadInvalidFileName) } let helper = Bundle.main.bundleURL.appendingPathComponent("Contents/Helpers/ParcelRealityDriver") guard FileManager.default.isExecutableFile(atPath: helper.path) else { throw CocoaError(.fileNoSuchFile) } let child = Process() child.executableURL = helper child.arguments = ["--socket", path] child.environment = [:] child.standardInput = FileHandle.nullDevice child.standardOutput = FileHandle.nullDevice child.standardError = FileHandle.standardError child.terminationHandler = { process in DispatchQueue.main.async { exit(process.terminationStatus) } } // Launch Services owns this app's TCC responsibility; the helper must remain its direct child. try child.run() let application = NSApplication.shared application.setActivationPolicy(.prohibited) withExtendedLifetime(child) { application.run() } } catch { FileHandle.standardError.write(Data("Reality helper launch failed: \(error)\n".utf8)) exit(EXIT_FAILURE) } return true } } #endif