# 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/Rosetta/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("