d3d9-patches.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 (10)
  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. 0x8BDB5, //win8.1 - 6.3.9431.00000
  58. 0x8E635, //win8.1 - 6.3.9600.17415
  59. 0x90352, //win8.1 - 6.3.9600.17085
  60. 0x9038A, //win8.1 - 6.3.9600.17095
  61. 0x93AFA, //win8.1 - 6.3.9600.16384
  62. 0x93B8A, //win8.1 - 6.3.9600.16404
  63. 0x1841E5 //win8 - 6.2.9200.16384
  64. };
  65. static const uint8_t patch_cmp[NUM_VERS][CMP_SIZE] = {
  66. {0x48, 0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x39, 0x98, 0x68, 0x50, 0x00, 0x00},
  67. {0x48, 0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x39, 0x98, 0x68, 0x50, 0x00, 0x00},
  68. {0x48, 0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x39, 0x98, 0x68, 0x50, 0x00, 0x00},
  69. {0x48, 0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x39, 0xB0, 0x28, 0x51, 0x00, 0x00},
  70. {0x48, 0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x39, 0xA8, 0x28, 0x51, 0x00, 0x00},
  71. {0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x44, 0x39, 0xA0, 0x28, 0x51, 0x00, 0x00},
  72. {0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x44, 0x39, 0xA0, 0x28, 0x51, 0x00, 0x00},
  73. {0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x44, 0x39, 0xA0, 0x28, 0x51, 0x00, 0x00},
  74. {0x8b, 0x81, 0xb8, 0x3d, 0x00, 0x00, 0x44, 0x39, 0xA0, 0x28, 0x51, 0x00, 0x00},
  75. {0x49, 0x8b, 0x85, 0xb8, 0x3d, 0x00, 0x00, 0x39, 0x88, 0xc8, 0x50, 0x00, 0x00},
  76. };
  77. static const struct patch_info patch[NUM_VERS] = {
  78. NEW_PATCH(force_jump),
  79. NEW_PATCH(force_jump),
  80. NEW_PATCH(force_jump),
  81. NEW_PATCH(ignore_jump),
  82. NEW_PATCH(ignore_jump),
  83. NEW_PATCH(ignore_jump),
  84. NEW_PATCH(ignore_jump),
  85. NEW_PATCH(ignore_jump),
  86. NEW_PATCH(ignore_jump),
  87. NEW_PATCH(ignore_jump),
  88. };
  89. #else
  90. #define NUM_VERS (10)
  91. #define CMP_SIZE (12)
  92. static const uintptr_t patch_offset[NUM_VERS] = {
  93. 0x79AA6, //win7 - 6.1.7601.16562
  94. 0x79C9E, //win7 - 6.1.7600.16385
  95. 0x79D96, //win7 - 6.1.7601.17514
  96. 0x7F9BD, //win8.1 - 6.3.9431.00000
  97. 0x8A3F4, //win8.1 - 6.3.9600.16404
  98. 0x8E9F7, //win8.1 - 6.3.9600.17095
  99. 0x8F00F, //win8.1 - 6.3.9600.17085
  100. 0x8FBB1, //win8.1 - 6.3.9600.16384
  101. 0x90264, //win8.1 - 6.3.9600.17415
  102. 0x166A08 //win8 - 6.2.9200.16384
  103. };
  104. static const uint8_t patch_cmp[NUM_VERS][CMP_SIZE] = {
  105. {0x8b, 0x89, 0xe8, 0x29, 0x00, 0x00, 0x39, 0xb9, 0x80, 0x4b, 0x00, 0x00},
  106. {0x8b, 0x89, 0xe8, 0x29, 0x00, 0x00, 0x39, 0xb9, 0x80, 0x4b, 0x00, 0x00},
  107. {0x8b, 0x89, 0xe8, 0x29, 0x00, 0x00, 0x39, 0xb9, 0x80, 0x4b, 0x00, 0x00},
  108. {0x8b, 0x80, 0xe8, 0x29, 0x00, 0x00, 0x39, 0xb0, 0x40, 0x4c, 0x00, 0x00},
  109. {0x80, 0xe8, 0x29, 0x00, 0x00, 0x83, 0xb8, 0x40, 0x4c, 0x00, 0x00, 0x00},
  110. {0x80, 0xe8, 0x29, 0x00, 0x00, 0x83, 0xb8, 0x40, 0x4c, 0x00, 0x00, 0x00},
  111. {0x80, 0xe8, 0x29, 0x00, 0x00, 0x83, 0xb8, 0x40, 0x4c, 0x00, 0x00, 0x00},
  112. {0x80, 0xe8, 0x29, 0x00, 0x00, 0x83, 0xb8, 0x40, 0x4c, 0x00, 0x00, 0x00},
  113. {0x87, 0xe8, 0x29, 0x00, 0x00, 0x83, 0xb8, 0x40, 0x4c, 0x00, 0x00, 0x00},
  114. {0x8b, 0x80, 0xe8, 0x29, 0x00, 0x00, 0x39, 0x90, 0xb0, 0x4b, 0x00, 0x00},
  115. };
  116. static const struct patch_info patch[NUM_VERS] = {
  117. NEW_PATCH(force_jump),
  118. NEW_PATCH(force_jump),
  119. NEW_PATCH(force_jump),
  120. NEW_PATCH(force_jump),
  121. NEW_PATCH(force_jump),
  122. NEW_PATCH(force_jump),
  123. NEW_PATCH(force_jump),
  124. NEW_PATCH(force_jump),
  125. NEW_PATCH(ignore_jump),
  126. NEW_PATCH(force_jump),
  127. };
  128. #endif
  129. static inline int get_d3d9_patch(HMODULE d3d9)
  130. {
  131. uint8_t *addr = (uint8_t*)d3d9;
  132. for (int i = 0; i < NUM_VERS; i++) {
  133. int ret = safe_memcmp(addr + patch_offset[i], patch_cmp[i],
  134. CMP_SIZE);
  135. if (ret == 0)
  136. return i;
  137. }
  138. return -1;
  139. }
  140. static inline uint8_t *get_d3d9_patch_addr(HMODULE d3d9, int patch)
  141. {
  142. if (patch == -1)
  143. return nullptr;
  144. uint8_t *addr = (uint8_t*)d3d9;
  145. return addr + patch_offset[patch] + CMP_SIZE;
  146. }