2021-06-26 22:02:00 +02:00
|
|
|
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-07-07 19:22:35 +02:00
|
|
|
import { getGame } from "./helpers";
|
2021-06-26 14:32:40 +02:00
|
|
|
import logger from "./logger";
|
2022-02-14 00:58:23 +01:00
|
|
|
import { migration as migration001 } from "./migrations/001";
|
|
|
|
import { migration as migration002 } from "./migrations/002";
|
|
|
|
import { migration as migration003 } from "./migrations/003";
|
|
|
|
import { migration as migration004 } from "./migrations/004";
|
|
|
|
import { migration as migration005 } from "./migrations/005";
|
2021-02-21 03:40:54 +01:00
|
|
|
import notifications from "./ui/notifications";
|
2021-01-19 03:31:40 +01:00
|
|
|
|
2021-01-20 22:11:53 +01:00
|
|
|
async function migrate(): Promise<void> {
|
2021-07-07 19:22:35 +02:00
|
|
|
if (!getGame().user?.isGM) {
|
2021-01-19 03:31:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-07 19:22:35 +02:00
|
|
|
const oldMigrationVersion = getGame().settings.get("ds4", "systemMigrationVersion");
|
2021-01-19 03:31:40 +01:00
|
|
|
|
2021-01-20 22:11:53 +01:00
|
|
|
const targetMigrationVersion = migrations.length;
|
|
|
|
|
|
|
|
if (isFirstWorldStart(oldMigrationVersion)) {
|
2021-07-07 19:22:35 +02:00
|
|
|
getGame().settings.set("ds4", "systemMigrationVersion", targetMigrationVersion);
|
2021-01-20 22:11:53 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return migrateFromTo(oldMigrationVersion, targetMigrationVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function migrateFromTo(oldMigrationVersion: number, targetMigrationVersion: number): Promise<void> {
|
2021-07-07 19:22:35 +02:00
|
|
|
if (!getGame().user?.isGM) {
|
2021-01-19 03:31:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-20 22:11:53 +01:00
|
|
|
const migrationsToExecute = migrations.slice(oldMigrationVersion, targetMigrationVersion);
|
|
|
|
|
|
|
|
if (migrationsToExecute.length > 0) {
|
2021-02-21 03:40:54 +01:00
|
|
|
notifications.info(
|
2021-07-07 19:22:35 +02:00
|
|
|
getGame().i18n.format("DS4.InfoSystemUpdateStart", {
|
2021-01-20 22:11:53 +01:00
|
|
|
currentVersion: oldMigrationVersion,
|
|
|
|
targetVersion: targetMigrationVersion,
|
2021-01-19 03:31:40 +01:00
|
|
|
}),
|
|
|
|
{ permanent: true },
|
|
|
|
);
|
|
|
|
|
2022-02-14 00:58:23 +01:00
|
|
|
for (const [i, { migrate }] of migrationsToExecute.entries()) {
|
2021-01-20 22:11:53 +01:00
|
|
|
const currentMigrationVersion = oldMigrationVersion + i + 1;
|
2021-06-26 14:32:40 +02:00
|
|
|
logger.info("executing migration script ", currentMigrationVersion);
|
2021-01-19 03:31:40 +01:00
|
|
|
try {
|
2022-02-14 00:58:23 +01:00
|
|
|
await migrate();
|
2021-07-07 19:22:35 +02:00
|
|
|
getGame().settings.set("ds4", "systemMigrationVersion", currentMigrationVersion);
|
2021-01-19 03:31:40 +01:00
|
|
|
} catch (err) {
|
2021-02-21 03:40:54 +01:00
|
|
|
notifications.error(
|
2021-07-07 19:22:35 +02:00
|
|
|
getGame().i18n.format("DS4.ErrorDuringMigration", {
|
2021-01-20 22:11:53 +01:00
|
|
|
currentVersion: oldMigrationVersion,
|
|
|
|
targetVersion: targetMigrationVersion,
|
|
|
|
migrationVersion: currentMigrationVersion,
|
2021-01-19 03:31:40 +01:00
|
|
|
}),
|
|
|
|
{ permanent: true },
|
|
|
|
);
|
2021-09-12 17:48:14 +02:00
|
|
|
logger.error("Failed ds4 system migration:", err);
|
2021-01-19 03:31:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-21 03:40:54 +01:00
|
|
|
notifications.info(
|
2021-07-07 19:22:35 +02:00
|
|
|
getGame().i18n.format("DS4.InfoSystemUpdateCompleted", {
|
2021-01-20 22:11:53 +01:00
|
|
|
currentVersion: oldMigrationVersion,
|
|
|
|
targetVersion: targetMigrationVersion,
|
2021-01-19 03:31:40 +01:00
|
|
|
}),
|
|
|
|
{ permanent: true },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 00:58:23 +01:00
|
|
|
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 },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 22:11:53 +01:00
|
|
|
function getTargetMigrationVersion(): number {
|
|
|
|
return migrations.length;
|
|
|
|
}
|
2021-01-19 03:31:40 +01:00
|
|
|
|
2022-02-14 00:58:23 +01:00
|
|
|
interface Migration {
|
|
|
|
migrate: () => Promise<void>;
|
|
|
|
migrateCompendium: (pack: CompendiumCollection<CompendiumCollection.Metadata>) => Promise<void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const migrations: Migration[] = [migration001, migration002, migration003, migration004, migration005];
|
2021-01-20 22:11:53 +01:00
|
|
|
|
|
|
|
function isFirstWorldStart(migrationVersion: number): boolean {
|
|
|
|
return migrationVersion < 0;
|
2021-01-19 03:31:40 +01:00
|
|
|
}
|
2021-01-20 22:11:53 +01:00
|
|
|
|
|
|
|
export const migration = {
|
2022-02-14 00:58:23 +01:00
|
|
|
migrate,
|
|
|
|
migrateFromTo,
|
|
|
|
getTargetMigrationVersion,
|
|
|
|
migrateCompendiumFromTo,
|
2021-01-20 22:11:53 +01:00
|
|
|
};
|