-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
218 lines (185 loc) · 3.67 KB
/
reader.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package gma
import (
"encoding/binary"
"encoding/json"
"errors"
"io"
)
type Reader struct {
src Source
}
// Descriptions of newer addons are in JSON format
type jsonDescription struct {
Description string
Type AddonType
Tags []AddonTag
}
func (r *Reader) readByte() (byte, error) {
b, err := r.readBytes(1)
return b[0], err
}
func (r *Reader) readBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := r.src.Read(b)
return b, err
}
func (r *Reader) readUint32() (uint32, error) {
b, err := r.readBytes(4)
if err != nil {
return 0, err
}
return binary.LittleEndian.Uint32(b), nil
}
func (r *Reader) readUint64() (uint64, error) {
b, err := r.readBytes(8)
if err != nil {
return 0, err
}
return binary.LittleEndian.Uint64(b), nil
}
func (r *Reader) readInt32() (int32, error) {
b, err := r.readBytes(4)
if err != nil {
return 0, err
}
return int32(binary.LittleEndian.Uint32(b)), nil
}
func (r *Reader) readString() (string, error) {
var (
err error
b = make([]byte, 1)
str string
)
// Read byte by byte until we get a 0 or EOF
for {
_, err = r.src.Read(b)
if err != nil {
if err == io.EOF {
err = nil
}
break
}
if b[0] == 0 {
break
}
str += string(b)
}
return str, err
}
// ReadAddon tries to read an addon from the source.
func (r *Reader) ReadAddon() (*Addon, error) {
var (
err error
ident []byte
parsedDescription *jsonDescription
addon = &Addon{}
)
_, err = r.src.Seek(0, io.SeekStart)
if err != nil {
return nil, err
}
// Ident
ident, err = r.readBytes(4)
if err != nil {
return nil, err
}
if string(ident) != Ident {
return nil, errors.New("not a valid GMA file")
}
// Format version
addon.FormatVersion, err = r.readByte()
if err != nil {
return nil, err
}
if addon.FormatVersion > Version {
return nil, errors.New("unsupported addon version")
}
// Steam ID
addon.SteamID, err = r.readUint64()
if err != nil {
return nil, err
}
// Timestamp
addon.Timestamp, err = r.readUint64()
if err != nil {
return nil, err
}
// Required content
if addon.FormatVersion > 1 {
var s string
for {
s, err = r.readString()
if err != nil {
return nil, err
}
if len(s) == 0 {
break
}
addon.RequiredContent += s
}
}
// Name
addon.Name, err = r.readString()
if err != nil {
return nil, err
}
// Description
addon.Description, err = r.readString()
if err != nil {
return nil, err
}
// Try to parse description as json. Ignore errors, as old addons have a plain text description
parsedDescription = &jsonDescription{}
err = json.Unmarshal([]byte(addon.Description), parsedDescription)
if err == nil {
addon.Description = parsedDescription.Description
addon.Type = parsedDescription.Type
addon.Tags = parsedDescription.Tags
}
// Author
addon.Author, err = r.readString()
if err != nil {
return nil, err
}
// Version
addon.Version, err = r.readInt32()
if err != nil {
return nil, err
}
// File entries
var offset uint64 = 0
for {
entry := AddonFileEntry{}
entry.ID, err = r.readUint32()
if err != nil {
return nil, err
}
if entry.ID == 0 {
break
}
entry.Name, err = r.readString()
if err != nil {
return nil, err
}
entry.Size, err = r.readUint64()
if err != nil {
return nil, err
}
entry.CRC, err = r.readUint32()
if err != nil {
return nil, err
}
entry.Offset = offset
offset += entry.Size
addon.Files = append(addon.Files, entry)
}
addon.Src = r.src
addon.FileBlockOffset, err = r.src.Seek(0, io.SeekCurrent)
if err != nil {
return nil, err
}
return addon, nil
}
func NewReader(src Source) *Reader {
return &Reader{src: src}
}