This commit is contained in:
aozhiwei 2023-08-11 14:31:08 +08:00
parent 1452f39030
commit 14346982c8

View File

@ -1,9 +1,6 @@
const protobuf = require('protobufjs');
const parseArgs = require('minimist');
function prettyJsonEncode(obj) {
return JSON.stringify(obj, "", " ");
}
const fs = require('fs');
class PBTools {
@ -60,8 +57,109 @@ class PBTools {
}
);
}
await this.genCsAutoGen();
}
async genCsAutoGen() {
let data = `package cs
import (
"f5"
proto "github.com/golang/protobuf/proto"
)
type MsgHandler interface {
CMPing(*f5.MsgHdr, *CMPing)
CMLogin(*f5.MsgHdr, *CMLogin)
CMReconnect(*f5.MsgHdr, *CMReconnect)
}
type NetMsgHandler struct {
MsgId int
HandlerId int
parseCb func([]byte) interface{}
cb func(*f5.MsgHdr, MsgHandler)
}
var handlers [2000]*NetMsgHandler
func GetNetMsgHandler(msgId uint16) *NetMsgHandler {
handler := handlers[msgId]
return handler
}
func DispatchMsg(handler *NetMsgHandler, hdr *f5.MsgHdr, msgHandler MsgHandler) {
handler.cb(hdr, msgHandler)
}
func RegHandlerId(msgId int, handlerId int) {
handler := handlers[msgId]
handler.HandlerId = handlerId
}
func ParsePb(msgId uint16, data []byte) interface{} {
handler := handlers[msgId]
if handler == nil {
return nil
}
return handler.parseCb(data)
}
`;
this.csProtoPb.nested.cs.nestedArray.forEach(
(item) => {
if (item.name[0] == 'C' &&
item.name[1] == 'M') {
data += `
func (this *MsgHandlerImpl) ${item.name}(hdr *f5.MsgHdr, msg *${item.name}) {
}
`;
}
});
this.csProtoPb.nested.cs.nestedArray.forEach(
(item) => {
if (item.name[0] == 'C' &&
item.name[1] == 'M') {
data += `
func (this *${item.name}) GetNetMsgId() uint16 {
return uint16(CMMessageIdE__${item.name})
}
`;
} else if (item.name[0] == 'S' &&
item.name[1] == 'M') {
data += `
func (this *${item.name}) GetNetMsgId() uint16 {
return uint16(SMMessageIdE__${item.name})
}
`;
}
});
data += `
func init() {
`;
this.csProtoPb.nested.cs.nestedArray.forEach(
(item) => {
if (item.name[0] == 'C' &&
item.name[1] == 'M') {
data += `
handlers[int(CMMessageIdE__${item.name})] = &NetMsgHandler{
MsgId: int(CMMessageIdE__${item.name}),
parseCb: func (data []byte) interface{} {
msg := ${item.name}{}
proto.Unmarshal(data, msg)
return msg
},
cb: func (hdr *f5.MsgHdr, handler MsgHandler) {
handler.${item.name}(hdr, hdr.Msg.(*CMPing))
},
}
`;
}
});
data += `
}`;
fs.writeFileSync('./cs/cs.auto_gen.go', data);
}
}
(new PBTools).init();