Kaynağa Gözat

去掉Entity的LoadSystem,已经被Singleton的Load代替

tanghai 2 yıl önce
ebeveyn
işleme
af3003a899

+ 9 - 0
DotNet/Core/DotNet.Core.csproj

@@ -39,5 +39,14 @@
       <ProjectReference Include="..\..\Share\Share.SourceGenerator\Share.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
       <ProjectReference Include="..\ThirdParty\DotNet.ThirdParty.csproj" />
     </ItemGroup>
+    
+    <ItemGroup>
+      <Folder Include="..\..\Unity\Assets\Scripts\Core\Fiber\Module\Console\">
+        <Link>Core\Fiber\Module\Console</Link>
+      </Folder>
+      <Folder Include="..\..\Unity\Assets\Scripts\Core\World\Module\Console\">
+        <Link>Core\World\Module\Console</Link>
+      </Folder>
+    </ItemGroup>
 
 </Project>

+ 0 - 39
Unity/Assets/Scripts/Core/Entity/ILoadSystem.cs

@@ -1,39 +0,0 @@
-using System;
-
-namespace ET
-{
-	public interface ILoad
-	{
-	}
-	
-	public interface ILoadSystem: ISystemType
-	{
-		void Run(Entity o);
-	}
-
-	[EntitySystem]
-	public abstract class LoadSystem<T> : ILoadSystem where T: Entity, ILoad
-	{
-		void ILoadSystem.Run(Entity o)
-		{
-			this.Load((T)o);
-		}
-
-		Type ISystemType.Type()
-		{
-			return typeof(T);
-		}
-
-		Type ISystemType.SystemType()
-		{
-			return typeof(ILoadSystem);
-		}
-
-		int ISystemType.GetInstanceQueueIndex()
-		{
-			return InstanceQueueIndex.Load;
-		}
-
-		protected abstract void Load(T self);
-	}
-}

+ 0 - 44
Unity/Assets/Scripts/Core/Fiber/EntitySystem.cs

@@ -13,52 +13,8 @@ namespace ET
             {
                 this.queues[i] = new Queue<EntityRef<Entity>>();
             }
-            this.Load();
         }
         
