--!strict -- data/q3shader: Quake III shader scripts (.shader; the Quake III Arena Shader Manual, tr_shader.c tokenizing: no code -- taken), read and write. A script is a list of shaders { name, directives, stages }; a directive is its tokens -- ({ "surfaceparm", "nolightmap" }, one line each), a stage is a list of directives. Keywords keep their case. -- Comments (// and /* */) are dropped. -- ------------------------------------------------------------------------------------------------ requires local Err = require("@rosetta/core/Err") local Bytes = require("@rosetta/core/Bytes") -- ------------------------------------------------------------------------------------------------ contract local Q3Shader = { name = "q3shader", category = "data", extensions = { "shader" }, mime = { "text/x-quake3-shader" }, deps = { "core/Err", "core/Bytes" }, caps = { decode = true, encode = true }, } -- ------------------------------------------------------------------------------------------------ types export type Directive = { string } export type Shader = { name: string, directives: { Directive }, stages: { { Directive } } } export type EncodeOptions = { indent: string? } -- default "\t" -- ------------------------------------------------------------------------------------------------ constants local KEYWORDS = { "surfaceparm", "map", "clampmap", "animmap", "blendfunc", "rgbgen", "alphagen", "tcgen", "tcmod", "cull", "deformvertexes", "qer_editorimage", "nopicmip", "nomipmaps", "skyparms", "q3map_", "depthwrite", "alphafunc" } -- ------------------------------------------------------------------------------------------------ sniff function Q3Shader.sniff(r: Bytes.Reader): number local head = r:peek(math.min(r:remaining(), 2048)) if #head < 4 or string.find(head, "[%z\1-\8\14-\31]") then return 0 end local s = string.gsub(string.gsub(head, "/%*.-%*/", " "), "//[^\n]*", "") if not string.match(s, "^%s*[^%s{}\"]+%s*{") then return 0 end local lower = string.lower(s) for _, k in KEYWORDS do if string.find(lower, "%f[%w_]" .. k) then return 0.7 end end return 0.2 end -- ------------------------------------------------------------------------------------------------ decode -- the token lines of a script; braces are lines of their own local function lex(text: string): ({ { string } }?, string?) local lines: { { string } } = {} local cur: { string } = {} local i, n = 1, #text local function flush() if #cur > 0 then table.insert(lines, cur) cur = {} end end while i <= n do local c = string.sub(text, i, i) if c == "\n" then flush() i += 1 elseif string.match(c, "%s") then i += 1 elseif string.sub(text, i, i + 1) == "//" then local e = string.find(text, "\n", i, true) i = e or n + 1 elseif string.sub(text, i, i + 1) == "/*" then local _, e = string.find(text, "*/", i + 2, true) if not e then return nil, "unterminated comment" end for _ in string.gmatch(string.sub(text, i, e), "\n") do flush() end i = e + 1 elseif c == "{" or c == "}" then flush() table.insert(lines, { c }) i += 1 elseif c == '"' then local e = string.find(text, '"', i + 1, true) if not e then return nil, "unterminated string" end table.insert(cur, string.sub(text, i + 1, e - 1)) i = e + 1 else local _, e = string.find(text, "^[^%s{}\"]+", i) local stop = e :: number local tok = string.sub(text, i, stop) local cm = string.find(tok, "//", 1, true) if cm and cm > 1 then tok = string.sub(tok, 1, cm - 1) stop = i + cm - 2 end table.insert(cur, tok) i = stop + 1 end end flush() return lines, nil end function Q3Shader.decode(r: Bytes.Reader): ({ Shader }?, Err.Err?) local text = buffer.readstring(r.buf, r.base, r.limit - r.base) local lines, msg = lex(text) if not lines then return nil, Err.new(Err.CORRUPT, msg or "bad script") end local out: { Shader } = {} local k = 1 while k <= #lines do local l = lines[k] if #l ~= 1 or l[1] == "{" or l[1] == "}" then return nil, Err.new(Err.CORRUPT, "expected a shader name, got '" .. table.concat(l, " ") .. "'") end local sh: Shader = { name = l[1], directives = {}, stages = {} } k += 1 if not lines[k] or lines[k][1] ~= "{" then return nil, Err.new(Err.CORRUPT, "expected '{' after " .. sh.name) end k += 1 local stage: { Directive }? = nil local closed = false while k <= #lines do local d = lines[k] k += 1 if d[1] == "{" then if stage then return nil, Err.new(Err.CORRUPT, "nested stage in " .. sh.name) end stage = {} elseif d[1] == "}" then if stage then table.insert(sh.stages, stage) stage = nil else closed = true break end elseif stage then table.insert(stage, d) else table.insert(sh.directives, d) end end if not closed then return nil, Err.new(Err.TRUNCATED, "shader " .. sh.name .. " not closed") end table.insert(out, sh) end return out, nil end -- ------------------------------------------------------------------------------------------------ encode local function tokens(d: any): (string?, string?) if type(d) ~= "table" or #d == 0 then return nil, "a directive is a list of strings" end local parts = {} for _, t in d do local s = tostring(t) if s == "" or string.find(s, "[%s{}]") then if string.find(s, '"', 1, true) or string.find(s, "\n") then return nil, "token cannot hold quotes or line breaks: " .. s end s = '"' .. s .. '"' end table.insert(parts, s) end return table.concat(parts, " "), nil end function Q3Shader.encode(list: { Shader }, opts: EncodeOptions?): (buffer?, Err.Err?) if type(list) ~= "table" then return nil, Err.new(Err.ARG, "expected a list of shaders") end local ind = opts and opts.indent or "\t" local out: { string } = {} for _, sh in list do if type(sh) ~= "table" or type(sh.name) ~= "string" or sh.name == "" or string.find(sh.name, "[%s{}\"]") then return nil, Err.new(Err.ARG, "a shader needs a name without spaces") end table.insert(out, sh.name) table.insert(out, "{") for _, d in sh.directives or {} do local line, e = tokens(d) if not line then return nil, Err.new(Err.ARG, e or "bad directive") end table.insert(out, ind .. line) end for _, st in sh.stages or {} do table.insert(out, ind .. "{") for _, d in st do local line, e = tokens(d) if not line then return nil, Err.new(Err.ARG, e or "bad directive") end table.insert(out, ind .. ind .. line) end table.insert(out, ind .. "}") end table.insert(out, "}") table.insert(out, "") end return buffer.fromstring(table.concat(out, "\n")), nil end -- ------------------------------------------------------------------------------------------------ extras -- the shader named name (case-insensitive, as the game looks them up) function Q3Shader.find(list: { Shader }, name: string): Shader? local want = string.lower(name) for _, sh in list do if string.lower(sh.name) == want then return sh end end return nil end -- the first directive starting with keyword (case-insensitive) in a shader or stage directive list function Q3Shader.directive(list: { Directive }, keyword: string): Directive? local want = string.lower(keyword) for _, d in list do if string.lower(d[1] or "") == want then return d end end return nil end return Q3Shader