PluginManager.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /******************************************************************************
  2. Copyright (C) 2025 by FiniteSingularity <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "PluginManager.hpp"
  15. #include "PluginManagerWindow.hpp"
  16. #include <OBSApp.hpp>
  17. #include <qt-wrappers.hpp>
  18. #include <widgets/OBSBasic.hpp>
  19. #include <QMessageBox>
  20. #include <nlohmann/json.hpp>
  21. #include <algorithm>
  22. #include <fstream>
  23. extern bool restart;
  24. namespace OBS {
  25. void addModuleToPluginManagerImpl(void *param, obs_module_t *newModule)
  26. {
  27. auto &instance = *static_cast<OBS::PluginManager *>(param);
  28. std::string moduleName = obs_get_module_file_name(newModule);
  29. moduleName = moduleName.substr(0, moduleName.rfind("."));
  30. if (!obs_get_module_allow_disable(moduleName.c_str()))
  31. return;
  32. const char *display_name = obs_get_module_name(newModule);
  33. std::string module_name = moduleName;
  34. const char *id = obs_get_module_id(newModule);
  35. const char *version = obs_get_module_version(newModule);
  36. auto it = std::find_if(instance.modules_.begin(), instance.modules_.end(),
  37. [&](OBS::ModuleInfo module) { return module.module_name == moduleName; });
  38. if (it == instance.modules_.end()) {
  39. instance.modules_.push_back({display_name ? display_name : "", module_name, id ? id : "",
  40. version ? version : "", true, true});
  41. } else {
  42. it->display_name = display_name ? display_name : "";
  43. it->module_name = module_name;
  44. it->id = id ? id : "";
  45. it->version = version ? version : "";
  46. }
  47. }
  48. constexpr std::string_view OBSPluginManagerPath = "obs-studio/plugin_manager";
  49. constexpr std::string_view OBSPluginManagerModulesFile = "modules.json";
  50. void PluginManager::preLoad()
  51. {
  52. loadModules_();
  53. disableModules_();
  54. }
  55. void PluginManager::postLoad()
  56. {
  57. // Find any new modules and add to Plugin Manager.
  58. obs_enum_modules(addModuleToPluginManager, this);
  59. // Get list of valid module types.
  60. addModuleTypes_();
  61. saveModules_();
  62. // Add provided features from any unloaded modules
  63. linkUnloadedModules_();
  64. }
  65. std::filesystem::path PluginManager::getConfigFilePath_()
  66. {
  67. std::filesystem::path path = App()->userPluginManagerSettingsLocation /
  68. std::filesystem::u8path(OBSPluginManagerPath) /
  69. std::filesystem::u8path(OBSPluginManagerModulesFile);
  70. return path;
  71. }
  72. void PluginManager::loadModules_()
  73. {
  74. auto modulesFile = getConfigFilePath_();
  75. if (std::filesystem::exists(modulesFile)) {
  76. std::ifstream jsonFile(modulesFile);
  77. nlohmann::json data = nlohmann::json::parse(jsonFile);
  78. modules_.clear();
  79. for (auto it : data) {
  80. ModuleInfo obsModule;
  81. try {
  82. obsModule = {it.at("display_name"),
  83. it.at("module_name"),
  84. it.at("id"),
  85. it.at("version"),
  86. it.at("enabled"),
  87. it.at("enabled"),
  88. it.at("sources"),
  89. it.at("outputs"),
  90. it.at("encoders"),
  91. it.at("services"),
  92. {},
  93. {},
  94. {},
  95. {}};
  96. } catch (const nlohmann::json::out_of_range &error) {
  97. blog(LOG_WARNING, "Error loading module info: %s", error.what());
  98. continue;
  99. }
  100. modules_.push_back(obsModule);
  101. }
  102. }
  103. }
  104. void PluginManager::linkUnloadedModules_()
  105. {
  106. for (const auto &moduleInfo : modules_) {
  107. if (!moduleInfo.enabled) {
  108. auto obsModule = obs_get_disabled_module(moduleInfo.module_name.c_str());
  109. if (!obsModule) {
  110. continue;
  111. }
  112. for (const auto &source : moduleInfo.sources) {
  113. obs_module_add_source(obsModule, source.c_str());
  114. }
  115. for (const auto &output : moduleInfo.outputs) {
  116. obs_module_add_output(obsModule, output.c_str());
  117. }
  118. for (const auto &encoder : moduleInfo.encoders) {
  119. obs_module_add_encoder(obsModule, encoder.c_str());
  120. }
  121. for (const auto &service : moduleInfo.services) {
  122. obs_module_add_service(obsModule, service.c_str());
  123. }
  124. }
  125. }
  126. }
  127. void PluginManager::saveModules_()
  128. {
  129. auto modulesFile = getConfigFilePath_();
  130. std::ofstream outFile(modulesFile);
  131. nlohmann::json data = nlohmann::json::array();
  132. for (auto const &moduleInfo : modules_) {
  133. nlohmann::json modData;
  134. modData["display_name"] = moduleInfo.display_name;
  135. modData["module_name"] = moduleInfo.module_name;
  136. modData["id"] = moduleInfo.id;
  137. modData["version"] = moduleInfo.version;
  138. modData["enabled"] = moduleInfo.enabled;
  139. modData["sources"] = moduleInfo.sources;
  140. modData["outputs"] = moduleInfo.outputs;
  141. modData["encoders"] = moduleInfo.encoders;
  142. modData["services"] = moduleInfo.services;
  143. data.push_back(modData);
  144. }
  145. outFile << std::setw(4) << data << std::endl;
  146. }
  147. void PluginManager::addModuleTypes_()
  148. {
  149. const char *source_id;
  150. int i = 0;
  151. while (obs_enum_source_types(i, &source_id)) {
  152. i += 1;
  153. obs_module_t *obsModule = obs_source_get_module(source_id);
  154. if (!obsModule) {
  155. continue;
  156. }
  157. std::string moduleName = obs_get_module_file_name(obsModule);
  158. moduleName = moduleName.substr(0, moduleName.rfind("."));
  159. auto it = std::find_if(modules_.begin(), modules_.end(),
  160. [moduleName](ModuleInfo const &m) { return m.module_name == moduleName; });
  161. if (it != modules_.end()) {
  162. it->sourcesLoaded.push_back(source_id);
  163. }
  164. }
  165. const char *output_id;
  166. i = 0;
  167. while (obs_enum_output_types(i, &output_id)) {
  168. i += 1;
  169. obs_module_t *obsModule = obs_source_get_module(output_id);
  170. if (!obsModule) {
  171. continue;
  172. }
  173. std::string moduleName = obs_get_module_file_name(obsModule);
  174. moduleName = moduleName.substr(0, moduleName.rfind("."));
  175. auto it = std::find_if(modules_.begin(), modules_.end(),
  176. [moduleName](ModuleInfo const &m) { return m.module_name == moduleName; });
  177. if (it != modules_.end()) {
  178. it->outputsLoaded.push_back(output_id);
  179. }
  180. }
  181. const char *encoder_id;
  182. i = 0;
  183. while (obs_enum_encoder_types(i, &encoder_id)) {
  184. i += 1;
  185. obs_module_t *obsModule = obs_source_get_module(encoder_id);
  186. if (!obsModule) {
  187. continue;
  188. }
  189. std::string moduleName = obs_get_module_file_name(obsModule);
  190. moduleName = moduleName.substr(0, moduleName.rfind("."));
  191. auto it = std::find_if(modules_.begin(), modules_.end(),
  192. [moduleName](ModuleInfo const &m) { return m.module_name == moduleName; });
  193. if (it != modules_.end()) {
  194. it->encodersLoaded.push_back(encoder_id);
  195. }
  196. }
  197. const char *service_id;
  198. i = 0;
  199. while (obs_enum_service_types(i, &service_id)) {
  200. i += 1;
  201. obs_module_t *obsModule = obs_source_get_module(service_id);
  202. if (!obsModule) {
  203. continue;
  204. }
  205. std::string moduleName = obs_get_module_file_name(obsModule);
  206. moduleName = moduleName.substr(0, moduleName.rfind("."));
  207. auto it = std::find_if(modules_.begin(), modules_.end(),
  208. [moduleName](ModuleInfo const &m) { return m.module_name == moduleName; });
  209. if (it != modules_.end()) {
  210. it->servicesLoaded.push_back(service_id);
  211. }
  212. }
  213. for (auto &moduleInfo : modules_) {
  214. if (moduleInfo.enabledAtLaunch) {
  215. moduleInfo.sources = moduleInfo.sourcesLoaded;
  216. moduleInfo.encoders = moduleInfo.encodersLoaded;
  217. moduleInfo.outputs = moduleInfo.outputsLoaded;
  218. moduleInfo.services = moduleInfo.servicesLoaded;
  219. } else {
  220. for (auto const &source : moduleInfo.sources) {
  221. disabledSources_.push_back(source);
  222. }
  223. for (auto const &output : moduleInfo.outputs) {
  224. disabledOutputs_.push_back(output);
  225. }
  226. for (auto const &encoder : moduleInfo.encoders) {
  227. disabledEncoders_.push_back(encoder);
  228. }
  229. for (auto const &service : moduleInfo.services) {
  230. disabledServices_.push_back(service);
  231. }
  232. }
  233. }
  234. }
  235. void PluginManager::disableModules_()
  236. {
  237. for (const auto &moduleInfo : modules_) {
  238. if (!moduleInfo.enabled) {
  239. obs_add_disabled_module(moduleInfo.module_name.c_str());
  240. }
  241. }
  242. }
  243. void PluginManager::open()
  244. {
  245. auto main = OBSBasic::Get();
  246. PluginManagerWindow pluginManagerWindow(modules_, main);
  247. auto result = pluginManagerWindow.exec();
  248. if (result == QDialog::Accepted) {
  249. modules_ = pluginManagerWindow.result();
  250. saveModules_();
  251. bool changed = false;
  252. for (auto const &moduleInfo : modules_) {
  253. if (moduleInfo.enabled != moduleInfo.enabledAtLaunch) {
  254. changed = true;
  255. break;
  256. }
  257. }
  258. if (changed) {
  259. QMessageBox::StandardButton button = OBSMessageBox::question(
  260. main, QTStr("PluginManager.Restart"), QTStr("PluginManager.NeedsRestart"));
  261. if (button == QMessageBox::Yes) {
  262. restart = true;
  263. main->close();
  264. }
  265. }
  266. }
  267. }
  268. }; // namespace OBS