From 02481f03e16dd9ec2c5d4433d07bf5eccc458c89 Mon Sep 17 00:00:00 2001 From: walon Date: Fri, 20 Aug 2021 18:42:06 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=9B=9E=E6=BB=9A=E3=80=91=E8=80=83?= =?UTF-8?q?=E8=99=91=E5=88=B0=20cfg=20table=E7=9A=84DataList=E5=BF=85?= =?UTF-8?q?=E9=A1=BB=E6=8C=89=E7=85=A7=E9=85=8D=E7=BD=AE=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E7=9A=84=E9=A1=BA=E5=BA=8F=E3=80=82=E5=A6=82=E6=9E=9C=E6=94=B9?= =?UTF-8?q?=E6=88=90data=5Fjson2=E6=A0=BC=E5=BC=8F=EF=BC=8C=E9=A1=BA?= =?UTF-8?q?=E5=BA=8F=E5=B0=B1=E6=97=A0=E6=B3=95=E7=A1=AE=E5=AE=9A=E4=BA=86?= =?UTF-8?q?=E3=80=82=E5=9B=9E=E6=BB=9A=E4=B8=8A=E4=B8=AA=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E4=BF=AE=E6=94=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/DataExporters/Json2Exportor.cs | 31 +---- src/Luban.Job.Cfg/Source/JobController.cs | 11 +- .../TypeVisitors/CsStringDeserialize.cs | 130 ++++++++++++++++++ 3 files changed, 138 insertions(+), 34 deletions(-) create mode 100644 src/Luban.Job.Cfg/Source/TypeVisitors/CsStringDeserialize.cs diff --git a/src/Luban.Job.Cfg/Source/DataExporters/Json2Exportor.cs b/src/Luban.Job.Cfg/Source/DataExporters/Json2Exportor.cs index e598fc3..8a40196 100644 --- a/src/Luban.Job.Cfg/Source/DataExporters/Json2Exportor.cs +++ b/src/Luban.Job.Cfg/Source/DataExporters/Json2Exportor.cs @@ -1,4 +1,5 @@ using Luban.Job.Cfg.Datas; +using Luban.Job.Cfg.DataVisitors; using Luban.Job.Cfg.Defs; using Luban.Job.Common.Types; using System; @@ -16,38 +17,10 @@ namespace Luban.Job.Cfg.DataExporters public override void Accept(DMap type, DefAssembly ass, Utf8JsonWriter x) { - var keyType = type.Type.KeyType; - if (keyType is not TString - && keyType is not TInt - && keyType is not TLong) - { - throw new Exception($"data_json2格式只支持key为int,long,string类型的map"); - } x.WriteStartObject(); foreach (var d in type.Datas) { - switch (d.Key) - { - case DString ds: - { - x.WritePropertyName(ds.Value); - break; - } - case DInt di: - { - x.WritePropertyName(di.Value.ToString()); - break; - } - case DLong dl: - { - x.WritePropertyName(dl.Value.ToString()); - break; - } - default: - { - throw new Exception($"data_json2格式只支持key为int,long,string类型的map"); - } - } + x.WritePropertyName(d.Key.Apply(ToJsonPropertyNameVisitor.Ins)); d.Value.Apply(this, ass, x); } x.WriteEndObject(); diff --git a/src/Luban.Job.Cfg/Source/JobController.cs b/src/Luban.Job.Cfg/Source/JobController.cs index e4b8483..15ec0b2 100644 --- a/src/Luban.Job.Cfg/Source/JobController.cs +++ b/src/Luban.Job.Cfg/Source/JobController.cs @@ -46,7 +46,7 @@ namespace Luban.Job.Cfg [Option("output_data_json_monolithic_file", Required = false, HelpText = "output monolithic json file")] public string OutputDataJsonMonolithicFile { get; set; } - [Option("gen_types", Required = true, HelpText = "code_cs_bin,code_cs_json,code_cs_unity_json,code_lua_bin,code_java_bin,code_go_bin,code_go_json,code_cpp_bin,code_python3_json,code_typescript_bin,code_typescript_json,data_bin,data_lua,data_json,data_json_monolithic,data_resources . can be multi")] + [Option("gen_types", Required = true, HelpText = "code_cs_bin,code_cs_json,code_cs_unity_json,code_lua_bin,code_java_bin,code_go_bin,code_go_json,code_cpp_bin,code_python3_json,code_typescript_bin,code_typescript_json,data_bin,data_lua,data_json,data_json2,data_json_monolithic,data_resources . can be multi")] public string GenType { get; set; } [Option('s', "service", Required = true, HelpText = "service")] @@ -126,7 +126,8 @@ namespace Luban.Job.Cfg switch (genType) { case "data_bin": return "bin"; - case "data_json": return "json"; + case "data_json": + case "data_json2": return "json"; case "data_lua": return "lua"; default: throw new Exception($"not support output data type:{genType}"); } @@ -167,8 +168,7 @@ namespace Luban.Job.Cfg errMsg = "--outputcodedir missing"; return false; } - List dataGenTypes = genTypes.Where(t => t.StartsWith("data_", StringComparison.Ordinal)).ToList(); - if (dataGenTypes.Count > 0) + if (genTypes.Any(t => t.StartsWith("data_", StringComparison.Ordinal))) { if (string.IsNullOrWhiteSpace(inputDataDir)) { @@ -384,6 +384,7 @@ namespace Luban.Job.Cfg } case "data_bin": case "data_json": + case "data_json2": case "data_lua": { await CheckLoadCfgDataAsync(); @@ -786,7 +787,7 @@ namespace {ctx.TopModule} { allJsonTask.Add(Task.Run(() => { - return (string)DataExporterUtil.ToOutputData(c, ctx.Assembly.GetTableExportDataList(c), "data_json"); + return (string)DataExporterUtil.ToOutputData(c, ctx.Assembly.GetTableExportDataList(c), "data_json2"); })); } diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsStringDeserialize.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsStringDeserialize.cs new file mode 100644 index 0000000..36a65d8 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsStringDeserialize.cs @@ -0,0 +1,130 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Cfg.TypeVisitors +{ + class CsStringDeserialize : ITypeFuncVisitor + { + public static CsStringDeserialize Ins { get; } = new(); + + public string Accept(TBool type, string strName, string varName) + { + return $"{varName} = bool.Parse({strName});"; + } + + public string Accept(TByte type, string strName, string varName) + { + return $"{varName} = byte.Parse({strName});"; + } + + public string Accept(TShort type, string strName, string varName) + { + return $"{varName} = short.Parse({strName});"; + } + + public string Accept(TFshort type, string strName, string varName) + { + return $"{varName} = short.Parse({strName});"; + } + + public string Accept(TInt type, string strName, string varName) + { + return $"{varName} = int.Parse({strName});"; + } + + public string Accept(TFint type, string strName, string varName) + { + return $"{varName} = int.Parse({strName});"; + } + + public string Accept(TLong type, string strName, string varName) + { + return $"{varName} = long.Parse({strName});"; + } + + public string Accept(TFlong type, string strName, string varName) + { + return $"{varName} = long.Parse({strName});"; + } + + public string Accept(TFloat type, string strName, string varName) + { + return $"{varName} = float.Parse({strName});"; + } + + public string Accept(TDouble type, string strName, string varName) + { + return $"{varName} = double.Parse({strName});"; + } + + public string Accept(TEnum type, string strName, string varName) + { + return $"{varName} = ({type.Apply(CsDefineTypeName.Ins)})int.Parse({strName});"; + } + + public string Accept(TString type, string strName, string varName) + { + return $"{varName} = {strName};"; + } + + public string Accept(TBytes type, string strName, string varName) + { + throw new NotSupportedException(); + } + + public string Accept(TText type, string strName, string varName) + { + throw new NotSupportedException(); + } + + public string Accept(TBean type, string strName, string varName) + { + throw new NotSupportedException(); + } + + public string Accept(TArray type, string strName, string varName) + { + throw new NotSupportedException(); + } + + public string Accept(TList type, string strName, string varName) + { + throw new NotSupportedException(); + } + + public string Accept(TSet type, string strName, string varName) + { + throw new NotSupportedException(); + } + + public string Accept(TMap type, string strName, string varName) + { + throw new NotSupportedException(); + } + + public string Accept(TVector2 type, string strName, string varName) + { + throw new NotSupportedException(); + } + + public string Accept(TVector3 type, string strName, string varName) + { + throw new NotSupportedException(); + } + + public string Accept(TVector4 type, string strName, string varName) + { + throw new NotSupportedException(); + } + + public string Accept(TDateTime type, string strName, string varName) + { + throw new NotSupportedException(); + } + } +}