|
| 1 | +// |
| 2 | +// Copyright RIME Developers |
| 3 | +// Distributed under the BSD License |
| 4 | +// |
| 5 | + |
| 6 | +#include <boost/algorithm/string.hpp> |
| 7 | +#include <boost/dll.hpp> |
| 8 | +#include <boost/filesystem.hpp> |
| 9 | +#include <rime/build_config.h> |
| 10 | +#include <rime/common.h> |
| 11 | +#include <rime/component.h> |
| 12 | +#include <rime/module.h> |
| 13 | +#include <rime/registry.h> |
| 14 | +#include <rime_api.h> |
| 15 | + |
| 16 | +namespace fs = boost::filesystem; |
| 17 | + |
| 18 | +namespace rime { |
| 19 | + |
| 20 | +class PluginManager { |
| 21 | + public: |
| 22 | + void LoadPlugins(fs::path plugins_dir); |
| 23 | + |
| 24 | + static string plugin_name_of(fs::path plugin_file); |
| 25 | + |
| 26 | + static PluginManager& instance(); |
| 27 | + |
| 28 | + private: |
| 29 | + PluginManager() = default; |
| 30 | + |
| 31 | + map<string, boost::dll::shared_library> plugin_libs_; |
| 32 | +}; |
| 33 | + |
| 34 | +void PluginManager::LoadPlugins(fs::path plugins_dir) { |
| 35 | + const fs::perms exe_perms = (fs::owner_exe | fs::group_exe | fs::others_exe); |
| 36 | + ModuleManager& mm = ModuleManager::instance(); |
| 37 | + if (!fs::is_directory(plugins_dir)) { |
| 38 | + return; |
| 39 | + } |
| 40 | + LOG(INFO) << "loading plugins from " << plugins_dir; |
| 41 | + for (fs::directory_iterator iter(plugins_dir), end; iter != end; ++iter) { |
| 42 | + fs::path plugin_file = iter->path(); |
| 43 | + if (plugin_file.extension() == boost::dll::shared_library::suffix()) { |
| 44 | + fs::file_status plugin_file_status = fs::status(plugin_file); |
| 45 | + if (fs::is_regular_file(plugin_file_status) && |
| 46 | + fs::status(plugin_file).permissions() & exe_perms) { |
| 47 | + DLOG(INFO) << "found plugin: " << plugin_file; |
| 48 | + string plugin_name = plugin_name_of(plugin_file); |
| 49 | + if (plugin_libs_.find(plugin_name) == plugin_libs_.end()) { |
| 50 | + LOG(INFO) << "loading plugin '" << plugin_name |
| 51 | + << "' from " << plugin_file; |
| 52 | + try { |
| 53 | + auto plugin_lib = boost::dll::shared_library(plugin_file); |
| 54 | + plugin_libs_[plugin_name] = plugin_lib; |
| 55 | + } catch (const std::exception& ex) { |
| 56 | + LOG(ERROR) << "error loading plugin " << plugin_name << ": " |
| 57 | + << ex.what(); |
| 58 | + continue; |
| 59 | + } |
| 60 | + } |
| 61 | + if (RimeModule* module = mm.Find(plugin_name)) { |
| 62 | + mm.LoadModule(module); |
| 63 | + LOG(INFO) << "loaded plugin: " << plugin_name; |
| 64 | + } else { |
| 65 | + LOG(WARNING) << "module '" << plugin_name |
| 66 | + << "' is not provided by plugin library " << plugin_file; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +string PluginManager::plugin_name_of(fs::path plugin_file) { |
| 74 | + string name = plugin_file.stem().string(); |
| 75 | + // remove prefix "(lib)rime-" |
| 76 | + if (boost::starts_with(name, "librime-")) { |
| 77 | + boost::erase_first(name, "librime-"); |
| 78 | + } else if (boost::starts_with(name, "rime-")) { |
| 79 | + boost::erase_first(name, "rime-"); |
| 80 | + } |
| 81 | + // replace dash with underscore, for the plugin name is part of the module |
| 82 | + // initializer function name. |
| 83 | + boost::replace_all(name, "-", "_"); |
| 84 | + return name; |
| 85 | +} |
| 86 | + |
| 87 | +PluginManager& PluginManager::instance() { |
| 88 | + static the<PluginManager> s_instance; |
| 89 | + if (!s_instance) { |
| 90 | + s_instance.reset(new PluginManager); |
| 91 | + } |
| 92 | + return *s_instance; |
| 93 | +} |
| 94 | + |
| 95 | +} // namespace rime |
| 96 | + |
| 97 | +static void rime_plugins_initialize() { |
| 98 | + rime::PluginManager::instance().LoadPlugins(RIME_PLUGINS_DIR); |
| 99 | +} |
| 100 | + |
| 101 | +static void rime_plugins_finalize() {} |
| 102 | + |
| 103 | +RIME_REGISTER_MODULE(plugins) |
0 commit comments