29 #include <Pluma/PluginManager.hpp>
30 #include <Pluma/DLibrary.hpp>
31 #include <Pluma/Dir.hpp>
50 std::string plugName = getPluginName(path);
51 std::string realPath = resolvePathExtension(path);
53 if (!lib)
return false;
55 fnRegisterPlugin* registerFunction;
56 registerFunction =
reinterpret_cast<fnRegisterPlugin*
>(lib->
getSymbol(
"connect"));
58 if(!registerFunction){
59 fprintf(stderr,
"Failed to initialize plugin \"%s\": connect function not found\n", plugName.c_str());
64 if (!registerFunction(host)){
66 fprintf(stderr,
"Self registry failed on plugin \"%s\".\n", plugName.c_str());
67 host.cancelAddictions();
72 if (host.confirmAddictions())
73 libraries[plugName] = lib;
76 fprintf(stderr,
"Nothing registered by plugin \"%s\".\n", plugName.c_str());
87 return load(pluginName);
88 else if (folder[folder.size()-1] ==
'/' || folder[folder.size()-1] ==
'\\')
89 return load(folder + pluginName);
90 return load(folder +
'/' + pluginName);
96 std::list<std::string> files;
97 dir::listFiles(files, folder, PLUMA_LIB_EXTENSION, recursive);
100 std::list<std::string>::const_iterator it;
101 for (it = files.begin() ; it != files.end() ; ++it){
102 if (
load(*it) ) ++res;
110 std::string plugName = getPluginName(pluginName);
111 LibMap::iterator it = libraries.find(plugName);
112 if( it != libraries.end() ) {
124 host.clearProviders();
126 for (it = libraries.begin() ; it != libraries.end() ; ++it){
134 std::string PluginManager::getPluginName(
const std::string& path){
135 size_t lastDash = path.find_last_of(
"/\\");
136 size_t lastDot = path.find_last_of(
'.');
137 if (lastDash == std::string::npos) lastDash = 0;
139 if (lastDot < lastDash || lastDot == std::string::npos){
141 lastDot = path.length();
143 return path.substr(lastDash, lastDot-lastDash);
148 std::string PluginManager::resolvePathExtension(
const std::string& path){
149 size_t lastDash = path.find_last_of(
"/\\");
150 size_t lastDot = path.find_last_of(
'.');
151 if (lastDash == std::string::npos) lastDash = 0;
153 if (lastDot < lastDash || lastDot == std::string::npos){
155 return path +
"." + PLUMA_LIB_EXTENSION;
163 host.registerType(type, version, lowestVersion);
169 if (provider == NULL){
170 fprintf(stderr,
"Trying to add null provider\n");
173 return host.registerProvider(provider);
179 pluginNames.reserve(pluginNames.size()+libraries.size());
180 LibMap::const_iterator it;
181 for(it = libraries.begin() ; it != libraries.end() ; ++it){
182 pluginNames.push_back(&(it->first));
189 return libraries.find(getPluginName(pluginName)) != libraries.end();
195 return host.getProviders(type);
bool unload(const std::string &pluginName)
Unload a plugin.
void registerType(const std::string &type, unsigned int version, unsigned int lowestVersion)
Register a provider type.
void unloadAll()
Unload all loaded plugins.
PluginManager()
Default constructor.
Interface to provide applications with objects from plugins.
const std::list< Provider * > * getProviders(const std::string &type) const
Get providers of a certain type.
static DLibrary * load(const std::string &path)
Load a library.
Manages a Dynamic Linking Library.
int loadFromFolder(const std::string &folder, bool recursive=false)
Load all plugins from a given folder.
void * getSymbol(const std::string &symbol)
Get a symbol from the library.
bool load(const std::string &path)
Load a plugin given it's path.
~PluginManager()
Destructor.
bool isLoaded(const std::string &pluginName) const
Check if a plug-in is loaded.
void getLoadedPlugins(std::vector< const std::string * > &pluginNames) const
Get the name of all loaded plugins.
bool addProvider(Provider *provider)
Directly add a new provider.