RU
games/alis / tools / compiled / readable.php · commit 9c722cf
<?php// The game's scripts as readable PHP: one class per script (<out>/<name>.php), each instruction a `case` commented// with its ALIS source line, the named variables (<data>/names.json) as constants of the class. The game runs these// files instead of generating them (tools/run.php --compiled-from=<out>, or a php/ folder in the game's data):// edit them, then check that the game is the same with tools/compiled/check.sh, or change it on purpose.// They are made from the game's data: keep them with the data (private), never in the repository.//   bin/jumphp php games/alis tools/compiled/readable.php ../../.alis-data/robinson ../../.alis-data/robinson/phpuse App\Alis\Compiled\Compiler;use App\Alis\Lang\Script;use App\Alis\Machine;use App\Alis\UnpackVars;require __DIR__ . '/../../vendor/autoload.php';$base = __DIR__ . '/../../';$dir = rtrim($argv[1] ?? 'data/robinson', '/');$out = rtrim($argv[2] ?? "$dir/php", '/');$dir = str_starts_with($dir, '/') ? $dir : $base . $dir;$out = str_starts_with($out, '/') ? $out : $base . $out;@mkdir($out, 0777, true);$m = new Machine();if (!$m->boot($dir)) {    fwrite(STDERR, "boot failed: {$m->error}\n");    exit(1);}// the names: <data>/names.json, or every <data>/names/*.json merged (names/main.json last: it decides the main// script's globals); keys = script name (or "main") => offset => name, "_notes" ignored$files = isset($argv[3]) ? [$argv[3]] : (is_file("$dir/names.json") ? ["$dir/names.json"] : glob("$dir/names/*.json"));usort($files, static fn ($a, $b) => (basename($a) === 'main.json') <=> (basename($b) === 'main.json') ?: strcmp($a, $b));foreach ($files as $f) {    foreach (json_decode((string) file_get_contents($f), true) ?: [] as $script => $map) {        if ($script[0] !== '_' && is_array($map)) {            Compiler::$names[$script] = $map + (Compiler::$names[$script] ?? []);        }    }}$n = $native = $vm = $named = 0;foreach (glob("$dir/*.[iI][oO]") as $path) {    $raw = file_get_contents($path);    $magic = unpack('V', $raw)[1];    $isMain = unpack('v', substr($raw, 4, 2))[1] === 0;    $pack = ($magic >> 24) & 0xff;    $name = strtolower(basename($path, '.' . pathinfo($path, PATHINFO_EXTENSION)));    if ($pack & 0x80) {        $size = ($magic & 0xffffff) - UnpackVars::$kPackedHeaderSize - ($isMain ? UnpackVars::$kVMSpecsSize : 0);        \App\Alis\Alis::$typepack = \App\Alis\s8($pack & 0xfe);        $buf = \App\Alis\script_scratch(1024 + $size);        $bytes = \App\Alis\mem_read($buf, \App\Alis\unpack_script($path, $buf));    } else {        $bytes = substr($raw, 6 + ($isMain ? 16 : 0));    }    if (!Script::isScript($bytes)) {        continue;    }    $php = Compiler::source($name, $bytes, $class, true);    file_put_contents("$out/" . preg_replace('/\W+/', '_', $name) . '.php', "<?php\n\n" . $php);    $n++;    preg_match('/\$native = (\d+);/', $php, $a);    preg_match('/\$interpreted = (\d+);/', $php, $b);    $native += (int) ($a[1] ?? 0);    $vm += (int) ($b[1] ?? 0);    $named += preg_match_all('/public const /', $php);}printf("%d scripts in PHP in %s: %d instructions in PHP, %d run by the VM, %d named variables\n", $n, $out, $native, $vm, $named);