feat: 弃用Type的CollectionLevel,改为在visitor添加depth参数以实现嵌套容器的解析

main
Carson Lin 2022-07-13 11:41:32 +08:00
parent 79940dcf09
commit 02f3ebd979
10 changed files with 146 additions and 172 deletions

View File

@ -5,92 +5,91 @@ using System;
namespace Luban.Job.Cfg.TypeVisitors
{
class CsJsonDeserialize : ITypeFuncVisitor<string, string, string>
class CsJsonDeserialize : ITypeFuncVisitor<string, string, int, string>
{
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();";
}

View File

@ -5,93 +5,92 @@ using System;
namespace Luban.Job.Cfg.TypeVisitors
{
class CsUnityJsonDeserialize : ITypeFuncVisitor<string, string, string>
class CsUnityJsonDeserialize : ITypeFuncVisitor<string, string, int, string>
{
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}; }}";
}

View File

@ -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);
}
}

View File

@ -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<string, string> 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);
}

View File

@ -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);
}
}

View File

@ -4,92 +4,91 @@ using Luban.Job.Common.Utils;
namespace Luban.Job.Common.TypeVisitors
{
class CsUnderingDeserializeVisitor : ITypeFuncVisitor<string, string, string>
class CsUnderingDeserializeVisitor : ITypeFuncVisitor<string, string, int, string>
{
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)};";

View File

@ -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<string, string> 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;
}
}

View File

@ -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)

View File

@ -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)

View File

@ -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)
{