ws-game: archive/grp (Build engine GRP read + write)
This commit is contained in:
1 parent
5af4aa361e
commit
eac069f7dc
5 files changed
+227
No files matched your search
@@ -52,6 +52,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
|
||||
| archive | 7z | .7z | yes | - | methods: "copy", "lzma", "lzma2", "deflate", "bcj", "delta", encrypted: false | `tests/archive/7z.luau` |
|
||||
| archive | br | .br | yes | - | stream: true | `tests/archive/br.luau` |
|
||||
| archive | bz2 | .bz2 .tbz2 .tbz | yes | - | stream: true | `tests/archive/bz2.luau` |
|
||||
| archive | grp | .grp | yes | yes | | `tests/archive/grp.luau` |
|
||||
| archive | gzip | .gz .tgz .gzip | yes | yes | stream: true | `tests/archive/gzip.luau` |
|
||||
| archive | pak | .pak .sin | yes | yes | variants: "quake", "sin" | `tests/archive/pak.luau` |
|
||||
| archive | tar | .tar | yes | yes | variants: "ustar", "pax", "gnu" | `tests/archive/tar.luau` |
|
||||
|
||||
@@ -578,6 +578,18 @@ local list: { Entry } = {
|
||||
return require("@formats/archive/bz2")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "grp",
|
||||
category = "archive",
|
||||
path = "archive/grp",
|
||||
extensions = { "grp" },
|
||||
mime = { "application/x-build-grp" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "containers/Archive" },
|
||||
caps = { decode = true, encode = true },
|
||||
load = function()
|
||||
return require("@formats/archive/grp")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "gzip",
|
||||
category = "archive",
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
--!strict
|
||||
-- archive/grp: Build engine GRP archives (Duke Nukem 3D, Shadow Warrior, Blood: "KenSilverman", a u32 file count,
|
||||
-- 16-byte entries of a 12-byte name and a u32 size, then the files back to back), read into the standard Archive
|
||||
-- (entries read lazily) and written from any Archive value (names upper-cased to 8.3 by the caller's choice: they
|
||||
-- are stored as given, at most 12 bytes). Written from Ken Silverman's GRP description (kenbuild); no code taken.
|
||||
-- Not supported: the "GRP 2" / RFF / SSI variants.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Sniff = require("@formats/core/Sniff")
|
||||
local Archive = require("@formats/containers/Archive")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Grp = {
|
||||
name = "grp",
|
||||
category = "archive",
|
||||
extensions = { "grp" },
|
||||
mime = { "application/x-build-grp" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "containers/Archive" },
|
||||
caps = { decode = true, encode = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = { maxEntries: number? } -- default 1e6
|
||||
export type EncodeOptions = { upper: boolean? } -- upper-case the names (Build looks them up case-insensitively; default false)
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local MAGIC = "KenSilverman"
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Grp.sniff(r: Bytes.Reader): number
|
||||
return Sniff.magic(r, MAGIC)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
local function encode(archive: any, opts: any?): (buffer?, Err.Err?)
|
||||
return Grp.encode(archive, opts)
|
||||
end
|
||||
|
||||
function Grp.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
if not r:match(MAGIC) then
|
||||
return nil, Err.new(Err.CORRUPT, "bad magic", 0)
|
||||
end
|
||||
local count = r:u32le()
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
if count > (opts and opts.maxEntries or 1e6) then
|
||||
return nil, Err.new(Err.LIMIT, "too many entries: " .. count)
|
||||
end
|
||||
if count * 16 > r:remaining() then
|
||||
return nil, Err.new(Err.TRUNCATED, "directory past the end", 12)
|
||||
end
|
||||
local a = Archive.new("grp", encode)
|
||||
local at = 16 + count * 16
|
||||
local size = r.limit - r.base
|
||||
for i = 0, count - 1 do
|
||||
local name = r:fixed(12)
|
||||
local n = r:u32le()
|
||||
if at + n > size then
|
||||
return nil, Err.new(Err.TRUNCATED, "entry " .. name .. " past the end", 16 + i * 16)
|
||||
end
|
||||
Archive.put(a, { name = name, size = n, dir = false, method = "store", offset = at, load = Archive.slice(r.buf, r.base + at, n) })
|
||||
at += n
|
||||
end
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
a.meta.source = { format = "grp", entries = count }
|
||||
return a, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
function Grp.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local items, err = Archive.collect(archive)
|
||||
if not items then
|
||||
return nil, err
|
||||
end
|
||||
local files: { { name: string, data: buffer } } = {}
|
||||
for _, it in items do
|
||||
if not it.entry.dir then
|
||||
local name = it.entry.name
|
||||
if opts and opts.upper then
|
||||
name = string.upper(name)
|
||||
end
|
||||
if #name > 12 then
|
||||
return nil, Err.new(Err.ARG, "name longer than 12 bytes: " .. name)
|
||||
end
|
||||
table.insert(files, { name = name, data = it.data })
|
||||
end
|
||||
end
|
||||
local w = Bytes.writer()
|
||||
w:string(MAGIC)
|
||||
w:u32le(#files)
|
||||
for _, f in files do
|
||||
w:string(f.name)
|
||||
w:fill(0, 12 - #f.name)
|
||||
w:u32le(buffer.len(f.data))
|
||||
end
|
||||
for _, f in files do
|
||||
w:bytes(f.data)
|
||||
end
|
||||
return w:finish(), nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new() -> an empty GRP Archive: a:add(name, data); a:write()
|
||||
function Grp.new(): any
|
||||
local a = Archive.new("grp", encode)
|
||||
a.meta.source = { format = "grp" }
|
||||
return a
|
||||
end
|
||||
|
||||
return Grp
|
||||
@@ -0,0 +1,64 @@
|
||||
-- archive/grp tests: Build GRP archives written by tools/fixtures/grp.py list the entries an independent Python
|
||||
-- reader sees (names, sizes, data CRCs); our writer produces the same bytes as the Python writer and round-trips;
|
||||
-- conversion from a zip; errors; the fuzz pass. Fixtures: tools/fixtures/grp.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Grp = require("@formats/archive/grp")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/grp")
|
||||
|
||||
local t = T.suite("grp")
|
||||
t:eq(Grp.name, "grp", "name")
|
||||
|
||||
for _, c in Fx do
|
||||
local a, err = Grp.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
local list = a:list()
|
||||
t:eq(#list, #c.entries, c.name .. ": entry count")
|
||||
for i, w in c.entries do
|
||||
t:eq(list[i].name, w.name, c.name .. ": name " .. i)
|
||||
t:eq(list[i].size, w.size, c.name .. ": size " .. w.name)
|
||||
t:eq(Crc32.of((a:read(w.name))), w.crc, c.name .. ": data " .. w.name)
|
||||
end
|
||||
t:eq(F.detect(c.data), "grp", c.name .. ": F.detect")
|
||||
local n = Grp.new()
|
||||
for _, f in c.files do
|
||||
n:add(f.name, f.data)
|
||||
end
|
||||
t:check(buffer.tostring(n:write() :: buffer) == c.data, c.name .. ": bytes identical to the Python writer")
|
||||
t:check(buffer.tostring(F.save(a) :: buffer) == c.data, c.name .. ": decode + F.save is lossless")
|
||||
end
|
||||
|
||||
do
|
||||
local zip = F.format("zip").create()
|
||||
zip:add("readme.txt", "hi")
|
||||
local g = F.load(F.save(zip, nil, { format = "grp", upper = true }) :: buffer)
|
||||
t:check(g.format == "grp" and g:list()[1].name == "README.TXT", "zip -> grp, upper-cased")
|
||||
local c = Fx[1]
|
||||
local _, e1 = Grp.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, #c.data - 1))))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
|
||||
local _, e2 = Grp.decode(Bytes.reader(buffer.fromstring("KenSilverman\255\255\255\255")))
|
||||
t:check(e2 and e2.code == Err.LIMIT, "count limit: " .. tostring(e2))
|
||||
local long = Grp.new()
|
||||
long:add("THIRTEEN.CHAR", "x")
|
||||
local _, e3 = long:write()
|
||||
t:check(e3 and e3.code == Err.ARG, "a 13-byte name does not fit")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local a = Grp.decode(Bytes.reader(b))
|
||||
if a then
|
||||
for _, e in a:list() do
|
||||
a:read(e.name)
|
||||
end
|
||||
end
|
||||
return a
|
||||
end, samples, { mutations = 40 })
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,35 @@
|
||||
# Build GRP fixtures (tools/fixtures.py): GRP files written here from Ken Silverman's layout ("KenSilverman", count,
|
||||
# 12-byte names + u32 sizes, files back to back), with the entries an independent Python reader sees.
|
||||
# tests/gen/grp.luau { { name, data, entries = { { name, size, crc } }, files = { { name, data } } } }
|
||||
import struct, zlib
|
||||
|
||||
|
||||
def write(files):
|
||||
out = bytearray(b"KenSilverman" + struct.pack("<I", len(files)))
|
||||
for name, data in files:
|
||||
out += struct.pack("<12sI", name.encode(), len(data))
|
||||
for _, data in files:
|
||||
out += data
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def read(data):
|
||||
n = struct.unpack_from("<I", data, 12)[0]
|
||||
at, out = 16 + 16 * n, []
|
||||
for i in range(n):
|
||||
name, size = struct.unpack_from("<12sI", data, 16 + 16 * i)
|
||||
out.append({"name": name.split(b"\0")[0].decode(), "size": size, "crc": zlib.crc32(data[at:at + size])})
|
||||
at += size
|
||||
return out
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
rng = ctx.rng(23)
|
||||
base = [("TILES000.ART", rng.randbytes(4000)), ("PALETTE.DAT", rng.randbytes(768 + 2 + 32 * 256)),
|
||||
("GAME.CON", b"// con\ndefine X 1\n"), ("E1L1.MAP", rng.randbytes(900)), ("EMPTY.VOC", b""),
|
||||
("TWELVECHARSX", b"12 bytes, no NUL")]
|
||||
out = []
|
||||
for name, files in (("duke", base), ("empty", []), ("one", base[:1])):
|
||||
data = write(files)
|
||||
out.append({"name": name, "data": data, "entries": read(data), "files": [{"name": n, "data": d} for n, d in files]})
|
||||
ctx.emit("grp", out)
|
||||
Reference in new issue
Block a user