load-graphics-offsets.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include <obs-module.h>
  2. #include <util/windows/win-version.h>
  3. #include <util/platform.h>
  4. #include <util/dstr.h>
  5. #include <util/config-file.h>
  6. #include <util/pipe.h>
  7. #include <windows.h>
  8. #include "graphics-hook-info.h"
  9. extern struct graphics_offsets offsets32;
  10. extern struct graphics_offsets offsets64;
  11. static inline bool load_offsets_from_string(struct graphics_offsets *offsets,
  12. const char *str)
  13. {
  14. config_t *config;
  15. if (config_open_string(&config, str) != CONFIG_SUCCESS) {
  16. return false;
  17. }
  18. offsets->d3d8.present =
  19. (uint32_t)config_get_uint(config, "d3d8", "present");
  20. offsets->d3d9.present =
  21. (uint32_t)config_get_uint(config, "d3d9", "present");
  22. offsets->d3d9.present_ex =
  23. (uint32_t)config_get_uint(config, "d3d9", "present_ex");
  24. offsets->d3d9.present_swap =
  25. (uint32_t)config_get_uint(config, "d3d9", "present_swap");
  26. offsets->d3d9.d3d9_clsoff =
  27. (uint32_t)config_get_uint(config, "d3d9", "d3d9_clsoff");
  28. offsets->d3d9.is_d3d9ex_clsoff =
  29. (uint32_t)config_get_uint(config, "d3d9", "is_d3d9ex_clsoff");
  30. offsets->dxgi.present =
  31. (uint32_t)config_get_uint(config, "dxgi", "present");
  32. offsets->dxgi.present1 =
  33. (uint32_t)config_get_uint(config, "dxgi", "present1");
  34. offsets->dxgi.resize =
  35. (uint32_t)config_get_uint(config, "dxgi", "resize");
  36. offsets->dxgi2.release =
  37. (uint32_t)config_get_uint(config, "dxgi", "release");
  38. config_close(config);
  39. return true;
  40. }
  41. static inline bool load_offsets_from_file(struct graphics_offsets *offsets,
  42. const char *file)
  43. {
  44. char *str = os_quick_read_utf8_file(file);
  45. bool success = false;
  46. if (str && *str)
  47. success = load_offsets_from_string(offsets, str);
  48. bfree(str);
  49. return success;
  50. }
  51. static inline bool config_ver_mismatch(config_t *ver_config,
  52. const char *section,
  53. struct win_version_info *ver)
  54. {
  55. struct win_version_info config_ver;
  56. bool mismatch = false;
  57. #define get_sub_ver(subver) \
  58. config_ver.subver = (int)config_get_int(ver_config, section, #subver); \
  59. mismatch |= config_ver.subver != ver->subver;
  60. get_sub_ver(major);
  61. get_sub_ver(minor);
  62. get_sub_ver(build);
  63. get_sub_ver(revis);
  64. #undef get_sub_ver
  65. return mismatch;
  66. }
  67. static inline void write_config_ver(config_t *ver_config, const char *section,
  68. struct win_version_info *ver)
  69. {
  70. #define set_sub_ver(subver) \
  71. config_set_int(ver_config, section, #subver, ver->subver);
  72. set_sub_ver(major);
  73. set_sub_ver(minor);
  74. set_sub_ver(build);
  75. set_sub_ver(revis);
  76. #undef set_sub_ver
  77. }
  78. static bool get_32bit_system_dll_ver(const wchar_t *system_lib,
  79. struct win_version_info *ver)
  80. {
  81. wchar_t path[MAX_PATH];
  82. UINT ret;
  83. #ifdef _WIN64
  84. ret = GetSystemWow64DirectoryW(path, MAX_PATH);
  85. #else
  86. ret = GetSystemDirectoryW(path, MAX_PATH);
  87. #endif
  88. if (!ret) {
  89. blog(LOG_ERROR,
  90. "Failed to get windows 32bit system path: "
  91. "%lu",
  92. GetLastError());
  93. return false;
  94. }
  95. wcscat(path, L"\\");
  96. wcscat(path, system_lib);
  97. return get_dll_ver(path, ver);
  98. }
  99. bool cached_versions_match(void)
  100. {
  101. struct win_version_info d3d8_ver = {0};
  102. struct win_version_info d3d9_ver = {0};
  103. struct win_version_info dxgi_ver = {0};
  104. bool ver_mismatch = false;
  105. config_t *config;
  106. char *ver_file;
  107. int ret;
  108. ver_mismatch |= !get_32bit_system_dll_ver(L"d3d8.dll", &d3d8_ver);
  109. ver_mismatch |= !get_32bit_system_dll_ver(L"d3d9.dll", &d3d9_ver);
  110. ver_mismatch |= !get_32bit_system_dll_ver(L"dxgi.dll", &dxgi_ver);
  111. ver_file = obs_module_config_path("version.ini");
  112. if (!ver_file)
  113. return false;
  114. ret = config_open(&config, ver_file, CONFIG_OPEN_ALWAYS);
  115. if (ret != CONFIG_SUCCESS)
  116. goto failed;
  117. ver_mismatch |= config_ver_mismatch(config, "d3d8", &d3d8_ver);
  118. ver_mismatch |= config_ver_mismatch(config, "d3d9", &d3d9_ver);
  119. ver_mismatch |= config_ver_mismatch(config, "dxgi", &dxgi_ver);
  120. if (ver_mismatch) {
  121. write_config_ver(config, "d3d8", &d3d8_ver);
  122. write_config_ver(config, "d3d9", &d3d9_ver);
  123. write_config_ver(config, "dxgi", &dxgi_ver);
  124. config_save_safe(config, "tmp", NULL);
  125. }
  126. failed:
  127. bfree(ver_file);
  128. config_close(config);
  129. return !ver_mismatch;
  130. }
  131. bool load_graphics_offsets(bool is32bit, bool use_hook_address_cache,
  132. const char *config_path)
  133. {
  134. char *offset_exe_path = NULL;
  135. struct dstr config_ini = {0};
  136. struct dstr offset_exe = {0};
  137. struct dstr str = {0};
  138. os_process_pipe_t *pp;
  139. bool success = false;
  140. char data[2048];
  141. #ifndef _WIN64
  142. if (!is32bit && !is_64_bit_windows()) {
  143. return true;
  144. }
  145. #endif
  146. dstr_copy(&offset_exe, "get-graphics-offsets");
  147. dstr_cat(&offset_exe, is32bit ? "32.exe" : "64.exe");
  148. offset_exe_path = obs_module_file(offset_exe.array);
  149. pp = os_process_pipe_create(offset_exe_path, "r");
  150. if (!pp) {
  151. blog(LOG_INFO, "load_graphics_offsets: Failed to start '%s'",
  152. offset_exe.array);
  153. goto error;
  154. }
  155. for (;;) {
  156. size_t len =
  157. os_process_pipe_read(pp, (uint8_t *)data, sizeof(data));
  158. if (!len)
  159. break;
  160. dstr_ncat(&str, data, len);
  161. }
  162. if (dstr_is_empty(&str)) {
  163. blog(LOG_INFO,
  164. "load_graphics_offsets: Failed to read "
  165. "from '%s'",
  166. offset_exe.array);
  167. goto error;
  168. }
  169. if (use_hook_address_cache) {
  170. dstr_copy(&config_ini, config_path);
  171. dstr_cat(&config_ini, is32bit ? "32.ini" : "64.ini");
  172. os_quick_write_utf8_file_safe(config_ini.array, str.array,
  173. str.len, false, "tmp", NULL);
  174. dstr_free(&config_ini);
  175. }
  176. success = load_offsets_from_string(is32bit ? &offsets32 : &offsets64,
  177. str.array);
  178. if (!success) {
  179. blog(LOG_INFO, "load_graphics_offsets: Failed to load string");
  180. }
  181. os_process_pipe_destroy(pp);
  182. error:
  183. bfree(offset_exe_path);
  184. dstr_free(&offset_exe);
  185. dstr_free(&str);
  186. return success;
  187. }
  188. bool load_cached_graphics_offsets(bool is32bit, const char *config_path)
  189. {
  190. struct dstr config_ini = {0};
  191. bool success;
  192. dstr_copy(&config_ini, config_path);
  193. dstr_cat(&config_ini, is32bit ? "32.ini" : "64.ini");
  194. success = load_offsets_from_file(is32bit ? &offsets32 : &offsets64,
  195. config_ini.array);
  196. if (!success)
  197. success = load_graphics_offsets(is32bit, true, config_path);
  198. dstr_free(&config_ini);
  199. return success;
  200. }