EventComponent.cs 2.5 KB

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