diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToErlangLiteralVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToErlangLiteralVisitor.cs new file mode 100644 index 0000000..31e86be --- /dev/null +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToErlangLiteralVisitor.cs @@ -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 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}}}"; + } + } +} diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToJsonLiteralVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToJsonLiteralVisitor.cs new file mode 100644 index 0000000..99e6691 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToJsonLiteralVisitor.cs @@ -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 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}}}"; + } + } +} diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToLiteralVisitorBase.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToLiteralVisitorBase.cs new file mode 100644 index 0000000..b7f8572 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToLiteralVisitorBase.cs @@ -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 + { + 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(); + } + } +} diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToLuaLiteralVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToLuaLiteralVisitor.cs new file mode 100644 index 0000000..ecd2481 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToLuaLiteralVisitor.cs @@ -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 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}}}"; + } + } +} diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToJsonStringVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToPythonLiteralVisitor.cs similarity index 57% rename from src/Luban.Job.Cfg/Source/DataVisitors/ToJsonStringVisitor.cs rename to src/Luban.Job.Cfg/Source/DataVisitors/ToPythonLiteralVisitor.cs index afb9fe4..c3dc35e 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/ToJsonStringVisitor.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToPythonLiteralVisitor.cs @@ -6,82 +6,17 @@ using System.Text; namespace Luban.Job.Cfg.DataVisitors { - class ToJsonStringVisitor : IDataFuncVisitor + 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(); - } } } diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToStringVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToStringVisitor.cs index 0994b45..ae0b9ac 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/ToStringVisitor.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToStringVisitor.cs @@ -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 + 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 datas, StringBuilder x) + protected void Append(List 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}}}"; } } } diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToLuaStringVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToXmlLiteralVisitor.cs similarity index 95% rename from src/Luban.Job.Cfg/Source/DataVisitors/ToLuaStringVisitor.cs rename to src/Luban.Job.Cfg/Source/DataVisitors/ToXmlLiteralVisitor.cs index 1c3bf33..1eb113b 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/ToLuaStringVisitor.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToXmlLiteralVisitor.cs @@ -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) { diff --git a/src/Luban.Job.Cfg/Source/Datas/DType.cs b/src/Luban.Job.Cfg/Source/Datas/DType.cs index 180764c..1cb6b22 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DType.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DType.cs @@ -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 : DType diff --git a/src/Luban.Job.Cfg/Source/RenderExtension.cs b/src/Luban.Job.Cfg/Source/RenderExtension.cs index d78fe50..2cb09fd 100644 --- a/src/Luban.Job.Cfg/Source/RenderExtension.cs +++ b/src/Luban.Job.Cfg/Source/RenderExtension.cs @@ -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 exportDatas, Dictionary extraModels = null) { var ctx = new TemplateContext(); - var env = new TTypeTemplateExtends + var env = new DTypeTemplateExtends { ["table"] = table, ["datas"] = exportDatas, diff --git a/src/Luban.Job.Cfg/Source/Utils/DTypeTemplateExtends.cs b/src/Luban.Job.Cfg/Source/Utils/DTypeTemplateExtends.cs new file mode 100644 index 0000000..f91db73 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/Utils/DTypeTemplateExtends.cs @@ -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); + } + } +} diff --git a/src/Luban.Job.Cfg/Source/Utils/DataUtil.cs b/src/Luban.Job.Cfg/Source/Utils/DataUtil.cs index 7123877..6e61f45 100644 --- a/src/Luban.Job.Cfg/Source/Utils/DataUtil.cs +++ b/src/Luban.Job.Cfg/Source/Utils/DataUtil.cs @@ -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) diff --git a/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs b/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs similarity index 95% rename from src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs rename to src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs index c8cbaec..b465773 100644 --- a/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs +++ b/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs @@ -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)}";