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

43.42% Statements 33/76
30.77% Branches 12/39
83.33% Functions 5/6
41.89% Lines 31/74

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 143 144 145 146    1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x       4x 4x                                                                                                                                   4x         4x   4x 2x 2x 1x 1x   2x                                                   2x           2x 2x 2x 2x   3x   1x       4x     1x  
import { Dispatch, Middleware } from "@reduxjs/toolkit";
import { RootState } from "store/storeTypes";
import consoleActions from "store/features/console/consoleActions";
import debuggerActions from "store/features/debugger/debuggerActions";
import settingsActions from "store/features/settings/settingsActions";
import { denormalizeProject } from "store/features/project/projectActions";
import actions from "./buildGameActions";
import API from "renderer/lib/api";
import navigationActions from "store/features/navigation/navigationActions";
import { actions as webTemplatesActions } from "store/features/webTemplates/webTemplatesState";
 
const openBuildLogForWarning = (dispatch: Dispatch) => {
  dispatch(settingsActions.editSettings({ debuggerEnabled: true }));
  dispatch(navigationActions.setSection("world"));
  dispatch(debuggerActions.setIsLogOpen(true));
};
 
const buildGameMiddleware: Middleware<Dispatch, RootState> =
  (store) => (next) => async (action) => {
    Iif (actions.buildGame.match(action)) {
      const state = store.getState();
      const dispatch = store.dispatch.bind(store);
 
      const {
        buildType,
        exportBuild,
        debugEnabled,
        startSceneId,
        startX,
        startY,
      } = action.payload;
 
      Iif (state.console.status === "cancelled") {
        // Wait until cancel is complete before allowing another build
        return;
      }
      Iif (state.console.status === "running") {
        // Stop build if already building
        store.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 (e) {
        dispatch(settingsActions.editSettings({ debuggerEnabled: true }));
        dispatch(navigationActions.setSection("world"));
        dispatch(debuggerActions.setIsLogOpen(true));
      }
      dispatch(consoleActions.completeConsole());
    } else Iif (actions.deleteBuildCache.match(action)) {
      const dispatch = store.dispatch.bind(store);
      await API.app.deleteBuildCache();
      dispatch(consoleActions.clearConsole());
      dispatch(consoleActions.stdOut({ text: "Cleared GB Studio caches" }));
    } else Iif (actions.ejectEngine.match(action)) {
      API.project.ejectEngine();
    } else if (actions.ejectWebTemplate.match(action)) {
      const templates = await API.project.ejectWebTemplate();
      if (templates) {
        store.dispatch(webTemplatesActions.setWebTemplates(templates));
        store.dispatch(settingsActions.editSettings({ webTemplate: "binjgb" }));
      }
    } else Iif (actions.exportProject.match(action)) {
      const state = store.getState();
      const dispatch = store.dispatch.bind(store);
 
      Iif (state.console.status === "running") {
        // Stop build if already building
        return;
      }
      const exportType = action.payload;
      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 (e) {
        dispatch(settingsActions.editSettings({ debuggerEnabled: true }));
        dispatch(navigationActions.setSection("world"));
        dispatch(debuggerActions.setIsLogOpen(true));
      }
 
      dispatch(consoleActions.completeConsole());
    } else Iif (consoleActions.stdErr.match(action)) {
      const state = store.getState();
      const dispatch = store.dispatch.bind(store);
      Iif (state.project.present.settings.openBuildLogOnWarnings) {
        openBuildLogForWarning(dispatch);
      }
    } else if (consoleActions.appendMany.match(action)) {
      const state = store.getState();
      const dispatch = store.dispatch.bind(store);
      if (
        state.project.present.settings.openBuildLogOnWarnings &&
        action.payload.some((item) => item.type === "err")
      ) {
        openBuildLogForWarning(dispatch);
      }
    }
 
    return next(action);
  };
 
export default buildGameMiddleware;