load-graphics-offsets.c 5.3 KB

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