All files / src/store/features/assets assetsMiddleware.ts

96.97% Statements 32/33
71.88% Branches 23/32
100% Functions 5/5
96.77% Lines 30/31

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  1x 1x   1x       1x             1x   1x               3x       3x 3x       3x 3x 3x       3x       3x   3x 3x 3x 3x 3x   3x   3x 3x               3x   2x               2x 2x                   2x                           2x                         3x     1x  
import { Middleware, Dispatch } from "@reduxjs/toolkit";
import actions from "./assetsActions";
import entitiesActions from "store/features/entities/entitiesActions";
import { RootState } from "store/configureStore";
import {
  backgroundSelectors,
  tilesetSelectors,
} from "store/features/entities/entitiesState";
import API from "renderer/lib/api";
import { Background } from "shared/lib/entities/entitiesTypes";
import { HexPalette } from "shared/lib/tiles/autoColor";
import {
  ColorCorrectionSetting,
  ColorModeSetting,
} from "shared/lib/resources/types";
import { DMG_PALETTE } from "consts";
 
const generateAssetHash = (
  background: Background,
  is360: boolean,
  uiPaletteId: string,
  colorMode: ColorModeSetting,
  colorCorrection: ColorCorrectionSetting,
  tilesetId: string,
): string => {
  return `${background._v}_${is360}_${uiPaletteId}_${colorMode}_${colorCorrection}_${tilesetId}_${background.autoColor}`;
};
 
const assetsMiddleware: Middleware<Dispatch, RootState> =
  (store) => (next) => (action) => {
    if (
      actions.loadBackgroundAssetInfo.match(action) ||
      actions.extractBackgroundAssetInfo.match(action)
    ) {
      const isExtracting = actions.extractBackgroundAssetInfo.match(action);
      const state = store.getState();
      const background = backgroundSelectors.selectById(
        state,
        action.payload.backgroundId,
      );
      const tileset = tilesetSelectors.selectById(
        state,
        action.payload.tilesetId ?? "",
      );
      const tilesetId = tileset?.id;
 
      const isCGBOnly = state.project.present.settings.colorMode === "color";
      const colorMode = state.project.present.settings.colorMode;
      const colorCorrection = state.project.present.settings.colorCorrection;
      const is360 = action.payload.is360;
      const uiPaletteId = action.payload.uiPaletteId;
 
      if (background) {
        const cachedInfo =
          state.assets.backgrounds[action.payload.backgroundId];
        const hash = generateAssetHash(
          background,
          is360,
          uiPaletteId,
          colorMode,
          colorCorrection,
          tilesetId,
        );
        if (!cachedInfo || cachedInfo.hash !== hash || isExtracting) {
          const palette =
            uiPaletteId === "dmg"
              ? DMG_PALETTE
              : state.project.present.entities.palettes.entities[uiPaletteId] ||
                state.project.present.entities.palettes.entities[
                  state.project.present.settings.defaultBackgroundPaletteIds[7]
                ] ||
                DMG_PALETTE;
          const uiPalette: HexPalette | undefined =
            uiPaletteId !== "auto" ? palette?.colors : undefined;
          API.project
            .getBackgroundInfo(
              isExtracting ? { ...background, autoColor: true } : background,
              tileset,
              is360,
              uiPalette,
              colorMode,
              colorCorrection,
            )
            .then((info) => {
              store.dispatch(
                actions.setBackgroundAssetInfo({
                  id: action.payload.backgroundId,
                  is360: is360,
                  tilesetId,
                  warnings: info.warnings,
                  numTiles: info.numTiles,
                  lookup: info.lookup,
                  autoPalettes: info.autoPalettes,
                  isCGBOnly,
                  hash,
                }),
              );
 
              Iif (isExtracting && info.autoPalettes) {
                store.dispatch(
                  entitiesActions.setSceneExtractedPalettes({
                    sceneId: action.payload.sceneId,
                    palettes: info.autoPalettes,
                    tileColors: info.attr,
                  }),
                );
              }
            });
        }
      }
    }
    return next(action);
  };
 
export default assetsMiddleware;