From 7a7673432a2168f45fe2b5759cd2e7d9907eb79f Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Fri, 13 Sep 2024 10:47:25 +0300 Subject: [PATCH 1/2] Calculate entry hash before replacing macros Fixes #2762 --- public/scripts/world-info.js | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/public/scripts/world-info.js b/public/scripts/world-info.js index 77dae1cad..92eae32d1 100644 --- a/public/scripts/world-info.js +++ b/public/scripts/world-info.js @@ -108,6 +108,7 @@ const KNOWN_DECORATORS = ['@@activate', '@@dont_activate']; * @property {number} [cooldown] The cooldown of the entry * @property {number} [delay] The delay of the entry * @property {string[]} [decorators] Array of decorators for the entry + * @property {number} [hash] The hash of the entry */ /** @@ -382,12 +383,6 @@ class WorldInfoBuffer { * Represents a timed effects manager for World Info. */ class WorldInfoTimedEffects { - /** - * Cache for entry hashes. Uses weak map to avoid memory leaks. - * @type {WeakMap} - */ - #entryHashCache = new WeakMap(); - /** * Array of chat messages. * @type {string[]} @@ -485,13 +480,7 @@ class WorldInfoTimedEffects { * @returns {number} String hash */ #getEntryHash(entry) { - if (this.#entryHashCache.has(entry)) { - return this.#entryHashCache.get(entry); - } - - const hash = getStringHash(JSON.stringify(entry)); - this.#entryHashCache.set(entry, hash); - return hash; + return entry.hash; } /** @@ -3603,10 +3592,11 @@ export async function getSortedEntries() { // Chat lore always goes first entries = [...chatLore.sort(sortFn), ...entries]; - // Parse decorators + // Calculate hash and parse decorators entries = entries.map((entry) => { + const hash = getStringHash(JSON.stringify(entry)); const [decorators, content] = parseDecorators(entry.content || ''); - return { ...entry, decorators, content }; + return { ...entry, decorators, content, hash }; }); console.debug(`[WI] Found ${entries.length} world lore entries. Sorted by strategy`, Object.entries(world_info_insertion_strategy).find((x) => x[1] === world_info_character_strategy)); From 34b2707895daa74d256ba95081d308a7f6098369 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Fri, 13 Sep 2024 10:52:35 +0300 Subject: [PATCH 2/2] Try to preserve old hashes --- public/scripts/world-info.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/public/scripts/world-info.js b/public/scripts/world-info.js index 92eae32d1..20ccfda0c 100644 --- a/public/scripts/world-info.js +++ b/public/scripts/world-info.js @@ -3592,11 +3592,13 @@ export async function getSortedEntries() { // Chat lore always goes first entries = [...chatLore.sort(sortFn), ...entries]; - // Calculate hash and parse decorators + // Calculate hash and parse decorators. Split maps to preserve old hashes. entries = entries.map((entry) => { - const hash = getStringHash(JSON.stringify(entry)); const [decorators, content] = parseDecorators(entry.content || ''); - return { ...entry, decorators, content, hash }; + return { ...entry, decorators, content }; + }).map((entry) => { + const hash = getStringHash(JSON.stringify(entry)); + return { ...entry, hash }; }); console.debug(`[WI] Found ${entries.length} world lore entries. Sorted by strategy`, Object.entries(world_info_insertion_strategy).find((x) => x[1] === world_info_character_strategy));