EventComponent.cs 2.4 KB

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