Просмотр исходного кода

消息也需要指定SceneType

tanghai 3 лет назад
Родитель
Сommit
5de83a59ac
38 измененных файлов с 131 добавлено и 147 удалено
  1. 40 16
      Apps/Hotfix/Module/Actor/ActorMessageDispatcherComponentSystem.cs
  2. 1 1
      Apps/Hotfix/Module/ActorLocation/ObjectAddRequestHandler.cs
  3. 1 1
      Apps/Hotfix/Module/ActorLocation/ObjectGetRequestHandler.cs
  4. 1 1
      Apps/Hotfix/Module/ActorLocation/ObjectLockRequestHandler.cs
  5. 1 1
      Apps/Hotfix/Module/ActorLocation/ObjectRemoveRequestHandler.cs
  6. 1 1
      Apps/Hotfix/Module/ActorLocation/ObjectUnLockRequestHandler.cs
  7. 1 1
      Apps/Hotfix/Server/AOI/AOIEntitySystem.cs
  8. 0 44
      Apps/Hotfix/Server/Actor_TransferHandler.cs
  9. 1 1
      Apps/Hotfix/Server/C2G_EnterMapHandler.cs
  10. 1 1
      Apps/Hotfix/Server/C2G_LoginGateHandler.cs
  11. 1 1
      Apps/Hotfix/Server/C2G_PingHandler.cs
  12. 1 1
      Apps/Hotfix/Server/C2M_PathfindingResultHandler.cs
  13. 0 29
      Apps/Hotfix/Server/C2M_ReloadHandler.cs
  14. 1 1
      Apps/Hotfix/Server/C2M_StopHandler.cs
  15. 1 0
      Apps/Hotfix/Server/C2M_TestRobotCaseHandler.cs
  16. 1 1
      Apps/Hotfix/Server/C2R_LoginHandler.cs
  17. 1 1
      Apps/Hotfix/Server/G2M_SessionDisconnectHandler.cs
  18. 1 1
      Apps/Hotfix/Server/M2M_UnitTransferRequestHandler.cs
  19. 1 1
      Apps/Hotfix/Server/R2G_GetLoginKeyHandler.cs
  20. 1 1
      Apps/Hotfix/Server/Scene/C2M_TransferMapHandler.cs
  21. 0 1
      Apps/Model/Module/Actor/AMActorHandler.cs
  22. 0 1
      Apps/Model/Module/Actor/AMActorRpcHandler.cs
  23. 0 14
      Apps/Model/Module/Actor/ActorMessageAttribute.cs
  24. 14 1
      Apps/Model/Module/Actor/ActorMessageDispatcherComponent.cs
  25. 6 0
      Apps/Model/Module/Actor/ActorMessageHandlerAttribute.cs
  26. 0 1
      Apps/Model/Module/ActorLocation/AMActorLocationHandler.cs
  27. 0 1
      Apps/Model/Module/ActorLocation/AMActorLocationRpcHandler.cs
  28. 0 1
      Apps/Model/Module/NetworkTCP/AMRpcHandler.cs
  29. 1 1
      Unity/Codes/Hotfix/Client/Move/M2C_PathfindingResultHandler.cs
  30. 1 1
      Unity/Codes/Hotfix/Client/Move/M2C_StopHandler.cs
  31. 1 1
      Unity/Codes/Hotfix/Client/Scene/M2C_StartSceneChangeHandler.cs
  32. 1 1
      Unity/Codes/Hotfix/Client/Unit/M2C_CreateMyUnitHandler.cs
  33. 1 1
      Unity/Codes/Hotfix/Client/Unit/M2C_CreateUnitsHandler.cs
  34. 1 1
      Unity/Codes/Hotfix/Client/Unit/M2C_RemoveUnitsHandler.cs
  35. 29 14
      Unity/Codes/Hotfix/Module/Message/MessageDispatcherComponentSystem.cs
  36. 0 1
      Unity/Codes/Model/Module/Message/AMHandler.cs
  37. 13 1
      Unity/Codes/Model/Module/Message/MessageDispatcherComponent.cs
  38. 6 0
      Unity/Codes/Model/Module/Message/MessageHandlerAttribute.cs

