EventComponent.cs 2.5 KB

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