Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: MPlugin.h
- ===================================================================
- --- MPlugin.h (revision 102)
- +++ MPlugin.h (working copy)
- @@ -31,11 +31,18 @@
- #ifndef _M_PLUGIN_H
- #define _M_PLUGIN_H
- -#ifdef WIN32
- -#include <windows.h>
- +#ifdef MPLUGIN_DYNAMIC
- +# ifdef WIN32
- +# include <windows.h>
- +# define MPLUGIN_EXPORT __declspec(dllexport)
- +# else
- +// automagically exported
- +# define MPLUGIN_EXPORT
- +# include <dlfcn.h>
- +# endif /*WIN32*/
- #else
- -#include <dlfcn.h>
- -#endif
- +typedef void * (*FunctionPtr)();
- +#endif/*MPLUGIN_DYNAMIC*/
- class MPlugin
- {
- @@ -44,14 +51,19 @@
- MPlugin(void);
- ~MPlugin(void);
- -private :
- +protected :
- string m_filename;
- -
- -#ifdef WIN32
- - HMODULE m_library;
- +
- +#ifdef MPLUGIN_DYNAMIC
- +# ifdef WIN32
- + HMODULE m_library;
- +# else
- + void * m_library;
- +# endif
- #else
- - void * m_library;
- + FunctionPtr Start;
- + FunctionPtr End;
- #endif
- public:
- @@ -60,4 +72,67 @@
- inline const char * getFilename(void){ return m_filename.c_str(); }
- };
- -#endif
- \ No newline at end of file
- +
- +// Some nasty macros to help making plugins for both systems that can dynamically
- +// load libraries (ie. sensible systems) and for annoying systems like iOS
- +// where we can't have shared objects.
- +//
- +// to use, in the plugin somewhere do as follows:
- +//------------AwesomePlugin.h---------------------------------------------------
- +// #include <MPlugin.h>
- +//
- +// MPLUGIN_DECLARE(MyAwesomePlugin);
- +//------------AwesomePlugin.cpp-------------------------------------------------
- +// #include "AwesomePlugin.h"
- +//
- +// MPLUGIN_START_IMPLEMENT(MyAwesomePlugin)
- +// {
- +// // this is where you do your code
- +// }
- +//
- +// MPLUGIN_END_IMPLEMENT(MyAwesomePlugin)
- +// {
- +// // cleanup here
- +// }
- +//
- +// Everything else should be taken care of
- +
- +#ifdef MPLUGIN_DYNAMIC
- +# define MPLUGIN_DECLARE(name) \
- + MPLUGIN_EXPORT void StartPlugin(); \
- + MPLUGIN_EXPORT void EndPlugin();
- +
- +# define MPLUGIN_START_IMPLEMENT(name) \
- + void StartPlugin()
- +
- +# define MPLUGIN_END_IMPLEMENT(name) \
- + void EndPlugin()
- +#else
- +# define MPLUGIN_DECLARE(name) \
- + void StartPlugin##name(); \
- + void EndPlugin##name(); \
- + class Plugin##name##AutoStart \
- + { \
- + public: \
- + Plugin##name##AutoStart(); \
- + } \
- + extern Plugin##name##AutoStart s_##name##AutoStarter;
- +
- +# define MPLUGIN_START_IMPLEMENT(name) \
- + Plugin##name##AutoStart s_##name##AutoStarter; \
- + Plugin##name##AutoStart::Plugin##name##AutoStart() \
- + { \
- + AddPluginFunctions(#name, StartPlugin##name, EndPlugin##name); \
- + } \
- + void StartPlugin##name()
- +
- +# define MPLUGIN_END_IMPLEMENT(name) \
- + void EndPlugin##name()
- +#endif/*MPLUGIN_DYNAMIC*/
- +
- +// don't worry about this:
- +#ifndef MPLUGIN_DYNAMIC
- +void AddPluginFunctions(const char* pluginName, FunctionPtr start, FunctionPtr end);
- +#endif
- +
- +#endif
- Index: MPlugin.cpp
- ===================================================================
- --- MPlugin.cpp (revision 102)
- +++ MPlugin.cpp (working copy)
- @@ -31,43 +31,74 @@
- #include <MEngine.h>
- #include "MPlugin.h"
- +#include <stdio.h>
- -typedef void * (*FunctionPtr)();
- +#ifndef MPLUGIN_DYNAMIC
- +#include <map>
- +typedef struct _plugin
- +{
- + FunctionPtr Start;
- + FunctionPtr End;
- + _plugin() : Start(0), End(0) {}
- + _plugin(FunctionPtr s, FunctionPtr e) : Start(s), End(e) {}
- +} plugin;
- +typedef std::map<const char*, plugin> pluginmap;
- +typedef pluginmap::iterator pluginiter;
- +pluginmap g_plugindefs;
- +
- +void AddPluginFunctions(const char* pluginName, FunctionPtr start, FunctionPtr end)
- +{
- + plugin p(start, end);
- + g_plugindefs[pluginName] = p;
- +}
- +#endif
- +
- MPlugin::MPlugin(void)
- {
- +#ifdef MPLUGIN_DYNAMIC
- m_library = NULL;
- +#else
- + Start= 0;
- + End = 0;
- +#endif
- }
- MPlugin::~MPlugin(void)
- {
- +#ifdef MPLUGIN_DYNAMIC
- if(m_library)
- {
- -#ifdef WIN32
- +# ifdef WIN32
- FunctionPtr function = reinterpret_cast<FunctionPtr>(GetProcAddress(m_library, "EndPlugin"));
- if(function)
- function();
- FreeLibrary(m_library);
- -#else
- +# else
- FunctionPtr function = (FunctionPtr)dlsym(m_library, "EndPlugin");
- if(function)
- function();
- dlclose(m_library);
- +# endif
- + }
- +#else
- + if(End)
- + End();
- #endif
- - }
- }
- void MPlugin::load(const char * filename)
- {
- -#ifdef WIN32
- +#ifdef MPLUGIN_DYNAMIC
- +# ifdef WIN32
- m_library = LoadLibrary(filename);
- if(! m_library)
- - return;
- + return;
- FunctionPtr function = reinterpret_cast<FunctionPtr>(GetProcAddress(m_library, "StartPlugin"));
- if(! function)
- @@ -76,18 +107,35 @@
- m_filename = filename;
- function();
- -#else
- +# else
- m_library = dlopen(filename, RTLD_LAZY);
- if(! m_library)
- - return;
- + {
- + printf("%s\n", dlerror());
- + return;
- + }
- FunctionPtr function = (FunctionPtr)dlsym(m_library, "StartPlugin");
- if(! function)
- - return;
- -
- + {
- + printf(dlerror());
- + return;
- + }
- +
- m_filename = filename;
- function();
- +# endif
- +#else
- + // still need to cut up the filename... or something
- + pluginiter iplug = g_plugindefs.find(filename);
- + if(iplug != g_plugindefs.end())
- + {
- + Start = iplug->second.Start;
- + End = iplug->second.End;
- + }
- + if(Start)
- + Start();
- #endif
- -}
- \ No newline at end of file
- +}
Advertisement
Add Comment
Please, Sign In to add comment