game-capture-file-init.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include <windows.h>
  2. #include <strsafe.h>
  3. #include <shlobj.h>
  4. #include <aclapi.h>
  5. #include <sddl.h>
  6. #include <obs-module.h>
  7. #include <util/windows/win-version.h>
  8. #include <util/platform.h>
  9. #include <util/c99defs.h>
  10. #include <util/base.h>
  11. /* ------------------------------------------------------------------------- */
  12. /* helper funcs */
  13. static bool has_elevation_internal()
  14. {
  15. SID_IDENTIFIER_AUTHORITY sia = SECURITY_NT_AUTHORITY;
  16. PSID sid = NULL;
  17. BOOL elevated = false;
  18. BOOL success;
  19. success = AllocateAndInitializeSid(&sia, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0,
  20. 0, &sid);
  21. if (success && sid) {
  22. CheckTokenMembership(NULL, sid, &elevated);
  23. FreeSid(sid);
  24. }
  25. return elevated;
  26. }
  27. static bool has_elevation()
  28. {
  29. static bool elevated = false;
  30. static bool initialized = false;
  31. if (!initialized) {
  32. elevated = has_elevation_internal();
  33. initialized = true;
  34. }
  35. return elevated;
  36. }
  37. static bool add_aap_perms(const wchar_t *dir)
  38. {
  39. PSECURITY_DESCRIPTOR sd = NULL;
  40. SID *aap_sid = NULL;
  41. SID *bu_sid = NULL;
  42. PACL new_dacl1 = NULL;
  43. PACL new_dacl2 = NULL;
  44. bool success = false;
  45. PACL dacl;
  46. if (GetNamedSecurityInfoW(dir, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, &dacl, NULL, &sd) !=
  47. ERROR_SUCCESS) {
  48. goto fail;
  49. }
  50. EXPLICIT_ACCESSW ea = {0};
  51. ea.grfAccessPermissions = GENERIC_READ | GENERIC_EXECUTE;
  52. ea.grfAccessMode = GRANT_ACCESS;
  53. ea.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  54. ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
  55. /* ALL_APP_PACKAGES */
  56. ConvertStringSidToSidW(L"S-1-15-2-1", &aap_sid);
  57. ea.Trustee.ptstrName = (wchar_t *)aap_sid;
  58. if (SetEntriesInAclW(1, &ea, dacl, &new_dacl1) != ERROR_SUCCESS) {
  59. goto fail;
  60. }
  61. ea.grfAccessPermissions = GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE;
  62. /* BUILTIN_USERS */
  63. ConvertStringSidToSidW(L"S-1-5-32-545", &bu_sid);
  64. ea.Trustee.ptstrName = (wchar_t *)bu_sid;
  65. DWORD s = SetEntriesInAclW(1, &ea, new_dacl1, &new_dacl2);
  66. if (s != ERROR_SUCCESS) {
  67. goto fail;
  68. }
  69. if (SetNamedSecurityInfoW((wchar_t *)dir, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, new_dacl2,
  70. NULL) != ERROR_SUCCESS) {
  71. goto fail;
  72. }
  73. success = true;
  74. fail:
  75. if (sd)
  76. LocalFree(sd);
  77. if (new_dacl1)
  78. LocalFree(new_dacl1);
  79. if (new_dacl2)
  80. LocalFree(new_dacl2);
  81. if (aap_sid)
  82. LocalFree(aap_sid);
  83. if (bu_sid)
  84. LocalFree(bu_sid);
  85. return success;
  86. }
  87. static inline bool file_exists(const wchar_t *path)
  88. {
  89. WIN32_FIND_DATAW wfd;
  90. HANDLE h = FindFirstFileW(path, &wfd);
  91. if (h == INVALID_HANDLE_VALUE)
  92. return false;
  93. FindClose(h);
  94. return true;
  95. }
  96. static LSTATUS get_reg(HKEY hkey, LPCWSTR sub_key, LPCWSTR value_name, bool b64)
  97. {
  98. HKEY key;
  99. LSTATUS status;
  100. DWORD flags = b64 ? KEY_WOW64_64KEY : KEY_WOW64_32KEY;
  101. DWORD size = sizeof(DWORD);
  102. DWORD val;
  103. status = RegOpenKeyEx(hkey, sub_key, 0, KEY_READ | flags, &key);
  104. if (status == ERROR_SUCCESS) {
  105. status = RegQueryValueExW(key, value_name, NULL, NULL, (LPBYTE)&val, &size);
  106. RegCloseKey(key);
  107. }
  108. return status;
  109. }
  110. #define get_programdata_path(path, subpath) \
  111. do { \
  112. SHGetFolderPathW(NULL, CSIDL_COMMON_APPDATA, NULL, SHGFP_TYPE_CURRENT, path); \
  113. StringCbCatW(path, sizeof(path), L"\\"); \
  114. StringCbCatW(path, sizeof(path), subpath); \
  115. } while (false)
  116. #define make_filename(str, name, ext) \
  117. do { \
  118. StringCbCatW(str, sizeof(str), name); \
  119. StringCbCatW(str, sizeof(str), b64 ? L"64" : L"32"); \
  120. StringCbCatW(str, sizeof(str), ext); \
  121. } while (false)
  122. /* ------------------------------------------------------------------------- */
  123. /* function to get the path to the hook */
  124. static bool programdata64_hook_exists = false;
  125. static bool programdata32_hook_exists = false;
  126. char *get_hook_path(bool b64)
  127. {
  128. wchar_t path[MAX_PATH];
  129. get_programdata_path(path, L"obs-studio-hook\\");
  130. make_filename(path, L"graphics-hook", L".dll");
  131. if ((b64 && programdata64_hook_exists) || (!b64 && programdata32_hook_exists)) {
  132. char *path_utf8 = NULL;
  133. os_wcs_to_utf8_ptr(path, 0, &path_utf8);
  134. return path_utf8;
  135. }
  136. return obs_module_file(b64 ? "graphics-hook64.dll" : "graphics-hook32.dll");
  137. }
  138. /* ------------------------------------------------------------------------- */
  139. /* initialization */
  140. #define IMPLICIT_LAYERS L"SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers"
  141. static bool update_hook_file(bool b64)
  142. {
  143. wchar_t temp[MAX_PATH];
  144. wchar_t src[MAX_PATH];
  145. wchar_t dst[MAX_PATH];
  146. wchar_t src_json[MAX_PATH];
  147. wchar_t dst_json[MAX_PATH];
  148. StringCbCopyW(temp, sizeof(temp),
  149. L"..\\..\\data\\obs-plugins\\"
  150. L"win-capture\\");
  151. make_filename(temp, L"obs-vulkan", L".json");
  152. if (_wfullpath(src_json, temp, MAX_PATH) == NULL)
  153. return false;
  154. StringCbCopyW(temp, sizeof(temp),
  155. L"..\\..\\data\\obs-plugins\\"
  156. L"win-capture\\");
  157. make_filename(temp, L"graphics-hook", L".dll");
  158. if (_wfullpath(src, temp, MAX_PATH) == NULL)
  159. return false;
  160. get_programdata_path(temp, L"obs-studio-hook\\");
  161. StringCbCopyW(dst_json, sizeof(dst_json), temp);
  162. StringCbCopyW(dst, sizeof(dst), temp);
  163. make_filename(dst_json, L"obs-vulkan", L".json");
  164. make_filename(dst, L"graphics-hook", L".dll");
  165. if (!file_exists(src)) {
  166. return false;
  167. }
  168. if (!file_exists(src_json)) {
  169. return false;
  170. }
  171. if (!file_exists(dst) || !file_exists(dst_json)) {
  172. CreateDirectoryW(temp, NULL);
  173. if (has_elevation())
  174. add_aap_perms(temp);
  175. if (!CopyFileW(src_json, dst_json, false))
  176. return false;
  177. if (!CopyFileW(src, dst, false))
  178. return false;
  179. return true;
  180. }
  181. if (has_elevation())
  182. add_aap_perms(temp);
  183. struct win_version_info ver_src = {0};
  184. struct win_version_info ver_dst = {0};
  185. if (!get_dll_ver(src, &ver_src))
  186. return false;
  187. #ifndef _DEBUG
  188. if (!get_dll_ver(dst, &ver_dst))
  189. return false;
  190. #endif
  191. /* if source is greater than dst, overwrite new file */
  192. while (win_version_compare(&ver_dst, &ver_src) < 0) {
  193. if (!CopyFileW(src_json, dst_json, false))
  194. return false;
  195. if (!CopyFileW(src, dst, false))
  196. return false;
  197. if (!get_dll_ver(dst, &ver_dst))
  198. return false;
  199. }
  200. /* do not use if major version incremented in target compared to
  201. * ours */
  202. if (ver_dst.major > ver_src.major) {
  203. return false;
  204. }
  205. return true;
  206. }
  207. #define warn(format, ...) blog(LOG_WARNING, "%s: " format, "[Vulkan Capture Init]", ##__VA_ARGS__)
  208. /* Sets vulkan layer registry if it doesn't already exist */
  209. static void init_vulkan_registry(bool b64)
  210. {
  211. DWORD flags = b64 ? KEY_WOW64_64KEY : KEY_WOW64_32KEY;
  212. HKEY key = NULL;
  213. LSTATUS s;
  214. wchar_t path[MAX_PATH];
  215. get_programdata_path(path, L"obs-studio-hook\\");
  216. make_filename(path, L"obs-vulkan", L".json");
  217. s = get_reg(HKEY_LOCAL_MACHINE, IMPLICIT_LAYERS, path, b64);
  218. if (s == ERROR_FILE_NOT_FOUND) {
  219. s = get_reg(HKEY_CURRENT_USER, IMPLICIT_LAYERS, path, b64);
  220. if (s != ERROR_FILE_NOT_FOUND && s != ERROR_SUCCESS) {
  221. warn("Failed to query registry keys: %d", (int)s);
  222. goto finish;
  223. }
  224. if (s == ERROR_SUCCESS && has_elevation()) {
  225. s = RegOpenKeyEx(HKEY_CURRENT_USER, IMPLICIT_LAYERS, 0, KEY_WRITE | flags, &key);
  226. if (s == ERROR_SUCCESS) {
  227. RegDeleteValueW(key, path);
  228. RegCloseKey(key);
  229. s = -1;
  230. key = NULL;
  231. }
  232. }
  233. } else if (s != ERROR_SUCCESS) {
  234. warn("Failed to query registry keys: %d", (int)s);
  235. goto finish;
  236. }
  237. if (s == ERROR_SUCCESS) {
  238. goto finish;
  239. }
  240. HKEY type = has_elevation() ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
  241. DWORD temp;
  242. s = RegCreateKeyExW(type, IMPLICIT_LAYERS, 0, NULL, 0, KEY_WRITE | flags, NULL, &key, &temp);
  243. if (s != ERROR_SUCCESS) {
  244. warn("Failed to create registry key");
  245. goto finish;
  246. }
  247. DWORD zero = 0;
  248. s = RegSetValueExW(key, path, 0, REG_DWORD, (const BYTE *)&zero, sizeof(zero));
  249. if (s != ERROR_SUCCESS) {
  250. warn("Failed to set registry value");
  251. }
  252. finish:
  253. if (key)
  254. RegCloseKey(key);
  255. }
  256. void init_hook_files()
  257. {
  258. if (update_hook_file(true)) {
  259. programdata64_hook_exists = true;
  260. init_vulkan_registry(true);
  261. }
  262. if (update_hook_file(false)) {
  263. programdata32_hook_exists = true;
  264. init_vulkan_registry(false);
  265. }
  266. }