vector/gamemaps: own test file (CONTRACT); type fixes in vswap / gamemaps

This commit is contained in:
4DBug committed 2026-11-01 18:54:01 -06:00
1 parent e51fd3585e
commit 69ee08a600
9 files changed
+1143 -1104

No files matched your search

+20 -11
View File
@@ -10700,7 +10700,7 @@ function Vswap.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
local b = r.buf
local a = Archive.new("vswap")
local pages: { Page } = {}
local counts = { wall = 0, sprite = 0, sound = 0 }
local counts: { [string]: number } = { 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)
@@ -10709,13 +10709,19 @@ function Vswap.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
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)
local kind: string, idx: number = "sound", i - so
if i < ss then
kind, idx = "wall", i
elseif i < so then
kind, idx = "sprite", i - ss
elseif i == n - 1 and so < n then
kind, idx = "soundinfo", 0
end
local name = if kind == "soundinfo" then "soundinfo" else kind .. "s/" .. string.format("%03d", idx)
if kind ~= "soundinfo" then
counts[kind] += 1
end
table.insert(pages, { kind = kind, index = idx, offset = o, size = l })
table.insert(pages, { kind = kind :: any, 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 }
@@ -184530,19 +184536,21 @@ 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
local head: buffer, maps: buffer
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
local got = asBuffer(opts and opts.mapHead) or sibling(opts, "MAPHEAD")
if not got then
return nil, Err.new(Err.UNSUPPORTED, "MAPHEAD not available (opts.mapHead / opts.files / opts.resolve)")
end
head = got
else
head = self
maps = asBuffer(opts and opts.gameMaps) or sibling(opts, "GAMEMAPS")
if not maps then
local got = asBuffer(opts and opts.gameMaps) or sibling(opts, "GAMEMAPS")
if not got then
return nil, Err.new(Err.UNSUPPORTED, "GAMEMAPS not available (opts.gameMaps / opts.files / opts.resolve)")
end
maps = got
end
if buffer.len(head) < 2 then
return nil, Err.new(Err.TRUNCATED, "MAPHEAD")
@@ -184622,7 +184630,8 @@ function Gamemaps.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.TileMap?
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 = {} })
local layer: Types.TileLayer = { type = "tiles", id = k + 1, name = LAYERS[k + 1], width = w, height = h, data = data, visible = true, opacity = 1, properties = {} }
table.insert(tm.layers, layer)
end
end
tm.properties.name = m.name
File diff suppressed because it is too large. Load diff
+21 -12
View File
@@ -11333,7 +11333,7 @@ if r:remaining() < 6 then
local b = r.buf
local a = Archive.new("vswap")
local pages= {}
local counts = { wall = 0, sprite = 0, sound = 0 }
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)
@@ -11342,13 +11342,19 @@ if r:remaining() < 6 then
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)
local kind, idx= "sound", i - so
if i < ss then
kind, idx = "wall", i
elseif i < so then
kind, idx = "sprite", i - ss
elseif i == n - 1 and so < n then
kind, idx = "soundinfo", 0
end
local name = kind == "soundinfo" and"soundinfo" or kind .. "s/" .. string.format("%03d", idx)
if kind ~= "soundinfo" then
counts[kind]=counts[kind]+ 1
end
table.insert(pages, { kind = kind, index = idx, offset = o, size = l })
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 }
@@ -185163,19 +185169,21 @@ 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
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
local got = asBuffer(opts and opts.mapHead) or sibling(opts, "MAPHEAD")
if not got then
return nil, Err.new(Err.UNSUPPORTED, "MAPHEAD not available (opts.mapHead / opts.files / opts.resolve)")
end
head = got
else
head = self
maps = asBuffer(opts and opts.gameMaps) or sibling(opts, "GAMEMAPS")
if not maps then
local got = asBuffer(opts and opts.gameMaps) or sibling(opts, "GAMEMAPS")
if not got then
return nil, Err.new(Err.UNSUPPORTED, "GAMEMAPS not available (opts.gameMaps / opts.files / opts.resolve)")
end
maps = got
end
if buffer.len(head) < 2 then
return nil, Err.new(Err.TRUNCATED, "MAPHEAD")
@@ -185255,7 +185263,8 @@ local p, err = pair(r, opts)
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 = {} })
local layer= { type = "tiles", id = k + 1, name = LAYERS[k + 1], width = w, height = h, data = data, visible = true, opacity = 1, properties = {} }
table.insert(tm.layers, layer)
end
end
tm.properties.name = m.name
File diff suppressed because it is too large. Load diff
+1 -1
View File
@@ -151,7 +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 | gamemaps | .wl6 .wl1 .sod .sdm | yes | - | tilemap: true | `tests/vector/gamemaps.luau` |
| 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` |
+11 -5
View File
@@ -85,7 +85,7 @@ function Vswap.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
local b = r.buf
local a = Archive.new("vswap")
local pages: { Page } = {}
local counts = { wall = 0, sprite = 0, sound = 0 }
local counts: { [string]: number } = { 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)
@@ -94,13 +94,19 @@ function Vswap.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
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)
local kind: string, idx: number = "sound", i - so
if i < ss then
kind, idx = "wall", i
elseif i < so then
kind, idx = "sprite", i - ss
elseif i == n - 1 and so < n then
kind, idx = "soundinfo", 0
end
local name = if kind == "soundinfo" then "soundinfo" else kind .. "s/" .. string.format("%03d", idx)
if kind ~= "soundinfo" then
counts[kind] += 1
end
table.insert(pages, { kind = kind, index = idx, offset = o, size = l })
table.insert(pages, { kind = kind :: any, 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 }
+9 -6
View File
@@ -79,19 +79,21 @@ 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
local head: buffer, maps: buffer
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
local got = asBuffer(opts and opts.mapHead) or sibling(opts, "MAPHEAD")
if not got then
return nil, Err.new(Err.UNSUPPORTED, "MAPHEAD not available (opts.mapHead / opts.files / opts.resolve)")
end
head = got
else
head = self
maps = asBuffer(opts and opts.gameMaps) or sibling(opts, "GAMEMAPS")
if not maps then
local got = asBuffer(opts and opts.gameMaps) or sibling(opts, "GAMEMAPS")
if not got then
return nil, Err.new(Err.UNSUPPORTED, "GAMEMAPS not available (opts.gameMaps / opts.files / opts.resolve)")
end
maps = got
end
if buffer.len(head) < 2 then
return nil, Err.new(Err.TRUNCATED, "MAPHEAD")
@@ -171,7 +173,8 @@ function Gamemaps.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.TileMap?
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 = {} })
local layer: Types.TileLayer = { type = "tiles", id = k + 1, name = LAYERS[k + 1], width = w, height = h, data = data, visible = true, opacity = 1, properties = {} }
table.insert(tm.layers, layer)
end
end
tm.properties.name = m.name
+2 -43
View File
@@ -1,10 +1,9 @@
-- 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.
-- archive/vswap tests: a Wolfenstein 3D page file written by the fixture's own writer (walls, sprites with gaps,
-- sounds across pages) against its source pixels and samples.
-- 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")
@@ -12,7 +11,6 @@ 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")
@@ -53,39 +51,6 @@ do
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
@@ -98,10 +63,4 @@ t:fuzz("vswap", function(b: buffer)
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()
+53
View File
@@ -0,0 +1,53 @@
-- vector/gamemaps tests: Wolfenstein 3D GAMEMAPS + MAPHEAD written by the fixture's own writer (Carmack near / far
-- copies and escapes, RLEW runs and a literal tag) against the source tiles. Fixtures: tools/fixtures/wolf3d.py.
local T = require("@tests/T")
local F = require("@rosetta")
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("gamemaps")
t:eq(Gamemaps.name, "gamemaps", "name")
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("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()