diff --git a/src/lib/project/migrateProject.ts b/src/lib/project/migrateProject.ts index 6d3a4353a..25ddc077a 100644 --- a/src/lib/project/migrateProject.ts +++ b/src/lib/project/migrateProject.ts @@ -54,7 +54,7 @@ import { const indexById = (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, @@ -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, @@ -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); diff --git a/test/migrate/migrate400releases.test.js b/test/migrate/migrate400releases.test.js new file mode 100644 index 000000000..7bab8642a --- /dev/null +++ b/test/migrate/migrate400releases.test.js @@ -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, + }, + }, + }, + }); +});