【修复】修复 proto lua 代码生成。同时支持 可空变量

【重构】将cs serialize 相关的visitor移到 Job.Common 项目
main
walon 2020-11-18 06:44:05 +08:00
parent fba34e501c
commit f9a60b6e63
19 changed files with 287 additions and 293 deletions

View File

@ -1,6 +1,7 @@
using Bright.Serialization; using Bright.Serialization;
using Luban.Job.Cfg.Datas; using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common.TypeVisitors;
using System.Collections.Generic; using System.Collections.Generic;
namespace Luban.Job.Cfg.DataVisitors namespace Luban.Job.Cfg.DataVisitors
@ -102,7 +103,7 @@ namespace Luban.Job.Cfg.DataVisitors
continue; continue;
} }
if (defField.NeedMarshalBoolPrefix) if (defField.CType.Apply(NeedMarshalBoolPrefixVisitor.Ins))
{ {
if (field != null) if (field != null)
{ {

View File

@ -115,8 +115,6 @@ namespace Luban.Job.Cfg.Defs
public bool IsResource => !string.IsNullOrEmpty(ResourceTag); public bool IsResource => !string.IsNullOrEmpty(ResourceTag);
public bool NeedMarshalBoolPrefix => CType.Apply(NeedMarshalBoolPrefixVisitor.Ins);
public string CsRefVarName => $"{CsStyleName}_Ref"; public string CsRefVarName => $"{CsStyleName}_Ref";
public string JavaRefVarName => $"{JavaStyleName}_Ref"; public string JavaRefVarName => $"{JavaStyleName}_Ref";

View File

@ -7,15 +7,6 @@ namespace Luban.Job.Cfg.Defs
{ {
class TTypeTemplateExtends : TTypeTemplateCommonExtends 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) 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); 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) public static string GoDefineType(TType type)
{ {
@ -106,7 +87,7 @@ namespace Luban.Job.Cfg.Defs
{ {
var name = field.CsStyleName; var name = field.CsStyleName;
TType type = field.CType; 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 }} }} }}"; 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 }} }} }}";
} }

View File

