Horrible refactor to allow unpacking net messages
This commit is contained in:
parent
a78bf12bf0
commit
81f6b89f70
|
@ -4,6 +4,7 @@ import (
|
|||
"slices"
|
||||
|
||||
"github.com/teeworlds-go/teeworlds/network7"
|
||||
"github.com/teeworlds-go/teeworlds/packer"
|
||||
)
|
||||
|
||||
type CtrlConnect struct {
|
||||
|
@ -33,3 +34,8 @@ func (msg CtrlConnect) Pack() []byte {
|
|||
[]byte{512: 0},
|
||||
)
|
||||
}
|
||||
|
||||
// TODO: no idea if this works
|
||||
func (msg *CtrlConnect) Unpack(u *packer.Unpacker) {
|
||||
msg.Token = [4]byte(u.Data())
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package messages7
|
|||
|
||||
import (
|
||||
"github.com/teeworlds-go/teeworlds/network7"
|
||||
"github.com/teeworlds-go/teeworlds/packer"
|
||||
)
|
||||
|
||||
type CtrlKeepAlive struct {
|
||||
|
@ -26,3 +27,6 @@ func (msg CtrlKeepAlive) Vital() bool {
|
|||
func (msg CtrlKeepAlive) Pack() []byte {
|
||||
return []byte{network7.MsgCtrlKeepAlive}
|
||||
}
|
||||
|
||||
func (msg *CtrlKeepAlive) Unpack(u *packer.Unpacker) {
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"slices"
|
||||
|
||||
"github.com/teeworlds-go/teeworlds/network7"
|
||||
"github.com/teeworlds-go/teeworlds/packer"
|
||||
)
|
||||
|
||||
type CtrlToken struct {
|
||||
|
@ -36,3 +37,8 @@ func (msg CtrlToken) Pack() []byte {
|
|||
[]byte{512: 0},
|
||||
)
|
||||
}
|
||||
|
||||
// TODO: no idea if this works
|
||||
func (msg *CtrlToken) Unpack(u *packer.Unpacker) {
|
||||
msg.Token = [4]byte(u.Data())
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package messages7
|
|||
|
||||
import (
|
||||
"github.com/teeworlds-go/teeworlds/network7"
|
||||
"github.com/teeworlds-go/teeworlds/packer"
|
||||
)
|
||||
|
||||
type EnterGame struct {
|
||||
|
@ -26,3 +27,6 @@ func (msg EnterGame) Vital() bool {
|
|||
func (msg EnterGame) Pack() []byte {
|
||||
return []byte{}
|
||||
}
|
||||
|
||||
func (msg *EnterGame) Unpack(u *packer.Unpacker) {
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package messages7
|
|||
|
||||
import (
|
||||
"github.com/teeworlds-go/teeworlds/network7"
|
||||
"github.com/teeworlds-go/teeworlds/packer"
|
||||
)
|
||||
|
||||
type Info struct {
|
||||
|
@ -31,3 +32,8 @@ func (msg Info) Pack() []byte {
|
|||
0x5F, 0x31, 0x32, 0x33, 0x00, 0x85, 0x1C, 0x00,
|
||||
}
|
||||
}
|
||||
|
||||
func (msg *Info) Unpack(u *packer.Unpacker) {
|
||||
// TODO: implement
|
||||
panic("not implemented")
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package messages7
|
||||
|
||||
import "github.com/teeworlds-go/teeworlds/network7"
|
||||
import (
|
||||
"github.com/teeworlds-go/teeworlds/network7"
|
||||
"github.com/teeworlds-go/teeworlds/packer"
|
||||
)
|
||||
|
||||
type NetMessage interface {
|
||||
MsgId() int
|
||||
|
@ -8,4 +11,5 @@ type NetMessage interface {
|
|||
System() bool
|
||||
Vital() bool
|
||||
Pack() []byte
|
||||
Unpack(u *packer.Unpacker)
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package messages7
|
|||
|
||||
import (
|
||||
"github.com/teeworlds-go/teeworlds/network7"
|
||||
"github.com/teeworlds-go/teeworlds/packer"
|
||||
)
|
||||
|
||||
type Ready struct {
|
||||
|
@ -26,3 +27,6 @@ func (msg Ready) Vital() bool {
|
|||
func (msg Ready) Pack() []byte {
|
||||
return []byte{}
|
||||
}
|
||||
|
||||
func (msg *Ready) Unpack(u *packer.Unpacker) {
|
||||
}
|
||||
|
|
32
messages7/ready_to_enter.go
Normal file
32
messages7/ready_to_enter.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package messages7
|
||||
|
||||
import (
|
||||
"github.com/teeworlds-go/teeworlds/network7"
|
||||
"github.com/teeworlds-go/teeworlds/packer"
|
||||
)
|
||||
|
||||
type ReadyToEnter struct {
|
||||
}
|
||||
|
||||
func (msg ReadyToEnter) MsgId() int {
|
||||
return network7.MsgGameReadyToEnter
|
||||
}
|
||||
|
||||
func (msg ReadyToEnter) MsgType() network7.MsgType {
|
||||
return network7.TypeNet
|
||||
}
|
||||
|
||||
func (msg ReadyToEnter) System() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (msg ReadyToEnter) Vital() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (msg ReadyToEnter) Pack() []byte {
|
||||
return []byte{}
|
||||
}
|
||||
|
||||
func (msg *ReadyToEnter) Unpack(u *packer.Unpacker) {
|
||||
}
|
|
@ -53,7 +53,7 @@ func (connection *Connection) CtrlToken() *Packet {
|
|||
response.Header.Flags.Control = true
|
||||
response.Messages = append(
|
||||
response.Messages,
|
||||
messages7.CtrlToken{
|
||||
&messages7.CtrlToken{
|
||||
Token: connection.ClientToken,
|
||||
},
|
||||
)
|
||||
|
@ -61,8 +61,8 @@ func (connection *Connection) CtrlToken() *Packet {
|
|||
return response
|
||||
}
|
||||
|
||||
func (client *Connection) MsgStartInfo() messages7.ClStartInfo {
|
||||
return messages7.ClStartInfo{
|
||||
func (client *Connection) MsgStartInfo() *messages7.ClStartInfo {
|
||||
return &messages7.ClStartInfo{
|
||||
Name: "gopher",
|
||||
Clan: "",
|
||||
Country: 0,
|
||||
|
@ -98,14 +98,14 @@ func byteSliceToString(s []byte) string {
|
|||
func (connection *Connection) OnSystemMsg(msg int, chunk chunk7.Chunk, u *packer.Unpacker, result *PacketResult) {
|
||||
if msg == network7.MsgSysMapChange {
|
||||
fmt.Println("got map change")
|
||||
result.Response.Messages = append(result.Response.Messages, messages7.Ready{})
|
||||
result.Response.Messages = append(result.Response.Messages, &messages7.Ready{})
|
||||
} else if msg == network7.MsgSysConReady {
|
||||
fmt.Println("got ready")
|
||||
result.Response.Messages = append(result.Response.Messages, connection.MsgStartInfo())
|
||||
} else if msg == network7.MsgSysSnapSingle {
|
||||
// tick := u.GetInt()
|
||||
// fmt.Printf("got snap single tick=%d\n", tick)
|
||||
result.Response.Messages = append(result.Response.Messages, messages7.CtrlKeepAlive{})
|
||||
result.Response.Messages = append(result.Response.Messages, &messages7.CtrlKeepAlive{})
|
||||
} else {
|
||||
fmt.Printf("unknown system message id=%d data=%x\n", msg, chunk.Data)
|
||||
}
|
||||
|
@ -123,7 +123,8 @@ func (client *Connection) OnMotd(motd string) {
|
|||
func (client *Connection) OnGameMsg(msg int, chunk chunk7.Chunk, u *packer.Unpacker, result *PacketResult) {
|
||||
if msg == network7.MsgGameReadyToEnter {
|
||||
fmt.Println("got ready to enter")
|
||||
result.Response.Messages = append(result.Response.Messages, messages7.EnterGame{})
|
||||
result.Packet.Messages = append(result.Packet.Messages, &messages7.Ready{})
|
||||
result.Response.Messages = append(result.Response.Messages, &messages7.EnterGame{})
|
||||
} else if msg == network7.MsgGameSvMotd {
|
||||
motd := u.GetString()
|
||||
if motd != "" {
|
||||
|
@ -203,14 +204,14 @@ func (connection *Connection) OnPacket(data []byte) (*PacketResult, error) {
|
|||
fmt.Printf("got server token %x\n", connection.ServerToken)
|
||||
result.Response.Messages = append(
|
||||
result.Response.Messages,
|
||||
messages7.CtrlConnect{
|
||||
&messages7.CtrlConnect{
|
||||
Token: connection.ClientToken,
|
||||
},
|
||||
)
|
||||
} else if ctrlMsg == network7.MsgCtrlAccept {
|
||||
fmt.Println("got accept")
|
||||
// TODO: don't hardcode info
|
||||
result.Response.Messages = append(result.Response.Messages, messages7.Info{})
|
||||
result.Response.Messages = append(result.Response.Messages, &messages7.Info{})
|
||||
} else if ctrlMsg == network7.MsgCtrlClose {
|
||||
// TODO: get length from packet header to determine if a reason is set or not
|
||||
// len(data) -> is 1400 (maxPacketLen)
|
||||
|
|
|
@ -76,7 +76,9 @@ func main() {
|
|||
// example of modifying outgoing traffic
|
||||
for i, msg := range result.Response.Messages {
|
||||
if msg.MsgId() == network7.MsgCtrlConnect {
|
||||
if connect, ok := result.Response.Messages[0].(messages7.CtrlConnect); ok {
|
||||
var connect *messages7.CtrlConnect
|
||||
var ok bool
|
||||
if connect, ok = result.Response.Messages[0].(*messages7.CtrlConnect); ok {
|
||||
connect.Token = [4]byte{0xaa, 0xaa, 0xaa, 0xaa}
|
||||
result.Response.Messages[i] = connect
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue