diff --git a/Assets/GameScripts/HotFix/GameProto/ConfigLoader.cs b/Assets/GameScripts/HotFix/GameProto/ConfigLoader.cs index b63649c4..23218d9f 100644 --- a/Assets/GameScripts/HotFix/GameProto/ConfigLoader.cs +++ b/Assets/GameScripts/HotFix/GameProto/ConfigLoader.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using System.Threading; using Bright.Serialization; -using System.Threading.Tasks; using Cysharp.Threading.Tasks; using GameBase; using GameConfig; @@ -34,7 +33,7 @@ public class ConfigLoader:Singleton /// /// 异步加载配置。 /// - public async Task LoadAsync() + public async UniTask LoadAsync() { _tables = new Tables(); await _tables.LoadAsync(LoadByteBufAsync); @@ -46,7 +45,7 @@ public class ConfigLoader:Singleton /// /// FileName /// ByteBuf - private async Task LoadByteBufAsync(string file) + private async UniTask LoadByteBufAsync(string file) { #if false GameTickWatcher gameTickWatcher = new GameTickWatcher(); diff --git a/Assets/GameScripts/HotFix/GameProto/GameConfig/Tables.cs b/Assets/GameScripts/HotFix/GameProto/GameConfig/Tables.cs index 543da9af..1c6bfd48 100644 --- a/Assets/GameScripts/HotFix/GameProto/GameConfig/Tables.cs +++ b/Assets/GameScripts/HotFix/GameProto/GameConfig/Tables.cs @@ -6,7 +6,8 @@ // //------------------------------------------------------------------------------ using Bright.Serialization; -using System.Threading.Tasks; +using Cysharp.Threading.Tasks; +using System.Collections.Generic; namespace GameConfig @@ -21,17 +22,32 @@ public sealed class Tables public Tables() { } - public async Task LoadAsync(System.Func> loader) + public async UniTask LoadAsync(System.Func> loader) { var tables = new System.Collections.Generic.Dictionary(); - TbItem = new item.TbItem(await loader("item_tbitem")); - tables.Add("item.TbItem", TbItem); - TbSkill = new Battle.TbSkill(await loader("battle_tbskill")); - tables.Add("Battle.TbSkill", TbSkill); - TbBuff = new Battle.TbBuff(await loader("battle_tbbuff")); - tables.Add("Battle.TbBuff", TbBuff); - TbBuffAttr = new Battle.TbBuffAttr(await loader("battle_tbbuffattr")); - tables.Add("Battle.TbBuffAttr", TbBuffAttr); + List list = new List(); + list.Add(UniTask.Create(async () => + { + TbItem = new item.TbItem(await loader("item_tbitem")); + tables.Add("item.TbItem", TbItem); + })); + list.Add(UniTask.Create(async () => + { + TbSkill = new Battle.TbSkill(await loader("battle_tbskill")); + tables.Add("Battle.TbSkill", TbSkill); + })); + list.Add(UniTask.Create(async () => + { + TbBuff = new Battle.TbBuff(await loader("battle_tbbuff")); + tables.Add("Battle.TbBuff", TbBuff); + })); + list.Add(UniTask.Create(async () => + { + TbBuffAttr = new Battle.TbBuffAttr(await loader("battle_tbbuffattr")); + tables.Add("Battle.TbBuffAttr", TbBuffAttr); + })); + + await UniTask.WhenAll(list); TbItem.Resolve(tables); TbSkill.Resolve(tables); diff --git a/Luban/CustomTemplate/config/cs_bin/bean.tpl b/Luban/CustomTemplate/config/cs_bin/bean.tpl new file mode 100644 index 00000000..7ce5006e --- /dev/null +++ b/Luban/CustomTemplate/config/cs_bin/bean.tpl @@ -0,0 +1,115 @@ +using Bright.Serialization; +using System.Collections.Generic; +{{ + name = x.name + parent_def_type = x.parent_def_type + export_fields = x.export_fields + hierarchy_export_fields = x.hierarchy_export_fields +}} + +{{cs_start_name_space_grace x.namespace_with_top_module}} +{{~if x.comment != '' ~}} +/// +/// {{x.escape_comment}} +/// +{{~end~}} +public {{x.cs_class_modifier}} partial class {{name}} : {{if parent_def_type}} {{x.parent}} {{else}} Bright.Config.BeanBase {{end}} +{ + public {{name}}(ByteBuf _buf) {{if parent_def_type}} : base(_buf) {{end}} + { + {{~ for field in export_fields ~}} + {{cs_deserialize '_buf' field.convention_name field.ctype}} + {{~if field.index_field~}} + foreach(var _v in {{field.convention_name}}) + { + {{field.convention_name}}_Index.Add(_v.{{field.index_field.convention_name}}, _v); + } + {{~end~}} + {{~end~}} + PostInit(); + } + + public static {{name}} Deserialize{{name}}(ByteBuf _buf) + { + {{~if x.is_abstract_type~}} + switch (_buf.ReadInt()) + { + {{~for child in x.hierarchy_not_abstract_children~}} + case {{child.full_name}}.__ID__: return new {{child.full_name}}(_buf); + {{~end~}} + default: throw new SerializationException(); + } + {{~else~}} + return new {{x.full_name}}(_buf); + {{~end~}} + } + + {{~ for field in export_fields ~}} +{{~if field.comment != '' ~}} + /// + /// {{field.escape_comment}} + /// +{{~end~}} + public {{cs_define_type field.ctype}} {{field.convention_name}} { get; private set; } + {{~if field.index_field~}} + public readonly Dictionary<{{cs_define_type field.index_field.ctype}}, {{cs_define_type field.ctype.element_type}}> {{field.convention_name}}_Index = new Dictionary<{{cs_define_type field.index_field.ctype}}, {{cs_define_type field.ctype.element_type}}>(); + {{~end~}} + {{~if field.gen_ref~}} + public {{field.cs_ref_validator_define}} + {{~end~}} + {{~if (gen_datetime_mills field.ctype) ~}} + public long {{field.convention_name}}_Millis => {{field.convention_name}} * 1000L; + {{~end~}} + {{~if field.gen_text_key~}} + public {{cs_define_text_key_field field}} { get; } + {{~end~}} + {{~end~}} + +{{~if !x.is_abstract_type~}} + public const int __ID__ = {{x.id}}; + public override int GetTypeId() => __ID__; +{{~end~}} + + public {{x.cs_method_modifier}} void Resolve(Dictionary _tables) + { + {{~if parent_def_type~}} + base.Resolve(_tables); + {{~end~}} + {{~ for field in export_fields ~}} + {{~if field.gen_ref~}} + {{cs_ref_validator_resolve field}} + {{~else if field.has_recursive_ref~}} + {{cs_recursive_resolve field '_tables'}} + {{~end~}} + {{~end~}} + PostResolve(); + } + + public {{x.cs_method_modifier}} void TranslateText(System.Func translator) + { + {{~if parent_def_type~}} + base.TranslateText(translator); + {{~end~}} + {{~ for field in export_fields ~}} + {{~if field.gen_text_key~}} + {{cs_translate_text field 'translator'}} + {{~else if field.has_recursive_text~}} + {{cs_recursive_translate_text field 'translator'}} + {{~end~}} + {{~end~}} + } + + public override string ToString() + { + return "{{full_name}}{ " + {{~for field in hierarchy_export_fields ~}} + + "{{field.convention_name}}:" + {{cs_to_string field.convention_name field.ctype}} + "," + {{~end~}} + + "}"; + } + + partial void PostInit(); + partial void PostResolve(); +} + +{{cs_end_name_space_grace x.namespace_with_top_module}} \ No newline at end of file diff --git a/Luban/CustomTemplate/config/cs_bin/table.tpl b/Luban/CustomTemplate/config/cs_bin/table.tpl new file mode 100644 index 00000000..516c1764 --- /dev/null +++ b/Luban/CustomTemplate/config/cs_bin/table.tpl @@ -0,0 +1,173 @@ +using Bright.Serialization; +using System.Collections.Generic; + + +{{cs_start_name_space_grace x.namespace_with_top_module}} + {{ + name = x.name + key_type = x.key_ttype + key_type1 = x.key_ttype1 + key_type2 = x.key_ttype2 + value_type = x.value_ttype + }} +{{~if x.comment != '' ~}} +/// +/// {{x.escape_comment}} +/// +{{~end~}} +public partial class {{name}} +{ + {{~if x.is_map_table ~}} + private readonly Dictionary<{{cs_define_type key_type}}, {{cs_define_type value_type}}> _dataMap; + private readonly List<{{cs_define_type value_type}}> _dataList; + + public {{name}}(ByteBuf _buf) + { + _dataMap = new Dictionary<{{cs_define_type key_type}}, {{cs_define_type value_type}}>(); + _dataList = new List<{{cs_define_type value_type}}>(); + + for(int n = _buf.ReadSize() ; n > 0 ; --n) + { + {{cs_define_type value_type}} _v; + {{cs_deserialize '_buf' '_v' value_type}} + _dataList.Add(_v); + _dataMap.Add(_v.{{x.index_field.convention_name}}, _v); + } + PostInit(); + } + + public Dictionary<{{cs_define_type key_type}}, {{cs_define_type value_type}}> DataMap => _dataMap; + public List<{{cs_define_type value_type}}> DataList => _dataList; + +{{~if value_type.is_dynamic~}} + public T GetOrDefaultAs({{cs_define_type key_type}} key) where T : {{cs_define_type value_type}} => _dataMap.TryGetValue(key, out var v) ? (T)v : null; + public T GetAs({{cs_define_type key_type}} key) where T : {{cs_define_type value_type}} => (T)_dataMap[key]; +{{~end~}} + public {{cs_define_type value_type}} GetOrDefault({{cs_define_type key_type}} key) => _dataMap.TryGetValue(key, out var v) ? v : null; + public {{cs_define_type value_type}} Get({{cs_define_type key_type}} key) => _dataMap[key]; + public {{cs_define_type value_type}} this[{{cs_define_type key_type}} key] => _dataMap[key]; + + public void Resolve(Dictionary _tables) + { + foreach(var v in _dataList) + { + v.Resolve(_tables); + } + PostResolve(); + } + + public void TranslateText(System.Func translator) + { + foreach(var v in _dataList) + { + v.TranslateText(translator); + } + } + {{~else if x.is_list_table ~}} + private readonly List<{{cs_define_type value_type}}> _dataList; + + {{~if x.is_union_index~}} + private {{cs_table_union_map_type_name x}} _dataMapUnion; + {{~else if !x.index_list.empty?~}} + {{~for idx in x.index_list~}} + private Dictionary<{{cs_define_type idx.type}}, {{cs_define_type value_type}}> _dataMap_{{idx.index_field.name}}; + {{~end~}} + {{~end~}} + + public {{name}}(ByteBuf _buf) + { + _dataList = new List<{{cs_define_type value_type}}>(); + + for(int n = _buf.ReadSize() ; n > 0 ; --n) + { + {{cs_define_type value_type}} _v; + {{cs_deserialize '_buf' '_v' value_type}} + _dataList.Add(_v); + } + {{~if x.is_union_index~}} + _dataMapUnion = new {{cs_table_union_map_type_name x}}(); + foreach(var _v in _dataList) + { + _dataMapUnion.Add(({{cs_table_key_list x "_v"}}), _v); + } + {{~else if !x.index_list.empty?~}} + {{~for idx in x.index_list~}} + _dataMap_{{idx.index_field.name}} = new Dictionary<{{cs_define_type idx.type}}, {{cs_define_type value_type}}>(); + {{~end~}} + foreach(var _v in _dataList) + { + {{~for idx in x.index_list~}} + _dataMap_{{idx.index_field.name}}.Add(_v.{{idx.index_field.convention_name}}, _v); + {{~end~}} + } + {{~end~}} + PostInit(); + } + + + public List<{{cs_define_type value_type}}> DataList => _dataList; + + {{~if x.is_union_index~}} + public {{cs_define_type value_type}} Get({{cs_table_get_param_def_list x}}) => _dataMapUnion.TryGetValue(({{cs_table_get_param_name_list x}}), out {{cs_define_type value_type}} __v) ? __v : null; + {{~else if !x.index_list.empty? ~}} + {{~for idx in x.index_list~}} + public {{cs_define_type value_type}} GetBy{{idx.index_field.convention_name}}({{cs_define_type idx.type}} key) => _dataMap_{{idx.index_field.name}}.TryGetValue(key, out {{cs_define_type value_type}} __v) ? __v : null; + {{~end~}} + {{~end~}} + + public void Resolve(Dictionary _tables) + { + foreach(var v in _dataList) + { + v.Resolve(_tables); + } + PostResolve(); + } + + public void TranslateText(System.Func translator) + { + foreach(var v in _dataList) + { + v.TranslateText(translator); + } + } + {{~else~}} + + private readonly {{cs_define_type value_type}} _data; + + public {{name}}(ByteBuf _buf) + { + int n = _buf.ReadSize(); + if (n != 1) throw new SerializationException("table mode=one, but size != 1"); + {{cs_deserialize '_buf' '_data' value_type}} + PostInit(); + } + + + {{~ for field in value_type.bean.hierarchy_export_fields ~}} +{{~if field.comment != '' ~}} + /// + /// {{field.escape_comment}} + /// +{{~end~}} + public {{cs_define_type field.ctype}} {{field.convention_name}} => _data.{{field.convention_name}}; + {{~end~}} + + public void Resolve(Dictionary _tables) + { + _data.Resolve(_tables); + PostResolve(); + } + + public void TranslateText(System.Func translator) + { + _data.TranslateText(translator); + } + + {{~end~}} + + partial void PostInit(); + partial void PostResolve(); +} + +{{cs_end_name_space_grace x.namespace_with_top_module}} \ No newline at end of file diff --git a/Luban/CustomTemplate/config/cs_bin/tables.tpl b/Luban/CustomTemplate/config/cs_bin/tables.tpl new file mode 100644 index 00000000..58875994 --- /dev/null +++ b/Luban/CustomTemplate/config/cs_bin/tables.tpl @@ -0,0 +1,53 @@ +using Bright.Serialization; +using Cysharp.Threading.Tasks; +using System.Collections.Generic; + +{{ + name = x.name + namespace = x.namespace + tables = x.tables +}} +namespace {{namespace}} +{ + +public sealed class {{name}} +{ + {{~for table in tables ~}} +{{~if table.comment != '' ~}} + /// + /// {{table.escape_comment}} + /// +{{~end~}} + public {{table.full_name}} {{table.name}} {get; private set; } + {{~end~}} + + public {{name}}() { } + + public async UniTask LoadAsync(System.Func> loader) + { + var tables = new System.Collections.Generic.Dictionary(); + List list = new List(); + {{~for table in tables ~}} + list.Add(UniTask.Create(async () => + { + {{table.name}} = new {{table.full_name}}(await loader("{{table.output_data_file}}")); + tables.Add("{{table.full_name}}", {{table.name}}); + })); + {{~end~}} + + await UniTask.WhenAll(list); + + {{~for table in tables ~}} + {{table.name}}.Resolve(tables); + {{~end~}} + } + + public void TranslateText(System.Func translator) + { + {{~for table in tables ~}} + {{table.name}}.TranslateText(translator); + {{~end~}} + } +} + +} \ No newline at end of file diff --git a/Luban/gen_code_bin_to_project.bat b/Luban/gen_code_bin_to_project.bat index 57979970..92dd7669 100644 --- a/Luban/gen_code_bin_to_project.bat +++ b/Luban/gen_code_bin_to_project.bat @@ -5,7 +5,7 @@ set GEN_CLIENT=%WORKSPACE%\Luban\Luban.ClientServer\Luban.ClientServer.exe set CONF_ROOT=%WORKSPACE%\Luban\Config set DATA_OUTPUT=%ROOT_PATH%..\GenerateDatas -%GEN_CLIENT% -j cfg --^ +%GEN_CLIENT% --template_search_path CustomTemplate -j cfg --^ -d %CONF_ROOT%\Defines\__root__.xml ^ --input_data_dir %CONF_ROOT%\Datas ^ --output_code_dir %WORKSPACE%/Assets/GameScripts/HotFix/GameProto/GameConfig ^ diff --git a/Luban/gen_code_bin_to_server.bat b/Luban/gen_code_bin_to_server.bat index 695cd72f..1208fdb7 100644 --- a/Luban/gen_code_bin_to_server.bat +++ b/Luban/gen_code_bin_to_server.bat @@ -10,7 +10,7 @@ set DATA_OUTPUT=%ROOT_PATH%..\GenerateDatas --input_data_dir %CONF_ROOT%\Datas ^ --output_code_dir %WORKSPACE%/DotNet/Logic/src/Config/GameConfig ^ --output_data_dir ..\DotNet\Config\GameConfig ^ - --gen_types code_cs_unity_bin,data_bin ^ + --gen_types code_cs_bin,data_bin ^ -s server echo ======== 生成配置文件结束 ========