【重构】重构json和lua转化为literal的代码

main
walon 2021-08-28 10:07:41 +08:00
parent 47ced0fbbb
commit 125504e5a7
12 changed files with 600 additions and 209 deletions

View File

@ -0,0 +1,130 @@
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.Defs;
using Luban.Job.Cfg.Utils;
using System.Collections.Generic;
using System.Text;
namespace Luban.Job.Cfg.DataVisitors
{
class ToErlangLiteralVisitor : ToLiteralVisitorBase
{
public static ToErlangLiteralVisitor Ins { get; } = new();
public override string Accept(DText type)
{
var ass = DefAssembly.LocalAssebmly as DefAssembly;
return $"{{\"{DText.KEY_NAME}\":\"{type.Key}\",\"{DText.TEXT_NAME}\":\"{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}\"}}";
}
public override string Accept(DBean type)
{
var x = new StringBuilder();
var bean = type.ImplType;
if (bean.IsAbstractType)
{
x.Append($"{{ \"_name\":\"{type.ImplType.Name}\",");
}
else
{
x.Append('{');
}
int index = 0;
foreach (var f in type.Fields)
{
if (index >= 1)
{
x.Append(',');
}
var defField = type.ImplType.HierarchyFields[index++];
x.Append('\"').Append(defField.Name).Append('\"').Append(':');
if (f != null)
{
x.Append(f.Apply(this));
}
else
{
x.Append("null");
}
}
x.Append('}');
return x.ToString();
}
protected void Append(List<DType> datas, StringBuilder x)
{
x.Append('[');
int index = 0;
foreach (var e in datas)
{
if (index > 0)
{
x.Append(',');
}
++index;
x.Append(e.Apply(this));
}
x.Append(']');
}
public override string Accept(DArray type)
{
var x = new StringBuilder();
Append(type.Datas, x);
return x.ToString();
}
public override string Accept(DList type)
{
var x = new StringBuilder();
Append(type.Datas, x);
return x.ToString();
}
public override string Accept(DSet type)
{
var x = new StringBuilder();
Append(type.Datas, x);
return x.ToString();
}
public override string Accept(DMap type)
{
var x = new StringBuilder();
x.Append('{');
int index = 0;
foreach (var e in type.Datas)
{
if (index > 0)
{
x.Append(',');
}
++index;
x.Append('"').Append(e.Key.ToString()).Append('"');
x.Append(':');
x.Append(e.Value.Apply(this));
}
x.Append('}');
return x.ToString();
}
public override string Accept(DVector2 type)
{
var v = type.Value;
return $"{{\"x\":{v.X},\"y\":{v.Y}}}";
}
public override string Accept(DVector3 type)
{
var v = type.Value;
return $"{{\"x\":{v.X},\"y\":{v.Y},\"z\":{v.Z}}}";
}
public override string Accept(DVector4 type)
{
var v = type.Value;
return $"{{\"x\":{v.X},\"y\":{v.Y},\"z\":{v.Z},\"w\":{v.W}}}";
}
}
}

View File

@ -0,0 +1,130 @@
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.Defs;
using Luban.Job.Cfg.Utils;
using System.Collections.Generic;
using System.Text;
namespace Luban.Job.Cfg.DataVisitors
{
class ToJsonLiteralVisitor : ToLiteralVisitorBase
{
public static ToJsonLiteralVisitor Ins { get; } = new();
public override string Accept(DText type)
{
var ass = DefAssembly.LocalAssebmly as DefAssembly;
return $"{{\"{DText.KEY_NAME}\":\"{type.Key}\",\"{DText.TEXT_NAME}\":\"{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}\"}}";
}
public override string Accept(DBean type)
{
var x = new StringBuilder();
var bean = type.ImplType;
if (bean.IsAbstractType)
{
x.Append($"{{ \"_name\":\"{type.ImplType.Name}\",");
}
else
{
x.Append('{');
}
int index = 0;
foreach (var f in type.Fields)
{
if (index >= 1)
{
x.Append(',');
}
var defField = type.ImplType.HierarchyExportFields[index++];
x.Append('\"').Append(defField.Name).Append('\"').Append(':');
if (f != null)
{
x.Append(f.Apply(this));
}
else
{
x.Append("null");
}
}
x.Append('}');
return x.ToString();
}
protected virtual void Append(List<DType> datas, StringBuilder x)
{
x.Append('[');
int index = 0;
foreach (var e in datas)
{
if (index > 0)
{
x.Append(',');
}
++index;
x.Append(e.Apply(this));
}
x.Append(']');
}
public override string Accept(DArray type)
{
var x = new StringBuilder();
Append(type.Datas, x);
return x.ToString();
}
public override string Accept(DList type)
{
var x = new StringBuilder();
Append(type.Datas, x);
return x.ToString();
}
public override string Accept(DSet type)
{
var x = new StringBuilder();
Append(type.Datas, x);
return x.ToString();
}
public override string Accept(DMap type)
{
var x = new StringBuilder();
x.Append('{');
int index = 0;
foreach (var e in type.Datas)
{
if (index > 0)
{
x.Append(',');
}
++index;
x.Append('"').Append(e.Key.Apply(ToJsonPropertyNameVisitor.Ins)).Append('"');
x.Append(':');
x.Append(e.Value.Apply(this));
}
x.Append('}');
return x.ToString();
}
public override string Accept(DVector2 type)
{
var v = type.Value;
return $"{{\"x\":{v.X},\"y\":{v.Y}}}";
}
public override string Accept(DVector3 type)
{
var v = type.Value;
return $"{{\"x\":{v.X},\"y\":{v.Y},\"z\":{v.Z}}}";
}
public override string Accept(DVector4 type)
{
var v = type.Value;
return $"{{\"x\":{v.X},\"y\":{v.Y},\"z\":{v.Z},\"w\":{v.W}}}";
}
}
}

