plugin-main.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <windows.h>
  2. #include <obs-module.h>
  3. #include <util/dstr.h>
  4. #include <util/windows/win-version.h>
  5. #include <util/platform.h>
  6. #include <file-updater/file-updater.h>
  7. #include "compat-helpers.h"
  8. #include "compat-format-ver.h"
  9. #include "compat-config.h"
  10. #define WIN_CAPTURE_LOG_STRING "[win-capture plugin] "
  11. #define WIN_CAPTURE_VER_STRING "win-capture plugin (libobs " OBS_VERSION ")"
  12. OBS_DECLARE_MODULE()
  13. OBS_MODULE_USE_DEFAULT_LOCALE("win-capture", "en-US")
  14. MODULE_EXPORT const char *obs_module_description(void)
  15. {
  16. return "Windows game/screen/window capture";
  17. }
  18. extern struct obs_source_info duplicator_capture_info;
  19. extern struct obs_source_info monitor_capture_info;
  20. extern struct obs_source_info window_capture_info;
  21. extern struct obs_source_info game_capture_info;
  22. static HANDLE init_hooks_thread = NULL;
  23. static update_info_t *update_info = NULL;
  24. extern bool cached_versions_match(void);
  25. extern bool load_cached_graphics_offsets(bool is32bit, const char *config_path);
  26. extern bool load_graphics_offsets(bool is32bit, bool use_hook_address_cache,
  27. const char *config_path);
  28. /* temporary, will eventually be erased once we figure out how to create both
  29. * 32bit and 64bit versions of the helpers/hook */
  30. #ifdef _WIN64
  31. #define IS32BIT false
  32. #else
  33. #define IS32BIT true
  34. #endif
  35. static const bool use_hook_address_cache = false;
  36. static DWORD WINAPI init_hooks(LPVOID param)
  37. {
  38. char *config_path = param;
  39. if (use_hook_address_cache && cached_versions_match() &&
  40. load_cached_graphics_offsets(IS32BIT, config_path)) {
  41. load_cached_graphics_offsets(!IS32BIT, config_path);
  42. obs_register_source(&game_capture_info);
  43. } else if (load_graphics_offsets(IS32BIT, use_hook_address_cache,
  44. config_path)) {
  45. load_graphics_offsets(!IS32BIT, use_hook_address_cache,
  46. config_path);
  47. }
  48. bfree(config_path);
  49. return 0;
  50. }
  51. void wait_for_hook_initialization(void)
  52. {
  53. static bool initialized = false;
  54. if (!initialized) {
  55. if (init_hooks_thread) {
  56. WaitForSingleObject(init_hooks_thread, INFINITE);
  57. CloseHandle(init_hooks_thread);
  58. init_hooks_thread = NULL;
  59. }
  60. initialized = true;
  61. }
  62. }
  63. static bool confirm_compat_file(void *param, struct file_download_data *file)
  64. {
  65. if (astrcmpi(file->name, "compatibility.json") == 0) {
  66. obs_data_t *data;
  67. int format_version;
  68. data = obs_data_create_from_json((char *)file->buffer.array);
  69. if (!data)
  70. return false;
  71. format_version = (int)obs_data_get_int(data, "format_version");
  72. obs_data_release(data);
  73. if (format_version != COMPAT_FORMAT_VERSION)
  74. return false;
  75. }
  76. UNUSED_PARAMETER(param);
  77. return true;
  78. }
  79. void init_hook_files(void);
  80. bool graphics_uses_d3d11 = false;
  81. bool wgc_supported = false;
  82. bool obs_module_load(void)
  83. {
  84. struct win_version_info ver;
  85. bool win8_or_above = false;
  86. char *local_dir;
  87. char *config_dir;
  88. char update_url[128];
  89. snprintf(update_url, sizeof(update_url), "%s/v%d", COMPAT_URL,
  90. COMPAT_FORMAT_VERSION);
  91. struct win_version_info win1903 = {
  92. .major = 10, .minor = 0, .build = 18362, .revis = 0};
  93. local_dir = obs_module_file(NULL);
  94. config_dir = obs_module_config_path(NULL);
  95. if (config_dir) {
  96. os_mkdirs(config_dir);
  97. if (local_dir) {
  98. update_info = update_info_create(
  99. WIN_CAPTURE_LOG_STRING, WIN_CAPTURE_VER_STRING,
  100. update_url, local_dir, config_dir,
  101. confirm_compat_file, NULL);
  102. }
  103. }
  104. bfree(config_dir);
  105. bfree(local_dir);
  106. get_win_ver(&ver);
  107. win8_or_above = ver.major > 6 || (ver.major == 6 && ver.minor >= 2);
  108. obs_enter_graphics();
  109. graphics_uses_d3d11 = gs_get_device_type() == GS_DEVICE_DIRECT3D_11;
  110. obs_leave_graphics();
  111. if (graphics_uses_d3d11)
  112. wgc_supported = win_version_compare(&ver, &win1903) >= 0;
  113. if (win8_or_above && graphics_uses_d3d11)
  114. obs_register_source(&duplicator_capture_info);
  115. else
  116. obs_register_source(&monitor_capture_info);
  117. obs_register_source(&window_capture_info);
  118. char *config_path = obs_module_config_path(NULL);
  119. init_hook_files();
  120. init_hooks_thread =
  121. CreateThread(NULL, 0, init_hooks, config_path, 0, NULL);
  122. obs_register_source(&game_capture_info);
  123. return true;
  124. }
  125. void obs_module_unload(void)
  126. {
  127. wait_for_hook_initialization();
  128. update_info_destroy(update_info);
  129. compat_json_free();
  130. }