【特性】新增msgpack导出支持

main
walon 2021-12-06 10:36:38 +08:00
parent a78c9abd1a
commit 5861001683
18 changed files with 281 additions and 30 deletions

View File

@ -16,7 +16,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="ClosedXML" Version="0.95.4" /> <PackageReference Include="ClosedXML" Version="0.95.4" />
<PackageReference Include="ExcelDataReader" Version="3.6.0" /> <PackageReference Include="ExcelDataReader" Version="3.6.0" />
<PackageReference Include="NeoLua" Version="1.3.13" /> <PackageReference Include="MessagePack" Version="2.3.85" />
<PackageReference Include="NeoLua" Version="1.3.14" />
<PackageReference Include="Ude.NetStandard" Version="1.2.0" /> <PackageReference Include="Ude.NetStandard" Version="1.2.0" />
<PackageReference Include="YamlDotNet" Version="11.2.1" /> <PackageReference Include="YamlDotNet" Version="11.2.1" />
</ItemGroup> </ItemGroup>

View File

@ -88,8 +88,7 @@ namespace Luban.Job.Cfg.DataExporters
public void Accept(DText type, ByteBuf x) public void Accept(DText type, ByteBuf x)
{ {
x.WriteString(type.Key); x.WriteString(type.Key);
var ass = DefAssembly.LocalAssebmly; x.WriteString(type.TextOfCurrentAssembly);
x.WriteString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet));
} }
public void Accept(DBean type, ByteBuf x) public void Accept(DBean type, ByteBuf x)
@ -180,7 +179,7 @@ namespace Luban.Job.Cfg.DataExporters
public void Accept(DDateTime type, ByteBuf x) public void Accept(DDateTime type, ByteBuf x)
{ {
x.WriteInt(type.GetUnixTime(DefAssembly.LocalAssebmly.TimeZone)); x.WriteInt(type.UnixTimeOfCurrentAssembly);
} }
} }
} }

View File

@ -93,8 +93,7 @@ namespace Luban.Job.Cfg.DataExporters
x.WritePropertyName(DText.KEY_NAME); x.WritePropertyName(DText.KEY_NAME);
x.WriteStringValue(type.Key); x.WriteStringValue(type.Key);
x.WritePropertyName(DText.TEXT_NAME); x.WritePropertyName(DText.TEXT_NAME);
var ass = DefAssembly.LocalAssebmly; x.WriteStringValue(type.TextOfCurrentAssembly);
x.WriteStringValue(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet));
x.WriteEndObject(); x.WriteEndObject();
} }
@ -196,7 +195,7 @@ namespace Luban.Job.Cfg.DataExporters
public virtual void Accept(DDateTime type, Utf8JsonWriter x) public virtual void Accept(DDateTime type, Utf8JsonWriter x)
{ {
x.WriteNumberValue(type.GetUnixTime(DefAssembly.LocalAssebmly.TimeZone)); x.WriteNumberValue(type.UnixTimeOfCurrentAssembly);
} }
} }
} }

View File

