refactor: improve structure of src
This commit is contained in:
parent
b74919b75b
commit
c5d4ec1abd
96 changed files with 146 additions and 157 deletions
46
src/migration/001.ts
Normal file
46
src/migration/001.ts
Normal file
|
@ -0,0 +1,46 @@
|
|||
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import {
|
||||
getCompendiumMigrator,
|
||||
getSceneUpdateDataGetter,
|
||||
migrateActors,
|
||||
migrateCompendiums,
|
||||
migrateScenes,
|
||||
} from "./migrationHelpers";
|
||||
|
||||
async function migrate(): Promise<void> {
|
||||
await migrateActors(getActorUpdateData);
|
||||
await migrateScenes(getSceneUpdateData);
|
||||
await migrateCompendiums(migrateCompendium);
|
||||
}
|
||||
|
||||
function getActorUpdateData(): Record<string, unknown> {
|
||||
const updateData = {
|
||||
data: {
|
||||
combatValues: [
|
||||
"hitPoints",
|
||||
"defense",
|
||||
"initiative",
|
||||
"movement",
|
||||
"meleeAttack",
|
||||
"rangedAttack",
|
||||
"spellcasting",
|
||||
"targetedSpellcasting",
|
||||
].reduce((acc: Partial<Record<string, { "-=base": null }>>, curr) => {
|
||||
acc[curr] = { "-=base": null };
|
||||
return acc;
|
||||
}, {}),
|
||||
},
|
||||
};
|
||||
return updateData;
|
||||
}
|
||||
|
||||
const getSceneUpdateData = getSceneUpdateDataGetter(getActorUpdateData);
|
||||
const migrateCompendium = getCompendiumMigrator({ getActorUpdateData, getSceneUpdateData });
|
||||
|
||||
export const migration = {
|
||||
migrate,
|
||||
migrateCompendium,
|
||||
};
|
39
src/migration/002.ts
Normal file
39
src/migration/002.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import {
|
||||
getActorUpdateDataGetter,
|
||||
getCompendiumMigrator,
|
||||
getSceneUpdateDataGetter,
|
||||
migrateActors,
|
||||
migrateCompendiums,
|
||||
migrateItems,
|
||||
migrateScenes,
|
||||
} from "./migrationHelpers";
|
||||
|
||||
async function migrate(): Promise<void> {
|
||||
await migrateItems(getItemUpdateData);
|
||||
await migrateActors(getActorUpdateData);
|
||||
await migrateScenes(getSceneUpdateData);
|
||||
await migrateCompendiums(migrateCompendium);
|
||||
}
|
||||
|
||||
function getItemUpdateData(
|
||||
itemData: Partial<foundry.data.ItemData["_source"]>,
|
||||
): DeepPartial<foundry.data.ItemData["_source"]> | undefined {
|
||||
if (!["equipment", "trinket"].includes(itemData.type ?? "")) return undefined;
|
||||
return { type: itemData.type === "equipment" ? ("loot" as const) : ("equipment" as const) };
|
||||
}
|
||||
|
||||
const getActorUpdateData = getActorUpdateDataGetter(getItemUpdateData);
|
||||
const getSceneUpdateData = getSceneUpdateDataGetter(getActorUpdateData);
|
||||
const migrateCompendium = getCompendiumMigrator(
|
||||
{ getItemUpdateData, getActorUpdateData, getSceneUpdateData },
|
||||
{ migrateToTemplateEarly: false },
|
||||
);
|
||||
|
||||
export const migration = {
|
||||
migrate,
|
||||
migrateCompendium,
|
||||
};
|
41
src/migration/003.ts
Normal file
41
src/migration/003.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import {
|
||||
getActorUpdateDataGetter,
|
||||
getCompendiumMigrator,
|
||||
getSceneUpdateDataGetter,
|
||||
migrateActors,
|
||||
migrateCompendiums,
|
||||
migrateItems,
|
||||
migrateScenes,
|
||||
} from "./migrationHelpers";
|
||||
|
||||
async function migrate(): Promise<void> {
|
||||
await migrateItems(getItemUpdateData);
|
||||
await migrateActors(getActorUpdateData);
|
||||
await migrateScenes(getSceneUpdateData);
|
||||
await migrateCompendiums(migrateCompendium);
|
||||
}
|
||||
|
||||
function getItemUpdateData(itemData: Partial<foundry.data.ItemData["_source"]>) {
|
||||
if (!["loot"].includes(itemData.type ?? "")) return undefined;
|
||||
return {
|
||||
data: {
|
||||
"-=equipped": null,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const getActorUpdateData = getActorUpdateDataGetter(getItemUpdateData);
|
||||
const getSceneUpdateData = getSceneUpdateDataGetter(getActorUpdateData);
|
||||
const migrateCompendium = getCompendiumMigrator(
|
||||
{ getItemUpdateData, getActorUpdateData },
|
||||
{ migrateToTemplateEarly: false },
|
||||
);
|
||||
|
||||
export const migration = {
|
||||
migrate,
|
||||
migrateCompendium,
|
||||
};
|
46
src/migration/004.ts
Normal file
46
src/migration/004.ts
Normal file
|
@ -0,0 +1,46 @@
|
|||
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import {
|
||||
getActorUpdateDataGetter,
|
||||
getCompendiumMigrator,
|
||||
getSceneUpdateDataGetter,
|
||||
migrateActors,
|
||||
migrateCompendiums,
|
||||
migrateItems,
|
||||
migrateScenes,
|
||||
} from "./migrationHelpers";
|
||||
|
||||
async function migrate(): Promise<void> {
|
||||
await migrateItems(getItemUpdateData);
|
||||
await migrateActors(getActorUpdateData);
|
||||
await migrateScenes(getSceneUpdateData);
|
||||
await migrateCompendiums(migrateCompendium);
|
||||
}
|
||||
|
||||
function getItemUpdateData(itemData: Partial<foundry.data.ItemData["_source"]>) {
|
||||
if (itemData.type !== "spell") return;
|
||||
// @ts-expect-error the type of cooldownDuration was UnitData<TemporalUnit> at the point for this migration, but it changed later on
|
||||
const cooldownDurationUnit: string | undefined = itemData.data?.cooldownDuration.unit;
|
||||
|
||||
const updateData: Record<string, unknown> = {
|
||||
data: {
|
||||
"-=scrollPrice": null,
|
||||
minimumLevels: { healer: null, wizard: null, sorcerer: null },
|
||||
cooldownDuration: {
|
||||
unit: cooldownDurationUnit === "custom" ? "rounds" : cooldownDurationUnit,
|
||||
},
|
||||
},
|
||||
};
|
||||
return updateData;
|
||||
}
|
||||
|
||||
const getActorUpdateData = getActorUpdateDataGetter(getItemUpdateData);
|
||||
const getSceneUpdateData = getSceneUpdateDataGetter(getActorUpdateData);
|
||||
const migrateCompendium = getCompendiumMigrator({ getItemUpdateData, getActorUpdateData, getSceneUpdateData });
|
||||
|
||||
export const migration = {
|
||||
migrate,
|
||||
migrateCompendium,
|
||||
};
|
118
src/migration/005.ts
Normal file
118
src/migration/005.ts
Normal file
|
@ -0,0 +1,118 @@
|
|||
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import {
|
||||
getActorUpdateDataGetter,
|
||||
getCompendiumMigrator,
|
||||
getSceneUpdateDataGetter,
|
||||
migrateActors,
|
||||
migrateCompendiums,
|
||||
migrateItems,
|
||||
migrateScenes,
|
||||
} from "./migrationHelpers";
|
||||
|
||||
const secondsPerRound = 5;
|
||||
const secondsPerMinute = 60;
|
||||
const roundsPerMinute = secondsPerMinute / secondsPerRound;
|
||||
const minutesPerHour = 60;
|
||||
const roundsPerHour = minutesPerHour / roundsPerMinute;
|
||||
const hoursPerDay = 24;
|
||||
const roundsPerDay = hoursPerDay / roundsPerHour;
|
||||
const secondsPerDay = secondsPerMinute * minutesPerHour * hoursPerDay;
|
||||
|
||||
async function migrate(): Promise<void> {
|
||||
await migrateItems(getItemUpdateData);
|
||||
await migrateActors(getActorUpdateData);
|
||||
await migrateScenes(getSceneUpdateData);
|
||||
await migrateCompendiums(migrateCompendium);
|
||||
}
|
||||
|
||||
function getItemUpdateData(itemData: Partial<foundry.data.ItemData["_source"]>) {
|
||||
if (itemData.type !== "spell") return;
|
||||
// @ts-expect-error the type of cooldownDuration is changed from UnitData<TemporalUnit> to CooldownDuation with this migration
|
||||
const cooldownDurationUnit: string | undefined = itemData.data?.cooldownDuration.unit;
|
||||
// @ts-expect-error the type of cooldownDuration is changed from UnitData<TemporalUnit> to CooldownDuation with this migration
|
||||
const cooldownDurationValue: string | undefined = itemData.data?.cooldownDuration.value;
|
||||
const cooldownDuration = migrateCooldownDuration(cooldownDurationValue, cooldownDurationUnit);
|
||||
|
||||
const updateData: Record<string, unknown> = {
|
||||
data: {
|
||||
cooldownDuration,
|
||||
},
|
||||
};
|
||||
return updateData;
|
||||
}
|
||||
|
||||
function migrateCooldownDuration(cooldownDurationValue = "", cooldownDurationUnit = "") {
|
||||
if (Number.isNumeric(cooldownDurationValue)) {
|
||||
const value = Number.fromString(cooldownDurationValue);
|
||||
const rounds = getRounds(cooldownDurationUnit, value);
|
||||
|
||||
if (rounds * secondsPerRound > secondsPerDay) {
|
||||
return "d20d";
|
||||
} else if (rounds > 100) {
|
||||
return "1d";
|
||||
} else if (rounds > 10) {
|
||||
return "100r";
|
||||
} else if (rounds > 5) {
|
||||
return "10r";
|
||||
} else if (rounds > 2) {
|
||||
return "5r";
|
||||
} else if (rounds > 1) {
|
||||
return "2r";
|
||||
} else if (rounds > 0) {
|
||||
return "1r";
|
||||
} else {
|
||||
return "0r";
|
||||
}
|
||||
} else {
|
||||
// if the value is not numeric, we can only make a best guess
|
||||
switch (cooldownDurationUnit) {
|
||||
case "rounds": {
|
||||
return "10r";
|
||||
}
|
||||
case "minutes": {
|
||||
return "100r";
|
||||
}
|
||||
case "hours": {
|
||||
return "1d";
|
||||
}
|
||||
case "days": {
|
||||
return "d20d";
|
||||
}
|
||||
default: {
|
||||
return "0r";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getRounds(unit: string, value: number): number {
|
||||
switch (unit) {
|
||||
case "rounds": {
|
||||
return value;
|
||||
}
|
||||
case "minutes": {
|
||||
return value * roundsPerMinute;
|
||||
}
|
||||
case "hours": {
|
||||
return value * roundsPerHour;
|
||||
}
|
||||
case "days": {
|
||||
return value * roundsPerDay;
|
||||
}
|
||||
default: {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const getActorUpdateData = getActorUpdateDataGetter(getItemUpdateData);
|
||||
const getSceneUpdateData = getSceneUpdateDataGetter(getActorUpdateData);
|
||||
const migrateCompendium = getCompendiumMigrator({ getItemUpdateData, getActorUpdateData, getSceneUpdateData });
|
||||
|
||||
export const migration = {
|
||||
migrate,
|
||||
migrateCompendium,
|
||||
};
|
117
src/migration/006.ts
Normal file
117
src/migration/006.ts
Normal file
|
@ -0,0 +1,117 @@
|
|||
// SPDX-FileCopyrightText: 2022 Johannes Loher
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import {
|
||||
getActorUpdateDataGetter,
|
||||
getCompendiumMigrator,
|
||||
getSceneUpdateDataGetter,
|
||||
migrateActors,
|
||||
migrateCompendiums,
|
||||
migrateItems,
|
||||
migrateScenes,
|
||||
} from "./migrationHelpers";
|
||||
|
||||
import type { DS4SpellDataSourceData } from "../documents/item/spell/spell-data-source";
|
||||
|
||||
async function migrate(): Promise<void> {
|
||||
await migrateItems(getItemUpdateData);
|
||||
await migrateActors(getActorUpdateData);
|
||||
await migrateScenes(getSceneUpdateData);
|
||||
await migrateCompendiums(migrateCompendium);
|
||||
}
|
||||
|
||||
function getItemUpdateData(itemData: Partial<foundry.data.ItemData["_source"]>) {
|
||||
if (itemData.type !== "spell") return;
|
||||
// @ts-expect-error spellCategory is removed with this migration
|
||||
const spellCategory: string | undefined = itemData.data?.spellCategory;
|
||||
const spellGroups = migrateSpellCategory(spellCategory);
|
||||
|
||||
// @ts-expect-error bonus is removed with this migration
|
||||
const bonus: string | undefined = itemData.data?.bonus;
|
||||
const spellModifier = migrateBonus(bonus);
|
||||
|
||||
const updateData: Record<string, unknown> = {
|
||||
data: {
|
||||
spellGroups,
|
||||
"-=spellCategory": null,
|
||||
spellModifier,
|
||||
"-=bonus": null,
|
||||
},
|
||||
};
|
||||
return updateData;
|
||||
}
|
||||
|
||||
function migrateSpellCategory(spellCategory: string | undefined): DS4SpellDataSourceData["spellGroups"] {
|
||||
const spellGroups = {
|
||||
lightning: false,
|
||||
earth: false,
|
||||
water: false,
|
||||
ice: false,
|
||||
fire: false,
|
||||
healing: false,
|
||||
light: false,
|
||||
air: false,
|
||||
transport: false,
|
||||
damage: false,
|
||||
shadow: false,
|
||||
protection: false,
|
||||
mindAffecting: false,
|
||||
demonology: false,
|
||||
necromancy: false,
|
||||
transmutation: false,
|
||||
area: false,
|
||||
};
|
||||
switch (spellCategory) {
|
||||
case "healing": {
|
||||
spellGroups.healing = true;
|
||||
break;
|
||||
}
|
||||
case "fire": {
|
||||
spellGroups.fire = true;
|
||||
break;
|
||||
}
|
||||
case "ice": {
|
||||
spellGroups.ice = true;
|
||||
break;
|
||||
}
|
||||
case "light": {
|
||||
spellGroups.light = true;
|
||||
break;
|
||||
}
|
||||
case "darkness": {
|
||||
spellGroups.shadow = true;
|
||||
break;
|
||||
}
|
||||
case "mindAffecting": {
|
||||
spellGroups.mindAffecting = true;
|
||||
break;
|
||||
}
|
||||
case "electricity": {
|
||||
spellGroups.lightning = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return spellGroups;
|
||||
}
|
||||
|
||||
function migrateBonus(bonus: string | undefined): DS4SpellDataSourceData["spellModifier"] {
|
||||
const spellModifier = { numerical: 0, complex: "" };
|
||||
if (bonus) {
|
||||
if (Number.isNumeric(bonus)) {
|
||||
spellModifier.numerical = +bonus;
|
||||
} else {
|
||||
spellModifier.complex = bonus;
|
||||
}
|
||||
}
|
||||
return spellModifier;
|
||||
}
|
||||
|
||||
const getActorUpdateData = getActorUpdateDataGetter(getItemUpdateData);
|
||||
const getSceneUpdateData = getSceneUpdateDataGetter(getActorUpdateData);
|
||||
const migrateCompendium = getCompendiumMigrator({ getItemUpdateData, getActorUpdateData, getSceneUpdateData });
|
||||
|
||||
export const migration = {
|
||||
migrate,
|
||||
migrateCompendium,
|
||||
};
|
150
src/migration/migration.ts
Normal file
150
src/migration/migration.ts
Normal file
|
@ -0,0 +1,150 @@
|
|||
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { notifications } from "../ui/notifications";
|
||||
import { logger } from "../utils/logger";
|
||||
import { getGame } from "../utils/utils";
|
||||
import { migration as migration001 } from "./001";
|
||||
import { migration as migration002 } from "./002";
|
||||
import { migration as migration003 } from "./003";
|
||||
import { migration as migration004 } from "./004";
|
||||
import { migration as migration005 } from "./005";
|
||||
import { migration as migration006 } from "./006";
|
||||
|
||||
async function migrate(): Promise<void> {
|
||||
if (!getGame().user?.isGM) {
|
||||
return;
|
||||
}
|
||||
|
||||
const oldMigrationVersion = getGame().settings.get("ds4", "systemMigrationVersion");
|
||||
|
||||
const targetMigrationVersion = migrations.length;
|
||||
|
||||
if (isFirstWorldStart(oldMigrationVersion)) {
|
||||
getGame().settings.set("ds4", "systemMigrationVersion", targetMigrationVersion);
|
||||
return;
|
||||
}
|
||||
|
||||
return migrateFromTo(oldMigrationVersion, targetMigrationVersion);
|
||||
}
|
||||
|
||||
async function migrateFromTo(oldMigrationVersion: number, targetMigrationVersion: number): Promise<void> {
|
||||
if (!getGame().user?.isGM) {
|
||||
return;
|
||||
}
|
||||
|
||||
const migrationsToExecute = migrations.slice(oldMigrationVersion, targetMigrationVersion);
|
||||
|
||||
if (migrationsToExecute.length > 0) {
|
||||
notifications.info(
|
||||
getGame().i18n.format("DS4.InfoSystemUpdateStart", {
|
||||
currentVersion: oldMigrationVersion,
|
||||
targetVersion: targetMigrationVersion,
|
||||
}),
|
||||
{ permanent: true },
|
||||
);
|
||||
|
||||
for (const [i, { migrate }] of migrationsToExecute.entries()) {
|
||||
const currentMigrationVersion = oldMigrationVersion + i + 1;
|
||||
logger.info("executing migration script ", currentMigrationVersion);
|
||||
try {
|
||||
await migrate();
|
||||
getGame().settings.set("ds4", "systemMigrationVersion", currentMigrationVersion);
|
||||
} catch (err) {
|
||||
notifications.error(
|
||||
getGame().i18n.format("DS4.ErrorDuringMigration", {
|
||||
currentVersion: oldMigrationVersion,
|
||||
targetVersion: targetMigrationVersion,
|
||||
migrationVersion: currentMigrationVersion,
|
||||
}),
|
||||
{ permanent: true },
|
||||
);
|
||||
logger.error("Failed ds4 system migration:", err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
notifications.info(
|
||||
getGame().i18n.format("DS4.InfoSystemUpdateCompleted", {
|
||||
currentVersion: oldMigrationVersion,
|
||||
targetVersion: targetMigrationVersion,
|
||||
}),
|
||||
{ permanent: true },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function migrateCompendiumFromTo(
|
||||
pack: CompendiumCollection<CompendiumCollection.Metadata>,
|
||||
oldMigrationVersion: number,
|
||||
targetMigrationVersion: number,
|
||||
): Promise<void> {
|
||||
if (!getGame().user?.isGM) {
|
||||
return;
|
||||
}
|
||||
|
||||
const migrationsToExecute = migrations.slice(oldMigrationVersion, targetMigrationVersion);
|
||||
|
||||
if (migrationsToExecute.length > 0) {
|
||||
notifications.info(
|
||||
getGame().i18n.format("DS4.InfoCompendiumMigrationStart", {
|
||||
pack: pack.title,
|
||||
currentVersion: oldMigrationVersion,
|
||||
targetVersion: targetMigrationVersion,
|
||||
}),
|
||||
{ permanent: true },
|
||||
);
|
||||
|
||||
for (const [i, { migrateCompendium }] of migrationsToExecute.entries()) {
|
||||
const currentMigrationVersion = oldMigrationVersion + i + 1;
|
||||
logger.info("executing compendium migration ", currentMigrationVersion);
|
||||
try {
|
||||
await migrateCompendium(pack);
|
||||
} catch (err) {
|
||||
notifications.error(
|
||||
getGame().i18n.format("DS4.ErrorDuringCompendiumMigration", {
|
||||
pack: pack.title,
|
||||
currentVersion: oldMigrationVersion,
|
||||
targetVersion: targetMigrationVersion,
|
||||
migrationVersion: currentMigrationVersion,
|
||||
}),
|
||||
{ permanent: true },
|
||||
);
|
||||
logger.error("Failed ds4 compendium migration:", err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
notifications.info(
|
||||
getGame().i18n.format("DS4.InfoCompendiumMigrationCompleted", {
|
||||
pack: pack.title,
|
||||
currentVersion: oldMigrationVersion,
|
||||
targetVersion: targetMigrationVersion,
|
||||
}),
|
||||
{ permanent: true },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function getTargetMigrationVersion(): number {
|
||||
return migrations.length;
|
||||
}
|
||||
|
||||
interface Migration {
|
||||
migrate: () => Promise<void>;
|
||||
migrateCompendium: (pack: CompendiumCollection<CompendiumCollection.Metadata>) => Promise<void>;
|
||||
}
|
||||
|
||||
const migrations: Migration[] = [migration001, migration002, migration003, migration004, migration005, migration006];
|
||||
|
||||
function isFirstWorldStart(migrationVersion: number): boolean {
|
||||
return migrationVersion < 0;
|
||||
}
|
||||
|
||||
export const migration = {
|
||||
migrate,
|
||||
migrateFromTo,
|
||||
getTargetMigrationVersion,
|
||||
migrateCompendiumFromTo,
|
||||
};
|
186
src/migration/migrationHelpers.ts
Normal file
186
src/migration/migrationHelpers.ts
Normal file
|
@ -0,0 +1,186 @@
|
|||
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { DS4Actor } from "../documents/actor/actor";
|
||||
import { DS4Item } from "../documents/item/item";
|
||||
import { logger } from "../utils/logger";
|
||||
import { getGame } from "../utils/utils";
|
||||
|
||||
type ItemUpdateDataGetter = (
|
||||
itemData: Partial<foundry.data.ItemData["_source"]>,
|
||||
) => DeepPartial<foundry.data.ItemData["_source"]> | Record<string, unknown> | undefined;
|
||||
|
||||
export async function migrateItems(getItemUpdateData: ItemUpdateDataGetter): Promise<void> {
|
||||
for (const item of getGame().items ?? []) {
|
||||
try {
|
||||
const updateData = getItemUpdateData(item.toObject());
|
||||
if (updateData) {
|
||||
logger.info(`Migrating Item document ${item.name} (${item.id})`);
|
||||
await item.update(updateData), { enforceTypes: false };
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(`Error during migration of Item document ${item.name} (${item.id}), continuing anyways.`, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type ActorUpdateDataGetter = (
|
||||
itemData: Partial<foundry.data.ActorData["_source"]>,
|
||||
) => DeepPartial<foundry.data.ActorData["_source"]> | undefined;
|
||||
|
||||
export async function migrateActors(getActorUpdateData: ActorUpdateDataGetter): Promise<void> {
|
||||
for (const actor of getGame().actors ?? []) {
|
||||
try {
|
||||
const updateData = getActorUpdateData(actor.toObject());
|
||||
if (updateData) {
|
||||
logger.info(`Migrating Actor document ${actor.name} (${actor.id})`);
|
||||
await actor.update(updateData);
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
`Error during migration of Actor document ${actor.name} (${actor.id}), continuing anyways.`,
|
||||
err,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type SceneUpdateDataGetter = (sceneData: foundry.data.SceneData) => DeepPartial<foundry.data.SceneData["_source"]>;
|
||||
|
||||
export async function migrateScenes(getSceneUpdateData: SceneUpdateDataGetter): Promise<void> {
|
||||
for (const scene of getGame().scenes ?? []) {
|
||||
try {
|
||||
const updateData = getSceneUpdateData(scene.data);
|
||||
if (updateData) {
|
||||
logger.info(`Migrating Scene document ${scene.name} (${scene.id})`);
|
||||
await scene.update(
|
||||
updateData as DeepPartial<Parameters<foundry.data.SceneData["_initializeSource"]>[0]>,
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
`Error during migration of Scene document ${scene.name} (${scene.id}), continuing anyways.`,
|
||||
err,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type CompendiumMigrator = (compendium: CompendiumCollection<CompendiumCollection.Metadata>) => Promise<void>;
|
||||
|
||||
export async function migrateCompendiums(migrateCompendium: CompendiumMigrator): Promise<void> {
|
||||
for (const compendium of getGame().packs ?? []) {
|
||||
if (compendium.metadata.package !== "world") continue;
|
||||
if (!["Actor", "Item", "Scene"].includes(compendium.metadata.type)) continue;
|
||||
await migrateCompendium(compendium);
|
||||
}
|
||||
}
|
||||
|
||||
export function getActorUpdateDataGetter(getItemUpdateData: ItemUpdateDataGetter): ActorUpdateDataGetter {
|
||||
return (
|
||||
actorData: Partial<foundry.data.ActorData["_source"]>,
|
||||
): DeepPartial<foundry.data.ActorData["_source"]> | undefined => {
|
||||
let hasItemUpdates = false;
|
||||
const items = actorData.items?.map((itemData) => {
|
||||
const update = getItemUpdateData(itemData);
|
||||
if (update) {
|
||||
hasItemUpdates = true;
|
||||
return { ...itemData, ...update };
|
||||
} else {
|
||||
return itemData;
|
||||
}
|
||||
});
|
||||
return hasItemUpdates ? { items } : undefined;
|
||||
};
|
||||
}
|
||||
|
||||
export function getSceneUpdateDataGetter(getActorUpdateData: ActorUpdateDataGetter): SceneUpdateDataGetter {
|
||||
return (sceneData: foundry.data.SceneData) => {
|
||||
const tokens = sceneData.tokens.map((token: TokenDocument) => {
|
||||
const t = token.toObject();
|
||||
if (!t.actorId || t.actorLink) {
|
||||
t.actorData = {};
|
||||
} else if (!getGame().actors?.has(t.actorId)) {
|
||||
t.actorId = null;
|
||||
t.actorData = {};
|
||||
} else if (!t.actorLink) {
|
||||
const actorData = foundry.utils.deepClone(t.actorData);
|
||||
actorData.type = token.actor?.type;
|
||||
const update = getActorUpdateData(actorData);
|
||||
if (update !== undefined) {
|
||||
["items" as const, "effects" as const].forEach((embeddedName) => {
|
||||
const embeddedUpdates = update[embeddedName];
|
||||
if (embeddedUpdates === undefined || !embeddedUpdates.length) return;
|
||||
const updates = new Map(embeddedUpdates.flatMap((u) => (u && u._id ? [[u._id, u]] : [])));
|
||||
const originals = t.actorData[embeddedName];
|
||||
if (!originals) return;
|
||||
originals.forEach((original) => {
|
||||
if (!original._id) return;
|
||||
const update = updates.get(original._id);
|
||||
if (update) foundry.utils.mergeObject(original, update);
|
||||
});
|
||||
delete update[embeddedName];
|
||||
});
|
||||
foundry.utils.mergeObject(t.actorData, update);
|
||||
}
|
||||
}
|
||||
return t;
|
||||
});
|
||||
return { tokens };
|
||||
};
|
||||
}
|
||||
|
||||
export function getCompendiumMigrator(
|
||||
{
|
||||
getItemUpdateData,
|
||||
getActorUpdateData,
|
||||
getSceneUpdateData,
|
||||
}: {
|
||||
getItemUpdateData?: ItemUpdateDataGetter;
|
||||
getActorUpdateData?: ActorUpdateDataGetter;
|
||||
getSceneUpdateData?: SceneUpdateDataGetter;
|
||||
} = {},
|
||||
{ migrateToTemplateEarly = true } = {},
|
||||
) {
|
||||
return async (compendium: CompendiumCollection<CompendiumCollection.Metadata>): Promise<void> => {
|
||||
const type = compendium.metadata.type;
|
||||
if (!["Actor", "Item", "Scene"].includes(type)) return;
|
||||
const wasLocked = compendium.locked;
|
||||
await compendium.configure({ locked: false });
|
||||
if (migrateToTemplateEarly) {
|
||||
await compendium.migrate();
|
||||
}
|
||||
|
||||
const documents = await compendium.getDocuments();
|
||||
|
||||
for (const doc of documents) {
|
||||
try {
|
||||
logger.info(`Migrating document ${doc.name} (${doc.id}) in compendium ${compendium.collection}`);
|
||||
if (doc instanceof DS4Item && getItemUpdateData) {
|
||||
const updateData = getItemUpdateData(doc.toObject());
|
||||
updateData && (await doc.update(updateData));
|
||||
} else if (doc instanceof DS4Actor && getActorUpdateData) {
|
||||
const updateData = getActorUpdateData(doc.toObject());
|
||||
updateData && (await doc.update(updateData));
|
||||
} else if (doc instanceof Scene && getSceneUpdateData) {
|
||||
const updateData = getSceneUpdateData(doc.data as foundry.data.SceneData);
|
||||
updateData &&
|
||||
(await doc.update(
|
||||
updateData as DeepPartial<Parameters<foundry.data.SceneData["_initializeSource"]>[0]>,
|
||||
));
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
`Error during migration of document ${doc.name} (${doc.id}) in compendium ${compendium.collection}, continuing anyways.`,
|
||||
err,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!migrateToTemplateEarly) {
|
||||
await compendium.migrate();
|
||||
}
|
||||
await compendium.configure({ locked: wasLocked });
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue