Files
feuerwerk/src/editor/assetImport.ts
T
adminandClaude Opus 5.5 e191dc4b2e
game-maker / gate (push) Successful in 4m14s
game-maker / suite (push) Successful in 17m7s
Record the game view from a behavior into a video asset (#405)
A game can now film itself. A behavior starts and stops a take, and
the editor turns it into a video asset with the run's picture and the
sound the player hears. The exported game hands the file over as a
download, since there is no project to keep it in.

- New action nodes `Start recording` (Name, Resolution, FPS) and
  `Stop recording`. An empty name becomes `Recording <timestamp>`.
- Resolution is `Game view`, `720p` or `1080p`. For the fixed sizes
  the height is set and the width follows the camera.
- The picture is the camera only, without hints, overlays or the
  letterbox. It is drawn into a render texture of the target size,
  so a smaller window still gives full 1080p.
- `PictureReader` reads the texture back through pixel buffers with
  a fence. The editor never waits on the GPU, and a frame with all
  three buffers busy is dropped and counted.
- Nothing of the take reaches the visible canvas. A first version
  drew tiles into it and copied them out, and with the low-latency
  canvas that midway state flickered on screen.
- The sound is tapped behind the master limiter in the FX worklet
  and encoded as Opus.
- H.264 is chosen when the browser has it, VP9 otherwise. A small
  own Matroska writer packs both tracks, no new dependency. It names
  the frame rate and BT.709 so ffmpeg keeps both.
- A second `Start recording` is ignored with a warning in the log.
  A take goes on through `Load scene` and a restart, pauses with the
  editor and is saved when the run ends. It stops at 2 GB.
- The editor uploads the take on the existing video route and makes
  the asset once the run is over. The video tab gets a Download
  button for the converted MP4.
- `downloadBlob` in `src/ui` replaces the song editor's own copy.

Findings:
- `On start` runs before the game screen knows the world. A take
  therefore fixes its size on the first frame that has a camera.
- Without a frame rate in the file, ffmpeg turned 30 fps into 59.94.
- The worker's poster JPEG is off in colour for every upload (red
  255 comes out as 229, BT.601). Not touched here.

Tests:
- Unit recording + matroska: 21 green, plus the node catalog cases.
  The Matroska test reads the file back with its own EBML reader.
- e2e 67 (new) on melektaus-atx: ALL PASSED. 1080p30 from a
  1280x800 window gives a 1920x1080 MP4 with sound and no dropped
  frame. Every 4th pixel is checked, a green quarter catches an
  upside-down picture. A take ended by Esc is saved with the
  timestamp name.
- e2e 60j: the exported game downloads an `.mkv` with EBML magic.
- The flicker is gone on the user's machine without `?vsync`.
- Gate on melektaus-atx: CHECK EXIT 0, 299 files, 4586 tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 21:11:05 +02:00

