Ticket #8795: plugins_version.patch

File plugins_version.patch, 3.7 KB (added by jvprat, 16 years ago)
  • base/plugins.h

     
    3333#include "common/util.h"
    3434#include "base/game.h"
    3535
     36#define PLUGIN_VERSION 1
     37
     38enum PluginType {
     39        PLUGIN_TYPE_ENGINE = 0,
     40        PLUGIN_TYPE_SCALER,
     41        PLUGIN_TYPE_MIDI,
     42
     43        PLUGIN_TYPE_MAX
     44};
     45
     46#define PLUGIN_TYPE_ENGINE_VERSION 1
     47#define PLUGIN_TYPE_SCALER_VERSION 1
     48#define PLUGIN_TYPE_MIDI_VERSION 1
     49
     50extern int pluginTypeVersions[PLUGIN_TYPE_MAX];
     51
    3652class Engine;
    3753class FSList;
    3854class MetaEngine;
     
    5268
    5369        virtual const char *getName() const = 0;
    5470        virtual const char *getCopyright() const = 0;
    55 //      virtual int getVersion() const  { return 0; }   // TODO!
    5671
    5772        virtual GameList getSupportedGames() const = 0;
    5873        virtual GameDescriptor findGame(const char *gameid) const = 0;
     
    7287 */
    7388
    7489#ifndef DYNAMIC_MODULES
    75 #define REGISTER_PLUGIN(ID,METAENGINE) \
     90#define REGISTER_PLUGIN(ID,TYPE,METAENGINE) \
     91        int g_##ID##_type = TYPE; \
    7692        MetaEngine *g_##ID##_MetaEngine_alloc() { \
    7793                return new METAENGINE(); \
    7894        } \
    7995        void dummyFuncToAllowTrailingSemicolon()
    8096#else
    81 #define REGISTER_PLUGIN(ID,METAENGINE) \
     97#define REGISTER_PLUGIN(ID,TYPE,METAENGINE) \
    8298        extern "C" { \
     99                PLUGIN_EXPORT int PLUGIN_getVersion() { return PLUGIN_VERSION; } \
     100                PLUGIN_EXPORT int PLUGIN_getType() { return TYPE; } \
     101                PLUGIN_EXPORT int PLUGIN_getTypeVersion() { return TYPE##_VERSION; } \
    83102                PLUGIN_EXPORT MetaEngine *PLUGIN_MetaEngine_alloc() { \
    84103                        return new METAENGINE(); \
    85104                } \
  • base/plugins.cpp

     
    2727#include "common/util.h"
    2828#include "engines/metaengine.h"
    2929
     30int pluginTypeVersions[PLUGIN_TYPE_MAX] = {
     31        PLUGIN_TYPE_ENGINE_VERSION,
     32        PLUGIN_TYPE_SCALER_VERSION,
     33        PLUGIN_TYPE_MIDI_VERSION
     34};
    3035
    3136#ifndef DYNAMIC_MODULES
    3237class StaticPlugin : public Plugin {
     
    8186                PluginList pl;
    8287
    8388                #define LINK_PLUGIN(ID) \
     89                        extern int *g_##ID##_type; \
    8490                        extern MetaEngine *g_##ID##_MetaEngine_alloc(); \
    8591                        pl.push_back(new StaticPlugin(g_##ID##_MetaEngine_alloc()));
    8692
  • backends/plugins/dynamic-plugin.h

     
    3232
    3333class DynamicPlugin : public Plugin {
    3434protected:
     35        typedef int (*IntFunc)();
     36
    3537        typedef void (*VoidFunc)();
    3638
    3739        typedef MetaEngine *(*MetaAllocFunc)();
     
    7173        }
    7274
    7375        virtual bool loadPlugin() {
     76                // Validate the plugin API version
     77                IntFunc verFunc = (IntFunc)findSymbol("PLUGIN_getVersion");
     78                if (!verFunc) {
     79                        unloadPlugin();
     80                        return false;
     81                }
     82                if (verFunc() != PLUGIN_VERSION) {
     83                        unloadPlugin();
     84                        return false;
     85                }
     86
     87                // Get the type of the plugin
     88                IntFunc typeFunc = (IntFunc)findSymbol("PLUGIN_getType");
     89                if (!typeFunc) {
     90                        unloadPlugin();
     91                        return false;
     92                }
     93                int type = typeFunc();
     94                if (type >= PLUGIN_TYPE_MAX) {
     95                        unloadPlugin();
     96                        return false;
     97                }
     98
     99                // Validate the plugin type API version
     100                IntFunc typeVerFunc = (IntFunc)findSymbol("PLUGIN_getTypeVersion");
     101                if (!typeVerFunc) {
     102                        unloadPlugin();
     103                        return false;
     104                }
     105                if (typeVerFunc() != pluginTypeVersions[type]) {
     106                        unloadPlugin();
     107                        return false;
     108                }
     109
    74110                // Query the plugin's name
    75111                MetaAllocFunc metaAlloc = (MetaAllocFunc)findSymbol("PLUGIN_MetaEngine_alloc");
    76112                if (!metaAlloc) {
     
    78114                        return false;
    79115                }
    80116
     117                // Allocate the metaengine
    81118                _metaengine = metaAlloc();
    82119                if (!_metaengine) {
    83120                        unloadPlugin();