【特性】新增转换到json和lua的源数据格式(注意,转换格式不同于导出格式)
parent
8ad27e8275
commit
96ce3443d5
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<DType> 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -71,7 +71,7 @@ namespace Luban.Job.Cfg.DataExporters
|
||||||
x.WriteNumberValue(type.Value);
|
x.WriteNumberValue(type.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Accept(DEnum type, Utf8JsonWriter x)
|
public virtual void Accept(DEnum type, Utf8JsonWriter x)
|
||||||
{
|
{
|
||||||
x.WriteNumberValue(type.Value);
|
x.WriteNumberValue(type.Value);
|
||||||
}
|
}
|
||||||
|
|
@ -86,7 +86,7 @@ namespace Luban.Job.Cfg.DataExporters
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Accept(DText type, Utf8JsonWriter x)
|
public virtual void Accept(DText type, Utf8JsonWriter x)
|
||||||
{
|
{
|
||||||
x.WriteStartObject();
|
x.WriteStartObject();
|
||||||
x.WritePropertyName(DText.KEY_NAME);
|
x.WritePropertyName(DText.KEY_NAME);
|
||||||
|
|
@ -97,7 +97,7 @@ namespace Luban.Job.Cfg.DataExporters
|
||||||
x.WriteEndObject();
|
x.WriteEndObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Accept(DBean type, Utf8JsonWriter x)
|
public virtual void Accept(DBean type, Utf8JsonWriter x)
|
||||||
{
|
{
|
||||||
x.WriteStartObject();
|
x.WriteStartObject();
|
||||||
|
|
||||||
|
|
@ -193,7 +193,7 @@ namespace Luban.Job.Cfg.DataExporters
|
||||||
x.WriteEndObject();
|
x.WriteEndObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Accept(DDateTime type, Utf8JsonWriter x)
|
public virtual void Accept(DDateTime type, Utf8JsonWriter x)
|
||||||
{
|
{
|
||||||
x.WriteNumberValue(type.GetUnixTime(DefAssembly.LocalAssebmly.TimeZone));
|
x.WriteNumberValue(type.GetUnixTime(DefAssembly.LocalAssebmly.TimeZone));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ namespace Luban.Job.Cfg.DataVisitors
|
||||||
return type.Value.ToString();
|
return type.Value.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Accept(DEnum type)
|
public virtual string Accept(DEnum type)
|
||||||
{
|
{
|
||||||
return type.Value.ToString();
|
return type.Value.ToString();
|
||||||
}
|
}
|
||||||
|
|
@ -89,7 +89,7 @@ namespace Luban.Job.Cfg.DataVisitors
|
||||||
|
|
||||||
public abstract string Accept(DVector4 type);
|
public abstract string Accept(DVector4 type);
|
||||||
|
|
||||||
public string Accept(DDateTime type)
|
public virtual string Accept(DDateTime type)
|
||||||
{
|
{
|
||||||
var ass = DefAssembly.LocalAssebmly as DefAssembly;
|
var ass = DefAssembly.LocalAssebmly as DefAssembly;
|
||||||
return type.GetUnixTime(ass.TimeZone).ToString();
|
return type.GetUnixTime(ass.TimeZone).ToString();
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ namespace Luban.Job.Cfg.DataVisitors
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected void Append(List<DType> datas, StringBuilder x)
|
private void Append(List<DType> datas, StringBuilder x)
|
||||||
{
|
{
|
||||||
x.Append('{');
|
x.Append('{');
|
||||||
foreach (var e in datas)
|
foreach (var e in datas)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using Luban.Job.Cfg.DataVisitors;
|
using Luban.Job.Cfg.DataVisitors;
|
||||||
|
using Luban.Job.Cfg.Utils;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Luban.Job.Cfg.Datas
|
namespace Luban.Job.Cfg.Datas
|
||||||
|
|
@ -30,6 +31,11 @@ namespace Luban.Job.Cfg.Datas
|
||||||
return _localTime.GetHashCode();
|
return _localTime.GetHashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string ToFormatString()
|
||||||
|
{
|
||||||
|
return DataUtil.FormatDateTime(Time);
|
||||||
|
}
|
||||||
|
|
||||||
public int GetUnixTime(TimeZoneInfo asTimeZone)
|
public int GetUnixTime(TimeZoneInfo asTimeZone)
|
||||||
{
|
{
|
||||||
if (asTimeZone == null || asTimeZone == TimeZoneInfo.Local)
|
if (asTimeZone == null || asTimeZone == TimeZoneInfo.Local)
|
||||||
|
|
|
||||||
|
|
@ -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 });
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using Bright.Serialization;
|
using Bright.Serialization;
|
||||||
|
using Luban.Job.Cfg.DataConverts;
|
||||||
using Luban.Job.Cfg.DataExporters;
|
using Luban.Job.Cfg.DataExporters;
|
||||||
using Luban.Job.Cfg.Datas;
|
using Luban.Job.Cfg.Datas;
|
||||||
using Luban.Job.Cfg.DataVisitors;
|
using Luban.Job.Cfg.DataVisitors;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue