Move the tweak prevention into a helper function and rename the module hooks w/ compatibility code #26

Merged
Oliver merged 4 commits from feature/change-hook-name into main 2025-12-20 01:39:52 +00:00
20 changed files with 104 additions and 102 deletions

View file

@ -1,6 +1,6 @@
// Settings
import { preventMovementHistory } from "../settings/preventMovementHistory.mjs";
import { toggleMouseBroadcast } from "../settings/toggleMouseBroadcast.mjs";
import { preventMovementHistory } from "../tweaks/preventMovementHistory.mjs";
import { toggleMouseBroadcast } from "../tweaks/toggleMouseBroadcast.mjs";
// Utils
import { Logger } from "../utils/Logger.mjs";

View file

@ -0,0 +1,19 @@
/*
This hook is used to give external modules or systems the ability to interact
with the tweak registration lifecycle to do something before a tweak is registered
or being able to prevent registration of incompatible tweaks.
The hook receives a string indicating which tweak this hook is being called for
and a boolean value indicating if the tweak is considered invasive. Returning
explicit false prevents that tweak from being registered.
Invasive tweaks are additions that manipulate or override Document or helper
classes. An example of an invasive tweak is the "toggleMouseBroadcast",
tweak which replaces the existing "CONFIG.Canvas.layers.controls.layerClass"
class, most of these tweaks do smartly extend from the same CONFIG class
that they replace, however if they override a part of the class that
other modules/systems rely on, then that is a good time to block that
specific tweak's registration.
Call Signature: (tweakKey: string, isInvasive: boolean) => (void | boolean)
*/

View file

@ -1,12 +0,0 @@
/*
This hook is used for invasive hooks that we want to provide the
option for systems and other modules to be able to disable in case
of incompatabilities for whatever reason. This can also be used
internally within this module if we discover incompatabilites with
systems and want to disable it on our side.
This file is meant more documentation than anything at this point in
time.
Call Signature: (settingKey: string) => (void | boolean)
*/

View file

@ -1,13 +0,0 @@
/*
This hook is used to enable any modules that attempt to disable settings
or just want to investigate what settings are enabled to be able to get
a ping with information about which settings where registered entirely
and which weren't. The object that is passed to this is frozen and is
not meant to be edited as you cannot de-register nor prevent setting
registration from this hook. For that see the "oft.preventSetting" hook.
This file is meant more documentation than anything at this point in
time.
Call Signature: (settings: Record<string, boolean>) => void
*/

View file

@ -0,0 +1,9 @@
/*
This hook is used to broadcast the final status of all tweaks within the module,
allowing other modules to either confirm their registration didn't happen or to
do something once all of the module setup has been finalized. Tweak statuses
cannot be blocked or changed from this hook, to prevent a tweak from being
registered you should use the "oft.preRegisterTweak" hook.
Call Signature: (settings: Record<string, string>) => void
*/

View file

