<?php/* * dmmotor — WAD file reader (after w_wad.c of linuxdoom-1.10). * Copyright (C) 1993-1996 by id Software, Inc.; PHP port for dmmotor. GPL v2 (see LICENSE). */namespace App\Dm;/** * A WAD: a directory of named lumps. The IWAD is stored in parts (assets/wad/<name>.0, .1, ...) * because the playground limits the size of one file; they are joined in memory. */final class Wad{ private string $data; /** @var list<array{0:string,1:int,2:int}> [name, offset, size] in directory order */ private array $lumps = []; /** @var array<string,int> last lump of each name (later lumps override earlier ones, like W_GetNumForName) */ private array $byName = []; /** @var array<string,string> */ private array $cache = []; /** IWAD (a whole game) or PWAD (a mod: levels, graphics... over an IWAD) */ public string $type; public function __construct(string $data) { $this->data = $data; $h = strlen($data) >= 12 ? unpack('a4id/Vcount/Voffset', $data) : false; if ($h === false || ($h['id'] !== 'IWAD' && $h['id'] !== 'PWAD')) { throw new \RuntimeException('not a WAD file'); } $this->type = $h['id']; $dir = substr($data, $h['offset'], $h['count'] * 16); for ($i = 0; $i < $h['count']; $i++) { $e = unpack('Vpos/Vsize/a8name', $dir, $i * 16); $name = strtoupper(rtrim($e['name'], "\0")); $this->lumps[] = [$name, $e['pos'], $e['size']]; $this->byName[$name] = $i; } } /** Joins the parts <base>.0, <base>.1... */ public static function fromParts(string $base): self { $data = ''; for ($i = 0; is_file("$base.$i"); $i++) { $data .= file_get_contents("$base.$i"); } if ($data === '' && is_file($base)) { $data = (string) file_get_contents($base); } return new self($data); } /** * W_AddFile of a PWAD: its lumps come after the IWAD's; a lump of the same name replaces the IWAD's * (levels, textures, sounds, music), sprites and flats between S_START / F_START markers join the lists. */ public function merge(Wad $pwad): void { $offset = strlen($this->data); $this->data .= $pwad->data; foreach ($pwad->lumps as [$name, $pos, $size]) { $this->byName[$name] = count($this->lumps); $this->lumps[] = [$name, $pos + $offset, $size]; unset($this->cache[$name]); } } /** The map lumps of this WAD in directory order (E1M1... or MAP01...). @return list<string> */ public function maps(): array { $maps = []; foreach ($this->lumps as $i => [$name]) { if (preg_match('~^(E[1-9]M[1-9]|MAP[0-9][0-9])$~', $name) && ($this->lumps[$i + 1][0] ?? '') === 'THINGS') { $maps[] = $name; } } return $maps; } public function count(): int { return count($this->lumps); } public function check(string $name): int { return $this->byName[strtoupper($name)] ?? -1; } public function has(string $name): bool { return isset($this->byName[strtoupper($name)]); } public function name(int $num): string { return $this->lumps[$num][0]; } public function size(int $num): int { return $this->lumps[$num][2]; } public function lumpNum(int $num): string { [, $pos, $size] = $this->lumps[$num]; return substr($this->data, $pos, $size); } public function lump(string $name): string { $name = strtoupper($name); if (!isset($this->cache[$name])) { $num = $this->byName[$name] ?? throw new \RuntimeException("lump $name not found"); $this->cache[$name] = $this->lumpNum($num); } return $this->cache[$name]; } /** * The lumps between two markers (S_START / S_END, F_START / F_END), also the doubled markers of PWADs. * * @return array<string,int> name => lump number */ public function between(string $start, string $end): array { $out = []; $inside = false; foreach ($this->lumps as $i => [$name, , $size]) { if ($name === $start || $name === $start[0] . $start) { $inside = true; continue; } if ($name === $end || $name === $end[0] . $end) { $inside = false; continue; } if ($inside && $size > 0) { $out[$name] = $i; } } return $out; }}