Переглянути джерело

ft: lua序列化代码

master
SisMaker 1 рік тому
джерело
коміт
7fe8b03ec4
7 змінених файлів з 2070 додано та 521 видалено
  1. +2
    -7
      README.md
  2. +83
    -72
      src/genProto.erl
  3. +76
    -74
      src/writeLua/gLuaField.erl
  4. +16
    -1
      src/writeLua/gLuaGen.erl
  5. +0
    -367
      src/writeLua/gLuaGen1.erl
  6. +1853
    -0
      test/protoMsg.lua
  7. +40
    -0
      test/protoName.lua

+ 2
- 7
README.md Переглянути файл

@ -42,15 +42,10 @@ Use
double: 直接存放64bit的值
bool: 占位8bit 如果为true 则存放的值为1 否则存放的值为0
record(struct): 如果是undefined 或者是空指针 则 8bit Tag 值为0, 否则 8bit的tag 值为1 + record的二进制数据
list_+上面的数据类型的时候: 16bit的tag 用于存放 数组的长度 + 按数组顺序序列化每个元素的值的二进制数据
list_+上面的数据类型的时候: 16bit的tag 用于存放 数组的长度 + 按数组顺序序列化每个元素的值的二进制数据(注意:如果列表类型为record(struct) 每个值序列化的时候并不会加 8bit的tag, 直接存record的二进制数据)
### maybe TODO
生成的protoMsg.erl
encode 函数参数列表太长时 换行显示
encode返回的编码列表参数太多时 换行显示
decodeBin simple类型解码列表过长时 换行显示
decodeBin 返回元组元素太多时 换行显示
lua 支持 integer number
### 关于消息接收转发解码和发送

+ 83
- 72
src/genProto.erl Переглянути файл

