load-graphics-offsets.c 5.7 KB

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