ds4/src/module/migrations/002.ts

155 lines
5.6 KiB
TypeScript
Raw Normal View History

2021-06-26 22:02:00 +02:00
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
import logger from "../logger";
2021-02-21 03:40:54 +01:00
export async function migrate(): Promise<void> {
await migrateItems();
await migrateActors();
await migrateScenes();
await migrateCompendiums();
}
async function migrateItems() {
2021-06-30 05:00:23 +02:00
for (const item of game.items?.contents ?? []) {
2021-02-21 03:40:54 +01:00
try {
2021-06-30 05:00:23 +02:00
const updateData = getItemUpdateData(item.toObject());
2021-02-21 03:40:54 +01:00
if (updateData) {
logger.info(`Migrating Item entity ${item.name} (${item.id})`);
2021-02-21 03:40:54 +01:00
await item.update(updateData), { enforceTypes: false };
}
} catch (err) {
err.message = `Error during migration of Item entity ${item.name} (${item.id}), continuing anyways.`;
logger.error(err);
2021-02-21 03:40:54 +01:00
}
}
}
2021-06-30 05:00:23 +02:00
function getItemUpdateData(itemData: foundry.data.ItemData["_source"]) {
2021-02-21 03:40:54 +01:00
if (!["equipment", "trinket"].includes(itemData.type ?? "")) return undefined;
2021-06-30 05:00:23 +02:00
return { type: itemData.type === "equipment" ? ("loot" as const) : ("equipment" as const) };
2021-02-21 03:40:54 +01:00
}
async function migrateActors() {
2021-06-30 05:00:23 +02:00
for (const actor of game.actors?.contents ?? []) {
2021-02-21 03:40:54 +01:00
try {
2021-06-30 05:00:23 +02:00
const updateData = getActorUpdateData(actor.toObject());
2021-02-21 03:40:54 +01:00
if (updateData) {
logger.info(`Migrating Actor entity ${actor.name} (${actor.id})`);
2021-06-30 05:00:23 +02:00
await actor.update(updateData);
2021-02-21 03:40:54 +01:00
}
} catch (err) {
err.message = `Error during migration of Actor entity ${actor.name} (${actor.id}), continuing anyways.`;
logger.error(err);
2021-02-21 03:40:54 +01:00
}
}
}
2021-06-30 05:00:23 +02:00
function getActorUpdateData(actorData: foundry.data.ActorData["_source"]) {
2021-02-21 03:40:54 +01:00
let hasItemUpdates = false;
2021-06-30 05:00:23 +02:00
const items = actorData.items.map((itemData: foundry.data.ItemData["_source"]) => {
2021-02-21 03:40:54 +01:00
const update = itemData ? getItemUpdateData(itemData) : undefined;
if (update) {
hasItemUpdates = true;
return { ...itemData, ...update };
} else {
return itemData;
}
});
return hasItemUpdates ? { items } : undefined;
}
async function migrateScenes() {
2021-06-30 05:00:23 +02:00
for (const scene of game.scenes?.contents ?? []) {
2021-02-21 03:40:54 +01:00
try {
2021-06-30 05:00:23 +02:00
const updateData = getSceneUpdateData(scene.toObject());
2021-02-21 03:40:54 +01:00
if (updateData) {
logger.info(`Migrating Scene entity ${scene.name} (${scene.id})`);
2021-06-30 05:00:23 +02:00
await scene.update(updateData);
2021-02-21 03:40:54 +01:00
}
} catch (err) {
2021-06-30 05:00:23 +02:00
err.message = `Error during migration of Scene document ${scene.name} (${scene.id}), continuing anyways.`;
logger.error(err);
2021-02-21 03:40:54 +01:00
}
}
}
2021-06-30 05:00:23 +02:00
function getSceneUpdateData(scene: Scene) {
2021-02-21 03:40:54 +01:00
let hasTokenUpdates = false;
2021-06-30 05:00:23 +02:00
// TODO: Continue from here
const tokens = scene.tokens.map((tokenData: foundry.data.TokenData["_source"]) => {
if (!tokenData.actorId || tokenData.actorLink) {
tokenData.actorData = {};
} else if (!game.actors?.has(tokenData.actorId)) {
tokenData.actorId = null;
tokenData.actorData = {};
} else if (!tokenData.actorLink) {
const actorData = duplicate(tokenData.actorData);
actorData.type = token.actor?.type;
}
2021-02-21 03:40:54 +01:00
if (!tokenData.actorId || tokenData.actorLink || tokenData.actorData.data) {
tokenData.actorData = {};
hasTokenUpdates = true;
return tokenData;
}
const token = new Token(tokenData);
if (!token.actor) {
2021-05-29 21:51:23 +02:00
tokenData.actorId = null as unknown as string;
2021-02-21 03:40:54 +01:00
tokenData.actorData = {};
hasTokenUpdates = true;
} else if (!tokenData.actorLink) {
const actorUpdateData = getActorUpdateData(token.data.actorData);
tokenData.actorData = mergeObject(token.data.actorData, actorUpdateData);
hasTokenUpdates = true;
}
return tokenData;
});
if (!hasTokenUpdates) return undefined;
return hasTokenUpdates ? { tokens } : undefined;
}
async function migrateCompendiums() {
for (const compendium of game.packs ?? []) {
if (compendium.metadata.package !== "world") continue;
if (!["Actor", "Item", "Scene"].includes(compendium.metadata.entity)) continue;
await migrateCompendium(compendium);
}
}
async function migrateCompendium(compendium: Compendium) {
const entityName = compendium.metadata.entity;
if (!["Actor", "Item", "Scene"].includes(entityName)) return;
const wasLocked = compendium.locked;
await compendium.configure({ locked: false });
const content = await compendium.getContent();
for (const entity of content) {
try {
const getUpdateData = (entity: Entity) => {
switch (entityName) {
case "Item":
return getItemUpdateData(entity._data);
case "Actor":
return getActorUpdateData(entity._data);
case "Scene":
return getSceneUpdateData(entity._data as Scene.Data);
}
};
const updateData = getUpdateData(entity);
if (updateData) {
logger.info(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`);
2021-02-21 03:40:54 +01:00
await compendium.updateEntity({ ...updateData, _id: entity._id });
}
} catch (err) {
err.message = `Error during migration of entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}, continuing anyways.`;
logger.error(err);
2021-02-21 03:40:54 +01:00
}
}
2021-02-21 04:05:50 +01:00
await compendium.migrate({});
2021-02-21 03:40:54 +01:00
await compendium.configure({ locked: wasLocked });
}