diff --git a/src/Luban.Job.Cfg/Source/DataExporters/YamlExportor.cs b/src/Luban.Job.Cfg/Source/DataExporters/YamlExportor.cs new file mode 100644 index 0000000..aa050e3 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/DataExporters/YamlExportor.cs @@ -0,0 +1,207 @@ +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 YamlDotNet.Core; +using YamlDotNet.RepresentationModel; +using YamlDotNet.Serialization; + +namespace Luban.Job.Cfg.DataExporters +{ + class YamlExportor : IDataFuncVisitor + { + public static YamlExportor Ins { get; } = new YamlExportor(); + + public YamlNode WriteAsArray(List datas) + { + + var seqNode = new YamlSequenceNode(); + foreach (var d in datas) + { + seqNode.Add(d.Data.Apply(this)); + } + return seqNode; + } + + + private static YamlScalarNode ToPlainNode(string x) + { + return new YamlScalarNode(x) { Style = ScalarStyle.Plain }; + } + + public YamlNode Accept(DBool type) + { + return ToPlainNode(type.Value.ToString()); + } + + public YamlNode Accept(DByte type) + { + return ToPlainNode(type.Value.ToString()); + } + + public YamlNode Accept(DShort type) + { + return ToPlainNode(type.Value.ToString()); + } + + public YamlNode Accept(DFshort type) + { + return ToPlainNode(type.Value.ToString()); + } + + public YamlNode Accept(DInt type) + { + return ToPlainNode(type.Value.ToString()); + } + + public YamlNode Accept(DFint type) + { + return ToPlainNode(type.Value.ToString()); + } + + public YamlNode Accept(DLong type) + { + return ToPlainNode(type.Value.ToString()); + } + + public YamlNode Accept(DFlong type) + { + return ToPlainNode(type.Value.ToString()); + } + + public YamlNode Accept(DFloat type) + { + return ToPlainNode(type.Value.ToString()); + } + + public YamlNode Accept(DDouble type) + { + return ToPlainNode(type.Value.ToString()); + } + + public YamlNode Accept(DEnum type) + { + return ToPlainNode(type.Value.ToString()); + } + + public YamlNode Accept(DString type) + { + return new YamlScalarNode(type.Value) { Style = ScalarStyle.SingleQuoted }; + } + + public YamlNode Accept(DBytes type) + { + throw new NotSupportedException(); + } + + public YamlNode Accept(DText type) + { + var m = new YamlMappingNode(); + m.Add(DText.KEY_NAME, type.Key); + m.Add(DText.TEXT_NAME, type.TextOfCurrentAssembly); + return m; + } + + public YamlNode Accept(DBean type) + { + var m = new YamlMappingNode(); + + if (type.Type.IsAbstractType) + { + m.Add(DefBean.JSON_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 + { + m.Add(defField.Name, d.Apply(this)); + } + } + + return m; + } + + public YamlSequenceNode ToSeqNode(List datas) + { + var seqNode = new YamlSequenceNode(); + foreach (var d in datas) + { + seqNode.Add(d.Apply(this)); + } + return seqNode; + } + + public YamlNode Accept(DArray type) + { + return ToSeqNode(type.Datas); + } + + public YamlNode Accept(DList type) + { + return ToSeqNode(type.Datas); + } + + public YamlNode Accept(DSet type) + { + return ToSeqNode(type.Datas); + } + + public YamlNode Accept(DMap type) + { + var seqNode = new YamlSequenceNode(); + foreach (var d in type.Datas) + { + var e = new YamlSequenceNode(); + e.Add(d.Key.Apply(this)); + e.Add(d.Value.Apply(this)); + seqNode.Add(e); + } + return seqNode; + } + + public YamlNode Accept(DVector2 type) + { + var m = new YamlMappingNode(); + m.Add("x", type.Value.X.ToString()); + m.Add("y", type.Value.Y.ToString()); + return m; + } + + public YamlNode Accept(DVector3 type) + { + var m = new YamlMappingNode(); + m.Add("x", type.Value.X.ToString()); + m.Add("y", type.Value.Y.ToString()); + m.Add("z", type.Value.Z.ToString()); + return m; + } + + public YamlNode Accept(DVector4 type) + { + var m = new YamlMappingNode(); + m.Add("x", type.Value.X.ToString()); + m.Add("y", type.Value.Y.ToString()); + m.Add("z", type.Value.Z.ToString()); + m.Add("w", type.Value.W.ToString()); + return m; + } + + public YamlNode Accept(DDateTime type) + { + return ToPlainNode(type.UnixTimeOfCurrentAssembly.ToString()); + } + } +} diff --git a/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs b/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs index 1e0ce38..ccfd992 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 YamlDotNet.RepresentationModel; namespace Luban.Job.Cfg.Utils { @@ -66,6 +67,17 @@ namespace Luban.Job.Cfg.Utils jsonWriter.Flush(); return DataUtil.StreamToBytes(ss); } + case "data_yaml": + { + var node = YamlExportor.Ins.WriteAsArray(records); + var ys = new YamlStream(new YamlDocument(node)); + + var ms = new MemoryStream(); + var tw = new StreamWriter(ms); + ys.Save(tw, false); + tw.Flush(); + return DataUtil.StreamToBytes(ms); + } case "data_lua": { var content = new StringBuilder();