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

新版Callback,更合理,编译器参数类型能检查

tanghai 3 лет назад
Родитель
Сommit
642b8450bb
37 измененных файлов с 236 добавлено и 220 удалено
  1. 10 7
      DotNet/App/ConfigLoader.cs
  2. 1 1
      Unity/Assets/Scripts/Codes/Hotfix/Client/Demo/Login/LoginHelper.cs
  3. 8 8
      Unity/Assets/Scripts/Codes/Hotfix/Client/Demo/Session/SessionStreamDispatcherClientOuter.cs
  4. 2 2
      Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Helper/SceneFactory.cs
  5. 7 3
      Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Helper/SessionStreamDispatcherServerInner.cs
  6. 5 3
      Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Helper/SessionStreamDispatcherServerOuter.cs
  7. 5 5
      Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/InitServer.cs
  8. 2 2
      Unity/Assets/Scripts/Codes/Hotfix/Server/Module/Actor/ActorMessageSenderComponentSystem.cs
  9. 2 2
      Unity/Assets/Scripts/Codes/Hotfix/Server/Module/ActorLocation/ActorLocationSenderComponentSystem.cs
  10. 1 1
      Unity/Assets/Scripts/Codes/Hotfix/Server/Module/MessageInner/NetInnerComponentSystem.cs
  11. 3 3
      Unity/Assets/Scripts/Codes/Hotfix/Share/Demo/InitShare.cs
  12. 2 2
      Unity/Assets/Scripts/Codes/Hotfix/Share/Module/AI/AIComponentSystem.cs
  13. 7 5
      Unity/Assets/Scripts/Codes/Hotfix/Share/Module/Config/ConfigComponentSystem.cs
  14. 1 1
      Unity/Assets/Scripts/Codes/Hotfix/Share/Module/Message/NetKcpComponentSystem.cs
  15. 2 2
      Unity/Assets/Scripts/Codes/Hotfix/Share/Module/Message/SessionAcceptTimeoutComponentSystem.cs
  16. 2 2
      Unity/Assets/Scripts/Codes/Hotfix/Share/Module/Message/SessionIdleCheckerComponentSystem.cs
  17. 2 2
      Unity/Assets/Scripts/Codes/Hotfix/Share/Module/Move/MoveComponentSystem.cs
  18. 1 1
      Unity/Assets/Scripts/Codes/Hotfix/Share/Module/Recast/NavmeshComponentSystem.cs
  19. 3 3
      Unity/Assets/Scripts/Codes/HotfixView/Client/Demo/InitClient.cs
  20. 4 4
      Unity/Assets/Scripts/Codes/Model/Server/Demo/Map/Recast/RecastFileReader.cs
  21. 2 2
      Unity/Assets/Scripts/Codes/Model/Server/Entry.cs
  22. 15 0
      Unity/Assets/Scripts/Codes/Model/Share/InitCallback.cs
  23. 1 1
      Unity/Assets/Scripts/Codes/Model/Share/InitCallback.cs.meta
  24. 4 3
      Unity/Assets/Scripts/Codes/Model/Share/Module/Config/ConfigComponent.cs
  25. 1 1
      Unity/Assets/Scripts/Codes/Model/Share/Module/Message/NetKcpComponent.cs
  26. 20 0
      Unity/Assets/Scripts/Codes/Model/Share/Module/Message/SessionStreamCallback.cs
  27. 11 0
      Unity/Assets/Scripts/Codes/Model/Share/Module/Message/SessionStreamCallback.cs.meta
  28. 7 0
      Unity/Assets/Scripts/Codes/Model/Share/Module/Recast/NavmeshComponent.cs
  29. 3 3
      Unity/Assets/Scripts/Codes/Model/Share/Module/Timer/ATimer.cs
  30. 8 2
      Unity/Assets/Scripts/Codes/Model/Share/Module/Timer/TimerComponent.cs
  31. 1 16
      Unity/Assets/Scripts/Codes/Model/Share/TimerCallbackId.cs
  32. 11 0
      Unity/Assets/Scripts/Codes/Model/Share/TimerCallbackId.cs.meta
  33. 10 7
      Unity/Assets/Scripts/Codes/ModelView/Client/Demo/Config/ConfigLoader.cs
  34. 5 5
      Unity/Assets/Scripts/Codes/ModelView/Client/Entry.cs
  35. 3 3
      Unity/Assets/Scripts/Core/Object/CallbackAttribute.cs
  36. 39 78
      Unity/Assets/Scripts/Core/Object/EventSystem.cs
  37. 25 40
      Unity/Assets/Scripts/Core/Object/ICallback.cs

+ 10 - 7
DotNet/App/ConfigLoader.cs

@@ -4,11 +4,12 @@ using System.IO;
 
 namespace ET.Server
 {
-    [Callback(CallbackType.GetAllConfigBytes)]
-    public class GetAllConfigBytes: IAction<ConfigComponent, Dictionary<string, byte[]>>
+    [Callback]
+    public class GetAllConfigBytes: ACallbackHandler<ConfigComponent.GetAllConfigBytes, Dictionary<string, byte[]>>
     {
-        public void Handle(ConfigComponent configComponent, Dictionary<string, byte[]> output)
+        public override Dictionary<string, byte[]> Handle(ConfigComponent.GetAllConfigBytes args)
         {
+            Dictionary<string, byte[]> output = new Dictionary<string, byte[]>();
             List<string> startConfigs = new List<string>()
             {
                 "StartMachineConfigCategory", 
@@ -30,15 +31,17 @@ namespace ET.Server
                 }
                 output[configType.Name] = File.ReadAllBytes(configFilePath);
             }
+
+            return output;
         }
     }
     
