graphics-hook.c 21 KB

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