-
Notifications
You must be signed in to change notification settings - Fork 3
/
reader.go
280 lines (247 loc) · 6.65 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package tf2vpk
import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path"
"sort"
"strings"
"time"
)
// Reader reads Titanfall 2 VPKs.
type Reader struct {
Root ValvePakDir
block map[ValvePakIndex]io.ReaderAt
close map[ValvePakIndex]io.Closer
}
// NewReader creates a new Reader reading from vpk.
func NewReader(vpk ValvePakRef) (*Reader, error) {
return NewReaderFunc(func(i ValvePakIndex) (io.ReaderAt, error) {
return os.Open(vpk.Resolve(i))
})
}
// NewReaderFunc creates a new Reader reading using the provided function. If
// the returned [io.ReaderAt] implements [io.Closer], it will be called when the
// Reader is closed.
func NewReaderFunc(open func(ValvePakIndex) (io.ReaderAt, error)) (*Reader, error) {
r := &Reader{
block: map[ValvePakIndex]io.ReaderAt{},
close: map[ValvePakIndex]io.Closer{},
}
// read dir index
dir, err := open(ValvePakIndexDir)
if err != nil {
return nil, fmt.Errorf("open vpk dir index: %w", err)
}
if err := r.Root.Deserialize(io.NewSectionReader(dir, 0, 1<<63-1)); err != nil {
return nil, fmt.Errorf("read root directory: %w", err)
}
// add dir block
chunkOffset, err := r.Root.ChunkOffset()
if err != nil {
return nil, fmt.Errorf("get chunk offset from root directory: %w", err)
}
if dir, ok := dir.(io.Closer); ok {
r.close[ValvePakIndexDir] = dir
}
r.block[ValvePakIndexDir] = io.NewSectionReader(dir, int64(chunkOffset), 1<<63-1)
// open blocks
var errs []error
for _, b := range r.Root.File {
if _, ok := r.block[b.Index]; !ok {
if x, err := open(b.Index); err != nil {
errs = append(errs, fmt.Errorf("open vpk block %s: %w", b.Index, err))
} else {
if x, ok := x.(io.Closer); ok {
r.close[b.Index] = x
}
r.block[b.Index] = x
}
}
}
if err := errors.Join(errs...); err != nil {
_ = r.Close()
return nil, fmt.Errorf("open blocks: %w", err)
}
return r, nil
}
// Close cleans files opened by the Reader.
func (r *Reader) Close() error {
var errs []error
if r.close != nil {
for i, x := range r.close {
if err := x.Close(); err != nil {
errs = append(errs, fmt.Errorf("close data reader for index %d: %w", i, err))
}
}
}
if err := errors.Join(errs...); err != nil {
return fmt.Errorf("close data readers: %w", err)
}
return nil
}
// OpenFile returns a new reader reading the contents of a specific file. The checksum is verified at EOF.
func (r *Reader) OpenFile(f ValvePakFile) (io.Reader, error) {
return f.CreateReader(r.block[f.Index])
}
// OpenFileParallel is like OpenFile, but but decompresses chunks in parallel
// using n goroutines going no more than n compressed chunks ahead.
func (r *Reader) OpenFileParallel(f ValvePakFile, n int) (io.Reader, error) {
return f.CreateReaderParallel(r.block[f.Index], n)
}
// OpenChunk returns a new reader reading the contents of a specific chunk.
func (r *Reader) OpenChunk(f ValvePakFile, c ValvePakChunk) (io.Reader, error) {
return c.CreateReader(r.block[f.Index])
}
// OpenChunkRaw returns a new reader reading the raw contents of a specific chunk.
func (r *Reader) OpenChunkRaw(f ValvePakFile, c ValvePakChunk) (io.Reader, error) {
return c.CreateReaderRaw(r.block[f.Index])
}
// OpenBlockRaw opens a new reader reading the contents of a specific block.
func (r *Reader) OpenBlockRaw(n ValvePakIndex) (io.ReaderAt, error) {
x, ok := r.block[n]
if !ok {
return nil, fmt.Errorf("block %#v out of range", n)
}
return x, nil
}
var (
_ fs.FS = (*Reader)(nil)
_ fs.File = (*readerFile)(nil)
_ fs.ReadDirFile = (*readerDir)(nil)
_ fs.DirEntry = (*readerInfo)(nil)
_ fs.FileInfo = (*readerInfo)(nil)
)
type readerFile struct {
info readerInfo
rc io.ReadCloser
}
func (f *readerFile) Stat() (fs.FileInfo, error) {
return &f.info, nil
}
func (f *readerFile) Read(b []byte) (n int, err error) {
return f.rc.Read(b)
}
func (f *readerFile) Close() error {
return f.rc.Close()
}
type readerDir struct {
info readerInfo
entry []*readerInfo
offset int
}
func (f *readerDir) Stat() (fs.FileInfo, error) {
return &f.info, nil
}
func (f *readerDir) Read(b []byte) (n int, err error) {
return 0, &fs.PathError{Op: "read", Path: f.info.name, Err: fs.ErrInvalid}
}
func (f *readerDir) Close() error {
return nil
}
func (d *readerDir) ReadDir(count int) ([]fs.DirEntry, error) {
n := len(d.entry) - d.offset
if n == 0 && count > 0 {
return nil, io.EOF
}
if count > 0 && n > count {
n = count
}
list := make([]fs.DirEntry, n)
for i := range list {
list[i] = d.entry[d.offset+i]
}
d.offset += n
return list, nil
}
type readerInfo struct {
name string
file *ValvePakFile
}
func (i *readerInfo) Info() (fs.FileInfo, error) {
return i, nil
}
func (i *readerInfo) Type() fs.FileMode {
return i.Mode().Type()
}
func (i *readerInfo) Name() string {
return i.name
}
func (i *readerInfo) Size() int64 {
var sz uint64
if !i.IsDir() {
for _, c := range i.file.Chunk {
sz += c.UncompressedSize
}
}
return int64(sz)
}
func (i *readerInfo) Mode() fs.FileMode {
if i.IsDir() {
return 0777 | fs.ModeDir
}
return 0666
}
func (i *readerInfo) ModTime() time.Time {
return time.Time{}
}
func (i *readerInfo) IsDir() bool {
return i.file == nil
}
func (i *readerInfo) Sys() interface{} {
if i.IsDir() {
return nil
}
return *i.file
}
// Open implements fs.FS.
func (r *Reader) Open(name string) (fs.File, error) {
if !fs.ValidPath(name) {
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
}
name = strings.TrimPrefix(name, "./")
for fi, f := range r.Root.File {
if f.Path == name {
if rc, err := r.OpenFile(f); err != nil {
return nil, &fs.PathError{Op: "open", Path: name, Err: err}
} else {
return &readerFile{readerInfo{path.Base(name), &r.Root.File[fi]}, io.NopCloser(rc)}, nil
}
}
}
things := map[string]*ValvePakFile{}
if name == "." {
for fi, f := range r.Root.File {
if i := strings.Index(f.Path, "/"); i < 0 {
things[f.Path] = &r.Root.File[fi]
} else {
things[f.Path[:i]] = nil
}
}
} else {
prefix := name + "/"
for fi, f := range r.Root.File {
if strings.HasPrefix(f.Path, prefix) {
tmp := f.Path[len(prefix):]
if i := strings.Index(tmp, "/"); i < 0 {
things[tmp] = &r.Root.File[fi]
} else {
things[tmp[:i]] = nil
}
}
}
if len(things) == 0 {
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist} // no file with the provided name, and the name isn't a dir prefix of other files
}
}
var dirents []*readerInfo
for thing, file := range things {
dirents = append(dirents, &readerInfo{thing, file})
}
sort.Slice(dirents, func(i, j int) bool {
return dirents[i].name < dirents[j].name
})
return &readerDir{readerInfo{name[strings.LastIndex(name, "/")+1:], nil}, dirents, 0}, nil
}