EventComponent.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using Model;
  4. namespace Hotfix
  5. {
  6. [EntityEvent(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.GetAllTypes();
  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. List<object> iEvents = null;
  36. if (!this.allEvents.TryGetValue(type, out iEvents))
  37. {
  38. return;
  39. }
  40. foreach (object obj in iEvents)
  41. {
  42. try
  43. {
  44. IEvent iEvent = (IEvent)obj;
  45. iEvent.Run();
  46. }
  47. catch (Exception e)
  48. {
  49. Log.Error(e.ToString());
  50. }
  51. }
  52. }
  53. public void Run<A>(int type, A a)
  54. {
  55. List<object> iEvents = null;
  56. if (!this.allEvents.TryGetValue(type, out iEvents))
  57. {
  58. return;
  59. }
  60. foreach (object obj in iEvents)
  61. {
  62. try
  63. {
  64. IEvent<A> iEvent = (IEvent<A>)obj;
  65. iEvent.Run(a);
  66. }
  67. catch (Exception err)
  68. {
  69. Log.Error(err.ToString());
  70. }
  71. }
  72. }
  73. public void Run<A, B>(int type, A a, B b)
  74. {
  75. List<object> iEvents = null;
  76. if (!this.allEvents.TryGetValue(type, out iEvents))
  77. {
  78. return;
  79. }
  80. foreach (object obj in iEvents)
  81. {
  82. try
  83. {
  84. IEvent<A, B> iEvent = (IEvent<A, B>)obj;
  85. iEvent.Run(a, b);
  86. }
  87. catch (Exception err)
  88. {
  89. Log.Error(err.ToString());
  90. }
  91. }
  92. }
  93. public void Run<A, B, C>(int type, A a, B b, C c)
  94. {
  95. List<object> iEvents = null;
  96. if (!this.allEvents.TryGetValue(type, out iEvents))
  97. {
  98. return;
  99. }
  100. foreach (object obj in iEvents)
  101. {
  102. try
  103. {
  104. IEvent<A, B, C> iEvent = (IEvent<A, B, C>)obj;
  105. iEvent.Run(a, b, c);
  106. }
  107. catch (Exception err)
  108. {
  109. Log.Error(err.ToString());
  110. }
  111. }
  112. }
  113. }
  114. }