All files / src/lib/project loadEngineSchema.ts

23.53% Statements 8/34
0% Branches 0/21
0% Functions 0/2
21.88% Lines 7/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 811x 1x 1x 1x 1x                       1x                 1x                                                                                                              
import Path from "path";
import { readJSON, pathExists } from "fs-extra";
import glob from "glob";
import uniqBy from "lodash/uniqBy";
import { defaultEngineMetaPath } from "consts";
import type {
  SceneTypeSchema,
  EngineFieldSchema,
} from "store/features/engine/engineState";
 
export interface EngineSchema {
  fields: EngineFieldSchema[];
  sceneTypes: SceneTypeSchema[];
  consts: Record<string, number>;
}
 
const defaultSceneTypes: SceneTypeSchema[] = [
  { key: "TOPDOWN", label: "GAMETYPE_TOP_DOWN" },
  { key: "PLATFORM", label: "GAMETYPE_PLATFORMER" },
  { key: "ADVENTURE", label: "GAMETYPE_ADVENTURE" },
  { key: "SHMUP", label: "GAMETYPE_SHMUP" },
  { key: "POINTNCLICK", label: "GAMETYPE_POINT_N_CLICK" },
  { key: "LOGO", label: "GAMETYPE_LOGO" },
];
 
export const loadEngineSchema = async (
  projectRoot: string,
): Promise<EngineSchema> => {
  const localEngineJsonPath = Path.join(
    projectRoot,
    "assets",
    "engine",
    "engine.json",
  );
  const pluginsPath = Path.join(projectRoot, "plugins");
 
  let defaultEngine: Partial<EngineSchema> = {};
  let localEngine: Partial<EngineSchema> = {};
 
  try {
    localEngine = await readJSON(localEngineJsonPath);
  } catch {
    defaultEngine = await readJSON(defaultEngineMetaPath);
  }
 
  let fields = localEngine.fields || defaultEngine.fields || [];
  let sceneTypes =
    localEngine.sceneTypes || defaultEngine.sceneTypes || defaultSceneTypes;
  let consts = localEngine.consts || defaultEngine.consts || {};
 
  const enginePlugins = glob.sync(`${pluginsPath}/*/engine`);
  for (const enginePluginPath of enginePlugins) {
    const enginePluginJsonPath = Path.join(enginePluginPath, "engine.json");
    Iif (await pathExists(enginePluginJsonPath)) {
      try {
        const pluginEngine: EngineSchema = await readJSON(enginePluginJsonPath);
        Iif (pluginEngine.fields?.length) {
          fields = fields.concat(pluginEngine.fields);
        }
        Iif (pluginEngine.sceneTypes?.length) {
          sceneTypes = sceneTypes.concat(pluginEngine.sceneTypes);
        }
        Iif (pluginEngine.consts) {
          consts = { ...consts, ...pluginEngine.consts }; // Plugin consts override existing ones
        }
      } catch (e) {
        console.warn(e);
      }
    }
  }
 
  // Deduplicate sceneTypes by key
  sceneTypes = uniqBy(sceneTypes, (s) => s.key);
 
  return {
    fields,
    sceneTypes,
    consts,
  };
};