Skip to content

Commit c061257

Browse files
committed
sendtables: property lookup table (better performance)
1 parent dbf083c commit c061257

File tree

14 files changed

+105
-103
lines changed

14 files changed

+105
-103
lines changed

examples/entities/entities.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ func main() {
2121

2222
p.RegisterEventHandler(func(events.DataTablesParsed) {
2323
p.ServerClasses().FindByName("CWeaponAWP").OnEntityCreated(func(ent *st.Entity) {
24-
ent.FindProperty("m_hOwnerEntity").OnUpdate(func(val st.PropertyValue) {
24+
ent.Property("m_hOwnerEntity").OnUpdate(func(val st.PropertyValue) {
2525
x := p.GameState().Participants().FindByHandle(val.IntVal)
2626
if x != nil {
2727
var prev string
28-
prevHandle := ent.FindProperty("m_hPrevOwner").Value().IntVal
28+
prevHandle := ent.Property("m_hPrevOwner").Value().IntVal
2929
prevPlayer := p.GameState().Participants().FindByHandle(prevHandle)
3030
if prevPlayer != nil {
3131
if prevHandle != val.IntVal {

pkg/demoinfocs/common/common_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func entityWithProperty(propName string, value st.PropertyValue) *stfake.Entity
151151
prop := new(stfake.Property)
152152
prop.On("Value").Return(value)
153153

154-
entity.On("FindProperty", propName).Return(prop)
154+
entity.On("Property", propName).Return(prop)
155155
entity.On("PropertyValue", propName).Return(prop, true)
156156
entity.On("PropertyValueMust", propName).Return(value)
157157

pkg/demoinfocs/common/inferno.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (f Fires) ConvexHull3D() quickhull.ConvexHull {
176176
// Thrower returns the player who threw the fire grenade.
177177
// Could be nil if the player disconnected after throwing it.
178178
func (inf Inferno) Thrower() *Player {
179-
return inf.demoInfoProvider.FindPlayerByHandle(inf.Entity.FindProperty("m_hOwnerEntity").Value().IntVal)
179+
return inf.demoInfoProvider.FindPlayerByHandle(inf.Entity.Property("m_hOwnerEntity").Value().IntVal)
180180
}
181181

182182
func convexHull(pointCloud []r3.Vector) quickhull.ConvexHull {

pkg/demoinfocs/common/player.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (p *Player) IsAirborne() bool {
6868
return false
6969
}
7070

71-
groundEntityHandle := p.Entity.FindProperty("m_hGroundEntity").Value().IntVal
71+
groundEntityHandle := p.Entity.Property("m_hGroundEntity").Value().IntVal
7272

7373
return groundEntityHandle == invalidEntityHandle
7474
}
@@ -148,10 +148,10 @@ func (p *Player) IsSpottedBy(other *Player) bool {
148148

149149
var mask st.IProperty
150150
if bit < 32 {
151-
mask = p.Entity.FindProperty("m_bSpottedByMask.000")
151+
mask = p.Entity.Property("m_bSpottedByMask.000")
152152
} else {
153153
bit -= 32
154-
mask = p.Entity.FindProperty("m_bSpottedByMask.001")
154+
mask = p.Entity.Property("m_bSpottedByMask.001")
155155
}
156156

157157
return (mask.Value().IntVal & (1 << bit)) != 0
@@ -210,7 +210,7 @@ func (p *Player) ControlledBot() *Player {
210210
return nil
211211
}
212212

213-
botHandle := p.Entity.FindProperty("m_iControlledBotEntIndex").Value().IntVal
213+
botHandle := p.Entity.Property("m_iControlledBotEntIndex").Value().IntVal
214214

215215
return p.demoInfoProvider.FindPlayerByHandle(botHandle)
216216
}

pkg/demoinfocs/datatables.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ func (p *Parser) bindBomb() {
9191
bomb.LastOnGroundPosition = pos
9292
})
9393

94-
bombEntity.FindProperty("m_hOwner").OnUpdate(func(val st.PropertyValue) {
94+
bombEntity.Property("m_hOwner").OnUpdate(func(val st.PropertyValue) {
9595
bomb.Carrier = p.gameState.Participants().FindByHandle(val.IntVal)
9696
})
9797

98-
bombEntity.FindProperty("m_bStartedArming").OnUpdate(func(val st.PropertyValue) {
98+
bombEntity.Property("m_bStartedArming").OnUpdate(func(val st.PropertyValue) {
9999
if val.IntVal != 0 {
100100
p.gameState.currentPlanter = bomb.Carrier
101101
} else if p.gameState.currentPlanter != nil {
@@ -140,7 +140,7 @@ func (p *Parser) bindTeamStates() {
140140

141141
// Register updates
142142
var score int
143-
entity.FindProperty("m_scoreTotal").OnUpdate(func(val st.PropertyValue) {
143+
entity.Property("m_scoreTotal").OnUpdate(func(val st.PropertyValue) {
144144
oldScore := score
145145
score = val.IntVal
146146

@@ -187,7 +187,7 @@ func (p *Parser) bindPlayers() {
187187
plInfo.BindProperty("m_iAssists."+iStr, &p.additionalPlayerInfo[i2].Assists, st.ValTypeInt)
188188
plInfo.BindProperty("m_iMVPs."+iStr, &p.additionalPlayerInfo[i2].MVPs, st.ValTypeInt)
189189
plInfo.BindProperty("m_iTotalCashSpent."+iStr, &p.additionalPlayerInfo[i2].MoneySpentTotal, st.ValTypeInt)
190-
if prop := plInfo.FindProperty("m_iCashSpentThisRound." + iStr); prop != nil {
190+
if prop := plInfo.Property("m_iCashSpentThisRound." + iStr); prop != nil {
191191
prop.Bind(&p.additionalPlayerInfo[i2].MoneySpentThisRound, st.ValTypeInt)
192192
}
193193
}
@@ -254,12 +254,12 @@ func (p *Parser) bindNewPlayer(playerEntity st.IEntity) {
254254
})
255255

256256
// General info
257-
playerEntity.FindProperty("m_iTeamNum").OnUpdate(func(val st.PropertyValue) {
257+
playerEntity.Property("m_iTeamNum").OnUpdate(func(val st.PropertyValue) {
258258
pl.Team = common.Team(val.IntVal)
259259
pl.TeamState = p.gameState.Team(pl.Team)
260260
})
261261

262-
playerEntity.FindProperty("m_flFlashDuration").OnUpdate(func(val st.PropertyValue) {
262+
playerEntity.Property("m_flFlashDuration").OnUpdate(func(val st.PropertyValue) {
263263
if val.FloatVal == 0 {
264264
pl.FlashTick = 0
265265
} else {
@@ -272,7 +272,7 @@ func (p *Parser) bindNewPlayer(playerEntity st.IEntity) {
272272
p.bindPlayerWeapons(playerEntity, pl)
273273

274274
// Active weapon
275-
playerEntity.FindProperty("m_hActiveWeapon").OnUpdate(func(val st.PropertyValue) {
275+
playerEntity.Property("m_hActiveWeapon").OnUpdate(func(val st.PropertyValue) {
276276
pl.IsReloading = false
277277
})
278278

@@ -281,7 +281,7 @@ func (p *Parser) bindNewPlayer(playerEntity st.IEntity) {
281281
playerEntity.BindProperty("m_iAmmo."+fmt.Sprintf("%03d", i2), &pl.AmmoLeft[i2], st.ValTypeInt)
282282
}
283283

284-
playerEntity.FindProperty("m_bIsDefusing").OnUpdate(func(val st.PropertyValue) {
284+
playerEntity.Property("m_bIsDefusing").OnUpdate(func(val st.PropertyValue) {
285285
if p.gameState.currentDefuser == pl && pl.IsDefusing && val.IntVal == 0 {
286286
p.eventDispatcher.Dispatch(events.BombDefuseAborted{Player: pl})
287287
p.gameState.currentDefuser = nil
@@ -290,14 +290,14 @@ func (p *Parser) bindNewPlayer(playerEntity st.IEntity) {
290290
pl.IsDefusing = val.IntVal != 0
291291
})
292292

293-
spottedByMaskProp := playerEntity.FindProperty("m_bSpottedByMask.000")
293+
spottedByMaskProp := playerEntity.Property("m_bSpottedByMask.000")
294294
if spottedByMaskProp != nil {
295295
spottersChanged := func(val st.PropertyValue) {
296296
p.eventDispatcher.Dispatch(events.PlayerSpottersChanged{Spotted: pl})
297297
}
298298

299299
spottedByMaskProp.OnUpdate(spottersChanged)
300-
playerEntity.FindProperty("m_bSpottedByMask.001").OnUpdate(spottersChanged)
300+
playerEntity.Property("m_bSpottedByMask.001").OnUpdate(spottersChanged)
301301
}
302302

303303
if isNew && pl.SteamID != 0 {
@@ -308,7 +308,7 @@ func (p *Parser) bindNewPlayer(playerEntity st.IEntity) {
308308
func (p *Parser) bindPlayerWeapons(playerEntity st.IEntity, pl *common.Player) {
309309
// Some demos have an additional prefix for player weapons weapon
310310
var wepPrefix string
311-
if playerEntity.FindProperty(playerWeaponPrefix+"000") != nil {
311+
if playerEntity.Property(playerWeaponPrefix+"000") != nil {
312312
wepPrefix = playerWeaponPrefix
313313
} else {
314314
wepPrefix = playerWeaponPrePrefix + playerWeaponPrefix
@@ -318,7 +318,7 @@ func (p *Parser) bindPlayerWeapons(playerEntity st.IEntity, pl *common.Player) {
318318
var cache [maxWeapons]int
319319
for i := range cache {
320320
i2 := i // Copy for passing to handler
321-
playerEntity.FindProperty(wepPrefix + fmt.Sprintf("%03d", i)).OnUpdate(func(val st.PropertyValue) {
321+
playerEntity.Property(wepPrefix + fmt.Sprintf("%03d", i)).OnUpdate(func(val st.PropertyValue) {
322322
entityID := val.IntVal & entityHandleIndexMask
323323
if entityID != entityHandleIndexMask {
324324
if cache[i2] != 0 {
@@ -405,16 +405,16 @@ func (p *Parser) bindGrenadeProjectiles(entity *st.Entity) {
405405
p.nadeProjectileDestroyed(proj)
406406
})
407407

408-
entity.FindProperty("m_nModelIndex").OnUpdate(func(val st.PropertyValue) {
408+
entity.Property("m_nModelIndex").OnUpdate(func(val st.PropertyValue) {
409409
wep = p.grenadeModelIndices[val.IntVal]
410410
})
411411

412412
// @micvbang: not quite sure what the difference between Thrower and Owner is.
413-
entity.FindProperty("m_hThrower").OnUpdate(func(val st.PropertyValue) {
413+
entity.Property("m_hThrower").OnUpdate(func(val st.PropertyValue) {
414414
proj.Thrower = p.gameState.Participants().FindByHandle(val.IntVal)
415415
})
416416

417-
entity.FindProperty("m_hOwnerEntity").OnUpdate(func(val st.PropertyValue) {
417+
entity.Property("m_hOwnerEntity").OnUpdate(func(val st.PropertyValue) {
418418
proj.Owner = p.gameState.Participants().FindByHandle(val.IntVal)
419419
})
420420

@@ -424,7 +424,7 @@ func (p *Parser) bindGrenadeProjectiles(entity *st.Entity) {
424424

425425
// Some demos don't have this property as it seems
426426
// So we need to check for nil and can't send out bounce events if it's missing
427-
if bounceProp := entity.FindProperty("m_nBounces"); bounceProp != nil {
427+
if bounceProp := entity.Property("m_nBounces"); bounceProp != nil {
428428
bounceProp.OnUpdate(func(val st.PropertyValue) {
429429
if val.IntVal != 0 {
430430
p.eventDispatcher.Dispatch(events.GrenadeProjectileBounce{
@@ -485,14 +485,14 @@ func (p *Parser) bindWeapon(entity *st.Entity, wepType common.EquipmentType) {
485485
delete(p.gameState.weapons, entityID)
486486
})
487487

488-
entity.FindProperty("m_iClip1").OnUpdate(func(val st.PropertyValue) {
488+
entity.Property("m_iClip1").OnUpdate(func(val st.PropertyValue) {
489489
if eq.Owner != nil {
490490
eq.Owner.IsReloading = false
491491
}
492492
})
493493

494494
// Detect alternative weapons (P2k -> USP, M4A4 -> M4A1-S etc.)
495-
modelIndex := entity.FindProperty("m_nModelIndex").Value().IntVal
495+
modelIndex := entity.Property("m_nModelIndex").Value().IntVal
496496
eq.OriginalString = p.modelPreCache[modelIndex]
497497

498498
wepFix := func(defaultName, altName string, alt common.EquipmentType) {
@@ -557,7 +557,7 @@ func (p *Parser) bindGameRules() {
557557

558558
gameRules := p.ServerClasses().FindByName("CCSGameRulesProxy")
559559
gameRules.OnEntityCreated(func(entity *st.Entity) {
560-
entity.FindProperty(grPrefix("m_gamePhase")).OnUpdate(func(val st.PropertyValue) {
560+
entity.Property(grPrefix("m_gamePhase")).OnUpdate(func(val st.PropertyValue) {
561561
oldGamePhase := p.gameState.gamePhase
562562
p.gameState.gamePhase = common.GamePhase(val.IntVal)
563563

@@ -575,7 +575,7 @@ func (p *Parser) bindGameRules() {
575575
})
576576

577577
entity.BindProperty(grPrefix("m_totalRoundsPlayed"), &p.gameState.totalRoundsPlayed, st.ValTypeInt)
578-
entity.FindProperty(grPrefix("m_bWarmupPeriod")).OnUpdate(func(val st.PropertyValue) {
578+
entity.Property(grPrefix("m_bWarmupPeriod")).OnUpdate(func(val st.PropertyValue) {
579579
oldIsWarmupPeriod := p.gameState.isWarmupPeriod
580580
p.gameState.isWarmupPeriod = val.IntVal == 1
581581

@@ -585,7 +585,7 @@ func (p *Parser) bindGameRules() {
585585
})
586586
})
587587

588-
entity.FindProperty(grPrefix("m_bHasMatchStarted")).OnUpdate(func(val st.PropertyValue) {
588+
entity.Property(grPrefix("m_bHasMatchStarted")).OnUpdate(func(val st.PropertyValue) {
589589
oldMatchStarted := p.gameState.isMatchStarted
590590
p.gameState.isMatchStarted = val.IntVal == 1
591591

pkg/demoinfocs/datatables_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func testPlayerSpotted(t *testing.T, propName string) {
9797
spottedByUpdateHandler = args.Get(0).(st.PropertyUpdateHandler)
9898
})
9999

100-
spotted.On("FindProperty", propName).Return(spottedByProp0)
100+
spotted.On("Property", propName).Return(spottedByProp0)
101101
configurePlayerEntityMock(1, spotted)
102102
p.bindNewPlayer(spotted)
103103

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

140140
entity.On("OnPositionUpdate", mock.Anything).Return()
141-
entity.On("FindProperty", mock.Anything).Return(new(st.Property))
141+
entity.On("Property", mock.Anything).Return(new(st.Property))
142142
entity.On("BindProperty", mock.Anything, mock.Anything, mock.Anything)
143143
entity.On("Destroy").Run(func(mock.Arguments) {
144144
destroyCallback()

pkg/demoinfocs/game_state_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,10 @@ func TestParticipants_SpottersOf(t *testing.T) {
216216
entity.On("ID").Return(3)
217217
prop0 := new(stfake.Property)
218218
prop0.On("Value").Return(st.PropertyValue{IntVal: 1})
219-
entity.On("FindProperty", "m_bSpottedByMask.000").Return(prop0)
219+
entity.On("Property", "m_bSpottedByMask.000").Return(prop0)
220220
prop1 := new(stfake.Property)
221221
prop1.On("Value").Return(st.PropertyValue{IntVal: 1 << 2})
222-
entity.On("FindProperty", "m_bSpottedByMask.001").Return(prop1)
222+
entity.On("Property", "m_bSpottedByMask.001").Return(prop1)
223223
spotted.Entity = entity
224224

225225
ptcps := Participants{
@@ -245,11 +245,11 @@ func TestParticipants_SpottedBy(t *testing.T) {
245245
prop0 := new(stfake.Property)
246246
prop0.On("Value").Return(st.PropertyValue{IntVal: 1})
247247
spotted1Entity := new(stfake.Entity)
248-
spotted1Entity.On("FindProperty", "m_bSpottedByMask.000").Return(prop0)
248+
spotted1Entity.On("Property", "m_bSpottedByMask.000").Return(prop0)
249249
spotted1.Entity = spotted1Entity
250250
spotted2Entity := new(stfake.Entity)
251251
spotted2Entity.On("ID").Return(35)
252-
spotted2Entity.On("FindProperty", "m_bSpottedByMask.000").Return(prop0)
252+
spotted2Entity.On("Property", "m_bSpottedByMask.000").Return(prop0)
253253
spotted2.Entity = spotted2Entity
254254

255255
unSpotted := newPlayer()
@@ -260,11 +260,11 @@ func TestParticipants_SpottedBy(t *testing.T) {
260260
unSpottedProp := new(stfake.Property)
261261
unSpottedProp.On("Value").Return(st.PropertyValue{IntVal: 0})
262262
unSpottedEntity := new(stfake.Entity)
263-
unSpottedEntity.On("FindProperty", "m_bSpottedByMask.000").Return(unSpottedProp)
263+
unSpottedEntity.On("Property", "m_bSpottedByMask.000").Return(unSpottedProp)
264264
unSpotted.Entity = unSpottedEntity
265265

266266
spotterEntity := new(stfake.Entity)
267-
spotterEntity.On("FindProperty", "m_bSpottedByMask.000").Return(unSpottedProp)
267+
spotterEntity.On("Property", "m_bSpottedByMask.000").Return(unSpottedProp)
268268
spotter.Entity = spotterEntity
269269

270270
ptcps := Participants{

pkg/demoinfocs/parser_interface.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,14 @@ type IParser interface {
4848
// CurrentTime returns the time elapsed since the start of the demo
4949
CurrentTime() time.Duration
5050
// TickRate returns the tick-rate the server ran on during the game.
51+
//
52+
// Returns tick rate based on CSVCMsg_ServerInfo if possible.
53+
// Otherwise returns tick rate based on demo header or -1 if the header info isn't available.
5154
TickRate() float64
5255
// TickTime returns the time a single tick takes in seconds.
56+
//
57+
// Returns tick time based on CSVCMsg_ServerInfo if possible.
58+
// Otherwise returns tick time based on demo header or -1 if the header info isn't available.
5359
TickTime() time.Duration
5460
// Progress returns the parsing progress from 0 to 1.
5561
// Where 0 means nothing has been parsed yet and 1 means the demo has been parsed to the end.

0 commit comments

Comments
 (0)