Add v1.2 development quest progression and experimental battle runtime

This commit is contained in:
AttilaNet
2026-11-08 19:29:28 +08:00
parent 79b7672471
commit 0d3e231cde
71 changed files with 14349 additions and 172 deletions
+110
View File
@@ -0,0 +1,110 @@
"""Prepare library-only copies without changing any original AVM2 code bytes."""
from __future__ import annotations
from collections import Counter
import hashlib
import struct
import zlib
def digest(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def unpack_swf(data: bytes) -> bytes:
if len(data) < 13 or data[:3] not in (b"FWS", b"CWS"):
raise ValueError("Expected FWS/CWS SWF")
result = data if data[:3] == b"FWS" else b"FWS" + data[3:8] + zlib.decompress(data[8:])
if len(result) != struct.unpack_from("<I", result, 4)[0]:
raise ValueError("SWF declared length mismatch")
return result
def tags(data: bytes):
start = 8 + (5 + 4 * (data[8] >> 3) + 7) // 8 + 4
position = start
while position < len(data):
if position + 2 > len(data):
raise ValueError("Truncated SWF tag")
header = struct.unpack_from("<H", data, position)[0]
code, size = header >> 6, header & 63
position += 2
if size == 63:
if position + 4 > len(data):
raise ValueError("Truncated long SWF tag")
size = struct.unpack_from("<I", data, position)[0]
position += 4
if position + size > len(data):
raise ValueError("Truncated SWF tag payload")
yield code, data[position:position + size]
position += size
if code == 0:
if size != 0 or position != len(data):
raise ValueError("Invalid SWF end tag")
return
raise ValueError("Missing SWF end tag")
def encode_tag(code: int, data: bytes) -> bytes:
size = len(data)
return (struct.pack("<H", (code << 6) | min(size, 63))
+ (struct.pack("<I", size) if size >= 63 else b"") + data)
def symbols(data: bytes) -> list[tuple[int, bytes]]:
if len(data) < 2:
raise ValueError("Truncated SymbolClass tag")
count = struct.unpack_from("<H", data)[0]
position, result = 2, []
for _ in range(count):
if position + 3 > len(data):
raise ValueError("Truncated SymbolClass entry")
symbol_id = struct.unpack_from("<H", data, position)[0]
end = data.index(0, position + 2)
result.append((symbol_id, data[position + 2:end]))
position = end + 1
if position != len(data):
raise ValueError("Trailing SymbolClass bytes")
return result
def library_only(original: bytes) -> tuple[bytes, dict]:
"""Drop document bindings, make DoABC lazy, reject executable timelines.
DoABC's flags/name are SWF metadata, outside the ABC payload. All ABC bytes
(including checks, methods, skills, callbacks and static initializers) remain
identical and ordered. Lazy loading prevents unrelated script initializers
from running just because a library was loaded.
"""
raw = unpack_swf(original)
start = 8 + (5 + 4 * (raw[8] >> 3) + 7) // 8 + 4
output = bytearray(raw[:start])
roots, code_hashes, lazy_changed = [], [], 0
counts = Counter()
for code, payload in tags(raw):
counts[code] += 1
if code in (12, 59, 72, 57, 71):
raise ValueError(f"Unsupported executable/import SWF tag {code}")
if code == 76:
entries = symbols(payload)
roots.extend(name.decode("utf-8") for sid, name in entries if sid == 0)
entries = [(sid, name) for sid, name in entries if sid != 0]
payload = struct.pack("<H", len(entries)) + b"".join(
struct.pack("<H", sid) + name + b"\0" for sid, name in entries)
elif code == 82:
if len(payload) < 6:
raise ValueError("Truncated DoABC")
end = payload.index(0, 4)
code_hashes.append(digest(payload[end + 1:]))
flags = struct.unpack_from("<I", payload)[0]
if flags not in (0, 1):
raise ValueError("Unknown DoABC flags")
lazy_changed += flags == 0
payload = struct.pack("<I", 1) + payload[4:]
output.extend(encode_tag(code, payload))
struct.pack_into("<I", output, 4, len(output))
result = bytes(output)
return result, {"original_sha256": digest(original), "library_sha256": digest(result),
"removed_document_classes": roots, "abc_sha256": code_hashes,
"abc_count": len(code_hashes), "lazy_flags_changed": lazy_changed,
"tag_counts": dict(counts), "abc_bytes_preserved": True}