All files / src/store/features/buildGame buildGameActions.ts

35.08% Statements 20/57
6.66% Branches 2/30
16.66% Functions 2/12
35.18% Lines 19/54

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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 1432x 2x 2x 2x 2x 2x   2x                           2x             2x                                                                                                                           2x           2x       2x 2x 2x 1x 1x         2x                                               2x               2x  
import API from "renderer/lib/api";
import consoleActions from "store/features/console/consoleActions";
import debuggerActions from "store/features/debugger/debuggerActions";
import navigationActions from "store/features/navigation/navigationActions";
import { denormalizeProject } from "store/features/project/projectActions";
import settingsActions from "store/features/settings/settingsActions";
import type { AppThunk } from "store/storeTypes";
import { actions as webTemplatesActions } from "store/features/webTemplates/webTemplatesState";
 
export type BuildType = "web" | "rom" | "pocket";
export type ProjectExportType = "src" | "data";
 
type BuildGameOptions = {
  buildType?: BuildType;
  exportBuild?: boolean;
  debugEnabled?: boolean;
  startSceneId?: string;
  startX?: number;
  startY?: number;
};
 
const openBuildLog = (dispatch: Parameters<AppThunk>[0]) => {
  dispatch(settingsActions.editSettings({ debuggerEnabled: true }));
  dispatch(navigationActions.setSection("world"));
  dispatch(debuggerActions.setIsLogOpen(true));
};
 
const buildGame =
  ({
    buildType = "web",
    exportBuild = false,
    debugEnabled = false,
    startSceneId,
    startX,
    startY,
  }: BuildGameOptions = {}): AppThunk<Promise<void>> =>
  async (dispatch, getState) => {
    const state = getState();
 
    if (state.console.status === "cancelled") {
      return;
    }
    if (state.console.status === "running") {
      dispatch(consoleActions.cancelConsole());
      await API.project.buildCancel();
      return;
    }
 
    dispatch(consoleActions.startConsole());
 
    const project = denormalizeProject(state.project.present);
    const engineSchema = {
      fields: state.engine.fields,
      sceneTypes: state.engine.sceneTypes,
      consts: state.engine.consts,
    };
    const selectionIds = state.editor.sceneSelectionIds;
 
    try {
      await API.project.build(
        {
          ...project,
          scenes:
            startSceneId && project.settings.runSceneSelectionOnly
              ? project.scenes.filter(
                  (scene) =>
                    scene.id === startSceneId ||
                    selectionIds.includes(scene.id),
                )
              : project.scenes,
          settings: {
            ...project.settings,
            startSceneId: startSceneId ?? project.settings.startSceneId,
            startX: startX ?? project.settings.startX,
            startY: startY ?? project.settings.startY,
          },
        },
        {
          buildType,
          engineSchema,
          exportBuild,
          debugEnabled,
        },
      );
    } catch {
      openBuildLog(dispatch);
    }
    dispatch(consoleActions.completeConsole());
  };
 
const deleteBuildCache = (): AppThunk<Promise<void>> => async (dispatch) => {
  await API.app.deleteBuildCache();
  dispatch(consoleActions.clearConsole());
  dispatch(consoleActions.stdOut({ text: "Cleared GB Studio caches" }));
};
 
const ejectEngine = (): AppThunk => () => {
  API.project.ejectEngine();
};
 
const ejectWebTemplate = (): AppThunk<Promise<void>> => async (dispatch) => {
  const templates = await API.project.ejectWebTemplate();
  if (templates) {
    dispatch(webTemplatesActions.setWebTemplates(templates));
    dispatch(settingsActions.editSettings({ webTemplate: "binjgb" }));
  }
};
 
const exportProject =
  (exportType: ProjectExportType): AppThunk<Promise<void>> =>
  async (dispatch, getState) => {
    const state = getState();
 
    if (state.console.status === "running") {
      return;
    }
    dispatch(consoleActions.startConsole());
 
    const project = denormalizeProject(state.project.present);
    const engineSchema = {
      fields: state.engine.fields,
      sceneTypes: state.engine.sceneTypes,
      consts: state.engine.consts,
    };
    try {
      await API.project.exportProject(project, engineSchema, exportType);
    } catch {
      openBuildLog(dispatch);
    }
 
    dispatch(consoleActions.completeConsole());
  };
 
const buildGameActions = {
  buildGame,
  deleteBuildCache,
  ejectEngine,
  ejectWebTemplate,
  exportProject,
};
 
export default buildGameActions;