All files / src/shared/lib/compiler buildUsageItems.ts

64.7% Statements 66/102
45.56% Branches 36/79
60.86% Functions 14/23
63.44% Lines 59/93

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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259          4x           4x   4x                                                                                       4x 30x   8x   4x   4x   4x   6x   4x       4x 2x                                       2x       4x 13x   13x 13x 13x 1x                                         4x 3x   4x                 7x 7x 7x     7x 12x     7x 13x 13x                 13x 1x 1x 1x   1x     1x       12x               12x 12x       24x           7x   5x 5x                         7x 1x           7x 7x 1x 7x   7x   7x 4x 7x     7x     3x                   7x     4x   2x                                
import type {
  BuildUsageSource,
  BuildUsageScript,
} from "lib/compiler/buildUsage";
import type { CompiledScriptSource } from "lib/compiler/compileData";
import {
  actorName,
  customEventName,
  sceneName,
  triggerName,
} from "shared/lib/entities/entitiesHelpers";
import l10n from "shared/lib/lang/l10n";
import type { ProjectResources } from "shared/lib/resources/types";
import {
  buildAssetUsageRows,
  type BuildAssetSourceType,
  type BuildAssetType,
} from "./buildAssetUsage";
 
type NamedEntity = { id: string; name: string };
type SymbolEntity = NamedEntity & { symbol: string };
 
export type BuildUsageItemEntities = {
  scenes: Array<SymbolEntity & { actors: string[]; triggers: string[] }>;
  actors: Array<NamedEntity & { collisionGroup?: string }>;
  triggers: NamedEntity[];
  customEvents: NamedEntity[];
  sprites: SymbolEntity[];
  backgrounds: SymbolEntity[];
  music: SymbolEntity[];
  sounds: SymbolEntity[];
};
 
export type BuildUsageItemType = "script" | BuildAssetType;
 
type BuildUsageItemBase = {
  key: string;
  type: BuildUsageItemType;
  name: string;
  symbol: string;
  sourceFile: string;
  size: number;
};
 
export type BuildUsageScriptItem = BuildUsageItemBase & {
  type: "script";
  sources: CompiledScriptSource[];
  sourceLabels: string[];
};
 
export type BuildUsageAssetItem = BuildUsageItemBase & {
  type: BuildAssetType;
  sourceType: BuildAssetSourceType;
};
 
export type BuildUsageItem = BuildUsageScriptItem | BuildUsageAssetItem;
 
export const buildUsageItemTypeLabel = (type: BuildUsageItemType) => {
  switch (type) {
    case "script":
      return l10n("FIELD_SCRIPT");
    case "scene":
      return l10n("FIELD_SCENE");
    case "sprite":
      return l10n("FIELD_SPRITE");
    case "background":
      return l10n("FIELD_BACKGROUND");
    case "music":
      return l10n("FIELD_MUSIC");
    case "sound":
      return l10n("FIELD_SOUND");
  }
};
 
export const buildUsageAssetSourceTypeLabel = (type: BuildAssetSourceType) => {
  switch (type) {
    case "actors":
      return l10n("FIELD_ACTORS");
    case "collisions":
      return l10n("FIELD_COLLISIONS");
    case "triggers":
      return l10n("FIELD_TRIGGERS");
    case "sprites":
      return l10n("FIELD_SPRITES");
    case "projectiles":
      return l10n("FIELD_PROJECTILES");
    case "tileset":
      return l10n("FIELD_TILESET");
    case "cgbTileset":
      return l10n("FIELD_CGB_TILESET");
    case "tilemap":
      return l10n("FIELD_TILEMAP");
    case "tilemapAttr":
      return l10n("FIELD_TILEMAP_ATTR");
    case "data":
      return undefined;
  }
};
 
const scriptKeyLabel = (source: CompiledScriptSource) => {
  switch (source.scriptKey) {
    case "script":
      Iif (source.entityType === "scene") return l10n("SIDEBAR_ON_INIT");
      Iif (source.entityType === "trigger") return l10n("SIDEBAR_ON_ENTER");
      if (source.entityType === "customEvent") return l10n("FIELD_SCRIPT");
      return l10n("SIDEBAR_ON_INTERACT");
    case "startScript":
      return l10n("SIDEBAR_ON_INIT");
    case "updateScript":
      return l10n("SIDEBAR_ON_UPDATE");
    case "leaveScript":
      return l10n("SIDEBAR_ON_LEAVE");
    case "playerHit1Script":
    case "hit1Script":
      return l10n("SIDEBAR_ON_PLAYER_HIT");
    case "playerHit2Script":
    case "hit2Script":
      return l10n("FIELD_COLLISION_GROUP_N", { n: 2 });
    case "playerHit3Script":
    case "hit3Script":
      return l10n("FIELD_COLLISION_GROUP_N", { n: 3 });
    default:
      return source.scriptKey;
  }
};
 
