Skip to content

Commit

Permalink
Merge pull request #423 from markus-wa/fix-hostage-cs2
Browse files Browse the repository at this point in the history
fix: possible panic hostage.Leader() with CS2 demos
  • Loading branch information
markus-wa authored Sep 30, 2023
2 parents 50a280a + f80018e commit f780c66
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/demoinfocs/common/hostage.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (hostage *Hostage) Health() int {
// Leader returns the possible player leading the hostage.
// Returns nil if the hostage is not following a player.
func (hostage *Hostage) Leader() *Player {
if hostage.demoInfoProvider.IsSource2() {
return hostage.demoInfoProvider.FindPlayerByPawnHandle(getUInt64(hostage.Entity, "m_leader"))
}

return hostage.demoInfoProvider.FindPlayerByHandle(getInt(hostage.Entity, "m_leader"))
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/demoinfocs/common/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,15 @@ func (p *Player) LastPlaceName() string {
return getString(p.Entity, "m_szLastPlaceName")
}

// IsGrabbingHostage returns true if the player is currently grabbing a hostage.
func (p *Player) IsGrabbingHostage() bool {
if p.demoInfoProvider.IsSource2() {
return getBool(p.PlayerPawnEntity(), "m_bIsGrabbingHostage")
}

return getBool(p.Entity, "m_bIsGrabbingHostage")
}

type demoInfoProvider interface {
IngameTick() int // current in-game tick, used for IsBlinded()
TickRate() float64 // in-game tick rate, used for Player.IsBlinded()
Expand Down
12 changes: 12 additions & 0 deletions pkg/demoinfocs/common/player_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,18 @@ func TestPlayer_CompetitiveWins(t *testing.T) {
assert.Equal(t, 190, pl.CompetitiveWins())
}

func TestPlayer_IsGrabbingHostage(t *testing.T) {
pl := playerWithProperty("m_bIsGrabbingHostage", st.PropertyValue{IntVal: 0})
pl.demoInfoProvider = s1DemoInfoProvider

assert.False(t, pl.IsGrabbingHostage())

pl = playerWithProperty("m_bIsGrabbingHostage", st.PropertyValue{IntVal: 1})
pl.demoInfoProvider = s1DemoInfoProvider

assert.True(t, pl.IsGrabbingHostage())
}

func newPlayer(tick int) *Player {
return NewPlayer(mockDemoInfoProvider(128, tick))
}
Expand Down

0 comments on commit f780c66

Please sign in to comment.