@ -9,87 +9,98 @@
-export([main/1]).
-export([
convertFile/1
, convert/1
, convertDir/0
, convertDir/1
, convertDir/3
convertFile/1
, convert/1
, convertDir/0
, convertDir/1
]).
main(Args) ->
case Args of
[] ->
convertDir(?DefProtoDir, ?DefErlDir, ?DefHrlDir);
_ ->
[ProtoDir, HrlDir, ErlDir] = Args,
convertDir(ProtoDir, HrlDir, ErlDir)
end.
case Args of
[] ->
convertDir(["./", "all", "./"]);
_ ->
convertDir(Args)
end.
convertFile(File) ->
erlang:erase(),
erlang:put(pd_errlist, []),
erlang:put(pd_handler, []),
case filename:extension(File) == ".mpdf" of
true ->
io:format("Convert proto msg file: ~s ~n", [File]),
BaseName = filename:basename(File, ".mpdf"),
[ModIndex, ModName] = re:split(BaseName, "_"),
Index = binary_to_integer(ModIndex),
erlang:put(pd_messageid, Index * ?MsgIdSegSize + 1),
erlang:put(pd_handler, [{Index, ModName} | erlang:get(pd_handler)]),
erlang:put(pd_errcodeid, Index * ?MsgIdSegSize + 1),
SProto = protoParse:parseFile(File),
ErrCode = erlang:get(pd_errlist),
Handler = erlang:get(pd_handler),
{SProto, Handler, ErrCode};
_ ->
io:format("not proto msg file: ~s ~n", [File])
end.
erlang:erase(),
erlang:put(pd_errlist, []),
erlang:put(pd_handler, []),
case filename:extension(File) == ".mpdf" of
true ->
io:format("Convert proto msg file: ~s ~n", [File]),
BaseName = filename:basename(File, ".mpdf"),
[ModIndex, ModName] = re:split(BaseName, "_"),
Index = binary_to_integer(ModIndex),
erlang:put(pd_messageid, Index * ?MsgIdSegSize + 1),
erlang:put(pd_handler, [{Index, ModName} | erlang:get(pd_handler)]),
erlang:put(pd_errcodeid, Index * ?MsgIdSegSize + 1),
SProto = protoParse:parseFile(File),
ErrCode = erlang:get(pd_errlist),
Handler = erlang:get(pd_handler),
{SProto, Handler, ErrCode};
_ ->
io:format("not proto msg file: ~s ~n", [File])
end.
convert([ProtoDir, HrlDir, ErlDir]) ->
convertDir(atom_to_list(ProtoDir), atom_to_list(HrlDir), atom_to_list(ErlDir)).
convert(Args) ->
convertDir(Args).
convertDir() ->
convertDir("./", "./", "./").
convertDir(ProtoDir) ->
convertDir(ProtoDir, "./", "./").
convertDir(ProtoDir, HrlDir, ErlDir) ->
erlang:erase(),
erlang:put(pd_errlist, []),
erlang:put(pd_handler, []),
FunRead =
fun(File, ProAcc) ->
case filename:extension(File) == ".mpdf" of
true ->
io:format("Convert proto msg file: ~s ~n", [File]),
BaseName = filename:basename(File, ".mpdf"),
[ModIndex, ModName] = re:split(BaseName, "_"),
Index = binary_to_integer(ModIndex),
erlang:put(pd_messageid, Index * ?MsgIdSegSize + 1),
erlang:put(pd_handler, [{Index, ModName} | erlang:get(pd_handler)]),
erlang:put(pd_errcodeid, Index * ?MsgIdSegSize + 1),
SProto = protoParse:parseFile(File),
ErrCode = erlang:get(pd_errlist),
Handler = erlang:get(pd_handler),
erlang:erase(),
erlang:put(pd_errlist, ErrCode),
erlang:put(pd_handler, Handler),
[SProto | ProAcc];
_ ->
ProAcc
end
end,
convertDir(["./", "all", "./"]).
convertDir([ProtoDir | LeftArgs]) ->
erlang:erase(),
erlang:put(pd_errlist, []),
erlang:put(pd_handler, []),
FunRead =
fun(File, ProAcc) ->
case filename:extension(File) == ".mpdf" of
true ->
io:format("Convert proto msg file: ~s ~n", [File]),
BaseName = filename:basename(File, ".mpdf"),
[ModIndex, ModName] = re:split(BaseName, "_"),
Index = binary_to_integer(ModIndex),
erlang:put(pd_messageid, Index * ?MsgIdSegSize + 1),
erlang:put(pd_handler, [{Index, ModName} | erlang:get(pd_handler)]),
erlang:put(pd_errcodeid, Index * ?MsgIdSegSize + 1),
SProto = protoParse:parseFile(File),
ErrCode = erlang:get(pd_errlist),
Handler = erlang:get(pd_handler),
erlang:erase(),
erlang:put(pd_errlist, ErrCode),
erlang:put(pd_handler, Handler),
[SProto | ProAcc];
_ ->
ProAcc
end
end,
SProtoListOfList = filelib:fold_files(ProtoDir, "\\.mpdf$", true, FunRead, []),
SProtoList = lists:append(SProtoListOfList),
ErrCodeList = erlang:get(pd_errlist),
SProtoListOfList = filelib:fold_files(ProtoDir, "\\.mpdf$", true, FunRead, []),
SProtoList = lists:append(SProtoListOfList),
ErrCodeList = erlang:get(pd_errlist),
SortedSProtoList = lists:sort(fun({_Name1, MessageId1, _FieldList1}, {_Name2, MessageId2, _FieldList2}) ->
MessageId1 < MessageId2 end, SProtoList),
SortedSProtoList = lists:sort(fun({_Name1, MessageId1, _FieldList1}, {_Name2, MessageId2, _FieldList2}) ->
MessageId1 < MessageId2 end, SProtoList),
SortedErrList = lists:sort(fun({_ErrName1, ErrCodeId1, _Desc1}, {_ErrName2, ErrCodeId2, _Desc2}) ->
ErrCodeId1 < ErrCodeId2 end, ErrCodeList),
genProto(LeftArgs, SortedSProtoList, SortedErrList).
%%
genProto([], _SortedSProtoList, _SortedErrList) ->
ok;
genProto(["all", AllDir], SortedSProtoList, SortedErrList) ->
gErlGen:genErl(SortedSProtoList, SortedErrList, AllDir, AllDir),
gCsGen:genCs(SortedSProtoList, SortedErrList, AllDir, AllDir),
gLuaGen:genLua(SortedSProtoList, SortedErrList, AllDir, AllDir);
genProto(["erl", HrlDir, ErlDir | LeftArgs], SortedSProtoList, SortedErrList) ->
gErlGen:genErl(SortedSProtoList, SortedErrList, HrlDir, ErlDir),
genProto(LeftArgs, SortedSProtoList, SortedErrList);
genProto(["cs", CsDir | LeftArgs], SortedSProtoList, SortedErrList) ->
gCsGen:genCs(SortedSProtoList, SortedErrList, CsDir, CsDir),
genProto(LeftArgs, SortedSProtoList, SortedErrList);
genProto(["lua", LuaDir | LeftArgs], SortedSProtoList, SortedErrList) ->
gLuaGen:genLua(SortedSProtoList, SortedErrList, LuaDir, LuaDir),
genProto(LeftArgs, SortedSProtoList, SortedErrList).
SortedErrList = lists:sort(fun({_ErrName1, ErrCodeId1, _Desc1}, {_ErrName2, ErrCodeId2, _Desc2}) ->
ErrCodeId1 < ErrCodeId2 end, ErrCodeList),
gErlGen:genErl(SortedSProtoList, SortedErrList, HrlDir, ErlDir),
gCsGen:genCs(SortedSProtoList, SortedErrList, HrlDir, ErlDir).
%%

