-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPatch.cpp
62 lines (55 loc) · 1.63 KB
/
Patch.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
#include "Patch.h"
Patch::Patch() {
this->Target = 0;
this->Length = 0;
this->Active = false;
this->oldBytes = nullptr;
this->newBytes = nullptr;
this->oldprotect = 0;
}
Patch::~Patch() {
this->Remove();
}
void Patch::CreatePatch(uintptr_t Target, BYTE* newBytes, bool Active) {
this->Target = Target;
this->newBytes = newBytes;
this->Length = sizeof(newBytes);
this->oldBytes = new byte[this->Length];
if (Active)
this->Activate();
}
bool Patch::Activate() {
if (!this->Target || !this->oldBytes || !this->newBytes || !this->Length)
return false;
if (!this->Active) {
VirtualProtect((void*)this->Target, this->Length, PAGE_EXECUTE_READWRITE, &oldprotect);
memcpy(this->oldBytes, (void*)this->Target, this->Length);
memcpy((void*)this->Target, this->newBytes, this->Length);
VirtualProtect((void*)this->Target, this->Length, oldprotect, &oldprotect);
this->Active = true;
}
return this->Active;
}
bool Patch::Deactivate() {
if (!this->Target || !this->oldBytes || !this->newBytes || !this->Length)
return false;
if (this->Active) {
VirtualProtect((void*)this->Target, this->Length, PAGE_EXECUTE_READWRITE, &oldprotect);
memcpy((void*)this->Target, this->oldBytes, this->Length);
VirtualProtect((void*)this->Target, this->Length, oldprotect, &oldprotect);
this->Active = false;
}
return !this->Active;
}
bool Patch::Remove() {
if (this->Deactivate()) {
this->Target = 0;
this->Length = 0;
this->Active = false;
delete[] this->oldBytes;
this->oldBytes = nullptr;
this->newBytes = nullptr;
this->oldprotect = 0;
}
return !this->Active;
}