-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhandler.go
165 lines (141 loc) · 4.1 KB
/
handler.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package peex
import (
"errors"
"github.com/df-mc/dragonfly/server/player"
"reflect"
)
// Handler is a struct that handles player-related events. It can query for certain components contained in the player
// session, and will only run if all those components are present.
type Handler interface {
}
/// Internal handler logic
/// ----------------------
type handlerId uint
// handlerInfo contains data about a specific handler type.
type handlerInfo struct {
h Handler
typ reflect.Type
components []componentQuery
events map[eventId]struct{}
playerField int
sessionField int
managerField int
copyFields []int // fields that need to be copied over to a new instance of the handler
}
type componentQuery struct {
id componentId
fieldNum int
optional bool
}
// createHandlerInfo creates a new handler info struct for a type of handler.
func (m *Manager) createHandlerInfo(h Handler) handlerInfo {
v := reflect.ValueOf(h)
if v.Kind() == reflect.Pointer {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
panic(errors.New("A handler must be of type struct, or a pointer to a one"))
}
info := handlerInfo{
h: h,
typ: reflect.TypeOf(h),
events: getHandlerEvents(h),
playerField: -1,
sessionField: -1,
managerField: -1,
}
for i := 0; i < v.NumField(); i++ {
// Fields marked with a `ignore:""` tag will be ignored by the library.
if _, ok := v.Type().Field(i).Tag.Lookup("ignore"); ok {
continue
}
// Ignore unexported fields
if !v.Field(i).CanInterface() {
if m.logger != nil {
m.logger.Debugf("warning: unexported handler fields cannot be copied by Peex")
}
continue
}
switch x := v.Field(i).Interface().(type) {
case queryType:
fieldType := x.getType()
cId := m.getComponentIdRefl(fieldType)
info.components = append(info.components, componentQuery{
id: cId,
fieldNum: i,
optional: x.optional(),
})
case *player.Player:
// We don't need to pass the same player multiple times
if info.playerField != -1 {
continue
}
info.playerField = i
case *Session:
// We don't need to pass the same session multiple times
if info.sessionField != -1 {
continue
}
info.sessionField = i
case *Manager:
// We don't need to pass the same manager multiple times
if info.managerField != -1 {
continue
}
info.managerField = i
default:
info.copyFields = append(info.copyFields, i)
continue
}
}
return info
}
// handleEvent handles all shared logic for events, such as assigning query values.
func (s *Session) handleEvent(eventId eventId, f func(h Handler)) {
s.componentsMu.RLock()
defer s.componentsMu.RUnlock()
handlerLoop:
for _, id := range s.m.eventHandlers[eventId] {
info := s.m.handlers[id]
// Figure out which components to set in the queries
comps := make([]componentQuery, 0, len(info.components))
for _, compQuery := range info.components {
_, isPresent := s.components[compQuery.id]
if !isPresent && !compQuery.optional {
continue handlerLoop
} else if !isPresent && compQuery.optional {
continue
}
comps = append(comps, compQuery)
}
actualType := reflect.New(info.typ).Elem()
structType := actualType
if actualType.Kind() == reflect.Pointer {
val := reflect.New(info.typ.Elem())
actualType.Set(val.Elem().Addr())
structType = actualType.Elem()
}
for _, compQuery := range comps {
field := structType.Field(compQuery.fieldNum)
query := field.Interface().(queryType)
field.Set(reflect.ValueOf(query.set(s.components[compQuery.id].(Component))))
}
if info.playerField != -1 {
structType.Field(info.playerField).Set(reflect.ValueOf(s.Player()))
}
if info.sessionField != -1 {
structType.Field(info.sessionField).Set(reflect.ValueOf(s))
}
if info.managerField != -1 {
structType.Field(info.managerField).Set(reflect.ValueOf(s.m))
}
// Copy the other fields if there are any
if len(info.copyFields) > 0 {
hVal := reflect.ValueOf(info.h)
for _, fieldNum := range info.copyFields {
structType.Field(fieldNum).Set(hVal.Elem().Field(fieldNum))
}
}
f(actualType.Interface().(Handler))
}
}