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
Stage game files with an upper case extension under a lowercase name (#554)
v0lumehi98eb06683ea9
diff --git a/frontend-qt/nspb_qt/input_staging.py b/frontend-qt/nspb_qt/input_staging.pyindex 6f42793a..bdb05fa6 100644--- a/frontend-qt/nspb_qt/input_staging.py+++ b/frontend-qt/nspb_qt/input_staging.py@@ -20,6 +20,23 @@ def folder(work_dir: str) -> str:return str(Path(work_dir, FOLDER_NAME))+def staged_name(name: str) -> str:+ """The name a file has in the staging folder: a game file keeps its name with the extension in lowercase. The script+ finds packages with *.nsp (and .xci, .nsz, .xcz) and that glob is case-sensitive, so Game [0100F2C0115B6000].NSP+ was not found ("no NSP with a readable title ID", #554)."""+ p = Path(name)+ suffix = p.suffix+ if suffix.lower() in GAME_SUFFIXES and suffix != suffix.lower():+ return p.stem + suffix.lower()+ return name+++def needs_lowercase_name(path: str) -> bool:+ """A game file with an upper or mixed case extension: the script would not find it as it is."""+ name = Path(path).name+ return staged_name(name) != name++def _same(src: str, dst: Path) -> bool:"""dst is already src: the same file, or a copy with its size and date."""try:@@ -35,7 +52,7 @@ def sync(files: list[str], into: str, *, hard_links: bool = True) -> int:"""Make `into` hold exactly `files` (by file name), with their dates. Returns how many were linked."""root = Path(into)root.mkdir(parents=True, exist_ok=True)- wanted = {Path(f).name: f for f in files if f}+ wanted = {staged_name(Path(f).name): f for f in files if f}for entry in root.iterdir(): # a stale entry from an earlier selection goes: only game files and linksif entry.is_symlink() or (entry.is_file() and entry.suffix.lower() in GAME_SUFFIXES):src = wanted.get(entry.name)diff --git a/frontend-qt/nspb_qt/model.py b/frontend-qt/nspb_qt/model.pyindex b843d601..67c621a4 100644--- a/frontend-qt/nspb_qt/model.py+++ b/frontend-qt/nspb_qt/model.py@@ -1306,7 +1306,8 @@ class Model(QObject):work_dir = str(Path(self.out_dir or base_dir, f'{tid_hex}_build'))input_dir = folder.script_input if folder is not None else base_dir- if len(dirs) > 1:+ if len(dirs) > 1 or any(input_staging.needs_lowercase_name(f) for f in all_files if f):+ # (a .NSP or .Xci file goes through it too, under a lowercase extension: the script's glob misses it, #554)# files from different folders: a stable folder of links per game, so the script sees one# folder and its fingerprints match on the next build (#132)staging = input_staging.folder(work_dir)diff --git a/frontend-qt/tests/test_input_staging.py b/frontend-qt/tests/test_input_staging.pyindex 19781833..d62cf9c8 100644--- a/frontend-qt/tests/test_input_staging.py+++ b/frontend-qt/tests/test_input_staging.py@@ -53,6 +53,30 @@ class InputStagingTests(unittest.TestCase):self.assertEqual(input_staging.sync([base], d), 1)self.assertEqual({p.name for p in Path(d).iterdir()}, {'base.nsp', '0100000000000000_build'})+ def test_extension_is_lowercased_in_the_staged_name(self): # #554+ S = input_staging+ self.assertEqual(S.staged_name('Game [0100F2C0115B6000].NSP'), 'Game [0100F2C0115B6000].nsp')+ self.assertEqual(S.staged_name('Game [0100F2C0115B6000].Nsz'), 'Game [0100F2C0115B6000].nsz')+ self.assertEqual(S.staged_name('Game.XcI'), 'Game.xci')+ self.assertEqual(S.staged_name('Game.nsp'), 'Game.nsp')+ self.assertEqual(S.staged_name('Game.NSP.XCZ'), 'Game.NSP.xcz')+ self.assertEqual(S.staged_name('PROD.KEYS'), 'PROD.KEYS')+ self.assertTrue(S.needs_lowercase_name('/x/Game [0100F2C0115B6000].NSP'))+ self.assertTrue(S.needs_lowercase_name('/x/Game.Nsp'))+ self.assertFalse(S.needs_lowercase_name('/x/Game.nsp'))+ self.assertFalse(S.needs_lowercase_name('/x/Game.NUP'))++ def test_uppercase_file_is_staged_under_a_lowercase_name(self): # #554+ for hard in (True, False):+ upper = make(self.root / 'a' / 'Game [0100F2C0115B6000].NSP', 100)+ mixed = make(self.root / 'a' / 'Game [0100F2C0115B6800][v65536].Nsp', 100)+ d = input_staging.folder(self.work + str(hard))+ self.assertEqual(input_staging.sync([upper, mixed], d, hard_links=hard), 2)+ self.assertEqual(sorted(p.name for p in Path(d).iterdir()),+ ['Game [0100F2C0115B6000].nsp', 'Game [0100F2C0115B6800][v65536].nsp'])+ self.assertEqual(input_staging.sync([upper, mixed], d, hard_links=hard), 2) # again: nothing stale+ self.assertEqual(len(list(Path(d).iterdir())), 2)+if __name__ == '__main__':unittest.main()diff --git a/frontend/Sources/NXCore/InputStaging.swift b/frontend/Sources/NXCore/InputStaging.swiftindex ea12dc7d..89d0acd9 100644--- a/frontend/Sources/NXCore/InputStaging.swift+++ b/frontend/Sources/NXCore/InputStaging.swift@@ -13,12 +13,27 @@ package enum InputStaging {package static func folder(workDir: String) -> String { workDir + "/" + folderName }+ /// The name a file has in the staging folder: a game file keeps its name with the extension in lowercase. The script+ /// finds packages with `*.nsp` (and `.xci`, `.nsz`, `.xcz`) and that glob is case-sensitive, so `Game [0100F2C0115B6000].NSP`+ /// was not found ("no NSP with a readable title ID", #554).+ package static func stagedName(_ name: String) -> String {+ let ext = (name as NSString).pathExtension+ guard gameExtensions.contains(ext.lowercased()), ext != ext.lowercased() else { return name }+ return (name as NSString).deletingPathExtension + "." + ext.lowercased()+ }++ /// Whether a file's name is one the script would not find as it is: a game file with an upper or mixed case extension.+ package static func needsLowercaseName(_ path: String) -> Bool {+ let name = (path as NSString).lastPathComponent+ return stagedName(name) != name+ }+/// Makes `dir` hold exactly `files`, by file name. Returns how many were linked.package static func sync(files: [String], into dir: String) -> Int {let fm = FileManager.defaulttry? fm.createDirectory(atPath: dir, withIntermediateDirectories: true)var wanted: [String: String] = [:]- for f in files where !f.isEmpty { wanted[(f as NSString).lastPathComponent] = f }+ for f in files where !f.isEmpty { wanted[stagedName((f as NSString).lastPathComponent)] = f }// A stale entry from an earlier selection goes. Only game files and links are touched.for name in (try? fm.contentsOfDirectory(atPath: dir)) ?? [] {let path = dir + "/" + namediff --git a/frontend/Sources/NXModel/BuildStore+Run.swift b/frontend/Sources/NXModel/BuildStore+Run.swiftindex aff181a2..853ee579 100644--- a/frontend/Sources/NXModel/BuildStore+Run.swift+++ b/frontend/Sources/NXModel/BuildStore+Run.swift@@ -88,7 +88,8 @@ extension BuildStore {// /NX2Apple-shell, a DMG chosen as Save to) failed at the last step after the whole build (#492).if target != BuildTarget.mac, !exportsOneGame, let why = shellRootProblem() { fail(why); return }var inputDir = folder?.scriptInput ?? baseDir // a NUP goes to the script as the file- if dirs.count > 1 || !unpicked.isEmpty {+ // A file named .NSP or .Xci is staged too, under a lowercase extension: the script's glob misses it (#554).+ if dirs.count > 1 || !unpicked.isEmpty || staged.contains(where: InputStaging.needsLowercaseName) {// A stable folder per game (#132): the script's fingerprints name its files and its profile path.let tmp = InputStaging.folder(workDir: workDir)let linked = InputStaging.sync(files: staged, into: tmp)diff --git a/frontend/Tests/NX2AppleTests/InputStagingTests.swift b/frontend/Tests/NX2AppleTests/InputStagingTests.swiftindex fb5fc8c2..10653487 100644--- a/frontend/Tests/NX2AppleTests/InputStagingTests.swift+++ b/frontend/Tests/NX2AppleTests/InputStagingTests.swift@@ -64,4 +64,32 @@ final class InputStagingTests: IsolatedDefaultsTestCase {_ = InputStaging.sync(files: [two], into: dir)XCTAssertEqual(try FileManager.default.destinationOfSymbolicLink(atPath: dir + "/base.nsp"), two)}++ /// #554: the script finds packages with a case-sensitive `*.nsp` glob, so the staging folder names them in lowercase.+ func testExtensionIsLowercasedInTheStagedName() {+ XCTAssertEqual(InputStaging.stagedName("Game [0100F2C0115B6000].NSP"), "Game [0100F2C0115B6000].nsp")+ XCTAssertEqual(InputStaging.stagedName("Game [0100F2C0115B6000].Nsz"), "Game [0100F2C0115B6000].nsz")+ XCTAssertEqual(InputStaging.stagedName("Game [0100F2C0115B6000].XcI"), "Game [0100F2C0115B6000].xci")+ XCTAssertEqual(InputStaging.stagedName("Game [0100F2C0115B6000].nsp"), "Game [0100F2C0115B6000].nsp")+ XCTAssertEqual(InputStaging.stagedName("Game.NSP.XCZ"), "Game.NSP.xcz")+ XCTAssertEqual(InputStaging.stagedName("PROD.KEYS"), "PROD.KEYS")+ XCTAssertTrue(InputStaging.needsLowercaseName("/x/Game [0100F2C0115B6000].NSP"))+ XCTAssertTrue(InputStaging.needsLowercaseName("/x/Game.Nsp"))+ XCTAssertFalse(InputStaging.needsLowercaseName("/x/Game.nsp"))+ XCTAssertFalse(InputStaging.needsLowercaseName("/x/Game.NUP"))+ }++ func testUppercaseFileIsStagedUnderALowercaseName() throws {+ let upper = try game("Game [0100F2C0115B6000].NSP", in: a, age: 100)+ let mixed = try game("Game [0100F2C0115B6800][v65536].Nsp", in: a, age: 100)+ let dir = InputStaging.folder(workDir: work)+ XCTAssertEqual(InputStaging.sync(files: [upper, mixed], into: dir), 2)+ XCTAssertEqual(Set(try FileManager.default.contentsOfDirectory(atPath: dir)),+ ["Game [0100F2C0115B6000].nsp", "Game [0100F2C0115B6800][v65536].nsp"])+ // a second run with the same selection keeps the links and leaves nothing stale+ XCTAssertEqual(InputStaging.sync(files: [upper, mixed], into: dir), 2)+ XCTAssertEqual(try FileManager.default.contentsOfDirectory(atPath: dir).count, 2)+ // the staged file is the original: the script sees its size and date+ XCTAssertEqual(try FileManager.default.destinationOfSymbolicLink(atPath: dir + "/Game [0100F2C0115B6000].nsp"), upper)+ }}