Horrible refactor to allow unpacking net messages

This commit is contained in:
ChillerDragon 2024-06-20 13:07:10 +08:00
parent a78bf12bf0
commit 81f6b89f70
10 changed files with 79 additions and 10 deletions

View file

@ -4,6 +4,7 @@ import (
"slices" "slices"
"github.com/teeworlds-go/teeworlds/network7" "github.com/teeworlds-go/teeworlds/network7"
"github.com/teeworlds-go/teeworlds/packer"
) )
type CtrlConnect struct { type CtrlConnect struct {
@ -33,3 +34,8 @@ func (msg CtrlConnect) Pack() []byte {
[]byte{512: 0}, []byte{512: 0},
) )
} }
// TODO: no idea if this works
func (msg *CtrlConnect) Unpack(u *packer.Unpacker) {
msg.Token = [4]byte(u.Data())
}

View file

@ -2,6 +2,7 @@ package messages7
import ( import (
"github.com/teeworlds-go/teeworlds/network7" "github.com/teeworlds-go/teeworlds/network7"
"github.com/teeworlds-go/teeworlds/packer"
) )
type CtrlKeepAlive struct { type CtrlKeepAlive struct {
@ -26,3 +27,6 @@ func (msg CtrlKeepAlive) Vital() bool {
func (msg CtrlKeepAlive) Pack() []byte { func (msg CtrlKeepAlive) Pack() []byte {
return []byte{network7.MsgCtrlKeepAlive} return []byte{network7.MsgCtrlKeepAlive}
} }
func (msg *CtrlKeepAlive) Unpack(u *packer.Unpacker) {
}

View file

@ -7,6 +7,7 @@ import (
"slices" "slices"
"github.com/teeworlds-go/teeworlds/network7" "github.com/teeworlds-go/teeworlds/network7"
"github.com/teeworlds-go/teeworlds/packer"
) )
type CtrlToken struct { type CtrlToken struct {
@ -36,3 +37,8 @@ func (msg CtrlToken) Pack() []byte {
[]byte{512: 0}, []byte{512: 0},
) )
} }
// TODO: no idea if this works
func (msg *CtrlToken) Unpack(u *packer.Unpacker) {
msg.Token = [4]byte(u.Data())
}

View file

@ -2,6 +2,7 @@ package messages7
import ( import (
"github.com/teeworlds-go/teeworlds/network7" "github.com/teeworlds-go/teeworlds/network7"
"github.com/teeworlds-go/teeworlds/packer"
) )
type EnterGame struct { type EnterGame struct {
@ -26,3 +27,6 @@ func (msg EnterGame) Vital() bool {
func (msg EnterGame) Pack() []byte { func (msg EnterGame) Pack() []byte {
return []byte{} return []byte{}
} }
func (msg *EnterGame) Unpack(u *packer.Unpacker) {
}

View file

@ -2,6 +2,7 @@ package messages7
import ( import (
"github.com/teeworlds-go/teeworlds/network7" "github.com/teeworlds-go/teeworlds/network7"
"github.com/teeworlds-go/teeworlds/packer"
) )
type Info struct { type Info struct {
@ -31,3 +32,8 @@ func (msg Info) Pack() []byte {
0x5F, 0x31, 0x32, 0x33, 0x00, 0x85, 0x1C, 0x00, 0x5F, 0x31, 0x32, 0x33, 0x00, 0x85, 0x1C, 0x00,
} }
} }
func (msg *Info) Unpack(u *packer.Unpacker) {
// TODO: implement
panic("not implemented")
}

View file

@ -1,6 +1,9 @@
package messages7 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 { type NetMessage interface {
MsgId() int MsgId() int
@ -8,4 +11,5 @@ type NetMessage interface {
System() bool System() bool
Vital() bool Vital() bool
Pack() []byte Pack() []byte
Unpack(u *packer.Unpacker)
} }

View file

@ -2,6 +2,7 @@ package messages7
import ( import (
"github.com/teeworlds-go/teeworlds/network7" "github.com/teeworlds-go/teeworlds/network7"
"github.com/teeworlds-go/teeworlds/packer"
) )
type Ready struct { type Ready struct {
@ -26,3 +27,6 @@ func (msg Ready) Vital() bool {
func (msg Ready) Pack() []byte { func (msg Ready) Pack() []byte {
return []byte{} return []byte{}
} }
func (msg *Ready) Unpack(u *packer.Unpacker) {
}

View 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) {
}

View file

@ -53,7 +53,7 @@ func (connection *Connection) CtrlToken() *Packet {
response.Header.Flags.Control = true response.Header.Flags.Control = true
response.Messages = append( response.Messages = append(
response.Messages, response.Messages,
messages7.CtrlToken{ &messages7.CtrlToken{
Token: connection.ClientToken, Token: connection.ClientToken,
}, },
) )
@ -61,8 +61,8 @@ func (connection *Connection) CtrlToken() *Packet {
return response return response
} }
func (client *Connection) MsgStartInfo() messages7.ClStartInfo { func (client *Connection) MsgStartInfo() *messages7.ClStartInfo {
return messages7.ClStartInfo{ return &messages7.ClStartInfo{
Name: "gopher", Name: "gopher",
Clan: "", Clan: "",
Country: 0, 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) { func (connection *Connection) OnSystemMsg(msg int, chunk chunk7.Chunk, u *packer.Unpacker, result *PacketResult) {
if msg == network7.MsgSysMapChange { if msg == network7.MsgSysMapChange {
fmt.Println("got map change") 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 { } else if msg == network7.MsgSysConReady {
fmt.Println("got ready") fmt.Println("got ready")
result.Response.Messages = append(result.Response.Messages, connection.MsgStartInfo()) result.Response.Messages = append(result.Response.Messages, connection.MsgStartInfo())
} else if msg == network7.MsgSysSnapSingle { } else if msg == network7.MsgSysSnapSingle {
// tick := u.GetInt() // tick := u.GetInt()
// fmt.Printf("got snap single tick=%d\n", tick) // 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 { } else {
fmt.Printf("unknown system message id=%d data=%x\n", msg, chunk.Data) 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) { func (client *Connection) OnGameMsg(msg int, chunk chunk7.Chunk, u *packer.Unpacker, result *PacketResult) {
if msg == network7.MsgGameReadyToEnter { if msg == network7.MsgGameReadyToEnter {
fmt.Println("got ready to enter") 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 { } else if msg == network7.MsgGameSvMotd {
motd := u.GetString() motd := u.GetString()
if motd != "" { if motd != "" {
@ -203,14 +204,14 @@ func (connection *Connection) OnPacket(data []byte) (*PacketResult, error) {
fmt.Printf("got server token %x\n", connection.ServerToken) fmt.Printf("got server token %x\n", connection.ServerToken)
result.Response.Messages = append( result.Response.Messages = append(
result.Response.Messages, result.Response.Messages,
messages7.CtrlConnect{ &messages7.CtrlConnect{
Token: connection.ClientToken, Token: connection.ClientToken,
}, },
) )
} else if ctrlMsg == network7.MsgCtrlAccept { } else if ctrlMsg == network7.MsgCtrlAccept {
fmt.Println("got accept") fmt.Println("got accept")
// TODO: don't hardcode info // 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 { } else if ctrlMsg == network7.MsgCtrlClose {
// TODO: get length from packet header to determine if a reason is set or not // TODO: get length from packet header to determine if a reason is set or not
// len(data) -> is 1400 (maxPacketLen) // len(data) -> is 1400 (maxPacketLen)

View file

@ -76,7 +76,9 @@ func main() {
// example of modifying outgoing traffic // example of modifying outgoing traffic
for i, msg := range result.Response.Messages { for i, msg := range result.Response.Messages {
if msg.MsgId() == network7.MsgCtrlConnect { 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} connect.Token = [4]byte{0xaa, 0xaa, 0xaa, 0xaa}
result.Response.Messages[i] = connect result.Response.Messages[i] = connect
} }