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 4.0KB

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