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.

EventManager.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. if (ExistsEventHandler(engine))
  23. {
  24. throw new EventAlreadyExistsException($"IEventHandlerEngine {engine.Name} already exists");
  25. }
  26. _eventHandlers[engine.Name] = engine;
  27. if (_lastEngineRoot != null)
  28. {
  29. Logging.MetaDebugLog($"Registering IEventHandlerEngine {engine.Name}");
  30. _lastEngineRoot.AddEngine(engine);
  31. }
  32. }
  33. public static bool ExistsEventHandler(string name)
  34. {
  35. return _eventHandlers.ContainsKey(name);
  36. }
  37. public static bool ExistsEventHandler(IEventHandlerEngine engine)
  38. {
  39. return ExistsEventHandler(engine.Name);
  40. }
  41. public static IEventHandlerEngine GetEventHandler(string name)
  42. {
  43. return _eventHandlers[name];
  44. }
  45. public static string[] GetEventHandlerNames()
  46. {
  47. return _eventHandlers.Keys.ToArray();
  48. }
  49. public static void RemoveEventHandler(string name)
  50. {
  51. _eventHandlers.Remove(name);
  52. }
  53. // event emitter management
  54. public static void AddEventEmitter(IEventEmitterEngine engine)
  55. {
  56. if (ExistsEventEmitter(engine))
  57. {
  58. throw new EventAlreadyExistsException($"IEventEmitterEngine {engine.Name} already exists");
  59. }
  60. _eventEmitters[engine.Name] = engine;
  61. if (_lastEngineRoot != null)
  62. {
  63. Logging.MetaDebugLog($"Registering IEventEmitterEngine {engine.Name}");
  64. _lastEngineRoot.AddEngine(engine);
  65. }
  66. }
  67. public static bool ExistsEventEmitter(string name)
  68. {
  69. return _eventEmitters.ContainsKey(name);
  70. }
  71. public static bool ExistsEventEmitter(IEventEmitterEngine engine)
  72. {
  73. return ExistsEventEmitter(engine.Name);
  74. }
  75. public static IEventEmitterEngine GetEventEmitter(string name)
  76. {
  77. return _eventEmitters[name];
  78. }
  79. public static string[] GetEventEmitterNames()
  80. {
  81. return _eventEmitters.Keys.ToArray();
  82. }
  83. public static void RemoveEventEmitter(string name)
  84. {
  85. if (_eventEmitters[name].isRemovable)
  86. {
  87. _eventEmitters.Remove(name);
  88. }
  89. }
  90. public static void RegisterEngines(EnginesRoot enginesRoot)
  91. {
  92. _lastEngineRoot = enginesRoot;
  93. // Register handlers before emitters so no events are missed
  94. var entityFactory = enginesRoot.GenerateEntityFactory();
  95. foreach (var key in _eventHandlers.Keys)
  96. {
  97. Logging.MetaDebugLog($"Registering IEventHandlerEngine {_eventHandlers[key].Name}");
  98. enginesRoot.AddEngine(_eventHandlers[key]);
  99. }
  100. foreach (var key in _eventEmitters.Keys)
  101. {
  102. Logging.MetaDebugLog($"Registering IEventEmitterEngine {_eventEmitters[key].Name}");
  103. _eventEmitters[key].Factory = entityFactory;
  104. enginesRoot.AddEngine(_eventEmitters[key]);
  105. }
  106. }
  107. }
  108. }