ws-game: archive/vpk (Valve VPK v1 / v2 read incl. multi-part via opts.resolve / opts.files, preload, CRC checks; single-file v1 / v2 write with MD5 section)
This commit is contained in:
1 parent
5ec3121f65
commit
34d0600109
6 files changed
+611
No files matched your search
@@ -0,0 +1,20 @@
|
||||
-- tools/fixtures/_vpk_encode.luau: run by tools/fixtures/vpk.py under the luau CLI: writes the files of
|
||||
-- tests/gen/vpk_src as VPK version 1 and 2 with our encoder and prints "<name>\t<hex>" lines for Python's vpk
|
||||
-- package to read back (and verify the version 2 MD5s).
|
||||
local Vpk = require("@formats/archive/vpk")
|
||||
local Src = require("@tests/gen/vpk_src")
|
||||
|
||||
local function hex(b: buffer): string
|
||||
local s = buffer.tostring(b)
|
||||
return (string.gsub(s, ".", function(c: string): string
|
||||
return string.format("%02x", string.byte(c))
|
||||
end))
|
||||
end
|
||||
|
||||
for _, version in { 1, 2 } do
|
||||
local a = Vpk.new(version)
|
||||
for _, f in Src do
|
||||
a:add(f.name, f.data)
|
||||
end
|
||||
print("v" .. version .. "\t" .. hex(a:write() :: buffer))
|
||||
end
|
||||
@@ -0,0 +1,122 @@
|
||||
# Valve VPK fixtures (tools/fixtures.py). References:
|
||||
# - Python's vpk package (nix python3Packages.vpk) writes version 1 and 2 directory files (data embedded, MD5
|
||||
# section) from a folder of random files; its reader lists them (names, sizes, CRC-32s);
|
||||
# - a multi-part archive written here from the VDC layout: a pak01_dir.vpk whose entries live in pak01_000.vpk /
|
||||
# pak01_001.vpk, with preload bytes in the tree, plus an embedded one;
|
||||
# - our encoder's output (tools/fixtures/_vpk_encode.luau through the luau CLI) read back by the vpk package, with
|
||||
# version 2's MD5 verify(): verdicts in `ours`.
|
||||
# tests/gen/vpk_src.luau { { name, data } } (the files)
|
||||
# tests/gen/vpk.luau { cases = { { name, data, version, entries = { { name, size, crc } } } }, multi = { dir,
|
||||
# parts = { [name] = data }, entries }, ours = { v1 = { ok, entries }, v2 = ... } }
|
||||
import importlib.util, json, os, shutil, struct, tempfile, zlib
|
||||
|
||||
INPUTS = ["src/Formats/archive/vpk", "tools/fixtures/_vpk_encode.luau"]
|
||||
|
||||
|
||||
def luau_encode(ctx, script):
|
||||
spec = importlib.util.spec_from_file_location("_model_ref", os.path.join(ctx.root, "tools", "fixtures", "_model_ref.py"))
|
||||
m = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(m)
|
||||
return m.luau_encode(ctx, script)
|
||||
|
||||
READ = r"""
|
||||
import json, sys, vpk, zlib
|
||||
out = {}
|
||||
for name, path in json.loads(sys.stdin.read()).items():
|
||||
p = vpk.open(path)
|
||||
ents = []
|
||||
for f in sorted(p):
|
||||
d = p.get_file(f).read()
|
||||
ents.append({"name": f, "size": len(d), "crc": zlib.crc32(d)})
|
||||
ok = True
|
||||
if p.version == 2:
|
||||
try:
|
||||
ok = bool(p.verify())
|
||||
except Exception as e:
|
||||
ok = False
|
||||
out[name] = {"version": p.version, "entries": ents, "ok": ok}
|
||||
print(json.dumps(out))
|
||||
"""
|
||||
|
||||
WRITE = r"""
|
||||
import json, sys, vpk
|
||||
args = json.loads(sys.stdin.read())
|
||||
n = vpk.new(args["src"])
|
||||
n.version = args["version"]
|
||||
n.save(args["out"])
|
||||
"""
|
||||
|
||||
|
||||
def files(rng):
|
||||
out = [("scripts/game.txt", b"\"game\" { }\n"), ("materials/brick/wall01.vmt", b"LightmappedGeneric { }"),
|
||||
("materials/brick/wall01.vtf", rng.randbytes(3000)), ("models/props/crate.mdl", rng.randbytes(2048)),
|
||||
("models/props/crate.vvd", rng.randbytes(777)), ("readme.txt", b"root file"), ("sound/empty.wav", b""),
|
||||
("maps/de_test.bsp", rng.randbytes(5000))]
|
||||
return out
|
||||
|
||||
|
||||
def multi(rng):
|
||||
# entries: (path, ext, name, preload, archive index, data)
|
||||
ents = [("materials", "vmt", "a", b"pre", 0, b"material a body"), ("materials", "vmt", "b", b"", 1, rng.randbytes(500)),
|
||||
("models", "mdl", "c", b"\x01\x02", 0, rng.randbytes(300)), (" ", "txt", "root", b"", 0x7fff, b"embedded"),
|
||||
("sound", "wav", "d", b"only preload", 1, b"")]
|
||||
parts = {0: bytearray(), 1: bytearray()}
|
||||
embedded = bytearray()
|
||||
tree = bytearray()
|
||||
exts = []
|
||||
for e in ents:
|
||||
if e[1] not in exts:
|
||||
exts.append(e[1])
|
||||
for ext in exts:
|
||||
tree += ext.encode() + b"\0"
|
||||
paths = []
|
||||
for e in ents:
|
||||
if e[1] == ext and e[0] not in paths:
|
||||
paths.append(e[0])
|
||||
for path in paths:
|
||||
tree += path.encode() + b"\0"
|
||||
for (p, x, name, pre, idx, data) in ents:
|
||||
if p == path and x == ext:
|
||||
target = embedded if idx == 0x7fff else parts[idx]
|
||||
off = len(target)
|
||||
target += data
|
||||
tree += name.encode() + b"\0" + struct.pack("<IHHIIH", zlib.crc32(pre + data), len(pre), idx, off, len(data), 0xffff) + pre
|
||||
tree += b"\0"
|
||||
tree += b"\0"
|
||||
tree += b"\0"
|
||||
d = struct.pack("<III", 0x55aa1234, 1, len(tree)) + bytes(tree) + bytes(embedded)
|
||||
entries = [{"name": ("" if p == " " else p + "/") + n + "." + x, "size": len(pre) + len(data), "crc": zlib.crc32(pre + data)} for (p, x, n, pre, i, data) in ents]
|
||||
return {"dir": d, "parts": {"pak01_000.vpk": bytes(parts[0]), "pak01_001.vpk": bytes(parts[1])}, "entries": entries}
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
rng = ctx.rng(31)
|
||||
fs = files(rng)
|
||||
tmp = tempfile.mkdtemp(prefix="formats-vpk-")
|
||||
try:
|
||||
src = os.path.join(tmp, "src")
|
||||
for name, data in fs:
|
||||
p = os.path.join(src, name)
|
||||
os.makedirs(os.path.dirname(p), exist_ok=True)
|
||||
open(p, "wb").write(data)
|
||||
paths = {}
|
||||
for v in (1, 2):
|
||||
out = os.path.join(tmp, "v%d_dir.vpk" % v)
|
||||
ctx.python(["vpk"], WRITE, input=json.dumps({"src": src, "version": v, "out": out}).encode())
|
||||
paths["v%d" % v] = out
|
||||
ctx.emit("vpk_src", [{"name": n, "data": d} for n, d in fs])
|
||||
ours = luau_encode(ctx, "tools/fixtures/_vpk_encode.luau")
|
||||
for k, b in ours.items():
|
||||
p = os.path.join(tmp, "ours_" + k + "_dir.vpk")
|
||||
open(p, "wb").write(b)
|
||||
paths["ours_" + k] = p
|
||||
ref = json.loads(ctx.python(["vpk"], READ, input=json.dumps(paths).encode()))
|
||||
cases = []
|
||||
for v in (1, 2):
|
||||
k = "v%d" % v
|
||||
cases.append({"name": "python-" + k, "version": v, "data": open(paths[k], "rb").read(), "entries": ref[k]["entries"]})
|
||||
verdicts = {k: {"ok": ref["ours_" + k]["ok"], "version": ref["ours_" + k]["version"], "entries": ref["ours_" + k]["entries"]} for k in ours}
|
||||
finally:
|
||||
shutil.rmtree(tmp)
|
||||
want = sorted([{"name": n, "size": len(d), "crc": zlib.crc32(d)} for n, d in fs], key=lambda e: e["name"])
|
||||
ctx.emit("vpk", {"cases": cases, "multi": multi(rng), "ours": verdicts, "want": want})
|
||||
Reference in new issue
Block a user