From 6620cca6e11c05913d7b98e1631cea35331107a0 Mon Sep 17 00:00:00 2001 From: carson <396098651@qq.com> Date: Wed, 13 Jul 2022 03:23:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=94=AF=E6=8C=81=E5=A4=9A=E7=BB=B4?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E5=B5=8C=E5=A5=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/TypeVisitors/CsJsonDeserialize.cs | 15 ++++++++++++- .../TypeVisitors/CsUnityJsonDeserialize.cs | 15 ++++++++++++- .../Source/Defs/DefAssemblyBase.cs | 7 +------ .../CsUnderingDeserializeVisitor.cs | 21 +++++++++++++++---- src/Luban.Job.Common/Source/Types/TArray.cs | 11 ++++++++++ 5 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs index 2e38343..1358f96 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs @@ -92,7 +92,20 @@ namespace Luban.Job.Cfg.TypeVisitors string __v = $"__v{Flag}"; string __json = $"__json{Flag}"; string __index = $"__index{Flag}"; - return $"{{ var {__json} = {json}; int {_n} = {__json}.GetArrayLength(); {x} = new {type.ElementType.CsUnderingDefineType()}[{_n}]; int {__index}=0; foreach(JsonElement {__e} in {__json}.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, $"{__e}", $"{__v}")} {x}[{__index}++] = {__v}; }} }}"; + string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{_n}]"; + if (type.Dimension > 1) + { + if (type.FinalElementType == null) + { + throw new System.Exception("多维数组没有元素类型"); + } + typeStr = $"{type.FinalElementType.Apply(CsUnderingDefineTypeName.Ins)}[{_n}]"; + for (int i = 0; i < type.Dimension - 1; i++) + { + 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}; }} }}"; } public string Accept(TList type, string json, string x) diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs index a459cd1..668fd73 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs @@ -94,7 +94,20 @@ namespace Luban.Job.Cfg.TypeVisitors string __json = $"__json{Flag}"; string __index = $"__index{Flag}"; string tempJsonName = __json; - return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} int {_n} = {tempJsonName}.Count; {x} = new {type.ElementType.CsUnderingDefineType()}[{_n}]; int {__index}=0; foreach(JSONNode {__e} in {tempJsonName}.Children) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, __e, __v)} {x}[{__index}++] = {__v}; }} }}"; + string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{_n}]"; + if (type.Dimension > 1) + { + if (type.FinalElementType == null) + { + throw new System.Exception("多维数组没有元素类型"); + } + typeStr = $"{type.FinalElementType.Apply(CsUnderingDefineTypeName.Ins)}[{_n}]"; + for (int i = 0; i < type.Dimension - 1; i++) + { + 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}; }} }}"; } public string Accept(TList type, string json, string x) diff --git a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs index c4e0778..c1541f5 100644 --- a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs @@ -373,12 +373,7 @@ namespace Luban.Job.Common.Defs { case "array": { - TType type = CreateType(module, elementType); - if (type.TypeName == "array") - { - throw new Exception("涓嶆敮鎸佸缁存暟缁勶紝璇锋敼涓哄祵濂條ist"); - } - return TArray.Create(false, containerTags, type); + return TArray.Create(false, containerTags, CreateType(module, elementType)); } case "list": return TList.Create(false, containerTags, CreateType(module, elementType), true); case "set": diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs index af23211..3e67aad 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs @@ -88,10 +88,23 @@ namespace Luban.Job.Common.TypeVisitors public string Accept(TArray type, string bufName, string fieldName) { Flag++; - string n = $"n{Flag}"; - string _e = $"_e{Flag}"; - string i = $"i{Flag}"; - return $"{{int {n} = System.Math.Min({bufName}.ReadSize(), {bufName}.Size);{fieldName} = new {type.ElementType.Apply(CsDefineTypeName.Ins)}[{n}];for(var {i} = 0 ; {i} < {n} ; {i}++) {{ {type.ElementType.Apply(CsDefineTypeName.Ins)} {_e};{type.ElementType.Apply(this, bufName, $"{_e}")} {fieldName}[{i}] = {_e};}}}}"; + string __n = $"__n{Flag}"; + string __e = $"__e{Flag}"; + string __index = $"__index{Flag}"; + string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{__n}]"; + if (type.Dimension > 1) + { + if (type.FinalElementType == null) + { + throw new System.Exception("多维数组没有元素类型"); + } + typeStr = $"{type.FinalElementType.Apply(CsUnderingDefineTypeName.Ins)}[{__n}]"; + for (int i = 0; i < type.Dimension - 1; i++) + { + 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};}}}}"; } public string Accept(TList type, string bufName, string fieldName) diff --git a/src/Luban.Job.Common/Source/Types/TArray.cs b/src/Luban.Job.Common/Source/Types/TArray.cs index 4068905..5bf73f6 100644 --- a/src/Luban.Job.Common/Source/Types/TArray.cs +++ b/src/Luban.Job.Common/Source/Types/TArray.cs @@ -15,10 +15,21 @@ namespace Luban.Job.Common.Types public override TType ElementType { get; } 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) { ElementType = elementType; + if (!elementType.IsCollection) + { + FinalElementType = ElementType; + } + if (ElementType.TypeName == "array") + { + Dimension = (ElementType as TArray).Dimension + 1; + FinalElementType = (ElementType as TArray).FinalElementType; + } } public override bool TryParseFrom(string s)