cAudio 2.3.0
3d Audio Engine
Loading...
Searching...
No Matches
cPluginManager.cpp
1// Copyright (c) 2008-2011 Raynaldo (Wildicv) Rivera, Joshua (Dark_Kilauea) Jones, Murat (wolfmanfx) Sari
2// This file is part of the "cAudio Engine"
3// For conditions of distribution and use, see copyright notice in cAudio.h
4
5#include "cPluginManager.h"
6#include "cUtils.h"
7#include "cAudioPlatform.h"
8#include "cAudioDefines.h"
9#include "ILogger.h"
10#include "cAudio.h"
11
12#ifdef CAUDIO_COMPILE_WITH_PLUGIN_SUPPORT
13
14namespace cAudio
15{
16 typedef IAudioPlugin* (*GetPluginModule)(const char* version);
17
18 cPluginManager::cPluginManager()
19 {
20 autoLoadPlugins();
21 }
22
23 cPluginManager::~cPluginManager()
24 {
25 DynamicallyLoadedPluginsIterator it;
26 for(it = DynamicallyLoadedPlugins.begin(); it != DynamicallyLoadedPlugins.end(); it++)
27 {
28 //Found a plugin we loaded from the filesystem, unload it and delete the plugin
29 it->first->drop();
30 if(DYNLIB_UNLOAD(it->second))
31 {
32 getLogger()->logError("cPluginManager", "Plugin Error: %s.", toUTF8(getError()));
33 }
34 }
35 }
36
37 bool cPluginManager::installPlugin(IAudioPlugin* plugin, const char* name)
38 {
39 if(plugin)
40 {
41 cAudioString theName = fromUTF8(name);
42 if(theName.empty())
43 theName = plugin->getPluginName();
44
45 if(plugin->installPlugin(getLogger()))
46 {
47 RegisteredPlugins[theName] = plugin;
48 return true;
49 }
50 }
51 return false;
52 }
53
54 bool cPluginManager::installPlugin(const char* filename, const char* name)
55 {
56 DYNLIB_HANDLE m_hInst = DYNLIB_LOAD(filename);
57 if(m_hInst)
58 {
59 GetPluginModule moduleFunc = (GetPluginModule)DYNLIB_GETSYM(m_hInst, "GetPluginModule");
60
61 if(moduleFunc)
62 {
63 IAudioPlugin* plugin = moduleFunc(CAUDIO_VERSION);
64
65 if(plugin)
66 {
67 DynamicallyLoadedPlugins[plugin] = m_hInst;
68
69 return installPlugin(plugin, name);
70 }
71 }
72 else
73 getLogger()->logError("cPluginManager", "installPlugin Error: %s.", toUTF8(getError()));
74 }
75 return false;
76 }
77
78 bool cPluginManager::checkForPlugin(const char* name)
79 {
80 return (RegisteredPlugins.find(name) != RegisteredPlugins.end());
81 }
82
83 IAudioPlugin* cPluginManager::getPlugin(const char* name)
84 {
85 if(RegisteredPlugins.find(name) != RegisteredPlugins.end())
86 {
87 return RegisteredPlugins[name];
88 }
89 return NULL;
90 }
91
92 unsigned int cPluginManager::getPluginCount()
93 {
94 return RegisteredPlugins.size();
95 }
96
97 cAudioVector<IAudioPlugin*>::Type cPluginManager::getPluginList()
98 {
99 cAudioVector<IAudioPlugin*>::Type list;
100 RegisteredPluginsIterator it;
101 for(it = RegisteredPlugins.begin(); it != RegisteredPlugins.end(); it++)
102 {
103 list.push_back(it->second);
104 }
105 return list;
106 }
107
108 void cPluginManager::uninstallPlugin(IAudioPlugin* plugin)
109 {
110 if(plugin)
111 {
112 RegisteredPluginsIterator it;
113 for(it = RegisteredPlugins.begin(); it != RegisteredPlugins.end(); it++)
114 {
115 if(it->second == plugin)
116 {
117 RegisteredPlugins.erase(it->first);
118 break;
119 }
120 }
121
122 DynamicallyLoadedPluginsIterator it2 = DynamicallyLoadedPlugins.find(plugin);
123 if(it2 != DynamicallyLoadedPlugins.end())
124 {
125 //Found a plugin we loaded from the filesystem, unload it and delete the plugin
126 it2->first->drop();
127 if(DYNLIB_UNLOAD(it2->second))
128 {
129 getLogger()->logError("cPluginManager", "Plugin Error: %s.", toUTF8(getError()));
130 }
131 DynamicallyLoadedPlugins.erase(it2->first);
132 }
133 }
134 }
135
136 void cPluginManager::uninstallPlugin(const char* name)
137 {
138 if(RegisteredPlugins.find(name) != RegisteredPlugins.end())
139 {
140 uninstallPlugin(RegisteredPlugins[name]);
141 }
142 }
143
144 void cPluginManager::autoLoadPlugins()
145 {
146 cAudioVector<cAudioString>::Type fileList = getFilesInDirectory(".");
147 for(size_t i=0; i<fileList.size(); ++i)
148 {
149 if(fileList[i].substr(0, 4) == _CTEXT("cAp_") ||fileList[i].substr(0, 7) == _CTEXT("libcAp_") )
150 {
151#ifdef CAUDIO_PLATFORM_WIN
152 if(fileList[i].substr(fileList[i].length()-4, 4) == _CTEXT(".dll"))
153#elif defined(CAUDIO_PLATFORM_LINUX)
154 if(fileList[i].substr(fileList[i].length()-3, 3) == ".so")
155#elif defined(CAUDIO_PLATFORM_MAC)
156 if(fileList[i].substr(fileList[i].length()-6, 6) == ".dylib")
157#endif
158 {
159 //Found a plugin, load it
160 installPlugin(toUTF8(cAudioString(_CTEXT("./") + fileList[i])), fileList[i].c_str());
161 }
162 }
163 }
164 }
165
166 cAudioString cPluginManager::getError()
167 {
168#ifdef CAUDIO_PLATFORM_WIN
169 LPVOID lpMsgBuf;
170 FormatMessage(
171 FORMAT_MESSAGE_ALLOCATE_BUFFER |
172 FORMAT_MESSAGE_FROM_SYSTEM |
173 FORMAT_MESSAGE_IGNORE_INSERTS,
174 NULL,
175 GetLastError(),
176 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
177 (LPTSTR) &lpMsgBuf,
178 0,
179 NULL
180 );
181
182 cAudioString ret = fromUTF8((char*)lpMsgBuf);
183 // remove line break
184 ret = ret.substr(0, ret.length()-2);
185 LocalFree(lpMsgBuf);
186 return ret;
187
188#elif defined(CAUDIO_PLATFORM_MAC) || defined(CAUDIO_PLATFORM_LINUX)
189 const char* error = dlerror();
190
191 return error != NULL ? cAudioString(error) : cAudioString("");
192#else
193 return cAudioString("");
194#endif
195 }
196
197 CAUDIO_API IPluginManager* getPluginManager()
198 {
199 return cPluginManager::Instance();
200 }
201
202};
203
204#endif
virtual void logError(const char *sender, const char *msg,...)=0
Used to log an error message to the logging system.
Main namespace for the entire cAudio library.
Definition: cAudioCapture.h:16
CAUDIO_API IPluginManager * getPluginManager()
Gets the interface to the plugin manager.
CAUDIO_API ILogger * getLogger()
Gets the interface to the logger.
Definition: cAudio.cpp:45
cAudioVector< cAudioString >::Type getFilesInDirectory(cAudioString path)
Returns a list of files/directories in the supplied directory. Used internally for auto-installation ...
Definition: cUtils.h:33