From 12b3c9d5df57087c4f7e2655bb37eda69abc502b Mon Sep 17 00:00:00 2001 From: walon Date: Thu, 26 Aug 2021 10:35:21 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=89=B9=E6=80=A7=E3=80=91cfg=20?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E6=95=B0=E6=8D=AE=E4=B8=ADtext=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E5=8C=85=E5=90=ABkey=E6=95=B0=E6=8D=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/DataExporters/BinaryExportor.cs | 1 + .../Source/DataExporters/Json2Exportor.cs | 32 +++++++++++++++ .../Source/DataExporters/JsonExportor.cs | 39 +++---------------- .../Source/DataExporters/LuaExportor.cs | 2 +- src/Luban.Job.Cfg/Source/Datas/DText.cs | 3 ++ src/Luban.Job.Cfg/Source/Defs/DefField.cs | 30 ++++++++++++++ .../Source/Defs/TTypeTemplateExtends.cs | 19 +++++++++ src/Luban.Job.Common/Source/Types/TText.cs | 2 + 8 files changed, 94 insertions(+), 34 deletions(-) diff --git a/src/Luban.Job.Cfg/Source/DataExporters/BinaryExportor.cs b/src/Luban.Job.Cfg/Source/DataExporters/BinaryExportor.cs index 80d2ba9..d745520 100644 --- a/src/Luban.Job.Cfg/Source/DataExporters/BinaryExportor.cs +++ b/src/Luban.Job.Cfg/Source/DataExporters/BinaryExportor.cs @@ -87,6 +87,7 @@ namespace Luban.Job.Cfg.DataExporters public void Accept(DText type, DefAssembly ass, ByteBuf x) { + x.WriteString(type.Key); x.WriteString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet)); } diff --git a/src/Luban.Job.Cfg/Source/DataExporters/Json2Exportor.cs b/src/Luban.Job.Cfg/Source/DataExporters/Json2Exportor.cs index 8a40196..a293bfe 100644 --- a/src/Luban.Job.Cfg/Source/DataExporters/Json2Exportor.cs +++ b/src/Luban.Job.Cfg/Source/DataExporters/Json2Exportor.cs @@ -15,6 +15,38 @@ namespace Luban.Job.Cfg.DataExporters { public new static Json2Exportor Ins { get; } = new(); + public void WriteAsObject(DefTable table, List datas, DefAssembly ass, Utf8JsonWriter x) + { + switch (table.Mode) + { + case RawDefs.ETableMode.ONE: + { + this.Accept(datas[0].Data, ass, x); + break; + } + case RawDefs.ETableMode.MAP: + { + + x.WriteStartObject(); + string indexName = table.IndexField.Name; + foreach (var rec in datas) + { + var indexFieldData = rec.Data.GetField(indexName); + + x.WritePropertyName(indexFieldData.Apply(ToJsonPropertyNameVisitor.Ins)); + this.Accept(rec.Data, ass, x); + } + + x.WriteEndObject(); + break; + } + default: + { + throw new NotSupportedException($"not support table mode:{table.Mode}"); + } + } + } + public override void Accept(DMap type, DefAssembly ass, Utf8JsonWriter x) { x.WriteStartObject(); diff --git a/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs b/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs index e67a18f..97f2fe7 100644 --- a/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs +++ b/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs @@ -21,38 +21,6 @@ namespace Luban.Job.Cfg.DataExporters x.WriteEndArray(); } - public void WriteAsObject(DefTable table, List datas, DefAssembly ass, Utf8JsonWriter x) - { - switch (table.Mode) - { - case RawDefs.ETableMode.ONE: - { - this.Accept(datas[0].Data, ass, x); - break; - } - case RawDefs.ETableMode.MAP: - { - - x.WriteStartObject(); - string indexName = table.IndexField.Name; - foreach (var rec in datas) - { - var indexFieldData = rec.Data.GetField(indexName); - - x.WritePropertyName(indexFieldData.Apply(ToJsonPropertyNameVisitor.Ins)); - this.Accept(rec.Data, ass, x); - } - - x.WriteEndObject(); - break; - } - default: - { - throw new NotSupportedException($"not support table mode:{table.Mode}"); - } - } - } - public void Accept(DBool type, DefAssembly ass, Utf8JsonWriter x) { x.WriteBooleanValue(type.Value); @@ -120,7 +88,12 @@ namespace Luban.Job.Cfg.DataExporters public void Accept(DText type, DefAssembly ass, Utf8JsonWriter x) { + x.WriteStartObject(); + x.WritePropertyName(DText.KEY_NAME); + x.WriteStringValue(type.Key); + x.WritePropertyName(DText.TEXT_NAME); x.WriteStringValue(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet)); + x.WriteEndObject(); } public void Accept(DBean type, DefAssembly ass, Utf8JsonWriter x) @@ -145,7 +118,7 @@ namespace Luban.Job.Cfg.DataExporters // 特殊处理 bean 多态类型 // 另外,不生成 xxx:null 这样 - if (d == null || (d is DBean db && db.ImplType == null)) + if (d == null /*|| (d is DBean db && db.ImplType == null)*/) { //x.WriteNullValue(); } diff --git a/src/Luban.Job.Cfg/Source/DataExporters/LuaExportor.cs b/src/Luban.Job.Cfg/Source/DataExporters/LuaExportor.cs index f81202c..f7f7679 100644 --- a/src/Luban.Job.Cfg/Source/DataExporters/LuaExportor.cs +++ b/src/Luban.Job.Cfg/Source/DataExporters/LuaExportor.cs @@ -117,7 +117,7 @@ namespace Luban.Job.Cfg.DataExporters public void Accept(DText type, DefAssembly ass, StringBuilder line) { - line.Append('\'').Append(EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))).Append('\''); + line.Append($"{{{DText.KEY_NAME}='{type.Key}',{DText.TEXT_NAME}='{EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}'}}"); } public void Accept(DBean type, DefAssembly ass, StringBuilder line) diff --git a/src/Luban.Job.Cfg/Source/Datas/DText.cs b/src/Luban.Job.Cfg/Source/Datas/DText.cs index 5c61388..06a03b4 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DText.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DText.cs @@ -6,6 +6,9 @@ namespace Luban.Job.Cfg.Datas { public class DText : DType { + public const string KEY_NAME = "key"; + public const string TEXT_NAME = "text"; + public string Key { get; } private readonly string _rawValue; diff --git a/src/Luban.Job.Cfg/Source/Defs/DefField.cs b/src/Luban.Job.Cfg/Source/Defs/DefField.cs index 21d28c6..6bf5f6f 100644 --- a/src/Luban.Job.Cfg/Source/Defs/DefField.cs +++ b/src/Luban.Job.Cfg/Source/Defs/DefField.cs @@ -142,6 +142,8 @@ namespace Luban.Job.Cfg.Defs public CfgField RawDefine { get; } + public bool GenTextKey => this.CType is TText; + public DefField(DefTypeBase host, CfgField f, int idOffset) : base(host, f, idOffset) { Index = f.Index; @@ -181,6 +183,10 @@ namespace Luban.Job.Cfg.Defs { throw new Exception($"container element type:'{e.Bean.FullName}' can't be empty bean"); } + if (t.ElementType is TText) + { + throw new Exception($"bean:{HostType.FullName} field:{Name} container element type can't text"); + } break; } case TList t: @@ -189,6 +195,30 @@ namespace Luban.Job.Cfg.Defs { throw new Exception($"container element type:'{e.Bean.FullName}' can't be empty bean"); } + if (t.ElementType is TText) + { + throw new Exception($"bean:{HostType.FullName} field:{Name} container element type can't text"); + } + break; + } + case TSet t: + { + if (t.ElementType is TText) + { + throw new Exception($"bean:{HostType.FullName} field:{Name} container element type can't text"); + } + break; + } + case TMap t: + { + if (t.KeyType is TText) + { + throw new Exception($"bean:{HostType.FullName} field:{Name} container key type can't text"); + } + if (t.ValueType is TText) + { + throw new Exception($"bean:{HostType.FullName} field:{Name} container value type can't text"); + } break; } } diff --git a/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs b/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs index 6cb50c1..09de5c1 100644 --- a/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs +++ b/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs @@ -2,6 +2,7 @@ using Luban.Job.Cfg.TypeVisitors; using Luban.Job.Common.Defs; using Luban.Job.Common.Types; using Luban.Job.Common.TypeVisitors; +using System; namespace Luban.Job.Cfg.Defs { @@ -191,5 +192,23 @@ namespace Luban.Job.Cfg.Defs return type.Apply(PyUnderingDeserializeVisitor.Ins, $"{jsonVarName}['{jsonFieldName}']", fieldName); } } + + public static string DefineTextKeyField(DefField field, string lan) + { + switch (lan) + { + case "cpp": return $"{CppDefineTypeName.Ins.Accept(field.CType.IsNullable ? TString.NullableIns : TString.Ins)} {field.CppStyleName}{TText.L10N_FIELD_SUFFIX};"; + default: throw new NotSupportedException($"not support lan:{lan}"); + } + } + + //public static string DeserializeTextKeyField(DefField field, string lan, string bufName) + //{ + // switch (lan) + // { + // case "cpp": return $"{CppDefineTypeName.Ins.Accept(TString.Ins)} {field.CppStyleName}{L10N_FIELD_SUFFIX};"; + // default: throw new NotSupportedException($"not support lan:{lan}"); + // } + //} } } diff --git a/src/Luban.Job.Common/Source/Types/TText.cs b/src/Luban.Job.Common/Source/Types/TText.cs index 5677609..eea7b69 100644 --- a/src/Luban.Job.Common/Source/Types/TText.cs +++ b/src/Luban.Job.Common/Source/Types/TText.cs @@ -4,6 +4,8 @@ namespace Luban.Job.Common.Types { public class TText : TType { + public const string L10N_FIELD_SUFFIX = "_l10n_key"; + public static TText Ins { get; } = new TText(false); public static TText NullableIns { get; } = new TText(true);