All files / src/lib/compiler/precompile determineUsedAssets.ts

88.14% Statements 52/59
65.96% Branches 31/47
90% Functions 9/10
87.93% Lines 51/58

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  2x 2x               2x 2x   2x     2x                 5x 5x 5x   5x 5x 5x     5x 10x 11x 11x 7x   11x     5x                           5x   5x   5x 8x 1x       5x         3x 3x 1x 3x 1x         5x 4x 4x       5x                   11x 1x 1x 1x 1x       11x         11x 11x 14x   14x   14x   7x 7x   1x 1x 1x                 5x            
import type { Reference } from "components/forms/ReferencesSelect";
import { MAX_NESTED_SCRIPT_DEPTH } from "consts";
import { eventHasArg } from "lib/helpers/eventSystem";
import type {
  CustomEvent,
  FontData,
  // Scene,
  SoundData,
  Variable,
} from "shared/lib/entities/entitiesTypes";
import { EVENT_SOUND_PLAY_EFFECT } from "consts";
import { walkScenesScripts } from "shared/lib/scripts/walk";
import { ScriptEventHandlers } from "lib/project/loadScriptEventHandlers";
import keyBy from "lodash/keyBy";
import { ProjectResources } from "shared/lib/resources/types";
 
export const determineUsedAssets = ({
  projectData,
  customEventsLookup,
  scriptEventHandlers,
}: {
  projectData: ProjectResources;
  customEventsLookup: Record<string, CustomEvent>;
  scriptEventHandlers: ScriptEventHandlers;
}) => {
  const variablesLookup = keyBy(projectData.variables.variables, "id");
  const soundsLookup = keyBy(projectData.sounds, "id");
  const fontsLookup = keyBy(projectData.fonts, "id");
 
  const usedVariablesLookup: Record<string, Variable> = {};
  const usedSoundsLookup: Record<string, SoundData> = {};
  const usedFontsLookup: Record<string, FontData> = {};
 
  const addAssetById =
    <T>(assetLookup: Record<string, T>, usedLookup: Record<string, T>) =>
    (id: string) => {
      const asset = assetLookup[id];
      if (asset && !usedLookup[id]) {
        usedLookup[id] = asset;
      }
      return !!asset;
    };
 
  const addVariableById = (id: string) => {
    Iif (!usedVariablesLookup[id]) {
      const variable = variablesLookup[id];
      if (variable) {
        usedVariablesLookup[id] = variable;
      } else {
        usedVariablesLookup[id] = {
          id,
          name: `VAR_${id}`,
          symbol: `VAR_${id}`,
        };
      }
    }
  };
  const addSoundById = addAssetById(soundsLookup, usedSoundsLookup);
 
  const addFontById = addAssetById(fontsLookup, usedFontsLookup);
 
  const addFontsFromString = (s: string) => {
    (s.match(/(!F:[0-9a-f-]+!)/g) || [])
      .map((id) => id.substring(3).replace(/!$/, ""))
      .forEach(addFontById);
  };
 
  const addReferences = (
    references: Reference[],
    filterType: string,
    addFn: (id: string) => void
  ) => {
    const referencedIds = references
      .filter((ref) => ref.type === filterType)
      .map((ref) => ref.id);
    for (const id of referencedIds) {
      addFn(id);
    }
  };
 
  // Add default font
  if (!addFontById(projectData.settings.defaultFontId)) {
    if (projectData.fonts.length > 0) {
      addFontById(projectData.fonts[0].id);
    }
  }
 
  walkScenesScripts(
    projectData.scenes,
    {
      customEvents: {
        lookup: customEventsLookup,
        maxDepth: MAX_NESTED_SCRIPT_DEPTH,
      },
    },
    (cmd) => {
      // Add Referenced Assets
      if (eventHasArg(cmd, "references") && cmd.args?.references) {
        const references = cmd.args?.references as Reference[];
        addReferences(references, "variable", addVariableById);
        addReferences(references, "sound", addSoundById);
        addReferences(references, "font", addFontById);
      }
 
      // Sounds
      Iif (cmd.command === EVENT_SOUND_PLAY_EFFECT) {
        const type = (cmd.args?.type as string) ?? "";
        addSoundById(type);
      }
 
      if (cmd.args) {
        for (const key in cmd.args) {
          const value = cmd.args[key];
          const fieldType =
            scriptEventHandlers[cmd.command]?.fieldsLookup[key]?.type;
 
          if (fieldType === "textarea" && typeof value === "string") {
            // String text fields
            addFontsFromString(value);
          } else if (fieldType === "textarea" && Array.isArray(value)) {
            // Multi value text fields
            for (const row of value) {
              if (typeof row === "string") {
                addFontsFromString(row);
              }
            }
          }
        }
      }
    }
  );
 
  return {
    referencedVariables: Object.values(usedVariablesLookup) as Variable[],
    referencedSounds: Object.values(usedSoundsLookup) as SoundData[],
    referencedFonts: Object.values(usedFontsLookup) as FontData[],
  };
};