archive/vswap (Wolfenstein 3D pages: walls, sprites and sounds through a palette passed in) and vector/gamemaps (GAMEMAPS + MAPHEAD, Carmack + RLEW, into a TileMap); test data from the fixture's own writers
This commit is contained in:
1 parent
719abb97c0
commit
e51fd3585e
15 files changed
+3139
-1085
No files matched your search
@@ -3041,6 +3041,18 @@ local list= {
|
||||
return __include("archive/vpk")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "vswap",
|
||||
category = "archive",
|
||||
path = "archive/vswap",
|
||||
extensions = { "wl6", "wl1", "sod", "sdm", "vswap" },
|
||||
mime = { "application/x-wolf3d-vswap" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Archive", "codecs/image/Pixels" },
|
||||
caps = { decode = true, encode = false, images = true, sounds = true },
|
||||
load = function()
|
||||
return __include("archive/vswap")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "wad",
|
||||
category = "archive",
|
||||
@@ -3257,6 +3269,18 @@ local list= {
|
||||
return __include("data/yaml")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "gamemaps",
|
||||
category = "vector",
|
||||
path = "vector/gamemaps",
|
||||
extensions = { "wl6", "wl1", "sod", "sdm" },
|
||||
mime = { "application/x-wolf3d-gamemaps" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Input", "Types" },
|
||||
caps = { decode = true, encode = false, tilemap = true },
|
||||
load = function()
|
||||
return __include("vector/gamemaps")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "svg",
|
||||
category = "vector",
|
||||
@@ -11221,6 +11245,270 @@ end
|
||||
|
||||
return Vpk
|
||||
end
|
||||
__FILES["archive/vswap"] = function()
|
||||
local __compat = __compat local buffer, bit32, type, __iter = __compat.buffer, __compat.bit32, __compat.type, __compat.__iter --!strict
|
||||
-- archive/vswap: Wolfenstein 3D / Spear of Destiny page files (VSWAP.WL1 / .WL6 / .SOD / .SDM: u16 chunks, sprite
|
||||
-- start, sound start; u32 offsets, u16 lengths; ModdingWiki "VSWAP Format", no code taken), read only. Pages load as
|
||||
-- entries walls/NNN, sprites/NNN, sounds/NNN and soundinfo; Vswap.image turns walls (64 x 64 column-major) and
|
||||
-- sprites (t_compshape posts) into Images through a palette passed in (the game's palette is not redistributable),
|
||||
-- Vswap.sound the digitized sounds into Audio (u8 PCM, 7042 Hz). Not supported: writing, Blake Stone / Corridor 7.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = __include("core/Err")
|
||||
local Bytes = __include("core/Bytes")
|
||||
local Types = __include("Types")
|
||||
local Archive = __include("containers/Archive")
|
||||
local Pixels = __include("codecs/image/Pixels")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Vswap = {
|
||||
name = "vswap",
|
||||
category = "archive",
|
||||
extensions = { "wl6", "wl1", "sod", "sdm", "vswap" },
|
||||
mime = { "application/x-wolf3d-vswap" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Archive", "codecs/image/Pixels" },
|
||||
caps = { decode = true, encode = false, images = true, sounds = true },
|
||||
}
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
local readu8, readu16, readi16, writeu32 = buffer.readu8, buffer.readu16, buffer.readi16, buffer.writeu32
|
||||
local SAMPLE_RATE = 7042 -- the digitized sounds' rate (wolf4sdl ORIGSAMPLERATE)
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
local function header(r)
|
||||
if r:remaining() < 6 then
|
||||
return nil, 0, 0
|
||||
end
|
||||
local n, ss, so = readu16(r.buf, r.base), readu16(r.buf, r.base + 2), readu16(r.buf, r.base + 4)
|
||||
if n == 0 or ss > so or so > n or 6 + n * 6 > r:remaining() then
|
||||
return nil, 0, 0
|
||||
end
|
||||
return n, ss, so
|
||||
end
|
||||
|
||||
function Vswap.sniff(r)
|
||||
local n = header(r)
|
||||
if not n then
|
||||
return 0
|
||||
end
|
||||
local size = r.limit - r.base
|
||||
local b = r.buf
|
||||
local last = 6 + n * 6
|
||||
local nonzero = 0
|
||||
for i = 0, n - 1 do
|
||||
local o = buffer.readu32(b, r.base + 6 + i * 4)
|
||||
local l = readu16(b, r.base + 6 + n * 4 + i * 2)
|
||||
if o ~= 0 then
|
||||
if o < last or o + l > size then
|
||||
return 0
|
||||
end
|
||||
last = o
|
||||
nonzero =nonzero+ 1
|
||||
end
|
||||
end
|
||||
return nonzero > 0 and 0.6 or 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
function Vswap.decode(r, opts)
|
||||
if r:remaining() < 6 then
|
||||
return nil, Err.new(Err.TRUNCATED, "header", 0)
|
||||
end
|
||||
local n, ss, so = header(r)
|
||||
if not n then
|
||||
return nil, Err.new((6 + readu16(r.buf, r.base) * 6 > r:remaining() and{Err.TRUNCATED }or{Err.CORRUPT})[1], "bad page directory", 0)
|
||||
end
|
||||
if n > (opts and opts.maxChunks or 65535) then
|
||||
return nil, Err.new(Err.LIMIT, "too many pages: " .. n)
|
||||
end
|
||||
local size = r.limit - r.base
|
||||
local b = r.buf
|
||||
local a = Archive.new("vswap")
|
||||
local pages= {}
|
||||
local counts = { wall = 0, sprite = 0, sound = 0 }
|
||||
for i = 0, n - 1 do
|
||||
local o = buffer.readu32(b, r.base + 6 + i * 4)
|
||||
local l = readu16(b, r.base + 6 + n * 4 + i * 2)
|
||||
if o == 0 then
|
||||
l = 0 -- a sparse page (Spear of Destiny)
|
||||
elseif o + l > size then
|
||||
return nil, Err.new(Err.TRUNCATED, "page " .. i .. " past the end", 6 + i * 4)
|
||||
end
|
||||
local kind= i < ss and"wall" or(i < so and"sprite" or(i == n - 1 and so < n and"soundinfo" or"sound"
|
||||
)) local idx = (kind == "wall" and{i }or{(kind == "sprite" and((i - ss ))or((kind == "sound" and((i - so ))or(0
|
||||
))))})[1] local name = kind == "soundinfo" and"soundinfo" or string.format("%ss/%03d", kind, idx)
|
||||
if kind ~= "soundinfo" then
|
||||
counts[kind]=counts[kind]+ 1
|
||||
end
|
||||
table.insert(pages, { kind = kind, index = idx, offset = o, size = l })
|
||||
Archive.put(a, { name = name, size = l, dir = false, method = "store", page = i, kind = kind, load = Archive.slice(b, r.base + o, l) })
|
||||
end
|
||||
a.meta.source = { format = "vswap", chunks = n, spriteStart = ss, soundStart = so, walls = counts.wall, sprites = counts.sprite, soundPages = counts.sound }
|
||||
a.meta.vswap = { pages = pages, buf = b, base = r.base, spriteStart = ss, soundStart = so }
|
||||
return a, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
local function info(a)
|
||||
return type(a) == "table" and type(a.meta) == "table" and a.meta.vswap or nil
|
||||
end
|
||||
|
||||
local function palette(opts)
|
||||
local p = opts and opts.palette
|
||||
if p == nil then
|
||||
local g = buffer.create(768)
|
||||
for i = 0, 255 do
|
||||
buffer.fill(g, i * 3, i, 3)
|
||||
end
|
||||
return Pixels.palette(g, 0, 256), false
|
||||
end
|
||||
local src = (type(p) == "string" and{(buffer.fromstring(p) )}or{p
|
||||
})[1] local n = math.min(buffer.len(src), 768)
|
||||
local six = opts and opts.sixBit
|
||||
if six == nil then
|
||||
six = true
|
||||
for i = 0, n - 1 do
|
||||
if readu8(src, i) > 63 then
|
||||
six = false
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
if six then
|
||||
local s = buffer.create(768)
|
||||
for i = 0, n - 1 do
|
||||
local v = bit32.band(readu8(src, i), 63)
|
||||
buffer.writeu8(s, i, bit32.bor(bit32.lshift(v, 2), bit32.rshift(v, 4)))
|
||||
end
|
||||
src = s
|
||||
end
|
||||
return Pixels.palette(src, 0, math.floor(n / 3)), true
|
||||
end
|
||||
|
||||
function Vswap.pages(a)
|
||||
local v = info(a)
|
||||
return v and v.pages
|
||||
end
|
||||
|
||||
function Vswap.image(a, kind, index, opts)
|
||||
local v = info(a)
|
||||
if not v then
|
||||
return nil, Err.new(Err.ARG, "expected a decoded VSWAP archive")
|
||||
end
|
||||
local page= nil
|
||||
for _, p in __iter(v.pages) do
|
||||
if p.kind == kind and p.index == index then
|
||||
page = p
|
||||
break
|
||||
end
|
||||
end
|
||||
if not page then
|
||||
return nil, Err.new(Err.ARG, "no " .. tostring(kind) .. " " .. tostring(index))
|
||||
end
|
||||
local pal, given = palette(opts)
|
||||
local b, at, n = v.buf, v.base + page.offset, page.size
|
||||
local img = Types.Image.new(64, 64)
|
||||
local px = img.pixels
|
||||
if kind == "wall" then
|
||||
if n < 4096 then
|
||||
return nil, Err.new(Err.CORRUPT, "wall page shorter than 4096 bytes", page.offset)
|
||||
end
|
||||
for x = 0, 63 do
|
||||
for y = 0, 63 do
|
||||
writeu32(px, (y * 64 + x) * 4, buffer.readu32(pal, readu8(b, at + x * 64 + y) * 4))
|
||||
end
|
||||
end
|
||||
else
|
||||
if n < 4 then
|
||||
return nil, Err.new(Err.CORRUPT, "sprite page too short", page.offset)
|
||||
end
|
||||
local left, right = readu16(b, at), readu16(b, at + 2)
|
||||
if right < left or right > 63 or 4 + (right - left + 1) * 2 > n then
|
||||
return nil, Err.new(Err.CORRUPT, "bad sprite columns", page.offset)
|
||||
end
|
||||
for x = left, right do
|
||||
local p = readu16(b, at + 4 + (x - left) * 2)
|
||||
for _ = 1, 64 do
|
||||
if p + 2 > n then
|
||||
return nil, Err.new(Err.CORRUPT, "sprite post past the page", page.offset + p)
|
||||
end
|
||||
local endy = math.floor(readu16(b, at + p) / 2
|
||||
) if endy == 0 then
|
||||
break
|
||||
end
|
||||
if p + 6 > n then
|
||||
return nil, Err.new(Err.CORRUPT, "sprite post past the page", page.offset + p)
|
||||
end
|
||||
local src, starty = readi16(b, at + p + 2), math.floor(readu16(b, at + p + 4) / 2
|
||||
) if endy > 64 or starty > endy or src + starty < 0 or src + endy > n then
|
||||
return nil, Err.new(Err.CORRUPT, "bad sprite post", page.offset + p)
|
||||
end
|
||||
for y = starty, endy - 1 do
|
||||
writeu32(px, (y * 64 + x) * 4, buffer.readu32(pal, readu8(b, at + src + y) * 4))
|
||||
end
|
||||
p =p+ 6
|
||||
end
|
||||
end
|
||||
end
|
||||
img.meta.source = { format = "vswap", kind = kind, index = index, palette = given }
|
||||
return img, nil
|
||||
end
|
||||
|
||||
function Vswap.sounds(a)
|
||||
local v = info(a)
|
||||
if not v then
|
||||
return 0
|
||||
end
|
||||
local last = v.pages[#v.pages]
|
||||
return (last and last.kind == "soundinfo" and{(math.floor(last.size / 4 ))}or{0
|
||||
})[1]end
|
||||
|
||||
function Vswap.sound(a, index)
|
||||
local v = info(a)
|
||||
if not v then
|
||||
return nil, Err.new(Err.ARG, "expected a decoded VSWAP archive")
|
||||
end
|
||||
local si = v.pages[#v.pages]
|
||||
if not si or si.kind ~= "soundinfo" or index < 1 or index > math.floor(si.size / 4 )or index % 1 ~= 0 then
|
||||
return nil, Err.new(Err.ARG, "no sound " .. tostring(index))
|
||||
end
|
||||
local b = v.buf
|
||||
local start = readu16(b, v.base + si.offset + (index - 1) * 4)
|
||||
local len = readu16(b, v.base + si.offset + (index - 1) * 4 + 2)
|
||||
local au = Types.Audio.new(SAMPLE_RATE, 1, len)
|
||||
local s = au.samples
|
||||
local got = 0
|
||||
local p = v.soundStart + start + 1
|
||||
while got < len do
|
||||
local page = v.pages[p]
|
||||
if not page or page.kind ~= "sound" then
|
||||
return nil, Err.new(Err.CORRUPT, "sound " .. index .. " runs past the sound pages")
|
||||
end
|
||||
local take = math.min(page.size, len - got)
|
||||
local at = v.base + page.offset
|
||||
for i = 0, take - 1 do
|
||||
buffer.writef32(s, (got + i) * 4, (readu8(b, at + i) - 128) / 128)
|
||||
end
|
||||
got =got+ take
|
||||
p =p+ 1
|
||||
if page.size == 0 then
|
||||
return nil, Err.new(Err.CORRUPT, "empty sound page in sound " .. index)
|
||||
end
|
||||
end
|
||||
au.meta.source = { format = "vswap", sound = index, startPage = start, bytes = len, encoding = "u8" }
|
||||
return au, nil
|
||||
end
|
||||
|
||||
return Vswap
|
||||
end
|
||||
__FILES["archive/wad"] = function()
|
||||
local __compat = __compat local buffer, type, __iter = __compat.buffer, __compat.type, __compat.__iter --!strict
|
||||
-- archive/wad: Doom-engine WADs (IWAD / PWAD; Unofficial Doom Specs, w_wad.c / r_data.c; no code taken), plus lumps,
|
||||
@@ -184793,6 +185081,287 @@ end
|
||||
|
||||
return Vtt
|
||||
end
|
||||
__FILES["vector/gamemaps"] = function()
|
||||
local __compat = __compat local buffer, type, __iter = __compat.buffer, __compat.type, __compat.__iter --!strict
|
||||
-- vector/gamemaps: Wolfenstein 3D / Spear of Destiny maps (GAMEMAPS "TED5v1.0" + MAPHEAD: RLEW tag, 100 header
|
||||
-- offsets; map headers of 3 planes, Carmack then RLEW compressed; ModdingWiki "GameMaps Format", no code taken) into a
|
||||
-- TileMap: one tile layer per plane ("walls", "objects", "plane3") with the raw tile values as data (no tileset).
|
||||
-- Either file loads; the other comes through opts.files / opts.resolve. Not supported: writing, uncompressed
|
||||
-- (MAPTEMP / Wolf3D 1.0) maps, Huffman-compressed (Keen) maps.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = __include("core/Err")
|
||||
local Bytes = __include("core/Bytes")
|
||||
local Input = __include("core/Input")
|
||||
local Types = __include("Types")
|
||||
local Expand = __include("vector/gamemaps/Expand")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Gamemaps = {
|
||||
name = "gamemaps",
|
||||
category = "vector",
|
||||
extensions = { "wl6", "wl1", "sod", "sdm" },
|
||||
mime = { "application/x-wolf3d-gamemaps" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Input", "Types" },
|
||||
caps = { decode = true, encode = false, tilemap = true },
|
||||
}
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
local MAGIC = "TED5v1.0"
|
||||
local LAYERS = { "walls", "objects", "plane3" }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Gamemaps.sniff(r)
|
||||
if r:peek(8) == MAGIC then
|
||||
return 1
|
||||
end
|
||||
-- MAPHEAD: the RLEW tag 0xABCD, then 100 offsets (0 / -1 when absent)
|
||||
if r:remaining() >= 402 and r:remaining() <= 1024 and buffer.readu16(r.buf, r.base) == 0xABCD then
|
||||
local first = buffer.readi32(r.buf, r.base + 2)
|
||||
if first > 0 then
|
||||
return 0.7
|
||||
end
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
local function asBuffer(v)
|
||||
if type(v) == "string" then
|
||||
return buffer.fromstring(v)
|
||||
end
|
||||
return (type(v) == "buffer" and{v }or{nil
|
||||
})[1]end
|
||||
|
||||
local function sibling(opts, stem)
|
||||
local name = opts and opts.name
|
||||
local ext = name and string.match(name, "%.([^./\\]+)$") or "WL6"
|
||||
local dir = name and string.match(name, "^(.*[/\\])") or ""
|
||||
for _, n in __iter({ stem .. "." .. ext, string.upper(stem .. "." .. ext), string.lower(stem .. "." .. ext) }) do
|
||||
local got = Input.sibling(opts, dir .. n) or Input.sibling(opts, n)
|
||||
if got then
|
||||
return got
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
|
||||
|
||||
local function pair(r, opts)
|
||||
local self = buffer.create(r.limit - r.base)
|
||||
buffer.copy(self, 0, r.buf, r.base, r.limit - r.base)
|
||||
local head, maps
|
||||
if buffer.len(self) >= 8 and buffer.readstring(self, 0, 8) == MAGIC then
|
||||
maps = self
|
||||
head = asBuffer(opts and opts.mapHead) or sibling(opts, "MAPHEAD")
|
||||
if not head then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "MAPHEAD not available (opts.mapHead / opts.files / opts.resolve)")
|
||||
end
|
||||
else
|
||||
head = self
|
||||
maps = asBuffer(opts and opts.gameMaps) or sibling(opts, "GAMEMAPS")
|
||||
if not maps then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "GAMEMAPS not available (opts.gameMaps / opts.files / opts.resolve)")
|
||||
end
|
||||
end
|
||||
if buffer.len(head) < 2 then
|
||||
return nil, Err.new(Err.TRUNCATED, "MAPHEAD")
|
||||
end
|
||||
return { head = head, maps = maps, tag = buffer.readu16(head, 0) }, nil
|
||||
end
|
||||
|
||||
local function list(p)
|
||||
local out= {}
|
||||
local hn = math.min(100, math.floor((buffer.len(p.head) - 2) / 4))
|
||||
local size = buffer.len(p.maps)
|
||||
for i = 0, hn - 1 do
|
||||
local o = buffer.readi32(p.head, 2 + i * 4)
|
||||
if o > 0 and o + 38 <= size then
|
||||
local nm = buffer.readstring(p.maps, o + 22, 16)
|
||||
local z = string.find(nm, "\0", 1, true)
|
||||
table.insert(out, { slot = i + 1, name = (z and{(string.sub(nm, 1, z - 1) )}or{nm})[1], width = buffer.readu16(p.maps, o + 18), height = buffer.readu16(p.maps, o + 20), offset = o })
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
function Gamemaps.maps(r, opts)
|
||||
local p, err = pair(r, opts)
|
||||
if not p then
|
||||
return nil, err
|
||||
end
|
||||
return list(p), nil
|
||||
end
|
||||
|
||||
function Gamemaps.decode(r, opts)
|
||||
local p, err = pair(r, opts)
|
||||
if not p then
|
||||
return nil, err
|
||||
end
|
||||
local maps = list(p)
|
||||
if #maps == 0 then
|
||||
return nil, Err.new(Err.CORRUPT, "no maps in MAPHEAD")
|
||||
end
|
||||
local want = opts and opts.map
|
||||
local m= (want and{nil }or{maps[1]
|
||||
})[1] for _, x in __iter(maps) do
|
||||
if x.slot == want then
|
||||
m = x
|
||||
end
|
||||
end
|
||||
if not m then
|
||||
return nil, Err.new(Err.ARG, "no map in slot " .. tostring(want))
|
||||
end
|
||||
local w, h = m.width, m.height
|
||||
if w == 0 or h == 0 or w * h > (opts and opts.maxTiles or 2 ^ 20) then
|
||||
return nil, Err.new((w * h == 0 and{Err.CORRUPT }or{Err.LIMIT})[1], "map size " .. w .. " x " .. h)
|
||||
end
|
||||
local b = p.maps
|
||||
local size = buffer.len(b)
|
||||
local tm = Types.TileMap.new(w, h, 64, 64)
|
||||
for k = 0, 2 do
|
||||
local at = buffer.readi32(b, m.offset + k * 4)
|
||||
local n = buffer.readu16(b, m.offset + 12 + k * 2)
|
||||
if at > 0 and n >= 2 then
|
||||
if at + n > size then
|
||||
return nil, Err.new(Err.TRUNCATED, "plane " .. (k + 1) .. " past the end", at)
|
||||
end
|
||||
local carmackLen = buffer.readu16(b, at)
|
||||
local mid, msg = Expand.carmack(b, at + 2, n - 2, carmackLen)
|
||||
if not mid then
|
||||
return nil, Err.from(msg, at)
|
||||
end
|
||||
if carmackLen < 2 or buffer.readu16(mid, 0) ~= w * h * 2 then
|
||||
return nil, Err.new(Err.CORRUPT, "plane " .. (k + 1) .. ": RLEW length is not width x height", at)
|
||||
end
|
||||
local plane, rmsg = Expand.rlew(mid, 2, carmackLen - 2, w * h, p.tag)
|
||||
if not plane then
|
||||
return nil, Err.from(rmsg, at)
|
||||
end
|
||||
local data = table.create(w * h, 0)
|
||||
for i = 0, w * h - 1 do
|
||||
data[i + 1] = buffer.readu16(plane, i * 2)
|
||||
end
|
||||
table.insert(tm.layers, { type = "tiles", id = k + 1, name = LAYERS[k + 1], width = w, height = h, data = data, visible = true, opacity = 1, properties = {} })
|
||||
end
|
||||
end
|
||||
tm.properties.name = m.name
|
||||
tm.meta.source = { format = "gamemaps", map = m.slot, name = m.name, rlewTag = p.tag, maps = maps }
|
||||
return tm, nil
|
||||
end
|
||||
|
||||
return Gamemaps
|
||||
end
|
||||
__FILES["vector/gamemaps/Expand"] = function()
|
||||
local __compat = __compat local buffer = __compat.buffer --!strict
|
||||
--!native
|
||||
-- vector/gamemaps/Expand: id's Carmack (near 0xA7 / far 0xA8 word copies) and RLEW (tag, count, value) expansion of
|
||||
-- GAMEMAPS planes (ModdingWiki "Carmack compression", "RLEW compression"; no code taken). Words are u16 LE.
|
||||
local Expand = {}
|
||||
|
||||
local readu8, readu16, writeu16 = buffer.readu8, buffer.readu16, buffer.writeu16
|
||||
|
||||
function Expand.carmack(b, at, n, outBytes)
|
||||
if outBytes % 2 ~= 0 then
|
||||
return nil, "odd Carmack length"
|
||||
end
|
||||
local out = buffer.create(outBytes)
|
||||
local words = math.floor(outBytes / 2
|
||||
) local o = 0
|
||||
local p, e = at, at + n
|
||||
while o < words do
|
||||
if p + 2 > e then
|
||||
return nil, "truncated Carmack data"
|
||||
end
|
||||
local w = readu16(b, p)
|
||||
p =p+ 2
|
||||
local hi = math.floor(w / 256
|
||||
) if hi == 0xA7 or hi == 0xA8 then
|
||||
local count = w % 256
|
||||
if count == 0 then
|
||||
if p + 1 > e then
|
||||
return nil, "truncated Carmack data"
|
||||
end
|
||||
writeu16(out, o * 2, hi * 256 + readu8(b, p))
|
||||
p =p+ 1
|
||||
o =o+ 1
|
||||
else
|
||||
local from
|
||||
if hi == 0xA7 then
|
||||
if p + 1 > e then
|
||||
return nil, "truncated Carmack data"
|
||||
end
|
||||
from = o - readu8(b, p)
|
||||
p =p+ 1
|
||||
else
|
||||
if p + 2 > e then
|
||||
return nil, "truncated Carmack data"
|
||||
end
|
||||
from = readu16(b, p)
|
||||
p =p+ 2
|
||||
end
|
||||
if from < 0 or from >= o or o + count > words then
|
||||
return nil, "bad Carmack copy"
|
||||
end
|
||||
for k = 0, count - 1 do
|
||||
writeu16(out, (o + k) * 2, readu16(out, (from + k) * 2))
|
||||
end
|
||||
o =o+ count
|
||||
end
|
||||
else
|
||||
writeu16(out, o * 2, w)
|
||||
o =o+ 1
|
||||
end
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
function Expand.rlew(b, at, n, outWords, tag)
|
||||
local out = buffer.create(outWords * 2)
|
||||
local o = 0
|
||||
local p, e = at, at + n
|
||||
while o < outWords do
|
||||
if p + 2 > e then
|
||||
return nil, "truncated RLEW data"
|
||||
end
|
||||
local w = readu16(b, p)
|
||||
p =p+ 2
|
||||
if w == tag then
|
||||
if p + 4 > e then
|
||||
return nil, "truncated RLEW data"
|
||||
end
|
||||
local count, v = readu16(b, p), readu16(b, p + 2)
|
||||
p =p+ 4
|
||||
if o + count > outWords then
|
||||
return nil, "RLEW run past the plane"
|
||||
end
|
||||
for k = 0, count - 1 do
|
||||
writeu16(out, (o + k) * 2, v)
|
||||
end
|
||||
o =o+ count
|
||||
else
|
||||
writeu16(out, o * 2, w)
|
||||
o =o+ 1
|
||||
end
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
return Expand
|
||||
end
|
||||
__FILES["vector/svg"] = function()
|
||||
local __compat = __compat local buffer, type = __compat.buffer, __compat.type --!strict
|
||||
-- vector/svg: SVG 1.1 / 2 static documents (W3C SVG 1.1 2nd ed., SVG 2 CR). Supports: shapes and paths, transforms,
|
||||
|
||||
Reference in new issue
Block a user