Merge the fixes line: the stutter, memory, flicker and crash fixes, co-op, the mesh shader fallback and the launcher fixes.

Brought onto the line with FSR 4, its frame generation and the debug menu. Where both sides had added to the same
place, both stay: the patch table keeps the pickup count and the free camera, the settings list keeps both sides'
names, the menu includes the co-op page and the debug menu, and the launcher builder takes the pipeline list and the
FSR 4 folder. Both sides had added the menu's typed-character event; one definition stays, with the window code that
passes characters outside the basic plane.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-11 08:18:01 -05:00
213 changed files with 39872 additions and 1598 deletions

View File

@@ -14,6 +14,11 @@ RUN = ('import os, runpy, sys; p = sys.argv[1]; s = sys.argv[2]; sys.argv = sys.
'runpy.run_path(s, run_name="__main__")')
CREATE_NO_WINDOW = 0x08000000
CREATE_NEW_PROCESS_GROUP = 0x00000200
# The build runs below normal priority: a compile on every processor for ten minutes and more otherwise takes the
# PC from whatever the player does meanwhile, which the job screen invites ("Keep using your PC if you like"). On an
# idle PC it costs nothing. Windows hands a below-normal process's class on to the processes it starts, so the
# compilers, the lifter's workers and the shader preparation that make_game.py starts run that way too.
BELOW_NORMAL_PRIORITY_CLASS = 0x00004000
def command(layout, args):
@@ -61,33 +66,47 @@ class Run:
self.error = None
kw = {}
if os.name == 'nt':
kw['creationflags'] = CREATE_NO_WINDOW | CREATE_NEW_PROCESS_GROUP
kw['creationflags'] = CREATE_NO_WINDOW | CREATE_NEW_PROCESS_GROUP | BELOW_NORMAL_PRIORITY_CLASS
else:
kw['start_new_session'] = True
env = dict(os.environ, PYTHONUNBUFFERED='1', PYTHONIOENCODING='utf-8')
self.proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL,
cwd=cwd, env=env, text=True, encoding='utf-8', errors='replace', bufsize=1, **kw)
self.cancelled = False
self.dropped = 0 # lines and events whose handler raised (_read)
self._reader = threading.Thread(target=self._read, args=(on_event, on_line), daemon=True)
self._reader.start()
def _read(self, on_event, on_line):
"""Reads the build's output to its end, whatever the handlers do. A handler that raises (the launcher's log
on a full disk, a drawing error) loses its line, counted in dropped, and the reading goes on: when this
thread ended on the exception nobody read the pipe any more, the build stopped at its next line once the
pipe was full, and the job waited for it for good."""
def call(handler, arg):
try:
handler(arg)
except Exception: # noqa: BLE001 - any failure of a handler must leave the pipe read
self.dropped += 1
for line in self.proc.stdout:
line = line.rstrip('\r\n')
if line.startswith('@@'):
try:
ev = json.loads(line[2:])
except ValueError:
on_line(line)
call(on_line, line)
continue
if ev.get('ev') == 'error':
self.error = ev.get('message')
on_event(ev)
if isinstance(ev, dict):
if ev.get('ev') == 'error':
self.error = ev.get('message')
call(on_event, ev)
else: # "@@" before something that is no event
call(on_line, line)
elif line.startswith('error: ') and not self.error:
self.error = line[7:]
on_line(line)
call(on_line, line)
else:
on_line(line)
call(on_line, line)
def wait(self):
code = self.proc.wait()
@@ -148,6 +167,38 @@ def built_version(game_dir):
return ''
def scan_report(game_dir):
"""What the check of the game files found (tools/romfs_check.py writes <game>/romfs-check.json): (the paths of
the damaged files, how many of them got a stand-in), or None when the folder has no readable report."""
try:
with open(os.path.join(str(game_dir), 'romfs-check.json'), encoding='utf-8') as f:
report = json.load(f)
return sorted(report['damaged']), len(report.get('repaired') or ())
except (OSError, ValueError, KeyError, TypeError):
return None
def can_rebuild(game_dir):
st = build_state(game_dir)
return st.get('version') == 2 and all(s in st.get('steps', {}) for s in ('exefs', 'romfs'))
def unpacked(game_dir):
"""True when the game files a rebuild reads are in the game folder (make_game.py BuiltSource names the same
ones): the unpacked ExeFS, the RomFS image and its index."""
gd = str(game_dir)
return all(os.path.isfile(os.path.join(gd, *parts)) for parts in
(('exefs', 'main'), ('romfs', 'romfs.bin'), ('romfs', 'index.json')))
def installed(game_dir):
"""True when the game folder holds a game the player can start: a finished build, or the exe of the last
finished build beside a build that stopped later. make_game.py drops a step's record when a step before it
runs again, so an update that lifted again and then stopped in the compile (an error, Cancel, a power cut)
leaves state.json without `compile` and built_version says '': the old exe is still there and playable, and
a rebuild continues from the unpacked game files without the dump (can_rebuild)."""
if not game_dir:
return False
if built_version(game_dir):
return True
return os.path.isfile(os.path.join(str(game_dir), 'ocarina.exe')) and can_rebuild(game_dir)

