1 | #include "Daodan_Patch.h"
|
---|
2 | #include <windows.h>
|
---|
3 | #include <string.h>
|
---|
4 |
|
---|
5 | bool DDrPatch_MakeJump(void* from, void* to)
|
---|
6 | {
|
---|
7 | DWORD oldp;
|
---|
8 |
|
---|
9 | if (VirtualProtect(from, 5, PAGE_EXECUTE_READWRITE, &oldp))
|
---|
10 | {
|
---|
11 | *(char*)from++ = 0xe9; // jmp rel32
|
---|
12 | *(int*)from = (unsigned int)to - (unsigned int)from - 4;
|
---|
13 | VirtualProtect(from, 5, oldp, &oldp);
|
---|
14 | return true;
|
---|
15 | }
|
---|
16 | else
|
---|
17 | return false;
|
---|
18 | }
|
---|
19 |
|
---|
20 | bool DDrPatch_MakeCall(void* from, void* to)
|
---|
21 | {
|
---|
22 | DWORD oldp;
|
---|
23 |
|
---|
24 | if (VirtualProtect(from, 5, PAGE_EXECUTE_READWRITE, &oldp))
|
---|
25 | {
|
---|
26 | *(char*)from++ = 0xe8; // call rel32
|
---|
27 | *(int*)from = (unsigned int)to - (unsigned int)from - 4;
|
---|
28 | VirtualProtect(from, 5, oldp, &oldp);
|
---|
29 | return true;
|
---|
30 | }
|
---|
31 | else
|
---|
32 | return false;
|
---|
33 | }
|
---|
34 |
|
---|
35 | bool DDrPatch_String(char* dest, const char* string, int length)
|
---|
36 | {
|
---|
37 | DWORD oldp;
|
---|
38 |
|
---|
39 | if (VirtualProtect(dest, length, PAGE_EXECUTE_READWRITE, &oldp))
|
---|
40 | {
|
---|
41 | memcpy(dest, string, length);
|
---|
42 | VirtualProtect(dest, length, oldp, &oldp);
|
---|
43 | return true;
|
---|
44 | }
|
---|
45 | else
|
---|
46 | return false;
|
---|
47 | }
|
---|
48 |
|
---|
49 | bool DDrPatch_Byte(char* dest, char value)
|
---|
50 | {
|
---|
51 | DWORD oldp;
|
---|
52 |
|
---|
53 | if (VirtualProtect(dest, 1, PAGE_EXECUTE_READWRITE, &oldp))
|
---|
54 | {
|
---|
55 | *dest = value;
|
---|
56 | VirtualProtect(dest, 1, oldp, &oldp);
|
---|
57 | return true;
|
---|
58 | }
|
---|
59 | else
|
---|
60 | return false;
|
---|
61 | }
|
---|
62 |
|
---|
63 | bool DDrPatch_Int32(int* dest, int value)
|
---|
64 | {
|
---|
65 | DWORD oldp;
|
---|
66 |
|
---|
67 | if (VirtualProtect(dest, 4, PAGE_EXECUTE_READWRITE, &oldp))
|
---|
68 | {
|
---|
69 | *dest = value;
|
---|
70 | VirtualProtect(dest, 4, oldp, &oldp);
|
---|
71 | return true;
|
---|
72 | }
|
---|
73 | else
|
---|
74 | return false;
|
---|
75 | }
|
---|
76 |
|
---|
77 | bool DDrPatch_Int16(short* dest, short value)
|
---|
78 | {
|
---|
79 | DWORD oldp;
|
---|
80 |
|
---|
81 | if (VirtualProtect(dest, 2, PAGE_EXECUTE_READWRITE, &oldp))
|
---|
82 | {
|
---|
83 | *dest = value;
|
---|
84 | VirtualProtect(dest, 2, oldp, &oldp);
|
---|
85 | return true;
|
---|
86 | }
|
---|
87 | else
|
---|
88 | return false;
|
---|
89 | }
|
---|
90 |
|
---|
91 | bool DDrPatch_StrDup(int* dest, const char* value)
|
---|
92 | {
|
---|
93 | DWORD oldp;
|
---|
94 |
|
---|
95 | if (VirtualProtect(dest, 4, PAGE_EXECUTE_READWRITE, &oldp))
|
---|
96 | {
|
---|
97 | *dest = (int)strdup(value);
|
---|
98 | VirtualProtect(dest, 4, oldp, &oldp);
|
---|
99 | return true;
|
---|
100 | }
|
---|
101 | else
|
---|
102 | return false;
|
---|
103 | }
|
---|