LogicManager.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, IHandler> 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. this.handlers = new Dictionary<int, IHandler>();
  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. if (this.handlers.ContainsKey(opcode))
  41. {
  42. throw new Exception(string.Format(
  43. "same handler opcode, opcode: {0}, name: {1}", opcode, type.Name));
  44. }
  45. this.handlers[opcode] = handler;
  46. }
  47. // 加载事件处理器
  48. this.events = new Dictionary<EventType, SortedDictionary<int, IEvent>>();
  49. foreach (var type in types)
  50. {
  51. object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
  52. if (attrs.Length == 0)
  53. {
  54. continue;
  55. }
  56. var evt = (IEvent)Activator.CreateInstance(type);
  57. var eventType = ((EventAttribute)attrs[0]).Type;
  58. var eventNumber = ((EventAttribute)attrs[0]).Number;
  59. if (!this.events.ContainsKey(eventType))
  60. {
  61. this.events[eventType] = new SortedDictionary<int, IEvent>();
  62. }
  63. if (this.events[eventType].ContainsKey(eventNumber))
  64. {
  65. throw new Exception(string.Format(
  66. "same event number, type: {0}, number: {1}, name: {2}",
  67. eventType, eventNumber, type.Name));
  68. }
  69. this.events[eventType][eventNumber] = evt;
  70. }
  71. }
  72. public void Reload()
  73. {
  74. this.Load();
  75. }
  76. public void Handle(short opcode, byte[] content)
  77. {
  78. IHandler handler = null;
  79. if (!handlers.TryGetValue(opcode, out handler))
  80. {
  81. throw new Exception(string.Format("not found handler opcode {0}", opcode));
  82. }
  83. try
  84. {
  85. var messageEnv = new MessageEnv();
  86. handler.Handle(messageEnv, content);
  87. }
  88. catch (Exception e)
  89. {
  90. Logger.Trace("message handle error: {0}", e.Message);
  91. }
  92. }
  93. public void Trigger(MessageEnv messageEnv, EventType type)
  94. {
  95. SortedDictionary<int, IEvent> iEventDict = null;
  96. if (!this.events.TryGetValue(type, out iEventDict))
  97. {
  98. return;
  99. }
  100. foreach (var iEvent in iEventDict)
  101. {
  102. iEvent.Value.Trigger(messageEnv);
  103. }
  104. }
  105. }
  106. }