Jelajahi Sumber

把ActorType改成ActorInterceptType(拦截器类型),也就是说默认没有拦截器,actor消息是由IMActorHandler处理,如果MailBoxComponent有设置拦截器,则交给拦截器处理,比如Gate Session的MailBoxComponent就设置了拦截器

tanghai 7 tahun lalu
induk
melakukan
55de3d8892

+ 20 - 28
Server/Hotfix/Module/Actor/ActorMessageDispatherComponentSystem.cs

@@ -26,7 +26,7 @@ namespace ETHotfix
 	/// <summary>
 	/// Actor消息分发组件
 	/// </summary>
-	public static class ActorMessageDispatherComponentEx
+	public static class ActorMessageDispatherComponentHelper
 	{
 		public static void Awake(this ActorMessageDispatherComponent self)
 		{
@@ -40,31 +40,31 @@ namespace ETHotfix
 			self.ActorMessageHandlers.Clear();
 			self.ActorTypeHandlers.Clear();
 
-			List<Type> types = Game.EventSystem.GetTypes(typeof(ActorTypeHandlerAttribute));
+			List<Type> types = Game.EventSystem.GetTypes(typeof(ActorInterceptTypeHandlerAttribute));
 
 			foreach (Type type in types)
 			{
-				object[] attrs = type.GetCustomAttributes(typeof(ActorTypeHandlerAttribute), false);
+				object[] attrs = type.GetCustomAttributes(typeof(ActorInterceptTypeHandlerAttribute), false);
 				if (attrs.Length == 0)
 				{
 					continue;
 				}
 
-				ActorTypeHandlerAttribute actorTypeHandlerAttribute = (ActorTypeHandlerAttribute) attrs[0];
-				if (!actorTypeHandlerAttribute.Type.Is(appType))
+				ActorInterceptTypeHandlerAttribute actorInterceptTypeHandlerAttribute = (ActorInterceptTypeHandlerAttribute) attrs[0];
+				if (!actorInterceptTypeHandlerAttribute.Type.Is(appType))
 				{
 					continue;
 				}
 
 				object obj = Activator.CreateInstance(type);
 
-				IActorTypeHandler iActorTypeHandler = obj as IActorTypeHandler;
-				if (iActorTypeHandler == null)
+				IActorInterceptTypeHandler iActorInterceptTypeHandler = obj as IActorInterceptTypeHandler;
+				if (iActorInterceptTypeHandler == null)
 				{
 					throw new Exception($"actor handler not inherit IEntityActorHandler: {obj.GetType().FullName}");
 				}
 
-				self.ActorTypeHandlers.Add(actorTypeHandlerAttribute.ActorType, iActorTypeHandler);
+				self.ActorTypeHandlers.Add(actorInterceptTypeHandlerAttribute.ActorType, iActorInterceptTypeHandler);
 			}
 
 			types = Game.EventSystem.GetTypes(typeof (ActorMessageHandlerAttribute));
@@ -95,32 +95,24 @@ namespace ETHotfix
 			}
 		}
 
-		/// <summary>
-		/// 一个actor收到的所有消息先由其指定的ActorTypeHandle处理
-		/// </summary>
-		public static async Task ActorTypeHandle(
-				this ActorMessageDispatherComponent self, string actorType, Entity entity, ActorMessageInfo actorMessageInfo)
+		public static async Task Handle(
+				this ActorMessageDispatherComponent self, MailBoxComponent mailBoxComponent, ActorMessageInfo actorMessageInfo)
 		{
-			IActorTypeHandler iActorTypeHandler;
-			if (!self.ActorTypeHandlers.TryGetValue(actorType, out iActorTypeHandler))
+			// 有拦截器使用拦截器处理
+			IActorInterceptTypeHandler iActorInterceptTypeHandler;
+			if (self.ActorTypeHandlers.TryGetValue(mailBoxComponent.ActorInterceptType, out iActorInterceptTypeHandler))
 			{
-				throw new Exception($"not found actortype handler: {actorType}");
+				await iActorInterceptTypeHandler.Handle(actorMessageInfo.Session, mailBoxComponent.Entity, actorMessageInfo.Message);
+				return;
 			}
-
-			await iActorTypeHandler.Handle(actorMessageInfo.Session, entity, actorMessageInfo.Message);
-		}
-
-		/// <summary>
-		/// 根据actor消息分发给ActorMessageHandler处理
-		/// </summary>
-		public static async Task Handle(this ActorMessageDispatherComponent self, Session session, Entity entity, IActorMessage actorRequest)
-		{
-			if (!self.ActorMessageHandlers.TryGetValue(actorRequest.GetType(), out IMActorHandler handler))
+			
+			// 没有拦截器就用IMActorHandler处理
+			if (!self.ActorMessageHandlers.TryGetValue(actorMessageInfo.Message.GetType(), out IMActorHandler handler))
 			{
-				throw new Exception($"not found message handler: {MongoHelper.ToJson(actorRequest)}");
+				throw new Exception($"not found message handler: {MongoHelper.ToJson(actorMessageInfo.Message)}");
 			}
 
-			await handler.Handle(session, entity, actorRequest);
+			await handler.Handle(actorMessageInfo.Session, mailBoxComponent.Entity, actorMessageInfo.Message);
 		}
 	}
 }

