d3d9-patches.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #pragma once
  2. #include <stdint.h>
  3. #ifdef __MINGW32__
  4. #include <excpt.h>
  5. #ifndef TRYLEVEL_NONE
  6. #ifndef __MINGW64__
  7. #define NO_SEH_MINGW
  8. #endif
  9. #ifndef __try
  10. #define __try
  11. #endif
  12. #ifndef __except
  13. #define __except(x) if (0)
  14. #endif
  15. #endif
  16. #endif
  17. static inline int safe_memcmp(const void *p1, const void *p2, size_t size)
  18. {
  19. // Disabled exceptions on mingw-w64 as it is broken
  20. #ifndef __MINGW32__
  21. #ifdef NO_SEH_MINGW
  22. __try1(EXCEPTION_EXECUTE_HANDLER)
  23. #else
  24. __try
  25. #endif
  26. #endif
  27. {
  28. return memcmp(p1, p2, size);
  29. }
  30. // Disabled exceptions on mingw-w64 as it is broken
  31. #ifndef __MINGW32__
  32. #ifdef NO_SEH_MINGW
  33. __except1
  34. #else
  35. __except(EXCEPTION_EXECUTE_HANDLER)
  36. #endif
  37. {
  38. return -1;
  39. }
  40. #endif
  41. }
  42. struct patch_info {
  43. size_t size;
  44. const BYTE *data;
  45. };
  46. #define NEW_PATCH(x) {sizeof(x), (x)}
  47. #define MAX_PATCH_SIZE 2
  48. static const BYTE force_jump[] = {0xEB};
  49. static const BYTE ignore_jump[] = {0x90, 0x90};
  50. #ifdef _WIN64
  51. #define NUM_VERS (13)
  52. #define CMP_SIZE (13)
  53. static const uintptr_t patch_offset[NUM_VERS] = {
  54. 0x54FE6, //win7 - 6.1.7600.16385
  55. 0x55095, //win7 - 6.1.7601.16562
  56. 0x550C5, //win7 - 6.1.7601.17514
  57. 0x6FE18, //win10 - 10.0.10240.16412
  58. 0x70050, //win10 - 10.0.10240.16384
  59. 0x703F8, //win10 - 10.0.10162.0
  60. 0x8BDB5, //win8.1 - 6.3.9431.00000
  61. 0x8E635, //win8.1 - 6.3.9600.17415
  62. 0x90352, //win8.1 - 6.3.9600.17085
  63. 0x9038A, //win8.1 - 6.3.9600.17095
  64. 0x93AFA, //win8.1 - 6.3.9600.16384
  65. 0x93B8A, //win8.1 - 6.3.9600.16404
  66. 0x1841E5 //win8 - 6.2.9200.16384
  67. };
  68. static const uint8_t patch_cmp[NUM_VERS][CMP_SIZE] = {
  69. {0x48, 0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x39, 0x98, 0x68, 0x50, 0x00, 0x00},
  70. {0x48, 0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x39, 0x98, 0x68, 0x50, 0x00, 0x00},
  71. {0x48, 0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x39, 0x98, 0x68, 0x50, 0x00, 0x00},
  72. {0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x44, 0x39, 0x98, 0x88, 0x51, 0x00, 0x00},
  73. {0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x44, 0x39, 0x98, 0x88, 0x51, 0x00, 0x00},
  74. {0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x44, 0x39, 0x98, 0x88, 0x51, 0x00, 0x00},
  75. {0x48, 0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x39, 0xB0, 0x28, 0x51, 0x00, 0x00},
  76. {0x48, 0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x39, 0xA8, 0x28, 0x51, 0x00, 0x00},
  77. {0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x44, 0x39, 0xA0, 0x28, 0x51, 0x00, 0x00},
  78. {0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x44, 0x39, 0xA0, 0x28, 0x51, 0x00, 0x00},
  79. {0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x44, 0x39, 0xA0, 0x28, 0x51, 0x00, 0x00},
  80. {0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x44, 0x39, 0xA0, 0x28, 0x51, 0x00, 0x00},
  81. {0x49, 0x8b, 0x85, 0xb8, 0x3d, 0x00, 0x00, 0x39, 0x88, 0xc8, 0x50, 0x00, 0x00},
  82. };
  83. static const struct patch_info patch[NUM_VERS] = {
  84. NEW_PATCH(force_jump),
  85. NEW_PATCH(force_jump),
  86. NEW_PATCH(force_jump),
  87. NEW_PATCH(force_jump),
  88. NEW_PATCH(force_jump),
  89. NEW_PATCH(force_jump),
  90. NEW_PATCH(ignore_jump),
  91. NEW_PATCH(ignore_jump),
  92. NEW_PATCH(ignore_jump),
  93. NEW_PATCH(ignore_jump),
  94. NEW_PATCH(ignore_jump),
  95. NEW_PATCH(ignore_jump),
  96. NEW_PATCH(ignore_jump),
  97. };
  98. #else
  99. #define NUM_VERS (13)
  100. #define CMP_SIZE (12)
  101. static const uintptr_t patch_offset[NUM_VERS] = {
  102. 0x79AA6, //win7 - 6.1.7601.16562
  103. 0x79C9E, //win7 - 6.1.7600.16385
  104. 0x79D96, //win7 - 6.1.7601.17514
  105. 0x7F9BD, //win8.1 - 6.3.9431.00000
  106. 0x8A3F4, //win8.1 - 6.3.9600.16404
  107. 0x8B15F, //win10 - 10.0.10240.16384
  108. 0x8B19F, //win10 - 10.0.10162.0
  109. 0x8B83F, //win10 - 10.0.10240.16412
  110. 0x8E9F7, //win8.1 - 6.3.9600.17095
  111. 0x8F00F, //win8.1 - 6.3.9600.17085
  112. 0x8FBB1, //win8.1 - 6.3.9600.16384
  113. 0x90264, //win8.1 - 6.3.9600.17415
  114. 0x166A08 //win8 - 6.2.9200.16384
  115. };
  116. static const uint8_t patch_cmp[NUM_VERS][CMP_SIZE] = {
  117. {0x8b, 0x89, 0xe8, 0x29, 0x00, 0x00, 0x39, 0xb9, 0x80, 0x4b, 0x00, 0x00},
  118. {0x8b, 0x89, 0xe8, 0x29, 0x00, 0x00, 0x39, 0xb9, 0x80, 0x4b, 0x00, 0x00},
  119. {0x8b, 0x89, 0xe8, 0x29, 0x00, 0x00, 0x39, 0xb9, 0x80, 0x4b, 0x00, 0x00},
  120. {0x8b, 0x80, 0xe8, 0x29, 0x00, 0x00, 0x39, 0xb0, 0x40, 0x4c, 0x00, 0x00},
  121. {0x80, 0xe8, 0x29, 0x00, 0x00, 0x83, 0xb8, 0x40, 0x4c, 0x00, 0x00, 0x00},
  122. {0x81, 0xe8, 0x29, 0x00, 0x00, 0x83, 0xb8, 0xa0, 0x4c, 0x00, 0x00, 0x00},
  123. {0x81, 0xe8, 0x29, 0x00, 0x00, 0x83, 0xb8, 0xa0, 0x4c, 0x00, 0x00, 0x00},
  124. {0x81, 0xe8, 0x29, 0x00, 0x00, 0x83, 0xb8, 0xa0, 0x4c, 0x00, 0x00, 0x00},
  125. {0x80, 0xe8, 0x29, 0x00, 0x00, 0x83, 0xb8, 0x40, 0x4c, 0x00, 0x00, 0x00},
  126. {0x80, 0xe8, 0x29, 0x00, 0x00, 0x83, 0xb8, 0x40, 0x4c, 0x00, 0x00, 0x00},
  127. {0x80, 0xe8, 0x29, 0x00, 0x00, 0x83, 0xb8, 0x40, 0x4c, 0x00, 0x00, 0x00},
  128. {0x87, 0xe8, 0x29, 0x00, 0x00, 0x83, 0xb8, 0x40, 0x4c, 0x00, 0x00, 0x00},
  129. {0x8b, 0x80, 0xe8, 0x29, 0x00, 0x00, 0x39, 0x90, 0xb0, 0x4b, 0x00, 0x00},
  130. };
  131. static const struct patch_info patch[NUM_VERS] = {
  132. NEW_PATCH(force_jump),
  133. NEW_PATCH(force_jump),
  134. NEW_PATCH(force_jump),
  135. NEW_PATCH(force_jump),
  136. NEW_PATCH(force_jump),
  137. NEW_PATCH(ignore_jump),
  138. NEW_PATCH(ignore_jump),
  139. NEW_PATCH(ignore_jump),
  140. NEW_PATCH(force_jump),
  141. NEW_PATCH(force_jump),
  142. NEW_PATCH(force_jump),
  143. NEW_PATCH(ignore_jump),
  144. NEW_PATCH(force_jump),
  145. };
  146. #endif
  147. static inline int get_d3d9_patch(HMODULE d3d9)
  148. {
  149. uint8_t *addr = (uint8_t*)d3d9;
  150. for (int i = 0; i < NUM_VERS; i++) {
  151. int ret = safe_memcmp(addr + patch_offset[i], patch_cmp[i],
  152. CMP_SIZE);
  153. if (ret == 0)
  154. return i;
  155. }
  156. return -1;
  157. }
  158. static inline uint8_t *get_d3d9_patch_addr(HMODULE d3d9, int patch)
  159. {
  160. if (patch == -1)
  161. return nullptr;
  162. uint8_t *addr = (uint8_t*)d3d9;
  163. return addr + patch_offset[patch] + CMP_SIZE;
  164. }