model/vmf: Hammer VMF read and write (Brush solids with Valve 220 axes, displacements, entities, cameras, visgroups, byte-exact write back, document builders); KeyValues: escapes = "quotes", bare / flat block layout for writing

This commit is contained in:
4DBug committed 2026-11-01 19:15:54 -06:00
1 parent c30e8618fc
commit e4840b719b
17 files changed
+4075 -1443

No files matched your search

@@ -3,6 +3,8 @@
-- CCoreDispInfo::GenerateDispSurf of the Source SDK 2013 (no code taken): corner 0 is the base face corner nearest the
-- start position, rows run along corner 0 -> 1, columns along 0 -> 3 (the face's own, clockwise, winding), each vertex
-- moved by its offset; the 2^power quads split along alternating diagonals. Normals come from the grid.
local Types = require("@rosetta/Types")
local Displacement = {}
local sqrt = math.sqrt
@@ -102,4 +104,44 @@ function Displacement.surface(corners: { number }, start: { number }, power: num
return { n = n, flat = flat, pos = pos, nrm = nrm, idx = idx }, nil
end
export type Arrays = {
pos: { number },
nrm: { number },
uv: { number },
idx: { number }, -- 0-based
blend: { number }?, -- per vertex 0..1: the _BLEND attribute (displacement alpha)
}
-- flat arrays -> an indexed triangle primitive (POSITION, NORMAL, TEXCOORD_0, _BLEND)
function Displacement.primitive(a: Arrays): Types.Primitive
local writef32, writeu32 = buffer.writef32, buffer.writeu32
local nv, ni = #a.pos // 3, #a.idx
local pb, nb, ub, ib = buffer.create(nv * 12), buffer.create(nv * 12), buffer.create(nv * 8), buffer.create(ni * 4)
for k = 1, nv * 3 do
writef32(pb, (k - 1) * 4, a.pos[k])
writef32(nb, (k - 1) * 4, a.nrm[k])
end
for k = 1, nv * 2 do
writef32(ub, (k - 1) * 4, a.uv[k])
end
for k = 1, ni do
writeu32(ib, (k - 1) * 4, a.idx[k])
end
local S = Types.Scene
local attrs: { [string]: Types.Accessor } = {
POSITION = S.accessor(pb, "f32", 3, nv),
NORMAL = S.accessor(nb, "f32", 3, nv),
TEXCOORD_0 = S.accessor(ub, "f32", 2, nv),
}
local blend = a.blend
if blend then
local bb = buffer.create(nv * 4)
for k = 1, nv do
writef32(bb, (k - 1) * 4, blend[k] or 0)
end
attrs._BLEND = S.accessor(bb, "f32", 1, nv)
end
return { attributes = attrs, indices = S.accessor(ib, "u32", 1, ni), mode = "triangles" }
end
return Displacement
@@ -11,6 +11,7 @@ export type Pair = {
key: string,
value: any, -- string | number | Block
cond: string?, -- text: the [$CONDITION] kept when not evaluated
bare: boolean?, -- text: a block whose name was unquoted (written back so with bareBlocks)
type: string?, -- binary: "int32" | "float32" | "pointer" | "wstring" | "color" | "uint64" | "int64" (nil: string / block)
}
export type Block = { Pair }
+29 -16
View File
@@ -11,7 +11,7 @@ local KeyValues = {}
export type Pair = Binary.Pair
export type Block = Binary.Block
export type ParseOptions = {
escapes: boolean?, -- \n \t \\ \" ... in quoted strings, default true
escapes: (boolean | "quotes")?, -- \n \t \\ \" ... in quoted strings, default true; "quotes": only \"
-- [$X] conditions: the flags that are true (case-insensitive names, without $); default { WIN32 = true }, the
-- other SDK names (X360, PS3, GAMECONSOLE, OSX, LINUX, POSIX, WINDOWS, DECK) false; false keeps every pair with
-- its cond
@@ -20,8 +20,11 @@ export type ParseOptions = {
maxDepth: number?, -- default 256
}
export type WriteOptions = {
escapes: boolean?, -- default true
escapes: (boolean | "quotes")?, -- default true; "quotes": only " is escaped
indent: string?, -- default "\t"
separator: string?, -- between a key and its value, default "\t" (Hammer: " ")
bareBlocks: boolean?, -- block names unquoted when they are plain words (Hammer), default false
flatBlocks: { [string]: boolean }?, -- blocks whose pairs keep the block's own indent (Hammer's dispinfo rows)
}
export type ObjectOptions = {
duplicates: ("array" | "last" | "first")?, -- default "array": a repeated key's values become an array
@@ -31,7 +34,7 @@ local DEFAULT_DEFINES: { [string]: boolean } = { win32 = true }
local ESCAPE: { [string]: string } = { n = "\n", t = "\t", v = "\v", b = "\b", r = "\r", f = "\f", a = "\a", ["\\"] = "\\", ["?"] = "?", ["'"] = "'", ['"'] = '"' }
type Lexer = { s: string, i: number, n: number, escapes: boolean }
type Lexer = { s: string, i: number, n: number, escapes: boolean, quotesOnly: boolean?, quoted: boolean? }
-- kind: "s" string, "{", "}", "[" condition (its text), nil at the end; err on an unterminated string
local function token(lx: Lexer): (string?, string?, number, string?)
@@ -60,6 +63,7 @@ local function token(lx: Lexer): (string?, string?, number, string?)
lx.i = i + 1
return c, nil, i - 1, nil
elseif c == '"' then
lx.quoted = true
local start = i
i += 1
if not lx.escapes then
@@ -84,7 +88,7 @@ local function token(lx: Lexer): (string?, string?, number, string?)
return "s", table.concat(parts), start - 1, nil
end
local x = string.sub(s, e + 1, e + 1)
local r = ESCAPE[x]
local r = if lx.quotesOnly and x ~= '"' then nil else ESCAPE[x]
if r then
table.insert(parts, r)
i = e + 2
@@ -101,6 +105,7 @@ local function token(lx: Lexer): (string?, string?, number, string?)
return "[", string.sub(s, i + 1, e - 1), i - 1, nil
end
end
lx.quoted = false
local e = string.find(s, '[ \t\r\n\f\v"{}]', i)
local stop = if e then e - 1 else n
lx.i = stop + 1
@@ -145,7 +150,7 @@ end
local function parseText(text: string, opts: ParseOptions?, depth0: number): (Block?, Err.Err?)
local o: ParseOptions = opts or {}
local lx: Lexer = { s = text, i = 1, n = #text, escapes = o.escapes ~= false }
local lx: Lexer = { s = text, i = 1, n = #text, escapes = o.escapes ~= false, quotesOnly = o.escapes == "quotes" }
if string.sub(text, 1, 3) == "\239\187\191" then
lx.i = 4
end
@@ -185,6 +190,7 @@ local function parseText(text: string, opts: ParseOptions?, depth0: number): (Bl
return nil, Err.new(Err.CORRUPT, if kind == "{" then "block without a key" else "condition without a key", at)
else
local key = v :: string
local keyQuoted = lx.quoted
local k2, v2, at2, terr2 = token(lx)
if terr2 then
return nil, Err.new(Err.TRUNCATED, terr2, at2)
@@ -208,7 +214,7 @@ local function parseText(text: string, opts: ParseOptions?, depth0: number): (Bl
if not pass then
skipping += 1
else
local pair: Pair = { key = key, value = {}, cond = if evaluate then nil else cond }
local pair: Pair = { key = key, value = {}, cond = if evaluate then nil else cond, bare = not keyQuoted }
local last = cur[#cur]
if cond and evaluate and canReplace and last and type(last.value) == "table" and last.key == key then
cur[#cur] = pair
@@ -311,7 +317,7 @@ function KeyValues.copy(b: Block): Block
local out: Block = table.create(#b) :: any
for i, p in b do
local v: any = p.value
out[i] = { key = p.key, value = if type(v) == "table" then KeyValues.copy(v) else v, cond = p.cond, type = p.type }
out[i] = { key = p.key, value = if type(v) == "table" then KeyValues.copy(v) else v, cond = p.cond, type = p.type, bare = p.bare }
end
return out
end
@@ -354,18 +360,23 @@ function KeyValues.all(b: Block, key: string): { Pair }
return out
end
local function quote(s: string, escapes: boolean): string
if escapes then
local function quote(s: string, escapes: boolean | string): string
if escapes == "quotes" then
s = string.gsub(s, '"', '\\"')
elseif escapes then
s = string.gsub(s, '[\\"\n\t\r]', { ["\\"] = "\\\\", ['"'] = '\\"', ["\n"] = "\\n", ["\t"] = "\\t", ["\r"] = "\\r" })
end
return '"' .. s .. '"'
end
local function writeBlock(b: Block, out: { string }, indent: string, level: number, escapes: boolean, depth: number): string?
if depth > 256 then
type WriteState = { indent: string, escapes: boolean | string, sep: string, bare: boolean, flat: { [string]: boolean } }
local function writeBlock(b: Block, out: { string }, w: WriteState, level: number, depth: number?): string?
local dp = depth or 0
if dp > 256 then
return "nesting deeper than 256"
end
local pad = string.rep(indent, level)
local pad = string.rep(w.indent, level)
for _, p in b do
if type(p) ~= "table" or type(p.key) ~= "string" then
return "a pair needs a string key"
@@ -373,15 +384,16 @@ local function writeBlock(b: Block, out: { string }, indent: string, level: numb
local v: any = p.value
local cond = if type(p.cond) == "string" then " [" .. p.cond .. "]" else ""
if type(v) == "table" then
table.insert(out, pad .. quote(p.key, escapes) .. cond .. "\n" .. pad .. "{\n")
local err = writeBlock(v, out, indent, level + 1, escapes, depth + 1)
local name = if w.bare and (p.bare or (p.bare == nil and string.match(p.key, "^[%w_]+$"))) then p.key else quote(p.key, w.escapes)
table.insert(out, pad .. name .. cond .. "\n" .. pad .. "{\n")
local err = writeBlock(v, out, w, if w.flat[p.key] then level else level + 1, dp + 1)
if err then
return err
end
table.insert(out, pad .. "}\n")
elseif type(v) == "string" or type(v) == "number" or type(v) == "boolean" then
local s = if type(v) == "boolean" then (if v then "1" else "0") else tostring(v)
table.insert(out, pad .. quote(p.key, escapes) .. "\t" .. quote(s, escapes) .. cond .. "\n")
table.insert(out, pad .. quote(p.key, w.escapes) .. w.sep .. quote(s, w.escapes) .. cond .. "\n")
else
return "unwritable value for key " .. p.key
end
@@ -394,7 +406,8 @@ function KeyValues.write(b: Block, opts: WriteOptions?): (string?, Err.Err?)
return nil, Err.new(Err.ARG, "KeyValues.write: expected a pair list")
end
local out: { string } = {}
local err = writeBlock(b, out, opts and opts.indent or "\t", 0, not (opts and opts.escapes == false), 0)
local o: WriteOptions = opts or {}
local err = writeBlock(b, out, { indent = o.indent or "\t", escapes = if o.escapes == nil then true else o.escapes, sep = o.separator or "\t", bare = o.bareBlocks == true, flat = o.flatBlocks or {} }, 0)
if err then
return nil, Err.new(Err.ARG, err)
end