Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
Hooks.cpp
Go to the documentation of this file.
1#include "Hooks.h"
2
3#include <string>
4
5#include <Logger/Logger.h>
6
7#include "Offsets.h"
8#include "IBaseApi.h"
9#include "detours/detours.h"
10
11namespace API
12{
14 {
15 //No Longer Required.
16 }
17
18 bool Hooks::SetHookInternal(const std::string& func_name, LPVOID detour, LPVOID* original)
19 {
20 LPVOID target = Offsets::Get().GetAddress(func_name);
21 if (target == nullptr)
22 {
23 Log::GetLog()->error("{} does not exist", func_name);
24 return false;
25 }
26
27 auto& hook_vector = all_hooks_[func_name];
28
29 LPVOID new_target = hook_vector.empty()
30 ? target
31 : hook_vector.back()->detour;
32
33
34 if (DetourTransactionBegin())
35 {
36 Log::GetLog()->error("Failed to create Detour Transaction for {}", func_name);
37 DetourTransactionAbort();
38 return false;
39 }
40 if (DetourUpdateThread(GetCurrentThread()))
41 {
42 Log::GetLog()->error("Failed to update thread for {}", func_name);
43 DetourTransactionAbort();
44 return false;
45 }
46 if (DetourAttach(&new_target, detour))
47 {
48 Log::GetLog()->error("Failed to attach hook for {}", func_name);
49 DetourTransactionAbort();
50 return false;
51 }
52
53 if (DetourTransactionCommit())
54 {
55 Log::GetLog()->error("Failed to commit Detour Transaction for {}", func_name);
56 DetourTransactionAbort();
57 return false;
58 }
59 *original = new_target; //same as ppOriginal in MH_CreateHook
60
61 hook_vector.push_back(std::make_shared<Hook>(new_target, detour, original));
62
63 return true;
64 }
65
66 bool Hooks::DisableHook(const std::string& func_name, LPVOID detour)
67 {
68 const LPVOID target = Offsets::Get().GetAddress(func_name);
69 if (target == nullptr)
70 {
71 Log::GetLog()->error("{} does not exist", func_name);
72 return false;
73 }
74
75 auto& hook_vector = all_hooks_[func_name];
76
77 const auto iter = std::find_if(hook_vector.begin(), hook_vector.end(),
78 [detour](const std::shared_ptr<Hook>& hook) -> bool
79 {
80 return hook->detour == detour;
81 });
82
83 if (iter == hook_vector.end())
84 {
85 Log::GetLog()->warn("Failed to find hook ({})", func_name);
86 return false;
87 }
88
89 if (DetourTransactionBegin())
90 {
91 Log::GetLog()->error("Failed to create Detour Transaction for {}", func_name);
92 DetourTransactionAbort();
93 return false;
94 }
95 if (DetourUpdateThread(GetCurrentThread()))
96 {
97 Log::GetLog()->error("Failed to update thread for {}", func_name);
98 DetourTransactionAbort();
99 return false;
100 }
101
102 // Remove all hooks placed on this function
103 for (const auto& hook : hook_vector)
104 {
105 if (DetourDetach(&hook->target, hook->detour))
106 {
107 Log::GetLog()->error("Failed to detach Detour Transaction for {}", func_name);
108 DetourTransactionAbort();
109 return false;
110 }
111 }
112
113 if (DetourTransactionCommit())
114 {
115 Log::GetLog()->error("Failed to commit Detour Transaction for {}", func_name);
116 DetourTransactionAbort();
117 return false;
118 }
119
120 // Remove hook from all_hooks vector
121 hook_vector.erase(std::remove(hook_vector.begin(), hook_vector.end(), *iter), hook_vector.end());
122
123 auto hook_vec(move(hook_vector));
124 hook_vector.clear();
125
126 // Enable all hooks again
127 for (const auto& hook : hook_vec)
128 {
129 SetHookInternal(func_name, hook->detour, hook->original);
130 }
131
132 return true;
133 }
134} // namespace API
135
136// Free function
138{
139 return reinterpret_cast<IHooks&>(*API::game_api->GetHooks());
140}
Definition IBaseApi.h:9
ARK_API IHooks &APIENTRY GetHooks()
Definition Hooks.cpp:137