2021-06-26 14:32:40 +02:00
|
|
|
import logger from "./logger";
|
2021-01-20 22:11:53 +01:00
|
|
|
import { migrate as migrate001 } from "./migrations/001";
|
2021-02-21 03:40:54 +01:00
|
|
|
import { migrate as migrate002 } from "./migrations/002";
|
2021-02-21 18:52:28 +01:00
|
|
|
import { migrate as migrate003 } from "./migrations/003";
|
2021-05-13 19:59:44 +02:00
|
|
|
import { migrate as migrate004 } from "./migrations/004";
|
2021-02-21 18:52:28 +01:00
|
|
|
|
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-02-07 13:51:20 +01:00
|
|
|
if (!game.user?.isGM) {
|
2021-01-19 03:31:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-22 09:04:45 +01:00
|
|
|
const oldMigrationVersion = game.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)) {
|
|
|
|
game.settings.set("ds4", "systemMigrationVersion", targetMigrationVersion);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return migrateFromTo(oldMigrationVersion, targetMigrationVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function migrateFromTo(oldMigrationVersion: number, targetMigrationVersion: number): Promise<void> {
|
2021-02-07 13:51:20 +01:00
|
|
|
if (!game.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-01-19 03:31:40 +01:00
|
|
|
game.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 },
|
|
|
|
);
|
|
|
|
|
2021-01-20 22:11:53 +01:00
|
|
|
for (const [i, migration] of migrationsToExecute.entries()) {
|
|
|
|
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 {
|
2021-01-20 22:11:53 +01:00
|
|
|
await migration();
|
|
|
|
game.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-01-19 03:31:40 +01:00
|
|
|
game.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 },
|
|
|
|
);
|
|
|
|
err.message = `Failed ds4 system migration: ${err.message}`;
|
2021-06-26 14:32:40 +02:00
|
|
|
logger.error(err);
|
2021-01-19 03:31:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-21 03:40:54 +01:00
|
|
|
notifications.info(
|
2021-01-19 03:31:40 +01:00
|
|
|
game.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 },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 22:11:53 +01:00
|
|
|
function getTargetMigrationVersion(): number {
|
|
|
|
return migrations.length;
|
|
|
|
}
|
2021-01-19 03:31:40 +01:00
|
|
|
|
2021-05-13 19:59:44 +02:00
|
|
|
const migrations: Array<() => Promise<void>> = [migrate001, migrate002, migrate003, migrate004];
|
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 = {
|
|
|
|
migrate: migrate,
|
|
|
|
migrateFromTo: migrateFromTo,
|
|
|
|
getTargetMigrationVersion: getTargetMigrationVersion,
|
|
|
|
};
|