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

把一些Entity单间类改成挂到World上

tanghai 2 лет назад
Родитель
Сommit
c1eb9bd107
29 измененных файлов с 357 добавлено и 518 удалено
  1. 1 1
      Share/Share.SourceGenerator/Generator/ETGetComponentGenerator.cs
  2. 2 2
      Unity/Assets/Scripts/Core/VProcess/Module/Entity/EntityHelper.cs
  3. 3 3
      Unity/Assets/Scripts/Core/VProcess/Module/Entity/Root.cs
  4. 1 1
      Unity/Assets/Scripts/Core/VProcess/VProcessSingleton.cs
  5. 7 0
      Unity/Assets/Scripts/Core/World/ISingletonLoad.cs
  6. 3 0
      Unity/Assets/Scripts/Core/World/ISingletonLoad.cs.meta
  7. 1 3
      Unity/Assets/Scripts/Core/World/Module/VProcess/VProcessManager.cs
  8. 2 2
      Unity/Assets/Scripts/Hotfix/Client/Demo/Scene/SceneFactory.cs
  9. 2 1
      Unity/Assets/Scripts/Hotfix/Server/Demo/EntryEvent2_InitServer.cs
  10. 10 3
      Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Robot/RobotCaseSystem.cs
  11. 1 1
      Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Robot/RobotManagerComponentSystem.cs
  12. 3 2
      Unity/Assets/Scripts/Hotfix/Server/Demo/Session/NetInnerComponentOnReadEvent.cs
  13. 0 233
      Unity/Assets/Scripts/Hotfix/Server/Module/Actor/ActorMessageDispatcherComponentSystem.cs
  14. 0 11
      Unity/Assets/Scripts/Hotfix/Server/Module/Actor/ActorMessageDispatcherComponentSystem.cs.meta
  15. 0 2
      Unity/Assets/Scripts/Hotfix/Server/Module/Actor/MailBoxComponentSystem.cs
  16. 0 3
      Unity/Assets/Scripts/Hotfix/Share/Demo/EntryEvent1_InitShare.cs
  17. 1 1
      Unity/Assets/Scripts/Hotfix/Share/Module/AI/AIComponentSystem.cs
  18. 0 40
      Unity/Assets/Scripts/Hotfix/Share/Module/AI/AIDispatcherComponentSystem.cs
  19. 0 11
      Unity/Assets/Scripts/Hotfix/Share/Module/AI/AIDispatcherComponentSystem.cs.meta
  20. 0 103
      Unity/Assets/Scripts/Hotfix/Share/Module/Message/MessageDispatcherComponentSystem.cs
  21. 0 11
      Unity/Assets/Scripts/Hotfix/Share/Module/Message/MessageDispatcherComponentSystem.cs.meta
  22. 1 13
      Unity/Assets/Scripts/Hotfix/Share/Module/Scene/ClientSceneManagerComponentSystem.cs
  23. 1 1
      Unity/Assets/Scripts/HotfixView/Client/Demo/EntryEvent3_InitClient.cs
  24. 188 7
      Unity/Assets/Scripts/Model/Server/Module/Actor/ActorMessageDispatcherComponent.cs
  25. 4 1
      Unity/Assets/Scripts/Model/Share/Entry.cs
  26. 27 5
      Unity/Assets/Scripts/Model/Share/Module/AI/AIDispatcherComponent.cs
  27. 73 9
      Unity/Assets/Scripts/Model/Share/Module/Message/MessageDispatcherComponent.cs
  28. 26 45
      Unity/Assets/Scripts/Model/Share/Module/Numeric/NumericWatcherComponent.cs
  29. 0 3
      Unity/Assets/Scripts/Model/Share/Module/Scene/ClientSceneManagerComponent.cs

+ 1 - 1
Share/Share.SourceGenerator/Generator/ETGetComponentGenerator.cs

@@ -13,7 +13,7 @@ public class ETGetComponentGenerator : ISourceGenerator
     
     public void Initialize(GeneratorInitializationContext context)
     {
-        context.RegisterForSyntaxNotifications(SyntaxContextReceiver.Create);
+        //context.RegisterForSyntaxNotifications(SyntaxContextReceiver.Create);
     }
 
     public void Execute(GeneratorExecutionContext context)

+ 2 - 2
Unity/Assets/Scripts/Core/VProcess/Module/Entity/EntityHelper.cs

@@ -12,9 +12,9 @@ namespace ET
             return entity.IScene as Scene;
         }
         
-        public static IScene Root(this Entity entity)
+        public static Scene Root(this Entity entity)
         {
-            return entity.IScene.Root;
+            return entity.IScene.Root as Scene;
         }
         
         public static VProcess VProcess(this Entity entity)

+ 3 - 3
Unity/Assets/Scripts/Core/VProcess/Module/Entity/Root.cs

@@ -6,6 +6,8 @@ namespace ET
     [EnableMethod]
     public class Root: VProcessSingleton<Root>
     {
+        private readonly Dictionary<long, Entity> mailboxEntities = new();
+        
         public Scene Scene { get; private set; }
 
         public Root()
@@ -21,9 +23,7 @@ namespace ET
             }
             
             base.Dispose();
-            
-            
-            
+
             Scene scene = this.Scene;
             this.Scene = null;
             scene.Dispose();

+ 1 - 1
Unity/Assets/Scripts/Core/VProcess/VProcessSingleton.cs

@@ -20,7 +20,7 @@ namespace ET
             }
         }
 
-        public virtual void Register()
+        public void Register()
         {
             this.VProcess.AddInstance(this);
         }

+ 7 - 0
Unity/Assets/Scripts/Core/World/ISingletonLoad.cs