@ -0,0 +1,239 @@
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.DataSources;
using Luban.Job.Cfg.DataVisitors;
using Luban.Job.Cfg.Defs;
using MessagePack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Luban.Job.Cfg.DataExporters
{
class MsgPackExportor
{
public static MsgPackExportor Ins { get; } = new();
public void WriteList(DefTable table, List<Record> records, ref MessagePackWriter writer)
{
writer.WriteArrayHeader(records.Count);
foreach (var record in records)
{
Accept(record.Data, ref writer);
}
}
public void Apply(DType type, ref MessagePackWriter writer)
{
switch (type)
{
case DInt x: Accept(x, ref writer); break;
case DString x: Accept(x, ref writer); break;
case DFloat x: Accept(x, ref writer); break;
case DBean x: Accept(x, ref writer); break;
case DBool x: Accept(x, ref writer); break;
case DEnum x: Accept(x, ref writer); break;
case DList x: Accept(x, ref writer); break;
case DArray x: Accept(x, ref writer); break;
case DLong x: Accept(x, ref writer); break;
case DDateTime x: Accept(x, ref writer); break;
case DMap x: Accept(x, ref writer); break;
case DText x: Accept(x, ref writer); break;
case DVector2 x: Accept(x, ref writer); break;
case DVector3 x: Accept(x, ref writer); break;
case DVector4 x: Accept(x, ref writer); break;
case DByte x: Accept(x, ref writer); break;
case DDouble x: Accept(x, ref writer); break;
case DFint x: Accept(x, ref writer); break;
case DFlong x: Accept(x, ref writer); break;
case DFshort x: Accept(x, ref writer); break;
case DSet x: Accept(x, ref writer); break;
case DShort x: Accept(x, ref writer); break;
default: throw new NotSupportedException($"DType:{type.GetType().FullName} not support");
}
}
public void Accept(DBool type, ref MessagePackWriter writer)
{
writer.Write(type.Value);
}
public void Accept(DByte type, ref MessagePackWriter writer)
{
writer.Write(type.Value);
}
public void Accept(DShort type, ref MessagePackWriter writer)
{
writer.Write(type.Value);
}
public void Accept(DFshort type, ref MessagePackWriter writer)
{
writer.Write(type.Value);
}
public void Accept(DInt type, ref MessagePackWriter writer)
{
writer.Write(type.Value);
}
public void Accept(DFint type, ref MessagePackWriter writer)
{
writer.Write(type.Value);
}
public void Accept(DLong type, ref MessagePackWriter writer)
{
writer.Write(type.Value);
}
public void Accept(DFlong type, ref MessagePackWriter writer)
{
writer.Write(type.Value);
}
public void Accept(DFloat type, ref MessagePackWriter writer)
{
writer.Write(type.Value);
}
public void Accept(DDouble type, ref MessagePackWriter writer)
{
writer.Write(type.Value);
}
public void Accept(DEnum type, ref MessagePackWriter writer)
{
writer.Write(type.Value);
}
public void Accept(DString type, ref MessagePackWriter writer)
{
writer.Write(type.Value);
}
public void Accept(DText type, ref MessagePackWriter writer)
{
writer.WriteArrayHeader(2);
writer.Write(type.Key);
writer.Write(type.TextOfCurrentAssembly);
}
public void Accept(DBytes type, ref MessagePackWriter writer)
{
writer.Write(type.Value);
}
public void Accept(DVector2 type, ref MessagePackWriter writer)
{
writer.WriteArrayHeader(2);
writer.Write(type.Value.X);
writer.Write(type.Value.Y);
}
public void Accept(DVector3 type, ref MessagePackWriter writer)
{
writer.WriteArrayHeader(3);
writer.Write(type.Value.X);
writer.Write(type.Value.Y);
writer.Write(type.Value.Z);
}
public void Accept(DVector4 type, ref MessagePackWriter writer)
{
writer.WriteArrayHeader(4);
writer.Write(type.Value.X);
writer.Write(type.Value.Y);
writer.Write(type.Value.Z);
writer.Write(type.Value.W);
}
public void Accept(DDateTime type, ref MessagePackWriter writer)
{
writer.Write(type.UnixTimeOfCurrentAssembly);
}
public void Accept(DBean type, ref MessagePackWriter writer)
{
var implType = type.ImplType;
var hierarchyFields = implType.HierarchyFields;
int exportCount = 0;
{
if (type.Type.IsAbstractType)
{
exportCount++;
}
int idx = 0;
foreach (var field in type.Fields)
{
var defField = (DefField)hierarchyFields[idx++];
if (field == null || !defField.NeedExport)
{
continue;
}
++exportCount;
}
}
writer.WriteMapHeader(exportCount);
if (type.Type.IsAbstractType)
{
writer.Write(DefBean.TYPE_NAME_KEY);
writer.Write(type.ImplType.Name);
}
int index = 0;
foreach (var field in type.Fields)
{
var defField = (DefField)hierarchyFields[index++];
if (field == null || !defField.NeedExport)
{
continue;
}
writer.Write(defField.Name);
Apply(field, ref writer);
}
}
public void Accept(DArray type, ref MessagePackWriter writer)
{
writer.WriteArrayHeader(type.Datas.Count);
foreach (var d in type.Datas)
{
Apply(d, ref writer);
}
}
public void Accept(DList type, ref MessagePackWriter writer)
{
writer.WriteArrayHeader(type.Datas.Count);
foreach (var d in type.Datas)
{
Apply(d, ref writer);
}
}
public void Accept(DSet type, ref MessagePackWriter writer)
{
writer.WriteArrayHeader(type.Datas.Count);
foreach (var d in type.Datas)
{
Apply(d, ref writer);
}
}
public void Accept(DMap type, ref MessagePackWriter writer)
{
writer.WriteMapHeader(type.Datas.Count);
foreach (var d in type.Datas)
{
Apply(d.Key, ref writer);
Apply(d.Value, ref writer);
}
}
}
}

View File

