mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-21 00:59:59 -04:00
Faster frames (the replay's memory work, held submits, helper threads, collection and capture cuts), the game letting old areas go (QueryMemoryInfo), the 0.5.2 stutter fixed, AMD RX 5000/6000 and integrated-GPU fixes, the GPU device chosen by name, the in-game menus and launcher reworked, controller, Linux and benchmark fixes.
254 lines
14 KiB
Python
254 lines
14 KiB
Python
"""make_game.py's last step, hookshot --prepare-shaders, run against stub games: its stage and progress lines go on to the builder,
|
|
its own @@done and @@error don't (the builders read those as the build's end or failure), a failure or a silent hang leaves the work
|
|
to the first start (never a failed build), and the game runs in its own environment, as the builders' Play starts it.
|
|
|
|
python -I -B tests/test_prepare_at_build.py
|
|
"""
|
|
import ast
|
|
import contextlib
|
|
import importlib.util
|
|
import io
|
|
import json
|
|
import os
|
|
import re
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
import unittest
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
from unittest import mock
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
SOURCE = ROOT / "nx2" / "make_game.py"
|
|
spec = importlib.util.spec_from_file_location("make_game_prepare", SOURCE)
|
|
make_game = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(make_game)
|
|
BUILDER = ROOT / "builder" / "linux" / "hookshot_builder.py"
|
|
|
|
|
|
def stub(code):
|
|
"""A stand-in for hookshot --prepare-shaders: authored Python, no game, no GPU"""
|
|
return [sys.executable, "-I", "-c", code]
|
|
|
|
|
|
def prepared(cmd, **kwargs):
|
|
out = io.StringIO()
|
|
with contextlib.redirect_stdout(out):
|
|
code = make_game.prepare_shaders(cmd, **kwargs)
|
|
return code, out.getvalue().splitlines()
|
|
|
|
|
|
def not_prepared(lines):
|
|
return [l for l in lines if l.startswith(make_game.NOT_PREPARED)]
|
|
|
|
|
|
class PrepareShadersTests(unittest.TestCase):
|
|
def test_progress_goes_on_and_the_games_done_does_not(self):
|
|
code, lines = prepared(stub("print('hookshot v'); print('@@stage shaders Preparing shaders'); print('@@progress 0.500');"
|
|
"print('@@done shaders')"))
|
|
self.assertEqual(code, 0)
|
|
self.assertEqual(lines, ["hookshot v", "@@stage shaders Preparing shaders", "@@progress 0.500", "done shaders"])
|
|
|
|
def test_no_vulkan_leaves_them_to_the_first_start(self):
|
|
code, lines = prepared(stub("import sys; print('@@error no usable Vulkan here'); sys.exit(3)"))
|
|
self.assertEqual(code, 3)
|
|
self.assertFalse(any(l.startswith("@@") for l in lines))
|
|
self.assertIn("error no usable Vulkan here", lines)
|
|
self.assertEqual(len(not_prepared(lines)), 1)
|
|
self.assertIn("(no usable Vulkan here)", lines[-1])
|
|
|
|
def test_a_crash_leaves_them_to_the_first_start(self):
|
|
code, lines = prepared(stub("import sys; print('@@stage shaders Preparing shaders'); sys.exit(5)"))
|
|
self.assertEqual(code, 5)
|
|
self.assertIn("exit status 5", not_prepared(lines)[0])
|
|
|
|
def test_a_silent_hang_is_stopped_and_not_a_failed_build(self):
|
|
# (prints its stage, then nothing: as a driver deadlock or a crash dialog left open. silence is 15 minutes in a build;
|
|
# a hang does not answer the stop request either, and is killed stop_wait seconds after it)
|
|
started = time.monotonic()
|
|
with tempfile.TemporaryDirectory(prefix="hookshot-prepare-") as folder:
|
|
stop = os.path.join(folder, make_game.PREPARE_STOP)
|
|
code, lines = prepared(stub("import time; print('@@stage shaders Preparing shaders', flush=True); time.sleep(120)"),
|
|
silence=1, stop=stop, stop_wait=1)
|
|
self.assertFalse(os.path.exists(stop)) # (the request written for it is not left behind)
|
|
self.assertLess(time.monotonic() - started, 30)
|
|
self.assertIsNone(code)
|
|
self.assertEqual(lines[0], "@@stage shaders Preparing shaders")
|
|
self.assertIn("printed nothing for 1 seconds and was stopped", not_prepared(lines)[0])
|
|
|
|
def test_a_silent_run_is_asked_to_stop_before_it_is_killed(self):
|
|
# ((Triforce, 11-18) the game answers triforce_prepare.stop as a closed window: it ends its driver's part cleanly. Here a
|
|
# stand-in that waits for the request, then says it ended by itself, in a file a kill would never write)
|
|
with tempfile.TemporaryDirectory(prefix="hookshot-prepare-") as folder:
|
|
stop, ended = os.path.join(folder, make_game.PREPARE_STOP), os.path.join(folder, "ended")
|
|
code = ("import os, time\nprint('@@stage shaders Preparing shaders', flush=True)\n"
|
|
"while not os.path.exists(%r): time.sleep(0.05)\nopen(%r, 'w').close()" % (stop, ended))
|
|
started = time.monotonic()
|
|
code, lines = prepared(stub(code), silence=1, stop=stop, stop_wait=60)
|
|
self.assertLess(time.monotonic() - started, 30)
|
|
self.assertTrue(os.path.exists(ended), "the run was killed, not asked to stop")
|
|
self.assertFalse(os.path.exists(stop))
|
|
self.assertIsNone(code)
|
|
self.assertEqual(len(not_prepared(lines)), 1)
|
|
|
|
def test_a_stale_stop_request_is_removed_before_the_run(self):
|
|
with tempfile.TemporaryDirectory(prefix="hookshot-prepare-") as folder:
|
|
stop = os.path.join(folder, make_game.PREPARE_STOP)
|
|
open(stop, "w").close()
|
|
code, lines = prepared(stub("import os; print(os.path.exists(%r))" % stop), stop=stop)
|
|
self.assertEqual((code, lines), (0, ["False"]))
|
|
self.assertFalse(os.path.exists(stop))
|
|
|
|
def test_the_stop_request_is_the_games_and_the_builders(self):
|
|
# (the file's name and the stopped run's line are the game's own; the builder's Cancel sets the event the game opens)
|
|
main = (ROOT / "gpu" / "vk" / "vk_main.cpp").read_text(encoding="utf-8")
|
|
runner = (ROOT / "builder" / "HookshotBuilder" / "Runner.cs").read_text(encoding="utf-8")
|
|
self.assertIn('"%s"' % make_game.PREPARE_STOP, main)
|
|
self.assertIn('printf("%s (' % make_game.NOT_PREPARED, main)
|
|
self.assertEqual(make_game.PREPARE_STOP_WAIT, 60)
|
|
default = make_game.prepare_shaders.__defaults__
|
|
self.assertIn(make_game.PREPARE_STOP_WAIT, default)
|
|
# (the event's name is the builder's run's own, passed to the game through make_game.py's environment, which keeps it;
|
|
# not an RX_VK_ name, which the shaders' stamp would take)
|
|
self.assertIn('const char* name = getenv("TRIFORCE_PREPARE_STOP");', main)
|
|
self.assertIn('if (name && !strncmp(name, "Local\\\\TriforcePrepareStop.", 26)) event_ = CreateEventA(nullptr, TRUE, FALSE, name);',
|
|
main)
|
|
self.assertEqual(len("Local\\TriforcePrepareStop."), 26)
|
|
self.assertIn('readonly string stopEvent = @"Local\\TriforcePrepareStop." + Guid.NewGuid().ToString("N");', runner)
|
|
self.assertIn('si.Environment["TRIFORCE_PREPARE_STOP"] = stopEvent;', runner)
|
|
self.assertIn("Process.Start(Info(tools, args, workers, ngxSdk, stopEvent))", runner)
|
|
self.assertEqual(make_game.game_env({"TRIFORCE_PREPARE_STOP": "x"}), {"TRIFORCE_PREPARE_STOP": "x"})
|
|
cancel = runner[runner.index("public Task Cancel()"):runner.index("public static async Task<(bool Ok, string Text)> Check(")]
|
|
self.assertIn("EventWaitHandle.TryOpenExisting(stopEvent, out var stop)", cancel)
|
|
self.assertLess(cancel.index("stop.Set()"), cancel.index("p.WaitForExit(30000)"))
|
|
self.assertLess(cancel.index("p.WaitForExit(30000)"), cancel.index("Kill(entireProcessTree: true)"))
|
|
window = (ROOT / "builder" / "HookshotBuilder" / "MainWindow.xaml.cs").read_text(encoding="utf-8")
|
|
self.assertIn("runner?.Cancel().Wait();", window)
|
|
# (a Cancel whose preparation stopped well still reaches make_game.py's @@done: it is a Cancel all the same, back to
|
|
# the files, not "Your game is ready" with shortcuts made and the build's files freed)
|
|
self.assertIn('Finished?.Invoke(cancelled ? "cancelled" :', runner)
|
|
ended = window[window.index("void Ended(string? failure)"):window.index("void LogToggled(")]
|
|
self.assertLess(ended.index('if (failure == "cancelled")'), ended.index("if (finished.Length > 0)"))
|
|
|
|
def test_steady_output_is_not_silence(self):
|
|
code, lines = prepared(stub("import time\nfor i in range(6):\n print('@@progress %.3f' % (i / 6), flush=True); "
|
|
"time.sleep(0.4)"), silence=1)
|
|
self.assertEqual(code, 0)
|
|
self.assertEqual(not_prepared(lines), [])
|
|
|
|
def test_missing_game_is_not_a_failed_build(self):
|
|
code, lines = prepared([str(ROOT / "no-such-folder" / "hookshot.exe"), "--prepare-shaders"])
|
|
self.assertEqual(code, 1)
|
|
self.assertEqual(len(not_prepared(lines)), 1)
|
|
self.assertFalse(any(l.startswith("@@") for l in lines))
|
|
|
|
def test_the_game_runs_in_its_folder_without_the_tools_environment(self):
|
|
dropped = dict(HOOKSHOT_TOOLS="/builder/tools", HOOKSHOT_NO_PREPARE="0", PYTHONPATH="/builder/python",
|
|
PYTHONHOME="/builder", APPDIR="/appdir", APPIMAGE="/tmp/Hookshot.AppImage", ARGV0="Hookshot", OWD="/old",
|
|
LD_LIBRARY_PATH="/builder/lib")
|
|
with tempfile.TemporaryDirectory(prefix="hookshot-prepare-") as folder, \
|
|
mock.patch.dict(os.environ, dict(dropped, HOOKSHOT_TEST_KEPT_NOT="x", KEPT_FOR_THE_GAME="yes")):
|
|
code, lines = prepared(stub("import json, os; print(json.dumps({'cwd': os.getcwd(), 'env': dict(os.environ)}))"),
|
|
folder=folder)
|
|
seen = json.loads(lines[0])
|
|
self.assertEqual(code, 0)
|
|
self.assertEqual(os.path.normcase(os.path.realpath(seen["cwd"])), os.path.normcase(os.path.realpath(folder)))
|
|
for name in list(dropped) + ["HOOKSHOT_TEST_KEPT_NOT"]:
|
|
self.assertNotIn(name, seen["env"], name)
|
|
self.assertEqual(seen["env"]["KEPT_FOR_THE_GAME"], "yes")
|
|
|
|
def test_game_env_is_the_play_buttons(self):
|
|
env = make_game.game_env({"HOOKSHOT_X": "1", "PYTHONPATH": "p", "APPIMAGE": "a", "APPDIR": "d", "ARGV0": "0", "OWD": "o",
|
|
"LD_LIBRARY_PATH": "l", "PATH": "/usr/bin", "HOME": "/home/p"})
|
|
self.assertEqual(env, {"PATH": "/usr/bin", "HOME": "/home/p"})
|
|
# (the shell launcher can't import it: the same names there)
|
|
launch = (ROOT / "builder" / "linux" / "hookshot-launch").read_text(encoding="utf-8")
|
|
names = re.search(r"\$1 ~ /\^\(([A-Z_|0-9]+)\)/ \|\| \$1 == \"LD_LIBRARY_PATH\"", launch).group(1).split("|")
|
|
self.assertEqual(tuple(names), make_game.GAME_ENV_DROP)
|
|
|
|
|
|
class RunSilenceTests(unittest.TestCase):
|
|
def test_a_silent_step_fails_the_build_when_checked(self):
|
|
out = io.StringIO()
|
|
with contextlib.redirect_stdout(out), self.assertRaises(SystemExit):
|
|
make_game.run(stub("import time; time.sleep(120)"), silence=1)
|
|
self.assertIn("@@error", out.getvalue())
|
|
self.assertIn("printed nothing for 1 seconds", out.getvalue())
|
|
|
|
def test_show_filters_what_is_printed_but_not_what_is_seen(self):
|
|
out, seen = io.StringIO(), []
|
|
with contextlib.redirect_stdout(out):
|
|
code = make_game.run(stub("print('keep'); print('drop')"), on_line=seen.append,
|
|
show=lambda line: None if line == "drop" else line.upper())
|
|
self.assertEqual(code, 0)
|
|
self.assertEqual(out.getvalue().splitlines(), ["KEEP"])
|
|
self.assertEqual(seen, ["keep", "drop"])
|
|
|
|
|
|
class SkipTests(unittest.TestCase):
|
|
def skipped(self, environ, flag=False, cross=False):
|
|
with mock.patch.object(make_game, "CROSS", cross):
|
|
return make_game.prepare_skipped(SimpleNamespace(no_prepare_shaders=flag), environ)
|
|
|
|
def test_prepared_by_default(self):
|
|
self.assertIsNone(self.skipped({}))
|
|
|
|
def test_the_unticked_box(self):
|
|
self.assertIn("unticked", self.skipped({}, flag=True))
|
|
|
|
def test_hookshot_no_prepare_as_the_game_reads_its_switches(self):
|
|
self.assertIsNone(self.skipped({"HOOKSHOT_NO_PREPARE": "0"}))
|
|
self.assertIsNotNone(self.skipped({"HOOKSHOT_NO_PREPARE": "1"}))
|
|
self.assertIsNotNone(self.skipped({"HOOKSHOT_NO_PREPARE": ""})) # (set, as the game's getenv && strcmp(.., "0"))
|
|
|
|
def test_a_windows_game_built_on_linux(self):
|
|
self.assertIn("Windows game built on Linux", self.skipped({}, cross=True))
|
|
|
|
def test_the_build_times_leave_the_preparation_out(self):
|
|
# (one check of main's order: the times are recorded and said before the preparation, which comes before @@done)
|
|
main = SOURCE.read_text(encoding="utf-8").split("\ndef main(", 1)[1]
|
|
self.assertLess(main.index("record_times("), main.index("prepare_skipped(a)"))
|
|
self.assertLess(main.index('say("times"'), main.index("prepare_skipped(a)"))
|
|
self.assertLess(main.index("prepare_skipped(a)"), main.index('say("done", out)'))
|
|
|
|
|
|
def builder_method(name):
|
|
"""One method of the Linux builder's window, compiled alone (no Tk)"""
|
|
tree = ast.parse(BUILDER.read_text(encoding="utf-8"))
|
|
cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == "Builder")
|
|
node = next(n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == name)
|
|
stages = next(n for n in tree.body if isinstance(n, ast.Assign) and getattr(n.targets[0], "id", "") == "STAGES")
|
|
namespace = {"make_game": make_game, "time": time}
|
|
exec(compile(ast.Module(body=[stages, node], type_ignores=[]), str(BUILDER), "exec"), namespace)
|
|
return namespace[name]
|
|
|
|
|
|
class LinuxBuilderTests(unittest.TestCase):
|
|
def window(self):
|
|
text = []
|
|
return SimpleNamespace(stage_text=SimpleNamespace(set=text.append), bar=SimpleNamespace(configure=lambda **k: None),
|
|
compile_started=None, eta=None, shaders_later=False), text
|
|
|
|
def test_a_later_stage_clears_the_compiles_clock_and_countdown(self):
|
|
on_line = builder_method("on_line")
|
|
w, text = self.window()
|
|
on_line(w, "@@stage compile Compiling")
|
|
on_line(w, "@@eta 300")
|
|
self.assertIsNotNone(w.compile_started)
|
|
self.assertEqual(w.eta, 300)
|
|
on_line(w, "@@stage shaders Preparing shaders")
|
|
self.assertIsNone(w.compile_started)
|
|
self.assertIsNone(w.eta)
|
|
self.assertEqual(text[-1], "Preparing shaders")
|
|
|
|
def test_the_not_prepared_line_is_noted(self):
|
|
on_line = builder_method("on_line")
|
|
w, _ = self.window()
|
|
on_line(w, "the shaders were not prepared now (no usable Vulkan here): the game prepares them at its first start")
|
|
self.assertTrue(w.shaders_later)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|