+ 40 - 16
Apps/Hotfix/Module/Actor/ActorMessageDispatcherComponentSystem.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Collections.Generic;
 
 namespace ET
 {
@@ -56,21 +57,40 @@ namespace ET
                 {
                     throw new Exception($"message handler not inherit IMActorHandler abstract class: {obj.GetType().FullName}");
                 }
-
-                Type messageType = imHandler.GetRequestType();
                 
-                Type handleResponseType = imHandler.GetResponseType();
-                if (handleResponseType != null)
+                object[] attrs = type.GetCustomAttributes(typeof(ActorMessageHandlerAttribute), false);
+
+                foreach (object attr in attrs)
                 {
-                    Type responseType = OpcodeTypeComponent.Instance.GetResponseType(messageType);
-                    if (handleResponseType != responseType)
+                    ActorMessageHandlerAttribute actorMessageHandlerAttribute = attr as ActorMessageHandlerAttribute;
+
+                    Type messageType = imHandler.GetRequestType();
+
+                    Type handleResponseType = imHandler.GetResponseType();
+                    if (handleResponseType != null)
                     {
-                        throw new Exception($"message handler response type error: {messageType.FullName}");
+                        Type responseType = OpcodeTypeComponent.Instance.GetResponseType(messageType);
+                        if (handleResponseType != responseType)
+                        {
+                            throw new Exception($"message handler response type error: {messageType.FullName}");
+                        }
                     }
-                }
 
-                self.ActorMessageHandlers.Add(messageType, imHandler);
+                    ActorMessageDispatcherInfo actorMessageDispatcherInfo = new ActorMessageDispatcherInfo(actorMessageHandlerAttribute.SceneType, imHandler);
+
+                    self.RegisterHandler(messageType, actorMessageDispatcherInfo);
+                }
+            }
+        }
+        
+        private static void RegisterHandler(this ActorMessageDispatcherComponent self, Type type, ActorMessageDispatcherInfo handler)
+        {
+            if (!self.ActorMessageHandlers.ContainsKey(type))
+            {
+                self.ActorMessageHandlers.Add(type, new List<ActorMessageDispatcherInfo>());
             }
+
+            self.ActorMessageHandlers[type].Add(handler);
         }
 
         /// <summary>
@@ -79,17 +99,21 @@ namespace ET
         public static async ETTask Handle(
             this ActorMessageDispatcherComponent self, Entity entity, object message, Action<IActorResponse> reply)
         {
-            if (!self.ActorMessageHandlers.TryGetValue(message.GetType(), out IMActorHandler handler))
+            List<ActorMessageDispatcherInfo> list;
+            if (!self.ActorMessageHandlers.TryGetValue(message.GetType(), out list))
             {
                 throw new Exception($"not found message handler: {message}");
             }
 
-            await handler.Handle(entity, message, reply);
-        }
-
-        public static bool TryGetHandler(this ActorMessageDispatcherComponent self,Type type, out IMActorHandler actorHandler)
-        {
-            return self.ActorMessageHandlers.TryGetValue(type, out actorHandler);
+            SceneType sceneType = entity.DomainScene().SceneType;
+            foreach (ActorMessageDispatcherInfo actorMessageDispatcherInfo in list)
+            {
+                if (actorMessageDispatcherInfo.SceneType != sceneType)
+                {
+                    continue;
+                }
+                await actorMessageDispatcherInfo.IMActorHandler.Handle(entity, message, reply);   
+            }
         }
     }
 }

+ 1 - 1
Apps/Hotfix/Module/ActorLocation/ObjectAddRequestHandler.cs

