Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

14.4 KB
Raw Blame
#!/usr/bin/env node
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file  check-live-asset-rebind.mjs — hot update reads the declaration, not a list.
 *
 * `component.assetFields` is what says a field carries an asset: discovery,
 * cook inclusion and `@uuid` resolution all read it. The rebinder did not — it
 * named Sprite and MeshRenderer — so five of the seven built-in texture fields
 * were never swapped on a hot update, and no project or plugin component was
 * either. Nothing failed; the game just kept drawing the old image.
 *
 * A list like that cannot be kept correct, because the thing it must agree with
 * is authored somewhere else (a C++ ES_PROPERTY, a project's defineComponent).
 * So the rules guarded here are that there is no list, and that a caller who
 * holds receipts does not fall back to naming paths:
 *
 *   1. No file on the rebind path names an asset-bearing component type.
 *   2. The walk goes through the registry and reads `assetFields`.
 *   3. The rebind path acquires by receipt. A `load*` hands back no receipt, so
 *      what it takes out of the ledger is a row nobody can give back — that is
 *      the leak the rebinder had, one row per successful hot update.
 *   4. The runtime scene loader hands the scene its receipts, not its paths.
 *   5. A loader owns nothing. What it acquires is the preparation's and comes
 *      back when the era retires, so a receipt kept in a result field or given
 *      back in unload is a second answer about who owns a sub-asset — and the
 *      one that fires twice.
 *   6. Who owns what an entity holds is answered in ONE place. A second copy of
 *      that rule is how a promoted entity's replacement went to the app scope,
 *      which ends only when the app does.
 *   7. A loader has exactly ONE door: `load`/`unload` for an asset a component
 *      holds by handle, `registry` for one it holds by ref. Two doors to the
 *      same asset is two answers about who owns it — and the registry door
 *      exists because publication has to be the slot's, not an era's.
 *   8. A registry-backed loader only PREPARES. Registering what it parsed into
 *      a subsystem's own map as well is a second copy of "which asset this name
 *      means" — and a module-global one crosses realms, so an editor world and
 *      a play world answered each other's lookups.
 *   9. No loader describes a dependency. An edge is a projection of the
 *      acquisition that made it, and a door for declaring one beside the work
 *      is a second source of fact — the exact shape this line of work removed
 *      from ownership.
 *
 * Run: node tools/check-live-asset-rebind.mjs   (exit 1 on violation)
 */
import { readFileSync, readdirSync, existsSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');

/** The files that answer "which live fields hold this asset?". */
const REBIND_PATH = [
  'sdk/src/asset/liveAssetBindings.ts',
  'sdk/src/asset/liveAssetRebind.ts',
  'sdk/src/hotUpdateRebind.ts',
];

const GENERATED = 'sdk/src/ecs/component.generated.ts';

const missing = [...REBIND_PATH, GENERATED].filter((f) => !existsSync(path.join(ROOT, f)));
if (missing.length) {
  console.error(`check-live-asset-rebind is stale: ${missing.join(', ')} does not exist.`);
  process.exit(1);
}

/** Comments out, line numbers intact — a finding names a line someone reads. */
const stripComments = (text) =>
  text
    .replace(/\/\*[\s\S]*?\*\//g, (m) => m.replace(/[^\n]/g, ' '))
    .replace(/\/\/[^\n]*/g, '');

/**
 * Components that declare an asset field, from the two places they are authored:
 * the generated C++ metadata, and `defineComponent` in the SDK. These are the
 * names a rebinder is tempted to write down.
 */
function assetBearingComponents() {
  const names = new Set();
  const generated = readFileSync(path.join(ROOT, GENERATED), 'utf8');
  const entry = /^ {4}([A-Za-z0-9_]+): \{$/gm;
  let m;
  const marks = [];
  while ((m = entry.exec(generated))) marks.push({ name: m[1], at: m.index });
  for (let i = 0; i < marks.length; i++) {
    const body = generated.slice(marks[i].at, marks[i + 1]?.at ?? generated.length);
    if (/assetFields: \[\s*\{/.test(body)) names.add(marks[i].name);
  }
  // SDK-side components declare theirs at the defineComponent call.
  const src = readFileSync(path.join(ROOT, 'sdk/src/ecs/component.ts'), 'utf8');
  const call = /defineComponent<[^>]*>\(\s*'([A-Za-z0-9_]+)'([\s\S]{0,2000}?)\n\}\);/g;
  while ((m = call.exec(src))) {
    if (m[2].includes('assetFields')) names.add(m[1]);
  }
  return names;
}

const components = assetBearingComponents();
// An empty set would pass every check below without reading a thing.
if (components.size < 6) {
  console.error(`check-live-asset-rebind: found only ${components.size} asset-bearing component(s) — the parser no longer matches how they are declared.`);
  process.exit(1);
}

const findings = [];
for (const file of REBIND_PATH) {
  const text = readFileSync(path.join(ROOT, file), 'utf8');
  const code = stripComments(text);
  for (const name of components) {
    const hit = new RegExp(`\\b${name}\\b`).exec(code);
    if (!hit) continue;
    const line = code.slice(0, hit.index).split('\n').length;
    findings.push(`${file}:${line}  names the component type "${name}".`);
  }
}

/** One exported function's body, braces balanced. */
function functionBody(text, name) {
  const m = new RegExp(`export function ${name}\\b`).exec(text);
  if (!m) return null;
  let i = text.indexOf('{', m.index);
  if (i < 0) return null;
  const start = i;
  for (let depth = 0; i < text.length; i++) {
    if (text[i] === '{') depth++;
    else if (text[i] === '}' && --depth === 0) break;
  }
  return text.slice(start + 1, i);
}

// Per function, not per file: the file kept the word `assetFields` in a
// neighbouring helper while the walk itself had been replaced by a literal.
const discovery = stripComments(readFileSync(path.join(ROOT, REBIND_PATH[0]), 'utf8'));
for (const fn of ['findLiveAssetBindings', 'componentsBindingAssetTypes']) {
  const body = functionBody(discovery, fn);
  if (body === null) {
    findings.push(`${REBIND_PATH[0]}  ${fn}() is gone — the rebind path's only door to the declaration.`);
    continue;
  }
  if (!/getComponentRegistry\(/.test(body) || !/\.assetFields\b/.test(body)) {
    findings.push(`${REBIND_PATH[0]}  ${fn}() no longer walks getComponentRegistry() reading assetFields — the declaration is the only thing that knows which fields carry an asset.`);
  }
}

// A replacement is acquired, never merely loaded: `load*` hands back no receipt.
for (const file of REBIND_PATH) {
  const code = stripComments(readFileSync(path.join(ROOT, file), 'utf8'));
  const call = /\bload(?:Texture|Material|Font|Audio|Typed|Prefab|Mesh)\s*\(/.exec(code);
  if (call) {
    findings.push(`${file}:${code.slice(0, call.index).split('\n').length}  takes an asset out with a load*, which hands back no receipt — acquire it.`);
  }
}

// A caller holding receipts must not hand over paths: a path-addressed release
// after a hot update gives back the oldest era, and what the paths omit is
// never given back at all.
const LOADER = 'sdk/src/runtime/runtimeLoader.ts';
const loader = stripComments(readFileSync(path.join(ROOT, LOADER), 'utf8'));
if (!/\bpreloadSceneAssets\(/.test(loader)) {
  findings.push(`${LOADER}  no longer preloads scene assets — this guard is reading the wrong file.`);
} else {
  const call = /\btrackAssets\(/.exec(loader);
  if (call) {
    findings.push(`${LOADER}:${loader.slice(0, call.index).split('\n').length}  hands the scene PATHS while holding the preload's receipts — use trackAssetScope.`);
  }
}

// The same rule one layer down: a loader owns nothing. It acquires through a
// door that records what it took, and the era gives it back — so a receipt in a
// result field, or a release() inside a loader, is the second owner.
/** Every `export interface` body in a file, by name. */
function interfaces(code) {
  const declaration = /export interface (\w+) \{/g;
  const out = [];
  let m;
  while ((m = declaration.exec(code))) {
    const start = code.indexOf('{', m.index);
    let i = start;
    for (let depth = 0; i < code.length; i++) {
      if (code[i] === '{') depth++;
      else if (code[i] === '}' && --depth === 0) break;
    }
    out.push({ name: m[1], body: code.slice(start, i), at: start });
  }
  return out;
}

const OWNS = [
  path.join(ROOT, 'sdk/src/asset/AssetLoader.ts'),
  ...readdirSync(path.join(ROOT, 'sdk/src/asset/loaders'))
    .filter((f) => f.endsWith('.ts'))
    .map((f) => path.join(ROOT, 'sdk/src/asset/loaders', f)),
];
for (const file of OWNS) {
  const rel = path.relative(ROOT, file);
  const code = stripComments(readFileSync(file, 'utf8'));
  for (const { name, body, at } of interfaces(code)) {
    // A FIELD of lease type. A door that HANDS one over — `acquireTexture(…):
    // Promise<AssetLease<…>>` — is the opposite: it is how a receipt reaches
    // the preparation that will own it.
    const field = /\n\s*(?:readonly\s+)?\w+\??:\s*[^(\n]*\bAssetLease\b/.exec(body);
    if (!field) continue;
    const line = code.slice(0, at + field.index).split('\n').length + 1;
    findings.push(`${rel}:${line}  ${name} keeps a receipt in a field — what a load acquired is the preparation's, and the era gives it back.`);
  }
  if (rel === 'sdk/src/asset/AssetLoader.ts') continue;
  const call = /\.release\(\s*\)/.exec(code);
  if (call) {
    findings.push(`${rel}:${code.slice(0, call.index).split('\n').length}  gives back a receipt it does not own — releasing it here frees it a second time when the era retires.`);
  }
}

// Owner resolution has one source of truth: assetScopeForEntity. A rebinder
// working it out from the scene tag cannot know about an entity that owns its
// assets itself.
const OWNER_RULE = /\bSceneOwner\b|\bappScope\b|\bassetScopeFor\s*\(/;
for (const file of REBIND_PATH) {
  const code = stripComments(readFileSync(path.join(ROOT, file), 'utf8'));
  const hit = OWNER_RULE.exec(code);
  if (!hit) continue;
  findings.push(`${file}:${code.slice(0, hit.index).split('\n').length}  works out who owns an entity's assets for itself — call assetScopeForEntity.`);
}

// Nobody declares an edge. The recorder is the asset layer's; a loader reaches
// dependencies only by acquiring something.
const DECLARES_EDGE = /\b(?:addEdge|addDependency|DependencyRecorder)\b/;

// A registry-backed loader writes nothing. `register*`/`publish*` inside one is
// the mirror this model exists to remove.
const PUBLISHES = /\b(?:register|publish|unregister|unpublish)[A-Z]\w*\s*\(/;

// One door per loader. A registry-backed loader that also published from a
// `load` would be an era writing the registry, which is how a retiring one
// deletes the entry its successor just put under the same name.
const LOADERS_DIR = path.join(ROOT, 'sdk/src/asset/loaders');
let loaders = 0;
for (const name of readdirSync(LOADERS_DIR).filter((f) => f.endsWith('.ts'))) {
  const code = stripComments(readFileSync(path.join(LOADERS_DIR, name), 'utf8'));
  if (!/implements AssetLoader</.test(code)) continue;
  loaders++;
  const registry = /\breadonly registry\b/.test(code);
  const direct = /^\s{4}(?:async )?load\s*\(/m.test(code);
  if (registry === direct) {
    findings.push(`sdk/src/asset/loaders/${name}  has ${registry ? 'BOTH doors' : 'neither door'} — a loader answers with load/unload or with registry, never both.`);
  }
  const declares = DECLARES_EDGE.exec(code);
  if (declares) {
    findings.push(`sdk/src/asset/loaders/${name}:${code.slice(0, declares.index).split('\n').length}  describes a dependency (${declares[0]}) — an edge comes from the acquisition, never from a loader saying so.`);
  }
  const writes = registry ? PUBLISHES.exec(code) : null;
  if (writes) {
    findings.push(`sdk/src/asset/loaders/${name}:${code.slice(0, writes.index).split('\n').length}  publishes into a registry of its own (${writes[0].trim()}) — a registry-backed loader only prepares; the slot holds the era and answers the lookup.`);
  }
}
if (loaders < 10) {
  console.error(`check-live-asset-rebind: found only ${loaders} loader(s) — the parser no longer matches how they are declared.`);
  process.exit(1);
}

/**
 * 6. A slot takes an asset out through the door its loader HAS. `Assets.loadX`
 *    rejects a loader that publishes by name, and the failure surfaces much
 *    later as a preflight blocker about the reference. The loaders' own shape
 *    against what the slot table calls, so neither can drift alone.
 */
const SLOTS = 'desktop/src/project/assetSlots.ts';
const byName = new Set();
for (const name of readdirSync(LOADERS_DIR).filter((f) => f.endsWith('.ts'))) {
  const code = stripComments(readFileSync(path.join(LOADERS_DIR, name), 'utf8'));
  const type = /readonly type = '([a-z-]+)'/.exec(code);
  if (!type) continue;
  const hasLoad = /^\s{4}(?:async )?load\(/m.test(code) || /^\s{4}load:/m.test(code);
  if (!hasLoad && /readonly registry/.test(code)) byName.add(type[1]);
}
if (byName.size === 0) {
  findings.push(`${LOADERS_DIR}: no name-publishing loader found — the parser no longer matches how they declare a registry.`);
} else if (existsSync(path.join(ROOT, SLOTS))) {
  const slots = stripComments(readFileSync(path.join(ROOT, SLOTS), 'utf8'));
  for (const type of byName) {
    const row = new RegExp(`['"]?${type}['"]?: \\{[^}]*\\}`).exec(slots);
    if (!row) {
      findings.push(`${SLOTS}: no slot for "${type}", which the preload acquires — a live load has no door.`);
    } else if (/\ba\.load[A-Z]/.test(row[0])) {
      findings.push(`${SLOTS}: the "${type}" slot loads through a load* door, and its loader publishes by name — acquire it through its slot.`);
    }
  }
} else {
  console.log(`check-live-asset-rebind: no editor checkout — ${SLOTS} was not read, so the ${byName.size} name-publishing slot(s) were not judged.`);
}

if (findings.length === 0) {
  console.log(`check-live-asset-rebind: the rebind path reads the declaration for all ${components.size} asset-bearing components, acquires by receipt, asks one place who owns what, and each of the ${loaders} loaders has one door, owns nothing and declares no edges; ${byName.size} name-publishing slot(s) acquire through theirs.`);
  process.exit(0);
}
for (const f of findings) console.error(f);
console.error('\nLive assets are addressed by what DECLARES them: the asset field for a binding, the receipt for an acquisition.');
process.exit(1);