@@ -0,0 +1,7 @@
+namespace ET
+{
+    public interface ISingletonLoad
+    {
+        void Load();
+    }
+}

+ 3 - 0
Unity/Assets/Scripts/Core/World/ISingletonLoad.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: a6552160d3164dee967d0dc9db1611a3
+timeCreated: 1687250763

+ 1 - 3
Unity/Assets/Scripts/Core/World/Module/VProcess/VProcessManager.cs

@@ -20,12 +20,10 @@ namespace ET
                     processId = --this.idGenerator;
                 }
                 VProcess vProcess = new(Options.Instance.Process, processId);
+                vProcess.AddSingleton<TimeInfo>();
                 vProcess.AddSingleton<IdGenerater>();
                 vProcess.AddSingleton<EntitySystem>();
                 vProcess.AddSingleton<VProcessActor>();
-                vProcess.AddSingleton<TimeInfo>();
-                vProcess.AddSingleton<TimerComponent>();
-                vProcess.AddSingleton<CoroutineLockComponent>();
                 vProcess.AddSingleton<Root>();
                 this.vProcesses.TryAdd(vProcess.Id, vProcess);
                 return processId;

+ 2 - 2
Unity/Assets/Scripts/Hotfix/Client/Demo/Scene/SceneFactory.cs

