13 #include <sys/types.h> 15 #include "PluginAPI.h" 18 #define OUTPUT_LOG printf 24 class ClassDescriptor;
25 class IParaEngineCore;
31 typedef const char* (*lpFnLibDescription)();
45 typedef void (STDCALL *pfnEnsureInit)(void);
46 typedef void (STDCALL *pfnForceTerm)(void);
56 : m_pFuncActivate(NULL), m_hDLL(NULL), m_bIsValid(false), m_bIsInitialized(false)
60 m_sDllFilePath = filename;
75 return (m_hDLL!=NULL);
83 return m_sDllFilePath.c_str();
95 return m_listClassDesc[i];
107 return m_sLibDescription.c_str();
115 return (
int)m_listClassDesc.size();
123 void Init(
const char* sFilename)
125 m_bIsInitialized =
true;
130 m_sDllFilePath = sFilename;
132 string sDLLPath = m_sDllFilePath;
136 m_hDLL = (HINSTANCE)LoadLibraryS(sDLLPath.c_str());
138 m_hDLL = LoadLibraryS(sDLLPath.c_str(), RTLD_LOCAL | RTLD_LAZY);
148 pfnEnsureInit pfnDll= (pfnEnsureInit) GetProcAddressS(m_hDLL,
"DllEnsureInit");
159 if (pLibDescription != 0)
162 m_sLibDescription = pLibDescription();
168 OUTPUT_LOG(
"failed loading %s : because it does not expose the LibDescription method\r\n", sDLLPath.c_str());
170 const char* sErrorMsg = dlerror();
172 sErrorMsg =
"unknown error";
173 OUTPUT_LOG(
"warning: GetProcAddress( %s ) failed because %s\n", sDLLPath.c_str(), sErrorMsg);
180 if (pLibVersion != 0)
183 m_nParaEngineVersion = pLibVersion();
188 OUTPUT_LOG(
"failed loading %s : because it does not expose the LibVersion method\r\n", sDLLPath.c_str());
190 const char* sErrorMsg = dlerror();
192 sErrorMsg =
"unknown error";
193 OUTPUT_LOG(
"warning: GetProcAddress( %s ) failed because %s\n", sDLLPath.c_str(), sErrorMsg);
201 if (pLibNumberClasses != 0)
204 nClassNum= pLibNumberClasses();
206 m_listClassDesc.reserve(nClassNum);
211 OUTPUT_LOG(
"failed loading %s : because it does not expose the LibNumberClasses method\r\n", sDLLPath.c_str());
213 const char* sErrorMsg = dlerror();
215 sErrorMsg =
"unknown error";
216 OUTPUT_LOG(
"warning: GetProcAddress( %s ) failed because %s\n", sDLLPath.c_str(), sErrorMsg);
222 if (pLibDescription != 0)
225 for (
int i=0; i<nClassNum; ++i)
230 m_listClassDesc.push_back(pClassDesc);
234 OUTPUT_LOG(
"the %d th class in %s is not loaded \r\n", i, sDLLPath.c_str());
241 OUTPUT_LOG(
"failed loading %s : because it does not expose the LibClassDesc method\r\n", sDLLPath.c_str());
243 const char* sErrorMsg = dlerror();
245 sErrorMsg =
"unknown error";
246 OUTPUT_LOG(
"warning: GetProcAddress( %s ) failed because %s\n", sDLLPath.c_str(), sErrorMsg);
254 if (pLibDescription != 0)
261 m_pFuncActivate = (
lpFnLibActivate)GetProcAddressS(m_hDLL,
"LibActivate");
275 OUTPUT_LOG(
"Failed loading plug-in: %s\r\n", sDLLPath.c_str());
287 pfnForceTerm pfnDll=( pfnForceTerm) GetProcAddressS(m_hDLL,
"DllForceTerm");
294 FreeLibraryS(m_hDLL);
309 if(
IsValid() && m_pFuncActivate!=0)
311 return m_pFuncActivate(nType, pVoid);
321 static void* LoadLibraryS(
const char *pcDllname,
int iMode=2)
323 std::string sDllName = pcDllname;
324 #ifdef WIN32 // Microsoft compiler 325 if(sDllName.find(
".") == string::npos)
327 return (
void*)::LoadLibraryA(pcDllname);
329 if(sDllName.find(
".") == string::npos)
331 return dlopen(sDllName.c_str(),iMode);
335 static void * GetProcAddressS(
void *Lib,
const char *Fnname)
337 #ifdef WIN32 // Microsoft compiler 340 return dlsym(Lib,Fnname);
344 static bool FreeLibraryS(
void *hDLL)
346 #ifdef WIN32 // Microsoft compiler 347 return ::FreeLibrary((HINSTANCE)hDLL);
349 return dlclose(hDLL);
355 string m_sDllFilePath;
357 vector<ClassDescriptor*> m_listClassDesc;
362 string m_sLibDescription;
368 unsigned long m_nParaEngineVersion;
373 bool m_bIsInitialized;
ClassDescriptor *(* lpFnLibClassDesc)(int i)
"LibClassDesc": must be implemented in a plug-in.
Definition: PluginLoader.hpp:37
CPluginLoader(const char *filename=NULL)
the dll to load, it does not load it immediately.
Definition: PluginLoader.hpp:55
PE_CORE_DECL void * GetProcAddress(void *Lib, const char *Fnname)
Definition: os_calls.cpp:99
int(* lpFnLibNumberClasses)()
"LibNumberClasses": must be implemented in a plug-in.
Definition: PluginLoader.hpp:35
void(* lpFnLibInitParaEngine)(IParaEngineCore *pIParaEngineCore)
"LibInitParaEngine": this is optional in a plug-in
Definition: PluginLoader.hpp:41
different physics engine has different winding order.
Definition: EventBinding.h:32
a DLL/so plug-in loaded explicitly at runtime.
Definition: PluginLoader.hpp:51
const char * GetDLLFilePath()
Definition: PluginLoader.hpp:81
unsigned long(* lpFnLibVersion)()
"LibVersion": must be implemented in a plug-in.
Definition: PluginLoader.hpp:33
void Init(const char *sFilename)
init the asset entity object.
Definition: PluginLoader.hpp:123
a table of virtual functions which are used by plug-ins to access the game engine ...
Definition: IParaEngineCore.h:17
const char *(* lpFnLibDescription)()
"LibDescription": must be implemented in a plug-in.
Definition: PluginLoader.hpp:31
int(* lpFnLibActivate)(int nType, void *pVoid)
"LibActivate": this is optional in a plug-in
Definition: PluginLoader.hpp:43
bool IsValid()
whether the plugin loaded is valid.
Definition: PluginLoader.hpp:73
int Activate(int nType=0, void *pVoid=NULL)
Activate the DLL.
Definition: PluginLoader.hpp:307
int GetNumberOfClasses()
return the number of plugin classes inside the DLL
Definition: PluginLoader.hpp:113
const char * GetLibDescription()
When a plugin file is loaded that contains an entity that the system does not have access to (i...
Definition: PluginLoader.hpp:105
void UnLoad()
Free the library.
Definition: PluginLoader.hpp:280
void(* lpFnLibInit)()
"LibInit": this is optional in a plug-in
Definition: PluginLoader.hpp:39
ClassDescriptor * GetClassDescriptor(int i)
The plugin must provide the system with a way to retrieve the Class Descriptors defined by the plugin...
Definition: PluginLoader.hpp:92
System keeps a list of the DLL's found on startup.
Definition: PluginAPI.h:61