mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-18 13:09:57 -04:00
Redraw the in-game menu with the game's own interface textures, and bring in the launcher's save import and faster builds.
In-game menu: the settings window, its tabs, rows, buttons and cursor, the performance overlay and the Preparing shaders box are drawn with the game's interface textures where the RomFS holds them (runtime/ui/menu_skin.cpp, perf_panel.cpp; plain shapes until they are decoded). Restart joins Continue and Quit, each behind a confirmation; VSync is an on/off switch; DLSS is listed only on NVIDIA GPUs; the menu sounds are quieter; Picture shape is gone and the setting descriptions are shorter; edge cases of input, layout and fonts are fixed. tools/menu_preview.py draws the menu without the game. Launcher: Import Saves takes a save from the game folder's saves/ folder or one the player picks; downloads fall back to Windows' own certificate check when Python's refuses a server; updates are faster; the title bar is drawn in the window and Navi is every icon. tools/make_game.py builds with CLANG64's clang where the launcher installed it, packs the RomFS beside the lift and the compile, takes a prebuilt runtime, and compiles the game's resources without cmd.exe, so a user folder with a space in its name builds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,27 +4,32 @@ Run: python tests/launcher_test.py (Python 3.14 for the zstd tests; the window
|
||||
|
||||
- versions, settings
|
||||
- toolchain: the package database, dependency resolution and an install from a local fake repository
|
||||
- net: downloads that resume, check their sha256 and stop on cancel
|
||||
- net: downloads that resume, check their sha256 and stop on cancel; the certificate checks against a local HTTPS
|
||||
server with certificates the openssl command makes (an expired one, an unknown one), the clock comparison
|
||||
- feed: GitHub releases (stable, preview, private with a token) and a plain manifest URL, served locally
|
||||
- updater: staging a release, switching, rollback, pruning, refusing unsafe archives
|
||||
- gamefiles: the .bfotf font wrapping and zstd dictionaries, on a font of this system made into a fake RomFS
|
||||
- make_game.py: a synthetic .nup through the check, exefs and romfs steps with --events, skipped steps on a second
|
||||
run, --rebuild without the game files, and the object cache of the compile step with the system's C compiler
|
||||
run, --rebuild without the game files, the object cache of the compile step with the system's C compiler, and its
|
||||
windres call under folders with spaces (a script in windres's place)
|
||||
- jobs: an install driven by a fake make_game.py
|
||||
- the window: --selftest draws every screen (skipped without a display)
|
||||
"""
|
||||
import contextlib
|
||||
import hashlib
|
||||
import http.server
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import ssl
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import tarfile
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
@@ -33,7 +38,7 @@ ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
sys.path.insert(0, str(ROOT / 'tools'))
|
||||
|
||||
from launcher import build, feed, gamefiles, jobs, net, settings, toolchain, updater, versions # noqa: E402
|
||||
from launcher import build, feed, gamefiles, jobs, net, saves, settings, toolchain, updater, versions # noqa: E402
|
||||
from launcher.context import Context # noqa: E402
|
||||
from launcher.paths import Layout # noqa: E402
|
||||
|
||||
@@ -46,9 +51,11 @@ except ImportError:
|
||||
# ---------------------------------------------------------------- helpers
|
||||
|
||||
class Server:
|
||||
"""A local HTTP server for a folder, with extra routes (path -> (status, headers, body)) and Range support."""
|
||||
"""A local HTTP server for a folder, with extra routes (path -> (status, headers, body)) and Range support; HTTPS
|
||||
with cert (a PEM file holding the certificate and its key). HEAD answers a redirect to https, as a server that
|
||||
serves only HTTPS does."""
|
||||
|
||||
def __init__(self, folder, routes=None):
|
||||
def __init__(self, folder, routes=None, cert=None):
|
||||
folder, routes = Path(folder), routes if routes is not None else {}
|
||||
self.hits = []
|
||||
server = self
|
||||
@@ -57,6 +64,13 @@ class Server:
|
||||
def log_message(self, *a):
|
||||
pass
|
||||
|
||||
def do_HEAD(self):
|
||||
server.hits.append((self.path, dict(self.headers)))
|
||||
self.send_response(301)
|
||||
self.send_header('Location', 'https://127.0.0.1' + self.path)
|
||||
self.send_header('Content-Length', '0')
|
||||
self.end_headers()
|
||||
|
||||
def do_GET(self):
|
||||
server.hits.append((self.path, dict(self.headers)))
|
||||
if self.path in routes:
|
||||
@@ -93,16 +107,65 @@ class Server:
|
||||
|
||||
self.httpd = http.server.ThreadingHTTPServer(('127.0.0.1', 0), H)
|
||||
self.url = f'http://127.0.0.1:{self.httpd.server_address[1]}/'
|
||||
if cert:
|
||||
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
||||
ctx.load_cert_chain(cert)
|
||||
self.httpd.socket = ctx.wrap_socket(self.httpd.socket, server_side=True)
|
||||
self.url = 'https' + self.url[4:]
|
||||
threading.Thread(target=self.httpd.serve_forever, daemon=True).start()
|
||||
|
||||
def close(self):
|
||||
self.httpd.shutdown()
|
||||
self.httpd.server_close()
|
||||
|
||||
|
||||
def sha(data):
|
||||
return hashlib.sha256(data).hexdigest()
|
||||
|
||||
|
||||
def make_cert(folder, start, end):
|
||||
"""A self-signed certificate for 127.0.0.1 valid from start to end (epoch seconds), with its key, as one PEM file
|
||||
in the new folder; made by the openssl command (its ca command takes any dates, its req command none in the
|
||||
past)."""
|
||||
folder = Path(folder)
|
||||
folder.mkdir(parents=True)
|
||||
(folder / 'index.txt').write_text('')
|
||||
(folder / 'serial.txt').write_text('01\n')
|
||||
(folder / 'ca.cnf').write_text('[ca]\ndefault_ca = d\n[d]\ndatabase = index.txt\nserial = serial.txt\n'
|
||||
'new_certs_dir = .\ndefault_md = sha256\npolicy = p\ncopy_extensions = copy\n'
|
||||
'unique_subject = no\n[p]\ncommonName = supplied\n')
|
||||
def stamp(t):
|
||||
return time.strftime('%Y%m%d%H%M%SZ', time.gmtime(t))
|
||||
|
||||
def run(*args):
|
||||
subprocess.run(['openssl', *args], cwd=folder, check=True, capture_output=True)
|
||||
|
||||
run('req', '-new', '-newkey', 'ec', '-pkeyopt', 'ec_paramgen_curve:P-256', '-nodes', '-keyout', 'key.pem',
|
||||
'-out', 'req.csr', '-subj', '/CN=127.0.0.1', '-addext', 'subjectAltName=IP:127.0.0.1')
|
||||
run('ca', '-batch', '-config', 'ca.cnf', '-selfsign', '-keyfile', 'key.pem', '-in', 'req.csr', '-out', 'cert.pem',
|
||||
'-startdate', stamp(start), '-enddate', stamp(end), '-notext')
|
||||
pem = folder / 'both.pem'
|
||||
pem.write_bytes((folder / 'cert.pem').read_bytes() + (folder / 'key.pem').read_bytes())
|
||||
return pem
|
||||
|
||||
|
||||
class CountingContext(ssl.SSLContext):
|
||||
"""A client SSLContext that counts its handshakes."""
|
||||
|
||||
def wrap_socket(self, *args, **kwargs):
|
||||
self.handshakes = getattr(self, 'handshakes', 0) + 1
|
||||
return super().wrap_socket(*args, **kwargs)
|
||||
|
||||
|
||||
def client_context(trust=None):
|
||||
ctx = CountingContext(ssl.PROTOCOL_TLS_CLIENT)
|
||||
if trust:
|
||||
ctx.load_verify_locations(cafile=trust)
|
||||
else:
|
||||
ctx.load_default_certs()
|
||||
return ctx
|
||||
|
||||
|
||||
def tar_bytes(files, compress=True):
|
||||
"""A tar (zstd-compressed when the stdlib has zstd, else gzip) of {name: bytes}."""
|
||||
buf = io.BytesIO()
|
||||
@@ -150,6 +213,24 @@ def fake_repo(folder, damage=None):
|
||||
(folder / 'ucrt64.db').write_bytes(tar_bytes(entries))
|
||||
|
||||
|
||||
def fake_fast_repo(folder, clang_version='1.0-1'):
|
||||
"""The CLANG64 packages toolchain.FAST_PACKAGES names, in folder/clang64."""
|
||||
folder = folder / 'clang64'
|
||||
folder.mkdir()
|
||||
P = toolchain.FAST_PREFIX
|
||||
files = {'clang': {'clang64/bin/clang.exe': b'clang64', 'clang64/bin/clang-repl.exe': b'not needed',
|
||||
'clang64/lib/clang/23/include/immintrin.h': b'// x86'},
|
||||
'llvm-libs': {'clang64/bin/libLLVM-23.dll': b'llvm', 'clang64/share/doc/x': b'skipped'}}
|
||||
entries = {}
|
||||
for root in toolchain.FAST_PACKAGES:
|
||||
name, ver = P + root, clang_version if root == 'clang' else '1.0-1'
|
||||
fn = f'{name}-{ver}-any.pkg.tar.zst'
|
||||
data = tar_bytes({'.PKGINFO': b'pkg', **files.get(root, {f'clang64/bin/{root}.dll': b'dll'})})
|
||||
(folder / fn).write_bytes(data)
|
||||
entries[f'{name}-{ver}/desc'] = desc(name, ver, fn, data, (), ())
|
||||
(folder / 'clang64.db').write_bytes(tar_bytes(entries))
|
||||
|
||||
|
||||
class Tmp(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = Path(tempfile.mkdtemp(prefix='nx2navi-test-'))
|
||||
@@ -215,6 +296,39 @@ class ToolchainTest(Tmp):
|
||||
self.assertEqual(len(srv.hits), hits)
|
||||
self.assertEqual(toolchain.lock_digest(lock), toolchain.installed(self.layout)['digest'])
|
||||
|
||||
def test_fast_clang_beside(self):
|
||||
"""CLANG64's clang goes into msys64/clang64 when its version matches UCRT64's: clang.exe, the DLLs and its
|
||||
headers only."""
|
||||
repo = self.tmp / 'repo'
|
||||
repo.mkdir()
|
||||
fake_repo(repo)
|
||||
fake_fast_repo(repo)
|
||||
srv = Server(repo)
|
||||
self.addCleanup(srv.close)
|
||||
lock = toolchain.make_lock(srv.url, fast_repo=srv.url + 'clang64/')
|
||||
self.assertEqual(lock.get('fast_repo'), srv.url + 'clang64/')
|
||||
self.assertEqual(sum(p.get('env') == 'clang64' for p in lock['packages']), len(toolchain.FAST_PACKAGES))
|
||||
toolchain.install(self.layout, lock, log=lambda m: None)
|
||||
fast = self.layout.msys.parent / 'clang64'
|
||||
self.assertEqual((fast / 'bin' / 'clang.exe').read_bytes(), b'clang64')
|
||||
self.assertTrue((fast / 'bin' / 'libLLVM-23.dll').is_file())
|
||||
self.assertTrue((fast / 'lib' / 'clang' / '23' / 'include' / 'immintrin.h').is_file())
|
||||
self.assertFalse((fast / 'bin' / 'clang-repl.exe').exists())
|
||||
self.assertFalse((fast / 'share').exists())
|
||||
self.assertTrue(toolchain.is_current(self.layout, lock))
|
||||
|
||||
def test_fast_clang_left_out_on_another_version(self):
|
||||
repo = self.tmp / 'repo'
|
||||
repo.mkdir()
|
||||
fake_repo(repo)
|
||||
fake_fast_repo(repo, clang_version='2.0-1')
|
||||
srv = Server(repo)
|
||||
self.addCleanup(srv.close)
|
||||
lock = toolchain.make_lock(srv.url, fast_repo=srv.url + 'clang64/')
|
||||
self.assertNotIn('fast_repo', lock)
|
||||
self.assertIn('2.0-1', lock['fast_skipped'])
|
||||
self.assertFalse(any(p.get('env') for p in lock['packages']))
|
||||
|
||||
def test_damaged_package_refused(self):
|
||||
repo = self.tmp / 'repo'
|
||||
repo.mkdir()
|
||||
@@ -253,6 +367,79 @@ class NetTest(Tmp):
|
||||
with self.assertRaises(net.DownloadError):
|
||||
net.download(srv.url + 'missing.bin', self.tmp / 'out' / 'm.bin')
|
||||
|
||||
def test_server_clock(self):
|
||||
srv = Server(self.tmp)
|
||||
self.addCleanup(srv.close)
|
||||
t = net.server_clock(srv.url) # the Date of the redirect to https, which is not followed
|
||||
self.assertIsNotNone(t)
|
||||
self.assertLess(abs(t - time.time()), 5)
|
||||
self.assertEqual([h[0] for h in srv.hits], ['/'])
|
||||
srv.close()
|
||||
self.assertIsNone(net.server_clock(srv.url, timeout=2))
|
||||
|
||||
|
||||
@unittest.skipUnless(shutil.which('openssl'), 'needs the openssl command')
|
||||
class CertificateTest(Tmp):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
saved = net._CHECKS, dict(net._ACCEPTED), net.server_clock
|
||||
self.addCleanup(lambda: setattr(net, '_CHECKS', saved[0]) or setattr(net, '_ACCEPTED', saved[1]) or
|
||||
setattr(net, 'server_clock', saved[2]))
|
||||
net._ACCEPTED = {}
|
||||
self.data = os.urandom(100_000)
|
||||
(self.tmp / 'f.bin').write_bytes(self.data)
|
||||
|
||||
def serve(self, start, end):
|
||||
cert = make_cert(self.tmp / f'cert{start}', start, end)
|
||||
srv = Server(self.tmp, cert=cert)
|
||||
self.addCleanup(srv.close)
|
||||
return srv, cert
|
||||
|
||||
def test_refused_certificate(self):
|
||||
"""An expired certificate in the trust store: the player's 'certificate has expired', at once, not retried,
|
||||
with the advice the server's clock calls for."""
|
||||
now = time.time()
|
||||
srv, cert = self.serve(now - 400 * 86400, now - 300 * 86400)
|
||||
check = client_context(trust=cert)
|
||||
net._CHECKS = [("Python's check", check)]
|
||||
asked = []
|
||||
cases = ((now + 400 * 86400, "This PC's clock reads"), (now - 3 * 86400, 'set the date, time and time zone'),
|
||||
(now + 60, "This PC's clock agrees with 127.0.0.1's. Security software"),
|
||||
(None, "Check that this PC's date, time and time zone are right"))
|
||||
for i, (theirs, says) in enumerate(cases):
|
||||
net.server_clock = lambda url, timeout=10, version='dev', t=theirs: asked.append(url) or t
|
||||
t0 = time.time()
|
||||
with self.assertRaises(net.CertificateError) as cm:
|
||||
net.download(srv.url + 'f.bin', self.tmp / 'out' / 'f.bin', sha256=sha(self.data))
|
||||
self.assertLess(time.time() - t0, 5) # no retries: the answer would be the same
|
||||
msg = str(cm.exception)
|
||||
self.assertIn("the certificate of 127.0.0.1 was not accepted (Python's check: certificate has expired)",
|
||||
msg)
|
||||
self.assertIn(says, msg)
|
||||
self.assertEqual(check.handshakes, i + 1)
|
||||
self.assertEqual(asked, ['http://127.0.0.1/'] * len(cases)) # the clock is asked over plain HTTP
|
||||
self.assertEqual(srv.hits, []) # no request got through
|
||||
self.assertFalse((self.tmp / 'out' / 'f.bin.part').exists())
|
||||
|
||||
def test_second_check_accepts(self):
|
||||
"""A certificate the first check refuses and the second accepts downloads, and the second goes first for that
|
||||
host from then on."""
|
||||
now = time.time()
|
||||
srv, cert = self.serve(now - 86400, now + 2 * 86400)
|
||||
refuses, accepts = client_context(), client_context(trust=cert)
|
||||
net._CHECKS = [("Python's check", refuses), ("Windows' check", accepts)]
|
||||
dest = net.download(srv.url + 'f.bin', self.tmp / 'out' / 'f.bin', sha256=sha(self.data))
|
||||
self.assertEqual(dest.read_bytes(), self.data)
|
||||
self.assertEqual((refuses.handshakes, accepts.handshakes), (1, 1))
|
||||
self.assertEqual(net._ACCEPTED, {'127.0.0.1': 1})
|
||||
self.assertEqual(net.get_bytes(srv.url + 'f.bin'), self.data)
|
||||
self.assertEqual((refuses.handshakes, accepts.handshakes), (1, 2))
|
||||
net._CHECKS = [("Python's check", refuses)]
|
||||
net.server_clock = lambda *a, **k: None
|
||||
with self.assertRaises(net.CertificateError) as cm:
|
||||
net.get_bytes(srv.url + 'f.bin')
|
||||
self.assertIn("(Python's check: self-signed certificate)", str(cm.exception))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- feed and updater
|
||||
|
||||
@@ -497,6 +684,26 @@ def _nest(flat):
|
||||
return tree
|
||||
|
||||
|
||||
# Stands in for windres.exe: records its arguments, working directory and temporary folder, opens the .rc and the
|
||||
# files it names from the working directory as windres does, and leaves two temporary files behind as windres does.
|
||||
FAKE_WINDRES = r'''
|
||||
import json, os, re, sys
|
||||
from pathlib import Path
|
||||
a = sys.argv[1:]
|
||||
tmp = os.environ['TMP']
|
||||
for n in ('cc1', 'cc2'):
|
||||
open(os.path.join(tmp, n), 'w').close()
|
||||
Path(__file__).with_suffix('.json').write_text(json.dumps(
|
||||
{'argv': a, 'cwd': os.getcwd(), 'tmp': tmp, 'temp': os.environ['TEMP']}))
|
||||
if os.environ.get('FAKE_WINDRES_FAIL'):
|
||||
sys.exit('windres: preprocessing failed.')
|
||||
text = open(a[a.index('-i') + 1]).read()
|
||||
for name in re.findall(r'^\s*\w+\s+\w+\s+"([^"]+)"', text, re.M):
|
||||
open(name, 'rb').close()
|
||||
open(a[a.index('-o') + 1], 'wb').write(b'coff')
|
||||
'''
|
||||
|
||||
|
||||
class MakeGameTest(Tmp):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
@@ -564,6 +771,121 @@ class MakeGameTest(Tmp):
|
||||
self.assertEqual(code, 1)
|
||||
self.assertTrue(any(e['ev'] == 'error' and '1.1.1' in e['message'] for e in ev))
|
||||
|
||||
@unittest.skipUnless(shutil.which('cc'), 'needs a C compiler')
|
||||
def test_object_cache_survives_a_moved_kit(self):
|
||||
"""The launcher keeps each version in its own folder: a kit at a new place reuses every object."""
|
||||
kits = []
|
||||
for name in ('0.3.10', '0.3.11'):
|
||||
kit = self.tmp / 'app' / name / 'runtime'
|
||||
kit.mkdir(parents=True)
|
||||
(kit / 'h.h').write_text('#define V 1\n')
|
||||
(kit / 'a.c').write_text('#include "h.h"\nint a(void) { return V; }\n')
|
||||
kits.append(kit.parent)
|
||||
obj = self.tmp / 'obj'
|
||||
obj.mkdir()
|
||||
|
||||
def go(kit):
|
||||
rt = kit / 'runtime'
|
||||
srcs = [(rt / 'a.c', obj / 'rt_a.o')]
|
||||
cache = self.mg.ObjectCache(obj / 'objects.json', {'<kit>': kit})
|
||||
return self.mg.compile_all(srcs, lambda s_, d: ['cc', '-c', '-I', str(rt), '-o', str(d), str(s_)], cache,
|
||||
1, dict(os.environ), salt='cc')
|
||||
self.assertEqual(go(kits[0])[:2], (1, 0))
|
||||
self.assertEqual(go(kits[1])[:2], (0, 1))
|
||||
(kits[1] / 'runtime' / 'h.h').write_text('#define V 2\n') # the new version changed the header
|
||||
self.assertEqual(go(kits[1])[:2], (1, 0))
|
||||
|
||||
def main_with(self, romfs, lift, compile_):
|
||||
"""make_game.main() in this process for a first build, with the given step functions in place of the real
|
||||
lift and compile (they need real modules and clang). Returns the SystemExit code, or 0."""
|
||||
sys.path.insert(0, str(self.layout.pydeps)) # the fake zbic
|
||||
saved = (self.mg.step_romfs, self.mg.step_lift, self.mg.step_compile, sys.argv)
|
||||
self.mg.step_romfs, self.mg.step_lift, self.mg.step_compile = romfs, lift, compile_
|
||||
sys.argv = ['make_game.py', '--nup', str(self.nup), '--out', str(self.out), '--msys', str(self.msys),
|
||||
'--until', 'compile'] # the shaders step needs the shader translator
|
||||
try:
|
||||
with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()):
|
||||
self.mg.main()
|
||||
return 0
|
||||
except SystemExit as e:
|
||||
return e.code
|
||||
finally:
|
||||
self.mg.step_romfs, self.mg.step_lift, self.mg.step_compile, sys.argv = saved
|
||||
sys.path.remove(str(self.layout.pydeps))
|
||||
|
||||
def test_romfs_beside_lift_and_compile(self):
|
||||
"""A first build packs the RomFS image while the lift and the compile run; the compile gets the running step
|
||||
and waits for it only for the RomFS index."""
|
||||
real_romfs, seen = self.mg.step_romfs, {}
|
||||
|
||||
def romfs(src, out, jobs):
|
||||
time.sleep(0.5)
|
||||
real_romfs(src, out, jobs)
|
||||
|
||||
def lift(out, cache):
|
||||
seen['lift_beside_romfs'] = not (out / 'romfs' / 'index.json').exists()
|
||||
|
||||
def compile_(out, cache, msys, jobs, romfs_job=None):
|
||||
seen['job'] = romfs_job is not None
|
||||
romfs_job.join()
|
||||
seen['image_after_join'] = (out / 'romfs' / 'romfs.bin').is_file()
|
||||
(out / self.mg.EXE_NAME).write_bytes(b'MZ')
|
||||
self.assertEqual(self.main_with(romfs, lift, compile_), 0)
|
||||
self.assertEqual(seen, {'lift_beside_romfs': True, 'job': True, 'image_after_join': True})
|
||||
steps = json.loads((self.out / 'build-cache' / 'state.json').read_text())['steps']
|
||||
self.assertEqual(set(steps), {'exefs', 'romfs', 'lift', 'compile'})
|
||||
|
||||
def test_romfs_failure_stops_the_build(self):
|
||||
ran, lifting, failed = [], threading.Event(), threading.Event()
|
||||
|
||||
def romfs(src, out, jobs):
|
||||
lifting.wait(5)
|
||||
failed.set()
|
||||
self.mg.fail('the RomFS image could not be written')
|
||||
|
||||
def lift(out, cache):
|
||||
lifting.set()
|
||||
failed.wait(5) # the RomFS step fails while the lift runs
|
||||
time.sleep(0.1)
|
||||
ran.append('lift')
|
||||
|
||||
def compile_(out, cache, msys, jobs, romfs_job=None):
|
||||
ran.append('compile')
|
||||
self.assertEqual(self.main_with(romfs, lift, compile_), 1)
|
||||
self.assertEqual(ran, ['lift'])
|
||||
steps = json.loads((self.out / 'build-cache' / 'state.json').read_text())['steps']
|
||||
self.assertNotIn('romfs', steps)
|
||||
|
||||
@unittest.skipIf(os.name == 'nt', 'uses a POSIX script as the compiler')
|
||||
def test_largest_source_compiles_first(self):
|
||||
src, obj = self.tmp / 'src', self.tmp / 'obj'
|
||||
src.mkdir()
|
||||
obj.mkdir()
|
||||
for name, size in (('small.c', 10), ('big.c', 5000), ('mid.c', 800)):
|
||||
(src / name).write_text('/*' + 'x' * size + '*/\n')
|
||||
order = self.tmp / 'order.txt'
|
||||
cc = self.tmp / 'fakecc'
|
||||
cc.write_text(f'#!{sys.executable}\nimport sys\nargs = sys.argv[1:]\n'
|
||||
'dep, out, s = args[args.index("-MF") + 1], args[args.index("-o") + 1], args[-1]\n'
|
||||
f'open({str(order)!r}, "a").write(s.rsplit("/", 1)[-1] + "\\n")\n'
|
||||
'open(out, "w").close(); open(dep, "w").write(out + ": " + s + "\\n")\n')
|
||||
cc.chmod(0o755)
|
||||
srcs = [(src / n, obj / (n[:-2] + '.o')) for n in ('small.c', 'big.c', 'mid.c')]
|
||||
cache = self.mg.ObjectCache(obj / 'objects.json')
|
||||
compiled, reused, bad = self.mg.compile_all(srcs, lambda s, d: [str(cc), '-c', '-o', str(d), str(s)], cache,
|
||||
1, dict(os.environ))
|
||||
self.assertEqual((compiled, bad), (3, []))
|
||||
self.assertEqual(order.read_text().split(), ['big.c', 'mid.c', 'small.c'])
|
||||
|
||||
def test_runtime_split(self):
|
||||
tainted = self.mg.per_install_names()
|
||||
self.assertTrue({'layout.h', 'patches.h', 'romfs_config.h'} <= tainted)
|
||||
names = {p.name: self.mg.per_install(p, tainted) for p, _ in self.mg.runtime_sources()}
|
||||
for f in ('loader.c', 'memory.c', 'romfs.c', 'patches.c'):
|
||||
self.assertTrue(names[f], f)
|
||||
self.assertFalse(names['vk_device.c'])
|
||||
self.assertLess(sum(names.values()), len(names) // 4) # most of the runtime can come prebuilt
|
||||
|
||||
def test_parse_deps(self):
|
||||
text = 'C:/o/x.o: C:/src/x.c C:/inc/a\\ b.h \\\n C:/inc/c.h\n'
|
||||
self.assertEqual(self.mg.parse_deps(text), ['C:/src/x.c', 'C:/inc/a b.h', 'C:/inc/c.h'])
|
||||
@@ -594,6 +916,48 @@ class MakeGameTest(Tmp):
|
||||
self.assertEqual((compiled, len(bad)), (1, 1))
|
||||
self.assertNotIn('b.o', json.loads((obj / 'objects.json').read_text()))
|
||||
|
||||
@unittest.skipIf(os.name == 'nt', 'a script stands in for windres.exe')
|
||||
def test_compile_resources(self):
|
||||
# The toolchain, the kit and the game under folders with spaces, as under C:\Users\Ali Sayed. windres must
|
||||
# start gcc itself (--use-temp-file) on a line that names no path but gcc's: the .rc by its bare name from
|
||||
# runtime/, no -I, -D or -U.
|
||||
msys, obj = self.tmp / 'tool chain' / 'ucrt64', self.out / 'build-cache' / 'obj'
|
||||
windres = msys / 'bin' / 'windres.exe'
|
||||
windres.parent.mkdir(parents=True)
|
||||
windres.write_text(f'#!{sys.executable}\n' + FAKE_WINDRES)
|
||||
windres.chmod(0o755)
|
||||
obj.mkdir(parents=True)
|
||||
rt = self.tmp / 'the kit' / 'runtime'
|
||||
rt.mkdir(parents=True)
|
||||
for name in ('nx2navi.rc', 'nx2navi.manifest', 'nx2navi.ico'):
|
||||
shutil.copy2(ROOT / 'runtime' / name, rt / name)
|
||||
rco = self.mg.compile_resources(msys, rt, obj, dict(os.environ))
|
||||
self.assertEqual((rco, rco.read_bytes()), (obj / 'nx2navi_rc.o', b'coff'))
|
||||
seen = json.loads(windres.with_suffix('.json').read_text())
|
||||
args = seen['argv']
|
||||
self.assertEqual(seen['cwd'], str(rt))
|
||||
self.assertIn('--use-temp-file', args)
|
||||
self.assertIn(f'--preprocessor={msys / "bin" / "gcc.exe"}', args)
|
||||
self.assertEqual([a for a in args if a.startswith('--preprocessor-arg=')],
|
||||
['--preprocessor-arg=-E', '--preprocessor-arg=-xc', '--preprocessor-arg=-DRC_INVOKED'])
|
||||
self.assertEqual(args[args.index('-i') + 1], 'nx2navi.rc')
|
||||
self.assertFalse([a for a in args if a.startswith(('-I', '--include-dir', '-D', '-U'))])
|
||||
# the temporary files went to a folder of the build cache, which is gone with the two windres left there
|
||||
self.assertEqual(seen['tmp'], seen['temp'])
|
||||
self.assertEqual(Path(seen['tmp']).parent, obj)
|
||||
self.assertEqual(sorted(p.name for p in obj.iterdir()), ['nx2navi_rc.o'])
|
||||
# the same resource again: the object keeps its time, so the link key does not change and nothing relinks
|
||||
before = rco.stat().st_mtime_ns
|
||||
time.sleep(0.02)
|
||||
self.mg.compile_resources(msys, rt, obj, dict(os.environ))
|
||||
self.assertEqual(rco.stat().st_mtime_ns, before)
|
||||
# a failing windres stops the build with its message
|
||||
err = io.StringIO()
|
||||
with self.assertRaises(SystemExit), contextlib.redirect_stderr(err):
|
||||
self.mg.compile_resources(msys, rt, obj, dict(os.environ, FAKE_WINDRES_FAIL='1'))
|
||||
self.assertIn('windres failed on runtime/nx2navi.rc: windres: preprocessing failed.', err.getvalue())
|
||||
self.assertEqual(sorted(p.name for p in obj.iterdir()), ['nx2navi_rc.o'])
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- jobs
|
||||
|
||||
@@ -650,6 +1014,111 @@ class JobTest(Tmp):
|
||||
self.assertEqual(j.step('check').state, jobs.FAILED)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- the icon
|
||||
|
||||
class SavesTest(unittest.TestCase):
|
||||
"""launcher/saves.py: where the game keeps its save, the save folders it recognises, and the import."""
|
||||
|
||||
def setUp(self):
|
||||
self.tmp = Path(tempfile.mkdtemp(prefix='nx2navi-saves-'))
|
||||
self.game = self.tmp / 'game'
|
||||
self.game.mkdir()
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.tmp, ignore_errors=True)
|
||||
|
||||
def files(self, folder, names):
|
||||
for n, data in names.items():
|
||||
(folder / n).parent.mkdir(parents=True, exist_ok=True)
|
||||
(folder / n).write_bytes(data)
|
||||
return folder
|
||||
|
||||
def test_account_folder_as_the_game_names_it(self):
|
||||
(self.game / 'profile').mkdir()
|
||||
lo, hi = 0x1122334455667788, 0x99aabbccddeeff00
|
||||
(self.game / 'profile' / 'account.uid').write_bytes(lo.to_bytes(8, 'little') + hi.to_bytes(8, 'little'))
|
||||
self.assertEqual(saves.account_dir(self.game).name, f'{hi:016x}{lo:016x}') # fs_host.c storage_init
|
||||
fresh = self.tmp / 'fresh'
|
||||
acct = saves.account_dir(fresh) # a game that never ran: the account is made
|
||||
self.assertEqual(len((fresh / 'profile' / 'account.uid').read_bytes()), 16)
|
||||
self.assertEqual(saves.account_dir(fresh), acct)
|
||||
|
||||
def test_save_folders_found(self):
|
||||
plain = self.files(self.tmp / 'backup', {'save.dat': b'1', 'option.dat': b'2'})
|
||||
self.assertEqual(saves.find_save(plain)[0], plain)
|
||||
emu = self.files(self.tmp / 'emu', {'0000000000000000/' + 'a' * 32 + '/0100F2C0115B6000/save.dat': b'1'})
|
||||
self.assertEqual(saves.find_save(emu)[0].name, '0100F2C0115B6000')
|
||||
other = self.files(self.tmp / 'other', {'profile/save/' + 'b' * 32 + '/save.dat': b'1', 'ocarina.exe': b''})
|
||||
self.assertEqual(saves.find_save(other)[0].name, 'b' * 32)
|
||||
self.assertEqual(saves.find_save(other / 'profile')[0].name, 'b' * 32)
|
||||
(self.tmp / 'empty').mkdir()
|
||||
self.assertIsNone(saves.find_save(self.tmp / 'empty')[0])
|
||||
|
||||
def test_import_backs_up_and_clears_the_journal(self):
|
||||
acct = saves.account_dir(self.game)
|
||||
self.files(acct, {'save.dat': b'old'})
|
||||
tx = self.game / 'profile' / 'save-tx'
|
||||
self.files(tx, {'ready-3/save.dat': b'newer', 'work/save.dat': b'w'})
|
||||
src = self.files(self.tmp / 'mine', {'save.dat': b'imported', 'sub/extra.bin': b'x'})
|
||||
r = saves.import_save(self.game, src, log=lambda m: None)
|
||||
self.assertEqual(r['files'], 2)
|
||||
self.assertEqual((acct / 'save.dat').read_bytes(), b'imported')
|
||||
self.assertEqual((acct / 'sub' / 'extra.bin').read_bytes(), b'x')
|
||||
self.assertEqual(list(tx.iterdir()), []) # the game loads the imported save
|
||||
self.assertEqual((r['backup'] / 'save.dat').read_bytes(), b'newer') # the save the game would have loaded
|
||||
self.assertTrue((self.game / 'saves' / 'README.txt').is_file())
|
||||
self.assertTrue((src / 'save.dat').is_file()) # a picked folder is left as it was
|
||||
|
||||
def test_import_from_the_saves_folder(self):
|
||||
d = saves.ensure_saves_dir(self.game)
|
||||
self.assertIsNone(saves.waiting(self.game)[0]) # README.txt alone is no save
|
||||
self.files(d, {'emu/0000000000000000/' + 'c' * 32 + '/0100F2C0115B6000/save.dat': b'emu'})
|
||||
self.assertIsNotNone(saves.waiting(self.game)[0])
|
||||
r = saves.import_save(self.game, log=lambda m: None)
|
||||
self.assertIsNone(r['backup']) # there was no save yet
|
||||
self.assertEqual((saves.account_dir(self.game) / 'save.dat').read_bytes(), b'emu')
|
||||
left = sorted(e.name for e in d.iterdir())
|
||||
self.assertEqual(left[0], 'README.txt')
|
||||
self.assertTrue(left[1].startswith('imported '))
|
||||
self.assertIsNone(saves.waiting(self.game)[0]) # not imported twice
|
||||
self.files(d, {'save.dat': b'loose'}) # files put straight into saves/
|
||||
r = saves.import_save(self.game, log=lambda m: None)
|
||||
self.assertEqual((saves.account_dir(self.game) / 'save.dat').read_bytes(), b'loose')
|
||||
self.assertEqual((r['backup'] / 'save.dat').read_bytes(), b'emu')
|
||||
self.assertIsNone(saves.waiting(self.game)[0])
|
||||
|
||||
def test_refusals(self):
|
||||
acct = saves.account_dir(self.game)
|
||||
self.files(acct, {'save.dat': b'1'})
|
||||
with self.assertRaises(saves.SaveError):
|
||||
saves.import_save(self.game, self.game / 'profile', log=lambda m: None) # its own save
|
||||
big = self.tmp / 'big'
|
||||
big.mkdir()
|
||||
for i in range(saves.MAX_FILES + 1):
|
||||
(big / f'{i}.txt').write_bytes(b'')
|
||||
with self.assertRaises(saves.SaveError):
|
||||
saves.import_save(self.game, big, log=lambda m: None)
|
||||
self.assertEqual((acct / 'save.dat').read_bytes(), b'1') # nothing changed
|
||||
|
||||
|
||||
class IconTest(unittest.TestCase):
|
||||
def test_navi(self):
|
||||
try:
|
||||
from launcher.ui import art
|
||||
except ImportError:
|
||||
self.skipTest('needs numpy')
|
||||
self.assertEqual(art.navi(2).shape, (42, 44, 4))
|
||||
data = art.navi_ico()
|
||||
count = struct.unpack_from('<H', data, 4)[0]
|
||||
sizes = [data[6 + 16 * i] or 256 for i in range(count)]
|
||||
self.assertEqual(sizes[0], 16)
|
||||
self.assertIn(256, sizes)
|
||||
off = struct.unpack_from('<I', data, 6 + 12)[0]
|
||||
self.assertEqual(struct.unpack_from('<Iii', data, off), (40, 16, 32)) # a bitmap entry, not PNG
|
||||
big = struct.unpack_from('<I', data, 6 + 16 * (count - 1) + 12)[0]
|
||||
self.assertEqual(data[big:big + 4], b'\x89PNG')
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- the window
|
||||
|
||||
def have_display():
|
||||
@@ -671,7 +1140,7 @@ class WindowTest(Tmp):
|
||||
r = subprocess.run([sys.executable, '-m', 'launcher', '--root', str(self.tmp / 'w'), '--selftest'],
|
||||
cwd=ROOT, capture_output=True, text=True, timeout=180)
|
||||
self.assertEqual(r.returncode, 0, r.stderr)
|
||||
self.assertIn('8 screens drawn', r.stdout)
|
||||
self.assertIn('9 screens drawn', r.stdout)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user