EventComponent.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Threading.Tasks;
  5. using Common.Base;
  6. namespace Model
  7. {
  8. public class EventComponent<AttributeType>: Component<World>, IAssemblyLoader
  9. where AttributeType : AEventAttribute
  10. {
  11. private Dictionary<int, List<IEventSync>> eventSyncs;
  12. private Dictionary<int, List<IEventAsync>> eventAsyncs;
  13. public void Load(Assembly assembly)
  14. {
  15. this.eventSyncs = new Dictionary<int, List<IEventSync>>();
  16. this.eventAsyncs = new Dictionary<int, List<IEventAsync>>();
  17. ServerType serverType = World.Instance.Options.ServerType;
  18. Type[] types = assembly.GetTypes();
  19. foreach (Type t in types)
  20. {
  21. object[] attrs = t.GetCustomAttributes(typeof (AttributeType), false);
  22. if (attrs.Length == 0)
  23. {
  24. continue;
  25. }
  26. AEventAttribute aEventAttribute = (AEventAttribute) attrs[0];
  27. if (!aEventAttribute.Contains(serverType))
  28. {
  29. continue;
  30. }
  31. object obj = Activator.CreateInstance(t);
  32. IEventSync iEventSync = obj as IEventSync;
  33. if (iEventSync != null)
  34. {
  35. if (!this.eventSyncs.ContainsKey(aEventAttribute.Type))
  36. {
  37. this.eventSyncs.Add(aEventAttribute.Type, new List<IEventSync>());
  38. }
  39. this.eventSyncs[aEventAttribute.Type].Add(iEventSync);
  40. continue;
  41. }
  42. IEventAsync iEventAsync = obj as IEventAsync;
  43. // ReSharper disable once InvertIf
  44. if (iEventAsync != null)
  45. {
  46. if (!this.eventAsyncs.ContainsKey(aEventAttribute.Type))
  47. {
  48. this.eventAsyncs.Add(aEventAttribute.Type, new List<IEventAsync>());
  49. }
  50. this.eventAsyncs[aEventAttribute.Type].Add(iEventAsync);
  51. continue;
  52. }
  53. throw new Exception(string.Format("event not inherit IEventSync or IEventAsync interface: {0}",
  54. obj.GetType().FullName));
  55. }
  56. }
  57. public void Run(int type, Env env)
  58. {
  59. List<IEventSync> iEventSyncs = null;
  60. if (!this.eventSyncs.TryGetValue(type, out iEventSyncs))
  61. {
  62. throw new Exception(string.Format("no event handler, AttributeType: {0} type: {1}",
  63. typeof (AttributeType).Name, type));
  64. }
  65. foreach (IEventSync iEventSync in iEventSyncs)
  66. {
  67. iEventSync.Run(env);
  68. }
  69. }
  70. public async Task RunAsync(int type, Env env)
  71. {
  72. List<IEventSync> iEventSyncs = null;
  73. this.eventSyncs.TryGetValue(type, out iEventSyncs);
  74. List<IEventAsync> iEventAsyncs = null;
  75. this.eventAsyncs.TryGetValue(type, out iEventAsyncs);
  76. if (iEventSyncs == null && iEventAsyncs == null)
  77. {
  78. throw new Exception(string.Format("no event handler, AttributeType: {0} type: {1}",
  79. typeof (AttributeType).Name, type));
  80. }
  81. if (iEventSyncs != null)
  82. {
  83. foreach (IEventSync iEventSync in iEventSyncs)
  84. {
  85. iEventSync.Run(env);
  86. }
  87. }
  88. if (iEventAsyncs != null)
  89. {
  90. foreach (IEventAsync iEventAsync in iEventAsyncs)
  91. {
  92. await iEventAsync.RunAsync(env);
  93. }
  94. }
  95. }
  96. }
  97. }