@@ -2,11 +2,11 @@ namespace ET.Client
 {
     public static partial class SceneFactory
     {
-        public static async ETTask<Scene> CreateClientScene(int zone, SceneType sceneType, string name)
+        public static async ETTask<Scene> CreateClientScene(Scene scene, int zone, SceneType sceneType, string name)
         {
             await ETTask.CompletedTask;
             
-            Scene clientScene = EntitySceneFactory.CreateScene(zone, sceneType, name, ClientSceneManagerComponent.Instance);
+            Scene clientScene = EntitySceneFactory.CreateScene(zone, sceneType, name, scene.GetComponent<ClientSceneManagerComponent>());
             clientScene.AddComponent<ObjectWait>();
             clientScene.AddComponent<PlayerComponent>();
             clientScene.AddComponent<CurrentScenesComponent>();

+ 2 - 1
Unity/Assets/Scripts/Hotfix/Server/Demo/EntryEvent2_InitServer.cs

@@ -7,13 +7,14 @@ namespace ET.Server
     {
         protected override async ETTask Run(Scene scene, ET.EventType.EntryEvent2 args)
         {
+            World.Instance.AddSingleton<ActorMessageDispatcherComponent>();
+            
             // 发送普通actor消息
             scene.AddComponent<ActorMessageSenderComponent>();
             // 发送location actor消息
             scene.AddComponent<ActorLocationSenderComponent>();
             // 访问location server的组件
             scene.AddComponent<LocationProxyComponent>();
-            scene.AddComponent<ActorMessageDispatcherComponent>();
             scene.AddComponent<ServerSceneManagerComponent>();
             scene.AddComponent<RobotCaseComponent>();
 

+ 10 - 3
Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Robot/RobotCaseSystem.cs

@@ -8,9 +8,16 @@ namespace ET.Server
         [EntitySystem]
         private static void Destroy(this RobotCase self)
         {
+            Scene root = self.Root();
+            if (root.Id == 0)
+            {
+                return;
+            }
+
+            ClientSceneManagerComponent clientSceneManagerComponent = root.GetComponent<ClientSceneManagerComponent>();
             foreach (long id in self.Scenes)
             {
-                ClientSceneManagerComponent.Instance.Remove(id);
+                clientSceneManagerComponent.Remove(id);
             }
         }
 
@@ -73,7 +80,7 @@ namespace ET.Server
             Scene clientScene = null;
             try
             {
-                clientScene = await Client.SceneFactory.CreateClientScene(zone, SceneType.Robot, name);
+                clientScene = await Client.SceneFactory.CreateClientScene(self.Root(), zone, SceneType.Robot, name);
                 await Client.LoginHelper.Login(clientScene, zone.ToString(), zone.ToString());
                 await Client.EnterMapHelper.EnterMapAsync(clientScene);
                 Log.Debug($"create robot ok: {zone}");
@@ -94,7 +101,7 @@ namespace ET.Server
 
             try
             {
-                clientScene = await Client.SceneFactory.CreateClientScene(zone, SceneType.Robot, $"Robot_{zone}");
+                clientScene = await Client.SceneFactory.CreateClientScene(self.Root(), zone, SceneType.Robot, $"Robot_{zone}");
                 await Client.LoginHelper.Login(clientScene, zone.ToString(), zone.ToString());
                 await Client.EnterMapHelper.EnterMapAsync(clientScene);
                 Log.Debug($"create robot ok: {zone}");

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Robot/RobotManagerComponentSystem.cs

@@ -10,7 +10,7 @@ namespace ET.Server
             Scene clientScene = null;
             try
             {
-                clientScene = await Client.SceneFactory.CreateClientScene(zone, SceneType.Robot, "Robot");
+                clientScene = await Client.SceneFactory.CreateClientScene(self.Root(), zone, SceneType.Robot, "Robot");
                 await Client.LoginHelper.Login(clientScene, zone.ToString(), zone.ToString());
                 await Client.EnterMapHelper.EnterMapAsync(clientScene);
                 Log.Debug($"create robot ok: {zone}");

+ 3 - 2
Unity/Assets/Scripts/Hotfix/Server/Demo/Session/NetInnerComponentOnReadEvent.cs

@@ -7,6 +7,7 @@ namespace ET.Server
     {
         protected override async ETTask Run(Scene scene, NetInnerComponentOnRead args)
         {
+            await ETTask.CompletedTask;
             try
             {
                 ActorId actorId = args.ActorId;
@@ -24,12 +25,12 @@ namespace ET.Server
                 {
                     case IActorRequest iActorRequest:
                     {
-                        await ActorMessageDispatcherComponent.Instance.HandleIActorRequest(actorId, iActorRequest);
+                        //await ActorMessageDispatcherComponent.Instance.HandleIActorRequest(actorId, iActorRequest);
                         break;
                     }
                     case IActorMessage iActorMessage:
                     {
-                        await ActorMessageDispatcherComponent.Instance.HandleIActorMessage(actorId, iActorMessage);
+                        //await ActorMessageDispatcherComponent.Instance.HandleIActorMessage(actorId, iActorMessage);
                         break;
                     }
                 }

+ 0 - 233
Unity/Assets/Scripts/Hotfix/Server/Module/Actor/ActorMessageDispatcherComponentSystem.cs

@@ -1,233 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using MongoDB.Bson;
-
-namespace ET.Server
-{
-    /// <summary>
-    /// Actor消息分发组件
-    /// </summary>
-    [FriendOf(typeof(ActorMessageDispatcherComponent))]
-    public static partial class ActorMessageDispatcherComponentHelper
-    {
-        [EntitySystem]
-        private static void Awake(this ActorMessageDispatcherComponent self)
-        {
-            ActorMessageDispatcherComponent.Instance = self;
-            self.Load();
-        }
-        
-        [EntitySystem]
-        private static void Destroy(this ActorMessageDispatcherComponent self)
-        {
-            self.ActorMessageHandlers.Clear();
-            ActorMessageDispatcherComponent.Instance = null;
-        }
-        
-        [EntitySystem]
-        private static void Load(this ActorMessageDispatcherComponent self)
-        {
-            self.ActorMessageHandlers.Clear();
-
-            HashSet<Type> types = EventSystem.Instance.GetTypes(typeof (ActorMessageHandlerAttribute));
-            
-            foreach (Type type in types)
-            {
-                self.Register(type);
-            }
-            
-            HashSet<Type> types2 = EventSystem.Instance.GetTypes(typeof (ActorMessageLocationHandlerAttribute));
-            
-            foreach (Type type in types2)
-            {
-                self.Register(type);
-            }
-        }
-
-        private static void Register(this ActorMessageDispatcherComponent self, Type type)
-        {
-            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}");
-            }
-                
-            object[] attrs = type.GetCustomAttributes(typeof(ActorMessageHandlerAttribute), true);
-
-            foreach (object attr in attrs)
-            {
-                ActorMessageHandlerAttribute actorMessageHandlerAttribute = attr as ActorMessageHandlerAttribute;
-
-                Type messageType = imHandler.GetRequestType();
-
-                Type handleResponseType = imHandler.GetResponseType();
-                if (handleResponseType != null)
-                {
-                    Type responseType = OpcodeType.Instance.GetResponseType(messageType);
-                    if (handleResponseType != responseType)
-                    {
-                        throw new Exception($"message handler response type error: {messageType.FullName}");
-                    }
-                }
-
-                ActorMessageDispatcherInfo actorMessageDispatcherInfo = new(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);
-        }
-
-        private static async ETTask Handle(this ActorMessageDispatcherComponent self, Entity entity, ActorId actorId, object message)
-        {
-            List<ActorMessageDispatcherInfo> list;
-            if (!self.ActorMessageHandlers.TryGetValue(message.GetType(), out list))
-            {
-                throw new Exception($"not found message handler: {message} {entity.GetType().FullName}");
-            }
-
-            SceneType sceneType = entity.IScene.SceneType;
-            foreach (ActorMessageDispatcherInfo actorMessageDispatcherInfo in list)
-            {
-                if (!actorMessageDispatcherInfo.SceneType.HasSameFlag(sceneType))
-                {
-                    continue;
-                }
-                await actorMessageDispatcherInfo.IMActorHandler.Handle(entity, actorId, message);   
-            }
-        }
-
-
-        /// <summary>
-        /// 分发actor消息
-        /// </summary>
-        [EnableAccessEntiyChild]
-        public static async ETTask HandleIActorRequest(this ActorMessageDispatcherComponent self, ActorId actorId, IActorRequest iActorRequest)
-        {
-            Entity entity = self.Get(actorId.InstanceId);
-            if (entity == null)
-            {
-                IActorResponse response = ActorHelper.CreateResponse(iActorRequest, ErrorCore.ERR_NotFoundActor);
-                ActorHandleHelper.Reply(actorId, response);
-                return;
-            }
-            
-            Log.Debug(iActorRequest.ToJson());
-
-            MailBoxComponent mailBoxComponent = entity.GetComponent<MailBoxComponent>();
-            if (mailBoxComponent == null)
-            {
-                Log.Warning($"actor not found mailbox: {entity.GetType().FullName} {actorId} {iActorRequest}");
-                IActorResponse response = ActorHelper.CreateResponse(iActorRequest, ErrorCore.ERR_NotFoundActor);
-                ActorHandleHelper.Reply(actorId, response);
-                return;
-            }
-            
-            switch (mailBoxComponent.MailboxType)
-            {
-                case MailboxType.OrderedMessage:
-                {
-                    using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Mailbox, actorId.InstanceId))
-                    {
-                        if (entity.InstanceId != actorId.InstanceId)
-                        {
-                            IActorResponse response = ActorHelper.CreateResponse(iActorRequest, ErrorCore.ERR_NotFoundActor);
-                            ActorHandleHelper.Reply(actorId, response);
-                            break;
-                        }
-                        await self.Handle(entity, actorId, iActorRequest);
-                    }
-                    break;
-                }
-                case MailboxType.UnOrderMessageDispatcher:
-                {
-                    await self.Handle(entity, actorId, iActorRequest);
-                    break;
-                }
-                default:
-                    throw new Exception($"no mailboxtype: {mailBoxComponent.MailboxType} {iActorRequest}");
-            }
-        }
-        
-        /// <summary>
-        /// 分发actor消息
-        /// </summary>
-        [EnableAccessEntiyChild]
-        public static async ETTask HandleIActorMessage(this ActorMessageDispatcherComponent self, ActorId actorId, IActorMessage iActorMessage)
-        {
-            Entity entity = self.Get(actorId.InstanceId);
-            if (entity == null)
-            {
-                Log.Error($"not found actor: {actorId} {iActorMessage}");
-                return;
-            }
-            
-            MailBoxComponent mailBoxComponent = entity.GetComponent<MailBoxComponent>();
-            if (mailBoxComponent == null)
-            {
-                Log.Error($"actor not found mailbox: {entity.GetType().FullName} {actorId} {iActorMessage}");
-                return;
-            }
-
-            switch (mailBoxComponent.MailboxType)
-            {
-                case MailboxType.OrderedMessage:
-                {
-                    using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Mailbox, actorId.InstanceId))
-                    {
-                        if (entity.InstanceId != actorId.InstanceId)
-                        {
-                            break;
-                        }
-                        await self.Handle(entity, actorId, iActorMessage);
-                    }
-                    break;
-                }
-                case MailboxType.UnOrderMessageDispatcher:
-                {
-                    await self.Handle(entity, actorId, iActorMessage);
-                    break;
-                }
-                case MailboxType.GateSession:
-                {
-                    if (entity is PlayerSessionComponent playerSessionComponent)
-                    {
-                        playerSessionComponent.Session?.Send(iActorMessage);
-                    }
-                    break;
-                }
-                default:
-                    throw new Exception($"no mailboxtype: {mailBoxComponent.MailboxType} {iActorMessage}");
-            }
-        }
-        
-        
-        public static void Add(this ActorMessageDispatcherComponent self, Entity entity)
-        {
-            self.mailboxEntities.Add(entity.InstanceId, entity);
-        }
-        
-        public static void Remove(this ActorMessageDispatcherComponent self, long instanceId)
-        {
-            self.mailboxEntities.Remove(instanceId);
-        }
-
-        private static Entity Get(this ActorMessageDispatcherComponent self, long instanceId)
-        {
-            Entity component = null;
-            self.mailboxEntities.TryGetValue(instanceId, out component);
-            return component;
-        }
-    }
-}

+ 0 - 11
Unity/Assets/Scripts/Hotfix/Server/Module/Actor/ActorMessageDispatcherComponentSystem.cs.meta

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

+ 0 - 2
Unity/Assets/Scripts/Hotfix/Server/Module/Actor/MailBoxComponentSystem.cs

@@ -10,13 +10,11 @@ namespace ET.Server
         {
             self.MailboxType = mailboxType;
             self.ParentInstanceId = self.Parent.InstanceId;
-            ActorMessageDispatcherComponent.Instance.Add(self.Parent);
         }
         
         [EntitySystem]
         private static void Destroy(this MailBoxComponent self)
         {
-            ActorMessageDispatcherComponent.Instance?.Remove(self.ParentInstanceId);
         }
     }
 }

+ 0 - 3
Unity/Assets/Scripts/Hotfix/Share/Demo/EntryEvent1_InitShare.cs

@@ -5,9 +5,6 @@ namespace ET
     {
         protected override async ETTask Run(Scene scene, EventType.EntryEvent1 args)
         {
-            scene.AddComponent<MessageDispatcherComponent>();
-            scene.AddComponent<NumericWatcherComponent>();
-            scene.AddComponent<AIDispatcherComponent>();
             scene.AddComponent<ClientSceneManagerComponent>();
 
             await ETTask.CompletedTask;

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

@@ -51,7 +51,7 @@ namespace ET
             foreach (AIConfig aiConfig in oneAI.Values)
             {
 
-                AIDispatcherComponent.Instance.AIHandlers.TryGetValue(aiConfig.Name, out AAIHandler aaiHandler);
+                AAIHandler aaiHandler = AIDispatcherComponent.Instance.Get(aiConfig.Name);
 
                 if (aaiHandler == null)
                 {

+ 0 - 40
Unity/Assets/Scripts/Hotfix/Share/Module/AI/AIDispatcherComponentSystem.cs

@@ -1,40 +0,0 @@
-using System;
-
-namespace ET
-{
-    [FriendOf(typeof(AIDispatcherComponent))]
-    public static partial class AIDispatcherComponentSystem
-    {
-        [EntitySystem]
-        private static void Awake(this AIDispatcherComponent self)
-        {
-            AIDispatcherComponent.Instance = self;
-            self.Load();
-        }
-
-        [EntitySystem]
-        private static void Destroy(this AIDispatcherComponent self)
-        {
-            self.AIHandlers.Clear();
-            AIDispatcherComponent.Instance = null;
-        }
-        
-        [EntitySystem]
-        private static void Load(this AIDispatcherComponent self)
-        {
-            self.AIHandlers.Clear();
-            
-            var types = EventSystem.Instance.GetTypes(typeof (AIHandlerAttribute));
-            foreach (Type type in types)
-            {
-                AAIHandler aaiHandler = Activator.CreateInstance(type) as AAIHandler;
-                if (aaiHandler == null)
-                {
-                    Log.Error($"robot ai is not AAIHandler: {type.Name}");
-                    continue;
-                }
-                self.AIHandlers.Add(type.Name, aaiHandler);
-            }
-        }
-    }
-}

+ 0 - 11
Unity/Assets/Scripts/Hotfix/Share/Module/AI/AIDispatcherComponentSystem.cs.meta

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

+ 0 - 103
Unity/Assets/Scripts/Hotfix/Share/Module/Message/MessageDispatcherComponentSystem.cs

@@ -1,103 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-
-namespace ET
-{
-    /// <summary>
-    /// 消息分发组件
-    /// </summary>
-    [FriendOf(typeof(MessageDispatcherComponent))]
-    public static partial class MessageDispatcherComponentHelper
-    {
-        [EntitySystem]
-        private static void Awake(this MessageDispatcherComponent self)
-        {
-            MessageDispatcherComponent.Instance = self;
-            self.Load();
-        }
-        
-        [EntitySystem]
-        private static void Destroy(this MessageDispatcherComponent self)
-        {
-            MessageDispatcherComponent.Instance = null;
-            self.Handlers.Clear();
-        }
-        
-        [EntitySystem]
-        private static void Load(this MessageDispatcherComponent self)
-        {
-            self.Handlers.Clear();
-
-            HashSet<Type> types = EventSystem.Instance.GetTypes(typeof (MessageHandlerAttribute));
-
-            foreach (Type type in types)
-            {
-                IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler;
-                if (iMHandler == null)
-                {
-                    Log.Error($"message handle {type.Name} 需要继承 IMHandler");
-                    continue;
-                }
-
-                object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), true);
-                
-                foreach (object attr in attrs)
-                {
-                    MessageHandlerAttribute messageHandlerAttribute = attr as MessageHandlerAttribute;
-                    
-                    Type messageType = iMHandler.GetMessageType();
-                    
-                    ushort opcode = OpcodeType.Instance.GetOpcode(messageType);
-                    if (opcode == 0)
-                    {
-                        Log.Error($"消息opcode为0: {messageType.Name}");
-                        continue;
-                    }
-
-                    MessageDispatcherInfo messageDispatcherInfo = new (messageHandlerAttribute.SceneType, iMHandler);
-                    self.RegisterHandler(opcode, messageDispatcherInfo);
-                }
-            }
-        }
-
-        private static void RegisterHandler(this MessageDispatcherComponent self, ushort opcode, MessageDispatcherInfo handler)
-        {
-            if (!self.Handlers.ContainsKey(opcode))
-            {
-                self.Handlers.Add(opcode, new List<MessageDispatcherInfo>());
-            }
-
-            self.Handlers[opcode].Add(handler);
-        }
-
-        public static void Handle(this MessageDispatcherComponent self, Session session, object message)
-        {
-            List<MessageDispatcherInfo> actions;
-            ushort opcode = OpcodeType.Instance.GetOpcode(message.GetType());
-            if (!self.Handlers.TryGetValue(opcode, out actions))
-            {
-                Log.Error($"消息没有处理: {opcode} {message}");
-                return;
-            }
-
-            SceneType sceneType = session.IScene.SceneType;
-            foreach (MessageDispatcherInfo ev in actions)
-            {
-                if (!ev.SceneType.HasSameFlag(sceneType))
-                {
-                    continue;
-                }
-                
-                try
-                {
-                    ev.IMHandler.Handle(session, message);
-                }
-                catch (Exception e)
-                {
-                    Log.Error(e);
-                }
-            }
-        }
-    }
-}

+ 0 - 11
Unity/Assets/Scripts/Hotfix/Share/Module/Message/MessageDispatcherComponentSystem.cs.meta

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

+ 1 - 13
Unity/Assets/Scripts/Hotfix/Share/Module/Scene/ClientSceneManagerComponentSystem.cs

@@ -3,21 +3,9 @@
     [FriendOf(typeof(ClientSceneManagerComponent))]
     public static partial class ClientSceneManagerComponentSystem
     {
-        [EntitySystem]
-        private static void Awake(this ClientSceneManagerComponent self)
-        {
-            ClientSceneManagerComponent.Instance = self;
-        }
-
-        [EntitySystem]
-        private static void Destroy(this ClientSceneManagerComponent self)
-        {
-            ClientSceneManagerComponent.Instance = null;
-        }
-        
         public static Scene ClientScene(this Entity entity)
         {
-            return ClientSceneManagerComponent.Instance.Get(entity.DomainZone());
+            return entity.Root().GetComponent<ClientSceneManagerComponent>().Get(entity.DomainZone());
         }
         
         public static Scene Get(this ClientSceneManagerComponent self, long id)

+ 1 - 1
Unity/Assets/Scripts/HotfixView/Client/Demo/EntryEvent3_InitClient.cs

@@ -15,7 +15,7 @@ namespace ET.Client
 
             SceneType sceneType = EnumHelper.FromString<SceneType>(GlobalComponent.Instance.GlobalConfig.AppType.ToString());
             
-            Scene clientScene = await SceneFactory.CreateClientScene(1, sceneType, sceneType.ToString());
+            Scene clientScene = await SceneFactory.CreateClientScene(scene, 1, sceneType, sceneType.ToString());
             
             await EventSystem.Instance.PublishAsync(clientScene, new EventType.AppStartInitFinish());
         }

+ 188 - 7
Unity/Assets/Scripts/Model/Server/Module/Actor/ActorMessageDispatcherComponent.cs

@@ -19,15 +19,196 @@ namespace ET.Server
     /// <summary>
     /// Actor消息分发组件
     /// </summary>
-    [ComponentOf(typeof(Scene))]
-    public class ActorMessageDispatcherComponent: Entity, IAwake, IDestroy, ILoad
+    public class ActorMessageDispatcherComponent: Singleton<ActorMessageDispatcherComponent>, ISingletonAwake, ISingletonLoad
     {
-        [ThreadStatic]
-        [StaticField]
-        public static ActorMessageDispatcherComponent Instance;
+        private readonly Dictionary<Type, List<ActorMessageDispatcherInfo>> ActorMessageHandlers = new();
 
-        public readonly Dictionary<Type, List<ActorMessageDispatcherInfo>> ActorMessageHandlers = new();
+        public void Awake()
+        {
+            HashSet<Type> types = EventSystem.Instance.GetTypes(typeof (ActorMessageHandlerAttribute));
+            
+            foreach (Type type in types)
+            {
+                this.Register(type);
+            }
+            
+            HashSet<Type> types2 = EventSystem.Instance.GetTypes(typeof (ActorMessageLocationHandlerAttribute));
+            
+            foreach (Type type in types2)
+            {
+                this.Register(type);
+            }
+        }
+        
+        
+        public void Load()
+        {
+        }
+
+        private void Register(Type type)
+        {
+            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}");
+            }
+                
+            object[] attrs = type.GetCustomAttributes(typeof(ActorMessageHandlerAttribute), true);
+
+            foreach (object attr in attrs)
+            {
+                ActorMessageHandlerAttribute actorMessageHandlerAttribute = attr as ActorMessageHandlerAttribute;
+
+                Type messageType = imHandler.GetRequestType();
+
+                Type handleResponseType = imHandler.GetResponseType();
+                if (handleResponseType != null)
+                {
+                    Type responseType = OpcodeType.Instance.GetResponseType(messageType);
+                    if (handleResponseType != responseType)
+                    {
+                        throw new Exception($"message handler response type error: {messageType.FullName}");
+                    }
+                }
+
+                ActorMessageDispatcherInfo actorMessageDispatcherInfo = new(actorMessageHandlerAttribute.SceneType, imHandler);
+
+                this.RegisterHandler(messageType, actorMessageDispatcherInfo);
+            }
+        }
         
-        public readonly Dictionary<long, Entity> mailboxEntities = new();
+        private void RegisterHandler(Type type, ActorMessageDispatcherInfo handler)
+        {
+            if (!this.ActorMessageHandlers.ContainsKey(type))
+            {
+                this.ActorMessageHandlers.Add(type, new List<ActorMessageDispatcherInfo>());
+            }
+
+            this.ActorMessageHandlers[type].Add(handler);
+        }
+
+        private async ETTask Handle(Entity entity, ActorId actorId, object message)
+        {
+            List<ActorMessageDispatcherInfo> list;
+            if (!this.ActorMessageHandlers.TryGetValue(message.GetType(), out list))
+            {
+                throw new Exception($"not found message handler: {message} {entity.GetType().FullName}");
+            }
+
+            SceneType sceneType = entity.IScene.SceneType;
+            foreach (ActorMessageDispatcherInfo actorMessageDispatcherInfo in list)
+            {
+                if (!actorMessageDispatcherInfo.SceneType.HasSameFlag(sceneType))
+                {
+                    continue;
+                }
+                await actorMessageDispatcherInfo.IMActorHandler.Handle(entity, actorId, message);   
+            }
+        }
+
+/*
+        /// <summary>
+        /// 分发actor消息
+        /// </summary>
+        public async ETTask HandleIActorRequest(ActorId actorId, IActorRequest iActorRequest)
+        {
+            Entity entity = self.Get(actorId.InstanceId);
+            if (entity == null)
+            {
+                IActorResponse response = ActorHelper.CreateResponse(iActorRequest, ErrorCore.ERR_NotFoundActor);
+                ActorHandleHelper.Reply(actorId, response);
+                return;
+            }
+            
+            Log.Debug(iActorRequest.ToJson());
+
+            MailBoxComponent mailBoxComponent = entity.GetComponent<MailBoxComponent>();
+            if (mailBoxComponent == null)
+            {
+                Log.Warning($"actor not found mailbox: {entity.GetType().FullName} {actorId} {iActorRequest}");
+                IActorResponse response = ActorHelper.CreateResponse(iActorRequest, ErrorCore.ERR_NotFoundActor);
+                ActorHandleHelper.Reply(actorId, response);
+                return;
+            }
+            
+            switch (mailBoxComponent.MailboxType)
+            {
+                case MailboxType.OrderedMessage:
+                {
+                    using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Mailbox, actorId.InstanceId))
+                    {
+                        if (entity.InstanceId != actorId.InstanceId)
+                        {
+                            IActorResponse response = ActorHelper.CreateResponse(iActorRequest, ErrorCore.ERR_NotFoundActor);
+                            ActorHandleHelper.Reply(actorId, response);
+                            break;
+                        }
+                        await self.Handle(entity, actorId, iActorRequest);
+                    }
+                    break;
+                }
+                case MailboxType.UnOrderMessageDispatcher:
+                {
+                    await self.Handle(entity, actorId, iActorRequest);
+                    break;
+                }
+                default:
+                    throw new Exception($"no mailboxtype: {mailBoxComponent.MailboxType} {iActorRequest}");
+            }
+        }
+        
+        /// <summary>
+        /// 分发actor消息
+        /// </summary>
+        [EnableAccessEntiyChild]
+        public static async ETTask HandleIActorMessage(this ActorMessageDispatcherComponent self, ActorId actorId, IActorMessage iActorMessage)
+        {
+            Entity entity = self.Get(actorId.InstanceId);
+            if (entity == null)
+            {
+                Log.Error($"not found actor: {actorId} {iActorMessage}");
+                return;
+            }
+            
+            MailBoxComponent mailBoxComponent = entity.GetComponent<MailBoxComponent>();
+            if (mailBoxComponent == null)
+            {
+                Log.Error($"actor not found mailbox: {entity.GetType().FullName} {actorId} {iActorMessage}");
+                return;
+            }
+
+            switch (mailBoxComponent.MailboxType)
+            {
+                case MailboxType.OrderedMessage:
+                {
+                    using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Mailbox, actorId.InstanceId))
+                    {
+                        if (entity.InstanceId != actorId.InstanceId)
+                        {
+                            break;
+                        }
+                        await self.Handle(entity, actorId, iActorMessage);
+                    }
+                    break;
+                }
+                case MailboxType.UnOrderMessageDispatcher:
+                {
+                    await self.Handle(entity, actorId, iActorMessage);
+                    break;
+                }
+                case MailboxType.GateSession:
+                {
+                    if (entity is PlayerSessionComponent playerSessionComponent)
+                    {
+                        playerSessionComponent.Session?.Send(iActorMessage);
+                    }
+                    break;
+                }
+                default:
+                    throw new Exception($"no mailboxtype: {mailBoxComponent.MailboxType} {iActorMessage}");
+            }
+        }*/
     }
 }

+ 4 - 1
Unity/Assets/Scripts/Model/Share/Entry.cs

@@ -39,7 +39,10 @@ namespace ET
             
             World.Instance.AddSingleton<EntitySystemSingleton>();
             World.Instance.AddSingleton<LSEntitySystemSingleton>();
-
+            World.Instance.AddSingleton<MessageDispatcherComponent>();
+            World.Instance.AddSingleton<NumericWatcherComponent>();
+            World.Instance.AddSingleton<AIDispatcherComponent>();
+            
             VProcess vProcess = VProcess.Instance;
             
             vProcess.AddSingleton<MainThreadSynchronizationContext>();

+ 27 - 5
Unity/Assets/Scripts/Model/Share/Module/AI/AIDispatcherComponent.cs

@@ -1,13 +1,35 @@
+using System;
 using System.Collections.Generic;
 
 namespace ET
 {
-    [ComponentOf(typeof(Scene))]
-    public class AIDispatcherComponent: Entity, IAwake, IDestroy, ILoad
+    public class AIDispatcherComponent: Singleton<AIDispatcherComponent>, ISingletonAwake, ISingletonLoad
     {
-        [StaticField]
-        public static AIDispatcherComponent Instance;
+        private readonly Dictionary<string, AAIHandler> aiHandlers = new();
         
-        public Dictionary<string, AAIHandler> AIHandlers = new Dictionary<string, AAIHandler>();
+        public void Awake()
+        {
+            var types = EventSystem.Instance.GetTypes(typeof (AIHandlerAttribute));
+            foreach (Type type in types)
+            {
+                AAIHandler aaiHandler = Activator.CreateInstance(type) as AAIHandler;
+                if (aaiHandler == null)
+                {
+                    Log.Error($"robot ai is not AAIHandler: {type.Name}");
+                    continue;
+                }
+                this.aiHandlers.Add(type.Name, aaiHandler);
+            }
+        }
+
+        public void Load()
+        {
+        }
+
+        public AAIHandler Get(string key)
+        {
+            this.aiHandlers.TryGetValue(key, out var aaiHandler);
+            return aaiHandler;
+        }
     }
 }

+ 73 - 9
Unity/Assets/Scripts/Model/Share/Module/Message/MessageDispatcherComponent.cs

@@ -1,4 +1,5 @@
 using System.Collections.Generic;
+using System;
 
 namespace ET
 {
@@ -14,18 +15,81 @@ namespace ET
         }
     }
     
-    /// <summary>
-    /// 消息分发组件
-    /// </summary>
-    [ComponentOf(typeof(Scene))]
-    public class MessageDispatcherComponent: Entity, IAwake, IDestroy, ILoad
+    public class MessageDispatcherComponent: Singleton<MessageDispatcherComponent>, ISingletonAwake
     {
-        public static MessageDispatcherComponent Instance
+        private readonly Dictionary<ushort, List<MessageDispatcherInfo>> handlers = new();
+        
+        public void Awake()
         {
-            get;
-            set;
+            HashSet<Type> types = EventSystem.Instance.GetTypes(typeof (MessageHandlerAttribute));
+
+            foreach (Type type in types)
+            {
+                IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler;
+                if (iMHandler == null)
+                {
+                    Log.Error($"message handle {type.Name} 需要继承 IMHandler");
+                    continue;
+                }
+
+                object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), true);
+                
+                foreach (object attr in attrs)
+                {
+                    MessageHandlerAttribute messageHandlerAttribute = attr as MessageHandlerAttribute;
+                    
+                    Type messageType = iMHandler.GetMessageType();
+                    
+                    ushort opcode = OpcodeType.Instance.GetOpcode(messageType);
+                    if (opcode == 0)
+                    {
+                        Log.Error($"消息opcode为0: {messageType.Name}");
+                        continue;
+                    }
+
+                    MessageDispatcherInfo messageDispatcherInfo = new (messageHandlerAttribute.SceneType, iMHandler);
+                    this.RegisterHandler(opcode, messageDispatcherInfo);
+                }
+            }
         }
+        
+        private void RegisterHandler(ushort opcode, MessageDispatcherInfo handler)
+        {
+            if (!this.handlers.ContainsKey(opcode))
+            {
+                this.handlers.Add(opcode, new List<MessageDispatcherInfo>());
+            }
 
-        public readonly Dictionary<ushort, List<MessageDispatcherInfo>> Handlers = new();
+            this.handlers[opcode].Add(handler);
+        }
+
+        public void Handle(Session session, object message)
+        {
+            List<MessageDispatcherInfo> actions;
+            ushort opcode = OpcodeType.Instance.GetOpcode(message.GetType());
+            if (!this.handlers.TryGetValue(opcode, out actions))
+            {
+                Log.Error($"消息没有处理: {opcode} {message}");
+                return;
+            }
+
+            SceneType sceneType = session.IScene.SceneType;
+            foreach (MessageDispatcherInfo ev in actions)
+            {
+                if (!ev.SceneType.HasSameFlag(sceneType))
+                {
+                    continue;
+                }
+                
+                try
+                {
+                    ev.IMHandler.Handle(session, message);
+                }
+                catch (Exception e)
+                {
+                    Log.Error(e);
+                }
+            }
+        }
     }
 }