+ 76
- 74
src/writeLua/gLuaField.erl Переглянути файл

@ -3,87 +3,89 @@
-compile([export_all, nowarn_export_all]).
-define(SimpleList, [
<<"int8">>
, <<"uint8">>
, <<"int16">>
, <<"uint16">>
, <<"int32">>
, <<"uint32">>
, <<"int64">>
, <<"uint64">>
, <<"float">>
, <<"double">>
<<"int8">>
, <<"uint8">>
, <<"int16">>
, <<"uint16">>
, <<"int32">>
, <<"uint32">>
, <<"int64">>
, <<"uint64">>
, <<"float">>
, <<"double">>
]).
-define(TypeValue, [
{<<"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">>, <<"\"\"">>}
{<<"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">>, <<"\"\"">>}
]).
builtMemberStr({TypeStr, NameStr}) ->
case lists:keyfind(TypeStr, 1, ?TypeValue) of
{TypeStr, _LuaTypeStr, DefValue} ->
<<"\tt.", NameStr/binary, " = ", DefValue/binary, "\n">>;
_ ->
<<"\tt.", NameStr/binary, " = {}", "\n">>
end.
case lists:keyfind(TypeStr, 1, ?TypeValue) of
{TypeStr, _LuaTypeStr, DefValue} ->
<<"\tt.", NameStr/binary, " = ", DefValue/binary, "\n">>;
_ ->
<<"\tt.", NameStr/binary, " = {}", "\n">>
end.
builtEncodeStr({TypeStr, NameStr}) ->
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%encode%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% byteArray.write_uint16(account.size());
%% for(int i=0; i<account.size();i++)
%% {
%% byteArray.write_string(account[i]);
%% }
case lists:keyfind(TypeStr, 1, ?TypeValue) of
{TypeStr, LuaTypeStr, _DefValue} ->
<<"\t\tbyteArray.write_", LuaTypeStr/binary, "(tb.", NameStr/binary, ")\n">>;
_ ->
case TypeStr of
<<"list[", LeftStr/binary>> ->
[SubTypeStr | _] = re:split(LeftStr, <<"\\]">>, [{return, binary}]),
case lists:keyfind(SubTypeStr, 1, ?TypeValue) of
{SubTypeStr, LuaTypeStr, _DefValue} ->
<<"\t\tbyteArray.write_uint16(#(tb.", NameStr/binary, "))\n",
"\t\tfor k, v in pairs (tb.", NameStr/binary, ") do\n",
"\t\t\tbyteArray.write_", LuaTypeStr/binary, "(v)\n",
"\t\tend\n">>;
_ ->
<<"\t\tbyteArray.write_uint16(#(tb.", NameStr/binary, "))\n",
"\t\tfor k, v in pairs(tb.", NameStr/binary, ") do\n",
"\t\t\tbyteArray = v.encode(byteArray)\n"
"\t\tend\n">>
end;
_ ->
<<"\t\tif tb.", NameStr/binary, " and next(tb.", NameStr/binary, ") then\n\t\t\tbyteArray.write_uint8(1)\n\t\t\ttb.", NameStr/binary, ".encode(byteArray)\n\t\telse\n\t\t\tbyteArray.write_uint8(0)\n\t\tend\n">>
end
end.
%% decode中的数组
make_protocal_encode_item({Type, Name}, TypeList) ->
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".
builtDecodeStr({TypeStr, NameStr}) ->
case lists:keyfind(TypeStr, 1, ?TypeValue) of
{TypeStr, LuaTypeStr, _DefValue} ->
<<"\t\ttb.", NameStr/binary, " = byteArray.read_", LuaTypeStr/binary, "()\n">>;
_ ->
case TypeStr of
<<"list[", LeftStr/binary>> ->
[SubTypeStr | _] = re:split(LeftStr, <<"\\]">>, [{return, binary}]),
case lists:keyfind(SubTypeStr, 1, ?TypeValue) of
{SubTypeStr, LuaTypeStr, _DefValue} ->
CntName = <<"cntOf", NameStr/binary>>,
ReadCnt = <<"\t\tlocal ", CntName/binary, " = byteArray.read_uint16()\n\t\ttb.", NameStr/binary, " = {}\n">>,
<<ReadCnt/binary, "\t\tfor i = 1, ", CntName/binary, " do\n\t\t\ttable.insert(tb.", NameStr/binary, ", byteArray.read_", LuaTypeStr/binary, "())\n\t\tend\n">>;
_ ->
CntName = <<"cntOf", NameStr/binary>>,
ReadCnt = <<"\t\tlocal ", CntName/binary, " = byteArray.read_uint16()\n\t\ttb.", NameStr/binary, " = {}\n">>,
<<ReadCnt/binary, "\t\tfor i = 1, ", CntName/binary, " do\n\t\t\tlocal temp = ", SubTypeStr/binary, "()\n\t\t\ttemp.decode(byteArray)\n\t\t\ttable.insert(tb.", NameStr/binary, ", temp)\n\t\tend\n">>
end;
_ ->
NotNilName = <<"isNil", NameStr/binary>>,
NotNil = <<"\t\tlocal ", NotNilName/binary, " = byteArray.read_uint8()\n">>,
ReadTable = <<"\t\t\ttb.", NameStr/binary, " = ", TypeStr/binary, "()\n\t\t\ttb.", NameStr/binary, ".decode(byteArray)\n">>,
<<NotNil/binary, "\t\tif ", NotNilName/binary, " > 0 then\n", ReadTable/binary, "\t\telse\n\t\t\ttb.", NameStr/binary, " = {}\n\t\tend\n">>
end
end.

