ds4/src/migration/008.js

61 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-11-21 03:00:46 +01:00
// SPDX-FileCopyrightText: 2022 Johannes Loher
//
// SPDX-License-Identifier: MIT
import {
getSceneMigrator,
migrateCollection,
2022-11-21 03:00:46 +01:00
migrateCompendiums,
getCompendiumMigrator,
getActorMigrator,
getItemMigrator,
} from "./migrationHelpers.js";
2022-11-21 03:00:46 +01:00
/** @type {import("./migration.js").Migration["migrate"]} */
2022-11-21 03:00:46 +01:00
async function migrate() {
await migrateCollection(game.items, migrateItem);
await migrateCollection(game.actors, migrateActor);
await migrateCollection(game.scenes, migrateScene);
2022-11-21 03:00:46 +01:00
await migrateCompendiums(migrateCompendium);
}
/** @type {import('./migrationHelpers.js').Migrator<ActiveEffect>} */
async function migrateActiveEffect(activeEffect) {
const data = activeEffect.toObject();
2022-11-21 03:00:46 +01:00
let hasUpdates = false;
2022-11-21 03:00:46 +01:00
if ("changes" in data) {
for (const change of data.changes) {
const newValue = change.value.replaceAll(/@data\./g, "@system.");
if (newValue !== change.value) {
hasUpdates = true;
change.value = newValue;
}
}
}
/** @type {string | undefined} */
const condition = data.flags?.ds4?.itemEffectConfig?.condition;
if (condition !== undefined) {
const newCondition = condition.replaceAll(/(@actor|@item|@effect)\.data/g, "$1.system");
if (newCondition !== condition) {
hasUpdates = true;
data.flags.ds4.itemEffectConfig.condition = newCondition;
}
}
if (hasUpdates) {
await activeEffect.update(data);
2022-11-21 03:00:46 +01:00
}
}
const migrateItem = getItemMigrator(migrateActiveEffect);
const migrateActor = getActorMigrator(migrateItem, migrateActiveEffect);
const migrateScene = getSceneMigrator(migrateActor);
const migrateCompendium = getCompendiumMigrator({ migrateItem, migrateActor, migrateScene });
2022-11-21 03:00:46 +01:00
/** @type {import("./migration.js").Migration} */
2022-11-21 03:00:46 +01:00
export const migration = {
migrate,
migrateCompendium,
};