| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- using System;
- using System.Collections.Generic;
- using Model;
- namespace Hotfix
- {
- [ObjectSystem]
- public class MessageDispatherComponentSystem : ObjectSystem<MessageDispatherComponent>, IAwake, ILoad
- {
- public void Awake()
- {
- this.Get().Awake();
- }
- public void Load()
- {
- this.Get().Load();
- }
- }
- /// <summary>
- /// 消息分发组件
- /// </summary>
- public class MessageDispatherComponent : Component
- {
- private readonly Dictionary<ushort, List<IMHandler>> handlers = new Dictionary<ushort, List<IMHandler>>();
- public void Awake()
- {
- this.Load();
- }
- public void Load()
- {
- this.handlers.Clear();
- Model.MessageDispatherComponent messageDispatherComponent = Game.Scene.GetComponent<Model.MessageDispatherComponent>();
- Model.OpcodeTypeComponent opcodeTypeComponent = Game.Scene.GetComponent<Model.OpcodeTypeComponent>();
- Type[] types = DllHelper.GetHotfixTypes();
- foreach (Type type in types)
- {
- object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
- if (attrs.Length == 0)
- {
- continue;
- }
- IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler;
- if (iMHandler == null)
- {
- Log.Error($"message handle {type.Name} 需要继承 IMHandler");
- continue;
- }
-
- Type messageType = iMHandler.GetMessageType();
- ushort opcode = this.Entity.GetComponent<OpcodeTypeComponent>().GetOpcode(messageType);
- if (opcode != 0)
- {
- this.RegisterHandler(opcode, iMHandler);
- }
- // 尝试注册到mono层
- if (messageDispatherComponent != null && opcodeTypeComponent != null)
- {
- ushort monoOpcode = opcodeTypeComponent.GetOpcode(messageType);
- if (monoOpcode == 0)
- {
- continue;
- }
- MessageProxy messageProxy = new MessageProxy(messageType, (session, o) => { iMHandler.Handle(session, o); });
- messageDispatherComponent.RegisterHandler(monoOpcode, messageProxy);
- }
- }
- }
- public void RegisterHandler(ushort opcode, IMHandler handler)
- {
- if (!this.handlers.ContainsKey(opcode))
- {
- this.handlers.Add(opcode, new List<IMHandler>());
- }
- this.handlers[opcode].Add(handler);
- }
- public void Handle(Session session, ushort opcode, object message)
- {
- if (!this.handlers.TryGetValue(opcode, out List<IMHandler> actions))
- {
- Log.Error($"消息 {message.GetType().FullName} 没有处理");
- return;
- }
-
- foreach (IMHandler ev in actions)
- {
- try
- {
- ev.Handle(null, message);
- }
- catch (Exception e)
- {
- Log.Error(e.ToString());
- }
- }
- }
- public override void Dispose()
- {
- if (this.Id == 0)
- {
- return;
- }
- base.Dispose();
- }
- }
- }
|