From 732297411c1ca7a58238a77cb7f4c8b8891cd411 Mon Sep 17 00:00:00 2001 From: walon Date: Sat, 28 Aug 2021 15:33:19 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91=E6=94=AF?= =?UTF-8?q?=E6=8C=81erlang=E5=AF=BC=E5=87=BA=E6=A0=BC=E5=BC=8F=20=E3=80=90?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E3=80=91=E5=AE=8C=E5=96=84=E4=BA=86=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=AF=BC=E5=87=BA=E6=A8=A1=E6=9D=BF=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=EF=BC=8C=E8=A1=A5=E5=85=85=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E8=BE=85=E5=8A=A9=E5=87=BD=E6=95=B0=EF=BC=8C=E6=96=B9=E4=BE=BF?= =?UTF-8?q?=E5=AE=9A=E5=88=B6=E7=94=9F=E6=88=90=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 +- .../Source/DataExporters/BinaryExportor.cs | 90 ++++---- .../Source/DataExporters/ErlangExport.cs | 52 +++++ .../Source/DataExporters/Json2Exportor.cs | 10 +- .../Source/DataExporters/JsonExportor.cs | 79 ++++--- .../Source/DataExporters/LuaExportor.cs | 202 +----------------- .../Source/DataVisitors/IsDefaultValue.cs | 2 +- .../IsSimpleLiteralDataVisitor.cs | 126 +++++++++++ .../DataVisitors/ToErlangLiteralVisitor.cs | 49 ++--- .../DataVisitors/ToJsonLiteralVisitor.cs | 34 ++- .../DataVisitors/ToLuaLiteralVisitor.cs | 16 +- .../DataVisitors/ToPythonLiteralVisitor.cs | 2 +- .../Source/DataVisitors/ToStringVisitor.cs | 3 +- src/Luban.Job.Cfg/Source/Datas/DArray.cs | 2 +- src/Luban.Job.Cfg/Source/Datas/DBean.cs | 2 +- src/Luban.Job.Cfg/Source/Datas/DBool.cs | 2 + src/Luban.Job.Cfg/Source/Datas/DByte.cs | 2 + src/Luban.Job.Cfg/Source/Datas/DBytes.cs | 3 + src/Luban.Job.Cfg/Source/Datas/DDateTime.cs | 2 + src/Luban.Job.Cfg/Source/Datas/DDouble.cs | 2 + src/Luban.Job.Cfg/Source/Datas/DEnum.cs | 2 + src/Luban.Job.Cfg/Source/Datas/DFint.cs | 2 + src/Luban.Job.Cfg/Source/Datas/DFloat.cs | 2 + src/Luban.Job.Cfg/Source/Datas/DFlong.cs | 2 + src/Luban.Job.Cfg/Source/Datas/DFshort.cs | 2 + src/Luban.Job.Cfg/Source/Datas/DInt.cs | 2 + src/Luban.Job.Cfg/Source/Datas/DList.cs | 2 +- src/Luban.Job.Cfg/Source/Datas/DLong.cs | 1 + src/Luban.Job.Cfg/Source/Datas/DMap.cs | 2 +- src/Luban.Job.Cfg/Source/Datas/DSet.cs | 2 +- src/Luban.Job.Cfg/Source/Datas/DShort.cs | 2 + src/Luban.Job.Cfg/Source/Datas/DString.cs | 2 + src/Luban.Job.Cfg/Source/Datas/DText.cs | 2 + src/Luban.Job.Cfg/Source/Datas/DType.cs | 2 + src/Luban.Job.Cfg/Source/Datas/DVector2.cs | 2 + src/Luban.Job.Cfg/Source/Datas/DVector3.cs | 2 + src/Luban.Job.Cfg/Source/Datas/DVector4.cs | 2 + src/Luban.Job.Cfg/Source/Defs/DefAssembly.cs | 2 + .../Source/Utils/DTypeTemplateExtends.cs | 18 +- .../Source/Utils/DataExporterUtil.cs | 34 ++- .../Source/Utils/RenderFileUtil.cs | 2 +- 41 files changed, 415 insertions(+), 364 deletions(-) create mode 100644 src/Luban.Job.Cfg/Source/DataExporters/ErlangExport.cs create mode 100644 src/Luban.Job.Cfg/Source/DataVisitors/IsSimpleLiteralDataVisitor.cs diff --git a/README.md b/README.md index 20e0f3c..56a84e3 100644 --- a/README.md +++ b/README.md @@ -57,8 +57,10 @@ Luban适合有以下需求的开发者: - 使用云生成模式。生成极快,MMORPG这样大型项目也能秒内生成。日常增量生成基本在300ms以内。节省迭代时间。支持watch监测模式,数据目录变化立即重新生成。 - 灵活的数据源定义。一个表可以来自多个文件或者一个文件内定义多个表或者一个目录下所有文件甚至来自云表格,以及以上的组合 - 支持表与字段级别分组。可以选择性地导出客户端或者服务器所用的表及字段 -- 多种导出数据格式支持。支持binary、json、lua 等导出数据格式 -- 支持自定义数据模板。可以用模板文件定制导出格式。 +- 多种导出数据格式支持。支持binary、json、lua、xml、erlang 等导出数据格式 +- **====>强大灵活的定制能力<====** + - **支持代码模板,可以用自定义模板定制生成的代码格式** + - **支持数据模板,可以用模板文件定制导出格式**。意味着可以在不改动现有程序代码的情况下,把luban当作**配置处理前端**,生成自定义格式的数据与自己项目的配置加载代码配合工作。开发已久的项目或者已经上线的老项目,也能从luban强大的数据处理工作流中获益 - 支持数据标签。 可以选择导出符合要求的数据,发布正式数据时策划不必手动注释掉那些测试数据了 - 强大的数据校验能力。支持内建数据格式检查;支持ref表引用检查(策划不用担心填错id);支持path资源检查(策划不用担心填错资源路径);支持range检查 - 支持常量别名。策划不必再为诸如 升级丹 这样的道具手写具体道具id了 @@ -66,7 +68,6 @@ Luban适合有以下需求的开发者: - 支持res资源标记。可以一键导出配置中引用的所有资源列表(icon,ui,assetbundle等等) - 统一了自定义编辑器的配置数据。与Unity和UE4的自定义编辑器良好配合,为编辑器生成合适的加载与保存json配置的的c#(Unity)或c++(UE4)代码。保存的json配置能够被luban识别和处理。 - 支持emmylua anntations。生成的lua包含符合emmylua 格式anntations信息。配合emmylua有良好的配置代码提示能力 -- 灵活的自定义生成能力。可以在不修改代码的情况下使用自定义模板取代默认生成模板。大多数自定义对象支持tags属性,允许通过tags生成特殊内容。 - **本地化支持** - 支持时间本地化。datetime类型数据会根据指定的timezone,转换为目标地区该时刻的UTC时间,方便程序使用。 - 支持文本静态本地化。导出时所有text类型数据正确替换为最终的本地化字符串。绝大多数的业务功能不再需要运行根据本地化id去查找文本的内容,简化程序员的工作。 @@ -80,7 +81,8 @@ Luban适合有以下需求的开发者: - go (1.10+) - lua (5.1+) - js 和 typescript (3.0+) - - python (3.0+) + - python (3.0+) + - erlang (18+) - 支持主流引擎和平台 - unity + c# - unity + [tolua](https://github.com/topameng/tolua)、[xlua](https://github.com/Tencent/xLua) diff --git a/src/Luban.Job.Cfg/Source/DataExporters/BinaryExportor.cs b/src/Luban.Job.Cfg/Source/DataExporters/BinaryExportor.cs index d745520..34d869f 100644 --- a/src/Luban.Job.Cfg/Source/DataExporters/BinaryExportor.cs +++ b/src/Luban.Job.Cfg/Source/DataExporters/BinaryExportor.cs @@ -7,118 +7,110 @@ using System.Collections.Generic; namespace Luban.Job.Cfg.DataExporters { - class BinaryExportor : IDataActionVisitor + class BinaryExportor : IDataActionVisitor { public static BinaryExportor Ins { get; } = new BinaryExportor(); - public void WriteList(List datas, DefAssembly ass, ByteBuf x) + public void WriteList(DefTable table, List datas, ByteBuf x) { x.WriteSize(datas.Count); foreach (var d in datas) { - d.Data.Apply(this, ass, x); + d.Data.Apply(this, x); } } - public void Accept(DBool type, DefAssembly ass, ByteBuf x) + public void Accept(DBool type, ByteBuf x) { x.WriteBool(type.Value); } - public void Accept(DByte type, DefAssembly ass, ByteBuf x) + public void Accept(DByte type, ByteBuf x) { x.WriteByte(type.Value); } - public void Accept(DShort type, DefAssembly ass, ByteBuf x) + public void Accept(DShort type, ByteBuf x) { x.WriteShort(type.Value); } - public void Accept(DFshort type, DefAssembly ass, ByteBuf x) + public void Accept(DFshort type, ByteBuf x) { x.WriteFshort(type.Value); } - public void Accept(DInt type, DefAssembly ass, ByteBuf x) + public void Accept(DInt type, ByteBuf x) { x.WriteInt(type.Value); } - public void Accept(DFint type, DefAssembly ass, ByteBuf x) + public void Accept(DFint type, ByteBuf x) { x.WriteFint(type.Value); } - public void Accept(DLong type, DefAssembly ass, ByteBuf x) + public void Accept(DLong type, ByteBuf x) { x.WriteLong(type.Value); } - public void Accept(DFlong type, DefAssembly ass, ByteBuf x) + public void Accept(DFlong type, ByteBuf x) { x.WriteFlong(type.Value); } - public void Accept(DFloat type, DefAssembly ass, ByteBuf x) + public void Accept(DFloat type, ByteBuf x) { x.WriteFloat(type.Value); } - public void Accept(DDouble type, DefAssembly ass, ByteBuf x) + public void Accept(DDouble type, ByteBuf x) { x.WriteDouble(type.Value); } - public void Accept(DEnum type, DefAssembly ass, ByteBuf x) + public void Accept(DEnum type, ByteBuf x) { x.WriteInt(type.Value); } - public void Accept(DString type, DefAssembly ass, ByteBuf x) + public void Accept(DString type, ByteBuf x) { x.WriteString(type.Value); } - public void Accept(DBytes type, DefAssembly ass, ByteBuf x) + public void Accept(DBytes type, ByteBuf x) { x.WriteBytes(type.Value); } - public void Accept(DText type, DefAssembly ass, ByteBuf x) + public void Accept(DText type, ByteBuf x) { x.WriteString(type.Key); + var ass = DefAssembly.LocalAssebmly; x.WriteString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet)); } - public void Accept(DBean type, DefAssembly ass, ByteBuf x) + public void Accept(DBean type, ByteBuf x) { var bean = type.Type; if (bean.IsAbstractType) { - // 调整设计后,多态bean不会为空 - //if (type.ImplType == null) - //{ - // x.WriteInt(0); - // return; - //} x.WriteInt(type.ImplType.Id); } - int index = -1; + + var defFields = type.ImplType.HierarchyExportFields; + int index = 0; foreach (var field in type.Fields) { - ++index; - var defField = (DefField)type.ImplType.HierarchyFields[index]; - if (!defField.NeedExport) - { - continue; - } + var defField = defFields[index++]; if (defField.CType.IsNullable) { if (field != null) { x.WriteBool(true); - field.Apply(this, ass, x); + field.Apply(this, x); } else { @@ -127,64 +119,64 @@ namespace Luban.Job.Cfg.DataExporters } else { - field.Apply(this, ass, x); + field.Apply(this, x); } } } - public void WriteList(List datas, DefAssembly ass, ByteBuf x) + public void WriteList(List datas, ByteBuf x) { x.WriteSize(datas.Count); foreach (var d in datas) { - d.Apply(this, ass, x); + d.Apply(this, x); } } - public void Accept(DArray type, DefAssembly ass, ByteBuf x) + public void Accept(DArray type, ByteBuf x) { - WriteList(type.Datas, ass, x); + WriteList(type.Datas, x); } - public void Accept(DList type, DefAssembly ass, ByteBuf x) + public void Accept(DList type, ByteBuf x) { - WriteList(type.Datas, ass, x); + WriteList(type.Datas, x); } - public void Accept(DSet type, DefAssembly ass, ByteBuf x) + public void Accept(DSet type, ByteBuf x) { - WriteList(type.Datas, ass, x); + WriteList(type.Datas, x); } - public void Accept(DMap type, DefAssembly ass, ByteBuf x) + public void Accept(DMap type, ByteBuf x) { Dictionary datas = type.Datas; x.WriteSize(datas.Count); foreach (var e in datas) { - e.Key.Apply(this, ass, x); - e.Value.Apply(this, ass, x); + e.Key.Apply(this, x); + e.Value.Apply(this, x); } } - public void Accept(DVector2 type, DefAssembly ass, ByteBuf x) + public void Accept(DVector2 type, ByteBuf x) { x.WriteVector2(type.Value); } - public void Accept(DVector3 type, DefAssembly ass, ByteBuf x) + public void Accept(DVector3 type, ByteBuf x) { x.WriteVector3(type.Value); } - public void Accept(DVector4 type, DefAssembly ass, ByteBuf x) + public void Accept(DVector4 type, ByteBuf x) { x.WriteVector4(type.Value); } - public void Accept(DDateTime type, DefAssembly ass, ByteBuf x) + public void Accept(DDateTime type, ByteBuf x) { - x.WriteInt(type.GetUnixTime(ass.TimeZone)); + x.WriteInt(type.GetUnixTime(DefAssembly.LocalAssebmly.TimeZone)); } } } diff --git a/src/Luban.Job.Cfg/Source/DataExporters/ErlangExport.cs b/src/Luban.Job.Cfg/Source/DataExporters/ErlangExport.cs new file mode 100644 index 0000000..60a3ed2 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/DataExporters/ErlangExport.cs @@ -0,0 +1,52 @@ +using Luban.Job.Cfg.Datas; +using Luban.Job.Cfg.DataVisitors; +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.DataExporters +{ + class ErlangExport + { + public static ErlangExport Ins { get; } = new(); + + public void ExportTableSingleton(DefTable t, Record record, StringBuilder s) + { + s.Append($"-module({t.FullName.Replace('.', '_').ToLower()}).").AppendLine(); + s.Append($"-export([get_data/0]).").AppendLine(); + + + s.Append("get_data() -> ").AppendLine(); + s.Append('\t').Append(record.Data.Apply(ToErlangLiteralVisitor.Ins)); + s.Append('.'); + } + + public void ExportTableMap(DefTable t, List records, StringBuilder s) + { + s.Append($"-module({t.FullName.Replace('.', '_').ToLower()}).").AppendLine(); + s.Append($"-export([get_data_map/0, get_key_list/0]).").AppendLine(); + + s.Append("get_data_map() -> #{").AppendLine(); + + int index = 0; + foreach (Record r in records) + { + if (index++ > 0) + { + s.Append(',').AppendLine(); + } + DBean d = r.Data; + s.Append($"\t{d.GetField(t.Index).Apply(ToErlangLiteralVisitor.Ins)} => "); + s.Append(d.Apply(ToErlangLiteralVisitor.Ins)); + } + s.Append("}.").AppendLine(); + + s.Append($"get_key_list() ->").AppendLine(); + s.Append($"\t[{string.Join(',', records.Select(r => r.Data.GetField(t.Index).Apply(ToErlangLiteralVisitor.Ins)))}]."); + } + } +} diff --git a/src/Luban.Job.Cfg/Source/DataExporters/Json2Exportor.cs b/src/Luban.Job.Cfg/Source/DataExporters/Json2Exportor.cs index a293bfe..d5fae82 100644 --- a/src/Luban.Job.Cfg/Source/DataExporters/Json2Exportor.cs +++ b/src/Luban.Job.Cfg/Source/DataExporters/Json2Exportor.cs @@ -15,13 +15,13 @@ namespace Luban.Job.Cfg.DataExporters { public new static Json2Exportor Ins { get; } = new(); - public void WriteAsObject(DefTable table, List datas, DefAssembly ass, Utf8JsonWriter x) + public void WriteAsObject(DefTable table, List datas, Utf8JsonWriter x) { switch (table.Mode) { case RawDefs.ETableMode.ONE: { - this.Accept(datas[0].Data, ass, x); + this.Accept(datas[0].Data, x); break; } case RawDefs.ETableMode.MAP: @@ -34,7 +34,7 @@ namespace Luban.Job.Cfg.DataExporters var indexFieldData = rec.Data.GetField(indexName); x.WritePropertyName(indexFieldData.Apply(ToJsonPropertyNameVisitor.Ins)); - this.Accept(rec.Data, ass, x); + this.Accept(rec.Data, x); } x.WriteEndObject(); @@ -47,13 +47,13 @@ namespace Luban.Job.Cfg.DataExporters } } - public override void Accept(DMap type, DefAssembly ass, Utf8JsonWriter x) + public override void Accept(DMap type, Utf8JsonWriter x) { x.WriteStartObject(); foreach (var d in type.Datas) { x.WritePropertyName(d.Key.Apply(ToJsonPropertyNameVisitor.Ins)); - d.Value.Apply(this, ass, x); + d.Value.Apply(this, x); } x.WriteEndObject(); } diff --git a/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs b/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs index 97f2fe7..aa42004 100644 --- a/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs +++ b/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs @@ -7,96 +7,97 @@ using System.Text.Json; namespace Luban.Job.Cfg.DataExporters { - class JsonExportor : IDataActionVisitor + class JsonExportor : IDataActionVisitor { public static JsonExportor Ins { get; } = new JsonExportor(); - public void WriteAsArray(List datas, DefAssembly ass, Utf8JsonWriter x) + public void WriteAsArray(List datas, Utf8JsonWriter x) { x.WriteStartArray(); foreach (var d in datas) { - d.Data.Apply(this, ass, x); + d.Data.Apply(this, x); } x.WriteEndArray(); } - public void Accept(DBool type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DBool type, Utf8JsonWriter x) { x.WriteBooleanValue(type.Value); } - public void Accept(DByte type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DByte type, Utf8JsonWriter x) { x.WriteNumberValue(type.Value); } - public void Accept(DShort type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DShort type, Utf8JsonWriter x) { x.WriteNumberValue(type.Value); } - public void Accept(DFshort type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DFshort type, Utf8JsonWriter x) { x.WriteNumberValue(type.Value); } - public void Accept(DInt type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DInt type, Utf8JsonWriter x) { x.WriteNumberValue(type.Value); } - public void Accept(DFint type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DFint type, Utf8JsonWriter x) { x.WriteNumberValue(type.Value); } - public void Accept(DLong type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DLong type, Utf8JsonWriter x) { x.WriteNumberValue(type.Value); } - public void Accept(DFlong type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DFlong type, Utf8JsonWriter x) { x.WriteNumberValue(type.Value); } - public void Accept(DFloat type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DFloat type, Utf8JsonWriter x) { x.WriteNumberValue(type.Value); } - public void Accept(DDouble type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DDouble type, Utf8JsonWriter x) { x.WriteNumberValue(type.Value); } - public void Accept(DEnum type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DEnum type, Utf8JsonWriter x) { x.WriteNumberValue(type.Value); } - public void Accept(DString type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DString type, Utf8JsonWriter x) { x.WriteStringValue(type.Value); } - public void Accept(DBytes type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DBytes type, Utf8JsonWriter x) { throw new NotImplementedException(); } - public void Accept(DText type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DText type, Utf8JsonWriter x) { x.WriteStartObject(); x.WritePropertyName(DText.KEY_NAME); x.WriteStringValue(type.Key); x.WritePropertyName(DText.TEXT_NAME); + var ass = DefAssembly.LocalAssebmly; x.WriteStringValue(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet)); x.WriteEndObject(); } - public void Accept(DBean type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DBean type, Utf8JsonWriter x) { x.WriteStartObject(); @@ -106,15 +107,11 @@ namespace Luban.Job.Cfg.DataExporters x.WriteStringValue(type.ImplType.Name); } - var defFields = type.ImplType.HierarchyFields; + var defFields = type.ImplType.HierarchyExportFields; int index = 0; foreach (var d in type.Fields) { - var defField = (DefField)defFields[index++]; - if (!defField.NeedExport) - { - continue; - } + var defField = defFields[index++]; // 特殊处理 bean 多态类型 // 另外,不生成 xxx:null 这样 @@ -125,51 +122,51 @@ namespace Luban.Job.Cfg.DataExporters else { x.WritePropertyName(defField.Name); - d.Apply(this, ass, x); + d.Apply(this, x); } } x.WriteEndObject(); } - public void WriteList(List datas, DefAssembly ass, Utf8JsonWriter x) + public void WriteList(List datas, Utf8JsonWriter x) { x.WriteStartArray(); foreach (var d in datas) { - d.Apply(this, ass, x); + d.Apply(this, x); } x.WriteEndArray(); } - public void Accept(DArray type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DArray type, Utf8JsonWriter x) { - WriteList(type.Datas, ass, x); + WriteList(type.Datas, x); } - public void Accept(DList type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DList type, Utf8JsonWriter x) { - WriteList(type.Datas, ass, x); + WriteList(type.Datas, x); } - public void Accept(DSet type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DSet type, Utf8JsonWriter x) { - WriteList(type.Datas, ass, x); + WriteList(type.Datas, x); } - public virtual void Accept(DMap type, DefAssembly ass, Utf8JsonWriter x) + public virtual void Accept(DMap type, Utf8JsonWriter x) { x.WriteStartArray(); foreach (var d in type.Datas) { x.WriteStartArray(); - d.Key.Apply(this, ass, x); - d.Value.Apply(this, ass, x); + d.Key.Apply(this, x); + d.Value.Apply(this, x); x.WriteEndArray(); } x.WriteEndArray(); } - public void Accept(DVector2 type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DVector2 type, Utf8JsonWriter x) { x.WriteStartObject(); x.WriteNumber("x", type.Value.X); @@ -177,7 +174,7 @@ namespace Luban.Job.Cfg.DataExporters x.WriteEndObject(); } - public void Accept(DVector3 type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DVector3 type, Utf8JsonWriter x) { x.WriteStartObject(); x.WriteNumber("x", type.Value.X); @@ -186,7 +183,7 @@ namespace Luban.Job.Cfg.DataExporters x.WriteEndObject(); } - public void Accept(DVector4 type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DVector4 type, Utf8JsonWriter x) { x.WriteStartObject(); x.WriteNumber("x", type.Value.X); @@ -196,9 +193,9 @@ namespace Luban.Job.Cfg.DataExporters x.WriteEndObject(); } - public void Accept(DDateTime type, DefAssembly ass, Utf8JsonWriter x) + public void Accept(DDateTime type, Utf8JsonWriter x) { - x.WriteNumberValue(type.GetUnixTime(ass.TimeZone)); + x.WriteNumberValue(type.GetUnixTime(DefAssembly.LocalAssebmly.TimeZone)); } } } diff --git a/src/Luban.Job.Cfg/Source/DataExporters/LuaExportor.cs b/src/Luban.Job.Cfg/Source/DataExporters/LuaExportor.cs index c9af468..1d100b2 100644 --- a/src/Luban.Job.Cfg/Source/DataExporters/LuaExportor.cs +++ b/src/Luban.Job.Cfg/Source/DataExporters/LuaExportor.cs @@ -9,208 +9,28 @@ using System.Text; namespace Luban.Job.Cfg.DataExporters { - class LuaExportor : IDataActionVisitor + class LuaExportor { public static LuaExportor Ins { get; } = new LuaExportor(); - public void ExportTableOne(DefTable t, List records, List result) + public void ExportTableSingleton(DefTable t, Record record, StringBuilder result) { - result.Add("return "); - var s = new StringBuilder(); - Accept(records[0].Data, t.Assembly, s); - result.Add(s.ToString()); + result.Append("return ").AppendLine(); + result.Append(record.Data.Apply(ToLuaLiteralVisitor.Ins)); } - public void ExportTableOneKeyMap(DefTable t, List records, List result) + public void ExportTableMap(DefTable t, List records, StringBuilder s) { - result.Add("return "); - result.Add("{"); - var s = new StringBuilder(); - var ks = new StringBuilder(); + s.Append("return").AppendLine(); + s.Append('{').AppendLine(); foreach (Record r in records) { DBean d = r.Data; - s.Clear(); - s.Append($"[{ToLuaCodeString(d.GetField(t.Index), t.Assembly, ks)}] = "); - Accept(d, t.Assembly, s); - s.Append(','); - result.Add(s.ToString()); + s.Append($"[{d.GetField(t.Index).Apply(ToLuaLiteralVisitor.Ins)}] = "); + s.Append(d.Apply(ToLuaLiteralVisitor.Ins)); + s.Append(',').AppendLine(); } - result.Add("}"); - } - - private string ToLuaCodeString(DType data, DefAssembly ass, StringBuilder b) - { - b.Clear(); - data.Apply(this, ass, b); - return b.ToString(); - } - - public void Accept(DBool type, DefAssembly ass, StringBuilder line) - { - line.Append(type.Value ? "true" : "false"); - } - - public void Accept(DByte type, DefAssembly ass, StringBuilder line) - { - line.Append(type.Value); - } - - public void Accept(DShort type, DefAssembly ass, StringBuilder line) - { - line.Append(type.Value); - } - - public void Accept(DFshort type, DefAssembly ass, StringBuilder line) - { - line.Append(type.Value); - } - - public void Accept(DInt type, DefAssembly ass, StringBuilder line) - { - line.Append(type.Value); - } - - public void Accept(DFint type, DefAssembly ass, StringBuilder line) - { - line.Append(type.Value); - } - - public void Accept(DLong type, DefAssembly ass, StringBuilder line) - { - line.Append(type.Value); - } - - public void Accept(DFlong type, DefAssembly ass, StringBuilder line) - { - line.Append(type.Value); - } - - public void Accept(DFloat type, DefAssembly ass, StringBuilder line) - { - line.Append(type.Value); - } - - public void Accept(DDouble type, DefAssembly ass, StringBuilder line) - { - line.Append(type.Value); - } - - public void Accept(DEnum type, DefAssembly ass, StringBuilder line) - { - line.Append(type.Value); - } - - public void Accept(DString type, DefAssembly ass, StringBuilder line) - { - line.Append('\'').Append(DataUtil.EscapeString(type.Value)).Append('\''); - } - - public void Accept(DBytes type, DefAssembly ass, StringBuilder line) - { - throw new NotImplementedException(); - } - - public void Accept(DText type, DefAssembly ass, StringBuilder line) - { - line.Append($"{{{DText.KEY_NAME}='{type.Key}',{DText.TEXT_NAME}='{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}'}}"); - } - - public void Accept(DBean type, DefAssembly ass, StringBuilder line) - { - var bean = type.Type; - if (bean.IsAbstractType) - { - line.Append($"{{ _name='{type.ImplType.Name}',"); - } - else - { - line.Append('{'); - } - int index = -1; - foreach (var field in type.Fields) - { - ++index; - var defField = (DefField)type.ImplType.HierarchyFields[index]; - if (!defField.NeedExport) - { - continue; - } - if (field != null) - { - line.Append(defField.Name).Append('='); - field.Apply(this, ass, line); - line.Append(','); - } - } - line.Append("}"); - } - - public void Accept(DArray type, DefAssembly ass, StringBuilder line) - { - line.Append('{'); - foreach (var d in type.Datas) - { - d.Apply(this, ass, line); - line.Append(','); - } - line.Append('}'); - } - - public void Accept(DList type, DefAssembly ass, StringBuilder line) - { - line.Append('{'); - foreach (var d in type.Datas) - { - d.Apply(this, ass, line); - line.Append(','); - } - line.Append('}'); - } - - public void Accept(DSet type, DefAssembly ass, StringBuilder line) - { - line.Append('{'); - foreach (var d in type.Datas) - { - d.Apply(this, ass, line); - line.Append(','); - } - line.Append('}'); - } - - public void Accept(DMap type, DefAssembly ass, StringBuilder line) - { - line.Append('{'); - foreach ((var k, var v) in type.Datas) - { - line.Append('['); - k.Apply(this, ass, line); - line.Append("]="); - v.Apply(this, ass, line); - line.Append(','); - } - line.Append('}'); - } - - public void Accept(DVector2 type, DefAssembly ass, StringBuilder line) - { - line.Append($"{{x={type.Value.X},y={type.Value.Y}}}"); - } - - public void Accept(DVector3 type, DefAssembly ass, StringBuilder line) - { - line.Append($"{{x={type.Value.X},y={type.Value.Y},z={type.Value.Z}}}"); - } - - public void Accept(DVector4 type, DefAssembly ass, StringBuilder line) - { - line.Append($"{{x={type.Value.X},y={type.Value.Y},z={type.Value.Z},w={type.Value.W}}}"); - } - - public void Accept(DDateTime type, DefAssembly ass, StringBuilder line) - { - line.Append(type.GetUnixTime(ass.TimeZone)); + s.Append('}'); } } } diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/IsDefaultValue.cs b/src/Luban.Job.Cfg/Source/DataVisitors/IsDefaultValue.cs index c138d4f..ac94d09 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/IsDefaultValue.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/IsDefaultValue.cs @@ -120,7 +120,7 @@ namespace Luban.Job.Cfg.DataVisitors public bool Accept(DDateTime type) { - throw new NotSupportedException(); + return false; } } } diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/IsSimpleLiteralDataVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/IsSimpleLiteralDataVisitor.cs new file mode 100644 index 0000000..f42ebc3 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/DataVisitors/IsSimpleLiteralDataVisitor.cs @@ -0,0 +1,126 @@ +using Luban.Job.Cfg.Datas; +using System; +using System.Numerics; + +namespace Luban.Job.Cfg.DataVisitors +{ + class IsSimpleLiteralDataVisitor : IDataFuncVisitor + { + public static IsSimpleLiteralDataVisitor Ins { get; } = new(); + + public bool Accept(DBool type) + { + return true; + } + + public bool Accept(DByte type) + { + return true; + } + + public bool Accept(DShort type) + { + return true; + } + + public bool Accept(DFshort type) + { + return true; + } + + public bool Accept(DInt type) + { + return true; + } + + public bool Accept(DFint type) + { + return true; + } + + public bool Accept(DLong type) + { + return true; + } + + public bool Accept(DFlong type) + { + return true; + } + + public bool Accept(DFloat type) + { + return true; + } + + public bool Accept(DDouble type) + { + return true; + } + + public bool Accept(DEnum type) + { + return true; + } + + public bool Accept(DString type) + { + return true; + } + + public bool Accept(DBytes type) + { + throw new NotSupportedException(); + } + + public bool Accept(DText type) + { + return false; + } + + public bool Accept(DBean type) + { + return false; + } + + public bool Accept(DArray type) + { + return false; + } + + public bool Accept(DList type) + { + return false; + } + + public bool Accept(DSet type) + { + return false; + } + + public bool Accept(DMap type) + { + return false; + } + + public bool Accept(DVector2 type) + { + return false; + } + + public bool Accept(DVector3 type) + { + return false; + } + + public bool Accept(DVector4 type) + { + return false; + } + + public bool Accept(DDateTime type) + { + return true; + } + } +} diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToErlangLiteralVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToErlangLiteralVisitor.cs index 31e86be..ae4ded6 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/ToErlangLiteralVisitor.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToErlangLiteralVisitor.cs @@ -13,39 +13,38 @@ namespace Luban.Job.Cfg.DataVisitors 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))}\"}}"; + 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) + if (type.Type.IsAbstractType) { - x.Append($"{{ \"_name\":\"{type.ImplType.Name}\","); + x.Append($"#{{name__ => \"{type.ImplType.Name}\""); + if (type.Fields.Count > 0) + { + x.Append(','); + } } else { - x.Append('{'); + x.Append("#{"); } int index = 0; foreach (var f in type.Fields) { - if (index >= 1) + var defField = type.ImplType.HierarchyFields[index++]; + if (f == null) + { + continue; + } + 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($"{defField.Name} => {f.Apply(this)}"); } x.Append('}'); return x.ToString(); @@ -58,11 +57,10 @@ namespace Luban.Job.Cfg.DataVisitors int index = 0; foreach (var e in datas) { - if (index > 0) + if (index++ > 0) { x.Append(','); } - ++index; x.Append(e.Apply(this)); } x.Append(']'); @@ -92,18 +90,15 @@ namespace Luban.Job.Cfg.DataVisitors public override string Accept(DMap type) { var x = new StringBuilder(); - x.Append('{'); + x.Append("#{"); int index = 0; foreach (var e in type.Datas) { - if (index > 0) + if (index++ > 0) { x.Append(','); } - ++index; - x.Append('"').Append(e.Key.ToString()).Append('"'); - x.Append(':'); - x.Append(e.Value.Apply(this)); + x.Append($"{e.Key.Apply(this)} => {e.Value.Apply(this)}"); } x.Append('}'); return x.ToString(); @@ -112,19 +107,19 @@ namespace Luban.Job.Cfg.DataVisitors public override string Accept(DVector2 type) { var v = type.Value; - return $"{{\"x\":{v.X},\"y\":{v.Y}}}"; + 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}}}"; + 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}}}"; + 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 index 99e6691..b741d4a 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/ToJsonLiteralVisitor.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToJsonLiteralVisitor.cs @@ -12,17 +12,20 @@ namespace Luban.Job.Cfg.DataVisitors public override string Accept(DText type) { - var ass = DefAssembly.LocalAssebmly as DefAssembly; + var ass = DefAssembly.LocalAssebmly; 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) + if (type.Type.IsAbstractType) { - x.Append($"{{ \"_name\":\"{type.ImplType.Name}\","); + x.Append($"{{ \"_name\":\"{type.ImplType.Name}\""); + if (type.Fields.Count > 0) + { + x.Append(','); + } } else { @@ -32,20 +35,17 @@ namespace Luban.Job.Cfg.DataVisitors int index = 0; foreach (var f in type.Fields) { - if (index >= 1) + var defField = type.ImplType.HierarchyExportFields[index++]; + if (f == null) + { + continue; + } + 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(f.Apply(this)); } x.Append('}'); return x.ToString(); @@ -58,11 +58,10 @@ namespace Luban.Job.Cfg.DataVisitors int index = 0; foreach (var e in datas) { - if (index > 0) + if (index++ > 0) { x.Append(','); } - ++index; x.Append(e.Apply(this)); } x.Append(']'); @@ -96,11 +95,10 @@ namespace Luban.Job.Cfg.DataVisitors int index = 0; foreach (var e in type.Datas) { - if (index > 0) + if (index++ > 0) { x.Append(','); } - ++index; x.Append('"').Append(e.Key.Apply(ToJsonPropertyNameVisitor.Ins)).Append('"'); x.Append(':'); x.Append(e.Value.Apply(this)); diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToLuaLiteralVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToLuaLiteralVisitor.cs index ecd2481..86488c0 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/ToLuaLiteralVisitor.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToLuaLiteralVisitor.cs @@ -19,8 +19,7 @@ namespace Luban.Job.Cfg.DataVisitors public override string Accept(DBean type) { var x = new StringBuilder(); - var bean = type.ImplType; - if (bean.IsAbstractType) + if (type.Type.IsAbstractType) { x.Append($"{{ _name='{type.ImplType.Name}',"); } @@ -33,15 +32,12 @@ namespace Luban.Job.Cfg.DataVisitors foreach (var f in type.Fields) { var defField = type.ImplType.HierarchyExportFields[index++]; + if (f == null) + { + continue; + } x.Append(defField.Name).Append('='); - if (f != null) - { - x.Append(f.Apply(this)); - } - else - { - x.Append("nil"); - } + x.Append(f.Apply(this)); x.Append(','); } x.Append('}'); diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToPythonLiteralVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToPythonLiteralVisitor.cs index c3dc35e..939e6eb 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/ToPythonLiteralVisitor.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToPythonLiteralVisitor.cs @@ -44,7 +44,7 @@ namespace Luban.Job.Cfg.DataVisitors } else { - x.Append("null"); + x.Append("None"); } } x.Append('}'); diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToStringVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToStringVisitor.cs index ae0b9ac..241fb52 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/ToStringVisitor.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToStringVisitor.cs @@ -19,8 +19,7 @@ namespace Luban.Job.Cfg.DataVisitors public override string Accept(DBean type) { var x = new StringBuilder(); - var bean = type.ImplType; - if (bean.IsAbstractType) + if (type.Type.IsAbstractType) { x.Append($"{{ _name:\"{type.ImplType.Name}\","); } diff --git a/src/Luban.Job.Cfg/Source/Datas/DArray.cs b/src/Luban.Job.Cfg/Source/Datas/DArray.cs index 7e68ae3..104c009 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DArray.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DArray.cs @@ -9,7 +9,7 @@ namespace Luban.Job.Cfg.Datas public TArray Type { get; } public List Datas { get; } - public bool IsArray => true; + public override string TypeName => "array"; public DArray(TArray type, List datas) { diff --git a/src/Luban.Job.Cfg/Source/Datas/DBean.cs b/src/Luban.Job.Cfg/Source/Datas/DBean.cs index 0d06792..0e44525 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DBean.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DBean.cs @@ -12,7 +12,7 @@ namespace Luban.Job.Cfg.Datas public List Fields { get; } - public bool IsBean => true; + public override string TypeName => "bean"; public DBean(DefBean defType, DefBean implType, List fields) { diff --git a/src/Luban.Job.Cfg/Source/Datas/DBool.cs b/src/Luban.Job.Cfg/Source/Datas/DBool.cs index 1b41968..eb25cce 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DBool.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DBool.cs @@ -13,6 +13,8 @@ namespace Luban.Job.Cfg.Datas return x ? s_true : s_false; } + public override string TypeName => "bool"; + private DBool(bool x) : base(x) { } diff --git a/src/Luban.Job.Cfg/Source/Datas/DByte.cs b/src/Luban.Job.Cfg/Source/Datas/DByte.cs index 4b4d12f..2f6e12e 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DByte.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DByte.cs @@ -6,6 +6,8 @@ namespace Luban.Job.Cfg.Datas { public static DByte Default { get; } = new DByte(0); + public override string TypeName => "byte"; + public DByte(byte x) : base(x) { } diff --git a/src/Luban.Job.Cfg/Source/Datas/DBytes.cs b/src/Luban.Job.Cfg/Source/Datas/DBytes.cs index 03a379b..0f8b148 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DBytes.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DBytes.cs @@ -4,6 +4,9 @@ namespace Luban.Job.Cfg.Datas { public class DBytes : DType { + + public override string TypeName => "bytes"; + public DBytes(byte[] x) : base(x) { } diff --git a/src/Luban.Job.Cfg/Source/Datas/DDateTime.cs b/src/Luban.Job.Cfg/Source/Datas/DDateTime.cs index 30d931b..6228c2f 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DDateTime.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DDateTime.cs @@ -10,6 +10,8 @@ namespace Luban.Job.Cfg.Datas //public int UnixTime { get; } private readonly int _localTime; + public override string TypeName => "datetime"; + public DDateTime(DateTime time) { this.Time = time; diff --git a/src/Luban.Job.Cfg/Source/Datas/DDouble.cs b/src/Luban.Job.Cfg/Source/Datas/DDouble.cs index 38ffc3a..5efd7c0 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DDouble.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DDouble.cs @@ -6,6 +6,8 @@ namespace Luban.Job.Cfg.Datas { public static DDouble Default { get; } = new DDouble(0); + public override string TypeName => "double"; + public DDouble(double x) : base(x) { } diff --git a/src/Luban.Job.Cfg/Source/Datas/DEnum.cs b/src/Luban.Job.Cfg/Source/Datas/DEnum.cs index e69200d..e190f36 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DEnum.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DEnum.cs @@ -11,6 +11,8 @@ namespace Luban.Job.Cfg.Datas public TEnum Type { get; } + public override string TypeName => "enum"; + public DEnum(TEnum type, string value) { Type = type; diff --git a/src/Luban.Job.Cfg/Source/Datas/DFint.cs b/src/Luban.Job.Cfg/Source/Datas/DFint.cs index 134ed47..8a14237 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DFint.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DFint.cs @@ -6,6 +6,8 @@ namespace Luban.Job.Cfg.Datas { public static DFint Default { get; } = new DFint(0); + public override string TypeName => "fint"; + public DFint(int x) : base(x) { } diff --git a/src/Luban.Job.Cfg/Source/Datas/DFloat.cs b/src/Luban.Job.Cfg/Source/Datas/DFloat.cs index 3e6d51f..20bb155 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DFloat.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DFloat.cs @@ -6,6 +6,8 @@ namespace Luban.Job.Cfg.Datas { private static DFloat Default { get; } = new DFloat(0); + public override string TypeName => "float"; + public static DFloat ValueOf(float x) { if (x == 0) diff --git a/src/Luban.Job.Cfg/Source/Datas/DFlong.cs b/src/Luban.Job.Cfg/Source/Datas/DFlong.cs index 1d7fec6..1b039ed 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DFlong.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DFlong.cs @@ -6,6 +6,8 @@ namespace Luban.Job.Cfg.Datas { public static DFlong Default { get; } = new DFlong(0); + public override string TypeName => "flong"; + public DFlong(long x) : base(x) { } diff --git a/src/Luban.Job.Cfg/Source/Datas/DFshort.cs b/src/Luban.Job.Cfg/Source/Datas/DFshort.cs index 96483cc..c5ba4af 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DFshort.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DFshort.cs @@ -6,6 +6,8 @@ namespace Luban.Job.Cfg.Datas { public static DFshort Default { get; } = new DFshort(0); + public override string TypeName => "fshort"; + public DFshort(short x) : base(x) { } diff --git a/src/Luban.Job.Cfg/Source/Datas/DInt.cs b/src/Luban.Job.Cfg/Source/Datas/DInt.cs index 876d05b..a2c668d 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DInt.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DInt.cs @@ -24,6 +24,8 @@ namespace Luban.Job.Cfg.Datas return new DInt(x); } + public override string TypeName => "int"; + private DInt(int x) : base(x) { } diff --git a/src/Luban.Job.Cfg/Source/Datas/DList.cs b/src/Luban.Job.Cfg/Source/Datas/DList.cs index 1644c57..fe1c00f 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DList.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DList.cs @@ -9,7 +9,7 @@ namespace Luban.Job.Cfg.Datas public TList Type { get; } public List Datas { get; } - public bool IsList => true; + public override string TypeName => "list"; public DList(TList type, List datas) { diff --git a/src/Luban.Job.Cfg/Source/Datas/DLong.cs b/src/Luban.Job.Cfg/Source/Datas/DLong.cs index c200512..3b574fa 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DLong.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DLong.cs @@ -25,6 +25,7 @@ namespace Luban.Job.Cfg.Datas return new DLong(x); } + public override string TypeName => "long"; private DLong(long x) : base(x) { diff --git a/src/Luban.Job.Cfg/Source/Datas/DMap.cs b/src/Luban.Job.Cfg/Source/Datas/DMap.cs index 5118f46..20a15e6 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DMap.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DMap.cs @@ -10,7 +10,7 @@ namespace Luban.Job.Cfg.Datas public TMap Type { get; } public Dictionary Datas { get; } - public bool IsMap => true; + public override string TypeName => "map"; public DMap(TMap type, Dictionary datas) { diff --git a/src/Luban.Job.Cfg/Source/Datas/DSet.cs b/src/Luban.Job.Cfg/Source/Datas/DSet.cs index c0ec347..c8df53c 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DSet.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DSet.cs @@ -10,7 +10,7 @@ namespace Luban.Job.Cfg.Datas public TSet Type { get; } public List Datas { get; } - public bool IsSet => true; + public override string TypeName => "set"; public DSet(TSet type, List datas) { diff --git a/src/Luban.Job.Cfg/Source/Datas/DShort.cs b/src/Luban.Job.Cfg/Source/Datas/DShort.cs index 073af78..1fd70c2 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DShort.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DShort.cs @@ -6,6 +6,8 @@ namespace Luban.Job.Cfg.Datas { public static DShort Default { get; } = new DShort(0); + public override string TypeName => "short"; + public DShort(short x) : base(x) { } diff --git a/src/Luban.Job.Cfg/Source/Datas/DString.cs b/src/Luban.Job.Cfg/Source/Datas/DString.cs index 8140154..61f96ee 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DString.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DString.cs @@ -15,6 +15,8 @@ namespace Luban.Job.Cfg.Datas return new DString(s); } + public override string TypeName => "string"; + private DString(string x) : base(x) { } diff --git a/src/Luban.Job.Cfg/Source/Datas/DText.cs b/src/Luban.Job.Cfg/Source/Datas/DText.cs index 06a03b4..8f5ea26 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DText.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DText.cs @@ -15,6 +15,8 @@ namespace Luban.Job.Cfg.Datas public string RawValue => _rawValue; + public override string TypeName => "text"; + public DText(string key, string x) { Key = key; diff --git a/src/Luban.Job.Cfg/Source/Datas/DType.cs b/src/Luban.Job.Cfg/Source/Datas/DType.cs index 1cb6b22..88ec6b2 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DType.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DType.cs @@ -14,6 +14,8 @@ namespace Luban.Job.Cfg.Datas public abstract TR Apply(IDataFuncVisitor visitor, T x); + public abstract string TypeName { get; } + public override string ToString() { return this.Apply(ToStringVisitor.Ins); diff --git a/src/Luban.Job.Cfg/Source/Datas/DVector2.cs b/src/Luban.Job.Cfg/Source/Datas/DVector2.cs index 2ae5085..c37d634 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DVector2.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DVector2.cs @@ -7,6 +7,8 @@ namespace Luban.Job.Cfg.Datas { public static DVector2 Default { get; } = new DVector2(default); + public override string TypeName => "vector2"; + public DVector2(Vector2 x) : base(x) { } diff --git a/src/Luban.Job.Cfg/Source/Datas/DVector3.cs b/src/Luban.Job.Cfg/Source/Datas/DVector3.cs index 86f202a..79808a3 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DVector3.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DVector3.cs @@ -7,6 +7,8 @@ namespace Luban.Job.Cfg.Datas { public static DVector3 Default { get; } = new DVector3(default); + public override string TypeName => "vector3"; + public DVector3(Vector3 x) : base(x) { } diff --git a/src/Luban.Job.Cfg/Source/Datas/DVector4.cs b/src/Luban.Job.Cfg/Source/Datas/DVector4.cs index 5786f1a..eef146c 100644 --- a/src/Luban.Job.Cfg/Source/Datas/DVector4.cs +++ b/src/Luban.Job.Cfg/Source/Datas/DVector4.cs @@ -7,6 +7,8 @@ namespace Luban.Job.Cfg.Datas { public static DVector4 Default { get; } = new DVector4(default); + public override string TypeName => "vector4"; + public DVector4(Vector4 x) : base(x) { } diff --git a/src/Luban.Job.Cfg/Source/Defs/DefAssembly.cs b/src/Luban.Job.Cfg/Source/Defs/DefAssembly.cs index a9cf7a9..2fcbd1e 100644 --- a/src/Luban.Job.Cfg/Source/Defs/DefAssembly.cs +++ b/src/Luban.Job.Cfg/Source/Defs/DefAssembly.cs @@ -45,6 +45,8 @@ namespace Luban.Job.Cfg.Defs { private static readonly NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger(); + public new static DefAssembly LocalAssebmly { get => (DefAssembly)DefAssemblyBase.LocalAssebmly; set => DefAssemblyBase.LocalAssebmly = value; } + public Service CfgTargetService { get; private set; } private readonly string _branchName; diff --git a/src/Luban.Job.Cfg/Source/Utils/DTypeTemplateExtends.cs b/src/Luban.Job.Cfg/Source/Utils/DTypeTemplateExtends.cs index f91db73..598c4f4 100644 --- a/src/Luban.Job.Cfg/Source/Utils/DTypeTemplateExtends.cs +++ b/src/Luban.Job.Cfg/Source/Utils/DTypeTemplateExtends.cs @@ -1,5 +1,6 @@ using Luban.Job.Cfg.Datas; using Luban.Job.Cfg.DataVisitors; +using Luban.Job.Cfg.Defs; using System; using System.Collections.Generic; using System.Linq; @@ -10,6 +11,17 @@ namespace Luban.Job.Cfg.Utils { class DTypeTemplateExtends : TTypeTemplateExtends { + public static bool IsSimpleLiteralData(DType type) + { + return type.Apply(IsSimpleLiteralDataVisitor.Ins); + } + + public static string ToLocalizedText(DText type) + { + var ass = DefAssembly.LocalAssebmly as DefAssembly; + return type.GetText(ass.ExportTextTable, ass.NotConvertTextSet); + } + public static DType GetField(DBean bean, string fieldName) { int index = 0; @@ -31,12 +43,12 @@ namespace Luban.Job.Cfg.Utils public static string ToJsonLiteral(DType type) { - return type.Apply(ToJsonLiteralVisitor.Ins); + return type != null ? type.Apply(ToJsonLiteralVisitor.Ins) : "null"; } public static string ToLuaLiteral(DType type) { - return type.Apply(ToLuaLiteralVisitor.Ins); + return type != null ? type.Apply(ToLuaLiteralVisitor.Ins) : "nil"; } public static string ToXmlLiteral(DType type) @@ -46,7 +58,7 @@ namespace Luban.Job.Cfg.Utils public static string ToPythonLiteral(DType type) { - return type.Apply(ToPythonLiteralVisitor.Ins); + return type != null ? type.Apply(ToPythonLiteralVisitor.Ins) : "None"; } public static string ToErlangLiteral(DType type) diff --git a/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs b/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs index d09a391..7846bbe 100644 --- a/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs +++ b/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs @@ -32,7 +32,7 @@ namespace Luban.Job.Cfg.Utils case "data_bin": { var buf = ThreadLocalTemporalByteBufPool.Alloc(1024 * 1024); - BinaryExportor.Ins.WriteList(records, table.Assembly, buf); + BinaryExportor.Ins.WriteList(table, records, buf); var bytes = buf.CopyData(); ThreadLocalTemporalByteBufPool.Free(buf); return bytes; @@ -52,30 +52,30 @@ namespace Luban.Job.Cfg.Utils }); if (dataType == "data_json") { - JsonExportor.Ins.WriteAsArray(records, table.Assembly, jsonWriter); + JsonExportor.Ins.WriteAsArray(records, jsonWriter); } else { - Json2Exportor.Ins.WriteAsObject(table, records, table.Assembly, jsonWriter); + Json2Exportor.Ins.WriteAsObject(table, records, jsonWriter); } jsonWriter.Flush(); return System.Text.Encoding.UTF8.GetString(DataUtil.StreamToBytes(ss)); } case "data_lua": { - var content = new List(); + var content = new StringBuilder(); switch (table.Mode) { case ETableMode.ONE: { - LuaExportor.Ins.ExportTableOne(table, records, content); + LuaExportor.Ins.ExportTableSingleton(table, records[0], content); break; } case ETableMode.MAP: { - LuaExportor.Ins.ExportTableOneKeyMap(table, records, content); + LuaExportor.Ins.ExportTableMap(table, records, content); break; } default: @@ -85,6 +85,28 @@ namespace Luban.Job.Cfg.Utils } return string.Join('\n', content); } + 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:{dataType}"); diff --git a/src/Luban.Job.Common/Source/Utils/RenderFileUtil.cs b/src/Luban.Job.Common/Source/Utils/RenderFileUtil.cs index 48692ec..ac338f9 100644 --- a/src/Luban.Job.Common/Source/Utils/RenderFileUtil.cs +++ b/src/Luban.Job.Common/Source/Utils/RenderFileUtil.cs @@ -137,7 +137,7 @@ namespace Luban.Job.Common.Utils public static string GetOutputFileName(string genType, string fileName) { - return $"{(genType.Contains("lua") ? fileName.Replace('.', '_') : fileName)}.{GetOutputFileSuffix(genType)}"; + return $"{fileName.Replace('.', '_')}.{GetOutputFileSuffix(genType)}"; } } }