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.

93 lines
3.1KB

  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 object type { get; set; }
  15. public string Name { get; set; }
  16. private bool isActivated = false;
  17. private readonly Action<IEntitiesDB> onActivated;
  18. private readonly Action<IEntitiesDB> onDestroyed;
  19. public IEntitiesDB 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, object type, string name, bool simple = true)
  63. : this((IEntitiesDB _) => { activated.Invoke(); }, (IEntitiesDB _) => { 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<IEntitiesDB> activated, Action<IEntitiesDB> removed, object type, string name)
  72. {
  73. this.type = type;
  74. this.Name = name;
  75. this.onActivated = activated;
  76. this.onDestroyed = removed;
  77. }
  78. }
  79. }