From 02f3ebd97994c902909a6d184b8b7d8ec7d9bdd1 Mon Sep 17 00:00:00 2001 From: Carson Lin <396098651@qq.com> Date: Wed, 13 Jul 2022 11:41:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BC=83=E7=94=A8Type=E7=9A=84Collecti?= =?UTF-8?q?onLevel=EF=BC=8C=E6=94=B9=E4=B8=BA=E5=9C=A8visitor=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0depth=E5=8F=82=E6=95=B0=E4=BB=A5=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E5=B5=8C=E5=A5=97=E5=AE=B9=E5=99=A8=E7=9A=84=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/TypeVisitors/CsJsonDeserialize.cs | 91 +++++++++--------- .../TypeVisitors/CsUnityJsonDeserialize.cs | 93 ++++++++++--------- .../Source/Utils/TTypeTemplateExtends.cs | 8 +- .../Source/Defs/DefAssemblyBase.cs | 4 +- .../TypeVisitors/CsDeserializeVisitor.cs | 4 +- .../CsUnderingDeserializeVisitor.cs | 82 ++++++++-------- src/Luban.Job.Common/Source/Types/TArray.cs | 12 +-- src/Luban.Job.Common/Source/Types/TList.cs | 10 -- src/Luban.Job.Common/Source/Types/TSet.cs | 10 -- src/Luban.Job.Common/Source/Types/TType.cs | 4 +- 10 files changed, 146 insertions(+), 172 deletions(-) diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs index 4cf1a55..ed0243e 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs @@ -5,92 +5,91 @@ using System; namespace Luban.Job.Cfg.TypeVisitors { - class CsJsonDeserialize : ITypeFuncVisitor + class CsJsonDeserialize : ITypeFuncVisitor { public static CsJsonDeserialize Ins { get; } = new(); - public string Accept(TBool type, string json, string x) + public string Accept(TBool type, string json, string x, int depth) { return $"{x} = {json}.GetBoolean();"; } - public string Accept(TByte type, string json, string x) + public string Accept(TByte type, string json, string x, int depth) { return $"{x} = {json}.GetByte();"; } - public string Accept(TShort type, string json, string x) + public string Accept(TShort type, string json, string x, int depth) { return $"{x} = {json}.GetInt16();"; } - public string Accept(TFshort type, string json, string x) + public string Accept(TFshort type, string json, string x, int depth) { return $"{x} = {json}.GetInt16();"; } - public string Accept(TInt type, string json, string x) + public string Accept(TInt type, string json, string x, int depth) { return $"{x} = {json}.GetInt32();"; } - public string Accept(TFint type, string json, string x) + public string Accept(TFint type, string json, string x, int depth) { return $"{x} = {json}.GetInt32();"; } - public string Accept(TLong type, string json, string x) + public string Accept(TLong type, string json, string x, int depth) { return $"{x} = {json}.GetInt64();"; } - public string Accept(TFlong type, string json, string x) + public string Accept(TFlong type, string json, string x, int depth) { return $"{x} = {json}.GetInt64();"; } - public string Accept(TFloat type, string json, string x) + public string Accept(TFloat type, string json, string x, int depth) { return $"{x} = {json}.GetSingle();"; } - public string Accept(TDouble type, string json, string x) + public string Accept(TDouble type, string json, string x, int depth) { return $"{x} = {json}.GetDouble();"; } - public string Accept(TEnum type, string json, string x) + public string Accept(TEnum type, string json, string x, int depth) { return $"{x} = ({type.CsUnderingDefineType()}){json}.GetInt32();"; } - public string Accept(TString type, string json, string x) + public string Accept(TString type, string json, string x, int depth) { return $"{x} = {json}.GetString();"; } - public string Accept(TBytes type, string json, string x) + public string Accept(TBytes type, string json, string x, int depth) { throw new NotSupportedException(); } - public string Accept(TText type, string json, string x) + public string Accept(TText type, string json, string x, int depth) { return $"{x}{TText.L10N_FIELD_SUFFIX} = {json}.GetProperty(\"{DText.KEY_NAME}\").GetString();{x} = {json}.GetProperty(\"{DText.TEXT_NAME}\").GetString();"; } - public string Accept(TBean type, string json, string x) + public string Accept(TBean type, string json, string x, int depth) { return $"{x} = {type.CsUnderingDefineType()}.Deserialize{type.Bean.Name}({json});"; } - public string Accept(TArray type, string json, string x) + public string Accept(TArray type, string json, string x, int depth) { - int level = type.CollectionLevel; - string _n = $"_n{level}"; - string __e = $"__e{level}"; - string __v = $"__v{level}"; - string __json = $"__json{level}"; - string __index = $"__index{level}"; + string _n = $"_n{depth}"; + string __e = $"__e{depth}"; + string __v = $"__v{depth}"; + string __json = $"__json{depth}"; + string __index = $"__index{depth}"; string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{_n}]"; if (type.Dimension > 1) { @@ -104,48 +103,50 @@ namespace Luban.Job.Cfg.TypeVisitors typeStr += "[]"; } } - return $"{{ var {__json} = {json}; int {_n} = {__json}.GetArrayLength(); {x} = new {typeStr}; int {__index}=0; foreach(JsonElement {__e} in {__json}.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, $"{__e}", $"{__v}")} {x}[{__index}++] = {__v}; }} }}"; + return $"{{ var {__json} = {json}; int {_n} = {__json}.GetArrayLength(); {x} = new {typeStr}; int {__index}=0; foreach(JsonElement {__e} in {__json}.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, $"{__e}", $"{__v}", depth + 1)} {x}[{__index}++] = {__v}; }} }}"; } - public string Accept(TList type, string json, string x) + public string Accept(TList type, string json, string x, int depth) { - int level = type.CollectionLevel; - string __e = $"__e{level}"; - string __v = $"__v{level}"; - string __json = $"__json{level}"; - return $"{{ var {__json} = {json}; {x} = new {type.CsUnderingDefineType()}({__json}.GetArrayLength()); foreach(JsonElement {__e} in {__json}.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, $"{__e}", $"{__v}")} {x}.Add({__v}); }} }}"; + string __e = $"__e{depth}"; + string __v = $"__v{depth}"; + string __json = $"__json{depth}"; + return $"{{ var {__json} = {json}; {x} = new {type.CsUnderingDefineType()}({__json}.GetArrayLength()); foreach(JsonElement {__e} in {__json}.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, $"{__e}", $"{__v}", depth + 1)} {x}.Add({__v}); }} }}"; } - public string Accept(TSet type, string json, string x) + public string Accept(TSet type, string json, string x, int depth) { - int level = type.CollectionLevel; - string __e = $"__e{level}"; - string __v = $"__v{level}"; - string __json = $"__json{level}"; - return $"{{ var {__json} = {json}; {x} = new {type.CsUnderingDefineType()}({__json}.GetArrayLength()); foreach(JsonElement {__e} in {__json}.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, $"{__e}", $"{__v}")} {x}.Add({__v}); }} }}"; + string __e = $"__e{depth}"; + string __v = $"__v{depth}"; + string __json = $"__json{depth}"; + return $"{{ var {__json} = {json}; {x} = new {type.CsUnderingDefineType()}({__json}.GetArrayLength()); foreach(JsonElement {__e} in {__json}.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, $"{__e}", $"{__v}", depth + 1)} {x}.Add({__v}); }} }}"; } - public string Accept(TMap type, string json, string x) + public string Accept(TMap type, string json, string x, int depth) { - return @$"{{ var _json0 = {json}; {x} = new {type.CsUnderingDefineType()}(_json0.GetArrayLength()); foreach(JsonElement __e in _json0.EnumerateArray()) {{ {type.KeyType.CsUnderingDefineType()} __k; {type.KeyType.Apply(this, "__e[0]", "__k")} {type.ValueType.CsUnderingDefineType()} __v; {type.ValueType.Apply(this, "__e[1]", "__v")} {x}.Add(__k, __v); }} }}"; + string __e = $"__e{depth}"; + string __k = $"_k{depth}"; + string __v = $"_v{depth}"; + string __json = $"__json{depth}"; + return @$"{{ var {__json} = {json}; {x} = new {type.CsUnderingDefineType()}({__json}.GetArrayLength()); foreach(JsonElement {__e} in {__json}.EnumerateArray()) {{ {type.KeyType.CsUnderingDefineType()} {__k}; {type.KeyType.Apply(this, $"{__e}[0]", __k,depth + 1)} {type.ValueType.CsUnderingDefineType()} {__v}; {type.ValueType.Apply(this, $"{__e}[1]", __v, depth + 1)} {x}.Add({__k}, {__v}); }} }}"; } - public string Accept(TVector2 type, string json, string x) + public string Accept(TVector2 type, string json, string x, int depth) { - return $"{{ var _json0 = {json}; float __x; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"y\")", "__y") } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y); }}"; + return $"{{ var _json0 = {json}; float __x; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"x\")", "__x", depth) } float __y; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"y\")", "__y", depth) } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y); }}"; } - public string Accept(TVector3 type, string json, string x) + public string Accept(TVector3 type, string json, string x, int depth) { - return $"{{ var _json0 = {json}; float __x; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"y\")", "__y") } float __z; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"z\")", "__z") } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y,__z); }}"; + return $"{{ var _json0 = {json}; float __x; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"x\")", "__x", depth) } float __y; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"y\")", "__y", depth) } float __z; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"z\")", "__z", depth) } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y,__z); }}"; } - public string Accept(TVector4 type, string json, string x) + public string Accept(TVector4 type, string json, string x, int depth) { - return $"{{ var _json0 = {json}; float __x; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"y\")", "__y") } float __z; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"z\")", "__z") } float __w; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"w\")", "__w") } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y, __z, __w); }}"; + return $"{{ var _json0 = {json}; float __x; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"x\")", "__x", depth) } float __y; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"y\")", "__y", depth) } float __z; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"z\")", "__z", depth) } float __w; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"w\")", "__w", depth) } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y, __z, __w); }}"; } - public string Accept(TDateTime type, string json, string x) + public string Accept(TDateTime type, string json, string x, int depth) { return $"{x} = {json}.GetInt32();"; } diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs index 1f15b32..d85b7f7 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs @@ -5,93 +5,92 @@ using System; namespace Luban.Job.Cfg.TypeVisitors { - class CsUnityJsonDeserialize : ITypeFuncVisitor + class CsUnityJsonDeserialize : ITypeFuncVisitor { public static CsUnityJsonDeserialize Ins { get; } = new(); - public string Accept(TBool type, string json, string x) + public string Accept(TBool type, string json, string x, int depth) { return $"{{ if(!{json}.IsBoolean) {{ throw new SerializationException(); }} {x} = {json}; }}"; } - public string Accept(TByte type, string json, string x) + public string Accept(TByte type, string json, string x, int depth) { return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}"; } - public string Accept(TShort type, string json, string x) + public string Accept(TShort type, string json, string x, int depth) { return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}"; } - public string Accept(TFshort type, string json, string x) + public string Accept(TFshort type, string json, string x, int depth) { return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}"; } - public string Accept(TInt type, string json, string x) + public string Accept(TInt type, string json, string x, int depth) { return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}"; } - public string Accept(TFint type, string json, string x) + public string Accept(TFint type, string json, string x, int depth) { return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}"; } - public string Accept(TLong type, string json, string x) + public string Accept(TLong type, string json, string x, int depth) { return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}"; } - public string Accept(TFlong type, string json, string x) + public string Accept(TFlong type, string json, string x, int depth) { return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}"; } - public string Accept(TFloat type, string json, string x) + public string Accept(TFloat type, string json, string x, int depth) { return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}"; } - public string Accept(TDouble type, string json, string x) + public string Accept(TDouble type, string json, string x, int depth) { return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}"; } - public string Accept(TEnum type, string json, string x) + public string Accept(TEnum type, string json, string x, int depth) { return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = ({type.CsUnderingDefineType()}){json}.AsInt; }}"; } - public string Accept(TString type, string json, string x) + public string Accept(TString type, string json, string x, int depth) { return $"{{ if(!{json}.IsString) {{ throw new SerializationException(); }} {x} = {json}; }}"; } - public string Accept(TBytes type, string json, string x) + public string Accept(TBytes type, string json, string x, int depth) { throw new NotSupportedException(); } - public string Accept(TText type, string json, string x) + public string Accept(TText type, string json, string x, int depth) { return $"{{ if(!{json}[\"{DText.KEY_NAME}\"].IsString) {{ throw new SerializationException(); }} {x}{TText.L10N_FIELD_SUFFIX} = {json}[\"{DText.KEY_NAME}\"]; if(!{json}[\"{DText.TEXT_NAME}\"].IsString) {{ throw new SerializationException(); }} {x} = {json}[\"{DText.TEXT_NAME}\"]; }}"; } - public string Accept(TBean type, string json, string x) + public string Accept(TBean type, string json, string x, int depth) { return $"{{ if(!{json}.IsObject) {{ throw new SerializationException(); }} {x} = {type.CsUnderingDefineType()}.Deserialize{type.Bean.Name}({json}); }}"; } - public string Accept(TArray type, string json, string x) + public string Accept(TArray type, string json, string x, int depth) { - int level = type.CollectionLevel; - string _n = $"_n{level}"; - string __e = $"__e{level}"; - string __v = $"__v{level}"; - string __json = $"__json{level}"; - string __index = $"__index{level}"; + string _n = $"_n{depth}"; + string __e = $"__e{depth}"; + string __v = $"__v{depth}"; + string __json = $"__json{depth}"; + string __index = $"__index{depth}"; string tempJsonName = __json; string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{_n}]"; if (type.Dimension > 1) @@ -106,54 +105,56 @@ namespace Luban.Job.Cfg.TypeVisitors typeStr += "[]"; } } - return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} int {_n} = {tempJsonName}.Count; {x} = new {typeStr}; int {__index}=0; foreach(JSONNode {__e} in {tempJsonName}.Children) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, __e, __v)} {x}[{__index}++] = {__v}; }} }}"; + return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} int {_n} = {tempJsonName}.Count; {x} = new {typeStr}; int {__index}=0; foreach(JSONNode {__e} in {tempJsonName}.Children) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, __e, __v, depth + 1)} {x}[{__index}++] = {__v}; }} }}"; } - public string Accept(TList type, string json, string x) + public string Accept(TList type, string json, string x, int depth) { - int level = type.CollectionLevel; - string __e = $"__e{level}"; - string __v = $"__v{level}"; - string __json = $"__json{level}"; + string __e = $"__e{depth}"; + string __v = $"__v{depth}"; + string __json = $"__json{depth}"; string tempJsonName = __json; - return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} {x} = new {type.CsUnderingDefineType()}({tempJsonName}.Count); foreach(JSONNode {__e} in {tempJsonName}.Children) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, __e, __v)} {x}.Add({__v}); }} }}"; + return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} {x} = new {type.CsUnderingDefineType()}({tempJsonName}.Count); foreach(JSONNode {__e} in {tempJsonName}.Children) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, __e, __v, depth + 1)} {x}.Add({__v}); }} }}"; } - public string Accept(TSet type, string json, string x) + public string Accept(TSet type, string json, string x, int depth) { - int level = type.CollectionLevel; - string __e = $"__e{level}"; - string __v = $"__v{level}"; - string __json = $"__json{level}"; + string __e = $"__e{depth}"; + string __v = $"__v{depth}"; + string __json = $"__json{depth}"; string tempJsonName = __json; - return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} {x} = new {type.CsUnderingDefineType()}(/*{tempJsonName}.Count*/); foreach(JSONNode {__e} in {tempJsonName}.Children) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, __e, __v)} {x}.Add({__v}); }} }}"; + return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} {x} = new {type.CsUnderingDefineType()}(/*{tempJsonName}.Count*/); foreach(JSONNode {__e} in {tempJsonName}.Children) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, __e, __v, depth + 1)} {x}.Add({__v}); }} }}"; } - public string Accept(TMap type, string json, string x) + public string Accept(TMap type, string json, string x, int depth) { - string tempJsonName = $"_json1"; - return @$"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} {x} = new {type.CsUnderingDefineType()}({tempJsonName}.Count); foreach(JSONNode __e in {tempJsonName}.Children) {{ {type.KeyType.CsUnderingDefineType()} __k; {type.KeyType.Apply(this, "__e[0]", "__k")} {type.ValueType.CsUnderingDefineType()} __v; {type.ValueType.Apply(this, "__e[1]", "__v")} {x}.Add(__k, __v); }} }}"; + string __e = $"__e{depth}"; + string __k = $"_k{depth}"; + string __v = $"_v{depth}"; + string __json = $"__json{depth}"; + string tempJsonName = __json; + return @$"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} {x} = new {type.CsUnderingDefineType()}({tempJsonName}.Count); foreach(JSONNode {__e} in {tempJsonName}.Children) {{ {type.KeyType.CsUnderingDefineType()} {__k}; {type.KeyType.Apply(this, $"{__e}[0]", __k, depth + 1)} {type.ValueType.CsUnderingDefineType()} { __v}; {type.ValueType.Apply(this, $"{__e}[1]", __v, depth + 1)} {x}.Add({__k}, { __v}); }} }}"; } - public string Accept(TVector2 type, string json, string x) + public string Accept(TVector2 type, string json, string x, int depth) { string tempJsonName = $"_json2"; - return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"x\"]", "__x") } float __y; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"y\"]", "__y") } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y); }}"; + return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"x\"]", "__x", depth) } float __y; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"y\"]", "__y", depth) } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y); }}"; } - public string Accept(TVector3 type, string json, string x) + public string Accept(TVector3 type, string json, string x, int depth) { string tempJsonName = $"_json2"; - return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"x\"]", "__x") } float __y; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"y\"]", "__y") } float __z; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"z\"]", "__z") } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y,__z); }}"; + return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"x\"]", "__x", depth) } float __y; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"y\"]", "__y", depth) } float __z; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"z\"]", "__z", depth) } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y,__z); }}"; } - public string Accept(TVector4 type, string json, string x) + public string Accept(TVector4 type, string json, string x, int depth) { string tempJsonName = $"_json2"; - return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"x\"]", "__x") } float __y; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"y\"]", "__y") } float __z; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"z\"]", "__z") } float __w; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"w\"]", "__w") } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y, __z, __w); }}"; + return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"x\"]", "__x", depth) } float __y; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"y\"]", "__y", depth) } float __z; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"z\"]", "__z", depth) } float __w; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"w\"]", "__w", depth) } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y, __z, __w); }}"; } - public string Accept(TDateTime type, string json, string x) + public string Accept(TDateTime type, string json, string x, int depth) { return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}"; } diff --git a/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs b/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs index 5690fb3..5eec48d 100644 --- a/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs +++ b/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs @@ -29,11 +29,11 @@ namespace Luban.Job.Cfg.Utils { if (type.IsNullable) { - return $"{{ if ({bufName}.TryGetProperty(\"{jsonFieldName}\", out var _j) && _j.ValueKind != JsonValueKind.Null) {{ {type.Apply(TypeVisitors.CsJsonDeserialize.Ins, "_j", fieldName)} }} else {{ {fieldName} = null; }} }}"; + return $"{{ if ({bufName}.TryGetProperty(\"{jsonFieldName}\", out var _j) && _j.ValueKind != JsonValueKind.Null) {{ {type.Apply(TypeVisitors.CsJsonDeserialize.Ins, "_j", fieldName, 0)} }} else {{ {fieldName} = null; }} }}"; } else { - return type.Apply(TypeVisitors.CsJsonDeserialize.Ins, $"{bufName}.GetProperty(\"{jsonFieldName}\")", fieldName); + return type.Apply(TypeVisitors.CsJsonDeserialize.Ins, $"{bufName}.GetProperty(\"{jsonFieldName}\")", fieldName, 0); } } @@ -46,11 +46,11 @@ namespace Luban.Job.Cfg.Utils { if (type.IsNullable) { - return $"{{ var _j = {bufName}[\"{jsonFieldName}\"]; if (_j.Tag != JSONNodeType.None && _j.Tag != JSONNodeType.NullValue) {{ {type.Apply(TypeVisitors.CsUnityJsonDeserialize.Ins, "_j", fieldName)} }} else {{ {fieldName} = null; }} }}"; + return $"{{ var _j = {bufName}[\"{jsonFieldName}\"]; if (_j.Tag != JSONNodeType.None && _j.Tag != JSONNodeType.NullValue) {{ {type.Apply(TypeVisitors.CsUnityJsonDeserialize.Ins, "_j", fieldName, 0)} }} else {{ {fieldName} = null; }} }}"; } else { - return type.Apply(TypeVisitors.CsUnityJsonDeserialize.Ins, $"{bufName}[\"{jsonFieldName}\"]", fieldName); + return type.Apply(TypeVisitors.CsUnityJsonDeserialize.Ins, $"{bufName}[\"{jsonFieldName}\"]", fieldName, 0); } } diff --git a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs index c1541f5..64e33d5 100644 --- a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs @@ -364,7 +364,7 @@ namespace Luban.Job.Common.Defs } return TMap.Create(false, tags, CreateNotContainerType(module, keyValueType.Substring(0, typeSepIndex).Trim()), - CreateNotContainerType(module, keyValueType.Substring(typeSepIndex + 1).Trim()), isTreeMap); + CreateType(module, keyValueType.Substring(typeSepIndex + 1).Trim()), isTreeMap); } protected TType CreateContainerType(string module, string containerType, Dictionary containerTags, string elementType) @@ -381,7 +381,7 @@ namespace Luban.Job.Common.Defs TType type = CreateType(module, elementType); if (type.IsCollection) { - throw new Exception("set的元素不支持容器类型,请改为嵌套list"); + throw new Exception("set的元素不支持容器类型"); } return TSet.Create(false, containerTags, type, false); } diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsDeserializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsDeserializeVisitor.cs index abf6b63..9954f90 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsDeserializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsDeserializeVisitor.cs @@ -9,11 +9,11 @@ namespace Luban.Job.Common.TypeVisitors { if (type.IsNullable) { - return $"if({bufName}.ReadBool()){{ {type.Apply(CsUnderingDeserializeVisitor.Ins, bufName, fieldName)} }} else {{ {fieldName} = null; }}"; + return $"if({bufName}.ReadBool()){{ {type.Apply(CsUnderingDeserializeVisitor.Ins, bufName, fieldName, 0)} }} else {{ {fieldName} = null; }}"; } else { - return type.Apply(CsUnderingDeserializeVisitor.Ins, bufName, fieldName); + return type.Apply(CsUnderingDeserializeVisitor.Ins, bufName, fieldName, 0); } } diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs index f56701c..8cc229a 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs @@ -4,92 +4,91 @@ using Luban.Job.Common.Utils; namespace Luban.Job.Common.TypeVisitors { - class CsUnderingDeserializeVisitor : ITypeFuncVisitor + class CsUnderingDeserializeVisitor : ITypeFuncVisitor { public static CsUnderingDeserializeVisitor Ins { get; } = new CsUnderingDeserializeVisitor(); - public string Accept(TBool type, string bufName, string fieldName) + public string Accept(TBool type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.ReadBool();"; } - public string Accept(TByte type, string bufName, string fieldName) + public string Accept(TByte type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.ReadByte();"; } - public string Accept(TShort type, string bufName, string fieldName) + public string Accept(TShort type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.ReadShort();"; } - public string Accept(TFshort type, string bufName, string fieldName) + public string Accept(TFshort type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.ReadFshort();"; } - public string Accept(TInt type, string bufName, string fieldName) + public string Accept(TInt type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.ReadInt();"; } - public string Accept(TFint type, string bufName, string fieldName) + public string Accept(TFint type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.ReadFint();"; } - public string Accept(TLong type, string bufName, string fieldName) + public string Accept(TLong type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.ReadLong();"; } - public string Accept(TFlong type, string bufName, string fieldName) + public string Accept(TFlong type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.ReadFlong();"; } - public string Accept(TFloat type, string bufName, string fieldName) + public string Accept(TFloat type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.ReadFloat();"; } - public string Accept(TDouble type, string bufName, string fieldName) + public string Accept(TDouble type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.ReadDouble();"; } - public string Accept(TEnum type, string bufName, string fieldName) + public string Accept(TEnum type, string bufName, string fieldName, int depth) { return $"{fieldName} = ({ type.Apply(CsUnderingDefineTypeName.Ins)}){bufName}.ReadInt();"; } - public string Accept(TString type, string bufName, string fieldName) + public string Accept(TString type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.ReadString();"; } - public string Accept(TBytes type, string bufName, string fieldName) + public string Accept(TBytes type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.ReadBytes();"; } - public string Accept(TText type, string bufName, string fieldName) + public string Accept(TText type, string bufName, string fieldName, int depth) { return $"{fieldName}{TText.L10N_FIELD_SUFFIX} = {bufName}.ReadString(); {fieldName} = {bufName}.ReadString();"; } - public string Accept(TBean type, string bufName, string fieldName) + public string Accept(TBean type, string bufName, string fieldName, int depth) { string src = $"{type.Bean.FullName}.Deserialize{type.Bean.Name}({bufName})"; return $"{fieldName} = {ExternalTypeUtil.CsCloneToExternal(type.Bean.FullName, src)};"; } - public string Accept(TArray type, string bufName, string fieldName) + public string Accept(TArray type, string bufName, string fieldName, int depth) { - int level = type.CollectionLevel; - string __n = $"__n{level}"; - string __e = $"__e{level}"; - string __index = $"__index{level}"; + string __n = $"__n{depth}"; + string __e = $"__e{depth}"; + string __index = $"__index{depth}"; string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{__n}]"; if (type.Dimension > 1) { @@ -103,54 +102,55 @@ namespace Luban.Job.Common.TypeVisitors typeStr += "[]"; } } - return $"{{int {__n} = System.Math.Min({bufName}.ReadSize(), {bufName}.Size);{fieldName} = new {typeStr};for(var {__index} = 0 ; {__index} < {__n} ; {__index}++) {{ {type.ElementType.Apply(CsDefineTypeName.Ins)} {__e};{type.ElementType.Apply(this, bufName, $"{__e}")} {fieldName}[{__index}] = {__e};}}}}"; + return $"{{int {__n} = System.Math.Min({bufName}.ReadSize(), {bufName}.Size);{fieldName} = new {typeStr};for(var {__index} = 0 ; {__index} < {__n} ; {__index}++) {{ {type.ElementType.Apply(CsDefineTypeName.Ins)} {__e};{type.ElementType.Apply(this, bufName, $"{__e}", depth + 1)} {fieldName}[{__index}] = {__e};}}}}"; } - public string Accept(TList type, string bufName, string fieldName) + public string Accept(TList type, string bufName, string fieldName, int depth) { - int level = type.CollectionLevel; - string n = $"n{level}"; - string _e = $"_e{level}"; - string i = $"i{level}"; - return $"{{int {n} = System.Math.Min({bufName}.ReadSize(), {bufName}.Size);{fieldName} = new {type.Apply(CsDefineTypeName.Ins)}({n});for(var {i} = 0 ; {i} < {n} ; {i}++) {{ {type.ElementType.Apply(CsDefineTypeName.Ins)} {_e}; {type.ElementType.Apply(this, bufName, $"{_e}")} {fieldName}.Add({_e});}}}}"; + string n = $"n{depth}"; + string _e = $"_e{depth}"; + string i = $"i{depth}"; + return $"{{int {n} = System.Math.Min({bufName}.ReadSize(), {bufName}.Size);{fieldName} = new {type.Apply(CsDefineTypeName.Ins)}({n});for(var {i} = 0 ; {i} < {n} ; {i}++) {{ {type.ElementType.Apply(CsDefineTypeName.Ins)} {_e}; {type.ElementType.Apply(this, bufName, $"{_e}", depth + 1)} {fieldName}.Add({_e});}}}}"; } - public string Accept(TSet type, string bufName, string fieldName) + public string Accept(TSet type, string bufName, string fieldName, int depth) { - int level = type.CollectionLevel; - string n = $"n{level}"; - string _e = $"_e{level}"; - string i = $"i{level}"; - return $"{{int {n} = System.Math.Min({bufName}.ReadSize(), {bufName}.Size);{fieldName} = new {type.Apply(CsDefineTypeName.Ins)}(/*{n} * 3 / 2*/);for(var {i} = 0 ; {i} < {n} ; {i}++) {{ {type.ElementType.Apply(CsDefineTypeName.Ins)} {_e}; {type.ElementType.Apply(this, bufName, $"{_e}")} {fieldName}.Add({_e});}}}}"; + string n = $"n{depth}"; + string _e = $"_e{depth}"; + string i = $"i{depth}"; + return $"{{int {n} = System.Math.Min({bufName}.ReadSize(), {bufName}.Size);{fieldName} = new {type.Apply(CsDefineTypeName.Ins)}(/*{n} * 3 / 2*/);for(var {i} = 0 ; {i} < {n} ; {i}++) {{ {type.ElementType.Apply(CsDefineTypeName.Ins)} {_e}; {type.ElementType.Apply(this, bufName, $"{_e}", +1)} {fieldName}.Add({_e});}}}}"; } - public string Accept(TMap type, string bufName, string fieldName) + public string Accept(TMap type, string bufName, string fieldName, int depth) { - return $"{{int n = System.Math.Min({bufName}.ReadSize(), {bufName}.Size);{fieldName} = new {type.Apply(CsDefineTypeName.Ins)}(n * 3 / 2);for(var i = 0 ; i < n ; i++) {{ {type.KeyType.Apply(CsDefineTypeName.Ins)} _k; {type.KeyType.Apply(this, bufName, "_k")} {type.ValueType.Apply(CsDefineTypeName.Ins)} _v; {type.ValueType.Apply(this, bufName, "_v")} {fieldName}.Add(_k, _v);}}}}"; - + string n = $"n{depth}"; + string _k = $"_k{depth}"; + string _v = $"_v{depth}"; + string i = $"i{depth}"; + return $"{{int {n} = System.Math.Min({bufName}.ReadSize(), {bufName}.Size);{fieldName} = new {type.Apply(CsDefineTypeName.Ins)}({n} * 3 / 2);for(var {i} = 0 ; {i} < {n} ; {i}++) {{ {type.KeyType.Apply(CsDefineTypeName.Ins)} {_k}; {type.KeyType.Apply(this, bufName, _k, depth + 1)} {type.ValueType.Apply(CsDefineTypeName.Ins)} {_v}; {type.ValueType.Apply(this, bufName, _v, depth + 1)} {fieldName}.Add({_k}, {_v});}}}}"; } public static string VectorName => (DefAssemblyBase.IsUseUnityVectors ? "UnityVector" : "Vector"); - public string Accept(TVector2 type, string bufName, string fieldName) + public string Accept(TVector2 type, string bufName, string fieldName, int depth) { string src = $"{bufName}.Read{VectorName}2()"; return $"{fieldName} = {ExternalTypeUtil.CsCloneToExternal("vector2", src)};"; } - public string Accept(TVector3 type, string bufName, string fieldName) + public string Accept(TVector3 type, string bufName, string fieldName, int depth) { string src = $"{bufName}.Read{VectorName}3()"; return $"{fieldName} = {ExternalTypeUtil.CsCloneToExternal("vector3", src)};"; } - public string Accept(TVector4 type, string bufName, string fieldName) + public string Accept(TVector4 type, string bufName, string fieldName, int depth) { string src = $"{bufName}.Read{VectorName}4()"; return $"{fieldName} = {ExternalTypeUtil.CsCloneToExternal("vector4", src)};"; } - public string Accept(TDateTime type, string bufName, string fieldName) + public string Accept(TDateTime type, string bufName, string fieldName, int depth) { string src = $"{bufName}.ReadInt()"; return $"{fieldName} = {ExternalTypeUtil.CsCloneToExternal("datetime", src)};"; diff --git a/src/Luban.Job.Common/Source/Types/TArray.cs b/src/Luban.Job.Common/Source/Types/TArray.cs index ade143f..496eb69 100644 --- a/src/Luban.Job.Common/Source/Types/TArray.cs +++ b/src/Luban.Job.Common/Source/Types/TArray.cs @@ -16,23 +16,15 @@ namespace Luban.Job.Common.Types public override string TypeName => "array"; public int Dimension { get; } = 1; + public TType FinalElementType { get; protected set; } private TArray(bool isNullable, Dictionary tags, TType elementType) : base(isNullable, tags) { ElementType = elementType; - if (!elementType.IsCollection) - { - FinalElementType = ElementType; - CollectionLevel = 0; - } - else - { - CollectionLevel = ElementType.CollectionLevel + 1; - FinalElementType = ElementType.FinalElementType; - } if (ElementType.TypeName == "array") { Dimension = (ElementType as TArray).Dimension + 1; + FinalElementType = (ElementType as TArray).FinalElementType; } } diff --git a/src/Luban.Job.Common/Source/Types/TList.cs b/src/Luban.Job.Common/Source/Types/TList.cs index 611b202..bfce885 100644 --- a/src/Luban.Job.Common/Source/Types/TList.cs +++ b/src/Luban.Job.Common/Source/Types/TList.cs @@ -22,16 +22,6 @@ namespace Luban.Job.Common.Types { ElementType = elementType; IsArrayList = isArrayList; - if (!elementType.IsCollection) - { - FinalElementType = ElementType; - CollectionLevel = 0; - } - else - { - CollectionLevel = ElementType.CollectionLevel + 1; - FinalElementType = ElementType.FinalElementType; - } } public override bool TryParseFrom(string s) diff --git a/src/Luban.Job.Common/Source/Types/TSet.cs b/src/Luban.Job.Common/Source/Types/TSet.cs index 3e6102d..180c172 100644 --- a/src/Luban.Job.Common/Source/Types/TSet.cs +++ b/src/Luban.Job.Common/Source/Types/TSet.cs @@ -22,16 +22,6 @@ namespace Luban.Job.Common.Types { ElementType = elementType; IsOrderSet = isOrderSet; - if (!elementType.IsCollection) - { - FinalElementType = ElementType; - CollectionLevel = 0; - } - else - { - CollectionLevel = ElementType.CollectionLevel + 1; - FinalElementType = ElementType.FinalElementType; - } } public override bool TryParseFrom(string s) diff --git a/src/Luban.Job.Common/Source/Types/TType.cs b/src/Luban.Job.Common/Source/Types/TType.cs index f81c57e..05e9a2b 100644 --- a/src/Luban.Job.Common/Source/Types/TType.cs +++ b/src/Luban.Job.Common/Source/Types/TType.cs @@ -19,8 +19,8 @@ namespace Luban.Job.Common.Types } public abstract string TypeName { get; } - public TType FinalElementType { get; protected set; } - public int CollectionLevel { get; protected set; } + + public int CollectionLevel { get; set; } public bool HasTag(string attrName) {