graphics-hook.c 20 KB

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