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

+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