codecs: JPEG / MPEG-1/2 yield hooks (per MCU row, per slice), banded Mpeg2.toRgba with RGB clamp table; build.py: reserved child names pass (work from the userspace session)

This commit is contained in:
4DBug committed 2026-11-01 01:16:04 -05:00
1 parent 551fd937b9
commit 05eedbc8cf
10 files changed
+246 -14

No files matched your search

+5 -1
View File
@@ -246,9 +246,13 @@ end
-- Inverse-transforms every block of a component: coef (i16, 64 per block, natural order) on a bpl x bpc block
-- grid, quantisation table q (64 u16, natural order), into plane (u16 samples, (bpl * size) per row) at size x
-- size samples per block. p1 is PASS1_BITS (2 for 8-bit, 1 for 12-bit), center the level shift, maxv the top.
function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, size: number, plane: buffer, p1: number, center: number, maxv: number)
-- hook (optional): a cooperative yield point after every block row (pcall'ed: it can't throw through here).
function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, size: number, plane: buffer, p1: number, center: number, maxv: number, hook: (() -> ())?)
local stride = bpl * size
for by = 0, bpc - 1 do
if hook and by > 0 then
pcall(hook)
end
for bx = 0, bpl - 1 do
local off = (by * bpl + bx) * 128
local out = by * size * stride + bx * size
+5
View File
@@ -60,6 +60,7 @@ export type Params = {
crg: buffer,
cbg: buffer,
toByte: buffer?, -- maxv + 1 u8 entries (12-bit), nil for 8-bit
hook: (() -> ())?, -- a cooperative yield point every 8 rows (pcall'ed: it can't throw through here)
}
-- jdcolor.c build_ycc_rgb_table
@@ -160,7 +161,11 @@ function Color.run(P: Params)
local crr, cbb, crg, cbg = P.crr, P.cbb, P.crg, P.cbg
local toByte = P.toByte
local o = 0
local hook = P.hook
for y = 0, outH - 1 do
if hook and y > 0 and y % 8 == 0 then
pcall(hook)
end
local s0, b0 = fetch(comps[1], y, outW)
if space == 0 or nc < 3 then
for x = 0, outW - 1 do
+6 -2
View File
@@ -232,6 +232,7 @@ export type State = {
scale: number,
avi1: number?, -- the AVI1 APP0 field polarity (0 = progressive, 1 / 2 = interlaced: two fields back to back)
endPos: number, -- the byte after the EOI marker (or the end of the data)
hook: (() -> ())?, -- opts.yield: the cooperative yield point (scans, IDCT, colour conversion)
}
-- parse(b, base, limit, opts, headerOnly) -> State | nil, code, message. Walks the markers in b[base, limit); with
@@ -264,6 +265,7 @@ function Decode.parse(b: buffer, base: number, n: number, opts: any, headerOnly:
scale = scale,
avi1 = nil,
endPos = n,
hook = if opts and type(opts.yield) == "function" then opts.yield else nil,
}
local iccChunks: { buffer } = {}
local ri = 0
@@ -527,6 +529,7 @@ function Decode.parse(b: buffer, base: number, n: number, opts: any, headerOnly:
ss = ss,
se = se,
al = al,
hook = st.hook,
dcL = dcL,
dcU = dcU,
acK = acK,
@@ -609,7 +612,7 @@ function Decode.output(st: State): (number?, any, any, string?)
end
q = zeroQ -- the component never appeared in a scan: mid grey
end
Dct.idctComponent(coef, q :: buffer, c.bpl, c.bpc, ssize, plane, p1, center, maxv)
Dct.idctComponent(coef, q :: buffer, c.bpl, c.bpc, ssize, plane, p1, center, maxv, st.hook)
local hIn, hOut = c.h * ssize // minSize, f.hmax
local vIn, vOut = c.v * ssize // minSize, f.vmax
local downW = math.ceil(f.width * c.h * ssize / (f.hmax * 8))
@@ -674,6 +677,7 @@ function Decode.output(st: State): (number?, any, any, string?)
crg = t[3],
cbg = t[4],
toByte = tb,
hook = st.hook,
})
return outW, outH, pixels, space
end
@@ -746,7 +750,7 @@ function Decode.outputYuv(st: State): (number?, number?, buffer?, string?)
zeroQ = zeroQ or buffer.create(128) -- the component never appeared in a scan: mid grey
q = zeroQ
end
Dct.idctComponent(coef, q :: buffer, c.bpl, c.bpc, 8, plane, 2, 128, 255)
Dct.idctComponent(coef, q :: buffer, c.bpl, c.bpc, 8, plane, 2, 128, 255, st.hook)
if i == 1 then
Yuv.crop(plane, stride, w, h, out, 0)
elseif mode == "420" then
+5
View File
@@ -374,6 +374,7 @@ export type ScanParams = {
ss: number,
se: number,
al: number,
hook: (() -> ())?, -- a cooperative yield point after every MCU row (pcall'ed: it can't throw through here)
}
-- Decodes one scan from the clean data d (cleanLen bytes, then PAD zeros). Returns the final bit position and
@@ -397,7 +398,11 @@ function Scan.run(d: buffer, cleanLen: number, rst: { number }, nrst: number, P:
local pos, eobrun, rstIdx = 0, 0, 0
local truncated, skipping = false, false
local bit = lshift(1, al)
local hook = P.hook
for u = 0, units - 1 do
if hook and u > 0 and u % ux == 0 then
pcall(hook)
end
if ri > 0 and u > 0 and u % ri == 0 then
rstIdx += 1
if rstIdx <= nrst then
+4
View File
@@ -45,6 +45,10 @@ export type DecodeOptions = {
fields: boolean?, -- two JPEGs back to back are two fields of one frame (default: when the AVI1 segment says so)
raw: boolean?, -- no colour conversion: 3 / 4 components as R, G, B (, A) bytes (default false; BLP1 BGRA JPEGs)
maxPixels: number?, -- refuse larger images (default 2^26)
-- a cooperative yield point, run after every MCU row of a scan, every block row of the IDCT and every 8 output
-- rows: e.g. a function that calls coroutine.yield() when a time budget is spent, so one image is decoded over
-- several frames (decode then runs inside a coroutine). Errors in it are ignored.
yield: (() -> ())?,
}
export type EncodeOptions = {
quality: number?, -- 1..100 (default 90), libjpeg's scaling of the T.81 annex K tables
+44 -11
View File
@@ -83,17 +83,32 @@ local RBIAS = COFF * 256 - 298 * 16 - 409 * 128 - 128
local GBIAS = COFF * 256 - 298 * 16 + 100 * 128 + 208 * 128 - 160
local BBIAS = COFF * 256 - 298 * 16 - 516 * 128 - 72
-- YCbCr 4:2:0 planes (coded strides) -> RGBA8 (alpha 255), w x h, nearest chroma like ffmpeg's default swscale path
local function toRgba(Y: buffer, Cb: buffer, Cr: buffer, cw: number, w: number, h: number, out: buffer)
-- clamp to 0..255 by lookup: RGB_CLAMP[v + COFF] = clamp(v, 0, 255) for v in [-COFF, COFF + 255] (the colour biases
-- include COFF * 256, so the shifted sums below are indices): the same values as math.clamp, 2-3x faster natively
local RGB_CLAMP = buffer.create(2 * COFF + 256)
for i = 0, 2 * COFF + 255 do
buffer.writeu8(RGB_CLAMP, i, math.clamp(i - COFF, 0, 255))
end
-- YCbCr 4:2:0 planes (coded strides) -> RGBA8 (alpha 255), w x h, nearest chroma like ffmpeg's default swscale path.
-- Rows row0 .. row1 - 1 of the planes (row0 even; default 0 .. h) into out from byte oo (default 0). hook (optional)
-- runs every 16 rows (a cooperative yield point; it is pcall'ed, so it can't throw through here). (From mpeg1-luau's
-- later banded conversion.)
local function toRgba(Y: buffer, Cb: buffer, Cr: buffer, cw: number, w: number, h: number, out: buffer, row0: number?, row1: number?, oo: number?, hook: (() -> ())?)
-- 2 x 2 pixels per chroma sample: the chroma terms are computed once per block of four
local r0: number = row0 or 0
local r1: number = row1 or h
local base: number = oo or 0
local chw = cw // 2
local w2 = w // 2 -- whole pixel pairs per row
local stride = w * 4
for y = 0, h - 1, 2 do
local C = RGB_CLAMP
local n = 0
for y = r0, r1 - 1, 2 do
local yo = y * cw
local co = (y // 2) * chw
local o = y * stride
local two = y + 1 < h
local o = base + (y - r0) * stride
local two = y + 1 < r1
for _ = 1, w2 do
local cb, cr = readu8(Cb, co), readu8(Cr, co)
co += 1
@@ -101,14 +116,14 @@ local function toRgba(Y: buffer, Cb: buffer, Cr: buffer, cw: number, w: number,
local gg = GBIAS - cb * 100 - cr * 208
local bb = cb * 516 + BBIAS
local yv = readu8(Y, yo) * 298
writeu32(out, o, math.clamp(rshift(yv + rr, 8) - COFF, 0, 255) + math.clamp(rshift(yv + gg, 8) - COFF, 0, 255) * 256 + math.clamp(rshift(yv + bb, 8) - COFF, 0, 255) * 65536 + 0xFF000000)
writeu32(out, o, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
yv = readu8(Y, yo + 1) * 298
writeu32(out, o + 4, math.clamp(rshift(yv + rr, 8) - COFF, 0, 255) + math.clamp(rshift(yv + gg, 8) - COFF, 0, 255) * 256 + math.clamp(rshift(yv + bb, 8) - COFF, 0, 255) * 65536 + 0xFF000000)
writeu32(out, o + 4, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
if two then
yv = readu8(Y, yo + cw) * 298
writeu32(out, o + stride, math.clamp(rshift(yv + rr, 8) - COFF, 0, 255) + math.clamp(rshift(yv + gg, 8) - COFF, 0, 255) * 256 + math.clamp(rshift(yv + bb, 8) - COFF, 0, 255) * 65536 + 0xFF000000)
writeu32(out, o + stride, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
yv = readu8(Y, yo + cw + 1) * 298
writeu32(out, o + stride + 4, math.clamp(rshift(yv + rr, 8) - COFF, 0, 255) + math.clamp(rshift(yv + gg, 8) - COFF, 0, 255) * 256 + math.clamp(rshift(yv + bb, 8) - COFF, 0, 255) * 65536 + 0xFF000000)
writeu32(out, o + stride + 4, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
end
yo += 2
o += 8
@@ -121,9 +136,14 @@ local function toRgba(Y: buffer, Cb: buffer, Cr: buffer, cw: number, w: number,
local bb = cb * 516 + BBIAS
for k = 0, if two then 1 else 0 do
local yv = readu8(Y, yo + k * cw) * 298
writeu32(out, o + k * stride, math.clamp(rshift(yv + rr, 8) - COFF, 0, 255) + math.clamp(rshift(yv + gg, 8) - COFF, 0, 255) * 256 + math.clamp(rshift(yv + bb, 8) - COFF, 0, 255) * 65536 + 0xFF000000)
writeu32(out, o + k * stride, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
end
end
n += 2
if hook and n >= 16 then
n = 0
pcall(hook)
end
end
end
D.toRgba = toRgba
@@ -175,6 +195,9 @@ export type Core = {
nextPicture: (atEnd: boolean, tag: any) -> (Store?, boolean),
toRgba: (s: Store, out: buffer) -> (),
reset: () -> (),
-- a cooperative yield point (e.g. coroutine.yield when over a time budget), run after every slice and every 16
-- rows of colour conversion; nil = none. pcall'ed: it can't throw through the native code.
setYield: (hook: (() -> ())?) -> (),
state: {
width: number,
height: number,
@@ -290,6 +313,7 @@ function D.new(maxPixels: number?): Core
local PMV = table.create(8, 0)
local FSEL = table.create(4, 0)
local blk = buffer.create(256)
local yieldHook: (() -> ())? = nil
local function peek32(): number
local bp = rshift(pos, 3)
@@ -1382,6 +1406,11 @@ function D.new(maxPixels: number?): Core
pos = (at + 4) * 8
err = false
decodeSlice(code, stop)
local hook = yieldHook
if hook then
-- the hook may suspend this coroutine: the picture state is all upvalues of this core
pcall(hook)
end
local nb = rshift(pos + 7, 3)
if nb >= stop then
break
@@ -1510,10 +1539,14 @@ function D.new(maxPixels: number?): Core
if s.chroma == 2 then
toRgba422(s.y, s.cb, s.cr, s.cw, s.w, s.h, out)
else
toRgba(s.y, s.cb, s.cr, s.cw, s.w, s.h, out)
toRgba(s.y, s.cb, s.cr, s.cw, s.w, s.h, out, 0, s.h, 0, yieldHook)
end
end
function core.setYield(hook: (() -> ())?)
yieldHook = hook
end
function core.reset()
dataLen = 0
readByte = 0
+50
View File
@@ -58,6 +58,11 @@ export type Options = {
-- display order for streams without B-frames.
rgba: boolean?, -- default true; false skips the RGBA conversion (use frame.y / cb / cr)
maxPixels: number?, -- larger sequence headers are rejected (default 4096 * 2304)
-- a cooperative yield point, run after every slice and every 16 rows of colour conversion: e.g. a function that
-- calls coroutine.yield() when a time budget is spent, to spread a picture over several frames (the decode call
-- then runs inside a coroutine). Don't call this decoder again while a decode is suspended. Errors in it are
-- ignored. See dec:setYield.
yield: (() -> ())?,
}
local Decoder = {}
@@ -83,6 +88,9 @@ export type Decoder = typeof(setmetatable(
function Mpeg2.new(opts: Options?): Decoder
local o: Options = if type(opts) == "table" then opts else {}
local core = D.new(o.maxPixels)
if type(o.yield) == "function" then
core.setYield(o.yield)
end
return setmetatable({
_core = core,
_reorder = o.reorder == true,
@@ -232,12 +240,54 @@ function Decoder.flush(self: Decoder): Frame?
return nil
end
-- Sets (or clears, nil) the cooperative yield hook (Options.yield).
function Decoder.setYield(self: Decoder, hook: (() -> ())?)
self._core.setYield(if type(hook) == "function" then hook else nil)
end
-- Forgets buffered data and references (after a seek: the next picture decoded is the next I-picture).
function Decoder.reset(self: Decoder)
self._core.reset()
self._held = nil
end
-- YCbCr 4:2:0 planes (a frame's y / cb / cr / codedWidth, or copies of them) -> RGBA8 rows row0 .. row1 - 1 of a
-- width x height picture, written to out from byte outOffset (default 0) as (row1 - row0) rows of width * 4 bytes.
-- row0 is rounded down to even (rows share chroma in pairs); defaults 0 and height. Lets the conversion run in
-- bands (on several threads) or only for the frames that are shown. The planes may hold only rows planeRow0 ..
-- (planeRow0 even, default 0; their row 0 is picture row planeRow0). hook: a cooperative yield point every 16 rows.
-- Returns false (writing nothing) when an argument is out of range. (4:2:0 only: a 4:2:2 frame's chroma has as many
-- rows as its luma; use frameImage(frame, true) for those.)
function Mpeg2.toRgba(y: buffer, cb: buffer, cr: buffer, codedWidth: number, width: number, height: number, out: buffer, row0: number?, row1: number?, outOffset: number?, planeRow0: number?, hook: (() -> ())?): boolean
if type(y) ~= "buffer" or type(cb) ~= "buffer" or type(cr) ~= "buffer" or type(out) ~= "buffer" then
return false
end
local cw, w, h = tonumber(codedWidth), tonumber(width), tonumber(height)
if not cw or not w or not h or cw % 2 ~= 0 or w < 1 or h < 1 or w > cw or cw ~= math.floor(cw) or w ~= math.floor(w) or h ~= math.floor(h) then
return false
end
local r0 = math.floor(tonumber(row0) or 0)
r0 -= r0 % 2
local r1 = math.floor(tonumber(row1) or h)
local pr = math.floor(tonumber(planeRow0) or 0)
local oo = math.floor(tonumber(outOffset) or 0)
if r0 < 0 or r1 > h or r1 <= r0 or pr < 0 or pr % 2 ~= 0 or pr > r0 or oo < 0 or oo % 4 ~= 0 then
return false
end
-- the planes must cover rows pr .. r1 - 1 (luma) and their chroma rows; out the band
local lumaRows = r1 - pr
local chromaRows = (r1 + 1) // 2 - pr // 2
if buffer.len(y) < lumaRows * cw or buffer.len(cb) < chromaRows * (cw // 2) or buffer.len(cr) < chromaRows * (cw // 2) then
return false
end
if buffer.len(out) < oo + (r1 - r0) * w * 4 then
return false
end
-- rows relative to the planes' first row
D.toRgba(y, cb, cr, cw, w, h, out, r0 - pr, r1 - pr, oo, if type(hook) == "function" then hook else nil)
return true
end
-- frameImage(frame, rgba?) -> Types.Image: a copy of a decoded frame (the decoder reuses its planes), cropped to
-- width x height: yuv420p from frame.y / cb / cr (BT.601 limited range, meta.source.range = "limited"; 4:2:2 chroma
-- averaged over line pairs of the same field), or rgba8 (the decoder's BT.601 conversion) when rgba is true. nil for
+29
View File
@@ -270,4 +270,33 @@ do
end, { Fx.fields.data }, { mutations = 3, garbage = 10 })
end
do -- opts.yield: the cooperative hook runs (scans, IDCT, colour), output unchanged, inside a coroutine, errors ignored
local s = Fx.streams[1]
local f = s.frames[1]
local b = buffer.fromstring(f.data)
local calls = 0
local img = Jpeg.decode(b, 0, nil, { yield = function()
calls += 1
end }) :: Types.Image
t:check(img and calls > 0, "yield hook runs (" .. calls .. " calls)")
t:eq(Crc32.of(img.pixels), f.rgba, "yield: rgba8 unchanged")
local yuv = Jpeg.decode(b, 0, nil, { yuv = true, yield = function() end }) :: Types.Image
t:eq(Crc32.of(yuv.pixels), f.yuv, "yield: yuv420p unchanged")
local resumes = 0
local co = coroutine.create(function()
return Jpeg.decode(b, 0, nil, { yield = coroutine.yield })
end)
local ok, res = coroutine.resume(co)
while ok and coroutine.status(co) == "suspended" do
resumes += 1
ok, res = coroutine.resume(co)
end
t:check(ok and resumes > 1 and res ~= nil, "a decode suspended at the hook resumes")
t:eq(res and Crc32.of((res :: any).pixels), f.rgba, "suspended decode: same pixels")
local e = Jpeg.decode(b, 0, nil, { yield = function()
error("hook error")
end }) :: Types.Image
t:eq(e and Crc32.of(e.pixels), f.rgba, "an erroring hook is ignored")
end
t:done()
+73
View File
@@ -291,6 +291,79 @@ do -- rgba = false: planes only, frameImage still gives both
t:check(y.format == "yuv420p" and buffer.readu8(y.pixels, W) == buffer.readu8(f.y, f.codedWidth), "frameImage crops the coded width")
end
do -- toRgba in bands (from bug tv's workers): whole, row bands into one buffer, bands of plane copies; odd sizes
local function bands(f: any, step: number): buffer
local w, h, cw = f.width, f.height, f.codedWidth
local out = buffer.create(w * h * 4)
for r0 = 0, h - 1, step do
local r1 = math.min(h, r0 + step)
-- copies holding only rows r0 .. r1 - 1 (planeRow0 = r0), converted into a band, then placed
local chw, c0, c1 = cw // 2, r0 // 2, (r1 + 1) // 2
local y = buffer.create((r1 - r0) * cw)
buffer.copy(y, 0, f.y, r0 * cw, (r1 - r0) * cw)
local cb, cr = buffer.create((c1 - c0) * chw), buffer.create((c1 - c0) * chw)
buffer.copy(cb, 0, f.cb, c0 * chw, (c1 - c0) * chw)
buffer.copy(cr, 0, f.cr, c0 * chw, (c1 - c0) * chw)
local band = buffer.create((r1 - r0) * w * 4)
assert(Mpeg1.toRgba(y, cb, cr, cw, w, h, band, r0, r1, 0, r0))
buffer.copy(out, r0 * w * 4, band)
end
return out
end
local dec = Mpeg1.new({ rgba = false })
local u1 = main.units[1]
local f = dec:decode(mainFile, u1[1], u1[2]) :: any
local whole = buffer.create(f.width * f.height * 4)
t:check(Mpeg1.toRgba(f.y, f.cb, f.cr, f.codedWidth, f.width, f.height, whole), "toRgba whole frame")
t:check(buffer.tostring(whole) == ref[1], "toRgba whole = the decoder's RGBA")
local into = buffer.create(f.width * f.height * 4)
for r0 = 0, f.height - 1, 32 do
Mpeg1.toRgba(f.y, f.cb, f.cr, f.codedWidth, f.width, f.height, into, r0, math.min(f.height, r0 + 32), r0 * f.width * 4)
end
t:check(buffer.tostring(into) == ref[1], "toRgba row bands into one buffer")
t:check(buffer.tostring(bands(f, 16)) == ref[1], "toRgba bands of plane copies (planeRow0)")
local fo = STREAMS[6] -- 173 x 95
local bo = b64(fo.file)
local odec = Mpeg1.new({ rgba = false })
local uo = fo.units[1]
local of = odec:decode(bo, uo[1], uo[2]) :: any
local oimg = Mpeg1.frameImage(of, true) :: Types.Image
t:check(of and buffer.tostring(bands(of, 10)) == buffer.tostring(oimg.pixels), "toRgba bands at an odd size")
t:check(Mpeg1.toRgba(f.y, f.cb, f.cr, f.codedWidth, f.width, f.height, buffer.create(4)) == false, "toRgba: out too small")
t:check(Mpeg1.toRgba(f.y, f.cb, f.cr, 7, f.width, f.height, whole) == false, "toRgba: odd coded width")
t:check(Mpeg1.toRgba(f.y, f.cb, f.cr, f.codedWidth, f.width, f.height, whole, 4, 2) == false, "toRgba: empty band")
t:check(Mpeg1.toRgba(f.y, f.cb, f.cr, f.codedWidth, f.width, f.height, whole, 0, nil, 2) == false, "toRgba: unaligned offset")
end
do -- the cooperative yield hook: after every slice and every 16 rows of conversion; output unchanged; errors ignored
local calls = 0
local dec = Mpeg1.new({ yield = function()
calls += 1
end })
local u1 = main.units[1]
local f = dec:decode(mainFile, u1[1], u1[2]) :: any
t:check(f ~= nil and buffer.tostring(f.pixels) == ref[1] and calls > 0, "yield hook runs, same frame")
local inCo = 0
local co = coroutine.create(function()
local d2 = Mpeg1.new()
d2:setYield(function()
coroutine.yield()
end)
return d2:decode(mainFile, u1[1], u1[2])
end)
local ok, res = coroutine.resume(co)
while ok and coroutine.status(co) == "suspended" do
inCo += 1
ok, res = coroutine.resume(co)
end
t:check(ok and inCo > 1 and res ~= nil and buffer.tostring((res :: any).pixels) == ref[1], "a decode suspended at the hook resumes to the same frame")
local bad = Mpeg1.new({ yield = function()
error("hook error")
end })
local g = bad:decode(mainFile, u1[1], u1[2]) :: any
t:check(g ~= nil and buffer.tostring(g.pixels) == ref[1], "an erroring hook is ignored")
end
do -- reset (seek): the next picture must be an I-picture again
local dec = Mpeg1.new()
for i = 1, 5 do
+25
View File
@@ -310,10 +310,35 @@ def files(root, ext=".luau"):
yield os.path.join(d, f)
# Module names that are also properties of a ModuleScript: `script.Parent.Video.Source` reads the Video ModuleScript's
# Source property (its code) instead of the child module named Source, so those path parts become :FindFirstChild.
RESERVED_CHILD_NAMES = {"Source", "Name", "ClassName", "Archivable", "LinkedSource", "Capabilities", "Sandboxed", "Tags"}
REQUIRE_PATH = re.compile(r"require\((script(?:\.Parent)*)((?:\.[A-Za-z_]\w*)+)\)")
def pass_reserved_children(src):
def sub(m):
parts = m.group(2).split(".")[1:]
if not any(p in RESERVED_CHILD_NAMES for p in parts):
return m.group(0)
out = m.group(1)
for p in parts:
out += ':FindFirstChild("%s")' % p if p in RESERVED_CHILD_NAMES else "." + p
return "require(%s)" % out
return REQUIRE_PATH.sub(sub, src)
def build_roblox():
out = os.path.join(BUILD, "roblox", "Formats")
shutil.rmtree(os.path.join(BUILD, "roblox"), ignore_errors=True)
darklua(ROBLOX_RULES, os.path.relpath(SRC, ROOT), os.path.relpath(out, ROOT), ROOT)
for f in files(out):
with open(f) as fh:
src = fh.read()
fixed = pass_reserved_children(src)
if fixed != src:
with open(f, "w") as fh:
fh.write(fixed)
n = sum(1 for _ in files(out))
print("build: roblox %3d modules -> %s" % (n, os.path.relpath(out, ROOT)))