Skip to content

Commit

Permalink
Add migration to store switch branch conditions as constvalues
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismaltby committed Sep 15, 2024
1 parent df79286 commit 5f93128
Show file tree
Hide file tree
Showing 19 changed files with 1,226 additions and 157 deletions.
2 changes: 1 addition & 1 deletion src/lib/compiler/compileEntityEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ScriptBuilder, {
} from "./scriptBuilder";
import { PrecompiledScene } from "./generateGBVMData";
import { ScriptEventHandlers } from "lib/project/loadScriptEventHandlers";
import { LATEST_PROJECT_VERSION } from "lib/project/migrateProjectResources";
import { LATEST_PROJECT_VERSION } from "lib/project/migration/migrateProjectResources";

const STRING_NOT_FOUND = "STRING_NOT_FOUND";
const VARIABLE_NOT_FOUND = "VARIABLE_NOT_FOUND";
Expand Down
4 changes: 2 additions & 2 deletions src/lib/project/loadProjectData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ import {
isProjectMetadataResource,
} from "shared/lib/resources/types";
import { defaultPalettes } from "consts";
import { migrateLegacyProject } from "./migrateLegacyProject";
import { migrateLegacyProject } from "./migration/legacy/migrateLegacyProject";
import { loadProjectResources } from "./loadProjectResources";
import { readJson } from "lib/helpers/fs/readJson";
import type { ProjectData } from "store/features/project/projectActions";
import { migrateProjectResources } from "./migrateProjectResources";
import { migrateProjectResources } from "./migration/migrateProjectResources";

