archive/wad2, image/lmp, image/spr, data/q3shader, data/q3skin
WAD2 / WAD3 with miptex / qpic / conchars / font / palette images and addImage; Quake .lmp pictures, palette and colormap (palette never embedded); Quake / Half-Life / SPR32 sprites with frame groups; Quake III shader scripts and .skin files. Miptex gains qpic decode / encode and palette mapping; Pixels.keyed keeps the key colour (alpha 0).
This commit is contained in:
1 parent
67e116a6c6
commit
175ca9e823
23 files changed
+5380
-1143
No files matched your search
@@ -0,0 +1,90 @@
|
||||
--!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 = 0, 0
|
||||
for line in string.gmatch(head, "[^\r\n]+") do
|
||||
if not string.match(line, "^%s*$") then
|
||||
lines += 1
|
||||
if string.match(line, "^%s*[%w_%-]+%s*,%s*[^,%s]*%s*$") then
|
||||
good += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
return if lines > 0 and good == lines 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