plugin-main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <windows.h>
  2. #include <obs-module.h>
  3. #include <util/windows/win-version.h>
  4. #include <util/platform.h>
  5. OBS_DECLARE_MODULE()
  6. OBS_MODULE_USE_DEFAULT_LOCALE("win-capture", "en-US")
  7. MODULE_EXPORT const char *obs_module_description(void)
  8. {
  9. return "Windows game/screen/window capture";
  10. }
  11. extern struct obs_source_info duplicator_capture_info;
  12. extern struct obs_source_info monitor_capture_info;
  13. extern struct obs_source_info window_capture_info;
  14. extern struct obs_source_info game_capture_info;
  15. static HANDLE init_hooks_thread = NULL;
  16. extern bool cached_versions_match(void);
  17. extern bool load_cached_graphics_offsets(bool is32bit, const char *config_path);
  18. extern bool load_graphics_offsets(bool is32bit, bool use_hook_address_cache,
  19. const char *config_path);
  20. /* temporary, will eventually be erased once we figure out how to create both
  21. * 32bit and 64bit versions of the helpers/hook */
  22. #ifdef _WIN64
  23. #define IS32BIT false
  24. #else
  25. #define IS32BIT true
  26. #endif
  27. static const bool use_hook_address_cache = false;
  28. static DWORD WINAPI init_hooks(LPVOID param)
  29. {
  30. char *config_path = param;
  31. if (use_hook_address_cache && cached_versions_match() &&
  32. load_cached_graphics_offsets(IS32BIT, config_path)) {
  33. load_cached_graphics_offsets(!IS32BIT, config_path);
  34. obs_register_source(&game_capture_info);
  35. } else if (load_graphics_offsets(IS32BIT, use_hook_address_cache,
  36. config_path)) {
  37. load_graphics_offsets(!IS32BIT, use_hook_address_cache,
  38. config_path);
  39. }
  40. bfree(config_path);
  41. return 0;
  42. }
  43. void wait_for_hook_initialization(void)
  44. {
  45. static bool initialized = false;
  46. if (!initialized) {
  47. if (init_hooks_thread) {
  48. WaitForSingleObject(init_hooks_thread, INFINITE);
  49. CloseHandle(init_hooks_thread);
  50. init_hooks_thread = NULL;
  51. }
  52. initialized = true;
  53. }
  54. }
  55. void init_hook_files(void);
  56. bool graphics_uses_d3d11 = false;
  57. bool wgc_supported = false;
  58. bool obs_module_load(void)
  59. {
  60. struct win_version_info ver;
  61. bool win8_or_above = false;
  62. char *config_dir;
  63. struct win_version_info win1903 = {
  64. .major = 10, .minor = 0, .build = 18362, .revis = 0};
  65. config_dir = obs_module_config_path(NULL);
  66. if (config_dir) {
  67. os_mkdirs(config_dir);
  68. bfree(config_dir);
  69. }
  70. get_win_ver(&ver);
  71. win8_or_above = ver.major > 6 || (ver.major == 6 && ver.minor >= 2);
  72. obs_enter_graphics();
  73. graphics_uses_d3d11 = gs_get_device_type() == GS_DEVICE_DIRECT3D_11;
  74. obs_leave_graphics();
  75. if (graphics_uses_d3d11)
  76. wgc_supported = win_version_compare(&ver, &win1903) >= 0;
  77. if (win8_or_above && graphics_uses_d3d11)
  78. obs_register_source(&duplicator_capture_info);
  79. else
  80. obs_register_source(&monitor_capture_info);
  81. obs_register_source(&window_capture_info);
  82. char *config_path = obs_module_config_path(NULL);
  83. init_hook_files();
  84. init_hooks_thread =
  85. CreateThread(NULL, 0, init_hooks, config_path, 0, NULL);
  86. obs_register_source(&game_capture_info);
  87. return true;
  88. }
  89. void obs_module_unload(void)
  90. {
  91. wait_for_hook_initialization();
  92. }