diff --git a/src/Luban.Job.Cfg/Source/DataExporters/XmlExportor.cs b/src/Luban.Job.Cfg/Source/DataExporters/XmlExportor.cs new file mode 100644 index 0000000..082f1b5 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/DataExporters/XmlExportor.cs @@ -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 + { + public static XmlExportor Ins { get; } = new XmlExportor(); + + public void WriteAsArray(List 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 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(); + } + } + } +} diff --git a/src/Luban.Job.Cfg/Source/GenArgs.cs b/src/Luban.Job.Cfg/Source/GenArgs.cs index e753575..2919abf 100644 --- a/src/Luban.Job.Cfg/Source/GenArgs.cs +++ b/src/Luban.Job.Cfg/Source/GenArgs.cs @@ -8,7 +8,7 @@ namespace Luban.Job.Cfg [Option('s', "service", Required = true, HelpText = "service")] 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; } [Option("input_data_dir", Required = true, HelpText = "input data dir")] diff --git a/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs b/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs index 470f950..fd91a17 100644 --- a/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs +++ b/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs @@ -18,6 +18,7 @@ using System.IO; using System.Linq; using System.Text; using System.Text.Json; +using System.Xml; using YamlDotNet.RepresentationModel; namespace Luban.Job.Cfg.Utils @@ -78,6 +79,15 @@ namespace Luban.Job.Cfg.Utils tw.Flush(); 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": { var content = new StringBuilder();