优化协议代理代码生成

This commit is contained in:
flswld
2023-01-30 21:30:04 +08:00
parent c753dc53cd
commit 5a175722d7
9 changed files with 71 additions and 66 deletions

View File

@@ -1,9 +1,7 @@
package client_proto
import (
"os"
"strconv"
"strings"
"reflect"
"hk4e/pkg/logger"
)
@@ -11,31 +9,16 @@ import (
type ClientCmdProtoMap struct {
clientCmdIdCmdNameMap map[uint16]string
clientCmdNameCmdIdMap map[string]uint16
RefValue reflect.Value
}
func NewClientCmdProtoMap() (r *ClientCmdProtoMap) {
r = new(ClientCmdProtoMap)
r.clientCmdIdCmdNameMap = make(map[uint16]string)
r.clientCmdNameCmdIdMap = make(map[string]uint16)
clientCmdFile, err := os.ReadFile("./client_cmd.csv")
if err != nil {
panic(err)
}
clientCmdData := string(clientCmdFile)
lineList := strings.Split(clientCmdData, "\n")
for _, line := range lineList {
item := strings.Split(line, ",")
if len(item) != 2 {
panic("parse client cmd file error")
}
cmdName := item[0]
cmdId, err := strconv.Atoi(item[1])
if err != nil {
panic(err)
}
r.clientCmdIdCmdNameMap[uint16(cmdId)] = cmdName
r.clientCmdNameCmdIdMap[cmdName] = uint16(cmdId)
}
r.RefValue = reflect.ValueOf(r)
fn := r.RefValue.MethodByName("LoadClientCmdIdAndCmdName")
fn.Call([]reflect.Value{})
return r
}

View File

@@ -12,7 +12,7 @@ func TestClientProtoGen(t *testing.T) {
if err != nil {
panic(err)
}
nameList := make([]string, 0)
protoObjNameList := make([]string, 0)
for _, entry := range dir {
if entry.IsDir() {
continue
@@ -21,17 +21,41 @@ func TestClientProtoGen(t *testing.T) {
if len(split) < 2 || split[len(split)-1] != "proto" {
continue
}
nameList = append(nameList, split[len(split)-2])
protoObjNameList = append(protoObjNameList, split[len(split)-2])
}
// 生成初始化cmdId和cmdName的方法
clientCmdFile, err := os.ReadFile("./proto/client_cmd.csv")
if err != nil {
panic(err)
}
clientCmdData := string(clientCmdFile)
clientCmdLineList := strings.Split(clientCmdData, "\n")
// 生成代码文件
fileData := "package client_proto\n"
fileData += "\n"
fileData += "import (\n"
fileData += "\t\"hk4e/gate/client_proto/proto\"\n"
fileData += ")\n"
fileData += "\n"
fileData += "func (c *ClientCmdProtoMap) LoadClientCmdIdAndCmdName() {\n"
for _, clientCmdLine := range clientCmdLineList {
if clientCmdLine == "" {
continue
}
item := strings.Split(clientCmdLine, ",")
if len(item) != 2 {
panic("parse client cmd file error")
}
cmdName := item[0]
cmdId := item[1]
fileData += "\tc.clientCmdIdCmdNameMap[uint16(" + cmdId + ")] = \"" + cmdName + "\"\n"
fileData += "\tc.clientCmdNameCmdIdMap[\"" + cmdName + "\"] = uint16(" + cmdId + ")\n"
}
fileData += "}\n"
fileData += "\n"
fileData += "func (c *ClientCmdProtoMap) GetClientProtoObjByName(protoObjName string) any {\n"
fileData += "\tswitch protoObjName {\n"
for _, protoObjName := range nameList {
for _, protoObjName := range protoObjNameList {
fileData += "\tcase \"" + protoObjName + "\":\n\t\treturn new(proto." + protoObjName + ")\n"
}
fileData += "\tdefault:\n"
@@ -62,6 +86,7 @@ func TestClientProtoGen(t *testing.T) {
continue
}
enumName := split[1]
// 从protocol/proto_hk4e下复制同名的枚举类替换掉原proto文件里的内容
refEnum := FindEnumInDirFile("../../protocol/proto_hk4e", enumName)
if refEnum == nil {
continue