-
Notifications
You must be signed in to change notification settings - Fork 131
/
gamecoordinator.go
80 lines (65 loc) · 1.95 KB
/
gamecoordinator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package steam
import (
"bytes"
"github.com/Philipp15b/go-steam/v3/protocol"
"github.com/Philipp15b/go-steam/v3/protocol/gamecoordinator"
"github.com/Philipp15b/go-steam/v3/protocol/protobuf"
"github.com/Philipp15b/go-steam/v3/protocol/steamlang"
"google.golang.org/protobuf/proto"
)
type GameCoordinator struct {
client *Client
handlers []GCPacketHandler
}
func newGC(client *Client) *GameCoordinator {
return &GameCoordinator{
client: client,
handlers: make([]GCPacketHandler, 0),
}
}
type GCPacketHandler interface {
HandleGCPacket(*gamecoordinator.GCPacket)
}
func (g *GameCoordinator) RegisterPacketHandler(handler GCPacketHandler) {
g.handlers = append(g.handlers, handler)
}
func (g *GameCoordinator) HandlePacket(packet *protocol.Packet) {
if packet.EMsg != steamlang.EMsg_ClientFromGC {
return
}
msg := new(protobuf.CMsgGCClient)
packet.ReadProtoMsg(msg)
p, err := gamecoordinator.NewGCPacket(msg)
if err != nil {
g.client.Errorf("Error reading GC message: %v", err)
return
}
for _, handler := range g.handlers {
handler.HandleGCPacket(p)
}
}
func (g *GameCoordinator) Write(msg gamecoordinator.IGCMsg) {
buf := new(bytes.Buffer)
msg.Serialize(buf)
msgType := msg.GetMsgType()
if msg.IsProto() {
msgType = msgType | 0x80000000 // mask with protoMask
}
g.client.Write(protocol.NewClientMsgProtobuf(steamlang.EMsg_ClientToGC, &protobuf.CMsgGCClient{
Msgtype: proto.Uint32(msgType),
Appid: proto.Uint32(msg.GetAppId()),
Payload: buf.Bytes(),
}))
}
// Sets you in the given games. Specify none to quit all games.
func (g *GameCoordinator) SetGamesPlayed(appIds ...uint64) {
games := make([]*protobuf.CMsgClientGamesPlayed_GamePlayed, 0)
for _, appId := range appIds {
games = append(games, &protobuf.CMsgClientGamesPlayed_GamePlayed{
GameId: proto.Uint64(appId),
})
}
g.client.Write(protocol.NewClientMsgProtobuf(steamlang.EMsg_ClientGamesPlayed, &protobuf.CMsgClientGamesPlayed{
GamesPlayed: games,
}))
}