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.

fix(assets): a .esmesh channel table is checked against the vocabulary it means

yhhc0e66e9bb793
diff --git a/sdk/src/asset/meshFormat.ts b/sdk/src/asset/meshFormat.ts
index 26b79056e..7af96ef5e 100644
--- a/sdk/src/asset/meshFormat.ts
+++ b/sdk/src/asset/meshFormat.ts
@@ -46,6 +46,9 @@ export const MeshChannel = {
} as const;
/** How a channel's components are stored. Append only — serialized. */
+/** Every id {@link MeshChannel} declares, for validating a file's channel table. */
+const CHANNEL_SEMANTICS: ReadonlySet<number> = new Set(Object.values(MeshChannel));
+
export const MeshChannelType = {
Float32: 0,
UNorm8: 1,
@@ -168,6 +171,12 @@ export function decodeMesh(bytes: Uint8Array): MeshData {
}
const channelCount = view.getUint16(6, true);
+ // A semantic IS the shader location it binds to, and the per-object instance
+ // block starts at location 8 — so an id outside the vocabulary collides with that
+ // block: WebGPU rejects the pipeline, GL binds it and draws a wrong frame.
+ if (channelCount > CHANNEL_SEMANTICS.size) {
+ throw new Error(`.esmesh declares ${channelCount} channels; the format has ${CHANNEL_SEMANTICS.size}`);
+ }
const vertexStride = view.getUint32(8, true);
const vertexCount = view.getUint32(12, true);
const indexCount = view.getUint32(16, true);
@@ -182,8 +191,12 @@ export function decodeMesh(bytes: Uint8Array): MeshData {
const channels: MeshChannelDesc[] = [];
let at = headerBytes;
for (let i = 0; i < channelCount; i++) {
+ const semantic = view.getUint8(at);
+ if (!CHANNEL_SEMANTICS.has(semantic)) {
+ throw new Error(`.esmesh channel ${i} has semantic ${semantic}, which the format does not define`);
+ }
channels.push({
- semantic: view.getUint8(at),
+ semantic,
components: view.getUint8(at + 1),
type: view.getUint8(at + 2),
offset: view.getUint32(at + 4, true),
diff --git a/sdk/tests/mesh-format-channels.test.ts b/sdk/tests/mesh-format-channels.test.ts
new file mode 100644
index 000000000..ec346f0b5
--- /dev/null
+++ b/sdk/tests/mesh-format-channels.test.ts
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file What `decodeMesh` refuses in a channel table, and why it has to be here.
+ *
+ * A channel's semantic IS the shader location the engine binds it to, and the
+ * per-object instance block starts at location 8. An id outside the vocabulary
+ * therefore collides with that block instead of failing: WebGPU rejects the
+ * pipeline with nothing naming the mesh, GL binds it and draws a wrong frame.
+ * The decoder is the one reader that can see the table as a table.
+ */
+import { describe, it, expect } from 'vitest';
+import { readFileSync, readdirSync } from 'node:fs';
+import path from 'node:path';
+import {
+ encodeMesh, decodeMesh, packChannels, MeshChannel, MeshChannelType,
+} from '../src/asset/meshFormat';
+
+/** A one-vertex mesh whose channel table is then edited in place. */
+function mesh(): Uint8Array {
+ const { channels, vertexStride } = packChannels([
+ { semantic: MeshChannel.Position, components: 3, type: MeshChannelType.Float32 },
+ ]);
+ return encodeMesh({
+ channels,
+ vertexStride,
+ vertexCount: 1,
+ vertices: new Uint8Array(vertexStride),
+ indices: Uint32Array.from([0, 0, 0]),
+ aabbMin: [0, 0, 0],
+ aabbMax: [0, 0, 0],
+ });
+}
+
+/** Where the channel table starts: the v2 header is 48 bytes. */
+const TABLE = 48;
+
+describe('decodeMesh channel table', () => {
+ it('refuses a semantic the format does not define', () => {
+ const bytes = mesh();
+ // 9 lands inside the instance block's locations (8..12), which is the case
+ // that reaches a backend as an unattributable pipeline error.
+ bytes[TABLE] = 9;
+ expect(() => decodeMesh(bytes)).toThrow(/semantic 9/);
+ });
+
+ it('refuses more channels than the vocabulary has', () => {
+ const bytes = mesh();
+ new DataView(bytes.buffer, bytes.byteOffset).setUint16(6, 64, true);
+ expect(() => decodeMesh(bytes)).toThrow(/64 channels/);
+ });
+
+ it('reads every .esmesh committed to the repo', () => {
+ // The guard is only worth having if it admits what the importers actually
+ // write; these are their output, cooked by the shipping pipeline.
+ const dir = path.resolve(__dirname, '../../desktop/public/scenes');
+ const files = readdirSync(dir).filter((f) => f.endsWith('.esmesh'));
+ expect(files.length).toBeGreaterThan(0);
+ for (const f of files) {
+ expect(() => decodeMesh(new Uint8Array(readFileSync(path.join(dir, f)))), f).not.toThrow();
+ }
+ });
+});