Răsfoiți Sursa

去掉了IEntityActorHandler接口,IEntityActorHandler需要每个actor都需要new一个对象,而且热更十分不方便,改成使用string来分发
每个ActorComponent在add的时候需要指定一个ActorType(string), actor在收到actor消息的时候会先分发到ActorTypeHandler处理

tanghai 7 ani în urmă
părinte
comite
d7a14b95d8

+ 20 - 26
Server/Hotfix/Module/Actor/ActorComponentSystem.cs

@@ -10,8 +10,8 @@ namespace ETHotfix
 	{
 		public override void Awake(ActorComponent self)
 		{
-			self.entityActorHandler = new CommonEntityActorHandler();
-			self.queue = new Queue<ActorMessageInfo>();
+			self.ActorType = ActorType.Common;
+			self.Queue = new Queue<ActorMessageInfo>();
 			Game.Scene.GetComponent<ActorManagerComponent>().Add(self.Entity);
 
 			self.HandleAsync();
@@ -19,27 +19,18 @@ namespace ETHotfix
 	}
 
 	[ObjectSystem]
-	public class ActorComponentAwake1System : AwakeSystem<ActorComponent, IEntityActorHandler>
+	public class ActorComponentAwake1System : AwakeSystem<ActorComponent, string>
 	{
-		public override void Awake(ActorComponent self, IEntityActorHandler iEntityActorHandler)
+		public override void Awake(ActorComponent self, string actorType)
 		{
-			self.entityActorHandler = iEntityActorHandler;
-			self.queue = new Queue<ActorMessageInfo>();
+			self.ActorType = actorType;
+			self.Queue = new Queue<ActorMessageInfo>();
 			Game.Scene.GetComponent<ActorManagerComponent>().Add(self.Entity);
 
 			self.HandleAsync();
 		}
 	}
-
-	[ObjectSystem]
-	public class ActorComponentLoadSystem : LoadSystem<ActorComponent>
-	{
-		public override void Load(ActorComponent self)
-		{
-			self.entityActorHandler = (IEntityActorHandler)HotfixHelper.Create(self.entityActorHandler);
-		}
-	}
-
+	
 	[ObjectSystem]
 	public class ActorComponentDestroySystem : DestroySystem<ActorComponent>
 	{
@@ -66,27 +57,27 @@ namespace ETHotfix
 
 		public static void Add(this ActorComponent self, ActorMessageInfo info)
 		{
-			self.queue.Enqueue(info);
+			self.Queue.Enqueue(info);
 
-			if (self.tcs == null)
+			if (self.Tcs == null)
 			{
 				return;
 			}
 
-			var t = self.tcs;
-			self.tcs = null;
-			t.SetResult(self.queue.Dequeue());
+			var t = self.Tcs;
+			self.Tcs = null;
+			t.SetResult(self.Queue.Dequeue());
 		}
 
 		private static Task<ActorMessageInfo> GetAsync(this ActorComponent self)
 		{
-			if (self.queue.Count > 0)
+			if (self.Queue.Count > 0)
 			{
-				return Task.FromResult(self.queue.Dequeue());
+				return Task.FromResult(self.Queue.Dequeue());
 			}
 
-			self.tcs = new TaskCompletionSource<ActorMessageInfo>();
-			return self.tcs.Task;
+			self.Tcs = new TaskCompletionSource<ActorMessageInfo>();
+			return self.Tcs.Task;
 		}
 
 		public static async void HandleAsync(this ActorComponent self)
@@ -105,7 +96,10 @@ namespace ETHotfix
 					{
 						return;
 					}
-					await self.entityActorHandler.Handle(info.Session, (Entity)self.Parent, info.Message);
+
+					// 根据这个actor的类型分发给相应的ActorHandler处理
+					await Game.Scene.GetComponent<ActorMessageDispatherComponent>().ActorTypeHandle(
+							self.ActorType, info.Session, (Entity)self.Parent, info.Message);
 				}
 				catch (Exception e)
 				{

+ 126 - 0
Server/Hotfix/Module/Actor/ActorMessageDispatherComponentSystem.cs

@@ -0,0 +1,126 @@
+using System;
+using System.Threading.Tasks;
+using ETModel;
+
+namespace ETHotfix
+{
+	[ObjectSystem]
+	public class ActorMessageDispatherComponentStartSystem: AwakeSystem<ActorMessageDispatherComponent>
+	{
+		public override void Awake(ActorMessageDispatherComponent self)
+		{
+			self.Awake();
+		}
+	}
+
+	[ObjectSystem]
+	public class ActorMessageDispatherComponentLoadSystem: LoadSystem<ActorMessageDispatherComponent>
+	{
+		public override void Load(ActorMessageDispatherComponent self)
+		{
+			self.Load();
+		}
+	}
+
+	/// <summary>
+	/// Actor消息分发组件
+	/// </summary>
+	public static class ActorMessageDispatherComponentEx
+	{
+		public static void Awake(this ActorMessageDispatherComponent self)
+		{
+			self.Load();
+		}
+
+		public static void Load(this ActorMessageDispatherComponent self)
+		{
+			AppType appType = self.Entity.GetComponent<StartConfigComponent>().StartConfig.AppType;
+
+			Log.Info("apptype: " + appType);
+
+			self.ActorMessageHandlers.Clear();
+			self.ActorTypeHandlers.Clear();
+
+			Type[] types = DllHelper.GetMonoTypes();
+
+			foreach (Type type in types)
+			{
+				object[] attrs = type.GetCustomAttributes(typeof(ActorTypeHandlerAttribute), false);
+				if (attrs.Length == 0)
+				{
+					continue;
+				}
+
+				ActorTypeHandlerAttribute actorTypeHandlerAttribute = (ActorTypeHandlerAttribute) attrs[0];
+				if (!actorTypeHandlerAttribute.Type.Is(appType))
+				{
+					continue;
+				}
+
+				object obj = Activator.CreateInstance(type);
+
+				IActorTypeHandler iActorTypeHandler = obj as IActorTypeHandler;
+				if (iActorTypeHandler == null)
+				{
+					throw new Exception($"actor handler not inherit IEntityActorHandler: {obj.GetType().FullName}");
+				}
+
+				self.ActorTypeHandlers.Add(actorTypeHandlerAttribute.ActorType, iActorTypeHandler);
+			}
+
+			foreach (Type type in types)
+			{
+				object[] attrs = type.GetCustomAttributes(typeof(ActorMessageHandlerAttribute), false);
+				if (attrs.Length == 0)
+				{
+					continue;
+				}
+
+				ActorMessageHandlerAttribute messageHandlerAttribute = (ActorMessageHandlerAttribute) attrs[0];
+				if (!messageHandlerAttribute.Type.Is(appType))
+				{
+					continue;
+				}
+
+				object obj = Activator.CreateInstance(type);
+
+				IMActorHandler imHandler = obj as IMActorHandler;
+				if (imHandler == null)
+				{
+					throw new Exception($"message handler not inherit IMActorHandler abstract class: {obj.GetType().FullName}");
+				}
+
+				Type messageType = imHandler.GetMessageType();
+				self.ActorMessageHandlers.Add(messageType, imHandler);
+			}
+		}
+
+		/// <summary>
+		/// 根据actor的类型分发给不同的ActorHandler处理
+		/// </summary>
+		public static async Task ActorTypeHandle(
+				this ActorMessageDispatherComponent self, string actorType, Session session, Entity entity, IActorMessage actorMessage)
+		{
+			IActorTypeHandler iActorTypeHandler;
+			if (!self.ActorTypeHandlers.TryGetValue(actorType, out iActorTypeHandler))
+			{
+				throw new Exception($"not found actortype handler: {actorType}");
+			}
+
+			await iActorTypeHandler.Handle(session, entity, actorMessage);
+		}
+
+		/// <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))
+			{
+				throw new Exception($"not found message handler: {MongoHelper.ToJson(actorRequest)}");
+			}
+
+			await handler.Handle(session, entity, actorRequest);
+		}
+	}
+}

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

@@ -0,0 +1,14 @@
+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);
+		}
+	}
+}

