From 12be29afd5052e3c3748471043ae67b5d603b0f2 Mon Sep 17 00:00:00 2001 From: walon Date: Thu, 26 Aug 2021 11:19:09 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=89=B9=E6=80=A7=E3=80=91=20cfg=20cs?= =?UTF-8?q?=5Fbin=E6=94=AF=E6=8C=81=E5=8A=A8=E6=80=81=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Luban.Job.Cfg/Source/Defs/DefField.cs | 4 + .../Source/Defs/TTypeTemplateExtends.cs | 16 ++- .../CppUnderingDeserializeVisitor.cs | 2 +- .../CsRecursiveTranslateVisitor.cs | 126 ++++++++++++++++++ .../CsUnderingDeserializeVisitor.cs | 2 +- .../Templates/config/cpp_bin/tables.tpl | 2 +- .../Templates/config/cs_bin/bean.tpl | 33 ++--- .../Templates/config/cs_bin/table.tpl | 19 ++- .../Templates/config/cs_bin/tables.tpl | 7 + 9 files changed, 186 insertions(+), 25 deletions(-) create mode 100644 src/Luban.Job.Cfg/Source/TypeVisitors/CsRecursiveTranslateVisitor.cs diff --git a/src/Luban.Job.Cfg/Source/Defs/DefField.cs b/src/Luban.Job.Cfg/Source/Defs/DefField.cs index 6bf5f6f..c87bf1a 100644 --- a/src/Luban.Job.Cfg/Source/Defs/DefField.cs +++ b/src/Luban.Job.Cfg/Source/Defs/DefField.cs @@ -142,8 +142,12 @@ namespace Luban.Job.Cfg.Defs public CfgField RawDefine { get; } + public string GetTextKeyName(string name) => name + TText.L10N_FIELD_SUFFIX; + public bool GenTextKey => this.CType is TText; + public bool HasRecursiveText => HasRecursiveRef; + public DefField(DefTypeBase host, CfgField f, int idOffset) : base(host, f, idOffset) { Index = f.Index; diff --git a/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs b/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs index 09de5c1..fea025e 100644 --- a/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs +++ b/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs @@ -8,6 +8,20 @@ namespace Luban.Job.Cfg.Defs { class TTypeTemplateExtends : TTypeTemplateCommonExtends { + public static string CsDefineTextKeyField(DefField field) + { + return $"string {field.GetTextKeyName(field.CsStyleName)}"; + } + + public static string CsTranslateText(DefField field, string translatorName) + { + return $"{field.CsStyleName} = {translatorName}({field.GetTextKeyName(field.CsStyleName)}, {field.CsStyleName});"; + } + + public static string CsRecursiveTranslateText(DefField field, string translatorName) + { + return field.CType.Apply(CsRecursiveTranslateVisitor.Ins, field.CsStyleName, translatorName); + } public static string CsJsonDeserialize(string bufName, string fieldName, string jsonFieldName, TType type) { @@ -197,7 +211,7 @@ namespace Luban.Job.Cfg.Defs { switch (lan) { - case "cpp": return $"{CppDefineTypeName.Ins.Accept(field.CType.IsNullable ? TString.NullableIns : TString.Ins)} {field.CppStyleName}{TText.L10N_FIELD_SUFFIX};"; + case "cs": return $"string {field.CsStyleName}{TText.L10N_FIELD_SUFFIX};"; default: throw new NotSupportedException($"not support lan:{lan}"); } } diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CppUnderingDeserializeVisitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CppUnderingDeserializeVisitor.cs index f793601..dbb2e0a 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CppUnderingDeserializeVisitor.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CppUnderingDeserializeVisitor.cs @@ -74,7 +74,7 @@ namespace Luban.Job.Cfg.TypeVisitors public string Accept(TText type, string bufName, string fieldName) { - return $"if(!{bufName}.readString({fieldName})) return false;"; + return $"if(!{bufName}.readString({fieldName})) return false; /* key */ if(!{bufName}.readString({fieldName})) return false; /* text */"; } public string Accept(TBean type, string bufName, string fieldName) diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsRecursiveTranslateVisitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsRecursiveTranslateVisitor.cs new file mode 100644 index 0000000..ffb669c --- /dev/null +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsRecursiveTranslateVisitor.cs @@ -0,0 +1,126 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; +using System; + +namespace Luban.Job.Cfg.TypeVisitors +{ + class CsRecursiveTranslateVisitor : ITypeFuncVisitor + { + public static CsRecursiveTranslateVisitor Ins { get; } = new(); + + public string Accept(TBool type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + + public string Accept(TByte type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + + public string Accept(TShort type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + + public string Accept(TFshort type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + + public string Accept(TInt type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + + public string Accept(TFint type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + + public string Accept(TLong type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + + public string Accept(TFlong type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + + public string Accept(TFloat type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + + public string Accept(TDouble type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + + public string Accept(TEnum type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + + public string Accept(TString type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + + public string Accept(TBytes type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + + public string Accept(TText type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + + public string Accept(TBean type, string fieldName, string tablesName) + { + return $"{fieldName}?.TranslateText({tablesName});"; + } + + public string Accept(TArray type, string fieldName, string tablesName) + { + return $@"foreach(var _e in {fieldName}) {{ _e?.TranslateText({tablesName}); }}"; + } + + public string Accept(TList type, string fieldName, string tablesName) + { + return $@"foreach(var _e in {fieldName}) {{ _e?.TranslateText({tablesName}); }}"; + } + + public string Accept(TSet type, string fieldName, string tablesName) + { + return $@"foreach(var _e in {fieldName}) {{ _e?.TranslateText({tablesName}); }}"; + } + + public string Accept(TMap type, string fieldName, string tablesName) + { + return $@"foreach(var _e in {fieldName}.Values) {{ _e?.TranslateText({tablesName}); }}"; + } + + public string Accept(TVector2 type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + + public string Accept(TVector3 type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + + public string Accept(TVector4 type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + + public string Accept(TDateTime type, string fieldName, string tablesName) + { + throw new NotImplementedException(); + } + } +} diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs index 25193fe..9a59e8f 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs @@ -75,7 +75,7 @@ namespace Luban.Job.Common.TypeVisitors public string Accept(TText type, string bufName, string fieldName) { - return $"{fieldName} = {bufName}.ReadString();"; + return $"{fieldName}{TText.L10N_FIELD_SUFFIX} = {bufName}.ReadString(); {fieldName} = {bufName}.ReadString();"; } public string Accept(TBean type, string bufName, string fieldName) diff --git a/src/Luban.Server/Templates/config/cpp_bin/tables.tpl b/src/Luban.Server/Templates/config/cpp_bin/tables.tpl index 8e64fc6..98e7f0d 100644 --- a/src/Luban.Server/Templates/config/cpp_bin/tables.tpl +++ b/src/Luban.Server/Templates/config/cpp_bin/tables.tpl @@ -10,7 +10,7 @@ class {{name}} {{table.cpp_full_name}} {{table.name}}; {{~end~}} - bool load(::bright::Function loader) + bool load(::bright::Loader loader) { ::bright::HashMap<::bright::String, void*> __tables__; diff --git a/src/Luban.Server/Templates/config/cs_bin/bean.tpl b/src/Luban.Server/Templates/config/cs_bin/bean.tpl index 0336e84..e5d0086 100644 --- a/src/Luban.Server/Templates/config/cs_bin/bean.tpl +++ b/src/Luban.Server/Templates/config/cs_bin/bean.tpl @@ -31,19 +31,6 @@ public {{x.cs_class_modifier}} partial class {{name}} : {{if parent_def_type}} { {{~end~}} } - public {{name}}({{- for field in hierarchy_export_fields }}{{cs_define_type field.ctype}} {{field.name}}{{if !for.last}},{{end}} {{end}}) {{if parent_def_type}} : base({{- for field in parent_def_type.hierarchy_export_fields }}{{field.name}}{{if !for.last}},{{end}}{{end}}) {{end}} - { - {{~ for field in export_fields ~}} - this.{{field.cs_style_name}} = {{field.name}}; - {{~if field.index_field~}} - foreach(var _v in {{field.cs_style_name}}) - { - {{field.cs_style_name}}_Index.Add(_v.{{field.index_field.cs_style_name}}, _v); - } - {{~end~}} - {{~end~}} - } - public static {{name}} Deserialize{{name}}(ByteBuf _buf) { {{~if x.is_abstract_type~}} @@ -65,13 +52,16 @@ public {{x.cs_class_modifier}} partial class {{name}} : {{if parent_def_type}} { /// {{field.comment}} /// {{~end~}} - public readonly {{cs_define_type field.ctype}} {{field.cs_style_name}}; + public {{cs_define_type field.ctype}} {{field.cs_style_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.cs_style_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 field.gen_text_key~}} + public {{cs_define_text_key_field field}} {get;} + {{~end~}} {{~end~}} {{~if !x.is_abstract_type~}} @@ -91,10 +81,21 @@ public {{x.cs_class_modifier}} partial class {{name}} : {{if parent_def_type}} { {{cs_recursive_resolve field '_tables'}} {{~end~}} {{~end~}} - OnResolveFinish(_tables); } - partial void OnResolveFinish(Dictionary _tables); + 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() { diff --git a/src/Luban.Server/Templates/config/cs_bin/table.tpl b/src/Luban.Server/Templates/config/cs_bin/table.tpl index 46a6f64..e4e554f 100644 --- a/src/Luban.Server/Templates/config/cs_bin/table.tpl +++ b/src/Luban.Server/Templates/config/cs_bin/table.tpl @@ -15,7 +15,7 @@ namespace {{x.namespace_with_top_module}} /// {{x.comment}} /// {{~end~}} -public sealed partial class {{name}} +public sealed class {{name}} { {{~if x.is_map_table ~}} private readonly Dictionary<{{cs_define_type key_type}}, {{cs_define_type value_type}}> _dataMap; @@ -52,7 +52,14 @@ public sealed partial class {{name}} { v.Resolve(_tables); } - OnResolveFinish(_tables); + } + + public void TranslateText(System.Func translator) + { + foreach(var v in _dataList) + { + v.TranslateText(translator); + } } {{~else~}} @@ -82,12 +89,14 @@ public sealed partial class {{name}} public void Resolve(Dictionary _tables) { _data.Resolve(_tables); - OnResolveFinish(_tables); + } + + public void TranslateText(System.Func translator) + { + _data.TranslateText(translator); } {{~end~}} - - partial void OnResolveFinish(Dictionary _tables); } } \ No newline at end of file diff --git a/src/Luban.Server/Templates/config/cs_bin/tables.tpl b/src/Luban.Server/Templates/config/cs_bin/tables.tpl index f21bf4a..69b5639 100644 --- a/src/Luban.Server/Templates/config/cs_bin/tables.tpl +++ b/src/Luban.Server/Templates/config/cs_bin/tables.tpl @@ -32,6 +32,13 @@ public sealed class {{name}} {{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