export interface LoadProjectResult {
resources: CompressedProjectResources;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/project/migrateWarning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import settings from "electron-settings";
import semverValid from "semver/functions/valid";
import semverGt from "semver/functions/gt";
import l10n from "shared/lib/lang/l10n";
import { LATEST_PROJECT_VERSION } from "./migrateProjectResources";
import { LATEST_PROJECT_VERSION } from "./migration/migrateProjectResources";

export const needsUpdate = (currentVersion: string) => {
if (semverValid(currentVersion) && semverValid(LATEST_PROJECT_VERSION)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import { ScriptEvent } from "shared/lib/entities/entitiesTypes";
import { CompressedProjectResources } from "shared/lib/resources/types";
import {
mapActorsScript,
mapCustomScriptsScript,
mapScenesScript,
mapActorsScript,
mapTriggersScript,
mapCustomScriptsScript,
} from "shared/lib/scripts/walk";

export const LATEST_PROJECT_VERSION = "4.2.0";
export const LATEST_PROJECT_MINOR_VERSION = "1";

type ScriptEventMigrationFn = (scriptEvent: ScriptEvent) => ScriptEvent;
type ProjectResourcesMigrationFn = (
export type ScriptEventMigrationFn = (scriptEvent: ScriptEvent) => ScriptEvent;
export type ProjectResourcesMigrationFn = (
resources: CompressedProjectResources
) => CompressedProjectResources;

type ProjectResourcesMigration = {
export type ProjectResourcesMigration = {
from: { version: string; release: string };
to: { version: string; release: string };
migrationFn: ProjectResourcesMigrationFn;
};

const applyProjectResourcesMigration = (
export const applyProjectResourcesMigration = (
resources: CompressedProjectResources,
migration: ProjectResourcesMigration
): CompressedProjectResources => {
Expand All @@ -40,7 +37,7 @@ const applyProjectResourcesMigration = (
};
};

const migrateEvents = (
export const migrateEvents = (
resources: CompressedProjectResources,
migrateFn: ScriptEventMigrationFn
): CompressedProjectResources => {
Expand All @@ -53,7 +50,32 @@ const migrateEvents = (
};
};

const isProjectVersion = (
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
Expand All @@ -63,39 +85,3 @@ const isProjectVersion = (
resources.metadata._release === release
);
};

const migrateFrom410r1To420r1Event: ScriptEventMigrationFn = (scriptEvent) => {
if (scriptEvent.args && scriptEvent.command === "EVENT_SWITCH") {
for (let i = 0; i < 16; i++) {
const key = `value${i}`;
const defaultValue = i + 1;
const storedValue = scriptEvent.args[key];
const value =
typeof storedValue === "number" ? storedValue : defaultValue;
// Convert to constvalue
scriptEvent.args[key] = {
type: "number",
value,
};
}
}
return scriptEvent;
};

const migrations: ProjectResourcesMigration[] = [
{
from: { version: "4.1.0", release: "1" },
to: { version: "4.2.0", release: "1" },
migrationFn: (resources) => {
return migrateEvents(resources, migrateFrom410r1To420r1Event);
},
},
];

export const migrateProjectResources = async (
resources: CompressedProjectResources
): Promise<CompressedProjectResources> => {
return migrations.reduce((migratedResources, migration) => {
return applyProjectResourcesMigration(migratedResources, migration);
}, resources);
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Static, TSchema } from "@sinclair/typebox";
import migrateProject from "lib/project/migrateProject";
import migrateProject from "./migrateLegacyProjectVersions";
import identity from "lodash/identity";
import { BackgroundData, Scene } from "shared/lib/entities/entitiesTypes";
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ import { ensureNumber } from "shared/types";

const indexById = <T>(arr: T[]) => keyBy(arr, "id");

export const LATEST_PROJECT_VERSION = "4.1.0";
export const LATEST_PROJECT_MINOR_VERSION = "1";
export const LATEST_LEGACY_PROJECT_VERSION = "4.1.0";
export const LATEST_LEGACY_PROJECT_MINOR_VERSION = "1";

const ensureProjectAssetSync = (
relativePath: string,
Expand Down Expand Up @@ -2754,8 +2754,8 @@ const migrateProject = (
}
}

data._version = LATEST_PROJECT_VERSION;
data._release = LATEST_PROJECT_MINOR_VERSION;
data._version = LATEST_LEGACY_PROJECT_VERSION;
data._release = LATEST_LEGACY_PROJECT_MINOR_VERSION;

return data;
};
Expand Down
24 changes: 24 additions & 0 deletions src/lib/project/migration/migrateProjectResources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { CompressedProjectResources } from "shared/lib/resources/types";
import {
ProjectResourcesMigration,
applyProjectResourcesMigration,
} from "./helpers";
import { migrate410r1To420r1 } from "./versions/410to420";

const migrations: ProjectResourcesMigration[] = [
// 4.1.0 to 4.2.0
migrate410r1To420r1,
];

const lastMigration = migrations[migrations.length - 1];

export const LATEST_PROJECT_VERSION = lastMigration.to.version;
export const LATEST_PROJECT_MINOR_VERSION = lastMigration.to.release;

export const migrateProjectResources = async (
resources: CompressedProjectResources
): Promise<CompressedProjectResources> => {
return migrations.reduce((migratedResources, migration) => {
return applyProjectResourcesMigration(migratedResources, migration);
}, resources);
};
36 changes: 36 additions & 0 deletions src/lib/project/migration/versions/410to420.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
ScriptEventMigrationFn,
ProjectResourcesMigration,
createScriptEventsMigrator,
} from "lib/project/migration/helpers";

export const migrateFrom410r1To420r1Event: ScriptEventMigrationFn = (
scriptEvent
) => {
if (scriptEvent.args && scriptEvent.command === "EVENT_SWITCH") {
const args: Record<string, unknown> = { ...scriptEvent.args };
for (let i = 0; i < 16; i++) {
const key = `value${i}`;
const defaultValue = i + 1;
const storedValue = args[key];
const value =
typeof storedValue === "number" ? storedValue : defaultValue;
// Convert to constvalue
args[key] = {
type: "number",
value,
};
}
return {
...scriptEvent,
args,
};
}
return scriptEvent;
};

export const migrate410r1To420r1: ProjectResourcesMigration = {
from: { version: "4.1.0", release: "1" },
to: { version: "4.2.0", release: "1" },
migrationFn: createScriptEventsMigrator(migrateFrom410r1To420r1Event),
};
30 changes: 30 additions & 0 deletions test/migrate/data/migrationTestProject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { CompressedProjectResources } from "shared/lib/resources/types";
import {
dummyCompressedProjectResources,
dummyScriptResource,
} from "../../dummydata";

export const migrationTestProject: CompressedProjectResources = {
...dummyCompressedProjectResources,
scripts: [
{
...dummyScriptResource,
script: [
{
id: "event1",
command: "EVENT_SWITCH",
args: {
value1: 1,
value2: 10,
value3: 100,
},
},
],
},
],
metadata: {
...dummyCompressedProjectResources.metadata,
_version: "4.1.0",
_release: "1",
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
migrateFrom1To110Scenes,
migrateFrom1To110Collisions,
migrateFrom1To110Actors,
} from "../../src/lib/project/migrateProject";
} from "../../../src/lib/project/migration/legacy/migrateLegacyProjectVersions";

test("should migrate 1 to 1.1.0 to add scene width & height values", () => {
const input = {
Expand Down
Loading

0 comments on commit 5f93128

Please sign in to comment.