LogicManager.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Helper;
  5. using Log;
  6. namespace Component
  7. {
  8. public class LogicManager : ILogic
  9. {
  10. private static readonly LogicManager instance = new LogicManager();
  11. private Dictionary<int, Tuple<IHandler, Type>> handlers;
  12. private Dictionary<EventType, SortedDictionary<int, IEvent>> events;
  13. public static LogicManager Instance
  14. {
  15. get
  16. {
  17. return instance;
  18. }
  19. }
  20. private LogicManager()
  21. {
  22. this.Load();
  23. }
  24. private void Load()
  25. {
  26. string dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logic.dll");
  27. var assembly = LoaderHelper.Load(dllPath);
  28. Type[] types = assembly.GetTypes();
  29. // 加载封包处理器
  30. var localHandlers = new Dictionary<int, Tuple<IHandler, Type>>();
  31. foreach (var type in types)
  32. {
  33. object[] attrs = type.GetCustomAttributes(typeof(HandlerAttribute), false);
  34. if (attrs.Length == 0)
  35. {
  36. continue;
  37. }
  38. var handler = (IHandler)Activator.CreateInstance(type);
  39. int opcode = ((HandlerAttribute)attrs[0]).Opcode;
  40. Type messageType = ((HandlerAttribute)attrs[0]).Type;
  41. if (opcode == 0 || messageType == null)
  42. {
  43. throw new Exception(string.Format("not set opcode or type, handler name: {0}", type.Name));
  44. }
  45. if (localHandlers.ContainsKey(opcode))
  46. {
  47. throw new Exception(string.Format(
  48. "same handler opcode, opcode: {0}, name: {1}",
  49. opcode, type.Name));
  50. }
  51. localHandlers[opcode] = new Tuple<IHandler, Type>(handler, messageType);
  52. }
  53. // 加载事件处理器
  54. var localEvents = new Dictionary<EventType, SortedDictionary<int, IEvent>>();
  55. foreach (var type in types)
  56. {
  57. object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
  58. if (attrs.Length == 0)
  59. {
  60. continue;
  61. }
  62. var evt = (IEvent)Activator.CreateInstance(type);
  63. EventType eventType = ((EventAttribute)attrs[0]).Type;
  64. int eventOrder = ((EventAttribute)attrs[0]).Order;
  65. if (eventOrder == 0 || eventType == EventType.DefaultEvent)
  66. {
  67. throw new Exception(string.Format("not set order or type, event name: {0}", type.Name));
  68. }
  69. if (!localEvents.ContainsKey(eventType))
  70. {
  71. localEvents[eventType] = new SortedDictionary<int, IEvent>();
  72. }
  73. if (localEvents[eventType].ContainsKey(eventOrder))
  74. {
  75. throw new Exception(string.Format(
  76. "same event number, type: {0}, number: {1}, name: {2}",
  77. eventType, eventOrder, type.Name));
  78. }
  79. localEvents[eventType][eventOrder] = evt;
  80. }
  81. //
  82. this.handlers = localHandlers;
  83. this.events = localEvents;
  84. }
  85. public void Reload()
  86. {
  87. this.Load();
  88. }
  89. public void Handle(short opcode, byte[] content)
  90. {
  91. Tuple<IHandler, Type> tuple = null;
  92. if (!handlers.TryGetValue(opcode, out tuple))
  93. {
  94. throw new Exception(string.Format("not found handler opcode {0}", opcode));
  95. }
  96. try
  97. {
  98. object message = MongoHelper.FromBson(content, tuple.Item2);
  99. var messageEnv = new MessageEnv();
  100. messageEnv[KeyDefine.KMessage] = message;
  101. tuple.Item1.Handle(messageEnv);
  102. }
  103. catch (Exception e)
  104. {
  105. Logger.Trace("message handle error: {0}", e.Message);
  106. }
  107. }
  108. public void Trigger(MessageEnv messageEnv, EventType type)
  109. {
  110. SortedDictionary<int, IEvent> iEventDict = null;
  111. if (!this.events.TryGetValue(type, out iEventDict))
  112. {
  113. return;
  114. }
  115. foreach (var iEvent in iEventDict)
  116. {
  117. iEvent.Value.Trigger(messageEnv);
  118. }
  119. }
  120. }
  121. }