Blame view

tools/protostostruct.go 3 KB
eadc9aff   zhangqijia   feat: 增加上阵下阵协议,增加...
1
  //弃用 使用python3重写了。
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
2
3
4
5
6
7
8
9
10
11
12
13
14
  package main
  
  import (
  	"bufio"
  	"bytes"
  	"fmt"
  	"io"
  	"io/ioutil"
  	"os"
  	"path"
  )
  
  var (
69d286a7   zhangqijia   fix: 插件用来做部分热更
15
  	ProtoCode        = "syntax = \"proto3\";\noption go_package = \"../pb;pb\";\n\npackage protocode;\n\nenum ProtoCode\n{\n  UNKNOWN = 0;\n %s\n}"
98b0736d   zhangqijia   添加定时器, 检查心跳
16
17
  	ProtoCodeLineReq = "\t%sReq = %d;\n"
  	ProtoCodeLineRsp = "\t%sRsp = %d;\n"
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
18
  
69d286a7   zhangqijia   fix: 插件用来做部分热更
19
20
21
  	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%s\n\treturn am\n}"
  
  	GoProtoCodeLine = "\tam[uint32(pb.ProtoCode_%sReq)] = %sRpc\n"
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
22
23
24
  )
  
  func ProtoToCode(readPath, filename string) (string, string) {
69d286a7   zhangqijia   fix: 插件用来做部分热更
25
  	protofile, err := os.Open(readPath + filename)
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  	code := 0x000
  	protoData := ""
  	goProtoData := ""
  	if err != nil {
  		fmt.Errorf("ReadExcel|xlsx.OpenFile is err :%v", err)
  		return "", ""
  	}
  	defer protofile.Close()
  
  	r := bufio.NewReader(protofile)
  	for {
  		line, isPrefix, err := r.ReadLine()
  		if len(line) > 0 && err != nil {
  			fmt.Errorf("ReadLine returned both data and error: %s", err)
  			return "", ""
  
  		}
  		if isPrefix {
  			fmt.Errorf("ReadLine returned prefix")
  			return "", ""
  		}
  		if err != nil {
  			if err != io.EOF {
  				fmt.Errorf("Got unknown error: %s", err)
  				return "", ""
  			}
  			break
  		}
  		n := bytes.Index(line, []byte("message"))
  		if n < 0 {
  			continue
  		}
  		lb := bytes.Split(line, []byte(" "))
  		for _, v := range lb {
98b0736d   zhangqijia   添加定时器, 检查心跳
60
61
  			n1 := bytes.Index(v, []byte("Req"))
  			n2 := bytes.Index(v, []byte("Rsp"))
69d286a7   zhangqijia   fix: 插件用来做部分热更
62
  			if n1 < 0 && n2 < 0 {
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
63
64
  				continue
  			}
98b0736d   zhangqijia   添加定时器, 检查心跳
65
66
  			if n1 >= 0 {
  				code++
69d286a7   zhangqijia   fix: 插件用来做部分热更
67
  				protoData += fmt.Sprintf(ProtoCodeLineReq, v[:n1], code)
98b0736d   zhangqijia   添加定时器, 检查心跳
68
69
70
71
  				goProtoData += fmt.Sprintf(GoProtoCodeLine, v[:n1], v[:n1])
  			}
  			if n2 >= 0 {
  				code++
69d286a7   zhangqijia   fix: 插件用来做部分热更
72
  				protoData += fmt.Sprintf(ProtoCodeLineRsp, v[:n2], code)
98b0736d   zhangqijia   添加定时器, 检查心跳
73
  			}
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
74
75
76
  		}
  	}
  
69d286a7   zhangqijia   fix: 插件用来做部分热更
77
  	return protoData, goProtoData
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
78
79
  }
  
69d286a7   zhangqijia   fix: 插件用来做部分热更
80
  func ReadProtos(readPath, OutPath string) error {
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
81
82
83
84
85
  	files, err := ioutil.ReadDir(readPath)
  	if err != nil {
  		return fmt.Errorf("ReadExcel|ReadDir is err:%v", err)
  	}
  
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
86
87
  	protoData, goProtoData := "", ""
  	for _, file := range files {
69d286a7   zhangqijia   fix: 插件用来做部分热更
88
  		if path.Ext(file.Name()) != ".proto" {
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
89
90
91
92
  			continue
  		}
  		tprotoData, tgoProtoData := ProtoToCode(readPath, file.Name())
  		if tprotoData == "" || tgoProtoData == "" {
69d286a7   zhangqijia   fix: 插件用来做部分热更
93
  			continue
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
94
95
96
97
98
  		}
  		protoData += tprotoData
  		goProtoData += tgoProtoData
  
  	}
69d286a7   zhangqijia   fix: 插件用来做部分热更
99
  	fw, err := os.OpenFile(OutPath+"protos/protocode.proto", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
100
101
102
103
104
105
106
107
108
109
  	if err != nil {
  		return fmt.Errorf("WriteNewFile|OpenFile is err:%v", err)
  	}
  	defer fw.Close()
  
  	_, err = fw.Write([]byte(fmt.Sprintf(ProtoCode, protoData)))
  	if err != nil {
  		return fmt.Errorf("WriteNewFile|Write is err:%v", err)
  	}
  
69d286a7   zhangqijia   fix: 插件用来做部分热更
110
  	fw, err = os.OpenFile(OutPath+"cmd/gameserver/action/protocode.go", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
111
112
113
114
115
116
117
118
119
120
121
  	if err != nil {
  		return fmt.Errorf("WriteNewFile|OpenFile is err:%v", err)
  	}
  	defer fw.Close()
  
  	_, err = fw.Write([]byte(fmt.Sprintf(GoProtoCodeStr, goProtoData)))
  	if err != nil {
  		return fmt.Errorf("WriteNewFile|Write is err:%v", err)
  	}
  	return nil
  }