Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OnModifierTableEntry event handler #164

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading