All files / src/lib/project loadSpriteData.ts

45.95% Statements 17/37
0% Branches 0/4
0% Functions 0/5
45.95% Lines 17/37

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 1001x 1x 1x 1x 1x 1x 1x 1x 1x       1x   1x 1x 1x     1x                                                                                                                       1x                                   1x 1x  
import glob from "glob";
import { promisify } from "util";
import uuidv4 from "uuid/v4";
import sizeOf from "image-size";
import { stat } from "fs";
import parseAssetPath from "shared/lib/assets/parseAssetPath";
import { checksumFile } from "lib/helpers/checksum";
import { toValidSymbol } from "shared/lib/helpers/symbols";
import {
  SpriteResource,
  SpriteResourceAsset,
} from "shared/lib/resources/types";
import { getAssetResource } from "./assets";
 
const globAsync = promisify(glob);
const sizeOfAsync = promisify(sizeOf);
const statAsync = promisify(stat);
 
const loadSpriteData =
  (projectRoot: string) =>
  async (filename: string): Promise<SpriteResourceAsset | null> => {
    const { file, plugin } = parseAssetPath(filename, projectRoot, "sprites");
 
    const resource = await getAssetResource(SpriteResource, filename);
 
    try {
      const size = await sizeOfAsync(filename);
      Iif (!size || !size.width || !size.height) {
        return null;
      }
      const fileStat = await statAsync(filename, { bigint: true });
      const inode = fileStat.ino.toString();
      const checksum = await checksumFile(filename);
      const name = file.replace(/.png/i, "");
      return {
        id: uuidv4(),
        plugin,
        name,
        symbol: toValidSymbol(`sprite_${name}`),
        states: [
          {
            id: uuidv4(),
            name: "",
            animationType: "multi_movement",
            flipLeft: true,
            animations: Array.from(Array(8)).map(() => ({
              id: uuidv4(),
              frames: [
                {
                  id: uuidv4(),
                  tiles: [],
                },
              ],
            })),
          },
        ],
        numTiles: 0,
        canvasWidth: 32,
        canvasHeight: 32,
        boundsX: 0,
        boundsY: 0,
        boundsWidth: 16,
        boundsHeight: 16,
        animSpeed: 15,
        _v: Date.now(),
        _resourceType: "sprite",
        ...resource,
        filename: file,
        width: size.width,
        height: size.height,
        inode,
        checksum,
      };
    } catch (e) {
      console.error(e);
      return null;
    }
  };
 
const loadAllSpriteData = async (projectRoot: string) => {
  const spritePaths = await globAsync(
    `${projectRoot}/assets/sprites/**/@(*.png|*.PNG)`
  );
  const pluginPaths = await globAsync(
    `${projectRoot}/plugins/*/**/sprites/**/@(*.png|*.PNG)`
  );
  const spriteData = (
    await Promise.all(
      ([] as Promise<SpriteResourceAsset | null>[]).concat(
        spritePaths.map(loadSpriteData(projectRoot)),
        pluginPaths.map(loadSpriteData(projectRoot))
      )
    )
  ).filter((i) => i) as SpriteResourceAsset[];
  return spriteData;
};
 
export default loadAllSpriteData;
export { loadSpriteData };