api: lazy format detection (Sniffs.luau): F.detect loads no format module
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.
This commit is contained in:
1 parent
dde6be117c
commit
e05392ee8e
18 files changed
+16513
-1466
No files matched your search
+48
-1
@@ -3,6 +3,8 @@
|
||||
# src/Rosetta/<category>/<fmt>/init.luau and writes
|
||||
# src/Rosetta/Registry.luau a static list (Roblox has no dynamic require): each format's contract metadata, copied
|
||||
# from its init.luau, and a load() that requires the module on first use
|
||||
# src/Rosetta/Sniffs.luau each format's sniff, copied with what it reaches (tools/sniff_extract.py), so detection
|
||||
# runs without requiring the format modules
|
||||
# docs/FORMATS.md the capability matrix, from each contract's caps
|
||||
# README.md the format table (read / write) and the codec + container list, between the markers
|
||||
# <!-- formats:begin --> / <!-- formats:end --> and <!-- codecs:begin --> / <!-- codecs:end -->
|
||||
@@ -11,6 +13,9 @@
|
||||
# --check: exit 1 when any of them is out of date (tools/lint.sh runs this).
|
||||
import os, re, sys
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
import sniff_extract
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
SRC = os.path.join(ROOT, "src", "Rosetta")
|
||||
CATEGORIES = ["image", "audio", "video", "model", "cad", "font", "archive", "data", "vector", "layered", "subtitle",
|
||||
@@ -50,6 +55,9 @@ def scan():
|
||||
sys.exit("gen_registry: %s/%s/init.luau: missing contract fields %s" % (cat, fmt, ", ".join(missing)))
|
||||
c["path"] = cat + "/" + fmt
|
||||
c["folder_category"] = cat
|
||||
text = open(init).read()
|
||||
c["sniff_cost"], c["sniff_block"], self_name = sniff_extract.extract(text, c["path"])
|
||||
c["variant"] = sniff_extract.has_variant(text, self_name)
|
||||
formats.append(c)
|
||||
return formats
|
||||
|
||||
@@ -60,6 +68,8 @@ def registry(formats):
|
||||
"-- Registry: GENERATED by tools/gen_registry.py from the format folders - do not edit.",
|
||||
"-- One entry per format: the contract metadata (copied from its init.luau) and load(), which requires the format",
|
||||
"-- module on first use (a static require, as Roblox needs). Order: category, then name.",
|
||||
"-- sniff: where detection finds the format's sniff (tools/sniff_extract.py): 0 Sniffs.luau, 1 Sniffs.luau reaching a",
|
||||
"-- codec / container on first call, 2 the format module. variant: the module has variant(r).",
|
||||
"export type Entry = {",
|
||||
"\tname: string,",
|
||||
"\tcategory: string,",
|
||||
@@ -68,6 +78,8 @@ def registry(formats):
|
||||
"\tmime: { string },",
|
||||
"\tdeps: { string },",
|
||||
"\tcaps: { [string]: any },",
|
||||
"\tsniff: number,",
|
||||
"\tvariant: boolean?,",
|
||||
"\tload: () -> any,",
|
||||
"}",
|
||||
"",
|
||||
@@ -83,6 +95,8 @@ def registry(formats):
|
||||
"\t\tmime = %s," % c["mime"],
|
||||
"\t\tdeps = %s," % c["deps"],
|
||||
"\t\tcaps = %s," % c["caps"],
|
||||
"\t\tsniff = %d," % c["sniff_cost"],
|
||||
] + (["\t\tvariant = true,"] if c["variant"] else []) + [
|
||||
"\t\tload = function()",
|
||||
'\t\t\treturn require("@rosetta/%s")' % c["path"],
|
||||
"\t\tend,",
|
||||
@@ -92,6 +106,38 @@ def registry(formats):
|
||||
return "\n".join(out)
|
||||
|
||||
|
||||
def sniffs(formats):
|
||||
out = [
|
||||
"--!strict",
|
||||
"--!optimize 2",
|
||||
"-- Sniffs: GENERATED by tools/gen_registry.py from the format folders - do not edit.",
|
||||
"-- Each format's sniff (format path -> sniff(r)), copied from its init.luau with the top-level locals and types it",
|
||||
"-- reaches (tools/sniff_extract.py), so detection requires no format module. Requires outside core/ are proxies that",
|
||||
"-- require on first use. Formats whose sniff needs their module (Registry sniff = 2) are not here.",
|
||||
"local S: { [string]: (any) -> number } = {}",
|
||||
"",
|
||||
"local function lazy(load: () -> any): any",
|
||||
"\tlocal proxy = {}",
|
||||
"\treturn setmetatable(proxy, {",
|
||||
"\t\t__index = function(_, k: any): any",
|
||||
"\t\t\tlocal m = load()",
|
||||
"\t\t\tsetmetatable(proxy, { __index = m })",
|
||||
"\t\t\treturn m[k]",
|
||||
"\t\tend,",
|
||||
"\t})",
|
||||
"end",
|
||||
"",
|
||||
]
|
||||
for c in formats:
|
||||
if c["sniff_block"] is None:
|
||||
continue
|
||||
out.append("do -- %s" % c["path"])
|
||||
out += c["sniff_block"]
|
||||
out += ["end", ""]
|
||||
out += ["return S", ""]
|
||||
return "\n".join(out)
|
||||
|
||||
|
||||
def caps_dict(lit):
|
||||
"""{ decode = true, depths = { 8 } } -> ordered [(key, text)] at the top level."""
|
||||
body = lit.strip()[1:-1]
|
||||
@@ -220,6 +266,7 @@ def main():
|
||||
readme_path = os.path.join(ROOT, "README.md")
|
||||
outputs = {
|
||||
os.path.join(SRC, "Registry.luau"): registry(formats),
|
||||
os.path.join(SRC, "Sniffs.luau"): sniffs(formats),
|
||||
os.path.join(ROOT, "docs", "FORMATS.md"): formats_md(formats),
|
||||
readme_path: readme(formats, readme_path),
|
||||
}
|
||||
@@ -239,7 +286,7 @@ def main():
|
||||
sys.exit(1)
|
||||
print("gen_registry: up to date (%d formats)" % len(formats))
|
||||
else:
|
||||
print("gen_registry: %d formats -> src/Rosetta/Registry.luau, docs/FORMATS.md, README.md%s" % (
|
||||
print("gen_registry: %d formats -> src/Rosetta/Registry.luau, Sniffs.luau, docs/FORMATS.md, README.md%s" % (
|
||||
len(formats), "" if stale else " (unchanged)"))
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user