From aa0d40c7589aebefb4228759e62d8f0c151ed0c8 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Tue, 15 Aug 2023 12:07:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9luban=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=EF=BC=8C=E6=94=AF=E6=8C=81=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E9=85=8D=E7=BD=AE=E5=AE=9E=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改luban模板配置,支持异步加载配置实例 --- .../GameScripts/HotFix/GameLogic/GameApp.cs | 2 +- .../GameLogic/GameApp_RegisterSystem.cs | 1 + .../HotFix/GameProto/ConfigLoader.cs | 106 +++++++++++------- .../HotFix/GameProto/GameConfig/Tables.cs | 32 +++--- DotNet/Logic/src/Config/ConfigLoader.cs | 29 +++-- DotNet/Logic/src/Config/GameConfig/Tables.cs | 32 +++--- DotNet/Logic/src/Helper/ConfigTableSystem.cs | 7 ++ .../Templates/config/cs_bin/tables.tpl | 24 ++-- 8 files changed, 132 insertions(+), 101 deletions(-) diff --git a/Assets/GameScripts/HotFix/GameLogic/GameApp.cs b/Assets/GameScripts/HotFix/GameLogic/GameApp.cs index 856bf98f..161baa98 100644 --- a/Assets/GameScripts/HotFix/GameLogic/GameApp.cs +++ b/Assets/GameScripts/HotFix/GameLogic/GameApp.cs @@ -35,7 +35,7 @@ public partial class GameApp:Singleton /// private void StartGameLogic() { - + } /// diff --git a/Assets/GameScripts/HotFix/GameLogic/GameApp_RegisterSystem.cs b/Assets/GameScripts/HotFix/GameLogic/GameApp_RegisterSystem.cs index f20814cf..f05b6632 100644 --- a/Assets/GameScripts/HotFix/GameLogic/GameApp_RegisterSystem.cs +++ b/Assets/GameScripts/HotFix/GameLogic/GameApp_RegisterSystem.cs @@ -38,6 +38,7 @@ public partial class GameApp //带生命周期的单例系统。 AddLogicSys(BehaviourSingleSystem.Instance); AddLogicSys(DataCenterSys.Instance); + AddLogicSys(ConfigSystem.Instance); GMBehaviourSystem.Instance.Active(); } diff --git a/Assets/GameScripts/HotFix/GameProto/ConfigLoader.cs b/Assets/GameScripts/HotFix/GameProto/ConfigLoader.cs index 9b3eee0f..b63649c4 100644 --- a/Assets/GameScripts/HotFix/GameProto/ConfigLoader.cs +++ b/Assets/GameScripts/HotFix/GameProto/ConfigLoader.cs @@ -1,8 +1,11 @@ +using System.Collections.Generic; +using System.Threading; using Bright.Serialization; -using System.IO; +using System.Threading.Tasks; +using Cysharp.Threading.Tasks; using GameBase; using GameConfig; -using SimpleJSON; +using TEngine; using UnityEngine; /// @@ -20,57 +23,84 @@ public class ConfigLoader:Singleton { if (!_init) { - _init = true; - Load(); + Log.Error("Config not loaded. You need Take LoadAsync at first."); } return _tables; } } - + + private readonly Dictionary _configs = new Dictionary(); + /// - /// 加载配置 + /// 异步加载配置。 /// - public void Load() + public async Task LoadAsync() { - var tablesCtor = typeof(Tables).GetConstructors()[0]; - var loaderReturnType = tablesCtor.GetParameters()[0].ParameterType.GetGenericArguments()[1]; - - System.Delegate loader = loaderReturnType == typeof(ByteBuf) - ? new System.Func(LoadByteBuf) - : (System.Delegate)new System.Func(LoadJson); - _tables = (Tables)tablesCtor.Invoke(new object[] { loader }); + _tables = new Tables(); + await _tables.LoadAsync(LoadByteBufAsync); + _init = true; } /// - /// 加载Json配置。 - /// - /// FileName - /// JSONNode - private JSONNode LoadJson(string file) - { -#if UNITY_EDITOR - var ret = File.ReadAllText($"{Application.dataPath}/../GenerateDatas/json/{file}.json", System.Text.Encoding.UTF8); -#else - var textAssets = GameModule.Resource.LoadAsset(file); - var ret = textAssets.text; -#endif - return JSON.Parse(ret); - } - - /// - /// 加载二进制配置。 + /// 异步加载二进制配置。 /// /// FileName /// ByteBuf - private ByteBuf LoadByteBuf(string file) + private async Task LoadByteBufAsync(string file) { - byte[] ret = null; -#if UNITY_EDITOR - ret = File.ReadAllBytes($"{Application.dataPath}/../GenerateDatas/bytes/{file}.bytes"); -#else - var textAssets = GameModule.Resource.LoadAsset(file); - ret = textAssets.bytes; +#if false + GameTickWatcher gameTickWatcher = new GameTickWatcher(); +#endif + byte[] ret; + var location = file; + if (_configs.TryGetValue(location, out var config)) + { + ret = config.bytes; + } + else + { + var textAssets = await GameModule.Resource.LoadAssetAsync(location,CancellationToken.None); + ret = textAssets.bytes; + RegisterTextAssets(file, textAssets); + } +#if false + Log.Warning($"LoadByteBuf {file} used time {gameTickWatcher.ElapseTime()}"); #endif return new ByteBuf(ret); } + + /// + /// 注册配置资源。 + /// + /// 资源Key。 + /// 资源实例。 + /// 注册成功。 + private bool RegisterTextAssets(string key, TextAsset value) + { + if (string.IsNullOrEmpty(key)) + { + return false; + } + + if (value == null) + { + return false; + } + _configs[key] = value; + return true; + } +} + +public class ConfigSystem : BaseLogicSys +{ + public override bool OnInit() + { + InitConfig().Forget(); + return base.OnInit(); + } + + private async UniTaskVoid InitConfig() + { + await ConfigLoader.Instance.LoadAsync(); + } } \ No newline at end of file diff --git a/Assets/GameScripts/HotFix/GameProto/GameConfig/Tables.cs b/Assets/GameScripts/HotFix/GameProto/GameConfig/Tables.cs index fc20ad45..543da9af 100644 --- a/Assets/GameScripts/HotFix/GameProto/GameConfig/Tables.cs +++ b/Assets/GameScripts/HotFix/GameProto/GameConfig/Tables.cs @@ -6,36 +6,37 @@ // //------------------------------------------------------------------------------ using Bright.Serialization; - +using System.Threading.Tasks; namespace GameConfig -{ -public partial class Tables { - public item.TbItem TbItem {get; } - public Battle.TbSkill TbSkill {get; } - public Battle.TbBuff TbBuff {get; } - public Battle.TbBuffAttr TbBuffAttr {get; } + +public sealed class Tables +{ + public item.TbItem TbItem {get; private set; } + public Battle.TbSkill TbSkill {get; private set; } + public Battle.TbBuff TbBuff {get; private set; } + public Battle.TbBuffAttr TbBuffAttr {get; private set; } - public Tables(System.Func loader) + public Tables() { } + + public async Task LoadAsync(System.Func> loader) { var tables = new System.Collections.Generic.Dictionary(); - TbItem = new item.TbItem(loader("item_tbitem")); + TbItem = new item.TbItem(await loader("item_tbitem")); tables.Add("item.TbItem", TbItem); - TbSkill = new Battle.TbSkill(loader("battle_tbskill")); + TbSkill = new Battle.TbSkill(await loader("battle_tbskill")); tables.Add("Battle.TbSkill", TbSkill); - TbBuff = new Battle.TbBuff(loader("battle_tbbuff")); + TbBuff = new Battle.TbBuff(await loader("battle_tbbuff")); tables.Add("Battle.TbBuff", TbBuff); - TbBuffAttr = new Battle.TbBuffAttr(loader("battle_tbbuffattr")); + TbBuffAttr = new Battle.TbBuffAttr(await loader("battle_tbbuffattr")); tables.Add("Battle.TbBuffAttr", TbBuffAttr); - PostInit(); TbItem.Resolve(tables); TbSkill.Resolve(tables); TbBuff.Resolve(tables); TbBuffAttr.Resolve(tables); - PostResolve(); } public void TranslateText(System.Func translator) @@ -45,9 +46,6 @@ public partial class Tables TbBuff.TranslateText(translator); TbBuffAttr.TranslateText(translator); } - - partial void PostInit(); - partial void PostResolve(); } } \ No newline at end of file diff --git a/DotNet/Logic/src/Config/ConfigLoader.cs b/DotNet/Logic/src/Config/ConfigLoader.cs index cb0f9149..3251260a 100644 --- a/DotNet/Logic/src/Config/ConfigLoader.cs +++ b/DotNet/Logic/src/Config/ConfigLoader.cs @@ -3,6 +3,7 @@ using Bright.Serialization; using GameConfig; using TEngine; using TEngine.Core; +using TEngine.Helper; /// /// 配置加载器。 @@ -13,19 +14,13 @@ public class ConfigLoader:Singleton private Tables _tables = null!; - public ConfigLoader() - { - this.Load(); - } - public Tables Tables { get { if (!_init) { - _init = true; - Load(); + Log.Error("Config not loaded."); } return _tables; } @@ -34,15 +29,13 @@ public class ConfigLoader:Singleton /// /// 加载配置。 /// - public void Load() + public async Task LoadAsync() { try { - var tablesCtor = typeof(Tables).GetConstructors()[0]; - var loaderReturnType = tablesCtor.GetParameters()[0].ParameterType.GetGenericArguments()[1]; - - System.Delegate loader = new System.Func(LoadByteBuf); - _tables = (Tables)tablesCtor.Invoke(new object[] { loader }); + _tables = new Tables(); + await _tables.LoadAsync(LoadByteBuf); + _init = true; } catch (Exception e) { @@ -56,9 +49,15 @@ public class ConfigLoader:Singleton /// /// FileName /// ByteBuf - private ByteBuf LoadByteBuf(string file) + private async Task LoadByteBuf(string file) { - byte[]ret = File.ReadAllBytes($"../../../Config/GameConfig/{file}.bytes"); +#if false + GameTickWatcher gameTickWatcher = new GameTickWatcher(); +#endif + var ret = await File.ReadAllBytesAsync($"../../../Config/GameConfig/{file}.bytes"); +#if false + Log.Warning($"LoadByteBuf {file} used time {gameTickWatcher.ElapseTime()}"); +#endif return new ByteBuf(ret); } } \ No newline at end of file diff --git a/DotNet/Logic/src/Config/GameConfig/Tables.cs b/DotNet/Logic/src/Config/GameConfig/Tables.cs index fc20ad45..543da9af 100644 --- a/DotNet/Logic/src/Config/GameConfig/Tables.cs +++ b/DotNet/Logic/src/Config/GameConfig/Tables.cs @@ -6,36 +6,37 @@ // //------------------------------------------------------------------------------ using Bright.Serialization; - +using System.Threading.Tasks; namespace GameConfig -{ -public partial class Tables { - public item.TbItem TbItem {get; } - public Battle.TbSkill TbSkill {get; } - public Battle.TbBuff TbBuff {get; } - public Battle.TbBuffAttr TbBuffAttr {get; } + +public sealed class Tables +{ + public item.TbItem TbItem {get; private set; } + public Battle.TbSkill TbSkill {get; private set; } + public Battle.TbBuff TbBuff {get; private set; } + public Battle.TbBuffAttr TbBuffAttr {get; private set; } - public Tables(System.Func loader) + public Tables() { } + + public async Task LoadAsync(System.Func> loader) { var tables = new System.Collections.Generic.Dictionary(); - TbItem = new item.TbItem(loader("item_tbitem")); + TbItem = new item.TbItem(await loader("item_tbitem")); tables.Add("item.TbItem", TbItem); - TbSkill = new Battle.TbSkill(loader("battle_tbskill")); + TbSkill = new Battle.TbSkill(await loader("battle_tbskill")); tables.Add("Battle.TbSkill", TbSkill); - TbBuff = new Battle.TbBuff(loader("battle_tbbuff")); + TbBuff = new Battle.TbBuff(await loader("battle_tbbuff")); tables.Add("Battle.TbBuff", TbBuff); - TbBuffAttr = new Battle.TbBuffAttr(loader("battle_tbbuffattr")); + TbBuffAttr = new Battle.TbBuffAttr(await loader("battle_tbbuffattr")); tables.Add("Battle.TbBuffAttr", TbBuffAttr); - PostInit(); TbItem.Resolve(tables); TbSkill.Resolve(tables); TbBuff.Resolve(tables); TbBuffAttr.Resolve(tables); - PostResolve(); } public void TranslateText(System.Func translator) @@ -45,9 +46,6 @@ public partial class Tables TbBuff.TranslateText(translator); TbBuffAttr.TranslateText(translator); } - - partial void PostInit(); - partial void PostResolve(); } } \ No newline at end of file diff --git a/DotNet/Logic/src/Helper/ConfigTableSystem.cs b/DotNet/Logic/src/Helper/ConfigTableSystem.cs index 931933f6..376f20ba 100644 --- a/DotNet/Logic/src/Helper/ConfigTableSystem.cs +++ b/DotNet/Logic/src/Helper/ConfigTableSystem.cs @@ -9,6 +9,8 @@ public static class ConfigTableSystem { public static void Bind() { + LoadConfigAsync(); + // 框架需要一些的配置文件来启动服务器和创建网络服务所以需要ServerConfig.xlsx和MachineConfig.xlsx的配置 // 由于配置表的代码是生成在框架外面的、框架没办法直接获取到配置文件 // 考虑到这两个配置文件开发者可能会修改结构、所以提供了一个委托来让开发者开自己定义如何获取框架需要的东西 @@ -139,5 +141,10 @@ public static class ConfigTableSystem return list; }; } + + public static async Task LoadConfigAsync() + { + await ConfigLoader.Instance.LoadAsync(); + } } #endif \ No newline at end of file diff --git a/Luban/Luban.ClientServer/Templates/config/cs_bin/tables.tpl b/Luban/Luban.ClientServer/Templates/config/cs_bin/tables.tpl index 0ab51ba5..4b638b1a 100644 --- a/Luban/Luban.ClientServer/Templates/config/cs_bin/tables.tpl +++ b/Luban/Luban.ClientServer/Templates/config/cs_bin/tables.tpl @@ -1,14 +1,15 @@ using Bright.Serialization; +using System.Threading.Tasks; {{ name = x.name namespace = x.namespace tables = x.tables - }} - -{{cs_start_name_space_grace x.namespace}} -public partial class {{name}} +namespace {{namespace}} +{ + +public sealed class {{name}} { {{~for table in tables ~}} {{~if table.comment != '' ~}} @@ -16,22 +17,22 @@ public partial class {{name}} /// {{table.escape_comment}} /// {{~end~}} - public {{table.full_name}} {{table.name}} {get; } + public {{table.full_name}} {{table.name}} {get; private set; } {{~end~}} - public {{name}}(System.Func loader) + public {{name}}() { } + + public async Task LoadAsync(System.Func> loader) { var tables = new System.Collections.Generic.Dictionary(); {{~for table in tables ~}} - {{table.name}} = new {{table.full_name}}(loader("{{table.output_data_file}}")); + {{table.name}} = new {{table.full_name}}(await loader("{{table.output_data_file}}")); tables.Add("{{table.full_name}}", {{table.name}}); {{~end~}} - PostInit(); {{~for table in tables ~}} {{table.name}}.Resolve(tables); {{~end~}} - PostResolve(); } public void TranslateText(System.Func translator) @@ -40,9 +41,6 @@ public partial class {{name}} {{table.name}}.TranslateText(translator); {{~end~}} } - - partial void PostInit(); - partial void PostResolve(); } -{{cs_end_name_space_grace x.namespace}} \ No newline at end of file +} \ No newline at end of file