+ 16
- 1
src/writeLua/gLuaGen.erl Переглянути файл

@ -5,7 +5,7 @@
]).
spellFunHead(MsgName, MsgId) ->
<<"function", MsgName/binary, "()\n\tlocal tb = {}\n\ttb.msgId =", (integer_to_binary(MsgId))/binary, "\n">>.
<<"function ", MsgName/binary, "()\n\tlocal tb = {}\n\ttb.msgId =", (integer_to_binary(MsgId))/binary, "\n">>.
spellMember(FieldList) ->
<<<<(gLuaField:builtMemberStr(OneTypeName))/binary>> || OneTypeName <- FieldList>>.
@ -16,6 +16,21 @@ spellEncode(FieldList) ->
EnEnd = <<"\t\treturn byteArray\n\tend\n\n">>,
<<EnHead/binary, EnBody/binary, EnEnd/binary>>.
spellDecode(FieldList) ->
EnHead = <<"\ttb.decode = function(byteArray)\n">>,
EnBody = <<<<(gLuaField:builtDecodeStr(OneTypeName))/binary>> || OneTypeName <- FieldList>>,
EnEnd = <<"\tend\n\n">>,
<<EnHead/binary, EnBody/binary, EnEnd/binary>>.
spellBuild(MsgId) ->
<<"\ttb.build = function(byteArray)\n\t\tbyteArray.write_uint16(", (integer_to_binary(MsgId))/binary, ")\n\t\treturn tb.encode(byteArray)\n\tend\n\n">>.
spellEnd() ->
<<"\treturn tb\nend\n\n">>.
spellMsgName(MsgId, MsgName) ->
<<"\t[", (integer_to_binary(MsgId))/binary, "] = \"", MsgName/binary, "\",\n">>.
genLua(SortedSProtoList, _SortedErrList, LuaDir, _) ->
FunSpell =
fun({MsgName, MsgId, FieldList}, {TableBinAcc, MsgNameBinAcc}) ->

