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.

8.5 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
import type { ESEngineModule, CppRegistry, CppResourceManager } from '../../src/wasm';
import { INVALID_ENTITY } from '../../src/types';
import type { Entity } from '../../src/types';

/**
 * The mock module, plus the handful of setters that stand in for computation the
 * mock does not do. `setUINodeHiddenInTree` is the layout pass's verdict, which a
 * test states instead of solving.
 */
export interface MockModule extends ESEngineModule {
    setUINodeHiddenInTree(entity: Entity, hidden: boolean): void;
    /** The one registry this module answers for — `app.connectCpp` takes it. */
    getRegistry(): CppRegistry;
}

export function createMockModule(): MockModule {
    const entities = new Set<Entity>();
    const components = new Map<Entity, Map<string, any>>();
    const destructionPending = new Set<Entity>();
    let nextEntity = 1;

    const baseRegistry = {
        create: () => {
            const entity = nextEntity++ as Entity;
            entities.add(entity);
            components.set(entity, new Map());
            return entity;
        },

        destroy: (entity: Entity) => {
            entities.delete(entity);
            components.delete(entity);
        },

        valid: (entity: Entity) => {
            return entities.has(entity);
        },

        entityCount: () => entities.size,

        // Shared subtree teardown ownership, as Registry::reserveDestruction has
        // it: one owner at a time, and only over a live entity. Every core binds
        // these, so World takes ownership without asking whether it may.
        reserveDestruction: (entity: Entity) => {
            if (!entities.has(entity) || destructionPending.has(entity)) return false;
            destructionPending.add(entity);
            return true;
        },
        releaseDestruction: (entity: Entity) => { destructionPending.delete(entity); },
        destructionPending: (entity: Entity) => destructionPending.has(entity),

        /**
         * Every engine pool's version. A constant, and the TRUTH rather than a
         * stub: this registry keeps components in JS maps, so nothing here has
         * an address that can move. Null would mean "assume everything moved",
         * which kept the AOT row cache from ever being reused in a suite.
         */
        layoutEpoch: () => 0,

        // Mirrors ecs::setParent: both halves of the link, or neither.
        setParent: (child: Entity, parent: Entity) => {
            const detach = (): void => {
                const current = components.get(child)?.get('Parent') as { entity: Entity } | undefined;
                if (!current) return;
                const siblings = components.get(current.entity)?.get('Children') as { entities: Entity[] } | undefined;
                if (siblings) {
                    siblings.entities = siblings.entities.filter((e) => e !== child);
                }
                components.get(child)?.delete('Parent');
            };

            detach();
            if (parent === INVALID_ENTITY || !entities.has(parent)) return;

            components.get(child)?.set('Parent', { entity: parent });
            const kids = components.get(parent)?.get('Children') as { entities: Entity[] } | undefined;
            if (kids) {
                if (!kids.entities.includes(child)) kids.entities.push(child);
            } else {
                components.get(parent)?.set('Children', { entities: [child] });
            }
        },

        delete: () => {},
        removeParent: (entity: Entity) => {
            baseRegistry.setParent(entity, INVALID_ENTITY);
        },

        // Legacy names used by some internal code paths
        createEntity: () => {
            const entity = nextEntity++ as Entity;
            entities.add(entity);
            components.set(entity, new Map());
            return entity;
        },

        destroyEntity: (entity: Entity) => {
            entities.delete(entity);
            components.delete(entity);
        },

        isValid: (entity: Entity) => {
            return entities.has(entity);
        },

        getEntitiesWithComponents: (componentNames: string[]) => {
            const result: Entity[] = [];
            for (const [entity, comps] of components.entries()) {
                if (componentNames.every(name => comps.has(name))) {
                    result.push(entity);
                }
            }
            return result;
        },

        hasComponent: (entity: Entity, componentName: string) => {
            return components.get(entity)?.has(componentName) ?? false;
        },

        insertComponent: (entity: Entity, componentName: string, data: any) => {
            const entityComps = components.get(entity);
            if (entityComps) {
                entityComps.set(componentName, data);
            }
        },

        removeComponent: (entity: Entity, componentName: string) => {
            components.get(entity)?.delete(componentName);
        },

        getComponentData: (entity: Entity, componentName: string) => {
            return components.get(entity)?.get(componentName);
        },
    };

    const registry = new Proxy(baseRegistry, {
        get(target, prop: string) {
            if (prop in target) {
                return (target as any)[prop];
            }
            if (prop.startsWith('add')) {
                const name = prop.slice(3);
                return (entity: Entity, data: any) => {
                    components.get(entity)?.set(name, JSON.parse(JSON.stringify(data)));
                };
            }
            if (prop.startsWith('get') && prop !== 'getEntitiesWithComponents' && prop !== 'getComponentData') {
                const name = prop.slice(3);
                return (entity: Entity) => {
                    const data = components.get(entity)?.get(name);
                    if (!data) return undefined;
                    const copy = JSON.parse(JSON.stringify(data));
                    if (name === 'Children') {
                        const children = copy.entities as Entity[];
                        // Match the owning vector returned by the actual core.
                        // This mock stores JS arrays, so deletion has no heap work.
                        copy.entities = { size: () => children.length, get: (i: number) => children[i], delete: () => {} };
                    }
                    return copy;
                };
            }
            if (prop.startsWith('has') && prop !== 'hasComponent') {
                const name = prop.slice(3);
                return (entity: Entity) => {
                    return components.get(entity)?.has(name) ?? false;
                };
            }
            if (prop.startsWith('remove') && prop !== 'removeComponent' && prop !== 'removeParent') {
                const name = prop.slice(6);
                return (entity: Entity) => {
                    components.get(entity)?.delete(name);
                };
            }
            return undefined;
        },
    }) as unknown as CppRegistry;

    const resourceManager: CppResourceManager = {
        loadTexture: () => 1,
        releaseTexture: () => {},
        getTextureSize: () => ({ width: 100, height: 100 }),
        getTextureDimensions: () => ({ width: 100, height: 100 }),
        loadBitmapFont: () => 1,
        releaseBitmapFont: () => {},
        setTextureMetadata: () => {},
        setTextureBudget: () => {},
        registerTextureWithPath: () => {},
        acquireTextureByPath: () => 0,
        invalidateTexturePath: () => false,
        trimTextureCache: () => 0,
        getResourceStats: () => ({
            shaderCount: 0, textureCount: 0, vertexBufferCount: 0, indexBufferCount: 0,
            cacheHits: 0, cacheMisses: 0,
            textureBytes: 0, textureBudget: 0, textureEvictableCount: 0, retainedBytes: 0,
        }),
    };

    // `hidden_in_tree_` is layout output, not a stored field, so the mock cannot
    // derive it — a test that needs it says what the layout pass resolved via
    // `setUINodeHiddenInTree`. Default false: a node nobody spoke for is on screen.
    const hiddenInTree = new Map<Entity, boolean>();

    return {
        getRegistry: () => registry,
        getResourceManager: () => resourceManager,
        getUINodeHiddenInTree: (_registry: unknown, entity: Entity) => hiddenInTree.get(entity) ?? false,
        setUINodeHiddenInTree: (entity: Entity, hidden: boolean) => { hiddenInTree.set(entity, hidden); },
        GL: {} as any,
    } as MockModule;
}