EventComponent.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using Model;
  4. namespace Model
  5. {
  6. [EntityEvent(EntityEventId.EventComponent)]
  7. public class EventComponent : Component
  8. {
  9. private Dictionary<int, List<object>> allEvents;
  10. private void Awake()
  11. {
  12. this.Load();
  13. }
  14. private 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. Log.Debug(obj.GetType().FullName);
  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>(int type, A a)
  55. {
  56. List<object> iEvents = null;
  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>(int type, A a, B b)
  75. {
  76. List<object> iEvents = null;
  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>(int type, A a, B b, C c)
  95. {
  96. List<object> iEvents = null;
  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. }