179 lines
7.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Pictures, typefaces and sounds all arrive the same way: a file goes up to the server, comes back
* as a content-addressed name, and an asset is made for it – or the asset already naming those
* bytes is used. What differs between them is an `ImportKind`; the pipeline is this file.
*/
import type { Command } from '../core/commands/Command';
import { nextAssetName } from '../core/commands/naming';
import type { AssetType } from '../core/model/AssetTypes';
import type { FileStore, UploadedFile } from '../core/store/FileStore';
import type { EditorState } from './state';
/** One file's outcome; exactly one of `assetId` and `error` is set. */
export interface ImportOutcome {
name: string;
assetId: string | null;
error: string | null;
/** The project already had this exact file, so its existing asset was used instead. */
reused?: boolean;
}
export interface ImportDeps {
state: EditorState;
}
export interface ImportKind<U extends UploadedFile, D extends ImportDeps> {
type: AssetType;
/** What the notices call one: "image", "font", "sound". */
noun: string;
/** What an asset is called when the file name gives nothing to go on. */
fallbackName: string;
store(deps: D): FileStore<U>;
/** The bytes to send, and their Content-Type; throws with the reason when the file cannot go up. */
prepare(file: File): Promise<{ blob: Blob; type: string }>;
create(name: string, uploaded: U): Command & { asset: { id: string } };
}
/**
* Imports the files sequentially, so each asset is named against the project as the previous one
* left it. A file that fails is reported in the notice; the remaining ones are still imported.
*/
export async function importFiles<U extends UploadedFile, D extends ImportDeps>(
kind: ImportKind<U, D>,
files: readonly File[],
deps: D,
): Promise<ImportOutcome[]> {
if (files.length === 0) return [];
const outcomes: ImportOutcome[] = [];
for (const file of files) outcomes.push(await importFile(kind, file, deps));
// The asset the batch created last, so the panel shows what just arrived.
const created = outcomes.filter((outcome) => outcome.assetId !== null);
const last = created[created.length - 1];
if (last?.assetId) deps.state.select({ kind: 'asset', assetId: last.assetId });
deps.state.setNotice(importNotice(outcomes, kind.noun));
return outcomes;
}
async function importFile<U extends UploadedFile, D extends ImportDeps>(
kind: ImportKind<U, D>,
file: File,
deps: D,
): Promise<ImportOutcome> {
try {
const { blob, type } = await kind.prepare(file);
const uploaded = await kind.store(deps).upload(blob, type, file.name);
return adopt(kind, uploaded, assetNameForFile(file.name, kind.fallbackName), file.name, deps);
} catch (error) {
return { name: file.name, assetId: null, error: error instanceof Error ? error.message : String(error) };
}
}
/**
* Makes an asset for a file the server holds. `label` names the source in messages, `nameBase` is
* what the asset is called. The server stores by content hash, so a re-imported file comes back
* as the one the project already references; two assets on one file would disagree about it.
*/
export function adopt<U extends UploadedFile, D extends ImportDeps>(
kind: ImportKind<U, D>,
uploaded: U,
nameBase: string,
label: string,
deps: D,
): ImportOutcome {
const existing = deps.state.project.assets.find(
(a) => a.type === kind.type && 'file' in a && a.file === uploaded.file,
);
if (existing) return { name: label, assetId: existing.id, error: null, reused: true };
const command = kind.create(nextAssetName(deps.state.project, nameBase), uploaded);
// A rejected command has already put its reason into the notice; that is the message here.
if (!deps.state.execute(command)) return { name: label, assetId: null, error: deps.state.notice };
return { name: label, assetId: command.asset.id, error: null, reused: false };
}
/**
* Adopts a file the server already holds – no upload, the bytes are there. Nothing collects
* unreferenced files, so one project's dropped picture is still there for the next to pick up.
* Chosen in the picker, so this is "+ → Texture": the tab opens like for any created asset (a kind
* without a tab is just selected). A dropped file only selects – it usually goes into the scene.
*/
export function importStored<U extends UploadedFile, D extends ImportDeps>(
kind: ImportKind<U, D>,
uploaded: U,
deps: D,
): ImportOutcome {
// Named after the file it was uploaded as, like a dropped one; a file from before names were
// kept has none and falls back to the kind's own word.
const nameBase = assetNameForFile(uploaded.name, kind.fallbackName);
const outcome = adopt(kind, uploaded, nameBase, uploaded.name || uploaded.file, deps);
if (outcome.assetId) deps.state.openAsset(outcome.assetId);
deps.state.setNotice(
outcome.error ??
(outcome.reused
? `That ${kind.noun} is already in the project.`
: `Added the ${kind.noun} to the project.`),
);
return outcome;
}
/** One line for the whole batch; reuse is worded apart, since "Imported 1 image" would be a lie. */
export function importNotice(outcomes: readonly ImportOutcome[], noun = 'image'): string {
const added = outcomes.filter((outcome) => outcome.assetId !== null && !outcome.reused).length;
const reused = outcomes.filter((outcome) => outcome.reused).length;
const failed = outcomes.filter((outcome) => outcome.error !== null);
const first = failed[0];
const known = reused === 0 ? '' : ` ${reused === 1 ? 'One was' : `${reused} were`} already in the project.`;
if (!first) {
if (added === 0 && reused > 0)
return reused === 1
? `That ${noun} is already in the project.`
: `Those ${reused} ${noun}s are already in the project.`;
return `Imported ${added} ${added === 1 ? noun : `${noun}s`}.${known}`;
}
const more = failed.length > 1 ? ` (and ${failed.length - 1} more)` : '';
const problem = `Could not import "${first.name}": ${first.error ?? ''}${more}`;
if (added + reused === 0) return problem;
return `Imported ${added + reused} of ${outcomes.length}.${known} ${problem}`;
}
/**
* A file name without its extension, as an asset is called: "hero-idle.png" reads better than
* "Texture 3". The picker gets the same name back from the server.
*/
export function assetNameForFile(fileName: string, fallback: string): string {
return fileName.replace(/\.[^.]+$/, '').trim() || fallback;
}
/** Opens the browser's file dialog; resolves empty when the dialog was cancelled. */
export function pickFiles(accept: string): Promise<File[]> {
return new Promise((resolve) => {
const input = document.createElement('input');
input.type = 'file';
input.accept = accept;
input.multiple = true;
input.style.display = 'none';
document.body.appendChild(input);
const finish = (files: File[]) => {
input.remove();
resolve(files);
};
input.addEventListener('change', () => finish([...(input.files ?? [])]));
// Without `cancel` a dismissed dialog leaves both the element and this promise behind.
input.addEventListener('cancel', () => finish([]));
input.click();
});
}
/** The *Upload…* button: the file dialog, then the batch import of whatever it returns. */
export function pickAndImport<U extends UploadedFile, D extends ImportDeps>(
kind: ImportKind<U, D>,
accept: string,
deps: D,
): void {
void pickFiles(accept)
.then((files) => importFiles(kind, files, deps))
.catch((error: unknown) => deps.state.setNotice(String(error), 'error'));
}