From a3353b7c3076cec9d2b1b78485462bf3ffc5a0db Mon Sep 17 00:00:00 2001 From: Francesco Di Biase Date: Tue, 30 Jul 2024 18:25:28 +0200 Subject: [PATCH] Add OnModifierTableEntry event handler (#164) --- modifier.go | 34 ++++++++++++++++++++++++++++++++++ parser.go | 33 +++++++++++++++++---------------- string_table.go | 14 ++++++++++++++ 3 files changed, 65 insertions(+), 16 deletions(-) create mode 100644 modifier.go diff --git a/modifier.go b/modifier.go new file mode 100644 index 0000000..4b7395f --- /dev/null +++ b/modifier.go @@ -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 +} diff --git a/parser.go b/parser.go index b57d513..75ea78d 100644 --- a/parser.go +++ b/parser.go @@ -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. diff --git a/string_table.go b/string_table.go index d490ea7..d2f75b3 100644 --- a/string_table.go +++ b/string_table.go @@ -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 } @@ -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 }