graphics-hook.c 19 KB

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