Commit ce072bf52e5ef2f267a0ba3f0c582161f21016fc
1 parent
ac362b38
add generate.py
Showing
2 changed files
with
118 additions
and
0 deletions
Show diff stats
README.md
... | ... | @@ -0,0 +1,117 @@ |
1 | +import os | |
2 | + | |
3 | +ProtoCodeGO = './cmd/gameserver/action/protocode.go' | |
4 | +ProtoCodeGOTest = './cmd/test/action/protocode.go' | |
5 | +ProtoCodeFile = './protos/protocode.proto' | |
6 | +ProtoFileDir = "./protos" | |
7 | + | |
8 | +ProtoCodeStr = "syntax = \"proto3\";\noption go_package = \"../pb;pb\";\n\npackage protocode;\n\nenum ProtoCode\n{{\n " \ | |
9 | + "UNKNOWN = 0;\n {}\n}} " | |
10 | +ProtoCodeLineReq = "\t{}Rpc = {};\n" | |
11 | +ProtoCodeLineRsp = "\t{}Rsp = {};\n" | |
12 | +ProtoCodeLineNty = "\t{}Nty = {};\n" | |
13 | + | |
14 | +GoProtoCodeStr = "package action\n\nimport (\n\t\"pro2d/common/logger\"\n\t\"pro2d/pb\"\n)\n\nfunc GetActionMap() " \ | |
15 | + "map[interface{{}}]interface{{}} {{\n\tlogger.Debug(\"init protocode...\")\n\tam := make(map[interface{{" \ | |
16 | + "}}]interface{{}})\n{}\n\treturn am\n}}" | |
17 | +GoProtoCodeLine = "\tam[uint32(pb.ProtoCode_{}Rpc)] = {}Rpc\n" | |
18 | + | |
19 | +GoProtoCodeTestStr = "package action\n\nimport (\n\t\"pro2d/pb\"\n)\n\nfunc GetTestActionMap() " \ | |
20 | + "map[interface{{}}]interface{{}} {{\n\tam := make(map[interface{{" \ | |
21 | + "}}]interface{{}})\n{}\n\treturn am\n}}" | |
22 | +GoProtoCodeTestReqLine = "\tam[uint32(pb.ProtoCode_{}Rpc)] = {}Rsp\n" | |
23 | +GoProtoCodeTestRspLine = "\tam[uint32(pb.ProtoCode_{}Rsp)] = {}Rsp\n" | |
24 | +GoProtoCodeTestNtyLine = "\tam[uint32(pb.ProtoCode_{}Nty)] = {}Nty\n" | |
25 | + | |
26 | +def generatorProto(path): | |
27 | + RpcStartCode = 500 | |
28 | + NtyStartCode = 1000 | |
29 | + | |
30 | + files = os.listdir(path) | |
31 | + ProtoCodeData = "" | |
32 | + GoCodeData = "" | |
33 | + GoCodeTestData = "" | |
34 | + for file in files: | |
35 | + if file.find("account.proto") != -1: | |
36 | + continue | |
37 | + | |
38 | + if os.path.isdir(file): | |
39 | + continue | |
40 | + | |
41 | + with open(path + "/" + file, 'r', encoding='utf-8', errors='ignore') as f: | |
42 | + lines = f.readlines() # 读取所有行 | |
43 | + firstline = lines[0] | |
44 | + if firstline.find("proto3") == -1: | |
45 | + continue | |
46 | + | |
47 | + # req + rar | |
48 | + for line in lines: | |
49 | + if line.find("message") == -1: | |
50 | + continue | |
51 | + sline = line.split(' ') | |
52 | + if len(sline) < 2: | |
53 | + continue | |
54 | + | |
55 | + messageStr = sline[1].replace('\n', '').replace('{', "") | |
56 | + n1 = messageStr.find('Req') | |
57 | + n2 = messageStr.find('Rar') | |
58 | + loginReq = messageStr.find('LoginReq') | |
59 | + | |
60 | + if n1 != -1: | |
61 | + RpcStartCode = RpcStartCode + 1 | |
62 | + ProtoCodeData += ProtoCodeLineReq.format(messageStr[:n1], RpcStartCode) | |
63 | + if loginReq != -1: | |
64 | + continue | |
65 | + GoCodeData += GoProtoCodeLine.format(messageStr[:n1], messageStr[:n1]) | |
66 | + GoCodeTestData += GoProtoCodeTestReqLine.format(messageStr[:n1], messageStr[:n1]) | |
67 | + elif n2 != -1: | |
68 | + RpcStartCode = RpcStartCode + 1 | |
69 | + ProtoCodeData += ProtoCodeLineReq.format(messageStr[:n2], RpcStartCode) | |
70 | + if loginReq != -1: | |
71 | + continue | |
72 | + GoCodeData += GoProtoCodeLine.format(messageStr[:n2], messageStr[:n2]) | |
73 | + GoCodeTestData += GoProtoCodeTestReqLine.format(messageStr[:n2], messageStr[:n2]) | |
74 | + ProtoCodeData += '\n' | |
75 | + # nty | |
76 | + for line in lines: | |
77 | + if line.find("message") == -1: | |
78 | + continue | |
79 | + sline = line.split(' ') | |
80 | + if len(sline) < 2: | |
81 | + continue | |
82 | + | |
83 | + messageStr = sline[1].replace('\n', '').replace('{', "") | |
84 | + n3 = messageStr.find('Nty') | |
85 | + loginReq = messageStr.find('LoginReq') | |
86 | + | |
87 | + if n3 != -1: | |
88 | + NtyStartCode = NtyStartCode + 1 | |
89 | + ProtoCodeData += ProtoCodeLineNty.format(messageStr[:n3], NtyStartCode) | |
90 | + if loginReq != -1: | |
91 | + continue | |
92 | + GoCodeTestData += GoProtoCodeTestNtyLine.format(messageStr[:n3], messageStr[:n3]) | |
93 | + | |
94 | + | |
95 | + | |
96 | + | |
97 | + # protocode.go | |
98 | + gostr = GoProtoCodeStr.format(GoCodeData) | |
99 | + fo = open(ProtoCodeGO, "w") | |
100 | + fo.write(gostr) | |
101 | + fo.close() | |
102 | + | |
103 | + # protocode.go | |
104 | + gostr = GoProtoCodeTestStr.format(GoCodeTestData) | |
105 | + fo = open(ProtoCodeGOTest, "w") | |
106 | + fo.write(gostr) | |
107 | + fo.close() | |
108 | + | |
109 | + #protocode.proto | |
110 | + protostr = ProtoCodeStr.format(ProtoCodeData) | |
111 | + fo = open(ProtoCodeFile, "w") | |
112 | + fo.write(protostr) | |
113 | + fo.close() | |
114 | + | |
115 | + | |
116 | +if __name__ == "__main__": | |
117 | + generatorProto(ProtoFileDir) | ... | ... |