Skip to content

Commit 7076960

Browse files
committed
clean up more csgo stuff + ParseBroadcast funcs
1 parent 4ed45f2 commit 7076960

File tree

8 files changed

+68
-43
lines changed

8 files changed

+68
-43
lines changed

examples/entities/entities.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ func main() {
2323
p.RegisterEventHandler(func(events.DataTablesParsed) {
2424
p.ServerClasses().FindByName("CWeaponAWP").OnEntityCreated(func(ent st.Entity) {
2525
ent.Property("m_hOwnerEntity").OnUpdate(func(val st.PropertyValue) {
26-
x := p.GameState().Participants().FindByHandle64(val.S2UInt64())
26+
x := p.GameState().Participants().FindByHandle64(val.UInt64())
2727
if x != nil {
2828
var prev string
29-
prevHandle := ent.Property("m_hPrevOwner").Value().S2UInt64()
29+
prevHandle := ent.Property("m_hPrevOwner").Value().UInt64()
3030
prevPlayer := p.GameState().Participants().FindByHandle64(prevHandle)
3131
if prevPlayer != nil {
32-
if prevHandle != val.S2UInt64() {
32+
if prevHandle != val.UInt64() {
3333
prev = prevPlayer.Name + "'s"
3434
} else {
3535
prev = "his dropped"

pkg/demoinfocs/common/entity_util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func getUInt64(entity st.Entity, propName string) uint64 {
1515
return 0
1616
}
1717

18-
return entity.PropertyValueMust(propName).S2UInt64()
18+
return entity.PropertyValueMust(propName).UInt64()
1919
}
2020

2121
func getFloat(entity st.Entity, propName string) float32 {

pkg/demoinfocs/common/equipment.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func (e *Equipment) AmmoInMagazine() int {
356356
return -1
357357
}
358358

359-
return int(ammo.S2UInt32() - 1)
359+
return int(ammo.UInt32() - 1)
360360
}
361361
}
362362

@@ -400,18 +400,9 @@ func (e *Equipment) AmmoReserve() int {
400400
return 0
401401
}
402402

403-
s2Prop := e.Entity.Property("m_pReserveAmmo.0000")
404-
if s2Prop != nil && s2Prop.Value().Any != nil {
405-
return s2Prop.Value().Int()
406-
}
407-
408-
if e.Class() == EqClassGrenade {
409-
if e.Owner != nil {
410-
// minus one for 'InMagazine'
411-
return e.Owner.AmmoLeft[e.AmmoType()] - 1
412-
}
413-
414-
return 0
403+
prop := e.Entity.Property("m_pReserveAmmo.0000")
404+
if prop != nil && prop.Value().Any != nil {
405+
return prop.Value().Int()
415406
}
416407

417408
if e.Class() == EqClassEquipment {

pkg/demoinfocs/common/player.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ type Player struct {
1818
UserID int // Mostly used in game-events to address this player
1919
Name string // Steam / in-game user name
2020
Inventory map[int]*Equipment // All weapons / equipment the player is currently carrying. See also Weapons().
21-
AmmoLeft [32]int // Ammo left for special weapons (e.g. grenades), index corresponds Equipment.AmmoType
2221
EntityID int // Usually the same as Entity.ID() but may be different between player death and re-spawn.
2322
Entity st.Entity // May be nil between player-death and re-spawn
2423
FlashDuration float32 // Blindness duration from the flashbang currently affecting the player (seconds)
@@ -52,7 +51,7 @@ func (p *Player) PlayerPawnEntity() st.Entity {
5251
}
5352

5453
func (p *Player) GetTeam() Team {
55-
return Team(p.PlayerPawnEntity().PropertyValueMust("m_iTeamNum").S2UInt64())
54+
return Team(p.PlayerPawnEntity().PropertyValueMust("m_iTeamNum").UInt64())
5655
}
5756

5857
func (p *Player) GetFlashDuration() float32 {
@@ -83,7 +82,7 @@ func (p *Player) IsAlive() bool {
8382

8483
pawnEntity := p.PlayerPawnEntity()
8584
if pawnEntity != nil {
86-
return pawnEntity.PropertyValueMust("m_lifeState").S2UInt64() == 0
85+
return pawnEntity.PropertyValueMust("m_lifeState").UInt64() == 0
8786
}
8887

8988
return getBool(p.Entity, "m_bPawnIsAlive")
@@ -153,7 +152,7 @@ This isn't very conclusive but it looks like IsFlashed isn't super reliable curr
153152
// ActiveWeaponID is used internally to set the active weapon, see ActiveWeapon()
154153
func (p *Player) ActiveWeaponID() int {
155154
if pawnEntity := p.PlayerPawnEntity(); pawnEntity != nil {
156-
return int(pawnEntity.PropertyValueMust("m_pWeaponServices.m_hActiveWeapon").S2UInt64() & constants.EntityHandleIndexMaskSource2)
155+
return int(pawnEntity.PropertyValueMust("m_pWeaponServices.m_hActiveWeapon").UInt64() & constants.EntityHandleIndexMaskSource2)
157156
}
158157

159158
return 0
@@ -196,7 +195,7 @@ func (p *Player) IsSpottedBy(other *Player) bool {
196195
mask = p.PlayerPawnEntity().Property("m_bSpottedByMask.0001")
197196
}
198197

199-
return (mask.Value().S2UInt64() & (1 << bit)) != 0
198+
return (mask.Value().UInt64() & (1 << bit)) != 0
200199
}
201200

202201
// HasSpotted returns true if the player has spotted the other player.
@@ -289,7 +288,7 @@ func (p *Player) ControlledBot() *Player {
289288
return nil
290289
}
291290

292-
controllerHandler := p.Entity.Property("m_hOriginalControllerOfCurrentPawn").Value().S2UInt64()
291+
controllerHandler := p.Entity.Property("m_hOriginalControllerOfCurrentPawn").Value().UInt64()
293292

294293
return p.demoInfoProvider.FindPlayerByHandle(controllerHandler)
295294
}

pkg/demoinfocs/datatables.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ func (p *parser) bindNewPlayerController(controllerEntity st.Entity) {
396396

397397
controllerEntity.Property("m_iConnected").OnUpdate(func(val st.PropertyValue) {
398398
pl := p.getOrCreatePlayerFromControllerEntity(controllerEntity)
399-
state := val.S2UInt32()
399+
state := val.UInt32()
400400
wasConnected := pl.IsConnected
401401
pl.IsConnected = state == 0
402402

@@ -425,7 +425,7 @@ func (p *parser) bindNewPlayerController(controllerEntity st.Entity) {
425425
})
426426

427427
controllerEntity.Property("m_iTeamNum").OnUpdate(func(val st.PropertyValue) {
428-
pl.Team = common.Team(val.S2UInt64())
428+
pl.Team = common.Team(val.UInt64())
429429
pl.TeamState = p.gameState.Team(pl.Team)
430430
})
431431

@@ -531,7 +531,7 @@ func (p *parser) bindPlayerWeapons(pawnEntity st.Entity, pl *common.Player) {
531531
playerInventory := make(map[int]eq)
532532

533533
getWep := func(wepSlotPropertyValue st.PropertyValue) (uint64, *common.Equipment) {
534-
entityID := wepSlotPropertyValue.S2UInt64() & constants.EntityHandleIndexMaskSource2
534+
entityID := wepSlotPropertyValue.UInt64() & constants.EntityHandleIndexMaskSource2
535535
wep := p.gameState.weapons[int(entityID)]
536536

537537
if wep == nil {
@@ -560,7 +560,7 @@ func (p *parser) bindPlayerWeapons(pawnEntity st.Entity, pl *common.Player) {
560560
}
561561

562562
pawnEntity.Property("m_pWeaponServices.m_hMyWeapons").OnUpdate(func(pv st.PropertyValue) {
563-
inventorySize = len(pv.S2Array())
563+
inventorySize = len(pv.Array())
564564
setPlayerInventory()
565565
})
566566

@@ -657,7 +657,7 @@ func (p *parser) bindGrenadeProjectiles(entity st.Entity) {
657657
modelVal := entity.PropertyValueMust("CBodyComponent.m_hModel")
658658

659659
if modelVal.Any != nil {
660-
model := modelVal.S2UInt64()
660+
model := modelVal.UInt64()
661661
weaponType, exists := p.equipmentTypePerModel[model]
662662
if exists {
663663
wep = weaponType
@@ -815,7 +815,7 @@ func (p *parser) bindWeaponS2(entity st.Entity) {
815815
return
816816
}
817817

818-
itemIndex := itemIndexVal.S2UInt64()
818+
itemIndex := itemIndexVal.UInt64()
819819
wepType := common.EquipmentIndexMapping[itemIndex]
820820

821821
if wepType == common.EqUnknown {
@@ -826,7 +826,7 @@ func (p *parser) bindWeaponS2(entity st.Entity) {
826826
Type: events.WarnTypeUnknownEquipmentIndex,
827827
})
828828
} else {
829-
model := entity.PropertyValueMust("CBodyComponent.m_hModel").S2UInt64()
829+
model := entity.PropertyValueMust("CBodyComponent.m_hModel").UInt64()
830830
p.equipmentTypePerModel[model] = wepType
831831
}
832832

pkg/demoinfocs/parser.go

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,13 @@ func NewCSTVBroadcastParserWithConfig(baseUrl string, config ParserConfig) (Pars
379379
return NewParserWithConfig(r, config), nil
380380
}
381381

382-
type ConfigureParserCallback func(Parser) error
382+
type ParserCallback func(Parser) error
383383

384384
// ParseWithConfig parses a demo from the given io.Reader with a custom configuration.
385385
// The handler is called with the Parser instance.
386386
//
387387
// Returns an error if the parser encounters an error.
388-
func ParseWithConfig(r io.Reader, config ParserConfig, configure ConfigureParserCallback) error {
388+
func ParseWithConfig(r io.Reader, config ParserConfig, configure ParserCallback) error {
389389
p := NewParserWithConfig(r, config)
390390
defer p.Close()
391391

@@ -406,15 +406,15 @@ func ParseWithConfig(r io.Reader, config ParserConfig, configure ConfigureParser
406406
// The handler is called with the Parser instance.
407407
//
408408
// Returns an error if the parser encounters an error.
409-
func Parse(r io.Reader, configure ConfigureParserCallback) error {
409+
func Parse(r io.Reader, configure ParserCallback) error {
410410
return ParseWithConfig(r, DefaultParserConfig, configure)
411411
}
412412

413413
// ParseFileWithConfig parses a demo file at the given path with a custom configuration.
414414
// The handler is called with the Parser instance.
415415
//
416416
// Returns an error if the file can't be opened or if the parser encounters an error.
417-
func ParseFileWithConfig(path string, config ParserConfig, configure ConfigureParserCallback) error {
417+
func ParseFileWithConfig(path string, config ParserConfig, configure ParserCallback) error {
418418
f, err := os.Open(path)
419419
if err != nil {
420420
return fmt.Errorf("failed to open file: %w", err)
@@ -429,10 +429,45 @@ func ParseFileWithConfig(path string, config ParserConfig, configure ConfigurePa
429429
// The handler is called with the Parser instance.
430430
//
431431
// Returns an error if the file can't be opened or if the parser encounters an error.
432-
func ParseFile(path string, configure ConfigureParserCallback) error {
432+
func ParseFile(path string, configure ParserCallback) error {
433433
return ParseFileWithConfig(path, DefaultParserConfig, configure)
434434
}
435435

436+
// ParseCSTVBroadcastWithConfig parses a live CSTV broadcast from the given base URL with a custom configuration.
437+
// The handler is called with the Parser instance.
438+
// The baseUrl is the base URL of the CSTV broadcast, e.g. "http://localhost:8080/s85568392932860274t1733091777".
439+
// Returns an error if the CSTV reader can't be created or if the parser encounters an error.
440+
// Note that the CSTV broadcast is a live stream and will not end until the broadcast ends.
441+
func ParseCSTVBroadcastWithConfig(baseUrl string, config ParserConfig, configure ParserCallback) error {
442+
p, err := NewCSTVBroadcastParserWithConfig(baseUrl, config)
443+
if err != nil {
444+
return fmt.Errorf("failed to create CSTV broadcast parser: %w", err)
445+
}
446+
447+
defer p.Close()
448+
449+
err = configure(p)
450+
if err != nil {
451+
return fmt.Errorf("failed to configure parser: %w", err)
452+
}
453+
454+
err = p.ParseToEnd()
455+
if err != nil {
456+
return fmt.Errorf("failed to parse CSTV broadcast: %w", err)
457+
}
458+
459+
return nil
460+
}
461+
462+
// ParseCSTVBroadcast parses a live CSTV broadcast from the given base URL.
463+
// The handler is called with the Parser instance.
464+
// The baseUrl is the base URL of the CSTV broadcast, e.g. "http://localhost:8080/s85568392932860274t1733091777".
465+
// Returns an error if the CSTV reader can't be created or if the parser encounters an error.
466+
// Note that the CSTV broadcast is a live stream and will not end until the broadcast ends.
467+
func ParseCSTVBroadcast(baseUrl string, configure ParserCallback) error {
468+
return ParseCSTVBroadcastWithConfig(baseUrl, DefaultParserConfig, configure)
469+
}
470+
436471
// ParserConfig contains the configuration for creating a new Parser.
437472
type ParserConfig struct {
438473
// MsgQueueBufferSize defines the size of the internal net-message queue.

pkg/demoinfocs/sendtables/sendtables.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,20 @@ func (v PropertyValue) Int64() int64 {
6161
return v.Any.(int64)
6262
}
6363

64-
func (v PropertyValue) S2UInt64() uint64 {
64+
func (v PropertyValue) UInt64() uint64 {
6565
return v.Any.(uint64)
6666
}
6767

68-
func (v PropertyValue) S2Array() []any {
68+
func (v PropertyValue) Array() []any {
6969
return v.Any.([]any)
7070
}
7171

72-
func (v PropertyValue) S2UInt32() uint32 {
72+
func (v PropertyValue) UInt32() uint32 {
7373
return v.Any.(uint32)
7474
}
7575

7676
func (v PropertyValue) Handle() uint64 {
77-
return v.S2UInt64()
77+
return v.UInt64()
7878
}
7979

8080
func (v PropertyValue) Float() float32 {

pkg/demoinfocs/sendtables/sendtablescs2/entity.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ var bindFactoryByType = map[st.PropertyValueType]bindFactory{
9292
},
9393
st.ValTypeArray: func(variable any) st.PropertyUpdateHandler {
9494
return func(v st.PropertyValue) {
95-
*variable.(*[]any) = v.S2Array()
95+
*variable.(*[]any) = v.Array()
9696
}
9797
},
9898
st.ValTypeString: func(variable any) st.PropertyUpdateHandler {
@@ -203,9 +203,9 @@ func (e *Entity) Position() r3.Vector {
203203
return r3.Vector{} // CS2 POV demos
204204
}
205205

206-
cellX := cellXVal.S2UInt64()
207-
cellY := cellYVal.S2UInt64()
208-
cellZ := cellZVal.S2UInt64()
206+
cellX := cellXVal.UInt64()
207+
cellY := cellYVal.UInt64()
208+
cellZ := cellZVal.UInt64()
209209
offsetX := offsetXVal.Float()
210210
offsetY := offsetYVal.Float()
211211
offsetZ := offsetZVal.Float()

0 commit comments

Comments
 (0)