From 79940dcf09e75948b25dfb5c08dd5972405b43f4 Mon Sep 17 00:00:00 2001 From: Carson Lin <396098651@qq.com> Date: Wed, 13 Jul 2022 10:28:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9C=A8TType=E6=B7=BB=E5=8A=A0Collect?= =?UTF-8?q?ionLevel=EF=BC=8C=E7=94=A8=E4=BA=8E=E8=BF=9E=E7=BB=AD=E5=B5=8C?= =?UTF-8?q?=E5=A5=97=E5=AE=B9=E5=99=A8=E6=97=B6=E7=94=9F=E6=88=90=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/TypeVisitors/CsJsonDeserialize.cs | 31 +++++++++---------- .../TypeVisitors/CsUnityJsonDeserialize.cs | 29 +++++++++-------- .../CsUnderingDeserializeVisitor.cs | 25 +++++++-------- src/Luban.Job.Common/Source/Types/TArray.cs | 8 +++-- 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 | 2 ++ 7 files changed, 68 insertions(+), 47 deletions(-) diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs index 1358f96..4cf1a55 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs @@ -8,7 +8,6 @@ namespace Luban.Job.Cfg.TypeVisitors class CsJsonDeserialize : ITypeFuncVisitor { public static CsJsonDeserialize Ins { get; } = new(); - public int Flag { get; set; } public string Accept(TBool type, string json, string x) { return $"{x} = {json}.GetBoolean();"; @@ -86,12 +85,12 @@ namespace Luban.Job.Cfg.TypeVisitors public string Accept(TArray type, string json, string x) { - Flag++; - string _n = $"_n{Flag}"; - string __e = $"__e{Flag}"; - string __v = $"__v{Flag}"; - string __json = $"__json{Flag}"; - string __index = $"__index{Flag}"; + 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 typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{_n}]"; if (type.Dimension > 1) { @@ -110,21 +109,19 @@ namespace Luban.Job.Cfg.TypeVisitors public string Accept(TList type, string json, string x) { - Flag++; - string _n = $"_n{Flag}"; - string __e = $"__e{Flag}"; - string __v = $"__v{Flag}"; - string __json = $"__json{Flag}"; + 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}); }} }}"; } public string Accept(TSet type, string json, string x) { - Flag++; - string _n = $"_n{Flag}"; - string __e = $"__e{Flag}"; - string __v = $"__v{Flag}"; - string __json = $"__json{Flag}"; + 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}); }} }}"; } diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs index 668fd73..1f15b32 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs @@ -8,7 +8,6 @@ namespace Luban.Job.Cfg.TypeVisitors class CsUnityJsonDeserialize : ITypeFuncVisitor { public static CsUnityJsonDeserialize Ins { get; } = new(); - public int Flag { get; set; } public string Accept(TBool type, string json, string x) { @@ -87,12 +86,12 @@ namespace Luban.Job.Cfg.TypeVisitors public string Accept(TArray type, string json, string x) { - Flag++; - string _n = $"_n{Flag}"; - string __e = $"__e{Flag}"; - string __v = $"__v{Flag}"; - string __json = $"__json{Flag}"; - string __index = $"__index{Flag}"; + 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 tempJsonName = __json; string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{_n}]"; if (type.Dimension > 1) @@ -112,20 +111,20 @@ namespace Luban.Job.Cfg.TypeVisitors public string Accept(TList type, string json, string x) { - Flag++; - string __e = $"__e{Flag}"; - string __v = $"__v{Flag}"; - string __json = $"__json{Flag}"; + int level = type.CollectionLevel; + string __e = $"__e{level}"; + string __v = $"__v{level}"; + string __json = $"__json{level}"; 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}); }} }}"; } public string Accept(TSet type, string json, string x) { - Flag++; - string __e = $"__e{Flag}"; - string __v = $"__v{Flag}"; - string __json = $"__json{Flag}"; + int level = type.CollectionLevel; + string __e = $"__e{level}"; + string __v = $"__v{level}"; + string __json = $"__json{level}"; 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}); }} }}"; } diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs index 3e67aad..f56701c 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs @@ -7,7 +7,6 @@ namespace Luban.Job.Common.TypeVisitors class CsUnderingDeserializeVisitor : ITypeFuncVisitor { public static CsUnderingDeserializeVisitor Ins { get; } = new CsUnderingDeserializeVisitor(); - public int Flag { get; set; } public string Accept(TBool type, string bufName, string fieldName) { @@ -87,10 +86,10 @@ namespace Luban.Job.Common.TypeVisitors public string Accept(TArray type, string bufName, string fieldName) { - Flag++; - string __n = $"__n{Flag}"; - string __e = $"__e{Flag}"; - string __index = $"__index{Flag}"; + int level = type.CollectionLevel; + string __n = $"__n{level}"; + string __e = $"__e{level}"; + string __index = $"__index{level}"; string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{__n}]"; if (type.Dimension > 1) { @@ -109,19 +108,19 @@ namespace Luban.Job.Common.TypeVisitors public string Accept(TList type, string bufName, string fieldName) { - Flag++; - string n = $"n{Flag}"; - string _e = $"_e{Flag}"; - string i = $"i{Flag}"; + 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});}}}}"; } public string Accept(TSet type, string bufName, string fieldName) { - Flag++; - string n = $"n{Flag}"; - string _e = $"_e{Flag}"; - string i = $"i{Flag}"; + 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});}}}}"; } diff --git a/src/Luban.Job.Common/Source/Types/TArray.cs b/src/Luban.Job.Common/Source/Types/TArray.cs index 5bf73f6..ade143f 100644 --- a/src/Luban.Job.Common/Source/Types/TArray.cs +++ b/src/Luban.Job.Common/Source/Types/TArray.cs @@ -16,7 +16,6 @@ namespace Luban.Job.Common.Types public override string TypeName => "array"; public int Dimension { get; } = 1; - public TType FinalElementType { get; } private TArray(bool isNullable, Dictionary tags, TType elementType) : base(isNullable, tags) { @@ -24,11 +23,16 @@ namespace Luban.Job.Common.Types 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 bfce885..611b202 100644 --- a/src/Luban.Job.Common/Source/Types/TList.cs +++ b/src/Luban.Job.Common/Source/Types/TList.cs @@ -22,6 +22,16 @@ 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 180c172..3e6102d 100644 --- a/src/Luban.Job.Common/Source/Types/TSet.cs +++ b/src/Luban.Job.Common/Source/Types/TSet.cs @@ -22,6 +22,16 @@ 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 b05313e..f81c57e 100644 --- a/src/Luban.Job.Common/Source/Types/TType.cs +++ b/src/Luban.Job.Common/Source/Types/TType.cs @@ -19,6 +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 bool HasTag(string attrName) {