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
@@ -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