Browse Source

AddSingleton加上Awake泛型约束

tanghai 2 năm trước cách đây
mục cha
commit
212b5d3a52

+ 5 - 1
Unity/Assets/Scripts/Core/VProcess/Module/ObjectPool/ObjectPool.cs

@@ -3,10 +3,14 @@ using System.Collections.Generic;
 
 namespace ET
 {
-    public class ObjectPool: Singleton<ObjectPool>
+    public class ObjectPool: Singleton<ObjectPool>, ISingletonAwake
     {
         private readonly Dictionary<Type, Queue<object>> pool = new();
         
+        public void Awake()
+        {
+        }
+        
         public T Fetch<T>() where T: class
         {
             return this.Fetch(typeof (T)) as T;

+ 5 - 3
Unity/Assets/Scripts/Core/World/Module/Config/ConfigComponent.cs

@@ -8,7 +8,7 @@ namespace ET
 	/// <summary>
     /// Config组件会扫描所有的有ConfigAttribute标签的配置,加载进来
     /// </summary>
-    public class ConfigComponent: Singleton<ConfigComponent>
+    public class ConfigComponent: Singleton<ConfigComponent>, ISingletonAwake
     {
         public struct GetAllConfigBytes
         {
@@ -20,6 +20,10 @@ namespace ET
         }
 		
         private readonly ConcurrentDictionary<Type, ISingleton> allConfig = new();
+        
+        public void Awake()
+        {
+        }
 
 		public void ReloadOneConfig(string configName)
 		{
@@ -40,7 +44,6 @@ namespace ET
 			this.allConfig.Remove(configType, out _);
 		}
 		
-		// 程序开始的时候调用,不加锁
 		public void Load()
 		{
 			this.allConfig.Clear();
@@ -53,7 +56,6 @@ namespace ET
 			}
 		}
 		
-		// 程序开始的时候调用,不加锁
 		public async ETTask LoadAsync()
 		{
 			this.allConfig.Clear();

+ 6 - 1
Unity/Assets/Scripts/Core/World/Module/IdGenerater/IdValueGenerater.cs

@@ -1,6 +1,6 @@
 namespace ET
 {
-    public class IdValueGenerater: Singleton<IdValueGenerater>
+    public class IdValueGenerater: Singleton<IdValueGenerater>, ISingletonAwake
     {
         private uint value;
 
@@ -18,5 +18,10 @@
                 }
             }
         }
+
+        public void Awake()
+        {
+            
+        }
     }
 }

+ 5 - 1
Unity/Assets/Scripts/Core/World/Module/Log/Logger.cs

@@ -3,7 +3,7 @@ using System.Diagnostics;
 
 namespace ET
 {
-    public class Logger: Singleton<Logger>
+    public class Logger: Singleton<Logger>, ISingletonAwake
     {
         private ILog iLog;
 
@@ -19,6 +19,10 @@ namespace ET
         private const int DebugLevel = 2;
         private const int InfoLevel = 3;
         private const int WarningLevel = 4;
+        
+        public void Awake()
+        {
+        }
 
         private bool CheckLogLevel(int level)
         {

+ 1 - 1
Unity/Assets/Scripts/Core/World/Module/Scheduler/MainThreadScheduler.cs

@@ -4,7 +4,7 @@ namespace ET
 {
     public partial class VProcessManager: Singleton<VProcessManager>
     {
-        public class MainThreadScheduler: Singleton<MainThreadScheduler>, IScheduler
+        public class MainThreadScheduler: Singleton<MainThreadScheduler>, IScheduler, ISingletonAwake
         {
             private readonly Queue<int> idQueue = new();
             private readonly Queue<int> addIds = new();

+ 11 - 2
Unity/Assets/Scripts/Core/World/Module/VProcess/VProcessManager.cs

@@ -2,11 +2,15 @@
 
 namespace ET
 {
-    public partial class VProcessManager: Singleton<VProcessManager>
+    public partial class VProcessManager: Singleton<VProcessManager>, ISingletonAwake
     {
         private int idGenerator = int.MaxValue;
         private readonly Dictionary<int, VProcess> vProcesses = new();
         
+        public void Awake()
+        {
+        }
+        
         public int Create(int processId = 0)
         {
             lock (this)
@@ -16,8 +20,13 @@ namespace ET
                     processId = --this.idGenerator;
                 }
                 VProcess vProcess = new(Options.Instance.Process, processId);
-                vProcess.AddSingleton<Root>();
+                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;
             }

+ 20 - 45
Unity/Assets/Scripts/Core/World/World.cs

@@ -15,69 +15,44 @@ namespace ET
 
         private readonly ConcurrentDictionary<Type, ISingleton> singletons = new();
 
-        public T AddSingleton<T>() where T : Singleton<T>, new()
+        public T AddSingleton<T>() where T : Singleton<T>, ISingletonAwake, new()
         {
-            ISingleton singleton = new T();
-            
-            if (singleton is ISingletonAwake awake)
-            {
-                awake.Awake();
-            }
+            T singleton = new();
+            singleton.Awake();
 
-            singletons[singleton.GetType()] = singleton;
-            singleton.Register();
-            return (T)singleton;
+            AddSingleton(singleton);
+            return singleton;
         }
         
-        public T AddSingleton<T, A>(A a) where T : Singleton<T>, new()
+        public T AddSingleton<T, A>(A a) where T : Singleton<T>, ISingletonAwake<A>, new()
         {
-            ISingleton singleton = new T();
-            
-            if (singleton is ISingletonAwake<A> awake)
-            {
-                awake.Awake(a);
-            }
+            T singleton = new();
+            singleton.Awake(a);
 
-            singletons[singleton.GetType()] = singleton;
-            singleton.Register();
-            return (T)singleton;
+            AddSingleton(singleton);
+            return singleton;
         }
         
-        public T AddSingleton<T, A, B>(A a, B b) where T : Singleton<T>, new()
+        public T AddSingleton<T, A, B>(A a, B b) where T : Singleton<T>, ISingletonAwake<A, B>, new()
         {
-            ISingleton singleton = new T();
-            
-            if (singleton is ISingletonAwake<A, B> awake)
-            {
-                awake.Awake(a, b);
-            }
+            T singleton = new();
+            singleton.Awake(a, b);
 
-            singletons[singleton.GetType()] = singleton;
-            singleton.Register();
-            return (T)singleton;
+            AddSingleton(singleton);
+            return singleton;
         }
         
-        public T AddSingleton<T, A, B, C>(A a, B b, C c) where T : Singleton<T>, new()
+        public T AddSingleton<T, A, B, C>(A a, B b, C c) where T : Singleton<T>, ISingletonAwake<A, B, C>, new()
         {
-            ISingleton singleton = new T();
-            
-            if (singleton is ISingletonAwake<A, B, C> awake)
-            {
-                awake.Awake(a, b, c);
-            }
+            T singleton = new();
+            singleton.Awake(a, b, c);
 
-            singletons[singleton.GetType()] = singleton;
-            singleton.Register();
-            return (T)singleton;
+            AddSingleton(singleton);
+            return singleton;
         }
 
         public void AddSingleton(ISingleton singleton)
         {
-            if (singleton is ISingletonAwake awake)
-            {
-                awake.Awake();
-            }
-            
             singletons[singleton.GetType()] = singleton;
             singleton.Register();
         }

+ 5 - 1
Unity/Assets/Scripts/Loader/CodeLoader.cs

@@ -7,9 +7,13 @@ using UnityEngine;
 
 namespace ET
 {
-	public class CodeLoader: Singleton<CodeLoader>
+	public class CodeLoader: Singleton<CodeLoader>, ISingletonAwake
 	{
 		private Assembly assembly;
+		
+		public void Awake()
+		{
+		}
 
 		public void Start()
 		{

+ 1 - 1
Unity/Assets/Scripts/Loader/MonoBehaviour/Init.cs

@@ -36,7 +36,7 @@ namespace ET
 			mainThreadScheduler.Add(vProcessId);
 			
 			// 发送消息
-			WorldActor.Instance.Send(new ActorId(Options.Instance.Process, vProcessId, 0), null);
+			WorldActor.Instance.Send(new ActorId(Options.Instance.Process, vProcessId, 1), null);
 		}
 
 		private void Update()