graphics-hook.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. if (WaitForSingleObject(tex_mutexes[id], 0) == WAIT_OBJECT_0) {
  392. return id;
  393. } else if (WaitForSingleObject(tex_mutexes[next], 0) == WAIT_OBJECT_0) {
  394. return next;
  395. }
  396. return -1;
  397. }
  398. static inline void unlock_shmem_tex(int id)
  399. {
  400. if (id != -1) {
  401. ReleaseMutex(tex_mutexes[id]);
  402. }
  403. }
  404. static inline bool init_shared_info(size_t size)
  405. {
  406. wchar_t name[64];
  407. _snwprintf(name, 64, L"%s%ld", SHMEM_TEXTURE, ++shmem_id_counter);
  408. shmem_file_handle = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
  409. PAGE_READWRITE, 0, (DWORD)size, name);
  410. if (!shmem_file_handle) {
  411. hlog("init_shared_info: Failed to create shared memory: %d",
  412. GetLastError());
  413. return false;
  414. }
  415. shmem_info = MapViewOfFile(shmem_file_handle, FILE_MAP_ALL_ACCESS,
  416. 0, 0, size);
  417. if (!shmem_info) {
  418. hlog("init_shared_info: Failed to map shared memory: %d",
  419. GetLastError());
  420. return false;
  421. }
  422. return true;
  423. }
  424. bool capture_init_shtex(struct shtex_data **data, HWND window,
  425. uint32_t base_cx, uint32_t base_cy, uint32_t cx, uint32_t cy,
  426. uint32_t format, bool flip, uintptr_t handle)
  427. {
  428. if (!init_shared_info(sizeof(struct shtex_data))) {
  429. hlog("capture_init_shtex: Failed to initialize memory");
  430. return false;
  431. }
  432. *data = shmem_info;
  433. (*data)->tex_handle = (uint32_t)handle;
  434. global_hook_info->window = (uint32_t)(uintptr_t)window;
  435. global_hook_info->type = CAPTURE_TYPE_TEXTURE;
  436. global_hook_info->format = format;
  437. global_hook_info->flip = flip;
  438. global_hook_info->map_id = shmem_id_counter;
  439. global_hook_info->map_size = sizeof(struct shtex_data);
  440. global_hook_info->cx = cx;
  441. global_hook_info->cy = cy;
  442. global_hook_info->base_cx = base_cx;
  443. global_hook_info->base_cy = base_cy;
  444. if (!SetEvent(signal_ready)) {
  445. hlog("capture_init_shtex: Failed to signal ready: %d",
  446. GetLastError());
  447. return false;
  448. }
  449. active = true;
  450. return true;
  451. }
  452. static DWORD CALLBACK copy_thread(LPVOID unused)
  453. {
  454. uint32_t pitch = thread_data.pitch;
  455. uint32_t cy = thread_data.cy;
  456. HANDLE events[2] = {NULL, NULL};
  457. int shmem_id = 0;
  458. if (!duplicate_handle(&events[0], thread_data.copy_event)) {
  459. hlog_hr("copy_thread: Failed to duplicate copy event: %d",
  460. GetLastError());
  461. return 0;
  462. }
  463. if (!duplicate_handle(&events[1], thread_data.stop_event)) {
  464. hlog_hr("copy_thread: Failed to duplicate stop event: %d",
  465. GetLastError());
  466. goto finish;
  467. }
  468. for (;;) {
  469. int copy_tex;
  470. void *cur_data;
  471. DWORD ret = WaitForMultipleObjects(2, events, false, INFINITE);
  472. if (ret != WAIT_OBJECT_0) {
  473. break;
  474. }
  475. EnterCriticalSection(&thread_data.data_mutex);
  476. copy_tex = thread_data.cur_tex;
  477. cur_data = thread_data.cur_data;
  478. LeaveCriticalSection(&thread_data.data_mutex);
  479. if (copy_tex < NUM_BUFFERS && !!cur_data) {
  480. EnterCriticalSection(&thread_data.mutexes[copy_tex]);
  481. int lock_id = try_lock_shmem_tex(shmem_id);
  482. if (lock_id != -1) {
  483. memcpy(thread_data.shmem_textures[lock_id],
  484. cur_data, pitch * cy);
  485. unlock_shmem_tex(lock_id);
  486. ((struct shmem_data*)shmem_info)->last_tex =
  487. lock_id;
  488. shmem_id = lock_id == 0 ? 1 : 0;
  489. }
  490. LeaveCriticalSection(&thread_data.mutexes[copy_tex]);
  491. }
  492. }
  493. finish:
  494. for (size_t i = 0; i < 2; i++) {
  495. if (events[i]) {
  496. CloseHandle(events[i]);
  497. }
  498. }
  499. (void)unused;
  500. return 0;
  501. }
  502. void shmem_copy_data(size_t idx, void *volatile data)
  503. {
  504. EnterCriticalSection(&thread_data.data_mutex);
  505. thread_data.cur_tex = (int)idx;
  506. thread_data.cur_data = data;
  507. thread_data.locked_textures[idx] = true;
  508. LeaveCriticalSection(&thread_data.data_mutex);
  509. SetEvent(thread_data.copy_event);
  510. }
  511. bool shmem_texture_data_lock(int idx)
  512. {
  513. bool locked;
  514. EnterCriticalSection(&thread_data.data_mutex);
  515. locked = thread_data.locked_textures[idx];
  516. LeaveCriticalSection(&thread_data.data_mutex);
  517. if (locked) {
  518. EnterCriticalSection(&thread_data.mutexes[idx]);
  519. return true;
  520. }
  521. return false;
  522. }
  523. void shmem_texture_data_unlock(int idx)
  524. {
  525. EnterCriticalSection(&thread_data.data_mutex);
  526. thread_data.locked_textures[idx] = false;
  527. LeaveCriticalSection(&thread_data.data_mutex);
  528. LeaveCriticalSection(&thread_data.mutexes[idx]);
  529. }
  530. static inline bool init_shmem_thread(uint32_t pitch, uint32_t cy)
  531. {
  532. struct shmem_data *data = shmem_info;
  533. thread_data.pitch = pitch;
  534. thread_data.cy = cy;
  535. thread_data.shmem_textures[0] = (uint8_t*)data + data->tex1_offset;
  536. thread_data.shmem_textures[1] = (uint8_t*)data + data->tex2_offset;
  537. thread_data.copy_event = CreateEvent(NULL, false, false, NULL);
  538. if (!thread_data.copy_event) {
  539. hlog("init_shmem_thread: Failed to create copy event: %d",
  540. GetLastError());
  541. return false;
  542. }
  543. thread_data.stop_event = CreateEvent(NULL, true, false, NULL);
  544. if (!thread_data.stop_event) {
  545. hlog("init_shmem_thread: Failed to create stop event: %d",
  546. GetLastError());
  547. return false;
  548. }
  549. for (size_t i = 0; i < NUM_BUFFERS; i++) {
  550. InitializeCriticalSection(&thread_data.mutexes[i]);
  551. }
  552. InitializeCriticalSection(&thread_data.data_mutex);
  553. thread_data.copy_thread = CreateThread(NULL, 0, copy_thread, NULL, 0,
  554. NULL);
  555. if (!thread_data.copy_thread) {
  556. hlog("init_shmem_thread: Failed to create thread: %d",
  557. GetLastError());
  558. return false;
  559. }
  560. return true;
  561. }
  562. #ifndef ALIGN
  563. #define ALIGN(bytes, align) (((bytes) + ((align) - 1)) & ~((align) - 1))
  564. #endif
  565. bool capture_init_shmem(struct shmem_data **data, HWND window,
  566. uint32_t base_cx, uint32_t base_cy, uint32_t cx, uint32_t cy,
  567. uint32_t pitch, uint32_t format, bool flip)
  568. {
  569. uint32_t tex_size = cy * pitch;
  570. uint32_t aligned_header = ALIGN(sizeof(struct shmem_data), 32);
  571. uint32_t aligned_tex = ALIGN(tex_size, 32);
  572. uint32_t total_size = aligned_header + aligned_tex * 2 + 32;
  573. uintptr_t align_pos;
  574. if (!init_shared_info(total_size)) {
  575. hlog("capture_init_shmem: Failed to initialize memory");
  576. return false;
  577. }
  578. *data = shmem_info;
  579. /* to ensure fast copy rate, align texture data to 256bit addresses */
  580. align_pos = (uintptr_t)shmem_info;
  581. align_pos += aligned_header;
  582. align_pos &= ~(32 - 1);
  583. align_pos -= (uintptr_t)shmem_info;
  584. if (align_pos < sizeof(struct shmem_data))
  585. align_pos += 32;
  586. (*data)->last_tex = -1;
  587. (*data)->tex1_offset = (uint32_t)align_pos;
  588. (*data)->tex2_offset = (*data)->tex1_offset + aligned_tex;
  589. global_hook_info->window = (uint32_t)(uintptr_t)window;
  590. global_hook_info->type = CAPTURE_TYPE_MEMORY;
  591. global_hook_info->format = format;
  592. global_hook_info->flip = flip;
  593. global_hook_info->map_id = shmem_id_counter;
  594. global_hook_info->map_size = total_size;
  595. global_hook_info->pitch = pitch;
  596. global_hook_info->cx = cx;
  597. global_hook_info->cy = cy;
  598. global_hook_info->base_cx = base_cx;
  599. global_hook_info->base_cy = base_cy;
  600. if (!init_shmem_thread(pitch, cy)) {
  601. return false;
  602. }
  603. if (!SetEvent(signal_ready)) {
  604. hlog("capture_init_shmem: Failed to signal ready: %d",
  605. GetLastError());
  606. return false;
  607. }
  608. active = true;
  609. return true;
  610. }
  611. static inline void thread_data_free(void)
  612. {
  613. if (thread_data.copy_thread) {
  614. DWORD ret;
  615. SetEvent(thread_data.stop_event);
  616. ret = WaitForSingleObject(thread_data.copy_thread, 500);
  617. if (ret != WAIT_OBJECT_0)
  618. TerminateThread(thread_data.copy_thread, (DWORD)-1);
  619. CloseHandle(thread_data.copy_thread);
  620. }
  621. if (thread_data.stop_event)
  622. CloseHandle(thread_data.stop_event);
  623. if (thread_data.copy_event)
  624. CloseHandle(thread_data.copy_event);
  625. for (size_t i = 0; i < NUM_BUFFERS; i++)
  626. DeleteCriticalSection(&thread_data.mutexes[i]);
  627. DeleteCriticalSection(&thread_data.data_mutex);
  628. memset(&thread_data, 0, sizeof(thread_data));
  629. }
  630. void capture_free(void)
  631. {
  632. thread_data_free();
  633. if (shmem_info) {
  634. UnmapViewOfFile(shmem_info);
  635. shmem_info = NULL;
  636. }
  637. close_handle(&shmem_file_handle);
  638. SetEvent(signal_restart);
  639. active = false;
  640. }
  641. BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID unused1)
  642. {
  643. if (reason == DLL_PROCESS_ATTACH) {
  644. wchar_t name[MAX_PATH];
  645. dll_inst = hinst;
  646. HANDLE cur_thread;
  647. bool success = DuplicateHandle(GetCurrentProcess(),
  648. GetCurrentThread(),
  649. GetCurrentProcess(), &cur_thread,
  650. SYNCHRONIZE, false, 0);
  651. if (!success)
  652. DbgOut("Failed to get current thread handle");
  653. if (!init_signals()) {
  654. return false;
  655. }
  656. if (!init_system_path()) {
  657. return false;
  658. }
  659. if (!init_hook_info()) {
  660. return false;
  661. }
  662. if (!init_mutexes()) {
  663. return false;
  664. }
  665. /* this prevents the library from being automatically unloaded
  666. * by the next FreeLibrary call */
  667. GetModuleFileNameW(hinst, name, MAX_PATH);
  668. LoadLibraryW(name);
  669. capture_thread = CreateThread(NULL, 0,
  670. (LPTHREAD_START_ROUTINE)main_capture_thread,
  671. (LPVOID)cur_thread, 0, 0);
  672. if (!capture_thread) {
  673. CloseHandle(cur_thread);
  674. return false;
  675. }
  676. } else if (reason == DLL_PROCESS_DETACH) {
  677. if (capture_thread) {
  678. stop_loop = true;
  679. WaitForSingleObject(capture_thread, 300);
  680. CloseHandle(capture_thread);
  681. }
  682. free_hook();
  683. }
  684. (void)unused1;
  685. return true;
  686. }
  687. __declspec(dllexport) LRESULT CALLBACK dummy_debug_proc(int code,
  688. WPARAM wparam, LPARAM lparam)
  689. {
  690. static bool hooking = true;
  691. MSG *msg = (MSG*)lparam;
  692. if (hooking && msg->message == (WM_USER + 432)) {
  693. HMODULE user32 = GetModuleHandleW(L"USER32");
  694. BOOL (WINAPI *unhook_windows_hook_ex)(HHOOK) = NULL;
  695. unhook_windows_hook_ex = get_obfuscated_func(user32,
  696. "VojeleY`bdgxvM`hhDz",
  697. 0x7F55F80C9EE3A213ULL);
  698. if (unhook_windows_hook_ex)
  699. unhook_windows_hook_ex((HHOOK)msg->lParam);
  700. hooking = false;
  701. }
  702. return CallNextHookEx(0, code, wparam, lparam);
  703. }