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

+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