@ -1,15 +1,15 @@
// Settings
import { addGlobalDocReferrer } from "../settings/addGlobalDocReferrer.mjs";
import { autoUnpauseOnLoad } from "../settings/autoUnpauseOnLoad.mjs";
import { chatImageLinks } from "../settings/chatImageLinks.mjs";
import { chatSidebarBackground } from "../settings/chatSidebarBackground.mjs";
import { hotbarButtonGap } from "../settings/hotbarButtonGap.mjs";
import { hotbarButtonSize } from "../settings/hotbarButtonSize.mjs";
import { preventTokenRotation } from "../settings/preventTokenRotation.mjs";
import { preventUserConfigOpen } from "../settings/preventUserConfigOpen.mjs";
import { repositionHotbar } from "../settings/repositionHotbar.mjs";
import { startingSidebarTab } from "../settings/startingSidebarTab.mjs";
import { startSidebarExpanded } from "../settings/startSidebarExpanded.mjs";
import { addGlobalDocReferrer } from "../tweaks/addGlobalDocReferrer.mjs";
import { autoUnpauseOnLoad } from "../tweaks/autoUnpauseOnLoad.mjs";
import { chatImageLinks } from "../tweaks/chatImageLinks.mjs";
import { chatSidebarBackground } from "../tweaks/chatSidebarBackground.mjs";
import { hotbarButtonGap } from "../tweaks/hotbarButtonGap.mjs";
import { hotbarButtonSize } from "../tweaks/hotbarButtonSize.mjs";
import { preventTokenRotation } from "../tweaks/preventTokenRotation.mjs";
import { preventUserConfigOpen } from "../tweaks/preventUserConfigOpen.mjs";
import { repositionHotbar } from "../tweaks/repositionHotbar.mjs";
import { startingSidebarTab } from "../tweaks/startingSidebarTab.mjs";
import { startSidebarExpanded } from "../tweaks/startSidebarExpanded.mjs";
// Apps
import { DevSettingsMenu } from "../apps/DevSettingsMenu.mjs";
@ -51,7 +51,16 @@ Hooks.on(`setup`, () => {
preventTokenRotation();
preventUserConfigOpen();
Hooks.callAll(`oft.settingStatuses`, deepFreeze(status));
// Compatibility Code
if (Hooks.events[`oft.settingStatuses`] != null) {
foundry.utils.logCompatibilityWarning(
`The hook "${__ID__}.settingStatuses" has been renamed "${__ID__}.tweakStatuses".`,
{ since: `v1.2.0`, until: `v2.0.0`, stack: false, once: true },
);
Hooks.callAll(`oft.settingStatuses`, deepFreeze(status));
};
Hooks.callAll(`${__ID__}.tweakStatuses`, deepFreeze(status));
game.modules.get(__ID__).api = deepFreeze({
settings: status,
});

View file

@ -1,12 +1,14 @@
import { SettingStatusEnum, status } from "../utils/SettingStatus.mjs";
import { __ID__ } from "../consts.mjs";
import { Logger } from "../utils/Logger.mjs";
import { preventTweakRegistration } from "../utils/preRegisterTweak.mjs";
import { registerDevSetting } from "../utils/SubMenuSettings.mjs";
const key = `addGlobalDocReferrer`;
export function addGlobalDocReferrer() {
status[key] = SettingStatusEnum.Unknown;
if (preventTweakRegistration(key)) { return };
// #region Registration
Logger.log(`Registering setting: ${key}`);

View file

@ -1,19 +1,14 @@
import { SettingStatusEnum, status } from "../utils/SettingStatus.mjs";
import { __ID__ } from "../consts.mjs";
import { Logger } from "../utils/Logger.mjs";
import { preventTweakRegistration } from "../utils/preRegisterTweak.mjs";
import { registerDevSetting } from "../utils/SubMenuSettings.mjs";
const key = `autoUnpauseOnLoad`;
export function autoUnpauseOnLoad() {
status[key] = SettingStatusEnum.Unknown;
const prevented = Hooks.call(`${__ID__}.preventSetting`, key);
if (!prevented) {
Logger.log(`Preventing setting "${key}" from being registered`);
status[key] = SettingStatusEnum.Blocked;
return;
};
if (preventTweakRegistration(key)) { return };
// #region Registration
Logger.log(`Registering setting: ${key}`);

View file

@ -1,8 +1,9 @@
import { SettingStatusEnum, status } from "../utils/SettingStatus.mjs";
import { __ID__ } from "../consts.mjs";
import { Logger } from "../utils/Logger.mjs";
import { preventTweakRegistration } from "../utils/preRegisterTweak.mjs";
const { DialogV2 } = foundry.applications.api;
// const { DialogV2 } = foundry.applications.api;
const key = `chatImageLinks`;
const IMAGE_TYPES = [
@ -15,13 +16,7 @@ const IMAGE_TYPES = [
export function chatImageLinks() {
status[key] = SettingStatusEnum.Unknown;
const prevented = Hooks.call(`${__ID__}.preventSetting`, key);
if (!prevented) {
Logger.log(`Preventing setting "${key}" from being registered`);
status[key] = SettingStatusEnum.Blocked;
return;
};
if (preventTweakRegistration(key)) { return };
// #region Registration
Logger.log(`Registering setting: ${key}`);

View file

@ -1,11 +1,13 @@
import { SettingStatusEnum, status } from "../utils/SettingStatus.mjs";
import { __ID__ } from "../consts.mjs";
import { Logger } from "../utils/Logger.mjs";
import { preventTweakRegistration } from "../utils/preRegisterTweak.mjs";
const key = `chatSidebarBackground`;
export function chatSidebarBackground() {
status[key] = SettingStatusEnum.Unknown;
if (preventTweakRegistration(key)) { return };
// #region Registration
Logger.log(`Registering setting: ${key}`);

View file

@ -1,19 +1,14 @@
import { SettingStatusEnum, status } from "../utils/SettingStatus.mjs";
import { __ID__ } from "../consts.mjs";
import { Logger } from "../utils/Logger.mjs";
import { preventTweakRegistration } from "../utils/preRegisterTweak.mjs";
import { registerCategorySetting } from "../utils/SubMenuSettings.mjs";
const key = `hotbarButtonGap`;
export function hotbarButtonGap() {
status[key] = SettingStatusEnum.Unknown;
const prevented = Hooks.call(`${__ID__}.preventSetting`, key);
if (!prevented) {
Logger.log(`Preventing setting "${key}" from being registered`);
status[key] = SettingStatusEnum.Blocked;
return;
};
if (preventTweakRegistration(key)) { return };
// #region Registration
Logger.log(`Registering setting: ${key}`);

View file

@ -1,19 +1,14 @@
import { SettingStatusEnum, status } from "../utils/SettingStatus.mjs";
import { __ID__ } from "../consts.mjs";
import { Logger } from "../utils/Logger.mjs";
import { preventTweakRegistration } from "../utils/preRegisterTweak.mjs";
import { registerCategorySetting } from "../utils/SubMenuSettings.mjs";
const key = `hotbarButtonSize`;
export function hotbarButtonSize() {
status[key] = SettingStatusEnum.Unknown;
const prevented = Hooks.call(`${__ID__}.preventSetting`, key);
if (!prevented) {
Logger.log(`Preventing setting "${key}" from being registered`);
status[key] = SettingStatusEnum.Blocked;
return;
};
if (preventTweakRegistration(key)) { return };
// #region Registration
Logger.log(`Registering setting: ${key}`);

View file

@ -1,18 +1,13 @@
import { SettingStatusEnum, status } from "../utils/SettingStatus.mjs";
import { __ID__ } from "../consts.mjs";
import { Logger } from "../utils/Logger.mjs";
import { preventTweakRegistration } from "../utils/preRegisterTweak.mjs";
const key = `preventMovementHistory`;
export function preventMovementHistory() {
status[key] = SettingStatusEnum.Unknown;
const prevented = Hooks.call(`${__ID__}.preventSetting`, key);
if (!prevented) {
Logger.log(`Preventing setting "${key}" from being registered`);
status[key] = SettingStatusEnum.Blocked;
return;
};
if (preventTweakRegistration(key, true)) { return };
// #region Registration
Logger.log(`Registering setting: ${key}`);

View file

@ -1,18 +1,13 @@
import { SettingStatusEnum, status } from "../utils/SettingStatus.mjs";
import { __ID__ } from "../consts.mjs";
import { Logger } from "../utils/Logger.mjs";
import { preventTweakRegistration } from "../utils/preRegisterTweak.mjs";
const key = `preventTokenRotation`;
export function preventTokenRotation() {
status[key] = SettingStatusEnum.Unknown;
const prevented = Hooks.call(`${__ID__}.preventSetting`, key);
if (!prevented) {
Logger.log(`Preventing setting "${key}" from being registered`);
status[key] = SettingStatusEnum.Blocked;
return;
};
if (preventTweakRegistration(key)) { return };
/** @type {number|null} */
let hookID = null;

View file

@ -1,18 +1,13 @@
import { SettingStatusEnum, status } from "../utils/SettingStatus.mjs";
import { __ID__ } from "../consts.mjs";
import { Logger } from "../utils/Logger.mjs";
import { preventTweakRegistration } from "../utils/preRegisterTweak.mjs";
const key = `preventUserConfigOpen`;
export function preventUserConfigOpen() {
status[key] = SettingStatusEnum.Unknown;
const prevented = Hooks.call(`${__ID__}.preventSetting`, key);
if (!prevented) {
Logger.log(`Preventing setting "${key}" from being registered`);
status[key] = SettingStatusEnum.Blocked;
return;
};
if (preventTweakRegistration(key)) { return };
// #region Registration
Logger.log(`Registering setting: ${key}`);

View file

@ -1,19 +1,14 @@
import { SettingStatusEnum, status } from "../utils/SettingStatus.mjs";
import { __ID__ } from "../consts.mjs";
import { Logger } from "../utils/Logger.mjs";
import { preventTweakRegistration } from "../utils/preRegisterTweak.mjs";
import { registerCategorySetting } from "../utils/SubMenuSettings.mjs";
const key = `repositionHotbar`;
export function repositionHotbar() {
status[key] = SettingStatusEnum.Unknown;
const prevented = Hooks.call(`${__ID__}.preventSetting`, key);
if (!prevented) {
Logger.log(`Preventing setting "${key}" from being registered`);
status[key] = SettingStatusEnum.Blocked;
return;
};
if (preventTweakRegistration(key)) { return };
// #region Registration
Logger.log(`Registering setting: ${key}`);

View file

@ -1,11 +1,13 @@
import { SettingStatusEnum, status } from "../utils/SettingStatus.mjs";
import { __ID__ } from "../consts.mjs";
import { Logger } from "../utils/Logger.mjs";
import { preventTweakRegistration } from "../utils/preRegisterTweak.mjs";
const key = `startSidebarExpanded`;
export function startSidebarExpanded() {
status[key] = SettingStatusEnum.Unknown;
if (preventTweakRegistration(key)) { return };
// #region Registration
Logger.log(`Registering setting: ${key}`);

View file

@ -1,11 +1,13 @@
import { SettingStatusEnum, status } from "../utils/SettingStatus.mjs";
import { __ID__ } from "../consts.mjs";
import { Logger } from "../utils/Logger.mjs";
import { preventTweakRegistration } from "../utils/preRegisterTweak.mjs";
const key = `startingSidebarTab`;
export function startingSidebarTab() {
status[key] = SettingStatusEnum.Unknown;
if (preventTweakRegistration(key)) { return };
// #region Registration
Logger.log(`Registering setting: ${key}`);

View file

@ -1,6 +1,7 @@
import { SettingStatusEnum, status } from "../utils/SettingStatus.mjs";
import { __ID__ } from "../consts.mjs";
import { Logger } from "../utils/Logger.mjs";
import { preventTweakRegistration } from "../utils/preRegisterTweak.mjs";
const key = `toggleMouseBroadcast`;
@ -9,14 +10,9 @@ let notifID = null;
export function toggleMouseBroadcast() {
status[key] = SettingStatusEnum.Unknown;
if (preventTweakRegistration(key, true)) { return };
// #region Registration
const prevented = Hooks.call(`${__ID__}.preventSetting`, key);
if (!prevented) {
Logger.log(`Preventing setting "${key}" from being registered`);
status[key] = SettingStatusEnum.Blocked;
return;
};
Logger.log(`Registering setting: ${key}`);
// MARK: setting

View file

@ -0,0 +1,26 @@
import { SettingStatusEnum, status } from "./SettingStatus.mjs";
import { __ID__ } from "../consts.mjs";
import { Logger } from "./Logger.mjs";
export function preventTweakRegistration(key, invasive = false) {
let prevented = Hooks.call(`${__ID__}.preRegisterTweak`, key, invasive);
// Compatibility Code
if (Hooks.events[`${__ID__}.preventSetting`] != null) {
foundry.utils.logCompatibilityWarning(
`The hook "${__ID__}.preventSetting" has been renamed "${__ID__}.registerTweak".`,
{ since: `v1.2.0`, until: `v2.0.0`, stack: false, once: true },
);
if (prevented !== false) {
prevented = Hooks.call(`${__ID__}.preventSetting`, key);
};
};
if (!prevented) {
Logger.log(`Preventing setting "${key}" from being registered`);
status[key] = SettingStatusEnum.Blocked;
return true;
};
return false;
};