[new] 新增xml导出数据格式

main
walon 2022-04-15 17:30:10 +08:00
parent 861f40c29b
commit 3f2b2c71eb
3 changed files with 213 additions and 1 deletions

View File

@ -0,0 +1,202 @@
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 System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace Luban.Job.Cfg.DataExporters
{
public class XmlExportor : IDataActionVisitor<XmlWriter>
{
public static XmlExportor Ins { get; } = new XmlExportor();
public void WriteAsArray(List<Record> datas, XmlWriter w)
{
w.WriteStartDocument();
w.WriteStartElement("table");
foreach (var d in datas)
{
w.WriteStartElement("record");
d.Data.Apply(this, w);
w.WriteEndElement();
}
w.WriteEndElement();
w.WriteEndDocument();
}
public void Accept(DBool type, XmlWriter w)
{
w.WriteValue(type.Value);
}
public void Accept(DByte type, XmlWriter w)
{
w.WriteValue(type.Value);
}
public void Accept(DShort type, XmlWriter w)
{
w.WriteValue(type.Value);
}
public void Accept(DFshort type, XmlWriter w)
{
w.WriteValue(type.Value);
}
public void Accept(DInt type, XmlWriter w)
{
w.WriteValue(type.Value);
}
public void Accept(DFint type, XmlWriter w)
{
w.WriteValue(type.Value);
}
public void Accept(DLong type, XmlWriter w)
{
w.WriteValue(type.Value);
}
public void Accept(DFlong type, XmlWriter w)
{
w.WriteValue(type.Value);
}
public void Accept(DFloat type, XmlWriter w)
{
w.WriteValue(type.Value);
}
public void Accept(DDouble type, XmlWriter w)
{
w.WriteValue(type.Value);
}
public void Accept(DEnum type, XmlWriter w)
{
w.WriteValue(type.Value);
}
public void Accept(DString type, XmlWriter w)
{
w.WriteValue(type.Value);
}
public void Accept(DText type, XmlWriter w)
{
w.WriteElementString(DText.KEY_NAME, type.Key);
w.WriteElementString(DText.TEXT_NAME, type.TextOfCurrentAssembly);
}
public void Accept(DBytes type, XmlWriter w)
{
throw new NotSupportedException();
}
public void Accept(DVector2 type, XmlWriter w)
{
Vector2 v = type.Value;
w.WriteElementString("x", v.X.ToString());
w.WriteElementString("y", v.Y.ToString());
}
public void Accept(DVector3 type, XmlWriter w)
{
Vector3 v = type.Value;
w.WriteElementString("x", v.X.ToString());
w.WriteElementString("y", v.Y.ToString());
w.WriteElementString("z", v.Z.ToString());
}
public void Accept(DVector4 type, XmlWriter w)
{
Vector4 v = type.Value;
w.WriteElementString("x", v.X.ToString());
w.WriteElementString("y", v.Y.ToString());
w.WriteElementString("z", v.Z.ToString());
w.WriteElementString("w", v.W.ToString());
}
public void Accept(DDateTime type, XmlWriter w)
{
w.WriteValue(type.UnixTimeOfCurrentAssembly);
}
public void Accept(DBean type, XmlWriter w)
{
if (type.Type.IsAbstractType)
{
w.WriteAttributeString(DefBean.XML_TYPE_NAME_KEY, 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
{
w.WriteStartElement(defField.Name);
d.Apply(this, w);
w.WriteEndElement();
}
}
}
private void WriteList(List<DType> datas, XmlWriter w)
{
foreach (var d in datas)
{
w.WriteStartElement("ele");
d.Apply(this, w);
w.WriteEndElement();
}
}
public void Accept(DArray type, XmlWriter w)
{
WriteList(type.Datas, w);
}
public void Accept(DList type, XmlWriter w)
{
WriteList(type.Datas, w);
}
public void Accept(DSet type, XmlWriter w)
{
WriteList(type.Datas, w);
}
public void Accept(DMap type, XmlWriter w)
{
foreach (var (k,v) in type.Datas)
{
w.WriteStartElement("ele");
w.WriteStartElement("key");
k.Apply(this, w);
w.WriteEndElement();
w.WriteStartElement("value");
v.Apply(this, w);
w.WriteEndElement();
w.WriteEndElement();
}
}
}
}

View File

@ -8,7 +8,7 @@ namespace Luban.Job.Cfg
[Option('s', "service", Required = true, HelpText = "service")] [Option('s', "service", Required = true, HelpText = "service")]
public string Service { get; set; } public string Service { get; set; }
[Option("gen_types", Required = true, HelpText = "code_cs_bin,code_cs_dotnet_json,code_cs_unity_json,code_cs_unity_editor_json,code_lua_bin,code_java_bin,code_java_json,code_go_bin,code_go_json,code_cpp_bin,code_cpp_ue_editor_json,code_python2_json,code_python3_json,code_typescript_bin,code_typescript_json,code_rust_json,code_protobuf,code_template,code_flatbuffers,data_bin,data_lua,data_json,data_json2,data_json_monolithic,data_yaml,data_resources,data_template,data_protobuf_bin,data_protobuf_json,data_flatbuffers_json,convert_json,convert_lua,convert_xlsx . can be multi")] [Option("gen_types", Required = true, HelpText = "code_cs_bin,code_cs_dotnet_json,code_cs_unity_json,code_cs_unity_editor_json,code_lua_bin,code_java_bin,code_java_json,code_go_bin,code_go_json,code_cpp_bin,code_cpp_ue_editor_json,code_python2_json,code_python3_json,code_typescript_bin,code_typescript_json,code_rust_json,code_protobuf,code_template,code_flatbuffers,data_bin,data_lua,data_json,data_json2,data_json_monolithic,data_yaml,data_xml,data_resources,data_template,data_protobuf_bin,data_protobuf_json,data_flatbuffers_json,convert_json,convert_lua,convert_xlsx . can be multi")]
public string GenType { get; set; } public string GenType { get; set; }
[Option("input_data_dir", Required = true, HelpText = "input data dir")] [Option("input_data_dir", Required = true, HelpText = "input data dir")]

View File

@ -18,6 +18,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Xml;
using YamlDotNet.RepresentationModel; using YamlDotNet.RepresentationModel;
namespace Luban.Job.Cfg.Utils namespace Luban.Job.Cfg.Utils
@ -78,6 +79,15 @@ namespace Luban.Job.Cfg.Utils
tw.Flush(); tw.Flush();
return DataUtil.StreamToBytes(ms); return DataUtil.StreamToBytes(ms);
} }
case "data_xml":
{
var xwSetting = new XmlWriterSettings() { Indent = true };
var ms = new MemoryStream();
using var xmlWriter = XmlWriter.Create(ms, xwSetting);
XmlExportor.Ins.WriteAsArray(records, xmlWriter);
xmlWriter.Flush();
return DataUtil.StreamToBytes(ms);
}
case "data_lua": case "data_lua":
{ {
var content = new StringBuilder(); var content = new StringBuilder();