Skip to content

Commit 2c97ec8

Browse files
committed
S2 progress: parsing with player entities successful
1 parent 845c1da commit 2c97ec8

File tree

4 files changed

+95
-49
lines changed

4 files changed

+95
-49
lines changed

pkg/demoinfocs/common/player.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ type Player struct {
3535
IsUnknown bool // Used to identify unknown/broken players. see https://github.com/markus-wa/demoinfocs-golang/issues/162
3636
}
3737

38+
func (p *Player) PlayerPawnEntity() st.Entity {
39+
return p.demoInfoProvider.FindEntityByHandle(p.Entity.PropertyValueMust("m_hPlayerPawn").Handle())
40+
}
41+
42+
func (p *Player) GetTeam() Team {
43+
return Team(p.PlayerPawnEntity().PropertyValueMust("m_iTeamNum").S2UInt64())
44+
}
45+
46+
func (p *Player) GetFlashDuration() float32 {
47+
return p.PlayerPawnEntity().PropertyValueMust("m_flFlashDuration").Float()
48+
}
49+
3850
// String returns the player's name.
3951
// Implements fmt.Stringer.
4052
func (p *Player) String() string {
@@ -501,6 +513,7 @@ type demoInfoProvider interface {
501513
FindPlayerByHandle(handle int) *Player
502514
PlayerResourceEntity() st.Entity
503515
FindWeaponByEntityID(id int) *Equipment
516+
FindEntityByHandle(handle uint64) st.Entity
504517
}
505518

506519
// NewPlayer creates a *Player with an initialized equipment map.

pkg/demoinfocs/datatables.go

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ func (p *parser) bindBombSites() {
191191

192192
func (p *parser) bindPlayers() {
193193
if p.isSource2() {
194+
p.stParser.ServerClasses().FindByName("CCSPlayerController").OnEntityCreated(func(player st.Entity) {
195+
p.bindNewPlayerControllerS2(player)
196+
})
194197
p.stParser.ServerClasses().FindByName("CCSPlayerPawn").OnEntityCreated(func(player st.Entity) {
195198
p.bindNewPlayerPawnS2(player)
196199
})
@@ -323,11 +326,7 @@ func (p *parser) bindNewPlayerS1(playerEntity st.Entity) {
323326
}
324327
}
325328

326-
func (p *parser) bindNewPlayerPawnS2(pawnEntity st.Entity) {
327-
controllerHandle := pawnEntity.Property("m_hController").Value().Handle()
328-
329-
controllerEntity := p.gameState.EntityByHandle(controllerHandle)
330-
329+
func (p *parser) bindNewPlayerControllerS2(controllerEntity st.Entity) {
331330
controllerEntityID := controllerEntity.ID()
332331

333332
rp := p.rawPlayers[controllerEntityID-1]
@@ -350,73 +349,78 @@ func (p *parser) bindNewPlayerPawnS2(pawnEntity st.Entity) {
350349
}
351350
})
352351

353-
// General info
354-
pawnEntity.Property("m_iTeamNum").OnUpdate(func(val st.PropertyValue) {
355-
pl.Team = common.Team(val.IntVal)
356-
pl.TeamState = p.gameState.Team(pl.Team)
357-
})
352+
if isNew {
353+
if pl.SteamID64 != 0 {
354+
p.eventDispatcher.Dispatch(events.PlayerConnect{Player: pl})
355+
} else {
356+
p.eventDispatcher.Dispatch(events.BotConnect{Player: pl})
357+
}
358+
}
359+
}
360+
361+
func (p *parser) bindNewPlayerPawnS2(pawnEntity st.Entity) {
362+
player := func() *common.Player {
363+
return p.gameState.Participants().FindByHandle64(pawnEntity.PropertyValueMust("m_hController").Handle())
364+
}
358365

359366
pawnEntity.Property("m_flFlashDuration").OnUpdate(func(val st.PropertyValue) {
360-
if val.FloatVal == 0 {
367+
pl := player()
368+
if pl == nil {
369+
return
370+
}
371+
372+
if val.Float() == 0 {
361373
pl.FlashTick = 0
362374
} else {
363375
pl.FlashTick = p.gameState.ingameTick
364376
}
365377

366-
pl.FlashDuration = val.FloatVal
378+
pl.FlashDuration = val.Float()
367379
})
368380

369-
if !p.isSource2() {
370-
p.bindPlayerWeapons(controllerEntity, pl) // FIXME: make weapons work for S2
371-
372-
for i := 0; i < 32; i++ {
373-
i2 := i // Copy so it stays the same
374-
controllerEntity.BindProperty("m_iAmmo."+fmt.Sprintf("%03d", i2), &pl.AmmoLeft[i2], st.ValTypeInt)
381+
//p.bindPlayerWeapons(controllerEntity, pl) // FIXME: make weapons work for S2
382+
//
383+
//for i := 0; i < 32; i++ {
384+
// i2 := i // Copy so it stays the same
385+
// controllerEntity.BindProperty("m_iAmmo."+fmt.Sprintf("%03d", i2), &pl.AmmoLeft[i2], st.ValTypeInt)
386+
//}
387+
388+
pawnEntity.Property("m_pWeaponServices.m_hActiveWeapon").OnUpdate(func(val st.PropertyValue) {
389+
pl := player()
390+
if pl == nil {
391+
return
375392
}
376-
}
377-
378-
var (
379-
activeWep st.Property
380-
spottedByPrefix string
381-
)
382393

383-
if p.isSource2() {
384-
activeWep = pawnEntity.Property("m_pWeaponServices.m_hActiveWeapon")
385-
spottedByPrefix = "m_bSpottedByMask.000"
386-
} else {
387-
activeWep = pawnEntity.Property("m_hActiveWeapon")
388-
spottedByPrefix = "m_bSpottedByMask.00"
389-
}
390-
391-
activeWep.OnUpdate(func(val st.PropertyValue) {
392394
pl.IsReloading = false
393395
})
394396

395397
pawnEntity.Property("m_bIsDefusing").OnUpdate(func(val st.PropertyValue) {
396-
if p.gameState.currentDefuser == pl && pl.IsDefusing && val.IntVal == 0 {
398+
pl := player()
399+
if pl == nil {
400+
return
401+
}
402+
403+
if p.gameState.currentDefuser == pl && pl.IsDefusing && !val.BoolVal() {
397404
p.eventDispatcher.Dispatch(events.BombDefuseAborted{Player: pl})
398405
p.gameState.currentDefuser = nil
399406
}
400407

401-
pl.IsDefusing = val.IntVal != 0
408+
pl.IsDefusing = val.BoolVal()
402409
})
403410

404-
spottedByMaskProp := pawnEntity.Property(spottedByPrefix + "0")
411+
spottedByMaskProp := pawnEntity.Property("m_bSpottedByMask.0000")
405412
if spottedByMaskProp != nil {
413+
pl := player()
414+
if pl == nil {
415+
return
416+
}
417+
406418
spottersChanged := func(val st.PropertyValue) {
407419
p.eventDispatcher.Dispatch(events.PlayerSpottersChanged{Spotted: pl})
408420
}
409421

410422
spottedByMaskProp.OnUpdate(spottersChanged)
411-
pawnEntity.Property(spottedByPrefix + "1").OnUpdate(spottersChanged)
412-
}
413-
414-
if isNew {
415-
if pl.SteamID64 != 0 {
416-
p.eventDispatcher.Dispatch(events.PlayerConnect{Player: pl})
417-
} else {
418-
p.eventDispatcher.Dispatch(events.BotConnect{Player: pl})
419-
}
423+
pawnEntity.Property("m_bSpottedByMask.0001").OnUpdate(spottersChanged)
420424
}
421425
}
422426

pkg/demoinfocs/parser.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,10 @@ func (p demoInfoProvider) FindPlayerByHandle(handle int) *common.Player {
436436
return p.parser.gameState.Participants().FindByHandle(handle)
437437
}
438438

439+
func (p demoInfoProvider) FindEntityByHandle(handle uint64) st.Entity {
440+
return p.parser.gameState.EntityByHandle(handle)
441+
}
442+
439443
func (p demoInfoProvider) PlayerResourceEntity() st.Entity {
440444
return p.parser.gameState.playerResourceEntity
441445
}

pkg/demoinfocs/sendtables2/entity.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,6 @@ func (p *Parser) OnPacketEntities(m *msgs2.CSVCMsg_PacketEntities) error {
465465
cmd uint32
466466
classId int32
467467
serial int32
468-
e *Entity
469468
)
470469

471470
if !m.GetIsDelta() {
@@ -475,12 +474,22 @@ func (p *Parser) OnPacketEntities(m *msgs2.CSVCMsg_PacketEntities) error {
475474
p.entityFullPackets++
476475
}
477476

477+
type tuple struct {
478+
ent *Entity
479+
op st.EntityOp
480+
}
481+
482+
var tuples []tuple
483+
478484
for ; updates > 0; updates-- {
485+
var (
486+
e *Entity
487+
op st.EntityOp
488+
)
489+
479490
next := index + int32(r.readUBitVar()) + 1
480491
index = next
481492

482-
var op st.EntityOp
483-
484493
cmd = r.readBits(2)
485494
if cmd&0x01 == 0 {
486495
if cmd&0x02 != 0 {
@@ -544,11 +553,27 @@ func (p *Parser) OnPacketEntities(m *msgs2.CSVCMsg_PacketEntities) error {
544553
}
545554
}
546555

556+
tuples = append(tuples, tuple{e, op})
557+
}
558+
559+
for _, t := range tuples {
560+
e := t.ent
561+
547562
for _, h := range p.entityHandlers {
548-
if err := h(e, op); err != nil {
563+
if err := h(e, t.op); err != nil {
549564
return err
550565
}
551566
}
567+
568+
if t.op&st.EntityOpCreated != 0 {
569+
for prop, hs := range e.updateHandlers {
570+
v := e.PropertyValueMust(prop)
571+
572+
for _, h := range hs {
573+
h(v)
574+
}
575+
}
576+
}
552577
}
553578

554579
if r.remBytes() > 1 || r.bitCount > 7 {

0 commit comments

Comments
 (0)