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.

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