Procházet zdrojové kódy

修改配置文件加载因为异步而顺序乱套的问题

大爷 před 2 roky
rodič
revize
98c7c04fd2

+ 16 - 0
Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/EntryLoadCfg_Server.cs

@@ -0,0 +1,16 @@
+using ET.EventType;
+
+namespace ET
+{
+    [Event(SceneType.Process)]
+    public class EntryLoadCfg_Server : AEvent<ET.EventType.EntryLoadCfg>
+    {
+        protected override async ETTask Run(Scene scene, EntryLoadCfg a)
+        {
+#if UNITY_EDITOR
+            return;
+#endif
+            await Game.AddSingleton<ConfigComponent>().LoadAsync();
+        }
+    }
+}

+ 1 - 1
Unity/Assets/Scripts/Codes/Model/Client/Config/ConfigLoader.cs.meta → Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/EntryLoadCfg_Server.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 10190f390e3576c41ac78a2ab042ddce
+guid: 7e82c893e680a6c4eb7a1cb1bc1f953d
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 1 - 2
Unity/Assets/Scripts/Codes/Hotfix/Unity.Hotfix.Codes.asmdef

@@ -6,8 +6,7 @@
         "Unity.Core",
         "Unity.Model.Codes",
         "Unity.Mathematics",
-        "Unity.Loader",
-        "FairyGUI.Runtime"
+        "Unity.Loader"
     ],
     "includePlatforms": [],
     "excludePlatforms": [],

+ 52 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/EntryLoadCfg_Client.cs

@@ -0,0 +1,52 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using System;
+using YooAsset;
+using ET.EventType;
+using System.Threading.Tasks;
+
+namespace ET
+{
+    [Event(SceneType.Process)]
+    public class EntryLoadCfg_Client : AEvent<ET.EventType.EntryLoadCfg>
+    {
+        protected override async ETTask Run(Scene scene, EntryLoadCfg args)
+        { 
+            Dictionary<Type, byte[]> configBytes = new Dictionary<Type, byte[]>();
+
+            HashSet<Type> configTypes = EventSystem.Instance.GetTypes(typeof(ConfigAttribute));
+            foreach (Type configType in configTypes)
+            {
+                var op = await YooAssetProxy.GetRawFileAsync(configType.Name);
+                var cfg = op.GetRawBytes();
+                if (cfg != null)
+                {
+                    configBytes[configType] = cfg;
+                    Log.Debug($"load config {configType} ok.");
+                }
+                else
+                {
+                    Log.Error($"load config {configType} failed.");
+                }
+            }
+
+            var 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());
+            Log.Debug("register all config ok");
+        }
+
+        private void LoadOneInThread(Type configType, byte[] oneConfigBytes)
+        {
+            object category = SerializeHelper.Deserialize(configType, oneConfigBytes, 0, oneConfigBytes.Length);
+            (category as ISingleton).Register();
+            Log.Debug($"Deserialize: {configType} ok");
+        }
+    }
+}

+ 11 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/EntryLoadCfg_Client.cs.meta

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

+ 0 - 8
Unity/Assets/Scripts/Codes/Model/Client/Config.meta

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

+ 0 - 36
Unity/Assets/Scripts/Codes/Model/Client/Config/ConfigLoader.cs

@@ -1,36 +0,0 @@
-using System;
-using System.Collections.Generic;
-using YooAsset;
-
-namespace ET.Client
-{
-    [Invoke]
-    public class GetAllConfigBytes: AInvokeHandler<ConfigComponent.GetAllConfigBytes, Dictionary<Type, byte[]>>
-    {
-        public override Dictionary<Type, byte[]> Handle(ConfigComponent.GetAllConfigBytes args)
-        {
-            Dictionary<Type, byte[]> output = new Dictionary<Type, byte[]>();
-            HashSet<Type> configTypes = EventSystem.Instance.GetTypes(typeof (ConfigAttribute));
-
-            foreach (Type configType in configTypes)
-            {
-                var op = YooAssets.GetRawFileAsync(configType.Name);
-                output[configType] = op.GetRawBytes();
-                Log.Debug($"load config {configType} ok.");
-            }
-
-            return output;
-        }
-    }
-    
-    [Invoke]
-    public class GetOneConfigBytes: AInvokeHandler<ConfigComponent.GetOneConfigBytes, byte[]>
-    {
-        public override byte[] Handle(ConfigComponent.GetOneConfigBytes args)
-        {
-            //TextAsset v = ResourcesComponent.Instance.GetAsset("config.unity3d", configName) as TextAsset;
-            //return v.bytes;
-            throw new NotImplementedException("client cant use LoadOneConfig");
-        }
-    }
-}

+ 6 - 14
Unity/Assets/Scripts/Codes/Model/Share/Entry.cs

@@ -2,24 +2,16 @@
 {
     namespace EventType
     {
-        public struct EntryEvent1
-        {
-        }   
-        
-        public struct EntryEvent2
-        {
-        } 
-        
-        public struct EntryEvent3
-        {
-        } 
+        public struct EntryLoadCfg { }
+        public struct EntryEvent1 { }   
+        public struct EntryEvent2 { } 
+        public struct EntryEvent3 { } 
     }
     
     public static class Entry
     {
         public static void Init()
         {
-            
         }
         
         public static void Start()
@@ -29,6 +21,7 @@
         
         private static async ETTask StartAsync()
         {
+            Log.Debug("Entry start");
             WinPeriod.Init();
             
             MongoHelper.Init();
@@ -36,8 +29,7 @@
             
             Game.AddSingleton<NetServices>();
             Game.AddSingleton<Root>();
-            await Game.AddSingleton<ConfigComponent>().LoadAsync();
-
+            await EventSystem.Instance.PublishAsync(Root.Instance.Scene, new EventType.EntryLoadCfg());
             await EventSystem.Instance.PublishAsync(Root.Instance.Scene, new EventType.EntryEvent1());
             await EventSystem.Instance.PublishAsync(Root.Instance.Scene, new EventType.EntryEvent2());
             await EventSystem.Instance.PublishAsync(Root.Instance.Scene, new EventType.EntryEvent3());