tools/gen_registry.py now also writes src/Rosetta/Sniffs.luau: each format's sniff copied (tools/sniff_extract.py) with the top-level locals, types and load-time loops it reaches; requires outside core/ become proxies that require on first field access. 198 of 199 sniffs are copied (model/fbx reads Binary.MAGIC at load time: its module sniffs). Registry entries gain sniff (0 copy, 1 copy reaching a codec / container, 2 module) and variant (the module has variant(r)). init.luau detect: the extension's formats first, then by sniff cost and registry order; a sniff runs only while its cap (1, or LATE) could still win, ties to registry order, so results equal the eager path. F.detect loads the winner only when it has variant(). tests/detect.luau: every copied sniff against its module's on samples of every fixture module (tools/fixtures/detect.py), scores within 0..1, and F.detect against the eager reference with no name, the right / wrong / unknown extension and per category. Outside init / Registry / gen_registry: tools/fixtures.py (LAST generators run after the others and rerun when their outputs change), tools/lint_rules.py (Sniffs is api layer, skipped by DUP), docs/STYLE.md and docs/API.md notes, build/ regenerated.
57 lines
2.2 KiB
Python
57 lines
2.2 KiB
Python
# Detection fixtures (tools/fixtures.py): samples of every other fixture module, for tests/detect.luau (lazy detection
|
|
# against the eager reference). Runs after the other generators (LAST) and reads their tests/gen output.
|
|
# tests/gen/detect_blobs_<1..4>.luau { { from = "<module>", data = <bytes> } }: up to PER_MODULE string literals of
|
|
# 16 bytes or more per module, evenly spaced, cut to CUT bytes, without repeats
|
|
import hashlib, os, re
|
|
|
|
LAST = True
|
|
PER_MODULE = 6
|
|
CUT = 16384
|
|
PARTS = 4
|
|
PART_BYTES = 5_000_000
|
|
HEADER = "-- generated by tools/fixtures.py"
|
|
LITERAL = re.compile(rb'"([^"]*)"')
|
|
ESCAPE = re.compile(rb"\\x([0-9a-f]{2})")
|
|
|
|
|
|
def decode(raw):
|
|
return ESCAPE.sub(lambda m: bytes([int(m.group(1), 16)]), raw)
|
|
|
|
|
|
def generate(ctx):
|
|
names = sorted(f[:-5] for f in os.listdir(ctx.gen) if f.endswith(".luau") and not f.startswith("detect_"))
|
|
seen, blobs = set(), []
|
|
for name in names:
|
|
with open(os.path.join(ctx.gen, name + ".luau"), "rb") as f:
|
|
text = f.read()
|
|
if not text.startswith(HEADER.encode()):
|
|
continue
|
|
lits = [m.span(1) for m in LITERAL.finditer(text) if m.end(1) - m.start(1) >= 16]
|
|
lits = [(a, b) for a, b in lits if len(decode(text[a:min(b, a + 64)])) >= 16]
|
|
if not lits:
|
|
continue
|
|
step = max(1, len(lits) / PER_MODULE)
|
|
picks = sorted({int(i * step) for i in range(PER_MODULE)} & set(range(len(lits))))
|
|
for i in picks:
|
|
a, b = lits[i]
|
|
data = decode(text[a:min(b, a + CUT * 4)])[:CUT]
|
|
h = hashlib.sha1(data).digest()
|
|
if h in seen:
|
|
continue
|
|
seen.add(h)
|
|
blobs.append({"from": name, "data": data})
|
|
parts, cur, size = [], [], 0
|
|
for b in blobs:
|
|
if size + len(b["data"]) > PART_BYTES and cur:
|
|
parts.append(cur)
|
|
cur, size = [], 0
|
|
cur.append(b)
|
|
size += len(b["data"])
|
|
parts.append(cur)
|
|
if len(parts) > PARTS:
|
|
raise SystemExit("fixtures/detect: %d parts, raise PARTS (and the requires in tests/detect.luau)" % len(parts))
|
|
while len(parts) < PARTS:
|
|
parts.append([])
|
|
for i, p in enumerate(parts, 1):
|
|
ctx.emit("detect_blobs_%d" % i, p)
|