forked from dredknight/H5_DLL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility_functions.cpp
150 lines (128 loc) · 5.76 KB
/
utility_functions.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
////////////////////////////////////////////////////////////////////
/////// UTILITY FUNCTIONS //////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
#include "pch.h"
#include "mainH5.h"
#include "libzippp.h"
/* True if file exists, otherwise false */
bool fexists(char* path) {
FILE* file;
if (fopen_s(&file, path, "r") == 0) {
fclose(file);
return true;
}
return false;
}
pugi::xml_document getXmlTree(const char* archive, const char* xml_source) {
pugi::xml_document doc;
if (!fexists(const_cast<char*>(archive))) {
//writeLog(ERRROR, archive + std::string(" does not exist!"));
return doc;
}
libzippp::ZipArchive data(archive);
data.open(libzippp::ZipArchive::ReadOnly);
libzippp::ZipEntry config = data.getEntry(xml_source);
if (config.isNull()) {
//writeLog(ERRROR, std::string("Failed to read") + archive + "/" + xml_source + "!");
return doc;
}
doc.load_string(config.readAsText().c_str());
data.close();
return doc;
}
void assignByteToAddress(long addrPtr, const int* value) {
DWORD oldProtect;
if (!VirtualProtect(reinterpret_cast<LPVOID>(addrPtr), 1, PAGE_EXECUTE_READWRITE, &oldProtect)) {
writeLog(ERRROR, "Cannot enable the current protection settings!");
return;
}
*(uint8_t*)addrPtr = static_cast<uint8_t>(*value);
}
void JumpToFunction(void* targetAddress, void* destinationFunction, size_t len, int patch_type)
{
DWORD oldProtect;
if (!VirtualProtect(reinterpret_cast<LPVOID>(targetAddress), len, PAGE_EXECUTE_READWRITE, &oldProtect)) {
writeLog(ERRROR, "Cannot enable the current protection settings!");
}
// Calculate the relative offset between the target and destination functions
intptr_t offset = reinterpret_cast<intptr_t>(destinationFunction) - reinterpret_cast<intptr_t>(targetAddress) - 5;
// Write the assembly bytes for the jmp instruction
unsigned char jmpBytes[] = { patch_type, 0x00, 0x00, 0x00, 0x00 };
std::memset((char*)targetAddress, 0x90, len);
std::memcpy(jmpBytes + 1, &offset, sizeof(offset));
// Copy the assembly bytes to the target function
std::memcpy(targetAddress, jmpBytes, sizeof(jmpBytes));
DWORD unused;
if (!VirtualProtect(reinterpret_cast<LPVOID>(targetAddress), sizeof(len), oldProtect, &unused)) {
writeLog(ERRROR, "Cannot revert the current protection settings!");
}
}
void ConditionalJumpToFunction(void* targetAddress, void* destinationFunction, size_t len, unsigned short patch_type)
{
DWORD oldProtect;
if (!VirtualProtect(reinterpret_cast<LPVOID>(targetAddress), len, PAGE_EXECUTE_READWRITE, &oldProtect)) {
writeLog(ERROR, "Cannot enable the current protection settings!");
}
// Calculate the relative offset between the target and destination functions
intptr_t offset = reinterpret_cast<intptr_t>(destinationFunction) - reinterpret_cast<intptr_t>(targetAddress) - 6;
// Write the assembly bytes for the conditional jump instruction
unsigned char jmpBytes[] = { 0x0F, static_cast<unsigned char>(patch_type), 0x00, 0x00, 0x00, 0x00 };
std::memset((char*)targetAddress, 0x90, len);
std::memcpy(jmpBytes + 2, &offset, sizeof(offset));
// Copy the assembly bytes to the target function
std::memcpy(targetAddress, jmpBytes, sizeof(jmpBytes));
DWORD unused;
if (!VirtualProtect(reinterpret_cast<LPVOID>(targetAddress), sizeof(len), oldProtect, &unused)) {
writeLog(ERROR, "Cannot revert the current protection settings!");
}
}
void AssignNopToAddressRange(int* hookAddress, size_t len) {
DWORD oldProtect;
if (!VirtualProtect(reinterpret_cast<LPVOID>(hookAddress), len, PAGE_EXECUTE_READWRITE, &oldProtect)) {
writeLog(ERRROR, "Cannot enable the current protection settings!");
}
for (size_t i = 0; i < len; i++) {
//sscanf_s(patch.value_bytes + 2 * i, "%2hhx", &byteArray[i]);
void* memoryAddress = reinterpret_cast<void*>(hookAddress);
unsigned char* targetAddress = static_cast<unsigned char*>(memoryAddress);
for (size_t i = 0; i < len; i++) {
targetAddress[i] = 0x90;
}
}
}
double getDoubleValueFromXdb(pugi::xml_document& doc, const char* abilityName) {
std::string xpath = std::string("/MMH55/") + abilityName;
auto node = doc.select_node(xpath.c_str()).node().text().as_double();
return node;
}
void assignFloatPtrToAddress(float** addrPtr, const float* value) {
DWORD oldProtect;
if (!VirtualProtect(reinterpret_cast<LPVOID>(addrPtr), 4, PAGE_EXECUTE_READWRITE, &oldProtect)) {
writeLog(ERRROR, "Cannot enable the current protection settings!");
return;
}
*addrPtr = const_cast<float*>(value);
}
void assignDoublePtrToAddress(double** addrPtr, const double* value) {
DWORD oldProtect;
if (!VirtualProtect(reinterpret_cast<LPVOID>(addrPtr), 8, PAGE_EXECUTE_READWRITE, &oldProtect)) {
writeLog(ERRROR, "Cannot enable the current protection settings!");
return;
}
*addrPtr = const_cast<double*>(value);
}
float getFloatValueFromXdb(pugi::xml_document& doc, const char* abilityName) {
std::string xpath = std::string("/MMH55/") + abilityName;
auto node = doc.select_node(xpath.c_str()).node().text().as_float();
return node;
}
int getIntValueFromXdb(pugi::xml_document& doc, const char* abilityName) {
std::string xpath = std::string("/MMH55/") + abilityName;
auto node = doc.select_node(xpath.c_str()).node().text().as_int();
return node;
}
std::string getStringValueFromXdb(pugi::xml_document& doc, const char* abilityName) {
std::string xpath = std::string("/MMH55/") + abilityName;
auto node = doc.select_node(xpath.c_str()).node().text().as_string();
return node;
}