Skip to content

Commit c95ea92

Browse files
committed
refactor: replace all occurences of FindProperty() with FindPropertyI()
1 parent 6019894 commit c95ea92

File tree

4 files changed

+40
-41
lines changed

4 files changed

+40
-41
lines changed

datatables.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ func (p *Parser) bindBomb() {
9595
// Track bomb when it is being held by a player
9696
scPlayerC4 := p.stParser.ServerClasses().FindByName("CC4")
9797
scPlayerC4.OnEntityCreated(func(bombEntity *st.Entity) {
98-
bombEntity.FindProperty("m_hOwner").OnUpdate(func(val st.PropertyValue) {
98+
bombEntity.FindPropertyI("m_hOwner").OnUpdate(func(val st.PropertyValue) {
9999
bomb.Carrier = p.gameState.Participants().FindByHandle(val.IntVal)
100100
})
101101
})
102102
}
103103

104104
func (p *Parser) bindTeamStates() {
105105
p.stParser.ServerClasses().FindByName("CCSTeam").OnEntityCreated(func(entity *st.Entity) {
106-
team := entity.FindProperty("m_szTeamname").Value().StringVal
106+
team := entity.FindPropertyI("m_szTeamname").Value().StringVal
107107

108108
var s *common.TeamState
109109

@@ -127,7 +127,7 @@ func (p *Parser) bindTeamStates() {
127127
entity.BindProperty("m_szClanTeamname", &s.ClanName, st.ValTypeString)
128128
entity.BindProperty("m_szTeamFlagImage", &s.Flag, st.ValTypeString)
129129

130-
entity.FindProperty("m_scoreTotal").OnUpdate(func(val st.PropertyValue) {
130+
entity.FindPropertyI("m_scoreTotal").OnUpdate(func(val st.PropertyValue) {
131131
oldScore := s.Score
132132
s.Score = val.IntVal
133133

@@ -349,16 +349,16 @@ func (p *Parser) bindGrenadeProjectiles(entity *st.Entity) {
349349
p.nadeProjectileDestroyed(proj)
350350
})
351351

352-
entity.FindProperty("m_nModelIndex").OnUpdate(func(val st.PropertyValue) {
352+
entity.FindPropertyI("m_nModelIndex").OnUpdate(func(val st.PropertyValue) {
353353
proj.Weapon = p.grenadeModelIndices[val.IntVal]
354354
})
355355

356356
// @micvbang: not quite sure what the difference between Thrower and Owner is.
357-
entity.FindProperty("m_hThrower").OnUpdate(func(val st.PropertyValue) {
357+
entity.FindPropertyI("m_hThrower").OnUpdate(func(val st.PropertyValue) {
358358
proj.Thrower = p.gameState.Participants().FindByHandle(val.IntVal)
359359
})
360360

361-
entity.FindProperty("m_hOwnerEntity").OnUpdate(func(val st.PropertyValue) {
361+
entity.FindPropertyI("m_hOwnerEntity").OnUpdate(func(val st.PropertyValue) {
362362
proj.Owner = p.gameState.Participants().FindByHandle(val.IntVal)
363363
})
364364

@@ -370,8 +370,7 @@ func (p *Parser) bindGrenadeProjectiles(entity *st.Entity) {
370370

371371
// Some demos don't have this property as it seems
372372
// So we need to check for nil and can't send out bounce events if it's missing
373-
bounceProp := entity.FindProperty("m_nBounces")
374-
if bounceProp != nil {
373+
if bounceProp := entity.FindPropertyI("m_nBounces"); bounceProp != nil {
375374
bounceProp.OnUpdate(func(val st.PropertyValue) {
376375
if val.IntVal != 0 {
377376
p.eventDispatcher.Dispatch(events.GrenadeProjectileBounce{
@@ -405,19 +404,19 @@ func (p *Parser) bindWeapon(entity *st.Entity, wepType common.EquipmentElement)
405404
eq.EntityID = entityID
406405
eq.AmmoInMagazine = -1
407406

408-
entity.FindProperty("m_iClip1").OnUpdate(func(val st.PropertyValue) {
407+
entity.FindPropertyI("m_iClip1").OnUpdate(func(val st.PropertyValue) {
409408
eq.AmmoInMagazine = val.IntVal - 1
410409
})
411410

412411
// Only weapons with scopes have m_zoomLevel property
413-
if entity.FindProperty("m_zoomLevel") != nil {
414-
entity.BindProperty("m_zoomLevel", &eq.ZoomLevel, st.ValTypeInt)
412+
if zoomLvlProp := entity.FindPropertyI("m_zoomLevel"); zoomLvlProp != nil {
413+
zoomLvlProp.Bind(&eq.ZoomLevel, st.ValTypeInt)
415414
}
416415

417-
eq.AmmoType = entity.FindProperty("LocalWeaponData.m_iPrimaryAmmoType").Value().IntVal
416+
eq.AmmoType = entity.FindPropertyI("LocalWeaponData.m_iPrimaryAmmoType").Value().IntVal
418417

419418
// Detect alternative weapons (P2k -> USP, M4A4 -> M4A1-S etc.)
420-
modelIndex := entity.FindProperty("m_nModelIndex").Value().IntVal
419+
modelIndex := entity.FindPropertyI("m_nModelIndex").Value().IntVal
421420
eq.OriginalString = p.modelPreCache[modelIndex]
422421

423422
wepFix := func(defaultName, altName string, alt common.EquipmentElement) {
@@ -459,13 +458,13 @@ func (p *Parser) bindNewInferno(entity *st.Entity) {
459458

460459
origin := entity.Position()
461460
nFires := 0
462-
entity.FindProperty("m_fireCount").OnUpdate(func(val st.PropertyValue) {
461+
entity.FindPropertyI("m_fireCount").OnUpdate(func(val st.PropertyValue) {
463462
for i := nFires; i < val.IntVal; i++ {
464463
iStr := fmt.Sprintf("%03d", i)
465464
offset := r3.Vector{
466-
X: float64(entity.FindProperty("m_fireXDelta." + iStr).Value().IntVal),
467-
Y: float64(entity.FindProperty("m_fireYDelta." + iStr).Value().IntVal),
468-
Z: float64(entity.FindProperty("m_fireZDelta." + iStr).Value().IntVal),
465+
X: float64(entity.FindPropertyI("m_fireXDelta." + iStr).Value().IntVal),
466+
Y: float64(entity.FindPropertyI("m_fireYDelta." + iStr).Value().IntVal),
467+
Z: float64(entity.FindPropertyI("m_fireZDelta." + iStr).Value().IntVal),
469468
}
470469

471470
fire := &common.Fire{Vector: origin.Add(offset), IsBurning: true}
@@ -499,7 +498,7 @@ func (p *Parser) bindGameRules() {
499498

500499
gameRules := p.ServerClasses().FindByName("CCSGameRulesProxy")
501500
gameRules.OnEntityCreated(func(entity *st.Entity) {
502-
entity.FindProperty(grPrefix("m_gamePhase")).OnUpdate(func(val st.PropertyValue) {
501+
entity.FindPropertyI(grPrefix("m_gamePhase")).OnUpdate(func(val st.PropertyValue) {
503502
oldGamePhase := p.gameState.gamePhase
504503
p.gameState.gamePhase = common.GamePhase(val.IntVal)
505504

@@ -517,7 +516,7 @@ func (p *Parser) bindGameRules() {
517516
})
518517

519518
entity.BindProperty(grPrefix("m_totalRoundsPlayed"), &p.gameState.totalRoundsPlayed, st.ValTypeInt)
520-
entity.FindProperty(grPrefix("m_bWarmupPeriod")).OnUpdate(func(val st.PropertyValue) {
519+
entity.FindPropertyI(grPrefix("m_bWarmupPeriod")).OnUpdate(func(val st.PropertyValue) {
521520
oldIsWarmupPeriod := p.gameState.isWarmupPeriod
522521
p.gameState.isWarmupPeriod = val.IntVal == 1
523522

@@ -527,7 +526,7 @@ func (p *Parser) bindGameRules() {
527526
})
528527
})
529528

530-
entity.FindProperty(grPrefix("m_bHasMatchStarted")).OnUpdate(func(val st.PropertyValue) {
529+
entity.FindPropertyI(grPrefix("m_bHasMatchStarted")).OnUpdate(func(val st.PropertyValue) {
531530
oldMatchStarted := p.gameState.isMatchStarted
532531
p.gameState.isMatchStarted = val.IntVal == 1
533532

examples/entities/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,11 +815,11 @@ p.RegisterEventHandler(func(events.DataTablesParsed) {
815815
// DataTablesParsed has been sent out, register entity-creation handler
816816
p.ServerClasses().FindByName("CWeaponAWP").OnEntityCreated(func(entity *st.Entity) {
817817
// Register update-hander on the owning entity (player who's holding the AWP)
818-
entity.FindProperty("m_hOwnerEntity").OnUpdate(func(val st.PropertyValue) {
818+
entity.FindPropertyI("m_hOwnerEntity").OnUpdate(func(val st.PropertyValue) {
819819
owner := p.GameState().Participants().FindByHandle(val.IntVal)
820820
if owner != nil {
821821
var prev string
822-
prevHandle := entity.FindProperty("m_hPrevOwner").Value().IntVal
822+
prevHandle := entity.FindPropertyI("m_hPrevOwner").Value().IntVal
823823
prevPlayer := p.GameState().Participants().FindByHandle(prevHandle)
824824
if prevPlayer != nil {
825825
if prevHandle != val.IntVal {

examples/entities/entities.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"os"
77

88
dem "github.com/markus-wa/demoinfocs-golang"
9-
events "github.com/markus-wa/demoinfocs-golang/events"
9+
"github.com/markus-wa/demoinfocs-golang/events"
1010
ex "github.com/markus-wa/demoinfocs-golang/examples"
1111
st "github.com/markus-wa/demoinfocs-golang/sendtables"
1212
)
@@ -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.FindPropertyI("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.FindPropertyI("m_hPrevOwner").Value().IntVal
2929
prevPlayer := p.GameState().Participants().FindByHandle(prevHandle)
3030
if prevPlayer != nil {
3131
if prevHandle != val.IntVal {

sendtables/entity.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ func (e *Entity) FindPropertyI(name string) IProperty {
7373
return prop
7474
}
7575

76-
// BindProperty combines FindProperty() & Property.Bind() into one.
76+
// BindProperty combines FindPropertyI() & Property.Bind() into one.
7777
// Essentially binds a property's value to a pointer.
7878
// See the docs of the two individual functions for more info.
7979
func (e *Entity) BindProperty(name string, variable interface{}, valueType PropertyValueType) {
80-
e.FindProperty(name).Bind(variable, valueType)
80+
e.FindPropertyI(name).Bind(variable, valueType)
8181
}
8282

8383
var updatedPropIndicesPool = sync.Pool{
@@ -189,13 +189,13 @@ const (
189189
)
190190

191191
// Sets up the Entity.Position() function
192-
// Necessary because FindProperty() is fairly slow
192+
// Necessary because FindPropertyI() is fairly slow
193193
// This way we only need to find the necessary properties once
194194
func (e *Entity) initialize() {
195195
// Player positions are calculated differently
196196
if e.isPlayer() {
197-
xyProp := e.FindProperty(propVecOriginPlayerXY)
198-
zProp := e.FindProperty(propVecOriginPlayerZ)
197+
xyProp := e.FindPropertyI(propVecOriginPlayerXY)
198+
zProp := e.FindPropertyI(propVecOriginPlayerZ)
199199

200200
e.position = func() r3.Vector {
201201
xy := xyProp.Value().VectorVal
@@ -207,11 +207,11 @@ func (e *Entity) initialize() {
207207
}
208208
}
209209
} else {
210-
cellBitsProp := e.FindProperty(propCellBits)
211-
cellXProp := e.FindProperty(propCellX)
212-
cellYProp := e.FindProperty(propCellY)
213-
cellZProp := e.FindProperty(propCellZ)
214-
offsetProp := e.FindProperty(propVecOrigin)
210+
cellBitsProp := e.FindPropertyI(propCellBits)
211+
cellXProp := e.FindPropertyI(propCellX)
212+
cellYProp := e.FindPropertyI(propCellY)
213+
cellZProp := e.FindPropertyI(propCellZ)
214+
offsetProp := e.FindPropertyI(propVecOrigin)
215215

216216
e.position = func() r3.Vector {
217217
cellWidth := 1 << uint(cellBitsProp.Value().IntVal)
@@ -253,13 +253,13 @@ func (e *Entity) OnPositionUpdate(h func(pos r3.Vector)) {
253253
}
254254

255255
if e.isPlayer() {
256-
e.FindProperty(propVecOriginPlayerXY).OnUpdate(firePosUpdate)
257-
e.FindProperty(propVecOriginPlayerZ).OnUpdate(firePosUpdate)
256+
e.FindPropertyI(propVecOriginPlayerXY).OnUpdate(firePosUpdate)
257+
e.FindPropertyI(propVecOriginPlayerZ).OnUpdate(firePosUpdate)
258258
} else {
259-
e.FindProperty(propCellX).OnUpdate(firePosUpdate)
260-
e.FindProperty(propCellY).OnUpdate(firePosUpdate)
261-
e.FindProperty(propCellZ).OnUpdate(firePosUpdate)
262-
e.FindProperty(propVecOrigin).OnUpdate(firePosUpdate)
259+
e.FindPropertyI(propCellX).OnUpdate(firePosUpdate)
260+
e.FindPropertyI(propCellY).OnUpdate(firePosUpdate)
261+
e.FindPropertyI(propCellZ).OnUpdate(firePosUpdate)
262+
e.FindPropertyI(propVecOrigin).OnUpdate(firePosUpdate)
263263
}
264264
}
265265

0 commit comments

Comments
 (0)