TR
games/alis / src / Alis / Trace.php · commit 9c722cf
<?phpnamespace App\Alis;/** * Debugging trace of a real game (desktop): on when the file var/trace.on exists. Writes into var/trace/<time>/: *   inputs.jsonl  one line per update: dt, msec, keys held, button, mouse, pad (enough to replay the session); *   hd.log        one line per HD 3D pass and per picture shown: is the HD land over it, and if not, why; *   NNNNN-*.bmp   at each change HD land on <-> off: the picture before and after, the HD land, the land as captured. */final class Trace{    private static mixed $inputs = null;    private static mixed $hd = null;    private static string $dir = '';    private static ?bool $lastShown = null;    private static string $prevBmp = '';    private static int $dumps = 0;    private static int $update = 0;    /** on without var/trace.on (the browser: JumPHP.php("\\App\\Alis\\Trace::$force = true;") before the game starts) */    public static bool $force = false;    /** @var list<string> the last lines of hd.log, for dump() (the browser's console) */    private static array $tail = [];    private static int $lastPanel = -1;    public static function start(string $varDir): void    {        self::stop();        if (!self::$force && !is_file("$varDir/trace.on")) {            return;        }        self::$tail = [];        self::$lastPanel = -1;        self::$dir = "$varDir/trace/" . date('Ymd-His');        @mkdir(self::$dir, 0777, true);        self::$inputs = fopen(self::$dir . '/inputs.jsonl', 'w');        self::$hd = fopen(self::$dir . '/hd.log', 'w');        self::$lastShown = null;        self::$prevBmp = '';        self::$dumps = 0;        self::$update = 0;    }    public static function on(): bool    {        return self::$hd !== null;    }    public static function stop(): void    {        foreach ([self::$inputs, self::$hd] as $f) {            if ($f !== null) {                fclose($f);            }        }        self::$inputs = self::$hd = null;    }    /** @param array<string, mixed> $data */    public static function input(array $data): void    {        if (self::$inputs === null) {            return;        }        self::$update++;        fwrite(self::$inputs, json_encode(['u' => self::$update] + $data) . "\n");    }    public static function line(string $text): void    {        if (self::$hd !== null) {            $line = sprintf('u%06d f%06d t%.2f %s', self::$update, SysVars::$frameCount, SysVars::$msec / 1000, $text);            fwrite(self::$hd, $line . "\n");            fflush(self::$hd);            self::$tail[] = $line;            if (count(self::$tail) > 6000) {                array_splice(self::$tail, 0, 1000);            }        }    }    /** The last $n lines, to the console (error_log: the browser's console), by blocks; $grep keeps the matching ones. */    public static function dump(int $n = 400, string $grep = ''): void    {        $lines = self::$tail;        if ($grep !== '') {            $lines = array_values(array_filter($lines, static fn ($l) => str_contains($l, $grep)));        }        foreach (array_chunk(array_slice($lines, -$n), 20) as $k => $chunk) {            error_log("ALISTRACE $k\n" . implode("\n", $chunk));        }        error_log('ALISTRACE end ' . count($lines));    }    /** An event of the engine (draw, film...), with the state of the interface on both pages. */    public static function event(string $what): void    {        if (self::$hd === null) {            return;        }        self::line(sprintf('ev %s | L=%x:%s P=%x:%s', $what, Img::$logic, self::region(Img::$logic), Img::$physic, self::region(Img::$physic)));    }    /** The interface of a page (the right column under the land, the frame's top border): a short checksum. */    private static function region(int $page): string    {        if ($page <= 0 || $page + 64000 > strlen(Alis::$mem)) {            return '-';        }        $s = '';        for ($y = 126; $y < 200; $y++) {            $s .= substr(Alis::$mem, $page + $y * 320 + 256, 64);        }        $zero = substr_count($s, "\0");        return sprintf('%04x/%d%%', crc32($s) & 0xffff, (int) round($zero * 100 / strlen($s)));    }    /** sys_render(): how much of the interface (the rows below the land) has something drawn, in %, and where the page is. */    public static function picture(string $pixels, int $w, int $h, int $page): void    {        if (self::$hd === null || $w <= 0) {            return;        }        $n = 0;        $all = 0;        for ($y = (int) ($h * 0.7); $y < $h; $y += 2) {            for ($x = 0; $x < $w; $x += 4) {                $n += $pixels[$y * $w + $x] !== "\0" ? 1 : 0;                $all++;            }        }        $panel = (int) round($n * 100 / max(1, $all));        $film = isset(FilmVars::$bfilm) && FilmVars::$bfilm->playing !== 0 ? 1 : 0;        self::line(sprintf('pic panel=%d page=%x switch=%d film=%d glyphs=%d', $panel, $page, Alis::$fswitch, $film, count(SysVars::$hdShown)));        if (self::$lastPanel >= 0 && abs($panel - self::$lastPanel) >= 30) {            self::line(sprintf('PANEL %d -> %d', self::$lastPanel, $panel));        }        self::$lastPanel = $panel;    }    /** sys_render(): the picture of this frame, with or without the HD land. */    public static function shown(bool $hdOn, string $why, int $differ, ?array $land): void    {        if (self::$hd === null) {            return;        }        $rect = $land !== null ? sprintf('%d,%d %dx%d', $land['x'], $land['y'], $land['w'], $land['h']) : '-';        self::line(sprintf('show hd=%d why=%s differ=%d land=%s', $hdOn ? 1 : 0, $why, $differ, $rect));        if (self::$lastShown !== null && self::$lastShown !== $hdOn && self::$dumps < 400) {            self::$dumps++;            $n = sprintf('%s/%06d-%s', self::$dir, SysVars::$frameCount, $hdOn ? 'on' : 'off');            file_put_contents("$n-before.bmp", self::$prevBmp);            file_put_contents("$n-after.bmp", SysVars::$frameBmp);            if (SysVars::$hdLand !== null) {                file_put_contents("$n-hdland.bmp", SysVars::$hdLand['bmp']);            }            if ($land !== null) {                file_put_contents("$n-low.raw", $land['low']);                file_put_contents("$n-low.json", json_encode(array_diff_key($land, ['low' => 1, 'hd' => 1])));            }        }        self::$lastShown = $hdOn;        self::$prevBmp = SysVars::$frameBmp;    }}