<?php/* * dmmotor: generates src/Dm/Info.php from id Software's GPL source (linuxdoom-1.10: * info.h, info.c, sounds.h, p_mobj.h). The tables (frames, things) are translated as they are. * * git clone https://github.com/id-Software/DOOM.git /tmp/idsrc * php tools/gen-info.php /tmp/idsrc/linuxdoom-1.10 * * Copyright (C) 1993-1996 by id Software, Inc. (GPL v2, see LICENSE). */$dir = rtrim($argv[1] ?? '', '/');if (!is_file("$dir/info.c")) { fwrite(STDERR, "usage: php tools/gen-info.php <linuxdoom-1.10>\n"); exit(1);}$infoH = file_get_contents("$dir/info.h");$infoC = file_get_contents("$dir/info.c");$soundsH = file_get_contents("$dir/sounds.h");$mobjH = file_get_contents("$dir/p_mobj.h");/** The names of a C enum that starts after $after, up to $last. */function enumNames(string $src, string $first, string $last): array{ $start = strpos($src, $first); $end = strpos($src, $last, $start); $body = substr($src, $start, $end - $start); $body = preg_replace('~//[^\n]*~', '', $body); $names = []; foreach (explode(',', $body) as $part) { $part = trim($part); if ($part !== '') { $names[] = $part; } } return $names;}$sprites = enumNames($infoH, 'SPR_TROO', 'NUMSPRITES');$states = enumNames($infoH, 'S_NULL', 'NUMSTATES');$types = enumNames($infoH, 'MT_PLAYER', 'NUMMOBJTYPES');$sfx = enumNames($soundsH, 'sfx_None', 'NUMSFX');$sprIndex = array_flip($sprites);$stIndex = array_flip($states);$sfxIndex = array_flip($sfx);preg_match_all('~(MF_\w+)\s*=\s*(0x[0-9a-fA-F]+|\d+)~', $mobjH, $m, PREG_SET_ORDER);$flags = [];foreach ($m as [, $name, $value]) { $flags[$name] = intval($value, 0);}// states: {SPR_TROO,0,-1,{NULL},S_NULL,0,0},preg_match_all('~\{(SPR_\w+),(\d+),(-?\d+),\{(\w+)\},(S_\w+),(-?\d+),(-?\d+)\}~', $infoC, $m, PREG_SET_ORDER);$stateRows = [];foreach ($m as [, $spr, $frame, $tics, $action, $next]) { $stateRows[] = [$sprIndex[$spr], (int) $frame, (int) $tics, $action === 'NULL' ? null : $action, $stIndex[$next]];}if (count($stateRows) !== count($states)) { fwrite(STDERR, 'states: ' . count($stateRows) . ' rows for ' . count($states) . " names\n"); exit(1);}$fields = ['doomednum', 'spawnstate', 'spawnhealth', 'seestate', 'seesound', 'reactiontime', 'attacksound', 'painstate', 'painchance', 'painsound', 'meleestate', 'missilestate', 'deathstate', 'xdeathstate', 'deathsound', 'speed', 'radius', 'height', 'mass', 'damage', 'activesound', 'flags', 'raisestate'];$infoStart = strpos($infoC, 'mobjinfo_t mobjinfo');$blocks = [];preg_match_all('~\{\s*//\s*(MT_\w+)(.*?)\n\s*\}~s', substr($infoC, $infoStart), $m, PREG_SET_ORDER);foreach ($m as [, $type, $body]) { $values = []; foreach (explode("\n", $body) as $line) { $line = trim(preg_replace('~//.*~', '', $line)); $line = rtrim($line, ','); if ($line === '') { continue; } $values[] = $line; } if (count($values) !== count($fields)) { fwrite(STDERR, "$type: " . count($values) . " values\n"); exit(1); } $row = []; foreach ($fields as $i => $field) { $v = $values[$i]; if (str_starts_with($v, 'S_')) { $v = $stIndex[$v]; } elseif (str_starts_with($v, 'sfx_')) { $v = $sfxIndex[$v]; } elseif (str_contains($v, 'MF_')) { $n = 0; foreach (explode('|', $v) as $f) { $n |= $flags[trim($f)]; } $v = $n; } elseif (str_contains($v, 'FRACUNIT')) { $v = (float) eval('return ' . str_replace('FRACUNIT', '1', $v) . ';'); } else { $v = (int) $v; } $row[$field] = $v; } $blocks[] = $row;}if (count($blocks) !== count($types)) { fwrite(STDERR, 'mobjinfo: ' . count($blocks) . ' rows for ' . count($types) . " types\n"); exit(1);}$out = "<?php\n\n/*\n * dmmotor — GENERATED by tools/gen-info.php from id Software's linuxdoom-1.10 (info.c, info.h, sounds.h,\n" . " * p_mobj.h). Do not edit by hand.\n *\n * Copyright (C) 1993-1996 by id Software, Inc.\n" . " * Released under the GNU General Public License v2 (see LICENSE). Translated to PHP for dmmotor.\n */\n\n" . "namespace App\\Dm;\n\nfinal class Info\n{\n";foreach ($flags as $name => $value) { if ($name !== 'MF_TRANSSHIFT') { $out .= " public const $name = $value;\n"; }}$out .= "\n";foreach ($types as $i => $name) { $out .= " public const $name = $i;\n";}$out .= "\n";foreach ($states as $i => $name) { $out .= " public const $name = $i;\n";}$out .= "\n /** Sprite names (4 letters), by SPR_ number. */\n public const SPRITES = " . var_export(array_map(fn ($s) => substr($s, 4), $sprites), true) . ";\n\n";$out .= " /** Sound names (lump DS<name>), by sfx number. */\n public const SOUNDS = " . var_export(array_map(fn ($s) => strtolower(substr($s, 4)), $sfx), true) . ";\n\n";$out .= " /** [sprite, frame (|32768 = full bright), tics, action, next state], by S_ number. */\n public const STATES = [\n";foreach ($stateRows as $i => [$spr, $frame, $tics, $action, $next]) { $out .= sprintf(" [%d, %d, %d, %s, %d], // %s\n", $spr, $frame, $tics, $action === null ? 'null' : "'$action'", $next, $states[$i]);}$out .= " ];\n\n /** The things' properties, by MT_ number (speed of missiles, radius, height in map units). */\n public const MOBJINFO = [\n";foreach ($blocks as $i => $row) { $parts = []; foreach ($row as $k => $v) { $parts[] = "'$k' => " . var_export($v, true); } $out .= ' [' . implode(', ', $parts) . "], // {$types[$i]}\n";}$out .= " ];\n}\n";file_put_contents(__DIR__ . '/../src/Dm/Info.php', $out);echo count($stateRows), ' states, ', count($blocks), ' things, ', count($sfx), " sounds\n";