All files / src/lib/project sceneTypes.ts

27.03% Statements 10/37
0% Branches 0/13
0% Functions 0/2
25.71% Lines 9/35

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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 991x 1x 1x   1x 1x 1x                   1x   1x                                                     1x                                                                                                          
import EventEmitter from "events";
import Path from "path";
import { readJSON, pathExists } from "fs-extra";
import type { SceneTypeSchema } from "store/features/engine/engineState";
import { engineRoot } from "consts";
import glob from "glob";
import uniqBy from "lodash/uniqBy";
 
interface EngineData {
  sceneTypes?: SceneTypeSchema[];
}
 
export interface SceneTypeSyncResult {
  sceneTypes: SceneTypeSchema[];
}
 
export const sceneTypesEmitter = new EventEmitter.EventEmitter();
 
const defaultSceneTypes = [
  {
    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 loadSceneTypes = async (
  projectRoot: string
): Promise<SceneTypeSchema[]> => {
  const defaultEngineJsonPath = Path.join(engineRoot, "gb", "engine.json");
  const localEngineJsonPath = Path.join(
    projectRoot,
    "assets",
    "engine",
    "engine.json"
  );
  const pluginsPath = Path.join(projectRoot, "plugins");
 
  let defaultEngine: EngineData = {};
  let localEngine: EngineData = {};
 
  try {
    localEngine = await readJSON(localEngineJsonPath);
  } catch (e) {
    defaultEngine = await readJSON(defaultEngineJsonPath);
  }
 
  let sceneTypes: SceneTypeSchema[] = [];
 
  if (localEngine && localEngine.sceneTypes) {
    sceneTypes = localEngine.sceneTypes;
  } else Iif (defaultEngine && defaultEngine.sceneTypes) {
    sceneTypes = defaultEngine.sceneTypes;
  }
 
  Iif (!sceneTypes || (sceneTypes && sceneTypes.length === 0)) {
    sceneTypes = defaultSceneTypes;
  }
 
  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 = await readJSON(enginePluginJsonPath);
        Iif (pluginEngine.sceneTypes) {
          sceneTypes = sceneTypes.concat(pluginEngine.sceneTypes);
        }
      } catch (e) {
        console.warn(e);
      }
    }
  }
 
  // Remove duplicate scene types
  sceneTypes = uniqBy(sceneTypes, (s) => s.key);
 
  return sceneTypes;
};