load-graphics-offsets.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.resize =
  33. (uint32_t)config_get_uint(config, "dxgi", "resize");
  34. config_close(config);
  35. return true;
  36. }
  37. static inline bool load_offsets_from_file(struct graphics_offsets *offsets,
  38. const char *file)
  39. {
  40. char *str = os_quick_read_utf8_file(file);
  41. bool success = false;
  42. if (str && *str)
  43. success = load_offsets_from_string(offsets, str);
  44. bfree(str);
  45. return success;
  46. }
  47. static inline bool config_ver_mismatch(
  48. config_t *ver_config,
  49. const char *section,
  50. struct win_version_info *ver)
  51. {
  52. struct win_version_info config_ver;
  53. bool mismatch = false;
  54. #define get_sub_ver(subver) \
  55. config_ver.subver = (int)config_get_int(ver_config, section, #subver); \
  56. mismatch |= config_ver.subver != ver->subver;
  57. get_sub_ver(major);
  58. get_sub_ver(minor);
  59. get_sub_ver(build);
  60. get_sub_ver(revis);
  61. #undef get_sub_ver
  62. return mismatch;
  63. }
  64. static inline void write_config_ver(config_t *ver_config, const char *section,
  65. struct win_version_info *ver)
  66. {
  67. #define set_sub_ver(subver) \
  68. config_set_int(ver_config, section, #subver, ver->subver);
  69. set_sub_ver(major);
  70. set_sub_ver(minor);
  71. set_sub_ver(build);
  72. set_sub_ver(revis);
  73. #undef set_sub_ver
  74. }
  75. static bool get_32bit_system_dll_ver(const wchar_t *system_lib,
  76. struct win_version_info *ver)
  77. {
  78. wchar_t path[MAX_PATH];
  79. UINT ret;
  80. #ifdef _WIN64
  81. ret = GetSystemWow64DirectoryW(path, MAX_PATH);
  82. #else
  83. ret = GetSystemDirectoryW(path, MAX_PATH);
  84. #endif
  85. if (!ret) {
  86. blog(LOG_ERROR, "Failed to get windows 32bit system path: "
  87. "%lu", GetLastError());
  88. return false;
  89. }
  90. wcscat(path, L"\\");
  91. wcscat(path, system_lib);
  92. return get_dll_ver(path, ver);
  93. }
  94. bool cached_versions_match(void)
  95. {
  96. struct win_version_info d3d8_ver = {0};
  97. struct win_version_info d3d9_ver = {0};
  98. struct win_version_info dxgi_ver = {0};
  99. bool ver_mismatch = false;
  100. config_t *config;
  101. char *ver_file;
  102. int ret;
  103. ver_mismatch |= !get_32bit_system_dll_ver(L"d3d8.dll", &d3d8_ver);
  104. ver_mismatch |= !get_32bit_system_dll_ver(L"d3d9.dll", &d3d9_ver);
  105. ver_mismatch |= !get_32bit_system_dll_ver(L"dxgi.dll", &dxgi_ver);
  106. ver_file = obs_module_config_path("version.ini");
  107. if (!ver_file)
  108. return false;
  109. ret = config_open(&config, ver_file, CONFIG_OPEN_ALWAYS);
  110. if (ret != CONFIG_SUCCESS)
  111. goto failed;
  112. ver_mismatch |= config_ver_mismatch(config, "d3d8", &d3d8_ver);
  113. ver_mismatch |= config_ver_mismatch(config, "d3d9", &d3d9_ver);
  114. ver_mismatch |= config_ver_mismatch(config, "dxgi", &dxgi_ver);
  115. if (ver_mismatch) {
  116. write_config_ver(config, "d3d8", &d3d8_ver);
  117. write_config_ver(config, "d3d9", &d3d9_ver);
  118. write_config_ver(config, "dxgi", &dxgi_ver);
  119. config_save_safe(config, "tmp", NULL);
  120. }
  121. failed:
  122. bfree(ver_file);
  123. config_close(config);
  124. return !ver_mismatch;
  125. }
  126. bool load_graphics_offsets(bool is32bit)
  127. {
  128. char *offset_exe_path = NULL;
  129. struct dstr offset_exe = {0};
  130. char *config_ini = NULL;
  131. struct dstr str = {0};
  132. os_process_pipe_t *pp;
  133. bool success = false;
  134. char data[128];
  135. #ifndef _WIN64
  136. if (!is32bit && !is_64_bit_windows()) {
  137. return true;
  138. }
  139. #endif
  140. dstr_copy(&offset_exe, "get-graphics-offsets");
  141. dstr_cat(&offset_exe, is32bit ? "32.exe" : "64.exe");
  142. offset_exe_path = obs_module_file(offset_exe.array);
  143. pp = os_process_pipe_create(offset_exe_path, "r");
  144. if (!pp) {
  145. blog(LOG_INFO, "load_graphics_offsets: Failed to start '%s'",
  146. offset_exe.array);
  147. goto error;
  148. }
  149. for (;;) {
  150. size_t len = os_process_pipe_read(pp, (uint8_t*)data, 128);
  151. if (!len)
  152. break;
  153. dstr_ncat(&str, data, len);
  154. }
  155. config_ini = obs_module_config_path(is32bit ? "32.ini" : "64.ini");
  156. os_quick_write_utf8_file_safe(config_ini, str.array, str.len, false,
  157. "tmp", NULL);
  158. bfree(config_ini);
  159. success = load_offsets_from_string(is32bit ? &offsets32 : &offsets64,
  160. str.array);
  161. if (!success) {
  162. blog(LOG_INFO, "load_graphics_offsets: Failed to load string");
  163. }
  164. os_process_pipe_destroy(pp);
  165. error:
  166. bfree(offset_exe_path);
  167. dstr_free(&offset_exe);
  168. dstr_free(&str);
  169. return success;
  170. }
  171. bool load_cached_graphics_offsets(bool is32bit)
  172. {
  173. char *config_ini = NULL;
  174. bool success;
  175. config_ini = obs_module_config_path(is32bit ? "32.ini" : "64.ini");
  176. success = load_offsets_from_file(is32bit ? &offsets32 : &offsets64,
  177. config_ini);
  178. if (!success)
  179. success = load_graphics_offsets(is32bit);
  180. bfree(config_ini);
  181. return success;
  182. }