d3d9-offsets.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <d3d9.h>
  4. #include "get-graphics-offsets.h"
  5. typedef HRESULT (WINAPI *d3d9createex_t)(UINT, IDirect3D9Ex**);
  6. struct d3d9_info {
  7. HMODULE module;
  8. HWND hwnd;
  9. IDirect3D9Ex *d3d9ex;
  10. IDirect3DDevice9Ex *device;
  11. IDirect3DSwapChain9 *swap;
  12. };
  13. static inline bool d3d9_init(d3d9_info &info)
  14. {
  15. d3d9createex_t create;
  16. HRESULT hr;
  17. info.hwnd = CreateWindowExA(0, DUMMY_WNDCLASS, "d3d9 get-offset window",
  18. WS_POPUP, 0, 0, 1, 1, nullptr, nullptr,
  19. GetModuleHandleA(nullptr), nullptr);
  20. if (!info.hwnd) {
  21. return false;
  22. }
  23. info.module = LoadLibraryA("d3d9.dll");
  24. if (!info.module) {
  25. return false;
  26. }
  27. create = (d3d9createex_t)GetProcAddress(info.module,
  28. "Direct3DCreate9Ex");
  29. if (!create) {
  30. return false;
  31. }
  32. hr = create(D3D_SDK_VERSION, &info.d3d9ex);
  33. if (FAILED(hr)) {
  34. return false;
  35. }
  36. D3DPRESENT_PARAMETERS pp = {};
  37. pp.Windowed = true;
  38. pp.SwapEffect = D3DSWAPEFFECT_FLIP;
  39. pp.BackBufferFormat = D3DFMT_A8R8G8B8;
  40. pp.BackBufferWidth = 2;
  41. pp.BackBufferHeight = 2;
  42. pp.BackBufferCount = 1;
  43. pp.hDeviceWindow = info.hwnd;
  44. pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  45. hr = info.d3d9ex->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
  46. info.hwnd,
  47. D3DCREATE_HARDWARE_VERTEXPROCESSING |
  48. D3DCREATE_NOWINDOWCHANGES, &pp, nullptr, &info.device);
  49. if (FAILED(hr)) {
  50. return false;
  51. }
  52. hr = info.device->GetSwapChain(0, &info.swap);
  53. if (FAILED(hr)) {
  54. return false;
  55. }
  56. return true;
  57. }
  58. static inline void d3d9_free(d3d9_info &info)
  59. {
  60. if (info.swap)
  61. info.swap->Release();
  62. if (info.device)
  63. info.device->Release();
  64. if (info.d3d9ex)
  65. info.d3d9ex->Release();
  66. if (info.hwnd)
  67. DestroyWindow(info.hwnd);
  68. }
  69. #ifdef _WIN64
  70. #define MAX_CMP_SIZE 22
  71. static const uint8_t mask[][MAX_CMP_SIZE] = {
  72. {
  73. 0xF8, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00,
  74. 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00,
  75. 0xFF, 0x00,
  76. 0xF8, 0xF8, 0x00, 0x00, 0x00, 0x00
  77. },
  78. {
  79. 0xF8, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00,
  80. 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00,
  81. 0xFF, 0x00,
  82. 0xF8, 0xF8, 0x00, 0x00, 0x00, 0x00
  83. }
  84. };
  85. static const uint8_t mask_cmp[][MAX_CMP_SIZE] = {
  86. /*
  87. * Windows 7
  88. * 48 8B 83 B8 3D 00 00 mov rax, [rbx+3DB8h]
  89. * 44 39 B8 68 50 00 00 cmp [rax+5068h], r15d
  90. * 75 12 jnz short loc_7FF7AA90530
  91. * 41 B8 F9 19 00 00 mov r8d, 19F9h
  92. */
  93. {
  94. 0x48, 0x8B, 0x80, 0x00, 0x00, 0x00, 0x00,
  95. 0x44, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00,
  96. 0x75, 0x00,
  97. 0x40, 0xB8, 0x00, 0x00, 0x00, 0x00
  98. },
  99. /*
  100. * Windows ???+
  101. * 49 8B 87 78 41 00 00 mov rax, [r15+4178h]
  102. * 39 98 E0 51 00 00 cmp [rax+51E0h], ebx
  103. * 75 12 jnz short loc_1800AEC9C
  104. * 41 B9 C3 1A 00 00 mov r9d, 1AC3h
  105. */
  106. {
  107. 0x48, 0x8B, 0x80, 0x00, 0x00, 0x00, 0x00,
  108. 0x39, 0x80, 0x00, 0x00, 0x00, 0x00,
  109. 0x75, 0x00,
  110. 0x40, 0xB8, 0x00, 0x00, 0x00, 0x00
  111. }
  112. };
  113. // Offset into the code for the numbers we're interested in
  114. static const uint32_t code_offsets[][2] = {
  115. {3, 10},
  116. {3, 9},
  117. };
  118. #else
  119. #define MAX_CMP_SIZE 20
  120. static const uint8_t mask[][MAX_CMP_SIZE] = {
  121. {
  122. 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00,
  123. 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00,
  124. 0xFF, 0x00,
  125. 0xFF, 0x00, 0x00, 0x00, 0x00
  126. },
  127. {
  128. 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00,
  129. 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0xFF,
  130. 0xFF, 0x00,
  131. 0xFF, 0x00, 0x00, 0x00, 0x00
  132. }
  133. };
  134. static const uint8_t mask_cmp[][MAX_CMP_SIZE] = {
  135. /*
  136. * Windows 7+
  137. * 8B 83 E8 29 00 00 mov eax, [ebx+29E8h]
  138. * 39 B0 80 4B 00 00 cmp [eax+4B80h], esi
  139. * 75 14 jnz short loc_754CD9E1
  140. * 68 F9 19 00 00 push 19F9h
  141. */
  142. {
  143. 0x8B, 0x80, 0x00, 0x00, 0x00, 0x00,
  144. 0x39, 0x80, 0x00, 0x00, 0x00, 0x00,
  145. 0x75, 0x00,
  146. 0x68, 0x00, 0x00, 0x00, 0x00
  147. },
  148. /* Windows 10 Creator's Edition+
  149. * 8B 86 F8 2B 00 00 mov eax, [esi+2BF8h]
  150. * 83 B8 00 4D 00 00 00 cmp dword ptr [eax+4D00h], 0
  151. * 75 0F jnz short loc_100D793C
  152. * 68 C3 1A 00 00 push 1AC3h
  153. */
  154. {
  155. 0x8B, 0x80, 0x00, 0x00, 0x00, 0x00,
  156. 0x83, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
  157. 0x75, 0x00,
  158. 0x68, 0x00, 0x00, 0x00, 0x00
  159. }
  160. };
  161. #endif
  162. #define MAX_FUNC_SCAN_BYTES 200
  163. static inline bool pattern_matches(uint8_t *byte, uint32_t *offset1,
  164. uint32_t *offset2)
  165. {
  166. for (size_t j = 0; j < sizeof(mask) / sizeof(mask[0]); j++) {
  167. for (size_t i = 0; i < MAX_CMP_SIZE; i++) {
  168. if ((byte[i] & mask[j][i]) != mask_cmp[j][i])
  169. goto next_signature;
  170. }
  171. *offset1 = code_offsets[j][0];
  172. *offset2 = code_offsets[j][1];
  173. return true;
  174. next_signature:;
  175. }
  176. return false;
  177. }
  178. void get_d3d9_offsets(struct d3d9_offsets *offsets)
  179. {
  180. d3d9_info info = {};
  181. bool success = d3d9_init(info);
  182. if (success) {
  183. uint8_t **vt = *(uint8_t***)info.device;
  184. uint8_t *crr = vt[125];
  185. offsets->present = vtable_offset(info.module, info.device, 17);
  186. offsets->present_ex = vtable_offset(info.module, info.device,
  187. 121);
  188. offsets->present_swap = vtable_offset(info.module, info.swap,
  189. 3);
  190. uint32_t offset1, offset2;
  191. for (size_t i = 0; i < MAX_FUNC_SCAN_BYTES; i++) {
  192. if (pattern_matches(&crr[i], &offset1, &offset2)) {
  193. #define get_offset(x) *(uint32_t*)&crr[i + x]
  194. uint32_t off1 = get_offset(offset1);
  195. uint32_t off2 = get_offset(offset2);
  196. /* check to make sure offsets are within
  197. * expected values */
  198. if (off1 > 0xFFFF || off2 > 0xFFFF)
  199. break;
  200. /* check to make sure offsets actually point
  201. * toward expected data */
  202. #ifdef _MSC_VER
  203. __try {
  204. uint8_t *ptr = (uint8_t*)(info.device);
  205. uint8_t *d3d9_ptr =
  206. *(uint8_t**)(ptr + off1);
  207. if (d3d9_ptr != (uint8_t*)info.d3d9ex)
  208. break;
  209. BOOL &is_d3d9ex =
  210. *(BOOL*)(d3d9_ptr + off2);
  211. if (is_d3d9ex != TRUE)
  212. break;
  213. } __except(EXCEPTION_EXECUTE_HANDLER) {
  214. break;
  215. }
  216. #endif
  217. offsets->d3d9_clsoff = off1;
  218. offsets->is_d3d9ex_clsoff = off2;
  219. break;
  220. }
  221. }
  222. }
  223. d3d9_free(info);
  224. }