temporary comment migrations

This commit is contained in:
Johannes Loher 2021-06-30 05:04:58 +02:00
parent 76f42fe280
commit 446340c064
4 changed files with 410 additions and 410 deletions

View file

@ -4,9 +4,9 @@
import logger from "./logger"; import logger from "./logger";
import { migrate as migrate001 } from "./migrations/001"; import { migrate as migrate001 } from "./migrations/001";
import { migrate as migrate002 } from "./migrations/002"; // import { migrate as migrate002 } from "./migrations/002";
import { migrate as migrate003 } from "./migrations/003"; // import { migrate as migrate003 } from "./migrations/003";
import { migrate as migrate004 } from "./migrations/004"; // import { migrate as migrate004 } from "./migrations/004";
import notifications from "./ui/notifications"; import notifications from "./ui/notifications";
@ -78,7 +78,7 @@ function getTargetMigrationVersion(): number {
return migrations.length; return migrations.length;
} }
const migrations: Array<() => Promise<void>> = [migrate001, migrate002, migrate003, migrate004]; const migrations: Array<() => Promise<void>> = [migrate001 /* migrate002, migrate003, migrate004 */];
function isFirstWorldStart(migrationVersion: number): boolean { function isFirstWorldStart(migrationVersion: number): boolean {
return migrationVersion < 0; return migrationVersion < 0;

View file

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

View file

@ -4,144 +4,144 @@
import logger from "../logger"; import logger from "../logger";
export async function migrate(): Promise<void> { // export async function migrate(): Promise<void> {
await migrateItems(); // await migrateItems();
await migrateActors(); // await migrateActors();
await migrateScenes(); // await migrateScenes();
await migrateCompendiums(); // await migrateCompendiums();
} // }
async function migrateItems() { // async function migrateItems() {
for (const item of game.items?.entities ?? []) { // for (const item of game.items?.entities ?? []) {
try { // try {
const updateData = getItemUpdateData(item._data); // const updateData = getItemUpdateData(item._data);
if (updateData) { // if (updateData) {
logger.info(`Migrating Item entity ${item.name} (${item.id})`); // logger.info(`Migrating Item entity ${item.name} (${item.id})`);
await item.update(updateData), { enforceTypes: false }; // await item.update(updateData), { enforceTypes: false };
} // }
} catch (err) { // } catch (err) {
err.message = `Error during migration of Item entity ${item.name} (${item.id}), continuing anyways.`; // err.message = `Error during migration of Item entity ${item.name} (${item.id}), continuing anyways.`;
logger.error(err); // logger.error(err);
} // }
} // }
} // }
function getItemUpdateData(itemData: DeepPartial<Item.Data>) { // function getItemUpdateData(itemData: DeepPartial<Item.Data>) {
if (!["loot"].includes(itemData.type ?? "")) return undefined; // if (!["loot"].includes(itemData.type ?? "")) return undefined;
return { // return {
data: { // data: {
"-=equipped": null, // "-=equipped": null,
}, // },
}; // };
} // }
async function migrateActors() { // async function migrateActors() {
for (const actor of game.actors?.entities ?? []) { // for (const actor of game.actors?.entities ?? []) {
try { // try {
const updateData = getActorUpdateData(actor._data); // const updateData = getActorUpdateData(actor._data);
if (updateData) { // if (updateData) {
logger.info(`Migrating Actor entity ${actor.name} (${actor.id})`); // logger.info(`Migrating Actor entity ${actor.name} (${actor.id})`);
await actor.update(updateData, { enforceTypes: false }); // await actor.update(updateData, { enforceTypes: false });
} // }
} catch (err) { // } catch (err) {
err.message = `Error during migration of Actor entity ${actor.name} (${actor.id}), continuing anyways.`; // err.message = `Error during migration of Actor entity ${actor.name} (${actor.id}), continuing anyways.`;
logger.error(err); // logger.error(err);
} // }
} // }
} // }
function getActorUpdateData(actorData: DeepPartial<Actor.Data>) { // function getActorUpdateData(actorData: DeepPartial<Actor.Data>) {
let hasItemUpdates = false; // let hasItemUpdates = false;
const items = actorData.items?.map((itemData) => { // const items = actorData.items?.map((itemData) => {
const update = itemData ? getItemUpdateData(itemData) : undefined; // const update = itemData ? getItemUpdateData(itemData) : undefined;
if (update) { // if (update) {
hasItemUpdates = true; // hasItemUpdates = true;
return mergeObject(itemData, update, { enforceTypes: false, inplace: false }); // return mergeObject(itemData, update, { enforceTypes: false, inplace: false });
} else { // } else {
return itemData; // return itemData;
} // }
}); // });
return hasItemUpdates ? { items } : undefined; // return hasItemUpdates ? { items } : undefined;
} // }
async function migrateScenes() { // async function migrateScenes() {
for (const scene of game.scenes?.entities ?? []) { // for (const scene of game.scenes?.entities ?? []) {
try { // try {
const updateData = getSceneUpdateData(scene._data); // const updateData = getSceneUpdateData(scene._data);
if (updateData) { // if (updateData) {
logger.info(`Migrating Scene entity ${scene.name} (${scene.id})`); // logger.info(`Migrating Scene entity ${scene.name} (${scene.id})`);
await scene.update(updateData, { enforceTypes: false }); // await scene.update(updateData, { enforceTypes: false });
} // }
} catch (err) { // } catch (err) {
err.message = `Error during migration of Scene entity ${scene.name} (${scene.id}), continuing anyways.`; // err.message = `Error during migration of Scene entity ${scene.name} (${scene.id}), continuing anyways.`;
logger.error(err); // logger.error(err);
} // }
} // }
} // }
function getSceneUpdateData(sceneData: Scene.Data) { // function getSceneUpdateData(sceneData: Scene.Data) {
let hasTokenUpdates = false; // let hasTokenUpdates = false;
const tokens = sceneData.tokens.map((tokenData) => { // const tokens = sceneData.tokens.map((tokenData) => {
if (!tokenData.actorId || tokenData.actorLink || tokenData.actorData.data) { // if (!tokenData.actorId || tokenData.actorLink || tokenData.actorData.data) {
tokenData.actorData = {}; // tokenData.actorData = {};
hasTokenUpdates = true; // hasTokenUpdates = true;
return tokenData; // return tokenData;
} // }
const token = new Token(tokenData); // const token = new Token(tokenData);
if (!token.actor) { // if (!token.actor) {
tokenData.actorId = null as unknown as string; // tokenData.actorId = null as unknown as string;
tokenData.actorData = {}; // tokenData.actorData = {};
hasTokenUpdates = true; // hasTokenUpdates = true;
} else if (!tokenData.actorLink) { // } else if (!tokenData.actorLink) {
const actorUpdateData = getActorUpdateData(token.data.actorData); // const actorUpdateData = getActorUpdateData(token.data.actorData);
tokenData.actorData = mergeObject(token.data.actorData, actorUpdateData); // tokenData.actorData = mergeObject(token.data.actorData, actorUpdateData);
hasTokenUpdates = true; // hasTokenUpdates = true;
} // }
return tokenData; // return tokenData;
}); // });
if (!hasTokenUpdates) return undefined; // if (!hasTokenUpdates) return undefined;
return hasTokenUpdates ? { tokens } : undefined; // return hasTokenUpdates ? { tokens } : undefined;
} // }
async function migrateCompendiums() { // async function migrateCompendiums() {
for (const compendium of game.packs ?? []) { // for (const compendium of game.packs ?? []) {
if (compendium.metadata.package !== "world") continue; // if (compendium.metadata.package !== "world") continue;
if (!["Actor", "Item", "Scene"].includes(compendium.metadata.entity)) continue; // if (!["Actor", "Item", "Scene"].includes(compendium.metadata.entity)) continue;
await migrateCompendium(compendium); // await migrateCompendium(compendium);
} // }
} // }
async function migrateCompendium(compendium: Compendium) { // async function migrateCompendium(compendium: Compendium) {
const entityName = compendium.metadata.entity; // const entityName = compendium.metadata.entity;
if (!["Actor", "Item", "Scene"].includes(entityName)) return; // if (!["Actor", "Item", "Scene"].includes(entityName)) return;
const wasLocked = compendium.locked; // const wasLocked = compendium.locked;
await compendium.configure({ locked: false }); // await compendium.configure({ locked: false });
const content = await compendium.getContent(); // const content = await compendium.getContent();
for (const entity of content) { // for (const entity of content) {
try { // try {
const getUpdateData = (entity: Entity) => { // const getUpdateData = (entity: Entity) => {
switch (entityName) { // switch (entityName) {
case "Item": // case "Item":
return getItemUpdateData(entity._data); // return getItemUpdateData(entity._data);
case "Actor": // case "Actor":
return getActorUpdateData(entity._data); // return getActorUpdateData(entity._data);
case "Scene": // case "Scene":
return getSceneUpdateData(entity._data as Scene.Data); // return getSceneUpdateData(entity._data as Scene.Data);
} // }
}; // };
const updateData = getUpdateData(entity); // const updateData = getUpdateData(entity);
if (updateData) { // if (updateData) {
logger.info(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`); // logger.info(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`);
await compendium.updateEntity({ ...updateData, _id: entity._id }); // await compendium.updateEntity({ ...updateData, _id: entity._id });
} // }
} catch (err) { // } catch (err) {
err.message = `Error during migration of entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}, continuing anyways.`; // err.message = `Error during migration of entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}, continuing anyways.`;
logger.error(err); // logger.error(err);
} // }
} // }
await compendium.migrate({}); // await compendium.migrate({});
await compendium.configure({ locked: wasLocked }); // await compendium.configure({ locked: wasLocked });
} // }

View file

@ -5,154 +5,154 @@
import { DS4SpellDataSourceData } from "../item/item-data-source"; import { DS4SpellDataSourceData } from "../item/item-data-source";
import logger from "../logger"; import logger from "../logger";
export async function migrate(): Promise<void> { // export async function migrate(): Promise<void> {
await migrateItems(); // await migrateItems();
await migrateActors(); // await migrateActors();
await migrateScenes(); // await migrateScenes();
await migrateCompendiums(); // await migrateCompendiums();
} // }
async function migrateItems() { // async function migrateItems() {
for (const item of game.items?.entities ?? []) { // for (const item of game.items?.entities ?? []) {
try { // try {
const updateData = getItemUpdateData(item._data); // const updateData = getItemUpdateData(item._data);
if (updateData) { // if (updateData) {
logger.info(`Migrating Item entity ${item.name} (${item.id})`); // logger.info(`Migrating Item entity ${item.name} (${item.id})`);
await item.update(updateData), { enforceTypes: false }; // await item.update(updateData), { enforceTypes: false };
} // }
} catch (err) { // } catch (err) {
err.message = `Error during migration of Item entity ${item.name} (${item.id}), continuing anyways.`; // err.message = `Error during migration of Item entity ${item.name} (${item.id}), continuing anyways.`;
logger.error(err); // logger.error(err);
} // }
} // }
} // }
function getItemUpdateData(itemData: DeepPartial<Item.Data>) { // function getItemUpdateData(itemData: DeepPartial<Item.Data>) {
if (!["spell"].includes(itemData.type ?? "")) return undefined; // if (!["spell"].includes(itemData.type ?? "")) return undefined;
const updateData: Record<string, unknown> = { // const updateData: Record<string, unknown> = {
"-=data.scrollPrice": null, // "-=data.scrollPrice": null,
"data.minimumLevels": { healer: null, wizard: null, sorcerer: null }, // "data.minimumLevels": { healer: null, wizard: null, sorcerer: null },
}; // };
if (((itemData.data as DS4SpellDataSourceData).cooldownDuration.unit as string) === "custom") { // if (((itemData.data as DS4SpellDataSourceData).cooldownDuration.unit as string) === "custom") {
updateData["data.cooldownDuration.unit"] = "rounds"; // updateData["data.cooldownDuration.unit"] = "rounds";
} // }
return updateData; // return updateData;
} // }
async function migrateActors() { // async function migrateActors() {
for (const actor of game.actors?.entities ?? []) { // for (const actor of game.actors?.entities ?? []) {
try { // try {
const updateData = getActorUpdateData(actor._data); // const updateData = getActorUpdateData(actor._data);
if (updateData) { // if (updateData) {
logger.info(`Migrating Actor entity ${actor.name} (${actor.id})`); // logger.info(`Migrating Actor entity ${actor.name} (${actor.id})`);
await actor.update(updateData, { enforceTypes: false }); // await actor.update(updateData, { enforceTypes: false });
} // }
} catch (err) { // } catch (err) {
err.message = `Error during migration of Actor entity ${actor.name} (${actor.id}), continuing anyways.`; // err.message = `Error during migration of Actor entity ${actor.name} (${actor.id}), continuing anyways.`;
logger.error(err); // logger.error(err);
} // }
} // }
} // }
function getActorUpdateData(actorData: DeepPartial<Actor.Data>) { // function getActorUpdateData(actorData: DeepPartial<Actor.Data>) {
let hasItemUpdates = false; // let hasItemUpdates = false;
const items = actorData.items?.map((itemData) => { // const items = actorData.items?.map((itemData) => {
const update = itemData ? getItemUpdateData(itemData) : undefined; // const update = itemData ? getItemUpdateData(itemData) : undefined;
if (update) { // if (update) {
hasItemUpdates = true; // hasItemUpdates = true;
return mergeObject(itemData, update, { enforceTypes: false, inplace: false }); // return mergeObject(itemData, update, { enforceTypes: false, inplace: false });
} else { // } else {
return itemData; // return itemData;
} // }
}); // });
const updateData: Record<string, unknown> = {}; // const updateData: Record<string, unknown> = {};
if (actorData.type === "character") { // if (actorData.type === "character") {
updateData["data.slayerPoints"] = { value: 0 }; // updateData["data.slayerPoints"] = { value: 0 };
} // }
if (hasItemUpdates) { // if (hasItemUpdates) {
updateData["items"] = items; // updateData["items"] = items;
} // }
return updateData; // return updateData;
} // }
async function migrateScenes() { // async function migrateScenes() {
for (const scene of game.scenes?.entities ?? []) { // for (const scene of game.scenes?.entities ?? []) {
try { // try {
const updateData = getSceneUpdateData(scene._data); // const updateData = getSceneUpdateData(scene._data);
if (updateData) { // if (updateData) {
logger.info(`Migrating Scene entity ${scene.name} (${scene.id})`); // logger.info(`Migrating Scene entity ${scene.name} (${scene.id})`);
await scene.update(updateData, { enforceTypes: false }); // await scene.update(updateData, { enforceTypes: false });
} // }
} catch (err) { // } catch (err) {
err.message = `Error during migration of Scene entity ${scene.name} (${scene.id}), continuing anyways.`; // err.message = `Error during migration of Scene entity ${scene.name} (${scene.id}), continuing anyways.`;
logger.error(err); // logger.error(err);
} // }
} // }
} // }
function getSceneUpdateData(sceneData: Scene.Data) { // function getSceneUpdateData(sceneData: Scene.Data) {
let hasTokenUpdates = false; // let hasTokenUpdates = false;
const tokens = sceneData.tokens.map((tokenData) => { // const tokens = sceneData.tokens.map((tokenData) => {
if (!tokenData.actorId || tokenData.actorLink || tokenData.actorData.data) { // if (!tokenData.actorId || tokenData.actorLink || tokenData.actorData.data) {
tokenData.actorData = {}; // tokenData.actorData = {};
hasTokenUpdates = true; // hasTokenUpdates = true;
return tokenData; // return tokenData;
} // }
const token = new Token(tokenData); // const token = new Token(tokenData);
if (!token.actor) { // if (!token.actor) {
tokenData.actorId = null as unknown as string; // tokenData.actorId = null as unknown as string;
tokenData.actorData = {}; // tokenData.actorData = {};
hasTokenUpdates = true; // hasTokenUpdates = true;
} else if (!tokenData.actorLink) { // } else if (!tokenData.actorLink) {
const actorUpdateData = getActorUpdateData(token.data.actorData); // const actorUpdateData = getActorUpdateData(token.data.actorData);
tokenData.actorData = mergeObject(token.data.actorData, actorUpdateData); // tokenData.actorData = mergeObject(token.data.actorData, actorUpdateData);
hasTokenUpdates = true; // hasTokenUpdates = true;
} // }
return tokenData; // return tokenData;
}); // });
if (!hasTokenUpdates) return undefined; // if (!hasTokenUpdates) return undefined;
return hasTokenUpdates ? { tokens } : undefined; // return hasTokenUpdates ? { tokens } : undefined;
} // }
async function migrateCompendiums() { // async function migrateCompendiums() {
for (const compendium of game.packs ?? []) { // for (const compendium of game.packs ?? []) {
if (compendium.metadata.package !== "world") continue; // if (compendium.metadata.package !== "world") continue;
if (!["Actor", "Item", "Scene"].includes(compendium.metadata.entity)) continue; // if (!["Actor", "Item", "Scene"].includes(compendium.metadata.entity)) continue;
await migrateCompendium(compendium); // await migrateCompendium(compendium);
} // }
} // }
async function migrateCompendium(compendium: Compendium) { // async function migrateCompendium(compendium: Compendium) {
const entityName = compendium.metadata.entity; // const entityName = compendium.metadata.entity;
if (!["Actor", "Item", "Scene"].includes(entityName)) return; // if (!["Actor", "Item", "Scene"].includes(entityName)) return;
const wasLocked = compendium.locked; // const wasLocked = compendium.locked;
await compendium.configure({ locked: false }); // await compendium.configure({ locked: false });
await compendium.migrate({}); // await compendium.migrate({});
const content = await compendium.getContent(); // const content = await compendium.getContent();
for (const entity of content) { // for (const entity of content) {
try { // try {
const getUpdateData = (entity: Entity) => { // const getUpdateData = (entity: Entity) => {
switch (entityName) { // switch (entityName) {
case "Item": // case "Item":
return getItemUpdateData(entity._data); // return getItemUpdateData(entity._data);
case "Actor": // case "Actor":
return getActorUpdateData(entity._data); // return getActorUpdateData(entity._data);
case "Scene": // case "Scene":
return getSceneUpdateData(entity._data as Scene.Data); // return getSceneUpdateData(entity._data as Scene.Data);
} // }
}; // };
const updateData = getUpdateData(entity); // const updateData = getUpdateData(entity);
if (updateData) { // if (updateData) {
logger.info(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`); // logger.info(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`);
await compendium.updateEntity({ ...updateData, _id: entity._id }); // await compendium.updateEntity({ ...updateData, _id: entity._id });
} // }
} catch (err) { // } catch (err) {
err.message = `Error during migration of entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}, continuing anyways.`; // err.message = `Error during migration of entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}, continuing anyways.`;
logger.error(err); // logger.error(err);
} // }
} // }
await compendium.configure({ locked: wasLocked }); // await compendium.configure({ locked: wasLocked });
} // }