Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <stdint.h>
  3. #include <Psapi.h>
  4. #include "MinHook.h"
  5.  
  6. // Base address
  7. static uintptr_t BaseAddress;
  8. HANDLE process;
  9. uintptr_t SlideAddress(uintptr_t offset) {
  10. return BaseAddress + offset;
  11. }
  12.  
  13. // Structs
  14. typedef void Block;
  15. struct Item
  16. {
  17. uintptr_t** vtable;
  18. short id;
  19. };
  20.  
  21. struct ItemInstance
  22. {
  23. short count, data;
  24. uintptr_t* tag;
  25. Item* item;
  26. Block* block;
  27. };
  28.  
  29. struct FullBlock
  30. {
  31. uint8_t id, data;
  32. };
  33.  
  34. struct BlockPos
  35. {
  36. int x, y, z;
  37. };
  38.  
  39. struct Player
  40. {
  41. char filler[408];
  42. uintptr_t* region;
  43. };
  44.  
  45. // Vtables
  46. uintptr_t** VTSurvivalMode;
  47. uintptr_t** VTCreativeMode;
  48. uintptr_t** VTLocalPlayer;
  49.  
  50. // Function pointers
  51. static void(*setBlock)(uintptr_t*, const BlockPos&, FullBlock*, int);
  52. static FullBlock*(*getBlock)(uintptr_t*, FullBlock&, const BlockPos&);
  53.  
  54. // Hook functions
  55. static void(*growTreeOrig)(uintptr_t*, uintptr_t*, const BlockPos&, uintptr_t*);
  56. static void(*growTreeTramp)(uintptr_t*, uintptr_t*, const BlockPos&, uintptr_t*);
  57.  
  58. void growTreeHook(uintptr_t* self, uintptr_t* BlockSource, const BlockPos& pos, uintptr_t* rand)
  59. {
  60. FullBlock sapling;
  61. getBlock(BlockSource, sapling, pos);
  62.  
  63. FullBlock wood{ 5, sapling.data };
  64.  
  65. for (int i = 0; i < 5; i++) {
  66. setBlock(BlockSource, { pos.x, pos.y + i, pos.z }, &wood, 3);
  67. setBlock(BlockSource, { pos.x + 4, pos.y + i, pos.z }, &wood, 3);
  68. setBlock(BlockSource, { pos.x + 8, pos.y + i, pos.z }, &wood, 3);
  69. setBlock(BlockSource, { pos.x + 12, pos.y + i, pos.z }, &wood, 3);
  70. }
  71.  
  72. for (int i = 0; i < 3; i++) {
  73. setBlock(BlockSource, { pos.x + 6, pos.y + i, pos.z }, &wood, 3);
  74. setBlock(BlockSource, { pos.x + 10, pos.y + 2 + i, pos.z }, &wood, 3);
  75. setBlock(BlockSource, { pos.x + 14, pos.y + i, pos.z }, &wood, 3);
  76. setBlock(BlockSource, { pos.x + 16, pos.y + i, pos.z }, &wood, 3);
  77.  
  78. setBlock(BlockSource, { pos.x + 7, pos.y + 2 * i, pos.z }, &wood, 3);
  79. setBlock(BlockSource, { pos.x + 11, pos.y + 2 * i, pos.z }, &wood, 3);
  80. setBlock(BlockSource, { pos.x + 14, pos.y + 2 * i, pos.z }, &wood, 3);
  81.  
  82. setBlock(BlockSource, { pos.x + 16 + i, pos.y, pos.z }, &wood, 3);
  83. setBlock(BlockSource, { pos.x + 16 + i, pos.y + 3, pos.z }, &wood, 3);
  84. }
  85.  
  86. setBlock(BlockSource, { pos.x + 1, pos.y + 3, pos.z }, &wood, 3);
  87. setBlock(BlockSource, { pos.x + 2, pos.y + 2, pos.z }, &wood, 3);
  88. setBlock(BlockSource, { pos.x + 3, pos.y + 3, pos.z }, &wood, 3);
  89. setBlock(BlockSource, { pos.x + 10, pos.y, pos.z }, &wood, 3);
  90. }
  91.  
  92. BOOL setHook(uintptr_t origAddress, uintptr_t* hookFunction, uintptr_t* trampFunction)
  93. {
  94. if (MH_CreateHook((LPVOID*)SlideAddress(origAddress), (LPVOID*)hookFunction, reinterpret_cast<LPVOID*>(trampFunction)) != MH_OK)
  95. {
  96. return FALSE;
  97. }
  98.  
  99. if (MH_EnableHook((LPVOID*)SlideAddress(origAddress)) != MH_OK)
  100. {
  101. return FALSE;
  102. }
  103.  
  104. return TRUE;
  105. }
  106. DWORD_PTR GetProcessBaseAddress(DWORD processID);
  107.  
  108. BOOL init()
  109. {
  110. DWORD procId = GetCurrentProcessId();
  111. process = OpenProcess(PROCESS_ALL_ACCESS | PROCESS_VM_OPERATION | PROCESS_QUERY_INFORMATION, FALSE, procId);
  112. BaseAddress = (uintptr_t)GetProcessBaseAddress(procId);
  113.  
  114. // DO NOT MOVE THIS TO DLLMain; it has to be called exactly once and DLLMain is called multiple times
  115. if (MH_Initialize() != MH_OK)
  116. {
  117. return FALSE;
  118. }
  119.  
  120. BOOL hook = TRUE;
  121. hook &= setHook(0x38AC50, (uintptr_t*)&growTreeHook, (uintptr_t*)&growTreeTramp);
  122.  
  123. // function calls
  124. setBlock = (void(*)(uintptr_t*, const BlockPos&, FullBlock*, int)) SlideAddress(0x39A970);
  125. getBlock = (FullBlock*(*)(uintptr_t*, FullBlock&, const BlockPos&)) SlideAddress(0x399860);
  126.  
  127. return hook;
  128. }
  129.  
  130. BOOL deinit()
  131. {
  132. if (MH_Uninitialize() != MH_OK)
  133. {
  134. return FALSE;
  135. }
  136. CloseHandle(process);
  137. return TRUE;
  138. }
  139.  
  140. // begin
  141. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  142. {
  143.  
  144. switch (ul_reason_for_call)
  145. {
  146. case DLL_PROCESS_ATTACH:
  147. if (!init())
  148. {
  149. CloseHandle(process);
  150. return FALSE;
  151. }
  152. case DLL_THREAD_ATTACH: break;
  153. case DLL_THREAD_DETACH: break;
  154. case DLL_PROCESS_DETACH:
  155. if (!deinit()) {
  156. CloseHandle(process);
  157. return FALSE;
  158. }
  159. break;
  160. }
  161. return TRUE;
  162. }
  163.  
  164. DWORD_PTR GetProcessBaseAddress(DWORD processID)
  165. {
  166. DWORD_PTR baseAddress = 0;
  167. HANDLE processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processID);
  168. HMODULE *moduleArray;
  169. LPBYTE moduleArrayBytes;
  170. DWORD bytesRequired;
  171.  
  172. if (processHandle)
  173. {
  174. if (EnumProcessModules(processHandle, NULL, 0, &bytesRequired))
  175. {
  176. if (bytesRequired)
  177. {
  178. moduleArrayBytes = (LPBYTE)LocalAlloc(LPTR, bytesRequired);
  179.  
  180. if (moduleArrayBytes)
  181. {
  182. unsigned int moduleCount;
  183.  
  184. moduleCount = bytesRequired / sizeof(HMODULE);
  185. moduleArray = (HMODULE *)moduleArrayBytes;
  186.  
  187. if (EnumProcessModules(processHandle, moduleArray, bytesRequired, &bytesRequired))
  188. {
  189. baseAddress = (DWORD_PTR)moduleArray[0];
  190. }
  191.  
  192. LocalFree(moduleArrayBytes);
  193. }
  194. }
  195. }
  196.  
  197. CloseHandle(processHandle);
  198. }
  199.  
  200. return baseAddress;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement