All files / src/shared/lib/resources compression.ts

100% Statements 62/62
100% Branches 15/15
100% Functions 8/8
100% Lines 54/54

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                  34x 7x 1x   6x 6x 6x   6x 63x 20x 3x 17x 11x   20x 20x 20x   63x   6x 2x 4x 4x     6x     34x 26x 26x 26x   279x 279x 279x 279x 275x   23x 23x     252x 252x 252x   5x   247x 247x       4x     270x 2634x     17x     34x     5x           34x     4x           34x     5x                 34x     1x           34x     1x           34x     25x            
import {
  BackgroundResource,
  CompressedBackgroundResource,
  CompressedProjectResources,
  CompressedSceneResourceWithChildren,
  ProjectResources,
  SceneResource,
} from "shared/lib/resources/types";
 
export const compress8bitNumberArray = (arr: number[] | undefined): string => {
  if (!arr) {
    return "";
  }
  let lastValue = -1;
  let output = "";
  let count = 0;
 
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] !== lastValue) {
      if (count === 1) {
        output += "!";
      } else if (count > 0) {
        output += `${count.toString(16)}+`;
      }
      count = 0;
      lastValue = arr[i];
      output += (lastValue % 256).toString(16).padStart(2, "0");
    }
    count++;
  }
  if (count === 1) {
    output += "!";
  } else if (count > 0) {
    output += `${count.toString(16)}+`;
  }
 
  return output;
};
 
export const decompress8bitNumberString = (str: string): number[] => {
  const arr: number[] = [];
  let i = 0;
  while (i < str.length) {
    // Read the value
    const value = parseInt(str.slice(i, i + 2), 16);
    i += 2;
    let count = 1;
    if (i < str.length) {
      if (str[i] === "!") {
        // Single occurrence
        count = 1;
        i++;
      } else {
        // Read the count
        const countStart = i;
        const countEnd = str.indexOf("+", countStart);
        if (countStart === countEnd || countEnd === -1) {
          // No count or no end of count marker found - string was invalid
          return [];
        }
        count = parseInt(str.slice(countStart, countEnd), 16);
        i = countEnd + 1;
      }
    } else {
      // value was missing count / markers - string was invalid
      return [];
    }
    // Add the value `count` times to the array
    for (let j = 0; j < count; j++) {
      arr.push(value);
    }
  }
  return arr;
};
 
const decompressSceneResource = (
  scene: CompressedSceneResourceWithChildren,
): SceneResource => {
  return {
    ...scene,
    collisions: decompress8bitNumberString(scene.collisions),
  };
};
 
const decompressBackgroundResource = (
  background: CompressedBackgroundResource,
): BackgroundResource => {
  return {
    ...background,
    tileColors: decompress8bitNumberString(background.tileColors),
  };
};
 
export const decompressProjectResources = (
  compressedResources: CompressedProjectResources,
): ProjectResources => {
  return {
    ...compressedResources,
    scenes: compressedResources.scenes.map(decompressSceneResource),
    backgrounds: compressedResources.backgrounds.map(
      decompressBackgroundResource,
    ),
  };
};
 
export const compressSceneResource = (
  scene: SceneResource,
): CompressedSceneResourceWithChildren => {
  return {
    ...scene,
    collisions: compress8bitNumberArray(scene.collisions),
  };
};
 
export const compressBackgroundResource = (
  background: BackgroundResource,
): CompressedBackgroundResource => {
  return {
    ...background,
    tileColors: compress8bitNumberArray(background.tileColors),
  };
};
 
export const compressProjectResources = (
  resources: ProjectResources,
): CompressedProjectResources => {
  return {
    ...resources,
    scenes: resources.scenes.map(compressSceneResource),
    backgrounds: resources.backgrounds.map(compressBackgroundResource),
  };
};