d3d9-offsets.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 CMP_SIZE 21
  71. static const uint8_t mask[CMP_SIZE] =
  72. {0xF8, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00,
  73. 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00,
  74. 0xFF, 0x00,
  75. 0xF8, 0xF8, 0x00, 0x00, 0x00, 0x00};
  76. static const uint8_t mask_cmp[CMP_SIZE] =
  77. {0x48, 0x8B, 0x80, 0x00, 0x00, 0x00, 0x00,
  78. 0x39, 0x80, 0x00, 0x00, 0x00, 0x00,
  79. 0x75, 0x00,
  80. 0x40, 0xB8, 0x00, 0x00, 0x00, 0x00};
  81. #else
  82. #define CMP_SIZE 19
  83. static const uint8_t mask[CMP_SIZE] =
  84. {0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00,
  85. 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00,
  86. 0xFF, 0x00,
  87. 0xFF, 0x00, 0x00, 0x00, 0x00};
  88. static const uint8_t mask_cmp[CMP_SIZE] =
  89. {0x8B, 0x80, 0x00, 0x00, 0x00, 0x00,
  90. 0x39, 0x80, 0x00, 0x00, 0x00, 0x00,
  91. 0x75, 0x00,
  92. 0x68, 0x00, 0x00, 0x00, 0x00};
  93. #endif
  94. #define MAX_FUNC_SCAN_BYTES 200
  95. static inline bool pattern_matches(uint8_t *byte)
  96. {
  97. for (size_t i = 0; i < CMP_SIZE; i++) {
  98. if ((byte[i] & mask[i]) != mask_cmp[i])
  99. return false;
  100. }
  101. return true;
  102. }
  103. void get_d3d9_offsets(struct d3d9_offsets *offsets)
  104. {
  105. d3d9_info info = {};
  106. bool success = d3d9_init(info);
  107. if (success) {
  108. uint8_t **vt = *(uint8_t***)info.device;
  109. uint8_t *crr = vt[125];
  110. offsets->present = vtable_offset(info.module, info.device, 17);
  111. offsets->present_ex = vtable_offset(info.module, info.device,
  112. 121);
  113. offsets->present_swap = vtable_offset(info.module, info.swap,
  114. 3);
  115. for (size_t i = 0; i < MAX_FUNC_SCAN_BYTES; i++) {
  116. if (pattern_matches(&crr[i])) {
  117. #define get_offset(x) *(uint32_t*)&crr[i + x]
  118. #ifdef _WIN64
  119. uint32_t off1 = get_offset(3);
  120. uint32_t off2 = get_offset(9);
  121. #else
  122. uint32_t off1 = get_offset(2);
  123. uint32_t off2 = get_offset(8);
  124. #endif
  125. /* check to make sure offsets are within
  126. * expected values */
  127. if (off1 > 0xFFFF || off2 > 0xFFFF)
  128. break;
  129. /* check to make sure offsets actually point
  130. * toward expected data */
  131. #ifdef _MSC_VER
  132. __try {
  133. uint8_t *ptr = (uint8_t*)(info.device);
  134. uint8_t *d3d9_ptr =
  135. *(uint8_t**)(ptr + off1);
  136. if (d3d9_ptr != (uint8_t*)info.d3d9ex)
  137. break;
  138. BOOL &is_d3d9ex =
  139. *(BOOL*)(d3d9_ptr + off2);
  140. if (is_d3d9ex != TRUE)
  141. break;
  142. } __except(EXCEPTION_EXECUTE_HANDLER) {
  143. break;
  144. }
  145. #endif
  146. offsets->d3d9_clsoff = off1;
  147. offsets->is_d3d9ex_clsoff = off2;
  148. break;
  149. }
  150. }
  151. }
  152. d3d9_free(info);
  153. }