5#include <Logger/Logger.h>
9#include "detours/detours.h"
18 bool Hooks::SetHookInternal(
const std::string& func_name, LPVOID detour, LPVOID* original)
20 LPVOID target = Offsets::Get().GetAddress(func_name);
21 if (target ==
nullptr)
23 Log::GetLog()->error(
"{} does not exist", func_name);
27 auto& hook_vector = all_hooks_[func_name];
29 LPVOID new_target = hook_vector.empty()
31 : hook_vector.back()->detour;
34 if (DetourTransactionBegin())
36 Log::GetLog()->error(
"Failed to create Detour Transaction for {}", func_name);
37 DetourTransactionAbort();
40 if (DetourUpdateThread(GetCurrentThread()))
42 Log::GetLog()->error(
"Failed to update thread for {}", func_name);
43 DetourTransactionAbort();
46 if (DetourAttach(&new_target, detour))
48 Log::GetLog()->error(
"Failed to attach hook for {}", func_name);
49 DetourTransactionAbort();
53 if (DetourTransactionCommit())
55 Log::GetLog()->error(
"Failed to commit Detour Transaction for {}", func_name);
56 DetourTransactionAbort();
59 *original = new_target;
61 hook_vector.push_back(std::make_shared<Hook>(new_target, detour, original));
66 bool Hooks::DisableHook(
const std::string& func_name, LPVOID detour)
68 const LPVOID target = Offsets::Get().GetAddress(func_name);
69 if (target ==
nullptr)
71 Log::GetLog()->error(
"{} does not exist", func_name);
75 auto& hook_vector = all_hooks_[func_name];
77 const auto iter = std::find_if(hook_vector.begin(), hook_vector.end(),
78 [detour](
const std::shared_ptr<Hook>& hook) ->
bool
80 return hook->detour == detour;
83 if (iter == hook_vector.end())
85 Log::GetLog()->warn(
"Failed to find hook ({})", func_name);
89 if (DetourTransactionBegin())
91 Log::GetLog()->error(
"Failed to create Detour Transaction for {}", func_name);
92 DetourTransactionAbort();
95 if (DetourUpdateThread(GetCurrentThread()))
97 Log::GetLog()->error(
"Failed to update thread for {}", func_name);
98 DetourTransactionAbort();
103 for (
const auto& hook : hook_vector)
105 if (DetourDetach(&hook->target, hook->detour))
107 Log::GetLog()->error(
"Failed to detach Detour Transaction for {}", func_name);
108 DetourTransactionAbort();
113 if (DetourTransactionCommit())
115 Log::GetLog()->error(
"Failed to commit Detour Transaction for {}", func_name);
116 DetourTransactionAbort();
121 hook_vector.erase(std::remove(hook_vector.begin(), hook_vector.end(), *iter), hook_vector.end());
123 auto hook_vec(move(hook_vector));
127 for (
const auto& hook : hook_vec)
129 SetHookInternal(func_name, hook->detour, hook->original);
139 return reinterpret_cast<IHooks&>(*API::game_api->GetHooks());
ARK_API IHooks &APIENTRY GetHooks()