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

0% Statements 0/64
0% Branches 0/31
0% Functions 0/4
0% Lines 0/62

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                                                                                                                                                                                                                                                             
import { Dispatch, Middleware } from "@reduxjs/toolkit";
import { RootState } from "store/configureStore";
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";
 
const buildGameMiddleware: Middleware<Dispatch, RootState> =
  (store) => (next) => async (action) => {
    if (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 engineFields = state.engine.fields;
      const sceneTypes = state.engine.sceneTypes;
      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,
            engineFields,
            exportBuild,
            debugEnabled,
            sceneTypes,
          }
        );
      } catch (e) {
        dispatch(settingsActions.editSettings({ debuggerEnabled: true }));
        dispatch(navigationActions.setSection("world"));
        dispatch(debuggerActions.setIsLogOpen(true));
      }
      dispatch(consoleActions.completeConsole());
    } else if (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 if (actions.ejectEngine.match(action)) {
      API.project.ejectEngine();
    } else if (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 engineFields = state.engine.fields;
      const sceneTypes = state.engine.sceneTypes;
 
      try {
        await API.project.exportProject(
          project,
          engineFields,
          sceneTypes,
          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) {
        dispatch(settingsActions.editSettings({ debuggerEnabled: true }));
        dispatch(navigationActions.setSection("world"));
        dispatch(debuggerActions.setIsLogOpen(true));
      }
    }
 
    return next(action);
  };
 
export default buildGameMiddleware;