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.

108 lines
3.2KB

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