CrossComponent.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Model
  4. {
  5. [ObjectEvent]
  6. public class CrossComponentEvent : ObjectEvent<CrossComponent>, IAwake
  7. {
  8. public void Awake()
  9. {
  10. this.Get().Awake();
  11. }
  12. }
  13. /// <summary>
  14. /// 事件分发,可以将事件分发到IL层
  15. /// </summary>
  16. public class CrossComponent : Component
  17. {
  18. private Dictionary<int, List<IInstanceMethod>> allEvents;
  19. public void Awake()
  20. {
  21. Load();
  22. }
  23. private void Load()
  24. {
  25. allEvents = new Dictionary<int, List<IInstanceMethod>>();
  26. Type[] types = DllHelper.GetHotfixTypes();
  27. foreach (Type type in types)
  28. {
  29. object[] attrs = type.GetCustomAttributes(typeof(CrossEventAttribute), false);
  30. foreach (object attr in attrs)
  31. {
  32. CrossEventAttribute aEventAttribute = (CrossEventAttribute)attr;
  33. #if ILRuntime
  34. IInstanceMethod method = new ILInstanceMethod(type, "Run");
  35. #else
  36. IInstanceMethod method = new MonoInstanceMethod(type, "Run");
  37. #endif
  38. if (!allEvents.ContainsKey(aEventAttribute.Type))
  39. {
  40. allEvents.Add(aEventAttribute.Type, new List<IInstanceMethod>());
  41. }
  42. allEvents[aEventAttribute.Type].Add(method);
  43. }
  44. }
  45. }
  46. public void Run(CrossIdType type)
  47. {
  48. List<IInstanceMethod> iEvents = null;
  49. if (!allEvents.TryGetValue((int)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>(CrossIdType type, A a)
  66. {
  67. List<IInstanceMethod> iEvents = null;
  68. if (!this.allEvents.TryGetValue((int)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>(CrossIdType type, A a, B b)
  85. {
  86. List<IInstanceMethod> iEvents = null;
  87. if (!this.allEvents.TryGetValue((int)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>(CrossIdType type, A a, B b, C c)
  104. {
  105. List<IInstanceMethod> iEvents = null;
  106. if (!this.allEvents.TryGetValue((int)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. }