View File

@ -0,0 +1,100 @@
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.Defs;
using Luban.Job.Cfg.Utils;
using System.Collections.Generic;
using System.Text;
namespace Luban.Job.Cfg.DataVisitors
{
abstract class ToLiteralVisitorBase : IDataFuncVisitor<string>
{
public string Accept(DBool type)
{
return type.Value ? "true" : "false";
}
public string Accept(DByte type)
{
return type.Value.ToString();
}
public string Accept(DShort type)
{
return type.Value.ToString();
}
public string Accept(DFshort type)
{
return type.Value.ToString();
}
public string Accept(DInt type)
{
return type.Value.ToString();
}
public string Accept(DFint type)
{
return type.Value.ToString();
}
public string Accept(DLong type)
{
return type.Value.ToString();
}
public string Accept(DFlong type)
{
return type.Value.ToString();
}
public string Accept(DFloat type)
{
return type.Value.ToString();
}
public string Accept(DDouble type)
{
return type.Value.ToString();
}
public string Accept(DEnum type)
{
return type.Value.ToString();
}
public virtual string Accept(DString type)
{
return "\"" + DataUtil.EscapeString(type.Value) + "\"";
}
public string Accept(DBytes type)
{
throw new System.NotSupportedException();
}
public abstract string Accept(DText type);
public abstract string Accept(DBean type);
public abstract string Accept(DArray type);
public abstract string Accept(DList type);
public abstract string Accept(DSet type);
public abstract string Accept(DMap type);
public abstract string Accept(DVector2 type);
public abstract string Accept(DVector3 type);
public abstract string Accept(DVector4 type);
public string Accept(DDateTime type)
{
var ass = DefAssembly.LocalAssebmly as DefAssembly;
return type.GetUnixTime(ass.TimeZone).ToString();
}
}
}

View File

@ -0,0 +1,119 @@
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.Defs;
using Luban.Job.Cfg.Utils;
using System.Collections.Generic;
using System.Text;
namespace Luban.Job.Cfg.DataVisitors
{
class ToLuaLiteralVisitor : ToLiteralVisitorBase
{
public static ToLuaLiteralVisitor Ins { get; } = new();
public override string Accept(DText type)
{
var ass = DefAssembly.LocalAssebmly as DefAssembly;
return $"{{{DText.KEY_NAME}='{type.Key}',{DText.TEXT_NAME}=\"{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}\"}}";
}
public override string Accept(DBean type)
{
var x = new StringBuilder();
var bean = type.ImplType;
if (bean.IsAbstractType)
{
x.Append($"{{ _name='{type.ImplType.Name}',");
}
else
{
x.Append('{');
}
int index = 0;
foreach (var f in type.Fields)
{
var defField = type.ImplType.HierarchyExportFields[index++];
x.Append(defField.Name).Append('=');
if (f != null)
{
x.Append(f.Apply(this));
}
else
{
x.Append("nil");
}
x.Append(',');
}
x.Append('}');
return x.ToString();
}
protected void Append(List<DType> datas, StringBuilder x)
{
x.Append('{');
foreach (var e in datas)
{
x.Append(e.Apply(this));
x.Append(',');
}
x.Append('}');
}
public override string Accept(DArray type)
{
var x = new StringBuilder();
Append(type.Datas, x);
return x.ToString();
}
public override string Accept(DList type)
{
var x = new StringBuilder();
Append(type.Datas, x);
return x.ToString();
}
public override string Accept(DSet type)
{
var x = new StringBuilder();
Append(type.Datas, x);
return x.ToString();
}
public override string Accept(DMap type)
{
var x = new StringBuilder();
x.Append('{');
foreach (var e in type.Datas)
{
x.Append('[');
x.Append(e.Key.Apply(this));
x.Append(']');
x.Append('=');
x.Append(e.Value.Apply(this));
x.Append(',');
}
x.Append('}');
return x.ToString();
}
public override string Accept(DVector2 type)
{
var v = type.Value;
return $"{{x={v.X},y={v.Y}}}";
}
public override string Accept(DVector3 type)
{
var v = type.Value;
return $"{{x={v.X},y={v.Y},z={v.Z}}}";
}
public override string Accept(DVector4 type)
{
var v = type.Value;
return $"{{x={v.X},y={v.Y},z={v.Z},w={v.W}}}";
}
}
}

