Skip to content

Commit

Permalink
fix hook updater, add settings cache
Browse files Browse the repository at this point in the history
  • Loading branch information
esheyw committed Jan 30, 2024
1 parent 0d1c9af commit 542a911
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 51 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
- Fix version string mismatch with foundry package admin (mistake you only make once? I doubt it)

## Version 1.0.2
- Remove debug warnings
- Remove debug warnings

## Version 1.0.3
- Fix hook updater to not double hooks on setting change.
34 changes: 32 additions & 2 deletions scripts/constants.mjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
export const MODULE = 'macro-creation-tweaks';
export const log = game?.pf2emhl?.mhlog ?? ((logable) => console.log(logable));
import { updateHooks } from "./hooks.mjs";

export const MODULE_ID = "macro-creation-tweaks";
export const SETTINGS = {
"append-number": {
config: true,
default: true,
hint: "MacroCreationTweaks.Setting.AppendNumber.Hint",
name: "MacroCreationTweaks.Setting.AppendNumber.Name",
scope: "world",
type: Boolean,
onChange: updateHooks,
},
"delete-empty": {
config: true,
default: true,
hint: "MacroCreationTweaks.Setting.DeleteEmpty.Hint",
name: "MacroCreationTweaks.Setting.DeleteEmpty.Name",
scope: "world",
type: Boolean,
onChange: updateHooks,
},
"change-default-type": {
config: true,
default: true,
hint: "MacroCreationTweaks.Setting.ChangeDefaultType.Hint",
name: "MacroCreationTweaks.Setting.ChangeDefaultType.Name",
scope: "world",
type: Boolean,
onChange: updateHooks,
},
};
26 changes: 15 additions & 11 deletions scripts/hooks.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { getModule } from "./init.mjs";
import { MODULE } from "./init.mjs";
import { setting } from "./settings.mjs";

export const HOOKS = new Map([
["change-default-type", { func: changeDefaultType, hook: "preCreateMacro" }],
["append-number", { func: appendNumber, hook: "preCreateMacro" }],
["delete-empty", { func: deleteEmpty, hook: "closeMacroConfig" }],
]);

function appendNumber(document, data) {
if (data.name === "New Macro") {
document.updateSource({
Expand All @@ -18,22 +24,20 @@ function deleteEmpty(app) {
if (doc.isOwner && doc.command === "") doc.delete();
}

export function updateHooks() {
const { hookIDs } = getModule();
const hooksMap = new Map([
["change-default-type", { func: changeDefaultType, hook: "preCreateMacro" }],
["append-number", { func: appendNumber, hook: "preCreateMacro" }],
["delete-empty", { func: deleteEmpty, hook: "closeMacroConfig" }],
]);
for (const [settingName, data] of hooksMap) {
export function updateHooks() {
const { hookIDs } = MODULE();
for (const [settingName, data] of HOOKS) {
const settingValue = setting(settingName);
if (settingValue) {
const existingHook = hookIDs[settingName] ?? null;
if (existingHook) continue;
hookIDs[settingName] = Hooks.on(data.hook, data.func);
} else {
} else {
const hookID = hookIDs[settingName] ?? null;
if (hookID) {
Hooks.off(data.hook, hookID);
}
delete hookIDs[settingName];
}
}
}
}
18 changes: 13 additions & 5 deletions scripts/init.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { MODULE } from "./constants.mjs";
import { updateHooks } from "./hooks.mjs";
import { registerSettings } from "./settings.mjs";
export const getModule = () => game.modules.get(MODULE);
import { MODULE_ID } from "./constants.mjs";
import { updateHooks, HOOKS } from "./hooks.mjs";
import { registerSettings, updateSettingsCache } from "./settings.mjs";
export const MODULE = () => game.modules.get(MODULE_ID);

Hooks.once("init", () => {
const modObject = getModule();
const modObject = MODULE();
game.mct = {
updateHooks,
updateSettingsCache,
modObject,
HOOKS,
};
modObject.hookIDs ??= {};
modObject.settings ??= {};
registerSettings();
});
Hooks.once("setup", () => {
updateSettingsCache();
updateHooks();
});
59 changes: 27 additions & 32 deletions scripts/settings.mjs
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
import { MODULE } from "./constants.mjs";
import { updateHooks } from "./hooks.mjs";
import { MODULE_ID, SETTINGS } from "./constants.mjs";
import { MODULE } from "./init.mjs";

export function setting(path) {
return game.settings.get(MODULE, path);
}
export function registerSettings() {
game.settings.register(MODULE, "append-number", {
config: true,
default: true,
hint: "MacroCreationTweaks.Setting.AppendNumber.Hint",
name: "MacroCreationTweaks.Setting.AppendNumber.Name",
scope: "world",
type: Boolean,
onChange: updateHooks,
});
game.settings.register(MODULE, "delete-empty", {
config: true,
default: true,
hint: "MacroCreationTweaks.Setting.DeleteEmpty.Hint",
name: "MacroCreationTweaks.Setting.DeleteEmpty.Name",
scope: "world",
type: Boolean,
onChange: updateHooks,
});
game.settings.register(MODULE, "change-default-type", {
config: true,
default: true,
hint: "MacroCreationTweaks.Setting.ChangeDefaultType.Hint",
name: "MacroCreationTweaks.Setting.ChangeDefaultType.Name",
scope: "world",
type: Boolean,
onChange: updateHooks,
});
for (const [setting, data] of Object.entries(SETTINGS)) {
const settingPath = setting.replace("_", ".");
fu.setProperty(MODULE().settings, settingPath, data?.default ?? null);
const originalOnChange = data?.onChange ?? null;
data.onChange = (value) => {
fu.setProperty(MODULE().settings, settingPath, value);
if (originalOnChange) originalOnChange(value);
};
game.settings.register(MODULE_ID, setting, data);
}
}

export function updateSettingsCache() {
const modSettings = MODULE().settings;
for (const setting of Object.keys(SETTINGS)) {
const settingPath = setting.replace("_", ".");
const value = game.settings.get(MODULE_ID, setting);
fu.setProperty(modSettings, settingPath, value);
}
}

export function setting(key) {
const settingPath = key.replace("_", ".");
const cached = fu.getProperty(MODULE().settings, settingPath);
return cached !== undefined ? cached : game.settings.get(MODULE_ID, key);
}

0 comments on commit 542a911

Please sign in to comment.