Skip to content

Commit d982660

Browse files
committed
core: parse and store ConVars (#97)
- add GameState.ConVars() - add events.ConVarsUpdated()
1 parent 5d26956 commit d982660

File tree

9 files changed

+54
-3
lines changed

9 files changed

+54
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Check out the [examples](examples) folder for more examples, like [how to genera
101101
## Features
102102

103103
* Game events (kills, shots, round starts/ends, footsteps etc.) - [docs](https://godoc.org/github.com/markus-wa/demoinfocs-golang/events) / [example](https://github.com/markus-wa/demoinfocs-golang/tree/master/examples/print-events)
104-
* Tracking of game-state (players, teams, grenades etc.) - [docs](https://godoc.org/github.com/markus-wa/demoinfocs-golang#GameState)
104+
* Tracking of game-state (players, teams, grenades, ConVars etc.) - [docs](https://godoc.org/github.com/markus-wa/demoinfocs-golang#GameState)
105105
* Grenade projectiles / trajectories - [docs](https://godoc.org/github.com/markus-wa/demoinfocs-golang#GameState.GrenadeProjectiles) / [example](https://github.com/markus-wa/demoinfocs-golang/tree/master/examples/nade-trajectories)
106106
* Access to entities, server-classes & data-tables - [docs](https://godoc.org/github.com/markus-wa/demoinfocs-golang/sendtables#ServerClasses) / [example](https://github.com/markus-wa/demoinfocs-golang/tree/master/examples/entities)
107107
* Access to all net-messages - [docs](https://godoc.org/github.com/markus-wa/demoinfocs-golang#NetMessageCreator) / [example](https://github.com/markus-wa/demoinfocs-golang/tree/master/examples/net-messages)

convars.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package demoinfocs
2+
3+
import (
4+
"github.com/markus-wa/demoinfocs-golang/events"
5+
"github.com/markus-wa/demoinfocs-golang/msg"
6+
)
7+
8+
func (p *Parser) handleSetConVar(setConVar *msg.CNETMsg_SetConVar) {
9+
updated := make(map[string]string)
10+
for _, cvar := range setConVar.Convars.Cvars {
11+
updated[cvar.Name] = cvar.Value
12+
p.gameState.conVars[cvar.Name] = cvar.Value
13+
}
14+
15+
p.eventDispatcher.Dispatch(events.ConVarsUpdated{
16+
UpdatedConVars: updated,
17+
})
18+
}

events/events.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,3 +536,9 @@ type IsWarmupPeriodChanged struct {
536536
type PlayerSpottersChanged struct {
537537
Spotted *common.Player
538538
}
539+
540+
// ConVarsUpdated signals that ConVars/CVars have been updated.
541+
// See GameState.ConVars().
542+
type ConVarsUpdated struct {
543+
UpdatedConVars map[string]string
544+
}

fake/game_state.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package fake
22

33
import (
4-
mock "github.com/stretchr/testify/mock"
4+
"github.com/stretchr/testify/mock"
55

66
dem "github.com/markus-wa/demoinfocs-golang"
7-
common "github.com/markus-wa/demoinfocs-golang/common"
7+
"github.com/markus-wa/demoinfocs-golang/common"
88
st "github.com/markus-wa/demoinfocs-golang/sendtables"
99
)
1010

@@ -79,3 +79,8 @@ func (gs *GameState) IsWarmupPeriod() bool {
7979
func (gs *GameState) IsMatchStarted() bool {
8080
return gs.Called().Bool(0)
8181
}
82+
83+
// ConVars is a mock-implementation of IGameState.ConVars().
84+
func (gs *GameState) ConVars() map[string]string {
85+
return gs.Called().Get(0).(map[string]string)
86+
}

game_state.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type GameState struct {
1818
grenadeProjectiles map[int]*common.GrenadeProjectile // Maps entity-IDs to active nade-projectiles. That's grenades that have been thrown, but have not yet detonated.
1919
infernos map[int]*common.Inferno // Maps entity-IDs to active infernos.
2020
entities map[int]*st.Entity // Maps entity IDs to entities
21+
conVars map[string]string
2122
bomb common.Bomb
2223
totalRoundsPlayed int
2324
gamePhase common.GamePhase
@@ -121,13 +122,21 @@ func (gs GameState) IsMatchStarted() bool {
121122
return gs.isMatchStarted
122123
}
123124

125+
// ConVars returns a map of CVar keys and values.
126+
// Not all values might be set.
127+
// See also: https://developer.valvesoftware.com/wiki/List_of_CS:GO_Cvars.
128+
func (gs *GameState) ConVars() map[string]string {
129+
return gs.conVars
130+
}
131+
124132
func newGameState() *GameState {
125133
gs := &GameState{
126134
playersByEntityID: make(map[int]*common.Player),
127135
playersByUserID: make(map[int]*common.Player),
128136
grenadeProjectiles: make(map[int]*common.GrenadeProjectile),
129137
infernos: make(map[int]*common.Inferno),
130138
entities: make(map[int]*st.Entity),
139+
conVars: make(map[string]string),
131140
}
132141

133142
gs.tState = common.NewTeamState(common.TeamTerrorists, gs.Participants().TeamMembers)

game_state_interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,8 @@ type IGameState interface {
5050
IsWarmupPeriod() bool
5151
// IsMatchStarted returns whether the match has started according to CCSGameRulesProxy.
5252
IsMatchStarted() bool
53+
// ConVars returns a map of CVar keys and values.
54+
// Not all values might be set.
55+
// See also: https://developer.valvesoftware.com/wiki/List_of_CS:GO_Cvars.
56+
ConVars() map[string]string
5357
}

game_state_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ func TestGameState_Participants(t *testing.T) {
5454
assert.NotEqual(t, byUserID, ptcp.ByUserID())
5555
}
5656

57+
func TestGameState_ConVars(t *testing.T) {
58+
cvars := make(map[string]string)
59+
gs := GameState{conVars: cvars}
60+
61+
assert.Equal(t, cvars, gs.ConVars())
62+
}
63+
5764
func TestParticipants_All(t *testing.T) {
5865
pl := newPlayer()
5966
ptcps := Participants{

parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ func NewParserWithConfig(demostream io.Reader, config ParserConfig) *Parser {
253253
p.msgDispatcher.RegisterHandler(p.handleCreateStringTable)
254254
p.msgDispatcher.RegisterHandler(p.handleUpdateStringTable)
255255
p.msgDispatcher.RegisterHandler(p.handleUserMessage)
256+
p.msgDispatcher.RegisterHandler(p.handleSetConVar)
256257
p.msgDispatcher.RegisterHandler(p.handleFrameParsed)
257258
p.msgDispatcher.RegisterHandler(p.gameState.handleIngameTickNumber)
258259

parsing.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ var defaultNetMessageCreators = map[int]NetMessageCreator{
261261
int(msg.SVC_Messages_svc_CreateStringTable): func() proto.Message { return new(msg.CSVCMsg_CreateStringTable) },
262262
int(msg.SVC_Messages_svc_UpdateStringTable): func() proto.Message { return new(msg.CSVCMsg_UpdateStringTable) },
263263
int(msg.SVC_Messages_svc_UserMessage): func() proto.Message { return new(msg.CSVCMsg_UserMessage) },
264+
int(msg.NET_Messages_net_SetConVar): func() proto.Message { return new(msg.CNETMsg_SetConVar) },
264265
}
265266

266267
func (p *Parser) parsePacket() {

0 commit comments

Comments
 (0)