All files / src/lib/compiler compileEmotes.ts

42.86% Statements 9/21
0% Branches 0/5
33.33% Functions 1/3
42.86% Lines 9/21

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 731x 1x 1x 1x                                     1x   1x         1x                                                                             1x     1x  
import promiseLimit from "lib/helpers/promiseLimit";
import getFileModifiedTime from "lib/helpers/fs/getModifiedTime";
import { assetFilename } from "shared/lib/helpers/assets";
import { readFileToSpriteTilesData } from "lib/sprites/readSpriteData";
import { EmoteData } from "shared/lib/entities/entitiesTypes";
 
type CompileEmoteOptions = {
  warnings: (msg: string) => void;
};
 
export type PrecompiledEmoteData = EmoteData & {
  data: Uint8Array;
  size: number;
  frames: number;
};
 
const emoteBuildCache: Record<
  string,
  {
    timestamp: number;
    data: Uint8Array;
  }
> = {};
 
const compileEmotes = async (
  emotes: EmoteData[],
  projectRoot: string,
  { warnings }: CompileEmoteOptions
): Promise<PrecompiledEmoteData[]> => {
  const emoteData = await promiseLimit(
    10,
    emotes.map((emote) => {
      return async (): Promise<PrecompiledEmoteData> => {
        const filename = assetFilename(projectRoot, "emotes", emote);
        const modifiedTime = await getFileModifiedTime(filename);
        let data;
 
        if (
          emoteBuildCache[emote.id] &&
          emoteBuildCache[emote.id].timestamp >= modifiedTime
        ) {
          data = emoteBuildCache[emote.id].data;
        } else {
          data = await readFileToSpriteTilesData(filename);
          emoteBuildCache[emote.id] = {
            data,
            timestamp: modifiedTime,
          };
        }
 
        const size = data.length;
        const frames = Math.ceil(size / 64);
        Iif (Math.ceil(size / 64) !== Math.floor(size / 64)) {
          warnings(
            `Emote '${emote.filename}' has invalid dimensions and may not appear correctly. Must be 16px tall and a multiple of 16px wide.`
          );
        }
 
        return {
          ...emote,
          data,
          size,
          frames,
        };
      };
    })
  );
 
  return emoteData;
};
 
export default compileEmotes;