Ticket #8397: plugin.patch

File plugin.patch, 6.8 KB (added by fingolfin, 19 years ago)
  • common.rules

    RCS file: /cvsroot/scummvm/scummvm/common.rules,v
    retrieving revision 1.16
    diff -u -d -r1.16 common.rules
     
    1010# TODO: Right now, for Mac OS X only. We either will have to generate this
    1111# via the configure script, or put in some 'if' statements to choose from
    1212# one of several build rules
    13 PLUGIN-$(MODULE) := $(MODULE)/$(PLUGIN_PREFIX)$(MODULE)$(PLUGIN_SUFFIX)
     13PLUGIN-$(MODULE) := plugins/$(PLUGIN_PREFIX)$(MODULE)$(PLUGIN_SUFFIX)
    1414$(PLUGIN-$(MODULE)): $(MODULE_OBJS) $(PLUGIN_EXTRA_DEPS)
     15        $(MKDIR) plugins
    1516        $(CXX) $(PLUGIN_LDFLAGS) $(filter-out $(PLUGIN_EXTRA_DEPS),$+) -o $@
    1617PLUGIN:=
    1718plugins: $(PLUGIN-$(MODULE))
  • base/plugins.cpp

    RCS file: /cvsroot/scummvm/scummvm/base/plugins.cpp,v
    retrieving revision 1.38
    diff -u -d -r1.38 plugins.cpp
     
    3939
    4040#ifdef UNIX
    4141#include <dlfcn.h>
    42 #define DYNAMIC_PLUGIN_PATH(name) (name "/" PLUGIN_PREFIX name PLUGIN_SUFFIX)
     42#define PLUGIN_DIRECTORY        "plugins/"
    4343#else
    4444#ifdef __DC__
    4545#include "dcloader.h"
    46 #define DYNAMIC_PLUGIN_PATH(name) (name ".plg")
     46#define PLUGIN_DIRECTORY        ""
     47#define PLUGIN_PREFIX           ""
     48#define PLUGIN_SUFFIX           ".plg"
    4749#else
    4850#error No support for loading plugins on non-unix systems at this point!
    4951#endif
    5052#endif
    5153
     54
     55#else
     56
     57PluginRegistrator *PluginRegistrator::_head = 0;
     58
     59PluginRegistrator::PluginRegistrator(const char *name, GameList games, EngineFactory ef, DetectFunc df)
     60        : _name(name), _ef(ef), _df(df), _games(games) {
     61        printf("Automatically registered plugin '%s'\n", name);
     62        _next = _head;
     63        _head = this;
     64}
     65
    5266#endif
    5367
    5468
     
    108122        void *findSymbol(const char *symbol);
    109123
    110124public:
    111         DynamicPlugin(const char *filename)
     125        DynamicPlugin(const Common::String &filename)
    112126                : _dlHandle(0), _filename(filename), _ef(0), _df(0), _games() {}
    113127
    114128        const char *getName() const                                     { return _name.c_str(); }
     
    222236        // Hence one more symbol should be exported by plugins which returns
    223237        // the "ABI" version the plugin was built for, and we can compare that
    224238        // to the ABI version of the executable.
    225         #define LOAD_MODULE(name, NAME) \
    226                 tryLoadPlugin(new DynamicPlugin(DYNAMIC_PLUGIN_PATH(name)));
    227 #else
    228         // "Loader" for the static plugins
    229         #define LOAD_MODULE(name, NAME) \
    230                 tryLoadPlugin(new StaticPlugin(name, Engine_##NAME##_gameList(), Engine_##NAME##_create, Engine_##NAME##_detectGames));
    231 #endif
    232239
    233240        // Load all plugins.
    234241        // Right now the list is hardcoded. On the long run, of course it should
    235242        // automatically be determined.
     243#if 0
     244        static const char *plugin_names[] = {
    236245#ifndef DISABLE_SCUMM
    237         LOAD_MODULE("scumm", SCUMM);
     246                "scumm",
    238247#endif
    239 
    240248#ifndef DISABLE_SIMON
    241         LOAD_MODULE("simon", SIMON);
     249                "simon",
    242250#endif
    243 
    244251#ifndef DISABLE_SKY
    245         LOAD_MODULE("sky", SKY);
     252                "sky",
    246253#endif
    247 
    248254#ifndef DISABLE_SWORD1
    249         LOAD_MODULE("sword1", SWORD1);
     255                "sword1",
    250256#endif
    251 
    252257#ifndef DISABLE_SWORD2
    253         LOAD_MODULE("sword2", SWORD2);
     258                "sword2",
    254259#endif
    255 
    256260#ifndef DISABLE_QUEEN
    257         LOAD_MODULE("queen", QUEEN);
     261                "queen",
    258262#endif
    259 
    260263#ifndef DISABLE_KYRA
    261         LOAD_MODULE("kyra", KYRA);
     264                "kyra",
    262265#endif
    263 
    264266#ifndef DISABLE_SAGA
    265         LOAD_MODULE("saga", SAGA);
     267                "saga",
     268#endif
     269                0
     270        };
     271        const char **plugin;
     272        for (plugin = plugin_names; *plugin != 0; plugin++) {
     273                Common::String path(PLUGIN_DIRECTORY);
     274                path += PLUGIN_PREFIX;
     275                path += *plugin;
     276                path += PLUGIN_SUFFIX;
     277                tryLoadPlugin(new DynamicPlugin(path));
     278        }
     279#else
     280        // Scan for all plugins in this directory
     281        FilesystemNode dir(PLUGIN_DIRECTORY);
     282        FSList files(dir.listDir(FilesystemNode::kListFilesOnly));
     283       
     284        for (FSList::const_iterator i = files.begin(); i != files.end(); ++i) {
     285                Common::String name(i->displayName());
     286                if (name.hasPrefix(PLUGIN_PREFIX) && name.hasSuffix(PLUGIN_SUFFIX)) {
     287                        tryLoadPlugin(new DynamicPlugin(i->path()));
     288                }
     289        }
     290
    266291#endif
     292
     293#else
     294        // "Loader" for the static plugins.
     295        // Iterate over all registered (static) plugins and load them.
     296        PluginRegistrator *plugin = PluginRegistrator::_head;
     297        for (; plugin != 0; plugin = plugin->_next) {
     298                tryLoadPlugin(new StaticPlugin(plugin->_name, plugin->_games, plugin->_ef, plugin->_df));
     299        }
     300#endif
     301
    267302}
    268303
    269304void PluginManager::unloadPlugins() {
  • base/plugins.h

    RCS file: /cvsroot/scummvm/scummvm/base/plugins.h,v
    retrieving revision 1.25
    diff -u -d -r1.25 plugins.h
     
    7878
    7979
    8080/**
    81  * The REGISTER_PLUGIN is a convenience macro meant to ease writing
     81 * REGISTER_PLUGIN is a convenience macro meant to ease writing
    8282 * the plugin interface for our modules. In particular, using it
    8383 * makes it possible to compile the very same code in a module
    8484 * both as a static and a dynamic plugin.
     
    8787 * @todo        on Windows, we might need __declspec(dllexport) ?
    8888 */
    8989#ifndef DYNAMIC_MODULES
    90 #define REGISTER_PLUGIN(name,gameListFactory,engineFactory,detectGames)
     90#define REGISTER_PLUGIN(name,gameListFactory,engineFactory,detectGames) \
     91        static PluginRegistrator pluginReg(name, gameListFactory(), engineFactory, detectGames);
    9192#else
    9293#define REGISTER_PLUGIN(name,gameListFactory,engineFactory,detectGames) \
    9394        extern "C" { \
     
    9899        }
    99100#endif
    100101
     102#ifndef DYNAMIC_MODULES
     103/**
     104 * The PluginRegistrator class is used by the static version of REGISTER_PLUGIN
     105 * to allow static 'plugins' to automatically register with the PluginManager.
     106 * The trick is to put a global instance of PluginRegistrator into each engine.
     107 * Then when the application is started, the constructor of each of these
     108 * objects is run. They then hook themselves into a linked list, and store
     109 * all data passed to them, which the PluginManager then can use to load
     110 * and initialize the static plugins.
     111 * Neat, isn't it ? ;-)
     112 *
     113 * Caveat: there may be systems / linkers where this trick doesn't work because
     114 * the linker doesn't invoke the object constructors. This system has currently
     115 * been tested on Mac OS X only.
     116 */
     117class PluginRegistrator {
     118        friend class PluginManager;
     119public:
     120        typedef Engine *(*EngineFactory)(GameDetector *detector, OSystem *syst);
     121        typedef DetectedGameList (*DetectFunc)(const FSList &fslist);
     122
     123protected:
     124        static PluginRegistrator *_head;
     125       
     126        PluginRegistrator *_next;
     127
     128        const char *_name;
     129        EngineFactory _ef;
     130        DetectFunc _df;
     131        GameList _games;
     132
     133public:
     134        PluginRegistrator(const char *name, GameList games, EngineFactory ef, DetectFunc df);
     135};
     136#endif
     137
    101138
    102139/** List of plugins. */
    103140typedef Common::Array<Plugin *> PluginList;