A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.6KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Svelto.ECS;
  7. using GamecraftModdingAPI.Utility;
  8. namespace GamecraftModdingAPI.Events
  9. {
  10. /// <summary>
  11. /// Keeps track of event handlers and emitters.
  12. /// This is used to add, remove and get API event handlers and emitters.
  13. /// </summary>
  14. public static class EventManager
  15. {
  16. private static Dictionary<string, IEventEmitterEngine> _eventEmitters = new Dictionary<string, IEventEmitterEngine>();
  17. private static Dictionary<string, IEventHandlerEngine> _eventHandlers = new Dictionary<string, IEventHandlerEngine>();
  18. private static EnginesRoot _lastEngineRoot;
  19. // event handler management
  20. public static void AddEventHandler(IEventHandlerEngine engine)
  21. {
  22. _eventHandlers[engine.Name] = engine;
  23. if (_lastEngineRoot != null)
  24. {
  25. Logging.MetaDebugLog($"Registering IEventHandlerEngine {engine.Name}");
  26. _lastEngineRoot.AddEngine(engine);
  27. }
  28. }
  29. public static bool ExistsEventHandler(string name)
  30. {
  31. return _eventHandlers.ContainsKey(name);
  32. }
  33. public static bool ExistsEventHandler(IEventHandlerEngine engine)
  34. {
  35. return ExistsEventHandler(engine.Name);
  36. }
  37. public static IEventHandlerEngine GetEventHandler(string name)
  38. {
  39. return _eventHandlers[name];
  40. }
  41. public static string[] GetEventHandlerNames()
  42. {
  43. return _eventHandlers.Keys.ToArray();
  44. }
  45. public static void RemoveEventHandler(string name)
  46. {
  47. _eventHandlers.Remove(name);
  48. }
  49. // event emitter management
  50. public static void AddEventEmitter(IEventEmitterEngine engine)
  51. {
  52. _eventEmitters[engine.Name] = engine;
  53. if (_lastEngineRoot != null)
  54. {
  55. Logging.MetaDebugLog($"Registering IEventEmitterEngine {engine.Name}");
  56. _lastEngineRoot.AddEngine(engine);
  57. }
  58. }
  59. public static bool ExistsEventEmitter(string name)
  60. {
  61. return _eventEmitters.ContainsKey(name);
  62. }
  63. public static bool ExistsEventEmitter(IEventEmitterEngine engine)
  64. {
  65. return ExistsEventEmitter(engine.Name);
  66. }
  67. public static IEventEmitterEngine GetEventEmitter(string name)
  68. {
  69. return _eventEmitters[name];
  70. }
  71. public static string[] GetEventEmitterNames()
  72. {
  73. return _eventEmitters.Keys.ToArray();
  74. }
  75. public static void RemoveEventEmitter(string name)
  76. {
  77. if (_eventEmitters[name].isRemovable)
  78. {
  79. _eventEmitters.Remove(name);
  80. }
  81. }
  82. public static void RegisterEngines(EnginesRoot enginesRoot)
  83. {
  84. _lastEngineRoot = enginesRoot;
  85. // Register handlers before emitters so no events are missed
  86. var entityFactory = enginesRoot.GenerateEntityFactory();
  87. foreach (var key in _eventHandlers.Keys)
  88. {
  89. Logging.MetaDebugLog($"Registering IEventHandlerEngine {_eventHandlers[key].Name}");
  90. enginesRoot.AddEngine(_eventHandlers[key]);
  91. }
  92. foreach (var key in _eventEmitters.Keys)
  93. {
  94. Logging.MetaDebugLog($"Registering IEventEmitterEngine {_eventEmitters[key].Name}");
  95. _eventEmitters[key].Factory = entityFactory;
  96. enginesRoot.AddEngine(_eventEmitters[key]);
  97. }
  98. }
  99. }
  100. }