ws-game: archive/pak (Quake PACK / SiN SPAK read + write); containers/Archive: slice() lazy loader and collect() for simple archive writers (additive)
This commit is contained in:
1 parent
d3dbba500b
commit
eb869811aa
6 files changed
+329
No files matched your search
@@ -0,0 +1,63 @@
|
||||
# Quake PAK fixtures (tools/fixtures.py): PAK files written here from the Quake source's dpackheader_t / dpackfile_t
|
||||
# layout (header, file data in order, directory at the end: the layout id's qpak and most tools write), in the Quake
|
||||
# ("PACK", 56-byte names) and SiN ("SPAK", 120-byte names) variants, plus one with the directory first and padding
|
||||
# between files; with the expected entries read back by an independent Python reader here.
|
||||
# tests/gen/pak.luau { { name, variant, data, entries = { { name, size, crc } }, files = { { name, data } } } }
|
||||
import struct, zlib
|
||||
|
||||
VARIANTS = {"quake": (b"PACK", 56), "sin": (b"SPAK", 120)}
|
||||
|
||||
|
||||
def write(files, variant="quake", dir_first=False, pad=0):
|
||||
magic, nl = VARIANTS[variant]
|
||||
dirlen = len(files) * (nl + 8)
|
||||
out = bytearray(magic + b"\0" * 8)
|
||||
if dir_first:
|
||||
dirofs = 12
|
||||
out += b"\0" * dirlen
|
||||
offs = []
|
||||
for name, data in files:
|
||||
out += b"\xee" * pad
|
||||
offs.append(len(out))
|
||||
out += data
|
||||
if not dir_first:
|
||||
dirofs = len(out)
|
||||
out += b"\0" * dirlen
|
||||
for i, (name, data) in enumerate(files):
|
||||
struct.pack_into("<%dsII" % nl, out, dirofs + i * (nl + 8), name.encode(), offs[i], len(data))
|
||||
struct.pack_into("<II", out, 4, dirofs, dirlen)
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def read(data):
|
||||
magic, dirofs, dirlen = struct.unpack_from("<4sII", data)
|
||||
nl = {b"PACK": 56, b"SPAK": 120}[magic]
|
||||
out = []
|
||||
for i in range(dirlen // (nl + 8)):
|
||||
name, at, n = struct.unpack_from("<%dsII" % nl, data, dirofs + i * (nl + 8))
|
||||
out.append({"name": name.split(b"\0")[0].decode(), "size": n, "crc": zlib.crc32(data[at:at + n])})
|
||||
return out
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
rng = ctx.rng(17)
|
||||
base = [
|
||||
("gfx/palette.lmp", bytes(rng.randrange(256) for _ in range(768))),
|
||||
("maps/start.bsp", rng.randbytes(5000)),
|
||||
("sound/misc/null.wav", b""),
|
||||
("progs/player.mdl", rng.randbytes(1234)),
|
||||
("default.cfg", b"bind w +forward\n"),
|
||||
("a" * 55, b"longest quake name"),
|
||||
]
|
||||
cases = [
|
||||
("quake", "quake", write(base), base),
|
||||
("quake-dir-first", "quake", write(base, dir_first=True, pad=3), base),
|
||||
("empty", "quake", write([]), []),
|
||||
("sin", "sin", write(base + [("models/" + "b" * 100 + ".def", b"sin")], "sin"), base + [("models/" + "b" * 100 + ".def", b"sin")]),
|
||||
]
|
||||
out = []
|
||||
for name, variant, data, files in cases:
|
||||
out.append({"name": name, "variant": variant, "data": data, "entries": read(data),
|
||||
"layoutDefault": not name.endswith("dir-first"),
|
||||
"files": [{"name": n, "data": d} for n, d in files]})
|
||||
ctx.emit("pak", out)
|
||||
Reference in new issue
Block a user