Skip to content

Commit

Permalink
Lua: Fix hook storage not working correctly with multiple hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Apr 28, 2024
1 parent d83a779 commit 36fe686
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
16 changes: 10 additions & 6 deletions src/mods/ScriptRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "sdk/SF6Utility.hpp"

#include "utility/String.hpp"
#include <utility/ScopeGuard.hpp>

#include "Mods.hpp"

Expand Down Expand Up @@ -63,16 +64,16 @@ uint32_t get_id() {
return std::this_thread::get_id()._Get_underlying_id();
}

sol::object get_hook_storage(sol::this_state s, size_t hash) {
sol::object get_hook_storage(sol::this_state s) {
auto sol_state = sol::state_view{s};
auto state = sol_state.registry()["state"].get<ScriptState*>();
auto result = state->get_hook_storage(get_hash());
auto result = state->get_hook_storage();

if (!result.has_value()) {
if (!result.valid()) {
return sol::make_object(s, sol::lua_nil);
}

return sol::make_object(s, result.value());
return sol::make_object(s, result);
}
}

Expand Down Expand Up @@ -667,9 +668,13 @@ void ScriptState::install_hooks() {
return;
}

const auto thash = std::hash<std::thread::id>{}(std::this_thread::get_id());
utility::ScopeGuard sg{[state, thash] { state->pop_hook_storage(thash); }};

try {
state->m_current_hook_storage = state->get_hook_storage_internal(thash);

if (post_cb.is<sol::nil_t>()) {
state->pop_hook_storage(std::hash<std::thread::id>{}(std::this_thread::get_id()));
return;
}

Expand All @@ -680,7 +685,6 @@ void ScriptState::install_hooks() {
}

ret_val = (uintptr_t)script_result.get<void*>();
state->pop_hook_storage(std::hash<std::thread::id>{}(std::this_thread::get_id()));
} catch (const std::exception& e) {
ScriptRunner::get()->spew_error(e.what());
} catch (...) {
Expand Down
27 changes: 19 additions & 8 deletions src/mods/ScriptRunner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <unordered_map>
#include <memory>
#include <mutex>
#include <deque>
#include <shared_mutex>

#include <Windows.h>
Expand Down Expand Up @@ -166,33 +167,42 @@ class ScriptState {
void push_hook_storage(size_t thread_hash) {
auto it = m_hook_storage.find(thread_hash);
if (it == m_hook_storage.end()) {
it = m_hook_storage.emplace(thread_hash, std::stack<sol::table>{}).first;
it = m_hook_storage.emplace(thread_hash, std::deque<sol::table>{}).first;
}

it->second.push(m_lua.create_table());
it->second.push_back(m_lua.create_table());
m_current_hook_storage = it->second.back();
}

void pop_hook_storage(size_t thread_hash) {
auto it = m_hook_storage.find(thread_hash);
if (it != m_hook_storage.end()) {
if (!it->second.empty()) {
it->second.pop();
it->second.pop_front();
}
}

m_current_hook_storage = sol::nil;
}

sol::reference get_hook_storage() {
return m_current_hook_storage;
}

std::optional<sol::table> get_hook_storage(size_t thread_hash) {
private:
sol::reference get_hook_storage_internal(size_t thread_hash) {
//return m_current_hook_storage;

auto it = m_hook_storage.find(thread_hash);
if (it != m_hook_storage.end()) {
if (!it->second.empty()) {
return it->second.top();
return it->second.front();
}
}

return std::nullopt;
return sol::nil;
}

private:
sol::state m_lua{};

GarbageCollectionData m_gc_data{};
Expand Down Expand Up @@ -221,7 +231,8 @@ class ScriptState {
std::deque<HookDef> m_hooks_to_add{};
std::unordered_map<sdk::REMethodDefinition*, std::vector<HookManager::HookId>> m_hooks{};

std::unordered_map<size_t, std::stack<sol::table>> m_hook_storage{};
std::unordered_map<size_t, std::deque<sol::table>> m_hook_storage{};
sol::reference m_current_hook_storage{};
};

class ScriptRunner : public Mod {
Expand Down

0 comments on commit 36fe686

Please sign in to comment.