Skip to content

Commit

Permalink
fix(mod_api): detour function
Browse files Browse the repository at this point in the history
  • Loading branch information
Curve committed Sep 1, 2023
1 parent 2220334 commit 6a20646
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/manager.impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,19 @@ namespace loader
return std::make_unique<std::function<void()>>(restore);
};

table["detour"] = [](sol::table table, const std::string &func, sol::function callback)
{
auto original = table.get<sol::function>(func);

const auto detour = [=, callback = std::move(callback)](const sol::variadic_args &args)
{
return callback.call<sol::object>(original, sol::as_args(args));
};

const auto restore = [=]() mutable
{
table[func] = original;
};

table[func] = detour;

return std::make_unique<std::function<void()>>(restore);
};
lua->do_string(R"lua(
mod_api.hooks.detour = function(table, func, callback)
local original = table[func]
table[func] = function(...)
return callback(original, ...)
end
return function()
table[func] = original
end
end
)lua");

auto require = lua->get<std::function<sol::object(const std::string &)>>("require");
(*lua)["require"] = [this, require](const std::string &module)
Expand Down

0 comments on commit 6a20646

Please sign in to comment.