From 21421d2e86cf44a3236dd39da847ee15b366d95f Mon Sep 17 00:00:00 2001 From: Marcus Comstedt Date: Wed, 20 Apr 2011 22:30:01 +0200 Subject: DC: Move dynamic plugin handling into the platform --- backends/platform/dc/Makefile | 2 +- backends/platform/dc/dc.h | 17 ++++++- backends/platform/dc/dcmain.cpp | 3 +- backends/platform/dc/module.mk | 2 +- backends/platform/dc/plugins.cpp | 100 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 backends/platform/dc/plugins.cpp (limited to 'backends/platform') diff --git a/backends/platform/dc/Makefile b/backends/platform/dc/Makefile index 637f474b4b..11d9421a0a 100644 --- a/backends/platform/dc/Makefile +++ b/backends/platform/dc/Makefile @@ -65,7 +65,7 @@ ENABLE_TOUCHE = $(ENABLED) ENABLE_TUCKER = $(ENABLED) OBJS := dcmain.o time.o display.o audio.o input.o selector.o icon.o \ - label.o vmsave.o softkbd.o dcloader.o cache.o dc-fs.o + label.o vmsave.o softkbd.o dcloader.o cache.o dc-fs.o plugins.o MODULE_DIRS += ./ diff --git a/backends/platform/dc/dc.h b/backends/platform/dc/dc.h index 76c4b8cfd2..f0c8c1f96c 100644 --- a/backends/platform/dc/dc.h +++ b/backends/platform/dc/dc.h @@ -31,6 +31,9 @@ #include "backends/audiocd/default/default-audiocd.h" #include "backends/fs/fs-factory.h" #include "audio/mixer_intern.h" +#ifdef DYNAMIC_MODULES +#include "backends/plugins/dynamic-plugin.h" +#endif #define NUM_BUFFERS 4 #define SOUND_BUFFER_SHIFT 3 @@ -69,7 +72,11 @@ class DCCDManager : public DefaultAudioCDManager { void updateCD(); }; -class OSystem_Dreamcast : private DCHardware, public BaseBackend, public PaletteManager, public FilesystemFactory { +class OSystem_Dreamcast : private DCHardware, public BaseBackend, public PaletteManager, public FilesystemFactory +#ifdef DYNAMIC_MODULES + , public FilePluginProvider +#endif + { public: OSystem_Dreamcast(); @@ -250,6 +257,14 @@ public: void logMessage(LogMessageType::Type type, const char *message); Common::String getSystemLanguage() const; + +#ifdef DYNAMIC_MODULES + class DCPlugin; + + protected: + Plugin* createPlugin(const Common::FSNode &node) const; + bool isPluginFilename(const Common::FSNode &node) const; +#endif }; diff --git a/backends/platform/dc/dcmain.cpp b/backends/platform/dc/dcmain.cpp index bbd4f994f7..f9640130d3 100644 --- a/backends/platform/dc/dcmain.cpp +++ b/backends/platform/dc/dcmain.cpp @@ -33,7 +33,6 @@ #include #include -#include "backends/plugins/dc/dc-provider.h" #include "audio/mixer_intern.h" @@ -336,7 +335,7 @@ int main() g_system = &osys_dc; #ifdef DYNAMIC_MODULES - PluginManager::instance().addPluginProvider(new DCPluginProvider()); + PluginManager::instance().addPluginProvider(&osys_dc); #endif scummvm_main(argc, argv); diff --git a/backends/platform/dc/module.mk b/backends/platform/dc/module.mk index c52ca1a474..9ab287c080 100644 --- a/backends/platform/dc/module.mk +++ b/backends/platform/dc/module.mk @@ -1,7 +1,7 @@ MODULE := backends/platform/dc MODULE_OBJS := dcmain.o time.o display.o audio.o input.o selector.o icon.o \ - label.o vmsave.o softkbd.o dcloader.o cache.o dc-fs.o + label.o vmsave.o softkbd.o dcloader.o cache.o dc-fs.o plugins.o # We don't use rules.mk but rather manually update OBJS and MODULE_DIRS. MODULE_OBJS := $(addprefix $(MODULE)/, $(MODULE_OBJS)) diff --git a/backends/platform/dc/plugins.cpp b/backends/platform/dc/plugins.cpp new file mode 100644 index 0000000000..7b07947847 --- /dev/null +++ b/backends/platform/dc/plugins.cpp @@ -0,0 +1,100 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#include "common/scummsys.h" + +#if defined(DYNAMIC_MODULES) + +#include "backends/plugins/dynamic-plugin.h" +#include "common/fs.h" + +#include "dcloader.h" + + +class OSystem_Dreamcast::DCPlugin : public DynamicPlugin { +protected: + void *_dlHandle; + + virtual VoidFunc findSymbol(const char *symbol) { + void *func = dlsym(_dlHandle, symbol); + if (!func) + warning("Failed loading symbol '%s' from plugin '%s' (%s)", symbol, _filename.c_str(), dlerror()); + + // FIXME HACK: This is a HACK to circumvent a clash between the ISO C++ + // standard and POSIX: ISO C++ disallows casting between function pointers + // and data pointers, but dlsym always returns a void pointer. For details, + // see e.g. . + assert(sizeof(VoidFunc) == sizeof(func)); + VoidFunc tmp; + memcpy(&tmp, &func, sizeof(VoidFunc)); + return tmp; + } + +public: + DCPlugin(const Common::String &filename) + : DynamicPlugin(filename), _dlHandle(0) {} + + bool loadPlugin() { + assert(!_dlHandle); + _dlHandle = dlopen(_filename.c_str(), RTLD_LAZY); + + if (!_dlHandle) { + warning("Failed loading plugin '%s' (%s)", _filename.c_str(), dlerror()); + return false; + } + + bool ret = DynamicPlugin::loadPlugin(); + + if (ret) + dlforgetsyms(_dlHandle); + + return ret; + } + + void unloadPlugin() { + DynamicPlugin::unloadPlugin(); + if (_dlHandle) { + if (dlclose(_dlHandle) != 0) + warning("Failed unloading plugin '%s' (%s)", _filename.c_str(), dlerror()); + _dlHandle = 0; + } + } +}; + + +Plugin* OSystem_Dreamcast::createPlugin(const Common::FSNode &node) const { + return new DCPlugin(node.getPath()); +} + +bool OSystem_Dreamcast::isPluginFilename(const Common::FSNode &node) const { + // Check the plugin suffix + Common::String filename = node.getName(); + if (!filename.hasSuffix(".PLG")) + return false; + + return true; +} + +#endif // defined(DYNAMIC_MODULES) -- cgit v1.2.3