From f3aa518526740608a651c98e3e92672c145a1d01 Mon Sep 17 00:00:00 2001 From: walon Date: Wed, 18 Nov 2020 01:51:23 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=20db=20cs=20=E5=BA=8F=E5=88=97=E5=8C=96=E5=8F=8Awrite?= =?UTF-8?q?blob=E7=9A=84bug=20=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20proto=20cs=20proto=E5=8F=8Abean=E9=9D=9E?= =?UTF-8?q?=E7=A9=BA=E5=80=BC=E6=9E=84=E9=80=A0=E4=BD=BF=E7=94=A8default?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/TypeVisitors/CsCtorValueVisitor.cs | 60 ++++++++ .../Source/Defs/TTypeTemplateExtends.cs | 5 +- .../Source/TypeVisitors/DbWriteBlob.cs | 128 +----------------- .../Source/Defs/TTypeTemplateExtends.cs | 4 +- .../Source/Generate/CsRender.cs | 8 +- 5 files changed, 71 insertions(+), 134 deletions(-) create mode 100644 src/Luban.Job.Common/Source/TypeVisitors/CsCtorValueVisitor.cs diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsCtorValueVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsCtorValueVisitor.cs new file mode 100644 index 0000000..db6411e --- /dev/null +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsCtorValueVisitor.cs @@ -0,0 +1,60 @@ +using Luban.Job.Common.Types; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Common.TypeVisitors +{ + public class CsCtorValueVisitor : DecoratorFuncVisitor + { + public static CsCtorValueVisitor Ins { get; } = new CsCtorValueVisitor(); + + public override string DoAccept(TType type) + { + return "default"; + } + + + public override string Accept(TString type) + { + return "\"\""; + } + + public override string Accept(TBytes type) + { + return "System.Array.Empty()"; + } + + public override string Accept(TText type) + { + return "\"\""; + } + + public override string Accept(TBean type) + { + return type.Bean.IsAbstractType ? "default" : $"new {type.Apply(CsDefineTypeName.Ins)}()"; + } + + public override string Accept(TArray type) + { + return $"System.Array.Empty<{type.ElementType.Apply(CsDefineTypeName.Ins)}>()"; + } + + public override string Accept(TList type) + { + return $"new System.Collections.Generic.List<{type.ElementType.Apply(CsDefineTypeName.Ins)}>()"; + } + + public override string Accept(TSet type) + { + return $"new System.Collections.Generic.HashSet<{type.ElementType.Apply(CsDefineTypeName.Ins)}>()"; + } + + public override string Accept(TMap type) + { + return $"new System.Collections.Generic.Dictionary<{type.KeyType.Apply(CsDefineTypeName.Ins)},{type.ValueType.Apply(CsDefineTypeName.Ins)}>()"; + } + } +} diff --git a/src/Luban.Job.Db/Source/Defs/TTypeTemplateExtends.cs b/src/Luban.Job.Db/Source/Defs/TTypeTemplateExtends.cs index 2b989f8..e6150cf 100644 --- a/src/Luban.Job.Db/Source/Defs/TTypeTemplateExtends.cs +++ b/src/Luban.Job.Db/Source/Defs/TTypeTemplateExtends.cs @@ -68,9 +68,10 @@ namespace Luban.Job.Db.Defs return $"{field.CsStyleName} = default;"; } - public static string CsWriteBlob(string bufName, string valueName, TType type) + public static string CsWriteBlob(string bufName, string fieldName, TType type) { - return type.Apply(DbWriteBlob.Ins, bufName, valueName); + //return type.Apply(DbWriteBlob.Ins, bufName, valueName); + return DbCsCompatibleSerialize(bufName, fieldName, type); } } } diff --git a/src/Luban.Job.Db/Source/TypeVisitors/DbWriteBlob.cs b/src/Luban.Job.Db/Source/TypeVisitors/DbWriteBlob.cs index a79ad4a..8158b18 100644 --- a/src/Luban.Job.Db/Source/TypeVisitors/DbWriteBlob.cs +++ b/src/Luban.Job.Db/Source/TypeVisitors/DbWriteBlob.cs @@ -4,133 +4,9 @@ using System; namespace Luban.Job.Db.TypeVisitors { - class DbWriteBlob : ITypeFuncVisitor + class DbWriteBlob { - public static DbWriteBlob Ins { get; } = new DbWriteBlob(); + public static DbCsCompatibleSerializeVisitor Ins { get; } = new DbCsCompatibleSerializeVisitor(); - public string Accept(TBool type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteBool({fieldName});"; - } - - public string Accept(TByte type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteByte({fieldName});"; - } - - public string Accept(TShort type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteShort({fieldName});"; - } - - public string Accept(TFshort type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteFshort({fieldName});"; - } - - public string Accept(TInt type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteInt({fieldName});"; - } - - public string Accept(TFint type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteFint({fieldName});"; - } - - public string Accept(TLong type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteLong({fieldName});"; - } - - public string Accept(TFlong type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteFlong({fieldName});"; - } - - public string Accept(TFloat type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteFloat({fieldName});"; - } - - public string Accept(TDouble type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteDouble({fieldName});"; - } - - public string Accept(TEnum type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteInt((int){fieldName});"; - } - - public string Accept(TString type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteString({fieldName});"; - } - - public string Accept(TBytes type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteBytes({fieldName});"; - } - - public string Accept(TText type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteString({fieldName});"; - } - - public string Accept(TBean type, string byteBufName, string fieldName) - { - var bean = type.Bean; - if (bean.IsNotAbstractType) - { - - return $"{fieldName}.Serialize({byteBufName});"; - } - else - { - return $"{bean.FullName}.Serialize{bean.Name}({byteBufName}, {fieldName});"; - } - } - - public string Accept(TArray type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteSize({fieldName}.Length); foreach(var _e in {fieldName}) {{ {type.ElementType.Apply(this, byteBufName, "_e")} }}"; - } - - public string Accept(TList type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteSize({fieldName}.Count); foreach(var _e in {fieldName}) {{ {type.ElementType.Apply(this, byteBufName, "_e")} }}"; - } - - public string Accept(TSet type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteSize({fieldName}.Count); foreach(var _e in {fieldName}) {{ {type.ElementType.Apply(this, byteBufName, "_e")} }}"; - } - - public string Accept(TMap type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteSize({fieldName}.Count); foreach((var _k, var _v) in {fieldName}) {{ {type.KeyType.Apply(this, byteBufName, "_k")} {type.ValueType.Apply(this, byteBufName, "_v")}}}"; - - } - - public string Accept(TVector2 type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteVector2({fieldName});"; - } - - public string Accept(TVector3 type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteVector3({fieldName});"; - } - - public string Accept(TVector4 type, string byteBufName, string fieldName) - { - return $"{byteBufName}.WriteVector4({fieldName});"; - } - - public string Accept(TDateTime type, string x, string y) - { - throw new NotImplementedException(); - } } } diff --git a/src/Luban.Job.Proto/Source/Defs/TTypeTemplateExtends.cs b/src/Luban.Job.Proto/Source/Defs/TTypeTemplateExtends.cs index 0e1a850..8c7cf34 100644 --- a/src/Luban.Job.Proto/Source/Defs/TTypeTemplateExtends.cs +++ b/src/Luban.Job.Proto/Source/Defs/TTypeTemplateExtends.cs @@ -37,9 +37,9 @@ namespace Luban.Job.Proto.Defs return type.Apply(LuaUnderingDeserializeVisitor.Ins, bufName); } - public static string CsInitFieldCtorValue(DefField field) + public static string CsInitFieldCtorValue(string bufName, TType type) { - return $"{field.CsStyleName} = default;"; + return $"{bufName} = {type.Apply(CsCtorValueVisitor.Ins)};"; } } } diff --git a/src/Luban.Job.Proto/Source/Generate/CsRender.cs b/src/Luban.Job.Proto/Source/Generate/CsRender.cs index e0dba2c..0f7ba5b 100644 --- a/src/Luban.Job.Proto/Source/Generate/CsRender.cs +++ b/src/Luban.Job.Proto/Source/Generate/CsRender.cs @@ -65,9 +65,9 @@ namespace {{x.namespace_with_top_module}} { {{~ for field in fields ~}} {{~if cs_need_init field.ctype~}} - {{cs_init_field_ctor_value field}} + {{cs_init_field_ctor_value field.cs_style_name field.ctype}} {{~else if is_value_type~}} - {field.cs_style_name} = default; + {{field.cs_style_name}} = default; {{~end~}} {{~end~}} } @@ -178,8 +178,8 @@ namespace {{x.namespace_with_top_module}} public {{name}}(Bright.Common.NotNullInitialization _) { {{~ for field in fields ~}} - {{~if field.ctype.need_init~}} - {{cs_init_field_ctor_value field}} + {{~if cs_need_init field.ctype~}} + {{cs_init_field_ctor_value field.cs_style_name field.ctype}} {{~end~}} {{~end~}} }