Skip to content

Commit

Permalink
flatten entity update handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
makindotcc committed Feb 10, 2024
1 parent 4c68b7a commit 9dcbb9c
Showing 1 changed file with 75 additions and 62 deletions.
137 changes: 75 additions & 62 deletions pkg/demoinfocs/sendtables2/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,25 +438,44 @@ func (e *Entity) readFields(r *reader, paths *[]*fieldPath) {
}
}

// updateFlag is a bitmask representing the type of operation performed on an Entity
type updateFlag = uint32
const (
updateFlagDelete = 0b10
updateFlagVisibleInPVS = 0b10000
)

type updateType = uint32

const (
updateFlagCreate updateFlag = 0b10
updateFlagLeave updateFlag = 0b01
updateFlagDelete updateFlag = 0b10
updateFlagPreserveEnt updateFlag = 0b01000
updateFlagVisibleInPVS updateFlag = 0b10000
updateTypeEnter updateType = iota
updateTypeLeave
updateTypeDelta
updateTypePreserve
)

func readEntityUpdateType(r *reader, has_vis_bits uint32) (updateType, uint32) {

Check failure on line 455 in pkg/demoinfocs/sendtables2/entity.go

View workflow job for this annotation

GitHub Actions / ci

[golangci-lint] reported by reviewdog 🐶 var-naming: don't use underscores in Go names; func parameter has_vis_bits should be hasVisBits (revive) Raw Output: pkg/demoinfocs/sendtables2/entity.go:455:38: var-naming: don't use underscores in Go names; func parameter has_vis_bits should be hasVisBits (revive) func readEntityUpdateType(r *reader, has_vis_bits uint32) (updateType, uint32) { ^
flags := r.readBits(2)
if flags&0x01 != 0 {
return updateTypeLeave, flags
}
if flags&0x02 != 0 {

Check failure on line 460 in pkg/demoinfocs/sendtables2/entity.go

View workflow job for this annotation

GitHub Actions / ci

[golangci-lint] reported by reviewdog 🐶 if statements should only be cuddled with assignments (wsl) Raw Output: pkg/demoinfocs/sendtables2/entity.go:460:2: if statements should only be cuddled with assignments (wsl) if flags&0x02 != 0 { ^
return updateTypeEnter, flags
}
if has_vis_bits != 0 {

Check failure on line 463 in pkg/demoinfocs/sendtables2/entity.go

View workflow job for this annotation

GitHub Actions / ci

[golangci-lint] reported by reviewdog 🐶 if statements should only be cuddled with assignments (wsl) Raw Output: pkg/demoinfocs/sendtables2/entity.go:463:2: if statements should only be cuddled with assignments (wsl) if has_vis_bits != 0 { ^
flags = r.readBits(2) << 3
if flags&0x08 != 0 {
return updateTypePreserve, flags

Check warning on line 466 in pkg/demoinfocs/sendtables2/entity.go

View check run for this annotation

Codecov / codecov/patch

pkg/demoinfocs/sendtables2/entity.go#L464-L466

Added lines #L464 - L466 were not covered by tests
}
}
return updateTypeDelta, flags

Check failure on line 469 in pkg/demoinfocs/sendtables2/entity.go

View workflow job for this annotation

GitHub Actions / ci

[golangci-lint] reported by reviewdog 🐶 return statements should not be cuddled if block has more than two lines (wsl) Raw Output: pkg/demoinfocs/sendtables2/entity.go:469:2: return statements should not be cuddled if block has more than two lines (wsl) return updateTypeDelta, flags ^
}

// Internal Callback for OnCSVCMsg_PacketEntities.
func (p *Parser) OnPacketEntities(m *msgs2.CSVCMsg_PacketEntities) error {
r := newReader(m.GetEntityData())

var (
index = int32(-1)
updates = int(m.GetUpdatedEntries())
cmd uint32
classId int32
serial int32
)
Expand All @@ -478,7 +497,7 @@ func (p *Parser) OnPacketEntities(m *msgs2.CSVCMsg_PacketEntities) error {
paths = make([]*fieldPath, 0)
)

isPvsPacket := m.GetHasPvsVisBits() != 0
hasVisBits := m.GetHasPvsVisBits()
for ; updates > 0; updates-- {
var (
e *Entity
Expand All @@ -488,68 +507,62 @@ func (p *Parser) OnPacketEntities(m *msgs2.CSVCMsg_PacketEntities) error {
next := index + int32(r.readUBitVar()) + 1
index = next

cmd = r.readBits(2)
if cmd == 0 && isPvsPacket {
cmd = r.readBits(2) << 3
}

if cmd&updateFlagLeave == 0 {
if cmd&updateFlagCreate != 0 {
classId = int32(r.readBits(p.classIdSize))
serial = int32(r.readBits(17))
r.readVarUint32()

class := p.classesById[classId]
if class == nil {
_panicf("unable to find new class %d", classId)
}
updateType, flags := readEntityUpdateType(r, hasVisBits)
switch updateType {
case updateTypeEnter:
classId = int32(r.readBits(p.classIdSize))
serial = int32(r.readBits(17))
r.readVarUint32()

baseline := p.classBaselines[classId]
if baseline == nil {
_panicf("unable to find new baseline %d", classId)
}
class := p.classesById[classId]
if class == nil {
_panicf("unable to find new class %d", classId)

Check warning on line 519 in pkg/demoinfocs/sendtables2/entity.go

View check run for this annotation

Codecov / codecov/patch

pkg/demoinfocs/sendtables2/entity.go#L519

Added line #L519 was not covered by tests
}

e = newEntity(index, serial, class)
p.entities[index] = e
baseline := p.classBaselines[classId]
if baseline == nil {
_panicf("unable to find new baseline %d", classId)

Check warning on line 524 in pkg/demoinfocs/sendtables2/entity.go

View check run for this annotation

Codecov / codecov/patch

pkg/demoinfocs/sendtables2/entity.go#L524

Added line #L524 was not covered by tests
}

e.readFields(newReader(baseline), &paths)
paths = paths[:0]
e = newEntity(index, serial, class)
p.entities[index] = e

e.readFields(r, &paths)
paths = paths[:0]
e.readFields(newReader(baseline), &paths)
paths = paths[:0]

// Fire created-handlers so update-handlers can be registered
for _, h := range class.createdHandlers {
h(e)
}
e.readFields(r, &paths)
paths = paths[:0]

// Fire all post-creation actions
for _, f := range e.onCreateFinished {
f()
}
// Fire created-handlers so update-handlers can be registered
for _, h := range class.createdHandlers {
h(e)
}

op = st.EntityOpCreated | st.EntityOpEntered
} else if cmd&updateFlagPreserveEnt != 0 {
// todo: handle visibility
// visibleInPvs := !isPvsPacket || cmd&updateFlagVisibleInPVS != 0
// fmt.Println("preserve visible in pvs", visibleInPvs)
} else { // delta update
if e = p.entities[index]; e == nil {
_panicf("unable to find existing entity %d", index)
}
// Fire all post-creation actions
for _, f := range e.onCreateFinished {
f()
}

op = st.EntityOpUpdated
if !e.active {
e.active = true
op |= st.EntityOpEntered
}
op = st.EntityOpCreated | st.EntityOpEntered
case updateTypeDelta:
if e = p.entities[index]; e == nil {
_panicf("unable to find existing entity %d", index)

Check warning on line 549 in pkg/demoinfocs/sendtables2/entity.go

View check run for this annotation

Codecov / codecov/patch

pkg/demoinfocs/sendtables2/entity.go#L549

Added line #L549 was not covered by tests
}

e.readFields(r, &paths)
paths = paths[:0]
// todo: handle visibility
// visibleInPVS := !isPvsPacket || cmd&updateFlagVisibleInPVS != 0
op = st.EntityOpUpdated
if !e.active {

Check failure on line 553 in pkg/demoinfocs/sendtables2/entity.go

View workflow job for this annotation

GitHub Actions / ci

[golangci-lint] reported by reviewdog 🐶 if statements should only be cuddled with assignments used in the if statement itself (wsl) Raw Output: pkg/demoinfocs/sendtables2/entity.go:553:4: if statements should only be cuddled with assignments used in the if statement itself (wsl) if !e.active { ^
e.active = true
op |= st.EntityOpEntered

Check warning on line 555 in pkg/demoinfocs/sendtables2/entity.go

View check run for this annotation

Codecov / codecov/patch

pkg/demoinfocs/sendtables2/entity.go#L554-L555

Added lines #L554 - L555 were not covered by tests
}
} else {

e.readFields(r, &paths)
paths = paths[:0]
// todo: handle visibility

Check failure on line 560 in pkg/demoinfocs/sendtables2/entity.go

View workflow job for this annotation

GitHub Actions / ci

[golangci-lint] reported by reviewdog 🐶 pkg/demoinfocs/sendtables2/entity.go:560: Line contains TODO/BUG/FIXME: "todo: handle visibility" (godox) Raw Output: pkg/demoinfocs/sendtables2/entity.go:560: pkg/demoinfocs/sendtables2/entity.go:560: Line contains TODO/BUG/FIXME: "todo: handle visibility" (godox) // todo: handle visibility
// visibleInPVS := hasVisBits == 0 || flags&updateFlagVisibleInPVS != 0
// fmt.Println("visible in pvs", visibleInPVS)
case updateTypePreserve:

Check warning on line 563 in pkg/demoinfocs/sendtables2/entity.go

View check run for this annotation

Codecov / codecov/patch

pkg/demoinfocs/sendtables2/entity.go#L563

Added line #L563 was not covered by tests
// visibleInPVS := hasVisBits == 0 || flags&updateFlagVisibleInPVS != 0
case updateTypeLeave:
e = p.entities[index]
if e == nil {
continue
Expand All @@ -561,7 +574,7 @@ func (p *Parser) OnPacketEntities(m *msgs2.CSVCMsg_PacketEntities) error {
}

op = st.EntityOpLeft
if cmd&updateFlagDelete != 0 {
if flags&updateFlagDelete != 0 {
op |= st.EntityOpDeleted

e.Destroy()
Expand Down

0 comments on commit 9dcbb9c

Please sign in to comment.