Skip to content

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

frontend-qt: true multi-process rewrite pool, reveal helper, SDK long paths, and UI polish

LNPANDY14efcb1600ff
diff --git a/.github/workflows/build-qt.yml b/.github/workflows/build-qt.yml
index dc409ceb..107d8a50 100644
--- a/.github/workflows/build-qt.yml
+++ b/.github/workflows/build-qt.yml
@@ -109,6 +109,7 @@ jobs:
--hidden-import nspb_qt.win.sdk_fetch \
--hidden-import nspb_qt.win.driver \
--hidden-import nspb_qt.win.shim \
+ --hidden-import nspb_qt.win.mp_worker \
--hidden-import nspb_qt.win.build_app \
--hidden-import nspb_qt.win.fix_ipa_modes \
--hidden-import nspb_qt.win.doctor \
diff --git a/frontend-qt/nspb_qt/platform_util.py b/frontend-qt/nspb_qt/platform_util.py
index 7c671e4f..b84326ad 100644
--- a/frontend-qt/nspb_qt/platform_util.py
+++ b/frontend-qt/nspb_qt/platform_util.py
@@ -377,6 +377,85 @@ def no_window_kwargs() -> dict:
return {}
+_ORIG_POPEN_INIT = subprocess.Popen.__init__
+
+
+def reveal(path: str | Path | None) -> bool:
+ """Show `path` in the system file manager (Explorer on Windows, Finder on macOS).
+
+ If `path` is a file (such as an `.ipa` or `romfs.img`), opens its parent folder with the
+ file selected; if `path` is a folder that contains `romfs.img`, opens that folder with
+ `romfs.img` selected; otherwise opens the folder itself.
+ """
+ if not path:
+ return False
+ p = Path(str(path)).expanduser()
+ if not p.exists():
+ if p.parent.is_dir():
+ p = p.parent
+ else:
+ return False
+ if p.is_dir() and (p / 'romfs.img').is_file():
+ target = (p / 'romfs.img').resolve()
+ is_file = True
+ else:
+ target = p.resolve()
+ is_file = target.is_file()
+
+ if IS_WIN:
+ norm = os.path.normpath(str(target))
+ if is_file:
+ try:
+ import ctypes
+ shell32 = ctypes.windll.shell32
+ shell32.ILCreateFromPathW.argtypes = [ctypes.c_wchar_p]
+ shell32.ILCreateFromPathW.restype = ctypes.c_void_p
+ shell32.SHOpenFolderAndSelectItems.argtypes = [
+ ctypes.c_void_p, ctypes.c_uint, ctypes.c_void_p, ctypes.c_ulong
+ ]
+ shell32.SHOpenFolderAndSelectItems.restype = ctypes.c_long
+ shell32.ILFree.argtypes = [ctypes.c_void_p]
+ shell32.ILFree.restype = None
+ ctypes.windll.ole32.CoInitialize(None)
+ pidl = shell32.ILCreateFromPathW(norm)
+ if pidl:
+ try:
+ hr = shell32.SHOpenFolderAndSelectItems(pidl, 0, None, 0)
+ if hr == 0:
+ return True
+ finally:
+ shell32.ILFree(pidl)
+ except Exception:
+ pass
+ try:
+ subprocess.Popen(['explorer.exe', f'/select,{norm}'])
+ return True
+ except OSError:
+ pass
+ folder_norm = os.path.normpath(str(target.parent if is_file else target))
+ if hasattr(os, 'startfile'):
+ try:
+ os.startfile(folder_norm) # type: ignore[attr-defined]
+ return True
+ except OSError:
+ pass
+ elif IS_MAC:
+ try:
+ cmd = ['/usr/bin/open', '-R', str(target)] if is_file else ['/usr/bin/open', str(target)]
+ subprocess.Popen(cmd)
+ return True
+ except OSError:
+ pass
+
+ try:
+ from PySide6.QtCore import QUrl
+ from PySide6.QtGui import QDesktopServices
+ open_dir = target.parent if is_file else target
+ return bool(QDesktopServices.openUrl(QUrl.fromLocalFile(str(open_dir))))
+ except Exception:
+ return False
+
+
def hide_console_for_children() -> None:
"""Stop *every* child this process starts from opening a console window (Windows only).
@@ -399,7 +478,12 @@ def hide_console_for_children() -> None:
original = subprocess.Popen.__init__
def patched(self, args, *a, **k): # noqa: ANN001 - mirrors Popen.__init__
- if not k.get('creationflags') and not k.get('startupinfo'):
+ first = ''
+ if isinstance(args, (list, tuple)) and args:
+ first = os.path.basename(str(args[0])).lower()
+ elif isinstance(args, str):
+ first = os.path.basename(args.split()[0] if args.split() else '').lower()
+ if first not in ('explorer', 'explorer.exe') and not k.get('creationflags') and not k.get('startupinfo'):
k['creationflags'] = subprocess.CREATE_NO_WINDOW
original(self, args, *a, **k)
diff --git a/frontend-qt/nspb_qt/ui.py b/frontend-qt/nspb_qt/ui.py
index 3be60a73..1816ae2f 100644
--- a/frontend-qt/nspb_qt/ui.py
+++ b/frontend-qt/nspb_qt/ui.py
@@ -1066,7 +1066,7 @@ class MainWindow(QMainWindow):
rs1_lay.addLayout(s1_col, 1)
self.step1_reveal_btn = QPushButton(self.m.t('shell.showInFinder'))
- self.step1_reveal_btn.clicked.connect(lambda: pu.reveal(self.m.ipa_path))
+ self.step1_reveal_btn.clicked.connect(lambda: pu.reveal(self._effective_ipa_path()))
self.step1_reveal_btn.hide()
rs1_lay.addWidget(self.step1_reveal_btn)
grp.add_row(r_s1)
@@ -1114,7 +1114,7 @@ class MainWindow(QMainWindow):
rs3_lay.addLayout(s3_col, 1)
self.step3_open_romfs_btn = QPushButton(self.m.t('shell.showInFinder'))
- self.step3_open_romfs_btn.clicked.connect(lambda: pu.reveal(self.m.archive_path))
+ self.step3_open_romfs_btn.clicked.connect(lambda: pu.reveal(self._effective_archive_path()))
self.step3_open_romfs_btn.hide()
rs3_lay.addWidget(self.step3_open_romfs_btn)
@@ -1131,6 +1131,31 @@ class MainWindow(QMainWindow):
return grp
+ def _effective_ipa_path(self) -> str:
+ if self.m.ipa_path and os.path.exists(self.m.ipa_path):
+ return self.m.ipa_path
+ tid = self.m.build_key
+ if tid and self.m.out_dir:
+ cand = os.path.join(self.m.out_dir, f'{tid}.ipa')
+ if os.path.isfile(cand):
+ return cand
+ sel = self.m.library.selected
+ if sel and sel.has_ipa:
+ return sel.ipa_path
+ return self.m.ipa_path
+
+ def _effective_archive_path(self) -> str:
+ if self.m.archive_path and os.path.isdir(self.m.archive_path):
+ return self.m.archive_path
+ if self.m.out_dir:
+ cand = os.path.join(self.m.out_dir, 'romfs')
+ if os.path.isdir(cand):
+ return cand
+ sel = self.m.library.selected
+ if sel and sel.has_archive:
+ return sel.archive_path
+ return self.m.archive_path
+
def _on_ipa_kind_changed(self, kind: str) -> None:
self.m.ipa_kind = kind
self._refresh_install_card()
@@ -1149,8 +1174,10 @@ class MainWindow(QMainWindow):
self.ipa_kind_note.setText(self.m.t('shell.ipaOneGameNote' if one else 'shell.ipaSharedNote'))
self.step1_title.setText('① ' + self.m.t('shell.step1Title'))
self.step1_sub.setText(self.m.t('shell.oneGameSub' if one else 'shell.step1Sub'))
- has_ipa = bool(self.m.ipa_path and os.path.exists(self.m.ipa_path))
+ ipa_p = self._effective_ipa_path()
+ has_ipa = bool(ipa_p and os.path.exists(ipa_p))
self.step1_reveal_btn.setText(self.m.t('shell.showInFinder'))
+ self.step1_reveal_btn.setToolTip(self.m.t('msg.ipaAt', ipa_p) if has_ipa else '')
self.step1_reveal_btn.setVisible(has_ipa)
self.step2_title.setText('② ' + self.m.t('shell.step2Title'))
self.step2_sub.setText(self.m.t('shell.oneGameStep2Sub' if one else 'shell.step2Sub'))
@@ -1160,7 +1187,9 @@ class MainWindow(QMainWindow):
self.drive_choose_btn.setText(self.m.t('shell.driveChoose'))
self.drive_copy_btn.setText(self.m.t('shell.driveCopy'))
self.step3_container.setVisible(not one)
- has_arch = bool(self.m.archive_path and os.path.isdir(self.m.archive_path))
+ arch_p = self._effective_archive_path()
+ has_arch = bool(arch_p and os.path.isdir(arch_p))
+ self.step3_open_romfs_btn.setToolTip(self.m.t('msg.archiveAt', arch_p) if has_arch else '')
self.step3_open_romfs_btn.setVisible(has_arch)
self.drive_copy_btn.setEnabled(has_arch)
@@ -1172,7 +1201,8 @@ class MainWindow(QMainWindow):
self._refresh_install_card()
def _copy_archive_to_drive(self) -> None:
- if not self.m.archive_path or not os.path.isdir(self.m.archive_path):
+ arch_p = self._effective_archive_path()
+ if not arch_p or not os.path.isdir(arch_p):
return
if not self._drive_folder:
d = QFileDialog.getExistingDirectory(self, self.m.t('shell.driveChoose'), str(Path.home()))
@@ -1181,7 +1211,7 @@ class MainWindow(QMainWindow):
self._drive_folder = d
self.drive_path_lbl.setText(d)
tid = self.m.build_key
- ok, info = lib.DriveCopy.copy_to_folder(self.m.archive_path, self._drive_folder, tid)
+ ok, info = lib.DriveCopy.copy_to_folder(arch_p, self._drive_folder, tid)
if ok:
self.step3_sub.setText(self.m.t('shell.driveDone', info, self.m.t('ios.ui.gameData')))
else:
@@ -1680,6 +1710,7 @@ class MainWindow(QMainWindow):
self.act_progress = QProgressBar()
self.act_progress.setRange(0, 100)
+ self.act_progress.setTextVisible(False)
self.act_progress.setFixedWidth(160)
self.act_progress.hide()
row.addWidget(self.act_progress)
@@ -2055,6 +2086,7 @@ class MainWindow(QMainWindow):
self.out_label.setText(self.m.out_dir or t('build.saveDefault'))
self.out_clear_btn.setVisible(bool(self.m.out_dir))
+ self._refresh_install_card()
self._refresh_running()
def _refresh_target(self) -> None:
@@ -2156,10 +2188,8 @@ class MainWindow(QMainWindow):
self._switch_page('builds')
self.scroll.ensureWidgetVisible(self.log, 0, 0)
self._show_failure()
- self.reveal_ipa_btn.setToolTip(t('msg.ipaAt', self.m.ipa_path) if self.m.ipa_path else '')
- self.reveal_arc_btn.setToolTip(t('msg.archiveAt', self.m.archive_path) if self.m.archive_path else '')
- self.reveal_ipa_btn.setVisible(bool(self.m.ipa_path) and Path(self.m.ipa_path).exists())
- self.reveal_arc_btn.setVisible(bool(self.m.archive_path) and Path(self.m.archive_path).exists())
+ self.reveal_ipa_btn.hide()
+ self.reveal_arc_btn.hide()
self.reveal_app_btn.setVisible(bool(self.m.mac_app_path) and Path(self.m.mac_app_path).exists())
self._refresh_install_card()
self._refresh_library()
diff --git a/frontend-qt/nspb_qt/win/build_app.py b/frontend-qt/nspb_qt/win/build_app.py
index 3271fe8b..a96d8df9 100644
--- a/frontend-qt/nspb_qt/win/build_app.py
+++ b/frontend-qt/nspb_qt/win/build_app.py
@@ -619,27 +619,68 @@ def main(argv: list[str] | None = None) -> int:
base = w / 'build/ios/win'
gen, app = base / 'Gen', base / 'Payload/DmIOS.app'
- shutil.rmtree(gen, ignore_errors=True)
shutil.rmtree(app, ignore_errors=True)
- (gen / 'profile').mkdir(parents=True)
+ (gen / 'profile').mkdir(parents=True, exist_ok=True)
app.mkdir(parents=True)
- # --- generate Gen/ (the same generators build_ipa.sh calls)
+ def _replace_if_changed(tmp_p: Path, dst_p: Path) -> None:
+ if dst_p.is_file():
+ try:
+ if dst_p.read_bytes() == tmp_p.read_bytes():
+ tmp_p.unlink(missing_ok=True)
+ return
+ except OSError:
+ pass
+ os.replace(tmp_p, dst_p)
+
+ # --- generate Gen/ (the same generators build_ipa.sh calls, preserving mtime when unchanged)
t('generate Gen/ (profile header, embedded modules, tables)')
- py(repo / 'dm/games/game.py', 'header', game, gen / 'profile/game_profile.h')
+ gp_h = gen / 'profile/game_profile.h'
+ gp_h_tmp = gen / 'profile/game_profile.h.tmp'
+ py(repo / 'dm/games/game.py', 'header', game, gp_h_tmp)
+ _replace_if_changed(gp_h_tmp, gp_h)
+
msl_srcs = sorted((repo / 'dm/gfx/msl').glob('*.metal'))
if msl_srcs:
- py(repo / 'dm/gfx/tools/host_msl.py', '--sdk', 'iphoneos',
- '--min', tc.setting('metal_ios_target', '18.0'),
- gen / 'profile/host_msl_gen.h', *msl_srcs)
+ msl_min = tc.setting('metal_ios_target', '18.0')
+ msl_h = gen / 'profile/host_msl_gen.h'
+ msl_key_file = gen / 'profile/host_msl.sha256'
+ h_msl = hashlib.sha256(msl_min.encode())
+ for ms in msl_srcs + [repo / 'dm/gfx/tools/host_msl.py']:
+ try:
+ h_msl.update(ms.name.encode() + b'\0' + ms.read_bytes())
+ except OSError:
+ pass
+ msl_digest = h_msl.hexdigest()
+ if not (msl_h.is_file() and msl_key_file.is_file()
+ and msl_key_file.read_text(encoding='utf-8', errors='replace').strip() == msl_digest):
+ msl_h_tmp = gen / 'profile/host_msl_gen.h.tmp'
+ py(repo / 'dm/gfx/tools/host_msl.py', '--sdk', 'iphoneos',
+ '--min', msl_min, msl_h_tmp, *msl_srcs)
+ _replace_if_changed(msl_h_tmp, msl_h)
+ msl_key_file.write_text(msl_digest + '\n', encoding='utf-8')
+
es = gen / 'embed.S'
+ es_tmp = gen / 'embed.S.tmp'
jit_img = romfs.parent / 'jit' / 'jit.img'
embed_extra = [jit_img] if jit_img.is_file() else []
- flags = py(repo / 'dm/ios/tools/gen_embed.py', patched, es, *mods, *embed_extra)
+ flags = py(repo / 'dm/ios/tools/gen_embed.py', patched, es_tmp, *mods, *embed_extra)
+ bin_meta = []
+ for m_name in mods:
+ bp = patched / f'{m_name}.patched.bin'
+ if bp.is_file():
+ st = bp.stat()
+ bin_meta.append(f'{m_name}:{st.st_size}:{st.st_mtime_ns}')
+ for ep in embed_extra:
+ if ep.is_file():
+ st = ep.stat()
+ bin_meta.append(f'{ep.name}:{st.st_size}:{st.st_mtime_ns}')
# the assembler treats '\' as an escape in .incbin paths
- es.write_text(re.sub(r'\.incbin "([^"]*)"',
- lambda mm: '.incbin "' + mm.group(1).replace('\\', '/') + '"',
- es.read_text(encoding='utf-8')), encoding='utf-8')
+ es_body = re.sub(r'\.incbin "([^"]*)"',
+ lambda mm: '.incbin "' + mm.group(1).replace('\\', '/') + '"',
+ es_tmp.read_text(encoding='utf-8'))
+ es_tmp.write_text(es_body + f'\n// inputs: {" ".join(bin_meta)}\n', encoding='utf-8')
+ _replace_if_changed(es_tmp, es)
dmres = app / 'dm'
dmres.mkdir()
diff --git a/frontend-qt/nspb_qt/win/mp_worker.py b/frontend-qt/nspb_qt/win/mp_worker.py
new file mode 100644
index 00000000..cbc2edac
--- /dev/null
+++ b/frontend-qt/nspb_qt/win/mp_worker.py
@@ -0,0 +1,123 @@
+"""Multi-process worker functions for `patch_nso.py` on Windows.
+
+`patch_nso.py` calls `in_parallel(x18_words, pieces)` and
+`in_parallel(spin_heads_with_region, jobs)` at module top level using
+`multiprocessing.get_context('fork').Pool`.
+
+On Windows there is no `fork()`, and a thread pool is serialized by the Python GIL because
+`x18_words` and `spin_heads_with_region` spend most of their time in Python loops, regexes
+and graph walks. Spawning worker processes against this standalone module avoids re-importing
+`patch_nso.py` at top level while giving each worker its own GIL and Capstone instance, reading
+`IMG` zero-copy from a read-only `mmap` mapping.
+"""
+from __future__ import annotations
+
+import marshal
+import mmap
+import pickle
+import re
+import struct
+import types
+
+from capstone import CS_ARCH_ARM64, CS_MODE_ARM, Cs
+from capstone.arm64 import ARM64_OP_IMM, ARM64_OP_MEM, ARM64_OP_REG
+
+_FILE = None
+_MM: mmap.mmap | bytes = b''
+_MD_LITE: Cs | None = None
+_R18 = re.compile(r'\b[xw]18\b')
+_SPIN_FN = None
+
+
+def _close_mmap() -> None:
+ global _FILE, _MM
+ if _FILE is not None:
+ try:
+ _MM.close() # type: ignore[union-attr]
+ except Exception:
+ pass
+ try:
+ _FILE.close()
+ except Exception:
+ pass
+ _FILE = None
+ _MM = b''
+
+
+def _attach_mmap(img_path: str) -> None:
+ global _FILE, _MM
+ _close_mmap()
+ _FILE = open(img_path, 'rb')
+ _MM = mmap.mmap(_FILE.fileno(), 0, access=mmap.ACCESS_READ)
+
+
+def init_x18(img_path: str) -> None:
+ """Initializer for `x18_words` worker processes."""
+ global _MD_LITE
+ _attach_mmap(img_path)
+ if _MD_LITE is None:
+ _MD_LITE = Cs(CS_ARCH_ARM64, CS_MODE_ARM)
+
+
+def run_x18_job(job: tuple[int, int]) -> set[int]:
+ """Execute `x18_words(lo, hi)` in a separate process with its own GIL."""
+ lo, hi = job
+ md = _MD_LITE or Cs(CS_ARCH_ARM64, CS_MODE_ARM)
+ img = _MM
+ r18 = _R18
+ out: set[int] = set()
+ a = lo
+ while a < hi:
+ end = min(hi, a + 0x4000)
+ last = a - 4
+ for addr, _sz, _mn, op in md.disasm_lite(img[a:end], a):
+ last = addr
+ if r18.search(op):
+ out.add(addr)
+ a = last + 4 if last + 4 >= end else last + 8
+ return out
+
+
+def init_spin(img_path: str, state_path: str) -> None:
+ """Initializer for `spin_heads_with_region` worker processes."""
+ global _SPIN_FN
+ _attach_mmap(img_path)
+ with open(state_path, 'rb') as sf:
+ codes_blob, state = pickle.load(sf)
+
+ img = _MM
+ md_detail = Cs(CS_ARCH_ARM64, CS_MODE_ARM)
+ md_detail.detail = True
+ dec: dict[int, object] = {}
+
+ def spin_ins(a: int, _dec=dec, _md=md_detail, _img=img):
+ if a not in _dec:
+ _dec[a] = next(_md.disasm(_img[a:a + 4], a), None)
+ return _dec[a]
+
+ class SpinNo(Exception):
+ pass
+
+ wg: dict[str, object] = {
+ '__builtins__': __builtins__,
+ 're': re,
+ 'struct': struct,
+ 'ARM64_OP_REG': ARM64_OP_REG,
+ 'ARM64_OP_IMM': ARM64_OP_IMM,
+ 'ARM64_OP_MEM': ARM64_OP_MEM,
+ 'IMG': img,
+ 'md': md_detail,
+ 'spin_dec': dec,
+ 'spin_ins': spin_ins,
+ 'SpinNo': SpinNo,
+ }
+ wg.update(state)
+ codes = marshal.loads(codes_blob)
+ for fname, (code_obj, defaults) in codes.items():
+ wg[fname] = types.FunctionType(code_obj, wg, fname, defaults)
+ _SPIN_FN = wg['spin_heads_with_region']
+
+
+def run_spin_job(job: tuple[int, int]) -> list[int]:
+ """Execute `spin_heads_with_region(first, step)` in a separate process."""
+ return _SPIN_FN(*job) # type: ignore[misc]
diff --git a/frontend-qt/nspb_qt/win/sdk_fetch.py b/frontend-qt/nspb_qt/win/sdk_fetch.py
index e73b3ce5..1d8b2df2 100644
--- a/frontend-qt/nspb_qt/win/sdk_fetch.py
+++ b/frontend-qt/nspb_qt/win/sdk_fetch.py
@@ -36,6 +36,16 @@ class SdkError(Exception):
"""A user-facing failure: the message says what to do next, not just what broke."""
+def _win_path(p: Path | str) -> str:
+ """Return an extended-length path (`\\\\?\\...`) on Windows so paths past MAX_PATH (260) work."""
+ s = os.path.abspath(os.fspath(p))
+ if os.name == 'nt' and not s.startswith('\\\\?\\'):
+ if s.startswith('\\\\'):
+ return '\\\\?\\UNC\\' + s[2:]
+ return '\\\\?\\' + s
+ return s
+
+
def rmtree(path: Path) -> None:
"""Remove a tree, clearing the read-only bits git leaves on Windows.
@@ -45,19 +55,20 @@ def rmtree(path: Path) -> None:
survivors, and only then given up on.
"""
import stat
- if not path.exists():
+ wp = Path(_win_path(path))
+ if not wp.exists() and not path.exists():
return
def clear(func, p, _exc): # onerror: drop the read-only bit and retry
try:
- os.chmod(p, stat.S_IWRITE | stat.S_IREAD)
- func(p)
+ os.chmod(_win_path(p), stat.S_IWRITE | stat.S_IREAD)
+ func(_win_path(p))
except OSError:
pass
- shutil.rmtree(path, onerror=clear)
- if path.exists(): # a file locked by another process (an indexer)
- shutil.rmtree(path, ignore_errors=True)
+ shutil.rmtree(str(wp), onerror=clear)
+ if wp.exists() or path.exists(): # a file locked by another process (an indexer)
+ shutil.rmtree(str(wp), ignore_errors=True)
# ---------------------------------------------------------------- git route
@@ -107,30 +118,43 @@ def materialize_symlinks(repo: Path, sub: str, log: Callable[[str], None]) -> in
for p in list(symlinks):
if p not in pending:
continue
- if not p.is_file() or p.is_symlink():
+ wp = Path(_win_path(p))
+ if not wp.is_file() or wp.is_symlink():
+ pending.discard(p)
+ continue
+ try:
+ target = wp.read_text(encoding='utf-8', errors='replace').strip()
+ except OSError:
pending.discard(p)
continue
- target = p.read_text(encoding='utf-8', errors='replace').strip()
- cand = p.parent / target
+ cand = (p.parent / target)
if cand in pending:
continue # target is itself a symlink stub; resolve it first
- real = cand.resolve()
- if real.is_dir():
- p.unlink()
- shutil.copytree(real, p, dirs_exist_ok=True)
- pending.discard(p)
- round_fixed += 1
- fixed += 1
- elif real.is_file():
- p.write_bytes(real.read_bytes())
+ wreal = Path(_win_path(cand.resolve()))
+ try:
+ if wreal.is_dir():
+ wp.unlink()
+ shutil.copytree(str(wreal), str(wp), dirs_exist_ok=True)
+ pending.discard(p)
+ round_fixed += 1
+ fixed += 1
+ elif wreal.is_file():
+ wp.write_bytes(wreal.read_bytes())
+ pending.discard(p)
+ round_fixed += 1
+ fixed += 1
+ except (OSError, shutil.Error) as e:
+ log(f' {p.relative_to(repo).as_posix()}: copy warning ({e}), skipped')
pending.discard(p)
- round_fixed += 1
- fixed += 1
if not round_fixed:
break
for p in sorted(pending):
- if p.is_file() and not p.is_symlink():
- target = p.read_text(encoding='utf-8', errors='replace').strip()
+ wp = Path(_win_path(p))
+ if wp.is_file() and not wp.is_symlink():
+ try:
+ target = wp.read_text(encoding='utf-8', errors='replace').strip()
+ except OSError:
+ target = ''
log(f' {p.relative_to(repo).as_posix()}: its target {target!r} is missing, left as it is')
if fixed:
log(f' materialized {fixed} symlink stubs into real files (Windows checkout of git symlinks)')
@@ -164,7 +188,10 @@ def clone_sdk(version: str = tc.SDK_PINNED, dest: Path | None = None,
repair_tbd_stubs(target, log=log)
log(f'SDK already present: {target}')
return target
- work = dest / f'.clone-{version}'
+ # Clean up any legacy `.clone-<version>` folder from an earlier interrupted run, then use a
+ # short `.tmp` directory name to keep deep SDK paths well under MAX_PATH.
+ rmtree(dest / f'.clone-{version}')
+ work = dest / '.tmp'
rmtree(work)
def run(args: list[str], what: str, repo: str = tc.SDK_REPO) -> None:
@@ -231,8 +258,8 @@ def clone_sdk(version: str = tc.SDK_PINNED, dest: Path | None = None,
# the index can still tell symlinks from the text files a Windows checkout left behind.
materialize_symlinks(work, version, log=log)
if target.exists():
- shutil.rmtree(target, ignore_errors=True)
- shutil.move(str(src), str(target))
+ rmtree(target)
+ shutil.move(_win_path(src), _win_path(target))
rmtree(work)
repair_tbd_stubs(target, log=log)
log(f'SDK ready: {target} ({tc.sdk_version(target)[0]})')
@@ -574,16 +601,21 @@ def _materialize(out: Path, links: list[tuple[str, str]], log: Callable[[str], N
if src is None:
log(f' link {rel} → {target}: absolute, skipped')
continue
- if dst.exists():
+ wdst = Path(_win_path(dst))
+ wsrc = Path(_win_path(src))
+ if wdst.exists():
continue
- if not src.exists():
+ if not wsrc.exists():
left.append((rel, target))
continue
- if src.is_dir():
- shutil.copytree(src, dst)
- else:
- dst.parent.mkdir(parents=True, exist_ok=True)
- shutil.copyfile(src, dst)
+ try:
+ if wsrc.is_dir():
+ shutil.copytree(str(wsrc), str(wdst), dirs_exist_ok=True)
+ else:
+ wdst.parent.mkdir(parents=True, exist_ok=True)
+ shutil.copyfile(str(wsrc), str(wdst))
+ except (OSError, shutil.Error) as e:
+ log(f' link {rel} → {target}: copy warning ({e}), skipped')
if len(left) == len(todo):
for rel, target in left:
log(f' link {rel} → {target}: target not in the SDK, skipped')
diff --git a/frontend-qt/nspb_qt/win/shim.py b/frontend-qt/nspb_qt/win/shim.py
index 832914f8..d6e45305 100644
--- a/frontend-qt/nspb_qt/win/shim.py
+++ b/frontend-qt/nspb_qt/win/shim.py
@@ -77,8 +77,20 @@ if os.name == 'nt' and not os.environ.get('NSPB_SHIM_OFF'):
os.execve = _win_execve
- import concurrent.futures as _cf, multiprocessing as _mp, threading as _mp_threading, types as _mp_types
+ import concurrent.futures as _cf, marshal as _marshal, multiprocessing as _mp, multiprocessing.spawn as _mp_spawn, pickle as _pickle, tempfile as _tempfile, threading as _mp_threading, types as _mp_types
_mp_tls = _mp_threading.local()
+ _orig_get_context = _mp.get_context
+ _orig_get_prep_data = _mp_spawn.get_preparation_data
+ _in_fork_pool_spawn = False
+
+ def _safe_get_prep_data(name):
+ d = _orig_get_prep_data(name)
+ if _in_fork_pool_spawn:
+ d.pop('init_main_from_path', None)
+ d.pop('init_main_from_name', None)
+ return d
+
+ _mp_spawn.get_preparation_data = _safe_get_prep_data
class _WinForkPool:
def __init__(self, processes=None, initializer=None, initargs=(), *a, **k):
@@ -108,11 +120,49 @@ if os.name == 'nt' and not os.environ.get('NSPB_SHIM_OFF'):
return self._ex.map(fn, iterable)
def starmap(self, fn, jobs, chunksize=1):
+ global _in_fork_pool_spawn
+ jobs = list(jobs)
g = getattr(fn, '__globals__', None)
fname = getattr(fn, '__name__', '')
if fname == 'x18_words' and isinstance(g, dict) and 'Cs' in g:
+ IMG = g['IMG']
+ if len(jobs) >= 2 and len(IMG) >= 0x20000:
+ img_path = None
+ try:
+ import mp_worker as _mpw
+ with _tempfile.NamedTemporaryFile(prefix='nspb_x18_', delete=False) as tf:
+ tf.write(IMG)
+ img_path = tf.name
+ n_procs = min(len(jobs), max(self._workers, os.cpu_count() or 4))
+ _in_fork_pool_spawn = True
+ try:
+ pool = _orig_get_context('spawn').Pool(
+ processes=n_procs,
+ initializer=_mpw.init_x18,
+ initargs=(img_path,),
+ )
+ finally:
+ _in_fork_pool_spawn = False
+ try:
+ res = pool.map(_mpw.run_x18_job, jobs)
+ pool.close()
+ pool.join()
+ return res
+ finally:
+ try:
+ pool.terminate()
+ except Exception:
+ pass
+ except Exception:
+ pass
+ finally:
+ if img_path:
+ try:
+ os.unlink(img_path)
+ except OSError:
+ pass
Cs, ARCH, MODE = g['Cs'], g['CS_ARCH_ARM64'], g['CS_MODE_ARM']
- IMG, R18 = g['IMG'], g['R18']
+ R18 = g['R18']
def _x18_job(job):
lo, hi = job
@@ -131,8 +181,60 @@ if os.name == 'nt' and not os.environ.get('NSPB_SHIM_OFF'):
return list(self._ex.map(_x18_job, jobs))
if fname == 'spin_heads_with_region' and isinstance(g, dict) and 'Cs' in g:
- Cs, ARCH, MODE = g['Cs'], g['CS_ARCH_ARM64'], g['CS_MODE_ARM']
IMG = g['IMG']
+ if len(jobs) >= 2 and len(g.get('spin_heads', ())) >= 8:
+ img_path = st_path = None
+ try:
+ import mp_worker as _mpw
+ fnames = (
+ 'spin_norm', 'spin_rw', 'spin_kind', 'spin_rmw', 'spin_lock',
+ 'spin_store', 'spin_quick', 'spin_region1', 'spin_region',
+ 'spin_heads_with_region',
+ )
+ codes_blob = _marshal.dumps(
+ {k: (g[k].__code__, g[k].__defaults__) for k in fnames if k in g and isinstance(g[k], _mp_types.FunctionType)}
+ )
+ state_keys = (
+ 'SPIN_PATHS', 'SPIN_STEPS', 'SPIN_STORES', 'SPIN_LSE', 'SPIN_BAD',
+ 'SPIN_STOP', 'DATA_BLOCKS', 'spin_touched', 'spin_cand', 'spin_heads',
+ )
+ state = {k: g[k] for k in state_keys if k in g}
+ with _tempfile.NamedTemporaryFile(prefix='nspb_spin_img_', delete=False) as tf_i, \
+ _tempfile.NamedTemporaryFile(prefix='nspb_spin_st_', delete=False) as tf_s:
+ tf_i.write(IMG)
+ img_path = tf_i.name
+ _pickle.dump((codes_blob, state), tf_s, protocol=_pickle.HIGHEST_PROTOCOL)
+ st_path = tf_s.name
+ n_procs = min(len(jobs), max(self._workers, os.cpu_count() or 4))
+ _in_fork_pool_spawn = True
+ try:
+ pool = _orig_get_context('spawn').Pool(
+ processes=n_procs,
+ initializer=_mpw.init_spin,
+ initargs=(img_path, st_path),
+ )
+ finally:
+ _in_fork_pool_spawn = False
+ try:
+ res = pool.map(_mpw.run_spin_job, jobs)
+ pool.close()
+ pool.join()
+ return res
+ finally:
+ try:
+ pool.terminate()
+ except Exception:
+ pass
+ except Exception:
+ pass
+ finally:
+ for _p in (img_path, st_path):
+ if _p:
+ try:
+ os.unlink(_p)
+ except OSError:
+ pass
+ Cs, ARCH, MODE = g['Cs'], g['CS_ARCH_ARM64'], g['CS_MODE_ARM']
def _spin_job(job):
t_fn = getattr(_mp_tls, 'spin_fn', None)
@@ -165,8 +267,6 @@ if os.name == 'nt' and not os.environ.get('NSPB_SHIM_OFF'):
class _WinForkContext:
Pool = staticmethod(_WinForkPool)
- _orig_get_context = _mp.get_context
-
def _win_get_context(method=None):
if method == 'fork':
return _WinForkContext()
diff --git a/frontend-qt/tests/test_swift_parity.py b/frontend-qt/tests/test_swift_parity.py
index ee9a9009..68be0bfc 100644
--- a/frontend-qt/tests/test_swift_parity.py
+++ b/frontend-qt/tests/test_swift_parity.py
@@ -331,7 +331,8 @@ class ParityWindowTests(unittest.TestCase):
r0, r1 = rects[0][2], rects[1][2]
self.assertGreaterEqual(r1.left() - r0.right(), 5.0)
- # 6. Step 1 & Step 3 reveal buttons + Copy game data button enabled when archive exists
+ # 6. Step 1 & Step 3 reveal buttons + Copy game data button enabled when archive exists,
+ # while redundant buttons on the Start Build row stay hidden
fake_ipa = td_path / '0100E65002BB8000.ipa'
fake_ipa.write_bytes(b'PK\x03\x04')
fake_romfs = td_path / 'romfs'
@@ -345,8 +346,31 @@ class ParityWindowTests(unittest.TestCase):
self.assertFalse(w.step1_reveal_btn.isHidden())
self.assertFalse(w.step3_open_romfs_btn.isHidden())
self.assertTrue(w.drive_copy_btn.isEnabled())
- self.assertIn('在文件夹中显示', w.reveal_ipa_btn.text())
- self.assertIn('在文件夹中显示', w.reveal_arc_btn.text())
+ self.assertTrue(w.reveal_ipa_btn.isHidden())
+ self.assertTrue(w.reveal_arc_btn.isHidden())
+
+ # 6b. pu.reveal exists and opens file/folder without raising AttributeError
+ from nspb_qt import platform_util as pu
+ from unittest.mock import patch
+ self.assertTrue(hasattr(pu, 'reveal') and callable(pu.reveal))
+ with patch('subprocess.Popen') as mock_popen:
+ self.assertTrue(pu.reveal(str(fake_ipa)))
+ self.assertTrue(pu.reveal(str(fake_romfs)))
+ self.assertFalse(pu.reveal(''))
+
+ # 6c. Thin 6px activity progress bar hides percentage text so digits are not clipped
+ self.assertFalse(w.act_progress.isTextVisible())
+
+ # 6d. sdk_fetch._win_path and mp_worker multi-process helpers work
+ from nspb_qt.win import sdk_fetch, mp_worker
+ self.assertTrue(callable(sdk_fetch._win_path))
+ img_tmp = td_path / 'test_x18.bin'
+ img_tmp.write_bytes(b'\xe0\x03\x12\xaa' * 16)
+ mp_worker.init_x18(str(img_tmp))
+ try:
+ self.assertEqual(len(mp_worker.run_x18_job((0, 64))), 16)
+ finally:
+ mp_worker._close_mmap()
# 8. Stage completion marks running stage 'done' (green) instead of staying 'running' (orange)
m.stages.clear()