PluginManager.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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;
  78. try {
  79. data = nlohmann::json::parse(jsonFile);
  80. } catch (const nlohmann::json::parse_error &error) {
  81. modules_.clear();
  82. blog(LOG_ERROR, "Error loading modules config file: %s", error.what());
  83. blog(LOG_ERROR, "Generating new config file.");
  84. return;
  85. }
  86. modules_.clear();
  87. for (auto it : data) {
  88. ModuleInfo obsModule;
  89. try {
  90. obsModule = {it.at("display_name"),
  91. it.at("module_name"),
  92. it.at("id"),
  93. it.at("version"),
  94. it.at("enabled"),
  95. it.at("enabled"),
  96. it.at("sources"),
  97. it.at("outputs"),
  98. it.at("encoders"),
  99. it.at("services"),
  100. {},
  101. {},
  102. {},
  103. {}};
  104. } catch (const nlohmann::json::out_of_range &error) {
  105. blog(LOG_WARNING, "Error loading module info: %s", error.what());
  106. continue;
  107. }
  108. modules_.push_back(obsModule);
  109. }
  110. }
  111. }
  112. void PluginManager::linkUnloadedModules_()
  113. {
  114. for (const auto &moduleInfo : modules_) {
  115. if (!moduleInfo.enabled) {
  116. auto obsModule = obs_get_disabled_module(moduleInfo.module_name.c_str());
  117. if (!obsModule) {
  118. continue;
  119. }
  120. for (const auto &source : moduleInfo.sources) {
  121. obs_module_add_source(obsModule, source.c_str());
  122. }
  123. for (const auto &output : moduleInfo.outputs) {
  124. obs_module_add_output(obsModule, output.c_str());
  125. }
  126. for (const auto &encoder : moduleInfo.encoders) {
  127. obs_module_add_encoder(obsModule, encoder.c_str());
  128. }
  129. for (const auto &service : moduleInfo.services) {
  130. obs_module_add_service(obsModule, service.c_str());
  131. }
  132. }
  133. }
  134. }
  135. void PluginManager::saveModules_()
  136. {
  137. auto modulesFile = getConfigFilePath_();
  138. std::ofstream outFile(modulesFile);
  139. nlohmann::json data = nlohmann::json::array();
  140. for (auto const &moduleInfo : modules_) {
  141. nlohmann::json modData;
  142. modData["display_name"] = moduleInfo.display_name;
  143. modData["module_name"] = moduleInfo.module_name;
  144. modData["id"] = moduleInfo.id;
  145. modData["version"] = moduleInfo.version;
  146. modData["enabled"] = moduleInfo.enabled;
  147. modData["sources"] = moduleInfo.sources;
  148. modData["outputs"] = moduleInfo.outputs;
  149. modData["encoders"] = moduleInfo.encoders;
  150. modData["services"] = moduleInfo.services;
  151. data.push_back(modData);
  152. }
  153. outFile << std::setw(4) << data << std::endl;
  154. }
  155. void PluginManager::addModuleTypes_()
  156. {
  157. const char *source_id;
  158. int i = 0;
  159. while (obs_enum_source_types(i, &source_id)) {
  160. i += 1;
  161. obs_module_t *obsModule = obs_source_get_module(source_id);
  162. if (!obsModule) {
  163. continue;
  164. }
  165. std::string moduleName = obs_get_module_file_name(obsModule);
  166. moduleName = moduleName.substr(0, moduleName.rfind("."));
  167. auto it = std::find_if(modules_.begin(), modules_.end(),
  168. [moduleName](ModuleInfo const &m) { return m.module_name == moduleName; });
  169. if (it != modules_.end()) {
  170. it->sourcesLoaded.push_back(source_id);
  171. }
  172. }
  173. const char *output_id;
  174. i = 0;
  175. while (obs_enum_output_types(i, &output_id)) {
  176. i += 1;
  177. obs_module_t *obsModule = obs_output_get_module(output_id);
  178. if (!obsModule) {
  179. continue;
  180. }
  181. std::string moduleName = obs_get_module_file_name(obsModule);
  182. moduleName = moduleName.substr(0, moduleName.rfind("."));
  183. auto it = std::find_if(modules_.begin(), modules_.end(),
  184. [moduleName](ModuleInfo const &m) { return m.module_name == moduleName; });
  185. if (it != modules_.end()) {
  186. it->outputsLoaded.push_back(output_id);
  187. }
  188. }
  189. const char *encoder_id;
  190. i = 0;
  191. while (obs_enum_encoder_types(i, &encoder_id)) {
  192. i += 1;
  193. obs_module_t *obsModule = obs_encoder_get_module(encoder_id);
  194. if (!obsModule) {
  195. continue;
  196. }
  197. std::string moduleName = obs_get_module_file_name(obsModule);
  198. moduleName = moduleName.substr(0, moduleName.rfind("."));
  199. auto it = std::find_if(modules_.begin(), modules_.end(),
  200. [moduleName](ModuleInfo const &m) { return m.module_name == moduleName; });
  201. if (it != modules_.end()) {
  202. it->encodersLoaded.push_back(encoder_id);
  203. }
  204. }
  205. const char *service_id;
  206. i = 0;
  207. while (obs_enum_service_types(i, &service_id)) {
  208. i += 1;
  209. obs_module_t *obsModule = obs_service_get_module(service_id);
  210. if (!obsModule) {
  211. continue;
  212. }
  213. std::string moduleName = obs_get_module_file_name(obsModule);
  214. moduleName = moduleName.substr(0, moduleName.rfind("."));
  215. auto it = std::find_if(modules_.begin(), modules_.end(),
  216. [moduleName](ModuleInfo const &m) { return m.module_name == moduleName; });
  217. if (it != modules_.end()) {
  218. it->servicesLoaded.push_back(service_id);
  219. }
  220. }
  221. for (auto &moduleInfo : modules_) {
  222. if (moduleInfo.enabledAtLaunch) {
  223. moduleInfo.sources = moduleInfo.sourcesLoaded;
  224. moduleInfo.encoders = moduleInfo.encodersLoaded;
  225. moduleInfo.outputs = moduleInfo.outputsLoaded;
  226. moduleInfo.services = moduleInfo.servicesLoaded;
  227. } else {
  228. for (auto const &source : moduleInfo.sources) {
  229. disabledSources_.push_back(source);
  230. }
  231. for (auto const &output : moduleInfo.outputs) {
  232. disabledOutputs_.push_back(output);
  233. }
  234. for (auto const &encoder : moduleInfo.encoders) {
  235. disabledEncoders_.push_back(encoder);
  236. }
  237. for (auto const &service : moduleInfo.services) {
  238. disabledServices_.push_back(service);
  239. }
  240. }
  241. }
  242. }
  243. void PluginManager::disableModules_()
  244. {
  245. for (const auto &moduleInfo : modules_) {
  246. if (!moduleInfo.enabled) {
  247. obs_add_disabled_module(moduleInfo.module_name.c_str());
  248. }
  249. }
  250. }
  251. void PluginManager::open()
  252. {
  253. auto main = OBSBasic::Get();
  254. PluginManagerWindow pluginManagerWindow(modules_, main);
  255. auto result = pluginManagerWindow.exec();
  256. if (result == QDialog::Accepted) {
  257. modules_ = pluginManagerWindow.result();
  258. saveModules_();
  259. bool changed = false;
  260. for (auto const &moduleInfo : modules_) {
  261. if (moduleInfo.enabled != moduleInfo.enabledAtLaunch) {
  262. changed = true;
  263. break;
  264. }
  265. }
  266. if (changed) {
  267. QMessageBox::StandardButton button =
  268. OBSMessageBox::question(main, QTStr("Restart"), QTStr("NeedsRestart"));
  269. if (button == QMessageBox::Yes) {
  270. restart = true;
  271. main->close();
  272. }
  273. }
  274. }
  275. }
  276. }; // namespace OBS