mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 13:18:37 +00:00
At start the launcher runs git fetch in its folder. When the clone is behind its upstream, the Play page says how many new commits there are and shows an Update button. Update runs this file's own --update mode: git pull --ff-only, then a rebuild of the runtime when d3d12/ changed, with the output streamed like the other tools. A copy downloaded as a zip has no .git; the launcher says that updating needs a git clone. A Settings box turns the check off. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
257 lines
13 KiB
Python
257 lines
13 KiB
Python
"""tools/leonida_launcher.py: settings, the command line and the mod list (no display, no game data).
|
|
|
|
python -I tests/test_leonida_launcher.py -v
|
|
"""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tools"))
|
|
import leonida_launcher as launcher # noqa: E402
|
|
|
|
|
|
def settings(**changes):
|
|
value = dict(launcher.DEFAULTS, game_dir="D:/Games/GTA")
|
|
value.update(changes)
|
|
return value
|
|
|
|
|
|
class SettingsTests(unittest.TestCase):
|
|
def test_round_trip_and_line_endings(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = os.path.join(tmp, "sub", "launcher.json")
|
|
launcher.save_settings(settings(dlss=False, resolution="1080", data_dir="E:/d"), path)
|
|
with open(path, "rb") as f:
|
|
raw = f.read()
|
|
self.assertNotIn(b"\r", raw)
|
|
self.assertTrue(raw.endswith(b"}\n"))
|
|
loaded = launcher.load_settings(path)
|
|
self.assertFalse(loaded["dlss"])
|
|
self.assertEqual((loaded["resolution"], loaded["data_dir"]), ("1080", "E:/d"))
|
|
|
|
def test_missing_or_broken_file_gives_defaults(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
self.assertEqual(launcher.load_settings(os.path.join(tmp, "none.json")), launcher.DEFAULTS)
|
|
bad = os.path.join(tmp, "bad.json")
|
|
Path(bad).write_text("{not json", encoding="utf-8")
|
|
self.assertEqual(launcher.load_settings(bad), launcher.DEFAULTS)
|
|
|
|
def test_wrong_types_and_unknown_keys_are_dropped(self):
|
|
got = launcher.normalise({"dlss": "yes", "resolution": "999", "game_dir": 5, "extra": 1, "log": True})
|
|
self.assertEqual(got["dlss"], True)
|
|
self.assertEqual(got["resolution"], "")
|
|
self.assertEqual(got["game_dir"], "")
|
|
self.assertTrue(got["log"])
|
|
self.assertNotIn("extra", got)
|
|
self.assertEqual(launcher.normalise([1]), launcher.DEFAULTS)
|
|
|
|
def test_game_folder_check(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
self.assertFalse(launcher.game_exe_present(tmp))
|
|
Path(tmp, launcher.EXPECTED_EXE).write_bytes(b"")
|
|
self.assertTrue(launcher.game_exe_present(tmp))
|
|
self.assertFalse(launcher.game_exe_present(""))
|
|
|
|
|
|
class CommandTests(unittest.TestCase):
|
|
def test_default_windows_command(self):
|
|
command = launcher.play_command(settings(), repo="R", windows=True)
|
|
self.assertEqual(command[:6], ["powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File",
|
|
os.path.join("R", "scripts", "play-d3d12.ps1")])
|
|
self.assertEqual(command[6:], ["-Game", "D:/Games/GTA"])
|
|
|
|
def test_every_option_maps_to_its_switch(self):
|
|
command = launcher.play_command(
|
|
settings(data_dir="E:/d", dlss=False, ray_tracing=True, smooth_motion=True, precompile=False,
|
|
series_x=True, quality=True, resolution="1440", capture=True, log=True),
|
|
repo="R", windows=True)
|
|
self.assertEqual(command[6:], ["-Game", "D:/Games/GTA", "-Data", "E:/d", "-NoDlss", "-RayTracing",
|
|
"-SmoothMotion", "-NoPrecompile", "-SeriesX", "-Quality",
|
|
"-Resolution", "1440", "-Capture", "-Log"])
|
|
|
|
def test_the_newer_switches(self):
|
|
got = launcher.switch_args(settings(series_s=True, series_x=True, dlss_mode="balanced", frame_gen=3,
|
|
resolution="1080", aspect="21:9", present_scale="stretch",
|
|
adapter="5070"))
|
|
# Series S wins over Series X, and -Resolution is left out with a DLSS mode, as the script asks
|
|
self.assertEqual(got, ["-SeriesS", "-Dlss", "balanced", "-FrameGen", "3", "-Aspect", "21:9",
|
|
"-PresentScale", "stretch", "-Adapter", "5070"])
|
|
self.assertEqual(launcher.switch_args(settings(dlss=False, dlss_mode="quality", resolution="1080")),
|
|
["-NoDlss", "-Resolution", "1080"])
|
|
self.assertEqual(launcher.switch_args(settings(present_scale="stretch")), []) # only with -Aspect
|
|
|
|
def test_the_newer_settings_are_checked(self):
|
|
got = launcher.normalise({"frame_gen": False, "dlss_mode": "max", "aspect": "21:9; calc", "adapter": 'a"b',
|
|
"present_scale": "fill", "series_s": True, "series_x": True})
|
|
self.assertEqual((got["frame_gen"], got["dlss_mode"], got["aspect"], got["adapter"], got["present_scale"]),
|
|
(0, "", "", "", ""))
|
|
self.assertEqual((got["series_s"], got["series_x"]), (True, False))
|
|
kept = launcher.normalise({"frame_gen": 4, "dlss_mode": "ultra", "aspect": " auto ", "adapter": "#1"})
|
|
self.assertEqual((kept["frame_gen"], kept["dlss_mode"], kept["aspect"], kept["adapter"]),
|
|
(4, "ultra", "auto", "#1"))
|
|
|
|
def test_switches_exist_in_the_script(self):
|
|
text = (ROOT / "scripts" / "play-d3d12.ps1").read_text(encoding="utf-8")
|
|
every = settings(dlss=False, ray_tracing=True, smooth_motion=True, precompile=False, series_x=True,
|
|
quality=True, resolution="720", capture=True, log=True, aspect="32:9",
|
|
present_scale="fit", adapter="#1", frame_gen=2)
|
|
newer = settings(series_s=True, dlss_mode="quality", precompile_all=True)
|
|
for arg in launcher.switch_args(every) + launcher.switch_args(newer):
|
|
if arg.startswith("-") and arg[1:].isalpha():
|
|
self.assertIn("$" + arg[1:], text, arg)
|
|
self.assertIn("$Game", text)
|
|
self.assertIn("$Data", text)
|
|
|
|
def test_precompile_all_only_with_the_precompile(self):
|
|
self.assertIn("-PrecompileAll", launcher.switch_args(settings(precompile_all=True)))
|
|
off = launcher.switch_args(settings(precompile=False, precompile_all=True))
|
|
self.assertIn("-NoPrecompile", off)
|
|
self.assertNotIn("-PrecompileAll", off)
|
|
self.assertNotIn("-PrecompileAll", launcher.switch_args(settings()))
|
|
|
|
def test_linux_command(self):
|
|
command = launcher.play_command(settings(game_dir="/g", series_x=True, dlss=False), repo="/r",
|
|
windows=False)
|
|
self.assertEqual(command, ["sh", os.path.join("/r", "scripts", "play-linux.sh"),
|
|
os.path.join("/g", launcher.EXPECTED_EXE), "--series-x"])
|
|
|
|
def test_no_game_folder_is_refused(self):
|
|
with self.assertRaises(ValueError):
|
|
launcher.play_command(settings(game_dir=" "), windows=True)
|
|
|
|
def test_tools(self):
|
|
self.assertEqual(launcher.tool_command("get_dlss", repo="R", windows=True)[:2], ["cmd", "/c"])
|
|
self.assertTrue(launcher.tool_command("build", repo="R", windows=True)[-1].endswith("build-nvidia.ps1"))
|
|
with self.assertRaises(ValueError):
|
|
launcher.tool_command("build", windows=False)
|
|
with self.assertRaises(ValueError):
|
|
launcher.tool_command("nope", windows=True)
|
|
|
|
def test_state_follows_the_scripts_messages(self):
|
|
state = "Starting"
|
|
for line, want in (("Series S, 720p. F1 in the game: settings.\n", "Starting"),
|
|
("Precompiling shaders (minutes) ...\n", "Preparing shaders"),
|
|
(" 1000 shaders\n", "Preparing shaders"),
|
|
("log: x.log\n", "Running")):
|
|
state = launcher.state_for_line(state, line)
|
|
self.assertEqual(state, want, line)
|
|
self.assertEqual(launcher.state_for_line("Starting", "Building the layer (a few minutes) ...\n"),
|
|
"Building the runtime")
|
|
|
|
|
|
class ModsAndPicturesTests(unittest.TestCase):
|
|
def test_list_and_toggle_mods(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
mod = Path(tmp, "alpha")
|
|
mod.mkdir()
|
|
manifest = mod / "mod.json"
|
|
manifest.write_text(json.dumps({"name": "Alpha", "archetype_swaps": [{"from": "a_b", "to": "c_d"}]}),
|
|
encoding="utf-8")
|
|
Path(tmp, "broken").mkdir()
|
|
Path(tmp, "broken", "mod.json").write_text("{", encoding="utf-8")
|
|
Path(tmp, "notamod").mkdir()
|
|
rows, errors = launcher.list_mods(tmp)
|
|
self.assertEqual([(r["folder"], r["name"], r["enabled"], r["swaps"]) for r in rows],
|
|
[("alpha", "Alpha", True, 1)])
|
|
self.assertEqual(len(errors), 1)
|
|
launcher.set_mod_enabled(rows[0]["path"], False)
|
|
self.assertFalse(json.loads(manifest.read_text(encoding="utf-8"))["enabled"])
|
|
self.assertNotIn(b"\r", manifest.read_bytes())
|
|
self.assertFalse(launcher.list_mods(tmp)[0][0]["enabled"])
|
|
self.assertEqual(launcher.list_mods(os.path.join(tmp, "missing")), ([], []))
|
|
|
|
def test_newest_picture(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
old, new = Path(tmp, "a", "old.png"), Path(tmp, "b", "new.png")
|
|
old.parent.mkdir()
|
|
new.parent.mkdir()
|
|
old.write_bytes(b"x")
|
|
new.write_bytes(b"x")
|
|
Path(tmp, "note.txt").write_bytes(b"x")
|
|
os.utime(old, (1000, 1000))
|
|
os.utime(new, (2000, 2000))
|
|
self.assertEqual(launcher.newest_picture([tmp]), str(new))
|
|
self.assertIsNone(launcher.newest_picture([os.path.join(tmp, "none")]))
|
|
|
|
|
|
class FakeGit:
|
|
"""`git -C repo <args>` answered from a table of the first argument; records each call."""
|
|
|
|
def __init__(self, answers):
|
|
self.answers = answers
|
|
self.calls = []
|
|
|
|
def __call__(self, repo, *args, timeout=60):
|
|
self.calls.append(args)
|
|
code, out = self.answers.get(args[0], (0, ""))
|
|
if callable(out):
|
|
out = out(args)
|
|
return launcher.subprocess.CompletedProcess(["git", *args], code, out, "")
|
|
|
|
|
|
class UpdateTests(unittest.TestCase):
|
|
def repo(self, tmp, git=True):
|
|
if git:
|
|
os.mkdir(os.path.join(tmp, ".git"))
|
|
return tmp
|
|
|
|
def test_a_zip_copy_is_told_to_clone(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
status = launcher.update_status(self.repo(tmp, git=False), FakeGit({}))
|
|
self.assertEqual(status["kind"], "zip")
|
|
self.assertIn("git clone", launcher.update_note(status))
|
|
|
|
def test_behind_counts_the_new_commits_after_a_fetch(self):
|
|
git = FakeGit({"rev-list": (0, "3\n")})
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
status = launcher.update_status(self.repo(tmp), git)
|
|
self.assertEqual((status["kind"], status["behind"]), ("behind", 3))
|
|
self.assertIn(("fetch", "--quiet"), git.calls)
|
|
self.assertIn("3 new commits", launcher.update_note(status))
|
|
self.assertEqual(launcher.update_note({"kind": "current", "behind": 0}), "")
|
|
|
|
def test_a_failed_fetch_or_no_upstream_is_said_not_raised(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
repo = self.repo(tmp)
|
|
self.assertEqual(launcher.update_status(repo, FakeGit({"rev-parse": (128, "")}))["kind"], "noupstream")
|
|
failed = launcher.update_status(repo, FakeGit({"fetch": (128, "no network")}))
|
|
self.assertEqual(failed["kind"], "error")
|
|
self.assertIn("no network", launcher.update_note(failed))
|
|
|
|
def test_update_rebuilds_only_when_d3d12_moved(self):
|
|
heads = iter(["aaa", "bbb", "aaa", "bbb"])
|
|
for d3d12_changed in (False, True):
|
|
built = []
|
|
git = FakeGit({"rev-list": (0, "2"), "rev-parse": (0, lambda a: "origin/main" if "@{u}" in a else next(heads)),
|
|
"diff": (1 if d3d12_changed else 0, "")})
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
code = launcher.run_update(self.repo(tmp), git, build=lambda: built.append(1) or 0, windows=True)
|
|
self.assertEqual(code, 0)
|
|
self.assertIn(("pull", "--ff-only"), git.calls)
|
|
self.assertEqual(built, [1] if d3d12_changed else [])
|
|
|
|
def test_a_pull_that_cannot_fast_forward_stops_without_a_build(self):
|
|
built = []
|
|
git = FakeGit({"rev-list": (0, "1"), "pull": (128, "Not possible to fast-forward")})
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
code = launcher.run_update(self.repo(tmp), git, build=lambda: built.append(1) or 0, windows=True)
|
|
self.assertNotEqual(code, 0)
|
|
self.assertEqual(built, [])
|
|
|
|
def test_the_update_command_is_this_file_in_isolated_mode(self):
|
|
command = launcher.update_command("C:/mandy")
|
|
self.assertEqual(command[1:2] + command[3:], ["-I", "--update", "C:/mandy"])
|
|
self.assertTrue(command[2].endswith("leonida_launcher.py"))
|
|
|
|
def test_the_check_can_be_turned_off(self):
|
|
self.assertTrue(launcher.DEFAULTS["check_updates"])
|
|
self.assertFalse(launcher.normalise({"check_updates": False})["check_updates"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|