ws-game: docs/guide sections (game archives, game models, game textures) with tested snippets (tools/fixtures/docs_game.py); bench lines (explode, mpq, wad map)
This commit is contained in:
1 parent
3d01293b82
commit
0b1c5ebfec
5 files changed
+267
No files matched your search
@@ -185,6 +185,70 @@ local gzipped = assert(F.save("some text", nil, { format = "gzip", name = "note.
|
||||
assert(F.archive.load(gzipped, { unwrap = false }):list()[1].name == "note.txt")
|
||||
```
|
||||
|
||||
## Game archives
|
||||
|
||||
The game archive formats load as the same `Archive`: Quake / Half-Life PAK, Doom WAD, Build GRP, Valve VPK and
|
||||
Blizzard MPQ (read-only). PAK, WAD, GRP and VPK are writable (and any Archive converts to them).
|
||||
|
||||
```luau
|
||||
local pak = assert(F.archive.load(pakBytes)) -- Quake PAK: 56-byte names, stored files
|
||||
assert(pak.format == "pak" and pak:read("default.cfg"))
|
||||
local grp = assert(F.archive.load(grpBytes)) -- Duke Nukem 3D GRP
|
||||
assert(grp:list()[1].name == "GAME.CON")
|
||||
|
||||
local new = F.format("pak").new()
|
||||
new:add("maps/e1m1.bsp", "...")
|
||||
local bytes = assert(F.save(new)) -- a PAK; F.save(zip, nil, { format = "pak" }) converts
|
||||
assert(F.archive.load(bytes):read("maps/e1m1.bsp"))
|
||||
```
|
||||
|
||||
A WAD's lumps keep their order; lumps of a map are named `"<map>/<lump>"` (`"E1M1/THINGS"`) and repeated names get
|
||||
`"~2"`, `"~3"`, ... so every entry is unique (writing strips both again). The format module turns lumps into game
|
||||
data: graphics (flats, patches, sprites, menu pictures) into Images through the `PLAYPAL` palette, wall textures
|
||||
composed from their patches, and maps into a Scene (z up, map units: floors and ceilings triangulated from the
|
||||
sector outlines, walls, things as nodes standing on their floor).
|
||||
|
||||
```luau
|
||||
local Wad = F.format("wad")
|
||||
local wad = assert(F.archive.load(wadBytes))
|
||||
for _, l in Wad.lumps(wad) do
|
||||
print(l.name, l.size, l.namespace, l.map) -- "FLOOR" 4096 "flats" nil, "THINGS" 20 nil "E1M1", ...
|
||||
end
|
||||
local flat = assert(Wad.image(wad, "FLOOR")) -- 64 x 64 rgba8
|
||||
local patch = assert(Wad.image(wad, "TALL")) -- a picture: meta.offset = { left, top }, alpha 0 = hole
|
||||
assert(flat.width == 64 and patch.height == 300)
|
||||
|
||||
assert(Wad.maps(wad)[1] == "E1M1")
|
||||
local map = assert(Wad.map(wad, "E1M1")) -- a Scene; Wad.mapData(wad, "E1M1") gives the raw tables
|
||||
assert(map.meta.up == "z" and #map.meshes[1].primitives > 0)
|
||||
local thing = map.nodes[2] -- things are child nodes of the map node
|
||||
assert(thing.extras.thing.type == 1) -- player 1 start
|
||||
local data = assert(Wad.mapData(wad, "E1M1"))
|
||||
assert(#data.sectors == 2 and #data.things == 2)
|
||||
```
|
||||
|
||||
VPK directories (`pak01_dir.vpk`) hold small files themselves and point to numbered part files for the rest; pass
|
||||
those through `files` or `resolve` (loading a path does it for you):
|
||||
|
||||
```luau
|
||||
local vpk = assert(F.archive.load(vpkBytes))
|
||||
assert(vpk:read("scripts/game.txt")) -- CRC-32 checked on read
|
||||
-- multi-part: F.archive.load(dir, { name = "pak01_dir.vpk", files = { ["pak01_000.vpk"] = part0 } })
|
||||
local out = assert(F.save(vpk)) -- one self-contained version 2 file (with its MD5s)
|
||||
assert(F.archive.load(out):read("materials/brick/wall.vmt"))
|
||||
```
|
||||
|
||||
MPQ archives are read through their hash table, so names come from the archive's `(listfile)`; files it does not
|
||||
list appear as `File00000012.xxx` (their block index), and `names` adds names to look up:
|
||||
|
||||
```luau
|
||||
local mpq = assert(F.archive.load(mpqBytes)) -- also .w3x / .w3m / .SC2Map
|
||||
local script = assert(mpq:read("war3map.j")) -- zlib / PKWARE implode / bzip2 / LZMA / sparse sectors
|
||||
assert(buffer.len(script) == buffer.len(assert(mpq:read("readme.txt"))))
|
||||
local named = assert(F.archive.load(mpqBytes, { names = { "war3map.w3e" } }))
|
||||
assert(#named:list() >= 2)
|
||||
```
|
||||
|
||||
## Formats
|
||||
|
||||
| Format | Read | Write | Notes |
|
||||
@@ -197,6 +261,11 @@ assert(F.archive.load(gzipped, { unwrap = false }):list()[1].name == "note.txt")
|
||||
| zstd (`.zst`, `.tzst`) | yes | - | single stream; no dictionaries |
|
||||
| bzip2 (`.bz2`, `.tbz2`) | yes | - | single stream |
|
||||
| Brotli (`.br`) | yes | - | single stream; no magic number, so pass a `.br` name or `format = "br"` |
|
||||
| PAK (`.pak`) | yes | yes | Quake / Quake II / Half-Life (and SiN `SPAK`) |
|
||||
| WAD (`.wad`) | yes | yes | IWAD / PWAD; lumps, graphics, wall textures and maps through `F.format("wad")` |
|
||||
| GRP (`.grp`) | yes | yes | Build engine |
|
||||
| VPK (`.vpk`) | yes | yes | versions 1 / 2, multi-part reading, preload bytes; writes one directory file |
|
||||
| MPQ (`.mpq`, `.w3x`, `.sc2map`, ...) | yes | - | versions 1-4 (classic tables), encryption; no Huffman / ADPCM (WAVE) sectors |
|
||||
|
||||
Full capability list: [FORMATS.md](../FORMATS.md).
|
||||
|
||||
|
||||
@@ -194,6 +194,28 @@ Encoder options:
|
||||
| ICO | `cursor`, `png` (PNG entries); `frames` become entries (each up to 256 x 256) |
|
||||
| PNM | the subtype from the extension or `format`: `pbm`, `pgm`, `ppm`, `pam`, `pfm` |
|
||||
|
||||
## Game textures
|
||||
|
||||
PlayStation TIM, Blizzard BLP and Valve VTF load as Images too (rgba8; the mip and texel facts in
|
||||
`meta.source`):
|
||||
|
||||
```luau
|
||||
local face = assert(F.image.load(timBytes)) -- 4 / 8-bit CLUT, 15 or 24-bit
|
||||
assert(face.meta.source.bpp == 16)
|
||||
local tim = assert(F.save(face, nil, { format = "tim", bpp = 16 })) -- TIM writes 4 / 8 / 16 / 24-bit
|
||||
|
||||
local icon = assert(F.image.load(blpBytes)) -- BLP1 JPEG / palette, BLP2 palette / DXT / BGRA
|
||||
assert(icon.width == 4 and icon.meta.source.compression == "bgra")
|
||||
|
||||
local wall = assert(F.image.load(vtfBytes)) -- every common VTF texel format, frames, mips
|
||||
assert(wall.meta.source.texel == "RGBA8888")
|
||||
local small = assert(F.image.load(vtfBytes, { mip = 1 })) -- or face = n (cube maps), slice = n (volumes)
|
||||
assert(F.detect(tim) == "tim")
|
||||
```
|
||||
|
||||
Doom's graphics come from WAD lumps rather than files: `F.format("wad").image(wad, name)` (see
|
||||
[archive.md](archive.md#game-archives)).
|
||||
|
||||
## Formats
|
||||
|
||||
Read and write unless noted; see [FORMATS.md](../FORMATS.md) for depths and details.
|
||||
@@ -207,6 +229,9 @@ Read and write unless noted; see [FORMATS.md](../FORMATS.md) for depths and deta
|
||||
| TIFF | pages | yes | 1..32-bit, float |
|
||||
| BMP, TGA, QOI, ICO / CUR, PNM / PAM / PFM | ICO entries | yes | |
|
||||
| HDR | | no | float (`native` gives rgbaf32) |
|
||||
| TIM | | yes | PlayStation; 4 / 8-bit CLUT, 15 / 24-bit |
|
||||
| BLP | | yes | Blizzard BLP1 / BLP2 (DXT through the BCn codec); writes BLP2 BGRA |
|
||||
| VTF | frames | yes | Valve; read only; mips / faces / slices by option |
|
||||
|
||||
An animated image also opens as a video (`F.video.open(gifBytes)`), and saves to a video format
|
||||
(`F.convert(gifBytes, "avi")`): see [video.md](video.md).
|
||||
|
||||
@@ -378,6 +378,57 @@ local vox = assert(F.save(grid, nil, { format = "vox" })) -- a grid alone saves
|
||||
assert(F.load(vox).voxels[1].size[1] == 4)
|
||||
```
|
||||
|
||||
## Game models
|
||||
|
||||
Game model formats load as Scenes too, with what games need: skins (joints + inverse bind matrices, `JOINTS_0` /
|
||||
`WEIGHTS_0`) and animations, vertex-animated models as morph targets. Units are as the game stores them, z up
|
||||
(`meta.up = "z"`).
|
||||
|
||||
| Format | What loads | Animations |
|
||||
|---|---|---|
|
||||
| MD2 (Quake II) | one primitive; skins as texture URIs | every frame a morph target (weight k = 1 shows frame k); one animation per frame-name group (`run1`..`run6` -> `run`) |
|
||||
| MD3 (Quake III) | a primitive per surface; shaders as URIs; tags as nodes | frames as morph targets, tag motion, one animation |
|
||||
| MDL (Quake 1, Half-Life 1) | Quake: skins decoded through the palette; Half-Life: bones, body parts, textures with their own palettes | Quake: morph targets per frame group; Half-Life: every sequence |
|
||||
| MD5 (Doom 3 `.md5mesh` / `.md5anim`) | skeleton, weighted meshes, shaders as URIs | the `.md5anim` files named in `anims` |
|
||||
| SMD (Valve, read + write) | skeleton, weight links, a primitive per material | the skeleton frames |
|
||||
| BSP (Quake 1 / 2 / 3, Half-Life) | each model a mesh, a primitive per texture (and Quake III lightmap); entities in `meta.entities` | - |
|
||||
|
||||
```luau
|
||||
local tank = assert(F.model.load(md2Bytes))
|
||||
local prim = tank.meshes[1].primitives[1]
|
||||
assert(#prim.targets == 5 and #tank.meshes[1].weights == 5) -- 5 frames
|
||||
assert(tank.animations[1].name == "stand" and tank.animations[2].name == "run")
|
||||
assert(tank.textures[1].uri == "models/tank/skin.pcx")
|
||||
assert(tank.meta.up == "z")
|
||||
```
|
||||
|
||||
MD5 animations are separate files: name them in `anims` (they are found through `files` or `resolve`, which the api
|
||||
fills in for paths), or load a `.md5anim` alone as a skeleton with its animation:
|
||||
|
||||
```luau
|
||||
local imp = assert(F.model.load(md5meshBytes, { anims = { "idle.md5anim" }, files = { ["idle.md5anim"] = md5animBytes } }))
|
||||
assert(#imp.skins == 1 and imp.animations[1].name == "idle")
|
||||
assert(imp.meshes[1].primitives[1].attributes.JOINTS_0)
|
||||
local idle = assert(F.model.load(md5animBytes, { format = "md5" }))
|
||||
assert(#idle.animations == 1)
|
||||
```
|
||||
|
||||
Quake's palette (`gfx/palette.lmp`, inside the game's PAK) is not redistributable, so Quake MDL skins and Quake 1
|
||||
BSP textures need it passed in (`palette`, or through `files` / `resolve`); without it they decode as grey ramps
|
||||
(`meta.source.palette = false`). Half-Life models and maps carry their own palettes.
|
||||
|
||||
```luau
|
||||
local map = assert(F.model.load(bspBytes)) -- a Half-Life map
|
||||
assert(map.meta.source.variant == "halflife")
|
||||
assert(map.meta.entities[1].classname == "worldspawn")
|
||||
local world = map.meshes[1] -- "world"; brush entities are "*1", "*2", ...
|
||||
assert(world.name == "world" and map.meshes[2].name == "*1")
|
||||
|
||||
local barney = assert(F.model.load(smdBytes))
|
||||
local again = assert(F.save(barney, nil, { format = "smd" })) -- SMD writes back (bind pose, weights)
|
||||
assert(F.model.load(again).skins[1])
|
||||
```
|
||||
|
||||
## Formats
|
||||
|
||||
| Format | Read | Write | Notes |
|
||||
@@ -393,6 +444,8 @@ assert(F.load(vox).voxels[1].size[1] == 4)
|
||||
| USDA (+ USDZ read) | yes | yes | |
|
||||
| VOX | yes | yes | voxel grids, scene graph, materials |
|
||||
| STEP | via the CAD plug-in | | see [cad.md](cad.md) |
|
||||
| MD2, MD3, MDL, MD5, BSP | yes | - | game models and maps: see "Game models" above |
|
||||
| SMD | yes | yes | Valve studiomdl data |
|
||||
|
||||
Capabilities per format: [FORMATS.md](../FORMATS.md).
|
||||
|
||||
|
||||
@@ -1102,3 +1102,45 @@ do
|
||||
Bcn.decode("bc6h", b6, 0, w, h)
|
||||
end)
|
||||
end
|
||||
do
|
||||
-- game formats (ws-game): PKWARE DCL explode (StormLib's implode streams from tools/fixtures/mpq.py), reading every
|
||||
-- file of a StormLib MPQ (MB/s of file data), Freedoom's MAP01 to a Scene (tools/fixtures/wad.py: sector
|
||||
-- outlines, triangulation, walls; MB/s of map lumps), an MD2 with every frame as a morph target
|
||||
local Explode = require("@formats/codecs/compress/Explode")
|
||||
local Mpq = require("@formats/archive/mpq")
|
||||
local Wad = require("@formats/archive/wad")
|
||||
local MQ = require("@tests/gen/mpq")
|
||||
local WD = require("@tests/gen/wad")
|
||||
for _, c in MQ.implode do
|
||||
if c.name == "text" or c.name == "ascii-4096" or c.name == "binary-2048" then
|
||||
local data = buffer.fromstring(c.data)
|
||||
bench("explode " .. c.name .. " (" .. c.size .. " B)", 20, c.size, function()
|
||||
Explode.decompress(data)
|
||||
end)
|
||||
end
|
||||
end
|
||||
local arch = MQ.archives[1]
|
||||
local mb = buffer.fromstring(arch.data)
|
||||
local a = assert(Mpq.decode(Bytes.reader(mb)))
|
||||
local total = 0
|
||||
for _, e in a:list() do
|
||||
total += e.size
|
||||
end
|
||||
bench("mpq read every file (v1)", 5, total, function()
|
||||
local m = Mpq.decode(Bytes.reader(mb))
|
||||
for _, e in m:list() do
|
||||
m:read(e.name)
|
||||
end
|
||||
end)
|
||||
local wb = buffer.fromstring(WD.freedoom.data)
|
||||
local w = assert(Wad.decode(Bytes.reader(wb)))
|
||||
local lumps = 0
|
||||
for _, l in Wad.lumps(w) :: any do
|
||||
if l.map == "MAP01" then
|
||||
lumps += l.size
|
||||
end
|
||||
end
|
||||
bench("wad MAP01 -> Scene (no textures)", 3, lumps, function()
|
||||
Wad.map(w, "MAP01", { textures = false })
|
||||
end)
|
||||
end
|
||||
@@ -0,0 +1,78 @@
|
||||
# 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",
|
||||
})
|
||||
Reference in new issue
Block a user