Skip to content

Commit e1ba260

Browse files
chat message parsing
1 parent 1927ba2 commit e1ba260

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

client_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func (ch *ClientHandler) handleCommand(o interface{}) error {
178178
case *commands.ServerTimeOfDay:
179179
fmt.Printf("Time of day: %d\n", cmd.TimeOfDay)
180180
case *commands.ServerChatMessage:
181-
fmt.Printf("Chat: '%s'\n", cmd.Message)
181+
fmt.Printf("Chat: %s\n", cmd)
182182
case *commands.ServerMovePlayer:
183183
fmt.Printf("Move player: '%s'\n", cmd)
184184

commands/server_chat_message.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package commands
22

33
import (
4+
"encoding/binary"
45
"fmt"
6+
7+
"golang.org/x/text/encoding/unicode"
58
)
69

10+
type ServerChatMessageType int
11+
712
type ServerChatMessage struct {
13+
Version int
14+
Type ServerChatMessageType
15+
Sender string
816
Message string
917
}
1018

@@ -17,11 +25,38 @@ func (p *ServerChatMessage) MarshalPacket() ([]byte, error) {
1725
}
1826

1927
func (p *ServerChatMessage) UnmarshalPacket(payload []byte) error {
20-
size := payload[5]
21-
p.Message = string(payload[6 : (size*2)+6])
28+
offset := 0
29+
p.Version = int(payload[offset])
30+
offset++
31+
p.Type = ServerChatMessageType(payload[offset])
32+
offset++
33+
34+
sender_len := binary.BigEndian.Uint16(payload[offset:])
35+
offset += 2
36+
37+
utf16 := unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)
38+
sender_bytes, err := utf16.NewDecoder().Bytes(payload[offset : offset+int(sender_len*2)])
39+
if err != nil {
40+
return err
41+
}
42+
43+
p.Sender = string(sender_bytes)
44+
offset += int(sender_len * 2)
45+
46+
message_len := binary.BigEndian.Uint16(payload[offset:])
47+
offset += 2
48+
49+
message_bytes, err := utf16.NewDecoder().Bytes(payload[offset : offset+int(message_len*2)])
50+
if err != nil {
51+
return err
52+
}
53+
54+
p.Message = string(message_bytes)
55+
2256
return nil
2357
}
2458

2559
func (p *ServerChatMessage) String() string {
26-
return fmt.Sprintf("{ServerChatMessage Message='%s'}", p.Message)
60+
return fmt.Sprintf("{ServerChatMessage Version=%d, Type=%d, Sender='%s', Message='%s'}",
61+
p.Version, p.Type, p.Sender, p.Message)
2762
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ require (
88
github.com/davecgh/go-spew v1.1.0 // indirect
99
github.com/gorilla/mux v1.8.0
1010
github.com/pmezard/go-difflib v1.0.0 // indirect
11+
golang.org/x/text v0.3.7
1112
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
1213
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
99
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1010
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
1111
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
12+
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
13+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
1214
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1315
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
1416
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)