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:
4DBug committed 2026-10-31 15:30:18 -05:00
1 parent 3d01293b82
commit 0b1c5ebfec
5 files changed
+267

No files matched your search

+69
View File
@@ -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).