2023-03-05 03:57:27 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2023-03-10 23:10:57 +00:00
|
|
|
"io"
|
2023-03-05 03:57:27 +00:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"unicode/utf16"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
2023-03-09 22:42:13 +00:00
|
|
|
this file handles serializing (and deserializing) structs to alignment-strict c structures generated via `tools/genstructs.py`.
|
|
|
|
see script for details on usage!
|
2023-03-05 03:57:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
type Packet struct {
|
2023-03-11 01:56:05 +00:00
|
|
|
readWriter io.ReadWriter
|
2023-03-05 03:57:27 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 02:00:06 +00:00
|
|
|
func NewPacket(readWriter io.ReadWriter) Packet {
|
|
|
|
return Packet{
|
2023-03-11 01:56:05 +00:00
|
|
|
readWriter: readWriter,
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
2023-03-05 03:57:27 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 02:07:48 +00:00
|
|
|
func (pkt Packet) encodeStructField(field reflect.StructField, value reflect.Value) error {
|
|
|
|
// log.Printf("Encoding '%s'", field.Name)
|
2023-03-05 08:14:36 +00:00
|
|
|
|
2023-03-05 03:57:27 +00:00
|
|
|
switch field.Type.Kind() {
|
|
|
|
case reflect.String: // all strings in fusionfall packets are encoded as utf16, we'll need to encode it
|
|
|
|
sz, err := strconv.Atoi(field.Tag.Get("size"))
|
|
|
|
if err != nil {
|
2023-08-23 23:03:14 +00:00
|
|
|
return fmt.Errorf("failed to grab string 'size' tag")
|
2023-03-05 03:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf16 := utf16.Encode([]rune(value.String()))
|
|
|
|
|
2023-03-10 23:10:57 +00:00
|
|
|
// len(buf16) needs to be the same size as sz
|
|
|
|
if len(buf16) > sz {
|
2023-03-05 03:57:27 +00:00
|
|
|
// truncate
|
2023-03-10 23:10:57 +00:00
|
|
|
buf16 = buf16[:sz]
|
2023-03-05 03:57:27 +00:00
|
|
|
} else {
|
|
|
|
// grow
|
2023-12-03 03:54:54 +00:00
|
|
|
buf16 = append(buf16, make([]uint16, sz-len(buf16))...)
|
2023-03-05 03:57:27 +00:00
|
|
|
}
|
|
|
|
|
2023-03-10 23:10:57 +00:00
|
|
|
// write
|
2023-03-11 02:07:48 +00:00
|
|
|
if err := binary.Write(pkt.readWriter, binary.LittleEndian, buf16); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-05 03:57:27 +00:00
|
|
|
default:
|
2023-03-11 06:24:20 +00:00
|
|
|
if err := pkt.Encode(value.Interface()); err != nil {
|
2023-03-11 02:07:48 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-03-05 03:57:27 +00:00
|
|
|
}
|
2023-03-05 08:14:36 +00:00
|
|
|
|
|
|
|
// write padding bytes
|
|
|
|
pad, err := strconv.Atoi(field.Tag.Get("pad"))
|
|
|
|
if err == nil {
|
2023-11-23 00:18:22 +00:00
|
|
|
dummy := make([]byte, pad)
|
|
|
|
if _, err := pkt.readWriter.Write(dummy); err != nil {
|
|
|
|
return err
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-11 02:07:48 +00:00
|
|
|
|
|
|
|
return nil
|
2023-03-05 03:57:27 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 02:07:48 +00:00
|
|
|
func (pkt Packet) Encode(data interface{}) error {
|
2023-03-05 03:57:27 +00:00
|
|
|
rv := reflect.Indirect(reflect.ValueOf(data))
|
|
|
|
|
|
|
|
switch rv.Kind() {
|
|
|
|
case reflect.Struct:
|
|
|
|
// walk through each struct fields
|
|
|
|
sz := rv.NumField()
|
|
|
|
for i := 0; i < sz; i++ {
|
2023-03-11 02:07:48 +00:00
|
|
|
if err := pkt.encodeStructField(rv.Type().Field(i), rv.Field(i)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-05 03:57:27 +00:00
|
|
|
}
|
2023-03-10 23:10:57 +00:00
|
|
|
default:
|
|
|
|
// we pass everything else to go's binary package
|
2023-03-11 02:07:48 +00:00
|
|
|
if err := binary.Write(pkt.readWriter, binary.LittleEndian, data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-05 03:57:27 +00:00
|
|
|
}
|
2023-03-11 02:07:48 +00:00
|
|
|
|
|
|
|
return nil
|
2023-03-05 03:57:27 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 02:07:48 +00:00
|
|
|
func (pkt Packet) decodeStructField(field reflect.StructField, value reflect.Value) error {
|
|
|
|
// log.Printf("Decoding '%s'", field.Name)
|
2023-03-05 03:57:27 +00:00
|
|
|
|
|
|
|
switch field.Type.Kind() {
|
|
|
|
case reflect.String: // all strings in fusionfall packets are encoded as utf16, we'll need to decode it
|
|
|
|
sz, err := strconv.Atoi(field.Tag.Get("size"))
|
|
|
|
if err != nil {
|
2023-08-23 23:03:14 +00:00
|
|
|
return fmt.Errorf("failed to grab string 'size' tag")
|
2023-03-05 03:57:27 +00:00
|
|
|
}
|
|
|
|
|
2023-03-10 23:10:57 +00:00
|
|
|
buf16 := make([]uint16, sz)
|
2023-03-11 02:07:48 +00:00
|
|
|
if err := binary.Read(pkt.readWriter, binary.LittleEndian, buf16); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-05 03:57:27 +00:00
|
|
|
|
|
|
|
// find null terminator
|
|
|
|
var realSize int
|
|
|
|
for ; realSize < len(buf16); realSize++ {
|
|
|
|
if buf16[realSize] == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-10 23:10:57 +00:00
|
|
|
value.SetString(string(utf16.Decode(buf16[:realSize])))
|
2023-03-05 03:57:27 +00:00
|
|
|
default:
|
2023-03-11 02:07:48 +00:00
|
|
|
if err := pkt.Decode(value.Addr().Interface()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-05 03:57:27 +00:00
|
|
|
}
|
2023-03-05 08:14:36 +00:00
|
|
|
|
2023-03-11 01:56:05 +00:00
|
|
|
// consume padding bytes
|
2023-03-05 08:14:36 +00:00
|
|
|
pad, err := strconv.Atoi(field.Tag.Get("pad"))
|
|
|
|
if err == nil {
|
2023-12-03 03:54:54 +00:00
|
|
|
if _, err := pkt.readWriter.Read(make([]byte, pad)); err != nil {
|
2023-11-23 00:18:22 +00:00
|
|
|
return err
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-11 02:07:48 +00:00
|
|
|
|
|
|
|
return nil
|
2023-03-05 03:57:27 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 02:07:48 +00:00
|
|
|
func (pkt Packet) Decode(data interface{}) error {
|
2023-03-05 03:57:27 +00:00
|
|
|
rv := reflect.Indirect(reflect.ValueOf(data))
|
|
|
|
|
|
|
|
switch rv.Kind() {
|
|
|
|
case reflect.Struct:
|
|
|
|
// walk through each struct fields
|
|
|
|
sz := rv.NumField()
|
|
|
|
for i := 0; i < sz; i++ {
|
2023-03-11 02:07:48 +00:00
|
|
|
if err := pkt.decodeStructField(rv.Type().Field(i), rv.Field(i)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-05 03:57:27 +00:00
|
|
|
}
|
2023-03-10 23:10:57 +00:00
|
|
|
default:
|
2023-03-11 02:07:48 +00:00
|
|
|
if err := binary.Read(pkt.readWriter, binary.LittleEndian, data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-05 03:57:27 +00:00
|
|
|
}
|
2023-03-11 02:07:48 +00:00
|
|
|
|
|
|
|
return nil
|
2023-03-05 03:57:27 +00:00
|
|
|
}
|