forked from Moonpaw/Plugy-EX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModifMemory.cpp
More file actions
233 lines (206 loc) · 6.43 KB
/
Copy pathModifMemory.cpp
File metadata and controls
233 lines (206 loc) · 6.43 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
/*=================================================================
File created by Yohann NICOLAS.
Modification of code in memory functions.
=================================================================*/
#include "error.h"
#include "parameters.h"
#include "modifMemory.h"
const char* MSG_ERROR_READ_MEMORY = "Error : Read access missing to patch memory at %08X.\nPlease install a clean version of Lord of Destruction\n\n"
"You can avoid this error message by setting ActiveCkeckMemory=0 in PlugY.ini\n(WARNING: Be careful when you use it, it shouldn't be used by common user)";
const char* MSG_CRITICAL_WRITE_MEMORY = "Error.\nWrite access missing to patch memory at %08X.\n\nPlease install a clean version of Lord of Destruction\n";
const char* ERROR_TESTING1 = "Error : BYTE %02X wanted but %02X found to change memory at %08X\n\n";
const char* ERROR_TESTING4 = "Error : DWORD %08X wanted but %08X found to change memory at %08X\n\n";
void* currentMemoryPos = 0;
DWORD mem_seek(DWORD newPos)
{
currentMemoryPos = (void*)newPos;
return (DWORD)currentMemoryPos;
}
void patchMemory1(BYTE value)
{
if (IsBadWritePtr(currentMemoryPos,1))
{
log_box(MSG_CRITICAL_WRITE_MEMORY, currentMemoryPos);
exit(1);
}
*(BYTE*)currentMemoryPos = value;
currentMemoryPos = (LPVOID)((DWORD)currentMemoryPos + 1);
}
void patchMemory4(DWORD value)
{
if (IsBadWritePtr(currentMemoryPos,4))
{
log_box(MSG_CRITICAL_WRITE_MEMORY, currentMemoryPos);
exit(1);
}
*(DWORD*)currentMemoryPos = value;
currentMemoryPos = (LPVOID)((DWORD)currentMemoryPos + 4);
}
BYTE getMemory1(LPVOID mempos)
{
if (IsBadReadPtr(mempos,1))
{
log_box(MSG_ERROR_READ_MEMORY, currentMemoryPos);
exit(1);
}
return *(BYTE*)mempos;
}
DWORD getMemory4(LPVOID mempos)
{
if (IsBadReadPtr(mempos,4))
{
log_box(MSG_ERROR_READ_MEMORY, currentMemoryPos);
exit(1);
}
return *(DWORD*)mempos;
}
void memt_byte(BYTE old, BYTE val)
{
BYTE current = getMemory1(currentMemoryPos);
if ( current == val)
{
log_msg("Warning : BYTE %02X is already set at memory %08X\n", val, currentMemoryPos);
currentMemoryPos = (LPVOID)((DWORD)currentMemoryPos + 1);
return;
}
if ( old != current)
{
if (active_CheckMemory)
{
log_box(ERROR_TESTING1, old, current, currentMemoryPos);
exit(1);
} else log_msg(ERROR_TESTING1, old, current, currentMemoryPos);
}
log_msg("BYTE\t%08X : %02X->%02X\n", currentMemoryPos, old, val);
patchMemory1(val);
}
bool testIfAlreadySet(DWORD current, DWORD wanted)
{
if (current == wanted)
{
log_msg("Warning : DWORD %08X is already set at memory %08X\n", wanted, currentMemoryPos);
currentMemoryPos = (LPVOID)((DWORD)currentMemoryPos + 4);
return true;
}
return false;
}
void testMemory4(DWORD old, DWORD found)
{
if (old != found)
if (active_CheckMemory)
{
log_box(ERROR_TESTING4, old, found, currentMemoryPos);
exit(1);
} else log_msg(ERROR_TESTING4, old, found, currentMemoryPos);
}
void memt_dword(DWORD old, DWORD val)
{
DWORD current = getMemory4(currentMemoryPos);
DWORD wanted = val;
if (testIfAlreadySet(current, wanted)) return;
testMemory4(old, current);
log_msg("DWORD\t%08X : %08X->%08X\n", currentMemoryPos, old, wanted);
patchMemory4(wanted);
}
void memt_ref4(DWORD old, DWORD ref)
{
DWORD current = getMemory4(currentMemoryPos);
DWORD wanted = ref-(DWORD)currentMemoryPos-4;
if (testIfAlreadySet(current, wanted)) return;
testMemory4(old, current);
log_msg("DWORD\t%08X : %08X->%08X\n", currentMemoryPos, old, wanted);
patchMemory4(wanted);
}
void memc_ref4(DWORD old, DWORD ref)
{
DWORD current = getMemory4(currentMemoryPos);
DWORD wanted = ref-(DWORD)currentMemoryPos-4;
if (testIfAlreadySet(current, wanted)) return;
testMemory4(old, current + (DWORD)currentMemoryPos + 4);
log_msg("DWORD\t%08X : %08X->%08X\n", currentMemoryPos, old, wanted);
patchMemory4(wanted);
}
void memj_ref4(DWORD old, DWORD ref)
{
DWORD current = getMemory4(currentMemoryPos);
DWORD wanted = ref-(DWORD)currentMemoryPos-4;
if (testIfAlreadySet(current, wanted)) return;
testMemory4(old, getMemory4((LPVOID)getMemory4((LPVOID)(current + (DWORD)currentMemoryPos + 6))));
log_msg("DWORD\t%08X : %08X->%08X\n", currentMemoryPos, old, wanted);
patchMemory4(wanted);
}
void memd_ref4(DWORD old, DWORD ref)
{
DWORD current = getMemory4(currentMemoryPos);
DWORD wanted = ref-(DWORD)currentMemoryPos-4;
if (testIfAlreadySet(current, wanted)) return;
testMemory4(old, getMemory4((LPVOID)current));
log_msg("DWORD\t%08X : %08X->%08X\n", currentMemoryPos, old, wanted);
patchMemory4(wanted);
}
/*
void mem_byte(BYTE val)
{
if ( *(BYTE*)currentMemoryPos == val)
{
log_msg("Warning : BYTE at %08X is already set to %02X\n", currentMemoryPos, val);
currentMemoryPos += 1;
return;
}
log_msg("BYTE\t%08X : %02X\n", currentMemoryPos, val);
*(BYTE*)currentMemoryPos = val;
currentMemoryPos += 1;
}
void mem_word(WORD val)
{
if ( *(WORD*)currentMemoryPos == val)
{
log_msg("Warning : WORD at %08X is already set to %04X\n", currentMemoryPos, val);
currentMemoryPos += 2;
return;
}
log_msg("WORD\t%08X : %04X\n", currentMemoryPos, val);
*(WORD*)currentMemoryPos = val;
currentMemoryPos += 2;
}
void memt_word(WORD old, WORD val)
{
if ( *(WORD*)currentMemoryPos == val)
{
log_msg("Warning : WORD at %08X is already set to %04X (normal:%04X)\n", currentMemoryPos, val, old);
currentMemoryPos += 2;
return;
}
if ( *(WORD*)currentMemoryPos != old)
{
log_msg("WORD %04X found, want %04X at %08X\n", *(WORD*)currentMemoryPos, old, currentMemoryPos);
if (active_CheckMemory)
{
log_box(MSG_ERROR_MEMORY);
exit(1);
}
}
log_msg("WORD\t%08X : %04X->%04X\n", currentMemoryPos, old, val);
*(WORD*)currentMemoryPos = val;
currentMemoryPos += 2;
}
void mem_dword(DWORD val)
{
if ( *(DWORD*)currentMemoryPos == val)
{
log_msg("Warning : DWORD at %08X is already set to %08X\n", currentMemoryPos, val);
currentMemoryPos += 4;
return;
}
log_msg("DWORD\t%08X : %08X\n", currentMemoryPos, val);
*(DWORD*)currentMemoryPos = val;
currentMemoryPos += 4;
}
void mem_ref4(DWORD ref)
{
log_msg("DWORD\t%08X : %08X\n", currentMemoryPos, ref-(DWORD)currentMemoryPos-4);
*(DWORD*)currentMemoryPos = ref-(DWORD)currentMemoryPos-4;
currentMemoryPos += 4;
}
*/
/*================================= END OF FILE =================================*/