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-16 00:35:59 +00:00
|
|
|
|
|
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>
|
2020-08-03 02:39:26 +00:00
|
|
|
|
[Obsolete]
|
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>();
|
|
|
|
|
|
2020-01-26 19:28:57 +00:00
|
|
|
|
private static EnginesRoot _lastEngineRoot;
|
|
|
|
|
|
2019-12-14 04:42:55 +00:00
|
|
|
|
// event handler management
|
|
|
|
|
|
|
|
|
|
public static void AddEventHandler(IEventHandlerEngine engine)
|
|
|
|
|
{
|
2020-05-13 20:52:06 +00:00
|
|
|
|
if (ExistsEventHandler(engine))
|
|
|
|
|
{
|
|
|
|
|
throw new EventAlreadyExistsException($"IEventHandlerEngine {engine.Name} already exists");
|
|
|
|
|
}
|
2019-12-14 04:42:55 +00:00
|
|
|
|
_eventHandlers[engine.Name] = engine;
|
2020-01-26 19:28:57 +00:00
|
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 00:35:59 +00:00
|
|
|
|
public static string[] GetEventHandlerNames()
|
2019-12-14 04:42:55 +00:00
|
|
|
|
{
|
2019-12-16 00:35:59 +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)
|
|
|
|
|
{
|
2020-05-13 20:52:06 +00:00
|
|
|
|
if (ExistsEventEmitter(engine))
|
|
|
|
|
{
|
|
|
|
|
throw new EventAlreadyExistsException($"IEventEmitterEngine {engine.Name} already exists");
|
|
|
|
|
}
|
2019-12-14 04:42:55 +00:00
|
|
|
|
_eventEmitters[engine.Name] = engine;
|
2020-01-26 19:28:57 +00:00
|
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 00:35:59 +00:00
|
|
|
|
public static string[] GetEventEmitterNames()
|
2019-12-14 04:42:55 +00:00
|
|
|
|
{
|
2019-12-16 00:35:59 +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)
|
|
|
|
|
{
|
2020-01-26 19:28:57 +00:00
|
|
|
|
_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]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|