-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathxSEPluginPreloader.h
More file actions
261 lines (224 loc) · 6.57 KB
/
Copy pathxSEPluginPreloader.h
File metadata and controls
261 lines (224 loc) · 6.57 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#pragma once
#include "Common.h"
#include "VectoredExceptionHandler.h"
#include "Utility.h"
#include <kxf/IO/IStream.h>
#include <kxf/System/NtStatus.h>
#include <kxf/System/DynamicLibrary.h>
#include <kxf/Threading/ReadWriteLock.h>
#include <kxf/FileSystem/NativeFileSystem.h>
#include <kxf/Serialization/XML.h>
#include <Windows.h>
#include <kxf/Win32/UndefMacros.h>
BOOL APIENTRY DllMain(HMODULE, DWORD, LPVOID);
namespace kxf
{
class ExecutableVersionResource;
}
namespace xSE
{
class Application;
enum class PluginStatus: uint32_t
{
Loaded,
Initialized,
FailedLoad,
FailedInitialize
};
enum class LoadMethod
{
OnProcessAttach,
OnThreadAttach,
ImportAddressHook
};
enum class InitializationMethod
{
None,
Standard,
xSEPluginPreload
};
}
namespace xSE::PluginPreloader
{
class OnProcessAttach final
{
};
class OnThreadAttach final
{
public:
size_t ThreadNumber = 0;
};
template<class TSignature>
class ImportAddressHook final
{
private:
TSignature* m_OriginalFunction = nullptr;
size_t m_CallCount = 0;
public:
kxf::String LibraryName;
kxf::String FunctionName;
public:
bool IsNull() const
{
return LibraryName.IsEmpty() || FunctionName.IsEmpty();
}
template<class... Args, class R = std::invoke_result_t<TSignature, Args...>>
R CallOriginal(kxf::NtStatus& status, Args&&... arg)
{
KXF_SCOPEDLOG_FUNC;
KXF_SCOPEDLOG.Info()
KXF_SCOPEDLOG_VALUE_AS(m_OriginalFunction, reinterpret_cast<void*>(m_OriginalFunction))
KXF_SCOPEDLOG_VALUE(m_CallCount)
KXF_SCOPEDLOG_VALUE(LibraryName)
KXF_SCOPEDLOG_VALUE(FunctionName);
if constexpr(std::is_void_v<R>)
{
status = Utility::SEHTryExcept([&]()
{
std::invoke(m_OriginalFunction, std::forward<Args>(arg)...);
});
m_CallCount++;
KXF_SCOPEDLOG.SetSuccess(status);
}
else
{
R result;
status = Utility::SEHTryExcept([&]()
{
result = std::invoke(m_OriginalFunction, std::forward<Args>(arg)...);
});
m_CallCount++;
KXF_SCOPEDLOG.LogReturn(result, status.IsSuccess());
return result;
}
}
TSignature* GetOriginal() const noexcept
{
return m_OriginalFunction;
}
void SaveOriginal(TSignature* func) noexcept
{
m_OriginalFunction = func;
}
bool IsHooked() const noexcept
{
return m_OriginalFunction != nullptr;
}
};
class ImportAddressHookHandler;
}
namespace xSE
{
class PreloadHandler final
{
friend BOOL APIENTRY ::DllMain(HMODULE, DWORD, LPVOID);
friend class PluginPreloader::ImportAddressHookHandler;
private:
static PreloadHandler& CreateInstance();
static void DestroyInstance();
public:
static kxf::String GetLibraryName();
static kxf::Version GetLibraryVersion();
static PreloadHandler* GetInstance() noexcept;
static void** GetFunctions() noexcept;
static size_t GetFunctionsCount() noexcept;
static size_t GetFunctionsEffectiveCount() noexcept;
private:
// General
std::shared_ptr<Application> m_Application;
kxf::NativeFileSystem m_InstallFS;
kxf::NativeFileSystem m_ConfigFS;
kxf::DynamicLibrary m_OriginalLibrary;
std::vector<kxf::DynamicLibrary> m_LoadedLibraries;
VectoredExceptionHandler m_VectoredExceptionHandler;
size_t m_VectoredExceptionCounter = 0;
kxf::FSPath m_ExecutablePath;
bool m_PluginsLoaded = false;
bool m_PluginsLoadAllowed = false;
std::atomic<size_t> m_ThreadAttachCount = 0;
bool m_WatchThreadAttach = false;
// Config
kxf::XMLDocument m_Config;
kxf::FSPath m_OriginalLibraryPath;
kxf::TimeSpan m_HookDelay;
kxf::TimeSpan m_LoadDelay;
bool m_InstallExceptionHandler = true;
bool m_KeepExceptionHandler = false;
std::vector<kxf::String> m_AllowedProcessNames;
std::optional<LoadMethod> m_LoadMethod;
PluginPreloader::OnProcessAttach m_OnProcessAttach;
PluginPreloader::OnThreadAttach m_OnThreadAttach;
#if xSE_PLATFORM_SKSE64 || xSE_PLATFORM_F4SE || xSE_PLATFORM_F4SEVR
PluginPreloader::ImportAddressHook<void*(__cdecl)(void*, void*)> m_ImportAddressHook;
#elif xSE_PLATFORM_SKSE || xSE_PLATFORM_NVSE
PluginPreloader::ImportAddressHook<char*(__stdcall)()> m_ImportAddressHook;
#endif
std::optional<InitializationMethod> m_InitializationMethod;
private:
kxf::FSPath GetOriginalLibraryPath() const;
kxf::FSPath GetOriginalLibraryDefaultPath() const;
void DoLoadPlugins();
void DoUnloadPlugins();
PluginStatus DoLoadSinglePlugin(const kxf::FSPath& path);
void OnPluginLoadFailed(const kxf::FSPath& path);
bool CheckAllowedProcesses() const;
void LoadOriginalLibrary();
void LoadOriginalLibraryFunctions();
void UnloadOriginalLibrary();
void ClearOriginalFunctions();
bool InstallVectoredExceptionHandler();
void RemoveVectoredExceptionHandler();
uint32_t OnVectoredContinue(const _EXCEPTION_POINTERS& exceptionInfo);
uint32_t OnVectoredException(const _EXCEPTION_POINTERS& exceptionInfo);
kxf::String DumpExceptionInformation(const _EXCEPTION_POINTERS& exceptionInfo) const;
bool InitializeFramework();
void LogEnvironmentInfo() const;
void LogCurrentModuleInfo() const;
kxf::ExecutableVersionResource LogHostProcessInfo() const;
kxf::ExecutableVersionResource LogScriptExtenderInfo(const kxf::ExecutableVersionResource& hostResourceInfo) const;
bool OnDLLMain(HMODULE handle, uint32_t event);
bool DisableThreadLibraryCalls(HMODULE handle);
bool HookImportTable();
bool LoadPlugins();
public:
PreloadHandler();
~PreloadHandler();
public:
bool IsNull() const
{
return m_OriginalLibrary.IsNull() || !m_LoadMethod.has_value() || !m_InitializationMethod.has_value();
}
LoadMethod GetLoadMethod() const
{
return *m_LoadMethod;
}
bool IsPluginsLoaded() const noexcept
{
return m_PluginsLoaded;
}
bool IsPluginsLoadAllowed() const noexcept
{
return m_PluginsLoadAllowed;
}
template<LoadMethod method>
const auto& GetLoadMethodOptions() const noexcept
{
if constexpr(method == LoadMethod::OnProcessAttach)
{
return m_OnProcessAttach;
}
else if constexpr(method == LoadMethod::OnThreadAttach)
{
return m_OnThreadAttach;
}
else if constexpr(method == LoadMethod::ImportAddressHook)
{
return m_ImportAddressHook;
}
else
{
static_assert(sizeof(LoadMethod*) == nullptr);
}
}
};
}