-
Notifications
You must be signed in to change notification settings - Fork 2
/
action_buttons.go
183 lines (153 loc) · 3.76 KB
/
action_buttons.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package spell
import (
"fmt"
"github.com/Gophercraft/core/packet"
"github.com/Gophercraft/core/realm/wdb/models"
"github.com/Gophercraft/core/vsn"
"github.com/Gophercraft/log"
)
type ActionButton struct {
Action uint32
Type models.ActionType
}
type ActionButtons struct {
Buttons []ActionButton
}
type SetActionButton struct {
Slot uint32
ActionButton
}
func NumActionButtons(build vsn.Build) int {
ab := 120
if build.AddedIn(vsn.V2_0_1) {
ab = 132
}
return ab
}
const (
spellMask = 0xffffff
)
func (ab ActionButton) typeNibble() uint32 {
switch ab.Type {
case models.ActionSpell:
return 0
case models.ActionMacro:
return 64
case models.ActionItem:
return 128
}
panic(ab.Type)
}
func (ab ActionButton) Uint32(build vsn.Build) uint32 {
// if build == vsn.Alpha {
// var sign int32
// switch ab.Type {
// case models.ActionItem:
// sign = -1
// case models.ActionSpell:
// sign = 1
// default:
// panic(fmt.Sprintf("spell: ActionButton.Uint32: cannot encode this type %d", ab.Type))
// }
// data := int32(ab.Action) * sign
// return uint32(data)
// }
if ab.Type >= 0xff {
log.Warn("actionbutton slot too big")
return 0
}
// uint24(ab.Spell) + uint8(ab.Slot)
return (ab.Action & spellMask) | (ab.typeNibble() << 24)
}
func (abs *ActionButtons) Encode(build vsn.Build, out *packet.WorldPacket) error {
out.Type = packet.SMSG_ACTION_BUTTONS
nab := NumActionButtons(build)
if len(abs.Buttons) > nab {
return fmt.Errorf("packet: action button overflow %d/%d", len(abs.Buttons), nab)
}
for ab := 0; ab < nab; ab++ {
if len(abs.Buttons) <= ab {
out.WriteUint32(0)
} else {
out.WriteUint32(abs.Buttons[ab].Uint32(build))
}
}
log.Dump("buttons", out.Bytes())
return nil
}
func convertTypeNibble(nibble uint32) (at models.ActionType, err error) {
switch nibble {
case 0:
at = models.ActionSpell
case 64:
at = models.ActionMacro
case 128:
at = models.ActionItem
default:
err = fmt.Errorf("spell: invalid nibble value %d", nibble)
}
return
}
func (abs *ActionButtons) Decode(build vsn.Build, in *packet.WorldPacket) error {
nab := in.Len() / 4
abs.Buttons = make([]ActionButton, nab)
// In Alpha, instead of type nibble, the sign is used to separate items
// No macros yet, only ( < 0 == Item ) ( > 0 == Spell )
if build == vsn.Alpha {
for i := 0; i < nab; i++ {
act := int32(in.ReadUint32())
if act < 0 {
abs.Buttons[i].Action = uint32(act * -1)
abs.Buttons[i].Type = models.ActionItem
} else {
abs.Buttons[i].Action = uint32(act)
abs.Buttons[i].Type = models.ActionSpell
}
}
return nil
}
var err error
for i := 0; i < nab; i++ {
act := in.ReadUint32()
abs.Buttons[i].Action = act & spellMask
typeNibble := (act >> 24) & 0xff
abs.Buttons[i].Type, err = convertTypeNibble(typeNibble)
if err != nil {
return err
}
}
return nil
}
func (sab *SetActionButton) Encode(build vsn.Build, out *packet.WorldPacket) error {
out.Type = packet.CMSG_SET_ACTION_BUTTON
out.WriteByte(uint8(sab.Slot))
// out.WriteUint32(sab.Action&spellMask | (sab.Type << 24))
out.WriteUint32(sab.ActionButton.Uint32(build))
return nil
}
func (sab *SetActionButton) Decode(build vsn.Build, in *packet.WorldPacket) (err error) {
sab.Slot = uint32(in.ReadByte())
if sab.Slot >= uint32(NumActionButtons(build)) {
err = fmt.Errorf("packet: SetActionButton slot %d too large for build %s", sab.Slot, build)
return
}
act := in.ReadUint32()
if build == vsn.Alpha {
sact := int32(act)
if sact < 0 {
sab.Type = models.ActionItem
sab.Action = uint32(sact * -1)
} else {
sab.Type = models.ActionSpell
sab.Action = act
}
return
}
sab.Action = act & spellMask
typeNibble := (act >> 24) & 0xff
sab.Type, err = convertTypeNibble(typeNibble)
if err != nil {
return
}
return
}