diff --git a/src/writeCs/gCsField.erl b/src/writeCs/gCsField.erl index a54ef07..44c5dc1 100644 --- a/src/writeCs/gCsField.erl +++ b/src/writeCs/gCsField.erl @@ -30,7 +30,7 @@ , {<<"string">>, <<"string">>} ]). -builtRecStr({TypeStr, NameStr}) -> +builtMemberStr({TypeStr, NameStr}) -> case lists:keyfind(TypeStr, 1, ?TypeValue) of {TypeStr, CSTypeStr} -> <<"\t\tpublic ", CSTypeStr/binary, " ", NameStr/binary, ";\n">>; diff --git a/src/writeCs/gCsGen.erl b/src/writeCs/gCsGen.erl index 45707c5..5cda89a 100644 --- a/src/writeCs/gCsGen.erl +++ b/src/writeCs/gCsGen.erl @@ -340,7 +340,7 @@ spellClassHead(MsgName, MsgId) -> <<"\tpublic class ", MsgName/binary, " : ProtocolCore.ISerialize, ProtocolCore.IDeserialize<", MsgName/binary, ">\n\t{\n\t\tpublic const int ProtoId = ", (integer_to_binary(MsgId))/binary, ";\n">>. spellClassMember(FieldList) -> - <<<<(gCsField:builtRecStr(OneTypeName))/binary>> || OneTypeName <- FieldList>>. + <<<<(gCsField:builtMemberStr(OneTypeName))/binary>> || OneTypeName <- FieldList>>. spellCalssDSTem() -> <<"\n\t\tpublic byte[] Serialize()\n\t\t{\n\t\t\tusing var memoryStream = new MemoryStream(); @@ -369,7 +369,7 @@ spellCalssDeserialize(FieldList) -> spellClassEnd() -> <<"\t}\n">>. -genCs(SortedSProtoList, _SortedErrList, _HrlDir, CSDir) -> +genCs(SortedSProtoList, _SortedErrList, CSDir, _) -> FunSpell = fun({MsgName, MsgId, FieldList}, ClassBinAcc) -> H = spellClassHead(MsgName, MsgId), @@ -383,15 +383,14 @@ genCs(SortedSProtoList, _SortedErrList, _HrlDir, CSDir) -> LastClassBinAcc = lists:foldl(FunSpell, <<>>, SortedSProtoList), %% todo error code - %% ErrCodeStr = spellErrCodeHrl(SortedErrList, <<>>), CSHeaderStr = protoHeader(), CSEndStr = protoEnd(), - OutputHrlStr = <>, - CSFilename = do_write_cs(CSDir, protoMsg, OutputHrlStr), + OutputCsStr = <>, + CSFilename = do_write_cs(CSDir, protoMsg, OutputCsStr), - io:format("protoConvert erl dir : ~s ~n", [CSDir]), - io:format("protoConvert to erl file ~s succ.~n", [CSFilename]), + io:format("protoConvert cs dir : ~s ~n", [CSDir]), + io:format("protoConvert to cs file ~s succ.~n", [CSFilename]), ok. do_write_cs(OutDir, Mod, BinStr) -> diff --git a/src/writeLua/gLuaField.erl b/src/writeLua/gLuaField.erl index 91bbd4e..dde1691 100644 --- a/src/writeLua/gLuaField.erl +++ b/src/writeLua/gLuaField.erl @@ -16,35 +16,74 @@ ]). -define(TypeValue, [ - {<<"bool">>, <<"bool">>} - , {<<"int8">>, <<"int8">>} - , {<<"uint8">>, <<"uint8">>} - , {<<"int16">>, <<"int16">>} - , {<<"uint16">>, <<"uint16">>} - , {<<"int32">>, <<"int32">>} - , {<<"uint32">>, <<"uint32">>} - , {<<"int64">>, <<"int64">>} - , {<<"uint64">>, <<"uint64">>} - , {<<"float">>, <<"float">>} - , {<<"double">>, <<"double">>} - , {<<"string">>, <<"string">>} + {<<"bool">>, <<"bool">>, <<"false">>} + , {<<"int8">>, <<"int8">>, <<"0">>} + , {<<"uint8">>, <<"uint8">>, <<"0">>} + , {<<"int16">>, <<"int16">>, <<"0">>} + , {<<"uint16">>, <<"uint16">>, <<"0">>} + , {<<"int32">>, <<"int32">>, <<"0">>} + , {<<"uint32">>, <<"uint32">>, <<"0">>} + , {<<"int64">>, <<"int64">>, <<"0">>} + , {<<"uint64">>, <<"uint64">>, <<"0">>} + , {<<"float">>, <<"float">>, <<"0">>} + , {<<"double">>, <<"double">>, <<"0">>} + , {<<"string">>, <<"string">>, <<"\"\"">>} ]). -builtRecStr({TypeStr, NameStr}) -> +builtMemberStr({TypeStr, NameStr}) -> case lists:keyfind(TypeStr, 1, ?TypeValue) of - {TypeStr, CSTypeStr} -> - <<"\t\tpublic ", CSTypeStr/binary, " ", NameStr/binary, ";\n">>; + {TypeStr, _LuaTypeStr, DefValue} -> + <<"\tt.", NameStr/binary, " = ", DefValue/binary, "\n">>; _ -> - case TypeStr of - <<"list[", LeftStr/binary>> -> - [SubTypeStr | _] = re:split(LeftStr, <<"\\]">>, [{return, binary}]), - case lists:keyfind(SubTypeStr, 1, ?TypeValue) of - {SubTypeStr, SubCSTypeStr} -> - <<"\t\tpublic List<", SubCSTypeStr/binary, "> ", NameStr/binary, ";\n">>; - _ -> - <<"\t\tpublic List<", SubTypeStr/binary, "> ", NameStr/binary, ";\n">> - end; - _ -> - <<"\t\tpublic ", TypeStr/binary, " ", NameStr/binary, ";\n">> - end + <<"\tt.", NameStr/binary, " = {}", "\n">> end. + +builtEncodeStr({TypeStr, NameStr}) -> + ok. +%%%%%%%%%%%%%%%%%%%%%%%%%%%%生成encode%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% byteArray.write_uint16(account.size()); +%% for(int i=0; i + case lists:keyfind(Type, 1, TypeList) of + {Type, base_type} -> + " \tbyteArray.write_"++ atom_to_list(Type) ++"(tb." ++ get_csharp_Var(Name) ++ ")\n"; + {Type, string_type} -> + " \tbyteArray.write_"++ atom_to_list(Type) ++"(tb." ++ get_csharp_Var(Name) ++ ")\n"; + {_Type, enum_type} -> + make_protocal_encode_item({int, Name}, TypeList); + _ -> + " tb." ++ get_csharp_Var(Name) ++ ".encode(byteArray);\n" + %%" " ++ get_csharp_Var(Name) ++ ".encode(byteArray)\n\n" + end; +make_protocal_encode_item({Type, Name, _DefaultValue}, TypeList) + when Type /= array-> + make_protocal_encode_item({Type, Name}, TypeList); +make_protocal_encode_item({array, Type, Name}, TypeList) -> + case lists:keyfind(Type, 1, TypeList) of + {Type1, base_type} -> + make_protocal_encode_item_base_type(Name, Type1); + {Type1, string_type} -> + make_protocal_encode_item_base_type(Name, Type1); + {_Type, enum_type} -> + make_protocal_encode_item({array, int, Name}, TypeList); + _ -> + NameStr = get_csharp_Var(Name), + " byteArray.write_uint16(#(tb." ++ NameStr ++ "))\n" ++ + " for k, v in pairs(tb." ++ NameStr ++ ") do\n" ++ + " byteArray = v.encode(byteArray)\n" ++ + " end\n" + end. + +make_protocal_encode_item_base_type(Name, Type) -> + NameStr = get_csharp_Var(Name), + TypeStr = get_csharp_type(Type), + " byteArray.write_uint16(#(tb." ++ NameStr ++ "))\n" ++ + " for k, v in pairs (tb." ++ NameStr ++ ") do\n" ++ + " byteArray.write_" ++ TypeStr ++ "(v)\n" ++ + " end\n". + diff --git a/src/writeLua/gLuaGen.erl b/src/writeLua/gLuaGen.erl index 3ade8d8..cfa7db3 100644 --- a/src/writeLua/gLuaGen.erl +++ b/src/writeLua/gLuaGen.erl @@ -4,69 +4,47 @@ genLua/4 ]). -spellClassHead(MsgName, MsgId) -> - <<"\tpublic class ", MsgName/binary, " : ProtocolCore.ISerialize, ProtocolCore.IDeserialize<", MsgName/binary, ">\n\t{\n\t\tpublic const int ProtoId = ", (integer_to_binary(MsgId))/binary, ";\n">>. +spellFunHead(MsgName, MsgId) -> + <<"function", MsgName/binary, "()\n\tlocal tb = {}\n\ttb.msgId =", (integer_to_binary(MsgId))/binary, "\n">>. -spellClassMember(FieldList) -> - <<<<(gCsField:builtRecStr(OneTypeName))/binary>> || OneTypeName <- FieldList>>. +spellMember(FieldList) -> + <<<<(gLuaField:builtMemberStr(OneTypeName))/binary>> || OneTypeName <- FieldList>>. -spellCalssDSTem() -> - <<"\n\t\tpublic byte[] Serialize()\n\t\t{\n\t\t\tusing var memoryStream = new MemoryStream(); -\t\t\tusing var binaryWriter = new BinaryWriter(memoryStream); -\t\t\tSerialize(binaryWriter); -\t\t\treturn memoryStream.ToArray(); -\t\t} +spellEncode(FieldList) -> + EnHead = <<"\n\ttb.encode = function(byteArray)\n">>, + EnBody = <<<<(gLuaField:builtEncodeStr(OneTypeName))/binary>> || OneTypeName <- FieldList>>, + EnEnd = <<"\t\treturn byteArray\n\tend\n\n">>, + <>. -\t\tpublic void Deserialize(byte[] data) -\t\t{ -\t\t\tusing var memoryStream = new MemoryStream(data); -\t\t\tusing var binaryReader = new BinaryReader(memoryStream); -\t\t\tDeserialize(binaryReader); -\t\t}\n">>. - -spellCalssSerialize(FieldList) -> - FunHead = <<"\t\tpublic void Serialize(BinaryWriter binaryWriter)\n\t\t{\n">>, - FunBody = <<<<"\t\t\tbinaryWriter.WriteValue(", NameStr/binary, ");\n">> || {_TypeStr, NameStr} <- FieldList>>, - <>. - -spellCalssDeserialize(FieldList) -> - FunHead = <<"\t\tpublic void Deserialize(BinaryReader binaryReader)\n\t\t{\n">>, - FunBody = <<<<"\t\t\tbinaryReader.ReadValue(out ", NameStr/binary, ");\n">> || {_TypeStr, NameStr} <- FieldList>>, - <>. - -spellClassEnd() -> - <<"\t}\n">>. - -genLua(SortedSProtoList, _SortedErrList, _HrlDir, CSDir) -> +genLua(SortedSProtoList, _SortedErrList, LuaDir, _) -> FunSpell = - fun({MsgName, MsgId, FieldList}, ClassBinAcc) -> - H = spellClassHead(MsgName, MsgId), - M = spellClassMember(FieldList), - DS = spellCalssDSTem(), - S = spellCalssSerialize(FieldList), - D = spellCalssDeserialize(FieldList), - E = spellClassEnd(), - <> + fun({MsgName, MsgId, FieldList}, {TableBinAcc, MsgNameBinAcc}) -> + H = spellFunHead(MsgName, MsgId), + M = spellMember(FieldList), + E = spellEncode(FieldList), + D = spellDecode(FieldList), + B = spellBuild(MsgId), + End = spellEnd(), + MsgNameBin = spellMsgName(MsgId, MsgName), + {<>, <>} end, - LastClassBinAcc = lists:foldl(FunSpell, <<>>, SortedSProtoList), + {LastTableBinAcc, MsgNameBody} = lists:foldl(FunSpell, {<<>>, <<>>}, SortedSProtoList), + LastMsgNameBody = <<"ProtoMsgName =\n{\n", MsgNameBody/binary, "}">>, %% todo error code - %% ErrCodeStr = spellErrCodeHrl(SortedErrList, <<>>), - - CSEndStr = protoEnd(), - OutputHrlStr = <>, - CSFilename = do_write_cs(CSDir, protoMsg, OutputHrlStr), + LuaFilename = do_write_lua(LuaDir, protoMsg, LastTableBinAcc), + MsgNameFile = do_write_lua(LuaDir, protoName, LastMsgNameBody), - io:format("protoConvert erl dir : ~s ~n", [CSDir]), - io:format("protoConvert to erl file ~s succ.~n", [CSFilename]), + io:format("protoConvert lua dir : ~s ~n", [LuaDir]), + io:format("protoConvert to lua file ~s ~s succ.~n", [LuaFilename, MsgNameFile]), ok. -do_write_cs(OutDir, Mod, BinStr) -> - Filename = filename:join([OutDir, atom_to_list(Mod) ++ ".cs"]), +do_write_lua(OutDir, Mod, BinStr) -> + Filename = filename:join([OutDir, atom_to_list(Mod) ++ ".lua"]), case file:write_file(Filename, BinStr) of ok -> ok; _Ret -> - io:format("write to cs file error:~p ~n", [_Ret]) + io:format("write to lua file error:~p ~n", [_Ret]) end, Filename. \ No newline at end of file diff --git a/src/writeLua/gLuaGen1.erl b/src/writeLua/gLuaGen1.erl index 9cd749c..e478211 100644 --- a/src/writeLua/gLuaGen1.erl +++ b/src/writeLua/gLuaGen1.erl @@ -14,13 +14,10 @@ start() -> StructList = protocol_def:get_struct_def(), TypeList = gen_common:get_type(), FileStr = make_protocal(StructList, "", TypeList), - file:write_file("NetPacket.lua", FileStr), - EnumStr = make_protocal_enum(protocol_def:get_enum_def(), ""), - ConstStr = make_protocal_const(protocol_def:get_version()), - file:write_file("NetEnumDef.lua", EnumStr ++ "\n" ++ ConstStr), + MsgTypeStr = make_protocal_msg_type(StructList, "", 1), - file:write_file("NetMsgType.lua", MsgTypeStr). + file:write_file("protoName.lua", MsgTypeStr). make_protocal([], StructStr, _TypeList) -> StructStr;