Skip to content

Commit f780c66

Browse files
authored
Merge pull request #423 from markus-wa/fix-hostage-cs2
fix: possible panic hostage.Leader() with CS2 demos
2 parents 50a280a + f80018e commit f780c66

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

pkg/demoinfocs/common/hostage.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ func (hostage *Hostage) Health() int {
5353
// Leader returns the possible player leading the hostage.
5454
// Returns nil if the hostage is not following a player.
5555
func (hostage *Hostage) Leader() *Player {
56+
if hostage.demoInfoProvider.IsSource2() {
57+
return hostage.demoInfoProvider.FindPlayerByPawnHandle(getUInt64(hostage.Entity, "m_leader"))
58+
}
59+
5660
return hostage.demoInfoProvider.FindPlayerByHandle(getInt(hostage.Entity, "m_leader"))
5761
}
5862

pkg/demoinfocs/common/player.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,15 @@ func (p *Player) LastPlaceName() string {
774774
return getString(p.Entity, "m_szLastPlaceName")
775775
}
776776

777+
// IsGrabbingHostage returns true if the player is currently grabbing a hostage.
778+
func (p *Player) IsGrabbingHostage() bool {
779+
if p.demoInfoProvider.IsSource2() {
780+
return getBool(p.PlayerPawnEntity(), "m_bIsGrabbingHostage")
781+
}
782+
783+
return getBool(p.Entity, "m_bIsGrabbingHostage")
784+
}
785+
777786
type demoInfoProvider interface {
778787
IngameTick() int // current in-game tick, used for IsBlinded()
779788
TickRate() float64 // in-game tick rate, used for Player.IsBlinded()

pkg/demoinfocs/common/player_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,18 @@ func TestPlayer_CompetitiveWins(t *testing.T) {
580580
assert.Equal(t, 190, pl.CompetitiveWins())
581581
}
582582

583+
func TestPlayer_IsGrabbingHostage(t *testing.T) {
584+
pl := playerWithProperty("m_bIsGrabbingHostage", st.PropertyValue{IntVal: 0})
585+
pl.demoInfoProvider = s1DemoInfoProvider
586+
587+
assert.False(t, pl.IsGrabbingHostage())
588+
589+
pl = playerWithProperty("m_bIsGrabbingHostage", st.PropertyValue{IntVal: 1})
590+
pl.demoInfoProvider = s1DemoInfoProvider
591+
592+
assert.True(t, pl.IsGrabbingHostage())
593+
}
594+
583595
func newPlayer(tick int) *Player {
584596
return NewPlayer(mockDemoInfoProvider(128, tick))
585597
}

0 commit comments

Comments
 (0)