+ 0 - 66
Server/Hotfix/Module/Actor/EntityActorHandler.cs

@@ -1,66 +0,0 @@
-using System;
-using System.Threading.Tasks;
-using ETModel;
-
-namespace ETHotfix
-{
-    /// <summary>
-    /// gate session收到的消息直接转发给客户端
-    /// </summary>
-    public class GateSessionEntityActorHandler : IEntityActorHandler
-    {
-        public async Task Handle(Session session, Entity entity, IActorMessage actorMessage)
-        {
-			ActorResponse actorResponse = new ActorResponse
-			{
-				RpcId = actorMessage.RpcId
-			};
-			try
-	        {
-		        // 发送给客户端
-		        Session clientSession = entity as Session;
-				clientSession.Send(actorMessage);
-
-				session.Reply(actorResponse);
-		        await Task.CompletedTask;
-	        }
-	        catch (Exception e)
-	        {
-		        actorResponse.Error = ErrorCode.ERR_SessionActorError;
-		        actorResponse.Message = $"session actor error {e}";
-				session.Reply(actorResponse);
-				throw;
-	        }
-        }
-    }
-
-    public class CommonEntityActorHandler : IEntityActorHandler
-    {
-        public async Task Handle(Session session, Entity entity, IActorMessage actorMessage)
-        {
-			await Game.Scene.GetComponent<ActorMessageDispatherComponent>().Handle(session, entity, actorMessage);
-        }
-    }
-
-    /// <summary>
-    /// 玩家收到帧同步消息交给帧同步组件处理
-    /// </summary>
-    public class MapUnitEntityActorHandler : IEntityActorHandler
-    {
-        public async Task Handle(Session session, Entity entity, IActorMessage actorMessage)
-        {
-			if (actorMessage is OneFrameMessage aFrameMessage)
-            {
-				Game.Scene.GetComponent<ServerFrameComponent>().Add(aFrameMessage);
-
-				ActorResponse actorResponse = new ActorResponse
-				{
-					RpcId = actorMessage.RpcId
-				};
-				session.Reply(actorResponse);
-				return;
-            }
-            await Game.Scene.GetComponent<ActorMessageDispatherComponent>().Handle(session, entity, actorMessage);
-        }
-    }
-}

