CrossComponent.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Model
  4. {
  5. [ObjectEvent]
  6. public class CrossComponentEvent : ObjectEvent<CrossComponent>, 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. /// <summary>
  18. /// 事件分发,可以将事件分发到IL层
  19. /// </summary>
  20. public class CrossComponent : Component
  21. {
  22. private Dictionary<int, List<IInstanceMethod>> allEvents;
  23. public void Awake()
  24. {
  25. this.Load();
  26. }
  27. public void Load()
  28. {
  29. this.allEvents = new Dictionary<int, List<IInstanceMethod>>();
  30. Type[] types = DllHelper.GetHotfixTypes();
  31. foreach (Type type in types)
  32. {
  33. object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
  34. foreach (object attr in attrs)
  35. {
  36. EventAttribute aEventAttribute = (EventAttribute)attr;
  37. IInstanceMethod method = new ILInstanceMethod(type, "Run");
  38. if (!this.allEvents.ContainsKey(aEventAttribute.Type))
  39. {
  40. this.allEvents.Add(aEventAttribute.Type, new List<IInstanceMethod>());
  41. }
  42. this.allEvents[aEventAttribute.Type].Add(method);
  43. }
  44. }
  45. }
  46. public void Run(int type)
  47. {
  48. List<IInstanceMethod> iEvents = null;
  49. if (!this.allEvents.TryGetValue(type, out iEvents))
  50. {
  51. return;
  52. }
  53. foreach (IInstanceMethod obj in iEvents)
  54. {
  55. try
  56. {
  57. obj.Run();
  58. }
  59. catch (Exception err)
  60. {
  61. Log.Error(err.ToString());
  62. }
  63. }
  64. }
  65. public void Run<A>(int type, A a)
  66. {
  67. List<IInstanceMethod> iEvents = null;
  68. if (!this.allEvents.TryGetValue(type, out iEvents))
  69. {
  70. return;
  71. }
  72. foreach (IInstanceMethod obj in iEvents)
  73. {
  74. try
  75. {
  76. obj.Run(a);
  77. }
  78. catch (Exception err)
  79. {
  80. Log.Error(err.ToString());
  81. }
  82. }
  83. }
  84. public void Run<A, B>(int type, A a, B b)
  85. {
  86. List<IInstanceMethod> iEvents = null;
  87. if (!this.allEvents.TryGetValue(type, out iEvents))
  88. {
  89. return;
  90. }
  91. foreach (IInstanceMethod obj in iEvents)
  92. {
  93. try
  94. {
  95. obj.Run(a, b);
  96. }
  97. catch (Exception err)
  98. {
  99. Log.Error(err.ToString());
  100. }
  101. }
  102. }
  103. public void Run<A, B, C>(int type, A a, B b, C c)
  104. {
  105. List<IInstanceMethod> iEvents = null;
  106. if (!this.allEvents.TryGetValue(type, out iEvents))
  107. {
  108. return;
  109. }
  110. foreach (IInstanceMethod obj in iEvents)
  111. {
  112. try
  113. {
  114. obj.Run(a, b, c);
  115. }
  116. catch (Exception err)
  117. {
  118. Log.Error(err.ToString());
  119. }
  120. }
  121. }
  122. }
  123. }