From b09edc2e6da23f3289afa14d923c58d066820fbd Mon Sep 17 00:00:00 2001 From: tanghai Date: Mon, 8 Feb 2021 23:48:11 +0800 Subject: [PATCH] =?UTF-8?q?ConfigComponent=E5=8A=A0=E8=BD=BDprotobuf?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- Client-Server.sln.DotSettings | 8 +++ Server/Hotfix/AppStart_Init.cs | 3 + Server/Hotfix/Helper/LoadConfigHelper.cs | 17 +++++ .../Module/Config/ConfigComponentSystem.cs | 60 ----------------- Server/Hotfix/Server.Hotfix.csproj | 3 + Server/Model/Module/Config/ConfigComponent.cs | 15 ----- Server/Model/Module/Config/ConfigHelper.cs | 27 -------- Server/Model/Server.Model.csproj | 15 +++-- Tools/ExcelExporter/Program.cs | 2 +- .../Module/Config/ConfigComponentSystem.cs | 53 +++++++++++---- Unity/Assets/HotfixView/AppStart_Init.cs | 3 + .../HotfixView/Helper/LoadConfigHelper.cs | 21 ++++++ Unity/Assets/Model/Core/FunctionCallback.cs | 10 +++ Unity/Assets/Model/Module/Config/ACategory.cs | 67 ------------------- .../Model/Module/Config/AConfigComponent.cs | 16 ----- .../Model/Module/Config/ConfigComponent.cs | 2 +- .../Model/Module/Config/ConfigHelper.cs | 26 ------- Unity/Unity.Hotfix.csproj | 2 +- Unity/Unity.HotfixView.csproj | 1 + Unity/Unity.Model.csproj | 4 +- 21 files changed, 121 insertions(+), 236 deletions(-) create mode 100644 Server/Hotfix/Helper/LoadConfigHelper.cs delete mode 100644 Server/Hotfix/Module/Config/ConfigComponentSystem.cs delete mode 100644 Server/Model/Module/Config/ConfigComponent.cs delete mode 100644 Server/Model/Module/Config/ConfigHelper.cs create mode 100644 Unity/Assets/HotfixView/Helper/LoadConfigHelper.cs create mode 100644 Unity/Assets/Model/Core/FunctionCallback.cs delete mode 100644 Unity/Assets/Model/Module/Config/ACategory.cs delete mode 100644 Unity/Assets/Model/Module/Config/AConfigComponent.cs delete mode 100644 Unity/Assets/Model/Module/Config/ConfigHelper.cs diff --git a/.gitignore b/.gitignore index 8175b3c0..0630ab5d 100644 --- a/.gitignore +++ b/.gitignore @@ -49,4 +49,4 @@ Server/.DS_Store .objs/ /Unity/.gradle /*.user -/ToolApp +/ToolsApp/ diff --git a/Client-Server.sln.DotSettings b/Client-Server.sln.DotSettings index e63dbcc1..4ab0dc67 100644 --- a/Client-Server.sln.DotSettings +++ b/Client-Server.sln.DotSettings @@ -377,6 +377,10 @@ II.2.12 <HandlesEvent /> 79 True False + Never + Never + False + Never False @@ -400,6 +404,10 @@ II.2.12 <HandlesEvent /> True True True + True + True + True + True True True True diff --git a/Server/Hotfix/AppStart_Init.cs b/Server/Hotfix/AppStart_Init.cs index 9004f321..04001038 100644 --- a/Server/Hotfix/AppStart_Init.cs +++ b/Server/Hotfix/AppStart_Init.cs @@ -8,7 +8,10 @@ namespace ET { public override async ETTask Run(EventType.AppStart args) { + FunctionCallback.GetAllConfigBytes = LoadConfigHelper.LoadAllConfigBytes; + Game.Scene.AddComponent(); + await ConfigComponent.Instance.LoadAsync(); StartProcessConfig processConfig = StartProcessConfigCategory.Instance.Get(Game.Options.Process); diff --git a/Server/Hotfix/Helper/LoadConfigHelper.cs b/Server/Hotfix/Helper/LoadConfigHelper.cs new file mode 100644 index 00000000..5e696ab6 --- /dev/null +++ b/Server/Hotfix/Helper/LoadConfigHelper.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; +using System.IO; + +namespace ET +{ + public static class LoadConfigHelper + { + public static void LoadAllConfigBytes(Dictionary output) + { + foreach (string file in Directory.GetFiles($"../Generate/Server/Proto", "*.bytes")) + { + string key = $"{Path.GetFileName(file)}.bytes"; + output[key] = File.ReadAllBytes(file); + } + } + } +} \ No newline at end of file diff --git a/Server/Hotfix/Module/Config/ConfigComponentSystem.cs b/Server/Hotfix/Module/Config/ConfigComponentSystem.cs deleted file mode 100644 index ae9362a9..00000000 --- a/Server/Hotfix/Module/Config/ConfigComponentSystem.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System; -using System.Collections.Generic; - - -namespace ET -{ - public class ConfigAwakeSystem : AwakeSystem - { - public override void Awake(ConfigComponent self) - { - ConfigComponent.Instance = self; - self.Awake(); - } - } - - public class ConfigLoadSystem : LoadSystem - { - public override void Load(ConfigComponent self) - { - self.Load(); - } - } - - public class ConfigDestroySystem : DestroySystem - { - public override void Destroy(ConfigComponent self) - { - ConfigComponent.Instance = null; - } - } - - public static class ConfigComponentSystem - { - public static void Awake(this ConfigComponent self) - { - self.Load(); - } - - public static void Load(this ConfigComponent self) - { - self.AllConfig.Clear(); - HashSet types = Game.EventSystem.GetTypes(typeof(ConfigAttribute)); - - foreach (Type type in types) - { - object obj = Activator.CreateInstance(type); - - ACategory iCategory = obj as ACategory; - if (iCategory == null) - { - throw new Exception($"class: {type.Name} not inherit from ACategory"); - } - iCategory.BeginInit(); - iCategory.EndInit(); - - self.AllConfig[iCategory.ConfigType] = iCategory; - } - } - } -} \ No newline at end of file diff --git a/Server/Hotfix/Server.Hotfix.csproj b/Server/Hotfix/Server.Hotfix.csproj index 1d2598e1..fd5116ce 100644 --- a/Server/Hotfix/Server.Hotfix.csproj +++ b/Server/Hotfix/Server.Hotfix.csproj @@ -20,6 +20,9 @@ + + Module\Config\ConfigComponentSystem.cs + Module\Message\MessageDispatcherComponentSystem.cs diff --git a/Server/Model/Module/Config/ConfigComponent.cs b/Server/Model/Module/Config/ConfigComponent.cs deleted file mode 100644 index 41257d62..00000000 --- a/Server/Model/Module/Config/ConfigComponent.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ET -{ - /// - /// Config组件会扫描所有的有ConfigAttribute标签的配置,加载进来 - /// - public class ConfigComponent: Entity - { - public static ConfigComponent Instance; - - public Dictionary AllConfig = new Dictionary(); - } -} \ No newline at end of file diff --git a/Server/Model/Module/Config/ConfigHelper.cs b/Server/Model/Module/Config/ConfigHelper.cs deleted file mode 100644 index 539b147c..00000000 --- a/Server/Model/Module/Config/ConfigHelper.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.IO; - -namespace ET -{ - public static class ConfigHelper - { - public static string GetText(string key) - { - string path = $"../Config/{key}.txt"; - try - { - string configStr = File.ReadAllText(path); - return configStr; - } - catch (Exception e) - { - throw new Exception($"load config file fail, path: {path} {e}"); - } - } - - public static T ToObject(string str) - { - return JsonHelper.FromJson(str); - } - } -} diff --git a/Server/Model/Server.Model.csproj b/Server/Model/Server.Model.csproj index 98614398..a1e67d6a 100644 --- a/Server/Model/Server.Model.csproj +++ b/Server/Model/Server.Model.csproj @@ -29,6 +29,10 @@ Core\%(RecursiveDir)%(FileName)%(Extension) + + + Module\Config\%(RecursiveDir)%(FileName)%(Extension) + @@ -41,9 +45,6 @@ Module\Actor\IActorMessage.cs - - Module\Config\AConfigComponent.cs - Module\CoroutineLock\CoroutineLock.cs @@ -92,11 +93,13 @@ + + + + + - - - diff --git a/Tools/ExcelExporter/Program.cs b/Tools/ExcelExporter/Program.cs index 0ecf23a4..53909597 100644 --- a/Tools/ExcelExporter/Program.cs +++ b/Tools/ExcelExporter/Program.cs @@ -310,7 +310,7 @@ namespace ET string json = File.ReadAllText(Path.Combine(string.Format(jsonDir, configType), $"{protoName}.txt")); object deserialize = BsonSerializer.Deserialize(json, type); - string path = Path.Combine(dir, $"{protoName}.bytes"); + string path = Path.Combine(dir, $"{protoName}Category.bytes"); using FileStream file = File.Create(path); Serializer.Serialize(file, deserialize); diff --git a/Unity/Assets/Hotfix/Module/Config/ConfigComponentSystem.cs b/Unity/Assets/Hotfix/Module/Config/ConfigComponentSystem.cs index 612d7de8..98396084 100644 --- a/Unity/Assets/Hotfix/Module/Config/ConfigComponentSystem.cs +++ b/Unity/Assets/Hotfix/Module/Config/ConfigComponentSystem.cs @@ -1,6 +1,6 @@ using System; using System.Collections.Generic; - +using System.Threading.Tasks; namespace ET { @@ -21,7 +21,6 @@ namespace ET } } - public class ConfigDestroySystem : DestroySystem { public override void Destroy(ConfigComponent self) @@ -40,21 +39,51 @@ namespace ET public static void Load(this ConfigComponent self) { self.AllConfig.Clear(); - HashSet types = Game.EventSystem.GetTypes(typeof(ConfigAttribute)); + HashSet types = Game.EventSystem.GetTypes(typeof (ConfigAttribute)); + + Dictionary configBytes = new Dictionary(); + FunctionCallback.GetAllConfigBytes(configBytes); + + List listTasks = new List(); + + foreach (Type type in types) + { + Task task = Task.Run(() => self.LoadOneInThread(type, configBytes)); + listTasks.Add(task); + } + + Task.WaitAll(listTasks.ToArray()); + } + + + public static async ETTask LoadAsync(this ConfigComponent self) + { + self.AllConfig.Clear(); + HashSet types = Game.EventSystem.GetTypes(typeof (ConfigAttribute)); + + Dictionary configBytes = new Dictionary(); + FunctionCallback.GetAllConfigBytes(configBytes); + + List listTasks = new List(); foreach (Type type in types) { - object obj = Activator.CreateInstance(type); + Task task = Task.Run(() => self.LoadOneInThread(type, configBytes)); + listTasks.Add(task); + } - ACategory iCategory = obj as ACategory; - if (iCategory == null) - { - throw new Exception($"class: {type.Name} not inherit from ACategory"); - } - iCategory.BeginInit(); - iCategory.EndInit(); + await Task.WhenAll(listTasks.ToArray()); + } - self.AllConfig[iCategory.ConfigType] = iCategory; + private static void LoadOneInThread(this ConfigComponent self, Type configType, Dictionary configBytes) + { + byte[] oneConfigBytes = configBytes[configType.Name]; + + object category = ProtobufHelper.FromBytes(configType, oneConfigBytes, 0, oneConfigBytes.Length); + + lock (self) + { + self.AllConfig[configType] = category; } } } diff --git a/Unity/Assets/HotfixView/AppStart_Init.cs b/Unity/Assets/HotfixView/AppStart_Init.cs index 57931d4d..1eefe324 100644 --- a/Unity/Assets/HotfixView/AppStart_Init.cs +++ b/Unity/Assets/HotfixView/AppStart_Init.cs @@ -4,6 +4,8 @@ namespace ET { public override async ETTask Run(EventType.AppStart args) { + FunctionCallback.GetAllConfigBytes = LoadConfigHelper.LoadAllConfigBytes; + Game.Scene.AddComponent(); @@ -16,6 +18,7 @@ namespace ET ResourcesComponent.Instance.LoadBundle("config.unity3d"); Game.Scene.AddComponent(); ResourcesComponent.Instance.UnloadBundle("config.unity3d"); + await ConfigComponent.Instance.LoadAsync(); Game.Scene.AddComponent(); Game.Scene.AddComponent(); diff --git a/Unity/Assets/HotfixView/Helper/LoadConfigHelper.cs b/Unity/Assets/HotfixView/Helper/LoadConfigHelper.cs new file mode 100644 index 00000000..73827792 --- /dev/null +++ b/Unity/Assets/HotfixView/Helper/LoadConfigHelper.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; +using System.IO; +using UnityEngine; + +namespace ET +{ + public static class LoadConfigHelper + { + public static void LoadAllConfigBytes(Dictionary output) + { + Dictionary keys = ResourcesComponent.Instance.GetBundleAll("config.unity3d"); + + foreach (var kv in keys) + { + TextAsset v = kv.Value as TextAsset; + string key = $"{kv.Key}.bytes"; + output[key] = v.bytes; + } + } + } +} \ No newline at end of file diff --git a/Unity/Assets/Model/Core/FunctionCallback.cs b/Unity/Assets/Model/Core/FunctionCallback.cs new file mode 100644 index 00000000..3742e80d --- /dev/null +++ b/Unity/Assets/Model/Core/FunctionCallback.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; + +namespace ET +{ + public static class FunctionCallback + { + public static Action> GetAllConfigBytes; + } +} \ No newline at end of file diff --git a/Unity/Assets/Model/Module/Config/ACategory.cs b/Unity/Assets/Model/Module/Config/ACategory.cs deleted file mode 100644 index 31272c9a..00000000 --- a/Unity/Assets/Model/Module/Config/ACategory.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; - -namespace ET -{ - public abstract class ACategory: ProtoObject - { - public abstract Type ConfigType { get; } - } - - /// - /// 管理该所有的配置 - /// - /// - public abstract class ACategory : ACategory where T : IConfig - { - protected Dictionary dict; - - public override void BeginInit() - { - string configStr = ConfigHelper.GetText(typeof(T).Name); - - try - { - this.dict = ConfigHelper.ToObject>(configStr); - } - catch (Exception e) - { - throw new Exception($"parser json fail: {configStr}", e); - } - } - - public override Type ConfigType - { - get - { - return typeof(T); - } - } - - public override void EndInit() - { - } - - public T Get(int id) - { - T t; - if (!this.dict.TryGetValue(id, out t)) - { - throw new Exception($"not found config: {typeof(T)} id: {id}"); - } - return t; - } - - public Dictionary GetAll() - { - return this.dict; - } - - public T GetOne() - { - return this.dict.Values.First(); - } - } -} \ No newline at end of file diff --git a/Unity/Assets/Model/Module/Config/AConfigComponent.cs b/Unity/Assets/Model/Module/Config/AConfigComponent.cs deleted file mode 100644 index 86804981..00000000 --- a/Unity/Assets/Model/Module/Config/AConfigComponent.cs +++ /dev/null @@ -1,16 +0,0 @@ -#if !SERVER -using UnityEngine; -#endif - -namespace ET -{ - /// - /// 每个Config的基类 - /// -#if !SERVER - [HideInHierarchy] -#endif - public abstract class AConfigComponent: Entity, ISerializeToEntity - { - } -} \ No newline at end of file diff --git a/Unity/Assets/Model/Module/Config/ConfigComponent.cs b/Unity/Assets/Model/Module/Config/ConfigComponent.cs index 459388ce..7891147a 100644 --- a/Unity/Assets/Model/Module/Config/ConfigComponent.cs +++ b/Unity/Assets/Model/Module/Config/ConfigComponent.cs @@ -10,6 +10,6 @@ namespace ET { public static ConfigComponent Instance; - public Dictionary AllConfig = new Dictionary(); + public Dictionary AllConfig = new Dictionary(); } } \ No newline at end of file diff --git a/Unity/Assets/Model/Module/Config/ConfigHelper.cs b/Unity/Assets/Model/Module/Config/ConfigHelper.cs deleted file mode 100644 index 42a405a8..00000000 --- a/Unity/Assets/Model/Module/Config/ConfigHelper.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using UnityEngine; - -namespace ET -{ - public static class ConfigHelper - { - public static string GetText(string key) - { - try - { - string configStr = ((TextAsset)Game.Scene.GetComponent().GetAsset("config.unity3d", key)).text; - return configStr; - } - catch (Exception e) - { - throw new Exception($"load config file fail, key: {key}", e); - } - } - - public static T ToObject(string str) - { - return JsonHelper.FromJson(str); - } - } -} \ No newline at end of file diff --git a/Unity/Unity.Hotfix.csproj b/Unity/Unity.Hotfix.csproj index 371a6d45..df20026f 100644 --- a/Unity/Unity.Hotfix.csproj +++ b/Unity/Unity.Hotfix.csproj @@ -52,11 +52,11 @@ false + - diff --git a/Unity/Unity.HotfixView.csproj b/Unity/Unity.HotfixView.csproj index a82d6003..5bbbc160 100644 --- a/Unity/Unity.HotfixView.csproj +++ b/Unity/Unity.HotfixView.csproj @@ -52,6 +52,7 @@ false + diff --git a/Unity/Unity.Model.csproj b/Unity/Unity.Model.csproj index f06ffbd9..694f2b25 100644 --- a/Unity/Unity.Model.csproj +++ b/Unity/Unity.Model.csproj @@ -52,6 +52,7 @@ false + @@ -91,7 +92,6 @@ - @@ -165,12 +165,10 @@ - - -- GitLab