@ -154,7 +154,7 @@ local function InitTypes(methods)
class._deserialize = function(bs) class._deserialize = function(bs)
local o = { local o = {
{{~ for field in bean.hierarchy_export_fields ~}} {{~ 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}}, {{field.name}} = {{lua_undering_deserialize 'bs' field.ctype}},
{{~else~}} {{~else~}}
{{field.name}} = {{if !field.ctype.is_bool}}readBool(bs) and {{lua_undering_deserialize 'bs' field.ctype}} or nil {{-else-}} readNullableBool(bs) {{-end-}}, {{field.name}} = {{if !field.ctype.is_bool}}readBool(bs) and {{lua_undering_deserialize 'bs' field.ctype}} or nil {{-else-}} readNullableBool(bs) {{-end-}},

View File

@ -1,126 +0,0 @@
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Cfg.TypeVisitors
{
class CsUnderingDeserializeVisitor : ITypeFuncVisitor<string, string, string>
{
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();";
}
}
}

View File

@ -65,7 +65,6 @@ namespace Luban.Job.Common.Defs
public string UpperCaseName => Name.ToUpper(); public string UpperCaseName => Name.ToUpper();
public DefFieldBase(DefTypeBase host, Field f, int idOffset) public DefFieldBase(DefTypeBase host, Field f, int idOffset)
{ {
HostType = host; HostType = host;

View File

@ -11,6 +11,11 @@ namespace Luban.Job.Common.Defs
return type.Apply(TagNameVisitor.Ins); return type.Apply(TagNameVisitor.Ins);
} }
public static bool NeedMarshalBoolPrefix(TType type)
{
return type.Apply(NeedMarshalBoolPrefixVisitor.Ins);
}
public static bool CsNeedInit(TType type) public static bool CsNeedInit(TType type)
{ {
return type.Apply(CsNeedInitVisitor.Ins); return type.Apply(CsNeedInitVisitor.Ins);
@ -31,6 +36,31 @@ namespace Luban.Job.Common.Defs
return type.Apply(CsConstValueVisitor.Ins, value); 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) public static string JavaDefineType(TType type)
{ {
return type.Apply(JavaDefineTypeName.Ins); return type.Apply(JavaDefineTypeName.Ins);
@ -66,6 +96,28 @@ namespace Luban.Job.Common.Defs
return type.Apply(LuaConstValueVisitor.Ins, value); 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) public static string GoConstValue(TType type, string value)
{ {
return type.Apply(LuaConstValueVisitor.Ins, value); return type.Apply(LuaConstValueVisitor.Ins, value);

View File

@ -1,9 +1,9 @@
using Luban.Job.Common.Types; using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors; using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Cfg.TypeVisitors namespace Luban.Job.Common.TypeVisitors
{ {
class CsDeserializeVisitor : DecoratorFuncVisitor<string, string, string> public class CsDeserializeVisitor : DecoratorFuncVisitor<string, string, string>
{ {
public static CsDeserializeVisitor Ins { get; } = new CsDeserializeVisitor(); public static CsDeserializeVisitor Ins { get; } = new CsDeserializeVisitor();

View File

@ -1,9 +1,9 @@
using Luban.Job.Common.Types; using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors; using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Proto.TypeVisitors namespace Luban.Job.Common.TypeVisitors
{ {
class CsSerializeVisitor : DecoratorFuncVisitor<string, string, string> public class CsSerializeVisitor : DecoratorFuncVisitor<string, string, string>
{ {
public static CsSerializeVisitor Ins { get; } = new CsSerializeVisitor(); public static CsSerializeVisitor Ins { get; } = new CsSerializeVisitor();

View File

@ -1,7 +1,7 @@
using Luban.Job.Common.Types; using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors; using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Proto.TypeVisitors namespace Luban.Job.Common.TypeVisitors
{ {
class CsUnderingDeserializeVisitor : ITypeFuncVisitor<string, string, string> class CsUnderingDeserializeVisitor : ITypeFuncVisitor<string, string, string>
{ {

View File

@ -1,7 +1,7 @@
using Luban.Job.Common.Types; using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors; using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Proto.TypeVisitors namespace Luban.Job.Common.TypeVisitors
{ {
class CsUnderingSerializeVisitor : ITypeFuncVisitor<string, string, string> class CsUnderingSerializeVisitor : ITypeFuncVisitor<string, string, string>
{ {

View File

@ -0,0 +1,124 @@
using Luban.Job.Common.Types;
namespace Luban.Job.Common.TypeVisitors
{
public class LuaSerializeMethodNameVisitor : ITypeFuncVisitor<string>
{
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";
}
}
}

View File

@ -13,7 +13,7 @@ namespace Luban.Job.Common.TypeVisitors
public override string Accept(TArray type, string x) 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) public override string Accept(TList type, string x)

View File

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

View File

@ -9,4 +9,8 @@
<ProjectReference Include="..\Luban.Job.Common\Luban.Job.Common.csproj" /> <ProjectReference Include="..\Luban.Job.Common\Luban.Job.Common.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Source\TypeVisitors\" />
</ItemGroup>
</Project> </Project>

View File

@ -1,45 +1,10 @@
using Luban.Job.Common.Defs; using Luban.Job.Common.Defs;
using Luban.Job.Common.Types; using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors; using Luban.Job.Common.TypeVisitors;
using Luban.Job.Proto.TypeVisitors;
namespace Luban.Job.Proto.Defs namespace Luban.Job.Proto.Defs
{ {
class TTypeTemplateExtends : TTypeTemplateCommonExtends 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)};";
}
} }
} }

View File

@ -20,6 +20,12 @@ namespace Luban.Job.Proto.Generate
var protos = types.Where(t => t is DefProto).ToList(); var protos = types.Where(t => t is DefProto).ToList();
var rpcs = types.Where(t => t is DefRpc).ToList(); var rpcs = types.Where(t => t is DefRpc).ToList();
var template = t_allRender ??= Template.Parse(@" var template = t_allRender ??= Template.Parse(@"
{{
consts = x.consts
enums = x.enums
beans = x.beans
protos = x.protos
}}
local setmetatable = setmetatable local setmetatable = setmetatable
local pairs = pairs local pairs = pairs
local ipairs = ipairs local ipairs = ipairs
@ -48,24 +54,24 @@ end
local consts = local consts =
{ {
{{- for c in consts }} {{~ for c in consts ~}}
---@class {{c.full_name}} ---@class {{c.full_name}}
{{- for item in c.items }} {{~ for item in c.items ~}}
---@field public {{item.name}} {{item.type}} ---@field public {{item.name}} {{item.type}}
{{-end}} {{~end~}}
['{{c.full_name}}'] = { {{ for item in c.items }} {{item.name}}={{item.to_lua_const_value}}, {{end}} }; ['{{c.full_name}}'] = { {{ for item in c.items }} {{item.name}}={{lua_const_value item.ctype item.value}}, {{end}} };
{{-end}} {{~end~}}
} }
local enums = local enums =
{ {
{{- for c in enums }} {{~ for c in enums ~}}
---@class {{c.full_name}} ---@class {{c.full_name}}
{{- for item in c.items }} {{~ for item in c.items ~}}
---@field public {{item.name}} int ---@field public {{item.name}} int
{{-end}} {{~end~}}
['{{c.full_name}}'] = { {{ for item in c.items }} {{item.name}}={{item.int_value}}, {{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 return map
end end
local function readNullableBool(bs)
if readBool(bs) then
return readBool(bs)
end
end
local default_vector2 = {x=0,y=0} local default_vector2 = {x=0,y=0}
local default_vector3 = {x=0,y=0,z=0} local default_vector3 = {x=0,y=0,z=0}
local default_vector4 = {x=0,y=0,z=0,w=0} local default_vector4 = {x=0,y=0,z=0,w=0}
@ -189,17 +201,16 @@ local function InitTypes(methods)
{{ for bean in beans }} {{ for bean in beans }}
do do
---@class {{bean.full_name}} {{if bean.parent_def_type}}:{{bean.parent}} {{end}} ---@class {{bean.full_name}} {{if bean.parent_def_type}}:{{bean.parent}} {{end}}
{{- for field in bean.fields}} {{~ for field in bean.fields~}}
---@field public {{field.name}} {{field.lua_comment_type}} ---@field public {{field.name}} {{lua_comment_type field.ctype}}
{{-end}} {{~end}}
local class = SimpleClass() local class = SimpleClass()
class._id = {{bean.id}} class._id = {{bean.id}}
class._name = '{{bean.full_name}}' 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}} } local id2name = { {{for c in bean.hierarchy_not_abstract_children}} [{{c.id}}] = '{{c.full_name}}', {{end}} }
{{if bean.is_abstract_type}} {{if bean.is_abstract_type}}
class._serialize = function(bs, self) class._serialize = function(bs, self)
writeInt(bs, self._id) writeInt(bs, {{bean.id}})
beans[self._name]._serialize(bs, self) beans[self._name]._serialize(bs, self)
end end
class._deserialize = function(bs) class._deserialize = function(bs)
@ -208,15 +219,19 @@ local function InitTypes(methods)
end end
{{else}} {{else}}
class._serialize = function(bs, self) class._serialize = function(bs, self)
{{- for field in bean.hierarchy_fields }} {{~ for field in bean.hierarchy_fields ~}}
{{field.proto_lua_serialize_while_nil}} {{lua_serialize_while_nil 'bs' ('self.' + field.name) field.ctype}}
{{-end}} {{~end~}}
end end
class._deserialize = function(bs) class._deserialize = function(bs)
local o = { local o = {
{{- for field in bean.hierarchy_fields }} {{~ for field in bean.hierarchy_fields ~}}
{{field.name}} = {{field.proto_lua_deserialize}}, {{~if !(need_marshal_bool_prefix field.ctype)~}}
{{-end}} {{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) setmetatable(o, class)
return o return o
@ -230,74 +245,43 @@ local function InitTypes(methods)
{{ for proto in protos }} {{ for proto in protos }}
do do
---@class {{proto.full_name}} ---@class {{proto.full_name}}
{{- for field in proto.fields}} {{~ for field in proto.fields~}}
---@field public {{field.name}} {{field.lua_comment_type}} ---@field public {{field.name}} {{lua_comment_type field.ctype}}
{{-end}} {{~end}}
local class = SimpleClass() local class = SimpleClass()
class._id = {{proto.id}} class._id = {{proto.id}}
class._name = '{{proto.full_name}}' class._name = '{{proto.full_name}}'
class._serialize = function(self, bs) class._serialize = function(bs, self)
{{- for field in proto.fields }} {{~ for field in proto.fields ~}}
{{field.proto_lua_serialize_while_nil}} {{lua_serialize_while_nil 'bs' ('self.' + field.name) field.ctype}}
{{-end}} {{~end~}}
end end
class._deserialize = function(self, bs) class._deserialize = function(bs)
{{- for field in proto.fields }} local o = {
self.{{field.name}} = {{field.proto_lua_deserialize}} {{~ for field in proto.fields ~}}
{{-end}} {{~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 end
protos[class._id] = class protos[class._id] = class
protos[class._name] = class protos[class._name] = class
end end
{{end}} {{end}}
local rpcs = { } return { consts = consts, enums = enums, beans = beans, protos = protos }
{{ 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 }
end end
return { InitTypes = InitTypes} 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 });
} }
} }
} }

View File

@ -139,7 +139,7 @@ namespace Luban.Job.Proto
{ {
var render = new LuaRender(); var render = new LuaRender();
var content = FileHeaderUtil.ConcatAutoGenerationHeader(render.RenderTypes(ass.Types.Values.ToList()), Common.ELanguage.LUA); 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); var md5 = CacheFileUtil.GenMd5AndAddCache(file, content);
genCodeFiles.Add(new FileInfo() { FilePath = file, MD5 = md5 }); genCodeFiles.Add(new FileInfo() { FilePath = file, MD5 = md5 });
})); }));

View File

@ -1,27 +0,0 @@
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Proto.TypeVisitors
{
class CsDeserializeVisitor : DecoratorFuncVisitor<string, string, string>
{
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);
}
}
}