1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 11:07:40 +08:00

fix: gen rely on jce

This commit is contained in:
fumiama 2021-12-09 23:30:12 +08:00
parent f1d2259956
commit ed7199def8
2 changed files with 71 additions and 29 deletions

View File

@ -1,5 +1,16 @@
package main package main
import (
"bufio"
"flag"
"fmt"
"os"
"os/exec"
"strings"
)
const tmphead = `package main
import ( import (
"flag" "flag"
"fmt" "fmt"
@ -9,39 +20,22 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"github.com/Mrs4s/MiraiGo/binary/jce"
) )
const head = `// Code generated by structs_parser; DO NOT EDIT. type IJceStruct interface {}
package jce
const head = "// Code generated by structs_parser; DO NOT EDIT.\npackage jce\n"
` `
func main() { const tmpmain = `func main() {
f := flag.String("f", "structs_tobytes.go", "output file.") w, err := os.Create(%s)
flag.Parse()
fmt.Println("gen runs on arg", *f)
w, err := os.Create(*f)
if err != nil { if err != nil {
panic(err) panic(err)
} }
tmp.WriteString(head)
w.WriteString(head) w.WriteString(head)
WriteJceStruct(w, &jce.RequestPacket{}) `
WriteJceStruct(w, &jce.RequestDataVersion3{}) const tmptail = `
WriteJceStruct(w, &jce.RequestDataVersion2{})
WriteJceStruct(w, &jce.SvcReqRegister{})
WriteJceStruct(w, &jce.FriendListRequest{})
WriteJceStruct(w, &jce.SummaryCardReq{})
WriteJceStruct(w, &jce.SummaryCardReqSearch{})
WriteJceStruct(w, &jce.TroopListRequest{})
WriteJceStruct(w, &jce.TroopMemberListRequest{})
WriteJceStruct(w, &jce.SvcRespPushMsg{})
WriteJceStruct(w, &jce.ModifyGroupCardRequest{})
WriteJceStruct(w, &jce.SvcReqGetDevLoginInfo{})
WriteJceStruct(w, &jce.SvcReqRegisterNew{})
WriteJceStruct(w, &jce.DelFriendReq{})
err = w.Close() err = w.Close()
if err != nil { if err != nil {
panic(err) panic(err)
@ -71,7 +65,7 @@ func writeObject(w io.Writer, v reflect.Value, tag byte, name string) {
w.Write([]byte(fmt.Sprintf("\tw.WriteInt64Slice(pkt.%s, %d)\n", name, tag))) w.Write([]byte(fmt.Sprintf("\tw.WriteInt64Slice(pkt.%s, %d)\n", name, tag)))
case [][]byte: case [][]byte:
w.Write([]byte(fmt.Sprintf("\tw.WriteBytesSlice(pkt.%s, %d)\n", name, tag))) w.Write([]byte(fmt.Sprintf("\tw.WriteBytesSlice(pkt.%s, %d)\n", name, tag)))
case []jce.IJceStruct: case []IJceStruct:
w.Write([]byte(fmt.Sprintf("\tw.WriteJceStructSlice(pkt.%s, %d)\n", name, tag))) w.Write([]byte(fmt.Sprintf("\tw.WriteJceStructSlice(pkt.%s, %d)\n", name, tag)))
default: default:
w.Write([]byte(fmt.Sprintf("\tw.writeSlice(pkt.%s, %d)\n", name, tag))) w.Write([]byte(fmt.Sprintf("\tw.writeSlice(pkt.%s, %d)\n", name, tag)))
@ -91,7 +85,7 @@ func writeObject(w io.Writer, v reflect.Value, tag byte, name string) {
w.Write([]byte(fmt.Sprintf("\tw.WriteString(pkt.%s, %d)\n", name, tag))) w.Write([]byte(fmt.Sprintf("\tw.WriteString(pkt.%s, %d)\n", name, tag)))
default: default:
switch v.Interface().(type) { switch v.Interface().(type) {
case jce.IJceStruct: case IJceStruct:
w.Write([]byte(fmt.Sprintf("\tw.WriteJceStruct(pkt.%s, %d)\n", name, tag))) w.Write([]byte(fmt.Sprintf("\tw.WriteJceStruct(pkt.%s, %d)\n", name, tag)))
case float32: case float32:
w.Write([]byte(fmt.Sprintf("\tw.WriteFloat32(pkt.%s, %d)\n", name, tag))) w.Write([]byte(fmt.Sprintf("\tw.WriteFloat32(pkt.%s, %d)\n", name, tag)))
@ -147,10 +141,58 @@ func writeJceStructRaw(w io.Writer, s interface{}) {
} }
} }
func WriteJceStruct(w io.Writer, s jce.IJceStruct) { func WriteJceStruct(w io.Writer, s IJceStruct) {
w.Write([]byte(fmt.Sprintf("\nfunc (pkt %s) ToBytes() []byte {\n", strings.ReplaceAll(reflect.TypeOf(s).String(), "jce.", "")))) w.Write([]byte(fmt.Sprintf("\nfunc (pkt %s) ToBytes() []byte {\n", strings.ReplaceAll(reflect.TypeOf(s).String(), "", ""))))
w.Write([]byte("\tw := NewJceWriter()\n")) w.Write([]byte("\tw := NewJceWriter()\n"))
writeJceStructRaw(w, s) writeJceStructRaw(w, s)
w.Write([]byte("\treturn w.Bytes()\n")) w.Write([]byte("\treturn w.Bytes()\n"))
w.Write([]byte("}\n")) w.Write([]byte("}\n"))
} }
`
func main() {
f := flag.String("f", "structs_tobytes.go", "output file.")
i := flag.String("i", "structs.go", "input file.")
flag.Parse()
fmt.Println("gen runs on arg", *f, *i)
tmp, err := os.Create("tmp.go")
if err != nil {
panic(err)
}
inp, err := os.Open(*i)
if err != nil {
panic(err)
}
var structs []string
tmp.WriteString(tmphead)
scanner := bufio.NewScanner(inp)
start := false
for scanner.Scan() {
if scanner.Text() == "type (" {
start = true
tmp.WriteString("type (\n")
}
if start {
t := scanner.Text()
tmp.WriteString(t + "\n")
if t == ")" {
break
}
if strings.Contains(t, " struct {") {
structs = append(structs, strings.Trim(t[:len(t)-9], "\t"))
}
}
}
inp.Close()
fmt.Fprintf(tmp, tmpmain, *i)
for _, s := range structs {
fmt.Fprintf(tmp, "\tWriteJceStruct(w, &%s{})\n", s)
}
tmp.WriteString(tmptail)
tmp.Close()
exec.Command("go", "tmp.go", "-o tmp")
exec.Command("./tmp")
os.Remove("tmp.go")
os.Remove("tmp")
}

View File

@ -5,7 +5,7 @@ type IJceStruct interface {
ReadFrom(*JceReader) ReadFrom(*JceReader)
} }
//go:generate go run gen/structs_parser.go -f structs_tobytes.go //go:generate go run gen/structs_parser.go -f structs_tobytes.go -i structs.go
type ( type (
RequestPacket struct { RequestPacket struct {
IVersion int16 `jceId:"1"` IVersion int16 `jceId:"1"`