Skip to content

Commit

Permalink
Add OnModifierTableEntry event handler (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
dibiasefrn authored Jul 30, 2024
1 parent 67533e5 commit a3353b7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 16 deletions.
34 changes: 34 additions & 0 deletions modifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package manta

import (
"github.com/dotabuff/manta/dota"
"github.com/golang/protobuf/proto"
)

type ModifierTableEntryHandler func(msg *dota.CDOTAModifierBuffTableEntry) error

// OnModifierTableEntry registers a handler for when a ModifierBuffTableEntry
// is created or updated.
func (p *Parser) OnModifierTableEntry(fn ModifierTableEntryHandler) {
p.modifierTableEntryHandlers = append(p.modifierTableEntryHandlers, fn)
}

// emitModifierTableEvents emits ModifierBuffTableEntry events
// from the given string table items.
func (p *Parser) emitModifierTableEvents(items []*stringTableItem) error {
for _, item := range items {
msg := &dota.CDOTAModifierBuffTableEntry{}
if err := proto.NewBuffer(item.Value).Unmarshal(msg); err != nil {
_debugf("unable to unmarshal ModifierBuffTableEntry: %s", err)
continue
}

for _, fn := range p.modifierTableEntryHandlers {
if err := fn(msg); err != nil {
return err
}
}
}

return nil
}
33 changes: 17 additions & 16 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,23 @@ type Parser struct {
// AfterStopCallback is a function to be called when the parser stops.
AfterStopCallback func()

classBaselines map[int32][]byte
classesById map[int32]*class
classesByName map[string]*class
classIdSize uint32
classInfo bool
entities map[int32]*Entity
entityFullPackets int
entityHandlers []EntityHandler
gameEventHandlers map[string][]GameEventHandler
gameEventNames map[int32]string
gameEventTypes map[string]*gameEventType
isStopping bool
serializers map[string]*serializer
stream *stream
stringTables *stringTables
stopAtTick uint32
classBaselines map[int32][]byte
classesById map[int32]*class
classesByName map[string]*class
classIdSize uint32
classInfo bool
entities map[int32]*Entity
entityFullPackets int
entityHandlers []EntityHandler
gameEventHandlers map[string][]GameEventHandler
gameEventNames map[int32]string
gameEventTypes map[string]*gameEventType
isStopping bool
modifierTableEntryHandlers []ModifierTableEntryHandler
serializers map[string]*serializer
stream *stream
stringTables *stringTables
stopAtTick uint32
}

// Create a new parser from a byte slice.
Expand Down
14 changes: 14 additions & 0 deletions string_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ func (p *Parser) onCSVCMsg_CreateStringTable(m *dota.CSVCMsg_CreateStringTable)
p.updateInstanceBaseline()
}

// Emit events for modifier table entry updates
if t.name == "ActiveModifiers" {
if err := p.emitModifierTableEvents(items); err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -159,6 +166,13 @@ func (p *Parser) onCSVCMsg_UpdateStringTable(m *dota.CSVCMsg_UpdateStringTable)
p.updateInstanceBaseline()
}

// Emit events for modifier table entry updates
if t.name == "ActiveModifiers" {
if err := p.emitModifierTableEvents(items); err != nil {
return err
}
}

return nil
}

Expand Down

0 comments on commit a3353b7

Please sign in to comment.