EventComponent.cs 2.5 KB

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