Add game clock commands, command completions and Cyrene login tooling
This commit is contained in:
1 parent
1fa9504176
commit
67a819a4d3
21 files changed
+1194
-7
No files matched your search
@@ -0,0 +1,97 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Install or restore a staged Lua selector without deleting source bundles."""
|
||||
import argparse
|
||||
import hashlib
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def selector_digest(data):
|
||||
if len(data) != 66 or data[:4] != b"SRMI":
|
||||
raise ValueError("unsupported LuaV selector")
|
||||
return b"".join(data[i:i + 4][::-1] for i in range(28, 44, 4)).hex()
|
||||
|
||||
|
||||
def atomic_copy(source, target):
|
||||
fd, name = tempfile.mkstemp(prefix=".gacy-luav-", dir=target.parent)
|
||||
try:
|
||||
with os.fdopen(fd, "wb") as out, source.open("rb") as inp:
|
||||
shutil.copyfileobj(inp, out)
|
||||
out.flush()
|
||||
os.fsync(out.fileno())
|
||||
os.replace(name, target)
|
||||
finally:
|
||||
if os.path.exists(name):
|
||||
os.unlink(name)
|
||||
|
||||
|
||||
def install(source, stage, backup):
|
||||
source, stage, backup = source.resolve(strict=True), stage.resolve(strict=True), backup.resolve()
|
||||
if source == stage or source in stage.parents or backup == source or source in backup.parents:
|
||||
raise ValueError("stage and backup must be outside the installation")
|
||||
current = source / "M_LuaV.bytes"
|
||||
current_bytes = current.read_bytes()
|
||||
old_digest = selector_digest(current_bytes)
|
||||
if not (source / ("LuaV_" + old_digest + ".bytes")).is_file():
|
||||
raise ValueError("original selected index is unavailable")
|
||||
new_selector = stage / "M_LuaV.bytes"
|
||||
new_digest = selector_digest(new_selector.read_bytes())
|
||||
index = stage / ("LuaV_" + new_digest + ".bytes")
|
||||
if hashlib.md5(index.read_bytes()).hexdigest() != new_digest:
|
||||
raise ValueError("staged index digest differs")
|
||||
blocks = [p for p in stage.glob("*.bytes") if p.name != "M_LuaV.bytes" and not p.name.startswith("LuaV_")]
|
||||
if len(blocks) != 1 or hashlib.md5(blocks[0].read_bytes()).hexdigest() != blocks[0].stem:
|
||||
raise ValueError("staged block digest differs")
|
||||
if bytes.fromhex(blocks[0].stem) not in index.read_bytes():
|
||||
raise ValueError("staged index does not reference staged block")
|
||||
backup.mkdir(parents=True, exist_ok=True)
|
||||
backup_file = backup / ("M_LuaV-" + old_digest + ".bytes")
|
||||
if backup_file.exists() and backup_file.read_bytes() != current_bytes:
|
||||
raise ValueError("existing selector backup differs")
|
||||
if not backup_file.exists():
|
||||
atomic_copy(current, backup_file)
|
||||
for item in (blocks[0], index):
|
||||
dst = source / item.name
|
||||
if dst.exists():
|
||||
if hashlib.md5(dst.read_bytes()).hexdigest() != hashlib.md5(item.read_bytes()).hexdigest():
|
||||
raise ValueError("existing content-addressed file differs")
|
||||
else:
|
||||
atomic_copy(item, dst)
|
||||
if current.read_bytes() != current_bytes:
|
||||
raise ValueError("selector changed during installation")
|
||||
atomic_copy(new_selector, current)
|
||||
if current.read_bytes() != new_selector.read_bytes():
|
||||
raise ValueError("installed selector did not verify")
|
||||
print("Installed staged Lua selector; original backup:", backup_file)
|
||||
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument("--restore", action="store_true", help="restore the backup selector")
|
||||
p.add_argument("source", type=Path)
|
||||
p.add_argument("stage", type=Path)
|
||||
p.add_argument("backup", type=Path)
|
||||
args = p.parse_args()
|
||||
if args.restore:
|
||||
source, stage, backup = args.source.resolve(strict=True), args.stage.resolve(strict=True), args.backup.resolve(strict=True)
|
||||
if source == stage or source in stage.parents or source == backup or source in backup.parents:
|
||||
raise ValueError("stage and backup must be outside the installation")
|
||||
target = source / "M_LuaV.bytes"
|
||||
if target.read_bytes() != (stage / "M_LuaV.bytes").read_bytes():
|
||||
raise ValueError("current selector no longer matches this staged patch")
|
||||
candidates = list(backup.glob("M_LuaV-*.bytes"))
|
||||
if len(candidates) != 1:
|
||||
raise ValueError("backup selector is missing or ambiguous")
|
||||
original_digest = selector_digest(candidates[0].read_bytes())
|
||||
if candidates[0].name != "M_LuaV-" + original_digest + ".bytes" or not (source / ("LuaV_" + original_digest + ".bytes")).is_file():
|
||||
raise ValueError("backup selector or original index does not match")
|
||||
atomic_copy(candidates[0], target)
|
||||
print("Restored original Lua selector")
|
||||
else:
|
||||
install(args.source, args.stage, args.backup)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,119 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Create a staged Lua bundle rewrite; never modify the source installation.
|
||||
|
||||
The caller supplies the Client's Lua/Windows directory, output directory and
|
||||
two exact asset paths. This handles the observed LuaV big-endian index and
|
||||
SRMI digest format only; it refuses ambiguous input instead of guessing.
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import struct
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def md5(data):
|
||||
return hashlib.md5(data).digest()
|
||||
|
||||
|
||||
def replace_once(buf, old, new):
|
||||
if len(old) >= 128 or len(new) >= 128:
|
||||
raise ValueError("only single-byte Lua string lengths are supported")
|
||||
original = bytes([len(old)]) + old
|
||||
replacement = bytes([len(new)]) + new
|
||||
if buf.count(original) != 1:
|
||||
raise ValueError("the exact length-prefixed string must occur once")
|
||||
index = buf.index(original)
|
||||
return buf[:index] + replacement + buf[index + len(original):], index
|
||||
|
||||
|
||||
def stage(source, output, old, new):
|
||||
source, output = source.resolve(strict=True), output.resolve()
|
||||
if output == source or source in output.parents:
|
||||
raise ValueError("output must be outside the source installation")
|
||||
if output.exists() and any(output.iterdir()):
|
||||
raise ValueError("output must be empty")
|
||||
selector = (source / "M_LuaV.bytes").read_bytes()
|
||||
if len(selector) != 66 or selector[:4] != b"SRMI":
|
||||
raise ValueError("unsupported LuaV selector")
|
||||
digest = b"".join(selector[i:i + 4][::-1] for i in range(28, 44, 4))
|
||||
index_path = source / ("LuaV_" + digest.hex() + ".bytes")
|
||||
index = bytearray(index_path.read_bytes())
|
||||
if md5(index) != digest or struct.unpack_from("<I", selector, 44)[0] != len(index):
|
||||
raise ValueError("LuaV selector digest or size does not match")
|
||||
if len(index) < 16 or struct.unpack_from(">II", index, 0) != (255, 4):
|
||||
raise ValueError("unsupported LuaV index header")
|
||||
blocks, total_modules = struct.unpack_from(">II", index, 8)
|
||||
pos, entries_seen, matches = 16, 0, []
|
||||
old, new = old.encode("utf-8"), new.encode("utf-8")
|
||||
for _ in range(blocks):
|
||||
if pos + 36 > len(index):
|
||||
raise ValueError("truncated LuaV block header")
|
||||
block_digest = bytes(index[pos + 8:pos + 24])
|
||||
reserved, size, count = struct.unpack_from(">III", index, pos + 24)
|
||||
table = pos + 36
|
||||
end = table + count * 16
|
||||
if reserved != 0 or end > len(index):
|
||||
raise ValueError("unsupported LuaV block table")
|
||||
block_path = source / (block_digest.hex() + ".bytes")
|
||||
data = block_path.read_bytes()
|
||||
if len(data) != size or md5(data) != block_digest:
|
||||
raise ValueError("Lua block digest or size differs")
|
||||
entries_seen += count
|
||||
needle = bytes([len(old)]) + old
|
||||
if data.count(needle):
|
||||
if data.count(needle) != 1:
|
||||
raise ValueError("ambiguous path within Lua block")
|
||||
location = data.index(needle)
|
||||
entries = [struct.unpack_from(">QII", index, table + 16 * i) for i in range(count)]
|
||||
containing = [(i, off, length) for i, (_, length, off) in enumerate(entries)
|
||||
if off <= location and location + len(needle) <= off + length]
|
||||
if len(containing) != 1:
|
||||
raise ValueError("Lua module index does not uniquely cover the string")
|
||||
matches.append((pos, table, data, entries, containing[0], location))
|
||||
if bytes(index[end:end + 3]) != b"\x00\x00\x80":
|
||||
raise ValueError("unsupported LuaV block trailer")
|
||||
pos = end + 3
|
||||
if entries_seen != total_modules or pos != len(index) or len(matches) != 1:
|
||||
raise ValueError("LuaV index size/count or matching block differs")
|
||||
pos, table, data, entries, (module, module_offset, module_len), location = matches[0]
|
||||
rewritten, _ = replace_once(data, old, new)
|
||||
delta = len(rewritten) - len(data)
|
||||
for i, (_, length, offset) in enumerate(entries):
|
||||
if i == module:
|
||||
length += delta
|
||||
elif offset >= module_offset + module_len:
|
||||
offset += delta
|
||||
elif offset < module_offset + module_len and offset + length > module_offset:
|
||||
raise ValueError("overlapping Lua module entries")
|
||||
if length < 0 or offset < 0 or offset + length > len(rewritten):
|
||||
raise ValueError("rewritten Lua module bounds invalid")
|
||||
struct.pack_into(">II", index, table + 16 * i + 8, length, offset)
|
||||
index[pos + 8:pos + 24] = md5(rewritten)
|
||||
struct.pack_into(">I", index, pos + 28, len(rewritten))
|
||||
new_index_digest = md5(index)
|
||||
updated_selector = bytearray(selector)
|
||||
for i in range(4):
|
||||
updated_selector[28 + i * 4:32 + i * 4] = new_index_digest[i * 4:i * 4 + 4][::-1]
|
||||
output.mkdir(parents=True, exist_ok=True)
|
||||
(output / (md5(rewritten).hex() + ".bytes")).write_bytes(rewritten)
|
||||
(output / ("LuaV_" + new_index_digest.hex() + ".bytes")).write_bytes(index)
|
||||
(output / "M_LuaV.bytes").write_bytes(updated_selector)
|
||||
return {"old_block_md5": md5(data).hex(), "new_block_md5": md5(rewritten).hex(),
|
||||
"old_index_md5": digest.hex(), "new_index_md5": new_index_digest.hex(),
|
||||
"module_offset": module_offset, "module_old_size": module_len,
|
||||
"module_new_size": module_len + delta, "string_offset": location}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("source", type=Path)
|
||||
parser.add_argument("output", type=Path)
|
||||
parser.add_argument("old_path")
|
||||
parser.add_argument("new_path")
|
||||
args = parser.parse_args()
|
||||
print(json.dumps(stage(args.source, args.output, args.old_path, args.new_path), indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,109 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Stage full shipped ASB manifests for early startup; retain originals.
|
||||
|
||||
Observed SRMI selector, SRAM v5 manifest and SRBM v1 block-table formats.
|
||||
This changes startup availability only; it does not select a login scene.
|
||||
All installation paths are explicit arguments. Restore refuses unrelated edits.
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import struct
|
||||
from pathlib import Path
|
||||
|
||||
from install_baked_lua_stage import atomic_copy, selector_digest
|
||||
|
||||
|
||||
KINDS = ('AsbV', 'BlockV')
|
||||
|
||||
|
||||
def selected(root, name):
|
||||
selector = root / ('M_' + name + '.bytes')
|
||||
raw = selector.read_bytes()
|
||||
digest = selector_digest(raw)
|
||||
data = root / (name + '_' + digest + '.bytes')
|
||||
content = data.read_bytes()
|
||||
if hashlib.md5(content).hexdigest() != digest or struct.unpack_from('<I', raw, 44)[0] != len(content):
|
||||
raise ValueError('selector size or digest mismatch: ' + name)
|
||||
return selector, raw, data, content
|
||||
|
||||
|
||||
def stage(source, output):
|
||||
source, output = source.resolve(strict=True), output.resolve()
|
||||
if source == output or source in output.parents:
|
||||
raise ValueError('stage must be outside installation')
|
||||
if output.exists() and any(output.iterdir()):
|
||||
raise ValueError('stage must be empty')
|
||||
prepared = []
|
||||
for kind in KINDS:
|
||||
_, old, _, before = selected(source, 'Start_' + kind)
|
||||
_, full, path, content = selected(source, kind)
|
||||
if old[:28] != full[:28] or before[:16] != content[:16]:
|
||||
raise ValueError('startup and full manifest schema/build differ')
|
||||
if kind == 'BlockV':
|
||||
if content[:8] != b'SRBM\x00\x01\x00\x01':
|
||||
raise ValueError('unsupported block table')
|
||||
offset, count, stride = struct.unpack_from('<III', content, 16)
|
||||
if offset != 28 or stride != 24 or len(content) != offset + count * stride:
|
||||
raise ValueError('unsupported block table bounds')
|
||||
for i in range(count):
|
||||
row = content[offset+i*stride:offset+(i+1)*stride]
|
||||
digest = b''.join(row[j:j+4][::-1] for j in range(0, 16, 4)).hex()
|
||||
if not (source / (digest + '.block')).is_file():
|
||||
raise ValueError('missing referenced block: ' + digest)
|
||||
prepared.append((kind, full, path, content))
|
||||
output.mkdir(parents=True, exist_ok=True)
|
||||
for kind, selector, path, content in prepared:
|
||||
(output / ('Start_' + path.name)).write_bytes(content)
|
||||
(output / ('M_Start_' + kind + '.bytes')).write_bytes(selector)
|
||||
print('Staged full manifests; all referenced block files exist')
|
||||
|
||||
|
||||
def apply(source, staged, backup, restore=False):
|
||||
source, staged, backup = source.resolve(strict=True), staged.resolve(strict=True), backup.resolve()
|
||||
if any(p == source or source in p.parents for p in (staged, backup)):
|
||||
raise ValueError('stage and backup must be outside installation')
|
||||
entries = []
|
||||
for kind in KINDS:
|
||||
name = 'M_Start_' + kind + '.bytes'
|
||||
current = source / name
|
||||
_, expected, blob, _ = selected(staged, 'Start_' + kind)
|
||||
saved = backup / name
|
||||
if restore:
|
||||
if current.read_bytes() != expected:
|
||||
raise ValueError('current selector is not this overlay: ' + name)
|
||||
selected_backup = saved.read_bytes()
|
||||
original = source / ('Start_' + kind + '_' + selector_digest(selected_backup) + '.bytes')
|
||||
if not original.is_file():
|
||||
raise ValueError('original manifest missing')
|
||||
else:
|
||||
selected(source, 'Start_' + kind)
|
||||
if saved.exists() and saved.read_bytes() != current.read_bytes():
|
||||
raise ValueError('existing backup differs')
|
||||
entries.append((current, saved, staged / name, blob))
|
||||
backup.mkdir(parents=True, exist_ok=True)
|
||||
for current, saved, selector, blob in entries:
|
||||
if restore:
|
||||
atomic_copy(saved, current)
|
||||
else:
|
||||
if not saved.exists():
|
||||
atomic_copy(current, saved)
|
||||
atomic_copy(blob, source / blob.name)
|
||||
atomic_copy(selector, current)
|
||||
if current.read_bytes() != (saved if restore else selector).read_bytes():
|
||||
raise ValueError('selector write verification failed')
|
||||
print('Restored original startup manifests' if restore else 'Installed startup manifest overlay; originals retained')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('mode', choices=('stage', 'install', 'restore'))
|
||||
parser.add_argument('source', type=Path)
|
||||
parser.add_argument('staged', type=Path)
|
||||
parser.add_argument('--backup', type=Path)
|
||||
args = parser.parse_args()
|
||||
if args.mode == 'stage':
|
||||
stage(args.source, args.staged)
|
||||
else:
|
||||
if args.backup is None:
|
||||
parser.error('--backup is required for install/restore')
|
||||
apply(args.source, args.staged, args.backup, args.mode == 'restore')
|
||||
@@ -0,0 +1,58 @@
|
||||
import hashlib
|
||||
import struct
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from rewrite_baked_lua_string import stage
|
||||
|
||||
|
||||
def fixture(root, contents):
|
||||
digest = hashlib.md5(contents).digest()
|
||||
(root / (digest.hex() + ".bytes")).write_bytes(contents)
|
||||
# One block and one module, with a 3-byte LuaV block trailer.
|
||||
index = (struct.pack(">IIII", 255, 4, 1, 1) + bytes(8) + digest +
|
||||
struct.pack(">III", 0, len(contents), 1) + bytes(8) +
|
||||
struct.pack(">II", len(contents), 0) + b"\x00\x00\x80")
|
||||
index_hash = hashlib.md5(index).digest()
|
||||
(root / ("LuaV_" + index_hash.hex() + ".bytes")).write_bytes(index)
|
||||
selector = bytearray(66)
|
||||
selector[:4] = b"SRMI"
|
||||
for i in range(4):
|
||||
selector[28 + 4 * i:32 + 4 * i] = index_hash[4 * i:4 * i + 4][::-1]
|
||||
struct.pack_into("<I", selector, 44, len(index))
|
||||
(root / "M_LuaV.bytes").write_bytes(selector)
|
||||
|
||||
|
||||
class RewriteTest(unittest.TestCase):
|
||||
def test_changed_string_updates_digest_and_round_trips(self):
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
root = Path(folder)
|
||||
src, out = root / "src", root / "stage"
|
||||
src.mkdir()
|
||||
old, new = b"scene/a.prefab", b"scene/big.prefab"
|
||||
fixture(src, b"prefix" + bytes([len(old)]) + old + b"suffix")
|
||||
result = stage(src, out, old.decode(), new.decode())
|
||||
changed = (out / (result["new_block_md5"] + ".bytes")).read_bytes()
|
||||
self.assertIn(bytes([len(new)]) + new, changed)
|
||||
self.assertEqual(result["module_new_size"] - result["module_old_size"], len(new) - len(old))
|
||||
reverse = root / "reverse"
|
||||
result2 = stage(out, reverse, new.decode(), old.decode())
|
||||
self.assertEqual(result2["new_block_md5"], result["old_block_md5"])
|
||||
self.assertEqual(result2["new_index_md5"], result["old_index_md5"])
|
||||
self.assertEqual((reverse / "M_LuaV.bytes").read_bytes(), (src / "M_LuaV.bytes").read_bytes())
|
||||
|
||||
def test_duplicate_string_refuses_write(self):
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
root = Path(folder)
|
||||
src, out = root / "src", root / "stage"
|
||||
src.mkdir()
|
||||
old = b"a.prefab"
|
||||
fixture(src, (bytes([len(old)]) + old) * 2)
|
||||
with self.assertRaisesRegex(ValueError, "ambiguous"):
|
||||
stage(src, out, old.decode(), "longer.prefab")
|
||||
self.assertFalse(out.exists())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user