-
Notifications
You must be signed in to change notification settings - Fork 2
/
RVA.h
193 lines (157 loc) · 6.2 KB
/
RVA.h
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
#pragma once
#include <unordered_map>
#include <memory>
#include <string>
#include <sstream>
#include <exception>
#include <functional>
#include "f4se_common/f4se_version.h"
#include "Pattern.h"
#include "Relocation.h"
struct sig_plugin_info
{
uint32_t runtime_version = CURRENT_RELEASE_RUNTIME;
const char * plugin_name = nullptr;
};
///fill this struct in F4SEPlugin_Query
extern sig_plugin_info plugin_info;
///To use RELOC_MEMBER_FN macro, first of all, you need to replace DEFINE_MEMBER_FN_LONG macro in f4se_common/Utilities.h with the following codes.
/*
#define DEFINE_MEMBER_FN_LONG(className, functionName, retnType, address, ...) \
typedef retnType (className::* _##functionName##_type)(__VA_ARGS__); \
\
inline _##functionName##_type * _##functionName##_GetPtr(void) \
{ \
static uintptr_t _address = address + RelocationManager::s_baseAddr; \
return (_##functionName##_type *)&_address; \
}
*/
#define RELOC_MEMBER_FN(className, fnName, ...) \
decltype(&className::_##fnName##_GetPtr) _className##_##fnName = &className::_##fnName##_GetPtr; \
*((*((uintptr_t *(**)())(&_className##_##fnName)))()) = sig_scan_processor(#className"::"#fnName, __VA_ARGS__).GetUIntPtr();
#define RELOC_MEMBER_FN_EX(className, fnName, ...) \
className##::_##fnName##_GetFnPtr() = sig_scan_processor(#className"::"#fnName, __VA_ARGS__).GetUIntPtr();
#define RELOC_GLOBAL_VAL(globalObj, ...) \
globalObj = sig_scan_processor(#globalObj, __VA_ARGS__).GetOffset();
#define RELOC_RUNTIME_ADDR(...) \
sig_scan_processor(nullptr, __VA_ARGS__).GetUIntPtr()
class no_result_exception : public std::exception
{
public:
virtual char const * what() const override;
};
struct sig_scan_util
{
static bool read_memory(uintptr_t addr, void* data, size_t len)
{
UInt32 oldProtect;
if (VirtualProtect((void *)addr, len, PAGE_EXECUTE_READWRITE, &oldProtect))
{
memcpy(data, (void*)addr, len);
if (VirtualProtect((void *)addr, len, oldProtect, &oldProtect))
return true;
}
return false;
}
};
class sig_scan_timer
{
public:
sig_scan_timer()
{
QueryPerformanceCounter(&countStart);
QueryPerformanceFrequency(&frequency);
}
~sig_scan_timer()
{
QueryPerformanceCounter(&countEnd);
_MESSAGE(">> sigscan time elapsed: %llu ms...", (countEnd.QuadPart - countStart.QuadPart) / (frequency.QuadPart / 1000));
}
private:
LARGE_INTEGER countStart, countEnd, frequency;
};
class sig_scan_processor
{
public:
using version = uint32_t;
using relative_address = uint64_t;
using runtime_address = uint64_t;
using version_map = std::unordered_map<version, relative_address>;
sig_scan_processor() = delete;
sig_scan_processor(const sig_scan_processor& rhs) = delete;
sig_scan_processor(sig_scan_processor&& rhs) = delete;
sig_scan_processor(const char * iniName, version_map versionData, const char * sig = nullptr, int relOffset = 0, int dataOffset = 0, int instructionLen = 0) : m_result(0)
{
std::unique_ptr<char[]> sResult(new char[MAX_PATH]);
static std::string fileName = ".\\Data\\F4SE\\Plugins\\" + std::string(plugin_info.plugin_name) + ".mem";
static std::string sectionName = (sprintf_s(sResult.get(), MAX_PATH, "%08X", plugin_info.runtime_version), sResult.get());
std::string settingName;
if (iniName)
{
settingName = iniName;
}
else if (sig)
{
std::hash<std::string> hash_fn;
size_t hash = hash_fn(sig + std::string("|") + std::to_string(relOffset << 0x10 | (dataOffset & 0xFF) << 0x8 | instructionLen & 0xFF));
settingName = std::string("SIG_") + (sprintf_s(sResult.get(), MAX_PATH, "%016I64X", hash), sResult.get()); //only sig...
}
if (versionData.count(plugin_info.runtime_version))
{
m_result = versionData[plugin_info.runtime_version] + RelocationManager::s_baseAddr;
}
else /*if (iniName && plugin_info.plugin_name)*/
{
uint32_t relMem = 0;
if (plugin_info.plugin_name && (relMem = GetPrivateProfileIntA(sectionName.c_str(), settingName.c_str(), 0, fileName.c_str()), relMem))
{
m_result = relMem + RelocationManager::s_baseAddr;
}
else if (sig != nullptr)
{
uintptr_t scan_result = (uintptr_t)Utility::pattern(sig).count(1).get(0).get<void>(0);
if (!scan_result)
{
throw no_result_exception();
}
scan_result += relOffset;
if (dataOffset)
{
int32_t rel32 = 0;
sig_scan_util::read_memory(scan_result + dataOffset, &rel32, sizeof(int32_t));
scan_result += instructionLen + rel32;
}
m_result = scan_result;
}
}
if (!m_result)
{
throw no_result_exception();
}
if (plugin_info.plugin_name && settingName.size())
{
sprintf_s(sResult.get(), MAX_PATH, "0x%08X", GetOffset());
WritePrivateProfileStringA(sectionName.c_str(), settingName.c_str(), sResult.get(), fileName.c_str());
}
}
sig_scan_processor(const char * iniName, const char * sig = nullptr, int relOffset = 0, int dataOffset = 0, int instructionLen = 0) \
: sig_scan_processor(iniName, { {} }, sig, relOffset, dataOffset, instructionLen) { }
sig_scan_processor(version_map versionData, const char * sig = nullptr, int relOffset = 0, int dataOffset = 0, int instructionLen = 0) \
: sig_scan_processor(nullptr, versionData, sig, relOffset, dataOffset, instructionLen) { }
sig_scan_processor(const char * sig = nullptr, int relOffset = 0, int dataOffset = 0, int instructionLen = 0) \
: sig_scan_processor(nullptr, { {} }, sig, relOffset, dataOffset, instructionLen) { }
sig_scan_processor(const char * iniName, relative_address relMem, const char * sig = nullptr, int relOffset = 0, int dataOffset = 0, int instructionLen = 0) \
: sig_scan_processor(iniName, { { CURRENT_RELEASE_RUNTIME, relMem } }, sig, relOffset, dataOffset, instructionLen) { }
sig_scan_processor(relative_address relMem, const char * sig = nullptr, int relOffset = 0, int dataOffset = 0, int instructionLen = 0) :\
sig_scan_processor(nullptr, { { CURRENT_RELEASE_RUNTIME, relMem } }, sig, relOffset, dataOffset, instructionLen) { }
uintptr_t GetUIntPtr() const
{
return m_result;
}
uint32_t GetOffset() const
{
return (m_result) ? static_cast<uint32_t>(m_result - RelocationManager::s_baseAddr) : 0;
}
private:
uintptr_t m_result;
};