Skip to content

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

launcher: the .NET row follows the dotnet the build runs, in NXCore (#342)

v0lumehi4c7baee02c60
diff --git a/frontend/Sources/NXCore/Toolchain.swift b/frontend/Sources/NXCore/Toolchain.swift
index 6cd7d8dd..20d1a90b 100644
--- a/frontend/Sources/NXCore/Toolchain.swift
+++ b/frontend/Sources/NXCore/Toolchain.swift
@@ -311,19 +311,35 @@ package enum Toolchain {
return parseDotnetRoot(out)
}
- package static func findDotnet() -> (path: String, ok: Bool) {
- let cands = [NSHomeDirectory() + "/.dotnet/dotnet",
- "/usr/local/share/dotnet/dotnet",
- "/opt/homebrew/bin/dotnet",
- "/usr/local/bin/dotnet"]
- if let d = firstExisting(cands) {
- // the extractor targets net10.0, so an SDK (not just a runtime) is needed
- let ok = runOK(d, ["--list-sdks"], env: ["DOTNET_ROOT": (d as NSString).deletingLastPathComponent,
- "PATH": "/usr/bin:/bin", "HOME": NSHomeDirectory(),
- "TMPDIR": NSTemporaryDirectory()])
- return (d, ok)
+ /// Where the build looks for `dotnet`, in its order: build_ipa_from_nsp.py puts TOOL_PATHS (/opt/homebrew/bin, /usr/local/bin,
+ /// /usr/local/share/dotnet, ~/.dotnet) in front of PATH, then the first of these that lists an SDK 10 in front of the rest (the
+ /// engine patch nsp-dotnet-sdk10-first). Before that patch, every build on a Mac whose /usr/local/share/dotnet held an Intel
+ /// .NET 7 ran that one and stopped at NETSDK1045.
+ package static func dotnetCandidates(home: String = NSHomeDirectory()) -> [String] {
+ ["/opt/homebrew/bin/dotnet", "/usr/local/bin/dotnet", "/usr/local/share/dotnet/dotnet", home + "/.dotnet/dotnet"]
+ }
+
+ /// Whether `dotnet --list-sdks` names an SDK the extractor builds with (it targets net10.0, so 10 or newer).
+ package static func hasSDK10(_ listSdks: String) -> Bool {
+ listSdks.split(separator: "\n").contains { ln in
+ Int(ln.prefix { $0.isNumber }).map { $0 >= 10 } ?? false
}
- return ("dotnet", false)
+ }
+
+ /// Whether this dotnet runs and lists an SDK 10 (an SDK, not just a runtime: the extractor is built).
+ package static func dotnetHasSDK10(_ dotnet: String, runner: ProcessRunner = SystemProcessRunner()) -> Bool {
+ let env = ["DOTNET_ROOT": (dotnet as NSString).deletingLastPathComponent,
+ "PATH": "/usr/bin:/bin", "HOME": NSHomeDirectory(), "TMPDIR": NSTemporaryDirectory()]
+ return runner.output(dotnet, ["--list-sdks"], env: env).map(hasSDK10) ?? false
+ }
+
+ /// The dotnet the build runs and whether it has an SDK 10: the first candidate that lists one, else the first that exists
+ /// (the row then says Install), else plain "dotnet". `exists` is injectable so a test needs no file on disk.
+ package static func findDotnet(_ cands: [String] = dotnetCandidates(), runner: ProcessRunner = SystemProcessRunner(),
+ exists: (String) -> Bool = { FileManager.default.isExecutableFile(atPath: $0) }) -> (path: String, ok: Bool) {
+ let found = cands.filter(exists)
+ if let d = found.first(where: { dotnetHasSDK10($0, runner: runner) }) { return (d, true) }
+ return (found.first ?? "dotnet", false)
}
/// Whether the selected developer directory exists (`xcode-select -p`), which is the same
diff --git a/frontend/Tests/NXCoreTests/DotnetDetectionTests.swift b/frontend/Tests/NXCoreTests/DotnetDetectionTests.swift
new file mode 100644
index 00000000..7c158791
--- /dev/null
+++ b/frontend/Tests/NXCoreTests/DotnetDetectionTests.swift
@@ -0,0 +1,46 @@
+import XCTest
+@testable import NXCore
+
+/// The .NET row looks for dotnet in the build's order and is ready with the first that lists an SDK 10, the one the build puts in front.
+final class DotnetDetectionTests: XCTestCase {
+ func testTheBuildsOrder() {
+ // build_ipa_from_nsp.py's TOOL_PATHS: Homebrew, /usr/local/bin, the installer's folder, then ~/.dotnet
+ XCTAssertEqual(Toolchain.dotnetCandidates(home: "/Users/p"),
+ ["/opt/homebrew/bin/dotnet", "/usr/local/bin/dotnet", "/usr/local/share/dotnet/dotnet", "/Users/p/.dotnet/dotnet"])
+ }
+
+ func testOnlyAnSDK10Counts() {
+ XCTAssertFalse(Toolchain.hasSDK10("7.0.402 [/usr/local/share/dotnet/sdk]\n")) // an Intel .NET 7 in front of ~/.dotnet
+ XCTAssertFalse(Toolchain.hasSDK10(""))
+ XCTAssertFalse(Toolchain.hasSDK10("8.0.318 [/x/sdk]\n9.0.100 [/x/sdk]"))
+ XCTAssertTrue(Toolchain.hasSDK10("7.0.402 [/a/sdk]\n10.0.403 [/b/sdk]\n"))
+ XCTAssertTrue(Toolchain.hasSDK10("11.0.100-preview.1 [/b/sdk]"))
+ }
+
+ func testTheFirstWithAnSDK10() {
+ let old = "/share/dotnet", home = "/home/.dotnet/dotnet", broken = "/broken/dotnet", absent = "/none/dotnet"
+ let sdks = [old: "7.0.402 [/s/sdk]\n", home: "9.0.100 [/h/sdk]\n10.0.403 [/h/sdk]\n", broken: "10.0.403 [/b/sdk]\n"]
+ let runner = FakeProcessRunner()
+ runner.answer = { l in
+ l.executable == broken ? ProcessResult(status: 1, out: sdks[broken]!) : ProcessResult(out: sdks[l.executable] ?? "")
+ }
+ func pick(_ cands: [String]) -> String {
+ let r = Toolchain.findDotnet(cands, runner: runner, exists: { $0 != absent })
+ return (r.ok ? "ready " : "missing ") + r.path
+ }
+ // an Intel .NET 7 in /usr/local/share/dotnet before the .NET 10 in ~/.dotnet: the build puts the second in front
+ XCTAssertEqual(pick([absent, old, home]), "ready " + home)
+ XCTAssertEqual(pick([home, old]), "ready " + home)
+ XCTAssertEqual(pick([broken, home]), "ready " + home)
+ // no SDK 10 anywhere: Install, as with no dotnet at all
+ XCTAssertEqual(pick([absent, old]), "missing " + old)
+ XCTAssertEqual(pick([absent]), "missing dotnet")
+ }
+
+ func testTheRootIsTheFolderOfTheCandidate() {
+ let runner = FakeProcessRunner()
+ runner.answer = { _ in ProcessResult(out: "10.0.100 [/h/sdk]\n") }
+ XCTAssertTrue(Toolchain.dotnetHasSDK10("/home/.dotnet/dotnet", runner: runner))
+ XCTAssertEqual(runner.launches.first?.environment?["DOTNET_ROOT"], "/home/.dotnet")
+ }
+}