|
|
@ -1,25 +1,20 @@ |
|
|
|
-module(protoGen). |
|
|
|
-module(gGenErl). |
|
|
|
-include("genDef.hrl"). |
|
|
|
|
|
|
|
-export([ |
|
|
|
convertFile/1 |
|
|
|
, convert/1 |
|
|
|
, convertDir/0 |
|
|
|
, convertDir/1 |
|
|
|
, convertDir/3 |
|
|
|
genErl/4 |
|
|
|
]). |
|
|
|
|
|
|
|
-define(MsgIdSegSize, 1000). |
|
|
|
|
|
|
|
protoHrlHeader() -> |
|
|
|
"-opaque int8() :: -128..127. |
|
|
|
-opaque int16() :: -32768..32767. |
|
|
|
-opaque int32() :: -2147483648..2147483647. |
|
|
|
-opaque int64() :: -9223372036854775808..9223372036854775807. |
|
|
|
-opaque uint8() :: 0..255. |
|
|
|
-opaque uint16() :: 0..65536. |
|
|
|
-opaque uint32() :: 0..4294967295. |
|
|
|
-opaque uint64() :: 0..18446744073709551615. |
|
|
|
-opaque double() :: float().\n\n". |
|
|
|
"-opaque int8() :: -128..127. |
|
|
|
-opaque int16() :: -32768..32767. |
|
|
|
-opaque int32() :: -2147483648..2147483647. |
|
|
|
-opaque int64() :: -9223372036854775808..9223372036854775807. |
|
|
|
-opaque uint8() :: 0..255. |
|
|
|
-opaque uint16() :: 0..65536. |
|
|
|
-opaque uint32() :: 0..4294967295. |
|
|
|
-opaque uint64() :: 0..18446744073709551615. |
|
|
|
-opaque double() :: float().\n\n". |
|
|
|
|
|
|
|
protoErlHeader() -> |
|
|
|
"-module(protoMsg).\n\n |
|
|
@ -189,7 +184,7 @@ genMsgHrl(FieldInfo, {Index, Len, AccList}) -> |
|
|
|
_ -> |
|
|
|
", " |
|
|
|
end, |
|
|
|
RecStr = TemStr ++ protoField:builtRecStr(FieldInfo) ++ (case Index == Len of true -> ""; _ -> "\t" end), |
|
|
|
RecStr = TemStr ++ gErlField:builtRecStr(FieldInfo) ++ (case Index == Len of true -> ""; _ -> "\t" end), |
|
|
|
{Index - 1, Len, [RecStr | AccList]}. |
|
|
|
|
|
|
|
genErrCodeHrl({ErrName, ErrCodeId, ComDesc}, AccList) -> |
|
|
@ -214,7 +209,7 @@ genEncodeRec({MsgName, MsgId, FieldList}, IsForBin) -> |
|
|
|
FunBody = |
|
|
|
fun({FieldType, _FieldName}, {Index, PStrAcc}) -> |
|
|
|
TemV = "V" ++ integer_to_list(Index), |
|
|
|
PackStr = protoField:builtPackStr(FieldType) ++ TemV ++ ")", |
|
|
|
PackStr = gErlField:builtPackStr(FieldType) ++ TemV ++ ")", |
|
|
|
case Index == 1 of |
|
|
|
true -> |
|
|
|
{Index - 1, PackStr ++ PStrAcc}; |
|
|
@ -650,50 +645,8 @@ genDecodeBin({MsgName, MsgId, FieldList}, SortedSProtoList, IsForBin) -> |
|
|
|
end, |
|
|
|
HeadStr ++ LBodyStr ++ RetStr. |
|
|
|
|
|
|
|
convertFile(File) -> |
|
|
|
protoParse:parseFile(File). |
|
|
|
|
|
|
|
convert([ProtoDir, HrlDir, ErlDir]) -> |
|
|
|
convertDir(atom_to_list(ProtoDir), atom_to_list(HrlDir), atom_to_list(ErlDir)). |
|
|
|
|
|
|
|
convertDir() -> |
|
|
|
convertDir("./", "./", "./"). |
|
|
|
convertDir(ProtoDir) -> |
|
|
|
convertDir(ProtoDir, "./", "./"). |
|
|
|
convertDir(ProtoDir, HrlDir, ErlDir) -> |
|
|
|
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, |
|
|
|
%% 下面文件帅选并不能准确的帅选出文件名为.mpdf结尾的文件 在FunRead函数中纠正处理一下 |
|
|
|
SProtoListOfList = filelib:fold_files(ProtoDir, "\\.mpdf$", true, FunRead, []), |
|
|
|
SProtoList = lists:append(SProtoListOfList), |
|
|
|
ErrCodeList = erlang:get(pd_errlist), |
|
|
|
genErl(SortedSProtoList, SortedErrList, HrlDir, ErlDir) -> |
|
|
|
initSubRec(), |
|
|
|
|
|
|
|
SortedSProtoList = lists:sort(fun({_Name1, MessageId1, _FieldList1}, {_Name2, MessageId2, _FieldList2}) -> |
|
|
|
MessageId1 > MessageId2 end, SProtoList), |
|
|
|
|
|
|
|
FunSpell = |
|
|
|
fun({MsgName, _MsgId, FieldList} = MsgInfo, {MsgHrlAcc, _MsgIdAcc, MsgEncodeAcc, MsgDecodeAcc}) -> |
|
|
|
%% gen hrl str |
|
|
@ -715,8 +668,6 @@ convertDir(ProtoDir, HrlDir, ErlDir) -> |
|
|
|
end, |
|
|
|
{MsgHrlStr, _MsgIdStr, MsgEncodeStr, MsgDecodeStr} = lists:foldl(FunSpell, {[], ["getMsgId(_) -> 0.\n\n"], ["encodeIol(_, _) ->\n\t[].\n\n"], ["decodeBin(_, _) ->\n\t{undefinedHer, undefined, {}}.\n\n"]}, SortedSProtoList), |
|
|
|
|
|
|
|
SortedErrList = lists:sort(fun({_ErrName1, ErrCodeId1, _Desc1}, {_ErrName2, ErrCodeId2, _Desc2}) -> |
|
|
|
ErrCodeId1 > ErrCodeId2 end, ErrCodeList), |
|
|
|
ErrCodeStr = lists:foldl(fun genErrCodeHrl/2, [], SortedErrList) ++ "\n\n", |
|
|
|
|
|
|
|
%% gen decodeRec str |