【特性】 cfg cs_bin支持动态本地化

main
walon 2021-08-26 11:19:09 +08:00
parent 12b3c9d5df
commit 12be29afd5
9 changed files with 186 additions and 25 deletions

View File

@ -142,8 +142,12 @@ namespace Luban.Job.Cfg.Defs
public CfgField RawDefine { get; } public CfgField RawDefine { get; }
public string GetTextKeyName(string name) => name + TText.L10N_FIELD_SUFFIX;
public bool GenTextKey => this.CType is TText; public bool GenTextKey => this.CType is TText;
public bool HasRecursiveText => HasRecursiveRef;
public DefField(DefTypeBase host, CfgField f, int idOffset) : base(host, f, idOffset) public DefField(DefTypeBase host, CfgField f, int idOffset) : base(host, f, idOffset)
{ {
Index = f.Index; Index = f.Index;

View File

@ -8,6 +8,20 @@ namespace Luban.Job.Cfg.Defs
{ {
class TTypeTemplateExtends : TTypeTemplateCommonExtends 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) public static string CsJsonDeserialize(string bufName, string fieldName, string jsonFieldName, TType type)
{ {
@ -197,7 +211,7 @@ namespace Luban.Job.Cfg.Defs
{ {
switch (lan) 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}"); default: throw new NotSupportedException($"not support lan:{lan}");
} }
} }

View File

@ -74,7 +74,7 @@ namespace Luban.Job.Cfg.TypeVisitors
public string Accept(TText type, string bufName, string fieldName) 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) public string Accept(TBean type, string bufName, string fieldName)

View File

@ -0,0 +1,126 @@
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
using System;
namespace Luban.Job.Cfg.TypeVisitors
{
class CsRecursiveTranslateVisitor : ITypeFuncVisitor<string, string, string>
{
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();
}
}
}

View File

@ -75,7 +75,7 @@ namespace Luban.Job.Common.TypeVisitors
public string Accept(TText type, string bufName, string fieldName) 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) public string Accept(TBean type, string bufName, string fieldName)

View File

@ -10,7 +10,7 @@ class {{name}}
{{table.cpp_full_name}} {{table.name}}; {{table.cpp_full_name}} {{table.name}};
{{~end~}} {{~end~}}
bool load(::bright::Function<bool(ByteBuf&, const ::bright::String&)> loader) bool load(::bright::Loader<ByteBuf> loader)
{ {
::bright::HashMap<::bright::String, void*> __tables__; ::bright::HashMap<::bright::String, void*> __tables__;

View File

@ -31,19 +31,6 @@ public {{x.cs_class_modifier}} partial class {{name}} : {{if parent_def_type}} {
{{~end~}} {{~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) public static {{name}} Deserialize{{name}}(ByteBuf _buf)
{ {
{{~if x.is_abstract_type~}} {{~if x.is_abstract_type~}}
@ -65,13 +52,16 @@ public {{x.cs_class_modifier}} partial class {{name}} : {{if parent_def_type}} {
/// {{field.comment}} /// {{field.comment}}
/// </summary> /// </summary>
{{~end~}} {{~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~}} {{~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}}>(); 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~}} {{~end~}}
{{~if field.gen_ref~}} {{~if field.gen_ref~}}
public {{field.cs_ref_validator_define}} public {{field.cs_ref_validator_define}}
{{~end~}} {{~end~}}
{{~if field.gen_text_key~}}
public {{cs_define_text_key_field field}} {get;}
{{~end~}}
{{~end~}} {{~end~}}
{{~if !x.is_abstract_type~}} {{~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'}} {{cs_recursive_resolve field '_tables'}}
{{~end~}} {{~end~}}
{{~end~}} {{~end~}}
OnResolveFinish(_tables);
} }
partial void OnResolveFinish(Dictionary<string, object> _tables); public {{x.cs_method_modifier}} void TranslateText(System.Func<string, string, string> 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() public override string ToString()
{ {

View File

@ -15,7 +15,7 @@ namespace {{x.namespace_with_top_module}}
/// {{x.comment}} /// {{x.comment}}
/// </summary> /// </summary>
{{~end~}} {{~end~}}
public sealed partial class {{name}} public sealed class {{name}}
{ {
{{~if x.is_map_table ~}} {{~if x.is_map_table ~}}
private readonly Dictionary<{{cs_define_type key_type}}, {{cs_define_type value_type}}> _dataMap; 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); v.Resolve(_tables);
} }
OnResolveFinish(_tables); }
public void TranslateText(System.Func<string, string, string> translator)
{
foreach(var v in _dataList)
{
v.TranslateText(translator);
}
} }
{{~else~}} {{~else~}}
@ -82,12 +89,14 @@ public sealed partial class {{name}}
public void Resolve(Dictionary<string, object> _tables) public void Resolve(Dictionary<string, object> _tables)
{ {
_data.Resolve(_tables); _data.Resolve(_tables);
OnResolveFinish(_tables); }
public void TranslateText(System.Func<string, string, string> translator)
{
_data.TranslateText(translator);
} }
{{~end~}} {{~end~}}
partial void OnResolveFinish(Dictionary<string, object> _tables);
} }
} }

View File

@ -32,6 +32,13 @@ public sealed class {{name}}
{{table.name}}.Resolve(tables); {{table.name}}.Resolve(tables);
{{~end~}} {{~end~}}
} }
public void TranslateText(System.Func<string, string, string> translator)
{
{{~for table in tables ~}}
{{table.name}}.TranslateText(translator);
{{~end~}}
}
} }
} }