All files / src/components/editors/script ScriptUses.worker.ts

0% Statements 0/47
0% Branches 0/17
0% Functions 0/5
0% Lines 0/46

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                                                                                                                                                                                                                                                                                                                                                                                                 
import {
  actorName,
  customEventName,
  sceneName,
  triggerName,
} from "shared/lib/entities/entitiesHelpers";
import {
  ActorNormalized,
  ActorPrefabNormalized,
  CustomEventNormalized,
  SceneNormalized,
  ScriptEventNormalized,
  TriggerNormalized,
  TriggerPrefabNormalized,
} from "shared/lib/entities/entitiesTypes";
import { L10NLookup, setL10NData } from "shared/lib/lang/l10n";
import {
  walkNormalizedCustomEventScripts,
  walkNormalizedScenesScripts,
} from "shared/lib/scripts/walk";
 
export type ScriptUse = {
  id: string;
  name: string;
  event: ScriptEventNormalized;
} & (
  | {
      type: "scene";
      sceneId: string;
      scene: SceneNormalized;
      sceneIndex: number;
    }
  | {
      type: "actor";
      actor: ActorNormalized;
      actorIndex: number;
      sceneId: string;
      scene: SceneNormalized;
      sceneIndex: number;
    }
  | {
      type: "trigger";
      trigger: TriggerNormalized;
      triggerIndex: number;
      sceneId: string;
      scene: SceneNormalized;
      sceneIndex: number;
    }
  | {
      type: "custom";
      customEvent: CustomEventNormalized;
      customEventIndex: number;
    }
);
 
export interface ScriptUseResult {
  id: string;
  uses: ScriptUse[];
}
 
// eslint-disable-next-line no-restricted-globals
const workerCtx: Worker = self as unknown as Worker;
 
workerCtx.onmessage = async (evt) => {
  const id = evt.data.id;
  const scriptId: string = evt.data.scriptId;
  const scenes: SceneNormalized[] = evt.data.scenes;
  const scriptEventsLookup: Record<string, ScriptEventNormalized> =
    evt.data.scriptEventsLookup;
  const actorsLookup: Record<string, ActorNormalized> = evt.data.actorsLookup;
  const triggersLookup: Record<string, TriggerNormalized> =
    evt.data.triggersLookup;
  const actorPrefabsLookup: Record<string, ActorPrefabNormalized> =
    evt.data.actorPrefabsLookup;
  const triggerPrefabsLookup: Record<string, TriggerPrefabNormalized> =
    evt.data.triggerPrefabsLookup;
  const customEventsLookup: Record<string, CustomEventNormalized> =
    evt.data.customEventsLookup;
  const l10NData: L10NLookup = evt.data.l10NData;
 
  setL10NData(l10NData);
 
  const uses: ScriptUse[] = [];
  const useLookup: Record<string, boolean> = {};
 
  walkNormalizedScenesScripts(
    scenes,
    scriptEventsLookup,
    actorsLookup,
    triggersLookup,
    actorPrefabsLookup,
    triggerPrefabsLookup,
    undefined,
    (scriptEvent, scene, actor, trigger) => {
      Iif (
        scriptEvent.command !== "EVENT_CALL_CUSTOM_EVENT" ||
        !scriptEvent.args ||
        scriptEvent.args.customEventId !== scriptId
      ) {
        return;
      }
 
      Iif (!useLookup[scene.id]) {
        const sceneIndex = scenes.indexOf(scene);
        uses.push({
          id: scene.id,
          name: sceneName(scene, sceneIndex),
          event: scriptEvent,
          type: "scene",
          scene,
          sceneIndex,
          sceneId: scene.id,
        });
        useLookup[scene.id] = true;
      }
 
      Iif (actor && !useLookup[actor.id]) {
        const sceneIndex = scenes.indexOf(scene);
        const actorIndex = scene.actors.indexOf(actor.id);
        uses.push({
          id: actor.id,
          name: actorName(actor, actorIndex),
          event: scriptEvent,
          type: "actor",
          actor,
          actorIndex,
          scene,
          sceneIndex,
          sceneId: scene.id,
        });
        useLookup[actor.id] = true;
      }
 
      Iif (trigger && !useLookup[trigger.id]) {
        const sceneIndex = scenes.indexOf(scene);
        const triggerIndex = scene.triggers.indexOf(trigger.id);
        uses.push({
          id: trigger.id,
          name: triggerName(trigger, triggerIndex),
          event: scriptEvent,
          type: "trigger",
          trigger,
          triggerIndex,
          scene,
          sceneIndex,
          sceneId: scene.id,
        });
        useLookup[trigger.id] = true;
      }
    }
  );
 
  Object.values(customEventsLookup).forEach((customEvent, customEventIndex) => {
    Iif (!customEvent) return;
    walkNormalizedCustomEventScripts(
      customEvent,
      scriptEventsLookup,
      undefined,
      (scriptEvent: ScriptEventNormalized) => {
        Iif (
          scriptEvent.command !== "EVENT_CALL_CUSTOM_EVENT" ||
          !scriptEvent.args ||
          scriptEvent.args.customEventId !== scriptId
        ) {
          return;
        }
 
        Iif (!useLookup[customEvent.id]) {
          uses.push({
            id: customEvent.id,
            name: customEventName(customEvent, customEventIndex),
            event: scriptEvent,
            type: "custom",
            customEvent,
            customEventIndex: customEventIndex,
          });
          useLookup[customEvent.id] = true;
        }
      }
    );
  });
 
  workerCtx.postMessage({ id, uses } as ScriptUseResult);
};
 
// -----------------------------------------------------------------
 
export default class W extends Worker {
  constructor() {
    super("");
  }
}