366 lines
22 KiB
Python
366 lines
22 KiB
Python
"""A whole update the way the app does it (desktop/PhantomGladiator.cs UpdateClient + desktop/AppUpdate.cs), on
|
|
this machine: an "installed" folder with version 0.40.11 checks a local fake server, downloads the 0.40.12 zip,
|
|
verifies its SHA-256 and installs it in place; the new exe waits for the old one, cleans up and reports. Then going
|
|
back: another folder at 0.40.11 picks 0.40.10 from versions.json, then updates forward again. Older versions run
|
|
in a separate installation.
|
|
The test exe stands in for PhantomGladiator.exe in every version (tools/test_update_install.cs as the entry point).
|
|
Needs a folder with the WebView2 assemblies, by default the newest built dist/PhantomGladiator-* folder:
|
|
|
|
python -I tools/test_update_install.py [webview2 folder]
|
|
"""
|
|
import hashlib, http.server, io, json, os, pathlib, shutil, subprocess, sys, tempfile, threading, time, zipfile
|
|
from urllib.parse import urlsplit
|
|
|
|
ROOT = pathlib.Path(__file__).resolve().parent.parent
|
|
CSC = pathlib.Path(r"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe")
|
|
REFS = ["System.dll", "System.Core.dll", "System.Drawing.dll", "System.Windows.Forms.dll", "System.Web.Extensions.dll",
|
|
"System.IO.Compression.dll", "System.IO.Compression.FileSystem.dll"]
|
|
WEBVIEW2 = ["Microsoft.Web.WebView2.Core.dll", "Microsoft.Web.WebView2.WinForms.dll"]
|
|
|
|
|
|
def webview2_folder():
|
|
if len(sys.argv) > 1:
|
|
return pathlib.Path(sys.argv[1])
|
|
built = [p for p in (ROOT / "dist").glob("PhantomGladiator-*") if (p / WEBVIEW2[0]).is_file()]
|
|
if not built:
|
|
sys.exit("no built dist/PhantomGladiator-* folder with the WebView2 assemblies; pass one")
|
|
return max(built, key=lambda p: p.stat().st_mtime)
|
|
|
|
|
|
def version_json(version, base, mod_build=None):
|
|
return json.dumps({"appVersion": version, "modBuild": mod_build or "v" + version, "verifiedGameBuild": "0", "repo": "test/test",
|
|
"manifests": [f"local|{base}/latest.json"]})
|
|
|
|
|
|
def main():
|
|
files = {}
|
|
|
|
class Handler(http.server.BaseHTTPRequestHandler):
|
|
def log_message(self, *args):
|
|
pass
|
|
|
|
def do_HEAD(self):
|
|
self.send_response(200)
|
|
self.end_headers()
|
|
|
|
def do_GET(self):
|
|
body = files.get(urlsplit(self.path).path)
|
|
self.send_response(200 if body is not None else 404)
|
|
self.end_headers()
|
|
if body is not None:
|
|
self.wfile.write(body)
|
|
|
|
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler)
|
|
base = f"http://127.0.0.1:{server.server_address[1]}"
|
|
threading.Thread(target=server.serve_forever, daemon=True).start()
|
|
wv = webview2_folder()
|
|
work = pathlib.Path(tempfile.mkdtemp(prefix="pg-install-"))
|
|
try:
|
|
exe = work / "PhantomGladiator.exe"
|
|
subprocess.run([str(CSC), "/nologo", "/target:exe", "/platform:x64", "/utf8output", "/codepage:65001",
|
|
"/main:PhantomGladiator.UpdateInstallTest", f"/out:{exe}", *[f"/r:{r}" for r in REFS],
|
|
*[f"/r:{wv / dll}" for dll in WEBVIEW2], str(ROOT / "desktop/PhantomGladiator.cs"),
|
|
str(ROOT / "desktop/AppUpdate.cs"), str(ROOT / "desktop/ManualBeta.cs"), str(ROOT / "tools/test_update_install.cs")], check=True)
|
|
|
|
# A version as a release zip: one top folder holding the exe, its DLLs and app\.
|
|
def release(version, mod_build=None):
|
|
package = io.BytesIO()
|
|
with zipfile.ZipFile(package, "w") as z:
|
|
top = f"PhantomGladiator-{version}/"
|
|
z.write(exe, top + "PhantomGladiator.exe")
|
|
for dll in WEBVIEW2:
|
|
z.write(wv / dll, top + dll)
|
|
z.writestr(top + "app/version.json", version_json(version, base, mod_build))
|
|
z.writestr(top + "app/python/lib.txt", version)
|
|
z.writestr(top + "licenses/new.txt", "B")
|
|
package = package.getvalue()
|
|
files[f"/pkg-{version}.zip"] = package
|
|
return {"version": version, "date": "2026-11-02T00:00:00+08:00", "size": len(package),
|
|
"sha256": hashlib.sha256(package).hexdigest(), "notes": {"zh": ["test"]},
|
|
"downloads": [{"id": "github", "url": f"{base}/pkg-{version}.zip"}],
|
|
"parts": split(version, package)}
|
|
|
|
def split(version, package):
|
|
parts = []
|
|
for at in range(0, len(package), 1024 * 1024):
|
|
piece, name = package[at:at + 1024 * 1024], f"{version}.{at // (1024 * 1024):03d}"
|
|
files[f"/parts/{name}"] = piece
|
|
parts.append({"name": name, "size": len(piece), "sha256": hashlib.sha256(piece).hexdigest()})
|
|
return {"files": parts, "sources": [{"id": "local", "url": f"{base}/parts/"}]}
|
|
|
|
def publish(entry):
|
|
files["/latest.json"] = json.dumps(entry).encode("utf-8")
|
|
files["/release-info.json"] = json.dumps({"tag_name": "v" + entry["version"], "draft": False, "prerelease": False,
|
|
"published_at": entry["date"], "assets": [{"name": "PhantomGladiator-" + entry["version"] + "-win64.zip",
|
|
"size": entry["size"], "digest": "sha256:" + entry["sha256"],
|
|
"browser_download_url": entry["downloads"][0]["url"]}]}).encode("utf-8")
|
|
|
|
publish(release("0.40.12"))
|
|
files["/versions.json"] = json.dumps({"versions": [release("0.40.11"), release("0.40.10"), release("0.40.9")]}).encode("utf-8")
|
|
|
|
def setup(name):
|
|
# The installed old version, with a file of the player's own and what versions before 0.37.6 left behind.
|
|
installed, data = work / name, work / (name + "-data")
|
|
(installed / "app" / "python").mkdir(parents=True)
|
|
(installed / "licenses").mkdir()
|
|
shutil.copy2(exe, installed / "PhantomGladiator.exe")
|
|
for dll in WEBVIEW2:
|
|
shutil.copy2(wv / dll, installed / dll)
|
|
(installed / "app" / "version.json").write_text(version_json("0.40.11", base), encoding="utf-8")
|
|
(installed / "app" / "python" / "lib.txt").write_text("A", encoding="utf-8")
|
|
(installed / "licenses" / "old.txt").write_text("A", encoding="utf-8")
|
|
(installed / "my-notes.txt").write_text("mine", encoding="utf-8")
|
|
(data / "updates").mkdir(parents=True)
|
|
(data / "settings.json").write_text('{"keep":"personal-settings"}', encoding="utf-8")
|
|
(data / "updates" / "updater.exe").write_bytes(b"left by an old update")
|
|
(data / "updates" / "Microsoft.Web.WebView2.Core.dll").write_bytes(b"left by an old update")
|
|
return installed, data
|
|
|
|
for case in ("missing", "bad-config", "registration-error", "success"):
|
|
installed, data = setup("components-" + case)
|
|
game = installed / "test-game"
|
|
ue4ss = game / "platform" / "steam" / "ue4ss"
|
|
mods = ue4ss / "Mods"
|
|
mod = mods / "PhantomGladiator"
|
|
live = mod / "Scripts"
|
|
live.mkdir(parents=True)
|
|
assert work.resolve() in live.resolve().parents
|
|
(mods / "shared").mkdir()
|
|
(ue4ss / "UE4SS.dll").write_bytes(b"test loader")
|
|
(ue4ss.parent / "dwmapi.dll").write_bytes(b"test proxy")
|
|
(live / "main.lua").write_text("old scripts", encoding="utf-8")
|
|
(live / "obsolete.lua").write_text("old only", encoding="utf-8")
|
|
runtime = mod / "Runtime"
|
|
runtime.mkdir()
|
|
(runtime / "catalog.tsv").write_text("existing catalog", encoding="utf-8")
|
|
(runtime / "monster-pools.tsv").write_text("existing pools", encoding="utf-8")
|
|
(runtime / "unit-keybinds.tsv").write_text("existing unit keys", encoding="utf-8")
|
|
mods_txt = mods / "mods.txt"
|
|
if case == "registration-error":
|
|
mods_txt.mkdir()
|
|
else:
|
|
mods_txt.write_text("OtherMod : 1\n", encoding="utf-8")
|
|
payload = installed / "app" / "payload"
|
|
(payload / "gameconfig").mkdir(parents=True)
|
|
(payload / "seed").mkdir()
|
|
(payload / "seed" / "catalog.tsv").write_text("shipped seed", encoding="utf-8")
|
|
if case != "missing":
|
|
scripts = payload / "mod" / "PhantomGladiator" / "Scripts"
|
|
scripts.mkdir(parents=True)
|
|
(scripts / "main.lua").write_text("new scripts", encoding="utf-8")
|
|
config = 'Config.Other = "bad"' if case == "bad-config" else 'Config.RuntimePath = "old path"'
|
|
(scripts / "config.lua").write_text(config + "\n", encoding="utf-8")
|
|
result = subprocess.run([str(installed / "PhantomGladiator.exe"), "--install-components"],
|
|
env=dict(os.environ, PG_DATA_DIR=str(data)), capture_output=True, timeout=30)
|
|
if case == "success":
|
|
assert result.returncode == 0, result.stderr
|
|
assert (live / "main.lua").read_text(encoding="utf-8") == "new scripts"
|
|
assert not (live / "obsolete.lua").exists()
|
|
assert f'Config.RuntimePath = "{runtime.as_posix()}"' in (live / "config.lua").read_text(encoding="utf-8")
|
|
assert mods_txt.read_text(encoding="utf-8").splitlines() == ["OtherMod : 1", "PhantomGladiator : 1"]
|
|
else:
|
|
assert result.returncode != 0, case
|
|
assert (live / "main.lua").is_file(), f"{case}: install removed the old scripts"
|
|
assert (live / "main.lua").read_text(encoding="utf-8") == "old scripts"
|
|
assert (live / "obsolete.lua").is_file()
|
|
assert (runtime / "catalog.tsv").read_text(encoding="utf-8") == "existing catalog"
|
|
assert (runtime / "monster-pools.tsv").read_text(encoding="utf-8") == "existing pools"
|
|
assert (runtime / "unit-keybinds.tsv").read_text(encoding="utf-8") == "existing unit keys"
|
|
assert not list(mod.glob(".pg-install-*")), case
|
|
print(f"PASS components {case}: read back scripts and Runtime")
|
|
|
|
def uninstall_fixture(case):
|
|
installed, data = setup("uninstall-" + case)
|
|
steam = installed / "test-game" / "platform" / "steam"
|
|
mods = steam / "ue4ss" / "Mods"
|
|
mod = mods / "PhantomGladiator"
|
|
live = mod / "Scripts"
|
|
live.mkdir(parents=True)
|
|
assert work.resolve() in live.resolve().parents
|
|
(live / "main.lua").write_bytes(b'local BUILD = "v0.40.23"')
|
|
(live / "config.lua").write_bytes(b"personal options")
|
|
(mod / "enabled.txt").touch()
|
|
protected = {mod / "Runtime" / "function-keybinds.tsv": b"personal shortcuts",
|
|
mod / "Runtime" / "hun-profile.json": b"personal profile",
|
|
mod / "keep.txt": b"personal mod file",
|
|
mods / "OtherMod" / "Scripts" / "main.lua": b"some other mod",
|
|
mods / "shared" / "UEHelpers.lua": b"shared helper",
|
|
steam / "ue4ss" / "UE4SS.dll": b"existing loader",
|
|
steam / "ue4ss" / "MemberVariableLayout.ini": b"existing game layout",
|
|
steam / "dwmapi.dll": b"existing proxy",
|
|
data / "settings.json": b"desktop settings"}
|
|
for path, body in protected.items():
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_bytes(body)
|
|
txt, js = mods / "mods.txt", mods / "mods.json"
|
|
old_txt = b"\xef\xbb\xbfothermod : 1\r\n; preserve \xdb\r\n PhantomGladiator : 1\r\nphantomgladiator : 0\r\n"
|
|
old_json = [{"mod_name": "OtherMod", "mod_enabled": True, "extra": [1, 2]},
|
|
{"mod_name": "PhantomGladiator", "mod_enabled": True}]
|
|
txt.write_bytes(old_txt)
|
|
js.write_text(json.dumps(old_json), encoding="utf-8")
|
|
app = installed / "PhantomGladiator.exe"
|
|
env = dict(os.environ, PG_DATA_DIR=str(data))
|
|
|
|
def call(mode):
|
|
return subprocess.run([str(app), mode], env=env, capture_output=True, timeout=20)
|
|
if case == "linked":
|
|
elsewhere = installed / "outside-mod"
|
|
live.rename(elsewhere)
|
|
literal = lambda path: "'" + str(path).replace("'", "''") + "'"
|
|
subprocess.run(["powershell", "-NoProfile", "-NonInteractive", "-Command",
|
|
f"New-Item -ItemType Junction -Path {literal(live)} -Target {literal(elsewhere)} -ErrorAction Stop | Out-Null"],
|
|
check=True, capture_output=True)
|
|
result = call("--uninstall-components")
|
|
assert result.returncode and b"uninstallPath" in result.stderr, result.stderr
|
|
assert (elsewhere / "main.lua").is_file() and txt.read_bytes() == old_txt
|
|
assert not (steam / "PhantomGladiator-backups").exists()
|
|
live.rmdir()
|
|
print("PASS uninstall refuses a linked Scripts folder before touching its target or loader entries")
|
|
return
|
|
if case == "locked":
|
|
result = call("--uninstall-components-locked")
|
|
assert result.returncode, result.stdout
|
|
assert live.is_dir() and (mod / "enabled.txt").exists(), result.stderr
|
|
assert txt.read_bytes() == old_txt and json.loads(js.read_text()) == old_json
|
|
print("PASS uninstall rollback restores scripts, enabled marker and first registry after the second is locked")
|
|
elif case == "running":
|
|
mock_game = steam / "PBZ.exe"
|
|
shutil.copy2(exe, mock_game)
|
|
process = subprocess.Popen([str(mock_game), "--hold-fixture"], env=env, creationflags=subprocess.CREATE_NO_WINDOW)
|
|
try:
|
|
result = call("--uninstall-components")
|
|
assert result.returncode and b"gameRunning" in result.stderr, result.stderr
|
|
assert live.is_dir() and txt.read_bytes() == old_txt and (mod / "enabled.txt").exists()
|
|
assert not (steam / "PhantomGladiator-backups").exists()
|
|
print("PASS uninstall refuses to change files while a process from the chosen game directory is running")
|
|
finally:
|
|
process.terminate()
|
|
process.wait(timeout=5)
|
|
result = call("--uninstall-components")
|
|
assert result.returncode == 0, result.stderr
|
|
backup = pathlib.Path(result.stdout.decode("utf-8").strip())
|
|
assert backup.parent == steam / "PhantomGladiator-backups"
|
|
assert not live.exists() and not (mod / "enabled.txt").exists()
|
|
assert (backup / "Scripts" / "config.lua").read_bytes() == b"personal options"
|
|
assert (backup / "enabled.txt").is_file() and (backup / "mods.txt.before").read_bytes() == old_txt
|
|
assert txt.read_bytes() == b"\xef\xbb\xbfothermod : 1\r\n; preserve \xdb\r\n"
|
|
assert json.loads(js.read_text()) == old_json[:1]
|
|
assert all(path.read_bytes() == body for path, body in protected.items())
|
|
result = call("--uninstall-components")
|
|
assert result.returncode == 0 and not result.stdout.strip(), result.stderr
|
|
if case == "legacy":
|
|
payload = installed / "app" / "payload"
|
|
(payload / "seed").mkdir(parents=True)
|
|
(payload / "gameconfig").mkdir()
|
|
shipped = payload / "mod" / "PhantomGladiator" / "Scripts"
|
|
shipped.mkdir(parents=True)
|
|
(shipped / "main.lua").write_text('local BUILD = "v0.40.25"', encoding="utf-8")
|
|
(shipped / "config.lua").write_text('Config.RuntimePath = "unused"\n', encoding="utf-8")
|
|
result = call("--install-components")
|
|
assert result.returncode == 0, result.stderr
|
|
assert 'v0.40.25' in (live / "main.lua").read_text(encoding="utf-8")
|
|
assert all(path.read_bytes() == body for path, body in protected.items())
|
|
result = call("--uninstall-components")
|
|
assert result.returncode == 0 and pathlib.Path(result.stdout.decode("utf-8").strip()) != backup
|
|
assert (backup / "Scripts" / "config.lua").read_bytes() == b"personal options"
|
|
print("PASS legacy uninstall, repeat uninstall and reinstall preserve unrelated files and old data")
|
|
|
|
for case in ("legacy", "locked", "running", "linked"):
|
|
uninstall_fixture(case)
|
|
|
|
def run_app(installed, data, back="", line=""):
|
|
report = work / "report.txt"
|
|
report.unlink(missing_ok=True)
|
|
env = dict(os.environ, PG_DATA_DIR=str(data), PG_TEST_REPORT=str(report), PG_TEST_ROLLBACK=back, PG_TEST_LINE=line,
|
|
PG_TEST_RELEASE_API=base + "/release-info.json")
|
|
run = subprocess.run([str(installed / "PhantomGladiator.exe")], env=env, timeout=120)
|
|
for _ in range(300):
|
|
if report.exists():
|
|
break
|
|
time.sleep(0.1)
|
|
assert report.exists(), f"no report (old exe exit code {run.returncode})"
|
|
time.sleep(0.3)
|
|
lines = report.read_text(encoding="utf-8").splitlines()
|
|
assert run.returncode == 0 and lines[0] == "ok", "\n".join(lines)
|
|
assert (data / "settings.json").read_text(encoding="utf-8") == '{"keep":"personal-settings"}'
|
|
return lines
|
|
|
|
installed, data = setup("installed")
|
|
lines = run_app(installed, data)
|
|
text = "\n".join(lines)
|
|
assert "version=0.40.12" in lines, text
|
|
assert (installed / "app" / "python" / "lib.txt").read_text(encoding="utf-8") == "0.40.12"
|
|
assert "licenses/new.txt" in lines and "licenses/old.txt" not in lines, text
|
|
assert "my-notes.txt" in lines, "a file of the player's own stays"
|
|
assert not any(line.startswith(".pg-app-swap-") for line in lines), "the new exe cleaned its completed backup"
|
|
updates = next(line for line in lines if line.startswith("updates=")).split("=", 1)[1].split(",")
|
|
assert updates == ["PhantomGladiator-0.40.12.zip"], f"the old updater copy and the unpacked files are gone: {updates}"
|
|
print("PASS a whole update: check, download, SHA-256, in-place install, the new exe takes over and cleans up")
|
|
|
|
installed, data = setup("rolled-back")
|
|
lines = run_app(installed, data, back="0.40.10")
|
|
text = "\n".join(lines)
|
|
assert "version=0.40.10" in lines, text
|
|
assert (installed / "app" / "python" / "lib.txt").read_text(encoding="utf-8") == "0.40.10"
|
|
assert "my-notes.txt" in lines, "a file of the player's own stays"
|
|
assert not any(line.startswith(".pg-app-swap-") for line in lines), text
|
|
print("PASS going back: the older version is picked from versions.json and installed the same way")
|
|
lines = run_app(installed, data)
|
|
assert "version=0.40.12" in lines, "\n".join(lines)
|
|
assert (installed / "app" / "python" / "lib.txt").read_text(encoding="utf-8") == "0.40.12"
|
|
print("PASS and from the older version the player updates forward again")
|
|
|
|
installed, data = setup("legacy-rollback")
|
|
(installed / "my-backup.old").write_text("mine", encoding="utf-8")
|
|
(installed / "notes.old").mkdir()
|
|
(installed / "notes.old" / "note.txt").write_text("mine", encoding="utf-8")
|
|
lines = run_app(installed, data, back="0.40.9")
|
|
directory = next(line.split("=", 1)[1] for line in lines if line.startswith("directory="))
|
|
assert "version=0.40.9" in lines
|
|
assert pathlib.Path(directory).parent.resolve() == (data / "versions").resolve()
|
|
assert json.loads((installed / "app" / "version.json").read_text(encoding="utf-8"))["appVersion"] == "0.40.11"
|
|
assert (installed / "my-backup.old").read_text(encoding="utf-8") == "mine"
|
|
assert (installed / "notes.old" / "note.txt").read_text(encoding="utf-8") == "mine"
|
|
print("PASS legacy rollback: old exe runs in its own directory and the original backups stay")
|
|
|
|
installed, data = setup("split")
|
|
lines = run_app(installed, data, line="parts")
|
|
text = "\n".join(lines)
|
|
assert "version=0.40.12" in lines, text
|
|
assert (installed / "app" / "python" / "lib.txt").read_text(encoding="utf-8") == "0.40.12"
|
|
updates = next(line for line in lines if line.startswith("updates=")).split("=", 1)[1].split(",")
|
|
assert updates == ["PhantomGladiator-0.40.12.zip"], f"the parts are joined and gone: {updates}"
|
|
print("PASS a split download joins, passes SHA-256 and installs the same way")
|
|
|
|
installed, data = setup("same-version-beta")
|
|
(installed / "app/version.json").write_text(version_json("0.40.12", base, "v0.40.12-beta-3"), encoding="utf-8")
|
|
lines = run_app(installed, data, line="parts")
|
|
assert "version=0.40.12" in lines and "build=v0.40.12" in lines, "\n".join(lines)
|
|
assert "directory=" + str(installed) in lines, "stable promotion must stay in the beta app's current directory"
|
|
assert (installed / "my-notes.txt").read_text(encoding="utf-8") == "mine"
|
|
print("PASS same-version beta promotes to stable in place, with personal files preserved")
|
|
|
|
installed, data = setup("bad-stable-package")
|
|
(installed / "app/version.json").write_text(version_json("0.40.12", base, "v0.40.12-beta-3"), encoding="utf-8")
|
|
publish(release("0.40.12", "v0.40.12-beta-99"))
|
|
report = work / "bad-stable-report.txt"
|
|
run = subprocess.run([str(installed / "PhantomGladiator.exe")], timeout=60,
|
|
env=dict(os.environ, PG_DATA_DIR=str(data), PG_TEST_REPORT=str(report), PG_TEST_RELEASE_API=base + "/release-info.json"))
|
|
assert run.returncode != 0 and "目标包不是对应的正式构建" in report.read_text(encoding="utf-8")
|
|
assert json.loads((installed / "app/version.json").read_text(encoding="utf-8"))["modBuild"] == "v0.40.12-beta-3"
|
|
assert (installed / "my-notes.txt").read_text(encoding="utf-8") == "mine"
|
|
print("PASS a beta package mislabeled as stable is rejected before replacing the existing app")
|
|
finally:
|
|
server.shutdown()
|
|
for _ in range(20):
|
|
try:
|
|
shutil.rmtree(work)
|
|
break
|
|
except OSError:
|
|
time.sleep(0.5)
|
|
print("update_install: PASS")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|