Skip to content

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

iOS shell: Show log finds the engine log of the game's last run

v0lumehie0d09c1b389c
diff --git a/frontend-ios/Sources/ShellHost/LogReader.swift b/frontend-ios/Sources/ShellHost/LogReader.swift
index aba6fea3..947f135a 100644
--- a/frontend-ios/Sources/ShellHost/LogReader.swift
+++ b/frontend-ios/Sources/ShellHost/LogReader.swift
@@ -24,4 +24,13 @@ public enum LogReader {
guard (try? handle.seek(toOffset: start)) != nil, let data = try? handle.readToEnd() else { return nil }
return (String(decoding: data, as: UTF8.self), start > 0)
}
+
+ /// True when the log at `url` is a run of the game `id`: the shell's first lines name the game's framework
+ /// (`shell: starting …/DmGame<title ID>.framework/…`, ShellMain.m).
+ public static func isRun(of id: String, _ url: URL) -> Bool {
+ guard let handle = try? FileHandle(forReadingFrom: url) else { return false }
+ defer { try? handle.close() }
+ guard let head = try? handle.read(upToCount: 8192) else { return false }
+ return String(decoding: head, as: UTF8.self).contains("/DmGame\(id).framework/")
+ }
}
diff --git a/frontend-ios/Sources/ShellHost/ShellHostModel.swift b/frontend-ios/Sources/ShellHost/ShellHostModel.swift
index f24bc582..20a37e75 100644
--- a/frontend-ios/Sources/ShellHost/ShellHostModel.swift
+++ b/frontend-ios/Sources/ShellHost/ShellHostModel.swift
@@ -243,7 +243,10 @@ public struct ShellHostConfig: Sendable {
guard let entry = entry(id) else { return }
let app = appFolder(for: id)
let folders = [data(for: entry).url, app].compactMap { $0 }
+ // The shell's engine writes Documents/log.txt (EngineLaunchPlan) for whichever game ran last, so it counts only when that run was this game's
+ let engine = config.appDataRoot.deletingLastPathComponent().appendingPathComponent("log.txt")
let found = folders.lazy.compactMap { LogReader.read($0.appendingPathComponent("log.txt")) }.first
+ ?? (LogReader.isRun(of: id, engine) ? LogReader.read(engine) : nil)
log = LogPresentation(gameID: id, title: entry.title, text: found?.text, truncated: found?.truncated ?? false)
}
diff --git a/frontend-ios/Tests/ShellHostTests/ShellHostModelTests.swift b/frontend-ios/Tests/ShellHostTests/ShellHostModelTests.swift
index 808fcd3c..c733251e 100644
--- a/frontend-ios/Tests/ShellHostTests/ShellHostModelTests.swift
+++ b/frontend-ios/Tests/ShellHostTests/ShellHostModelTests.swift
@@ -176,6 +176,18 @@ final class Clock: @unchecked Sendable { var date: Date; init(_ d: Date) { date
#expect(model.log?.text == "boot ok\nframe 1\n" && model.log?.truncated == false)
}
+ @Test func showLogReadsTheEngineLogOnlyForTheGameThatRanLast() throws {
+ let box = try Sandbox()
+ let model = ShellHostModel(config: box.config())
+ let engine = box.root.appendingPathComponent("Documents/log.txt")
+ try box.write("shell: starting /app/Frameworks/DmGame\(Fixtures.orbit).framework/dm\nboot ok\n", to: engine)
+ model.actions.onShowLog(Fixtures.orbit)
+ #expect(model.log?.text?.hasSuffix("boot ok\n") == true)
+ try box.write("shell: starting /app/Frameworks/DmGame0100000000020000.framework/dm\nboot ok\n", to: engine)
+ model.actions.onShowLog(Fixtures.orbit)
+ #expect(model.log?.text == nil)
+ }
+
@Test func aLongLogShowsItsEnd() throws {
let box = try Sandbox()
let url = box.appData.appendingPathComponent("log.txt")