+ 38 - 0
Server/Hotfix/Module/Actor/GateSessionActorTypeHandler.cs

@@ -0,0 +1,38 @@
+using System;
+using System.Threading.Tasks;
+using ETModel;
+
+namespace ETHotfix
+{
+	/// <summary>
+	/// gate session收到的actor消息直接转发给客户端
+	/// </summary>
+	[ActorTypeHandler(AppType.Gate, ActorType.GateSession)]
+	public class GateSessionActorTypeHandler : IActorTypeHandler
+	{
+		public async Task Handle(Session session, Entity entity, IActorMessage actorMessage)
+		{
+			ActorResponse actorResponse = new ActorResponse
+			{
+				RpcId = actorMessage.RpcId
+			};
+			try
+			{
+				// 发送给客户端
+				Session clientSession = entity as Session;
+				actorMessage.ActorId = 0;
+				clientSession.Send(actorMessage);
+
+				session.Reply(actorResponse);
+				await Task.CompletedTask;
+			}
+			catch (Exception e)
+			{
+				actorResponse.Error = ErrorCode.ERR_SessionActorError;
+				actorResponse.Message = $"session actor error {e}";
+				session.Reply(actorResponse);
+				throw;
+			}
+		}
+	}
+}

+ 28 - 0
Server/Hotfix/Module/Actor/MapUnitActorTypeHandler.cs

@@ -0,0 +1,28 @@
+using System.Threading.Tasks;
+using ETModel;
+
+namespace ETHotfix
+{
+	/// <summary>
+	/// 玩家收到帧同步消息交给帧同步组件处理
+	/// </summary>
+	[ActorTypeHandler(AppType.Map, ActorType.Unit)]
+	public class MapUnitActorTypeHandler : IActorTypeHandler
+    {
+        public async Task Handle(Session session, Entity entity, IActorMessage actorMessage)
+        {
+			if (actorMessage is OneFrameMessage aFrameMessage)
+            {
+				Game.Scene.GetComponent<ServerFrameComponent>().Add(aFrameMessage);
+
+				ActorResponse actorResponse = new ActorResponse
+				{
+					RpcId = actorMessage.RpcId
+				};
+				session.Reply(actorResponse);
+				return;
+            }
+            await Game.Scene.GetComponent<ActorMessageDispatherComponent>().Handle(session, entity, actorMessage);
+        }
+    }
+}

+ 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;
-				await session.AddComponent<ActorComponent, IEntityActorHandler>(new GateSessionEntityActorHandler()).AddLocation();
+				await session.AddComponent<ActorComponent, string>(ActorType.GateSession).AddLocation();
 
 				response.PlayerId = player.Id;
 				reply(response);

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

