|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#include "tpluginmanager.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "tsystem.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "tconvert.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "tlogger.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
9f5a1b |
#ifdef _WIN32
|
|
Toshihiro Shimizu |
890ddd |
#include <windows.h></windows.h>
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// QUALE DI QUESTI SERVE VERAMENTE??
|
|
Toshihiro Shimizu |
890ddd |
#include <grp.h></grp.h>
|
|
Toshihiro Shimizu |
890ddd |
#include <utime.h></utime.h>
|
|
Toshihiro Shimizu |
890ddd |
#include <sys param.h=""></sys>
|
|
Toshihiro Shimizu |
890ddd |
#include <unistd.h></unistd.h>
|
|
Toshihiro Shimizu |
890ddd |
#include <sys types.h=""></sys>
|
|
Rozhuk Ivan |
ac51ab |
#ifndef FREEBSD
|
|
Rozhuk Ivan |
ac51ab |
#include <dirent.h></dirent.h>
|
|
Rozhuk Ivan |
ac51ab |
#endif
|
|
Toshihiro Shimizu |
890ddd |
#include <stdio.h></stdio.h>
|
|
Toshihiro Shimizu |
890ddd |
#include <unistd.h></unistd.h>
|
|
Toshihiro Shimizu |
890ddd |
#include <sys dir.h=""></sys>
|
|
Shinya Kitaoka |
120a6e |
#include <sys param.h=""> // for getfsstat</sys>
|
|
Campbell Barton |
107701 |
#ifdef MACOSX
|
|
Toshihiro Shimizu |
890ddd |
#include <sys ucred.h=""></sys>
|
|
Campbell Barton |
107701 |
#endif
|
|
Toshihiro Shimizu |
890ddd |
#include <sys mount.h=""></sys>
|
|
Toshihiro Shimizu |
890ddd |
#include <pwd.h></pwd.h>
|
|
Toshihiro Shimizu |
890ddd |
#include <dlfcn.h></dlfcn.h>
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
class TPluginManager::Plugin {
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Shinya Kitaoka |
9f5a1b |
#ifdef _WIN32
|
|
Shinya Kitaoka |
120a6e |
typedef HINSTANCE Handle;
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Shinya Kitaoka |
120a6e |
typedef void *Handle;
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
private:
|
|
Shinya Kitaoka |
120a6e |
Handle m_handle;
|
|
Shinya Kitaoka |
120a6e |
TPluginInfo m_info;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Shinya Kitaoka |
120a6e |
Plugin(Handle handle) : m_handle(handle) {}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
Handle getHandle() const { return m_handle; }
|
|
Shinya Kitaoka |
120a6e |
const TPluginInfo &getInfo() const { return m_info; }
|
|
Shinya Kitaoka |
120a6e |
void setInfo(const TPluginInfo &info) { m_info = info; }
|
|
Shinya Kitaoka |
120a6e |
std::string getName() const { return m_info.getName(); }
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
typedef const TPluginInfo *TnzLibMainProcType();
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
namespace {
|
|
Toshihiro Shimizu |
890ddd |
const char *TnzLibMainProcName = "TLibMain";
|
|
Shinya Kitaoka |
9f5a1b |
#if !defined(_WIN32)
|
|
Toshihiro Shimizu |
890ddd |
const char *TnzLibMainProcName2 = "_TLibMain";
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//=============================================================================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
TPluginManager::TPluginManager() { m_ignoreList.insert("tnzimagevector"); }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
TPluginManager::~TPluginManager() {
|
|
Shinya Kitaoka |
120a6e |
// try { unloadPlugins(); } catch(...) {}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
TPluginManager *TPluginManager::instance() {
|
|
Shinya Kitaoka |
120a6e |
static TPluginManager _instance;
|
|
Shinya Kitaoka |
120a6e |
return &_instance;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
bool TPluginManager::isIgnored(std::string name) const {
|
|
Shinya Kitaoka |
120a6e |
return m_ignoreList.count(toLower(name)) > 0;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
void TPluginManager::unloadPlugins() {
|
|
Shinya Kitaoka |
120a6e |
for (PluginTable::iterator it = m_pluginTable.begin();
|
|
Shinya Kitaoka |
120a6e |
it != m_pluginTable.end(); ++it) {
|
|
Shinya Kitaoka |
120a6e |
Plugin::Handle handle = (*it)->getHandle();
|
|
Rozhuk Ivan |
ac51ab |
#if !(defined(LINUX) || defined(FREEBSD))
|
|
Shinya Kitaoka |
9f5a1b |
#ifdef _WIN32
|
|
Shinya Kitaoka |
120a6e |
FreeLibrary(handle);
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Shinya Kitaoka |
120a6e |
dlclose(handle);
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Shinya Kitaoka |
120a6e |
delete (*it);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
m_pluginTable.clear();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
void TPluginManager::loadPlugin(const TFilePath &fp) {
|
|
Shinya Kitaoka |
120a6e |
if ((int)m_loadedPlugins.count(fp) > 0) {
|
|
Shinya Kitaoka |
120a6e |
TLogger::debug() << "Already loaded " << fp;
|
|
Shinya Kitaoka |
120a6e |
return;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
std::string name = fp.getName();
|
|
Shinya Kitaoka |
120a6e |
if (isIgnored(name)) {
|
|
Shinya Kitaoka |
120a6e |
TLogger::debug() << "Ignored " << fp;
|
|
Shinya Kitaoka |
120a6e |
return;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
TLogger::debug() << "Loading " << fp;
|
|
Shinya Kitaoka |
9f5a1b |
#ifdef _WIN32
|
|
Shinya Kitaoka |
120a6e |
Plugin::Handle handle = LoadLibraryW(fp.getWideString().c_str());
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Shinya Kitaoka |
120a6e |
Plugin::Handle handle =
|
|
Shinya Kitaoka |
120a6e |
dlopen(::to_string(fp).c_str(), RTLD_NOW); // RTLD_LAZY
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Shinya Kitaoka |
120a6e |
if (!handle) {
|
|
Shinya Kitaoka |
120a6e |
// non riesce a caricare la libreria;
|
|
Shinya Kitaoka |
120a6e |
TLogger::warning() << "Unable to load " << fp;
|
|
Shinya Kitaoka |
9f5a1b |
#ifdef _WIN32
|
|
Shinya Kitaoka |
120a6e |
std::wstring getFormattedMessage(DWORD lastError);
|
|
Shinya Kitaoka |
120a6e |
TLogger::warning() << ::to_string(getFormattedMessage(GetLastError()));
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Shinya Kitaoka |
120a6e |
TLogger::warning() << dlerror();
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
m_loadedPlugins.insert(fp);
|
|
Shinya Kitaoka |
120a6e |
Plugin *plugin = new Plugin(handle);
|
|
Shinya Kitaoka |
120a6e |
m_pluginTable.push_back(plugin);
|
|
Shinya Kitaoka |
120a6e |
// cout << "loaded" << endl;
|
|
Shinya Kitaoka |
120a6e |
TnzLibMainProcType *tnzLibMain = 0;
|
|
Shinya Kitaoka |
9f5a1b |
#ifdef _WIN32
|
|
Shinya Kitaoka |
120a6e |
tnzLibMain =
|
|
Shinya Kitaoka |
120a6e |
(TnzLibMainProcType *)GetProcAddress(handle, TnzLibMainProcName);
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Shinya Kitaoka |
120a6e |
tnzLibMain = (TnzLibMainProcType *)dlsym(handle, TnzLibMainProcName);
|
|
Shinya Kitaoka |
120a6e |
if (!tnzLibMain) // provo _ come prefisso
|
|
Shinya Kitaoka |
120a6e |
tnzLibMain = (TnzLibMainProcType *)dlsym(handle, TnzLibMainProcName2);
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
if (!tnzLibMain) {
|
|
Shinya Kitaoka |
120a6e |
// La libreria non esporta TLibMain;
|
|
Shinya Kitaoka |
120a6e |
TLogger::warning() << "Corrupted " << fp;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
9f5a1b |
#ifdef _WIN32
|
|
Shinya Kitaoka |
120a6e |
FreeLibrary(handle);
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Shinya Kitaoka |
120a6e |
dlclose(handle);
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
const TPluginInfo *info = tnzLibMain();
|
|
Shinya Kitaoka |
120a6e |
if (info) plugin->setInfo(*info);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
void TPluginManager::loadPlugins(const TFilePath &dir) {
|
|
Shinya Kitaoka |
9f5a1b |
#if defined(_WIN32)
|
|
Shinya Kitaoka |
120a6e |
const std::string extension = "dll";
|
|
Rozhuk Ivan |
ac51ab |
#elif defined(LINUX) || defined(FREEBSD) || defined(__sgi)
|
|
Shinya Kitaoka |
120a6e |
const std::string extension = "so";
|
|
Toshihiro Shimizu |
890ddd |
#elif defined(MACOSX)
|
|
Shinya Kitaoka |
120a6e |
const std::string extension = "dylib";
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
TFilePathSet dirContent = TSystem::readDirectory(dir, false);
|
|
Shinya Kitaoka |
120a6e |
if (dirContent.empty()) return;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
for (TFilePathSet::iterator it = dirContent.begin(); it != dirContent.end();
|
|
Shinya Kitaoka |
120a6e |
it++) {
|
|
Shinya Kitaoka |
120a6e |
TFilePath fp = *it;
|
|
Shinya Kitaoka |
120a6e |
if (fp.getType() != extension) continue;
|
|
Shinya Kitaoka |
120a6e |
std::wstring fullpath = fp.getWideString();
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
9f5a1b |
#ifdef _WIN32
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
bool isDebugLibrary =
|
|
Shinya Kitaoka |
120a6e |
(fullpath.find(L".d.") == fullpath.size() - (extension.size() + 3));
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#ifdef _DEBUG
|
|
Shinya Kitaoka |
120a6e |
if (!isDebugLibrary)
|
|
Toshihiro Shimizu |
890ddd |
#else
|
|
Shinya Kitaoka |
120a6e |
if (isDebugLibrary)
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Shinya Kitaoka |
120a6e |
continue;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#endif
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
try {
|
|
Shinya Kitaoka |
120a6e |
loadPlugin(fp);
|
|
Shinya Kitaoka |
120a6e |
} catch (...) {
|
|
Shinya Kitaoka |
120a6e |
TLogger::warning() << "unexpected error loading " << fp;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
void TPluginManager::loadStandardPlugins() {
|
|
Shinya Kitaoka |
120a6e |
TFilePath pluginsDir = TSystem::getDllDir() + "plugins";
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
// loadPlugins(pluginsDir + "io");
|
|
Shinya Kitaoka |
120a6e |
loadPlugins(pluginsDir + "fx");
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
void TPluginManager::setIgnoredList(const std::set<std::string> &names) {</std::string>
|
|
Shinya Kitaoka |
120a6e |
m_ignoreList.clear();
|
|
Shinya Kitaoka |
120a6e |
for (std::set<std::string>::const_iterator it = names.begin();</std::string>
|
|
Shinya Kitaoka |
120a6e |
it != names.end(); ++it)
|
|
Shinya Kitaoka |
120a6e |
m_ignoreList.insert(toLower(*it));
|
|
Toshihiro Shimizu |
890ddd |
}
|