Skip to content

Commit

Permalink
Merge pull request #491 from markus-wa/fix-bot-takeover
Browse files Browse the repository at this point in the history
fix: panic on bot_takeover events
  • Loading branch information
markus-wa authored Jan 8, 2024
2 parents 0ac0c4b + 60a3075 commit 7b0c35e
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 14 deletions.
6 changes: 3 additions & 3 deletions pkg/demoinfocs/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ type fakeProp struct {
type demoInfoProviderMock struct {
tickRate float64
ingameTick int
playersByHandle map[int]*Player
playersByHandle map[uint64]*Player
entitiesByHandle map[uint64]st.Entity
playerResourceEntity st.Entity
equipment *Equipment
Expand All @@ -235,12 +235,12 @@ func (p demoInfoProviderMock) IngameTick() int {
return p.ingameTick
}

func (p demoInfoProviderMock) FindPlayerByHandle(handle int) *Player {
func (p demoInfoProviderMock) FindPlayerByHandle(handle uint64) *Player {
return p.playersByHandle[handle]
}

func (p demoInfoProviderMock) FindPlayerByPawnHandle(handle uint64) *Player {
return p.playersByHandle[int(handle)]
return p.playersByHandle[handle]
}

func (p demoInfoProviderMock) PlayerResourceEntity() st.Entity {
Expand Down
2 changes: 1 addition & 1 deletion pkg/demoinfocs/common/hostage.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (hostage *Hostage) Leader() *Player {
return hostage.demoInfoProvider.FindPlayerByPawnHandle(getUInt64(hostage.Entity, "m_leader"))
}

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

// NewHostage creates a hostage.
Expand Down
2 changes: 1 addition & 1 deletion pkg/demoinfocs/common/hostage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestHostage_Leader(t *testing.T) {
player := new(Player)
player.EntityID = 10
provider := demoInfoProviderMock{
playersByHandle: map[int]*Player{10: player},
playersByHandle: map[uint64]*Player{10: player},
}
hostage := hostageWithProperty("m_leader", st.PropertyValue{IntVal: 10}, provider)

Expand Down
2 changes: 1 addition & 1 deletion pkg/demoinfocs/common/inferno.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (inf *Inferno) Thrower() *Player {
return inf.demoInfoProvider.FindPlayerByPawnHandle(handleProp.Handle())
}

return inf.demoInfoProvider.FindPlayerByHandle(handleProp.Int())
return inf.demoInfoProvider.FindPlayerByHandle(uint64(handleProp.Int()))
}

// Fires returns all fires (past + present).
Expand Down
2 changes: 1 addition & 1 deletion pkg/demoinfocs/common/inferno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestInferno_Thrower(t *testing.T) {

player := new(Player)
provider := demoInfoProviderMock{
playersByHandle: map[int]*Player{1: player},
playersByHandle: map[uint64]*Player{1: player},
}

assert.Equal(t, player, NewInferno(provider, entity, nil).Thrower())
Expand Down
12 changes: 9 additions & 3 deletions pkg/demoinfocs/common/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,15 @@ func (p *Player) ControlledBot() *Player {
return nil
}

botHandle := p.Entity.Property("m_iControlledBotEntIndex").Value().IntVal
if p.demoInfoProvider.IsSource2() {
controllerHandler := p.Entity.Property("m_hOriginalControllerOfCurrentPawn").Value().S2UInt64()

return p.demoInfoProvider.FindPlayerByHandle(controllerHandler)
}

botHandle := p.Entity.Property("m_iControlledBotEntIndex").Value().Int()

return p.demoInfoProvider.FindPlayerByHandle(botHandle)
return p.demoInfoProvider.FindPlayerByHandle(uint64(botHandle))
}

// Health returns the player's health points, normally 0-100.
Expand Down Expand Up @@ -796,7 +802,7 @@ func (p *Player) IsGrabbingHostage() bool {
type demoInfoProvider interface {
IngameTick() int // current in-game tick, used for IsBlinded()
TickRate() float64 // in-game tick rate, used for Player.IsBlinded()
FindPlayerByHandle(handle int) *Player
FindPlayerByHandle(handle uint64) *Player
FindPlayerByPawnHandle(handle uint64) *Player
PlayerResourceEntity() st.Entity
FindWeaponByEntityID(id int) *Equipment
Expand Down
25 changes: 24 additions & 1 deletion pkg/demoinfocs/common/player_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func TestPlayer_ControlledBot(t *testing.T) {
IsBot: true,
}
demoInfoProvider := &demoInfoProviderMock{
playersByHandle: map[int]*Player{
playersByHandle: map[uint64]*Player{
12: dave,
},
}
Expand All @@ -347,6 +347,29 @@ func TestPlayer_ControlledBot(t *testing.T) {
assert.Same(t, dave, pl.ControlledBot())
}

func TestPlayer_ControlledBotS2(t *testing.T) {
dave := &Player{
Name: "Dave",
IsBot: true,
}
demoInfoProvider := &demoInfoProviderMock{
playersByHandle: map[uint64]*Player{
12: dave,
},
isSource2: true,
}

pl := playerWithProperty("m_hOriginalControllerOfCurrentPawn", st.PropertyValue{Any: uint64(0)})
pl.demoInfoProvider = demoInfoProvider

assert.Nil(t, pl.ControlledBot())

pl = playerWithProperty("m_hOriginalControllerOfCurrentPawn", st.PropertyValue{Any: uint64(12)})
pl.demoInfoProvider = demoInfoProvider

assert.Same(t, dave, pl.ControlledBot())
}

/*
TODO: fix for CS2
func TestPlayer_Armor(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/demoinfocs/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (p demoInfoProviderMock) TickRate() float64 {
return 128
}

func (p demoInfoProviderMock) FindPlayerByHandle(int) *common.Player {
func (p demoInfoProviderMock) FindPlayerByHandle(uint64) *common.Player {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/demoinfocs/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@ func (p demoInfoProvider) TickRate() float64 {
return p.parser.TickRate()
}

func (p demoInfoProvider) FindPlayerByHandle(handle int) *common.Player {
return p.parser.gameState.Participants().FindByHandle(handle)
func (p demoInfoProvider) FindPlayerByHandle(handle uint64) *common.Player {
return p.parser.gameState.Participants().FindByHandle64(handle)
}

func (p demoInfoProvider) FindPlayerByPawnHandle(handle uint64) *common.Player {
Expand Down

0 comments on commit 7b0c35e

Please sign in to comment.