All files / src/lib/project/migration helpers.ts

100% Statements 36/36
100% Branches 3/3
100% Functions 16/16
100% Lines 31/31

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    12x                                       12x       14x     1x   13x                   12x     20x 20x 8x 14x     20x 8x 14x     20x     12x       20x 20x                         12x 12x 34x 13x   12x     11x 4x 8x         12x     1x 8x 16x         12x         18x          
import { ScriptEvent } from "shared/lib/entities/entitiesTypes";
import { CompressedProjectResources } from "shared/lib/resources/types";
import {
  mapScenesScript,
  mapActorsScript,
  mapTriggersScript,
  mapCustomScriptsScript,
  walkActorScripts,
  walkTriggerScripts,
} from "shared/lib/scripts/walk";
 
export type ScriptEventMigrationFn = (scriptEvent: ScriptEvent) => ScriptEvent;
export type ProjectResourcesMigrationFn = (
  resources: CompressedProjectResources,
) => CompressedProjectResources;
 
export type ProjectResourcesMigration = {
  from: { version: string; release: string };
  to: { version: string; release: string };
  migrationFn: ProjectResourcesMigrationFn;
};
 
export const applyProjectResourcesMigration = (
  resources: CompressedProjectResources,
  migration: ProjectResourcesMigration,
): CompressedProjectResources => {
  if (
    !isProjectVersion(migration.from.version, migration.from.release, resources)
  ) {
    return resources;
  }
  return {
    ...migration.migrationFn(resources),
    metadata: {
      ...resources.metadata,
      _version: migration.to.version,
      _release: migration.to.release,
    },
  };
};
 
const buildPrefabEventsLookup = (
  resources: CompressedProjectResources,
): Record<string, ScriptEvent> => {
  const prefabEventsLookup: Record<string, ScriptEvent> = {};
  resources.actorPrefabs.forEach((actorPrefab) => {
    walkActorScripts(actorPrefab, undefined, (e) => {
      prefabEventsLookup[e.id] = e;
    });
  });
  resources.triggerPrefabs.forEach((triggerPrefab) => {
    walkTriggerScripts(triggerPrefab, undefined, (e) => {
      prefabEventsLookup[e.id] = e;
    });
  });
  return prefabEventsLookup;
};
 
export const migrateEvents = (
  resources: CompressedProjectResources,
  migrateFn: ScriptEventMigrationFn,
): CompressedProjectResources => {
  const prefabEventsLookup = buildPrefabEventsLookup(resources);
  return {
    ...resources,
    scenes: mapScenesScript(
      resources.scenes,
      { includePrefabOverrides: true, prefabEventsLookup },
      migrateFn,
    ),
    actorPrefabs: mapActorsScript(resources.actorPrefabs, migrateFn),
    triggerPrefabs: mapTriggersScript(resources.triggerPrefabs, migrateFn),
    scripts: mapCustomScriptsScript(resources.scripts, migrateFn),
  };
};
 
export const createScriptEventsMigrator =
  (migrateFn: ScriptEventMigrationFn) =>
  (resources: CompressedProjectResources): CompressedProjectResources =>
    migrateEvents(resources, migrateFn);
 
export const pipeMigrationFns = (
  migrationFns: ProjectResourcesMigrationFn[],
): ProjectResourcesMigrationFn => {
  return (resources: CompressedProjectResources): CompressedProjectResources =>
    migrationFns.reduce(
      (currentResources, migrationFn) => migrationFn(currentResources),
      resources,
    );
};
 
export const pipeScriptEventMigrationFns = (
  scriptEventMigrationFns: ScriptEventMigrationFn[],
): ScriptEventMigrationFn => {
  return (scriptEvent: ScriptEvent): ScriptEvent =>
    scriptEventMigrationFns.reduce(
      (currentScriptEvent, migrationFn) => migrationFn(currentScriptEvent),
      scriptEvent,
    );
};
 
export const isProjectVersion = (
  version: string,
  release: string,
  resources: CompressedProjectResources,
): boolean => {
  return (
    resources.metadata._version === version &&
    resources.metadata._release === release
  );
};