"""The six setup steps of docs/installer.md: check, extract, lift, compile, prepare, cleanup. Extraction runs in this process through tools/extract.py (the package's sibling tools/, which ships with it); lift, compile and prepare run the bundled Python on recomp/gen.py, tools/build_game.py, tools/make_romfs_patches.py and tools/prepare_shaders.py, whose output goes to /logs/-.log and is read for progress. No Win32 call here: the host check is a small C program built with the bundled clang (hostcheck.c). Install folder: exefs/, romfs/ (romfs.bin, index.json), profile/, totk.exe and its DLLs, romfs-index.bin, romfs-patches/, totk_xlate.exe, the shader state of totk.exe --prepare-shaders, totk.ini, totk-install.json. Temp folder (/-): check/ (ExeFS of both inputs, the host check), gen/ (C), build/ (objects), logs/ (kept by the cleanup). """ import hashlib import json import os import re import shutil import subprocess import sys import threading import time from pathlib import Path from . import ini from .layout import EXE from .runner import InstallError, Step sys.path.insert(0, str(Path(__file__).resolve().parent.parent / 'tools')) import extract # noqa: E402 from extract_exefs import (common_tickets, container_entries, load_keys, nca_ctr_key, nca_inventory, # noqa: E402 program_nca) GiB = 1 << 30 DEST_EXTRA = 4 * GiB # beside the RomFS: shader and pipeline caches (about 2 GB), exefs, totk.exe, seed files TEMP_NEED = 3 * GiB # generated C (about 0.9 GB) and objects UPDATE_VERSION = '1.2.1' HOSTCHECK = Path(__file__).resolve().parent / 'hostcheck.c' STAMP = '.install-key' # ---- helpers def file_sha256(path, progress=None): """SHA-256 hex of a file; progress(done, total) after each block.""" h = hashlib.sha256() total = path.stat().st_size done = 0 with open(path, 'rb', buffering=0) as f: buf = bytearray(8 << 20) view = memoryview(buf) while n := f.readinto(buf): h.update(view[:n]) done += n if progress: progress(done, total) return h.hexdigest() def fingerprint(*parts): """SHA-256 over strings and the contents of files and directory trees (a missing path counts as missing).""" h = hashlib.sha256() for p in parts: if isinstance(p, Path): if p.is_dir(): for f in sorted(p.rglob('*')): if f.is_file() and '__pycache__' not in f.parts: h.update(f.relative_to(p).as_posix().encode() + b'\0' + bytes.fromhex(file_sha256(f))) elif p.is_file(): h.update(p.name.encode() + b'\0' + bytes.fromhex(file_sha256(p))) else: h.update(b'missing\0' + p.name.encode()) else: h.update(str(p).encode()) h.update(b'\1') return h.hexdigest() def remove(path): if path.is_dir() and not path.is_symlink(): shutil.rmtree(path) elif path.exists() or path.is_symlink(): path.unlink() def replace_file(src, dst): """Copy src over dst through a temporary name, so a cancelled copy never leaves half a file under dst's name.""" tmp = dst.with_name(dst.name + '.tmp') shutil.copy2(src, tmp) os.replace(tmp, dst) def kill_tree(proc): if proc.poll() is not None: return if os.name == 'nt': subprocess.run(['taskkill', '/T', '/F', '/PID', str(proc.pid)], capture_output=True) else: proc.kill() def run_tool(ctx, step, script, args, parse=None, cwd=None): """Run a tool script with the bundled Python. Its output goes to /logs/-