diff --git a/README.md b/README.md index 336b35c..0c930a7 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ pro2d 服务器客户端共享的protobuffer 生成golang文件 ``` $ chmod +x generate.sh && ./generate.sh +$ python3 generator.py ``` ### C# diff --git a/generator.py b/generator.py new file mode 100644 index 0000000..590e048 --- /dev/null +++ b/generator.py @@ -0,0 +1,117 @@ +import os + +ProtoCodeGO = './cmd/gameserver/action/protocode.go' +ProtoCodeGOTest = './cmd/test/action/protocode.go' +ProtoCodeFile = './protos/protocode.proto' +ProtoFileDir = "./protos" + +ProtoCodeStr = "syntax = \"proto3\";\noption go_package = \"../pb;pb\";\n\npackage protocode;\n\nenum ProtoCode\n{{\n " \ + "UNKNOWN = 0;\n {}\n}} " +ProtoCodeLineReq = "\t{}Rpc = {};\n" +ProtoCodeLineRsp = "\t{}Rsp = {};\n" +ProtoCodeLineNty = "\t{}Nty = {};\n" + +GoProtoCodeStr = "package action\n\nimport (\n\t\"pro2d/common/logger\"\n\t\"pro2d/pb\"\n)\n\nfunc GetActionMap() " \ + "map[interface{{}}]interface{{}} {{\n\tlogger.Debug(\"init protocode...\")\n\tam := make(map[interface{{" \ + "}}]interface{{}})\n{}\n\treturn am\n}}" +GoProtoCodeLine = "\tam[uint32(pb.ProtoCode_{}Rpc)] = {}Rpc\n" + +GoProtoCodeTestStr = "package action\n\nimport (\n\t\"pro2d/pb\"\n)\n\nfunc GetTestActionMap() " \ + "map[interface{{}}]interface{{}} {{\n\tam := make(map[interface{{" \ + "}}]interface{{}})\n{}\n\treturn am\n}}" +GoProtoCodeTestReqLine = "\tam[uint32(pb.ProtoCode_{}Rpc)] = {}Rsp\n" +GoProtoCodeTestRspLine = "\tam[uint32(pb.ProtoCode_{}Rsp)] = {}Rsp\n" +GoProtoCodeTestNtyLine = "\tam[uint32(pb.ProtoCode_{}Nty)] = {}Nty\n" + +def generatorProto(path): + RpcStartCode = 500 + NtyStartCode = 1000 + + files = os.listdir(path) + ProtoCodeData = "" + GoCodeData = "" + GoCodeTestData = "" + for file in files: + if file.find("account.proto") != -1: + continue + + if os.path.isdir(file): + continue + + with open(path + "/" + file, 'r', encoding='utf-8', errors='ignore') as f: + lines = f.readlines() # 读取所有行 + firstline = lines[0] + if firstline.find("proto3") == -1: + continue + + # req + rar + for line in lines: + if line.find("message") == -1: + continue + sline = line.split(' ') + if len(sline) < 2: + continue + + messageStr = sline[1].replace('\n', '').replace('{', "") + n1 = messageStr.find('Req') + n2 = messageStr.find('Rar') + loginReq = messageStr.find('LoginReq') + + if n1 != -1: + RpcStartCode = RpcStartCode + 1 + ProtoCodeData += ProtoCodeLineReq.format(messageStr[:n1], RpcStartCode) + if loginReq != -1: + continue + GoCodeData += GoProtoCodeLine.format(messageStr[:n1], messageStr[:n1]) + GoCodeTestData += GoProtoCodeTestReqLine.format(messageStr[:n1], messageStr[:n1]) + elif n2 != -1: + RpcStartCode = RpcStartCode + 1 + ProtoCodeData += ProtoCodeLineReq.format(messageStr[:n2], RpcStartCode) + if loginReq != -1: + continue + GoCodeData += GoProtoCodeLine.format(messageStr[:n2], messageStr[:n2]) + GoCodeTestData += GoProtoCodeTestReqLine.format(messageStr[:n2], messageStr[:n2]) + ProtoCodeData += '\n' + # nty + for line in lines: + if line.find("message") == -1: + continue + sline = line.split(' ') + if len(sline) < 2: + continue + + messageStr = sline[1].replace('\n', '').replace('{', "") + n3 = messageStr.find('Nty') + loginReq = messageStr.find('LoginReq') + + if n3 != -1: + NtyStartCode = NtyStartCode + 1 + ProtoCodeData += ProtoCodeLineNty.format(messageStr[:n3], NtyStartCode) + if loginReq != -1: + continue + GoCodeTestData += GoProtoCodeTestNtyLine.format(messageStr[:n3], messageStr[:n3]) + + + + + # protocode.go + gostr = GoProtoCodeStr.format(GoCodeData) + fo = open(ProtoCodeGO, "w") + fo.write(gostr) + fo.close() + + # protocode.go + gostr = GoProtoCodeTestStr.format(GoCodeTestData) + fo = open(ProtoCodeGOTest, "w") + fo.write(gostr) + fo.close() + + #protocode.proto + protostr = ProtoCodeStr.format(ProtoCodeData) + fo = open(ProtoCodeFile, "w") + fo.write(protostr) + fo.close() + + +if __name__ == "__main__": + generatorProto(ProtoFileDir) -- libgit2 0.21.2