-        public void Load()
-        {
-            Queue<EntityRef<Entity>> queue = this.queues[InstanceQueueIndex.Load];
-            int count = queue.Count;
-            while (count-- > 0)
-            {
-                Entity component = queue.Dequeue();
-                if (component == null)
-                {
-                    continue;
-                }
-
-                if (component.IsDisposed)
-                {
-                    continue;
-                }
-
-                if (component is not ILoad)
-                {
-                    continue;
-                }
-
-                List<object> iLoadSystems = EntitySystemSingleton.Instance.TypeSystems.GetSystems(component.GetType(), typeof (ILoadSystem));
-                if (iLoadSystems == null)
-                {
-                    continue;
-                }
-
-                queue.Enqueue(component);
-
-                foreach (ILoadSystem iLoadSystem in iLoadSystems)
-                {
-                    try
-                    {
-                        iLoadSystem.Run(component);
-                    }
-                    catch (Exception e)
-                    {
-                        Log.Error(e);
-                    }
-                }
-            }
-        }
         
         public virtual void RegisterSystem(Entity component)
         {

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

@@ -8,7 +8,8 @@ namespace ET.Server
     {
         protected override async ETTask Run(Scene root, ET.EventType.EntryEvent2 args)
         {
-            await ETTask.CompletedTask;
+            World.Instance.AddSingleton<HttpDispatcher>();
+            World.Instance.AddSingleton<ConsoleDispatcher>();
             
             switch (Options.Instance.AppType)
             {

+ 4 - 44
Unity/Assets/Scripts/Hotfix/Server/Module/Http/HttpComponentSystem.cs

@@ -13,8 +13,6 @@ namespace ET.Server
         {
             try
             {
-                self.Load();
-                
                 self.Listener = new HttpListener();
 
                 foreach (string s in address.Split(';'))
@@ -42,43 +40,8 @@ namespace ET.Server
             self.Listener.Stop();
             self.Listener.Close();
         }
-        
-        [EntitySystem]
-        private static void Load(this HttpComponent self)
-        {
-            self.dispatcher = new Dictionary<string, IHttpHandler>();
 
-            HashSet<Type> types = EventSystem.Instance.GetTypes(typeof (HttpHandlerAttribute));
-
-            SceneType sceneType = (self.Parent as IScene).SceneType;
-
-            foreach (Type type in types)
-            {
-                object[] attrs = type.GetCustomAttributes(typeof(HttpHandlerAttribute), false);
-                if (attrs.Length == 0)
-                {
-                    continue;
-                }
-
-                HttpHandlerAttribute httpHandlerAttribute = (HttpHandlerAttribute)attrs[0];
-
-                if (httpHandlerAttribute.SceneType != sceneType)
-                {
-                    continue;
-                }
-
-                object obj = Activator.CreateInstance(type);
-
-                IHttpHandler ihttpHandler = obj as IHttpHandler;
-                if (ihttpHandler == null)
-                {
-                    throw new Exception($"HttpHandler handler not inherit IHttpHandler class: {obj.GetType().FullName}");
-                }
-                self.dispatcher.Add(httpHandlerAttribute.Path, ihttpHandler);
-            }
-        }
-        
-        public static async ETTask Accept(this HttpComponent self)
+        private static async ETTask Accept(this HttpComponent self)
         {
             long instanceId = self.InstanceId;
             while (self.InstanceId == instanceId)
@@ -98,15 +61,12 @@ namespace ET.Server
             }
         }
 
-        public static async ETTask Handle(this HttpComponent self, HttpListenerContext context)
+        private static async ETTask Handle(this HttpComponent self, HttpListenerContext context)
         {
             try
             {
-                IHttpHandler handler;
-                if (self.dispatcher.TryGetValue(context.Request.Url.AbsolutePath, out handler))
-                {
-                    await handler.Handle(self.Scene(), context);
-                }
+                IHttpHandler handler = HttpDispatcher.Instance.Get(self.IScene.SceneType, context.Request.Url.AbsolutePath);
+                await handler.Handle(self.Scene(), context);
             }
             catch (Exception e)
             {

+ 1 - 36
Unity/Assets/Scripts/Hotfix/Share/Module/Console/ConsoleComponentSystem.cs

@@ -13,40 +13,10 @@ namespace ET
         [EntitySystem]
         private static void Awake(this ConsoleComponent self)
         {
-            self.Load();
-        
             self.Start().Coroutine();
         }
 
         
-        [EntitySystem]
-        private static void Load(this ConsoleComponent self)
-        {
-            self.Handlers.Clear();
-
-            HashSet<Type> types = EventSystem.Instance.GetTypes(typeof (ConsoleHandlerAttribute));
-
-            foreach (Type type in types)
-            {
-                object[] attrs = type.GetCustomAttributes(typeof(ConsoleHandlerAttribute), false);
-                if (attrs.Length == 0)
-                {
-                    continue;
-                }
-
-                ConsoleHandlerAttribute consoleHandlerAttribute = (ConsoleHandlerAttribute)attrs[0];
-
-                object obj = Activator.CreateInstance(type);
-
-                IConsoleHandler iConsoleHandler = obj as IConsoleHandler;
-                if (iConsoleHandler == null)
-                {
-                    throw new Exception($"ConsoleHandler handler not inherit IConsoleHandler class: {obj.GetType().FullName}");
-                }
-                self.Handlers.Add(consoleHandlerAttribute.Mode, iConsoleHandler);
-            }
-        }
-
         private static async ETTask Start(this ConsoleComponent self)
         {
             self.CancellationTokenSource = new CancellationTokenSource();
@@ -76,12 +46,7 @@ namespace ET
                             string[] lines = line.Split(" ");
                             string mode = modeContex == null? lines[0] : modeContex.Mode;
 
-                            if (!self.Handlers.TryGetValue(mode, out IConsoleHandler iConsoleHandler))
-                            {
-                                Log.Console($"not found command: {line}");
-                                break;
-                            }
-
+                            IConsoleHandler iConsoleHandler = ConsoleDispatcher.Instance.Get(mode);
                             if (modeContex == null)
                             {
                                 modeContex = self.AddComponent<ModeContex>();

+ 1 - 2
Unity/Assets/Scripts/Model/Server/Module/Http/HttpComponent.cs

@@ -7,9 +7,8 @@ namespace ET.Server
     /// http请求分发器
     /// </summary>
     [ComponentOf(typeof(Scene))]
-    public class HttpComponent: Entity, IAwake<string>, IDestroy, ILoad
+    public class HttpComponent: Entity, IAwake<string>, IDestroy
     {
         public HttpListener Listener;
-        public Dictionary<string, IHttpHandler> dispatcher;
     }
 }

+ 0 - 11
Unity/Assets/Scripts/Model/Server/Module/Http/HttpComponent.cs.meta

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

+ 52 - 0
Unity/Assets/Scripts/Model/Server/Module/Http/HttpDispatcher.cs

@@ -0,0 +1,52 @@
+using System;
+using System.Collections.Generic;
+
+namespace ET.Server
+{
+    public class HttpDispatcher: SingletonLock<HttpDispatcher>, ISingletonAwake
+    {
+        private readonly Dictionary<string, Dictionary<int, IHttpHandler>> dispatcher = new();
+        
+        public override void Load()
+        {
+            World.Instance.AddSingleton<HttpDispatcher>();
+        }
+
+        public void Awake()
+        {
+            HashSet<Type> types = EventSystem.Instance.GetTypes(typeof (HttpHandlerAttribute));
+
+            foreach (Type type in types)
+            {
+                object[] attrs = type.GetCustomAttributes(typeof(HttpHandlerAttribute), false);
+                if (attrs.Length == 0)
+                {
+                    continue;
+                }
+
+                HttpHandlerAttribute httpHandlerAttribute = (HttpHandlerAttribute)attrs[0];
+                
+                object obj = Activator.CreateInstance(type);
+
+                IHttpHandler ihttpHandler = obj as IHttpHandler;
+                if (ihttpHandler == null)
+                {
+                    throw new Exception($"HttpHandler handler not inherit IHttpHandler class: {obj.GetType().FullName}");
+                }
+
+                if (!this.dispatcher.TryGetValue(httpHandlerAttribute.Path, out var dict))
+                {
+                    dict = new Dictionary<int, IHttpHandler>();
+                    this.dispatcher.Add(httpHandlerAttribute.Path, dict);
+                }
+                
+                dict.Add((int)httpHandlerAttribute.SceneType, ihttpHandler);
+            }
+        }
+
+        public IHttpHandler Get(SceneType sceneType, string path)
+        {
+            return this.dispatcher[path][(int)sceneType];
+        }
+    }
+}

+ 1 - 1
Unity/Assets/Scripts/Model/Server/Module/Http/IHttpHandler.cs.meta → Unity/Assets/Scripts/Model/Server/Module/Http/HttpDispatcher.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 850082c851b74ff419893672cb613cf3
+guid: 5f32ee5a97327b54dad8859600342649
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 0 - 11
Unity/Assets/Scripts/Model/Server/Module/Http/HttpHandlerAttribute.cs.meta

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

+ 0 - 8
Unity/Assets/Scripts/Model/Share/Module/Actor.meta

@@ -1,8 +0,0 @@
-fileFormatVersion: 2
-guid: decef8ed4c105ea488877735ee5302fe
-folderAsset: yes
-DefaultImporter:
-  externalObjects: {}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 2 - 2
Unity/Assets/Scripts/Model/Share/Module/Console/ConsoleComponent.cs

@@ -15,9 +15,9 @@ namespace ET
     }
 
     [ComponentOf(typeof(Scene))]
-    public class ConsoleComponent: Entity, IAwake, ILoad
+    public class ConsoleComponent: Entity, IAwake
     {
         public CancellationTokenSource CancellationTokenSource;
-        public Dictionary<string, IConsoleHandler> Handlers = new Dictionary<string, IConsoleHandler>();
+
     }
 }

+ 45 - 0
Unity/Assets/Scripts/Model/Share/Module/Console/ConsoleDispatcher.cs

@@ -0,0 +1,45 @@
+using System;
+using System.Collections.Generic;
+
+namespace ET
+{
+    public class ConsoleDispatcher: SingletonLock<ConsoleDispatcher>, ISingletonAwake
+    {
+        private readonly Dictionary<string, IConsoleHandler> handlers = new();
+        
+        public override void Load()
+        {
+            World.Instance.AddSingleton<ConsoleDispatcher>(true);
+        }
+
+        public void Awake()
+        {
+            HashSet<Type> types = EventSystem.Instance.GetTypes(typeof (ConsoleHandlerAttribute));
+
+            foreach (Type type in types)
+            {
+                object[] attrs = type.GetCustomAttributes(typeof(ConsoleHandlerAttribute), false);
+                if (attrs.Length == 0)
+                {
+                    continue;
+                }
+
+                ConsoleHandlerAttribute consoleHandlerAttribute = (ConsoleHandlerAttribute)attrs[0];
+
+                object obj = Activator.CreateInstance(type);
+
+                IConsoleHandler iConsoleHandler = obj as IConsoleHandler;
+                if (iConsoleHandler == null)
+                {
+                    throw new Exception($"ConsoleHandler handler not inherit IConsoleHandler class: {obj.GetType().FullName}");
+                }
+                this.handlers.Add(consoleHandlerAttribute.Mode, iConsoleHandler);
+            }
+        }
+
+        public IConsoleHandler Get(string key)
+        {
+            return this.handlers[key];
+        }
+    }
+}

+ 1 - 1
Unity/Assets/Scripts/Core/Entity/ILoadSystem.cs.meta → Unity/Assets/Scripts/Model/Share/Module/Console/ConsoleDispatcher.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 7386bc90438118745bac0ef813723a0f
+guid: 7cf505646a7016d40817dc8783f37ed1
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2