aozhiwei c0b745e42c 1
2023-08-12 15:54:10 +08:00

363 lines
9.3 KiB
JavaScript

const protobuf = require('protobufjs');
const parseArgs = require('minimist');
const fs = require('fs');
const assert = require('assert');
class PBTools {
constructor() {
this.csMsgIdPb = null;
this.csProtoPb = null;
this.ssMsgIdPb = null;
this.ssProtoPb = null;
this.mtPb = null;
this.protoDir = './proto/';
}
async init() {
{
this.csProtoPb = await (new protobuf.Root()).load(
this.protoDir + 'cs_proto.proto',
{
'keepCase': true
}
);
}
{
this.csMsgIdPb = await (new protobuf.Root()).load(
this.protoDir + 'cs_msgid.proto',
{
'keepCase': true
}
);
this.csCmMsgId = this.csMsgIdPb.lookup('CMMessageId_e');
this.csSmMsgId = this.csMsgIdPb.lookup('SMMessageId_e');
}
{
this.ssProtoPb = await (new protobuf.Root()).load(
this.protoDir + 'ss_proto.proto',
{
'keepCase': true
}
);
}
{
this.ssMsgIdPb = await (new protobuf.Root()).load(
this.protoDir + 'ss_msgid.proto',
{
'keepCase': true
}
);
this.ssSSmMsgId = this.ssMsgIdPb.lookup('SSMMessageId_e');
}
{
this.mtPb = await (new protobuf.Root()).load(
this.protoDir + 'mt.proto',
{
'keepCase': true
}
);
}
await this.genCsAutoGen();
await this.genMtbAutoGen();
//await this.genMtAutoGen();
}
async genCsAutoGen() {
let data = `package cs
import (
"f5"
proto "github.com/golang/protobuf/proto"
)
type MsgHandlerImpl struct {
}
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)
}
`;
data += `
type MsgHandler interface {`;
this.csProtoPb.nested.cs.nestedArray.forEach(
(item) => {
if (item.name[0] == 'C' &&
item.name[1] == 'M') {
data += `
${item.name}(*f5.MsgHdr, *${item.name})`;
}
});
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.(*${item.name}))
},
}
`;
}
});
data += `
}`;
fs.writeFileSync('./cs/cs.auto_gen.go', data);
}
async genMtbAutoGen() {
let data = `package mtb
import (
"f5"
)
`;
this.mtPb.nested.mt.nestedArray.forEach(
(item) => {
data += `type ${item.name} struct {\n`;
item.fieldsArray.forEach
(
(item2, index) => {
assert(item2.id <= 127);
data += ` ${item2.name} ` + this.dumpClassField(item, item2, index) + `\n`;
}
);
data += `
_flags1_ uint64
_flags2_ uint64
}`;
}
);
data += '\n\n';
this.mtPb.nested.mt.nestedArray.forEach(
(item) => {
item.fieldsArray.forEach
(
(item2, index) => {
const newName = this.converUpperCamelCaseName(item2.name);
data += `func (this *${item.name}) Get${newName}() ` +
this.dumpClassField(item, item2, index) + ` {\n`;
data += ` return this.${item2.name}\n`;
data += `}\n\n`;
data += `func (this *${item.name}) Has${newName}() bool {\n`;
if (item2.id < 64) {
data += ` return (this._flags1_ & (uint64(1) << ${item2.id})) > 0\n`;
} else {
data += ` return (this._flags2_ & (uint64(1) << (${item2.id} - 64))) > 0\n`;
}
data += `}\n`;
}
);
}
);
this.mtPb.nested.mt.nestedArray.forEach(
(item) => {
data += `
func (this *${item.name}) LoadFromKv(kv map[string]interface{}) {
`;
item.fieldsArray.forEach
(
(item2, index) => {
if (item2.id < 64) {
data += ` f5.ReadMetaTableField(&this.${item2.name}, "${item2.name}", &this._flags1_, ${item2.id}, kv)\n`;
} else {
data += ` f5.ReadMetaTableField(&this.${item2.name}, "${item2.name}", &this._flags2_, ${item2.id} - 64, kv)\n`;
}
}
);
data += '}\n';
}
);
fs.writeFileSync('./mtb/mtb.auto_gen.go', data);
}
async genMtAutoGen() {
let data = `package mt
`;
this.mtPb.nested.mt.nestedArray.forEach(
(item) => {
data += `type ${item.name}RawTable struct {\n`;
data += ` rawList []*${item.name}\n`;
data += '}\n\n';
data += `type ${item.name}IdTable struct {\n`;
data += ` ${item.name}RawTable\n`;
data += ` idHash map[int64]*${item.name}\n`;
data += '}\n\n';
data += `type ${item.name}AutoIdTable struct {\n`;
data += ` ${item.name}RawTable\n`;
data += ` idHash map[int64]*${item.name}\n`;
data += '}\n\n';
data += `type ${item.name}NameTable struct {\n`;
data += ` ${item.name}RawTable\n`;
data += ` nameHash map[string]*${item.name}\n`;
data += '}\n\n';
}
);
this.mtPb.nested.mt.nestedArray.forEach(
(item) => {
data += `func (this *${item.name}RawTable) Traverse(cb func (*${item.name}) bool) {\n`;
data += ` for _, val := range this.rawList {\n`;
data += ` if !cb(val) {\n`;
data += ` break\n`;
data += ` }\n`;
data += ` }\n`;
data += '}\n\n';
data += `func (this *${item.name}IdTable) GetById(id int64) *${item.name} {\n`;
data += ` if v, ok := this.idHash[id]; ok {\n`;
data += ` return v\n`;
data += ` } else {\n`;
data += ` return nil\n`;
data += ` }\n`;
data += '}\n\n';
data += `func (this *${item.name}AutoIdTable) GetById(id int64) *${item.name} {\n`;
data += ` if v, ok := this.idHash[id]; ok {\n`;
data += ` return v\n`;
data += ` } else {\n`;
data += ` return nil\n`;
data += ` }\n`;
data += '}\n\n';
data += `func (this *${item.name}NameTable) GetByName(name string) *${item.name} {\n`;
data += ` if v, ok := this.nameHash[name]; ok {\n`;
data += ` return v\n`;
data += ` } else {\n`;
data += ` return nil\n`;
data += ` }\n`;
data += '}\n\n';
}
);
data += '\n\n';
fs.writeFileSync('./mt/mt.auto_gen.go', data);
}
dumpClassField(cls, field, index) {
const fieldName = field.name;
switch (field.type) {
case 'int32':
{
return `int32`;
}
break;
case 'int64':
{
return `int64`;
}
break;
case 'float':
{
return `float32`;
}
break;
case 'double':
{
return `float64`;
}
break;
case 'string':
{
return `string`;
}
break;
default:
console.log(field);
assert(false, 'error field type:' + field.type);
break;
}
}
converUpperCamelCaseName(name) {
let newName = '';
let preIs_ = false;
for (let i = 0; i < name.length; ++i) {
if (i == 0) {
newName += name[i].toUpperCase();
preIs_ = name[i] == '_';
} else {
if (preIs_) {
if (name[i] != '_') {
newName += name[i].toUpperCase();
}
} else {
if (name[i] != '_') {
newName += name[i];
}
}
preIs_ = name[i] == '_';
}
}
return newName;
}
}
(new PBTools).init();