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.

SimpleEventHandlerEngine.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. using GamecraftModdingAPI.Utility;
  8. namespace GamecraftModdingAPI.Events
  9. {
  10. /// <summary>
  11. /// A simple implementation of IEventHandlerEngine sufficient for most uses
  12. /// </summary>
  13. public class SimpleEventHandlerEngine : IEventHandlerEngine
  14. {
  15. public int type { get; set; }
  16. public string Name { get; set; }
  17. private bool isActivated = false;
  18. private readonly Action<EntitiesDB> onActivated;
  19. private readonly Action<EntitiesDB> onDestroyed;
  20. public EntitiesDB entitiesDB { set; private get; }
  21. public bool isRemovable => true;
  22. public void Add(ref ModEventEntityStruct entityView, EGID egid)
  23. {
  24. if (entityView.type.Equals(this.type))
  25. {
  26. isActivated = true;
  27. onActivatedInvokeCatchError(entitiesDB);
  28. }
  29. }
  30. /// <summary>
  31. /// Manually activate the EventHandler.
  32. /// Once activated, the next remove event will not be ignored.
  33. /// </summary>
  34. /// <param name="handle">Whether to invoke the activated action</param>
  35. public void Activate(bool handle = false)
  36. {
  37. isActivated = true;
  38. if (handle && entitiesDB != null)
  39. {
  40. onActivatedInvokeCatchError(entitiesDB);
  41. }
  42. }
  43. public void Ready() { }
  44. public void Remove(ref ModEventEntityStruct entityView, EGID egid)
  45. {
  46. if (entityView.type.Equals(this.type) && isActivated)
  47. {
  48. isActivated = false;
  49. onDestroyedInvokeCatchError(entitiesDB);
  50. }
  51. }
  52. public void Dispose()
  53. {
  54. if (isActivated)
  55. {
  56. isActivated = false;
  57. onDestroyedInvokeCatchError(entitiesDB);
  58. }
  59. }
  60. /// <summary>
  61. /// Construct the engine
  62. /// </summary>
  63. /// <param name="activated">The operation to do when the event is created</param>
  64. /// <param name="removed">The operation to do when the event is destroyed (if applicable)</param>
  65. /// <param name="type">The type of event to handle</param>
  66. /// <param name="name">The name of the engine</param>
  67. /// <param name="simple">A useless parameter to use to avoid Python overload resolution errors</param>
  68. public SimpleEventHandlerEngine(Action activated, Action removed, int type, string name, bool simple = true)
  69. : this((EntitiesDB _) => { activated.Invoke(); }, (EntitiesDB _) => { removed.Invoke(); }, type, name) { }
  70. /// <summary>
  71. /// Construct the engine
  72. /// </summary>
  73. /// <param name="activated">The operation to do when the event is created</param>
  74. /// <param name="removed">The operation to do when the event is destroyed (if applicable)</param>
  75. /// <param name="type">The type of event to handler</param>
  76. /// <param name="name">The name of the engine</param>
  77. public SimpleEventHandlerEngine(Action<EntitiesDB> activated, Action<EntitiesDB> removed, int type, string name)
  78. {
  79. this.type = type;
  80. this.Name = name;
  81. this.onActivated = activated;
  82. this.onDestroyed = removed;
  83. }
  84. private void onActivatedInvokeCatchError(EntitiesDB _entitiesDB)
  85. {
  86. try
  87. {
  88. onActivated.Invoke(_entitiesDB);
  89. }
  90. catch (Exception e)
  91. {
  92. EventRuntimeException wrappedException = new EventRuntimeException($"EventHandler {Name} threw an exception when activated", e);
  93. Logging.LogWarning(wrappedException.ToString());
  94. }
  95. }
  96. private void onDestroyedInvokeCatchError(EntitiesDB _entitiesDB)
  97. {
  98. try
  99. {
  100. onDestroyed.Invoke(_entitiesDB);
  101. }
  102. catch (Exception e)
  103. {
  104. EventRuntimeException wrappedException = new EventRuntimeException($"EventHandler {Name} threw an exception when destroyed", e);
  105. Logging.LogWarning(wrappedException.ToString());
  106. }
  107. }
  108. public SimpleEventHandlerEngine(Action activated, Action removed, EventType type, string name, bool simple = true)
  109. : this((EntitiesDB _) => { activated.Invoke(); }, (EntitiesDB _) => { removed.Invoke(); }, (int)type, name) { }
  110. public SimpleEventHandlerEngine(Action<EntitiesDB> activated, Action<EntitiesDB> removed, EventType type, string name, bool simple = true)
  111. : this(activated, removed, (int)type, name) { }
  112. }
  113. }