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.

104 lines
2.9KB

  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. namespace GamecraftModdingAPI.Events
  8. {
  9. /// <summary>
  10. /// Keeps track of event handlers and emitters.
  11. /// This class can be used to add, remove and get event handlers and emitters.
  12. /// </summary>
  13. public static class Manager
  14. {
  15. private static Dictionary<string, IEventEmitterEngine> _eventEmitters = new Dictionary<string, IEventEmitterEngine>();
  16. private static Dictionary<string, IEventHandlerEngine> _eventHandlers = new Dictionary<string, IEventHandlerEngine>();
  17. // event handler management
  18. public static void AddEventHandler(IEventHandlerEngine engine)
  19. {
  20. _eventHandlers[engine.Name] = engine;
  21. }
  22. public static bool ExistsEventHandler(string name)
  23. {
  24. return _eventHandlers.ContainsKey(name);
  25. }
  26. public static bool ExistsEventHandler(IEventHandlerEngine engine)
  27. {
  28. return ExistsEventHandler(engine.Name);
  29. }
  30. public static IEventHandlerEngine GetEventHandler(string name)
  31. {
  32. return _eventHandlers[name];
  33. }
  34. public static Dictionary<string, IEventHandlerEngine> GetEventHandlers()
  35. {
  36. return _eventHandlers;
  37. }
  38. public static void RemoveEventHandler(string name)
  39. {
  40. _eventHandlers.Remove(name);
  41. }
  42. // event emitter management
  43. public static void AddEventEmitter(IEventEmitterEngine engine)
  44. {
  45. _eventEmitters[engine.Name] = engine;
  46. }
  47. public static bool ExistsEventEmitter(string name)
  48. {
  49. return _eventEmitters.ContainsKey(name);
  50. }
  51. public static bool ExistsEventEmitter(IEventEmitterEngine engine)
  52. {
  53. return ExistsEventEmitter(engine.Name);
  54. }
  55. public static IEventEmitterEngine GetEventEmitter(string name)
  56. {
  57. return _eventEmitters[name];
  58. }
  59. public static Dictionary<string, IEventEmitterEngine> GetEventEmitters()
  60. {
  61. return _eventEmitters;
  62. }
  63. public static void RemoveEventEmitter(string name)
  64. {
  65. if (_eventEmitters[name].isRemovable)
  66. {
  67. _eventEmitters.Remove(name);
  68. }
  69. }
  70. public static void RegisterEngines(EnginesRoot enginesRoot)
  71. {
  72. // Register handlers before emitters so no events are missed
  73. var entityFactory = enginesRoot.GenerateEntityFactory();
  74. foreach (var key in _eventHandlers.Keys)
  75. {
  76. enginesRoot.AddEngine(_eventHandlers[key]);
  77. }
  78. foreach (var key in _eventEmitters.Keys)
  79. {
  80. _eventEmitters[key].Factory = entityFactory;
  81. enginesRoot.AddEngine(_eventEmitters[key]);
  82. }
  83. }
  84. }
  85. }