merge ws/quake
This commit is contained in:
commit
58df9e8a3a
39 files changed
+10044
-1413
No files matched your search
@@ -0,0 +1,237 @@
|
||||
--!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
|
||||
@@ -0,0 +1,94 @@
|
||||
--!strict
|
||||
-- data/q3skin: Quake III model skins (.skin: "surface,shader" lines, "tag_*," for tags; tr_image.c RE_RegisterSkin, no
|
||||
-- code taken), read and write. Decodes to a list of { surface, shader } in file order (shader "" for tags).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Q3Skin = {
|
||||
name = "q3skin",
|
||||
category = "data",
|
||||
extensions = { "skin" },
|
||||
mime = { "text/x-quake3-skin" },
|
||||
deps = { "core/Err", "core/Bytes" },
|
||||
caps = { decode = true, encode = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type Entry = { surface: string, shader: string }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Q3Skin.sniff(r: Bytes.Reader): number
|
||||
local head = r:peek(math.min(r:remaining(), 1024))
|
||||
if #head < 3 or string.find(head, "[%z\1-\8\14-\31]") then
|
||||
return 0
|
||||
end
|
||||
local lines, good, paths = 0, 0, 0
|
||||
for line in string.gmatch(head, "[^\r\n]+") do
|
||||
if not string.match(line, "^%s*$") then
|
||||
lines += 1
|
||||
local surface, shader = string.match(line, "^%s*([%w_%-]+)%s*,%s*([^,%s]*)%s*$")
|
||||
if surface and shader == "" and string.sub(string.lower(surface), 1, 4) == "tag_" then
|
||||
good += 1
|
||||
elseif shader and string.find(shader, "/", 1, true) then
|
||||
good += 1
|
||||
paths += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
return if lines > 0 and good == lines and paths > 0 then 0.5 else 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
function Q3Skin.decode(r: Bytes.Reader): ({ Entry }?, Err.Err?)
|
||||
local text = buffer.readstring(r.buf, r.base, r.limit - r.base)
|
||||
local out: { Entry } = {}
|
||||
local n = 0
|
||||
for line in string.gmatch(text, "[^\r\n]+") do
|
||||
n += 1
|
||||
local s = string.match(line, "^%s*(.-)%s*$") :: string
|
||||
if s ~= "" and string.sub(s, 1, 2) ~= "//" then
|
||||
local surface, shader = string.match(s, "^([^,]-)%s*,%s*(.-)$")
|
||||
if not surface or surface == "" then
|
||||
return nil, Err.new(Err.CORRUPT, "line " .. n .. ": expected surface,shader")
|
||||
end
|
||||
table.insert(out, { surface = surface, shader = shader :: string })
|
||||
end
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
function Q3Skin.encode(list: { Entry }): (buffer?, Err.Err?)
|
||||
if type(list) ~= "table" then
|
||||
return nil, Err.new(Err.ARG, "expected a list of { surface, shader }")
|
||||
end
|
||||
local out: { string } = {}
|
||||
for _, e in list do
|
||||
if type(e) ~= "table" or type(e.surface) ~= "string" or e.surface == "" or string.find(e.surface, "[,\r\n]") then
|
||||
return nil, Err.new(Err.ARG, "a skin entry needs a surface name without commas")
|
||||
end
|
||||
local sh = if e.shader == nil then "" else tostring(e.shader)
|
||||
if string.find(sh, "[,\r\n]") then
|
||||
return nil, Err.new(Err.ARG, "a shader path cannot hold commas or line breaks")
|
||||
end
|
||||
table.insert(out, e.surface .. "," .. sh)
|
||||
end
|
||||
table.insert(out, "")
|
||||
return buffer.fromstring(table.concat(out, "\n")), nil
|
||||
end
|
||||
|
||||
-- the shader of a surface (case-insensitive)
|
||||
function Q3Skin.shader(list: { Entry }, surface: string): string?
|
||||
local want = string.lower(surface)
|
||||
for _, e in list do
|
||||
if string.lower(e.surface) == want then
|
||||
return e.shader
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
return Q3Skin
|
||||
Reference in new issue
Block a user