diff --git a/src/Luban.Job.Cfg/Source/DataConverts/JsonConvertor.cs b/src/Luban.Job.Cfg/Source/DataConverts/JsonConvertor.cs new file mode 100644 index 0000000..f64ccfc --- /dev/null +++ b/src/Luban.Job.Cfg/Source/DataConverts/JsonConvertor.cs @@ -0,0 +1,69 @@ +using Luban.Job.Cfg.Datas; +using Luban.Job.Cfg.Defs; +using Luban.Job.Cfg.Utils; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace Luban.Job.Cfg.DataConverts +{ + class JsonConvertor : DataExporters.JsonExportor + { + public static new JsonConvertor Ins { get; } = new(); + + public override void Accept(DText type, Utf8JsonWriter x) + { + x.WriteStartObject(); + x.WritePropertyName(DText.KEY_NAME); + x.WriteStringValue(type.Key); + x.WritePropertyName(DText.TEXT_NAME); + x.WriteStringValue(type.RawValue); + x.WriteEndObject(); + } + + public override void Accept(DEnum type, Utf8JsonWriter x) + { + x.WriteStringValue(type.StrValue); + } + + public override void Accept(DBean type, Utf8JsonWriter x) + { + x.WriteStartObject(); + + if (type.Type.IsAbstractType) + { + x.WritePropertyName(DefBean.TYPE_NAME_KEY); + x.WriteStringValue(type.ImplType.Name); + } + + 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) + { + //x.WriteNullValue(); + } + else + { + x.WritePropertyName(defField.Name); + d.Apply(this, x); + } + } + x.WriteEndObject(); + } + + + public override void Accept(DDateTime type, Utf8JsonWriter x) + { + x.WriteStringValue(type.ToFormatString()); + } + } +} diff --git a/src/Luban.Job.Cfg/Source/DataConverts/LuaConvertor.cs b/src/Luban.Job.Cfg/Source/DataConverts/LuaConvertor.cs new file mode 100644 index 0000000..ad8791e --- /dev/null +++ b/src/Luban.Job.Cfg/Source/DataConverts/LuaConvertor.cs @@ -0,0 +1,128 @@ +using Luban.Job.Cfg.Datas; +using Luban.Job.Cfg.Defs; +using Luban.Job.Cfg.Utils; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Cfg.DataConverts +{ + class LuaConvertor : DataVisitors.ToLuaLiteralVisitor + { + //public static new LuaConvertor Ins { get; } = new(); + + private string _indentStr = ""; + + private const string INDENT_STEP = " "; + + public string ExportRecord(DefTable t, Record record) + { + return "return " + record.Data.Apply(this); + } + + public override string Accept(DText type) + { + return $"{{{DText.KEY_NAME}='{type.Key}',{DText.TEXT_NAME}=\"{DataUtil.EscapeString(type.RawValue)}\"}}"; + } + + public override string Accept(DEnum type) + { + return type.Value.ToString(); + } + + public override string Accept(DBean type) + { + string curIndent = _indentStr; + string subIndent = _indentStr + INDENT_STEP; + _indentStr = subIndent; + + var x = new StringBuilder(); + x.AppendLine("{"); + if (type.Type.IsAbstractType) + { + x.Append(subIndent).AppendLine($"_name = '{type.ImplType.Name}',"); + } + + int index = 0; + foreach (var f in type.Fields) + { + var defField = (DefField)type.ImplType.HierarchyFields[index++]; + if (f == null) + { + continue; + } + x.Append(subIndent); + x.Append(defField.Name).Append(" = "); + x.Append(f.Apply(this)); + x.AppendLine(","); + } + x.Append(curIndent).Append("}"); + _indentStr = curIndent; + return x.ToString(); + } + + protected void AppendToString(List datas, StringBuilder x) + { + string curIndent = _indentStr; + _indentStr += INDENT_STEP; + x.Append("{"); + foreach (var e in datas) + { + x.AppendLine(); + x.Append(_indentStr).Append(e.Apply(this)); + x.Append(","); + } + + x.AppendLine().Append(curIndent).Append("}"); + _indentStr = curIndent; + } + + public override string Accept(DArray type) + { + var x = new StringBuilder(); + AppendToString(type.Datas, x); + return x.ToString(); + } + + public override string Accept(DList type) + { + var x = new StringBuilder(); + AppendToString(type.Datas, x); + return x.ToString(); + } + + public override string Accept(DSet type) + { + var x = new StringBuilder(); + AppendToString(type.Datas, x); + return x.ToString(); + } + + public override string Accept(DMap type) + { + string curIndent = _indentStr; + _indentStr += INDENT_STEP; + var x = new StringBuilder(); + x.AppendLine("{"); + foreach (var e in type.Datas) + { + x.Append(_indentStr).Append('['); + x.Append(e.Key.Apply(this)); + x.Append(']'); + x.Append(" = "); + x.Append(e.Value.Apply(this)); + x.AppendLine(","); + } + x.Append(curIndent).AppendLine("}"); + _indentStr = curIndent; + return x.ToString(); + } + + public override string Accept(DDateTime type) + { + return type.ToFormatString(); + } + } +} diff --git a/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs b/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs index e7c7171..774f90d 100644 --- a/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs +++ b/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs @@ -71,7 +71,7 @@ namespace Luban.Job.Cfg.DataExporters x.WriteNumberValue(type.Value); } - public void Accept(DEnum type, Utf8JsonWriter x) + public virtual void Accept(DEnum type, Utf8JsonWriter x) { x.WriteNumberValue(type.Value); } @@ -86,7 +86,7 @@ namespace Luban.Job.Cfg.DataExporters throw new NotImplementedException(); } - public void Accept(DText type, Utf8JsonWriter x) + public virtual void Accept(DText type, Utf8JsonWriter x) { x.WriteStartObject(); x.WritePropertyName(DText.KEY_NAME); @@ -97,7 +97,7 @@ namespace Luban.Job.Cfg.DataExporters x.WriteEndObject(); } - public void Accept(DBean type, Utf8JsonWriter x) + public virtual void Accept(DBean type, Utf8JsonWriter x) { x.WriteStartObject(); @@ -193,7 +193,7 @@ namespace Luban.Job.Cfg.DataExporters x.WriteEndObject(); } - public void Accept(DDateTime type, Utf8JsonWriter x) + public virtual void Accept(DDateTime type, Utf8JsonWriter x) { x.WriteNumberValue(type.GetUnixTime(DefAssembly.LocalAssebmly.TimeZone)); } diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToLiteralVisitorBase.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToLiteralVisitorBase.cs index 43f7b10..e11f35f 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/ToLiteralVisitorBase.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToLiteralVisitorBase.cs @@ -56,7 +56,7 @@ namespace Luban.Job.Cfg.DataVisitors return type.Value.ToString(); } - public string Accept(DEnum type) + public virtual string Accept(DEnum type) { return type.Value.ToString(); } @@ -89,7 +89,7 @@ namespace Luban.Job.Cfg.DataVisitors public abstract string Accept(DVector4 type); - public string Accept(DDateTime type) + public virtual string Accept(DDateTime type) { var ass = DefAssembly.LocalAssebmly as DefAssembly; return type.GetUnixTime(ass.TimeZone).ToString(); diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToLuaLiteralVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToLuaLiteralVisitor.cs index 6557d26..6eaf58a 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/ToLuaLiteralVisitor.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToLuaLiteralVisitor.cs @@ -45,7 +45,7 @@ namespace Luban.Job.Cfg.DataVisitors } - protected void Append(List datas, StringBuilder x) + private void Append(List datas, StringBuilder x) { x.Append('{'); foreach (var e in datas) diff --git a/src/Luban.Job.Cfg/Source/Datas/DDateTime.cs b/src/Luban.Job.Cfg/Source/Datas/DDateTime.cs index 0ec3089..02ae769 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DDateTime.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DDateTime.cs @@ -1,4 +1,5 @@ using Luban.Job.Cfg.DataVisitors; +using Luban.Job.Cfg.Utils; using System; namespace Luban.Job.Cfg.Datas @@ -30,6 +31,11 @@ namespace Luban.Job.Cfg.Datas return _localTime.GetHashCode(); } + public string ToFormatString() + { + return DataUtil.FormatDateTime(Time); + } + public int GetUnixTime(TimeZoneInfo asTimeZone) { if (asTimeZone == null || asTimeZone == TimeZoneInfo.Local) diff --git a/src/Luban.Job.Cfg/Source/Generate/ConvertRender.cs b/src/Luban.Job.Cfg/Source/Generate/ConvertRender.cs new file mode 100644 index 0000000..e028937 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/Generate/ConvertRender.cs @@ -0,0 +1,41 @@ +using Luban.Common.Protos; +using Luban.Job.Cfg.Cache; +using Luban.Job.Cfg.Utils; +using Luban.Job.Common.Utils; +using System.Threading.Tasks; + +namespace Luban.Job.Cfg.Generate +{ + [Render("convert_json")] + [Render("convert_lua")] + class ConvertRender : DataRenderBase + { + public override void Render(GenContext ctx) + { + string genType = ctx.GenType; + foreach (var table in ctx.ExportTables) + { + var records = ctx.Assembly.GetTableAllDataList(table); + int index = 0; + string dirName = table.FullName; + foreach (var record in records) + { + var fileName = table.IsMapTable ? + record.Data.GetField(table.IndexField.Name).ToString().Replace("\"", "").Replace("'", "") + : (++index).ToString(); + var file = RenderFileUtil.GetOutputFileName(genType, $"{dirName}/{fileName}", ctx.GenArgs.DataFileExtension); + ctx.Tasks.Add(Task.Run(() => + { + //if (!FileRecordCacheManager.Ins.TryGetRecordOutputData(table, records, genType, out string md5)) + //{ + var content = DataConvertUtil.ToConvertRecord(table, record, genType); + var md5 = CacheFileUtil.GenStringOrBytesMd5AndAddCache(file, content); + FileRecordCacheManager.Ins.AddCachedRecordOutputData(table, records, genType, md5); + //} + ctx.GenDataFilesInOutputDataDir.Add(new FileInfo() { FilePath = file, MD5 = md5 }); + })); + } + } + } + } +} diff --git a/src/Luban.Job.Cfg/Source/Utils/DataConvertUtil.cs b/src/Luban.Job.Cfg/Source/Utils/DataConvertUtil.cs new file mode 100644 index 0000000..aff923a --- /dev/null +++ b/src/Luban.Job.Cfg/Source/Utils/DataConvertUtil.cs @@ -0,0 +1,71 @@ +using Luban.Job.Cfg.DataConverts; +using Luban.Job.Cfg.Datas; +using Luban.Job.Cfg.Defs; +using Luban.Job.Cfg.Utils; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace Luban.Job.Cfg.Utils +{ + static class DataConvertUtil + { + public static object ToConvertRecord(DefTable table, Record record, string converType) + { + switch (converType) + { + case "convert_json": + { + // data_json与data_json2格式区别在于 + // data_json的map格式是 [[key1,value1],[] ..] + // data_json2的map格式是 { key1:value1, ...} + var ss = new MemoryStream(); + var jsonWriter = new Utf8JsonWriter(ss, new JsonWriterOptions() + { + Indented = true, + SkipValidation = false, + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(System.Text.Unicode.UnicodeRanges.All), + }); + record.Data.Apply(JsonConvertor.Ins, jsonWriter); + jsonWriter.Flush(); + return System.Text.Encoding.UTF8.GetString(DataUtil.StreamToBytes(ss)); + } + case "convert_lua": + { + return new LuaConvertor().ExportRecord(table, record); + } + //case "data_erlang": + //{ + // var content = new StringBuilder(); + // switch (table.Mode) + // { + // case ETableMode.ONE: + // { + // ErlangExport.Ins.ExportTableSingleton(table, records[0], content); + // break; + // } + // case ETableMode.MAP: + // { + // ErlangExport.Ins.ExportTableMap(table, records, content); + // break; + // } + // default: + // { + // throw new NotSupportedException(); + // } + // } + // return content.ToString(); + //} + default: + { + throw new ArgumentException($"not support datatype:{converType}"); + } + } + } + + } +} diff --git a/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs b/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs index 25aa1d4..dc97170 100644 --- a/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs +++ b/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs @@ -1,4 +1,5 @@ using Bright.Serialization; +using Luban.Job.Cfg.DataConverts; using Luban.Job.Cfg.DataExporters; using Luban.Job.Cfg.Datas; using Luban.Job.Cfg.DataVisitors;