-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmtkdll.cpp
230 lines (184 loc) · 6.02 KB
/
fmtkdll.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <Windows.h>
#include <algorithm>
#include <codecvt>
#include <cstring>
#include <cwctype>
#include <detours.h>
#include <locale>
#include <filesystem>
#include <optional>
#include <toml++/toml.h>
#include "fmtkdll.hpp"
#include "instrument.hpp"
#include "logging.hpp"
constexpr const char *FMTK_TOML_PATH = "mods/fmtk/fmtk.toml";
std::vector<FMTKMod *> mods;
void print(const char *msg) { LOG(trace, FMTK, "{}", msg); }
std::string fmtkModsDirectoryPath;
const char *GetModsDirectoryPath() { return fmtkModsDirectoryPath.c_str(); }
void Log(LogLevel level, const char *source, const char *msg) {
switch (level) {
case LogLevel::TRACE:
spdlog::trace("[" + std::string(source) + "] {}", msg);
break;
case LogLevel::DEBUG:
spdlog::debug("[" + std::string(source) + "] {}", msg);
break;
case LogLevel::INFO:
spdlog::info("[" + std::string(source) + "] {}", msg);
break;
case LogLevel::WARN:
spdlog::warn("[" + std::string(source) + "] {}", msg);
break;
case LogLevel::ERR:
spdlog::error("[" + std::string(source) + "] {}", msg);
break;
case LogLevel::CRITICAL:
spdlog::critical("[" + std::string(source) + "] {}", msg);
break;
default:
break;
}
}
void Alias(const char *originalPath, const char *newPath) {
std::wstring originalPathString =
std::filesystem::absolute(originalPath).wstring();
std::transform(originalPathString.begin(), originalPathString.end(),
originalPathString.begin(), std::towlower);
std::wstring newPathString = std::filesystem::absolute(newPath).wstring();
std::transform(newPathString.begin(), newPathString.end(),
newPathString.begin(), std::towlower);
aliases[originalPathString] = newPathString;
}
void Unalias(const char *originalPath) {
std::wstring originalPathString =
std::filesystem::absolute(originalPath).wstring();
std::transform(originalPathString.begin(), originalPathString.end(),
originalPathString.begin(), std::towlower);
aliases.erase(originalPathString);
}
FMTKApi fmtkApi{
GetModsDirectoryPath,
GetPlayerPosition,
RunCommand,
RegisterCommand,
UnregisterCommand,
Log,
Alias,
Unalias,
};
bool loadModDll(const std::filesystem::path &modDllPath) {
HMODULE handle = LoadLibraryA(modDllPath.string().c_str());
if (!handle) {
LOG(error, FMTK, "Could not LoadLibrary: {}, {}", modDllPath.string(), GetLastError());
return false;
}
const FMTKVersion *(*GetFMTKVersion)() =
reinterpret_cast<const FMTKVersion *(*)()>(
GetProcAddress(handle, "GetFMTKVersion"));
if (!handle) {
LOG(error, FMTK, "Does not implement GetFMTKVersion: {}",
modDllPath.string());
return false;
}
const FMTKVersion *modFMTKVersion = GetFMTKVersion();
if (FMTK_VERSION_MAJOR != modFMTKVersion->major ||
FMTK_VERSION_MINOR != modFMTKVersion->minor ||
FMTK_VERSION_PATCH != modFMTKVersion->patch ||
FMTK_VERSION_TWEAK != modFMTKVersion->tweak) {
LOG(error, FMTK,
"The FMTK version and the mods FMTKSDK version must match exactly for "
"the time being.");
return false;
}
FMTKMod *(*GetMod)(const FMTKApi *fmtkApi) =
reinterpret_cast<FMTKMod *(*)(const FMTKApi *fmtkApi)>(
GetProcAddress(handle, "GetMod"));
if (!handle) {
LOG(error, FMTK, "Does not implement GetMod: {}", modDllPath.string());
return false;
}
mods.push_back(GetMod(&fmtkApi));
return true;
}
bool loadModsDirectory(const std::filesystem::path &modsDirectoryPath) {
std::function<void(const std::filesystem::path &)> loadDirectory;
loadDirectory = [&](const std::filesystem::path &dir) {
LOG(trace, FMTK, "Load dir: {}", dir.string());
for (auto it : std::filesystem::directory_iterator(dir)) {
LOG(trace, FMTK, "Processing: {}", it.path().string());
if (it.path().filename().string()[0] != '_') {
if (it.is_directory()) {
loadDirectory(it.path());
} else if (it.path().extension() == ".dll") {
LOG(trace, FMTK, "Load dll: {}", it.path().string());
if (loadModDll(it.path())) {
LOG(trace, FMTK, "Loaded successfully");
} else {
LOG(error, FMTK, "Failed to load: {}", it.path().string());
}
}
}
}
};
if (std::filesystem::exists(modsDirectoryPath)) {
loadDirectory(std::filesystem::absolute(modsDirectoryPath));
} else {
LOG(error, FMTK, "The specified mods directory does not exist");
return false;
}
return true;
}
std::unordered_map<std::wstring, std::wstring> aliases;
BOOL ProcessAttach(HMODULE hDll) {
DetourRestoreAfterWith();
if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
AllocConsole();
}
LOG_INIT("mods/fmtk/fmtk.log");
LOG(trace, FMTK, "Attaching to FUEL");
LONG error = AttachDetours();
if (error != NO_ERROR) {
LOG(critical, FMTK, "Error attaching detours: {}", error);
}
LOG(trace, FMTK, "Attached to FUEL");
toml::table tbl;
try {
tbl = toml::parse_file(FMTK_TOML_PATH);
} catch (const toml::parse_error &err) {
LOG(error, FMTK, "TOML Parsing failed:\n{}", err.what());
return -2;
}
std::wstring modsDirectoryPath = L"mods";
fmtkModsDirectoryPath =
(std::filesystem::absolute(modsDirectoryPath) / "").string();
SetDllDirectory((modsDirectoryPath + L"\\path").c_str());
loadModsDirectory(modsDirectoryPath + L"\\native");
return TRUE;
}
BOOL ProcessDetach(HMODULE hDll) {
BROADCAST(Shutdown);
LOG(trace, FMTK, "Detaching from FUEL");
LONG error = DetachDetours();
if (error != NO_ERROR) {
LOG(critical, FMTK, "Error detaching detours: {}", error);
}
LOG(trace, FMTK, "Detatched from FUEL");
return TRUE;
}
BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD dwReason, PVOID lpReserved) {
if (DetourIsHelperProcess()) {
return TRUE;
}
switch (dwReason) {
case DLL_PROCESS_ATTACH:
return ProcessAttach(hModule);
case DLL_PROCESS_DETACH:
return ProcessDetach(hModule);
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
default:
break;
}
return TRUE;
}