+ 0
- 367
src/writeLua/gLuaGen1.erl Переглянути файл

@ -1,367 +0,0 @@
-module(gLuaGen1).
-export([
genLua/4
]).
-export([start/0]).
-export([test/0]).
%%
%% API Functions
%%
start() ->
StructList = protocol_def:get_struct_def(),
TypeList = gen_common:get_type(),
FileStr = make_protocal(StructList, "", TypeList),
file:write_file("NetPacket.lua", FileStr),
MsgTypeStr = make_protocal_msg_type(StructList, "", 1),
file:write_file("protoName.lua", MsgTypeStr).
make_protocal([], StructStr, _TypeList) ->
StructStr;
make_protocal([Struct | Structs], StructStr, TypeList) ->
[StructName | NewStructList] = Struct,
VariableStr = make_protocal_Variable(NewStructList, "", TypeList),
EncodeStr = make_protocal_encode(NewStructList, "", TypeList),
DecodeStr = make_protocal_decode(NewStructList, "", TypeList),
BuildStr = make_protocal_build(StructName),
CreateStr = make_protocal_create(StructName),
StructStr1 = StructStr ++ "\nfunction " ++ atom_to_list(StructName) ++ " ()" ++
"\n" ++
" local tb = {}\n" ++
make_getmsgid(StructName) ++
%% " public int getMsgID()\n" ++
%% " {\n" ++
%% " return NetMsgType.msg_" ++ atom_to_list(StructName) ++ ";\n" ++
%% " }\n" ++
VariableStr ++ EncodeStr ++ DecodeStr ++ BuildStr ++ CreateStr ++ "\nend\n",
make_protocal(Structs, StructStr1, TypeList).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
make_protocal_Variable([], VariableStr, _TypeList) ->
VariableStr;
make_protocal_Variable([{Type, Name} | Vars], VariableStr, TypeList) ->
VariableStr1 =
case lists:keyfind(Type, 1, TypeList) of
{_Type1, base_type} ->
VariableStr ++ " tb." ++ get_csharp_Var(Name) ++ " = 0\n";
{_Type1, string_type} ->
VariableStr ++ " tb." ++ get_csharp_Var(Name) ++ " = \"\"\n";
{_Type, enum_type} ->
VariableStr ++ " tb." ++ get_csharp_Var(Name) ++ " = 0\n";
_ ->
VariableStr ++ " tb." ++ get_csharp_Var(Name) ++ " = {}\n"
end,
make_protocal_Variable(Vars, VariableStr1, TypeList);
make_protocal_Variable([{Type, Name, DefaultValue} | Vars], VariableStr, TypeList)
when Type =/= array ->
VariableStr1 =
case lists:keyfind(Type, 1, TypeList) of
{_Type1, base_type} ->
VariableStr ++ " tb." ++ get_csharp_Var(Name) ++ " = " ++ get_default_value(Type, DefaultValue) ++ "\n";
{_Type1, string_type} ->
VariableStr ++ " tb." ++ get_csharp_Var(Name) ++ " = " ++ get_default_value(Type, DefaultValue) ++ ";\n";
{_Type, enum_type} ->
VariableStr ++ " tb." ++ get_csharp_Var(Name) ++ " = 0\n";
_ ->
VariableStr ++ " tb." ++ get_csharp_Var(Name) ++ " = {}\n"
end,
make_protocal_Variable(Vars, VariableStr1, TypeList);
make_protocal_Variable([{array, Type, Name} | Vars], VariableStr, TypeList) ->
VariableStr1 =
case lists:keyfind(Type, 1, TypeList) of
{_Type1, base_type} ->
VariableStr ++ " tb." ++ get_csharp_Var(Name) ++ " = {}\n";
{_Type1, string_type} ->
VariableStr ++ " tb." ++ get_csharp_Var(Name) ++ " = {}\n";
{_Type, enum_type} ->
VariableStr ++ " tb." ++ atom_to_list(Name) ++ " = {}\n";
_ ->
VariableStr ++ " tb." ++ get_csharp_Var(Name) ++ " = {}\n"
end,
make_protocal_Variable(Vars, VariableStr1, TypeList).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
make_protocal_build(StructName) ->
"\n tb.build = function(byteArray)\n" ++
" byteArray.write_uint16(NetMsgType[\"msg_" ++ atom_to_list(StructName) ++ "\"])\n" ++
" return tb.encode(byteArray)\n" ++
" end\n".
make_protocal_create(_StructName)->
"\n return tb\n".
%%%%%%%%%%%%%%%%%%%%%%%%%%%%encode%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% byteArray.write_uint16(account.size());
%% for(int i=0; i<account.size();i++)
%% {
%% byteArray.write_string(account[i]);
%% }
make_protocal_encode([], EncodeStr, _TypeList) ->
"\n tb.encode = function(byteArray)\n"
++ EncodeStr ++ " return byteArray\n"
++ " end\n\n";
make_protocal_encode([Struct | Vars], EncodeStr, TypeList) ->
EncodeStr1 = EncodeStr ++ make_protocal_encode_item(Struct, TypeList),
make_protocal_encode(Vars, EncodeStr1, TypeList).
%% decode中的数组
make_protocal_encode_item({Type, Name}, TypeList) ->
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".
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%decode%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
make_protocal_decode([], DecodeStr, _TypeList) ->
" tb.decode = function(byteArray)\n" ++ DecodeStr ++ " end\n";
make_protocal_decode([Struct | Vars], DecodeStr, TypeList) ->
DecodeStr1 = DecodeStr ++ make_protocal_decode_item(Struct, TypeList),
make_protocal_decode(Vars, DecodeStr1, TypeList).
make_protocal_decode_item({Type, Name}, TypeList) ->
case lists:keyfind(Type, 1, TypeList) of
{Type1, base_type} ->
" tb." ++ get_csharp_Var(Name) ++ " = byteArray.read_"++ atom_to_list(Type1) ++"();\n";
{Type1, string_type} ->
" tb." ++ get_csharp_Var(Name) ++ " = byteArray.read_"++ atom_to_list(Type1) ++"();\n";
{_Type, enum_type} ->
make_protocal_decode_item({int, Name}, TypeList);
_ ->
TypeString = get_csharp_type(Type),
" tb." ++ get_csharp_Var(Name) ++ " = " ++ TypeString ++ "();\n" ++
" tb." ++ get_csharp_Var(Name) ++ ".decode(byteArray);\n"
%%" " ++ get_csharp_Var(Name) ++ ".decode(byteArray);\n"
end;
make_protocal_decode_item({Type, Name, _DefaultValue}, TypeList)
when Type /= array->
make_protocal_decode_item({Type, Name}, TypeList);
make_protocal_decode_item({array, Type, Name}, TypeList) ->
case lists:keyfind(Type, 1, TypeList) of
{Type1, base_type} ->
make_protocal_encode_item_decode_type(Name, Type1);
{Type1, string_type} ->
make_protocal_encode_item_decode_type(Name, Type1);
{_Type, enum_type} ->
make_protocal_decode_item({array, int, Name}, TypeList);
_ ->
TypeString = get_csharp_type(Type),
NameStr = get_csharp_Var(Name),
CountString = "countOf" ++ NameStr,
_ArrayString = "arrayOf" ++ NameStr,
" local " ++ CountString ++ " = byteArray.read_uint16()\n" ++
" tb."++NameStr++" = {}\n" ++
" for i = 1, " ++ CountString ++ " do\n" ++
" local temp = " ++ TypeString ++ "()\n" ++
" temp.decode(byteArray)\n" ++
" table.insert(tb."++NameStr++", temp)\n" ++
" end\n"
end.
make_protocal_encode_item_decode_type(Name, Type) ->
NameStr = get_csharp_Var(Name),
TypeStr = atom_to_list(Type),
CountString = "countOf" ++ NameStr,
" local " ++ CountString ++ " = byteArray.read_uint16()\n" ++
" tb."++NameStr++" = {}\n" ++
" for i = 1, " ++ CountString ++ " do\n" ++
" table.insert(tb." ++ NameStr ++ ", byteArray.read_" ++ TypeStr ++ "())\n" ++
" end\n".
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
make_protocal_msg_type([], MsgTypeStr, _Index) ->
"NetMsgType = \n" ++
"{\n" ++
MsgTypeStr ++
"}";
make_protocal_msg_type([Struct|Structs], MsgTypeStr, Index) ->
[StructName | _NewStructList] = Struct,
case Structs == [] of
true ->
make_protocal_msg_type(Structs, MsgTypeStr ++ " [\"msg_" ++ atom_to_list(StructName) ++ "\"] = "++integer_to_list(Index)++"\n", Index+1);
false ->
make_protocal_msg_type(Structs, MsgTypeStr ++ " [\"msg_" ++ atom_to_list(StructName) ++ "\"] = "++integer_to_list(Index)++",\n", Index+1)
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
make_protocal_const(Ver) ->
"function get_proto_version()
return "++integer_to_list(Ver)++";
end\n".
make_protocal_enum([], EnumStr) ->
EnumStr;
make_protocal_enum([Enum | Enums], EnumStr) ->
{EnumName , EnumItems} = Enum,
EnumStr1 = EnumStr ++
string:to_lower(atom_to_list(EnumName)) ++ " = \n{\n" ++
make_protocal_enum_item(EnumItems, 1, "") ++ "}\n",
make_protocal_enum(Enums, EnumStr1).
make_protocal_enum_item([EnumItem | []], Index, EnumItemStr) ->
EnumItemStr ++ " [\"" ++ atom_to_list(EnumItem) ++ "\"] = "++integer_to_list(Index)++"\n";
make_protocal_enum_item([EnumItem | EnumItems], Index, EnumItemStr) ->
EnumItemStr1 = EnumItemStr ++ " [\"" ++ atom_to_list(EnumItem) ++ "\"]= "++integer_to_list(Index)++",\n",
make_protocal_enum_item(EnumItems, Index + 1, EnumItemStr1).
get_csharp_Var(Type)->
TypeString = atom_to_list(Type),
case lists:keyfind(TypeString, 1, get_keyword_mapping()) of
{TypeString, CSharpTypeString}->
CSharpTypeString;
_ ->
TypeString
end.
get_csharp_type(Type) ->
TypeString = atom_to_list(Type),
case lists:keyfind(TypeString, 1, get_cshapr_type_mapping()) of
{TypeString, CSharpTypeString}->
CSharpTypeString;
_ ->
TypeString
end.
get_cshapr_type_mapping()->
[
{"uint", "UInt"},
{"int16", "Int16"},
{"uint16", "UInt16"},
{"int64", "Int64"},
{"uint64", "uint64"},
{"uchar", "char"}
].
get_keyword_mapping() ->
[
{"lock", "Lock"},
{"params", "Params"}
].
make_getmsgid(StructName)->
" tb.getMsgID = function()\n" ++
" " ++
" return NetMsgType[\"msg_" ++ atom_to_list(StructName) ++ "\"]\n" ++
" end\n".
get_default_value(Type, DefaultValue)
when (Type == int orelse
Type == uint orelse
Type == uint64 orelse
Type == uint16 orelse
Type == int64 orelse
Type == short orelse
Type == int16)
andalso is_integer(DefaultValue)->
integer_to_list(DefaultValue);
get_default_value(Type, DefaultValue)
when (Type == float orelse
Type == long orelse
Type == double orelse
Type == uint16 orelse
Type == int64 orelse
Type == int16)
andalso is_float(DefaultValue)->
float_to_list(DefaultValue);
get_default_value(char, DefaultValue)
when is_list(DefaultValue)->
"'" ++ DefaultValue ++ "'";
get_default_value(string, DefaultValue)
when is_atom(DefaultValue)->
get_default_value(string, atom_to_list(DefaultValue));
get_default_value(string, DefaultValue)
when is_list(DefaultValue)->
"\"" ++ DefaultValue ++ "\"".
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
test() ->
%% test_make_protocal_encode_item(),
%% test_make_protocal_decode_item(),
%% test_make_protocal_encode(),
%% test_make_protocal_decode().
start().
%% test_make_protocal_encode_item() ->
%% "\t\tbyteArray.write_uint16(result.size());\n\t\tfor(int i=0; i<result.size();i++)\n\t{\n\t\t\tresult[i].encode(byteArray);\n\t}\n"
%% = make_protocal_encode_item({array, notify_login_result, result}, gen_common:get_type()),
%% "\t\tbyteArray.write_uint16(account.size());\n\t\tfor(int i=0; i<account.size();i++)\n\t{\n\t\t\tbyteArray.write_string(account[i]);\n\t}\n"
%% = make_protocal_encode_item({array, string, account}, gen_common:get_type()),
%% "\t\tbyteArray.write_uint16(account.size());\n\t\tfor(int i=0; i<account.size();i++)\n\t{\n\t\t\tbyteArray.write_int(account[i]);\n\t}\n"
%% = make_protocal_encode_item({array, login_result, account}, gen_common:get_type()),
%% "\t\tbyteArray.write_string(account);\n" = make_protocal_encode_item({string, account}, gen_common:get_type()),
%% "\t\tbyteArray.write_int(account);\n" = make_protocal_encode_item({login_result, account}, gen_common:get_type()),
%% "\t\taccount.encode(byteArray);\n" = make_protocal_encode_item({notify_login_result, account}, gen_common:get_type()).
%%
%% test_make_protocal_decode_item() ->
%% "uint16 size = byteArray.read_uint16();\naccount.reserve(size);\nfor(int i=0; i<size;i++)\n{\n\taccount.push_back(byteArray.read_string());\n}\n"
%% = make_protocal_decode_item({array, string, account}, gen_common:get_type()),
%% "uint16 size = byteArray.read_uint16();\nresult.resize(size);\nfor(int i=0; i<size;i++)\n{\n\tresult[i].decode(byteArray);\n}\n"
%% = make_protocal_decode_item({array, notify_login_result, result}, gen_common:get_type()),
%% "uint16 size = byteArray.read_uint16();\naccount.reserve(size);\nfor(int i=0; i<size;i++)\n{\n\taccount.push_back(byteArray.read_int());\n}\n"
%% = make_protocal_decode_item({array, login_result, account}, gen_common:get_type()),
%% "account = byteArray.read_string();\n" = make_protocal_decode_item({string, account}, gen_common:get_type()),
%% "account = byteArray.read_int();\n" = make_protocal_decode_item({login_result, account}, gen_common:get_type()),
%% "account.decode(byteArray);\n" = make_protocal_decode_item({notify_login_result, account}, gen_common:get_type()).
%%
%% test_make_protocal_encode() ->
%% Result = make_protocal_encode([{array, string, account},
%% {string, pwd},
%% {player_data, data},
%% {array, notify_login_result, result}], "", gen_common:get_type()),
%% io:format("~p~n", [Result]).
%%
%% test_make_protocal_decode() ->
%% Result = make_protocal_decode([{array, string, account},
%% {string, pwd},
%% {player_data, data},
%% {array, notify_login_result, result}], "", gen_common:get_type()),
%% io:format("~p~n", [Result]).
%%
%% test_make_protocal_Variable() ->
%% Result = make_protocal_Variable([{array, string, account},
%% {string, pwd},
%% {player_data, data},
%% {array, notify_login_result, result}], "", gen_common:get_type()),
%% io:format("~p~n", [Result]).

