All files / src/lib/helpers validation.ts

62.5% Statements 30/48
60.87% Branches 14/23
50% Functions 1/2
61.7% Lines 29/47

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 1513x 3x 3x         3x       3x 3x 3x   3x 3x 3x               3x                         3x               29x   29x 29x   29x 29x                                                       29x     29x               29x               29x                   29x       6x   29x 2x               29x 1x               29x                     29x            
import l10n from "shared/lib/lang/l10n";
import Path from "path";
import { divisibleBy8 } from "shared/lib/helpers/8bit";
import type {
  BackgroundData,
  Tileset,
} from "shared/lib/entities/entitiesTypes";
import {
  toTileLookup,
  tilesAndLookupToTilemap,
} from "shared/lib/tiles/tileData";
import { assetFilename } from "shared/lib/helpers/assets";
import { readFileToTilesDataArray } from "lib/tiles/readFileToTiles";
import { MAX_BACKGROUND_TILES, MAX_BACKGROUND_TILES_CGB } from "consts";
 
const MAX_IMAGE_WIDTH = 2040;
const MAX_IMAGE_HEIGHT = 2040;
const MAX_PIXELS = 16380 * 64;
 
export interface BackgroundInfo {
  numTiles: number;
  warnings: string[];
  lookup: number[];
}
 
const mergeCommonTiles = async (
  tileData: Uint8Array[],
  commonTileset: Tileset | undefined,
  projectPath: string
) => {
  Iif (!commonTileset) {
    return tileData;
  }
  const commonFilename = assetFilename(projectPath, "tilesets", commonTileset);
  const commonTileData = await readFileToTilesDataArray(commonFilename);
  return [...commonTileData, ...tileData];
};
 
export const getBackgroundInfo = async (
  background: BackgroundData,
  commonTileset: Tileset | undefined,
  is360: boolean,
  isCGBOnly: boolean,
  projectPath: string,
  precalculatedTilesetLength?: number
): Promise<BackgroundInfo> => {
  const warnings: string[] = [];
 
  let tilesetLength = precalculatedTilesetLength;
  let tilesets: number[] = [];
 
  try {
    Iif (!tilesetLength) {
      const filename = assetFilename(projectPath, "backgrounds", background);
      const tileData = await readFileToTilesDataArray(filename);
      const tileDataWithCommon = await mergeCommonTiles(
        tileData,
        commonTileset,
        projectPath
      );
      const tilesetLookup = toTileLookup(tileDataWithCommon);
      tilesets = tilesAndLookupToTilemap(tileData, tilesetLookup);
      tilesetLength = Object.keys(tilesetLookup).length;
    }
  } catch (e) {
    warnings.push(String(e));
    return {
      warnings: [
        l10n("WARNING_BACKGROUND_IS_NOT_A_VALID_PNG", {
          filename: Path.relative(
            projectPath,
            assetFilename(projectPath, "backgrounds", background)
          ),
        }),
      ],
      numTiles: 0,
      lookup: [],
    };
  }
 
  Iif (background.imageWidth < 160 || background.imageHeight < 144) {
    warnings.push(l10n("WARNING_BACKGROUND_TOO_SMALL"));
  }
  Iif (background.imageWidth > MAX_IMAGE_WIDTH) {
    warnings.push(
      l10n("WARNING_BACKGROUND_TOO_WIDE", {
        width: background.imageWidth,
        maxWidth: MAX_IMAGE_WIDTH,
      })
    );
  }
  Iif (background.imageHeight > MAX_IMAGE_HEIGHT) {
    warnings.push(
      l10n("WARNING_BACKGROUND_TOO_TALL", {
        height: background.imageHeight,
        maxHeight: MAX_IMAGE_HEIGHT,
      })
    );
  }
  Iif (background.imageWidth * background.imageHeight > MAX_PIXELS) {
    warnings.push(
      l10n("WARNING_BACKGROUND_TOO_MANY_PIXELS", {
        width: background.imageWidth,
        height: background.imageHeight,
        numPixels: background.imageWidth * background.imageHeight,
        maxPixels: MAX_PIXELS,
      })
    );
  }
  if (
    !divisibleBy8(background.imageWidth) ||
    !divisibleBy8(background.imageHeight)
  ) {
    warnings.push(l10n("WARNING_BACKGROUND_NOT_MULTIPLE_OF_8"));
  }
  if (tilesetLength > MAX_BACKGROUND_TILES && !is360 && !isCGBOnly) {
    warnings.push(
      l10n("WARNING_BACKGROUND_TOO_MANY_TILES", {
        tilesetLength,
        maxTilesetLength: MAX_BACKGROUND_TILES,
      })
    );
  }
 
  if (tilesetLength > MAX_BACKGROUND_TILES_CGB && !is360 && isCGBOnly) {
    warnings.push(
      l10n("WARNING_BACKGROUND_TOO_MANY_TILES", {
        tilesetLength,
        maxTilesetLength: MAX_BACKGROUND_TILES_CGB,
      })
    );
  }
 
  Iif (
    is360 &&
    (background.imageWidth !== 160 || background.imageHeight !== 144)
  ) {
    warnings.push(
      l10n("WARNING_LOGO_WRONG_SIZE", {
        width: background.imageWidth,
        height: background.imageHeight,
      })
    );
  }
  return {
    warnings,
    numTiles: tilesetLength,
    lookup: tilesets,
  };
};