|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- using Svelto.ECS;
-
- using GamecraftModdingAPI.Utility;
-
- namespace GamecraftModdingAPI.Events
- {
- /// <summary>
- /// Keeps track of event handlers and emitters.
- /// This is used to add, remove and get API event handlers and emitters.
- /// </summary>
- public static class EventManager
- {
- private static Dictionary<string, IEventEmitterEngine> _eventEmitters = new Dictionary<string, IEventEmitterEngine>();
-
- private static Dictionary<string, IEventHandlerEngine> _eventHandlers = new Dictionary<string, IEventHandlerEngine>();
-
- private static EnginesRoot _lastEngineRoot;
-
- // event handler management
-
- public static void AddEventHandler(IEventHandlerEngine engine)
- {
- if (ExistsEventHandler(engine))
- {
- throw new EventAlreadyExistsException($"IEventHandlerEngine {engine.Name} already exists");
- }
- _eventHandlers[engine.Name] = engine;
- if (_lastEngineRoot != null)
- {
- Logging.MetaDebugLog($"Registering IEventHandlerEngine {engine.Name}");
- _lastEngineRoot.AddEngine(engine);
- }
- }
-
- public static bool ExistsEventHandler(string name)
- {
- return _eventHandlers.ContainsKey(name);
- }
-
- public static bool ExistsEventHandler(IEventHandlerEngine engine)
- {
- return ExistsEventHandler(engine.Name);
- }
-
- public static IEventHandlerEngine GetEventHandler(string name)
- {
- return _eventHandlers[name];
- }
-
- public static string[] GetEventHandlerNames()
- {
- return _eventHandlers.Keys.ToArray();
- }
-
- public static void RemoveEventHandler(string name)
- {
- _eventHandlers.Remove(name);
- }
-
- // event emitter management
-
- public static void AddEventEmitter(IEventEmitterEngine engine)
- {
- if (ExistsEventEmitter(engine))
- {
- throw new EventAlreadyExistsException($"IEventEmitterEngine {engine.Name} already exists");
- }
- _eventEmitters[engine.Name] = engine;
- if (_lastEngineRoot != null)
- {
- Logging.MetaDebugLog($"Registering IEventEmitterEngine {engine.Name}");
- _lastEngineRoot.AddEngine(engine);
- }
- }
-
- public static bool ExistsEventEmitter(string name)
- {
- return _eventEmitters.ContainsKey(name);
- }
-
- public static bool ExistsEventEmitter(IEventEmitterEngine engine)
- {
- return ExistsEventEmitter(engine.Name);
- }
-
- public static IEventEmitterEngine GetEventEmitter(string name)
- {
- return _eventEmitters[name];
- }
-
- public static string[] GetEventEmitterNames()
- {
- return _eventEmitters.Keys.ToArray();
- }
-
- public static void RemoveEventEmitter(string name)
- {
- if (_eventEmitters[name].isRemovable)
- {
- _eventEmitters.Remove(name);
- }
- }
-
- public static void RegisterEngines(EnginesRoot enginesRoot)
- {
- _lastEngineRoot = enginesRoot;
- // Register handlers before emitters so no events are missed
- var entityFactory = enginesRoot.GenerateEntityFactory();
- foreach (var key in _eventHandlers.Keys)
- {
- Logging.MetaDebugLog($"Registering IEventHandlerEngine {_eventHandlers[key].Name}");
- enginesRoot.AddEngine(_eventHandlers[key]);
- }
- foreach (var key in _eventEmitters.Keys)
- {
- Logging.MetaDebugLog($"Registering IEventEmitterEngine {_eventEmitters[key].Name}");
- _eventEmitters[key].Factory = entityFactory;
- enginesRoot.AddEngine(_eventEmitters[key]);
- }
- }
- }
- }
|