【特性】cfg 导出数据中text类型包含key数据。

main
walon 2021-08-26 10:35:21 +08:00
parent 35fd2b38cb
commit 12b3c9d5df
8 changed files with 94 additions and 34 deletions

View File

@ -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));
}

View File

@ -15,6 +15,38 @@ namespace Luban.Job.Cfg.DataExporters
{
public new static Json2Exportor Ins { get; } = new();
public void WriteAsObject(DefTable table, List<Record> 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();

View File

@ -21,38 +21,6 @@ namespace Luban.Job.Cfg.DataExporters
x.WriteEndArray();
}
public void WriteAsObject(DefTable table, List<Record> 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();
}

View File

@ -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)

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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}");
// }
//}
}
}

View File

@ -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);