ScummVM API documentation
dynamic-plugin.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef BACKENDS_PLUGINS_DYNAMICPLUGIN_H
23 #define BACKENDS_PLUGINS_DYNAMICPLUGIN_H
24 
25 #include "base/plugins.h"
26 #include "common/textconsole.h"
27 
28 
29 class DynamicPlugin : public Plugin {
30 protected:
31  typedef int32 (*IntFunc)();
32  typedef void (*VoidFunc)();
33  typedef PluginObject *(*GetObjectFunc)();
34 
35  virtual VoidFunc findSymbol(const char *symbol) = 0;
36 
37  const Common::Path _filename;
38 
39 public:
40  DynamicPlugin(const Common::Path &filename) :
41  _filename(filename) {}
42 
43  bool loadPlugin() override {
44  // Validate the plugin API version
45  IntFunc verFunc = (IntFunc)findSymbol("PLUGIN_getVersion");
46  if (!verFunc) {
47  unloadPlugin();
48  return false;
49  }
50  if (verFunc() != PLUGIN_VERSION) {
51  warning("Plugin uses a different API version (you have: '%d', needed is: '%d')", verFunc(), PLUGIN_VERSION);
52  unloadPlugin();
53  return false;
54  }
55 
56  // Get the type of the plugin
57  IntFunc typeFunc = (IntFunc)findSymbol("PLUGIN_getType");
58  if (!typeFunc) {
59  unloadPlugin();
60  return false;
61  }
62  _type = (PluginType)typeFunc();
63  if (_type >= PLUGIN_TYPE_MAX) {
64  warning("Plugin type unknown: %d", _type);
65  unloadPlugin();
66  return false;
67  }
68 
69  // Validate the plugin type API version
70  IntFunc typeVerFunc = (IntFunc)findSymbol("PLUGIN_getTypeVersion");
71  if (!typeVerFunc) {
72  unloadPlugin();
73  return false;
74  }
75  if (typeVerFunc() != pluginTypeVersions[_type]) {
76  warning("Plugin uses a different type API version (you have: '%d', needed is: '%d')", typeVerFunc(), pluginTypeVersions[_type]);
77  unloadPlugin();
78  return false;
79  }
80 
81  // Get the plugin's instantiator object
82  GetObjectFunc getObject = (GetObjectFunc)findSymbol("PLUGIN_getObject");
83  if (!getObject) {
84  unloadPlugin();
85  return false;
86  }
87 
88  // Get the plugin object
89  _pluginObject = getObject();
90  if (!_pluginObject) {
91  warning("Couldn't get the plugin object");
92  unloadPlugin();
93  return false;
94  }
95 
96  return true;
97  }
98 
99  void unloadPlugin() override {
100  delete _pluginObject;
101  _pluginObject = nullptr;
102  }
103 
104  Common::Path getFileName() const override {
105  return _filename;
106  }
107 };
108 
109 #endif
void warning(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: path.h:52
Definition: plugins.h:145
Definition: dynamic-plugin.h:29
Definition: plugins.h:131
Common::Path getFileName() const override
Definition: dynamic-plugin.h:104