2003-09-08 15:38:34 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
|
|
|
* Copyright (C) 2001 Ludvig Strigeus
|
2005-01-01 16:09:25 +00:00
|
|
|
* Copyright (C) 2001-2005 The ScummVM project
|
2003-09-08 15:38:34 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2003-09-08 15:38:34 +00:00
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-06-24 15:23:51 +00:00
|
|
|
#include "common/stdafx.h"
|
2003-10-17 12:18:58 +00:00
|
|
|
#include "backends/fs/fs.h"
|
2003-09-17 22:41:01 +00:00
|
|
|
#include "base/gameDetector.h"
|
|
|
|
#include "base/plugins.h"
|
2003-09-18 02:07:18 +00:00
|
|
|
#include "base/engine.h"
|
2003-09-18 18:23:53 +00:00
|
|
|
#include "common/util.h"
|
2003-09-08 17:13:40 +00:00
|
|
|
|
2003-10-17 12:18:58 +00:00
|
|
|
/** Type of factory functions which make new Engine objects. */
|
2003-10-02 22:52:57 +00:00
|
|
|
typedef Engine *(*EngineFactory)(GameDetector *detector, OSystem *syst);
|
|
|
|
|
2003-10-17 23:16:53 +00:00
|
|
|
typedef const char *(*NameFunc)();
|
|
|
|
typedef GameList (*TargetListFunc)();
|
2003-12-21 15:29:52 +00:00
|
|
|
typedef DetectedGameList (*DetectFunc)(const FSList &fslist);
|
2003-10-17 23:16:53 +00:00
|
|
|
|
2003-10-02 22:52:57 +00:00
|
|
|
|
2003-09-18 18:23:53 +00:00
|
|
|
#ifdef DYNAMIC_MODULES
|
|
|
|
|
|
|
|
#ifdef UNIX
|
|
|
|
#include <dlfcn.h>
|
2005-03-25 17:55:57 +00:00
|
|
|
#define PLUGIN_DIRECTORY "plugins/"
|
2004-08-22 21:41:22 +00:00
|
|
|
#else
|
|
|
|
#ifdef __DC__
|
|
|
|
#include "dcloader.h"
|
2005-08-14 13:44:20 +00:00
|
|
|
#define PLUGIN_DIRECTORY "/"
|
2005-03-25 17:55:57 +00:00
|
|
|
#define PLUGIN_PREFIX ""
|
2005-08-14 13:44:20 +00:00
|
|
|
#define PLUGIN_SUFFIX ".PLG"
|
2003-09-18 18:23:53 +00:00
|
|
|
#else
|
2005-06-05 12:55:33 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define PLUGIN_DIRECTORY ""
|
|
|
|
#define PLUGIN_PREFIX ""
|
|
|
|
#define PLUGIN_SUFFIX ".dll"
|
|
|
|
#else
|
2003-09-18 18:23:53 +00:00
|
|
|
#error No support for loading plugins on non-unix systems at this point!
|
|
|
|
#endif
|
2004-08-22 21:41:22 +00:00
|
|
|
#endif
|
2005-06-05 12:55:33 +00:00
|
|
|
#endif
|
2003-09-18 18:23:53 +00:00
|
|
|
|
2005-03-25 17:55:57 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
PluginRegistrator::PluginRegistrator(const char *name, GameList games, EngineFactory ef, DetectFunc df)
|
|
|
|
: _name(name), _ef(ef), _df(df), _games(games) {
|
|
|
|
//printf("Automatically registered plugin '%s'\n", name);
|
|
|
|
}
|
|
|
|
|
2003-09-18 18:23:53 +00:00
|
|
|
#endif
|
|
|
|
|
2003-09-17 23:05:07 +00:00
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2003-10-17 12:18:58 +00:00
|
|
|
GameSettings Plugin::findGame(const char *gameName) const {
|
|
|
|
// Find the GameSettings for this game
|
2003-10-12 18:40:12 +00:00
|
|
|
assert(gameName);
|
2003-10-17 12:18:58 +00:00
|
|
|
GameList games = getSupportedGames();
|
2003-12-13 00:20:01 +00:00
|
|
|
GameSettings result = {NULL, NULL, 0};
|
2004-02-05 00:19:57 +00:00
|
|
|
for (GameList::iterator g = games.begin(); g != games.end(); ++g) {
|
2003-12-13 00:20:01 +00:00
|
|
|
if (!scumm_stricmp(g->name, gameName)) {
|
2003-10-17 12:18:58 +00:00
|
|
|
result = *g;
|
|
|
|
break;
|
2003-09-08 17:13:40 +00:00
|
|
|
}
|
|
|
|
}
|
2003-10-17 12:18:58 +00:00
|
|
|
return result;
|
2003-09-08 17:13:40 +00:00
|
|
|
}
|
|
|
|
|
2003-10-17 23:16:53 +00:00
|
|
|
#pragma mark -
|
|
|
|
|
2003-10-26 12:44:20 +00:00
|
|
|
class StaticPlugin : public Plugin {
|
2005-11-19 17:57:24 +00:00
|
|
|
PluginRegistrator *_plugin;
|
2003-09-08 17:13:40 +00:00
|
|
|
public:
|
2005-11-19 17:57:24 +00:00
|
|
|
StaticPlugin(PluginRegistrator *plugin)
|
|
|
|
: _plugin(plugin) {
|
|
|
|
}
|
|
|
|
|
|
|
|
~StaticPlugin() {
|
|
|
|
delete _plugin;
|
2003-09-08 17:13:40 +00:00
|
|
|
}
|
|
|
|
|
2005-11-19 17:57:24 +00:00
|
|
|
const char *getName() const { return _plugin->_name; }
|
2003-09-08 17:13:40 +00:00
|
|
|
|
|
|
|
Engine *createInstance(GameDetector *detector, OSystem *syst) const {
|
2005-11-19 17:57:24 +00:00
|
|
|
return (*_plugin->_ef)(detector, syst);
|
2003-09-08 17:13:40 +00:00
|
|
|
}
|
2003-10-17 23:16:53 +00:00
|
|
|
|
2005-11-19 17:57:24 +00:00
|
|
|
GameList getSupportedGames() const { return _plugin->_games; }
|
2003-12-21 15:29:52 +00:00
|
|
|
DetectedGameList detectGames(const FSList &fslist) const {
|
2005-11-19 17:57:24 +00:00
|
|
|
return (*_plugin->_df)(fslist);
|
2003-10-17 23:16:53 +00:00
|
|
|
}
|
2003-09-08 17:13:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#pragma mark -
|
2003-09-08 15:38:34 +00:00
|
|
|
|
2003-09-18 18:23:53 +00:00
|
|
|
#ifdef DYNAMIC_MODULES
|
|
|
|
|
2003-10-26 12:44:20 +00:00
|
|
|
class DynamicPlugin : public Plugin {
|
2003-09-18 18:23:53 +00:00
|
|
|
void *_dlHandle;
|
2003-10-02 17:43:02 +00:00
|
|
|
Common::String _filename;
|
2003-09-18 18:23:53 +00:00
|
|
|
|
2003-10-02 17:43:02 +00:00
|
|
|
Common::String _name;
|
2003-09-18 18:23:53 +00:00
|
|
|
EngineFactory _ef;
|
2003-10-17 23:16:53 +00:00
|
|
|
DetectFunc _df;
|
2003-10-26 12:44:20 +00:00
|
|
|
GameList _games;
|
2003-11-08 22:05:58 +00:00
|
|
|
|
2003-09-18 18:23:53 +00:00
|
|
|
void *findSymbol(const char *symbol);
|
|
|
|
|
|
|
|
public:
|
2005-03-25 17:55:57 +00:00
|
|
|
DynamicPlugin(const Common::String &filename)
|
2003-10-26 15:58:49 +00:00
|
|
|
: _dlHandle(0), _filename(filename), _ef(0), _df(0), _games() {}
|
2003-11-08 22:05:58 +00:00
|
|
|
|
2003-09-18 18:23:53 +00:00
|
|
|
const char *getName() const { return _name.c_str(); }
|
|
|
|
|
|
|
|
Engine *createInstance(GameDetector *detector, OSystem *syst) const {
|
|
|
|
assert(_ef);
|
|
|
|
return (*_ef)(detector, syst);
|
|
|
|
}
|
|
|
|
|
2003-10-26 12:44:20 +00:00
|
|
|
GameList getSupportedGames() const { return _games; }
|
2003-12-21 15:29:52 +00:00
|
|
|
DetectedGameList detectGames(const FSList &fslist) const {
|
2003-10-17 23:16:53 +00:00
|
|
|
assert(_df);
|
|
|
|
return (*_df)(fslist);
|
|
|
|
}
|
|
|
|
|
2003-09-18 18:23:53 +00:00
|
|
|
bool loadPlugin();
|
|
|
|
void unloadPlugin();
|
|
|
|
};
|
|
|
|
|
|
|
|
void *DynamicPlugin::findSymbol(const char *symbol) {
|
2005-06-05 12:55:33 +00:00
|
|
|
#if defined(UNIX) || defined(__DC__)
|
2003-09-18 18:23:53 +00:00
|
|
|
void *func = dlsym(_dlHandle, symbol);
|
|
|
|
if (!func)
|
2004-03-23 00:12:49 +00:00
|
|
|
warning("Failed loading symbol '%s' from plugin '%s' (%s)", symbol, _filename.c_str(), dlerror());
|
2003-09-18 18:23:53 +00:00
|
|
|
return func;
|
|
|
|
#else
|
2005-06-05 12:55:33 +00:00
|
|
|
#if defined(_WIN32)
|
2005-08-28 12:19:36 +00:00
|
|
|
void *func = (void*)GetProcAddress((HMODULE)_dlHandle, symbol);
|
2005-06-05 12:55:33 +00:00
|
|
|
if (!func)
|
|
|
|
warning("Failed loading symbol '%s' from plugin '%s'", symbol, _filename.c_str());
|
|
|
|
return func;
|
|
|
|
#else
|
2003-09-18 18:23:53 +00:00
|
|
|
#error TODO
|
|
|
|
#endif
|
2005-06-05 12:55:33 +00:00
|
|
|
#endif
|
2003-09-18 18:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DynamicPlugin::loadPlugin() {
|
|
|
|
assert(!_dlHandle);
|
2005-06-05 12:55:33 +00:00
|
|
|
#if defined(UNIX) || defined(__DC__)
|
2003-09-18 18:23:53 +00:00
|
|
|
_dlHandle = dlopen(_filename.c_str(), RTLD_LAZY);
|
2003-11-08 22:05:58 +00:00
|
|
|
|
2003-09-18 18:23:53 +00:00
|
|
|
if (!_dlHandle) {
|
2003-09-19 21:36:27 +00:00
|
|
|
warning("Failed loading plugin '%s' (%s)", _filename.c_str(), dlerror());
|
2003-09-18 18:23:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2005-06-05 12:55:33 +00:00
|
|
|
#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
|
2003-11-08 22:05:58 +00:00
|
|
|
|
2003-09-18 18:23:53 +00:00
|
|
|
// Query the plugin's name
|
|
|
|
NameFunc nameFunc = (NameFunc)findSymbol("PLUGIN_name");
|
|
|
|
if (!nameFunc) {
|
|
|
|
unloadPlugin();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_name = nameFunc();
|
2003-11-08 22:05:58 +00:00
|
|
|
|
2003-09-18 18:23:53 +00:00
|
|
|
// Query the plugin for the targets it supports
|
2003-10-17 23:16:53 +00:00
|
|
|
TargetListFunc gameListFunc = (TargetListFunc)findSymbol("PLUGIN_getSupportedGames");
|
|
|
|
if (!gameListFunc) {
|
2003-09-18 18:23:53 +00:00
|
|
|
unloadPlugin();
|
|
|
|
return false;
|
|
|
|
}
|
2003-10-17 23:16:53 +00:00
|
|
|
_games = gameListFunc();
|
2003-11-08 22:05:58 +00:00
|
|
|
|
2003-10-17 23:16:53 +00:00
|
|
|
// Retrieve the factory function
|
2003-09-18 18:23:53 +00:00
|
|
|
_ef = (EngineFactory)findSymbol("PLUGIN_createEngine");
|
|
|
|
if (!_ef) {
|
|
|
|
unloadPlugin();
|
|
|
|
return false;
|
|
|
|
}
|
2003-11-08 22:05:58 +00:00
|
|
|
|
2003-10-17 23:16:53 +00:00
|
|
|
// Retrieve the detector function
|
|
|
|
_df = (DetectFunc)findSymbol("PLUGIN_detectGames");
|
|
|
|
if (!_df) {
|
|
|
|
unloadPlugin();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-08-22 21:41:22 +00:00
|
|
|
#ifdef __DC__
|
|
|
|
dlforgetsyms(_dlHandle);
|
|
|
|
#endif
|
|
|
|
|
2003-09-18 18:23:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicPlugin::unloadPlugin() {
|
2005-06-05 12:55:33 +00:00
|
|
|
#if defined(UNIX) || defined(__DC__)
|
2003-09-18 18:23:53 +00:00
|
|
|
if (_dlHandle) {
|
|
|
|
if (dlclose(_dlHandle) != 0)
|
2003-09-19 21:36:27 +00:00
|
|
|
warning("Failed unloading plugin '%s' (%s)", _filename.c_str(), dlerror());
|
2003-09-18 18:23:53 +00:00
|
|
|
}
|
|
|
|
}
|
2005-06-05 12:55:33 +00:00
|
|
|
#else
|
|
|
|
#if defined(_WIN32)
|
|
|
|
if (_dlHandle) {
|
|
|
|
if (!FreeLibrary((HMODULE)_dlHandle))
|
|
|
|
warning("Failed unloading plugin '%s'", _filename.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#error TODO
|
|
|
|
#endif
|
|
|
|
#endif
|
2003-09-18 18:23:53 +00:00
|
|
|
|
|
|
|
#endif // DYNAMIC_MODULES
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2004-12-28 20:29:42 +00:00
|
|
|
DECLARE_SINGLETON(PluginManager);
|
|
|
|
|
2003-09-08 15:38:34 +00:00
|
|
|
PluginManager::PluginManager() {
|
|
|
|
}
|
|
|
|
|
|
|
|
PluginManager::~PluginManager() {
|
|
|
|
// Explicitly unload all loaded plugins
|
|
|
|
unloadPlugins();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PluginManager::loadPlugins() {
|
2003-10-17 23:16:53 +00:00
|
|
|
#ifdef DYNAMIC_MODULES
|
2003-09-18 18:23:53 +00:00
|
|
|
// Load dynamic plugins
|
2005-07-30 21:11:48 +00:00
|
|
|
// TODO... this is right now just a nasty hack.
|
2003-09-20 00:37:09 +00:00
|
|
|
// This should search one or multiple directories for all plugins it can
|
|
|
|
// find (to this end, we maybe should use a special prefix/suffix; e.g.
|
|
|
|
// instead of libscumm.so, use scumm.engine or scumm.plugin etc.).
|
|
|
|
//
|
|
|
|
// The list of directories to search could be e.g.:
|
|
|
|
// User specified (via config file), ".", "./plugins", "$(prefix)/lib".
|
|
|
|
//
|
|
|
|
// We also need to add code which ensures what we are looking at is
|
|
|
|
// a) a ScummVM engine and b) matches the version of the executable.
|
|
|
|
// Hence one more symbol should be exported by plugins which returns
|
|
|
|
// the "ABI" version the plugin was built for, and we can compare that
|
|
|
|
// to the ABI version of the executable.
|
2003-10-17 23:16:53 +00:00
|
|
|
|
|
|
|
// Load all plugins.
|
2005-03-25 17:55:57 +00:00
|
|
|
// Scan for all plugins in this directory
|
|
|
|
FilesystemNode dir(PLUGIN_DIRECTORY);
|
2005-08-19 17:27:47 +00:00
|
|
|
if (!dir.isValid() || !dir.isDirectory()) {
|
|
|
|
error("Couldn't open plugin directory '%s'", PLUGIN_DIRECTORY);
|
|
|
|
}
|
2005-03-25 17:55:57 +00:00
|
|
|
FSList files(dir.listDir(FilesystemNode::kListFilesOnly));
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-03-25 17:55:57 +00:00
|
|
|
for (FSList::const_iterator i = files.begin(); i != files.end(); ++i) {
|
|
|
|
Common::String name(i->displayName());
|
|
|
|
if (name.hasPrefix(PLUGIN_PREFIX) && name.hasSuffix(PLUGIN_SUFFIX)) {
|
|
|
|
tryLoadPlugin(new DynamicPlugin(i->path()));
|
|
|
|
}
|
|
|
|
}
|
2003-10-17 23:16:53 +00:00
|
|
|
|
2005-09-04 16:38:38 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
#define LINK_PLUGIN(ID) \
|
|
|
|
extern PluginRegistrator *g_##ID##_PluginReg; \
|
|
|
|
extern void g_##ID##_PluginReg_alloc(); \
|
|
|
|
g_##ID##_PluginReg_alloc(); \
|
|
|
|
plugin = g_##ID##_PluginReg; \
|
2005-11-19 17:57:24 +00:00
|
|
|
tryLoadPlugin(new StaticPlugin(plugin));
|
|
|
|
|
2005-03-25 17:55:57 +00:00
|
|
|
// "Loader" for the static plugins.
|
|
|
|
// Iterate over all registered (static) plugins and load them.
|
|
|
|
PluginRegistrator *plugin;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-03-28 09:43:09 +00:00
|
|
|
#ifndef DISABLE_SCUMM
|
2005-03-25 17:55:57 +00:00
|
|
|
LINK_PLUGIN(SCUMM)
|
|
|
|
#endif
|
|
|
|
#ifndef DISABLE_SKY
|
|
|
|
LINK_PLUGIN(SKY)
|
|
|
|
#endif
|
|
|
|
#ifndef DISABLE_SWORD1
|
|
|
|
LINK_PLUGIN(SWORD1)
|
|
|
|
#endif
|
|
|
|
#ifndef DISABLE_SWORD2
|
|
|
|
LINK_PLUGIN(SWORD2)
|
|
|
|
#endif
|
|
|
|
#ifndef DISABLE_SIMON
|
|
|
|
LINK_PLUGIN(SIMON)
|
|
|
|
#endif
|
|
|
|
#ifndef DISABLE_QUEEN
|
|
|
|
LINK_PLUGIN(QUEEN)
|
|
|
|
#endif
|
|
|
|
#ifndef DISABLE_SAGA
|
|
|
|
LINK_PLUGIN(SAGA)
|
|
|
|
#endif
|
|
|
|
#ifndef DISABLE_KYRA
|
2005-03-25 22:37:34 +00:00
|
|
|
LINK_PLUGIN(KYRA)
|
2005-03-25 17:55:57 +00:00
|
|
|
#endif
|
2005-04-05 15:07:40 +00:00
|
|
|
#ifndef DISABLE_GOB
|
|
|
|
LINK_PLUGIN(GOB)
|
|
|
|
#endif
|
2004-03-14 23:37:11 +00:00
|
|
|
|
2004-04-09 12:36:06 +00:00
|
|
|
#endif
|
2003-09-08 15:38:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PluginManager::unloadPlugins() {
|
2004-08-29 19:08:08 +00:00
|
|
|
unloadPluginsExcept(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PluginManager::unloadPluginsExcept(const Plugin *plugin) {
|
|
|
|
Plugin *found = NULL;
|
2004-02-05 00:19:57 +00:00
|
|
|
uint i;
|
2003-09-08 15:38:34 +00:00
|
|
|
for (i = 0; i < _plugins.size(); i++) {
|
2004-08-29 19:08:08 +00:00
|
|
|
if (_plugins[i] == plugin) {
|
|
|
|
found = _plugins[i];
|
|
|
|
} else {
|
|
|
|
_plugins[i]->unloadPlugin();
|
|
|
|
delete _plugins[i];
|
|
|
|
}
|
2003-09-08 15:38:34 +00:00
|
|
|
}
|
|
|
|
_plugins.clear();
|
2004-08-29 19:08:08 +00:00
|
|
|
if (found != NULL) {
|
|
|
|
_plugins.push_back(found);
|
|
|
|
}
|
2003-09-08 15:38:34 +00:00
|
|
|
}
|
2003-09-18 18:23:53 +00:00
|
|
|
|
|
|
|
bool PluginManager::tryLoadPlugin(Plugin *plugin) {
|
|
|
|
assert(plugin);
|
|
|
|
// Try to load the plugin
|
|
|
|
if (plugin->loadPlugin()) {
|
2003-11-07 02:31:44 +00:00
|
|
|
// If successful, add it to the list of known plugins and return.
|
2003-09-18 18:23:53 +00:00
|
|
|
_plugins.push_back(plugin);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Failed to load the plugin
|
|
|
|
delete plugin;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2003-12-21 15:29:52 +00:00
|
|
|
|
|
|
|
DetectedGameList PluginManager::detectGames(const FSList &fslist) const {
|
|
|
|
DetectedGameList candidates;
|
|
|
|
|
|
|
|
// Iterate over all known games and for each check if it might be
|
|
|
|
// the game in the presented directory.
|
2004-02-05 00:19:57 +00:00
|
|
|
PluginList::const_iterator iter;
|
2003-12-21 15:29:52 +00:00
|
|
|
for (iter = _plugins.begin(); iter != _plugins.end(); ++iter) {
|
|
|
|
candidates.push_back((*iter)->detectGames(fslist));
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2003-12-21 15:29:52 +00:00
|
|
|
return candidates;
|
|
|
|
}
|