+ 26 - 45
Unity/Assets/Scripts/Model/Share/Module/Numeric/NumericWatcherComponent.cs

@@ -3,26 +3,27 @@ using System.Collections.Generic;
 
 namespace ET
 {
-    [FriendOf(typeof(NumericWatcherComponent))]
-    public static partial class NumericWatcherComponentSystem
+    public class NumericWatcherInfo
     {
-        [EntitySystem]
-        private static void Awake(this NumericWatcherComponent self)
-        {
-            NumericWatcherComponent.Instance = self;
-            self.Init();
-        }
+        public SceneType SceneType { get; }
+        public INumericWatcher INumericWatcher { get; }
 
-	    [EntitySystem]
-        private static void Load(this NumericWatcherComponent self)
+        public NumericWatcherInfo(SceneType sceneType, INumericWatcher numericWatcher)
         {
-            self.Init();
+            this.SceneType = sceneType;
+            this.INumericWatcher = numericWatcher;
         }
-            
-        private static void Init(this NumericWatcherComponent self)
-        {
-            self.allWatchers = new Dictionary<int, List<NumericWatcherInfo>>();
+    }
 
+    /// <summary>
+    /// 监视数值变化组件,分发监听
+    /// </summary>
+    public class NumericWatcherComponent : Singleton<NumericWatcherComponent>, ISingletonAwake, ISingletonLoad
+    {
+        private readonly Dictionary<int, List<NumericWatcherInfo>> allWatchers = new();
+        
+        public void Awake()
+        {
             HashSet<Type> types = EventSystem.Instance.GetTypes(typeof(NumericWatcherAttribute));
             foreach (Type type in types)
             {
@@ -32,20 +33,24 @@ namespace ET
                 {
                     NumericWatcherAttribute numericWatcherAttribute = (NumericWatcherAttribute)attr;
                     INumericWatcher obj = (INumericWatcher)Activator.CreateInstance(type);
-                    NumericWatcherInfo numericWatcherInfo = new NumericWatcherInfo(numericWatcherAttribute.SceneType, obj);
-                    if (!self.allWatchers.ContainsKey(numericWatcherAttribute.NumericType))
+                    NumericWatcherInfo numericWatcherInfo = new(numericWatcherAttribute.SceneType, obj);
+                    if (!this.allWatchers.ContainsKey(numericWatcherAttribute.NumericType))
                     {
-                        self.allWatchers.Add(numericWatcherAttribute.NumericType, new List<NumericWatcherInfo>());
+                        this.allWatchers.Add(numericWatcherAttribute.NumericType, new List<NumericWatcherInfo>());
                     }
-                    self.allWatchers[numericWatcherAttribute.NumericType].Add(numericWatcherInfo);
+                    this.allWatchers[numericWatcherAttribute.NumericType].Add(numericWatcherInfo);
                 }
             }
         }
 
-        public static void Run(this NumericWatcherComponent self, Unit unit, EventType.NumbericChange args)
+        public void Load()
+        {
+        }
+        
+        public void Run(Unit unit, EventType.NumbericChange args)
         {
             List<NumericWatcherInfo> list;
-            if (!self.allWatchers.TryGetValue(args.NumericType, out list))
+            if (!this.allWatchers.TryGetValue(args.NumericType, out list))
             {
                 return;
             }
@@ -61,28 +66,4 @@ namespace ET
             }
         }
     }
-
-    public class NumericWatcherInfo
-    {
-        public SceneType SceneType { get; }
-        public INumericWatcher INumericWatcher { get; }
-
-        public NumericWatcherInfo(SceneType sceneType, INumericWatcher numericWatcher)
-        {
-            this.SceneType = sceneType;
-            this.INumericWatcher = numericWatcher;
-        }
-    }
-    
-    
-    /// <summary>
-    /// 监视数值变化组件,分发监听
-    /// </summary>
-    [ComponentOf(typeof(Scene))]
-    public class NumericWatcherComponent : Entity, IAwake, ILoad
-    {
-        public static NumericWatcherComponent Instance { get; set; }
-		
-        public Dictionary<int, List<NumericWatcherInfo>> allWatchers;
-    }
 }

+ 0 - 3
Unity/Assets/Scripts/Model/Share/Module/Scene/ClientSceneManagerComponent.cs

@@ -5,8 +5,5 @@ namespace ET
     [ComponentOf(typeof(Scene))]
     public class ClientSceneManagerComponent: Entity, IAwake, IDestroy
     {
-        [ThreadStatic]
-        [StaticField]
-        public static ClientSceneManagerComponent Instance;
     }
 }