diff --git a/src/functions/cacheClearer.ts b/src/functions/cacheClearer.ts index 20d206e..f1a4ede 100644 --- a/src/functions/cacheClearer.ts +++ b/src/functions/cacheClearer.ts @@ -4,7 +4,7 @@ import { patchFunction, SDK, HOOK_PRIORITIES } from "../util/modding"; import { fbcSettings } from "../util/settings"; import { waitFor } from "../util/utils"; -type ChatRoomMenuButtonsWCE = (ChatRoomMenuButton | "clearCache")[]; +type ChatRoomMenuButtonWCE = ChatRoomMenuButton | "clearCache"; export default function cacheClearer(): void { const cacheClearInterval = 1 * 60 * 60 * 1000; @@ -26,35 +26,49 @@ export default function cacheClearer(): void { SDK.hookFunction("ChatRoomMenuBuild", HOOK_PRIORITIES.AddBehaviour, (args, next) => { const ret = next(args); - if (fbcSettings.manualCacheClear) (ChatRoomMenuButtons as ChatRoomMenuButtonsWCE).splice(ChatRoomMenuButtons.indexOf("Cut"), 0, "clearCache"); + if (fbcSettings.manualCacheClear) (ChatRoomMenuButtons as ChatRoomMenuButtonWCE[]).splice(ChatRoomMenuButtons.indexOf("Cut"), 0, "clearCache"); return ret; }); - SDK.hookFunction("ChatRoomMenuClick", HOOK_PRIORITIES.AddBehaviour, (args, next) => { - const ret = next(args); - if (fbcSettings.manualCacheClear) { - const Space = 992 / ChatRoomMenuButtons.length; - for (let B = 0; B < ChatRoomMenuButtons.length; B++) { - if (MouseXIn(1005 + Space * B, Space - 2) && (ChatRoomMenuButtons as ChatRoomMenuButtonsWCE)[B] === "clearCache") { - doClearCaches(); + // ToDo: remove once R128 is out + if (GameVersion === "R127") { + SDK.hookFunction("ChatRoomMenuClick", HOOK_PRIORITIES.AddBehaviour, (args, next) => { + const ret = next(args); + if (fbcSettings.manualCacheClear) { + const Space = 992 / ChatRoomMenuButtons.length; + for (let B = 0; B < ChatRoomMenuButtons.length; B++) { + if (MouseXIn(1005 + Space * B, Space - 2) && (ChatRoomMenuButtons[B] as ChatRoomMenuButtonWCE) === "clearCache") { + doClearCaches(); + } } } - } - return ret; - }); + return ret; + }); - patchFunction( - "ChatRoomMenuDraw", - { - 'let suffix = "";': `let suffix = ""; - if (name === "clearCache") { - DrawButton(1005 + Space * Number(idx), 2, Space - 2, 60, "", color, null, "[WCE] clear and reload the drawing cache of all characters"); - DrawImage("Icons/Small/Reset.png", 976 + Space * Number(idx) + Space / 2, 4); - continue; - }`, - }, - "manual clearing and reloading of drawing cache" - ); + patchFunction( + "ChatRoomMenuDraw", + { + 'let suffix = "";': `let suffix = ""; + if (name === "clearCache") { + DrawButton(1005 + Space * Number(idx), 2, Space - 2, 60, "", color, null, "[WCE] clear and reload the drawing cache of all characters"); + DrawImage("Icons/Small/Reset.png", 976 + Space * Number(idx) + Space / 2, 4); + continue; + }`, + }, + "manual clearing and reloading of drawing cache" + ); + } else { + SDK.hookFunction("ChatRoomMenuButtonVisualState", HOOK_PRIORITIES.AddBehaviour, (args, next) => { + if ((args[0] as ChatRoomMenuButtonWCE) !== "clearCache") return next(args); + const state = "Default" as const; + return { image: "Icons/Small/Reset.png", state, hoverText: "[WCE] clear and reload the drawing cache of all characters" }; + }); + + SDK.hookFunction("ChatRoomMenuPerformAction", HOOK_PRIORITIES.AddBehaviour, (args, next) => { + if ((args[0] as ChatRoomMenuButtonWCE) !== "clearCache") return next(args); + return doClearCaches(); + }); + } async function clearCaches(): Promise { const start = Date.now();