added code for loading dynamic plugins(DLLs) for windows

svn-id: r18356
This commit is contained in:
Paweł Kołodziejski 2005-06-05 12:55:33 +00:00
parent e1aec42c63
commit bc65eb8ea5
3 changed files with 50 additions and 5 deletions

View file

@ -47,9 +47,15 @@ typedef DetectedGameList (*DetectFunc)(const FSList &fslist);
#define PLUGIN_PREFIX "" #define PLUGIN_PREFIX ""
#define PLUGIN_SUFFIX ".plg" #define PLUGIN_SUFFIX ".plg"
#else #else
#ifdef _WIN32
#define PLUGIN_DIRECTORY ""
#define PLUGIN_PREFIX ""
#define PLUGIN_SUFFIX ".dll"
#else
#error No support for loading plugins on non-unix systems at this point! #error No support for loading plugins on non-unix systems at this point!
#endif #endif
#endif #endif
#endif
#else #else
@ -138,24 +144,44 @@ public:
}; };
void *DynamicPlugin::findSymbol(const char *symbol) { void *DynamicPlugin::findSymbol(const char *symbol) {
#if defined(UNIX)||defined(__DC__) #if defined(UNIX) || defined(__DC__)
void *func = dlsym(_dlHandle, symbol); void *func = dlsym(_dlHandle, symbol);
if (!func) if (!func)
warning("Failed loading symbol '%s' from plugin '%s' (%s)", symbol, _filename.c_str(), dlerror()); warning("Failed loading symbol '%s' from plugin '%s' (%s)", symbol, _filename.c_str(), dlerror());
return func; return func;
#else #else
#if defined(_WIN32)
void *func = GetProcAddress((HMODULE)_dlHandle, symbol);
if (!func)
warning("Failed loading symbol '%s' from plugin '%s'", symbol, _filename.c_str());
return func;
#else
#error TODO #error TODO
#endif #endif
#endif
} }
bool DynamicPlugin::loadPlugin() { bool DynamicPlugin::loadPlugin() {
assert(!_dlHandle); assert(!_dlHandle);
#if defined(UNIX) || defined(__DC__)
_dlHandle = dlopen(_filename.c_str(), RTLD_LAZY); _dlHandle = dlopen(_filename.c_str(), RTLD_LAZY);
if (!_dlHandle) { if (!_dlHandle) {
warning("Failed loading plugin '%s' (%s)", _filename.c_str(), dlerror()); warning("Failed loading plugin '%s' (%s)", _filename.c_str(), dlerror());
return false; return false;
} }
#else
#if defined(_WIN32)
_dlHandle = LoadLibrary(_filename.c_str());
if (!_dlHandle) {
warning("Failed loading plugin '%s'", _filename.c_str());
return false;
}
#else
#error TODO
#endif
#endif
// Query the plugin's name // Query the plugin's name
NameFunc nameFunc = (NameFunc)findSymbol("PLUGIN_name"); NameFunc nameFunc = (NameFunc)findSymbol("PLUGIN_name");
@ -195,11 +221,23 @@ bool DynamicPlugin::loadPlugin() {
} }
void DynamicPlugin::unloadPlugin() { void DynamicPlugin::unloadPlugin() {
#if defined(UNIX) || defined(__DC__)
if (_dlHandle) { if (_dlHandle) {
if (dlclose(_dlHandle) != 0) if (dlclose(_dlHandle) != 0)
warning("Failed unloading plugin '%s' (%s)", _filename.c_str(), dlerror()); warning("Failed unloading plugin '%s' (%s)", _filename.c_str(), dlerror());
} }
} }
#else
#if defined(_WIN32)
if (_dlHandle) {
if (!FreeLibrary((HMODULE)_dlHandle))
warning("Failed unloading plugin '%s'", _filename.c_str());
}
}
#else
#error TODO
#endif
#endif
#endif // DYNAMIC_MODULES #endif // DYNAMIC_MODULES

View file

@ -92,10 +92,10 @@ public:
#else #else
#define REGISTER_PLUGIN(ID,name) \ #define REGISTER_PLUGIN(ID,name) \
extern "C" { \ extern "C" { \
const char *PLUGIN_name() { return name; } \ PLUGIN_EXPORT const char *PLUGIN_name() { return name; } \
GameList PLUGIN_getSupportedGames() { return Engine_##ID##_gameList(); } \ PLUGIN_EXPORT GameList PLUGIN_getSupportedGames() { return Engine_##ID##_gameList(); } \
Engine *PLUGIN_createEngine(GameDetector *detector, OSystem *syst) { return Engine_##ID##_create(detector, syst); } \ PLUGIN_EXPORT Engine *PLUGIN_createEngine(GameDetector *detector, OSystem *syst) { return Engine_##ID##_create(detector, syst); } \
DetectedGameList PLUGIN_detectGames(const FSList &fslist) { return Engine_##ID##_detectGames(fslist); } \ PLUGIN_EXPORT DetectedGameList PLUGIN_detectGames(const FSList &fslist) { return Engine_##ID##_detectGames(fslist); } \
} }
#endif #endif

View file

@ -53,6 +53,7 @@
#define FORCEINLINE __forceinline #define FORCEINLINE __forceinline
#define NORETURN _declspec(noreturn) #define NORETURN _declspec(noreturn)
#define PLUGIN_EXPORT __declspec(dllexport)
typedef unsigned char byte; typedef unsigned char byte;
typedef unsigned char uint8; typedef unsigned char uint8;
@ -92,6 +93,8 @@
#define START_PACK_STRUCTS pack (push, 1) #define START_PACK_STRUCTS pack (push, 1)
#define END_PACK_STRUCTS pack(pop) #define END_PACK_STRUCTS pack(pop)
#define PLUGIN_EXPORT __declspec(dllexport)
#elif defined(UNIX) #elif defined(UNIX)
#define scumm_stricmp strcasecmp #define scumm_stricmp strcasecmp
@ -322,6 +325,10 @@
#define CDECL #define CDECL
#endif #endif
#ifndef PLUGIN_EXPORT
#define PLUGIN_EXPORT
#endif
#ifndef NORETURN #ifndef NORETURN
#define NORETURN #define NORETURN
#endif #endif