Files
rosetta/tools/fixtures/docs_game.py
T

79 lines
4.3 KiB
Python

# Docs inputs for the game formats sections of the archive / scene / image pages (tools/fixtures.py;
# tools/docs_check.py explains the inputs), made with the writers of the format fixtures: a Quake PAK
# (tools/fixtures/pak.py), a PWAD with a room map, a flat and a tall patch (wad.py), a Build GRP, a VPK (Python's vpk
# package, through vpk.py), an MPQ (StormLib, through mpq.py), an MD2 (md2.py), an .md5mesh + .md5anim (md5.py), an
# SMD (smd.py), a Half-Life BSP (bsp.py), a PlayStation TIM (tim.py), a BLP2 (blp.py) and a VTF 7.2.
# tests/gen/docs_game.luau { files = { [name] = bytes }, vars = { pakBytes = "pak0.pak", ... } }
import importlib.util, json, os, struct, tempfile, shutil
INPUTS = ["tools/docs_check.py"]
def mod(ctx, name):
spec = importlib.util.spec_from_file_location("fx_" + name, os.path.join(ctx.root, "tools", "fixtures", name + ".py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
def vtf72(w, h, rgba):
hd = bytearray(80)
struct.pack_into("<4sIIIHHIHH", hd, 0, b"VTF\0", 7, 2, 80, w, h, 0, 1, 0)
struct.pack_into("<f", hd, 48, 1.0)
struct.pack_into("<iBiBBH", hd, 52, 0, 1, -1, 0, 0, 1)
return bytes(hd) + rgba
def generate(ctx):
rng = ctx.rng(3)
spec = importlib.util.spec_from_file_location("docs_check", os.path.join(ctx.root, "tools", "docs_check.py"))
check = importlib.util.module_from_spec(spec)
spec.loader.exec_module(check)
files = {}
files["pak0.pak"] = mod(ctx, "pak").write([("maps/start.bsp", rng.randbytes(64)), ("gfx/palette.lmp", bytes(range(256)) * 3),
("progs/player.mdl", rng.randbytes(32)), ("default.cfg", b"bind w +forward\n")])
files["room.wad"], _ = mod(ctx, "wad").room()
files["duke.grp"] = mod(ctx, "grp").write([("GAME.CON", b"define X 1\n"), ("E1L1.MAP", rng.randbytes(40))])
# VPK: the vpk package
vpk = mod(ctx, "vpk")
tmp = tempfile.mkdtemp(prefix="formats-docs-game-")
try:
src = os.path.join(tmp, "src")
for name, data in (("materials/brick/wall.vmt", b"LightmappedGeneric { }"), ("scripts/game.txt", b"\"game\" { }\n")):
p = os.path.join(src, name)
os.makedirs(os.path.dirname(p), exist_ok=True)
open(p, "wb").write(data)
out = os.path.join(tmp, "pak01_dir.vpk")
ctx.python(["vpk"], vpk.WRITE, input=json.dumps({"src": src, "version": 2, "out": out}).encode())
files["pak01_dir.vpk"] = open(out, "rb").read()
# MPQ: StormLib
mpq = mod(ctx, "mpq")
txt = os.path.join(tmp, "readme.txt")
open(txt, "wb").write(b"Warcraft III map notes\n" * 20)
arch = {"name": "map", "path": os.path.join(tmp, "map.w3x"), "create": 0x00100000,
"files": [{"name": "war3map.j", "src": txt, "flags": mpq.COMPRESS | mpq.REPLACE, "comp": mpq.ZLIB},
{"name": "readme.txt", "src": txt, "flags": mpq.IMPLODE | mpq.REPLACE, "comp": mpq.PKWARE}]}
ctx.python([], mpq.CODE, input=json.dumps({"lib": mpq.lib_path(), "archives": [arch], "implode": [], "modes": []}).encode())
files["map.w3x"] = open(arch["path"], "rb").read()
finally:
shutil.rmtree(tmp)
files["tank.md2"], _, _ = mod(ctx, "md2").md2(rng, 12, 12, 10, ["stand1", "stand2", "run1", "run2", "run3"], ["models/tank/skin.pcx"])
md5 = mod(ctx, "md5")
text, info = md5.mesh(rng)
files["imp.md5mesh"] = text.encode()
atext, _ = md5.anim(rng, info["joints"], 5, 24, "idle")
files["idle.md5anim"] = atext.encode()
stext, _ = mod(ctx, "smd").smd(rng, 3, 4, 6, ["skin.bmp"], True, False)
files["barney.smd"] = stext.encode()
files["crossfire.bsp"] = mod(ctx, "bsp").brush_map(rng, "hl", bytes(768))["data"]
tdata, _, _, _ = mod(ctx, "tim").tim(rng, 16, 8, 4)
files["face.tim"] = tdata
files["icon.blp"] = mod(ctx, "blp").blp2(1, 3, 8, 8, 4, 4, [rng.randbytes(64)])
files["wall.vtf"] = vtf72(4, 4, rng.randbytes(64))
check.emit_inputs(ctx, files, {
"pakBytes": "pak0.pak", "wadBytes": "room.wad", "grpBytes": "duke.grp", "vpkBytes": "pak01_dir.vpk",
"mpqBytes": "map.w3x", "md2Bytes": "tank.md2", "md5meshBytes": "imp.md5mesh", "md5animBytes": "idle.md5anim",
"smdBytes": "barney.smd", "bspBytes": "crossfire.bsp", "timBytes": "face.tim", "blpBytes": "icon.blp",
"vtfBytes": "wall.vtf",
})