pluginmanager.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. class PluginManager
  2. {
  3. private ref array<typename> m_PluginRegister; // list of modules for creation
  4. private ref map<typename, ref PluginBase> m_PluginsPtrs; // plugin, plugin pointer
  5. //=================================
  6. // Constructor
  7. //=================================
  8. void PluginManager()
  9. {
  10. m_PluginRegister = new array<typename>;
  11. m_PluginsPtrs = new map<typename, ref PluginBase>;
  12. }
  13. //=================================
  14. // Destructor
  15. //=================================
  16. void ~PluginManager()
  17. {
  18. int i;
  19. PluginBase plugin;
  20. for ( i = 0; i < m_PluginsPtrs.Count(); ++i)
  21. {
  22. plugin = m_PluginsPtrs.GetElement(i);
  23. if ( plugin != NULL )
  24. {
  25. plugin.OnDestroy();
  26. delete plugin;
  27. }
  28. }
  29. GetGame().GetUpdateQueue(CALL_CATEGORY_GAMEPLAY).Remove(this.MainOnUpdate);
  30. }
  31. //=================================
  32. // Init
  33. //=================================
  34. void Init()
  35. {
  36. //----------------------------------------------------------------------
  37. // Register modules
  38. //----------------------------------------------------------------------
  39. // Module Class Name Client Server
  40. //----------------------------------------------------------------------
  41. RegisterPlugin( "PluginHorticulture", true, true );
  42. RegisterPlugin( "PluginRepairing", true, true );
  43. RegisterPlugin( "PluginPlayerStatus", true, true );
  44. RegisterPlugin( "PluginMessageManager", true, true );
  45. RegisterPlugin( "PluginLifespan", true, true );
  46. RegisterPlugin( "PluginVariables", true, true );
  47. RegisterPlugin( "PluginObjectsInteractionManager", false, true );
  48. RegisterPlugin( "PluginRecipesManager", true, true );
  49. RegisterPlugin( "PluginTransmissionAgents", true, true );
  50. RegisterPlugin( "PluginConfigEmotesProfile", true, true );
  51. RegisterPlugin( "PluginPresenceNotifier", true, false );
  52. RegisterPlugin( "PluginAdminLog", false, true );
  53. // Developer + Diag
  54. RegisterPluginDiag( "PluginKeyBinding", true, false );
  55. RegisterPluginDiag( "PluginDeveloper", true, true );
  56. RegisterPluginDiag( "PluginDeveloperSync", true, true );
  57. RegisterPluginDiag( "PluginDiagMenuClient", true, false );
  58. RegisterPluginDiag( "PluginDiagMenuServer", false, true );
  59. RegisterPluginDiag( "PluginPermanentCrossHair", true, false );
  60. RegisterPluginDiag( "PluginRemotePlayerDebugClient", true, false );
  61. RegisterPluginDiag( "PluginRemotePlayerDebugServer", false, true );
  62. RegisterPluginDiag( "PluginUniversalTemperatureSourceClient", true, false );
  63. RegisterPluginDiag( "PluginUniversalTemperatureSourceServer", false, true );
  64. RegisterPluginDiag( "PluginTargetTemperature", true, false );
  65. RegisterPluginDiag( "PluginDrawCheckerboard", true, false );
  66. RegisterPluginDiag( "PluginItemDiagnostic", true, true );
  67. RegisterPluginDiag( "PluginDayZCreatureAIDebug", true, true );
  68. // Only In Debug / Internal
  69. RegisterPluginDebug( "PluginConfigViewer", true, true );
  70. RegisterPluginDebug( "PluginLocalEnscriptHistory", true, true );
  71. RegisterPluginDebug( "PluginLocalEnscriptHistoryServer", true, true );
  72. RegisterPluginDebug( "PluginSceneManager", true, true );
  73. RegisterPluginDebug( "PluginConfigScene", true, true );
  74. RegisterPluginDebug( "PluginMissionConfig", true, true );
  75. RegisterPluginDebug( "PluginConfigEmotesProfile", true, true );
  76. RegisterPluginDebug( "PluginConfigDebugProfile", true, true );
  77. RegisterPluginDebug( "PluginConfigDebugProfileFixed", true, true );
  78. RegisterPluginDebug( "PluginDayzPlayerDebug", true, true );
  79. RegisterPluginDebug( "PluginDayZInfectedDebug", true, true );
  80. RegisterPluginDebug( "PluginDoorRuler", true, false );
  81. RegisterPluginDebug( "PluginCharPlacement", true, false );
  82. //RegisterPluginDebug( "PluginSoundDebug", false, false );
  83. RegisterPluginDebug( "PluginCameraTools", true, true );
  84. RegisterPluginDebug( "PluginNutritionDumper", true, false );
  85. GetGame().GetUpdateQueue(CALL_CATEGORY_GAMEPLAY).Insert(MainOnUpdate);
  86. }
  87. //=================================
  88. // PluginsInit
  89. //=================================
  90. void PluginsInit()
  91. {
  92. int i = 0;
  93. int regCount = m_PluginRegister.Count();
  94. array<PluginBase> pluginPtrs = {};
  95. pluginPtrs.Reserve(regCount);
  96. foreach (typename pluginType : m_PluginRegister)
  97. {
  98. PluginBase moduleExist = GetPluginByType( pluginType );
  99. if ( moduleExist )
  100. {
  101. m_PluginsPtrs.Remove( pluginType );
  102. }
  103. PluginBase moduleNew = PluginBase.Cast(pluginType.Spawn());
  104. m_PluginsPtrs.Set(pluginType, moduleNew);
  105. pluginPtrs.Insert(moduleNew);
  106. }
  107. foreach (PluginBase plugin : pluginPtrs)
  108. {
  109. if ( plugin )
  110. {
  111. plugin.OnInit();
  112. }
  113. }
  114. }
  115. //=================================
  116. // MainOnUpdate
  117. //=================================
  118. void MainOnUpdate(float delta_time)
  119. {
  120. for ( int i = 0; i < m_PluginsPtrs.Count(); ++i)
  121. {
  122. PluginBase plugin = m_PluginsPtrs.GetElement(i);
  123. if ( plugin != NULL )
  124. {
  125. plugin.OnUpdate(delta_time);
  126. }
  127. }
  128. }
  129. /**
  130. \brief Returns registred plugin by class type, better is to use global funtion GetPlugin(typename plugin_type)
  131. \param module_tpye \p typename class type of plugin
  132. \return \p PluginBase
  133. @code
  134. PluginRepairing plugin = GetPluginManager().GetPluginByType(PluginRepairing);
  135. @endcode
  136. */
  137. //=================================
  138. // GetPluginByType
  139. //=================================
  140. PluginBase GetPluginByType( typename plugin_type )
  141. {
  142. if ( m_PluginsPtrs.Contains( plugin_type ) )
  143. {
  144. return m_PluginsPtrs.Get( plugin_type );
  145. }
  146. return null;
  147. }
  148. /**
  149. \brief Register new PluginBase to PluginManager for storing and handling plugin.
  150. \param module_tpye \p typename class type of plugin
  151. \return \p void
  152. @code
  153. class PluginRepairing extends PluginBase
  154. {
  155. ...
  156. }
  157. RegisterPlugin(PluginRepairing);
  158. @endcode
  159. */
  160. //=================================
  161. // RegisterPlugin
  162. //=================================
  163. protected void RegisterPlugin( string plugin_class_name, bool reg_on_client, bool reg_on_server, bool reg_on_release = true )
  164. {
  165. if ( !reg_on_client )
  166. {
  167. if ( GetGame().IsMultiplayer() && GetGame().IsClient() )
  168. {
  169. return;
  170. }
  171. }
  172. if ( !reg_on_server )
  173. {
  174. if ( GetGame().IsMultiplayer() )
  175. {
  176. if ( GetGame().IsServer() )
  177. {
  178. return;
  179. }
  180. }
  181. }
  182. if ( !reg_on_release )
  183. {
  184. if ( !GetGame().IsDebug() )
  185. {
  186. return;
  187. }
  188. }
  189. m_PluginRegister.Insert( plugin_class_name.ToType() );
  190. }
  191. /**
  192. \brief Register new PluginBase to PluginManager for storing and handling plugin.
  193. \param module_tpye \p typename class type of plugin
  194. \return \p void
  195. @code
  196. class PluginRepairing extends PluginBase
  197. {
  198. ...
  199. }
  200. RegisterPlugin(PluginRepairing);
  201. @endcode
  202. */
  203. //=================================
  204. // RegisterPlugin
  205. //=================================
  206. protected void RegisterPluginDebug( string plugin_class_name, bool reg_on_client, bool reg_on_server )
  207. {
  208. RegisterPlugin(plugin_class_name, reg_on_client, reg_on_server, false);
  209. }
  210. //=================================
  211. // RegisterPlugin
  212. //=================================
  213. protected void RegisterPluginDiag( string plugin_class_name, bool reg_on_client, bool reg_on_server )
  214. {
  215. #ifdef DIAG_DEVELOPER
  216. RegisterPlugin(plugin_class_name, reg_on_client, reg_on_server, true);
  217. #else
  218. return;
  219. #endif
  220. }
  221. //---------------------------------
  222. // UnregisterPlugin
  223. //---------------------------------
  224. protected bool UnregisterPlugin( string plugin_class_name )
  225. {
  226. typename plugin_type = plugin_class_name.ToType();
  227. if ( m_PluginRegister.Find( plugin_type ) != -1 )
  228. {
  229. m_PluginRegister.RemoveItem( plugin_type );
  230. return true;
  231. }
  232. return false;
  233. }
  234. }
  235. ref PluginManager g_Plugins;
  236. /**
  237. \brief Returns registred plugin by class type, better is to use global funtion GetPlugin(typename plugin_type)
  238. \param module_tpye \p typename class type of plugin
  239. \return \p PluginBase
  240. @code
  241. PluginRepairing plugin = GetPluginManager().GetPluginByType(PluginRepairing);
  242. @endcode
  243. */
  244. PluginManager GetPluginManager()
  245. {
  246. /*
  247. if ( g_Plugins == NULL )
  248. {
  249. PluginManagerInit();
  250. }
  251. */
  252. return g_Plugins;
  253. }
  254. void PluginManagerInit()
  255. {
  256. if (g_Plugins == NULL)
  257. {
  258. g_Plugins = new PluginManager;
  259. g_Plugins.Init();
  260. g_Plugins.PluginsInit();
  261. }
  262. }
  263. void PluginManagerDelete()
  264. {
  265. if ( g_Plugins )
  266. {
  267. delete g_Plugins;
  268. g_Plugins = NULL;
  269. }
  270. }
  271. bool IsPluginManagerExists()
  272. {
  273. if ( g_Plugins != null )
  274. {
  275. return true;
  276. }
  277. return false;
  278. }
  279. PluginBase GetPlugin(typename plugin_type)
  280. {
  281. PluginBase plugin = null;
  282. if ( IsPluginManagerExists() )
  283. {
  284. plugin = GetPluginManager().GetPluginByType(plugin_type);
  285. if ( plugin == null )
  286. {
  287. #ifdef DIAG_DEVELOPER
  288. if ( IsPluginManagerExists() )
  289. {
  290. PrintString("Module " + plugin_type.ToString() + " is not Registred in PluginManager.c!");
  291. DumpStack();
  292. }
  293. #endif
  294. }
  295. }
  296. return plugin;
  297. }
  298. bool IsModuleExist(typename plugin_type)
  299. {
  300. if ( IsPluginManagerExists() )
  301. {
  302. return ( GetPlugin(plugin_type) != NULL );
  303. }
  304. return false;
  305. }