game-capture-file-init.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include <windows.h>
  2. #include <strsafe.h>
  3. #include <shlobj.h>
  4. #include <obs-module.h>
  5. #include <util/windows/win-version.h>
  6. #include <util/platform.h>
  7. #include <util/c99defs.h>
  8. #include <util/base.h>
  9. /* ------------------------------------------------------------------------- */
  10. /* helper funcs */
  11. static bool has_elevation()
  12. {
  13. SID_IDENTIFIER_AUTHORITY sia = SECURITY_NT_AUTHORITY;
  14. PSID sid = NULL;
  15. BOOL elevated = false;
  16. BOOL success;
  17. success = AllocateAndInitializeSid(&sia, 2, SECURITY_BUILTIN_DOMAIN_RID,
  18. DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0,
  19. 0, 0, &sid);
  20. if (success && sid) {
  21. CheckTokenMembership(NULL, sid, &elevated);
  22. FreeSid(sid);
  23. }
  24. return elevated;
  25. }
  26. static inline bool file_exists(const wchar_t *path)
  27. {
  28. WIN32_FIND_DATAW wfd;
  29. HANDLE h = FindFirstFileW(path, &wfd);
  30. if (h == INVALID_HANDLE_VALUE)
  31. return false;
  32. FindClose(h);
  33. return true;
  34. }
  35. static LSTATUS get_reg(HKEY hkey, LPCWSTR sub_key, LPCWSTR value_name, bool b64)
  36. {
  37. HKEY key;
  38. LSTATUS status;
  39. DWORD flags = b64 ? KEY_WOW64_64KEY : KEY_WOW64_32KEY;
  40. DWORD size = sizeof(DWORD);
  41. DWORD val;
  42. status = RegOpenKeyEx(hkey, sub_key, 0, KEY_READ | flags, &key);
  43. if (status == ERROR_SUCCESS) {
  44. status = RegQueryValueExW(key, value_name, NULL, NULL,
  45. (LPBYTE)&val, &size);
  46. RegCloseKey(key);
  47. }
  48. return status;
  49. }
  50. #define get_programdata_path(path, subpath) \
  51. do { \
  52. SHGetFolderPathW(NULL, CSIDL_COMMON_APPDATA, NULL, \
  53. SHGFP_TYPE_CURRENT, path); \
  54. StringCbCatW(path, sizeof(path), L"\\"); \
  55. StringCbCatW(path, sizeof(path), subpath); \
  56. } while (false)
  57. #define make_filename(str, name, ext) \
  58. do { \
  59. StringCbCatW(str, sizeof(str), name); \
  60. StringCbCatW(str, sizeof(str), b64 ? L"64" : L"32"); \
  61. StringCbCatW(str, sizeof(str), ext); \
  62. } while (false)
  63. /* ------------------------------------------------------------------------- */
  64. /* function to get the path to the hook */
  65. static bool programdata64_hook_exists = false;
  66. static bool programdata32_hook_exists = false;
  67. char *get_hook_path(bool b64)
  68. {
  69. wchar_t path[MAX_PATH];
  70. get_programdata_path(path, L"obs-studio-hook\\");
  71. make_filename(path, L"graphics-hook", L".dll");
  72. if ((b64 && programdata64_hook_exists) ||
  73. (!b64 && programdata32_hook_exists)) {
  74. char *path_utf8 = NULL;
  75. os_wcs_to_utf8_ptr(path, 0, &path_utf8);
  76. return path_utf8;
  77. }
  78. return obs_module_file(b64 ? "graphics-hook64.dll"
  79. : "graphics-hook32.dll");
  80. }
  81. /* ------------------------------------------------------------------------- */
  82. /* initialization */
  83. #define IMPLICIT_LAYERS L"SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers"
  84. static bool update_hook_file(bool b64)
  85. {
  86. wchar_t temp[MAX_PATH];
  87. wchar_t src[MAX_PATH];
  88. wchar_t dst[MAX_PATH];
  89. wchar_t src_json[MAX_PATH];
  90. wchar_t dst_json[MAX_PATH];
  91. StringCbCopyW(temp, sizeof(temp),
  92. L"..\\..\\data\\obs-plugins\\"
  93. L"win-capture\\");
  94. make_filename(temp, L"obs-vulkan", L".json");
  95. if (_wfullpath(src_json, temp, MAX_PATH) == NULL)
  96. return false;
  97. StringCbCopyW(temp, sizeof(temp),
  98. L"..\\..\\data\\obs-plugins\\"
  99. L"win-capture\\");
  100. make_filename(temp, L"graphics-hook", L".dll");
  101. if (_wfullpath(src, temp, MAX_PATH) == NULL)
  102. return false;
  103. get_programdata_path(temp, L"obs-studio-hook\\");
  104. StringCbCopyW(dst_json, sizeof(dst_json), temp);
  105. StringCbCopyW(dst, sizeof(dst), temp);
  106. make_filename(dst_json, L"obs-vulkan", L".json");
  107. make_filename(dst, L"graphics-hook", L".dll");
  108. if (!file_exists(src)) {
  109. return false;
  110. }
  111. if (!file_exists(src_json)) {
  112. return false;
  113. }
  114. if (!file_exists(dst) || !file_exists(dst_json)) {
  115. CreateDirectoryW(temp, NULL);
  116. if (!CopyFileW(src_json, dst_json, false))
  117. return false;
  118. if (!CopyFileW(src, dst, false))
  119. return false;
  120. return true;
  121. }
  122. struct win_version_info ver_src = {0};
  123. struct win_version_info ver_dst = {0};
  124. if (!get_dll_ver(src, &ver_src))
  125. return false;
  126. if (!get_dll_ver(dst, &ver_dst))
  127. return false;
  128. /* if source is greater than dst, overwrite new file */
  129. while (win_version_compare(&ver_dst, &ver_src) < 0) {
  130. if (!CopyFileW(src_json, dst_json, false))
  131. return false;
  132. if (!CopyFileW(src, dst, false))
  133. return false;
  134. if (!get_dll_ver(dst, &ver_dst))
  135. return false;
  136. }
  137. /* do not use if major version incremented in target compared to
  138. * ours */
  139. if (ver_dst.major > ver_src.major) {
  140. return false;
  141. }
  142. return true;
  143. }
  144. #define warn(format, ...) \
  145. blog(LOG_WARNING, "%s: " format, "[Vulkan Capture Init]", ##__VA_ARGS__)
  146. /* Sets vulkan layer registry if it doesn't already exist */
  147. static void init_vulkan_registry(bool b64)
  148. {
  149. HKEY key = NULL;
  150. LSTATUS s;
  151. wchar_t path[MAX_PATH];
  152. get_programdata_path(path, L"obs-studio-hook\\");
  153. make_filename(path, L"obs-vulkan", L".json");
  154. s = get_reg(HKEY_LOCAL_MACHINE, IMPLICIT_LAYERS, path, b64);
  155. if (s == ERROR_FILE_NOT_FOUND) {
  156. s = get_reg(HKEY_CURRENT_USER, IMPLICIT_LAYERS, path, b64);
  157. if (s != ERROR_FILE_NOT_FOUND && s != ERROR_SUCCESS) {
  158. warn("Failed to query registry keys: %d", (int)s);
  159. goto finish;
  160. }
  161. } else if (s != ERROR_SUCCESS) {
  162. warn("Failed to query registry keys: %d", (int)s);
  163. goto finish;
  164. }
  165. if (s == ERROR_SUCCESS) {
  166. goto finish;
  167. }
  168. HKEY type = has_elevation() ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
  169. DWORD flags = b64 ? KEY_WOW64_64KEY : KEY_WOW64_32KEY;
  170. DWORD temp;
  171. s = RegCreateKeyExW(type, IMPLICIT_LAYERS, 0, NULL, 0,
  172. KEY_WRITE | flags, NULL, &key, &temp);
  173. if (s != ERROR_SUCCESS) {
  174. warn("Failed to create registry key");
  175. goto finish;
  176. }
  177. DWORD zero = 0;
  178. s = RegSetValueExW(key, path, 0, REG_DWORD, (const BYTE *)&zero,
  179. sizeof(zero));
  180. if (s != ERROR_SUCCESS) {
  181. warn("Failed to set registry value");
  182. }
  183. finish:
  184. if (key)
  185. RegCloseKey(key);
  186. }
  187. void init_hook_files()
  188. {
  189. if (update_hook_file(true)) {
  190. programdata64_hook_exists = true;
  191. init_vulkan_registry(true);
  192. }
  193. if (update_hook_file(false)) {
  194. programdata32_hook_exists = true;
  195. init_vulkan_registry(false);
  196. }
  197. }