EventComponent.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using Model;
  4. namespace Hotfix
  5. {
  6. [ObjectEvent(EntityEventId.EventComponent)]
  7. public class EventComponent : Component, IAwake
  8. {
  9. private Dictionary<int, List<object>> allEvents;
  10. public void Awake()
  11. {
  12. this.Load();
  13. }
  14. public void Load()
  15. {
  16. this.allEvents = new Dictionary<int, List<object>>();
  17. Type[] types = DllHelper.GetHotfixTypes();
  18. foreach (Type type in types)
  19. {
  20. object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
  21. foreach (object attr in attrs)
  22. {
  23. EventAttribute aEventAttribute = (EventAttribute)attr;
  24. object obj = Activator.CreateInstance(type);
  25. if (!this.allEvents.ContainsKey(aEventAttribute.Type))
  26. {
  27. this.allEvents.Add(aEventAttribute.Type, new List<object>());
  28. }
  29. this.allEvents[aEventAttribute.Type].Add(obj);
  30. }
  31. }
  32. }
  33. public void Run(int type)
  34. {
  35. if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
  36. {
  37. return;
  38. }
  39. foreach (object obj in iEvents)
  40. {
  41. try
  42. {
  43. IEvent iEvent = (IEvent)obj;
  44. iEvent.Run();
  45. }
  46. catch (Exception e)
  47. {
  48. Log.Error(e.ToString());
  49. }
  50. }
  51. }
  52. public void Run<A>(int type, A a)
  53. {
  54. if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
  55. {
  56. return;
  57. }
  58. foreach (object obj in iEvents)
  59. {
  60. try
  61. {
  62. IEvent<A> iEvent = (IEvent<A>)obj;
  63. iEvent.Run(a);
  64. }
  65. catch (Exception err)
  66. {
  67. Log.Error(err.ToString());
  68. }
  69. }
  70. }
  71. public void Run<A, B>(int type, A a, B b)
  72. {
  73. if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
  74. {
  75. return;
  76. }
  77. foreach (object obj in iEvents)
  78. {
  79. try
  80. {
  81. IEvent<A, B> iEvent = (IEvent<A, B>)obj;
  82. iEvent.Run(a, b);
  83. }
  84. catch (Exception err)
  85. {
  86. Log.Error(err.ToString());
  87. }
  88. }
  89. }
  90. public void Run<A, B, C>(int type, A a, B b, C c)
  91. {
  92. if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
  93. {
  94. return;
  95. }
  96. foreach (object obj in iEvents)
  97. {
  98. try
  99. {
  100. IEvent<A, B, C> iEvent = (IEvent<A, B, C>)obj;
  101. iEvent.Run(a, b, c);
  102. }
  103. catch (Exception err)
  104. {
  105. Log.Error(err.ToString());
  106. }
  107. }
  108. }
  109. }
  110. }