View File

@@ -98,7 +98,7 @@ def main(argv=None):
if a.cmd == 'continue-update':
gd = s.get('game_dir')
return follow(jobs.ContinueUpdateJob(ctx, gd if gd and build.built_version(gd) else '', a.old,
return follow(jobs.ContinueUpdateJob(ctx, gd if build.installed(gd) else '', a.old,
s.get('dump')))
if a.cmd == 'tools':

View File

@@ -34,12 +34,18 @@ class Context:
'it was is kept as launcher.json.bad')
def log(self, line):
"""One line (or several) into launcher.log. A log that cannot be written (a full disk, a file closed under
it) loses the line and nothing else: the callers log from the build's reader thread and from the window's
timers, where an exception would stop the build's output from being read or the timer from running."""
stamp = time.strftime('%H:%M:%S')
with self._lock:
if self._file:
for part in str(line).splitlines() or ['']:
self._file.write(f'{stamp} {part}\n')
self._file.flush()
try:
for part in str(line).splitlines() or ['']:
self._file.write(f'{stamp} {part}\n')
self._file.flush()
except (OSError, ValueError): # ValueError: the file was closed
pass
if self.echo:
print(line, flush=True)

View File

@@ -14,6 +14,7 @@ The feed (app/<version>/feed.json, or the player's override in settings) is eith
"""
import json
import re
import time
import urllib.parse
from pathlib import Path
@@ -78,6 +79,30 @@ def _check_manifest(m, where):
return m
def retry_text(headers, now=None):
"""When GitHub takes requests again, for the message of a refused one: "after 14:32 (in 25 minutes)". From
X-RateLimit-Reset, the time the hourly allowance is whole again, when the response says the allowance is used
up (X-RateLimit-Remaining: 0), else from Retry-After, the seconds to wait. '' when the response says neither
or says something that cannot be right (more than a day)."""
now = time.time() if now is None else now
try:
if headers.get('X-RateLimit-Remaining') == '0' and headers.get('X-RateLimit-Reset'):
wait = int(headers.get('X-RateLimit-Reset')) - now
elif headers.get('Retry-After'):
wait = int(headers.get('Retry-After'))
else:
return ''
except (AttributeError, TypeError, ValueError):
return ''
if wait > 86400:
return ''
if wait <= 0: # the allowance is whole again by this PC's clock
return 'in a minute'
minutes = int(-(-wait // 60))
return (f'after {time.strftime("%H:%M", time.localtime(now + wait))} '
f'(in {minutes} minute{"" if minutes == 1 else "s"})')
def _github(feed, channel, token, version):
api = feed.get('api', GITHUB_API).rstrip('/')
headers = {'Accept': 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28'}
@@ -89,8 +114,9 @@ def _github(feed, channel, token, version):
code = getattr(e, 'code', None)
if code == 404:
raise FeedError(f'no releases found for {feed["repo"]} (a private repository needs a token)') from e
if code == 403:
raise FeedError('GitHub refused the request (rate limit); try again in an hour') from e
if code in (403, 429): # GitHub answers either when the allowance of requests is used up
when = retry_text(getattr(e, 'headers', None))
raise FeedError(f'GitHub refused the request (rate limit); try again {when or "in an hour"}') from e
raise FeedError(f'GitHub could not be reached: {e}') from e
# The API lists the newest release first: the first one the channel takes that has a manifest is the answer.
for r in releases:

View File

@@ -326,7 +326,9 @@ def extract_fonts(layout, game_dir='', dump=''):
fam = font_family(otf) if otf else None
if not otf or not fam:
continue
(d / f'{role}.otf').write_bytes(otf)
p = d / f'{role}.otf'
if not (p.is_file() and p.read_bytes() == otf): # a file this process registered as a font stays as it is
p.write_bytes(otf)
(d / f'{role}.family').write_text(fam, encoding='utf-8')
out[role] = (d / f'{role}.otf', fam)
return out

View File

@@ -13,6 +13,16 @@ RECHECK = 3600 # seconds between update checks while the launcher stays open,
PENDING, RUNNING, DONE, SKIPPED, FAILED = 'pending', 'running', 'done', 'skipped', 'failed'
# Failures whose message says all there is to say: the log gets one line for them (failure_line), not a traceback.
# A refused update check wrote 22 lines of two tracebacks into launcher.log each time.
PLAIN_FAILURES = (feed.FeedError, build.BuildError)
def failure_line(e):
"""A failure in one line for the log: its kind, its message and what it came from."""
cause = e.__cause__
return f'{type(e).__name__}: {e}' + (f' ({type(cause).__name__}: {cause})' if cause else '')
STEPS = {
'tools': ('Gather Build Tools', 'Downloading the compiler and libraries the build uses. This happens once and '
'takes about 170 MB.'),
@@ -40,6 +50,36 @@ WEIGHTS = {'tools': 12, 'tools_update': 4, 'check': 1, 'exefs': 2, 'romfs': 14,
'compile_changed': 55, 'shaders': 18, 'prompts': 4, 'scan': 5, 'ready': 0, 'download': 8, 'verify': 1, 'swap': 6}
# The game files tools/romfs_check.py reports as damaged on every copy of the game looked at so far: they are that
# way in the game's own data, not through a player's dump, so a new dump does not change them. The three textures
# get a stand-in; the game does without the fourth file.
SCAN_EXPECTED = frozenset((
'Tex/CmnTex_Map_Soil_FairyFountain_Floor_D_Nrm.bntx.zs',
'Tex/RBake_Scene_GanonStage_G_2_1_d0_t0.bntx.zs',
'Tex/Rock_WaterTemple_Gate_M_01_Nrm.bntx.zs',
'gsys/EnvObjSetMgrParam/Artwork.gsys__EnvObjSetMgrParam.bgyml',
))
def scan_summary(damaged, stood_in):
"""(the scan row's value, a sentence for the home screen) for what the check of the game files found
(build.scan_report); ('', '') when nothing is damaged. The files of SCAN_EXPECTED are told as what they are, the
same ones as on every copy: the player learns what the check did and that nothing needs doing. Other files are
counted and pointed to, without advice the check cannot back."""
n = len(damaged)
if not n:
return '', ''
files = '1 game file' if n == 1 else f'{n} game files'
stand = ('none got a stand-in' if not stood_in else '1 texture got a stand-in' if stood_in == 1 else
f'{stood_in} textures got a stand-in')
value = f'{n} Flagged, {stood_in} Stand-In' + ('' if stood_in == 1 else 's')
if set(damaged) <= SCAN_EXPECTED:
return value, (f'The file check flagged {files}, the same as in every copy of the game: {stand}, and nothing '
'needs doing.')
return value, (f'The file check flagged {files} as damaged: {stand}, so what uses them still loads. '
'romfs-check.json in the game folder lists them.')
def prompt_rows(ctx):
"""The prompts row, for a version with button art (make_game.py draws the prompts after the shaders), or none."""
return ['prompts'] if build.has_prompt_art(ctx.layout) else []
@@ -75,6 +115,7 @@ class Job:
self.started = time.time()
self.finished = None
self.after = None # what the window does next ('restart', ...)
self.note = '' # a sentence for the home screen after the job (note_scan)
self._lock = threading.Lock()
# ---- state the window reads
@@ -199,7 +240,7 @@ class Job:
self.result = 'cancelled'
else:
self.result, self.error = 'failed', str(e) or type(e).__name__
self.ctx.log(traceback.format_exc())
self.ctx.log(failure_line(e) if isinstance(e, PLAIN_FAILURES) else traceback.format_exc())
finally:
for s in self.steps:
if s.state == RUNNING:
@@ -212,6 +253,26 @@ class Job:
raise NotImplementedError
# ---- shared pieces
def note_scan(self, game_dir):
"""After a build: what the check of the game files found goes into its row, and when the check ran in this
job, into the note the home screen shows and into the log. The check itself only printed its counts into
the build's lines, which scroll away, so a player never learned that files were flagged or that stand-ins
are in use. A check kept from an earlier build shows its count in the row only."""
s = self.step('scan')
found = build.scan_report(game_dir)
if not s or s.state not in (DONE, SKIPPED) or not found:
return
value, note = scan_summary(*found)
if not value:
return
if s.state == DONE:
s.value = value
self.note = note
self.ctx.log(f'file check: {note}')
elif s.value == 'Unchanged':
s.value = f'Unchanged · {value}'
self._touch()
def ensure_tools(self, sid):
layout = self.ctx.layout
self.begin(sid)
@@ -301,13 +362,23 @@ class InstallJob(Job):
'scan', 'ready'])
self.dump, self.game_dir = str(dump), str(game_dir)
def args(self):
"""make_game.py's arguments. A game folder whose game files an earlier run already unpacked and checked
(build.can_rebuild, build.unpacked) is built on from those, for this processor's level as a first build is:
the dump is not read again, so an install that stopped after the RomFS image goes on although the dump
was moved or deleted since, and the 26 GB image is not packed a second time."""
if build.can_rebuild(self.game_dir) and build.unpacked(self.game_dir):
return build.rebuild_args(self.ctx.layout, self.game_dir, retune=True)
return build.install_args(self.ctx.layout, self.dump, self.game_dir)
def run(self):
self.ensure_tools('tools')
self.make_game(build.install_args(self.ctx.layout, self.dump, self.game_dir),
self.make_game(self.args(),
{s: s for s in ('check', 'exefs', 'romfs', 'lift', 'compile', 'shaders', 'prompts', 'scan')})
for sid in ('shaders', 'prompts', 'scan'):
if self.step(sid) and self.step(sid).state == PENDING:
self.skip(sid)
self.note_scan(self.game_dir)
self.finish('ready', 'Ready')
try:
gamefiles.extract_fonts(self.ctx.layout, self.game_dir)
@@ -340,6 +411,7 @@ class RebuildJob(Job):
for sid in ('lift', 'compile_changed', 'swap', 'shaders', 'prompts', 'scan'):
if self.step(sid) and self.step(sid).state == PENDING:
self.skip(sid)
self.note_scan(self.game_dir)
class UpdateDownloadJob(Job):

View File

@@ -390,15 +390,80 @@ def _fresh_file(folder, stem, suffix):
return p
NO_SAVE = 'the game has no save yet: play until it saves, then export'
# Settings whose values stay on the player's PC (runtime/cfg_private.h has the same list): a co-op room's secret and
# the offer number made for it, the relay's address, the player's name, the key a maker removes, the menu's test hook.
PRIVATE_SETTINGS = ('coop_room', 'coop_server', 'coop_name', 'coop_accept', 'coop_remove', 'menu_test_set')
PRIVATE_SHOWN = b'(set, not shown)'
def scrub_settings(data):
"""The bytes of an nx2navi.ini for a bug report: every private setting that has a value reads
"key = (set, not shown)"; everything else, comments and line ends included, byte for byte (cfg_ini_scrub in
runtime/cfg_private.h does the same for the game's own export)."""
out = []
for n, line in enumerate(data.split(b'\n')):
text = line[:-1] if line.endswith(b'\r') else line
start = 3 if n == 0 and text.startswith(b'\xef\xbb\xbf') else 0
body = text[start:].lstrip(b' \t')
key, eq, value = body.partition(b'=')
if (eq and value.strip(b' \t') and body[:1] not in (b'#', b';', b'[')
and key.strip(b' \t').lower() in tuple(k.encode() for k in PRIVATE_SETTINGS)):
line = text[:text.index(b'=') + 1] + b' ' + PRIVATE_SHOWN + line[len(text):]
out.append(line)
return b'\n'.join(out)
def _add_report_file(z, f):
"""One file of a bug report into the zip: nx2navi.ini without its private values, anything else as it is."""
if f.name == 'nx2navi.ini':
z.writestr(REPORT_DIR + f.name, scrub_settings(f.read_bytes()))
else:
z.write(f, REPORT_DIR + f.name)
def _export_log(game, dest, version, log):
"""The bug report of a game without a save (it stopped before its first one): NX2navi-log-<date>_<time>-bug-report.zip
with the game's latest log, nx2navi.ini and a manifest that counts no save files; import_zip refuses it as a zip
without a save. SaveError when the game has written no log either: then there is nothing to report with."""
extras = [f for f in (game / 'logs' / 'nx2navi.log', game / 'nx2navi.ini') if f.is_file()]
if not extras or extras[0].name != 'nx2navi.log':
raise SaveError(NO_SAVE)
dest.mkdir(parents=True, exist_ok=True)
out = _fresh_file(dest, time.strftime('NX2navi-log-%Y-%m-%d_%H-%M-%S') + '-bug-report', '.zip')
tmp = out.with_name(out.name + '.part')
manifest = {'format': FORMAT, 'format_version': FORMAT_VERSION, 'game': GAME, 'title_id': _title_hex(),
'game_version': GAME_VERSION, 'nx2navi': version,
'exported': time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()), 'exported_by': 'launcher',
'files': 0, 'bug_report': True}
try:
with zipfile.ZipFile(tmp, 'w', zipfile.ZIP_DEFLATED) as z:
z.writestr(MANIFEST, json.dumps(manifest, indent=1) + '\n')
for f in extras:
_add_report_file(z, f)
os.replace(tmp, out)
except BaseException:
tmp.unlink(missing_ok=True)
raise
log(f'saves: the game has no save yet; exported {", ".join(f.name for f in extras)} to {out}')
return {'path': out, 'files': 0, 'extras': [f.name for f in extras]}
def export_save(game, dest=None, bug_report=False, version='', log=print):
"""Write the save the game would load now into one zip in dest (default_export_dir when None), for a bug report
or another player. bug_report adds the game's latest log and nx2navi.ini. Never a key file. Returns {'path',
'files', 'extras'} (extras: the names of the bug report files that were there)."""
or another player. bug_report adds the game's latest log and nx2navi.ini, and still writes those two when the game
has no save yet (_export_log): a game that stops before its first save is the one its player wants to report.
Never a key file. Returns {'path', 'files', 'extras'} (files 0: no save; extras: the names of the bug report files
that were there)."""
game = Path(game)
if not (game / 'profile' / 'account.uid').is_file():
raise SaveError('the game has no save yet: play until it saves, then export')
account = account_dir(game)
dest = Path(dest) if dest else default_export_dir()
if not (game / 'profile' / 'account.uid').is_file(): # account_dir would make the account: not for an export
if bug_report:
return _export_log(game, dest, version, log)
raise SaveError(NO_SAVE)
account = account_dir(game)
dest.mkdir(parents=True, exist_ok=True)
stem = time.strftime('NX2navi-save-%Y-%m-%d_%H-%M-%S') + ('-bug-report' if bug_report else '')
extras = [f for f in (game / 'logs' / 'nx2navi.log', game / 'nx2navi.ini') if bug_report and f.is_file()]
@@ -406,7 +471,9 @@ def export_save(game, dest=None, bug_report=False, version='', log=print):
# a running game may swap a commit in while its files are read (runtime/fs_host.c swap_in): read again
current = _newest(game, account)
if current is None or not _has_file(current):
raise SaveError('the game has no save yet: play until it saves, then export')
if bug_report:
return _export_log(game, dest, version, log)
raise SaveError(NO_SAVE)
files = [rel for rel in _files(current, set()) if not _key_file(rel)]
out = _fresh_file(dest, stem, '.zip')
tmp = out.with_name(out.name + '.part')
@@ -420,7 +487,7 @@ def export_save(game, dest=None, bug_report=False, version='', log=print):
for rel in files:
z.write(current / rel, SAVE_DIR + rel.as_posix())
for f in extras:
z.write(f, REPORT_DIR + f.name)
_add_report_file(z, f)
os.replace(tmp, out)
except FileNotFoundError:
tmp.unlink(missing_ok=True)

View File

@@ -1,27 +1,79 @@
"""What NX2navi.exe runs: python\\pythonw.exe app\\<version>\\launcher\\start.pyw [arguments].
The embeddable Python puts only its ._pth entries on sys.path, so the release folder (the parent of launcher/) goes
first here. A failure before the window is up is written to data/logs/crash.log, shown in a message box and ends the
process with exit code 86, which tells the stub to go back to the previous version (launcher/native/nx2navi.c).
first here. A failure before the window is up is written to data/logs/crash.log and ends the process with exit code
86, which tells the stub to go back to the previous version (launcher/native/nx2navi.c wWinMain). The stub waits
six seconds for that code (QUICK_MS), so the failure is shown in a message box only when the stub has no version to
go back to or no longer waits (quiet_exit): a box holds the exit code back until the player closes it, and a new
version that could not start then stayed the current one.
"""
import os
import sys
import traceback
APP = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, APP)
STARTUP_FAILED = 86
# The stub's wait for this process's exit code is QUICK_MS, 6 s (launcher/native/nx2navi.c start_release). An exit
# this long after the process was made still reaches it, with a second to spare for the exit itself.
STUB_WAITS = 5.0
def crash(text):
root = os.environ.get('NX2NAVI_LAUNCHER_ROOT') or os.path.dirname(os.path.dirname(APP))
def launcher_root():
return os.environ.get('NX2NAVI_LAUNCHER_ROOT') or os.path.dirname(os.path.dirname(APP))
def rollback_target(root):
"""The version the stub goes back to when this one ends with STARTUP_FAILED (launcher/native/nx2navi.c
wWinMain): the one app/previous.txt names, when it is another than this one and its folder holds a launcher.
'' when there is none."""
try:
logs = os.path.join(root, 'data', 'logs')
with open(os.path.join(root, 'app', 'previous.txt'), encoding='utf-8-sig') as f:
prev = (f.read().splitlines() or [''])[0].strip()
except (OSError, UnicodeDecodeError):
return ''
if not prev or prev == os.path.basename(APP):
return ''
return prev if os.path.isfile(os.path.join(root, 'app', prev, 'launcher', 'start.pyw')) else ''
def process_age():
"""Seconds since Windows made this process, which is when the stub began to wait for it. Infinite where that
cannot be read, so that the failure is then shown rather than left to a stub that may have stopped waiting."""
try:
import ctypes
from ctypes import wintypes
k = ctypes.windll.kernel32
k.GetCurrentProcess.restype = wintypes.HANDLE
k.GetProcessTimes.argtypes = [wintypes.HANDLE] + [ctypes.POINTER(wintypes.FILETIME)] * 4
made, ended, kernel, user, now = (wintypes.FILETIME() for _ in range(5))
if not k.GetProcessTimes(k.GetCurrentProcess(), *(ctypes.byref(t) for t in (made, ended, kernel, user))):
return float('inf')
k.GetSystemTimeAsFileTime(ctypes.byref(now))
ticks = [t.dwHighDateTime << 32 | t.dwLowDateTime for t in (made, now)] # 100 ns each
return max(0.0, (ticks[1] - ticks[0]) / 1e7)
except Exception:
return float('inf')
def quiet_exit(root, argv, age):
"""True when a failed start ends without a message box: the stub goes back to the previous version on the
exit code (rollback_target), and that version's home screen says which version did not start. Not when Steam
started this version without the stub (--play, launcher/steam.py PLAY_TEXT), and not once the stub has stopped
waiting (age, process_age): nobody acts on the exit code then, and without the box nothing would be seen."""
return bool(rollback_target(root)) and '--play' not in argv and age < STUB_WAITS
def crash(text, box=True):
"""The failure into data/logs/crash.log and, with box, into a message box that waits for the player."""
try:
logs = os.path.join(launcher_root(), 'data', 'logs')
os.makedirs(logs, exist_ok=True)
with open(os.path.join(logs, 'crash.log'), 'a', encoding='utf-8') as f:
f.write(text + '\n')
except OSError:
pass
if not box:
return
try:
import ctypes
ctypes.windll.user32.MessageBoxW(None, 'NX2navi could not start.\n\n' + text[-1500:], 'NX2navi', 0x10)
@@ -29,17 +81,22 @@ def crash(text):
print(text, file=sys.stderr)
try:
from launcher.ui.app import main
except Exception:
crash(traceback.format_exc())
sys.exit(STARTUP_FAILED)
def run():
sys.path.insert(0, APP)
try:
from launcher.ui.app import main
except Exception:
crash(traceback.format_exc(), box=not quiet_exit(launcher_root(), sys.argv[1:], process_age()))
sys.exit(STARTUP_FAILED)
try:
code = main()
except SystemExit:
raise
except Exception:
crash(traceback.format_exc())
sys.exit(1)
sys.exit(code or 0)
try:
code = main()
except SystemExit:
raise
except Exception:
crash(traceback.format_exc())
sys.exit(1)
sys.exit(code or 0)
if __name__ == '__main__':
run()

View File

@@ -139,6 +139,31 @@ def exit_code_text(code):
return f'{shown} ({meaning})' if meaning else shown
FATAL_TAIL = 256 << 10 # bytes of the game's log last_fatal reads from its end
FATAL_SHOWN = 240 # characters of a FATAL line the home screen shows; the launcher's log has all of it
def last_fatal(game_dir):
"""The last FATAL line of the game's latest run, without its "FATAL: ", or ''. The game writes one before it
exits with 70 (runtime/dispatch.c rt_fatal) or 71 (runtime/main.c crash_handler) into logs/nx2navi.log, which
each run starts anew (runtime/main.c log_open). A line this launcher added for an earlier run's end
(note_exit_in_log) ends the search: what stands before it is not this run's."""
if not game_dir:
return ''
try:
with open(Path(game_dir) / 'logs' / 'nx2navi.log', 'rb') as f:
f.seek(max(0, f.seek(0, 2) - FATAL_TAIL))
lines = f.read().decode('utf-8', 'replace').splitlines()
except OSError:
return ''
for line in reversed(lines):
if line.startswith('[launcher] the game ended'):
return ''
if line.startswith('FATAL: '):
return line[len('FATAL: '):].strip()
return ''
def note_exit_in_log(game_dir, text):
"""Adds the exit code to the end of the game's log, so a log a player sends shows how the run ended even when the
process died before it could write a line itself."""
@@ -152,6 +177,39 @@ def note_exit_in_log(game_dir, text):
pass
def partial_image_gb(game_dir):
"""The size of what an install that was cut short left of the RomFS image (romfs/romfs.bin.part, up to 26 GB).
The next install writes the image anew (tools/make_game.py step_romfs), so that space is the install's to use:
the first-run screen counts it as free."""
try:
return (Path(game_dir) / 'romfs' / 'romfs.bin.part').stat().st_size / 1e9
except OSError:
return 0.0
def drop_partial_image(game_dir):
"""Removes that file (partial_image_gb), so that it does not count against the free space an install asks
for."""
try:
(Path(game_dir) / 'romfs' / 'romfs.bin.part').unlink(missing_ok=True)
except OSError:
pass
# Seconds after an update check before the Updates row or F5 starts another. Each check is a request to GitHub,
# which allows 60 an hour for an address: a player who selected the row 25 times in 90 seconds used the allowance
# up, and every check of the next hour was refused.
FORCED_CHECK_PAUSE = 60
# How long an update's rebuild takes, for the Updates row. No single figure holds: a version that changed little
# compiles a few files and links, minutes; one that changed the lifter translates and compiles the game's code again,
# and one that changed what the shaders are built with builds all of them into the graphics driver's cache again
# (tools/make_game.py warm_driver_cache). Players' updates took 24, 34 and 45 minutes where the text said "a few
# minutes".
UPDATE_TAKES = ('That takes a few minutes when little changed, and half an hour or more when the game\'s code or '
'its shaders are built again.')
def drive_of(path):
return Path(path).anchor.rstrip('\\/') or str(Path(path).parent)
@@ -333,6 +391,11 @@ class App:
got = heard = {}
try:
if fonts:
# The cached files are registered (load_fonts) and were not usable, or this would not run:
# registered, Windows keeps them open, and decoding them again failed with "Permission denied"
# at every start, so a cache file that had gone bad was never replaced.
for path, _family in gamefiles.cached_fonts(self.layout).values():
winsys.unregister_font(path)
got = gamefiles.extract_fonts(self.layout, game_dir, dump)
except Exception as e:
self.ctx.log(f'fonts: {e}')
@@ -690,9 +753,11 @@ class App:
elif a.continue_update is not None and not a.rolled_back:
# after a rollback the stub passes the failed version's --continue-update on to this one: the game was
# never rebuilt for the new version, so there is nothing to finish, and home tells why the version is back
self.run_job(jobs.ContinueUpdateJob(self.ctx, gd if gd and build.built_version(gd) else '',
self.run_job(jobs.ContinueUpdateJob(self.ctx, gd if build.installed(gd) else '',
a.continue_update, self.settings.get('dump')), 'home')
elif gd and build.built_version(gd):
elif build.installed(gd):
# also a game whose last update stopped in the compile (build.installed): home, where Play starts the
# build from before the update and Rebuild the Game finishes it, not the first-run screen and its dump
self.show('home')
self.after_update_notice()
if self.install_staged('a staged update waited for the launcher to start'):
@@ -766,12 +831,12 @@ class App:
dump_desc = ('Choose your own dump of Ocarina of Time with the 1.1.1 update, as one .nup file. None was '
'found in Downloads, Desktop or Documents.',)
gd = self.game_dir()
need = 30
free = free_gb(gd)
need = 34
free = free_gb(gd) + partial_image_gb(gd) # Begin Installation removes the partial image (begin_install)
folder_desc = (f'The game is built into this folder. {free:.0f} GB free on {drive_of(gd)}; the game needs '
f'about {need} GB. Select this row to choose another folder.',)
ready = bool(dump)
begin_desc = (f'Build Ocarina of Time on this PC from your own dump. It takes 10 to 25 minutes, once, and '
begin_desc = (f'Build Ocarina of Time on this PC from your own dump. It takes 20 to 45 minutes, once, and '
f'downloads 170 MB of build tools. The game needs about {need} GB on {drive_of(gd)} '
f'({free:.0f} GB free).',) if ready else \
('Choose your dump first: select Game Dump above.',)
@@ -864,8 +929,9 @@ class App:
self.choose_dump()
return
gd = self.game_dir()
if free_gb(gd) < 31 and not (Path(gd) / 'romfs' / 'romfs.bin').is_file():
self.message = f'{drive_of(gd)} has {free_gb(gd):.0f} GB free; the game needs about 30 GB.'
drop_partial_image(gd) # before the free space is measured
if free_gb(gd) < 34 and not (Path(gd) / 'romfs' / 'romfs.bin').is_file():
self.message = f'{drive_of(gd)} has {free_gb(gd):.0f} GB free; the game needs about 34 GB.'
self.render()
return
self.settings['game_dir'] = gd
@@ -908,11 +974,17 @@ class App:
upd = Row('value', 'Updates', f'Version {mine}', 'normal', big=True, icon='refresh',
on=lambda: self.check_now(force=True),
desc=(f'You have NX2navi {mine}. Select this row to look for a new version.',))
rebuild_value = f'Built with {versions.display(built)}' if built else 'Not built'
# an update that stopped in the compile left no finished build on record, but the exe of the build before it
unfinished = not built and build.installed(gd)
rebuild_value = (f'Built with {versions.display(built)}' if built else
'Not Finished' if unfinished else 'Not built')
rebuild_style = 'normal' if versions.display(built) == mine else 'salmon'
play_desc = ('Start Ocarina of Time. The launcher steps aside while you play and comes back when you quit.',)
if built and versions.display(built) != mine:
play_desc += (f'The game was built with {versions.display(built)}. Rebuild the Game brings it to {mine}.',)
elif unfinished:
play_desc += (f'The last rebuild of the game did not finish, so Play starts the build from before it. '
f'Rebuild the Game finishes it and brings the game to {mine}; your dump is not needed.',)
if staged and self.settings.get('auto_install'):
play_desc += (f'NX2navi {staged["version"]} is downloaded and installs after you quit the game.',)
if self.message:
@@ -1360,7 +1432,8 @@ class App:
Row('value', 'Export Save', out.name or str(out), on=self.export_save, desc=export_desc),
Row('toggle', 'Add Log and Settings', on=report, get=lambda: bool(self.settings.get('export_with_log')),
desc=('On: the zip also holds the game\'s latest log (logs\\nx2navi.log) and its settings '
'(nx2navi.ini), which show what happened and with which settings. Keep it on for a bug report.',
'(nx2navi.ini), which show what happened and with which settings. Keep it on for a bug report: '
'while the game has no save yet, Export Save then writes these two alone.',
'Off: the save alone, to give to another player.')),
Row('value', 'Export To', str(out), on=self.choose_export_dir,
desc=(f'Export Save writes into {out}.', 'Select this row to choose another folder.')),
@@ -1398,6 +1471,9 @@ class App:
else:
extra = ' with the game\'s log and settings' if r['extras'] else ''
self.export_message = f'Exported {r["files"]} files{extra} to {r["path"]}.'
if not r['files']: # a game that stopped before its first save: the report files alone
self.export_message = (f'The game has no save yet: exported its log and settings alone to '
f'{r["path"]}.')
try:
winsys.reveal(r['path'])
except OSError:
@@ -1451,8 +1527,8 @@ class App:
nv, mine = versions.display(r.version), versions.display(self.layout.version)
notes = feed.plain_notes(r.notes, 6)
return {'title': f'Version {nv} Is Ready',
'text': [f'You have {mine}. Updating downloads the new version and rebuilds the game in a few '
f'minutes. Your saves and settings stay as they are.'],
'text': [f'You have {mine}. Updating downloads the new version and rebuilds the game. {UPDATE_TAKES} '
f'Your saves and settings stay as they are.'],
'divider': bool(notes), 'subtitle': "What's New" if notes else '', 'bullets': notes}
def staged_desc(self, m):
@@ -1466,7 +1542,7 @@ class App:
text = f'You have {mine}. Version {nv} is downloaded. Select this row to install it.'
return {'title': f'Version {nv} Is Ready', 'text': [text],
'divider': bool(notes), 'subtitle': "What's New" if notes else '', 'bullets': notes,
'note': 'Installing restarts the launcher and rebuilds the game in a few minutes. Your saves and '
'note': f'Installing restarts the launcher and rebuilds the game. {UPDATE_TAKES} Your saves and '
'settings stay as they are.'}
def staging_desc(self):
@@ -1494,6 +1570,11 @@ class App:
return
if not force and time.time() - self.settings.get('last_check', 0) < 600 and self.release:
return
if force and time.time() - self.check_started < FORCED_CHECK_PAUSE:
self.message = 'NX2navi looked for updates less than a minute ago.'
if self.screen == 'home':
self.show('home', self.focus)
return
self.check_error = ''
self.check_started = time.time()
self.check = jobs.CheckJob(self.ctx).start()
@@ -1554,8 +1635,17 @@ class App:
self.root.lift()
if code not in (0, None):
text = exit_code_text(code)
self.ctx.log(f'the game ended with code {text}')
self.message = f'The game ended with code {text}. Its log is logs\\nx2navi.log in the game folder.'
# 70 and 71 follow a FATAL line of the game's own, which names the error: the player gets that line
# and the launcher's log too, where the exit code alone said only that there was one
fatal = last_fatal(self.game_dir()) if code & 0xFFFFFFFF in (70, 71) else ''
if fatal:
shown = fatal if len(fatal) <= FATAL_SHOWN else fatal[:FATAL_SHOWN].rstrip() + '…'
self.ctx.log(f'the game ended with code {code & 0xFFFFFFFF}; its log says: FATAL: {fatal}')
self.message = (f'The game ended with a fatal error (code {code & 0xFFFFFFFF}): {shown}. Its log is '
'logs\\nx2navi.log in the game folder.')
else:
self.ctx.log(f'the game ended with code {text}')
self.message = f'The game ended with code {text}. Its log is logs\\nx2navi.log in the game folder.'
note_exit_in_log(self.game_dir(), text)
if self.install_staged('the game exited'):
return
@@ -1645,7 +1735,7 @@ class App:
self.toggle('Hide Launcher While Playing', 'hide_while_playing',
'Hide this window while the game runs; it comes back when you quit the game.'),
self.toggle('Keep Build Cache', 'keep_cache',
'Keep the translated code and compiled files (about 5 GB) in the game folder, so an update '
'Keep the translated code and compiled files (about 1.5 GB) in the game folder, so an update '
'compiles only what changed. Off: they are deleted after each build.'),
self.channel_row(),
*([self.discord_row(gd)] if gd and Path(gd).is_dir() else []),
@@ -1832,6 +1922,9 @@ class App:
self.message = 'The game is rebuilt and up to date.' if j.kind == 'rebuild' else \
f'NX2navi is now {versions.display(self.layout.version)}.'
self.release = None
note = getattr(j, 'note', '') # what the file check found (jobs.Job.note_scan)
if note and j.after is None:
self.message = f'{self.message} {note}'.strip()
self.reload_fonts_if_new()
self.show(self.job_return)
if j.kind == 'install' and self.settings.get('auto_check'):
@@ -1842,7 +1935,7 @@ class App:
self.show('job')
def drop_build_cache(self):
"""Keep Build Cache off: delete the lifted C and the objects (about 5 GB) but keep state.json, which an
"""Keep Build Cache off: delete the lifted C and the objects (about 1.5 GB) but keep state.json, which an
update needs to rebuild without the dump; the next build then lifts and compiles everything again."""
gd = self.settings.get('game_dir')
if not gd:

View File

@@ -141,6 +141,17 @@ def register_font(path):
return False
def unregister_font(path):
'''Give up a font file register_font added, so the file can be written again: Windows keeps a registered font
file open, and writing it then fails with "Permission denied".'''
if os.name != 'nt':
return
try:
ctypes.windll.gdi32.RemoveFontResourceExW(ctypes.c_wchar_p(str(path)), 0x10, None) # FR_PRIVATE
except (AttributeError, OSError):
pass
def open_path(path):
path = str(path)
if os.name == 'nt':