-    [Callback(CallbackType.GetOneConfigBytes)]
-    public class GetOneConfigBytes: IFunc<string, byte[]>
+    [Callback]
+    public class GetOneConfigBytes: ACallbackHandler<ConfigComponent.GetOneConfigBytes, byte[]>
     {
-        public byte[] Handle(string configName)
+        public override byte[] Handle(ConfigComponent.GetOneConfigBytes args)
         {
-            byte[] configBytes = File.ReadAllBytes($"../Config/{configName}.bytes");
+            byte[] configBytes = File.ReadAllBytes($"../Config/{args.ConfigName}.bytes");
             return configBytes;
         }
     }

+ 1 - 1
Unity/Assets/Scripts/Codes/Hotfix/Client/Demo/Login/LoginHelper.cs

@@ -23,7 +23,7 @@ namespace ET.Client
                         routerAddressComponent = clientScene.AddComponent<RouterAddressComponent, string, int>(ConstValue.RouterHttpHost, ConstValue.RouterHttpPort);
                         await routerAddressComponent.Init();
                         
-                        clientScene.AddComponent<NetKcpComponent, AddressFamily, int>(routerAddressComponent.RouterManagerIPAddress.AddressFamily, CallbackType.SessionStreamDispatcherClientOuter);
+                        clientScene.AddComponent<NetKcpComponent, AddressFamily, int>(routerAddressComponent.RouterManagerIPAddress.AddressFamily, SessionStreamCallbackId.SessionStreamDispatcherClientOuter);
                     }
                     IPEndPoint realmAddress = routerAddressComponent.GetRealmAddress(account);
                     

+ 8 - 8
Unity/Assets/Scripts/Codes/Hotfix/Client/Demo/Session/SessionStreamDispatcherClientOuter.cs

@@ -3,24 +3,24 @@ using System.IO;
 
 namespace ET.Client
 {
-    [Callback(CallbackType.SessionStreamDispatcherClientOuter)]
-    public class SessionStreamDispatcherClientOuter: IAction<Session, MemoryStream>
+    [Callback(SessionStreamCallbackId.SessionStreamDispatcherClientOuter)]
+    public class SessionStreamDispatcherClientOuter: ACallbackHandler<SessionStreamCallback>
     {
-        public void Handle(Session session, MemoryStream memoryStream)
+        public override void Handle(SessionStreamCallback sessionStreamCallback)
         {
-            ushort opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.KcpOpcodeIndex);
+            ushort opcode = BitConverter.ToUInt16(sessionStreamCallback.MemoryStream.GetBuffer(), Packet.KcpOpcodeIndex);
             Type type = OpcodeTypeComponent.Instance.GetType(opcode);
-            object message = MessageSerializeHelper.DeserializeFrom(opcode, type, memoryStream);
+            object message = MessageSerializeHelper.DeserializeFrom(opcode, type, sessionStreamCallback.MemoryStream);
             
             if (message is IResponse response)
             {
-                session.OnRead(opcode, response);
+                sessionStreamCallback.Session.OnRead(opcode, response);
                 return;
             }
 
-            OpcodeHelper.LogMsg(session.DomainZone(), opcode, message);
+            OpcodeHelper.LogMsg(sessionStreamCallback.Session.DomainZone(), opcode, message);
             // 普通消息或者是Rpc请求消息
-            MessageDispatcherComponent.Instance.Handle(session, opcode, message);
+            MessageDispatcherComponent.Instance.Handle(sessionStreamCallback.Session, opcode, message);
         }
     }
 }

+ 2 - 2
Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Helper/SceneFactory.cs

@@ -28,10 +28,10 @@ namespace ET.Server
                     scene.AddComponent<HttpComponent, string>($"http://{startSceneConfig.OuterIPPort}/");
                     break;
                 case SceneType.Realm:
-                    scene.AddComponent<NetKcpComponent, IPEndPoint, int>(startSceneConfig.InnerIPOutPort, CallbackType.SessionStreamDispatcherServerOuter);
+                    scene.AddComponent<NetKcpComponent, IPEndPoint, int>(startSceneConfig.InnerIPOutPort, SessionStreamCallbackId.SessionStreamDispatcherServerOuter);
                     break;
                 case SceneType.Gate:
-                    scene.AddComponent<NetKcpComponent, IPEndPoint, int>(startSceneConfig.InnerIPOutPort, CallbackType.SessionStreamDispatcherServerOuter);
+                    scene.AddComponent<NetKcpComponent, IPEndPoint, int>(startSceneConfig.InnerIPOutPort, SessionStreamCallbackId.SessionStreamDispatcherServerOuter);
                     scene.AddComponent<PlayerComponent>();
                     scene.AddComponent<GateSessionKeyComponent>();
                     break;

+ 7 - 3
Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Helper/SessionStreamDispatcherServerInner.cs

@@ -3,11 +3,13 @@ using System.IO;
 
 namespace ET.Server
 {
-    [Callback(CallbackType.SessionStreamDispatcherServerInner)]
-    public class SessionStreamDispatcherServerInner: IAction<Session, MemoryStream>
+    [Callback(SessionStreamCallbackId.SessionStreamDispatcherServerInner)]
+    public class SessionStreamDispatcherServerInner: ACallbackHandler<SessionStreamCallback>
     {
-        public void Handle(Session session, MemoryStream memoryStream)
+        public override void Handle(SessionStreamCallback args)
         {
+            Session session = args.Session;
+            MemoryStream memoryStream = args.MemoryStream;
             ushort opcode = 0;
             try
             {
@@ -101,5 +103,7 @@ namespace ET.Server
                 Log.Error($"InnerMessageDispatcher error: {opcode}\n{e}");
             }
         }
+
+
     }
 }

