codecs/model/DoomMap: the Doom map-to-Scene builder moved out of archive/wad, plus a UDMF 1.1 TEXTMAP parser (fields kept, namespace flag bits); Wad.map loads UDMF maps; model/udmf for a standalone TEXTMAP; codecs/text/Lexer (C-style tokens for id Tech / UDMF text)
STYLE.md: layout block and the 2a table list DoomMap and Lexer. tests/archive/wad: the UDMF stub map now loads (empty) instead of UNSUPPORTED.
This commit is contained in:
1 parent
01c044805f
commit
7d4e0381fe
20 files changed
+3857
-1979
No files matched your search
@@ -1022,6 +1022,18 @@ local list: { Entry } = {
|
||||
return require("@rosetta/model/stl")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "udmf",
|
||||
category = "model",
|
||||
path = "model/udmf",
|
||||
extensions = { "udmf", "textmap" },
|
||||
mime = { "text/x-udmf" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/model/DoomMap" },
|
||||
caps = { decode = true, encode = false, maps = true },
|
||||
load = function()
|
||||
return require("@rosetta/model/udmf")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "usda",
|
||||
category = "model",
|
||||
@@ -1520,7 +1532,7 @@ local list: { Entry } = {
|
||||
path = "archive/wad",
|
||||
extensions = { "wad" },
|
||||
mime = { "application/x-doom" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Archive", "codecs/image/Pixels", "codecs/model/Polygon" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Archive", "codecs/image/Pixels", "codecs/model/DoomMap" },
|
||||
caps = { decode = true, encode = true, images = true, maps = true, variants = { "iwad", "pwad" } },
|
||||
load = function()
|
||||
return require("@rosetta/archive/wad")
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
-- archive/wad: Doom-engine WADs (IWAD / PWAD; Unofficial Doom Specs, w_wad.c / r_data.c; no code taken), plus lumps,
|
||||
-- palettes, pictures, wall textures and maps. Entry names: "<map>/<lump>", repeats "<name>~<n>". Not supported: UDMF
|
||||
-- geometry (lumps listed), Strife's TEXTURE variant, big-endian (Jaguar) WADs, compressed Jaguar lumps.
|
||||
-- palettes, pictures, wall textures and maps (binary and UDMF TEXTMAP). Entry names: "<map>/<lump>", repeats
|
||||
-- "<name>~<n>". Not supported: Strife's TEXTURE variant, big-endian (Jaguar) WADs, compressed Jaguar lumps.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -10,7 +10,7 @@ local Types = require("@rosetta/Types")
|
||||
local Archive = require("@rosetta/containers/Archive")
|
||||
local Pixels = require("@rosetta/codecs/image/Pixels")
|
||||
local Picture = require("@rosetta/archive/wad/Picture")
|
||||
local Map = require("@rosetta/archive/wad/Map")
|
||||
local Map = require("@rosetta/codecs/model/DoomMap")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Wad = {
|
||||
@@ -18,7 +18,7 @@ local Wad = {
|
||||
category = "archive",
|
||||
extensions = { "wad" },
|
||||
mime = { "application/x-doom" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Archive", "codecs/image/Pixels", "codecs/model/Polygon" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Archive", "codecs/image/Pixels", "codecs/model/DoomMap" },
|
||||
caps = { decode = true, encode = true, images = true, maps = true, variants = { "iwad", "pwad" } },
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ export type Lump = {
|
||||
export type MapOptions = {
|
||||
textures: boolean?, -- default true
|
||||
palette: number?, -- default 0
|
||||
maxBlocks: number?, -- UDMF, default 2^22
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
@@ -377,7 +378,7 @@ function Wad.maps(a: any): { string }
|
||||
return out
|
||||
end
|
||||
|
||||
function Wad.mapData(a: any, name: string): (Map.MapData?, Err.Err?)
|
||||
function Wad.mapData(a: any, name: string, opts: MapOptions?): (Map.MapData?, Err.Err?)
|
||||
local w = info(a)
|
||||
if not w then
|
||||
return nil, Err.new(Err.ARG, "expected a decoded WAD archive")
|
||||
@@ -392,10 +393,13 @@ function Wad.mapData(a: any, name: string): (Map.MapData?, Err.Err?)
|
||||
if not next(lumps) then
|
||||
return nil, Err.new(Err.ARG, "no map named " .. tostring(name))
|
||||
end
|
||||
if lumps.TEXTMAP then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "UDMF (TEXTMAP) maps")
|
||||
local m, msg
|
||||
local tm = lumps.TEXTMAP
|
||||
if tm then
|
||||
m, msg = Map.udmf(buffer.readstring(tm.buf, tm.at, tm.size), opts and opts.maxBlocks)
|
||||
else
|
||||
m, msg = Map.parse(lumps, lumps.BEHAVIOR ~= nil)
|
||||
end
|
||||
local m, msg = Map.parse(lumps, lumps.BEHAVIOR ~= nil)
|
||||
if not m then
|
||||
return nil, Err.from(msg)
|
||||
end
|
||||
@@ -403,7 +407,7 @@ function Wad.mapData(a: any, name: string): (Map.MapData?, Err.Err?)
|
||||
end
|
||||
|
||||
function Wad.map(a: any, name: string, opts: MapOptions?): (Types.Scene?, Err.Err?)
|
||||
local m, err = Wad.mapData(a, name)
|
||||
local m, err = Wad.mapData(a, name, opts)
|
||||
if not m then
|
||||
return nil, err
|
||||
end
|
||||
@@ -441,7 +445,7 @@ function Wad.map(a: any, name: string, opts: MapOptions?): (Types.Scene?, Err.Er
|
||||
image = if images then image else nil,
|
||||
}
|
||||
local scene = Map.scene(m, tex, string.upper(name))
|
||||
scene.meta.source = { format = "wad", map = string.upper(name), mapFormat = m.format, things = #m.things, linedefs = #m.linedefs, sectors = #m.sectors, vertices = #m.vertices // 2 }
|
||||
scene.meta.source = { format = "wad", map = string.upper(name), mapFormat = m.format, namespace = m.namespace, things = #m.things, linedefs = #m.linedefs, sectors = #m.sectors, vertices = #m.vertices // 2 }
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,248 @@
|
||||
--!strict
|
||||
-- codecs/model/DoomMap/Udmf: UDMF 1.1 TEXTMAP text (zdoom.org specs/udmf.txt; no code taken) to the DoomMap tables.
|
||||
-- Every block keeps its fields (lower-case keys, namespace and user_ keys included); unknown blocks and global
|
||||
-- assignments are kept as data. Flags are rebuilt in the binary layout of the namespace (Doom or Hexen bits).
|
||||
local Lexer = require("@rosetta/codecs/text/Lexer")
|
||||
|
||||
local Udmf = {}
|
||||
|
||||
export type Block = { type: string, fields: { [string]: any } }
|
||||
export type Document = { namespace: string?, globals: { [string]: any }, blocks: { Block } }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ parse
|
||||
local function value(L: Lexer.Lexer): (any, string?)
|
||||
local k, v = L:next()
|
||||
if k == "number" then
|
||||
local raw = L.raw :: string
|
||||
-- integer := ... | 0[0-9]+ (octal) | 0x[0-9A-Fa-f]+
|
||||
if string.match(raw, "^[+-]?0[0-7]+$") then
|
||||
local neg = string.sub(raw, 1, 1) == "-"
|
||||
local o = tonumber((string.gsub(raw, "^[+-]?0", "")), 8) :: number
|
||||
return if neg then -o else o, nil
|
||||
end
|
||||
return v, nil
|
||||
elseif k == "string" then
|
||||
return v, nil
|
||||
elseif k == "word" then
|
||||
local w = string.lower(v)
|
||||
if w == "true" then
|
||||
return true, nil
|
||||
elseif w == "false" then
|
||||
return false, nil
|
||||
end
|
||||
return v, nil
|
||||
end
|
||||
return nil, L.err or "expected a value"
|
||||
end
|
||||
|
||||
function Udmf.parse(text: string, maxBlocks: number?): (Document?, string?)
|
||||
local L = Lexer.new(text)
|
||||
local doc: Document = { namespace = nil, globals = {}, blocks = {} }
|
||||
local limit = maxBlocks or 2 ^ 22
|
||||
while true do
|
||||
local k, id = L:next()
|
||||
if k == nil then
|
||||
if L.err then
|
||||
return nil, L.err .. " at line " .. L:line()
|
||||
end
|
||||
break
|
||||
end
|
||||
if k ~= "word" then
|
||||
return nil, "expected an identifier at line " .. L:line()
|
||||
end
|
||||
local name = string.lower(id)
|
||||
local pk, pv = L:next()
|
||||
if pk == "punct" and pv == "=" then
|
||||
local v, err = value(L)
|
||||
if err then
|
||||
return nil, err .. " at line " .. L:line()
|
||||
end
|
||||
local sk, sv = L:next()
|
||||
if sk ~= "punct" or sv ~= ";" then
|
||||
return nil, "expected ; at line " .. L:line()
|
||||
end
|
||||
doc.globals[name] = v
|
||||
if name == "namespace" and type(v) == "string" then
|
||||
doc.namespace = v
|
||||
end
|
||||
elseif pk == "punct" and pv == "{" then
|
||||
if #doc.blocks >= limit then
|
||||
return nil, "limit: more than " .. limit .. " blocks"
|
||||
end
|
||||
local fields: { [string]: any } = {}
|
||||
while true do
|
||||
local fk, fv = L:next()
|
||||
if fk == "punct" and fv == "}" then
|
||||
break
|
||||
end
|
||||
if fk ~= "word" then
|
||||
return nil, (L.err or "expected a field name") .. " at line " .. L:line()
|
||||
end
|
||||
local ek, ev = L:next()
|
||||
if ek ~= "punct" or ev ~= "=" then
|
||||
return nil, "expected = at line " .. L:line()
|
||||
end
|
||||
local v, err = value(L)
|
||||
if err then
|
||||
return nil, err .. " at line " .. L:line()
|
||||
end
|
||||
local sk, sv = L:next()
|
||||
if sk ~= "punct" or sv ~= ";" then
|
||||
return nil, "expected ; at line " .. L:line()
|
||||
end
|
||||
fields[string.lower(fv)] = v
|
||||
end
|
||||
table.insert(doc.blocks, { type = name, fields = fields })
|
||||
else
|
||||
return nil, "expected = or { at line " .. L:line()
|
||||
end
|
||||
end
|
||||
return doc, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ map
|
||||
local LINE_FLAGS: { [string]: number } = { blocking = 1, blockmonsters = 2, twosided = 4, dontpegtop = 8, dontpegbottom = 16, secret = 32, blocksound = 64, dontdraw = 128, mapped = 256 }
|
||||
-- Hexen SPAC activation (bits 10..12) and repeat (bit 9)
|
||||
local SPAC: { [string]: number } = { playercross = 0, playeruse = 1, monstercross = 2, impact = 3, playerpush = 4, missilecross = 5 }
|
||||
|
||||
local function num(f: { [string]: any }, k: string, default: number): number
|
||||
local v = f[k]
|
||||
return if type(v) == "number" then v else default
|
||||
end
|
||||
|
||||
local function flag(f: { [string]: any }, k: string): boolean
|
||||
return f[k] == true
|
||||
end
|
||||
|
||||
local function tex(f: { [string]: any }, k: string, default: string): string
|
||||
local v = f[k]
|
||||
return if type(v) == "string" then string.upper(v) else default
|
||||
end
|
||||
|
||||
local function args(f: { [string]: any }): { number }
|
||||
return { num(f, "arg0", 0), num(f, "arg1", 0), num(f, "arg2", 0), num(f, "arg3", 0), num(f, "arg4", 0) }
|
||||
end
|
||||
|
||||
local function index(f: { [string]: any }, k: string): number?
|
||||
local v = f[k]
|
||||
return if type(v) == "number" and v >= 0 and v % 1 == 0 then v + 1 else nil
|
||||
end
|
||||
|
||||
function Udmf.map(doc: Document): (any, string?)
|
||||
local ns = string.lower(doc.namespace or "")
|
||||
local hexenBits = ns ~= "doom" and ns ~= "heretic" and ns ~= "strife"
|
||||
local m = { format = "udmf", namespace = doc.namespace, globals = doc.globals, things = {}, linedefs = {}, sidedefs = {}, vertices = {}, sectors = {}, blocks = {} }
|
||||
for _, b in doc.blocks do
|
||||
local f = b.fields
|
||||
if b.type == "vertex" then
|
||||
local x, y = f.x, f.y
|
||||
if type(x) ~= "number" or type(y) ~= "number" then
|
||||
return nil, "vertex " .. (#m.vertices // 2) .. " has no x / y"
|
||||
end
|
||||
table.insert(m.vertices, x)
|
||||
table.insert(m.vertices, y)
|
||||
elseif b.type == "linedef" then
|
||||
local flags = 0
|
||||
for k, bit in LINE_FLAGS do
|
||||
if flag(f, k) then
|
||||
flags += bit
|
||||
end
|
||||
end
|
||||
if hexenBits then
|
||||
if flag(f, "repeatspecial") then
|
||||
flags += 512
|
||||
end
|
||||
for k, a in SPAC do
|
||||
if flag(f, k) then
|
||||
flags = bit32.bor(bit32.band(flags, bit32.bnot(0x1C00)), a * 1024)
|
||||
end
|
||||
end
|
||||
end
|
||||
local v1, v2 = index(f, "v1"), index(f, "v2")
|
||||
if not v1 or not v2 then
|
||||
return nil, "linedef " .. #m.linedefs .. " has no v1 / v2"
|
||||
end
|
||||
local a = args(f)
|
||||
table.insert(m.linedefs, {
|
||||
v1 = v1,
|
||||
v2 = v2,
|
||||
flags = flags,
|
||||
special = num(f, "special", 0),
|
||||
tag = if hexenBits then num(f, "id", -1) else a[1],
|
||||
args = a,
|
||||
front = index(f, "sidefront"),
|
||||
back = index(f, "sideback"),
|
||||
fields = f,
|
||||
})
|
||||
elseif b.type == "sidedef" then
|
||||
local s = index(f, "sector")
|
||||
if not s then
|
||||
return nil, "sidedef " .. #m.sidedefs .. " has no sector"
|
||||
end
|
||||
table.insert(m.sidedefs, {
|
||||
xoff = num(f, "offsetx", 0),
|
||||
yoff = num(f, "offsety", 0),
|
||||
upper = tex(f, "texturetop", "-"),
|
||||
lower = tex(f, "texturebottom", "-"),
|
||||
middle = tex(f, "texturemiddle", "-"),
|
||||
sector = s,
|
||||
fields = f,
|
||||
})
|
||||
elseif b.type == "sector" then
|
||||
table.insert(m.sectors, {
|
||||
floor = num(f, "heightfloor", 0),
|
||||
ceiling = num(f, "heightceiling", 0),
|
||||
floorFlat = tex(f, "texturefloor", "-"),
|
||||
ceilingFlat = tex(f, "textureceiling", "-"),
|
||||
light = num(f, "lightlevel", 160),
|
||||
special = num(f, "special", 0),
|
||||
tag = num(f, "id", 0),
|
||||
fields = f,
|
||||
})
|
||||
elseif b.type == "thing" then
|
||||
local x, y = f.x, f.y
|
||||
if type(x) ~= "number" or type(y) ~= "number" then
|
||||
return nil, "thing " .. #m.things .. " has no x / y"
|
||||
end
|
||||
local fl = 0
|
||||
if flag(f, "skill1") or flag(f, "skill2") then
|
||||
fl += 1
|
||||
end
|
||||
if flag(f, "skill3") then
|
||||
fl += 2
|
||||
end
|
||||
if flag(f, "skill4") or flag(f, "skill5") then
|
||||
fl += 4
|
||||
end
|
||||
if flag(f, "ambush") then
|
||||
fl += 8
|
||||
end
|
||||
if hexenBits then
|
||||
fl += (if flag(f, "dormant") then 16 else 0) + (if flag(f, "class1") then 32 else 0) + (if flag(f, "class2") then 64 else 0)
|
||||
+ (if flag(f, "class3") then 128 else 0) + (if flag(f, "single") then 256 else 0) + (if flag(f, "coop") then 512 else 0)
|
||||
+ (if flag(f, "dm") then 1024 else 0)
|
||||
else
|
||||
-- Doom / Boom / MBF: 16 not in single player, 32 not in deathmatch, 64 not in coop, 128 friend
|
||||
fl += (if flag(f, "single") then 0 else 16) + (if flag(f, "dm") then 0 else 32) + (if flag(f, "coop") then 0 else 64)
|
||||
+ (if flag(f, "friend") then 128 else 0)
|
||||
end
|
||||
table.insert(m.things, {
|
||||
x = x,
|
||||
y = y,
|
||||
z = num(f, "height", 0),
|
||||
angle = num(f, "angle", 0),
|
||||
type = num(f, "type", 0),
|
||||
flags = fl,
|
||||
tid = num(f, "id", 0),
|
||||
special = num(f, "special", 0),
|
||||
args = args(f),
|
||||
fields = f,
|
||||
})
|
||||
else
|
||||
table.insert(m.blocks, b)
|
||||
end
|
||||
end
|
||||
return m, nil
|
||||
end
|
||||
|
||||
return Udmf
|
||||
@@ -1,9 +1,10 @@
|
||||
--!strict
|
||||
-- archive/wad/Map: Doom / Heretic / Hexen binary map lumps (Hexen when BEHAVIOR is present) to tables and a Scene, with
|
||||
-- vanilla texture alignment (offsets, unpegged) and the sky hack; z up, map units. From the Unofficial Doom Specs and
|
||||
-- the Hexen specs; no code taken. Not supported: UDMF (TEXTMAP).
|
||||
-- codecs/model/DoomMap: Doom / Heretic / Hexen binary map lumps (Hexen when BEHAVIOR is present) and UDMF TEXTMAP to
|
||||
-- tables and a Scene, with vanilla texture alignment (offsets, unpegged) and the sky hack; z up, map units. From the
|
||||
-- Unofficial Doom Specs, the Hexen specs and UDMF 1.1; no code taken. Not supported: UDMF slopes / per-part offsets.
|
||||
local Types = require("@rosetta/Types")
|
||||
local Polygon = require("@rosetta/codecs/model/Polygon")
|
||||
local Udmf = require("@rosetta/codecs/model/DoomMap/Udmf")
|
||||
|
||||
local Map = {}
|
||||
|
||||
@@ -12,12 +13,16 @@ local readu8, readi16, readu16 = buffer.readu8, buffer.readi16, buffer.readu16
|
||||
local writef32, writeu32 = buffer.writef32, buffer.writeu32
|
||||
|
||||
export type Lump = { buf: buffer, at: number, size: number }
|
||||
export type Thing = { x: number, y: number, z: number?, angle: number, type: number, flags: number, tid: number?, special: number?, args: { number }? }
|
||||
export type Linedef = { v1: number, v2: number, flags: number, special: number, tag: number?, args: { number }?, front: number?, back: number? }
|
||||
export type Sidedef = { xoff: number, yoff: number, upper: string, lower: string, middle: string, sector: number }
|
||||
export type Sector = { floor: number, ceiling: number, floorFlat: string, ceilingFlat: string, light: number, special: number, tag: number }
|
||||
export type Fields = { [string]: any } -- UDMF: the block's fields as written (lower-case keys)
|
||||
export type Thing = { x: number, y: number, z: number?, angle: number, type: number, flags: number, tid: number?, special: number?, args: { number }?, fields: Fields? }
|
||||
export type Linedef = { v1: number, v2: number, flags: number, special: number, tag: number?, args: { number }?, front: number?, back: number?, fields: Fields? }
|
||||
export type Sidedef = { xoff: number, yoff: number, upper: string, lower: string, middle: string, sector: number, fields: Fields? }
|
||||
export type Sector = { floor: number, ceiling: number, floorFlat: string, ceilingFlat: string, light: number, special: number, tag: number, fields: Fields? }
|
||||
export type MapData = {
|
||||
format: "doom" | "hexen",
|
||||
format: "doom" | "hexen" | "udmf",
|
||||
namespace: string?, -- UDMF
|
||||
globals: Fields?, -- UDMF global assignments
|
||||
blocks: { Udmf.Block }?, -- UDMF blocks other than vertex / linedef / sidedef / sector / thing
|
||||
things: { Thing },
|
||||
linedefs: { Linedef },
|
||||
sidedefs: { Sidedef },
|
||||
@@ -136,6 +141,16 @@ function Map.parse(lumps: { [string]: Lump }, hexen: boolean): (MapData?, string
|
||||
return m, nil
|
||||
end
|
||||
|
||||
function Map.udmf(text: string, maxBlocks: number?): (MapData?, string?)
|
||||
local doc, err = Udmf.parse(text, maxBlocks)
|
||||
if not doc then
|
||||
return nil, err
|
||||
end
|
||||
return Udmf.map(doc)
|
||||
end
|
||||
|
||||
Map.parseUdmf = Udmf.parse
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sector outlines
|
||||
-- sector loops: front sides v1 -> v2, back sides v2 -> v1, lines with the sector on both sides left out; the sharpest
|
||||
-- right turn at forks; unclosed chains dropped
|
||||
@@ -0,0 +1,130 @@
|
||||
--!strict
|
||||
-- codecs/text/Lexer: C-style tokens for id Tech / UDMF text (idLib idLexer conventions, UDMF 1.1 §I; no code taken):
|
||||
-- // and /* */ comments, "strings" with backslash escapes, numbers (decimal, float, 0x hex), bare words, and single
|
||||
-- punctuation characters { } ( ) [ ] ; = ,.
|
||||
local Lexer = {}
|
||||
|
||||
export type Kind = "word" | "number" | "string" | "punct"
|
||||
export type Lexer = {
|
||||
s: string,
|
||||
p: number, -- next byte to read, 1-based
|
||||
n: number,
|
||||
raw: string?, -- the last token's text as written
|
||||
err: string?,
|
||||
next: (self: Lexer) -> (Kind?, any),
|
||||
peek: (self: Lexer) -> (Kind?, any),
|
||||
line: (self: Lexer, at: number?) -> number,
|
||||
}
|
||||
|
||||
local find, sub, byte, match = string.find, string.sub, string.byte, string.match
|
||||
local ESC: { [string]: string } = { n = "\n", t = "\t", r = "\r", ["0"] = "\0" }
|
||||
|
||||
local function skip(L: Lexer): number?
|
||||
local s, p = L.s, L.p
|
||||
while true do
|
||||
p = find(s, "[^ \t\r\n\f\v]", p)
|
||||
if not p then
|
||||
return nil
|
||||
end
|
||||
if byte(s, p) == 47 then
|
||||
local c2 = byte(s, p + 1)
|
||||
if c2 == 47 then
|
||||
local e = find(s, "\n", p + 2, true)
|
||||
if not e then
|
||||
return nil
|
||||
end
|
||||
p = e + 1
|
||||
elseif c2 == 42 then
|
||||
local _, e = find(s, "*/", p + 2, true)
|
||||
if not e then
|
||||
L.err = "unterminated comment"
|
||||
return nil
|
||||
end
|
||||
p = e + 1
|
||||
else
|
||||
return p
|
||||
end
|
||||
else
|
||||
return p
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function token(L: Lexer): (Kind?, any, number)
|
||||
local p = skip(L)
|
||||
if not p then
|
||||
return nil, nil, L.n + 1
|
||||
end
|
||||
local s = L.s
|
||||
local c = byte(s, p)
|
||||
if c == 34 then
|
||||
local parts: { string }? = nil
|
||||
local q = p + 1
|
||||
while true do
|
||||
local e = find(s, "[\"\\]", q)
|
||||
if not e then
|
||||
L.err = "unterminated string"
|
||||
return nil, nil, L.n + 1
|
||||
end
|
||||
if byte(s, e) == 34 then
|
||||
local tail = sub(s, q, e - 1)
|
||||
if parts then
|
||||
table.insert(parts, tail)
|
||||
L.raw = sub(s, p, e)
|
||||
return "string", table.concat(parts), e + 1
|
||||
end
|
||||
L.raw = sub(s, p, e)
|
||||
return "string", tail, e + 1
|
||||
end
|
||||
parts = parts or {}
|
||||
local ch = sub(s, e + 1, e + 1)
|
||||
table.insert(parts :: { string }, sub(s, q, e - 1))
|
||||
table.insert(parts :: { string }, ESC[ch] or ch)
|
||||
q = e + 2
|
||||
end
|
||||
end
|
||||
if find("{}()[];=,", string.char(c), 1, true) then
|
||||
L.raw = string.char(c)
|
||||
return "punct", L.raw, p + 1
|
||||
end
|
||||
local w = match(s, "^[^ \t\r\n\f\v{}()%[%];=,\"]+", p) :: string
|
||||
local e = p + #w
|
||||
-- a comment right after a word ends it
|
||||
local cm = find(w, "/[/*]")
|
||||
if cm then
|
||||
w = sub(w, 1, cm - 1)
|
||||
e = p + #w
|
||||
end
|
||||
L.raw = w
|
||||
if (c >= 48 and c <= 57) or c == 45 or c == 43 or c == 46 then
|
||||
local v = tonumber(w)
|
||||
if v and not find(w, "[iInN]") then
|
||||
return "number", v, e
|
||||
end
|
||||
end
|
||||
return "word", w, e
|
||||
end
|
||||
|
||||
local function nextToken(L: Lexer): (Kind?, any)
|
||||
local k, v, e = token(L)
|
||||
L.p = e
|
||||
return k, v
|
||||
end
|
||||
|
||||
local function peek(L: Lexer): (Kind?, any)
|
||||
local raw = L.raw
|
||||
local k, v = token(L)
|
||||
L.raw = raw
|
||||
return k, v
|
||||
end
|
||||
|
||||
local function line(L: Lexer, at: number?): number
|
||||
local _, n = string.gsub(sub(L.s, 1, (at or L.p) - 1), "\n", "")
|
||||
return n + 1
|
||||
end
|
||||
|
||||
function Lexer.new(s: string): Lexer
|
||||
return { s = s, p = 1, n = #s, raw = nil, err = nil, next = nextToken, peek = peek, line = line }
|
||||
end
|
||||
|
||||
return Lexer
|
||||
@@ -0,0 +1,81 @@
|
||||
--!strict
|
||||
-- model/udmf: a standalone UDMF TEXTMAP (UDMF 1.1, zdoom.org specs/udmf.txt) into a Scene through codecs/model/DoomMap,
|
||||
-- read only. Texture sizes come from opts.textureSize (default 64 x 128 walls, 64 x 64 flats); textures are URIs.
|
||||
-- Maps inside a WAD load through archive/wad (Wad.map). Not supported: writing, slopes, per-part offsets.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Sniff = require("@rosetta/core/Sniff")
|
||||
local Types = require("@rosetta/Types")
|
||||
local DoomMap = require("@rosetta/codecs/model/DoomMap")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Udmf = {
|
||||
name = "udmf",
|
||||
category = "model",
|
||||
extensions = { "udmf", "textmap" },
|
||||
mime = { "text/x-udmf" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/model/DoomMap" },
|
||||
caps = { decode = true, encode = false, maps = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
textureSize: ((kind: string, name: string) -> (number?, number?))?, -- kind "texture" or "flat"
|
||||
decodeTexture: ((kind: string, name: string) -> Types.Image?)?,
|
||||
name: string?, -- the mesh name, default "MAP"
|
||||
maxBlocks: number?, -- default 2^22
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Udmf.sniff(r: Bytes.Reader): number
|
||||
local head = Sniff.text(r, 1024, true)
|
||||
for _ = 1, 32 do
|
||||
local rest = string.match(head, "^//[^\n]*\n%s*(.*)$") or string.match(head, "^/%*.-%*/%s*(.*)$")
|
||||
if not rest then
|
||||
break
|
||||
end
|
||||
head = rest
|
||||
end
|
||||
if string.match(head, "^[Nn][Aa][Mm][Ee][Ss][Pp][Aa][Cc][Ee]%s*=%s*\"") then
|
||||
return 0.9
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
function Udmf.data(r: Bytes.Reader, opts: DecodeOptions?): (DoomMap.MapData?, Err.Err?)
|
||||
local text = Sniff.dropBom(r:string(r:remaining()))
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
local m, msg = DoomMap.udmf(text, opts and opts.maxBlocks)
|
||||
if not m then
|
||||
return nil, Err.from(msg)
|
||||
end
|
||||
return m, nil
|
||||
end
|
||||
|
||||
function Udmf.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
||||
local m, err = Udmf.data(r, opts)
|
||||
if not m then
|
||||
return nil, err
|
||||
end
|
||||
local size = opts and opts.textureSize
|
||||
local tex: DoomMap.Textures = {
|
||||
size = function(kind: string, name: string): (number?, number?)
|
||||
if size then
|
||||
return size(kind, name)
|
||||
end
|
||||
return nil, nil
|
||||
end,
|
||||
image = opts and opts.decodeTexture,
|
||||
}
|
||||
local name = opts and opts.name or "MAP"
|
||||
local scene = DoomMap.scene(m, tex, name)
|
||||
scene.meta.source = { format = "udmf", namespace = m.namespace, things = #m.things, linedefs = #m.linedefs, sectors = #m.sectors, vertices = #m.vertices // 2 }
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
return Udmf
|
||||
Reference in new issue
Block a user