ソースを参照

配置文件线程安全

tanghai 2 年 前
コミット
17659e3323

+ 26 - 16
Unity/Assets/Scripts/Core/Game/Module/Config/ConfigComponent.cs

@@ -18,34 +18,43 @@ namespace ET
             public string ConfigName;
         }
 		
-        private readonly Dictionary<Type, ISingleton> allConfig = new();
+        private readonly Dictionary<Type, object> allConfig = new();
 
 		public override void Dispose()
 		{
-			foreach (var kv in this.allConfig)
+		}
+
+		public T GetOneConfig<T>() where T: class
+		{
+			Type configType = typeof (T);
+			lock (this)
 			{
-				kv.Value.Destroy();
+				if (this.allConfig.TryGetValue(configType, out object oneConfig))
+				{
+					return oneConfig as T;
+				}
+				
+				byte[] oneConfigBytes =
+						EventSystem.Instance.Invoke<GetOneConfigBytes, byte[]>(new GetOneConfigBytes() { ConfigName = configType.FullName });
+
+				object category = MongoHelper.Deserialize(configType, oneConfigBytes, 0, oneConfigBytes.Length);
+				ISingleton singleton = category as ISingleton;
+				singleton.Register();
+
+				this.allConfig[configType] = singleton;
+				return category as T;
 			}
 		}
 
-		public object LoadOneConfig(Type configType)
+		public void RemoveOneConfig(Type configType)
 		{
-			this.allConfig.TryGetValue(configType, out ISingleton oneConfig);
-			if (oneConfig != null)
+			lock (this)
 			{
-				oneConfig.Destroy();
+				this.allConfig.Remove(configType);
 			}
-			
-			byte[] oneConfigBytes = EventSystem.Instance.Invoke<GetOneConfigBytes, byte[]>(new GetOneConfigBytes() {ConfigName = configType.FullName});
-
-			object category = MongoHelper.Deserialize(configType, oneConfigBytes, 0, oneConfigBytes.Length);
-			ISingleton singleton = category as ISingleton;
-			singleton.Register();
-			
-			this.allConfig[configType] = singleton;
-			return category;
 		}
 		
+		// 程序开始的时候调用,不加锁
 		public void Load()
 		{
 			this.allConfig.Clear();
@@ -58,6 +67,7 @@ namespace ET
 			}
 		}
 		
+		// 程序开始的时候调用,不加锁
 		public async ETTask LoadAsync()
 		{
 			this.allConfig.Clear();

+ 2 - 30
Unity/Assets/Scripts/Core/Game/Module/Config/ConfigSingleton.cs

@@ -2,42 +2,14 @@
 
 namespace ET
 {
-    public abstract class ConfigSingleton<T>: ProtoObject, ISingleton where T: ConfigSingleton<T>, new()
+    public abstract class ConfigSingleton<T>: ProtoObject where T: ConfigSingleton<T>, new()
     {
-        [StaticField]
-        private static T instance;
-
         public static T Instance
         {
             get
             {
-                return instance ??= ConfigComponent.Instance.LoadOneConfig(typeof (T)) as T;
-            }
-        }
-
-        void ISingleton.Register()
-        {
-            if (instance != null)
-            {
-                throw new Exception($"singleton register twice! {typeof (T).Name}");
+                return ConfigComponent.Instance.GetOneConfig<T>();
             }
-            instance = (T)this;
-        }
-
-        void ISingleton.Destroy()
-        {
-            T t = instance;
-            instance = null;
-            t.Dispose();
-        }
-
-        bool ISingleton.IsDisposed()
-        {
-            throw new NotImplementedException();
-        }
-
-        public virtual void Dispose()
-        {
         }
     }
 }

+ 1 - 3
Unity/Assets/Scripts/Core/Game/Module/EventSystem/EventSystem.cs

@@ -1,7 +1,5 @@
 using System;
 using System.Collections.Generic;
-using System.Linq;
-using System.Text;
 
 namespace ET
 {
@@ -109,7 +107,7 @@ namespace ET
                 }
             }
             
-            Process.Load();
+            Game.Instance.Load();
         }
 
         public HashSet<Type> GetTypes(Type systemAttributeType)

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

@@ -23,7 +23,7 @@ namespace ET
                         Log.Console($"reload config but not find {category}");
                         return;
                     }
-                    ConfigComponent.Instance.LoadOneConfig(type);
+                    ConfigComponent.Instance.RemoveOneConfig(type);
                     Log.Console($"reload config {configName} finish!");
                     break;
             }