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