@@ -13,7 +13,7 @@ namespace ETHotfix
 			{
 				Unit unit = ComponentFactory.Create<Unit>();
 
-				await unit.AddComponent<ActorComponent, IEntityActorHandler>(new MapUnitEntityActorHandler()).AddLocation();
+				await unit.AddComponent<ActorComponent, string>(ActorType.Unit).AddLocation();
 				unit.AddComponent<UnitGateComponent, long>(message.GateSessionId);
 				Game.Scene.GetComponent<UnitComponent>().Add(unit);
 				response.UnitId = unit.Id;

+ 1 - 1
Server/Hotfix/Server.Hotfix.csproj

@@ -3,7 +3,7 @@
   <PropertyGroup>
     <TargetFramework>netcoreapp2.0</TargetFramework>
     <AssemblyName>Hotfix</AssemblyName>
-    <RootNamespace>Hotfix</RootNamespace>
+    <RootNamespace>ETHotfix</RootNamespace>
   </PropertyGroup>
 	
   <PropertyGroup>

+ 6 - 7
Server/Model/Module/Actor/ActorComponent.cs

@@ -1,5 +1,4 @@
-using System;
-using System.Collections.Generic;
+using System.Collections.Generic;
 using System.Threading.Tasks;
 
 namespace ETModel
@@ -15,12 +14,12 @@ namespace ETModel
 	/// </summary>
 	public class ActorComponent: Component
 	{
-		public IEntityActorHandler entityActorHandler;
+		public string ActorType;
 
 		// 队列处理消息
-		public Queue<ActorMessageInfo> queue;
+		public Queue<ActorMessageInfo> Queue;
 
-		public TaskCompletionSource<ActorMessageInfo> tcs;
+		public TaskCompletionSource<ActorMessageInfo> Tcs;
 
 		public override void Dispose()
 		{
@@ -31,8 +30,8 @@ namespace ETModel
 
 			base.Dispose();
 
-			var t = this.tcs;
-			this.tcs = null;
+			var t = this.Tcs;
+			this.Tcs = null;
 			t?.SetResult(new ActorMessageInfo());
 		}
 	}

+ 5 - 76
Server/Model/Module/Actor/ActorMessageDispatherComponent.cs

@@ -1,89 +1,16 @@
 using System;
 using System.Collections.Generic;
-using System.Threading.Tasks;
 
 namespace ETModel
 {
-	[ObjectSystem]
-	public class ActorMessageDispatherComponentStartSystem : AwakeSystem<ActorMessageDispatherComponent>
-	{
-		public override void Awake(ActorMessageDispatherComponent self)
-		{
-			self.Awake();
-		}
-	}
-
-	[ObjectSystem]
-	public class ActorMessageDispatherComponentLoadSystem : LoadSystem<ActorMessageDispatherComponent>
-	{
-		public override void Load(ActorMessageDispatherComponent self)
-		{
-			self.Load();
-		}
-	}
-
 	/// <summary>
 	/// Actor消息分发组件
 	/// </summary>
 	public class ActorMessageDispatherComponent : Component
 	{
-		private Dictionary<Type, IMActorHandler> handlers;
-		
-		public void Awake()
-		{
-			this.Load();
-		}
-
-		public void Load()
-		{
-			AppType appType = this.Entity.GetComponent<StartConfigComponent>().StartConfig.AppType;
-			Log.Info("apptype: " + appType);
-			this.handlers = new Dictionary<Type, IMActorHandler>();
-
-			Type[] types = DllHelper.GetMonoTypes();
-
-			foreach (Type type in types)
-			{
-				object[] attrs = type.GetCustomAttributes(typeof(ActorMessageHandlerAttribute), false);
-				if (attrs.Length == 0)
-				{
-					continue;
-				}
-
-				ActorMessageHandlerAttribute messageHandlerAttribute = (ActorMessageHandlerAttribute)attrs[0];
-				if (!messageHandlerAttribute.Type.Is(appType))
-				{
-					continue;
-				}
-
-				object obj = Activator.CreateInstance(type);
-
-				IMActorHandler imHandler = obj as IMActorHandler;
-				if (imHandler == null)
-				{
-					throw new Exception($"message handler not inherit AMEvent or AMRpcEvent abstract class: {obj.GetType().FullName}");
-				}
-
-				Type messageType = imHandler.GetMessageType();
-				handlers.Add(messageType, imHandler);
-			}
-		}
-
-		public IMActorHandler GetActorHandler(Type type)
-		{
-			this.handlers.TryGetValue(type, out IMActorHandler actorHandler);
-			return actorHandler;
-		}
+		public readonly Dictionary<string, IActorTypeHandler> ActorTypeHandlers = new Dictionary<string, IActorTypeHandler>();
 
-		public async Task Handle(Session session, Entity entity, IActorMessage actorRequest)
-		{
-			if (!this.handlers.TryGetValue(actorRequest.GetType(), out IMActorHandler handler))
-			{
-				throw new Exception($"not found message handler: {MongoHelper.ToJson(actorRequest)}");
-			}
-			
-			await handler.Handle(session, entity, actorRequest);
-		}
+		public readonly Dictionary<Type, IMActorHandler> ActorMessageHandlers = new Dictionary<Type, IMActorHandler>();
 
 		public override void Dispose()
 		{
@@ -91,8 +18,10 @@ namespace ETModel
 			{
 				return;
 			}
-
 			base.Dispose();
+
+			this.ActorMessageHandlers.Clear();
+			this.ActorTypeHandlers.Clear();
 		}
 	}
 }

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

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

+ 17 - 0
Server/Model/Module/Actor/ActorTypeHandlerAttribute.cs

@@ -0,0 +1,17 @@
+using System;
+
+namespace ETModel
+{
+	public class ActorTypeHandlerAttribute : Attribute
+	{
+		public AppType Type { get; }
+
+		public string ActorType { get; }
+
+		public ActorTypeHandlerAttribute(AppType appType, string actorType)
+		{
+			this.Type = appType;
+			this.ActorType = actorType;
+		}
+	}
+}

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

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