mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-20 01:49:59 -04:00
Merge the launcher fixes: updates that stop keep the game, honest progress and errors, and a faster file check.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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':
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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,)
|
||||
|
||||
|
||||
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):
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -127,6 +127,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."""
|
||||
@@ -140,6 +165,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)
|
||||
|
||||
@@ -653,9 +711,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'):
|
||||
@@ -729,7 +789,7 @@ class App:
|
||||
'found in Downloads, Desktop or Documents.',)
|
||||
gd = self.game_dir()
|
||||
need = 30
|
||||
free = free_gb(gd)
|
||||
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)
|
||||
@@ -826,6 +886,7 @@ class App:
|
||||
self.choose_dump()
|
||||
return
|
||||
gd = self.game_dir()
|
||||
drop_partial_image(gd) # before the free space is measured
|
||||
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.'
|
||||
self.render()
|
||||
@@ -870,11 +931,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:
|
||||
@@ -1211,8 +1278,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):
|
||||
@@ -1226,7 +1293,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):
|
||||
@@ -1254,6 +1321,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()
|
||||
@@ -1314,8 +1386,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
|
||||
@@ -1592,6 +1673,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'):
|
||||
|
||||
Reference in New Issue
Block a user