graphics-hook.c 19 KB

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