View File

@ -6,82 +6,17 @@ using System.Text;
namespace Luban.Job.Cfg.DataVisitors
{
class ToJsonStringVisitor : IDataFuncVisitor<string>
class ToPythonLiteralVisitor : ToLiteralVisitorBase
{
public static ToJsonStringVisitor Ins { get; } = new();
public static ToPythonLiteralVisitor Ins { get; } = new();
public string Accept(DBool type)
{
return type.Value ? "true" : "false";
}
public string Accept(DByte type)
{
return type.Value.ToString();
}
public string Accept(DShort type)
{
return type.Value.ToString();
}
public string Accept(DFshort type)
{
return type.Value.ToString();
}
public string Accept(DInt type)
{
return type.Value.ToString();
}
public string Accept(DFint type)
{
return type.Value.ToString();
}
public string Accept(DLong type)
{
return type.Value.ToString();
}
public string Accept(DFlong type)
{
return type.Value.ToString();
}
public string Accept(DFloat type)
{
return type.Value.ToString();
}
public string Accept(DDouble type)
{
return type.Value.ToString();
}
public string Accept(DEnum type)
{
return type.Value.ToString();
}
public string Accept(DString type)
{
return "\"" + DataUtil.EscapeString(type.Value) + "\"";
}
public string Accept(DBytes type)
{
throw new System.NotSupportedException();
}
public virtual string Accept(DText type)
public override string Accept(DText type)
{
var ass = DefAssembly.LocalAssebmly as DefAssembly;
return $"{{\"{DText.KEY_NAME}\":\"{type.Key}\",\"{DText.TEXT_NAME}\":\"{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}\"}}";
}
public virtual string Accept(DBean type)
public override string Accept(DBean type)
{
var x = new StringBuilder();
var bean = type.ImplType;
@ -101,7 +36,7 @@ namespace Luban.Job.Cfg.DataVisitors
{
x.Append(',');
}
var defField = type.ImplType.HierarchyFields[index++];
var defField = type.ImplType.HierarchyExportFields[index++];
x.Append('\"').Append(defField.Name).Append('\"').Append(':');
if (f != null)
{
@ -133,28 +68,28 @@ namespace Luban.Job.Cfg.DataVisitors
x.Append(']');
}
public string Accept(DArray type)
public override string Accept(DArray type)
{
var x = new StringBuilder();
Append(type.Datas, x);
return x.ToString();
}
public string Accept(DList type)
public override string Accept(DList type)
{
var x = new StringBuilder();
Append(type.Datas, x);
return x.ToString();
}
public string Accept(DSet type)
public override string Accept(DSet type)
{
var x = new StringBuilder();
Append(type.Datas, x);
return x.ToString();
}
public virtual string Accept(DMap type)
public override string Accept(DMap type)
{
var x = new StringBuilder();
x.Append('{');
@ -174,28 +109,22 @@ namespace Luban.Job.Cfg.DataVisitors
return x.ToString();
}
public virtual string Accept(DVector2 type)
public override string Accept(DVector2 type)
{
var v = type.Value;
return $"{{\"x\":{v.X},\"y\":{v.Y}}}";
}
public virtual string Accept(DVector3 type)
public override string Accept(DVector3 type)
{
var v = type.Value;
return $"{{\"x\":{v.X},\"y\":{v.Y},\"z\":{v.Z}}}";
}
public virtual string Accept(DVector4 type)
public override string Accept(DVector4 type)
{
var v = type.Value;
return $"{{\"x\":{v.X},\"y\":{v.Y},\"z\":{v.Z},\"w\":{v.W}}}";
}
public string Accept(DDateTime type)
{
var ass = DefAssembly.LocalAssebmly as DefAssembly;
return type.GetUnixTime(ass.TimeZone).ToString();
}
}
}

View File

@ -1,99 +1,42 @@
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.Defs;
using Luban.Job.Cfg.Utils;
using System.Collections.Generic;
using System.Text;
namespace Luban.Job.Cfg.DataVisitors
{
class ToStringVisitor : IDataActionVisitor<StringBuilder>
class ToStringVisitor : ToLiteralVisitorBase
{
public static ToStringVisitor Ins { get; } = new ToStringVisitor();
public void Accept(DBool type, StringBuilder x)
public override string Accept(DText type)
{
x.Append(type.Value ? "true" : "false");
var ass = DefAssembly.LocalAssebmly as DefAssembly;
return $"\"{type.Key}#{type.GetText(ass.ExportTextTable, ass.NotConvertTextSet)}\"";
}
public void Accept(DByte type, StringBuilder x)
public override string Accept(DBean type)
{
x.Append(type.Value);
}
public void Accept(DShort type, StringBuilder x)
{
x.Append(type.Value);
}
public void Accept(DFshort type, StringBuilder x)
{
x.Append(type.Value);
}
public void Accept(DInt type, StringBuilder x)
{
x.Append(type.Value);
}
public void Accept(DFint type, StringBuilder x)
{
x.Append(type.Value);
}
public void Accept(DLong type, StringBuilder x)
{
x.Append(type.Value);
}
public void Accept(DFlong type, StringBuilder x)
{
x.Append(type.Value);
}
public void Accept(DFloat type, StringBuilder x)
{
x.Append(type.Value);
}
public void Accept(DDouble type, StringBuilder x)
{
x.Append(type.Value);
}
public void Accept(DEnum type, StringBuilder x)
{
x.Append(type.StrValue);
}
public void Accept(DString type, StringBuilder x)
{
x.Append(type.Value);
}
public void Accept(DBytes type, StringBuilder x)
{
x.Append(Bright.Common.StringUtil.ArrayToString(type.Value));
}
public void Accept(DText type, StringBuilder x)
{
x.Append(type.Key).Append('|').Append(type.RawValue);
}
public void Accept(DBean type, StringBuilder x)
{
if (type.ImplType == null)
var x = new StringBuilder();
var bean = type.ImplType;
if (bean.IsAbstractType)
{
x.Append("null");
return;
x.Append($"{{ _name:\"{type.ImplType.Name}\",");
}
x.Append(type.ImplType.FullName).Append('{');
else
{
x.Append('{');
}
int index = 0;
foreach (var f in type.Fields)
{
var defField = type.ImplType.HierarchyFields[index++];
var defField = type.ImplType.HierarchyExportFields[index++];
x.Append(defField.Name).Append(':');
if (f != null)
{
f.Apply(this, x);
x.Append(f.Apply(this));
}
else
{
@ -102,69 +45,71 @@ namespace Luban.Job.Cfg.DataVisitors
x.Append(',');
}
x.Append('}');
return x.ToString();
}
private void Append(List<DType> datas, StringBuilder x)
protected void Append(List<DType> datas, StringBuilder x)
{
x.Append('{');
x.Append('[');
foreach (var e in datas)
{
e.Apply(this, x);
x.Append(',');
x.Append(e.Apply(this)).Append(',');
}
x.Append('}');
x.Append(']');
}
public void Accept(DArray type, StringBuilder x)
public override string Accept(DArray type)
{
var x = new StringBuilder();
Append(type.Datas, x);
return x.ToString();
}
public void Accept(DList type, StringBuilder x)
public override string Accept(DList type)
{
var x = new StringBuilder();
Append(type.Datas, x);
return x.ToString();
}
public void Accept(DSet type, StringBuilder x)
public override string Accept(DSet type)
{
var x = new StringBuilder();
Append(type.Datas, x);
return x.ToString();
}
public void Accept(DMap type, StringBuilder x)
public override string Accept(DMap type)
{
var x = new StringBuilder();
x.Append('{');
foreach (var e in type.Datas)
{
e.Key.Apply(this, x);
x.Append('"').Append(e.Key.ToString()).Append('"');
x.Append(':');
e.Value.Apply(this, x);
x.Append(',');
x.Append(e.Value.Apply(this)).Append(',');
}
x.Append('}');
return x.ToString();
}
public void Accept(DVector2 type, StringBuilder x)
public override string Accept(DVector2 type)
{
var v = type.Value;
x.Append($"vector2(x={v.X},y={v.Y})");
return $"{{x:{v.X},y:{v.Y}}}";
}
public void Accept(DVector3 type, StringBuilder x)
public override string Accept(DVector3 type)
{
var v = type.Value;
x.Append($"vector3(x={v.X},y={v.Y},z={v.Z})");
return $"{{x:{v.X},y:{v.Y},z:{v.Z}}}";
}
public void Accept(DVector4 type, StringBuilder x)
public override string Accept(DVector4 type)
{
var v = type.Value;
x.Append($"vector4(x={v.X},y={v.Y},z={v.Z},w={v.W})");
}
public void Accept(DDateTime type, StringBuilder x)
{
x.Append(type.Time.ToString());
return $"{{x:{v.X},y:{v.Y},z:{v.Z},w:{v.W}}}";
}
}
}

View File

@ -6,9 +6,9 @@ using System.Text;
namespace Luban.Job.Cfg.DataVisitors
{
class ToLuaStringVisitor : ToJsonStringVisitor
class ToXmlLiteralVisitor : ToJsonLiteralVisitor
{
public static new ToLuaStringVisitor Ins { get; } = new();
public static new ToXmlLiteralVisitor Ins { get; } = new();
public override string Accept(DText type)
{

View File

@ -16,14 +16,8 @@ namespace Luban.Job.Cfg.Datas
public override string ToString()
{
var s = new StringBuilder();
this.Apply(ToStringVisitor.Ins, s);
return s.ToString();
return this.Apply(ToStringVisitor.Ins);
}
public string JsonValue => this.Apply(ToJsonStringVisitor.Ins);
public string LuaValue => this.Apply(ToLuaStringVisitor.Ins);
}
public abstract class DType<T> : DType

View File

@ -1,5 +1,6 @@
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.Defs;
using Luban.Job.Cfg.Utils;
using Scriban;
using System.Collections.Generic;
@ -28,7 +29,7 @@ namespace Luban.Job.Cfg
public static string RenderData(this Template template, DefTable table, List<DBean> exportDatas, Dictionary<string, object> extraModels = null)
{
var ctx = new TemplateContext();
var env = new TTypeTemplateExtends
var env = new DTypeTemplateExtends
{
["table"] = table,
["datas"] = exportDatas,

View File

@ -0,0 +1,57 @@
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.DataVisitors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Luban.Job.Cfg.Utils
{
class DTypeTemplateExtends : TTypeTemplateExtends
{
public static DType GetField(DBean bean, string fieldName)
{
int index = 0;
foreach (var f in bean.ImplType.HierarchyExportFields)
{
if (f.Name == fieldName)
{
return bean.Fields[index];
}
++index;
}
return null;
}
public static string ToJsonPropertyName(DType type)
{
return "\"" + type.Apply(ToJsonPropertyNameVisitor.Ins) + "\"";
}
public static string ToJsonLiteral(DType type)
{
return type.Apply(ToJsonLiteralVisitor.Ins);
}
public static string ToLuaLiteral(DType type)
{
return type.Apply(ToLuaLiteralVisitor.Ins);
}
public static string ToXmlLiteral(DType type)
{
return type.Apply(ToXmlLiteralVisitor.Ins);
}
public static string ToPythonLiteral(DType type)
{
return type.Apply(ToPythonLiteralVisitor.Ins);
}
public static string ToErlangLiteral(DType type)
{
return type.Apply(ToErlangLiteralVisitor.Ins);
}
}
}

View File

@ -80,12 +80,12 @@ namespace Luban.Job.Cfg.Utils
public static string EscapeString(string s)
{
return s.Replace("\\", "\\\\").Replace("'", "\\'");
return s.Replace("\\", "\\\\");
}
public static string EscapeStringWithQuote(string s)
{
return "'" + s.Replace("\\", "\\\\").Replace("'", "\\'") + "'";
return "\"" + s.Replace("\\", "\\\\") + "\"";
}
public static (string Key, string Text) ExtractText(string rawKeyAndText)

View File

@ -1,28 +1,14 @@
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.Defs;
using Luban.Job.Cfg.TypeVisitors;
using Luban.Job.Common.Defs;
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
using System;
namespace Luban.Job.Cfg.Defs
namespace Luban.Job.Cfg.Utils
{
class TTypeTemplateExtends : TTypeTemplateCommonExtends
{
public static DType GetField(DBean bean, string fieldName)
{
int index = 0;
foreach (var f in bean.ImplType.HierarchyExportFields)
{
if (f.Name == fieldName)
{
return bean.Fields[index];
}
++index;
}
return null;
}
public static string CsDefineTextKeyField(DefField field)
{
return $"string {field.GetTextKeyName(field.CsStyleName)}";