Logic.cs 2.8 KB

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