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
Build log pipes detach at end of output, so the launcher goes idle after a build
v0lumehic52f29293a6c
diff --git a/frontend/Sources/NSPortBuilder/ContentView.swift b/frontend/Sources/NSPortBuilder/ContentView.swiftindex 0ac1cc25..c3e33612 100644--- a/frontend/Sources/NSPortBuilder/ContentView.swift+++ b/frontend/Sources/NSPortBuilder/ContentView.swift@@ -1244,9 +1244,8 @@ final class BuildModel: ObservableObject {}}}- pipe.fileHandleForReading.readabilityHandler = { [weak self] h in- let d = h.availableData- guard !d.isEmpty, let s = String(data: d, encoding: .utf8) else { return }+ PipeReader.attach(to: pipe.fileHandleForReading) { [weak self] d in+ guard let s = String(data: d, encoding: .utf8) else { return }DispatchQueue.main.async { self?.ingest(s) }}proc = p@@ -1678,9 +1677,8 @@ final class BuildModel: ObservableObject {.merging(BundledTools.environment(for: romfsPy)) { $1 }Self.inOwnGroup(p, python: p.executableURL!.path)let pipe = Pipe(); p.standardOutput = pipe; p.standardError = pipe- pipe.fileHandleForReading.readabilityHandler = { [weak self] h in- let d = h.availableData- guard !d.isEmpty, let s = String(data: d, encoding: .utf8) else { return }+ PipeReader.attach(to: pipe.fileHandleForReading) { [weak self] d in+ guard let s = String(data: d, encoding: .utf8) else { return }DispatchQueue.main.async { self?.append(s) }}p.terminationHandler = { [weak self] st in Self.untrack(st); DispatchQueue.main.async {@@ -2030,9 +2028,8 @@ final class BuildModel: ObservableObject {Self.inOwnGroup(p, python: pythonPath.isEmpty ? "/usr/bin/python3" : pythonPath)let pipe = Pipe(); p.standardOutput = pipe; p.standardError = pipevar buffer = "", lastError = ""- pipe.fileHandleForReading.readabilityHandler = { [weak self] h in- let d = h.availableData- guard !d.isEmpty, let chunk = String(data: d, encoding: .utf8) else { return }+ PipeReader.attach(to: pipe.fileHandleForReading) { [weak self] d in+ guard let chunk = String(data: d, encoding: .utf8) else { return }DispatchQueue.main.async {guard let self else { return }buffer += chunk@@ -2625,8 +2622,8 @@ final class BuildModel: ObservableObject {p.standardOutput = out; p.standardError = err; p.standardInput = FileHandle.nullDevicedo { try p.run() } catch { line("could not start \(args[0]): \(error.localizedDescription)"); return false }for h in [out.fileHandleForReading, err.fileHandleForReading] {- h.readabilityHandler = { hd in- if let s = String(data: hd.availableData, encoding: .utf8) {+ PipeReader.attach(to: h) { d in+ if let s = String(data: d, encoding: .utf8) {for l in s.split(separator: "\n") { line(String(l)) }}}diff --git a/frontend/Sources/NSPortBuilder/PipeReader.swift b/frontend/Sources/NSPortBuilder/PipeReader.swiftnew file mode 100644index 00000000..e4352911--- /dev/null+++ b/frontend/Sources/NSPortBuilder/PipeReader.swift@@ -0,0 +1,19 @@+import Foundation++/// Wires a pipe's read end to a callback and detaches at end of output.+///+/// A `readabilityHandler` fires whenever the descriptor is readable, and at end of output (every writer has+/// closed) it stays readable forever: `availableData` returns empty data at once, the handler returns, and+/// the dispatch source fires again, which pins a CPU core (#201). So the handler removes itself the moment it+/// reads empty data.+enum PipeReader {+ /// `onData` gets each non-empty chunk on a background queue. Returns when nothing more will arrive: the+ /// handler is nil by then.+ static func attach(to handle: FileHandle, onData: @escaping (Data) -> Void) {+ handle.readabilityHandler = { h in+ let d = h.availableData+ if d.isEmpty { h.readabilityHandler = nil; return }+ onData(d)+ }+ }+}diff --git a/frontend/Tests/NSPortBuilderTests/PipeReaderTests.swift b/frontend/Tests/NSPortBuilderTests/PipeReaderTests.swiftnew file mode 100644index 00000000..0aa44afc--- /dev/null+++ b/frontend/Tests/NSPortBuilderTests/PipeReaderTests.swift@@ -0,0 +1,26 @@+import XCTest+@testable import NSPortBuilder++/// The build log pipes (#201): at end of output the read handler must remove itself, or the dispatch source+/// fires on the closed pipe forever and pins a CPU core.+final class PipeReaderTests: XCTestCase {+ func testHandlerDetachesAtEndOfOutputAndKeepsAllOutput() throws {+ let p = Process()+ p.executableURL = URL(fileURLWithPath: "/bin/sh")+ p.arguments = ["-c", "echo one; echo two; echo three"]+ let pipe = Pipe(); p.standardOutput = pipe; p.standardError = pipe+ let lock = NSLock()+ var received = Data()+ PipeReader.attach(to: pipe.fileHandleForReading) { d in lock.lock(); received.append(d); lock.unlock() }+ XCTAssertNotNil(pipe.fileHandleForReading.readabilityHandler)+ try p.run()+ p.waitUntilExit()+ let deadline = Date().addingTimeInterval(5)+ while pipe.fileHandleForReading.readabilityHandler != nil, Date() < deadline {+ Thread.sleep(forTimeInterval: 0.02)+ }+ XCTAssertNil(pipe.fileHandleForReading.readabilityHandler, "the handler is still attached after end of output")+ lock.lock(); defer { lock.unlock() }+ XCTAssertEqual(String(data: received, encoding: .utf8), "one\ntwo\nthree\n")+ }+}