[new] 新增Bson导出格式支持

main
walon 2022-07-18 18:03:53 +08:00
parent c0ddce497c
commit c0706a84f1
5 changed files with 215 additions and 0 deletions

View File

@ -18,6 +18,7 @@
<PackageReference Include="ExcelDataReader" Version="3.6.0" />
<PackageReference Include="MessagePack" Version="2.3.85" />
<PackageReference Include="NeoLua" Version="1.3.14" />
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2" />
<PackageReference Include="Ude.NetStandard" Version="1.2.0" />
<PackageReference Include="YamlDotNet" Version="11.2.1" />
</ItemGroup>

View File

@ -0,0 +1,203 @@
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.DataSources;
using Luban.Job.Cfg.DataVisitors;
using Luban.Job.Cfg.Defs;
using Luban.Job.Cfg.Utils;
using Newtonsoft.Json.Bson;
using System;
using System.Collections.Generic;
using System.Text.Json;
namespace Luban.Job.Cfg.DataExporters
{
class BsonExportor : IDataActionVisitor<BsonDataWriter>
{
public static BsonExportor Ins { get; } = new BsonExportor();
public void WriteAsArray(List<Record> datas, BsonDataWriter x)
{
x.WriteStartArray();
foreach (var d in datas)
{
d.Data.Apply(this, x);
}
x.WriteEndArray();
}
public void Accept(DBool type, BsonDataWriter x)
{
x.WriteValue(type.Value);
}
public void Accept(DByte type, BsonDataWriter x)
{
x.WriteValue(type.Value);
}
public void Accept(DShort type, BsonDataWriter x)
{
x.WriteValue(type.Value);
}
public void Accept(DFshort type, BsonDataWriter x)
{
x.WriteValue(type.Value);
}
public void Accept(DInt type, BsonDataWriter x)
{
x.WriteValue(type.Value);
}
public void Accept(DFint type, BsonDataWriter x)
{
x.WriteValue(type.Value);
}
public void Accept(DLong type, BsonDataWriter x)
{
x.WriteValue(type.Value);
}
public void Accept(DFlong type, BsonDataWriter x)
{
x.WriteValue(type.Value);
}
public void Accept(DFloat type, BsonDataWriter x)
{
x.WriteValue(type.Value);
}
public void Accept(DDouble type, BsonDataWriter x)
{
x.WriteValue(type.Value);
}
public virtual void Accept(DEnum type, BsonDataWriter x)
{
x.WriteValue(type.Value);
}
public void Accept(DString type, BsonDataWriter x)
{
x.WriteValue(type.Value);
}
public void Accept(DBytes type, BsonDataWriter x)
{
throw new NotImplementedException();
}
public virtual void Accept(DText type, BsonDataWriter x)
{
x.WriteStartObject();
x.WritePropertyName(DText.KEY_NAME);
x.WriteValue(type.Key);
x.WritePropertyName(DText.TEXT_NAME);
x.WriteValue(type.TextOfCurrentAssembly);
x.WriteEndObject();
}
public virtual void Accept(DBean type, BsonDataWriter x)
{
x.WriteStartObject();
if (type.Type.IsAbstractType)
{
x.WritePropertyName(DefBean.JSON_TYPE_NAME_KEY);
x.WriteValue(DataUtil.GetImplTypeName(type));
}
var defFields = type.ImplType.HierarchyFields;
int index = 0;
foreach (var d in type.Fields)
{
var defField = (DefField)defFields[index++];
// 特殊处理 bean 多态类型
// 另外,不生成 xxx:null 这样
if (d == null || !defField.NeedExport)
{
//x.WriteNullValue();
}
else
{
x.WritePropertyName(defField.Name);
d.Apply(this, x);
}
}
x.WriteEndObject();
}
public void WriteList(List<DType> datas, BsonDataWriter x)
{
x.WriteStartArray();
foreach (var d in datas)
{
d.Apply(this, x);
}
x.WriteEndArray();
}
public void Accept(DArray type, BsonDataWriter x)
{
WriteList(type.Datas, x);
}
public void Accept(DList type, BsonDataWriter x)
{
WriteList(type.Datas, x);
}
public void Accept(DSet type, BsonDataWriter x)
{
WriteList(type.Datas, x);
}
public virtual void Accept(DMap type, BsonDataWriter x)
{
x.WriteStartArray();
foreach (var d in type.Datas)
{
x.WriteStartArray();
d.Key.Apply(this, x);
d.Value.Apply(this, x);
x.WriteEndArray();
}
x.WriteEndArray();
}
public void Accept(DVector2 type, BsonDataWriter x)
{
x.WriteStartObject();
x.WritePropertyName("x"); x.WriteValue(type.Value.X);
x.WritePropertyName("y"); x.WriteValue(type.Value.Y);
x.WriteEndObject();
}
public void Accept(DVector3 type, BsonDataWriter x)
{
x.WriteStartObject();
x.WritePropertyName("x"); x.WriteValue(type.Value.X);
x.WritePropertyName("y"); x.WriteValue(type.Value.Y);
x.WritePropertyName("z"); x.WriteValue(type.Value.Z);
x.WriteEndObject();
}
public void Accept(DVector4 type, BsonDataWriter x)
{
x.WriteStartObject();
x.WritePropertyName("x"); x.WriteValue(type.Value.X);
x.WritePropertyName("y"); x.WriteValue(type.Value.Y);
x.WritePropertyName("z"); x.WriteValue(type.Value.Z);
x.WritePropertyName("w"); x.WriteValue(type.Value.W);
x.WriteEndObject();
}
public virtual void Accept(DDateTime type, BsonDataWriter x)
{
x.WriteValue(type.UnixTimeOfCurrentAssembly);
}
}
}

View File

@ -10,6 +10,7 @@ namespace Luban.Job.Cfg.Generate
[Render("data_bin")]
[Render("data_json")]
[Render("data_json2")]
[Render("data_bson")]
[Render("data_lua")]
[Render("data_xml")]
[Render("data_yaml")]

View File

@ -11,6 +11,7 @@ using Luban.Job.Common.Tpl;
using Luban.Job.Common.Types;
using Luban.Job.Common.Utils;
using MessagePack;
using Newtonsoft.Json.Bson;
using Scriban;
using System;
using System.Collections.Generic;
@ -79,6 +80,14 @@ namespace Luban.Job.Cfg.Utils
tw.Flush();
return DataUtil.StreamToBytes(ms);
}
case "data_bson":
{
var ss = new MemoryStream();
var bsonWriter = new BsonDataWriter(ss);
BsonExportor.Ins.WriteAsArray(records, bsonWriter);
bsonWriter.Flush();
return DataUtil.StreamToBytes(ss);
}
case "data_xml":
{
var xwSetting = new XmlWriterSettings() { Indent = true };

View File

@ -111,6 +111,7 @@ namespace Luban.Job.Common.Utils
private static readonly Dictionary<string, string> s_name2Suxxifx = new()
{
{ "json", "json" },
{"bson", "bson" },
{ "lua", "lua" },
{ "bin", "bytes" },
{ "xml", "xml" },