Canvas-only 2D game editor/runtime built on PixiJS v8 + Vite + TypeScript. - core: asset model (Scene/Camera/Sprite), scene objects with sparse overrides, property schema + resolve, command stack (undo/redo), serialization and project store interface (16 unit tests) - ui: custom in-canvas widget toolkit (panels, fields, color picker, list/scroll, popup) with a hidden <input> keyboard bridge - editor: asset/hierarchy panels, schema-driven inspector, toolbar - viewport: box rendering, camera frame, selection gizmo, drag/zoom/pan - runtime: world/entity instantiation for play mode, input, camera follow - api: PHP JSON project store with revision counter (409 on conflict) - tools: CDP-based e2e smoke test Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
166 lines
5.5 KiB
PHP
166 lines
5.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/** Thrown by Store::save when the client's revision is older than the stored one. */
|
|
final class StaleRevisionException extends RuntimeException
|
|
{
|
|
public function __construct(public readonly int $currentRevision)
|
|
{
|
|
parent::__construct("Project was modified elsewhere (revision {$currentRevision})");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* File-based project store: one JSON document per project in `data/projects/{id}.json`.
|
|
* Each file wraps the project with metadata: { id, name, revision, updatedAt, project }.
|
|
* Writes are atomic (temp file + rename) and guarded by an integer revision so that a
|
|
* stale client (second tab, old session) cannot silently overwrite newer work.
|
|
*/
|
|
final class Store
|
|
{
|
|
public function __construct(private readonly string $dir)
|
|
{
|
|
if (!is_dir($this->dir) && !mkdir($this->dir, 0775, true) && !is_dir($this->dir)) {
|
|
throw new RuntimeException("Cannot create data directory {$this->dir}");
|
|
}
|
|
}
|
|
|
|
public static function isValidId(string $id): bool
|
|
{
|
|
return (bool) preg_match('/^[a-z0-9]{6,32}$/', $id);
|
|
}
|
|
|
|
public static function newId(): string
|
|
{
|
|
return substr(bin2hex(random_bytes(8)), 0, 12);
|
|
}
|
|
|
|
/** @return list<array{id:string,name:string,revision:int,updatedAt:string}> */
|
|
public function list(): array
|
|
{
|
|
$items = [];
|
|
foreach (glob($this->dir . '/*.json') ?: [] as $file) {
|
|
$doc = $this->readFile($file);
|
|
if ($doc === null) {
|
|
continue;
|
|
}
|
|
$items[] = [
|
|
'id' => $doc['id'],
|
|
'name' => $doc['name'],
|
|
'revision' => $doc['revision'],
|
|
'updatedAt' => $doc['updatedAt'],
|
|
];
|
|
}
|
|
usort($items, static fn(array $a, array $b): int => strcmp($b['updatedAt'], $a['updatedAt']));
|
|
return $items;
|
|
}
|
|
|
|
/** @return array{id:string,name:string,revision:int,updatedAt:string,project:array}|null */
|
|
public function load(string $id): ?array
|
|
{
|
|
return $this->readFile($this->path($id));
|
|
}
|
|
|
|
/** @return array{id:string,name:string,revision:int,updatedAt:string,project:array} */
|
|
public function create(string $name): array
|
|
{
|
|
do {
|
|
$id = self::newId();
|
|
} while (file_exists($this->path($id)));
|
|
|
|
$project = [
|
|
'formatVersion' => 1,
|
|
'id' => $id,
|
|
'name' => $name,
|
|
'assets' => [],
|
|
'activeSceneId' => '',
|
|
];
|
|
$doc = $this->document($id, $name, 1, $project);
|
|
$this->writeFile($this->path($id), $doc);
|
|
return $doc;
|
|
}
|
|
|
|
/**
|
|
* Saves a project when `$expectedRevision` matches the stored one.
|
|
* Returns the new document, null when the project does not exist, or throws
|
|
* StaleRevisionException (carrying the current revision) on a conflict.
|
|
*/
|
|
public function save(string $id, int $expectedRevision, array $project): ?array
|
|
{
|
|
$path = $this->path($id);
|
|
$lock = fopen($path . '.lock', 'c');
|
|
if ($lock === false || !flock($lock, LOCK_EX)) {
|
|
throw new RuntimeException('Cannot acquire project lock');
|
|
}
|
|
try {
|
|
$current = $this->readFile($path);
|
|
if ($current === null) {
|
|
return null;
|
|
}
|
|
if ($current['revision'] !== $expectedRevision) {
|
|
throw new StaleRevisionException($current['revision']);
|
|
}
|
|
$name = is_string($project['name'] ?? null) && $project['name'] !== '' ? $project['name'] : $current['name'];
|
|
$project['id'] = $id;
|
|
$doc = $this->document($id, $name, $expectedRevision + 1, $project);
|
|
$this->writeFile($path, $doc);
|
|
return $doc;
|
|
} finally {
|
|
flock($lock, LOCK_UN);
|
|
fclose($lock);
|
|
@unlink($path . '.lock');
|
|
}
|
|
}
|
|
|
|
public function delete(string $id): bool
|
|
{
|
|
$path = $this->path($id);
|
|
return file_exists($path) && unlink($path);
|
|
}
|
|
|
|
private function path(string $id): string
|
|
{
|
|
return $this->dir . '/' . $id . '.json';
|
|
}
|
|
|
|
private function document(string $id, string $name, int $revision, array $project): array
|
|
{
|
|
return [
|
|
'id' => $id,
|
|
'name' => $name,
|
|
'revision' => $revision,
|
|
'updatedAt' => gmdate('c'),
|
|
'project' => $project,
|
|
];
|
|
}
|
|
|
|
private function readFile(string $path): ?array
|
|
{
|
|
if (!is_file($path)) {
|
|
return null;
|
|
}
|
|
$raw = file_get_contents($path);
|
|
$doc = $raw === false ? null : json_decode($raw, true);
|
|
if (!is_array($doc) || !isset($doc['id'], $doc['revision'], $doc['project'])) {
|
|
return null;
|
|
}
|
|
$doc['name'] = is_string($doc['name'] ?? null) ? $doc['name'] : 'Untitled';
|
|
$doc['updatedAt'] = is_string($doc['updatedAt'] ?? null) ? $doc['updatedAt'] : '';
|
|
$doc['revision'] = (int) $doc['revision'];
|
|
return $doc;
|
|
}
|
|
|
|
private function writeFile(string $path, array $doc): void
|
|
{
|
|
$json = json_encode($doc, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
|
if ($json === false) {
|
|
throw new RuntimeException('Cannot encode project');
|
|
}
|
|
$tmp = $path . '.' . bin2hex(random_bytes(4)) . '.tmp';
|
|
if (file_put_contents($tmp, $json, LOCK_EX) === false || !rename($tmp, $path)) {
|
|
@unlink($tmp);
|
|
throw new RuntimeException('Cannot write project file');
|
|
}
|
|
}
|
|
}
|