graphics-hook.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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. HWND top = GetAncestor(window, GA_ROOT);
  427. swprintf(name, 64, SHMEM_TEXTURE "_%" PRIu64 "_%u",
  428. (uint64_t)(uintptr_t)top, ++shmem_id_counter);
  429. shmem_file_handle = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
  430. PAGE_READWRITE, 0, (DWORD)size,
  431. name);
  432. if (!shmem_file_handle) {
  433. hlog("init_shared_info: Failed to create shared memory: %d",
  434. GetLastError());
  435. return false;
  436. }
  437. shmem_info = MapViewOfFile(shmem_file_handle, FILE_MAP_ALL_ACCESS, 0, 0,
  438. size);
  439. if (!shmem_info) {
  440. hlog("init_shared_info: Failed to map shared memory: %d",
  441. GetLastError());
  442. return false;
  443. }
  444. return true;
  445. }
  446. bool capture_init_shtex(struct shtex_data **data, HWND window, uint32_t base_cx,
  447. uint32_t base_cy, uint32_t cx, uint32_t cy,
  448. uint32_t format, bool flip, uintptr_t handle)
  449. {
  450. if (!init_shared_info(sizeof(struct shtex_data), window)) {
  451. hlog("capture_init_shtex: Failed to initialize memory");
  452. return false;
  453. }
  454. *data = shmem_info;
  455. (*data)->tex_handle = (uint32_t)handle;
  456. global_hook_info->hook_ver_major = HOOK_VER_MAJOR;
  457. global_hook_info->hook_ver_minor = HOOK_VER_MINOR;
  458. global_hook_info->window = (uint32_t)(uintptr_t)window;
  459. global_hook_info->type = CAPTURE_TYPE_TEXTURE;
  460. global_hook_info->format = format;
  461. global_hook_info->flip = flip;
  462. global_hook_info->map_id = shmem_id_counter;
  463. global_hook_info->map_size = sizeof(struct shtex_data);
  464. global_hook_info->cx = cx;
  465. global_hook_info->cy = cy;
  466. global_hook_info->base_cx = base_cx;
  467. global_hook_info->base_cy = base_cy;
  468. if (!SetEvent(signal_ready)) {
  469. hlog("capture_init_shtex: Failed to signal ready: %d",
  470. GetLastError());
  471. return false;
  472. }
  473. active = true;
  474. return true;
  475. }
  476. static DWORD CALLBACK copy_thread(LPVOID unused)
  477. {
  478. uint32_t pitch = thread_data.pitch;
  479. uint32_t cy = thread_data.cy;
  480. HANDLE events[2] = {NULL, NULL};
  481. int shmem_id = 0;
  482. if (!duplicate_handle(&events[0], thread_data.copy_event)) {
  483. hlog_hr("copy_thread: Failed to duplicate copy event: %d",
  484. GetLastError());
  485. return 0;
  486. }
  487. if (!duplicate_handle(&events[1], thread_data.stop_event)) {
  488. hlog_hr("copy_thread: Failed to duplicate stop event: %d",
  489. GetLastError());
  490. goto finish;
  491. }
  492. for (;;) {
  493. int copy_tex;
  494. void *cur_data;
  495. DWORD ret = WaitForMultipleObjects(2, events, false, INFINITE);
  496. if (ret != WAIT_OBJECT_0) {
  497. break;
  498. }
  499. EnterCriticalSection(&thread_data.data_mutex);
  500. copy_tex = thread_data.cur_tex;
  501. cur_data = thread_data.cur_data;
  502. LeaveCriticalSection(&thread_data.data_mutex);
  503. if (copy_tex < NUM_BUFFERS && !!cur_data) {
  504. EnterCriticalSection(&thread_data.mutexes[copy_tex]);
  505. int lock_id = try_lock_shmem_tex(shmem_id);
  506. if (lock_id != -1) {
  507. memcpy(thread_data.shmem_textures[lock_id],
  508. cur_data, (size_t)pitch * (size_t)cy);
  509. unlock_shmem_tex(lock_id);
  510. ((struct shmem_data *)shmem_info)->last_tex =
  511. lock_id;
  512. shmem_id = lock_id == 0 ? 1 : 0;
  513. }
  514. LeaveCriticalSection(&thread_data.mutexes[copy_tex]);
  515. }
  516. }
  517. finish:
  518. for (size_t i = 0; i < 2; i++) {
  519. if (events[i]) {
  520. CloseHandle(events[i]);
  521. }
  522. }
  523. (void)unused;
  524. return 0;
  525. }
  526. void shmem_copy_data(size_t idx, void *volatile data)
  527. {
  528. EnterCriticalSection(&thread_data.data_mutex);
  529. thread_data.cur_tex = (int)idx;
  530. thread_data.cur_data = data;
  531. thread_data.locked_textures[idx] = true;
  532. LeaveCriticalSection(&thread_data.data_mutex);
  533. SetEvent(thread_data.copy_event);
  534. }
  535. bool shmem_texture_data_lock(int idx)
  536. {
  537. bool locked;
  538. EnterCriticalSection(&thread_data.data_mutex);
  539. locked = thread_data.locked_textures[idx];
  540. LeaveCriticalSection(&thread_data.data_mutex);
  541. if (locked) {
  542. EnterCriticalSection(&thread_data.mutexes[idx]);
  543. return true;
  544. }
  545. return false;
  546. }
  547. void shmem_texture_data_unlock(int idx)
  548. {
  549. EnterCriticalSection(&thread_data.data_mutex);
  550. thread_data.locked_textures[idx] = false;
  551. LeaveCriticalSection(&thread_data.data_mutex);
  552. LeaveCriticalSection(&thread_data.mutexes[idx]);
  553. }
  554. static inline bool init_shmem_thread(uint32_t pitch, uint32_t cy)
  555. {
  556. struct shmem_data *data = shmem_info;
  557. thread_data.pitch = pitch;
  558. thread_data.cy = cy;
  559. thread_data.shmem_textures[0] = (uint8_t *)data + data->tex1_offset;
  560. thread_data.shmem_textures[1] = (uint8_t *)data + data->tex2_offset;
  561. thread_data.copy_event = CreateEvent(NULL, false, false, NULL);
  562. if (!thread_data.copy_event) {
  563. hlog("init_shmem_thread: Failed to create copy event: %d",
  564. GetLastError());
  565. return false;
  566. }
  567. thread_data.stop_event = CreateEvent(NULL, true, false, NULL);
  568. if (!thread_data.stop_event) {
  569. hlog("init_shmem_thread: Failed to create stop event: %d",
  570. GetLastError());
  571. return false;
  572. }
  573. for (size_t i = 0; i < NUM_BUFFERS; i++) {
  574. InitializeCriticalSection(&thread_data.mutexes[i]);
  575. }
  576. InitializeCriticalSection(&thread_data.data_mutex);
  577. thread_data.copy_thread =
  578. CreateThread(NULL, 0, copy_thread, NULL, 0, NULL);
  579. if (!thread_data.copy_thread) {
  580. hlog("init_shmem_thread: Failed to create thread: %d",
  581. GetLastError());
  582. return false;
  583. }
  584. return true;
  585. }
  586. #ifndef ALIGN
  587. #define ALIGN(bytes, align) (((bytes) + ((align)-1)) & ~((align)-1))
  588. #endif
  589. bool capture_init_shmem(struct shmem_data **data, HWND window, uint32_t base_cx,
  590. uint32_t base_cy, uint32_t cx, uint32_t cy,
  591. uint32_t pitch, uint32_t format, bool flip)
  592. {
  593. uint32_t tex_size = cy * pitch;
  594. uint32_t aligned_header = ALIGN(sizeof(struct shmem_data), 32);
  595. uint32_t aligned_tex = ALIGN(tex_size, 32);
  596. uint32_t total_size = aligned_header + aligned_tex * 2 + 32;
  597. uintptr_t align_pos;
  598. if (!init_shared_info(total_size, window)) {
  599. hlog("capture_init_shmem: Failed to initialize memory");
  600. return false;
  601. }
  602. *data = shmem_info;
  603. /* to ensure fast copy rate, align texture data to 256bit addresses */
  604. align_pos = (uintptr_t)shmem_info;
  605. align_pos += aligned_header;
  606. align_pos &= ~(32 - 1);
  607. align_pos -= (uintptr_t)shmem_info;
  608. if (align_pos < sizeof(struct shmem_data))
  609. align_pos += 32;
  610. (*data)->last_tex = -1;
  611. (*data)->tex1_offset = (uint32_t)align_pos;
  612. (*data)->tex2_offset = (*data)->tex1_offset + aligned_tex;
  613. global_hook_info->hook_ver_major = HOOK_VER_MAJOR;
  614. global_hook_info->hook_ver_minor = HOOK_VER_MINOR;
  615. global_hook_info->window = (uint32_t)(uintptr_t)window;
  616. global_hook_info->type = CAPTURE_TYPE_MEMORY;
  617. global_hook_info->format = format;
  618. global_hook_info->flip = flip;
  619. global_hook_info->map_id = shmem_id_counter;
  620. global_hook_info->map_size = total_size;
  621. global_hook_info->pitch = pitch;
  622. global_hook_info->cx = cx;
  623. global_hook_info->cy = cy;
  624. global_hook_info->base_cx = base_cx;
  625. global_hook_info->base_cy = base_cy;
  626. if (!init_shmem_thread(pitch, cy)) {
  627. return false;
  628. }
  629. if (!SetEvent(signal_ready)) {
  630. hlog("capture_init_shmem: Failed to signal ready: %d",
  631. GetLastError());
  632. return false;
  633. }
  634. active = true;
  635. return true;
  636. }
  637. static inline void thread_data_free(void)
  638. {
  639. if (thread_data.copy_thread) {
  640. DWORD ret;
  641. SetEvent(thread_data.stop_event);
  642. ret = WaitForSingleObject(thread_data.copy_thread, 500);
  643. if (ret != WAIT_OBJECT_0)
  644. TerminateThread(thread_data.copy_thread, (DWORD)-1);
  645. CloseHandle(thread_data.copy_thread);
  646. }
  647. if (thread_data.stop_event)
  648. CloseHandle(thread_data.stop_event);
  649. if (thread_data.copy_event)
  650. CloseHandle(thread_data.copy_event);
  651. for (size_t i = 0; i < NUM_BUFFERS; i++)
  652. DeleteCriticalSection(&thread_data.mutexes[i]);
  653. DeleteCriticalSection(&thread_data.data_mutex);
  654. memset(&thread_data, 0, sizeof(thread_data));
  655. }
  656. void capture_free(void)
  657. {
  658. thread_data_free();
  659. if (shmem_info) {
  660. UnmapViewOfFile(shmem_info);
  661. shmem_info = NULL;
  662. }
  663. close_handle(&shmem_file_handle);
  664. SetEvent(signal_restart);
  665. active = false;
  666. }
  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. /* 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. free_hook();
  735. }
  736. (void)unused1;
  737. return true;
  738. }
  739. __declspec(dllexport) LRESULT CALLBACK
  740. dummy_debug_proc(int code, WPARAM wparam, LPARAM lparam)
  741. {
  742. static bool hooking = true;
  743. MSG *msg = (MSG *)lparam;
  744. if (hooking && msg->message == (WM_USER + 432)) {
  745. HMODULE user32 = GetModuleHandleW(L"USER32");
  746. BOOL(WINAPI * unhook_windows_hook_ex)(HHOOK) = NULL;
  747. unhook_windows_hook_ex = get_obfuscated_func(
  748. user32, "VojeleY`bdgxvM`hhDz", 0x7F55F80C9EE3A213ULL);
  749. if (unhook_windows_hook_ex)
  750. unhook_windows_hook_ex((HHOOK)msg->lParam);
  751. hooking = false;
  752. }
  753. return CallNextHookEx(0, code, wparam, lparam);
  754. }