Skip to content

Commit 13daf80

Browse files
committed
sendtables: make structs private, only expose interfaces
1 parent 1a36444 commit 13daf80

25 files changed

+139
-136
lines changed

examples/entities/entities.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func main() {
2020
p := demoinfocs.NewParser(f)
2121

2222
p.RegisterEventHandler(func(events.DataTablesParsed) {
23-
p.ServerClasses().FindByName("CWeaponAWP").OnEntityCreated(func(ent *st.Entity) {
23+
p.ServerClasses().FindByName("CWeaponAWP").OnEntityCreated(func(ent st.Entity) {
2424
ent.Property("m_hOwnerEntity").OnUpdate(func(val st.PropertyValue) {
2525
x := p.GameState().Participants().FindByHandle(val.IntVal)
2626
if x != nil {

pkg/demoinfocs/common/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (h DemoHeader) FrameTime() time.Duration {
6262
// GrenadeProjectile is a grenade thrown intentionally by a player. It is used to track grenade projectile
6363
// positions between the time at which they are thrown and until they detonate.
6464
type GrenadeProjectile struct {
65-
Entity st.IEntity
65+
Entity st.Entity
6666
WeaponInstance *Equipment
6767
Thrower *Player // Always seems to be the same as Owner, even if the grenade was picked up
6868
Owner *Player // Always seems to be the same as Thrower, even if the grenade was picked up
@@ -115,7 +115,7 @@ type TeamState struct {
115115
team Team
116116
membersCallback func(Team) []*Player
117117

118-
Entity st.IEntity
118+
Entity st.Entity
119119

120120
// Terrorist TeamState for CTs, CT TeamState for Terrorists
121121
Opponent *TeamState

pkg/demoinfocs/common/common_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ type demoInfoProviderMock struct {
117117
tickRate float64
118118
ingameTick int
119119
playersByHandle map[int]*Player
120-
playerResourceEntity st.IEntity
120+
playerResourceEntity st.Entity
121121
}
122122

123123
func (p demoInfoProviderMock) TickRate() float64 {
@@ -132,7 +132,7 @@ func (p demoInfoProviderMock) FindPlayerByHandle(handle int) *Player {
132132
return p.playersByHandle[handle]
133133
}
134134

135-
func (p demoInfoProviderMock) PlayerResourceEntity() st.IEntity {
135+
func (p demoInfoProviderMock) PlayerResourceEntity() st.Entity {
136136
return p.playerResourceEntity
137137
}
138138

pkg/demoinfocs/common/entity_util.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@ package common
22

33
import st "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/sendtables"
44

5-
func getInt(entity st.IEntity, propName string) int {
5+
func getInt(entity st.Entity, propName string) int {
66
if entity == nil {
77
return 0
88
}
99

1010
return entity.PropertyValueMust(propName).IntVal
1111
}
1212

13-
func getFloat(entity st.IEntity, propName string) float32 {
13+
func getFloat(entity st.Entity, propName string) float32 {
1414
if entity == nil {
1515
return 0
1616
}
1717

1818
return entity.PropertyValueMust(propName).FloatVal
1919
}
2020

21-
func getString(entity st.IEntity, propName string) string {
21+
func getString(entity st.Entity, propName string) string {
2222
if entity == nil {
2323
return ""
2424
}
2525

2626
return entity.PropertyValueMust(propName).StringVal
2727
}
2828

29-
func getBool(entity st.IEntity, propName string) bool {
29+
func getBool(entity st.Entity, propName string) bool {
3030
if entity == nil {
3131
return false
3232
}

pkg/demoinfocs/common/equipment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ const (
285285
// This also includes the skin and some additional data.
286286
type Equipment struct {
287287
Type EquipmentType // The type of weapon which the equipment instantiates.
288-
Entity st.IEntity // The game entity instance
288+
Entity st.Entity // The game entity instance
289289
Owner *Player // The player carrying the equipment, not necessarily the buyer.
290290
OriginalString string // E.g. 'models/weapons/w_rif_m4a1_s.mdl'. Used internally to differentiate alternative weapons (M4A4 / M4A1-S etc.).
291291

pkg/demoinfocs/common/inferno.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
//
1818
// See also: Inferno.Active() and Fire.IsBurning
1919
type Inferno struct {
20-
Entity st.IEntity
20+
Entity st.Entity
2121

2222
// uniqueID is used to distinguish different infernos (which potentially have the same, reused entityID) from each other.
2323
uniqueID int64
@@ -186,7 +186,7 @@ func convexHull(pointCloud []r3.Vector) quickhull.ConvexHull {
186186
// NewInferno creates a inferno and sets the Unique-ID.
187187
//
188188
// Intended for internal use only.
189-
func NewInferno(demoInfoProvider demoInfoProvider, entity st.IEntity) *Inferno {
189+
func NewInferno(demoInfoProvider demoInfoProvider, entity st.Entity) *Inferno {
190190
return &Inferno{
191191
Entity: entity,
192192
uniqueID: rand.Int63(),

pkg/demoinfocs/common/inferno_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import (
88
"github.com/stretchr/testify/assert"
99

1010
st "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/sendtables"
11+
stfake "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/sendtables/fake"
1112
)
1213

1314
func TestInferno_UniqueID(t *testing.T) {
14-
entity := new(st.Entity)
15+
entity := new(stfake.Entity)
1516
assert.NotEqual(t, NewInferno(nil, entity).UniqueID(), NewInferno(nil, entity).UniqueID(), "UniqueIDs of different infernos should be different")
1617
}
1718

pkg/demoinfocs/common/player.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Player struct {
2828
Inventory map[int]*Equipment // All weapons / equipment the player is currently carrying. See also Weapons().
2929
AmmoLeft [32]int // Ammo left for special weapons (e.g. grenades), index corresponds Equipment.AmmoType
3030
EntityID int // Usually the same as Entity.ID() but may be different between player death and re-spawn.
31-
Entity st.IEntity // May be nil between player-death and re-spawn
31+
Entity st.Entity // May be nil between player-death and re-spawn
3232
FlashDuration float32 // Blindness duration from the flashbang currently affecting the player (seconds)
3333
FlashTick int // In-game tick at which the player was last flashed
3434
TeamState *TeamState // When keeping the reference make sure you notice when the player changes teams
@@ -152,7 +152,7 @@ func (p *Player) IsSpottedBy(other *Player) bool {
152152
clientSlot := other.EntityID - 1
153153
bit := uint(clientSlot)
154154

155-
var mask st.IProperty
155+
var mask st.Property
156156
if bit < 32 {
157157
mask = p.Entity.Property("m_bSpottedByMask.000")
158158
} else {
@@ -343,7 +343,7 @@ type demoInfoProvider interface {
343343
IngameTick() int // current in-game tick, used for IsBlinded()
344344
TickRate() float64 // in-game tick rate, used for Player.IsBlinded()
345345
FindPlayerByHandle(handle int) *Player
346-
PlayerResourceEntity() st.IEntity
346+
PlayerResourceEntity() st.Entity
347347
}
348348

349349
// NewPlayer creates a *Player with an initialized equipment map.

pkg/demoinfocs/datatables.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (p *parser) bindBomb() {
8383

8484
// Track bomb when it is dropped on the ground or being held by a player
8585
scC4 := p.stParser.ServerClasses().FindByName("CC4")
86-
scC4.OnEntityCreated(func(bombEntity *st.Entity) {
86+
scC4.OnEntityCreated(func(bombEntity st.Entity) {
8787
bombEntity.OnPositionUpdate(func(pos r3.Vector) {
8888
// Bomb only has a position when not held by a player
8989
bomb.Carrier = nil
@@ -107,7 +107,7 @@ func (p *parser) bindBomb() {
107107

108108
// Track bomb when it has been planted
109109
scPlantedC4 := p.stParser.ServerClasses().FindByName("CPlantedC4")
110-
scPlantedC4.OnEntityCreated(func(bombEntity *st.Entity) {
110+
scPlantedC4.OnEntityCreated(func(bombEntity st.Entity) {
111111
// Player can't hold the bomb when it has been planted
112112
p.gameState.bomb.Carrier = nil
113113

@@ -116,7 +116,7 @@ func (p *parser) bindBomb() {
116116
}
117117

118118
func (p *parser) bindTeamStates() {
119-
p.stParser.ServerClasses().FindByName("CCSTeam").OnEntityCreated(func(entity *st.Entity) {
119+
p.stParser.ServerClasses().FindByName("CCSTeam").OnEntityCreated(func(entity st.Entity) {
120120
team := entity.PropertyValueMust("m_szTeamname").StringVal
121121

122122
var s *common.TeamState
@@ -155,12 +155,12 @@ func (p *parser) bindTeamStates() {
155155
}
156156

157157
func (p *parser) bindBombSites() {
158-
p.stParser.ServerClasses().FindByName("CCSPlayerResource").OnEntityCreated(func(playerResource *st.Entity) {
158+
p.stParser.ServerClasses().FindByName("CCSPlayerResource").OnEntityCreated(func(playerResource st.Entity) {
159159
playerResource.BindProperty("m_bombsiteCenterA", &p.bombsiteA.center, st.ValTypeVector)
160160
playerResource.BindProperty("m_bombsiteCenterB", &p.bombsiteB.center, st.ValTypeVector)
161161
})
162162

163-
p.stParser.ServerClasses().FindByName("CBaseTrigger").OnEntityCreated(func(baseTrigger *st.Entity) {
163+
p.stParser.ServerClasses().FindByName("CBaseTrigger").OnEntityCreated(func(baseTrigger st.Entity) {
164164
t := new(boundingBoxInformation)
165165
p.triggers[baseTrigger.ID()] = t
166166

@@ -170,11 +170,11 @@ func (p *parser) bindBombSites() {
170170
}
171171

172172
func (p *parser) bindPlayers() {
173-
p.stParser.ServerClasses().FindByName("CCSPlayer").OnEntityCreated(func(player *st.Entity) {
173+
p.stParser.ServerClasses().FindByName("CCSPlayer").OnEntityCreated(func(player st.Entity) {
174174
p.bindNewPlayer(player)
175175
})
176176

177-
p.stParser.ServerClasses().FindByName("CCSPlayerResource").OnEntityCreated(func(entity *st.Entity) {
177+
p.stParser.ServerClasses().FindByName("CCSPlayerResource").OnEntityCreated(func(entity st.Entity) {
178178
p.playerResourceEntity = entity
179179
})
180180
}
@@ -215,7 +215,7 @@ func (p *parser) getOrCreatePlayer(entityID int, rp *playerInfo) (isNew bool, pl
215215
return isNew, player
216216
}
217217

218-
func (p *parser) bindNewPlayer(playerEntity st.IEntity) {
218+
func (p *parser) bindNewPlayer(playerEntity st.Entity) {
219219
entityID := playerEntity.ID()
220220
rp := p.rawPlayers[entityID-1]
221221

@@ -289,7 +289,7 @@ func (p *parser) bindNewPlayer(playerEntity st.IEntity) {
289289
}
290290
}
291291

292-
func (p *parser) bindPlayerWeapons(playerEntity st.IEntity, pl *common.Player) {
292+
func (p *parser) bindPlayerWeapons(playerEntity st.Entity, pl *common.Player) {
293293
// Some demos have an additional prefix for player weapons weapon
294294
var wepPrefix string
295295
if playerEntity.Property(playerWeaponPrefix+"000") != nil {
@@ -345,7 +345,7 @@ func (p *parser) bindWeapons() {
345345
switch bc.Name() {
346346
case "CWeaponCSBase":
347347
sc2 := sc // Local copy for loop
348-
sc.OnEntityCreated(func(e *st.Entity) { p.bindWeapon(e, p.equipmentMapping[sc2]) })
348+
sc.OnEntityCreated(func(e st.Entity) { p.bindWeapon(e, p.equipmentMapping[sc2]) })
349349
case "CBaseGrenade": // Grenade that has been thrown by player.
350350
sc.OnEntityCreated(p.bindGrenadeProjectiles)
351351
case "CBaseCSGrenade":
@@ -360,7 +360,7 @@ func (p *parser) bindWeapons() {
360360

361361
// bindGrenadeProjectiles keeps track of the location of live grenades (parser.gameState.grenadeProjectiles), actively thrown by players.
362362
// It does NOT track the location of grenades lying on the ground, i.e. that were dropped by dead players.
363-
func (p *parser) bindGrenadeProjectiles(entity *st.Entity) {
363+
func (p *parser) bindGrenadeProjectiles(entity st.Entity) {
364364
entityID := entity.ID()
365365

366366
proj := common.NewGrenadeProjectile()
@@ -448,7 +448,7 @@ func (p *parser) nadeProjectileDestroyed(proj *common.GrenadeProjectile) {
448448
}
449449
}
450450

451-
func (p *parser) bindWeapon(entity *st.Entity, wepType common.EquipmentType) {
451+
func (p *parser) bindWeapon(entity st.Entity, wepType common.EquipmentType) {
452452
entityID := entity.ID()
453453

454454
eq, eqExists := p.gameState.weapons[entityID]
@@ -502,7 +502,7 @@ func (p *parser) bindWeapon(entity *st.Entity, wepType common.EquipmentType) {
502502
}
503503
}
504504

505-
func (p *parser) bindNewInferno(entity *st.Entity) {
505+
func (p *parser) bindNewInferno(entity st.Entity) {
506506
inf := common.NewInferno(p.demoInfoProvider, entity)
507507
p.gameState.infernos[entity.ID()] = inf
508508

@@ -540,7 +540,7 @@ func (p *parser) bindGameRules() {
540540
}
541541

542542
gameRules := p.ServerClasses().FindByName("CCSGameRulesProxy")
543-
gameRules.OnEntityCreated(func(entity *st.Entity) {
543+
gameRules.OnEntityCreated(func(entity st.Entity) {
544544
entity.Property(grPrefix("m_gamePhase")).OnUpdate(func(val st.PropertyValue) {
545545
oldGamePhase := p.gameState.gamePhase
546546
p.gameState.gamePhase = common.GamePhase(val.IntVal)

pkg/demoinfocs/datatables_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
common "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/common"
1010
events "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/events"
1111
st "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/sendtables"
12-
fakest "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/sendtables/fake"
12+
stfake "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/sendtables/fake"
1313
)
1414

1515
type DevNullReader struct {
@@ -89,8 +89,8 @@ func testPlayerSpotted(t *testing.T, propName string) {
8989
}
9090

9191
// TODO: Player interface so we don't have to mock all this
92-
spotted := new(fakest.Entity)
93-
spottedByProp0 := new(fakest.Property)
92+
spotted := new(stfake.Entity)
93+
spottedByProp0 := new(stfake.Property)
9494

9595
var spottedByUpdateHandler st.PropertyUpdateHandler
9696
spottedByProp0.On("OnUpdate", mock.Anything).Run(func(args mock.Arguments) {
@@ -122,14 +122,14 @@ func newParser() *parser {
122122
return p
123123
}
124124

125-
func fakePlayerEntity(id int) *fakest.Entity {
126-
entity := new(fakest.Entity)
125+
func fakePlayerEntity(id int) *stfake.Entity {
126+
entity := new(stfake.Entity)
127127
configurePlayerEntityMock(id, entity)
128128

129129
return entity
130130
}
131131

132-
func configurePlayerEntityMock(id int, entity *fakest.Entity) {
132+
func configurePlayerEntityMock(id int, entity *stfake.Entity) {
133133
entity.On("ID").Return(id)
134134

135135
var destroyCallback func()
@@ -138,7 +138,9 @@ func configurePlayerEntityMock(id int, entity *fakest.Entity) {
138138
})
139139

140140
entity.On("OnPositionUpdate", mock.Anything).Return()
141-
entity.On("Property", mock.Anything).Return(new(st.Property))
141+
prop := new(stfake.Property)
142+
prop.On("OnUpdate", mock.Anything).Return()
143+
entity.On("Property", mock.Anything).Return(prop)
142144
entity.On("BindProperty", mock.Anything, mock.Anything, mock.Anything)
143145
entity.On("Destroy").Run(func(mock.Arguments) {
144146
destroyCallback()

0 commit comments

Comments
 (0)