@ -87,7 +87,7 @@ namespace Luban.Job.Cfg.DataExporters
public void Accept(DDateTime type, CodedOutputStream x) public void Accept(DDateTime type, CodedOutputStream x)
{ {
x.WriteInt32(type.GetUnixTime(DefAssembly.LocalAssebmly.TimeZone)); x.WriteInt32(type.UnixTimeOfCurrentAssembly);
} }
public void Accept(DString type, CodedOutputStream x) public void Accept(DString type, CodedOutputStream x)
@ -104,12 +104,12 @@ namespace Luban.Job.Cfg.DataExporters
{ {
// 此处与 binary格式不同. binary格式还包含了key // 此处与 binary格式不同. binary格式还包含了key
// 意味pb格式是无法支持动态本土化的。 // 意味pb格式是无法支持动态本土化的。
var ass = DefAssembly.LocalAssebmly; x.WriteString(type.TextOfCurrentAssembly);
x.WriteString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet));
} }
private MemoryStream AllocMemoryStream() private MemoryStream AllocMemoryStream()
{ {
// TODO 优化
return new MemoryStream(); return new MemoryStream();
} }

View File

@ -12,8 +12,7 @@ namespace Luban.Job.Cfg.DataVisitors
public override string Accept(DText type) public override string Accept(DText type)
{ {
var ass = DefAssembly.LocalAssebmly; return $"#{{{DText.KEY_NAME}=>\"{type.Key}\",{DText.TEXT_NAME}=>\"{DataUtil.EscapeString(type.TextOfCurrentAssembly)}\"}}";
return $"#{{{DText.KEY_NAME}=>\"{type.Key}\",{DText.TEXT_NAME}=>\"{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}\"}}";
} }
public override string Accept(DBean type) public override string Accept(DBean type)

View File

@ -12,8 +12,7 @@ namespace Luban.Job.Cfg.DataVisitors
public override string Accept(DText type) public override string Accept(DText type)
{ {
var ass = DefAssembly.LocalAssebmly; return $"{{\"{DText.KEY_NAME}\":\"{type.Key}\",\"{DText.TEXT_NAME}\":\"{DataUtil.EscapeString(type.TextOfCurrentAssembly)}\"}}";
return $"{{\"{DText.KEY_NAME}\":\"{type.Key}\",\"{DText.TEXT_NAME}\":\"{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}\"}}";
} }
public override string Accept(DBean type) public override string Accept(DBean type)

View File

@ -92,8 +92,7 @@ namespace Luban.Job.Cfg.DataVisitors
public virtual string Accept(DDateTime type) public virtual string Accept(DDateTime type)
{ {
var ass = DefAssembly.LocalAssebmly; return type.UnixTimeOfCurrentAssembly.ToString();
return type.GetUnixTime(ass.TimeZone).ToString();
} }
} }
} }

View File

@ -12,8 +12,7 @@ namespace Luban.Job.Cfg.DataVisitors
public override string Accept(DText type) public override string Accept(DText type)
{ {
var ass = DefAssembly.LocalAssebmly; return $"{{{DText.KEY_NAME}='{type.Key}',{DText.TEXT_NAME}=\"{DataUtil.EscapeString(type.TextOfCurrentAssembly)}\"}}";
return $"{{{DText.KEY_NAME}='{type.Key}',{DText.TEXT_NAME}=\"{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}\"}}";
} }
public override string Accept(DBean type) public override string Accept(DBean type)

View File

@ -12,8 +12,7 @@ namespace Luban.Job.Cfg.DataVisitors
public override string Accept(DText type) public override string Accept(DText type)
{ {
var ass = DefAssembly.LocalAssebmly; return $"{{\"{DText.KEY_NAME}\":\"{type.Key}\",\"{DText.TEXT_NAME}\":\"{DataUtil.EscapeString(type.TextOfCurrentAssembly)}\"}}";
return $"{{\"{DText.KEY_NAME}\":\"{type.Key}\",\"{DText.TEXT_NAME}\":\"{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}\"}}";
} }
public override string Accept(DBean type) public override string Accept(DBean type)

View File

