graphics-hook.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. #include <windows.h>
  2. #include <psapi.h>
  3. #include <inttypes.h>
  4. #include "graphics-hook.h"
  5. #include "../graphics-hook-ver.h"
  6. #include "../obfuscate.h"
  7. #define DEBUG_OUTPUT
  8. #ifdef DEBUG_OUTPUT
  9. #define DbgOut(x) OutputDebugStringA(x)
  10. #else
  11. #define DbgOut(x)
  12. #endif
  13. struct thread_data {
  14. CRITICAL_SECTION mutexes[NUM_BUFFERS];
  15. CRITICAL_SECTION data_mutex;
  16. void *volatile cur_data;
  17. uint8_t *shmem_textures[2];
  18. HANDLE copy_thread;
  19. HANDLE copy_event;
  20. HANDLE stop_event;
  21. volatile int cur_tex;
  22. unsigned int pitch;
  23. unsigned int cy;
  24. volatile bool locked_textures[NUM_BUFFERS];
  25. };
  26. ipc_pipe_client_t pipe = {0};
  27. HANDLE signal_restart = NULL;
  28. HANDLE signal_stop = NULL;
  29. HANDLE signal_ready = NULL;
  30. HANDLE signal_exit = NULL;
  31. static HANDLE signal_init = NULL;
  32. HANDLE tex_mutexes[2] = {NULL, NULL};
  33. static HANDLE filemap_hook_info = NULL;
  34. static HINSTANCE dll_inst = NULL;
  35. static volatile bool stop_loop = false;
  36. static HANDLE dup_hook_mutex = NULL;
  37. static HANDLE capture_thread = NULL;
  38. char system_path[MAX_PATH] = {0};
  39. char process_name[MAX_PATH] = {0};
  40. wchar_t keepalive_name[64] = {0};
  41. HWND dummy_window = NULL;
  42. static unsigned int shmem_id_counter = 0;
  43. static void *shmem_info = NULL;
  44. static HANDLE shmem_file_handle = 0;
  45. static struct thread_data thread_data = {0};
  46. volatile bool active = false;
  47. struct hook_info *global_hook_info = NULL;
  48. static inline void wait_for_dll_main_finish(HANDLE thread_handle)
  49. {
  50. if (thread_handle) {
  51. WaitForSingleObject(thread_handle, 100);
  52. CloseHandle(thread_handle);
  53. }
  54. }
  55. bool init_pipe(void)
  56. {
  57. char new_name[64];
  58. sprintf(new_name, "%s%lu", PIPE_NAME, GetCurrentProcessId());
  59. const bool success = ipc_pipe_client_open(&pipe, new_name);
  60. if (!success) {
  61. DbgOut("[OBS] Failed to open pipe\n");
  62. }
  63. return success;
  64. }
  65. static HANDLE init_event(const wchar_t *name, DWORD pid)
  66. {
  67. HANDLE handle = create_event_plus_id(name, pid);
  68. if (!handle)
  69. hlog("Failed to get event '%s': %lu", name, GetLastError());
  70. return handle;
  71. }
  72. static HANDLE init_mutex(const wchar_t *name, DWORD pid)
  73. {
  74. HANDLE handle = create_mutex_plus_id(name, pid);
  75. if (!handle)
  76. hlog("Failed to open mutex '%s': %lu", name, GetLastError());
  77. return handle;
  78. }
  79. static inline bool init_signals(void)
  80. {
  81. DWORD pid = GetCurrentProcessId();
  82. signal_restart = init_event(EVENT_CAPTURE_RESTART, pid);
  83. if (!signal_restart) {
  84. return false;
  85. }
  86. signal_stop = init_event(EVENT_CAPTURE_STOP, pid);
  87. if (!signal_stop) {
  88. return false;
  89. }
  90. signal_ready = init_event(EVENT_HOOK_READY, pid);
  91. if (!signal_ready) {
  92. return false;
  93. }
  94. signal_exit = init_event(EVENT_HOOK_EXIT, pid);
  95. if (!signal_exit) {
  96. return false;
  97. }
  98. signal_init = init_event(EVENT_HOOK_INIT, pid);
  99. if (!signal_init) {
  100. return false;
  101. }
  102. return true;
  103. }
  104. static inline bool init_mutexes(void)
  105. {
  106. DWORD pid = GetCurrentProcessId();
  107. tex_mutexes[0] = init_mutex(MUTEX_TEXTURE1, pid);
  108. if (!tex_mutexes[0]) {
  109. return false;
  110. }
  111. tex_mutexes[1] = init_mutex(MUTEX_TEXTURE2, pid);
  112. if (!tex_mutexes[1]) {
  113. return false;
  114. }
  115. return true;
  116. }
  117. static inline bool init_system_path(void)
  118. {
  119. UINT ret = GetSystemDirectoryA(system_path, MAX_PATH);
  120. if (!ret) {
  121. hlog("Failed to get windows system path: %lu", GetLastError());
  122. return false;
  123. }
  124. return true;
  125. }
  126. static inline void log_current_process(void)
  127. {
  128. DWORD len = GetModuleBaseNameA(GetCurrentProcess(), NULL, process_name,
  129. MAX_PATH);
  130. if (len > 0) {
  131. process_name[len] = 0;
  132. hlog("graphics-hook.dll loaded against process: %s",
  133. process_name);
  134. } else {
  135. hlog("graphics-hook.dll loaded");
  136. }
  137. hlog("(half life scientist) everything.. seems to be in order");
  138. }
  139. static inline bool init_hook_info(void)
  140. {
  141. filemap_hook_info = create_hook_info(GetCurrentProcessId());
  142. if (!filemap_hook_info) {
  143. hlog("Failed to create hook info file mapping: %lu",
  144. GetLastError());
  145. return false;
  146. }
  147. global_hook_info = MapViewOfFile(filemap_hook_info, FILE_MAP_ALL_ACCESS,
  148. 0, 0, sizeof(struct hook_info));
  149. if (!global_hook_info) {
  150. hlog("Failed to map the hook info file mapping: %lu",
  151. GetLastError());
  152. return false;
  153. }
  154. return true;
  155. }
  156. #define DEF_FLAGS (WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS)
  157. static DWORD WINAPI dummy_window_thread(LPVOID *unused)
  158. {
  159. static const wchar_t dummy_window_class[] = L"temp_d3d_window_4039785";
  160. WNDCLASSW wc;
  161. MSG msg;
  162. memset(&wc, 0, sizeof(wc));
  163. wc.style = CS_OWNDC;
  164. wc.hInstance = dll_inst;
  165. wc.lpfnWndProc = (WNDPROC)DefWindowProc;
  166. wc.lpszClassName = dummy_window_class;
  167. if (!RegisterClass(&wc)) {
  168. hlog("Failed to create temp D3D window class: %lu",
  169. GetLastError());
  170. return 0;
  171. }
  172. dummy_window = CreateWindowExW(0, dummy_window_class, L"Temp Window",
  173. DEF_FLAGS, 0, 0, 1, 1, NULL, NULL,
  174. dll_inst, NULL);
  175. if (!dummy_window) {
  176. hlog("Failed to create temp D3D window: %lu", GetLastError());
  177. return 0;
  178. }
  179. while (GetMessage(&msg, NULL, 0, 0)) {
  180. TranslateMessage(&msg);
  181. DispatchMessage(&msg);
  182. }
  183. (void)unused;
  184. return 0;
  185. }
  186. static inline void init_dummy_window_thread(void)
  187. {
  188. HANDLE thread =
  189. CreateThread(NULL, 0, dummy_window_thread, NULL, 0, NULL);
  190. if (!thread) {
  191. hlog("Failed to create temp D3D window thread: %lu",
  192. GetLastError());
  193. return;
  194. }
  195. CloseHandle(thread);
  196. }
  197. static inline bool init_hook(HANDLE thread_handle)
  198. {
  199. wait_for_dll_main_finish(thread_handle);
  200. _snwprintf(keepalive_name, sizeof(keepalive_name) / sizeof(wchar_t),
  201. L"%s%lu", WINDOW_HOOK_KEEPALIVE, GetCurrentProcessId());
  202. init_pipe();
  203. init_dummy_window_thread();
  204. log_current_process();
  205. SetEvent(signal_restart);
  206. return true;
  207. }
  208. static inline void close_handle(HANDLE *handle)
  209. {
  210. if (*handle) {
  211. CloseHandle(*handle);
  212. *handle = NULL;
  213. }
  214. }
  215. static void free_hook(void)
  216. {
  217. if (filemap_hook_info) {
  218. CloseHandle(filemap_hook_info);
  219. filemap_hook_info = NULL;
  220. }
  221. if (global_hook_info) {
  222. UnmapViewOfFile(global_hook_info);
  223. global_hook_info = NULL;
  224. }
  225. close_handle(&tex_mutexes[1]);
  226. close_handle(&tex_mutexes[0]);
  227. close_handle(&signal_exit);
  228. close_handle(&signal_ready);
  229. close_handle(&signal_stop);
  230. close_handle(&signal_restart);
  231. close_handle(&dup_hook_mutex);
  232. ipc_pipe_client_free(&pipe);
  233. }
  234. static inline bool d3d8_hookable(void)
  235. {
  236. return !!global_hook_info->offsets.d3d8.present;
  237. }
  238. static inline bool ddraw_hookable(void)
  239. {
  240. return !!global_hook_info->offsets.ddraw.surface_create &&
  241. !!global_hook_info->offsets.ddraw.surface_restore &&
  242. !!global_hook_info->offsets.ddraw.surface_release &&
  243. !!global_hook_info->offsets.ddraw.surface_unlock &&
  244. !!global_hook_info->offsets.ddraw.surface_blt &&
  245. !!global_hook_info->offsets.ddraw.surface_flip &&
  246. !!global_hook_info->offsets.ddraw.surface_set_palette &&
  247. !!global_hook_info->offsets.ddraw.palette_set_entries;
  248. }
  249. static inline bool d3d9_hookable(void)
  250. {
  251. return !!global_hook_info->offsets.d3d9.present &&
  252. !!global_hook_info->offsets.d3d9.present_ex &&
  253. !!global_hook_info->offsets.d3d9.present_swap;
  254. }
  255. static inline bool dxgi_hookable(void)
  256. {
  257. return !!global_hook_info->offsets.dxgi.present &&
  258. !!global_hook_info->offsets.dxgi.resize;
  259. }
  260. static inline bool attempt_hook(void)
  261. {
  262. //static bool ddraw_hooked = false;
  263. static bool d3d8_hooked = false;
  264. static bool d3d9_hooked = false;
  265. static bool d3d12_hooked = false;
  266. static bool dxgi_hooked = false;
  267. static bool gl_hooked = false;
  268. #if COMPILE_VULKAN_HOOK
  269. static bool vulkan_hooked = false;
  270. if (!vulkan_hooked) {
  271. vulkan_hooked = hook_vulkan();
  272. if (vulkan_hooked) {
  273. return true;
  274. }
  275. }
  276. #endif //COMPILE_VULKAN_HOOK
  277. #if COMPILE_D3D12_HOOK
  278. if (!d3d12_hooked) {
  279. d3d12_hooked = hook_d3d12();
  280. }
  281. #endif
  282. if (!d3d9_hooked) {
  283. if (!d3d9_hookable()) {
  284. DbgOut("[OBS] no D3D9 hook address found!\n");
  285. d3d9_hooked = true;
  286. } else {
  287. d3d9_hooked = hook_d3d9();
  288. if (d3d9_hooked) {
  289. return true;
  290. }
  291. }
  292. }
  293. if (!dxgi_hooked) {
  294. if (!dxgi_hookable()) {
  295. DbgOut("[OBS] no DXGI hook address found!\n");
  296. dxgi_hooked = true;
  297. } else {
  298. dxgi_hooked = hook_dxgi();
  299. if (dxgi_hooked) {
  300. return true;
  301. }
  302. }
  303. }
  304. if (!gl_hooked) {
  305. gl_hooked = hook_gl();
  306. if (gl_hooked) {
  307. return true;
  308. }
  309. /*} else {
  310. rehook_gl();*/
  311. }
  312. if (!d3d8_hooked) {
  313. if (!d3d8_hookable()) {
  314. d3d8_hooked = true;
  315. } else {
  316. d3d8_hooked = hook_d3d8();
  317. if (d3d8_hooked) {
  318. return true;
  319. }
  320. }
  321. }
  322. /*if (!ddraw_hooked) {
  323. if (!ddraw_hookable()) {
  324. ddraw_hooked = true;
  325. } else {
  326. ddraw_hooked = hook_ddraw();
  327. if (ddraw_hooked) {
  328. return true;
  329. }
  330. }
  331. }*/
  332. #if HOOK_VERBOSE_LOGGING
  333. DbgOut("[OBS] Attempt hook: D3D8=");
  334. DbgOut(d3d8_hooked ? "1" : "0");
  335. DbgOut(", D3D9=");
  336. DbgOut(d3d9_hooked ? "1" : "0");
  337. DbgOut(", D3D12=");
  338. DbgOut(d3d12_hooked ? "1" : "0");
  339. DbgOut(", DXGI=");
  340. DbgOut(dxgi_hooked ? "1" : "0");
  341. DbgOut(", GL=");
  342. DbgOut(gl_hooked ? "1" : "0");
  343. #if COMPILE_VULKAN_HOOK
  344. DbgOut(", VK=");
  345. DbgOut(vulkan_hooked ? "1" : "0");
  346. #endif
  347. DbgOut("\n");
  348. #endif
  349. return false;
  350. }
  351. static inline void capture_loop(void)
  352. {
  353. WaitForSingleObject(signal_init, INFINITE);
  354. while (!attempt_hook())
  355. Sleep(40);
  356. for (size_t n = 0; !stop_loop; n++) {
  357. /* this causes it to check every 4 seconds, but still with
  358. * a small sleep interval in case the thread needs to stop */
  359. if (n % 100 == 0)
  360. attempt_hook();
  361. Sleep(40);
  362. }
  363. }
  364. static DWORD WINAPI main_capture_thread(HANDLE thread_handle)
  365. {
  366. if (!init_hook(thread_handle)) {
  367. DbgOut("[OBS] Failed to init hook\n");
  368. free_hook();
  369. return 0;
  370. }
  371. capture_loop();
  372. return 0;
  373. }
  374. static inline void hlogv(const char *format, va_list args)
  375. {
  376. char message[1024] = "";
  377. int num = _vsprintf_p(message, 1024, format, args);
  378. if (num > 0) {
  379. if (!ipc_pipe_client_write(&pipe, message, (size_t)num + 1)) {
  380. ipc_pipe_client_free(&pipe);
  381. }
  382. DbgOut("[OBS] ");
  383. DbgOut(message);
  384. DbgOut("\n");
  385. }
  386. }
  387. void hlog(const char *format, ...)
  388. {
  389. va_list args;
  390. va_start(args, format);
  391. hlogv(format, args);
  392. va_end(args);
  393. }
  394. void hlog_hr(const char *text, HRESULT hr)
  395. {
  396. LPSTR buffer = NULL;
  397. FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
  398. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  399. FORMAT_MESSAGE_IGNORE_INSERTS,
  400. NULL, hr, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  401. (LPSTR)&buffer, 0, NULL);
  402. if (buffer) {
  403. hlog("%s (0x%08lX): %s", text, hr, buffer);
  404. LocalFree(buffer);
  405. } else {
  406. hlog("%s (0x%08lX)", text, hr);
  407. }
  408. }
  409. static inline uint64_t get_clockfreq(void)
  410. {
  411. static bool have_clockfreq = false;
  412. static LARGE_INTEGER clock_freq;
  413. if (!have_clockfreq) {
  414. QueryPerformanceFrequency(&clock_freq);
  415. have_clockfreq = true;
  416. }
  417. return clock_freq.QuadPart;
  418. }
  419. uint64_t os_gettime_ns(void)
  420. {
  421. LARGE_INTEGER current_time;
  422. double time_val;
  423. QueryPerformanceCounter(&current_time);
  424. time_val = (double)current_time.QuadPart;
  425. time_val *= 1000000000.0;
  426. time_val /= (double)get_clockfreq();
  427. return (uint64_t)time_val;
  428. }
  429. static inline int try_lock_shmem_tex(int id)
  430. {
  431. int next = id == 0 ? 1 : 0;
  432. DWORD wait_result = WAIT_FAILED;
  433. wait_result = WaitForSingleObject(tex_mutexes[id], 0);
  434. if (wait_result == WAIT_OBJECT_0 || wait_result == WAIT_ABANDONED) {
  435. return id;
  436. }
  437. wait_result = WaitForSingleObject(tex_mutexes[next], 0);
  438. if (wait_result == WAIT_OBJECT_0 || wait_result == WAIT_ABANDONED) {
  439. return next;
  440. }
  441. return -1;
  442. }
  443. static inline void unlock_shmem_tex(int id)
  444. {
  445. if (id != -1) {
  446. ReleaseMutex(tex_mutexes[id]);
  447. }
  448. }
  449. static inline bool init_shared_info(size_t size, HWND window)
  450. {
  451. wchar_t name[64];
  452. HWND top = GetAncestor(window, GA_ROOT);
  453. swprintf(name, 64, SHMEM_TEXTURE "_%" PRIu64 "_%u",
  454. (uint64_t)(uintptr_t)top, ++shmem_id_counter);
  455. shmem_file_handle = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
  456. PAGE_READWRITE, 0, (DWORD)size,
  457. name);
  458. if (!shmem_file_handle) {
  459. hlog("init_shared_info: Failed to create shared memory: %d",
  460. GetLastError());
  461. return false;
  462. }
  463. shmem_info = MapViewOfFile(shmem_file_handle, FILE_MAP_ALL_ACCESS, 0, 0,
  464. size);
  465. if (!shmem_info) {
  466. hlog("init_shared_info: Failed to map shared memory: %d",
  467. GetLastError());
  468. return false;
  469. }
  470. return true;
  471. }
  472. bool capture_init_shtex(struct shtex_data **data, HWND window, uint32_t cx,
  473. uint32_t cy, uint32_t format, bool flip,
  474. uintptr_t handle)
  475. {
  476. if (!init_shared_info(sizeof(struct shtex_data), window)) {
  477. hlog("capture_init_shtex: Failed to initialize memory");
  478. return false;
  479. }
  480. *data = shmem_info;
  481. (*data)->tex_handle = (uint32_t)handle;
  482. global_hook_info->hook_ver_major = HOOK_VER_MAJOR;
  483. global_hook_info->hook_ver_minor = HOOK_VER_MINOR;
  484. global_hook_info->window = (uint32_t)(uintptr_t)window;
  485. global_hook_info->type = CAPTURE_TYPE_TEXTURE;
  486. global_hook_info->format = format;
  487. global_hook_info->flip = flip;
  488. global_hook_info->map_id = shmem_id_counter;
  489. global_hook_info->map_size = sizeof(struct shtex_data);
  490. global_hook_info->cx = cx;
  491. global_hook_info->cy = cy;
  492. global_hook_info->UNUSED_base_cx = cx;
  493. global_hook_info->UNUSED_base_cy = cy;
  494. if (!SetEvent(signal_ready)) {
  495. hlog("capture_init_shtex: Failed to signal ready: %d",
  496. GetLastError());
  497. return false;
  498. }
  499. active = true;
  500. return true;
  501. }
  502. static DWORD CALLBACK copy_thread(LPVOID unused)
  503. {
  504. uint32_t pitch = thread_data.pitch;
  505. uint32_t cy = thread_data.cy;
  506. HANDLE events[2] = {NULL, NULL};
  507. int shmem_id = 0;
  508. if (!duplicate_handle(&events[0], thread_data.copy_event)) {
  509. hlog_hr("copy_thread: Failed to duplicate copy event: %d",
  510. GetLastError());
  511. return 0;
  512. }
  513. if (!duplicate_handle(&events[1], thread_data.stop_event)) {
  514. hlog_hr("copy_thread: Failed to duplicate stop event: %d",
  515. GetLastError());
  516. goto finish;
  517. }
  518. for (;;) {
  519. int copy_tex;
  520. void *cur_data;
  521. DWORD ret = WaitForMultipleObjects(2, events, false, INFINITE);
  522. if (ret != WAIT_OBJECT_0) {
  523. break;
  524. }
  525. EnterCriticalSection(&thread_data.data_mutex);
  526. copy_tex = thread_data.cur_tex;
  527. cur_data = thread_data.cur_data;
  528. LeaveCriticalSection(&thread_data.data_mutex);
  529. if (copy_tex < NUM_BUFFERS && !!cur_data) {
  530. EnterCriticalSection(&thread_data.mutexes[copy_tex]);
  531. int lock_id = try_lock_shmem_tex(shmem_id);
  532. if (lock_id != -1) {
  533. memcpy(thread_data.shmem_textures[lock_id],
  534. cur_data, (size_t)pitch * (size_t)cy);
  535. unlock_shmem_tex(lock_id);
  536. ((struct shmem_data *)shmem_info)->last_tex =
  537. lock_id;
  538. shmem_id = lock_id == 0 ? 1 : 0;
  539. }
  540. LeaveCriticalSection(&thread_data.mutexes[copy_tex]);
  541. }
  542. }
  543. finish:
  544. for (size_t i = 0; i < 2; i++) {
  545. if (events[i]) {
  546. CloseHandle(events[i]);
  547. }
  548. }
  549. (void)unused;
  550. return 0;
  551. }
  552. void shmem_copy_data(size_t idx, void *volatile data)
  553. {
  554. EnterCriticalSection(&thread_data.data_mutex);
  555. thread_data.cur_tex = (int)idx;
  556. thread_data.cur_data = data;
  557. thread_data.locked_textures[idx] = true;
  558. LeaveCriticalSection(&thread_data.data_mutex);
  559. SetEvent(thread_data.copy_event);
  560. }
  561. bool shmem_texture_data_lock(int idx)
  562. {
  563. bool locked;
  564. EnterCriticalSection(&thread_data.data_mutex);
  565. locked = thread_data.locked_textures[idx];
  566. LeaveCriticalSection(&thread_data.data_mutex);
  567. if (locked) {
  568. EnterCriticalSection(&thread_data.mutexes[idx]);
  569. return true;
  570. }
  571. return false;
  572. }
  573. void shmem_texture_data_unlock(int idx)
  574. {
  575. EnterCriticalSection(&thread_data.data_mutex);
  576. thread_data.locked_textures[idx] = false;
  577. LeaveCriticalSection(&thread_data.data_mutex);
  578. LeaveCriticalSection(&thread_data.mutexes[idx]);
  579. }
  580. static inline bool init_shmem_thread(uint32_t pitch, uint32_t cy)
  581. {
  582. struct shmem_data *data = shmem_info;
  583. thread_data.pitch = pitch;
  584. thread_data.cy = cy;
  585. thread_data.shmem_textures[0] = (uint8_t *)data + data->tex1_offset;
  586. thread_data.shmem_textures[1] = (uint8_t *)data + data->tex2_offset;
  587. thread_data.copy_event = CreateEvent(NULL, false, false, NULL);
  588. if (!thread_data.copy_event) {
  589. hlog("init_shmem_thread: Failed to create copy event: %d",
  590. GetLastError());
  591. return false;
  592. }
  593. thread_data.stop_event = CreateEvent(NULL, true, false, NULL);
  594. if (!thread_data.stop_event) {
  595. hlog("init_shmem_thread: Failed to create stop event: %d",
  596. GetLastError());
  597. return false;
  598. }
  599. for (size_t i = 0; i < NUM_BUFFERS; i++) {
  600. InitializeCriticalSection(&thread_data.mutexes[i]);
  601. }
  602. InitializeCriticalSection(&thread_data.data_mutex);
  603. thread_data.copy_thread =
  604. CreateThread(NULL, 0, copy_thread, NULL, 0, NULL);
  605. if (!thread_data.copy_thread) {
  606. hlog("init_shmem_thread: Failed to create thread: %d",
  607. GetLastError());
  608. return false;
  609. }
  610. return true;
  611. }
  612. #ifndef ALIGN
  613. #define ALIGN(bytes, align) (((bytes) + ((align)-1)) & ~((align)-1))
  614. #endif
  615. bool capture_init_shmem(struct shmem_data **data, HWND window, uint32_t cx,
  616. uint32_t cy, uint32_t pitch, uint32_t format, bool flip)
  617. {
  618. uint32_t tex_size = cy * pitch;
  619. uint32_t aligned_header = ALIGN(sizeof(struct shmem_data), 32);
  620. uint32_t aligned_tex = ALIGN(tex_size, 32);
  621. uint32_t total_size = aligned_header + aligned_tex * 2 + 32;
  622. uintptr_t align_pos;
  623. if (!init_shared_info(total_size, window)) {
  624. hlog("capture_init_shmem: Failed to initialize memory");
  625. return false;
  626. }
  627. *data = shmem_info;
  628. /* to ensure fast copy rate, align texture data to 256bit addresses */
  629. align_pos = (uintptr_t)shmem_info;
  630. align_pos += aligned_header;
  631. align_pos &= ~(32 - 1);
  632. align_pos -= (uintptr_t)shmem_info;
  633. if (align_pos < sizeof(struct shmem_data))
  634. align_pos += 32;
  635. (*data)->last_tex = -1;
  636. (*data)->tex1_offset = (uint32_t)align_pos;
  637. (*data)->tex2_offset = (*data)->tex1_offset + aligned_tex;
  638. global_hook_info->hook_ver_major = HOOK_VER_MAJOR;
  639. global_hook_info->hook_ver_minor = HOOK_VER_MINOR;
  640. global_hook_info->window = (uint32_t)(uintptr_t)window;
  641. global_hook_info->type = CAPTURE_TYPE_MEMORY;
  642. global_hook_info->format = format;
  643. global_hook_info->flip = flip;
  644. global_hook_info->map_id = shmem_id_counter;
  645. global_hook_info->map_size = total_size;
  646. global_hook_info->pitch = pitch;
  647. global_hook_info->cx = cx;
  648. global_hook_info->cy = cy;
  649. global_hook_info->UNUSED_base_cx = cx;
  650. global_hook_info->UNUSED_base_cy = cy;
  651. if (!init_shmem_thread(pitch, cy)) {
  652. return false;
  653. }
  654. if (!SetEvent(signal_ready)) {
  655. hlog("capture_init_shmem: Failed to signal ready: %d",
  656. GetLastError());
  657. return false;
  658. }
  659. active = true;
  660. return true;
  661. }
  662. static inline void thread_data_free(void)
  663. {
  664. if (thread_data.copy_thread) {
  665. DWORD ret;
  666. SetEvent(thread_data.stop_event);
  667. ret = WaitForSingleObject(thread_data.copy_thread, 500);
  668. if (ret != WAIT_OBJECT_0)
  669. TerminateThread(thread_data.copy_thread, (DWORD)-1);
  670. CloseHandle(thread_data.copy_thread);
  671. }
  672. if (thread_data.stop_event)
  673. CloseHandle(thread_data.stop_event);
  674. if (thread_data.copy_event)
  675. CloseHandle(thread_data.copy_event);
  676. for (size_t i = 0; i < NUM_BUFFERS; i++)
  677. DeleteCriticalSection(&thread_data.mutexes[i]);
  678. DeleteCriticalSection(&thread_data.data_mutex);
  679. memset(&thread_data, 0, sizeof(thread_data));
  680. }
  681. void capture_free(void)
  682. {
  683. thread_data_free();
  684. if (shmem_info) {
  685. UnmapViewOfFile(shmem_info);
  686. shmem_info = NULL;
  687. }
  688. close_handle(&shmem_file_handle);
  689. SetEvent(signal_restart);
  690. active = false;
  691. }
  692. #define HOOK_NAME L"graphics_hook_dup_mutex"
  693. static inline HANDLE open_mutex_plus_id(const wchar_t *name, DWORD id)
  694. {
  695. wchar_t new_name[64];
  696. _snwprintf(new_name, 64, L"%s%lu", name, id);
  697. return open_mutex(new_name);
  698. }
  699. static bool init_dll(void)
  700. {
  701. DWORD pid = GetCurrentProcessId();
  702. HANDLE h;
  703. h = open_mutex_plus_id(HOOK_NAME, pid);
  704. if (h) {
  705. CloseHandle(h);
  706. return false;
  707. }
  708. dup_hook_mutex = create_mutex_plus_id(HOOK_NAME, pid);
  709. return !!dup_hook_mutex;
  710. }
  711. BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID unused1)
  712. {
  713. if (reason == DLL_PROCESS_ATTACH) {
  714. wchar_t name[MAX_PATH];
  715. dll_inst = hinst;
  716. if (!init_dll()) {
  717. DbgOut("[OBS] Duplicate hook library");
  718. return false;
  719. }
  720. HANDLE cur_thread;
  721. bool success = DuplicateHandle(GetCurrentProcess(),
  722. GetCurrentThread(),
  723. GetCurrentProcess(), &cur_thread,
  724. SYNCHRONIZE, false, 0);
  725. if (!success)
  726. DbgOut("[OBS] Failed to get current thread handle");
  727. if (!init_signals()) {
  728. return false;
  729. }
  730. if (!init_system_path()) {
  731. return false;
  732. }
  733. if (!init_hook_info()) {
  734. return false;
  735. }
  736. if (!init_mutexes()) {
  737. return false;
  738. }
  739. /* this prevents the library from being automatically unloaded
  740. * by the next FreeLibrary call */
  741. GetModuleFileNameW(hinst, name, MAX_PATH);
  742. LoadLibraryW(name);
  743. capture_thread = CreateThread(
  744. NULL, 0, (LPTHREAD_START_ROUTINE)main_capture_thread,
  745. (LPVOID)cur_thread, 0, 0);
  746. if (!capture_thread) {
  747. CloseHandle(cur_thread);
  748. return false;
  749. }
  750. } else if (reason == DLL_PROCESS_DETACH) {
  751. if (!dup_hook_mutex) {
  752. return true;
  753. }
  754. if (capture_thread) {
  755. stop_loop = true;
  756. WaitForSingleObject(capture_thread, 300);
  757. CloseHandle(capture_thread);
  758. }
  759. free_hook();
  760. }
  761. (void)unused1;
  762. return true;
  763. }
  764. __declspec(dllexport) LRESULT CALLBACK
  765. dummy_debug_proc(int code, WPARAM wparam, LPARAM lparam)
  766. {
  767. static bool hooking = true;
  768. MSG *msg = (MSG *)lparam;
  769. if (hooking && msg->message == (WM_USER + 432)) {
  770. HMODULE user32 = GetModuleHandleW(L"USER32");
  771. BOOL(WINAPI * unhook_windows_hook_ex)(HHOOK) = NULL;
  772. unhook_windows_hook_ex = get_obfuscated_func(
  773. user32, "VojeleY`bdgxvM`hhDz", 0x7F55F80C9EE3A213ULL);
  774. if (unhook_windows_hook_ex)
  775. unhook_windows_hook_ex((HHOOK)msg->lParam);
  776. hooking = false;
  777. }
  778. return CallNextHookEx(0, code, wparam, lparam);
  779. }