23 #include "pluginloader.h"
26 #include "configuration/doomseekerconfig.h"
27 #include "ini/inisection.h"
28 #include "ini/inivariable.h"
29 #include "plugins/engineplugin.h"
30 #include "serverapi/masterclient.h"
37 #define dlopen(a,b) LoadLibrary(a)
38 #define dlsym(a,b) GetProcAddress(a, b)
39 #define dlclose(a) FreeLibrary(a)
40 #define dlerror() GetLastError()
42 #pragma warning(disable: 4251)
50 DClass<PluginLoader::Plugin>
64 PluginLoader::Plugin::Plugin(
unsigned int type, QString file)
68 d->library = dlopen(d->file.toAscii().constData(), RTLD_NOW);
70 if(d->library != NULL)
72 unsigned int (*doomSeekerABI)() = (
unsigned int(*)()) (dlsym(d->library,
"doomSeekerABI"));
73 if(!doomSeekerABI || doomSeekerABI() != DOOMSEEKER_ABI_VERSION)
81 if(doomSeekerInit == NULL)
87 d->info = doomSeekerInit();
88 if(!info()->data()->valid)
94 gLog << QObject::tr(
"Loaded plugin: \"%1\"!").arg(info()->data()->name);
98 gLog << QObject::tr(
"Failed to open plugin: %1").arg(file);
99 gLog << QString(
"Last error was: %1").arg(dlerror());
103 PluginLoader::Plugin::~Plugin()
110 return (
void *) dlsym(d->library, func);
118 void PluginLoader::Plugin::initConfig()
122 IniSection cfgSection = gConfig.iniSectionForPlugin(
info()->data()->name);
123 info()->setConfig(cfgSection);
127 bool PluginLoader::Plugin::isValid()
const
129 return d->library != NULL;
132 void PluginLoader::Plugin::unload()
134 if (d->library != NULL)
146 QString pluginsDirectory;
147 QList<PluginLoader::Plugin*> plugins;
157 foreach (
const QString &dir, directories)
159 d->pluginsDirectory = dir;
167 gLog << QObject::tr(
"Failed to locate plugins.");
171 PluginLoader::~PluginLoader()
173 qDeleteAll(d->plugins);
178 qDeleteAll(d->plugins);
184 if (staticInstance != NULL)
186 delete staticInstance;
187 staticInstance = NULL;
191 bool PluginLoader::filesInDir()
193 gLog << QString(
"Attempting to load plugins from directory: %1").arg(d->pluginsDirectory);
194 QDir dir(d->pluginsDirectory);
200 QStringList windowsNamesFilter;
201 windowsNamesFilter <<
"*.dll";
202 dir.setNameFilters(windowsNamesFilter);
204 foreach (
const QString& entry, dir.entryList(QDir::Files))
207 Plugin *
plugin =
new Plugin(d->type, pluginFilePath);
208 if (plugin->isValid())
209 d->plugins << plugin;
228 if (staticInstance != NULL)
230 qDebug() <<
"Attempting to re-init PluginLoader";
234 staticInstance =
new PluginLoader(MAKEID(
'E',
'N',
'G',
'N'), directories);
241 plugin->initConfig();
247 assert(staticInstance != NULL);
248 return staticInstance;
253 return d->plugins.size();
256 const QList<PluginLoader::Plugin*> &PluginLoader::plugins()
const
263 return d->plugins[index];
276 QString mangledName = QString(name).replace(
" ",
"");
277 for (
int i = 0; i < d->plugins.size(); ++i)
279 QString mangledCandidate = QString(d->plugins[i]->info()->data()->name).replace(
" ",
"");
280 if (mangledName.compare(mangledCandidate) == 0)
291 d->pluginsDirectory = pluginsDirectory;
298 return d->plugins[index];
static QString combinePaths(QString pathFront, QString pathEnd)
void resetPluginsDirectory(const QString &pluginsDirectory)
Resets the plugins directory, clearing the loaded plugins and getting new loaded plugins in the proce...
const Plugin * plugin(unsigned int index) const
Returns the requested plugin or NULL.
void clearPlugins()
Clears the plugins list.
int pluginIndexFromName(const QString &name) const
Looks for a plugin which name equals to parameter.
EnginePlugin * info(int pluginIndex) const
Convenience method - calls Plugin::info() for specified plugin.
static void deinit()
Destroys the init() instance.
static void init(const QStringList &directories)
Attempts to load plugins from given set of directories.
const unsigned int numPlugins() const
Gets the number of loaded plugins.
static PluginLoader * instance()
Accesses instance of the class after init().
void initConfig()
Inits configuration for plugins.
EnginePlugin * info() const
Main plugin interface.
const Plugin * operator[](unsigned int index) const
Returns the requested plugin or NULL.
INI section representation.
void * function(const char *func) const
Returns a pointer to the requested function or NULL.