+ 5 - 3
Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Helper/SessionStreamDispatcherServerOuter.cs

@@ -3,11 +3,13 @@ using System.IO;
 
 namespace ET.Server
 {
-    [Callback(CallbackType.SessionStreamDispatcherServerOuter)]
-    public class SessionStreamDispatcherServerOuter: IAction<Session, MemoryStream>
+    [Callback(SessionStreamCallbackId.SessionStreamDispatcherServerOuter)]
+    public class SessionStreamDispatcherServerOuter: ACallbackHandler<SessionStreamCallback>
     {
-        public void Handle(Session session, MemoryStream memoryStream)
+        public override void Handle(SessionStreamCallback args)
         {
+            Session session = args.Session;
+            MemoryStream memoryStream = args.MemoryStream;
             ushort opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.KcpOpcodeIndex);
             Type type = OpcodeTypeComponent.Instance.GetType(opcode);
             object message = MessageSerializeHelper.DeserializeFrom(opcode, type, memoryStream);

+ 5 - 5
Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/InitServer.cs

@@ -2,10 +2,10 @@ using System.Net;
 
 namespace ET.Server
 {
-    [Callback(CallbackType.InitServer)]
-    public class InitServer: IFunc<ETTask>
+    [Callback(InitCallbackId.InitServer)]
+    public class InitServer: ACallbackHandler<InitCallback, ETTask>
     {
-        public async ETTask Handle()
+        public override async ETTask Handle(InitCallback args)
         {
             // 发送普通actor消息
             Game.Scene.AddComponent<ActorMessageSenderComponent>();
@@ -25,7 +25,7 @@ namespace ET.Server
             {
                 case AppType.Server:
                 {
-                    Game.Scene.AddComponent<NetInnerComponent, IPEndPoint, int>(processConfig.InnerIPPort, CallbackType.SessionStreamDispatcherServerInner);
+                    Game.Scene.AddComponent<NetInnerComponent, IPEndPoint, int>(processConfig.InnerIPPort, SessionStreamCallbackId.SessionStreamDispatcherServerInner);
 
                     var processScenes = StartSceneConfigCategory.Instance.GetByProcess(Game.Options.Process);
                     foreach (StartSceneConfig startConfig in processScenes)
@@ -41,7 +41,7 @@ namespace ET.Server
                     StartMachineConfig startMachineConfig = WatcherHelper.GetThisMachineConfig();
                     WatcherComponent watcherComponent = Game.Scene.AddComponent<WatcherComponent>();
                     watcherComponent.Start(Game.Options.CreateScenes);
-                    Game.Scene.AddComponent<NetInnerComponent, IPEndPoint, int>(NetworkHelper.ToIPEndPoint($"{startMachineConfig.InnerIP}:{startMachineConfig.WatcherPort}"), CallbackType.SessionStreamDispatcherServerInner);
+                    Game.Scene.AddComponent<NetInnerComponent, IPEndPoint, int>(NetworkHelper.ToIPEndPoint($"{startMachineConfig.InnerIP}:{startMachineConfig.WatcherPort}"), SessionStreamCallbackId.SessionStreamDispatcherServerInner);
                     break;
                 }
                 case AppType.GameTool:

+ 2 - 2
Unity/Assets/Scripts/Codes/Hotfix/Server/Module/Actor/ActorMessageSenderComponentSystem.cs

@@ -6,7 +6,7 @@ namespace ET.Server
     [FriendOf(typeof(ActorMessageSenderComponent))]
     public static class ActorMessageSenderComponentSystem
     {
-        [Callback(CallbackType.ActorMessageSenderChecker)]
+        [Callback(TimerCallbackId.ActorMessageSenderChecker)]
         public class ActorMessageSenderChecker: ATimer<ActorMessageSenderComponent>
         {
             protected override void Run(ActorMessageSenderComponent self)
@@ -29,7 +29,7 @@ namespace ET.Server
             {
                 ActorMessageSenderComponent.Instance = self;
 
-                self.TimeoutCheckTimer = TimerComponent.Instance.NewRepeatedTimer(1000, CallbackType.ActorMessageSenderChecker, self);
+                self.TimeoutCheckTimer = TimerComponent.Instance.NewRepeatedTimer(1000, TimerCallbackId.ActorMessageSenderChecker, self);
             }
         }
 

+ 2 - 2
Unity/Assets/Scripts/Codes/Hotfix/Server/Module/ActorLocation/ActorLocationSenderComponentSystem.cs

@@ -3,7 +3,7 @@ using System.IO;
 
 namespace ET.Server
 {
-    [Callback(CallbackType.ActorLocationSenderChecker)]
+    [Callback(TimerCallbackId.ActorLocationSenderChecker)]
     public class ActorLocationSenderChecker: ATimer<ActorLocationSenderComponent>
     {
         protected override void Run(ActorLocationSenderComponent self)
@@ -28,7 +28,7 @@ namespace ET.Server
 
             // 每10s扫描一次过期的actorproxy进行回收,过期时间是2分钟
             // 可能由于bug或者进程挂掉,导致ActorLocationSender发送的消息没有确认,结果无法自动删除,每一分钟清理一次这种ActorLocationSender
-            self.CheckTimer = TimerComponent.Instance.NewRepeatedTimer(10 * 1000, CallbackType.ActorLocationSenderChecker, self);
+            self.CheckTimer = TimerComponent.Instance.NewRepeatedTimer(10 * 1000, TimerCallbackId.ActorLocationSenderChecker, self);
         }
     }
 

+ 1 - 1
Unity/Assets/Scripts/Codes/Hotfix/Server/Module/MessageInner/NetInnerComponentSystem.cs

@@ -61,7 +61,7 @@ namespace ET.Server
             }
 
             session.LastRecvTime = TimeHelper.ClientNow();
-            Game.EventSystem.Callback(self.SessionStreamDispatcherType, session, memoryStream);
+            Game.EventSystem.Callback(new SessionStreamCallback() {Id = self.SessionStreamDispatcherType, Session = session, MemoryStream = memoryStream});
         }
 
         public static void OnError(this NetInnerComponent self, long channelId, int error)

+ 3 - 3
Unity/Assets/Scripts/Codes/Hotfix/Share/Demo/InitShare.cs

@@ -1,9 +1,9 @@
 namespace ET
 {
-    [Callback(CallbackType.InitShare)]
-    public class InitShare: IFunc<ETTask>
+    [Callback(InitCallbackId.InitShare)]
+    public class InitShare: ACallbackHandler<InitCallback, ETTask>
     {
-        public async ETTask Handle()
+        public override async ETTask Handle(InitCallback args)
         {
             Game.Scene.AddComponent<TimerComponent>();
             Game.Scene.AddComponent<OpcodeTypeComponent>();

+ 2 - 2
Unity/Assets/Scripts/Codes/Hotfix/Share/Module/AI/AIComponentSystem.cs

@@ -6,7 +6,7 @@ namespace ET
     [FriendOf(typeof(AIDispatcherComponent))]
     public static class AIComponentSystem
     {
-        [Callback(CallbackType.AITimer)]
+        [Callback(TimerCallbackId.AITimer)]
         public class AITimer: ATimer<AIComponent>
         {
             protected override void Run(AIComponent self)
@@ -28,7 +28,7 @@ namespace ET
             protected override void Awake(AIComponent self, int aiConfigId)
             {
                 self.AIConfigId = aiConfigId;
-                self.Timer = TimerComponent.Instance.NewRepeatedTimer(1000, CallbackType.AITimer, self);
+                self.Timer = TimerComponent.Instance.NewRepeatedTimer(1000, TimerCallbackId.AITimer, self);
             }
         }
 

+ 7 - 5
Unity/Assets/Scripts/Codes/Hotfix/Share/Module/Config/ConfigComponentSystem.cs

@@ -27,7 +27,7 @@ namespace ET
 		
 		public static void LoadOneConfig(this ConfigComponent self, Type configType)
 		{
-			byte[] oneConfigBytes = Game.EventSystem.Callback<string, byte[]>(CallbackType.GetOneConfigBytes, configType.FullName);
+			byte[] oneConfigBytes = Game.EventSystem.Callback<ConfigComponent.GetOneConfigBytes, byte[]>(new ConfigComponent.GetOneConfigBytes() {ConfigName = configType.FullName});
 
 			object category = ProtobufHelper.FromBytes(configType, oneConfigBytes, 0, oneConfigBytes.Length);
 
@@ -39,8 +39,9 @@ namespace ET
 			self.AllConfig.Clear();
 			HashSet<Type> types = Game.EventSystem.GetTypes(typeof (ConfigAttribute));
 			
-			Dictionary<string, byte[]> configBytes = new Dictionary<string, byte[]>();
-			Game.EventSystem.Callback(CallbackType.GetAllConfigBytes, self, configBytes);
+			Dictionary<string, byte[]> configBytes = 
+			Game.EventSystem.Callback<ConfigComponent.GetAllConfigBytes, Dictionary<string, byte[]>>(
+				new ConfigComponent.GetAllConfigBytes());
 
 			foreach (Type type in types)
 			{
@@ -53,8 +54,9 @@ namespace ET
 			self.AllConfig.Clear();
 			HashSet<Type> types = Game.EventSystem.GetTypes(typeof (ConfigAttribute));
 			
-			Dictionary<string, byte[]> configBytes = new Dictionary<string, byte[]>();
-			Game.EventSystem.Callback(CallbackType.GetAllConfigBytes, self, configBytes);
+			Dictionary<string, byte[]> configBytes = 
+					Game.EventSystem.Callback<ConfigComponent.GetAllConfigBytes, Dictionary<string, byte[]>>(
+						new ConfigComponent.GetAllConfigBytes());
 
 			using (ListComponent<Task> listTasks = ListComponent<Task>.Create())
 			{

+ 1 - 1
Unity/Assets/Scripts/Codes/Hotfix/Share/Module/Message/NetKcpComponentSystem.cs

@@ -58,7 +58,7 @@ namespace ET
 
             session.LastRecvTime = TimeHelper.ClientNow();
 
-            Game.EventSystem.Callback(self.SessionStreamDispatcherType, session, memoryStream);
+            Game.EventSystem.Callback(new SessionStreamCallback() {Id = self.SessionStreamDispatcherType, Session = session, MemoryStream = memoryStream});
         }
 
         public static void OnError(this NetKcpComponent self, long channelId, int error)

+ 2 - 2
Unity/Assets/Scripts/Codes/Hotfix/Share/Module/Message/SessionAcceptTimeoutComponentSystem.cs

@@ -2,7 +2,7 @@
 
 namespace ET
 {
-    [Callback(CallbackType.SessionAcceptTimeout)]
+    [Callback(TimerCallbackId.SessionAcceptTimeout)]
     public class SessionAcceptTimeout: ATimer<SessionAcceptTimeoutComponent>
     {
         protected override void Run(SessionAcceptTimeoutComponent self)
@@ -23,7 +23,7 @@ namespace ET
     {
         protected override void Awake(SessionAcceptTimeoutComponent self)
         {
-            self.Timer = TimerComponent.Instance.NewOnceTimer(TimeHelper.ServerNow() + 5000, CallbackType.SessionAcceptTimeout, self);
+            self.Timer = TimerComponent.Instance.NewOnceTimer(TimeHelper.ServerNow() + 5000, TimerCallbackId.SessionAcceptTimeout, self);
         }
     }
 

+ 2 - 2
Unity/Assets/Scripts/Codes/Hotfix/Share/Module/Message/SessionIdleCheckerComponentSystem.cs

@@ -2,7 +2,7 @@ using System;
 
 namespace ET
 {
-    [Callback(CallbackType.SessionIdleChecker)]
+    [Callback(TimerCallbackId.SessionIdleChecker)]
     public class SessionIdleChecker: ATimer<SessionIdleCheckerComponent>
     {
         protected override void Run(SessionIdleCheckerComponent self)
@@ -23,7 +23,7 @@ namespace ET
     {
         protected override void Awake(SessionIdleCheckerComponent self, int checkInteral)
         {
-            self.RepeatedTimer = TimerComponent.Instance.NewRepeatedTimer(checkInteral, CallbackType.SessionIdleChecker, self);
+            self.RepeatedTimer = TimerComponent.Instance.NewRepeatedTimer(checkInteral, TimerCallbackId.SessionIdleChecker, self);
         }
     }
 

+ 2 - 2
Unity/Assets/Scripts/Codes/Hotfix/Share/Module/Move/MoveComponentSystem.cs

@@ -7,7 +7,7 @@ namespace ET
     [FriendOf(typeof(MoveComponent))]
     public static class MoveComponentSystem
     {
-        [Callback(CallbackType.MoveTimer)]
+        [Callback(TimerCallbackId.MoveTimer)]
         public class MoveTimer: ATimer<MoveComponent>
         {
             protected override void Run(MoveComponent self)
@@ -197,7 +197,7 @@ namespace ET
             self.StartTime = self.BeginTime;
             self.SetNextTarget();
 
-            self.MoveTimer = TimerComponent.Instance.NewFrameTimer(CallbackType.MoveTimer, self);
+            self.MoveTimer = TimerComponent.Instance.NewFrameTimer(TimerCallbackId.MoveTimer, self);
         }
 
         private static void SetNextTarget(this MoveComponent self)

+ 1 - 1
Unity/Assets/Scripts/Codes/Hotfix/Share/Module/Recast/NavmeshComponentSystem.cs

@@ -21,7 +21,7 @@ namespace ET
                 return ptr;
             }
 
-            byte[] buffer = Game.EventSystem.Callback<string, byte[]>(CallbackType.RecastFileLoader, name);
+            byte[] buffer = Game.EventSystem.Callback<NavmeshComponent.RecastFileLoader, byte[]>(new NavmeshComponent.RecastFileLoader() {Name = name});
             if (buffer.Length == 0)
             {
                 throw new Exception($"no nav data: {name}");

+ 3 - 3
Unity/Assets/Scripts/Codes/HotfixView/Client/Demo/InitClient.cs

@@ -3,10 +3,10 @@ using System.IO;
 
 namespace ET.Client
 {
-    [Callback(CallbackType.InitClient)]
-    public class InitClient: IFunc<ETTask>
+    [Callback(InitCallbackId.InitClient)]
+    public class InitClient: ACallbackHandler<InitCallback, ETTask>
     {
-        public async ETTask Handle()
+        public override async ETTask Handle(InitCallback args)
         {
             // 加载配置
             Game.Scene.AddComponent<ResourcesComponent>();

+ 4 - 4
Unity/Assets/Scripts/Codes/Model/Server/Demo/Map/Recast/RecastFileReader.cs

@@ -2,12 +2,12 @@
 
 namespace ET.Server
 {
-    [Callback(CallbackType.RecastFileLoader)]
-    public class RecastFileReader: IFunc<string, byte[]>
+    [Callback]
+    public class RecastFileReader: ACallbackHandler<NavmeshComponent.RecastFileLoader, byte[]>
     {
-        public byte[] Handle(string name)
+        public override byte[] Handle(NavmeshComponent.RecastFileLoader args)
         {
-            return File.ReadAllBytes(Path.Combine("../Config/Recast", name));
+            return File.ReadAllBytes(Path.Combine("../Config/Recast", args.Name));
         }
     }
 }

+ 2 - 2
Unity/Assets/Scripts/Codes/Model/Server/Entry.cs

@@ -9,8 +9,8 @@
         
         private static async ETTask StartAsync()
         {
-            await Game.EventSystem.Callback<ETTask>(CallbackType.InitShare);
-            await Game.EventSystem.Callback<ETTask>(CallbackType.InitServer);
+            await Game.EventSystem.Callback<InitCallback, ETTask>(new InitCallback() {Id = InitCallbackId.InitShare});
+            await Game.EventSystem.Callback<InitCallback, ETTask>(new InitCallback() {Id = InitCallbackId.InitServer});
         }
     }
 }

+ 15 - 0
Unity/Assets/Scripts/Codes/Model/Share/InitCallback.cs

@@ -0,0 +1,15 @@
+namespace ET
+{
+    [UniqueId(1,10000)]
+    public static class InitCallbackId
+    {
+        public const int InitShare = 1;
+        public const int InitClient = 2;
+        public const int InitServer = 3;
+    }
+    
+    public struct InitCallback: ICallback
+    {
+        public int Id { get; set; }
+    }
+}

+ 1 - 1
Unity/Assets/Scripts/Codes/Model/Share/CallbackType.cs.meta → Unity/Assets/Scripts/Codes/Model/Share/InitCallback.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 920bdf897f7e7184fa7ed4a9493d5e92
+guid: 5d294cb05a84f4f4cb65bb21b4b6a448
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 4 - 3
Unity/Assets/Scripts/Codes/Model/Share/Module/Config/ConfigComponent.cs

@@ -9,13 +9,14 @@ namespace ET
     [ComponentOf(typeof(Scene))]
     public class ConfigComponent: Entity, IAwake, IDestroy
     {
-        public struct GetAllConfigBytes
+        public struct GetAllConfigBytes: ICallback
         {
-            
+            public int Id { get; set; }
         }
         
-        public struct GetOneConfigBytes
+        public struct GetOneConfigBytes: ICallback
         {
+            public int Id { get; set; }
             public string ConfigName;
         }
         

+ 1 - 1
Unity/Assets/Scripts/Codes/Model/Share/Module/Message/NetKcpComponent.cs

@@ -4,7 +4,7 @@ using System.Net.Sockets;
 namespace ET
 {
     [ComponentOf(typeof(Scene))]
-    public class NetKcpComponent: Entity, IAwake<AddressFamily, int>, IAwake<IPEndPoint, int>, IDestroy, IAwake<IAction<int>>
+    public class NetKcpComponent: Entity, IAwake<AddressFamily, int>, IAwake<IPEndPoint, int>, IDestroy
     {
         public AService Service;
         public int SessionStreamDispatcherType { get; set; }

+ 20 - 0
Unity/Assets/Scripts/Codes/Model/Share/Module/Message/SessionStreamCallback.cs

@@ -0,0 +1,20 @@
+using System.IO;
+
+namespace ET
+{
+    public static class SessionStreamCallbackId
+    {
+        public const int SessionStreamDispatcherClientOuter = 1;
+        public const int SessionStreamDispatcherServerOuter = 2;
+        public const int SessionStreamDispatcherServerInner = 3;
+    }
+    
+    public struct SessionStreamCallback: ICallback
+    {
+        public int Id { get; set; }
+
+        public Session Session;
+
+        public MemoryStream MemoryStream;
+    }
+}

+ 11 - 0
Unity/Assets/Scripts/Codes/Model/Share/Module/Message/SessionStreamCallback.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 4762d11453a41f940ad28fcd37018f7f
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 7 - 0
Unity/Assets/Scripts/Codes/Model/Share/Module/Recast/NavmeshComponent.cs

@@ -8,6 +8,13 @@ namespace ET
     {
         public static NavmeshComponent Instance;
         
+        public struct RecastFileLoader: ICallback
+        {
+            public int Id { get; set; }
+            
+            public string Name { get; set; }
+        }
+        
         public Dictionary<string, long> Navmeshs = new Dictionary<string, long>();
     }
 }

+ 3 - 3
Unity/Assets/Scripts/Codes/Model/Share/Module/Timer/ATimer.cs

@@ -1,10 +1,10 @@
 namespace ET
 {
-    public abstract class ATimer<T>: IAction<object> where T: class
+    public abstract class ATimer<T>: ACallbackHandler<TimerCallback> where T: class
     {
-        public void Handle(object a)
+        public override void Handle(TimerCallback a)
         {
-            this.Run(a as T);
+            this.Run(a.Args as T);
         }
 
         protected abstract void Run(T t);

+ 8 - 2
Unity/Assets/Scripts/Codes/Model/Share/Module/Timer/TimerComponent.cs

@@ -132,7 +132,7 @@ namespace ET
                 case TimerClass.OnceTimer:
                 {
                     int type = timerAction.Type;
-                    Game.EventSystem.Callback(type, timerAction.Object);
+                    Game.EventSystem.Callback(new TimerCallback() {Id = type, Args = timerAction.Object});
                     break;
                 }
                 case TimerClass.OnceWaitTimer:
@@ -147,7 +147,7 @@ namespace ET
                     int type = timerAction.Type;
                     long tillTime = TimeHelper.ServerNow() + timerAction.Time;
                     self.AddTimer(tillTime, timerAction);
-                    Game.EventSystem.Callback(type, timerAction.Object);
+                    Game.EventSystem.Callback(new TimerCallback() {Id = type, Args = timerAction.Object});
                     break;
                 }
             }
@@ -313,6 +313,12 @@ namespace ET
         }
     }
 
+    public struct TimerCallback: ICallback
+    {
+        public int Id { get; set; }
+        public object Args;
+    }
+
     
     [ComponentOf(typeof(Scene))]
     public class TimerComponent: Entity, IAwake, IUpdate, ILoad, IDestroy

+ 1 - 16
Unity/Assets/Scripts/Codes/Model/Share/CallbackType.cs → Unity/Assets/Scripts/Codes/Model/Share/TimerCallbackId.cs

@@ -1,23 +1,8 @@
 namespace ET
 {
     [UniqueId(1,10000)]
-    public static class CallbackType
+    public class TimerCallbackId
     {
-        public const int SessionStreamDispatcherClientOuter = 1;
-        public const int SessionStreamDispatcherServerOuter = 2;
-        public const int SessionStreamDispatcherServerInner = 3;
-
-        public const int InitShare = 5;
-        public const int InitClient = 6;
-        public const int InitServer = 7;
-
-        public const int GetAllConfigBytes = 11;
-        public const int GetOneConfigBytes = 12;
-
-        public const int RecastFileLoader = 13;
-        
-        
-        
         // 框架层100-200,逻辑层的timer type从200起
         public const int WaitTimer = 100;
         public const int SessionIdleChecker = 101;

+ 11 - 0
Unity/Assets/Scripts/Codes/Model/Share/TimerCallbackId.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: df66f1997153578449788df8d9285e3a
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 10 - 7
Unity/Assets/Scripts/Codes/ModelView/Client/Demo/Config/ConfigLoader.cs

@@ -4,11 +4,12 @@ using UnityEngine;
 
 namespace ET.Client
 {
-    [Callback(CallbackType.GetAllConfigBytes)]
-    public class GetAllConfigBytes: IAction<ConfigComponent, Dictionary<string, byte[]>>
+    [Callback]
+    public class GetAllConfigBytes: ACallbackHandler<ConfigComponent.GetAllConfigBytes, Dictionary<string, byte[]>>
     {
-        public void Handle(ConfigComponent configComponent, Dictionary<string, byte[]> output)
+        public override Dictionary<string, byte[]> Handle(ConfigComponent.GetAllConfigBytes args)
         {
+            Dictionary<string, byte[]> output = new Dictionary<string, byte[]>();
             using (Game.Scene.AddComponent<ResourcesComponent>())
             {
                 const string configBundleName = "config.unity3d";
@@ -21,13 +22,15 @@ namespace ET.Client
                     output[configType.Name] = v.bytes;
                 }
             }
+
+            return output;
         }
     }
-
-    [Callback(CallbackType.GetOneConfigBytes)]
-    public class GetOneConfigBytes: IFunc<string, byte[]>
+    
+    [Callback]
+    public class GetOneConfigBytes: ACallbackHandler<ConfigComponent.GetOneConfigBytes, byte[]>
     {
-        public byte[] Handle(string configName)
+        public override byte[] Handle(ConfigComponent.GetOneConfigBytes args)
         {
             //TextAsset v = ResourcesComponent.Instance.GetAsset("config.unity3d", configName) as TextAsset;
             //return v.bytes;

+ 5 - 5
Unity/Assets/Scripts/Codes/ModelView/Client/Entry.cs

@@ -33,19 +33,19 @@ namespace ET.Client
 
 				Options.Instance = new Options();
 
-				await Game.EventSystem.Callback<ETTask>(CallbackType.InitShare);
+				await Game.EventSystem.Callback<InitCallback, ETTask>(new InitCallback() {Id = InitCallbackId.InitShare});
 				
 				switch (CodeLoader.Instance.GlobalConfig.CodeMode)
 				{
 					case CodeMode.Client:
-						await Game.EventSystem.Callback<ETTask>(CallbackType.InitClient);
+						await Game.EventSystem.Callback<InitCallback, ETTask>(new InitCallback() {Id = InitCallbackId.InitClient});
 						break;
 					case CodeMode.Server:
-						await Game.EventSystem.Callback<ETTask>(CallbackType.InitServer);
+						await Game.EventSystem.Callback<InitCallback, ETTask>(new InitCallback() {Id = InitCallbackId.InitServer});
 						break;
 					case CodeMode.ClientServer:
-						await Game.EventSystem.Callback<ETTask>(CallbackType.InitServer);
-						await Game.EventSystem.Callback<ETTask>(CallbackType.InitClient);
+						await Game.EventSystem.Callback<InitCallback, ETTask>(new InitCallback() {Id = InitCallbackId.InitServer});
+						await Game.EventSystem.Callback<InitCallback, ETTask>(new InitCallback() {Id = InitCallbackId.InitClient});
 						break;
 					default:
 						throw new ArgumentOutOfRangeException();

+ 3 - 3
Unity/Assets/Scripts/Core/Object/CallbackAttribute.cs

@@ -2,11 +2,11 @@
 {
     public class CallbackAttribute: BaseAttribute
     {
-        public int Type { get; }
+        public int Id { get; }
 
-        public CallbackAttribute(int type)
+        public CallbackAttribute(int id = 0)
         {
-            this.Type = type;
+            this.Id = id;
         }
     }
 }

+ 39 - 78
Unity/Assets/Scripts/Core/Object/EventSystem.cs

@@ -98,7 +98,7 @@ namespace ET
 
         private readonly Dictionary<Type, List<EventInfo>> allEvents = new();
         
-        private object[] allCallbacks;
+        private Dictionary<Type, Dictionary<int, object>> allCallbacks = new Dictionary<Type, Dictionary<int, object>>(); 
 
         private TypeSystems typeSystems = new();
 
@@ -207,11 +207,12 @@ namespace ET
                 }
             }
 
-            this.allCallbacks = new object[10000];
+            this.allCallbacks = new Dictionary<Type, Dictionary<int, object>>();
             foreach (Type type in types[typeof (CallbackAttribute)])
             {
                 object obj = Activator.CreateInstance(type);
-                if (obj == null)
+                ICallbackType iCallbackType = obj as ICallbackType;
+                if (iCallbackType == null)
                 {
                     throw new Exception($"type not is callback: {type.Name}");
                 }
@@ -219,13 +220,23 @@ namespace ET
                 object[] attrs = type.GetCustomAttributes(typeof(CallbackAttribute), false);
                 foreach (object attr in attrs)
                 {
+                    if (!this.allCallbacks.TryGetValue(iCallbackType.Type, out var dict))
+                    {
+                        dict = new Dictionary<int, object>();
+                        this.allCallbacks.Add(iCallbackType.Type, dict);
+                    }
+                    
                     CallbackAttribute callbackAttribute = attr as CallbackAttribute;
-
-                    if (this.allCallbacks[callbackAttribute.Type] != null)
+                    
+                    try
+                    {
+                        dict.Add(callbackAttribute.Id, obj);
+                    }
+                    catch (Exception e)
                     {
-                        throw new Exception($"action type duplicate: {callbackAttribute.Type}");
+                        throw new Exception($"action type duplicate: {iCallbackType.Type.Name} {callbackAttribute.Id}", e);
                     }
-                    this.allCallbacks[callbackAttribute.Type] = obj;
+                    
                 }
             }
         }
@@ -730,95 +741,45 @@ namespace ET
                 aEvent.Handle(scene, a).Coroutine();
             }
         }
-
-        public void Callback(int type)
-        {
-            if (this.allCallbacks[type] is not IAction action)
-            {
-                throw new Exception($"callback error: {type}");
-            }
-            action.Handle();
-        }
-
-        public void Callback<A>(int type, A a)
-        {
-            if (this.allCallbacks[type] is not IAction<A> action)
-            {
-                throw new Exception($"callback error: {type}");
-            }
-            action.Handle(a);
-        }
         
-        public void Callback<A, B>(int type, A a, B b)
+        public void Callback<A>(A args) where A: struct, ICallback
         {
-            if (this.allCallbacks[type] is not IAction<A, B> action)
+            if (!this.allCallbacks.TryGetValue(typeof(A), out var callbackHandlers))
             {
-                throw new Exception($"callback error: {type}");
+                throw new Exception($"Callback error: {typeof(A).Name}");
             }
-            action.Handle(a, b);
-        }
-        
-        public void Callback<A, B, C>(int type, A a, B b, C c)
-        {
-            if (this.allCallbacks[type] is not IAction<A, B, C> action)
-            {
-                throw new Exception($"callback error: {type}");
-            }
-            action.Handle(a, b, c);
-        }
-        
-        public void Callback<A, B, C, D>(int type, A a, B b, C c, D d)
-        {
-            if (this.allCallbacks[type] is not IAction<A, B, C, D> action)
-            {
-                throw new Exception($"callback error: {type}");
-            }
-            action.Handle(a, b, c, d);
-        }
-        
-        public T Callback<T>(int type)
-        {
-            if (this.allCallbacks[type] is not IFunc<T> action)
+            if (!callbackHandlers.TryGetValue(args.Id, out var callbackHandler))
             {
-                throw new Exception($"callback error: {type}");
+                throw new Exception($"Callback error: {typeof(A).Name} {args.Id}");
             }
-            return action.Handle();
-        }
 
-        public T Callback<A, T>(int type, A a)
-        {
-            if (this.allCallbacks[type] is not IFunc<A, T> action)
+            var aCallbackHandler = callbackHandler as ACallbackHandler<A>;
+            if (aCallbackHandler == null)
             {
-                throw new Exception($"callback error: {type}");
+                throw new Exception($"Callback error, not ACallbackHandler: {typeof(A).Name} {args.Id}");
             }
-            return action.Handle(a);
+            
+            aCallbackHandler.Handle(args);
         }
         
-        public T Callback<A, B, T>(int type, A a, B b)
+        public T Callback<A, T>(A args) where A: struct, ICallback
         {
-            if (this.allCallbacks[type] is not IFunc<A, B, T> action)
+            if (!this.allCallbacks.TryGetValue(typeof(A), out var callbackHandlers))
             {
-                throw new Exception($"callback error: {type}");
+                throw new Exception($"ResultCallback error: {typeof(A).Name}");
             }
-            return action.Handle(a, b);
-        }
-        
-        public T Callback<A, B, C, T>(int type, A a, B b, C c)
-        {
-            if (this.allCallbacks[type] is not IFunc<A, B, C, T> action)
+            if (!callbackHandlers.TryGetValue(args.Id, out var callbackHandler))
             {
-                throw new Exception($"callback error: {type}");
+                throw new Exception($"ResultCallback error: {typeof(A).Name} {args.Id}");
             }
-            return action.Handle(a, b, c);
-        }
-        
-        public T Callback<A, B, C, D, T>(int type, A a, B b, C c, D d)
-        {
-            if (this.allCallbacks[type] is not IFunc<A, B, C, D, T> action)
+
+            var aCallbackHandler = callbackHandler as ACallbackHandler<A, T>;
+            if (aCallbackHandler == null)
             {
-                throw new Exception($"callback error: {type}");
+                throw new Exception($"ResultCallback error, not AResultCallbackHandler: {typeof(T).Name} {args.Id}");
             }
-            return action.Handle(a, b, c, d);
+            
+            return aCallbackHandler.Handle(args);
         }
 
         public override string ToString()

+ 25 - 40
Unity/Assets/Scripts/Core/Object/ICallback.cs

@@ -1,53 +1,38 @@
 namespace ET
 {
-
-    public interface IAction
-    {
-        public void Handle();
-    }
-
-    public interface IAction<in A>
-    {
-        public void Handle(A a);
-    }
-
-    public interface IAction<in A, in B>
-    {
-        public void Handle(A a, B b);
-    }
-
-    public interface IAction<in A, in B, in C>
+    public interface ICallback
     {
-        public void Handle(A a, B b, C c);
+        public int Id { get; set; }
     }
-
-    public interface IAction<in A, in B, in C, in D>
+    
+    public interface ICallbackType
     {
-        public void Handle(A a, B b, C c, D d);
+        public System.Type Type { get; }
     }
-
-    public interface IFunc<out T>
-    {
-        public T Handle();
-    }
-
-    public interface IFunc<in A, out T>
+    
+    public abstract class ACallbackHandler<A>: ICallbackType where A: struct, ICallback
     {
-        public T Handle(A a);
-    }
+        public System.Type Type
+        {
+            get
+            {
+                return typeof (A);
+            }
+        }
 
-    public interface IFunc<in A, in B, out T>
-    {
-        public T Handle(A a, B b);
+        public abstract void Handle(A a);
     }
-
-    public interface IFunc<in A, in B, in C, out T>
+    
+    public abstract class ACallbackHandler<A, T>: ICallbackType where A: struct, ICallback
     {
-        public T Handle(A a, B b, C c);
-    }
+        public System.Type Type
+        {
+            get
+            {
+                return typeof (A);
+            }
+        }
 
-    public interface IFunc<in A, in B, in C, in D, out T>
-    {
-        public T Handle(A a, B b, C c, D d);
+        public abstract T Handle(A a);
     }
 }