Răsfoiți Sursa

fix 'ConfigLoader' potential crash on Android (#573)

* fix 'ConfigLoader' crash on Android

相关链接:https://et-framework.cn/d/2254-proto

其他:删除allConfig字段

* static
EP-Toushirou 1 an în urmă
părinte
comite
b7573acb85
1 a modificat fișierele cu 39 adăugiri și 45 ștergeri
  1. 39 45
      Unity/Assets/Scripts/Core/World/Module/Config/ConfigLoader.cs

+ 39 - 45
Unity/Assets/Scripts/Core/World/Module/Config/ConfigLoader.cs

@@ -1,70 +1,64 @@
 using System;
-using System.Collections.Concurrent;
 using System.Collections.Generic;
+#if DOTNET || UNITY_STANDALONE
 using System.Threading.Tasks;
+#endif
 
 namespace ET
 {
-	/// <summary>
+    /// <summary>
     /// ConfigLoader会扫描所有的有ConfigAttribute标签的配置,加载进来
     /// </summary>
-    public class ConfigLoader: Singleton<ConfigLoader>, ISingletonAwake
+    public class ConfigLoader : Singleton<ConfigLoader>, ISingletonAwake
     {
         public struct GetAllConfigBytes
         {
         }
-        
+
         public struct GetOneConfigBytes
         {
             public string ConfigName;
         }
-		
-        private readonly ConcurrentDictionary<Type, ASingleton> allConfig = new();
-        
+
         public void Awake()
         {
         }
 
-		public async ETTask Reload(Type configType)
-		{
-			byte[] oneConfigBytes =
-					await EventSystem.Instance.Invoke<GetOneConfigBytes, ETTask<byte[]>>(new GetOneConfigBytes() { ConfigName = configType.Name });
+        public async ETTask Reload(Type configType)
+        {
+            GetOneConfigBytes getOneConfigBytes = new() { ConfigName = configType.Name };
+            byte[] oneConfigBytes = await EventSystem.Instance.Invoke<GetOneConfigBytes, ETTask<byte[]>>(getOneConfigBytes);
+            LoadOneConfig(configType, oneConfigBytes);
+        }
+
+        public async ETTask LoadAsync()
+        {
+            Dictionary<Type, byte[]> configBytes = await EventSystem.Instance.Invoke<GetAllConfigBytes, ETTask<Dictionary<Type, byte[]>>>(new GetAllConfigBytes());
+
+#if DOTNET || UNITY_STANDALONE
+            using ListComponent<Task> listTasks = ListComponent<Task>.Create();
 
-			object category = MongoHelper.Deserialize(configType, oneConfigBytes, 0, oneConfigBytes.Length);
-			ASingleton singleton = category as ASingleton;
-			this.allConfig[configType] = singleton;
-			
-			World.Instance.AddSingleton(singleton);
-		}
-		
-		public async ETTask LoadAsync()
-		{
-			this.allConfig.Clear();
-			Dictionary<Type, byte[]> configBytes = await EventSystem.Instance.Invoke<GetAllConfigBytes, ETTask<Dictionary<Type, byte[]>>>(new GetAllConfigBytes());
+            foreach (Type type in configBytes.Keys)
+            {
+                byte[] oneConfigBytes = configBytes[type];
+                Task task = Task.Run(() => LoadOneConfig(type, oneConfigBytes));
+                listTasks.Add(task);
+            }
 
-			using ListComponent<Task> listTasks = ListComponent<Task>.Create();
-			
-			foreach (Type type in configBytes.Keys)
-			{
-				byte[] oneConfigBytes = configBytes[type];
-				Task task = Task.Run(() => LoadOneInThread(type, oneConfigBytes));
-				listTasks.Add(task);
-			}
+            await Task.WhenAll(listTasks.ToArray());
+#else
+            foreach (Type type in configBytes.Keys)
+            {
+                LoadOneConfig(type, configBytes[type]);
+            }
+#endif
+        }
 
-			await Task.WhenAll(listTasks.ToArray());
-		}
-		
-		private void LoadOneInThread(Type configType, byte[] oneConfigBytes)
-		{
-			object category = MongoHelper.Deserialize(configType, oneConfigBytes, 0, oneConfigBytes.Length);
-			
-			lock (this)
-			{
-				ASingleton singleton = category as ASingleton;
-				this.allConfig[configType] = singleton;
-				
-				World.Instance.AddSingleton(singleton);
-			}
-		}
+        private static void LoadOneConfig(Type configType, byte[] oneConfigBytes)
+        {
+            object category = MongoHelper.Deserialize(configType, oneConfigBytes, 0, oneConfigBytes.Length);
+            ASingleton singleton = category as ASingleton;
+            World.Instance.AddSingleton(singleton);
+        }
     }
 }