forked from paralin/go-dota2
/
socache.go
137 lines (111 loc) · 3.05 KB
/
socache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package socache
import (
"sync"
gcsdkm "github.com/MrJoshLab/go-dota2/protocol"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
// gcsm "github.com/MrJoshLab/go-dota2/protocol"
)
// SOCache implements the shared-object cache from DOTA.
type SOCache struct {
containerMap sync.Map
le *logrus.Entry
}
// NewSOCache builds a new SOCache.
func NewSOCache(le *logrus.Entry) *SOCache {
return &SOCache{le: le}
}
// GetContainerForTypeID returns the container for the type id.
func (c *SOCache) GetContainerForTypeID(id uint32) (*SOCacheContainer, error) {
if valInter, ok := c.containerMap.Load(id); ok {
return valInter.(*SOCacheContainer), nil
}
ctr, err := NewSOCacheContainer(c.le, id)
if err != nil {
return nil, err
}
valInter, loaded := c.containerMap.LoadOrStore(id, ctr)
if loaded {
ctr = valInter.(*SOCacheContainer)
}
return ctr, nil
}
// HandleSubscribed handles a subscribed message.
func (c *SOCache) HandleSubscribed(msg *gcsdkm.CMsgSOCacheSubscribed) error {
var retErr error
for _, obj := range msg.GetObjects() {
if obj.GetTypeId() == 0 {
continue
}
ctr, err := c.GetContainerForTypeID(uint32(obj.GetTypeId()))
if err == nil {
err = ctr.HandleSubscribed(msg, obj)
}
if retErr == nil && err != nil {
retErr = err
}
}
return retErr
}
// HandleUnsubscribed handles a subscribed message.
func (c *SOCache) HandleUnsubscribed(msg *gcsdkm.CMsgSOCacheUnsubscribed) error {
if msg.GetOwnerSoid().GetId() == 0 {
return errors.Errorf("object id was empty in unsubscribed")
}
var retErr error
c.containerMap.Range(func(key interface{}, value interface{}) bool {
val := value.(*SOCacheContainer)
_, hasObj := val.objects.Load(msg.GetOwnerSoid().GetId())
if !hasObj {
return true
}
retErr = val.HandleUnsubscribed(msg)
return false
})
return retErr
}
// HandleUpdateMultiple handles a update multiple message.
func (c *SOCache) HandleUpdateMultiple(msg *gcsdkm.CMsgSOMultipleObjects) error {
addUpdateObj := func(obj *gcsdkm.CMsgSOMultipleObjects_SingleObject) error {
ctr, err := c.GetContainerForTypeID(uint32(obj.GetTypeId()))
if err != nil {
return nil
}
m := &gcsdkm.CMsgSOCacheSubscribed_SubscribedType{
TypeId: obj.TypeId,
ObjectData: [][]byte{obj.ObjectData},
}
if err := ctr.addUpdateObject(msg.GetOwnerSoid(), m); err != nil {
return err
}
return nil
}
for _, obj := range msg.GetObjectsAdded() {
if err := addUpdateObj(obj); err != nil {
return err
}
}
for _, obj := range msg.GetObjectsModified() {
if err := addUpdateObj(obj); err != nil {
return err
}
}
for _, obj := range msg.GetObjectsRemoved() {
ctr, err := c.GetContainerForTypeID(uint32(obj.GetTypeId()))
if err != nil {
continue
}
if err := ctr.removeObject(msg.GetOwnerSoid()); err != nil {
return err
}
}
return nil
}
// HandleDestroy handles a object destroy message.
func (c *SOCache) HandleDestroy(msg *gcsdkm.CMsgSOSingleObject) error {
ctr, err := c.GetContainerForTypeID(uint32(msg.GetTypeId()))
if err != nil {
return err
}
return ctr.HandleDestroy(msg)
}