Skip to content

Commit

Permalink
Fix migrating Engine Field Update events
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismaltby committed May 15, 2024
1 parent 4f6f19a commit 44dc3fc
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 1 deletion.
53 changes: 52 additions & 1 deletion src/lib/project/migrateProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import {
const indexById = <T>(arr: T[]) => keyBy(arr, "id");

export const LATEST_PROJECT_VERSION = "4.0.0";
export const LATEST_PROJECT_MINOR_VERSION = "1";
export const LATEST_PROJECT_MINOR_VERSION = "2";

const ensureProjectAssetSync = (
relativePath: string,
Expand Down Expand Up @@ -2442,6 +2442,50 @@ const migrateFrom330r6To330r7Events = (data: ProjectData): ProjectData => {
};
};

/* Version 4.0.0 r2 handles migrating EVENT_ENGINE_FIELD_SET events from 3.3.0 correctly
*/
export const migrateFrom400r1To400r2Event = (
event: ScriptEvent
): ScriptEvent => {
const migrateMeta = generateMigrateMeta(event);

if (event.args && event.command === "EVENT_ENGINE_FIELD_SET") {
const currentValue = event.args.value;
if (
isScriptValue(currentValue) ||
!isUnionValue(currentValue) ||
currentValue.type === "variable" ||
typeof currentValue.value !== "number"
) {
return event;
}
return migrateMeta({
...event,
args: {
...event.args,
value: {
type: "number",
value: currentValue.value,
},
},
});
}
return event;
};

const migrateFrom400r1To400r2Events = (data: ProjectData): ProjectData => {
return {
...data,
scenes: mapScenesScript(data.scenes, migrateFrom400r1To400r2Event),
customEvents: (data.customEvents || []).map((customEvent) => {
return {
...customEvent,
script: mapScript(customEvent.script, migrateFrom400r1To400r2Event),
};
}),
};
};

const migrateProject = (
project: ProjectData,
projectRoot: string,
Expand Down Expand Up @@ -2634,6 +2678,13 @@ const migrateProject = (
}
}

if (version === "4.0.0") {
if (release === "1") {
data = migrateFrom400r1To400r2Events(data);
release = "2";
}
}

if (process.env.NODE_ENV !== "production") {
if (version === "2.0.0") {
data = migrateFrom120To200Collisions(data);
Expand Down
151 changes: 151 additions & 0 deletions test/migrate/migrate400releases.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { migrateFrom400r1To400r2Event } from "../../src/lib/project/migrateProject";
import initElectronL10N from "../../src/lib/lang/initElectronL10N";

beforeAll(async () => {
await initElectronL10N();
});

test("should migrate 3.3.0 and below Engine Field Update slider values", () => {
const oldEvent = {
command: "EVENT_ENGINE_FIELD_SET",
args: {
engineFieldKey: "plat_walk_vel",
value: {
type: "slider",
value: 794,
},
},
};
expect(migrateFrom400r1To400r2Event(oldEvent)).toMatchObject({
command: "EVENT_ENGINE_FIELD_SET",
args: {
engineFieldKey: "plat_walk_vel",
value: {
type: "number",
value: 794,
},
},
});
});

test("should migrate 3.3.0 and below Engine Field Update select values", () => {
const oldEvent = {
command: "EVENT_ENGINE_FIELD_SET",
args: {
engineFieldKey: "topdown_grid",
value: {
type: "select",
value: 16,
},
},
};
expect(migrateFrom400r1To400r2Event(oldEvent)).toMatchObject({
command: "EVENT_ENGINE_FIELD_SET",
args: {
engineFieldKey: "topdown_grid",
value: {
type: "number",
value: 16,
},
},
});
});

test("should keep 3.3.0 and below Engine Field Update variable values unchanged", () => {
const oldEvent = {
command: "EVENT_ENGINE_FIELD_SET",
args: {
engineFieldKey: "fade_style",
value: {
type: "variable",
value: "L0",
},
},
};
expect(migrateFrom400r1To400r2Event(oldEvent)).toMatchObject({
command: "EVENT_ENGINE_FIELD_SET",
args: {
engineFieldKey: "fade_style",
value: {
type: "variable",
value: "L0",
},
},
});
});

test("shouldn't add value to 3.3.0 and below Engine Field Update variable values that have empty values", () => {
const oldEvent = {
command: "EVENT_ENGINE_FIELD_SET",
args: {
engineFieldKey: "plat_walk_vel",
},
};
expect(migrateFrom400r1To400r2Event(oldEvent)).toMatchObject({
command: "EVENT_ENGINE_FIELD_SET",
args: {
engineFieldKey: "plat_walk_vel",
},
});
expect(migrateFrom400r1To400r2Event(oldEvent).args.value).toBeUndefined();
});

test("should keep 3.3.0 and below Engine Field Update variable values that have invalid values", () => {
const oldEvent = {
command: "EVENT_ENGINE_FIELD_SET",
args: {
engineFieldKey: "plat_walk_vel",
value: {
type: "slider",
value: "",
},
},
};
expect(migrateFrom400r1To400r2Event(oldEvent)).toMatchObject({
command: "EVENT_ENGINE_FIELD_SET",
args: {
engineFieldKey: "plat_walk_vel",
value: {
type: "slider",
value: "",
},
},
});
});

test("should keep Engine Field Update script values", () => {
const oldEvent = {
command: "EVENT_ENGINE_FIELD_SET",
args: {
engineFieldKey: "plat_walk_vel",
value: {
type: "add",
valueA: {
type: "number",
value: 1792,
},
valueB: {
type: "number",
value: 55,
},
},
},
};
expect(migrateFrom400r1To400r2Event(oldEvent)).toMatchObject({
command: "EVENT_ENGINE_FIELD_SET",
args: {
engineFieldKey: "plat_walk_vel",
value: {
type: "add",
valueA: {
type: "number",
value: 1792,
},
valueB: {
type: "number",
value: 55,
},
},
},
});
});

0 comments on commit 44dc3fc

Please sign in to comment.