@@ -2,7 +2,7 @@
 
 namespace ET
 {
-    [ActorMessageHandler]
+    [ActorMessageHandler(SceneType.Location)]
     public class ObjectAddRequestHandler: AMActorRpcHandler<Scene, ObjectAddRequest, ObjectAddResponse>
     {
         protected override async ETTask Run(Scene scene, ObjectAddRequest request, ObjectAddResponse response, Action reply)

+ 1 - 1
Apps/Hotfix/Module/ActorLocation/ObjectGetRequestHandler.cs

@@ -2,7 +2,7 @@
 
 namespace ET
 {
-    [ActorMessageHandler]
+    [ActorMessageHandler(SceneType.Location)]
     public class ObjectGetRequestHandler: AMActorRpcHandler<Scene, ObjectGetRequest, ObjectGetResponse>
     {
         protected override async ETTask Run(Scene scene, ObjectGetRequest request, ObjectGetResponse response, Action reply)

+ 1 - 1
Apps/Hotfix/Module/ActorLocation/ObjectLockRequestHandler.cs

@@ -2,7 +2,7 @@
 
 namespace ET
 {
-    [ActorMessageHandler]
+    [ActorMessageHandler(SceneType.Location)]
     public class ObjectLockRequestHandler: AMActorRpcHandler<Scene, ObjectLockRequest, ObjectLockResponse>
     {
         protected override async ETTask Run(Scene scene, ObjectLockRequest request, ObjectLockResponse response, Action reply)

+ 1 - 1
Apps/Hotfix/Module/ActorLocation/ObjectRemoveRequestHandler.cs

@@ -2,7 +2,7 @@
 
 namespace ET
 {
-    [ActorMessageHandler]
+    [ActorMessageHandler(SceneType.Location)]
     public class ObjectRemoveRequestHandler: AMActorRpcHandler<Scene, ObjectRemoveRequest, ObjectRemoveResponse>
     {
         protected override async ETTask Run(Scene scene, ObjectRemoveRequest request, ObjectRemoveResponse response, Action reply)

+ 1 - 1
Apps/Hotfix/Module/ActorLocation/ObjectUnLockRequestHandler.cs

@@ -2,7 +2,7 @@
 
 namespace ET
 {
-    [ActorMessageHandler]
+    [ActorMessageHandler(SceneType.Location)]
     public class ObjectUnLockRequestHandler: AMActorRpcHandler<Scene, ObjectUnLockRequest, ObjectUnLockResponse>
     {
         protected override async ETTask Run(Scene scene, ObjectUnLockRequest request, ObjectUnLockResponse response, Action reply)

+ 1 - 1
Apps/Hotfix/Server/AOI/AOIEntitySystem.cs

@@ -136,7 +136,7 @@ namespace ET.Server
                     enter.BeSeeUnits.Add(self.Id, self);
                 }
             }
-            Game.EventSystem.Publish(self.Unit, new EventType.UnitEnterSightRange() { B = enter });
+            Game.EventSystem.Publish(self, new EventType.UnitEnterSightRange() { B = enter });
         }
 
         // leave离开self视野

+ 0 - 44
Apps/Hotfix/Server/Actor_TransferHandler.cs

@@ -1,44 +0,0 @@
-using System;
-using System.Net;
-
-
-namespace ET.Server
-{
-	[ActorMessageHandler]
-	public class Actor_TransferHandler : AMActorLocationRpcHandler<Unit, Actor_TransferRequest, Actor_TransferResponse>
-	{
-		protected override async ETTask Run(Unit unit, Actor_TransferRequest request, Actor_TransferResponse response, Action reply)
-		{
-			//long unitId = unit.Id;
-//
-			//// 先在location锁住unit的地址
-			//await Game.Scene.GetComponent<LocationProxyComponent>().Lock(unitId, unit.InstanceId);
-//
-			//// 删除unit,让其它进程发送过来的消息找不到actor,重发
-			//Game.EventSystem.Remove(unitId);
-			//
-			//long instanceId = unit.InstanceId;
-			//
-			//int mapIndex = request.MapIndex;
-//
-			//StartConfigComponent startConfigComponent = StartConfigComponent.Instance;
-//
-			//// 传送到map
-			//StartConfig mapConfig = startConfigComponent.MapConfigs[mapIndex];
-			//IPEndPoint address = mapConfig.GetComponent<InnerConfig>().IPEndPoint;
-			//Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(address);
-//
-			//// 只删除不disponse否则M2M_TrasferUnitRequest无法序列化Unit
-			//Game.Scene.GetComponent<UnitComponent>().RemoveNoDispose(unitId);
-			//M2M_TrasferUnitResponse m2m_TrasferUnitResponse = (M2M_TrasferUnitResponse)await session.Call(new M2M_TrasferUnitRequest() { Unit = unit });
-			//unit.Dispose();
-//
-			//// 解锁unit的地址,并且更新unit的instanceId
-			//await Game.Scene.GetComponent<LocationProxyComponent>().UnLock(unitId, instanceId, m2m_TrasferUnitResponse.InstanceId);
-
-			reply();
-			
-			await ETTask.CompletedTask;
-		}
-	}
-}

+ 1 - 1
Apps/Hotfix/Server/C2G_EnterMapHandler.cs

@@ -3,7 +3,7 @@
 
 namespace ET.Server
 {
-	[MessageHandler]
+	[MessageHandler(SceneType.Gate)]
 	public class C2G_EnterMapHandler : AMRpcHandler<C2G_EnterMap, G2C_EnterMap>
 	{
 		protected override async ETTask Run(Session session, C2G_EnterMap request, G2C_EnterMap response, Action reply)

+ 1 - 1
Apps/Hotfix/Server/C2G_LoginGateHandler.cs

@@ -3,7 +3,7 @@
 
 namespace ET.Server
 {
-	[MessageHandler]
+	[MessageHandler(SceneType.Gate)]
 	public class C2G_LoginGateHandler : AMRpcHandler<C2G_LoginGate, G2C_LoginGate>
 	{
 		protected override async ETTask Run(Session session, C2G_LoginGate request, G2C_LoginGate response, Action reply)

+ 1 - 1
Apps/Hotfix/Server/C2G_PingHandler.cs

@@ -3,7 +3,7 @@
 
 namespace ET
 {
-	[MessageHandler]
+	[MessageHandler(SceneType.Gate)]
 	public class C2G_PingHandler : AMRpcHandler<C2G_Ping, G2C_Ping>
 	{
 		protected override async ETTask Run(Session session, C2G_Ping request, G2C_Ping response, Action reply)

+ 1 - 1
Apps/Hotfix/Server/C2M_PathfindingResultHandler.cs

@@ -3,7 +3,7 @@ using UnityEngine;
 
 namespace ET.Server
 {
-	[ActorMessageHandler]
+	[ActorMessageHandler(SceneType.Map)]
 	public class C2M_PathfindingResultHandler : AMActorLocationHandler<Unit, C2M_PathfindingResult>
 	{
 		protected override async ETTask Run(Unit unit, C2M_PathfindingResult message)

+ 0 - 29
Apps/Hotfix/Server/C2M_ReloadHandler.cs

@@ -1,29 +0,0 @@
-using System;
-
-
-namespace ET
-{
-	[MessageHandler]
-	public class C2M_ReloadHandler: AMRpcHandler<C2M_Reload, M2C_Reload>
-	{
-		protected override async ETTask Run(Session session, C2M_Reload request, M2C_Reload response, Action reply)
-		{
-			//if (request.Account != "panda" && request.Password != "panda")
-			//{
-			//	Log.Error($"error reload account and password: {MongoHelper.ToJson(request)}");
-			//	return;
-			//}
-			//StartConfigComponent startConfigComponent = Game.Scene.GetComponent<StartConfigComponent>();
-			//NetInnerComponent netInnerComponent = Game.Scene.GetComponent<NetInnerComponent>();
-			//foreach (StartConfig startConfig in startConfigComponent.GetAll())
-			//{
-			//	InnerConfig innerConfig = startConfig.GetComponent<InnerConfig>();
-			//	Session serverSession = netInnerComponent.Get(innerConfig.IPEndPoint);
-			//	await serverSession.Call(new M2A_Reload());
-			//}
-			reply();
-			
-			await ETTask.CompletedTask;
-		}
-	}
-}

+ 1 - 1
Apps/Hotfix/Server/C2M_StopHandler.cs

@@ -3,7 +3,7 @@ using UnityEngine;
 
 namespace ET.Server
 {
-	[ActorMessageHandler]
+	[ActorMessageHandler(SceneType.Map)]
 	public class C2M_StopHandler : AMActorLocationHandler<Unit, C2M_Stop>
 	{
 		protected override async ETTask Run(Unit unit, C2M_Stop message)

+ 1 - 0
Apps/Hotfix/Server/C2M_TestRobotCaseHandler.cs

@@ -2,6 +2,7 @@
 
 namespace ET.Server
 {
+	[ActorMessageHandler(SceneType.Map)]
 	public class C2M_TestRobotCaseHandler : AMActorLocationRpcHandler<Unit, C2M_TestRobotCase, M2C_TestRobotCase>
 	{
 		protected override async ETTask Run(Unit unit, C2M_TestRobotCase request, M2C_TestRobotCase response, Action reply)

+ 1 - 1
Apps/Hotfix/Server/C2R_LoginHandler.cs

@@ -4,7 +4,7 @@ using System.Net;
 
 namespace ET.Server
 {
-	[MessageHandler]
+	[MessageHandler(SceneType.Realm)]
 	public class C2R_LoginHandler : AMRpcHandler<C2R_Login, R2C_Login>
 	{
 		protected override async ETTask Run(Session session, C2R_Login request, R2C_Login response, Action reply)

+ 1 - 1
Apps/Hotfix/Server/G2M_SessionDisconnectHandler.cs

@@ -2,7 +2,7 @@
 
 namespace ET.Server
 {
-	[ActorMessageHandler]
+	[ActorMessageHandler(SceneType.Map)]
 	public class G2M_SessionDisconnectHandler : AMActorLocationHandler<Unit, G2M_SessionDisconnect>
 	{
 		protected override async ETTask Run(Unit unit, G2M_SessionDisconnect message)

+ 1 - 1
Apps/Hotfix/Server/M2M_UnitTransferRequestHandler.cs

@@ -3,7 +3,7 @@ using UnityEngine;
 
 namespace ET.Server
 {
-	[ActorMessageHandler]
+	[ActorMessageHandler(SceneType.Map)]
 	public class M2M_UnitTransferRequestHandler : AMActorRpcHandler<Scene, M2M_UnitTransferRequest, M2M_UnitTransferResponse>
 	{
 		protected override async ETTask Run(Scene scene, M2M_UnitTransferRequest request, M2M_UnitTransferResponse response, Action reply)

+ 1 - 1
Apps/Hotfix/Server/R2G_GetLoginKeyHandler.cs

@@ -3,7 +3,7 @@
 
 namespace ET.Server
 {
-	[ActorMessageHandler]
+	[ActorMessageHandler(SceneType.Gate)]
 	public class R2G_GetLoginKeyHandler : AMActorRpcHandler<Scene, R2G_GetLoginKey, G2R_GetLoginKey>
 	{
 		protected override async ETTask Run(Scene scene, R2G_GetLoginKey request, G2R_GetLoginKey response, Action reply)

+ 1 - 1
Apps/Hotfix/Server/Scene/C2M_TransferMapHandler.cs

@@ -2,7 +2,7 @@
 
 namespace ET.Server
 {
-	[ActorMessageHandler]
+	[ActorMessageHandler(SceneType.Map)]
 	public class C2M_TransferMapHandler : AMActorLocationRpcHandler<Unit, C2M_TransferMap, M2C_TransferMap>
 	{
 		protected override async ETTask Run(Unit unit, C2M_TransferMap request, M2C_TransferMap response, Action reply)

+ 0 - 1
Apps/Model/Module/Actor/AMActorHandler.cs

@@ -2,7 +2,6 @@
 
 namespace ET
 {
-    [ActorMessageHandler]
     public abstract class AMActorHandler<E, Message>: IMActorHandler where E : Entity where Message : class, IActorMessage
     {
         protected abstract ETTask Run(E entity, Message message);

+ 0 - 1
Apps/Model/Module/Actor/AMActorRpcHandler.cs

@@ -2,7 +2,6 @@
 
 namespace ET
 {
-    [ActorMessageHandler]
     public abstract class AMActorRpcHandler<E, Request, Response>: IMActorHandler where E : Entity where Request : class, IActorRequest where Response : class, IActorResponse
     {
         protected abstract ETTask Run(E unit, Request request, Response response, Action reply);

+ 0 - 14
Apps/Model/Module/Actor/ActorMessageAttribute.cs

@@ -1,14 +0,0 @@
-using System;
-
-namespace ET
-{
-    public class ActorMessageAttribute: Attribute
-    {
-        public ushort Opcode { get; private set; }
-
-        public ActorMessageAttribute(ushort opcode)
-        {
-            this.Opcode = opcode;
-        }
-    }
-}

+ 14 - 1
Apps/Model/Module/Actor/ActorMessageDispatcherComponent.cs

@@ -3,6 +3,19 @@ using System.Collections.Generic;
 
 namespace ET
 {
+    public class ActorMessageDispatcherInfo
+    {
+        public SceneType SceneType { get; }
+        
+        public IMActorHandler IMActorHandler { get; }
+
+        public ActorMessageDispatcherInfo(SceneType sceneType, IMActorHandler imActorHandler)
+        {
+            this.SceneType = sceneType;
+            this.IMActorHandler = imActorHandler;
+        }
+    }
+    
     /// <summary>
     /// Actor消息分发组件
     /// </summary>
@@ -10,6 +23,6 @@ namespace ET
     {
         public static ActorMessageDispatcherComponent Instance;
 
-        public readonly Dictionary<Type, IMActorHandler> ActorMessageHandlers = new Dictionary<Type, IMActorHandler>();
+        public readonly Dictionary<Type, List<ActorMessageDispatcherInfo>> ActorMessageHandlers = new();
     }
 }

+ 6 - 0
Apps/Model/Module/Actor/ActorMessageHandlerAttribute.cs

@@ -2,5 +2,11 @@
 {
     public class ActorMessageHandlerAttribute: BaseAttribute
     {
+        public SceneType SceneType { get; }
+
+        public ActorMessageHandlerAttribute(SceneType sceneType)
+        {
+            this.SceneType = sceneType;
+        }
     }
 }

+ 0 - 1
Apps/Model/Module/ActorLocation/AMActorLocationHandler.cs

@@ -2,7 +2,6 @@
 
 namespace ET
 {
-    [ActorMessageHandler]
     public abstract class AMActorLocationHandler<E, Message>: IMActorHandler where E : Entity where Message : class, IActorLocationMessage
     {
         protected abstract ETTask Run(E entity, Message message);

+ 0 - 1
Apps/Model/Module/ActorLocation/AMActorLocationRpcHandler.cs

@@ -2,7 +2,6 @@
 
 namespace ET
 {
-    [ActorMessageHandler]
     public abstract class AMActorLocationRpcHandler<E, Request, Response>: IMActorHandler where E : Entity where Request : class, IActorLocationRequest where Response : class, IActorLocationResponse
     {
         protected abstract ETTask Run(E unit, Request request, Response response, Action reply);

+ 0 - 1
Apps/Model/Module/NetworkTCP/AMRpcHandler.cs

@@ -2,7 +2,6 @@ using System;
 
 namespace ET
 {
-    [MessageHandler]
     public abstract class AMRpcHandler<Request, Response>: IMHandler where Request : class, IRequest where Response : class, IResponse
     {
         protected abstract ETTask Run(Session session, Request request, Response response, Action reply);

+ 1 - 1
Unity/Codes/Hotfix/Client/Move/M2C_PathfindingResultHandler.cs

@@ -3,7 +3,7 @@ using UnityEngine;
 
 namespace ET.Client
 {
-	[MessageHandler]
+	[MessageHandler(SceneType.Zone)]
 	public class M2C_PathfindingResultHandler : AMHandler<M2C_PathfindingResult>
 	{
 		protected override async ETTask Run(Session session, M2C_PathfindingResult message)

+ 1 - 1
Unity/Codes/Hotfix/Client/Move/M2C_StopHandler.cs

@@ -2,7 +2,7 @@
 
 namespace ET.Client
 {
-	[MessageHandler]
+	[MessageHandler(SceneType.Zone)]
 	public class M2C_StopHandler : AMHandler<M2C_Stop>
 	{
 		protected override async ETTask Run(Session session, M2C_Stop message)

+ 1 - 1
Unity/Codes/Hotfix/Client/Scene/M2C_StartSceneChangeHandler.cs

@@ -1,6 +1,6 @@
 namespace ET.Client
 {
-	[MessageHandler]
+	[MessageHandler(SceneType.Zone)]
 	public class M2C_StartSceneChangeHandler : AMHandler<M2C_StartSceneChange>
 	{
 		protected override async ETTask Run(Session session, M2C_StartSceneChange message)

+ 1 - 1
Unity/Codes/Hotfix/Client/Unit/M2C_CreateMyUnitHandler.cs

@@ -1,6 +1,6 @@
 namespace ET.Client
 {
-	[MessageHandler]
+	[MessageHandler(SceneType.Zone)]
 	public class M2C_CreateMyUnitHandler : AMHandler<M2C_CreateMyUnit>
 	{
 		protected override async ETTask Run(Session session, M2C_CreateMyUnit message)

+ 1 - 1
Unity/Codes/Hotfix/Client/Unit/M2C_CreateUnitsHandler.cs

@@ -1,6 +1,6 @@
 namespace ET.Client
 {
-	[MessageHandler]
+	[MessageHandler(SceneType.Zone)]
 	public class M2C_CreateUnitsHandler : AMHandler<M2C_CreateUnits>
 	{
 		protected override async ETTask Run(Session session, M2C_CreateUnits message)

+ 1 - 1
Unity/Codes/Hotfix/Client/Unit/M2C_RemoveUnitsHandler.cs

@@ -1,6 +1,6 @@
 namespace ET.Client
 {
-	[MessageHandler]
+	[MessageHandler(SceneType.Zone)]
 	public class M2C_RemoveUnitsHandler : AMHandler<M2C_RemoveUnits>
 	{
 		protected override async ETTask Run(Session session, M2C_RemoveUnits message)

+ 29 - 14
Unity/Codes/Hotfix/Module/Message/MessageDispatcherComponentSystem.cs

@@ -37,8 +37,8 @@ namespace ET
                 self.Handlers.Clear();
             }
         }
-        
-        public static void Load(this MessageDispatcherComponent self)
+
+        private static void Load(this MessageDispatcherComponent self)
         {
             self.Handlers.Clear();
 
@@ -53,23 +53,32 @@ namespace ET
                     continue;
                 }
 
-                Type messageType = iMHandler.GetMessageType();
-                ushort opcode = OpcodeTypeComponent.Instance.GetOpcode(messageType);
-                if (opcode == 0)
+                object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
+                
+                foreach (object attr in attrs)
                 {
-                    Log.Error($"消息opcode为0: {messageType.Name}");
-                    continue;
-                }
+                    MessageHandlerAttribute messageHandlerAttribute = attr as MessageHandlerAttribute;
+                    
+                    Type messageType = iMHandler.GetMessageType();
+                    
+                    ushort opcode = OpcodeTypeComponent.Instance.GetOpcode(messageType);
+                    if (opcode == 0)
+                    {
+                        Log.Error($"消息opcode为0: {messageType.Name}");
+                        continue;
+                    }
 
-                self.RegisterHandler(opcode, iMHandler);
+                    MessageDispatcherInfo messageDispatcherInfo = new (messageHandlerAttribute.SceneType, iMHandler);
+                    self.RegisterHandler(opcode, messageDispatcherInfo);
+                }
             }
         }
 
-        public static void RegisterHandler(this MessageDispatcherComponent self, ushort opcode, IMHandler handler)
+        private static void RegisterHandler(this MessageDispatcherComponent self, ushort opcode, MessageDispatcherInfo handler)
         {
             if (!self.Handlers.ContainsKey(opcode))
             {
-                self.Handlers.Add(opcode, new List<IMHandler>());
+                self.Handlers.Add(opcode, new List<MessageDispatcherInfo>());
             }
 
             self.Handlers[opcode].Add(handler);
@@ -77,18 +86,24 @@ namespace ET
 
         public static void Handle(this MessageDispatcherComponent self, Session session, ushort opcode, object message)
         {
-            List<IMHandler> actions;
+            List<MessageDispatcherInfo> actions;
             if (!self.Handlers.TryGetValue(opcode, out actions))
             {
                 Log.Error($"消息没有处理: {opcode} {message}");
                 return;
             }
 
-            foreach (IMHandler ev in actions)
+            SceneType sceneType = session.DomainScene().SceneType;
+            foreach (MessageDispatcherInfo ev in actions)
             {
+                if (ev.SceneType != sceneType)
+                {
+                    continue;
+                }
+                
                 try
                 {
-                    ev.Handle(session, message);
+                    ev.IMHandler.Handle(session, message);
                 }
                 catch (Exception e)
                 {

+ 0 - 1
Unity/Codes/Model/Module/Message/AMHandler.cs

@@ -2,7 +2,6 @@ using System;
 
 namespace ET
 {
-    [MessageHandler]
     public abstract class AMHandler<Message>: IMHandler where Message : class
     {
         protected abstract ETTask Run(Session session, Message message);

+ 13 - 1
Unity/Codes/Model/Module/Message/MessageDispatcherComponent.cs

@@ -2,6 +2,18 @@
 
 namespace ET
 {
+    public class MessageDispatcherInfo
+    {
+        public SceneType SceneType { get; }
+        public IMHandler IMHandler { get; }
+
+        public MessageDispatcherInfo(SceneType sceneType, IMHandler imHandler)
+        {
+            this.SceneType = sceneType;
+            this.IMHandler = imHandler;
+        }
+    }
+    
     /// <summary>
     /// 消息分发组件
     /// </summary>
@@ -13,6 +25,6 @@ namespace ET
             set;
         }
 
-        public readonly Dictionary<ushort, List<IMHandler>> Handlers = new Dictionary<ushort, List<IMHandler>>();
+        public readonly Dictionary<ushort, List<MessageDispatcherInfo>> Handlers = new();
     }
 }

+ 6 - 0
Unity/Codes/Model/Module/Message/MessageHandlerAttribute.cs

@@ -2,5 +2,11 @@
 {
     public class MessageHandlerAttribute: BaseAttribute
     {
+        public SceneType SceneType { get; }
+
+        public MessageHandlerAttribute(SceneType sceneType)
+        {
+            this.SceneType = sceneType;
+        }
     }
 }