Blame view

tools/generator.py 3.68 KB
d500811b   zhangqijia   feat: 使用python3 自...
1
2
3
  import os
  
  ProtoCodeGO = './cmd/gameserver/action/protocode.go'
2ea16684   zhangqijia   fix: update
4
  ProtoCodeGOTest = './cmd/test/action/protocode.go'
d500811b   zhangqijia   feat: 使用python3 自...
5
6
7
8
9
  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}} "
2e0d2609   zhangqijia   fix: 英雄升级返回值+prot...
10
  ProtoCodeLineReq = "\t{}Rpc = {};\n"
d500811b   zhangqijia   feat: 使用python3 自...
11
12
13
14
15
  ProtoCodeLineRsp = "\t{}Rsp = {};\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}}"
2e0d2609   zhangqijia   fix: 英雄升级返回值+prot...
16
  GoProtoCodeLine = "\tam[uint32(pb.ProtoCode_{}Rpc)] = {}Rpc\n"
d500811b   zhangqijia   feat: 使用python3 自...
17
  
2ea16684   zhangqijia   fix: update
18
19
20
  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}}"
2e0d2609   zhangqijia   fix: 英雄升级返回值+prot...
21
  GoProtoCodeTestReqLine = "\tam[uint32(pb.ProtoCode_{}Rpc)] = {}Rsp\n"
2ea16684   zhangqijia   fix: update
22
  GoProtoCodeTestRspLine = "\tam[uint32(pb.ProtoCode_{}Rsp)] = {}Rsp\n"
d500811b   zhangqijia   feat: 使用python3 自...
23
24
25
26
27
28
  
  def generatorProto(path):
      files = os.listdir(path)
      code = 0
      ProtoCodeData = ""
      GoCodeData = ""
2ea16684   zhangqijia   fix: update
29
      GoCodeTestData = ""
d500811b   zhangqijia   feat: 使用python3 自...
30
      for file in files:
a7566463   zhangqijia   fix: update proto...
31
32
33
          if file.find("account.proto") != -1:
              continue
  
d500811b   zhangqijia   feat: 使用python3 自...
34
35
36
37
38
39
40
41
42
43
44
45
          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
  
              for line in lines:
                  if line.find("message") == -1:
                      continue
d500811b   zhangqijia   feat: 使用python3 自...
46
47
48
49
50
51
                  sline = line.split(' ')
                  if len(sline) < 2:
                      continue
  
                  messageStr = sline[1].replace('\n', '').replace('{', "")
                  n1 = messageStr.find('Req')
3e92be67   zhangqijia   fix: 优化协议,除了登录和创建...
52
                  loginReq = messageStr.find('LoginReq')
d500811b   zhangqijia   feat: 使用python3 自...
53
54
55
56
  
                  if n1 != -1:
                      code += 1
                      ProtoCodeData += ProtoCodeLineReq.format(messageStr[:n1], code)
a7566463   zhangqijia   fix: update proto...
57
58
                      if loginReq != -1:
                          continue
d500811b   zhangqijia   feat: 使用python3 自...
59
                      GoCodeData += GoProtoCodeLine.format(messageStr[:n1], messageStr[:n1])
2ea16684   zhangqijia   fix: update
60
                      GoCodeTestData += GoProtoCodeTestReqLine.format(messageStr[:n1], messageStr[:n1])
d500811b   zhangqijia   feat: 使用python3 自...
61
  
2e0d2609   zhangqijia   fix: 英雄升级返回值+prot...
62
63
64
65
66
67
68
69
70
71
72
              ProtoCodeData += "\n"
              for line in lines:
                      if line.find("message") == -1:
                          continue
                      sline = line.split(' ')
                      if len(sline) < 2:
                          continue
  
                      messageStr = sline[1].replace('\n', '').replace('{', "")
                      n2 = messageStr.find('Rsp')
                      loginReq = messageStr.find('LoginReq')
2e0d2609   zhangqijia   fix: 英雄升级返回值+prot...
73
74
75
76
  
                      if n2 != -1:
                          code += 1
                          ProtoCodeData += ProtoCodeLineRsp.format(messageStr[:n2], code)
a7566463   zhangqijia   fix: update proto...
77
78
                          if loginReq != -1:
                              continue
2e0d2609   zhangqijia   fix: 英雄升级返回值+prot...
79
80
                          GoCodeTestData += GoProtoCodeTestRspLine.format(messageStr[:n2], messageStr[:n2])
  
2ea16684   zhangqijia   fix: update
81
  
d500811b   zhangqijia   feat: 使用python3 自...
82
83
84
85
86
87
      # protocode.go
      gostr = GoProtoCodeStr.format(GoCodeData)
      fo = open(ProtoCodeGO, "w")
      fo.write(gostr)
      fo.close()
  
2ea16684   zhangqijia   fix: update
88
89
90
91
92
93
      # protocode.go
      gostr = GoProtoCodeTestStr.format(GoCodeTestData)
      fo = open(ProtoCodeGOTest, "w")
      fo.write(gostr)
      fo.close()
  
d500811b   zhangqijia   feat: 使用python3 自...
94
95
96
97
98
99
100
101
102
      #protocode.proto
      protostr = ProtoCodeStr.format(ProtoCodeData)
      fo = open(ProtoCodeFile, "w")
      fo.write(protostr)
      fo.close()
  
  
  if __name__ == "__main__":
      generatorProto(ProtoFileDir)