gottdad/packet.go
2020-06-15 12:46:48 +02:00

187 lines
4.6 KiB
Go

package main
import (
"bufio"
"bytes"
"encoding/binary"
)
const (
AdminPacketAdminJoin = 0
AdminPacketAdminQuit = 1
AdminPacketAdminUpdateFrequency = 2
AdminPacketAdminPoll = 3
AdminPacketAdminChat = 4
AdminPacketAdminRCon = 5
AdminPacketAdminGamescript = 6
AdminPacketAdminPing = 7
AdminPacketServerFull = 100
AdminPacketServerBanned = 101
AdminPacketServerError = 102
AdminPacketServerProtocol = 103
AdminPacketServerWelcome = 104
AdminPacketServerNewgame = 105
AdminPacketServerShutdown = 106
AdminPacketServerDate = 107
AdminPacketServerClientJoin = 108
AdminPacketServerClientInfo = 109
AdminPacketServerClientUpdate = 110
AdminPacketServerClientQuit = 111
AdminPacketServerClientError = 112
AdminPacketServerCompanyNew = 113
AdminPacketServerCompanyInfo = 114
AdminPacketServerCompanyUpdate = 115
AdminPacketServerCompanyRemove = 116
AdminPacketServerCompanyEconomy = 117
AdminPacketServerCompanyStats = 118
AdminPacketServerChat = 119
AdminPacketServerRCon = 120
AdminPacketServerConsole = 121
AdminPacketServerCmdNames = 122
AdminPacketServerCmdLogging = 123
AdminPacketServerGamescript = 124
AdminPacketServerRConEnd = 125
AdminPacketServerPong = 126
InvalidAdminPacket = 255
AdminUpdateDate = 0
AdminUpdateClientInfo = 1
AdminUpdateCompanyInfo = 2
AdminUpdateCompanyEconomy = 3
AdminUpdateCompanyStats = 4
AdminUpdateChat = 5
AdminUpdateConsole = 6
AdminUpdateCmdNames = 7
AdminUpdateCmdLogging = 8
AdminUpdateGamescript = 9
AdminUpdateEnd = 10
AdminFrequencyPoll = 0x01
AdminFrequencyDaily = 0x02
AdminFrequencyWeekly = 0x04
AdminFrequencyMonthly = 0x08
AdminFrequencyQuarterly = 0x10
AdminFrequencyAnnually = 0x20
AdminFrequencyAutomatic = 0x40
)
type Packet struct {
PLength uint16
PType uint8
}
type PacketAdminJoin struct {
Packet
Password string
AppName string
AppVersion string
}
type PacketAdminServerProtocol struct {
Packet
ProtocolVersion uint8
FurtherData bool
UpdatePacketType uint16
FrequenciesAllowed uint16
}
type PacketAdminServerWelcome struct {
Packet
ServerName string
OpenTTDVersion string
Dedicated bool
MapName string
MapSeed uint32
MapLandscape uint8
MapStartDate uint32
MapX uint16
MapY uint16
}
type PacketAdminUpdateFrequency struct {
Packet
UpdateType uint16
UpdateFrequency uint16
}
type PacketAdminServerDate struct {
Packet
Date uint32
}
func (p *PacketAdminJoin) Bytes() []byte {
buf := new(bytes.Buffer)
p.PLength = uint16(len(p.Password) + len(p.AppName) + len(p.AppVersion) + 6)
binary.Write(buf, binary.LittleEndian, p.PLength)
binary.Write(buf, binary.LittleEndian, p.PType)
buf.WriteString(p.Password)
buf.WriteByte(0)
buf.WriteString(p.AppName)
buf.WriteByte(0)
buf.WriteString(p.AppVersion)
buf.WriteByte(0)
return buf.Bytes()
}
func (p *PacketAdminUpdateFrequency) Bytes() []byte {
buf := new(bytes.Buffer)
p.PLength = 7
binary.Write(buf, binary.LittleEndian, p.PLength)
binary.Write(buf, binary.LittleEndian, p.PType)
binary.Write(buf, binary.LittleEndian, p.UpdateType)
binary.Write(buf, binary.LittleEndian, p.UpdateFrequency)
return buf.Bytes()
}
func (p *PacketAdminServerWelcome) Read(b []byte) {
r := bufio.NewReader(bytes.NewReader(b))
r.Discard(3)
p.ServerName, _ = r.ReadString(0)
p.OpenTTDVersion, _ = r.ReadString(0)
c, _ := r.ReadByte()
if c > 0 {
p.Dedicated = true
} else {
p.Dedicated = false
}
p.MapName, _ = r.ReadString(0)
bs := make([]byte, 13)
_, _ = r.Read(bs)
p.MapSeed = binary.LittleEndian.Uint32(bs[0:])
r.Discard(4)
c, _ = r.ReadByte()
p.MapLandscape = uint8(c)
p.MapStartDate = binary.LittleEndian.Uint32(bs[5:])
p.MapX = binary.LittleEndian.Uint16(bs[9:])
p.MapY = binary.LittleEndian.Uint16(bs[11:])
}
func (p *PacketAdminServerProtocol) Read(b []byte) {
r := bufio.NewReader(bytes.NewReader(b))
r.Discard(3)
c, _ := r.ReadByte()
p.ProtocolVersion = uint8(c)
c, _ = r.ReadByte()
if c > 0 {
p.FurtherData = true
} else {
p.FurtherData = false
}
bs := make([]byte, 13)
_, _ = r.Read(bs)
p.UpdatePacketType = binary.LittleEndian.Uint16(bs[0:])
p.FrequenciesAllowed = binary.LittleEndian.Uint16(bs[2:])
}
func (p *PacketAdminServerDate) Read(b []byte) {
r := bufio.NewReader(bytes.NewReader(b))
r.Discard(3)
bs := make([]byte, 4)
_, _ = r.Read(bs)
p.Date = binary.LittleEndian.Uint32(bs[0:])
}