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.

146 lines
4.8KB

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