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

92.72% Statements 51/55
75.92% Branches 41/54
100% Functions 7/7
96% Lines 48/50

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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163  2x 2x   2x         2x             2x   2x 2x   2x                 3x       6x 6x 2x 2x 2x 2x 2x 2x     1x 1x 1x 1x 1x               1x 1x             6x       3x 3x 3x       3x       3x   3x 3x 3x   3x 3x 3x   3x   3x 3x                 3x   2x               2x   2x               2x                     2x                           2x                         6x     2x  
import { Middleware, Dispatch } from "@reduxjs/toolkit";
import actions from "./assetsActions";
import entitiesActions from "store/features/entities/entitiesActions";
import { RootState } from "store/storeTypes";
import {
  backgroundSelectors,
  tilesetSelectors,
  sceneSelectors,
} from "store/features/entities/entitiesSelectors";
import API from "renderer/lib/api";
import { HexPalette } from "shared/lib/tiles/autoColor";
import {
  BackgroundAsset,
  ColorCorrectionSetting,
  ColorModeSetting,
} from "shared/lib/resources/types";
import { DMG_PALETTE } from "consts";
 
const sceneTilemapTimers = new Map<string, ReturnType<typeof setTimeout>>();
const sceneTilemapRequests = new Map<string, number>();
 
const generateAssetHash = (
  background: BackgroundAsset,
  is360: boolean,
  uiPaletteId: string,
  colorMode: ColorModeSetting,
  colorCorrection: ColorCorrectionSetting,
  autoTileFlipEnabled: boolean,
  tilesetId: string,
): string => {
  return `${background._v}_${is360}_${uiPaletteId}_${colorMode}_${colorCorrection}_${tilesetId}_${background.autoColor}_${autoTileFlipEnabled}_${background.autoTileFlipOverride}`;
};
 
const assetsMiddleware: Middleware<Dispatch, RootState> =
  (store) => (next) => (action) => {
    if (actions.loadSceneTilemapAssetInfo.match(action)) {
      const { sceneId } = action.payload;
      const requestId = (sceneTilemapRequests.get(sceneId) ?? 0) + 1;
      sceneTilemapRequests.set(sceneId, requestId);
      const previousTimer = sceneTilemapTimers.get(sceneId);
      if (previousTimer) clearTimeout(previousTimer);
      sceneTilemapTimers.set(
        sceneId,
        setTimeout(() => {
          sceneTilemapTimers.delete(sceneId);
          const state = store.getState();
          const scene = sceneSelectors.selectById(state, sceneId);
          Iif (!scene?.tilemap) return;
          API.project
            .getSceneTilemapInfo(
              scene,
              tilesetSelectors.selectAll(state),
              state.project.present.settings.colorMode,
              state.project.present.settings.autoTileFlipEnabled,
            )
            .then((info) => {
              Iif (sceneTilemapRequests.get(sceneId) !== requestId) return;
              store.dispatch(
                actions.setSceneTilemapAssetInfo({ sceneId, ...info }),
              );
            });
        }, 750),
      );
    }
    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 colorMode = action.payload.colorMode;
      const isCGBOnly = colorMode === "color";
      const colorCorrection = state.project.present.settings.colorCorrection;
      const autoTileFlipEnabled =
        state.project.present.settings.autoTileFlipEnabled;
      const is360 = action.payload.is360;
      const uiPaletteId = action.payload.uiPaletteId;
 
      Eif (background) {
        const cachedInfo =
          state.assets.backgrounds[action.payload.backgroundId];
        const hash = generateAssetHash(
          background,
          is360,
          uiPaletteId,
          colorMode,
          colorCorrection,
          autoTileFlipEnabled,
          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;
 
          Iif (isExtracting) {
            API.project.extractBackgroundMonoTiles(
              background,
              uiPalette,
              colorCorrection,
            );
          }
 
          API.project
            .getBackgroundInfo(
              isExtracting ? { ...background, autoColor: true } : background,
              tileset,
              is360,
              uiPalette,
              colorMode,
              colorCorrection,
              autoTileFlipEnabled,
            )
            .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;