FR
games/alis / src / Alis / GameLibrary.php · commit 9c722cf
<?phpnamespace App\Alis;/** * The ALIS games the engine can start: one folder per game in data/ (the original data files: main script * main.*, man.sng, prog.oo or prog.io), described by catalog/<game>.json (title, files loaded on demand). */final class GameLibrary{    /** the editions made from the sources => what their name gets */    public const EDITIONS = ['source' => ' - ALIS sources', 'hd' => ' HD', 'php' => ' - PHP code', 'phphd' => ' - PHP code HD'];    /**     * One entry per game, plus its other editions when it has the ALIS sources of its scripts (catalog "sources",     * or data/<game>/src/main.als): "source" (the scripts compiled from src/*.als, docs/language.md), "hd", "php" (the scripts run as PHP, not by the VM:     * src/Alis/Compiled/) and "phphd" (the same with the HD edition's picture).     * @return list<array{id: string, dir: string, label: string, main: string, lazy: array, edition: string}>     */    public static function scan(string $dataRoot, string $catalogRoot): array    {        $games = [];        foreach (@scandir($dataRoot) ?: [] as $entry) {            if ($entry[0] === '.' || !is_dir("$dataRoot/$entry")) {                continue;            }            $main = self::mainScript("$dataRoot/$entry");            if ($main === null) {                continue;            }            $catalog = self::catalog($catalogRoot, $entry);            $game = [                'id' => $entry,                'dir' => "$dataRoot/$entry",                'label' => $catalog['title'] ?? str_replace(['-', '_'], ' ', $entry),                'main' => $main,                'lazy' => $catalog['lazy'] ?? [],                'edition' => 'original',            ];            $games[] = $game;            if (($catalog['sources'] ?? false) || is_file("$dataRoot/$entry/src/main.als")) {                foreach (self::EDITIONS as $edition => $suffix) {                    $games[] = ['edition' => $edition, 'label' => $game['label'] . $suffix] + $game;                }            }        }        usort($games, fn ($a, $b) => strcmp($a['label'], $b['label']));        return $games;    }    public static function catalog(string $catalogRoot, string $id): array    {        $file = "$catalogRoot/$id.json";        return is_file($file) ? (json_decode((string) file_get_contents($file), true) ?: []) : [];    }    public static function mainScript(string $dir): ?string    {        foreach (@scandir($dir) ?: [] as $file) {            $lower = strtolower($file);            if (str_starts_with($lower, 'main.') || in_array($lower, ['man.sng', 'prog.oo', 'prog.io'], true)) {                return $file;            }        }        return null;    }}