-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanimdata.go
151 lines (118 loc) · 3.48 KB
/
animdata.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
package cof
import (
"errors"
"fmt"
"strings"
"github.com/OpenDiablo2/bitstream"
)
const (
numBlocks = 256
maxRecordsPerBlock = 67
byteCountName = 8
byteCountSpeedPadding = 2
numEvents = 144
speedDivisor = 256
speedBaseFPS = 25
milliseconds = 1000
)
// AnimationData is a representation of the binary data from `data/global/AnimData.d2`
type AnimationData struct {
hashTable
blocks [numBlocks]*block
entries map[string][]*AnimationDataRecord
}
// GetRecordNames returns a slice of all record name strings
func (ad *AnimationData) GetRecordNames() []string {
result := make([]string, 0)
for name := range ad.entries {
result = append(result, name)
}
return result
}
// GetRecord returns a single AnimationDataRecord with the given name string. If there is more
// than one record with the given name string, the last record entry will be returned.
func (ad *AnimationData) GetRecord(name string) *AnimationDataRecord {
records := ad.GetRecords(name)
numRecords := len(records)
if numRecords < 1 {
return nil
}
return records[numRecords-1]
}
// GetRecords returns all records that have the given name string. The AnimData.d2 files have
// multiple records with the same name, but other values in the record are different.
func (ad *AnimationData) GetRecords(name string) []*AnimationDataRecord {
return ad.entries[name]
}
// Load loads the data into an AnimationData struct
func Load(data []byte) (*AnimationData, error) {
reader := bitstream.NewReader().FromBytes(data...)
animdata := &AnimationData{}
hashIdx := 0
animdata.entries = make(map[string][]*AnimationDataRecord)
for blockIdx := range animdata.blocks {
recordCount, err := reader.Next(32).Bits().AsUInt32()
if err != nil {
return nil, err
}
if recordCount > maxRecordsPerBlock {
return nil, fmt.Errorf("more than %d records in block", maxRecordsPerBlock)
}
records := make([]*AnimationDataRecord, recordCount)
for recordIdx := uint32(0); recordIdx < recordCount; recordIdx++ {
nameBytes, err := reader.Next(byteCountName).Bytes().AsBytes()
if err != nil {
return nil, err
}
if nameBytes[byteCountName-1] != byte(0) {
return nil, errors.New("animdata AnimationDataRecord name missing null terminator byte")
}
name := string(nameBytes)
name = strings.ReplaceAll(name, string(byte(0)), "")
animdata.hashTable[hashIdx] = hashName(name)
frames, err := reader.Next(32).Bits().AsUInt32()
if err != nil {
return nil, err
}
speed, err := reader.Next(2).Bytes().AsUInt16()
if err != nil {
return nil, err
}
_, err = reader.Next(byteCountSpeedPadding).Bytes().AsBytes()
if err != nil {
return nil, err
}
events := make(map[int]FrameEvent)
for eventIdx := 0; eventIdx < numEvents; eventIdx++ {
b, err := reader.Next(1).Bytes().AsByte()
if err != nil {
return nil, err
}
event := FrameEvent(b)
if event != EventNone {
events[eventIdx] = event
}
}
r := &AnimationDataRecord{
name,
frames,
speed,
events,
}
records[recordIdx] = r
if _, found := animdata.entries[r.name]; !found {
animdata.entries[r.name] = make([]*AnimationDataRecord, 0)
}
animdata.entries[r.name] = append(animdata.entries[r.name], r)
}
b := &block{
recordCount,
records,
}
animdata.blocks[blockIdx] = b
}
if reader.Position() != len(data) {
return nil, errors.New("unable to parse animation data")
}
return animdata, nil
}