<?phpnamespace App\Editor;use App\Alis\Lang\Human;use App\Alis\Lang\CompileError;use App\Alis\SysVars;use JumPHP\Save\Storage;// the VM's functions (sys_find_file()...): the playground does not run the game's composer "files"require_once dirname(__DIR__) . '/Alis/fn/_load.php';/** * The ALIS sources of a game for the editor (docs/editor.md): the original ones (data/<game>/src/*.als, downloaded * on demand in the browser like the films) and the player's changes, kept in the player's files (var/, IndexedDB * in the browser): "alis-src-<game>-<script>.als". The source edition of the game compiles the changed one * instead of the original (sys_source_io()). */final class SourceStore{ public function __construct(private Storage $storage, private string $game, private string $srcDir) { } /** The scripts of the game (names without .als), from the folder or the catalog's list. @return list<string> */ public function scripts(): array { $names = []; foreach (@scandir($this->srcDir) ?: [] as $f) { if (str_ends_with(strtolower($f), '.als')) { $names[strtolower(substr($f, 0, -4))] = true; } } foreach (SysVars::$lazy as $f => $_) { if (str_ends_with($f, '.als')) { $names[substr($f, 0, -4)] = true; } } $names = array_keys($names); sort($names); // the main script first usort($names, static fn (string $a, string $b) => [$a !== 'main', $a] <=> [$b !== 'main', $b]); return $names; } public static function key(string $game, string $script): string { return 'alis-src-' . preg_replace('/[^a-z0-9_-]/', '_', strtolower($game)) . '-' . preg_replace('/[^a-z0-9_-]/', '_', strtolower($script)) . '.als'; } public function isChanged(string $script): bool { return $this->storage->exists(self::key($this->game, $script)); } /** The player's version, else the original; null while it is being downloaded (ask again next frame). */ public function load(string $script): ?string { $mine = $this->storage->read(self::key($this->game, $script)); return $mine ?? $this->original($script); } /** The original source; null while it is being downloaded. */ public function original(string $script): ?string { $file = \App\Alis\sys_find_file($this->srcDir . '/' . $script . '.als'); return $file === null ? null : (string) file_get_contents($file); } /** Asks for the resources file a source names (a download in the browser): true when it is here. */ public function resources(string $source): bool { if (!preg_match('/^\s*resources\s+"([^"]+)"/m', $source, $m)) { return true; } return \App\Alis\sys_find_file($this->srcDir . '/' . $m[1]) !== null; } /** True when the original cannot come (no such file, the download failed). */ public function failed(string $script): bool { $name = $script . '.als'; return isset(SysVars::$lazyFailed[$name]) || (!isset(SysVars::$lazy[$name]) && !is_file($this->srcDir . '/' . $name)); } /** * Compiles a source: null if it is right, else the error [line, message]. * @return array{0: int, 1: string}|null */ public function check(string $source): ?array { try { Human::compile($source, function (string $file): string { $f = \App\Alis\sys_find_file($this->srcDir . '/' . $file); if ($f === null) { throw new CompileError("missing $file (still downloading?)"); } return (string) file_get_contents($f); }); } catch (CompileError $e) { return [$e->sourceLine, $e->getMessage()]; } return null; } /** Saves the player's version (the same text as the original: the change is dropped). */ public function save(string $script, string $source): void { $canonical = \App\Alis\Lang\Forms::canonical($source); if ($canonical === $this->original($script)) { $this->revert($script); return; } $this->storage->write(self::key($this->game, $script), $source); } /** Back to the original. */ public function revert(string $script): void { $this->storage->delete(self::key($this->game, $script)); }}