diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs index a108c7a..ed0243e 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs @@ -5,121 +5,148 @@ 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) { - return $"{{ var _json0 = {json}; int _n = _json0.GetArrayLength(); {x} = new {type.ElementType.CsUnderingDefineType()}[_n]; int _index=0; foreach(JsonElement __e in _json0.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}[_index++] = __v; }} }}"; + 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) + { + 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}", 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) { - return $"{{ var _json0 = {json}; {x} = new {type.CsUnderingDefineType()}(_json0.GetArrayLength()); foreach(JsonElement __e in _json0.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) { - return $"{{ var _json0 = {json}; {x} = new {type.CsUnderingDefineType()}(_json0.GetArrayLength()); foreach(JsonElement __e in _json0.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 c6e3a01..d85b7f7 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs @@ -5,128 +5,156 @@ 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) { - string tempJsonName = $"_json1"; - 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 _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) + { + 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, 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) { - 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.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}.Add(__v); }} }}"; + 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, 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) { - 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.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}.Add(__v); }} }}"; + 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, 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 233a692..8c72367 100644 --- a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs @@ -34,7 +34,7 @@ namespace Luban.Job.Common.Defs public Dictionary Types { get; } = new Dictionary(); - private readonly Dictionary _notCaseSenseTypes = new (); + private readonly Dictionary _notCaseSenseTypes = new(); private readonly HashSet _namespaces = new(); @@ -275,6 +275,7 @@ namespace Luban.Job.Common.Defs public TType CreateType(string module, string type) { + type = DefUtil.TrimBracePairs(type); int sepIndex = DefUtil.IndexOfBaseTypeEnd(type); if (sepIndex > 0) { @@ -340,17 +341,17 @@ namespace Luban.Job.Common.Defs case "time": case "datetime": return SupportDatetimeType ? TDateTime.Create(nullable, tags) : throw new NotSupportedException($"鍙湁閰嶇疆鏀寔datetime鏁版嵁绫诲瀷"); default: - { - var dtype = GetDefTType(module, type, nullable, tags); - if (dtype != null) { - return dtype; + var dtype = GetDefTType(module, type, nullable, tags); + if (dtype != null) + { + return dtype; + } + else + { + throw new ArgumentException($"invalid type. module:'{module}' type:'{type}'"); + } } - else - { - throw new ArgumentException($"invalid type. module:'{module}' type:'{type}'"); - } - } } } @@ -363,21 +364,32 @@ 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) { switch (containerType) { - case "array": return TArray.Create(false, containerTags, CreateNotContainerType(module, elementType)); - case "list": return TList.Create(false, containerTags, CreateNotContainerType(module, elementType), true); - case "set": return TSet.Create(false, containerTags, CreateNotContainerType(module, elementType), false); + case "array": + { + return TArray.Create(false, containerTags, CreateType(module, elementType)); + } + case "list": return TList.Create(false, containerTags, CreateType(module, elementType), true); + case "set": + { + TType type = CreateType(module, elementType); + if (type.IsCollection) + { + throw new Exception("set鐨勫厓绱犱笉鏀寔瀹瑰櫒绫诲瀷"); + } + return TSet.Create(false, containerTags, type, false); + } case "map": return CreateMapType(module, containerTags, elementType, false); default: - { - throw new ArgumentException($"invalid container type. module:'{module}' container:'{containerType}' element:'{elementType}'"); - } + { + throw new ArgumentException($"invalid container type. module:'{module}' container:'{containerType}' element:'{elementType}'"); + } } } } diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsDeserializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsDeserializeVisitor.cs index 19f7dbe..9954f90 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsDeserializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsDeserializeVisitor.cs @@ -5,16 +5,15 @@ namespace Luban.Job.Common.TypeVisitors public class CsDeserializeVisitor : DecoratorFuncVisitor { public static CsDeserializeVisitor Ins { get; } = new CsDeserializeVisitor(); - public override string DoAccept(TType type, string bufName, string fieldName) { 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 a4a8c0f..8cc229a 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs @@ -4,128 +4,153 @@ 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) { - 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{depth}"; + string __e = $"__e{depth}"; + string __index = $"__index{depth}"; + 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}", 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) { - 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) { - 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 4068905..496eb69 100644 --- a/src/Luban.Job.Common/Source/Types/TArray.cs +++ b/src/Luban.Job.Common/Source/Types/TArray.cs @@ -15,10 +15,17 @@ namespace Luban.Job.Common.Types public override TType ElementType { get; } 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.TypeName == "array") + { + Dimension = (ElementType as TArray).Dimension + 1; + FinalElementType = (ElementType as TArray).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..05e9a2b 100644 --- a/src/Luban.Job.Common/Source/Types/TType.cs +++ b/src/Luban.Job.Common/Source/Types/TType.cs @@ -20,6 +20,8 @@ namespace Luban.Job.Common.Types public abstract string TypeName { get; } + public int CollectionLevel { get; set; } + public bool HasTag(string attrName) { return Tags != null && Tags.ContainsKey(attrName); diff --git a/src/Luban.Job.Common/Source/Utils/DefUtil.cs b/src/Luban.Job.Common/Source/Utils/DefUtil.cs index 399130a..3ba7024 100644 --- a/src/Luban.Job.Common/Source/Utils/DefUtil.cs +++ b/src/Luban.Job.Common/Source/Utils/DefUtil.cs @@ -137,6 +137,44 @@ namespace Luban.Job.Common.Utils } public static string TrimBracePairs(string rawType) + { + while (rawType.Length > 0 && rawType[0] == '(') + { + int braceDepth = 0; + int level1Left = -1; + int level1Right = -1; + for (int i = 0; i < rawType.Length; i++) + { + if (rawType[i] == '(') + { + braceDepth++; + if (level1Left < 0) + { + level1Left = i; + } + } + if (rawType[i] == ')') + { + braceDepth--; + if (level1Right < 0 && braceDepth == 0) + { + level1Right = i; + break; + } + } + } + if (level1Left >= 0 && level1Right == rawType.Length - 1) + { + rawType = rawType.Substring(1, rawType.Length - 2); + } + else + { + break; + } + } + return rawType; + } + public static string TrimBracePairs2(string rawType, bool soft = false) { while (rawType.Length > 0 && rawType[0] == '(') { @@ -146,7 +184,14 @@ namespace Luban.Job.Common.Utils } else { - throw new Exception($"type:{rawType} brace not match"); + if (soft) + { + return rawType; + } + else + { + throw new Exception($"type:{rawType} brace not match"); + } } } return rawType; @@ -200,7 +245,7 @@ namespace Luban.Job.Common.Utils return (typeStr, attrs); } - public static bool ParseOrientation(string value) + public static bool ParseOrientation(string value) { switch (value.Trim()) { @@ -210,9 +255,9 @@ namespace Luban.Job.Common.Utils case "c": case "column": return false; default: - { - throw new Exception($"orientation 灞炴у煎彧鑳戒负row|r|column|c"); - } + { + throw new Exception($"orientation 灞炴у煎彧鑳戒负row|r|column|c"); + } } }