mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2026-11-19 17:05:20 +08:00
feat(livestreamassist): add resilient JavaScript overlay client
Add a JSON-RPC client, session-aware game queries, and generated offline locale data. Update research and stats overlays with cancellable single-flight polling and reconnect handling. Add 41 JavaScript regression tests, Python locale and formatting checks, and generation and validation documentation.
This commit is contained in:
1 parent
92e6511d91
commit
f03d6c758e
25 files changed
+3330
-70
No files matched your search
@@ -0,0 +1,4 @@
|
||||
from generate_text import main
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,382 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Convert DSP Locale files and prototype tables into LiveStreamAssist JS text data."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
DSP_APPID = "1366540"
|
||||
DEFAULT_UNITY_VERSION = "2022.3.7"
|
||||
KIND_FROM_SET = {
|
||||
"ItemProtoSet": "item",
|
||||
"TechProtoSet": "tech",
|
||||
"RecipeProtoSet": "recipe",
|
||||
"SignalProtoSet": "signal",
|
||||
"VeinProtoSet": "vein",
|
||||
"EnemyProtoSet": "enemy",
|
||||
"ModelProtoSet": "model",
|
||||
"ThemeProtoSet": "theme",
|
||||
"AchievementProtoSet": "achievement",
|
||||
"TutorialProtoSet": "tutorial",
|
||||
"MilestoneProtoSet": "milestone",
|
||||
"GoalProtoSet": "goal",
|
||||
"AudioProtoSet": "audio",
|
||||
"PromptProtoSet": "prompt",
|
||||
"AdvisorTipProtoSet": "advisorTip",
|
||||
"JournalPatternProtoSet": "journalPattern",
|
||||
"CosmicMessageProtoSet": "cosmicMessage",
|
||||
"DoodadProtoSet": "doodad",
|
||||
"AbnormalityProtoSet": "abnormality",
|
||||
"FleetProtoSet": "fleet",
|
||||
"CreationPartProtoSet": "creationPart",
|
||||
"EffectEmitterProtoSet": "effectEmitter",
|
||||
"MIDIProtoSet": "midi",
|
||||
"PlayerProtoSet": "player",
|
||||
"VegeProtoSet": "vege",
|
||||
}
|
||||
EXTRA_STRING_KEYS = ("千分分隔符", "小数点", "杠等级")
|
||||
|
||||
|
||||
def unescape_string(text: str) -> str:
|
||||
out = []
|
||||
i = 0
|
||||
while i < len(text):
|
||||
ch = text[i]
|
||||
if ch == "\\" and i + 1 < len(text):
|
||||
i += 1
|
||||
nxt = text[i]
|
||||
mapping = {"\\": "\\", "r": "\r", "n": "\n", "t": "\t", "v": "\v", "f": "\f"}
|
||||
if nxt in mapping:
|
||||
out.append(mapping[nxt])
|
||||
else:
|
||||
out.append("\\")
|
||||
out.append(nxt)
|
||||
else:
|
||||
out.append(ch)
|
||||
i += 1
|
||||
return "".join(out)
|
||||
|
||||
|
||||
def first_field(line: str) -> str:
|
||||
tab = line.find("\t")
|
||||
raw = line if tab < 0 else line[:tab]
|
||||
return unescape_string(raw)
|
||||
|
||||
|
||||
def translation_field(line: str) -> str | None:
|
||||
tab1 = line.find(" ")
|
||||
if tab1 < 0:
|
||||
return None
|
||||
tab2 = line.find(" ", tab1 + 1)
|
||||
if tab2 < 0:
|
||||
return None
|
||||
tab3 = line.find(" ", tab2 + 1)
|
||||
if tab3 < 0:
|
||||
return None
|
||||
return unescape_string(line[tab3 + 1 :])
|
||||
|
||||
|
||||
def read_locale_text(path: Path) -> str:
|
||||
data = path.read_bytes()
|
||||
if data.startswith((b'\xff\xfe', b'\xfe\xff')):
|
||||
return data.decode('utf-16')
|
||||
if data.startswith(b'\xef\xbb\xbf'):
|
||||
return data.decode('utf-8-sig')
|
||||
try:
|
||||
return data.decode('utf-8')
|
||||
except UnicodeDecodeError:
|
||||
return data.decode('utf-16')
|
||||
|
||||
|
||||
def parse_header(header_path: Path) -> tuple[list[dict], list[str]]:
|
||||
text = read_locale_text(header_path)
|
||||
lines = text.splitlines()
|
||||
languages: list[dict] = []
|
||||
page_order: dict[str, int] = {}
|
||||
in_langs = False
|
||||
for line in lines:
|
||||
stripped = line.strip()
|
||||
if stripped.startswith("Version="):
|
||||
in_langs = True
|
||||
continue
|
||||
if in_langs and stripped and "," in stripped and not stripped.startswith("[") and "=" not in stripped.split(",", 1)[0]:
|
||||
parts = stripped.split(",")
|
||||
if len(parts) < 2:
|
||||
continue
|
||||
try:
|
||||
lcid = int(parts[0])
|
||||
except ValueError:
|
||||
continue
|
||||
if lcid <= 0 or any(lang["lcid"] == lcid for lang in languages):
|
||||
continue
|
||||
languages.append(
|
||||
{
|
||||
"lcid": lcid,
|
||||
"name": parts[1],
|
||||
"abbr": parts[2] if len(parts) > 2 else parts[1].lower(),
|
||||
"abbr2": parts[3] if len(parts) > 3 else (parts[2] if len(parts) > 2 else parts[1].lower()),
|
||||
"fallback": int(parts[4]) if len(parts) > 4 and parts[4].isdigit() else 0,
|
||||
}
|
||||
)
|
||||
continue
|
||||
if "=" in stripped:
|
||||
key, _, value = stripped.partition("=")
|
||||
key = key.strip()
|
||||
try:
|
||||
page_order[key] = int(value.strip())
|
||||
except ValueError:
|
||||
continue
|
||||
pages = sorted(page_order, key=page_order.get)
|
||||
return languages, [name + ".txt" for name in pages]
|
||||
|
||||
|
||||
def load_name_keys(names_dir: Path, pages: list[str]) -> list[str]:
|
||||
keys: list[str] = []
|
||||
seen: set[str] = set()
|
||||
for page in pages:
|
||||
path = names_dir / page
|
||||
if not path.is_file():
|
||||
continue
|
||||
for line in read_locale_text(path).splitlines():
|
||||
if not line:
|
||||
continue
|
||||
key = first_field(line)
|
||||
if not key or key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
keys.append(key)
|
||||
return keys
|
||||
|
||||
|
||||
def load_language_strings(lang_dir: Path, pages: list[str], keys: list[str]) -> dict[str, str]:
|
||||
index = {key: i for i, key in enumerate(keys)}
|
||||
values: list[str | None] = [None] * len(keys)
|
||||
for page in pages:
|
||||
path = lang_dir / page
|
||||
if not path.is_file():
|
||||
continue
|
||||
for line in read_locale_text(path).splitlines():
|
||||
if not line:
|
||||
continue
|
||||
key = first_field(line)
|
||||
if key not in index:
|
||||
continue
|
||||
slot = index[key]
|
||||
if values[slot] is not None:
|
||||
continue
|
||||
translated = translation_field(line)
|
||||
if translated is None:
|
||||
continue
|
||||
values[slot] = translated
|
||||
result = {}
|
||||
for key, value in zip(keys, values):
|
||||
result[key] = key if value is None else value
|
||||
return result
|
||||
|
||||
|
||||
def steam_path() -> Path | None:
|
||||
if os.name != "nt":
|
||||
return None
|
||||
try:
|
||||
import winreg
|
||||
except ImportError:
|
||||
return None
|
||||
roots = [
|
||||
(winreg.HKEY_CURRENT_USER, r"Software\Valve\Steam"),
|
||||
(winreg.HKEY_LOCAL_MACHINE, r"Software\Valve\Steam"),
|
||||
(winreg.HKEY_LOCAL_MACHINE, r"Software\Wow6432Node\Valve\Steam"),
|
||||
]
|
||||
for hive, key in roots:
|
||||
try:
|
||||
with winreg.OpenKey(hive, key) as handle:
|
||||
value, _ = winreg.QueryValueEx(handle, "SteamPath")
|
||||
except OSError:
|
||||
continue
|
||||
if value:
|
||||
path = Path(str(value))
|
||||
if path.exists():
|
||||
return path
|
||||
return None
|
||||
|
||||
|
||||
def parse_library_folders(vdf_text: str) -> list[Path]:
|
||||
block_re = re.compile(r'"(?:\d+)"\s*\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}', re.S)
|
||||
path_re = re.compile(r'"path"\s+"([^"]+)"')
|
||||
app_re = re.compile(r'"' + DSP_APPID + r'"\s+"[^"]+"')
|
||||
paths = []
|
||||
for block in block_re.finditer(vdf_text):
|
||||
text = block.group(1)
|
||||
if not app_re.search(text):
|
||||
continue
|
||||
match = path_re.search(text)
|
||||
if not match:
|
||||
continue
|
||||
raw = match.group(1).replace("\\\\", "\\").replace("/", "\\")
|
||||
paths.append(Path(raw))
|
||||
return paths
|
||||
|
||||
|
||||
def find_game_root(explicit: str | None) -> Path | None:
|
||||
if explicit:
|
||||
path = Path(explicit)
|
||||
if (path / "DSPGAME_Data").is_dir():
|
||||
return path
|
||||
raise SystemExit(f"DSP game root not found at {path}")
|
||||
env = os.environ.get("DSP_ROOT") or os.environ.get("DSPGAME_ROOT")
|
||||
if env:
|
||||
path = Path(env)
|
||||
if (path / "DSPGAME_Data").is_dir():
|
||||
return path
|
||||
steam = steam_path()
|
||||
if steam is None:
|
||||
return None
|
||||
vdf = steam / "steamapps" / "libraryfolders.vdf"
|
||||
if not vdf.is_file():
|
||||
return None
|
||||
for library in parse_library_folders(vdf.read_text(encoding="utf-8", errors="ignore")):
|
||||
candidate = library / "steamapps" / "common" / "Dyson Sphere Program"
|
||||
if (candidate / "DSPGAME_Data").is_dir():
|
||||
return candidate
|
||||
fallback = steam / "steamapps" / "common" / "Dyson Sphere Program"
|
||||
if (fallback / "DSPGAME_Data").is_dir():
|
||||
return fallback
|
||||
return None
|
||||
|
||||
|
||||
def extract_proto_ids(game_root: Path, unity_version: str) -> dict[str, dict[str, str]]:
|
||||
try:
|
||||
import UnityPy
|
||||
from UnityPy.helpers.TypeTreeGenerator import TypeTreeGenerator
|
||||
except ImportError as exc:
|
||||
raise SystemExit(
|
||||
"UnityPy and TypeTreeGeneratorAPI are required to extract prototype IDs. "
|
||||
"Install LiveStreamAssist/js/scripts/requirements.txt."
|
||||
) from exc
|
||||
|
||||
data_dir = game_root / "DSPGAME_Data"
|
||||
generator = TypeTreeGenerator(unity_version)
|
||||
generator.load_local_game(str(game_root))
|
||||
ggm = UnityPy.load(str(data_dir / "globalgamemanagers.assets"))
|
||||
script_ids = {}
|
||||
for obj in ggm.objects:
|
||||
if getattr(obj.type, "name", str(obj.type)) != "MonoScript":
|
||||
continue
|
||||
script = obj.read()
|
||||
class_name = getattr(script, "m_ClassName", "")
|
||||
if class_name in KIND_FROM_SET:
|
||||
script_ids[obj.path_id] = class_name
|
||||
|
||||
resources = UnityPy.load(str(data_dir / "resources.assets"))
|
||||
nodes = {
|
||||
class_name: generator.get_nodes_up("Assembly-CSharp.dll", class_name)
|
||||
for class_name in KIND_FROM_SET
|
||||
}
|
||||
ids: dict[str, dict[str, str]] = {kind: {} for kind in KIND_FROM_SET.values()}
|
||||
for obj in resources.objects:
|
||||
if getattr(obj.type, "name", str(obj.type)) != "MonoBehaviour":
|
||||
continue
|
||||
try:
|
||||
peek = obj.read_typetree(check_read=False)
|
||||
except Exception:
|
||||
continue
|
||||
script = peek.get("m_Script") or {}
|
||||
class_name = script_ids.get(script.get("m_PathID"))
|
||||
if class_name is None:
|
||||
continue
|
||||
kind = KIND_FROM_SET[class_name]
|
||||
tree = obj.read_typetree(nodes[class_name], check_read=False)
|
||||
for proto in tree.get("dataArray") or []:
|
||||
proto_id = proto.get("ID")
|
||||
name = proto.get("Name")
|
||||
if not proto_id or not name:
|
||||
continue
|
||||
ids[kind][str(int(proto_id))] = name
|
||||
return {kind: mapping for kind, mapping in ids.items() if mapping}
|
||||
|
||||
|
||||
def collect_needed_keys(all_keys: list[str], ids: dict[str, dict[str, str]]) -> list[str]:
|
||||
needed = set(EXTRA_STRING_KEYS)
|
||||
for mapping in ids.values():
|
||||
needed.update(mapping.values())
|
||||
ordered = [key for key in all_keys if key in needed]
|
||||
for key in EXTRA_STRING_KEYS:
|
||||
if key not in ordered:
|
||||
ordered.append(key)
|
||||
return ordered
|
||||
|
||||
|
||||
def build_payload(game_root: Path, unity_version: str) -> dict:
|
||||
locale_dir = game_root / "Locale"
|
||||
header = locale_dir / "Header.txt"
|
||||
if not header.is_file():
|
||||
raise SystemExit(f"Locale header not found: {header}")
|
||||
languages, pages = parse_header(header)
|
||||
keys = load_name_keys(locale_dir / "Names", pages)
|
||||
ids = extract_proto_ids(game_root, unity_version)
|
||||
if not ids.get("item") or not ids.get("tech"):
|
||||
raise SystemExit("Prototype extraction did not produce item and tech IDs; generated data was not written.")
|
||||
needed_keys = collect_needed_keys(keys, ids)
|
||||
strings: dict[str, dict[str, str]] = {key: {} for key in needed_keys}
|
||||
language_meta = {}
|
||||
for lang in languages:
|
||||
lang_dir = locale_dir / str(lang["lcid"])
|
||||
if not lang_dir.is_dir():
|
||||
continue
|
||||
table = load_language_strings(lang_dir, pages, keys)
|
||||
language_meta[str(lang["lcid"])] = lang
|
||||
for key in needed_keys:
|
||||
strings[key][str(lang["lcid"])] = table.get(key, key)
|
||||
return {
|
||||
"version": "1.0.0",
|
||||
"languages": language_meta,
|
||||
"ids": ids,
|
||||
"strings": strings,
|
||||
}
|
||||
|
||||
|
||||
def emit_js(payload: dict) -> str:
|
||||
body = json.dumps(payload, ensure_ascii=False, separators=(",", ":"))
|
||||
return (
|
||||
"/* Generated from a local Dyson Sphere Program install by generate_text.py. Do not edit. */\n"
|
||||
"globalThis.LiveStreamAssistTextData = "
|
||||
+ body
|
||||
+ ";\n"
|
||||
)
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--game-root", help="DSP install directory containing DSPGAME_Data and Locale")
|
||||
parser.add_argument("--unity-version", default=DEFAULT_UNITY_VERSION)
|
||||
parser.add_argument(
|
||||
"--out",
|
||||
default=str(Path(__file__).resolve().parents[1] / "generated" / "dsp-text.js"),
|
||||
help="Output JS path",
|
||||
)
|
||||
parser.add_argument("--json-out", help="Optional JSON output path")
|
||||
args = parser.parse_args(argv)
|
||||
game_root = find_game_root(args.game_root)
|
||||
if game_root is None:
|
||||
raise SystemExit("Dyson Sphere Program install not found. Pass --game-root.")
|
||||
payload = build_payload(game_root, args.unity_version)
|
||||
out_path = Path(args.out)
|
||||
out_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
out_path.write_text(emit_js(payload), encoding="utf-8", newline="\n")
|
||||
if args.json_out:
|
||||
json_path = Path(args.json_out)
|
||||
json_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
json_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8", newline="\n")
|
||||
item_count = len(payload["ids"].get("item", {}))
|
||||
tech_count = len(payload["ids"].get("tech", {}))
|
||||
print(f"Wrote {out_path} (items={item_count}, techs={tech_count}, strings={len(payload['strings'])})")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,89 @@
|
||||
"""Reference implementation of DSP StringBuilderUtility KMG formatting."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
NUM = "0123456789"
|
||||
THIN_SPACE = "\u2009"
|
||||
|
||||
|
||||
def write_kmg(
|
||||
value,
|
||||
last_index: int = 8,
|
||||
blank: bool = False,
|
||||
blank_char: str = THIN_SPACE,
|
||||
fill_char: str = " ",
|
||||
decimal: str = ".",
|
||||
kind: str = "count",
|
||||
) -> str:
|
||||
buf = [fill_char] * (last_index + 1)
|
||||
n = int(value)
|
||||
sign = 0
|
||||
if n > 0:
|
||||
sign = 1
|
||||
elif n < 0:
|
||||
sign = -1
|
||||
n = -n
|
||||
pos = last_index
|
||||
if kind == "power":
|
||||
buf[pos] = "W"
|
||||
pos -= 1
|
||||
elif kind == "energy":
|
||||
buf[pos] = "J"
|
||||
pos -= 1
|
||||
threshold = 1000 if kind == "si1000" else 10000
|
||||
if n < threshold:
|
||||
if blank:
|
||||
buf[pos] = blank_char
|
||||
pos -= 1
|
||||
while n > 0:
|
||||
buf[pos] = NUM[n % 10]
|
||||
pos -= 1
|
||||
if pos < 0:
|
||||
return "".join(buf)
|
||||
n //= 10
|
||||
else:
|
||||
if n < 1_000_000:
|
||||
suffix, scale = "k", 1000
|
||||
elif n < 1_000_000_000:
|
||||
suffix, scale = "M", 1_000_000
|
||||
elif n < 1_000_000_000_000:
|
||||
suffix, scale = "G", 1_000_000_000
|
||||
elif n < 1_000_000_000_000_000:
|
||||
suffix, scale = "T", 1_000_000_000_000
|
||||
elif n < 1_000_000_000_000_000_000:
|
||||
suffix, scale = "P", 1_000_000_000_000_000
|
||||
else:
|
||||
suffix, scale = "E", 1_000_000_000_000_000_000
|
||||
buf[pos] = suffix
|
||||
pos -= 1
|
||||
if blank:
|
||||
buf[pos] = blank_char
|
||||
pos -= 1
|
||||
acc = 1
|
||||
while n >= 1000:
|
||||
n //= 10
|
||||
acc *= 10
|
||||
while n > 0:
|
||||
buf[pos] = NUM[n % 10]
|
||||
pos -= 1
|
||||
if pos < 0:
|
||||
return "".join(buf)
|
||||
n //= 10
|
||||
acc *= 10
|
||||
if acc == scale:
|
||||
buf[pos] = decimal
|
||||
pos -= 1
|
||||
if pos < 0:
|
||||
return "".join(buf)
|
||||
if sign == 0:
|
||||
buf[pos] = "0"
|
||||
pos -= 1
|
||||
elif sign < 0:
|
||||
buf[pos] = "-"
|
||||
pos -= 1
|
||||
return "".join(buf)
|
||||
|
||||
|
||||
def format_kmg(value, kind: str = "count", blank: bool = False, decimal: str = ".", trim: bool = True) -> str:
|
||||
raw = write_kmg(value, kind=kind, blank=blank, decimal=decimal)
|
||||
return raw.strip() if trim else raw
|
||||
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SRC = ROOT / "src"
|
||||
OUT = ROOT / "livestream-assist.js"
|
||||
parts = [
|
||||
SRC / "header.js",
|
||||
SRC / "format.js",
|
||||
SRC / "client.js",
|
||||
SRC / "query.js",
|
||||
SRC / "text.js",
|
||||
SRC / "footer.js",
|
||||
]
|
||||
text = "".join(path.read_text(encoding="utf-8") for path in parts)
|
||||
if not text.endswith("\n"):
|
||||
text += "\n"
|
||||
OUT.write_text(text, encoding="utf-8", newline="\n")
|
||||
print(f"Wrote {OUT} ({OUT.stat().st_size} bytes)")
|
||||
@@ -0,0 +1,2 @@
|
||||
UnityPy>=1.20
|
||||
TypeTreeGeneratorAPI>=0.0.10
|
||||
@@ -0,0 +1,221 @@
|
||||
const assert = require("node:assert/strict");
|
||||
const test = require("node:test");
|
||||
const { createHarness, observe, flush } = require("./test_helpers");
|
||||
|
||||
test("close cancels a connecting socket and permits an explicit reconnect", async () => {
|
||||
const { api, sockets } = createHarness();
|
||||
const client = new api.Client();
|
||||
const first = observe(client.connect());
|
||||
client.close();
|
||||
await flush();
|
||||
assert.equal(first.status, "rejected");
|
||||
assert.equal(first.error.kind, "DISCONNECTED");
|
||||
assert.equal(sockets[0].readyState, 3);
|
||||
const second = observe(client.connect());
|
||||
assert.equal(sockets.length, 2);
|
||||
sockets[1].open();
|
||||
await flush();
|
||||
assert.equal(second.status, "fulfilled");
|
||||
assert.equal(client.autoReconnect, true);
|
||||
client.close();
|
||||
});
|
||||
|
||||
test("a malformed URL does not poison later connections", async () => {
|
||||
const { api, sockets } = createHarness();
|
||||
const client = new api.Client({ url: "not a URL" });
|
||||
const failed = observe(client.connect());
|
||||
await flush();
|
||||
assert.equal(failed.status, "rejected");
|
||||
const retry = observe(client.connect("ws://localhost:18080/api/v1"));
|
||||
assert.equal(sockets.length, 1);
|
||||
sockets[0].open();
|
||||
await flush();
|
||||
assert.equal(retry.status, "fulfilled");
|
||||
client.close();
|
||||
});
|
||||
|
||||
test("connect with another URL replaces an open connection", async () => {
|
||||
const { api, sockets } = createHarness();
|
||||
const client = new api.Client();
|
||||
const first = client.connect();
|
||||
sockets[0].open();
|
||||
await first;
|
||||
const second = observe(client.connect("ws://localhost:19090/api/v1"));
|
||||
assert.equal(sockets[0].readyState, 3);
|
||||
assert.equal(sockets.length, 2);
|
||||
sockets[1].open();
|
||||
await flush();
|
||||
assert.equal(second.status, "fulfilled");
|
||||
client.close();
|
||||
});
|
||||
|
||||
test("disconnect rejects sent and queued calls without leaking capacity", async () => {
|
||||
const { api, sockets } = createHarness();
|
||||
const client = new api.Client({ maxPending: 1, autoReconnect: false });
|
||||
const requests = [observe(client.ping()), observe(client.ping()), observe(client.ping())];
|
||||
sockets[0].open();
|
||||
await flush();
|
||||
assert.equal(sockets[0].sent.length, 1);
|
||||
sockets[0].close(1006);
|
||||
await flush();
|
||||
for (const request of requests) {
|
||||
assert.equal(request.status, "rejected");
|
||||
assert.equal(request.error.kind, "DISCONNECTED");
|
||||
}
|
||||
assert.equal(client._active, 0);
|
||||
const retry = observe(client.ping());
|
||||
sockets[1].open();
|
||||
await flush();
|
||||
assert.equal(sockets[1].sent.length, 1);
|
||||
sockets[1].reply(sockets[1].sent[0], "fresh");
|
||||
await flush();
|
||||
assert.equal(retry.value, "fresh");
|
||||
client.close();
|
||||
});
|
||||
|
||||
test("requests do not implicitly reopen an explicitly closed client", async () => {
|
||||
const { api, sockets } = createHarness();
|
||||
const client = new api.Client();
|
||||
client.close();
|
||||
const request = observe(client.ping());
|
||||
await flush();
|
||||
assert.equal(request.status, "rejected");
|
||||
assert.equal(request.error.kind, "DISCONNECTED");
|
||||
assert.equal(sockets.length, 0);
|
||||
});
|
||||
|
||||
test("responses correlate by id and preserve Int64 strings", async () => {
|
||||
const { api, sockets } = createHarness();
|
||||
const client = new api.Client({ maxPending: 2 });
|
||||
const first = observe(client.ping());
|
||||
const second = observe(client.ping());
|
||||
sockets[0].open();
|
||||
await flush();
|
||||
sockets[0].reply(sockets[0].sent[1], "9223372036854775807");
|
||||
sockets[0].reply(sockets[0].sent[0], "first");
|
||||
await flush();
|
||||
assert.equal(first.value, "first");
|
||||
assert.equal(second.value, "9223372036854775807");
|
||||
assert.equal(client._active, 0);
|
||||
client.close();
|
||||
});
|
||||
|
||||
test("timeouts release the next queued call exactly once", async () => {
|
||||
const { api, sockets, clock } = createHarness();
|
||||
const client = new api.Client({ maxPending: 1, requestTimeoutMs: 50 });
|
||||
const first = observe(client.ping());
|
||||
const second = observe(client.ping());
|
||||
sockets[0].open();
|
||||
await flush();
|
||||
clock.advance(50);
|
||||
await flush();
|
||||
assert.equal(first.error.kind, "CLIENT_TIMEOUT");
|
||||
assert.equal(sockets[0].sent.length, 2);
|
||||
sockets[0].reply(sockets[0].sent[0], "late");
|
||||
sockets[0].reply(sockets[0].sent[1], "second");
|
||||
await flush();
|
||||
assert.equal(second.value, "second");
|
||||
assert.equal(client._active, 0);
|
||||
assert.equal(clock.size, 0);
|
||||
client.close();
|
||||
});
|
||||
|
||||
test("server limits respect a lower caller limit", async () => {
|
||||
const { api, sockets } = createHarness();
|
||||
const client = new api.Client({ maxPending: 1 });
|
||||
const info = client.systemInfo();
|
||||
sockets[0].open();
|
||||
await flush();
|
||||
sockets[0].reply(sockets[0].sent[0], { limits: { maxPendingRequestsPerConnection: 8 } });
|
||||
await info;
|
||||
assert.equal(client.maxPending, 1);
|
||||
client.close();
|
||||
});
|
||||
|
||||
test("close cancels automatic reconnect timers", async () => {
|
||||
const { api, sockets, clock } = createHarness();
|
||||
const client = new api.Client();
|
||||
const connection = client.connect();
|
||||
sockets[0].open();
|
||||
await connection;
|
||||
sockets[0].close(1006);
|
||||
assert.equal(clock.size, 1);
|
||||
client.close();
|
||||
assert.equal(clock.size, 0);
|
||||
clock.advance(10000);
|
||||
assert.equal(sockets.length, 1);
|
||||
});
|
||||
|
||||
test("invalid request limits fail instead of hanging the queue", () => {
|
||||
const { api } = createHarness();
|
||||
for (const maxPending of [0, -1, NaN, 1.5]) {
|
||||
assert.throws(() => new api.Client({ maxPending }), { name: "RangeError" });
|
||||
}
|
||||
});
|
||||
|
||||
test("a slot released just before reconnect cannot send on the new socket", async () => {
|
||||
const { api, sockets } = createHarness();
|
||||
const client = new api.Client({ maxPending: 1 });
|
||||
observe(client.ping());
|
||||
const queued = observe(client.ping());
|
||||
sockets[0].open();
|
||||
await flush();
|
||||
sockets[0].reply(sockets[0].sent[0], "complete");
|
||||
client.close();
|
||||
const connection = client.connect();
|
||||
sockets[1].open();
|
||||
await connection;
|
||||
const current = observe(client.ping());
|
||||
await flush();
|
||||
assert.equal(queued.error.kind, "DISCONNECTED");
|
||||
assert.equal(sockets[1].sent.length, 1);
|
||||
assert.equal(client._active, 1);
|
||||
sockets[1].reply(sockets[1].sent[0], "current");
|
||||
await flush();
|
||||
assert.equal(current.value, "current");
|
||||
assert.equal(client._active, 0);
|
||||
client.close();
|
||||
});
|
||||
|
||||
test("closing after open but before request continuations rejects old work", async () => {
|
||||
const { api, sockets } = createHarness();
|
||||
const client = new api.Client();
|
||||
const old = observe(client.ping());
|
||||
sockets[0].open();
|
||||
client.close();
|
||||
const connection = client.connect();
|
||||
sockets[1].open();
|
||||
await connection;
|
||||
await flush();
|
||||
assert.equal(old.error.kind, "DISCONNECTED");
|
||||
assert.equal(sockets[1].sent.length, 0);
|
||||
client.close();
|
||||
});
|
||||
|
||||
test("status callback failures do not strand connections", async () => {
|
||||
const { api, sockets } = createHarness();
|
||||
const client = new api.Client({ onStatus() { throw new Error("Consumer callback"); } });
|
||||
const connection = observe(client.connect());
|
||||
sockets[0].open();
|
||||
await flush();
|
||||
assert.equal(connection.status, "fulfilled");
|
||||
client.close();
|
||||
});
|
||||
|
||||
test("automatic reconnect recovers from an initial handshake failure", async () => {
|
||||
const { api, sockets, clock } = createHarness();
|
||||
const client = new api.Client();
|
||||
const initial = observe(client.connect());
|
||||
sockets[0].close(1006);
|
||||
await flush();
|
||||
assert.equal(initial.error.kind, "CONNECT_FAILED");
|
||||
clock.advance(500);
|
||||
assert.equal(sockets.length, 2);
|
||||
sockets[1].open();
|
||||
const request = observe(client.ping());
|
||||
await flush();
|
||||
sockets[1].reply(sockets[1].sent[0], "reconnected");
|
||||
await flush();
|
||||
assert.equal(request.value, "reconnected");
|
||||
client.close();
|
||||
});
|
||||
@@ -0,0 +1,97 @@
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const vm = require("node:vm");
|
||||
|
||||
const ROOT = path.resolve(__dirname, "..");
|
||||
|
||||
function createHarness() {
|
||||
let now = 0;
|
||||
let sequence = 0;
|
||||
const timers = new Map();
|
||||
const sockets = [];
|
||||
const clock = {
|
||||
setTimeout(callback, delay) {
|
||||
const timerId = ++sequence;
|
||||
timers.set(timerId, { callback, due: now + delay });
|
||||
return timerId;
|
||||
},
|
||||
setInterval(callback, delay) {
|
||||
const timerId = ++sequence;
|
||||
timers.set(timerId, { callback, due: now + delay, interval: delay });
|
||||
return timerId;
|
||||
},
|
||||
clearTimeout(timerId) { timers.delete(timerId); },
|
||||
advance(duration) {
|
||||
const end = now + duration;
|
||||
while (true) {
|
||||
const next = [...timers.entries()].filter(([, timer]) => timer.due <= end)
|
||||
.sort((left, right) => left[1].due - right[1].due)[0];
|
||||
if (!next) break;
|
||||
timers.delete(next[0]);
|
||||
now = next[1].due;
|
||||
if (next[1].interval) timers.set(next[0], { ...next[1], due: now + next[1].interval });
|
||||
next[1].callback();
|
||||
}
|
||||
now = end;
|
||||
},
|
||||
get size() { return timers.size; }
|
||||
};
|
||||
|
||||
class FakeWebSocket {
|
||||
constructor(url) {
|
||||
if (!/^wss?:\/\//.test(url)) throw new SyntaxError("Invalid WebSocket URL");
|
||||
this.url = url;
|
||||
this.readyState = 0;
|
||||
this.sent = [];
|
||||
sockets.push(this);
|
||||
}
|
||||
open() { this.readyState = 1; if (this.onopen) this.onopen(); }
|
||||
close(code = 1000) {
|
||||
this.readyState = 3;
|
||||
if (this.onclose) this.onclose({ code });
|
||||
}
|
||||
send(body) {
|
||||
if (this.readyState !== 1) throw new Error("Socket is not open");
|
||||
this.sent.push(JSON.parse(body));
|
||||
}
|
||||
reply(request, result) {
|
||||
this.onmessage({ data: JSON.stringify({ jsonrpc: "2.0", id: request.id, result }) });
|
||||
}
|
||||
}
|
||||
|
||||
const context = vm.createContext({
|
||||
console, WebSocket: FakeWebSocket,
|
||||
setTimeout: clock.setTimeout, clearTimeout: clock.clearTimeout
|
||||
});
|
||||
vm.runInContext(fs.readFileSync(path.join(ROOT, "livestream-assist.js"), "utf8"), context);
|
||||
return { api: context.LiveStreamAssist, context, clock, sockets };
|
||||
}
|
||||
|
||||
function observe(promise) {
|
||||
const outcome = { status: "pending" };
|
||||
promise.then(
|
||||
value => Object.assign(outcome, { status: "fulfilled", value }),
|
||||
error => Object.assign(outcome, { status: "rejected", error })
|
||||
);
|
||||
return outcome;
|
||||
}
|
||||
|
||||
function flush() { return new Promise(resolve => setImmediate(resolve)); }
|
||||
|
||||
function createQuery(handler) {
|
||||
const { api } = createHarness();
|
||||
const calls = [];
|
||||
const client = {
|
||||
read(params) {
|
||||
calls.push(params);
|
||||
return Promise.resolve().then(() => handler(params));
|
||||
}
|
||||
};
|
||||
return { query: new api.GameQuery(client), calls, api };
|
||||
}
|
||||
|
||||
function result(value, sessionId = "save-a", gameTick = "100") {
|
||||
return { value, sessionId, gameTick };
|
||||
}
|
||||
|
||||
module.exports = { ROOT, createHarness, createQuery, observe, flush, result };
|
||||
@@ -0,0 +1,109 @@
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const test = require("node:test");
|
||||
const vm = require("node:vm");
|
||||
const { ROOT, createHarness, flush } = require("./test_helpers");
|
||||
|
||||
function openOverlay(filename, options = {}) {
|
||||
const { context, clock, api } = createHarness();
|
||||
const elements = new Map();
|
||||
const events = new Map();
|
||||
const clients = [];
|
||||
let reads = 0;
|
||||
const html = fs.readFileSync(path.join(ROOT, "..", "examples", filename), "utf8");
|
||||
function element(id) {
|
||||
if (!elements.has(id)) elements.set(id, { value: "", textContent: "", style: {} });
|
||||
return elements.get(id);
|
||||
}
|
||||
for (const match of html.matchAll(/<input id="([^"]+)"[^>]*value="([^"]+)"/g)) element(match[1]).value = match[2];
|
||||
const languages = [...html.matchAll(/<option value="([^"]+)"/g)].map(match => match[1]);
|
||||
let language = languages[0];
|
||||
Object.defineProperty(element("lang"), "value", {
|
||||
get() { return language; },
|
||||
set(value) { language = languages.includes(String(value)) ? String(value) : ""; }
|
||||
});
|
||||
class Client {
|
||||
constructor(settings) { this.url = settings.url; this.settings = settings; this.connections = []; clients.push(this); }
|
||||
connect(url) {
|
||||
if (url != null) this.url = url;
|
||||
this.connections.push(this.url);
|
||||
if (options.failFirst && this.connections.length === 1) return Promise.reject({ kind: "CONNECT_FAILED" });
|
||||
return Promise.resolve(this);
|
||||
}
|
||||
close() { this.infoSnapshot = null; }
|
||||
systemInfo() { this.infoSnapshot = {}; return Promise.resolve(this.infoSnapshot); }
|
||||
validate() { return Promise.resolve({}); }
|
||||
}
|
||||
class GameQuery {
|
||||
getCurrentResearch() {
|
||||
reads += 1;
|
||||
return options.research ? options.research(reads) : Promise.resolve({ active: false });
|
||||
}
|
||||
getItemRates() { return Promise.resolve({ production: 1, consumption: 1, theoreticalProduction: 1, theoreticalConsumption: 1 }); }
|
||||
getDysonPower() { return Promise.resolve({ watts: "60" }); }
|
||||
}
|
||||
context.LiveStreamAssist = {
|
||||
Client, GameQuery,
|
||||
GameText: { fromGenerated(settings) { return new api.GameText({}, settings); } }
|
||||
};
|
||||
Object.assign(context, {
|
||||
location: { search: options.search || "" }, URLSearchParams,
|
||||
document: { getElementById: element },
|
||||
setInterval: clock.setInterval, clearInterval: clock.clearTimeout,
|
||||
addEventListener(name, callback) { events.set(name, callback); }
|
||||
});
|
||||
const scripts = [...html.matchAll(/<script(?:[^>]*)>([\s\S]*?)<\/script>/g)];
|
||||
vm.runInContext(scripts.map(match => match[1]).join("\n"), context, { filename });
|
||||
return { clock, element, events, clients, get reads() { return reads; } };
|
||||
}
|
||||
|
||||
for (const filename of ["research-overlay.html", "stats-overlay.html"]) {
|
||||
test(filename + " resumes polling after the first connection fails", async () => {
|
||||
const overlay = openOverlay(filename, { failFirst: true });
|
||||
await flush();
|
||||
assert.equal(overlay.reads, 0);
|
||||
overlay.clock.advance(2000);
|
||||
await flush();
|
||||
assert.equal(overlay.reads, 1);
|
||||
assert.equal(overlay.element("status").textContent, "connected");
|
||||
});
|
||||
|
||||
test(filename + " does not overlap slow polls", async () => {
|
||||
let finish;
|
||||
const pending = new Promise(resolve => { finish = resolve; });
|
||||
const overlay = openOverlay(filename, { research: () => pending });
|
||||
await flush();
|
||||
overlay.clock.advance(10000);
|
||||
await flush();
|
||||
assert.equal(overlay.reads, 1);
|
||||
finish({ active: false });
|
||||
await flush();
|
||||
});
|
||||
|
||||
test(filename + " ignores old results after a URL change", async () => {
|
||||
let finish;
|
||||
const pending = new Promise(resolve => { finish = resolve; });
|
||||
const current = { active: true, techId: 1002, curLevel: 1, hashUploaded: "1", hashNeeded: "2", progressPercent: 50 };
|
||||
const overlay = openOverlay(filename, { research: count => count === 1 ? pending : Promise.resolve(current) });
|
||||
await flush();
|
||||
overlay.element("url").value = "ws://localhost:19090/api/v1";
|
||||
assert.equal(typeof overlay.element("url").onchange, "function");
|
||||
overlay.element("url").onchange();
|
||||
await flush();
|
||||
finish({ ...current, techId: 1001 });
|
||||
await flush();
|
||||
const title = overlay.element(filename === "research-overlay.html" ? "title" : "techName");
|
||||
assert.equal(title.textContent, "Tech 1002");
|
||||
assert.equal(overlay.clients[0].connections.at(-1), "ws://localhost:19090/api/v1");
|
||||
});
|
||||
|
||||
test(filename + " accepts LCID query parameters and stops on pagehide", async () => {
|
||||
const overlay = openOverlay(filename, { search: "?lang=2052" });
|
||||
await flush();
|
||||
assert.equal(overlay.element("lang").value, "2052");
|
||||
assert.equal(typeof overlay.events.get("pagehide"), "function");
|
||||
overlay.events.get("pagehide")();
|
||||
assert.equal(overlay.clock.size, 0);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
const assert = require("node:assert/strict");
|
||||
const test = require("node:test");
|
||||
const { createQuery, result, observe, flush } = require("./test_helpers");
|
||||
|
||||
function itemReader(params, poolIndex = 1, sessionId = "save-a") {
|
||||
const member = params.path[params.path.length - 1];
|
||||
if (member === "factoryCount") return result(1, sessionId);
|
||||
if (params.path.includes("productIndices")) return result(poolIndex, sessionId);
|
||||
if (member === "total") {
|
||||
const totals = Array(14).fill("0");
|
||||
totals[1] = "60";
|
||||
totals[8] = "20";
|
||||
return result({ items: totals.slice(params.offset || 0, (params.offset || 0) + params.limit) }, sessionId);
|
||||
}
|
||||
return result({ refProductSpeed: 120, refConsumeSpeed: 40 }, sessionId);
|
||||
}
|
||||
|
||||
test("a missing product is rediscovered when production starts", async () => {
|
||||
let poolIndex = 0;
|
||||
const { query } = createQuery(params => itemReader(params, poolIndex));
|
||||
assert.equal((await query.getItemRates(1101)).production, 0);
|
||||
poolIndex = 1;
|
||||
const rates = await query.getItemRates(1101);
|
||||
assert.equal(rates.production, 60);
|
||||
assert.equal(rates.consumption, 20);
|
||||
assert.equal(rates.theoreticalProduction, 120);
|
||||
});
|
||||
|
||||
test("duplicate item ids share reads without doubling totals", async () => {
|
||||
const { query, calls } = createQuery(itemReader);
|
||||
const rates = await query.getItemRates([1101, 1101]);
|
||||
assert.equal(rates.length, 2);
|
||||
assert.equal(rates[0].production, 60);
|
||||
assert.equal(rates[1].production, 60);
|
||||
assert.equal(calls.filter(params => params.path.includes("productIndices")).length, 1);
|
||||
});
|
||||
|
||||
test("unbuilt local planets return zero rates without negative indexes", async () => {
|
||||
const { query, calls } = createQuery(params => {
|
||||
if (params.root === "localPlanet") return result(-1);
|
||||
throw new Error("An unbuilt planet has no factory statistics");
|
||||
});
|
||||
const rates = await query.getItemRates(1101, { scope: "localPlanet" });
|
||||
assert.equal(rates.production, 0);
|
||||
assert.equal(rates.factories, 0);
|
||||
assert.equal(rates.sessionId, "save-a");
|
||||
assert.equal(calls.length, 1);
|
||||
});
|
||||
|
||||
test("empty production results retain session metadata", async () => {
|
||||
const { query } = createQuery(() => result(0));
|
||||
const rates = await query.getItemRates(1101);
|
||||
assert.equal(rates.sessionId, "save-a");
|
||||
assert.equal(rates.gameTick, "100");
|
||||
});
|
||||
|
||||
test("explicit factory scopes refresh cached indices after a save change", async () => {
|
||||
let sessionId = "save-a";
|
||||
let poolIndex = 1;
|
||||
const { query, calls } = createQuery(params => itemReader(params, poolIndex, sessionId));
|
||||
await query.getItemRates(1101, { scope: { factoryIndex: 0 } });
|
||||
await query.getItemRates(1101, { scope: { factoryIndex: 0 } });
|
||||
calls.length = 0;
|
||||
sessionId = "save-b";
|
||||
poolIndex = 2;
|
||||
await query.getItemRates(1101, { scope: { factoryIndex: 0 } });
|
||||
const productReads = calls.filter(params => params.path.includes("productPool"));
|
||||
assert.equal(productReads.length, 2);
|
||||
for (const params of productReads) assert.equal(params.path[4], 2);
|
||||
});
|
||||
|
||||
test("research from different saves is not combined", async () => {
|
||||
const { query } = createQuery(params => params.path[0] === "currentTech"
|
||||
? result(1001)
|
||||
: result({ hashUploaded: "50", hashNeeded: "100", curLevel: 1 }, "save-b"));
|
||||
await assert.rejects(query.getCurrentResearch(), { kind: "SESSION_CHANGED" });
|
||||
});
|
||||
|
||||
test("research progress keeps integer precision", async () => {
|
||||
const { query } = createQuery(() => result({
|
||||
hashUploaded: "9007199254740993", hashNeeded: "18014398509481986", curLevel: 1
|
||||
}));
|
||||
const research = await query.getTechState(1001);
|
||||
assert.equal(research.progressPercent, 50);
|
||||
assert.equal(research.hashUploaded, "9007199254740993");
|
||||
});
|
||||
|
||||
test("Dyson collection pages project energy instead of rereading each sphere", async () => {
|
||||
const energy = { energyGenCurrentTick: "100", energyGenOriginalCurrentTick: "200", energyReqCurrentTick: "5" };
|
||||
const { query, calls } = createQuery(params => {
|
||||
if (params.path.length === 1) {
|
||||
const entries = Array(130).fill(null);
|
||||
entries[0] = energy;
|
||||
entries[129] = energy;
|
||||
const offset = params.offset || 0;
|
||||
return result({ items: entries.slice(offset, offset + params.limit), offset, hasMore: offset + params.limit < entries.length });
|
||||
}
|
||||
if (params.path[2] === "starData") return result({ id: params.path[1] + 1, name: "Star" });
|
||||
return result(energy);
|
||||
});
|
||||
const power = await query.getDysonPower();
|
||||
assert.equal(power.watts, "12000");
|
||||
assert.equal(power.originalWatts, "24000");
|
||||
assert.equal(power.sphereCount, 2);
|
||||
assert.equal(calls.length, 4);
|
||||
for (const page of calls.filter(params => params.path.length === 1)) {
|
||||
assert.ok(page.select.includes("energyGenCurrentTick"));
|
||||
}
|
||||
});
|
||||
|
||||
test("a targeted Dyson query does not scan all stars", async () => {
|
||||
const { query, calls } = createQuery(params => {
|
||||
if (params.path.length === 1) return result({ items: [null, {}], offset: 0, hasMore: false });
|
||||
if (params.path[2] === "starData") return result({ id: 2, name: "Star" });
|
||||
return result({ energyGenCurrentTick: "9007199254740993", energyGenOriginalCurrentTick: "9007199254740993" });
|
||||
});
|
||||
const power = await query.getDysonPower({ starIndex: 1 });
|
||||
assert.equal(power.watts, "540431955284459580");
|
||||
assert.equal(calls.length, 2);
|
||||
assert.equal(calls[0].path[1], 1);
|
||||
});
|
||||
|
||||
test("star lookup failures are not reported as successful power reads", async () => {
|
||||
const { query } = createQuery(params => {
|
||||
if (params.path.length === 1) return result({ items: [{}], offset: 0, hasMore: false });
|
||||
if (params.path[2] === "starData") throw { kind: "SESSION_CHANGED" };
|
||||
return result({ energyGenCurrentTick: "100" });
|
||||
});
|
||||
await assert.rejects(query.getDysonPower(), { kind: "SESSION_CHANGED" });
|
||||
});
|
||||
|
||||
test("empty Dyson results retain session metadata", async () => {
|
||||
const { query } = createQuery(() => result({ items: [], offset: 0, hasMore: false }));
|
||||
const power = await query.getDysonPower();
|
||||
assert.equal(power.watts, "0");
|
||||
assert.equal(power.sessionId, "save-a");
|
||||
assert.equal(power.gameTick, "100");
|
||||
});
|
||||
|
||||
test("late responses cannot restore a previous session", async () => {
|
||||
let finish;
|
||||
const pending = new Promise(resolve => { finish = resolve; });
|
||||
const { query } = createQuery(params => params.path[0] === "techStates" ? pending : result(0, "save-b"));
|
||||
const old = observe(query.getTechState(1001));
|
||||
await flush();
|
||||
await query.getCurrentResearch();
|
||||
finish(result({ hashUploaded: "1", hashNeeded: "2" }, "save-a"));
|
||||
await flush();
|
||||
assert.equal(old.error.kind, "SESSION_CHANGED");
|
||||
assert.equal(query._sessionId, "save-b");
|
||||
});
|
||||
|
||||
test("failed queries wait for their other reads before settling", async () => {
|
||||
let finish;
|
||||
const pending = new Promise(resolve => { finish = resolve; });
|
||||
const { query } = createQuery(params => {
|
||||
if (params.path.at(-1) === "total") return pending;
|
||||
if (params.select) throw { kind: "SERVER_BUSY" };
|
||||
return itemReader(params);
|
||||
});
|
||||
const rates = observe(query.getItemRates(1101));
|
||||
await flush();
|
||||
assert.equal(rates.status, "pending");
|
||||
finish(result({ items: Array(8).fill("0") }));
|
||||
await flush();
|
||||
assert.equal(rates.error.kind, "SERVER_BUSY");
|
||||
});
|
||||
@@ -0,0 +1,63 @@
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const test = require("node:test");
|
||||
const vm = require("node:vm");
|
||||
const { ROOT, createHarness } = require("./test_helpers");
|
||||
|
||||
test("the packed script matches its sources", () => {
|
||||
const names = ["header", "format", "client", "query", "text", "footer"];
|
||||
const sources = names.map(name => fs.readFileSync(path.join(ROOT, "src", name + ".js"), "utf8").replace(/\r\n/g, "\n")).join("");
|
||||
const packed = fs.readFileSync(path.join(ROOT, "livestream-assist.js"), "utf8").replace(/\r\n/g, "\n");
|
||||
assert.ok(packed === sources, "The bundle is stale; run python LiveStreamAssist/js/scripts/pack_js.py.");
|
||||
});
|
||||
|
||||
test("empty and missing translations follow the game instead of English fallback", () => {
|
||||
const { api, sockets } = createHarness();
|
||||
const text = new api.GameText({
|
||||
languages: { 2052: { fallback: 1033 } },
|
||||
strings: { empty: { 2052: "", 1033: "English" }, missing: { 1033: "English" } }
|
||||
}, { language: 2052 });
|
||||
assert.equal(text.translate("empty"), "");
|
||||
assert.equal(text.translate("missing"), "missing");
|
||||
assert.equal(sockets.length, 0);
|
||||
});
|
||||
|
||||
test("language aliases are case insensitive without metadata", () => {
|
||||
const { api } = createHarness();
|
||||
const text = new api.GameText({}, { language: "ZH-CN" });
|
||||
assert.equal(text.lcid, 2052);
|
||||
text.setLanguage("JA-JA");
|
||||
assert.equal(text.lcid, 1041);
|
||||
});
|
||||
|
||||
test("JavaScript KMG formatting covers thresholds, signs and 64-bit values", () => {
|
||||
const { api } = createHarness();
|
||||
const cases = [
|
||||
[0, "count", "0"], [9999, "count", "9999"], [10000, "count", "10.0k"],
|
||||
[12345, "count", "12.3k"], [999999, "count", "999k"], [1000000, "count", "1.00M"],
|
||||
[-12345, "count", "-12.3k"], [12345, "power", "12.3kW"], [0, "power", "0W"],
|
||||
[12345, "energy", "12.3kJ"], [1000, "si1000", "1.00k"],
|
||||
["9223372036854775807", "power", "9.22EW"]
|
||||
];
|
||||
for (const [value, kind, expected] of cases) {
|
||||
assert.equal(api.format.write(value, { kind }).trim(), expected);
|
||||
}
|
||||
assert.equal(api.format.kmg(12345, { decimal: "," }), "12,3k");
|
||||
assert.ok(api.format.write(12345, { blank: true }).includes("\u2009k"));
|
||||
});
|
||||
|
||||
test("generated prototype names are available offline without locale BOMs", () => {
|
||||
const { api, context } = createHarness();
|
||||
vm.runInContext(fs.readFileSync(path.join(ROOT, "generated", "dsp-text.js"), "utf8"), context);
|
||||
const text = api.GameText.fromGenerated({ language: "en" });
|
||||
assert.equal(text.translate("\u94c1\u77ff"), "Iron Ore");
|
||||
assert.ok(Object.keys(text.data.strings).every(key => !key.startsWith("\ufeff")));
|
||||
});
|
||||
|
||||
test("README usage examples parse as classic scripts", () => {
|
||||
const readme = fs.readFileSync(path.join(ROOT, "README.md"), "utf8");
|
||||
for (const script of readme.matchAll(/^<script>([\s\S]*?)^<\/script>/gm)) {
|
||||
assert.doesNotThrow(() => new vm.Script(script[1]));
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,138 @@
|
||||
#!/usr/bin/env python3
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
SCRIPT_DIR = Path(__file__).resolve().parent
|
||||
sys.path.insert(0, str(SCRIPT_DIR))
|
||||
|
||||
from generate_text import (
|
||||
build_payload,
|
||||
find_game_root,
|
||||
first_field,
|
||||
load_language_strings,
|
||||
load_name_keys,
|
||||
parse_header,
|
||||
read_locale_text,
|
||||
translation_field,
|
||||
unescape_string,
|
||||
)
|
||||
from kmg import THIN_SPACE, format_kmg, write_kmg
|
||||
|
||||
|
||||
def assert_eq(actual, expected, label: str) -> None:
|
||||
if actual != expected:
|
||||
raise SystemExit(f"{label}: expected {expected!r}, got {actual!r}")
|
||||
|
||||
|
||||
def test_unescape() -> None:
|
||||
assert_eq(unescape_string("plain"), "plain", "plain")
|
||||
assert_eq(unescape_string("a\\nb"), "a\nb", "newline")
|
||||
assert_eq(unescape_string("a\\tb"), "a\tb", "tab")
|
||||
assert_eq(unescape_string("c:\\\\x"), "c:\\x", "backslash")
|
||||
|
||||
|
||||
def test_header_and_locale() -> None:
|
||||
game = find_game_root(None)
|
||||
if game is None:
|
||||
print("skip locale tests: DSP not found")
|
||||
return
|
||||
languages, pages = parse_header(game / "Locale" / "Header.txt")
|
||||
lcids = {lang["lcid"] for lang in languages}
|
||||
if 1033 not in lcids or 2052 not in lcids:
|
||||
raise SystemExit(f"missing core languages: {lcids}")
|
||||
if "prototype.txt" not in pages:
|
||||
raise SystemExit("prototype.txt missing from page order")
|
||||
iron = None
|
||||
for line in read_locale_text(game / "Locale" / "1033" / "prototype.txt").splitlines():
|
||||
if first_field(line) == "铁矿":
|
||||
iron = translation_field(line)
|
||||
break
|
||||
assert_eq(iron, "Iron Ore", "iron ore en")
|
||||
|
||||
|
||||
def test_locale_bom_and_empty_translations() -> None:
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
locale_dir = Path(directory)
|
||||
locale_file = locale_dir / "prototype.txt"
|
||||
source = "first\t\tPrototype\tTranslated\nempty\t\tPrototype\t\n"
|
||||
for encoding in ("utf-16", "utf-8-sig"):
|
||||
locale_file.write_text(source, encoding=encoding, newline="\n")
|
||||
assert_eq(read_locale_text(locale_file), source, f"decode {encoding}")
|
||||
keys = load_name_keys(locale_dir, ["prototype.txt"])
|
||||
assert_eq(keys, ["first", "empty"], f"keys {encoding}")
|
||||
values = load_language_strings(locale_dir, ["prototype.txt"], keys)
|
||||
assert_eq(values, {"first": "Translated", "empty": ""}, f"translations {encoding}")
|
||||
locale_file.write_bytes(b"\xfe\xff" + source.encode("utf-16-be"))
|
||||
assert_eq(read_locale_text(locale_file), source, "decode utf-16-be")
|
||||
|
||||
|
||||
def test_header_page_order() -> None:
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
header = Path(directory) / "Header.txt"
|
||||
header.write_text(
|
||||
"[Localization Project]\nVersion=1.1\n1033,English,en-US,en,2052,0\n\n"
|
||||
"keys=0\ncustom=0\nbase=0\nprototype=-1\n",
|
||||
encoding="utf-16",
|
||||
)
|
||||
_, pages = parse_header(header)
|
||||
assert_eq(pages, ["prototype.txt", "keys.txt", "custom.txt", "base.txt"], "stable page order")
|
||||
|
||||
|
||||
def test_incomplete_prototypes_are_rejected() -> None:
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
game = Path(directory)
|
||||
locale_dir = game / "Locale"
|
||||
locale_dir.mkdir()
|
||||
(locale_dir / "Header.txt").write_text(
|
||||
"Version=1.1\n1033,English,en-US,en,2052,0\n\nprototype=-1\n", encoding="utf-8"
|
||||
)
|
||||
with patch("generate_text.extract_proto_ids", return_value={}):
|
||||
try:
|
||||
build_payload(game, "2022.3.7")
|
||||
except SystemExit:
|
||||
return
|
||||
raise SystemExit("incomplete prototypes must not overwrite generated data")
|
||||
|
||||
|
||||
def test_kmg() -> None:
|
||||
cases = [
|
||||
(0, "count", "0"),
|
||||
(12, "count", "12"),
|
||||
(9999, "count", "9999"),
|
||||
(10000, "count", "10.0k"),
|
||||
(12345, "count", "12.3k"),
|
||||
(999999, "count", "999k"),
|
||||
(1000000, "count", "1.00M"),
|
||||
(1234567890, "count", "1.23G"),
|
||||
(-12345, "count", "-12.3k"),
|
||||
(12345, "power", "12.3kW"),
|
||||
(123, "power", "123W"),
|
||||
(0, "power", "0W"),
|
||||
(12345, "energy", "12.3kJ"),
|
||||
(999, "si1000", "999"),
|
||||
(1000, "si1000", "1.00k"),
|
||||
(1234, "si1000", "1.23k"),
|
||||
]
|
||||
for value, kind, expected in cases:
|
||||
assert_eq(format_kmg(value, kind=kind), expected, f"kmg {value} {kind}")
|
||||
raw = write_kmg(12345, blank=True)
|
||||
if THIN_SPACE + "k" not in raw:
|
||||
raise SystemExit(f"blank thin space missing: {raw!r}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
test_unescape()
|
||||
test_kmg()
|
||||
test_locale_bom_and_empty_translations()
|
||||
test_header_page_order()
|
||||
test_incomplete_prototypes_are_rejected()
|
||||
test_header_and_locale()
|
||||
print("ok")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user