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
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-11-03 22:28:37 +00:00
|
|
|
#include "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-03-25 17:55:57 +00:00
|
|
|
#define PLUGIN_DIRECTORY ""
|
|
|
|
#define PLUGIN_PREFIX ""
|
|
|
|
#define PLUGIN_SUFFIX ".plg"
|
2003-09-18 18:23:53 +00:00
|
|
|
#else
|
|
|
|
#error No support for loading plugins on non-unix systems at this point!
|
|
|
|
#endif
|
2004-08-22 21:41:22 +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 {
|
2003-09-08 17:13:40 +00:00
|
|
|
const char *_name;
|
|
|
|
EngineFactory _ef;
|
2003-10-17 23:16:53 +00:00
|
|
|
DetectFunc _df;
|
2003-10-26 12:44:20 +00:00
|
|
|
GameList _games;
|
2003-09-08 17:13:40 +00:00
|
|
|
public:
|
2003-10-17 23:16:53 +00:00
|
|
|
StaticPlugin(const char *name, GameList games, EngineFactory ef, DetectFunc df)
|
2003-10-26 12:44:20 +00:00
|
|
|
: _name(name), _ef(ef), _df(df), _games(games) {
|
2003-09-08 17:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *getName() const { return _name; }
|
|
|
|
|
|
|
|
Engine *createInstance(GameDetector *detector, OSystem *syst) const {
|
|
|
|
return (*_ef)(detector, syst);
|
|
|
|
}
|
2003-10-17 23:16:53 +00:00
|
|
|
|
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
|
|
|
return (*_df)(fslist);
|
|
|
|
}
|
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) {
|
2004-08-22 21:41:22 +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
|
|
|
|
#error TODO
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DynamicPlugin::loadPlugin() {
|
|
|
|
assert(!_dlHandle);
|
|
|
|
_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;
|
|
|
|
}
|
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() {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
// 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);
|
|
|
|
FSList files(dir.listDir(FilesystemNode::kListFilesOnly));
|
|
|
|
|
|
|
|
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-03-25 17:55:57 +00:00
|
|
|
#else
|
|
|
|
#define LINK_PLUGIN(ID) \
|
|
|
|
extern PluginRegistrator g_##ID##_PluginReg; \
|
|
|
|
plugin = &g_##ID##_PluginReg; \
|
|
|
|
tryLoadPlugin(new StaticPlugin(plugin->_name, plugin->_games, plugin->_ef, plugin->_df));
|
|
|
|
|
|
|
|
// "Loader" for the static plugins.
|
|
|
|
// Iterate over all registered (static) plugins and load them.
|
|
|
|
PluginRegistrator *plugin;
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
return candidates;
|
|
|
|
}
|