+ 1853
- 0
test/protoMsg.lua
Різницю між файлами не показано, бо вона завелика
Переглянути файл


+ 40
- 0
test/protoName.lua Переглянути файл

@ -0,0 +1,40 @@
ProtoMsgName =
{
[1] = "test",
[2] = "phoneNumber",
[3] = "person",
[4] = "addressBook",
[5] = "union",
[6] = "tbool",
[7] = "tint8",
[8] = "tuint8",
[9] = "tint16",
[10] = "tuint16",
[11] = "tint32",
[12] = "tuint32",
[13] = "tint64",
[14] = "tuint64",
[15] = "tinteger",
[16] = "tnumber",
[17] = "tfloat",
[18] = "tdouble",
[19] = "tstring",
[20] = "tlistbool",
[21] = "tlistint8",
[22] = "tlistuint8",
[23] = "tlistint16",
[24] = "tlistuint16",
[25] = "tlistint32",
[26] = "tlistuint32",
[27] = "tlistint64",
[28] = "tlistuint64",
[29] = "tlistinteger",
[30] = "tlistnumber",
[31] = "tlistfloat",
[32] = "tlistdouble",
[33] = "tliststring",
[34] = "tlistunion",
[35] = "allType",
[36] = "testnull",
[1001] = "person1",
}

Завантаження…
Відмінити
Зберегти