@ -12,8 +12,7 @@ namespace Luban.Job.Cfg.DataVisitors
public override string Accept(DText type) public override string Accept(DText type)
{ {
#if !LUBAN_LITE #if !LUBAN_LITE
var ass = DefAssembly.LocalAssebmly; return $"\"{type.Key}#{type.TextOfCurrentAssembly}\"";
return $"\"{type.Key}#{type.GetText(ass.ExportTextTable, ass.NotConvertTextSet)}\"";
#else #else
return $"\"{type.Key}#{type.RawValue}\""; return $"\"{type.Key}#{type.RawValue}\"";
#endif #endif

View File

@ -12,8 +12,7 @@ namespace Luban.Job.Cfg.DataVisitors
public override string Accept(DText type) public override string Accept(DText type)
{ {
var ass = DefAssembly.LocalAssebmly; return $"{{{DText.KEY_NAME}='{type.Key}',{DText.TEXT_NAME}='{DataUtil.EscapeString(type.TextOfCurrentAssembly)}'}}";
return $"{{{DText.KEY_NAME}='{type.Key}',{DText.TEXT_NAME}='{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}'}}";
} }
public override string Accept(DBean type) public override string Accept(DBean type)

View File

@ -1,5 +1,6 @@
using Luban.Common.Utils; using Luban.Common.Utils;
using Luban.Job.Cfg.DataVisitors; using Luban.Job.Cfg.DataVisitors;
using Luban.Job.Cfg.Defs;
using Luban.Job.Cfg.Utils; using Luban.Job.Cfg.Utils;
using System; using System;
@ -60,6 +61,8 @@ namespace Luban.Job.Cfg.Datas
} }
} }
public int UnixTimeOfCurrentAssembly => GetUnixTime(DefAssembly.LocalAssebmly.TimeZone);
public override void Apply<T>(IDataActionVisitor<T> visitor, T x) public override void Apply<T>(IDataActionVisitor<T> visitor, T x)
{ {
visitor.Accept(this, x); visitor.Accept(this, x);

View File

@ -1,4 +1,5 @@
using Luban.Job.Cfg.DataVisitors; using Luban.Job.Cfg.DataVisitors;
using Luban.Job.Cfg.Defs;
#if !LUBAN_LITE #if !LUBAN_LITE
using Luban.Job.Cfg.l10n; using Luban.Job.Cfg.l10n;
#endif #endif
@ -40,6 +41,15 @@ namespace Luban.Job.Cfg.Datas
} }
return _rawValue; return _rawValue;
} }
public string TextOfCurrentAssembly
{
get
{
var ass = DefAssembly.LocalAssebmly;
return GetText(ass.ExportTextTable, ass.NotConvertTextSet);
}
}
#endif #endif
public override void Apply<T>(IDataActionVisitor<T> visitor, T x) public override void Apply<T>(IDataActionVisitor<T> visitor, T x)

View File

@ -14,6 +14,8 @@ namespace Luban.Job.Cfg.Generate
[Render("data_xml")] [Render("data_xml")]
[Render("data_yaml")] [Render("data_yaml")]
[Render("data_protobuf")] [Render("data_protobuf")]
[Render("data_msgpack")]
[Render("data_flatbuffers")]
class DataScatterRender : DataRenderBase class DataScatterRender : DataRenderBase
{ {
public override void Render(GenContext ctx) public override void Render(GenContext ctx)

View File

@ -18,12 +18,6 @@ namespace Luban.Job.Cfg.Utils
return type.Apply(IsSimpleLiteralDataVisitor2.Ins); return type.Apply(IsSimpleLiteralDataVisitor2.Ins);
} }
public static string ToLocalizedText(DText type)
{
var ass = DefAssembly.LocalAssebmly;
return type.GetText(ass.ExportTextTable, ass.NotConvertTextSet);
}
public static DType GetField(DBean bean, string fieldName) public static DType GetField(DBean bean, string fieldName)
{ {
int index = 0; int index = 0;

View File

@ -9,6 +9,7 @@ using Luban.Job.Cfg.l10n;
using Luban.Job.Cfg.RawDefs; using Luban.Job.Cfg.RawDefs;
using Luban.Job.Common.Tpl; using Luban.Job.Common.Tpl;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using MessagePack;
using Scriban; using Scriban;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -98,6 +99,14 @@ namespace Luban.Job.Cfg.Utils
ProtobufExportor.Ins.WriteList(table, records, ms); ProtobufExportor.Ins.WriteList(table, records, ms);
return DataUtil.StreamToBytes(ms); return DataUtil.StreamToBytes(ms);
} }
case "data_msgpack":
{
var ms = new System.Buffers.ArrayBufferWriter<byte>();
var writer = new MessagePackWriter(ms);
MsgPackExportor.Ins.WriteList(table, records, ref writer);
writer.Flush();
return ms.WrittenSpan.ToArray();
}
//case "data_erlang": //case "data_erlang":
//{ //{
// var content = new StringBuilder(); // var content = new StringBuilder();

View File

@ -118,6 +118,8 @@ namespace Luban.Job.Common.Utils
{ "erl", "erl" }, { "erl", "erl" },
{ "xlsx", "xlsx" }, { "xlsx", "xlsx" },
{ "protobuf", "bytes" }, { "protobuf", "bytes" },
{ "msgpack", "bytes" },
{ "flatbuffers", "bytes" },
}; };
public static string GetOutputFileSuffix(string genType) public static string GetOutputFileSuffix(string genType)