Commit d500811b2835195ce2f21bf47d1ff149d48cca66
1 parent
25687703
feat: 使用python3 自动生成proto文件
Showing
2 changed files
with
68 additions
and
0 deletions
 
Show diff stats
Makefile
| ... | ... | @@ -0,0 +1,67 @@ | 
| 1 | +import os | |
| 2 | + | |
| 3 | +ProtoCodeGO = './cmd/gameserver/action/protocode.go' | |
| 4 | +ProtoCodeFile = './protos/protocode.proto' | |
| 5 | +ProtoFileDir = "./protos" | |
| 6 | + | |
| 7 | +ProtoCodeStr = "syntax = \"proto3\";\noption go_package = \"../pb;pb\";\n\npackage protocode;\n\nenum ProtoCode\n{{\n " \ | |
| 8 | + "UNKNOWN = 0;\n {}\n}} " | |
| 9 | +ProtoCodeLineReq = "\t{}Req = {};\n" | |
| 10 | +ProtoCodeLineRsp = "\t{}Rsp = {};\n" | |
| 11 | + | |
| 12 | +GoProtoCodeStr = "package action\n\nimport (\n\t\"pro2d/common/logger\"\n\t\"pro2d/pb\"\n)\n\nfunc GetActionMap() " \ | |
| 13 | + "map[interface{{}}]interface{{}} {{\n\tlogger.Debug(\"init protocode...\")\n\tam := make(map[interface{{" \ | |
| 14 | + "}}]interface{{}})\n{}\n\treturn am\n}}" | |
| 15 | +GoProtoCodeLine = "\tam[uint32(pb.ProtoCode_{}Req)] = {}Rpc\n" | |
| 16 | + | |
| 17 | + | |
| 18 | +def generatorProto(path): | |
| 19 | + files = os.listdir(path) | |
| 20 | + code = 0 | |
| 21 | + ProtoCodeData = "" | |
| 22 | + GoCodeData = "" | |
| 23 | + for file in files: | |
| 24 | + if os.path.isdir(file): | |
| 25 | + continue | |
| 26 | + | |
| 27 | + with open(path + "/" + file, 'r', encoding='utf-8', errors='ignore') as f: | |
| 28 | + lines = f.readlines() # 读取所有行 | |
| 29 | + firstline = lines[0] | |
| 30 | + if firstline.find("proto3") == -1: | |
| 31 | + continue | |
| 32 | + | |
| 33 | + for line in lines: | |
| 34 | + if line.find("message") == -1: | |
| 35 | + continue | |
| 36 | + | |
| 37 | + sline = line.split(' ') | |
| 38 | + if len(sline) < 2: | |
| 39 | + continue | |
| 40 | + | |
| 41 | + messageStr = sline[1].replace('\n', '').replace('{', "") | |
| 42 | + n1 = messageStr.find('Req') | |
| 43 | + n2 = messageStr.find('Rsp') | |
| 44 | + | |
| 45 | + if n1 != -1: | |
| 46 | + code += 1 | |
| 47 | + ProtoCodeData += ProtoCodeLineReq.format(messageStr[:n1], code) | |
| 48 | + GoCodeData += GoProtoCodeLine.format(messageStr[:n1], messageStr[:n1]) | |
| 49 | + elif n2 != -1: | |
| 50 | + code += 1 | |
| 51 | + ProtoCodeData += ProtoCodeLineRsp.format(messageStr[:n2], code) | |
| 52 | + | |
| 53 | + # protocode.go | |
| 54 | + gostr = GoProtoCodeStr.format(GoCodeData) | |
| 55 | + fo = open(ProtoCodeGO, "w") | |
| 56 | + fo.write(gostr) | |
| 57 | + fo.close() | |
| 58 | + | |
| 59 | + #protocode.proto | |
| 60 | + protostr = ProtoCodeStr.format(ProtoCodeData) | |
| 61 | + fo = open(ProtoCodeFile, "w") | |
| 62 | + fo.write(protostr) | |
| 63 | + fo.close() | |
| 64 | + | |
| 65 | + | |
| 66 | +if __name__ == "__main__": | |
| 67 | + generatorProto(ProtoFileDir) | ... | ... |