Skip to content

Commit

Permalink
feat: adjust migrations to not cause a reload
Browse files Browse the repository at this point in the history
Additionally, on failed world migrations, effects are disabled to
prevent any potential scene crashes.
  • Loading branch information
ghost91- committed Dec 16, 2021
1 parent e3942bb commit 89f707e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
"FXMASTER.Delete": "Delete Effects",
"FXMASTER.DeleteConfirm": "<p><strong>Are you sure?</strong> Every effect will be deleted.</p>",
"FXMASTER.Enable": "Enable Effects",
"FXMASTER.DisableAll": "Disable Effects For Everybody",
"FXMASTER.DisableAllHint": "Setting this will disable effects for all users. It overrides the enable flag. It's mostly a safe guard to deactivate effects when migrations fail. You shouldn't need to set this yourself. After changing this setting, a manual reload is required.",
"FXMASTER.Angle": "Angle",
"FXMASTER.AngleHint": "Initial angle of the effect compared to the left to right axis",
"FXMASTER.Anchor": "Anchor",
Expand All @@ -58,7 +60,7 @@
"FXMASTER.CastExpand": "Expand to target",
"FXMASTER.CastRotate": "Rotate",
"FXMASTER.MigrationWorldStart": "Performing world migrations for FXMaster. Please be patient and do not close your game or shut down your server.",
"FXMASTER.MigrationWorldCompletedWithErrors": "World migrations for FXMaster completed with errors. For more details, please take a look at the developer console (F12).",
"FXMASTER.MigrationWorldCompletedWithErrors": "World migrations for FXMaster completed with errors. For more details, please take a look at the developer console (F12). Effects have been disabled via the \"Disable Effects For Everybody\" setting to avoid any potential scene crashes. Only reactivate them if you are sure it's safe to do so. You might want to remove all weather and filter effects from any affected scenes first.",
"FXMASTER.MigrationWorldCompletedSuccessfully": "World migrations for FXMaster completed successfully.",
"FXMASTER.MigrationClientStart": "Performing client migrations for FXMaster. Please be patient and do not close your game.",
"FXMASTER.MigrationClientCompletedWithErrors": "Client migrations for FXMaster completed with errors. For more details, please take a look at the developer console (F12).",
Expand Down
24 changes: 15 additions & 9 deletions src/module/fxmaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { registerHooks } from "./hooks.js";
import { FXMASTER } from "./config.js";
import { WeatherLayer } from "./weatherEffects/WeatherLayer.js";
import { filterManager } from "./filterEffects/FilterManager.js";
import { isOnTargetMigration, migrate } from "./migration.js";
import { executeWhenWorldIsMigratedToLatest, isOnTargetMigration, migrate } from "./migration.js";
import { SpecialsLayer } from "./specialEffects/SpecialsLayer.js";
import { registerHelpers } from "./helpers.js";
import { registerGetSceneControlButtonsHook } from "./controls.js";
Expand Down Expand Up @@ -57,24 +57,30 @@ Hooks.once("ready", () => {
});

Hooks.on("canvasInit", () => {
if (!game.settings.get("fxmaster", "enable") || !isOnTargetMigration()) {
if (!game.settings.get("fxmaster", "enable") || game.settings.get("fxmaster", "disableAll")) {
return;
}
parseSpecialEffects();
filterManager.clear();
});

Hooks.on("canvasReady", async () => {
if (!game.settings.get("fxmaster", "enable") || !isOnTargetMigration()) {
return;
}
await filterManager.activate();
canvas.fxmaster.drawWeather();
canvas.fxmaster.updateMask();
executeWhenWorldIsMigratedToLatest(async () => {
if (game.settings.get("fxmaster", "disableAll")) {
return;
}
await filterManager.activate();
canvas.fxmaster.drawWeather();
canvas.fxmaster.updateMask();
});
});

Hooks.on("updateScene", (scene, data) => {
if (!game.settings.get("fxmaster", "enable") || !isOnTargetMigration()) {
if (
!game.settings.get("fxmaster", "enable") ||
game.settings.get("fxmaster", "disableAll") ||
!isOnTargetMigration()
) {
return;
}
if (hasProperty(data, "flags.fxmaster")) {
Expand Down
2 changes: 1 addition & 1 deletion src/module/logger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const loggingContext = "fxmaster";
const loggingContext = "FXMaster";
const loggingSeparator = "|";

/**
Expand Down
26 changes: 26 additions & 0 deletions src/module/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async function migrateWolrd() {
}

if (isError) {
await game.settings.set("fxmaster", "disableAll", true);
ui.notifications.error("FXMASTER.MigrationWorldCompletedWithErrors", { localize: true, permanent: true });
} else {
ui.notifications.info("FXMASTER.MigrationWorldCompletedSuccessfully", { localize: true, permanent: true });
Expand Down Expand Up @@ -133,3 +134,28 @@ export function isOnTargetMigration() {
game.settings.get("fxmaster", "clientMigration") === targetClientMigration
);
}

/**
* Schedule a callback to be executed as soon as the world has been migrated to the target migration. If it is on the
* latest migration already, the callback is executed immediately. Callbacks are executed in the order in that they have
* been registered.
* @param {() => (Promise<void> | void)} callback A callback to execute when the world is migrated
*/
export function executeWhenWorldIsMigratedToLatest(callback) {
if (game.settings.get("fxmaster", "migration") !== targetServerMigration) {
worldMigrationCallbacks.push(callback);
} else {
callback();
}
}

/** @type {Array<() => (Promise<void> | void)>} */
const worldMigrationCallbacks = [];

export async function onWorldMigrated() {
if (game.settings.get("fxmaster", "migration") === targetServerMigration) {
for (const callback of worldMigrationCallbacks) {
await callback();
}
}
}
13 changes: 12 additions & 1 deletion src/module/settings.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { onWorldMigrated } from "./migration.js";

export const registerSettings = function () {
game.settings.register("fxmaster", "enable", {
name: "FXMASTER.Enable",
Expand All @@ -22,7 +24,7 @@ export const registerSettings = function () {
scope: "world",
type: Number,
config: false,
onChange: debouncedReload,
onChange: onWorldMigrated,
});

game.settings.register("fxmaster", "clientMigration", {
Expand All @@ -48,6 +50,15 @@ export const registerSettings = function () {
},
onChange: debouncedReload,
});

game.settings.register("fxmaster", "disableAll", {
name: "FXMASTER.DisableAll",
hint: "FXMASTER.DisableAllHint",
default: false,
scope: "world",
type: Boolean,
config: true,
});
};

export const debouncedReload = foundry.utils.debounce(() => {
Expand Down

0 comments on commit 89f707e

Please sign in to comment.