Guest User

Untitled

a guest
Jul 2nd, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 5.46 KB | None | 0 0
  1. Index: MPlugin.h
  2. ===================================================================
  3. --- MPlugin.h   (revision 102)
  4. +++ MPlugin.h   (working copy)
  5. @@ -31,11 +31,18 @@
  6.  #ifndef _M_PLUGIN_H
  7.  #define _M_PLUGIN_H
  8.  
  9. -#ifdef WIN32
  10. -#include <windows.h>
  11. +#ifdef MPLUGIN_DYNAMIC
  12. +#  ifdef WIN32
  13. +#    include <windows.h>
  14. +#    define MPLUGIN_EXPORT __declspec(dllexport)
  15. +#  else
  16. +// automagically exported
  17. +#    define MPLUGIN_EXPORT
  18. +#    include <dlfcn.h>
  19. +#  endif /*WIN32*/
  20.  #else
  21. -#include <dlfcn.h>
  22. -#endif
  23. +typedef void * (*FunctionPtr)();
  24. +#endif/*MPLUGIN_DYNAMIC*/
  25.  
  26.  class MPlugin
  27.  {
  28. @@ -44,14 +51,19 @@
  29.     MPlugin(void);
  30.     ~MPlugin(void);
  31.  
  32. -private :
  33. +protected :
  34.  
  35.     string m_filename;
  36. -  
  37. -#ifdef WIN32
  38. -    HMODULE m_library;
  39. +
  40. +#ifdef MPLUGIN_DYNAMIC
  41. +#    ifdef WIN32
  42. +   HMODULE m_library;
  43. +#    else
  44. +   void * m_library;
  45. +#    endif
  46.  #else
  47. -   void * m_library;
  48. +        FunctionPtr Start;
  49. +        FunctionPtr End;
  50.  #endif
  51.  
  52.  public:
  53. @@ -60,4 +72,67 @@
  54.     inline const char * getFilename(void){ return m_filename.c_str(); }
  55.  };
  56.  
  57. -#endif
  58. \ No newline at end of file
  59. +
  60. +// Some nasty macros to help making plugins for both systems that can dynamically
  61. +// load libraries (ie. sensible systems) and for annoying systems like iOS
  62. +// where we can't have shared objects.
  63. +//
  64. +// to use, in the plugin somewhere do as follows:
  65. +//------------AwesomePlugin.h---------------------------------------------------
  66. +// #include <MPlugin.h>
  67. +//
  68. +// MPLUGIN_DECLARE(MyAwesomePlugin);
  69. +//------------AwesomePlugin.cpp-------------------------------------------------
  70. +// #include "AwesomePlugin.h"
  71. +//
  72. +// MPLUGIN_START_IMPLEMENT(MyAwesomePlugin)
  73. +// {
  74. +//   // this is where you do your code
  75. +// }
  76. +//
  77. +// MPLUGIN_END_IMPLEMENT(MyAwesomePlugin)
  78. +// {
  79. +//   // cleanup here
  80. +// }
  81. +//
  82. +// Everything else should be taken care of
  83. +
  84. +#ifdef MPLUGIN_DYNAMIC
  85. +#    define MPLUGIN_DECLARE(name)      \
  86. +  MPLUGIN_EXPORT void StartPlugin();       \
  87. +  MPLUGIN_EXPORT void EndPlugin();
  88. +
  89. +#    define MPLUGIN_START_IMPLEMENT(name)  \
  90. +  void StartPlugin()
  91. +
  92. +#    define MPLUGIN_END_IMPLEMENT(name)        \
  93. +  void EndPlugin()
  94. +#else
  95. +#    define MPLUGIN_DECLARE(name)          \
  96. +  void StartPlugin##name();                \
  97. +  void EndPlugin##name();              \
  98. +  class Plugin##name##AutoStart                \
  99. +  {                            \
  100. +  public:                      \
  101. +    Plugin##name##AutoStart();             \
  102. +  }                            \
  103. +  extern Plugin##name##AutoStart s_##name##AutoStarter;
  104. +
  105. +#   define MPLUGIN_START_IMPLEMENT(name)               \
  106. +  Plugin##name##AutoStart s_##name##AutoStarter;           \
  107. +  Plugin##name##AutoStart::Plugin##name##AutoStart()           \
  108. +  {                                    \
  109. +    AddPluginFunctions(#name, StartPlugin##name, EndPlugin##name); \
  110. +  }                                    \
  111. +  void StartPlugin##name()
  112. +
  113. +#   define MPLUGIN_END_IMPLEMENT(name)     \
  114. +  void EndPlugin##name()
  115. +#endif/*MPLUGIN_DYNAMIC*/
  116. +
  117. +// don't worry about this:
  118. +#ifndef MPLUGIN_DYNAMIC
  119. +void AddPluginFunctions(const char* pluginName, FunctionPtr start, FunctionPtr end);
  120. +#endif
  121. +
  122. +#endif
  123. Index: MPlugin.cpp
  124. ===================================================================
  125. --- MPlugin.cpp (revision 102)
  126. +++ MPlugin.cpp (working copy)
  127. @@ -31,43 +31,74 @@
  128.  #include <MEngine.h>
  129.  #include "MPlugin.h"
  130.  
  131. +#include <stdio.h>
  132.  
  133. -typedef void * (*FunctionPtr)();
  134. +#ifndef MPLUGIN_DYNAMIC
  135. +#include <map>
  136. +typedef struct _plugin
  137. +{
  138. +  FunctionPtr Start;
  139. +  FunctionPtr End;
  140. +  _plugin() : Start(0), End(0) {}
  141. +  _plugin(FunctionPtr s, FunctionPtr e) : Start(s), End(e) {}
  142. +} plugin;
  143.  
  144. +typedef std::map<const char*, plugin> pluginmap;
  145. +typedef pluginmap::iterator pluginiter;
  146. +pluginmap g_plugindefs;
  147. +
  148. +void AddPluginFunctions(const char* pluginName, FunctionPtr start, FunctionPtr end)
  149. +{
  150. +  plugin p(start, end);
  151. +  g_plugindefs[pluginName] = p;
  152. +}
  153. +#endif
  154. +
  155.  MPlugin::MPlugin(void)
  156.  {
  157. +#ifdef MPLUGIN_DYNAMIC
  158.     m_library = NULL;
  159. +#else
  160. +   Start= 0;
  161. +   End = 0;
  162. +#endif
  163.  }
  164.  
  165.  MPlugin::~MPlugin(void)
  166.  {
  167. +#ifdef MPLUGIN_DYNAMIC
  168.     if(m_library)
  169.     {  
  170. -#ifdef WIN32
  171. +#    ifdef WIN32
  172.        
  173.         FunctionPtr function = reinterpret_cast<FunctionPtr>(GetProcAddress(m_library, "EndPlugin"));
  174.         if(function)
  175.              function();
  176.         FreeLibrary(m_library);
  177.        
  178. -#else
  179. +#    else
  180.        
  181.         FunctionPtr function = (FunctionPtr)dlsym(m_library, "EndPlugin");
  182.         if(function)
  183.             function();
  184.         dlclose(m_library);
  185.        
  186. +#    endif
  187. +   }
  188. +#else
  189. +   if(End)
  190. +     End();
  191.  #endif
  192. -   }
  193.  }
  194.  
  195.  void MPlugin::load(const char * filename)
  196.  {
  197. -#ifdef WIN32
  198. +#ifdef MPLUGIN_DYNAMIC
  199. +#    ifdef WIN32
  200.    
  201.      m_library = LoadLibrary(filename);
  202.      if(! m_library)
  203. -       return;
  204. +   return;
  205.  
  206.      FunctionPtr function = reinterpret_cast<FunctionPtr>(GetProcAddress(m_library, "StartPlugin"));
  207.      if(! function)
  208. @@ -76,18 +107,35 @@
  209.     m_filename = filename;
  210.      function();
  211.    
  212. -#else
  213. +#    else
  214.    
  215.     m_library = dlopen(filename, RTLD_LAZY);
  216.     if(! m_library)
  217. -       return;
  218. +        {
  219. +     printf("%s\n", dlerror());
  220. +     return;
  221. +   }
  222.    
  223.     FunctionPtr function = (FunctionPtr)dlsym(m_library, "StartPlugin");
  224.     if(! function)
  225. -       return;
  226. -  
  227. +        {
  228. +     printf(dlerror());
  229. +     return;
  230. +   }
  231. +
  232.     m_filename = filename;
  233.      function();
  234.  
  235. +#    endif
  236. +#else
  237. +    // still need to cut up the filename... or something
  238. +    pluginiter iplug = g_plugindefs.find(filename);
  239. +    if(iplug != g_plugindefs.end())
  240. +    {
  241. +      Start = iplug->second.Start;
  242. +      End = iplug->second.End;
  243. +    }
  244. +    if(Start)
  245. +      Start();
  246.  #endif
  247. -}
  248. \ No newline at end of file
  249. +}
Advertisement
Add Comment
Please, Sign In to add comment