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
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
+77
-44
@@ -78,6 +78,56 @@ end
|
||||
|
||||
local LATE: { [string]: number } = { br = 0.3 } -- archive/br: a trial decode of up to 256 KB
|
||||
|
||||
-- detection order: by the cost of a sniff (Registry.sniff: Sniffs.luau, Sniffs.luau reaching a codec, the format
|
||||
-- module), then registry order; the costly LATE sniffs last
|
||||
local order: { number }? = nil
|
||||
local byExt: { [string]: { number } } = {}
|
||||
local Sniffs: { [string]: (any) -> number }? = nil
|
||||
|
||||
local function sniffOrder(): { number }
|
||||
local o = order
|
||||
if o then
|
||||
return o
|
||||
end
|
||||
local list: { number } = {}
|
||||
for idx, e in Registry do
|
||||
table.insert(list, idx)
|
||||
for _, x in e.extensions do
|
||||
byExt[x] = byExt[x] or {}
|
||||
table.insert(byExt[x], idx)
|
||||
end
|
||||
end
|
||||
local function rank(idx: number): number
|
||||
local e = Registry[idx]
|
||||
return if LATE[e.name] then 3 else e.sniff
|
||||
end
|
||||
table.sort(list, function(a: number, b: number): boolean
|
||||
local ra, rb = rank(a), rank(b)
|
||||
if ra ~= rb then
|
||||
return ra < rb
|
||||
end
|
||||
return a < b
|
||||
end)
|
||||
order = list
|
||||
return list
|
||||
end
|
||||
|
||||
local function sniffer(e: Registry.Entry): ((any) -> number)?
|
||||
if e.sniff < 2 then
|
||||
local s = Sniffs
|
||||
if not s then
|
||||
s = require("@rosetta/Sniffs") :: any
|
||||
Sniffs = s
|
||||
end
|
||||
local f = (s :: { [string]: (any) -> number })[e.path]
|
||||
if f then
|
||||
return f
|
||||
end
|
||||
end
|
||||
local m = module(e)
|
||||
return if m and type(m.sniff) == "function" then m.sniff else nil
|
||||
end
|
||||
|
||||
local function detect(buf: buffer, name: string?, category: string?, format: string?): (Registry.Entry?, Err.Err?)
|
||||
if format then
|
||||
local e = find(format, category)
|
||||
@@ -87,52 +137,35 @@ local function detect(buf: buffer, name: string?, category: string?, format: str
|
||||
return e, nil
|
||||
end
|
||||
local ext = Input.extension(name)
|
||||
local list = sniffOrder()
|
||||
local hits = if ext then byExt[ext] else nil
|
||||
local best: Registry.Entry? = nil
|
||||
local bestScore, bestIdx = 0, 0
|
||||
-- sniffs score 0..1 (core/Sniff); the extension's format scoring 1 cannot be beaten. Formats with a costly sniff
|
||||
-- (LATE: the most they score) run last, and only when they could still win; ties go to registry order.
|
||||
local late: { number }? = nil
|
||||
for idx, e in Registry do
|
||||
if inCategory(e, category) and (e.caps.decode or e.caps.open) then
|
||||
local cap = LATE[e.name]
|
||||
if cap then
|
||||
late = late or {}
|
||||
table.insert(late :: { number }, idx)
|
||||
continue
|
||||
end
|
||||
local m = module(e)
|
||||
if m and m.sniff then
|
||||
local ok, score = pcall(m.sniff, Bytes.reader(buf))
|
||||
if ok and type(score) == "number" and score > 0 then
|
||||
local extHit = ext and table.find(e.extensions, ext)
|
||||
if extHit then
|
||||
score += 0.01 -- ties go to the extension
|
||||
end
|
||||
if score > bestScore then
|
||||
best, bestScore, bestIdx = e, score, idx
|
||||
if extHit and score >= 1.01 then
|
||||
return e, nil
|
||||
end
|
||||
end
|
||||
-- sniffs score 0..1 (core/Sniff), +0.01 for the extension's formats; the best wins, ties go to registry order. The
|
||||
-- extension's formats run first, and a sniff runs only while its cap (1, or LATE) could still win.
|
||||
for pass = 1, 2 do
|
||||
local ids = if pass == 1 then hits else list
|
||||
if ids then
|
||||
for _, idx in ids do
|
||||
if pass == 2 and bestScore > 1 then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if late then
|
||||
for _, idx in late do
|
||||
local e = Registry[idx]
|
||||
local extHit = ext and table.find(e.extensions, ext)
|
||||
local cap = LATE[e.name] + (if extHit then 0.01 else 0)
|
||||
if cap > bestScore or (cap == bestScore and idx < bestIdx) then
|
||||
local m = module(e)
|
||||
if m and m.sniff then
|
||||
local ok, score = pcall(m.sniff, Bytes.reader(buf))
|
||||
if ok and type(score) == "number" and score > 0 then
|
||||
if extHit then
|
||||
score += 0.01
|
||||
end
|
||||
if score > bestScore or (score == bestScore and idx < bestIdx) then
|
||||
best, bestScore, bestIdx = e, score, idx
|
||||
local e = Registry[idx]
|
||||
local extHit = pass == 1 or (hits ~= nil and table.find(hits, idx) ~= nil)
|
||||
if (pass == 1 or not extHit) and inCategory(e, category) and (e.caps.decode or e.caps.open) then
|
||||
local cap = (LATE[e.name] or 1) + (if extHit then 0.01 else 0)
|
||||
if cap > bestScore or (cap == bestScore and idx < bestIdx) then
|
||||
local sniff = sniffer(e)
|
||||
if sniff then
|
||||
local ok, score = pcall(sniff, Bytes.reader(buf))
|
||||
if ok and type(score) == "number" and score > 0 then
|
||||
if extHit then
|
||||
score += 0.01
|
||||
end
|
||||
if score > bestScore or (score == bestScore and idx < bestIdx) then
|
||||
best, bestScore, bestIdx = e, score, idx
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -696,7 +729,7 @@ function F.detect(src: any, opts: Options?): (string?, Err.Err?)
|
||||
return nil, err
|
||||
end
|
||||
-- a format may name the file's flavour more precisely (video/mkv: "webm" for DocType webm)
|
||||
local m = module(entry)
|
||||
local m = if entry.variant then module(entry) else nil
|
||||
if m and type(m.variant) == "function" then
|
||||
local ok, v = pcall(m.variant, Bytes.reader(buf))
|
||||
if ok and type(v) == "string" then
|
||||
|
||||
Reference in new issue
Block a user