2019-12-16 00:35:59 +00:00
|
|
|
|
using System;
|
2019-12-14 04:42:55 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2019-12-16 00:35:59 +00:00
|
|
|
|
using Svelto.ECS;
|
|
|
|
|
|
2020-05-13 20:52:06 +00:00
|
|
|
|
using GamecraftModdingAPI.Utility;
|
|
|
|
|
|
2019-12-14 04:42:55 +00:00
|
|
|
|
namespace GamecraftModdingAPI.Events
|
|
|
|
|
{
|
2019-12-14 18:52:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A simple implementation of IEventHandlerEngine sufficient for most uses
|
|
|
|
|
/// </summary>
|
2019-12-15 07:20:20 +00:00
|
|
|
|
public class SimpleEventHandlerEngine : IEventHandlerEngine
|
2019-12-14 04:42:55 +00:00
|
|
|
|
{
|
2020-04-02 17:08:05 +00:00
|
|
|
|
public int type { get; set; }
|
2019-12-14 04:42:55 +00:00
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
2019-12-14 18:52:24 +00:00
|
|
|
|
private bool isActivated = false;
|
|
|
|
|
|
2020-03-12 22:36:23 +00:00
|
|
|
|
private readonly Action<EntitiesDB> onActivated;
|
2019-12-14 18:52:24 +00:00
|
|
|
|
|
2020-03-12 22:36:23 +00:00
|
|
|
|
private readonly Action<EntitiesDB> onDestroyed;
|
2019-12-14 04:42:55 +00:00
|
|
|
|
|
2020-03-12 22:36:23 +00:00
|
|
|
|
public EntitiesDB entitiesDB { set; private get; }
|
2019-12-14 04:42:55 +00:00
|
|
|
|
|
2020-05-12 00:28:26 +00:00
|
|
|
|
public bool isRemovable => true;
|
|
|
|
|
|
|
|
|
|
public void Add(ref ModEventEntityStruct entityView, EGID egid)
|
2019-12-14 04:42:55 +00:00
|
|
|
|
{
|
|
|
|
|
if (entityView.type.Equals(this.type))
|
|
|
|
|
{
|
2019-12-14 18:52:24 +00:00
|
|
|
|
isActivated = true;
|
2020-05-13 20:52:06 +00:00
|
|
|
|
onActivatedInvokeCatchError(entitiesDB);
|
2019-12-14 04:42:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-26 19:28:57 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Manually activate the EventHandler.
|
|
|
|
|
/// Once activated, the next remove event will not be ignored.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="handle">Whether to invoke the activated action</param>
|
|
|
|
|
public void Activate(bool handle = false)
|
|
|
|
|
{
|
|
|
|
|
isActivated = true;
|
2020-05-13 20:52:06 +00:00
|
|
|
|
if (handle && entitiesDB != null)
|
|
|
|
|
{
|
|
|
|
|
onActivatedInvokeCatchError(entitiesDB);
|
|
|
|
|
}
|
2020-01-26 19:28:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-14 04:42:55 +00:00
|
|
|
|
public void Ready() { }
|
|
|
|
|
|
2019-12-14 18:52:24 +00:00
|
|
|
|
public void Remove(ref ModEventEntityStruct entityView, EGID egid)
|
|
|
|
|
{
|
|
|
|
|
if (entityView.type.Equals(this.type) && isActivated)
|
|
|
|
|
{
|
|
|
|
|
isActivated = false;
|
2020-05-13 20:52:06 +00:00
|
|
|
|
onDestroyedInvokeCatchError(entitiesDB);
|
2019-12-14 18:52:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (isActivated)
|
|
|
|
|
{
|
|
|
|
|
isActivated = false;
|
2020-05-13 20:52:06 +00:00
|
|
|
|
onDestroyedInvokeCatchError(entitiesDB);
|
2019-12-14 18:52:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-14 04:42:55 +00:00
|
|
|
|
|
2019-12-14 18:52:24 +00:00
|
|
|
|
/// <summary>
|
2019-12-15 07:20:20 +00:00
|
|
|
|
/// Construct the engine
|
2019-12-14 18:52:24 +00:00
|
|
|
|
/// </summary>
|
2019-12-15 07:20:20 +00:00
|
|
|
|
/// <param name="activated">The operation to do when the event is created</param>
|
|
|
|
|
/// <param name="removed">The operation to do when the event is destroyed (if applicable)</param>
|
|
|
|
|
/// <param name="type">The type of event to handle</param>
|
|
|
|
|
/// <param name="name">The name of the engine</param>
|
2019-12-25 19:25:53 +00:00
|
|
|
|
/// <param name="simple">A useless parameter to use to avoid Python overload resolution errors</param>
|
2020-04-02 17:08:05 +00:00
|
|
|
|
public SimpleEventHandlerEngine(Action activated, Action removed, int type, string name, bool simple = true)
|
2020-03-12 22:36:23 +00:00
|
|
|
|
: this((EntitiesDB _) => { activated.Invoke(); }, (EntitiesDB _) => { removed.Invoke(); }, type, name) { }
|
2019-12-15 07:20:20 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Construct the engine
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="activated">The operation to do when the event is created</param>
|
|
|
|
|
/// <param name="removed">The operation to do when the event is destroyed (if applicable)</param>
|
|
|
|
|
/// <param name="type">The type of event to handler</param>
|
|
|
|
|
/// <param name="name">The name of the engine</param>
|
2020-04-02 17:08:05 +00:00
|
|
|
|
public SimpleEventHandlerEngine(Action<EntitiesDB> activated, Action<EntitiesDB> removed, int type, string name)
|
2019-12-14 04:42:55 +00:00
|
|
|
|
{
|
|
|
|
|
this.type = type;
|
|
|
|
|
this.Name = name;
|
2019-12-14 18:52:24 +00:00
|
|
|
|
this.onActivated = activated;
|
|
|
|
|
this.onDestroyed = removed;
|
2019-12-14 04:42:55 +00:00
|
|
|
|
}
|
2020-04-02 17:08:05 +00:00
|
|
|
|
|
2020-05-13 20:52:06 +00:00
|
|
|
|
private void onActivatedInvokeCatchError(EntitiesDB _entitiesDB)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
onActivated.Invoke(_entitiesDB);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
EventRuntimeException wrappedException = new EventRuntimeException($"EventHandler {Name} threw an exception when activated", e);
|
|
|
|
|
Logging.LogWarning(wrappedException.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onDestroyedInvokeCatchError(EntitiesDB _entitiesDB)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
onDestroyed.Invoke(_entitiesDB);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
EventRuntimeException wrappedException = new EventRuntimeException($"EventHandler {Name} threw an exception when destroyed", e);
|
|
|
|
|
Logging.LogWarning(wrappedException.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-02 17:08:05 +00:00
|
|
|
|
public SimpleEventHandlerEngine(Action activated, Action removed, EventType type, string name, bool simple = true)
|
|
|
|
|
: this((EntitiesDB _) => { activated.Invoke(); }, (EntitiesDB _) => { removed.Invoke(); }, (int)type, name) { }
|
|
|
|
|
|
|
|
|
|
public SimpleEventHandlerEngine(Action<EntitiesDB> activated, Action<EntitiesDB> removed, EventType type, string name, bool simple = true)
|
|
|
|
|
: this(activated, removed, (int)type, name) { }
|
2019-12-14 04:42:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|