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.

99 lines
3.5KB

  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. namespace GamecraftModdingAPI.Events
  8. {
  9. /// <summary>
  10. /// A simple implementation of IEventHandlerEngine sufficient for most uses
  11. /// </summary>
  12. public class SimpleEventHandlerEngine : IEventHandlerEngine
  13. {
  14. public int type { get; set; }
  15. public string Name { get; set; }
  16. private bool isActivated = false;
  17. private readonly Action<EntitiesDB> onActivated;
  18. private readonly Action<EntitiesDB> onDestroyed;
  19. public EntitiesDB entitiesDB { set; private get; }
  20. public void Add(ref ModEventEntityStruct entityView, EGID egid)
  21. {
  22. if (entityView.type.Equals(this.type))
  23. {
  24. isActivated = true;
  25. onActivated.Invoke(entitiesDB);
  26. }
  27. }
  28. /// <summary>
  29. /// Manually activate the EventHandler.
  30. /// Once activated, the next remove event will not be ignored.
  31. /// </summary>
  32. /// <param name="handle">Whether to invoke the activated action</param>
  33. public void Activate(bool handle = false)
  34. {
  35. isActivated = true;
  36. }
  37. public void Ready() { }
  38. public void Remove(ref ModEventEntityStruct entityView, EGID egid)
  39. {
  40. if (entityView.type.Equals(this.type) && isActivated)
  41. {
  42. isActivated = false;
  43. onDestroyed.Invoke(entitiesDB);
  44. }
  45. }
  46. public void Dispose()
  47. {
  48. if (isActivated)
  49. {
  50. isActivated = false;
  51. onDestroyed.Invoke(entitiesDB);
  52. }
  53. }
  54. /// <summary>
  55. /// Construct the engine
  56. /// </summary>
  57. /// <param name="activated">The operation to do when the event is created</param>
  58. /// <param name="removed">The operation to do when the event is destroyed (if applicable)</param>
  59. /// <param name="type">The type of event to handle</param>
  60. /// <param name="name">The name of the engine</param>
  61. /// <param name="simple">A useless parameter to use to avoid Python overload resolution errors</param>
  62. public SimpleEventHandlerEngine(Action activated, Action removed, int type, string name, bool simple = true)
  63. : this((EntitiesDB _) => { activated.Invoke(); }, (EntitiesDB _) => { removed.Invoke(); }, type, name) { }
  64. /// <summary>
  65. /// Construct the engine
  66. /// </summary>
  67. /// <param name="activated">The operation to do when the event is created</param>
  68. /// <param name="removed">The operation to do when the event is destroyed (if applicable)</param>
  69. /// <param name="type">The type of event to handler</param>
  70. /// <param name="name">The name of the engine</param>
  71. public SimpleEventHandlerEngine(Action<EntitiesDB> activated, Action<EntitiesDB> removed, int type, string name)
  72. {
  73. this.type = type;
  74. this.Name = name;
  75. this.onActivated = activated;
  76. this.onDestroyed = removed;
  77. }
  78. public SimpleEventHandlerEngine(Action activated, Action removed, EventType type, string name, bool simple = true)
  79. : this((EntitiesDB _) => { activated.Invoke(); }, (EntitiesDB _) => { removed.Invoke(); }, (int)type, name) { }
  80. public SimpleEventHandlerEngine(Action<EntitiesDB> activated, Action<EntitiesDB> removed, EventType type, string name, bool simple = true)
  81. : this(activated, removed, (int)type, name) { }
  82. }
  83. }