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:
4DBug committed 2026-11-01 18:50:33 -06:00
1 parent 719abb97c0
commit e51fd3585e
15 files changed
+3139 -1085

No files matched your search

+3 -1
View File
@@ -44,7 +44,7 @@ print(F.detect(webp), #F.formats()) -- "webp", the format list
## Formats
<!-- formats:begin -->
176 formats. Generated from the format contracts by `tools/gen_registry.py`; [docs/FORMATS.md](docs/FORMATS.md)
178 formats. Generated from the format contracts by `tools/gen_registry.py`; [docs/FORMATS.md](docs/FORMATS.md)
adds each format's capabilities.
| Category | Format | Extensions | Read | Write |
@@ -176,6 +176,7 @@ adds each format's capabilities.
| Archives | tar | .tar | yes | yes |
| Archives | uu | .uu .uue .xxe .b64 | yes | yes |
| Archives | vpk | .vpk | yes | yes |
| Archives | vswap | .wl6 .wl1 .sod .sdm .vswap | yes | - |
| Archives | wad | .wad | yes | yes |
| Archives | xz | .xz .txz .lzma | yes | - |
| Archives | yenc | .yenc .ync | yes | yes |
@@ -194,6 +195,7 @@ adds each format's capabilities.
| Data | toml | .toml | yes | yes |
| Data | xml | .xml .xsd .xsl .xslt .rss .atom .xhtml | yes | yes |
| Data | yaml | .yaml .yml | yes | yes |
| Vector graphics and tile maps | gamemaps | .wl6 .wl1 .sod .sdm | yes | - |
| Vector graphics and tile maps | svg | .svg | yes | yes |
| Vector graphics and tile maps | tiled | .tmx .tmj .tsx .tsj | yes | yes |
| Layered images | aseprite | .aseprite .ase | yes | yes |
+569
View File
@@ -2408,6 +2408,18 @@ local list: { Entry } = {
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",
@@ -2624,6 +2636,18 @@ local list: { Entry } = {
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",
@@ -10588,6 +10612,270 @@ end
return Vpk
end
__FILES["archive/vswap"] = function()
-- 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
type DecodeOptions = { maxChunks: number? } -- default 65535
type ImageOptions = {
palette: (buffer | string)?, -- 768 bytes RGB (6-bit VGA values are scaled up when no byte is above 63); default grey
sixBit: boolean?, -- force the 6-bit reading of the palette
}
type Page = { kind: "wall" | "sprite" | "sound" | "soundinfo", index: number, offset: number, size: number }
-- ------------------------------------------------------------------------------------------------ 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: Bytes.Reader): (number?, number, number)
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: Bytes.Reader): number
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 += 1
end
end
return if nonzero > 0 then 0.6 else 0
end
-- ------------------------------------------------------------------------------------------------ decode
function Vswap.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
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(if 6 + readu16(r.buf, r.base) * 6 > r:remaining() then Err.TRUNCATED else Err.CORRUPT, "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: { Page } = {}
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: any = if i < ss then "wall" elseif i < so then "sprite" elseif i == n - 1 and so < n then "soundinfo" else "sound"
local idx = if kind == "wall" then i elseif kind == "sprite" then i - ss elseif kind == "sound" then i - so else 0
local name = if kind == "soundinfo" then "soundinfo" else string.format("%ss/%03d", kind, idx)
if kind ~= "soundinfo" then
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: any): any
return type(a) == "table" and type(a.meta) == "table" and a.meta.vswap or nil
end
local function palette(opts: ImageOptions?): (buffer, boolean)
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 = if type(p) == "string" then buffer.fromstring(p) else p
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, n // 3), true
end
function Vswap.pages(a: any): { Page }?
local v = info(a)
return v and v.pages
end
function Vswap.image(a: any, kind: "wall" | "sprite", index: number, opts: ImageOptions?): (Types.Image?, Err.Err?)
local v = info(a)
if not v then
return nil, Err.new(Err.ARG, "expected a decoded VSWAP archive")
end
local page: Page? = nil
for _, p in 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 = 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), 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 += 6
end
end
end
img.meta.source = { format = "vswap", kind = kind, index = index, palette = given }
return img, nil
end
function Vswap.sounds(a: any): number
local v = info(a)
if not v then
return 0
end
local last = v.pages[#v.pages]
return if last and last.kind == "soundinfo" then last.size // 4 else 0
end
function Vswap.sound(a: any, index: number): (Types.Audio?, Err.Err?)
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 > 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 += take
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()
-- archive/wad: Doom-engine WADs (IWAD / PWAD; Unofficial Doom Specs, w_wad.c / r_data.c; no code taken), plus lumps,
@@ -184160,6 +184448,287 @@ end
return Vtt
end
__FILES["vector/gamemaps"] = function()
-- 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
type DecodeOptions = {
map: number?, -- 1-based map slot, default the first present
name: string?, -- the file name: picks the sibling's extension (GAMEMAPS.WL6 <-> MAPHEAD.WL6)
files: { [string]: buffer | string }?,
resolve: ((uri: string) -> (buffer | string)?)?,
mapHead: (buffer | string)?, -- MAPHEAD given directly
gameMaps: (buffer | string)?, -- GAMEMAPS given directly
maxTiles: number?, -- default 2^20 per plane
}
type MapInfo = { slot: number, name: string, width: number, height: number, offset: number }
-- ------------------------------------------------------------------------------------------------ constants
local MAGIC = "TED5v1.0"
local LAYERS = { "walls", "objects", "plane3" }
-- ------------------------------------------------------------------------------------------------ sniff
function Gamemaps.sniff(r: Bytes.Reader): number
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: any): buffer?
if type(v) == "string" then
return buffer.fromstring(v)
end
return if type(v) == "buffer" then v else nil
end
local function sibling(opts: DecodeOptions?, stem: string): buffer?
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 { 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
type Pair = { head: buffer, maps: buffer, tag: number }
local function pair(r: Bytes.Reader, opts: DecodeOptions?): (Pair?, Err.Err?)
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: Pair): { MapInfo }
local out: { MapInfo } = {}
local hn = math.min(100, (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 = if z then string.sub(nm, 1, z - 1) else nm, 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: Bytes.Reader, opts: DecodeOptions?): ({ MapInfo }?, Err.Err?)
local p, err = pair(r, opts)
if not p then
return nil, err
end
return list(p), nil
end
function Gamemaps.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.TileMap?, Err.Err?)
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: MapInfo? = if want then nil else maps[1]
for _, x in 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(if w * h == 0 then Err.CORRUPT else Err.LIMIT, "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"] = @native function()
-- 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
@native function Expand.carmack(b: buffer, at: number, n: number, outBytes: number): (buffer?, string?)
if outBytes % 2 ~= 0 then
return nil, "odd Carmack length"
end
local out = buffer.create(outBytes)
local words = 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 += 2
local hi = 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 += 1
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 += 1
else
if p + 2 > e then
return nil, "truncated Carmack data"
end
from = readu16(b, 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 += count
end
else
writeu16(out, o * 2, w)
o += 1
end
end
return out, nil
end
@native function Expand.rlew(b: buffer, at: number, n: number, outWords: number, tag: number): (buffer?, string?)
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 += 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 += 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 += count
else
writeu16(out, o * 2, w)
o += 1
end
end
return out, nil
end
return Expand
end
__FILES["vector/svg"] = function()
-- vector/svg: SVG 1.1 / 2 static documents (W3C SVG 1.1 2nd ed., SVG 2 CR). Supports: shapes and paths, transforms,
File diff suppressed because it is too large. Load diff
+569
View File
@@ -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,
File diff suppressed because it is too large. Load diff
+2
View File
@@ -132,6 +132,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
| archive | tar | .tar | yes | yes | variants: "ustar", "pax", "gnu" | `tests/archive/tar.luau` |
| archive | uu | .uu .uue .xxe .b64 | yes | yes | variants: "uu", "xx", "base64" | `tests/archive/uu.luau` |
| archive | vpk | .vpk | yes | yes | versions: 1, 2, multipart: true | `tests/archive/vpk.luau` |
| archive | vswap | .wl6 .wl1 .sod .sdm .vswap | yes | - | images: true, sounds: true | `tests/archive/vswap.luau` |
| archive | wad | .wad | yes | yes | images: true, maps: true, variants: "iwad", "pwad" | `tests/archive/wad.luau` |
| archive | xz | .xz .txz .lzma | yes | - | stream: true, filters: "lzma2", "lzma", "delta", "x86", "powerpc", "arm", "armthumb", "sparc", "arm64" | `tests/archive/xz.luau` |
| archive | yenc | .yenc .ync | yes | yes | multipart: true | `tests/archive/yenc.luau` |
@@ -150,6 +151,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
| data | toml | .toml | yes | yes | ordered: true, dates: true | `tests/data/toml.luau` |
| data | xml | .xml .xsd .xsl .xslt .rss .atom .xhtml | yes | yes | namespaces: true, streaming: true | `tests/data/xml.luau` |
| data | yaml | .yaml .yml | yes | yes | ordered: true, multiDocument: true, anchors: true | `tests/data/yaml.luau` |
| vector | gamemaps | .wl6 .wl1 .sod .sdm | yes | - | tilemap: true | **missing** |
| vector | svg | .svg | yes | yes | rasterize: true, text: false | `tests/vector/svg.luau` |
| vector | tiled | .tmx .tmj .tsx .tsj | yes | yes | tilesets: true, infinite: true | `tests/vector/tiled.luau` |
| layered | aseprite | .aseprite .ase | yes | yes | layers: true, groups: true, animation: true, depths: 8, 16, 32 | `tests/layered/aseprite.luau` |
+18 -1
View File
@@ -254,6 +254,22 @@ local named = assert(F.archive.load(mpqBytes, { names = { "war3map.w3e" } }))
assert(#named:list() >= 2)
```
Wolfenstein 3D's page file (`VSWAP.WL6`, also `.WL1`, Spear of Destiny's `.SOD` / `.SDM`) loads as an Archive of
its pages (`walls/000`, `sprites/000`, `sounds/000`, `soundinfo`). The format module turns walls and sprites into
64 x 64 Images and the digitized sounds into Audio. The game's palette is not part of the file (nor redistributable):
pass its 768 bytes as `palette` (6-bit VGA values are scaled up); without it the images are grey.
```luau
local Vswap = F.format("vswap")
local pages = assert(F.archive.load(vswapBytes, { name = "VSWAP.WL6" }))
assert(pages:list()[1].name == "walls/000")
local wall = assert(Vswap.image(pages, "wall", 0, { palette = wolfPaletteBytes }))
local guard = assert(Vswap.image(pages, "sprite", 0, { palette = wolfPaletteBytes })) -- alpha 0 = hole
assert(wall.width == 64 and guard.height == 64)
local sound = assert(Vswap.sound(pages, 1)) -- u8 PCM at 7042 Hz, as f32 Audio
assert(sound.sampleRate == 7042 and Vswap.sounds(pages) == 2)
```
## Unix, package and disc archives
### cpio
@@ -431,7 +447,8 @@ assert(F.archive.load(out):list()[1].method == "store")
| bzip2 (`.bz2`, `.tbz2`) | yes | - | single stream |
| Brotli (`.br`) | yes | - | single stream; no magic number, so pass a `.br` name or `format = "br"` |
| PAK (`.pak`) | yes | yes | Quake / Quake II / Half-Life (and SiN `SPAK`) |
| WAD (`.wad`) | yes | yes | IWAD / PWAD; lumps, graphics, wall textures and maps through `F.format("wad")` |
| WAD (`.wad`) | yes | yes | IWAD / PWAD; lumps, graphics, wall textures and maps (binary and UDMF) through `F.format("wad")` |
| VSWAP (Wolfenstein 3D `.wl6` / `.wl1` / `.sod` / `.sdm`) | yes | | pages; walls, sprites and sounds through `F.format("vswap")` |
| GRP (`.grp`) | yes | yes | Build engine |
| VPK (`.vpk`) | yes | yes | versions 1 / 2, multi-part reading, preload bytes; writes one directory file |
| MPQ (`.mpq`, `.w3x`, `.sc2map`, ...) | yes | - | versions 1-4 (classic tables), encryption; no Huffman / ADPCM (WAVE) sectors |
+20 -1
View File
@@ -194,14 +194,33 @@ local back = assert(F.load(tmx))
assert(back.layers[1].data[6] == 0x80000009)
```
## Wolfenstein 3D maps
Wolfenstein 3D / Spear of Destiny maps are two files: `GAMEMAPS.WL6` (the Carmack + RLEW compressed planes) and
`MAPHEAD.WL6` (the RLEW tag and where each map starts). Load either one: the other comes through `files` /
`resolve` (loading a path finds it next to the file) or `mapHead` / `gameMaps`. A map loads as a TileMap with one
tile layer per plane (`walls`, `objects`, `plane3`) holding the raw tile values (1..63 walls, 90..101 doors, 106+
floor areas; things in `objects`), without a tileset; `map` picks the slot (default: the first present) and
`F.format("gamemaps").maps` lists them.
```luau
local files = { ["MAPHEAD.WL6"] = mapheadBytes }
local level = assert(F.load(gamemapsBytes, { name = "GAMEMAPS.WL6", files = files }))
assert(level.kind == "tilemap" and level.width == 16 and level.properties.name == "Tiny map")
assert(level.layers[1].name == "walls" and level.layers[1].data[1] == 1) -- the border wall
local maps = assert(F.format("gamemaps").maps(F.Bytes.reader(mapheadBytes), { gameMaps = gamemapsBytes }))
assert(#maps == 1 and maps[1].slot == 1)
```
## Formats
| Format | Read | Write | Notes |
|---|---|---|---|
| TMX / TSX | yes | yes | CSV, base64 (raw, zlib, gzip) and `<tile>` data; zstd data returns `UNSUPPORTED` |
| TMJ / TSJ | yes | yes | |
| Wolfenstein 3D GAMEMAPS + MAPHEAD | yes | | the `gamemaps` format: raw tile values, no tileset |
Both are the `tiled` format in [FORMATS.md](../FORMATS.md). Wang sets and terrains are dropped.
TMX and TMJ are the `tiled` format in [FORMATS.md](../FORMATS.md). Wang sets and terrains are dropped.
## Gotchas
+24
View File
@@ -1538,6 +1538,18 @@ local list: { Entry } = {
return require("@rosetta/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 require("@rosetta/archive/vswap")
end,
},
{
name = "wad",
category = "archive",
@@ -1754,6 +1766,18 @@ local list: { Entry } = {
return require("@rosetta/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 require("@rosetta/vector/gamemaps")
end,
},
{
name = "svg",
category = "vector",
+262
View File
@@ -0,0 +1,262 @@
--!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 = require("@rosetta/core/Err")
local Bytes = require("@rosetta/core/Bytes")
local Types = require("@rosetta/Types")
local Archive = require("@rosetta/containers/Archive")
local Pixels = require("@rosetta/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
export type DecodeOptions = { maxChunks: number? } -- default 65535
export type ImageOptions = {
palette: (buffer | string)?, -- 768 bytes RGB (6-bit VGA values are scaled up when no byte is above 63); default grey
sixBit: boolean?, -- force the 6-bit reading of the palette
}
export type Page = { kind: "wall" | "sprite" | "sound" | "soundinfo", index: number, offset: number, size: number }
-- ------------------------------------------------------------------------------------------------ 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: Bytes.Reader): (number?, number, number)
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: Bytes.Reader): number
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 += 1
end
end
return if nonzero > 0 then 0.6 else 0
end
-- ------------------------------------------------------------------------------------------------ decode
function Vswap.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
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(if 6 + readu16(r.buf, r.base) * 6 > r:remaining() then Err.TRUNCATED else Err.CORRUPT, "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: { Page } = {}
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: any = if i < ss then "wall" elseif i < so then "sprite" elseif i == n - 1 and so < n then "soundinfo" else "sound"
local idx = if kind == "wall" then i elseif kind == "sprite" then i - ss elseif kind == "sound" then i - so else 0
local name = if kind == "soundinfo" then "soundinfo" else string.format("%ss/%03d", kind, idx)
if kind ~= "soundinfo" then
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: any): any
return type(a) == "table" and type(a.meta) == "table" and a.meta.vswap or nil
end
local function palette(opts: ImageOptions?): (buffer, boolean)
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 = if type(p) == "string" then buffer.fromstring(p) else p
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, n // 3), true
end
function Vswap.pages(a: any): { Page }?
local v = info(a)
return v and v.pages
end
function Vswap.image(a: any, kind: "wall" | "sprite", index: number, opts: ImageOptions?): (Types.Image?, Err.Err?)
local v = info(a)
if not v then
return nil, Err.new(Err.ARG, "expected a decoded VSWAP archive")
end
local page: Page? = nil
for _, p in 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 = 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), 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 += 6
end
end
end
img.meta.source = { format = "vswap", kind = kind, index = index, palette = given }
return img, nil
end
function Vswap.sounds(a: any): number
local v = info(a)
if not v then
return 0
end
local last = v.pages[#v.pages]
return if last and last.kind == "soundinfo" then last.size // 4 else 0
end
function Vswap.sound(a: any, index: number): (Types.Audio?, Err.Err?)
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 > 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 += take
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
+95
View File
@@ -0,0 +1,95 @@
--!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: buffer, at: number, n: number, outBytes: number): (buffer?, string?)
if outBytes % 2 ~= 0 then
return nil, "odd Carmack length"
end
local out = buffer.create(outBytes)
local words = 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 += 2
local hi = 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 += 1
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 += 1
else
if p + 2 > e then
return nil, "truncated Carmack data"
end
from = readu16(b, 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 += count
end
else
writeu16(out, o * 2, w)
o += 1
end
end
return out, nil
end
function Expand.rlew(b: buffer, at: number, n: number, outWords: number, tag: number): (buffer?, string?)
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 += 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 += 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 += count
else
writeu16(out, o * 2, w)
o += 1
end
end
return out, nil
end
return Expand
+182
View File
@@ -0,0 +1,182 @@
--!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 = require("@rosetta/core/Err")
local Bytes = require("@rosetta/core/Bytes")
local Input = require("@rosetta/core/Input")
local Types = require("@rosetta/Types")
local Expand = require("@rosetta/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
export type DecodeOptions = {
map: number?, -- 1-based map slot, default the first present
name: string?, -- the file name: picks the sibling's extension (GAMEMAPS.WL6 <-> MAPHEAD.WL6)
files: { [string]: buffer | string }?,
resolve: ((uri: string) -> (buffer | string)?)?,
mapHead: (buffer | string)?, -- MAPHEAD given directly
gameMaps: (buffer | string)?, -- GAMEMAPS given directly
maxTiles: number?, -- default 2^20 per plane
}
export type MapInfo = { slot: number, name: string, width: number, height: number, offset: number }
-- ------------------------------------------------------------------------------------------------ constants
local MAGIC = "TED5v1.0"
local LAYERS = { "walls", "objects", "plane3" }
-- ------------------------------------------------------------------------------------------------ sniff
function Gamemaps.sniff(r: Bytes.Reader): number
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: any): buffer?
if type(v) == "string" then
return buffer.fromstring(v)
end
return if type(v) == "buffer" then v else nil
end
local function sibling(opts: DecodeOptions?, stem: string): buffer?
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 { 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
type Pair = { head: buffer, maps: buffer, tag: number }
local function pair(r: Bytes.Reader, opts: DecodeOptions?): (Pair?, Err.Err?)
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: Pair): { MapInfo }
local out: { MapInfo } = {}
local hn = math.min(100, (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 = if z then string.sub(nm, 1, z - 1) else nm, 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: Bytes.Reader, opts: DecodeOptions?): ({ MapInfo }?, Err.Err?)
local p, err = pair(r, opts)
if not p then
return nil, err
end
return list(p), nil
end
function Gamemaps.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.TileMap?, Err.Err?)
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: MapInfo? = if want then nil else maps[1]
for _, x in 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(if w * h == 0 then Err.CORRUPT else Err.LIMIT, "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
+107
View File
@@ -0,0 +1,107 @@
-- archive/vswap + vector/gamemaps tests: Wolfenstein 3D page files and maps written by the fixture's own writers
-- (walls, sprites with gaps, sounds; Carmack + RLEW planes) against their source pixels and tiles.
-- Fixtures: tools/fixtures/wolf3d.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Vswap = require("@rosetta/archive/vswap")
local Gamemaps = require("@rosetta/vector/gamemaps")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/wolf3d")
local t = T.suite("vswap")
t:eq(Vswap.name, "vswap", "name")
t:eq(Gamemaps.name, "gamemaps", "name")
local vs = Fx.vswap
t:eq(F.detect(vs.data, { name = "VSWAP.WL6" }), "vswap", "F.detect VSWAP")
local a, err = Vswap.decode(Bytes.reader(buffer.fromstring(vs.data)))
t:check(a, "vswap: " .. tostring(err))
do
local list = a:list()
t:eq(list[1].name, "walls/000", "wall entry")
t:eq(list[#vs.walls + 1].name, "sprites/000", "sprite entry")
t:eq(list[#list].name, "soundinfo", "sound info entry")
t:eq(a.meta.source.walls, #vs.walls, "wall count")
t:eq(a.meta.source.sprites, #vs.sprites, "sprite count")
for i, crc in vs.walls do
local img, e = Vswap.image(a, "wall", i - 1, { palette = Fx.palette })
t:check(img, "wall " .. i .. ": " .. tostring(e))
t:eq(Crc32.of((img :: any).pixels), crc, "wall " .. i .. " pixels (column-major, 6-bit palette)")
end
for i, crc in vs.sprites do
local img, e = Vswap.image(a, "sprite", i - 1, { palette = Fx.palette })
t:check(img, "sprite " .. i .. ": " .. tostring(e))
t:eq(Crc32.of((img :: any).pixels), crc, "sprite " .. i .. " pixels and holes")
end
local grey = Vswap.image(a, "wall", 0) :: any
t:eq(grey.meta.source.palette, false, "no palette: grey")
t:eq(Vswap.sounds(a), #vs.sounds, "sound count")
for i, s in vs.sounds do
local au, e = Vswap.sound(a, i)
t:check(au, "sound " .. i .. ": " .. tostring(e))
local x = au :: any
t:eq(x.frames, s.bytes, "sound " .. i .. " length (across pages)")
t:eq(buffer.readf32(x.samples, 0), (s.first - 128) / 128, "sound " .. i .. " first sample")
t:eq(buffer.readf32(x.samples, (s.bytes - 1) * 4), (s.last - 128) / 128, "sound " .. i .. " last sample")
t:eq(x.sampleRate, 7042, "rate")
end
local _, e1 = Vswap.image(a, "wall", 99)
t:check(e1 and e1.code == Err.ARG, "missing wall")
local _, e2 = Vswap.sound(a, 0)
t:check(e2 and e2.code == Err.ARG, "missing sound")
end
do
t:eq(F.detect(Fx.gamemaps, { name = "GAMEMAPS.WL6" }), "gamemaps", "F.detect GAMEMAPS")
t:eq(F.detect(Fx.maphead, { name = "MAPHEAD.WL6" }), "gamemaps", "F.detect MAPHEAD")
local files = { ["MAPHEAD.WL6"] = Fx.maphead, ["GAMEMAPS.WL6"] = Fx.gamemaps }
local list = Gamemaps.maps(Bytes.reader(buffer.fromstring(Fx.gamemaps)), { name = "GAMEMAPS.WL6", files = files }) :: any
t:eq(#list, #Fx.maps, "maps listed (slot 2 is -1)")
for _, m in Fx.maps do
for _, from in { "gamemaps", "maphead" } do
local src = if from == "gamemaps" then Fx.gamemaps else Fx.maphead
local tm, e = Gamemaps.decode(Bytes.reader(buffer.fromstring(src)), { name = string.upper(from) .. ".WL6", files = files, map = m.slot })
t:check(tm, m.name .. " from " .. from .. ": " .. tostring(e))
local x = tm :: any
t:eq(x.width, m.width, m.name .. ": width")
t:eq(x.height, m.height, m.name .. ": height")
t:eq(x.properties.name, m.name, m.name .. ": name")
t:eq(#x.layers, 3, m.name .. ": planes")
for k, crc in m.planes do
local b = buffer.create(m.width * m.height * 2)
for i, v in x.layers[k].data do
buffer.writeu16(b, (i - 1) * 2, v)
end
t:eq(Crc32.of(b), crc, m.name .. ": plane " .. k .. " after Carmack + RLEW")
end
end
end
local tm = F.load(Fx.gamemaps, { name = "GAMEMAPS.WL6", files = files }) :: any
t:check(tm and tm.kind == "tilemap" and tm.layers[1].name == "walls", "F.load: a TileMap")
local _, e = Gamemaps.decode(Bytes.reader(buffer.fromstring(Fx.gamemaps)))
t:check(e and e.code == Err.UNSUPPORTED, "no MAPHEAD")
local _, e2 = Gamemaps.decode(Bytes.reader(buffer.fromstring(Fx.gamemaps)), { mapHead = Fx.maphead, map = 2 })
t:check(e2 and e2.code == Err.ARG, "empty slot")
end
t:fuzz("vswap", function(b: buffer)
local w = Vswap.decode(Bytes.reader(b))
if w then
for i = 0, 3 do
Vswap.image(w, "wall", i)
Vswap.image(w, "sprite", i)
end
Vswap.sound(w, 1)
Vswap.sound(w, 2)
end
return w
end, { buffer.fromstring(vs.data) }, { mutations = 300 })
t:fuzz("gamemaps", function(b: buffer)
return Gamemaps.decode(Bytes.reader(b), { mapHead = Fx.maphead, map = 3 })
end, { buffer.fromstring(Fx.gamemaps) }, { mutations = 300 })
t:fuzz("maphead", function(b: buffer)
return Gamemaps.decode(Bytes.reader(b), { gameMaps = Fx.gamemaps })
end, { buffer.fromstring(Fx.maphead) }, { mutations = 300 })
t:done()
+21 -4
View File
@@ -1,10 +1,10 @@
# Docs inputs for the id Tech sections of the scene / tilemap / archive pages (tools/fixtures.py; tools/docs_check.py
# explains the inputs), made by the format fixtures' writers: the hand-written UDMF room (udmf.py), a dmap-layout
# .proc (proc.py).
# .proc (proc.py), a VSWAP and a one-map GAMEMAPS + MAPHEAD with a random palette (wolf3d.py).
# tests/gen/docs_idtech.luau { files = { [name] = bytes }, vars = { udmfText = "room.udmf", ... } }
import importlib.util, os
import importlib.util, os, struct
INPUTS = ["tools/docs_check.py", "tools/fixtures/udmf.py", "tools/fixtures/proc.py"]
INPUTS = ["tools/docs_check.py", "tools/fixtures/udmf.py", "tools/fixtures/proc.py", "tools/fixtures/wolf3d.py"]
def mod(ctx, name):
@@ -21,5 +21,22 @@ def generate(ctx):
models, portals, nodes, areas, shadow = P.level(ctx.rng(2), False)
files = {"room.udmf": mod(ctx, "udmf").ROOM.encode(),
"level.proc": P.write(models, portals, nodes, areas, shadow, "mapProcFile003", False).encode()}
vars = {"udmfText": "room.udmf", "procText": "level.proc"}
W = mod(ctx, "wolf3d")
rng = ctx.rng(5)
pal = bytes(rng.randrange(64) for _ in range(768))
files["VSWAP.WL6"], _ = W.vswap(rng, pal)
files["wolf.pal"] = pal
g = W.grid(rng, 16, 16, 0)
planes = [W.plane(g), W.plane([0] * 256), W.plane([0] * 256)]
body = bytearray(b"TED5v1.0")
starts = []
for pl in planes:
starts.append(len(body))
body += pl
head = len(body)
body += struct.pack("<iiiHHHHH16s", *starts, *(len(pl) for pl in planes), 16, 16, b"Tiny map") + b"!ID!"
files["GAMEMAPS.WL6"] = bytes(body)
files["MAPHEAD.WL6"] = struct.pack("<H100i", W.TAG, head, *([0] * 99))
vars = {"udmfText": "room.udmf", "procText": "level.proc", "vswapBytes": "VSWAP.WL6", "wolfPaletteBytes": "wolf.pal",
"gamemapsBytes": "GAMEMAPS.WL6", "mapheadBytes": "MAPHEAD.WL6"}
check.emit_inputs(ctx, files, vars)
+183
View File
@@ -0,0 +1,183 @@
# Wolfenstein 3D fixtures (tools/fixtures.py). id's data and source are not redistributable / GPL, so everything is
# written here by this script's own writers from the ModdingWiki format descriptions: a random 6-bit palette; a VSWAP
# with walls, sprites (t_compshape posts, with gaps), sound pages and the sound info page; a GAMEMAPS + MAPHEAD pair
# with two maps (slots 1 and 3) whose planes are RLEW then Carmack compressed (near and far copies, literal 0xA7xx /
# 0xA8xx words, RLEW runs and a literal tag value). The references are the source pixels / tiles: CRC-32 of the RGBA
# (6-bit values scaled as (v << 2) | (v >> 4), sprite holes 0) and of the tiles as u16 LE.
# tests/gen/wolf3d.luau { palette, vswap = { data, walls = { crc }, sprites = { crc }, sounds = { { bytes, crc } } },
# gamemaps, maphead, maps = { { slot, name, width, height, planes = { crc } } } }
import struct, zlib
TAG = 0xABCD
def rgba(pal, idx, holes=None):
out = bytearray()
for k, i in enumerate(idx):
if holes is not None and holes[k]:
out += b"\0\0\0\0"
else:
r, g, b = (pal[i * 3 + c] for c in range(3))
out += bytes([(r << 2) | (r >> 4), (g << 2) | (g >> 4), (b << 2) | (b >> 4), 255])
return zlib.crc32(bytes(out))
def sprite(rng):
# columns left..right, each with 1-2 posts; pixel data first, then the column post lists
left, right = rng.randint(0, 10), rng.randint(40, 63)
img = [None] * 4096 # row-major y * 64 + x: palette index, None = hole
pix = bytearray()
cols = []
hdr = 4 + (right - left + 1) * 2
for x in range(left, right + 1):
posts = []
y = rng.randint(0, 8)
for _ in range(rng.randint(1, 2)):
y0 = y
y1 = min(64, y0 + rng.randint(4, 30))
if y0 >= y1:
break
base = hdr + len(pix)
for yy in range(y0, y1):
v = rng.randrange(256)
img[yy * 64 + x] = v
pix.append(v)
posts.append((y1 * 2, base - y0, y0 * 2))
y = y1 + rng.randint(2, 10)
if y >= 60:
break
cols.append(posts)
body = bytearray(pix)
ofs = []
for posts in cols:
ofs.append(hdr + len(body))
for e, s, st in posts:
body += struct.pack("<HhH", e, s, st)
body += struct.pack("<H", 0)
data = struct.pack("<HH", left, right) + b"".join(struct.pack("<H", o) for o in ofs) + bytes(body)
return data, img
def vswap(rng, pal):
walls, sprites, sounds = [], [], []
for _ in range(3):
cm = bytes(rng.randrange(256) for _ in range(4096)) # column-major x * 64 + y
walls.append((cm, rgba(pal, [cm[x * 64 + y] for y in range(64) for x in range(64)])))
for _ in range(3):
data, img = sprite(rng)
sprites.append((data, rgba(pal, [v or 0 for v in img], [v is None for v in img])))
pages = [w for w, _ in walls] + [s for s, _ in sprites]
ss, so = len(walls), len(walls) + len(sprites)
# sound 1: one page; sound 2: two pages (4096 + 1000)
snd = [bytes(rng.randrange(256) for _ in range(700)), bytes(rng.randrange(256) for _ in range(5096))]
spages, info = [], b""
for s in snd:
info += struct.pack("<HH", len(spages), len(s))
for k in range(0, len(s), 4096):
spages.append(s[k:k + 4096])
pages += spages + [info]
n = len(pages)
# a sparse page (offset 0) after the walls is legal: the second wall page stays, an extra sprite slot is sparse
head = struct.pack("<HHH", n, ss, so)
at = 6 + n * 6
at = (at + 511) // 512 * 512
offs, body = [], bytearray()
for p in pages:
offs.append(at + len(body))
body += p
body += b"\0" * ((-len(body)) % 512)
out = head + b"".join(struct.pack("<I", o) for o in offs) + b"".join(struct.pack("<H", len(p)) for p in pages)
out += b"\0" * (at - len(out)) + bytes(body)
return out, {"walls": [c for _, c in walls], "sprites": [c for _, c in sprites],
"sounds": [{"bytes": len(s), "first": s[0], "last": s[-1]} for s in snd]}
def rlew(words):
out = []
i = 0
while i < len(words):
j = i
while j < len(words) and words[j] == words[i] and j - i < 0xFFFF:
j += 1
if j - i > 3 or words[i] == TAG:
out += [TAG, j - i, words[i]]
else:
out += words[i:j]
i = j
return out
def carmack(words):
out = bytearray()
pos = {}
i = 0
while i < len(words):
best, bo = 0, 0
for cand in reversed(pos.get((words[i], words[i + 1]) if i + 1 < len(words) else None, [])[-32:]):
k = 0
while i + k < len(words) and words[cand + k] == words[i + k] and k < 255 and cand + k < i:
k += 1
if k > best:
best, bo = k, cand
if best >= 2:
if i - bo <= 255:
out += struct.pack("<HB", 0xA700 | best, i - bo)
else:
out += struct.pack("<HH", 0xA800 | best, bo)
step = best
else:
w = words[i]
if w >> 8 in (0xA7, 0xA8):
out += struct.pack("<HB", w & 0xFF00, w & 0xFF)
else:
out += struct.pack("<H", w)
step = 1
for k in range(i, i + step):
if k + 1 < len(words):
pos.setdefault((words[k], words[k + 1]), []).append(k)
i += step
return bytes(out)
def plane(words):
r = [len(words) * 2] + rlew(words)
return struct.pack("<H", len(r) * 2) + carmack(r)
def grid(rng, w, h, k):
g = []
for y in range(h):
for x in range(w):
if k == 0:
v = 1 if x in (0, w - 1) or y in (0, h - 1) else (rng.choice([108, 108, 108, 2, 90]) if rng.random() < 0.2 else 108)
elif k == 1:
v = rng.choice([0] * 20 + [19, 23, 0xA7FF, 0xA801, TAG])
else:
v = 0
g.append(v)
return g
def generate(ctx):
rng = ctx.rng(64)
pal = bytes(rng.randrange(64) for _ in range(768))
vs, vref = vswap(rng, pal)
maps, body = [], bytearray(b"TED5v1.0")
heads = [0] * 100
for slot, (name, w, h) in [(1, ("Wolf1 Map1", 64, 64)), (3, ("Test map", 20, 12))]:
planes, crcs = [], []
for k in range(3):
g = grid(rng, w, h, k)
crcs.append(zlib.crc32(struct.pack("<%dH" % len(g), *g)))
planes.append(plane(g))
starts = []
for p in planes:
starts.append(len(body))
body += p
heads[slot - 1] = len(body)
body += struct.pack("<iiiHHHHH16s", *starts, *(len(p) for p in planes), w, h, name.encode())
body += b"!ID!"
maps.append({"slot": slot, "name": name, "width": w, "height": h, "planes": crcs})
heads[1] = -1
maphead = struct.pack("<H100i", TAG, *heads)
ctx.emit("wolf3d", {"palette": pal, "vswap": dict(vref, data=vs), "gamemaps": bytes(body), "maphead": maphead, "maps": maps})