graphics-hook.c 20 KB

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