const sourceFileBasename = (sourceFile: string) =>
  sourceFile.split(/[\\/]/).pop() ?? sourceFile;
 
export const buildUsageItems = ({
  scripts,
  sources,
  entities,
}: {
  scripts: BuildUsageScript[];
  sources: BuildUsageSource[];
  entities: BuildUsageItemEntities;
}): BuildUsageItem[] => {
  const sceneLookup = new Map(entities.scenes.map((item) => [item.id, item]));
  const actorLookup = new Map(entities.actors.map((item) => [item.id, item]));
  const triggerLookup = new Map(
    entities.triggers.map((item) => [item.id, item]),
  );
  const customEventLookup = new Map(
    entities.customEvents.map((item) => [item.id, item]),
  );
 
  const sourceLabel = (source: CompiledScriptSource) => {
    const label = scriptKeyLabel(source);
    Iif (source.entityType === "scene") {
      const entity = sceneLookup.get(source.entityId);
      return entity
        ? `${sceneName(
            entity,
            entities.scenes.findIndex((item) => item.id === source.entityId),
          )} (${label})`
        : label;
    }
    if (source.entityType === "actor") {
      const entity = actorLookup.get(source.entityId);
      const scene = sceneLookup.get(source.sceneId);
      const index = scene?.actors.indexOf(source.entityId) ?? -1;
      const sourceScriptLabel =
        source.scriptKey === "script" && entity?.collisionGroup
          ? l10n("SIDEBAR_ON_HIT")
          : label;
      return entity
        ? `${actorName(entity, Math.max(0, index))} (${sourceScriptLabel})`
        : label;
    }
    Iif (source.entityType === "trigger") {
      const entity = triggerLookup.get(source.entityId);
      const scene = sceneLookup.get(source.sceneId);
      const index = scene?.triggers.indexOf(source.entityId) ?? -1;
      return entity
        ? `${triggerName(entity, Math.max(0, index))} (${label})`
        : label;
    }
    const entity = customEventLookup.get(source.entityId);
    return entity
      ? `${customEventName(
          entity,
          entities.customEvents.findIndex(
            (item) => item.id === source.entityId,
          ),
        )} (${label})`
      : label;
  };
 
  const scriptItems: BuildUsageScriptItem[] = scripts.map(
    ({ symbol, size, sources }) => {
      const sourceLabels = sources.map(sourceLabel);
      return {
        key: `script:${symbol}`,
        type: "script",
        name: sourceLabels.join(", "),
        symbol,
        sourceFile: `${symbol}.s`,
        size,
        sources,
        sourceLabels,
      };
    },
  );
 
  const knownSymbols = {
    scenes: new Set(entities.scenes.map((scene) => scene.symbol)),
    sprites: new Set(entities.sprites.map((sprite) => sprite.symbol)),
    backgrounds: new Set(
      entities.backgrounds.map((background) => background.symbol),
    ),
  };
  const nameLookup = new Map<string, string>();
  for (const scene of entities.scenes)
    nameLookup.set(`scene:${scene.symbol}`, scene.name);
  for (const sprite of entities.sprites)
    nameLookup.set(`sprite:${sprite.symbol}`, sprite.name);
  for (const background of entities.backgrounds)
    nameLookup.set(`background:${background.symbol}`, background.name);
  for (const track of entities.music)
    nameLookup.set(`music:${track.symbol}`, track.name);
  for (const sound of entities.sounds)
    nameLookup.set(`sound:${sound.symbol}`, sound.name);
 
  const assetItems: BuildUsageAssetItem[] = buildAssetUsageRows(
    sources,
    knownSymbols,
  ).map((asset) => ({
    key: `${asset.type}:${asset.symbol}:${asset.sourceFile}`,
    type: asset.type,
    name: nameLookup.get(`${asset.type}:${asset.symbol}`) ?? asset.symbol,
    symbol: asset.symbol,
    sourceType: asset.sourceType,
    sourceFile: sourceFileBasename(asset.sourceFile),
    size: asset.rom,
  }));
 
  return [...scriptItems, ...assetItems];
};
 
export const buildUsageItemEntitiesFromProject = (
  project: ProjectResources,
): BuildUsageItemEntities => ({
  scenes: project.scenes.map((scene) => ({
    id: scene.id,
    name: scene.name,
    symbol: scene.symbol,
    actors: scene.actors.map((actor) => actor.id),
    triggers: scene.triggers.map((trigger) => trigger.id),
  })),
  actors: project.scenes.flatMap((scene) => scene.actors),
  triggers: project.scenes.flatMap((scene) => scene.triggers),
  customEvents: project.scripts,
  sprites: project.sprites,
  backgrounds: project.backgrounds,
  music: project.music,
  sounds: project.sounds,
});