diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/BinaryExportor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/BinaryExportor.cs index 9b40860..d499462 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/BinaryExportor.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/BinaryExportor.cs @@ -1,6 +1,7 @@ using Bright.Serialization; using Luban.Job.Cfg.Datas; using Luban.Job.Cfg.Defs; +using Luban.Job.Common.TypeVisitors; using System.Collections.Generic; namespace Luban.Job.Cfg.DataVisitors @@ -102,7 +103,7 @@ namespace Luban.Job.Cfg.DataVisitors continue; } - if (defField.NeedMarshalBoolPrefix) + if (defField.CType.Apply(NeedMarshalBoolPrefixVisitor.Ins)) { if (field != null) { diff --git a/src/Luban.Job.Cfg/Source/Defs/DefField.cs b/src/Luban.Job.Cfg/Source/Defs/DefField.cs index c1b1038..e0a1589 100644 --- a/src/Luban.Job.Cfg/Source/Defs/DefField.cs +++ b/src/Luban.Job.Cfg/Source/Defs/DefField.cs @@ -115,8 +115,6 @@ namespace Luban.Job.Cfg.Defs public bool IsResource => !string.IsNullOrEmpty(ResourceTag); - public bool NeedMarshalBoolPrefix => CType.Apply(NeedMarshalBoolPrefixVisitor.Ins); - public string CsRefVarName => $"{CsStyleName}_Ref"; public string JavaRefVarName => $"{JavaStyleName}_Ref"; diff --git a/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs b/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs index 8b30af2..bbd1351 100644 --- a/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs +++ b/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs @@ -7,15 +7,6 @@ namespace Luban.Job.Cfg.Defs { class TTypeTemplateExtends : TTypeTemplateCommonExtends { - public static string CsDeserialize(string bufName, string fieldName, TType type) - { - return type.Apply(CsDeserializeVisitor.Ins, bufName, fieldName); - } - - public static string CsCompatibleDeserialize(string bufName, string fieldName, TType type) - { - return type.Apply(CsDeserializeVisitor.Ins, bufName, fieldName); - } public static string CsJsonDeserialize(string bufName, string fieldName, string jsonFieldName, TType type) { @@ -81,16 +72,6 @@ namespace Luban.Job.Cfg.Defs return type.Apply(CppDeserializeVisitor.Ins, bufName, fieldName); } - public static string LuaCommentType(TType type) - { - return type.Apply(LuaCommentTypeVisitor.Ins); - } - - public static string LuaUnderingDeserialize(string bufName, TType type) - { - return type.Apply(LuaUnderingDeserializeVisitor.Ins, bufName); - } - public static string GoDefineType(TType type) { @@ -106,7 +87,7 @@ namespace Luban.Job.Cfg.Defs { var name = field.CsStyleName; TType type = field.CType; - if (field.NeedMarshalBoolPrefix) + if (field.CType.Apply(NeedMarshalBoolPrefixVisitor.Ins)) { return $"{{ var _exists bool; if _exists, err = {bufName}.ReadBool(); err != nil {{ return }}; if _exists {{ if _v.{name}, err = {type.Apply(GoDeserializeVisitor.Ins, bufName)}; err != nil {{ return }} }} }}"; } diff --git a/src/Luban.Job.Cfg/Source/Generate/LuaRender.cs b/src/Luban.Job.Cfg/Source/Generate/LuaRender.cs index a3574b2..1b7c414 100644 --- a/src/Luban.Job.Cfg/Source/Generate/LuaRender.cs +++ b/src/Luban.Job.Cfg/Source/Generate/LuaRender.cs @@ -154,7 +154,7 @@ local function InitTypes(methods) class._deserialize = function(bs) local o = { {{~ for field in bean.hierarchy_export_fields ~}} - {{~if !field.need_marshal_bool_prefix~}} + {{~if !need_marshal_bool_prefix field.ctype~}} {{field.name}} = {{lua_undering_deserialize 'bs' field.ctype}}, {{~else~}} {{field.name}} = {{if !field.ctype.is_bool}}readBool(bs) and {{lua_undering_deserialize 'bs' field.ctype}} or nil {{-else-}} readNullableBool(bs) {{-end-}}, diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs deleted file mode 100644 index 892afc4..0000000 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs +++ /dev/null @@ -1,126 +0,0 @@ -using Luban.Job.Common.Types; -using Luban.Job.Common.TypeVisitors; - -namespace Luban.Job.Cfg.TypeVisitors -{ - class CsUnderingDeserializeVisitor : ITypeFuncVisitor - { - public static CsUnderingDeserializeVisitor Ins { get; } = new CsUnderingDeserializeVisitor(); - - public string Accept(TBool type, string bufName, string fieldName) - { - return $"{fieldName} = {bufName}.ReadBool();"; - } - - public string Accept(TByte type, string bufName, string fieldName) - { - return $"{fieldName} = {bufName}.ReadByte();"; - } - - public string Accept(TShort type, string bufName, string fieldName) - { - return $"{fieldName} = {bufName}.ReadShort();"; - } - - public string Accept(TFshort type, string bufName, string fieldName) - { - return $"{fieldName} = {bufName}.ReadFshort();"; - } - - public string Accept(TInt type, string bufName, string fieldName) - { - return $"{fieldName} = {bufName}.ReadInt();"; - } - - public string Accept(TFint type, string bufName, string fieldName) - { - return $"{fieldName} = {bufName}.ReadFint();"; - } - - public string Accept(TLong type, string bufName, string fieldName) - { - return $"{fieldName} = {bufName}.ReadLong();"; - } - - public string Accept(TFlong type, string bufName, string fieldName) - { - return $"{fieldName} = {bufName}.ReadFlong();"; - } - - public string Accept(TFloat type, string bufName, string fieldName) - { - return $"{fieldName} = {bufName}.ReadFloat();"; - } - - public string Accept(TDouble type, string bufName, string fieldName) - { - return $"{fieldName} = {bufName}.ReadDouble();"; - } - - public string Accept(TEnum type, string bufName, string fieldName) - { - return $"{fieldName} = ({type.DefineEnum.FullName}){bufName}.ReadInt();"; - } - - public string Accept(TString type, string bufName, string fieldName) - { - return $"{fieldName} = {bufName}.ReadString();"; - } - - public string Accept(TBytes type, string bufName, string fieldName) - { - return $"{fieldName} = {bufName}.ReadBytes();"; - } - - public string Accept(TText type, string bufName, string fieldName) - { - return $"{fieldName} = {bufName}.ReadString();"; - } - - public string Accept(TBean type, string bufName, string fieldName) - { - return $"{fieldName} = {type.Bean.FullName}.Deserialize{type.Bean.Name}({bufName});"; - } - - public string Accept(TArray type, string bufName, string fieldName) - { - 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;}}}}"; - } - - public string Accept(TList type, string bufName, string fieldName) - { - 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) - { - 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);}}}}"; - } - - public string Accept(TMap type, string bufName, string fieldName) - { - 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);}}}}"; - - } - - public string Accept(TVector2 type, string bufName, string fieldName) - { - return $"{fieldName} = {bufName}.ReadVector2();"; - } - - public string Accept(TVector3 type, string bufName, string fieldName) - { - return $"{fieldName} = {bufName}.ReadVector3();"; - } - - public string Accept(TVector4 type, string bufName, string fieldName) - { - return $"{fieldName} = {bufName}.ReadVector4();"; - } - - public string Accept(TDateTime type, string bufName, string fieldName) - { - return $"{fieldName} = {bufName}.ReadInt();"; - } - } -} diff --git a/src/Luban.Job.Common/Source/Defs/DefFieldBase.cs b/src/Luban.Job.Common/Source/Defs/DefFieldBase.cs index b735d56..5eb78bb 100644 --- a/src/Luban.Job.Common/Source/Defs/DefFieldBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefFieldBase.cs @@ -65,7 +65,6 @@ namespace Luban.Job.Common.Defs public string UpperCaseName => Name.ToUpper(); - public DefFieldBase(DefTypeBase host, Field f, int idOffset) { HostType = host; diff --git a/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs b/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs index ecc0970..1a337b3 100644 --- a/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs +++ b/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs @@ -11,6 +11,11 @@ namespace Luban.Job.Common.Defs return type.Apply(TagNameVisitor.Ins); } + public static bool NeedMarshalBoolPrefix(TType type) + { + return type.Apply(NeedMarshalBoolPrefixVisitor.Ins); + } + public static bool CsNeedInit(TType type) { return type.Apply(CsNeedInitVisitor.Ins); @@ -31,6 +36,31 @@ namespace Luban.Job.Common.Defs return type.Apply(CsConstValueVisitor.Ins, value); } + public static string CsInitFieldCtorValue(string bufName, TType type) + { + return $"{bufName} = {type.Apply(CsCtorValueVisitor.Ins)};"; + } + + public static string CsSerialize(string bufName, string fieldName, TType type) + { + return type.Apply(CsSerializeVisitor.Ins, bufName, fieldName); + } + + public static string CsCompatibleSerialize(string bufName, string fieldName, TType type) + { + return type.Apply(CsSerializeVisitor.Ins, bufName, fieldName); + } + + public static string CsDeserialize(string bufName, string fieldName, TType type) + { + return type.Apply(CsDeserializeVisitor.Ins, bufName, fieldName); + } + + public static string CsCompatibleDeserialize(string bufName, string fieldName, TType type) + { + return type.Apply(CsDeserializeVisitor.Ins, bufName, fieldName); + } + public static string JavaDefineType(TType type) { return type.Apply(JavaDefineTypeName.Ins); @@ -66,6 +96,28 @@ namespace Luban.Job.Common.Defs return type.Apply(LuaConstValueVisitor.Ins, value); } + public static string LuaCommentType(TType type) + { + return type.Apply(LuaCommentTypeVisitor.Ins); + } + + public static string LuaSerializeWhileNil(string bufName, string fieldName, TType type) + { + if (type.IsNullable) + { + return $"if {fieldName} == nil then writeBool(false) elseif writeBool(true) {type.Apply(LuaUnderingSerializeVisitor.Ins, bufName, fieldName)} end"; + } + else + { + return $"{type.Apply(LuaUnderingSerializeVisitor.Ins, bufName, type.Apply(LuaValueOrDefaultVisitor.Ins, fieldName))}"; + } + } + + public static string LuaUnderingDeserialize(string bufName, TType type) + { + return type.Apply(LuaUnderingDeserializeVisitor.Ins, bufName); + } + public static string GoConstValue(TType type, string value) { return type.Apply(LuaConstValueVisitor.Ins, value); diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsDeserializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsDeserializeVisitor.cs similarity index 86% rename from src/Luban.Job.Cfg/Source/TypeVisitors/CsDeserializeVisitor.cs rename to src/Luban.Job.Common/Source/TypeVisitors/CsDeserializeVisitor.cs index 4df90ff..7861796 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsDeserializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsDeserializeVisitor.cs @@ -1,9 +1,9 @@ using Luban.Job.Common.Types; using Luban.Job.Common.TypeVisitors; -namespace Luban.Job.Cfg.TypeVisitors +namespace Luban.Job.Common.TypeVisitors { - class CsDeserializeVisitor : DecoratorFuncVisitor + public class CsDeserializeVisitor : DecoratorFuncVisitor { public static CsDeserializeVisitor Ins { get; } = new CsDeserializeVisitor(); diff --git a/src/Luban.Job.Proto/Source/TypeVisitors/CsSerializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsSerializeVisitor.cs similarity index 87% rename from src/Luban.Job.Proto/Source/TypeVisitors/CsSerializeVisitor.cs rename to src/Luban.Job.Common/Source/TypeVisitors/CsSerializeVisitor.cs index 8bc0a44..fe8adbe 100644 --- a/src/Luban.Job.Proto/Source/TypeVisitors/CsSerializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsSerializeVisitor.cs @@ -1,9 +1,9 @@ using Luban.Job.Common.Types; using Luban.Job.Common.TypeVisitors; -namespace Luban.Job.Proto.TypeVisitors +namespace Luban.Job.Common.TypeVisitors { - class CsSerializeVisitor : DecoratorFuncVisitor + public class CsSerializeVisitor : DecoratorFuncVisitor { public static CsSerializeVisitor Ins { get; } = new CsSerializeVisitor(); diff --git a/src/Luban.Job.Proto/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs similarity index 99% rename from src/Luban.Job.Proto/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs rename to src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs index aae60f9..a57a4d9 100644 --- a/src/Luban.Job.Proto/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs @@ -1,7 +1,7 @@ using Luban.Job.Common.Types; using Luban.Job.Common.TypeVisitors; -namespace Luban.Job.Proto.TypeVisitors +namespace Luban.Job.Common.TypeVisitors { class CsUnderingDeserializeVisitor : ITypeFuncVisitor { diff --git a/src/Luban.Job.Proto/Source/TypeVisitors/CsUnderingSerializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingSerializeVisitor.cs similarity index 99% rename from src/Luban.Job.Proto/Source/TypeVisitors/CsUnderingSerializeVisitor.cs rename to src/Luban.Job.Common/Source/TypeVisitors/CsUnderingSerializeVisitor.cs index 53d8d75..1275393 100644 --- a/src/Luban.Job.Proto/Source/TypeVisitors/CsUnderingSerializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingSerializeVisitor.cs @@ -1,7 +1,7 @@ using Luban.Job.Common.Types; using Luban.Job.Common.TypeVisitors; -namespace Luban.Job.Proto.TypeVisitors +namespace Luban.Job.Common.TypeVisitors { class CsUnderingSerializeVisitor : ITypeFuncVisitor { diff --git a/src/Luban.Job.Common/Source/TypeVisitors/LuaSerializeMethodNameVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/LuaSerializeMethodNameVisitor.cs new file mode 100644 index 0000000..3180ab4 --- /dev/null +++ b/src/Luban.Job.Common/Source/TypeVisitors/LuaSerializeMethodNameVisitor.cs @@ -0,0 +1,124 @@ +using Luban.Job.Common.Types; + +namespace Luban.Job.Common.TypeVisitors +{ + public class LuaSerializeMethodNameVisitor : ITypeFuncVisitor + { + public static LuaSerializeMethodNameVisitor Ins { get; } = new LuaSerializeMethodNameVisitor(); + + public string Accept(TBool type) + { + return "writeBool"; + } + + public string Accept(TByte type) + { + return "writeByte"; + } + + public string Accept(TShort type) + { + return "writeShort"; + } + + public string Accept(TFshort type) + { + return "writeFshort"; + } + + public string Accept(TInt type) + { + return "writeInt"; + } + + public string Accept(TFint type) + { + return "writeFint"; + } + + public string Accept(TLong type) + { + return "writeLong"; + } + + public string Accept(TFlong type) + { + return "writeFlong"; + } + + public string Accept(TFloat type) + { + return "writeFloat"; + } + + public string Accept(TDouble type) + { + return "writeDouble"; + } + + public string Accept(TEnum type) + { + return "writeInt"; + } + + public string Accept(TString type) + { + return "writeString"; + } + + public string Accept(TBytes type) + { + return "writeString"; + } + + public string Accept(TText type) + { + return "writeString"; + } + + public string Accept(TBean type) + { + return $"beans['{type.Bean.FullName}']._serialize"; + } + + public string Accept(TArray type) + { + return "writeList"; + } + + public string Accept(TList type) + { + return "writeList"; + } + + public string Accept(TSet type) + { + return "writeSet"; + } + + public string Accept(TMap type) + { + return "writeMap"; + } + + public string Accept(TVector2 type) + { + return "writeVector2"; + } + + public string Accept(TVector3 type) + { + return "writeVector3"; + } + + public string Accept(TVector4 type) + { + return "writeVector4"; + } + + public string Accept(TDateTime type) + { + return "writeInt"; + } + } +} diff --git a/src/Luban.Job.Common/Source/TypeVisitors/LuaUnderingDeserializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/LuaUnderingDeserializeVisitor.cs index 1837b89..0b49dcd 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/LuaUnderingDeserializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/LuaUnderingDeserializeVisitor.cs @@ -13,7 +13,7 @@ namespace Luban.Job.Common.TypeVisitors public override string Accept(TArray type, string x) { - return $"readList({x}, {type.ElementType.Apply(LuaDeserializeMethodNameVisitor.Ins)})"; + return $"readArray({x}, {type.ElementType.Apply(LuaDeserializeMethodNameVisitor.Ins)})"; } public override string Accept(TList type, string x) diff --git a/src/Luban.Job.Common/Source/TypeVisitors/LuaUnderingSerializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/LuaUnderingSerializeVisitor.cs new file mode 100644 index 0000000..2cf8639 --- /dev/null +++ b/src/Luban.Job.Common/Source/TypeVisitors/LuaUnderingSerializeVisitor.cs @@ -0,0 +1,39 @@ +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 +{ + class LuaUnderingSerializeVisitor : DecoratorFuncVisitor + { + public static LuaUnderingSerializeVisitor Ins { get; } = new LuaUnderingSerializeVisitor(); + + public override string DoAccept(TType type, string bufName, string fieldName) + { + return $"{type.Apply(LuaSerializeMethodNameVisitor.Ins)}({bufName}, {fieldName})"; + } + + public override string Accept(TArray type, string bufName, string fieldName) + { + return $"writeArray({bufName}, {fieldName}, {type.ElementType.Apply(LuaSerializeMethodNameVisitor.Ins)})"; + } + + public override string Accept(TList type, string bufName, string fieldName) + { + return $"writeList({bufName}, {fieldName}, {type.ElementType.Apply(LuaSerializeMethodNameVisitor.Ins)})"; + } + + public override string Accept(TSet type, string bufName, string fieldName) + { + return $"writeBool({bufName}, {fieldName}, {type.ElementType.Apply(LuaSerializeMethodNameVisitor.Ins)})"; + } + + public override string Accept(TMap type, string bufName, string fieldName) + { + return $"writeBool({bufName}, {fieldName}, {type.KeyType.Apply(LuaSerializeMethodNameVisitor.Ins)}, {type.ValueType.Apply(LuaSerializeMethodNameVisitor.Ins)})"; + } + } +} diff --git a/src/Luban.Job.Proto/Luban.Job.Proto.csproj b/src/Luban.Job.Proto/Luban.Job.Proto.csproj index e3fda32..c69ba47 100644 --- a/src/Luban.Job.Proto/Luban.Job.Proto.csproj +++ b/src/Luban.Job.Proto/Luban.Job.Proto.csproj @@ -9,4 +9,8 @@ + + + + diff --git a/src/Luban.Job.Proto/Source/Defs/TTypeTemplateExtends.cs b/src/Luban.Job.Proto/Source/Defs/TTypeTemplateExtends.cs index 8c7cf34..e79facb 100644 --- a/src/Luban.Job.Proto/Source/Defs/TTypeTemplateExtends.cs +++ b/src/Luban.Job.Proto/Source/Defs/TTypeTemplateExtends.cs @@ -1,45 +1,10 @@ using Luban.Job.Common.Defs; using Luban.Job.Common.Types; using Luban.Job.Common.TypeVisitors; -using Luban.Job.Proto.TypeVisitors; namespace Luban.Job.Proto.Defs { class TTypeTemplateExtends : TTypeTemplateCommonExtends { - public static string CsSerialize(string bufName, string fieldName, TType type) - { - return type.Apply(CsSerializeVisitor.Ins, bufName, fieldName); - } - - public static string CsCompatibleSerialize(string bufName, string fieldName, TType type) - { - return type.Apply(CsSerializeVisitor.Ins, bufName, fieldName); - } - - public static string CsDeserialize(string bufName, string fieldName, TType type) - { - return type.Apply(CsDeserializeVisitor.Ins, bufName, fieldName); - } - - public static string CsCompatibleDeserialize(string bufName, string fieldName, TType type) - { - return type.Apply(CsDeserializeVisitor.Ins, bufName, fieldName); - } - - public static string LuaCommentType(TType type) - { - return type.Apply(LuaCommentTypeVisitor.Ins); - } - - public static string LuaUnderingDeserialize(string bufName, TType type) - { - return type.Apply(LuaUnderingDeserializeVisitor.Ins, bufName); - } - - public static string CsInitFieldCtorValue(string bufName, TType type) - { - return $"{bufName} = {type.Apply(CsCtorValueVisitor.Ins)};"; - } } } diff --git a/src/Luban.Job.Proto/Source/Generate/LuaRender.cs b/src/Luban.Job.Proto/Source/Generate/LuaRender.cs index 17d6500..30bc4fa 100644 --- a/src/Luban.Job.Proto/Source/Generate/LuaRender.cs +++ b/src/Luban.Job.Proto/Source/Generate/LuaRender.cs @@ -20,6 +20,12 @@ namespace Luban.Job.Proto.Generate var protos = types.Where(t => t is DefProto).ToList(); var rpcs = types.Where(t => t is DefRpc).ToList(); var template = t_allRender ??= Template.Parse(@" +{{ + consts = x.consts + enums = x.enums + beans = x.beans + protos = x.protos +}} local setmetatable = setmetatable local pairs = pairs local ipairs = ipairs @@ -48,24 +54,24 @@ end local consts = { - {{- for c in consts }} + {{~ for c in consts ~}} ---@class {{c.full_name}} - {{- for item in c.items }} + {{~ for item in c.items ~}} ---@field public {{item.name}} {{item.type}} - {{-end}} - ['{{c.full_name}}'] = { {{ for item in c.items }} {{item.name}}={{item.to_lua_const_value}}, {{end}} }; - {{-end}} + {{~end~}} + ['{{c.full_name}}'] = { {{ for item in c.items }} {{item.name}}={{lua_const_value item.ctype item.value}}, {{end}} }; + {{~end~}} } local enums = { - {{- for c in enums }} + {{~ for c in enums ~}} ---@class {{c.full_name}} - {{- for item in c.items }} + {{~ for item in c.items ~}} ---@field public {{item.name}} int - {{-end}} + {{~end~}} ['{{c.full_name}}'] = { {{ for item in c.items }} {{item.name}}={{item.int_value}}, {{end}} }; - {{-end}} + {{~end~}} } @@ -181,6 +187,12 @@ local function InitTypes(methods) return map end + local function readNullableBool(bs) + if readBool(bs) then + return readBool(bs) + end + end + local default_vector2 = {x=0,y=0} local default_vector3 = {x=0,y=0,z=0} local default_vector4 = {x=0,y=0,z=0,w=0} @@ -189,17 +201,16 @@ local function InitTypes(methods) {{ for bean in beans }} do ---@class {{bean.full_name}} {{if bean.parent_def_type}}:{{bean.parent}} {{end}} - {{- for field in bean.fields}} - ---@field public {{field.name}} {{field.lua_comment_type}} - {{-end}} + {{~ for field in bean.fields~}} + ---@field public {{field.name}} {{lua_comment_type field.ctype}} + {{~end}} local class = SimpleClass() class._id = {{bean.id}} class._name = '{{bean.full_name}}' - --local name2id = { {{for c in bean.hierarchy_not_abstract_children}} ['{{c.full_name}}'] = {{c.id}}, {{end}} } local id2name = { {{for c in bean.hierarchy_not_abstract_children}} [{{c.id}}] = '{{c.full_name}}', {{end}} } {{if bean.is_abstract_type}} class._serialize = function(bs, self) - writeInt(bs, self._id) + writeInt(bs, {{bean.id}}) beans[self._name]._serialize(bs, self) end class._deserialize = function(bs) @@ -208,15 +219,19 @@ local function InitTypes(methods) end {{else}} class._serialize = function(bs, self) - {{- for field in bean.hierarchy_fields }} - {{field.proto_lua_serialize_while_nil}} - {{-end}} + {{~ for field in bean.hierarchy_fields ~}} + {{lua_serialize_while_nil 'bs' ('self.' + field.name) field.ctype}} + {{~end~}} end class._deserialize = function(bs) local o = { - {{- for field in bean.hierarchy_fields }} - {{field.name}} = {{field.proto_lua_deserialize}}, - {{-end}} + {{~ for field in bean.hierarchy_fields ~}} + {{~if !(need_marshal_bool_prefix field.ctype)~}} + {{field.name}} = {{lua_undering_deserialize 'bs' field.ctype}}, + {{~else~}} + {{field.name}} = {{if !field.ctype.is_bool}}readBool(bs) and {{lua_undering_deserialize 'bs' field.ctype}} or nil {{else}} readNullableBool(bs) {{end}}, + {{~end~}} + {{~end~}} } setmetatable(o, class) return o @@ -230,74 +245,43 @@ local function InitTypes(methods) {{ for proto in protos }} do ---@class {{proto.full_name}} - {{- for field in proto.fields}} - ---@field public {{field.name}} {{field.lua_comment_type}} - {{-end}} + {{~ for field in proto.fields~}} + ---@field public {{field.name}} {{lua_comment_type field.ctype}} + {{~end}} local class = SimpleClass() class._id = {{proto.id}} class._name = '{{proto.full_name}}' - class._serialize = function(self, bs) - {{- for field in proto.fields }} - {{field.proto_lua_serialize_while_nil}} - {{-end}} + class._serialize = function(bs, self) + {{~ for field in proto.fields ~}} + {{lua_serialize_while_nil 'bs' ('self.' + field.name) field.ctype}} + {{~end~}} end - class._deserialize = function(self, bs) - {{- for field in proto.fields }} - self.{{field.name}} = {{field.proto_lua_deserialize}} - {{-end}} + class._deserialize = function(bs) + local o = { + {{~ for field in proto.fields ~}} + {{~if !(need_marshal_bool_prefix field.ctype)~}} + {{field.name}} = {{lua_undering_deserialize 'bs' field.ctype}}, + {{~else~}} + {{field.name}} = {{if !field.ctype.is_bool}}readBool(bs) and {{lua_undering_deserialize 'bs' field.ctype}} or nil {{else}} readNullableBool(bs) {{end}}, + {{~end~}} + {{~end~}} + } + setmetatable(o, class) + return o end protos[class._id] = class protos[class._name] = class end {{end}} - local rpcs = { } -{{ for rpc in rpcs }} - do - ---@class {{rpc.full_name}} - ---@field public is_request bool - ---@field public rpc_id long - ---@field public arg {{rpc.targ_type.lua_comment_type}} - ---@field public res {{rpc.tres_type.lua_comment_type}} - local class = SimpleClass() - class._id = {{rpc.id}} - class._name = '{{rpc.full_name}}' - class._arg_name = '{{rpc.targ_type.bean.full_name}}' - class._res_name = '{{rpc.tres_type.bean.full_name}}' - class._serialize = function(self, bs) - local composite_id = self.rpc_id * 2 - if self.is_request then - writeLong(bs, composite_id) - beans['{{rpc.targ_type.bean.full_name}}']._serialize(self.arg, bs) - else - writeLong(bs, composite_id + 1) - beans['{{rpc.tres_type.bean.full_name}}']._serialize(self.res, bs) - end - end - class._deserialize = function(self, bs) - local composite_id = readLong(bs) - self.rpc_id = composite_id // 2 - if composite_id % 2 == 0 then - self.is_request = true - self.arg = beans['{{rpc.targ_type.bean.full_name}}']._deserialize(bs) - else - self.is_request = false - self.res = beans['{{rpc.tres_type.bean.full_name}}']._deserialize(bs) - end - end - rpcs[class._id] = class - rpcs[class._name] = class - end -{{end}} - - return { consts = consts, enums = enums, beans = beans, protos = protos, rpcs = rpcs } + return { consts = consts, enums = enums, beans = beans, protos = protos } end return { InitTypes = InitTypes} "); - return template.Render(new { Consts = consts, Enums = enums, Beans = beans, Protos = protos, Rpcs = rpcs }); + return template.RenderCode(new { Consts = consts, Enums = enums, Beans = beans, Protos = protos, Rpcs = rpcs }); } } } diff --git a/src/Luban.Job.Proto/Source/JobController.cs b/src/Luban.Job.Proto/Source/JobController.cs index 0bda24c..2e432c6 100644 --- a/src/Luban.Job.Proto/Source/JobController.cs +++ b/src/Luban.Job.Proto/Source/JobController.cs @@ -139,7 +139,7 @@ namespace Luban.Job.Proto { var render = new LuaRender(); var content = FileHeaderUtil.ConcatAutoGenerationHeader(render.RenderTypes(ass.Types.Values.ToList()), Common.ELanguage.LUA); - var file = "ProtoTypes.lua"; + var file = "Types.lua"; var md5 = CacheFileUtil.GenMd5AndAddCache(file, content); genCodeFiles.Add(new FileInfo() { FilePath = file, MD5 = md5 }); })); diff --git a/src/Luban.Job.Proto/Source/TypeVisitors/CsDeserializeVisitor.cs b/src/Luban.Job.Proto/Source/TypeVisitors/CsDeserializeVisitor.cs deleted file mode 100644 index 20ed8b4..0000000 --- a/src/Luban.Job.Proto/Source/TypeVisitors/CsDeserializeVisitor.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Luban.Job.Common.Types; -using Luban.Job.Common.TypeVisitors; - -namespace Luban.Job.Proto.TypeVisitors -{ - 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; }}"; - } - else - { - return type.Apply(CsUnderingDeserializeVisitor.Ins, bufName, fieldName); - } - } - - public override string Accept(TBean type, string bufName, string fieldName) - { - return type.Apply(CsUnderingDeserializeVisitor.Ins, bufName, fieldName); - } - } -}