graphics-hook.c 20 KB

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