From 75e571b9ac4d2ad719807a9ee19ead4e45e530e8 Mon Sep 17 00:00:00 2001 From: praydog Date: Sun, 19 May 2024 12:25:45 -0700 Subject: [PATCH] Lua: Possible fix for hook storage not always working --- src/HookManager.cpp | 6 +++++- src/mods/ScriptRunner.hpp | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/HookManager.cpp b/src/HookManager.cpp index 22d3624e2..d723c9a48 100644 --- a/src/HookManager.cpp +++ b/src/HookManager.cpp @@ -1,3 +1,5 @@ +#include + #include #include @@ -127,7 +129,9 @@ void HookManager::HookedFn::on_post_hook() { auto& ret_val = storage->ret_val; //auto& ret_addr = storage->ret_addr_post; - for (const auto& cb : cbs) { + // Iterate in reverse because it helps with the hook storage we use in Lua + // It should help with any other system that wants to use a stack-based storage system. + for (const auto& cb : cbs | std::views::reverse) { if (cb.post_fn) { // Valid return address in recursion scenario is no longer supported with this API. // We just pass ret_addr_pre for now, even though it's not accurate. diff --git a/src/mods/ScriptRunner.hpp b/src/mods/ScriptRunner.hpp index 81a07749e..8a79348ff 100644 --- a/src/mods/ScriptRunner.hpp +++ b/src/mods/ScriptRunner.hpp @@ -178,7 +178,7 @@ class ScriptState { auto it = m_hook_storage.find(thread_hash); if (it != m_hook_storage.end()) { if (!it->second.empty()) { - it->second.pop_front(); + it->second.pop_back(); } } @@ -196,7 +196,7 @@ class ScriptState { auto it = m_hook_storage.find(thread_hash); if (it != m_hook_storage.end()) { if (!it->second.empty()) { - return it->second.front(); + return it->second.back(); } }