TechbloxModdingAPI/GamecraftModdingAPI/Events/EventManager.cs

130 lines
4 KiB
C#
Raw Normal View History

2019-12-14 04:42:55 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2019-12-14 04:42:55 +00:00
using Svelto.ECS;
2019-12-17 01:55:52 +00:00
using GamecraftModdingAPI.Utility;
2019-12-14 04:42:55 +00:00
namespace GamecraftModdingAPI.Events
{
/// <summary>
/// Keeps track of event handlers and emitters.
2019-12-15 07:20:20 +00:00
/// This is used to add, remove and get API event handlers and emitters.
2019-12-14 04:42:55 +00:00
/// </summary>
[Obsolete("This will be removed in an upcoming update. Use the new C# event architecture from GamecraftModdingAPI.App")]
2019-12-15 07:20:20 +00:00
public static class EventManager
2019-12-14 04:42:55 +00:00
{
private static Dictionary<string, IEventEmitterEngine> _eventEmitters = new Dictionary<string, IEventEmitterEngine>();
private static Dictionary<string, IEventHandlerEngine> _eventHandlers = new Dictionary<string, IEventHandlerEngine>();
private static EnginesRoot _lastEngineRoot;
2019-12-14 04:42:55 +00:00
// event handler management
public static void AddEventHandler(IEventHandlerEngine engine)
{
if (ExistsEventHandler(engine))
{
throw new EventAlreadyExistsException($"IEventHandlerEngine {engine.Name} already exists");
}
2019-12-14 04:42:55 +00:00
_eventHandlers[engine.Name] = engine;
if (_lastEngineRoot != null)
{
Logging.MetaDebugLog($"Registering IEventHandlerEngine {engine.Name}");
_lastEngineRoot.AddEngine(engine);
}
2019-12-14 04:42:55 +00:00
}
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()
2019-12-14 04:42:55 +00:00
{
return _eventHandlers.Keys.ToArray();
2019-12-14 04:42:55 +00:00
}
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");
}
2019-12-14 04:42:55 +00:00
_eventEmitters[engine.Name] = engine;
if (_lastEngineRoot != null)
{
Logging.MetaDebugLog($"Registering IEventEmitterEngine {engine.Name}");
_lastEngineRoot.AddEngine(engine);
}
2019-12-14 04:42:55 +00:00
}
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()
2019-12-14 04:42:55 +00:00
{
return _eventEmitters.Keys.ToArray();
2019-12-14 04:42:55 +00:00
}
public static void RemoveEventEmitter(string name)
{
2019-12-14 18:52:24 +00:00
if (_eventEmitters[name].isRemovable)
{
_eventEmitters.Remove(name);
}
2019-12-14 04:42:55 +00:00
}
public static void RegisterEngines(EnginesRoot enginesRoot)
{
_lastEngineRoot = enginesRoot;
2019-12-14 04:42:55 +00:00
// Register handlers before emitters so no events are missed
var entityFactory = enginesRoot.GenerateEntityFactory();
foreach (var key in _eventHandlers.Keys)
{
2019-12-17 01:55:52 +00:00
Logging.MetaDebugLog($"Registering IEventHandlerEngine {_eventHandlers[key].Name}");
2019-12-14 04:42:55 +00:00
enginesRoot.AddEngine(_eventHandlers[key]);
}
foreach (var key in _eventEmitters.Keys)
{
2019-12-17 01:55:52 +00:00
Logging.MetaDebugLog($"Registering IEventEmitterEngine {_eventEmitters[key].Name}");
2019-12-14 04:42:55 +00:00
_eventEmitters[key].Factory = entityFactory;
enginesRoot.AddEngine(_eventEmitters[key]);
}
}
}
}