+ 1 - 1
Server/Hotfix/Module/Actor/ActorMessageSenderSystem.cs

@@ -73,7 +73,7 @@ namespace ETHotfix
 		}
 	}
 
-	public static class ActorMessageSenderEx
+	public static class ActorMessageSenderHelper
 	{
 		private static void Add(this ActorMessageSender self, ActorTask task)
 		{

+ 0 - 14
Server/Hotfix/Module/Actor/CommonActorTypeHandler.cs

@@ -1,14 +0,0 @@
-using System.Threading.Tasks;
-using ETModel;
-
-namespace ETHotfix
-{
-	[ActorTypeHandler(AppType.AllServer, ActorType.Common)]
-	public class CommonActorTypeHandler : IActorTypeHandler
-	{
-		public async Task Handle(Session session, Entity entity, IActorMessage actorMessage)
-		{
-			await Game.Scene.GetComponent<ActorMessageDispatherComponent>().Handle(session, entity, actorMessage);
-		}
-	}
-}

+ 3 - 3
Server/Hotfix/Module/Actor/GateSessionActorTypeHandler.cs → Server/Hotfix/Module/Actor/GateSessionActorInterceptInterceptTypeHandler.cs

@@ -5,10 +5,10 @@ using ETModel;
 namespace ETHotfix
 {
 	/// <summary>
-	/// gate session收到的actor消息直接转发给客户端
+	/// gate session 拦截器,收到的actor消息直接转发给客户端
 	/// </summary>
-	[ActorTypeHandler(AppType.Gate, ActorType.GateSession)]
-	public class GateSessionActorTypeHandler : IActorTypeHandler
+	[ActorInterceptTypeHandler(AppType.Gate, ActorInterceptType.GateSession)]
+	public class GateSessionActorInterceptInterceptTypeHandler : IActorInterceptTypeHandler
 	{
 		public async Task Handle(Session session, Entity entity, IActorMessage actorMessage)
 		{

+ 5 - 13
Server/Hotfix/Module/Actor/MailBoxComponentSystem.cs

@@ -10,7 +10,7 @@ namespace ETHotfix
 	{
 		public override void Awake(MailBoxComponent self)
 		{
-			self.ActorType = ActorType.Common;
+			self.ActorInterceptType = ActorInterceptType.None;
 			self.Queue.Clear();
 		}
 	}
@@ -18,9 +18,9 @@ namespace ETHotfix
 	[ObjectSystem]
 	public class MailBoxComponentAwake1System : AwakeSystem<MailBoxComponent, string>
 	{
-		public override void Awake(MailBoxComponent self, string actorType)
+		public override void Awake(MailBoxComponent self, string actorInterceptType)
 		{
-			self.ActorType = actorType;
+			self.ActorInterceptType = actorInterceptType;
 			self.Queue.Clear();
 		}
 	}
@@ -34,18 +34,10 @@ namespace ETHotfix
 		}
 	}
 
-	[ObjectSystem]
-	public class MailBoxComponentDestroySystem : DestroySystem<MailBoxComponent>
-	{
-		public override void Destroy(MailBoxComponent self)
-		{
-		}
-	}
-
 	/// <summary>
 	/// 挂上这个组件表示该Entity是一个Actor, 接收的消息将会队列处理
 	/// </summary>
-	public static class MailBoxComponentEx
+	public static class MailBoxComponentHelper
 	{
 		public static async Task AddLocation(this MailBoxComponent self)
 		{
@@ -104,7 +96,7 @@ namespace ETHotfix
 					}
 
 					// 根据这个actor的类型分发给相应的ActorHandler处理
-					await actorMessageDispatherComponent.ActorTypeHandle(self.ActorType, (Entity)self.Parent, info);
+					await actorMessageDispatherComponent.Handle(self, info);
 				}
 				catch (Exception e)
 				{

+ 1 - 1
Server/Hotfix/Module/FrameSync/C2G_LoginGateHandler.cs

@@ -22,7 +22,7 @@ namespace ETHotfix
 				Player player = ComponentFactory.Create<Player, string>(account);
 				Game.Scene.GetComponent<PlayerComponent>().Add(player);
 				session.AddComponent<SessionPlayerComponent>().Player = player;
-				session.AddComponent<MailBoxComponent, string>(ActorType.GateSession);
+				session.AddComponent<MailBoxComponent, string>(ActorInterceptType.GateSession);
 
 				response.PlayerId = player.Id;
 				reply(response);

+ 8 - 0
Server/Model/Module/Actor/ActorInterceptType.cs

@@ -0,0 +1,8 @@
+namespace ETModel
+{
+    public static partial class ActorInterceptType
+    {
+	    public const string None = "None";
+		public const string GateSession = "GateSession";
+    }
+}

+ 2 - 2
Server/Model/Module/Actor/ActorTypeHandlerAttribute.cs → Server/Model/Module/Actor/ActorInterceptTypeHandlerAttribute.cs

@@ -2,13 +2,13 @@
 
 namespace ETModel
 {
-	public class ActorTypeHandlerAttribute : BaseAttribute
+	public class ActorInterceptTypeHandlerAttribute : BaseAttribute
 	{
 		public AppType Type { get; }
 
 		public string ActorType { get; }
 
-		public ActorTypeHandlerAttribute(AppType appType, string actorType)
+		public ActorInterceptTypeHandlerAttribute(AppType appType, string actorType)
 		{
 			this.Type = appType;
 			this.ActorType = actorType;

+ 1 - 1
Server/Model/Module/Actor/ActorMessageDispatherComponent.cs

@@ -8,7 +8,7 @@ namespace ETModel
 	/// </summary>
 	public class ActorMessageDispatherComponent : Component
 	{
-		public readonly Dictionary<string, IActorTypeHandler> ActorTypeHandlers = new Dictionary<string, IActorTypeHandler>();
+		public readonly Dictionary<string, IActorInterceptTypeHandler> ActorTypeHandlers = new Dictionary<string, IActorInterceptTypeHandler>();
 
 		public readonly Dictionary<Type, IMActorHandler> ActorMessageHandlers = new Dictionary<Type, IMActorHandler>();
 

+ 0 - 8
Server/Model/Module/Actor/ActorType.cs

@@ -1,8 +0,0 @@
-namespace ETModel
-{
-    public static partial class ActorType
-    {
-	    public const string Common = "Common";
-		public const string GateSession = "GateSession";
-    }
-}

+ 1 - 1
Server/Model/Module/Actor/IActorTypeHandler.cs → Server/Model/Module/Actor/IActorInterceptTypeHandler.cs

@@ -2,7 +2,7 @@ using System.Threading.Tasks;
 
 namespace ETModel
 {
-	public interface IActorTypeHandler
+	public interface IActorInterceptTypeHandler
 	{
 		Task Handle(Session session, Entity entity, IActorMessage actorMessage);
 	}

+ 2 - 1
Server/Model/Module/Actor/MailBoxComponent.cs

@@ -14,7 +14,8 @@ namespace ETModel
 	/// </summary>
 	public class MailBoxComponent: Component
 	{
-		public string ActorType;
+		// 拦截器类型,默认没有拦截器
+		public string ActorInterceptType;
 
 		// 队列处理消息
 		public Queue<ActorMessageInfo> Queue = new Queue<ActorMessageInfo>();