forked from g0dsCookie/gopbo
/
pbo.go
75 lines (63 loc) · 1.55 KB
/
pbo.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
package pbo
import (
"io"
)
// File contains methods to handle PBO files.
type File struct {
Path string // Path contains the path to the current PBO file.
Files []*FileEntry // Files contains all files within the PBO file.
Headers map[string]string // Headers contains all header fields of the PBO file.
file *pboStream
}
// Load loads the PBO file.
func Load(path string) (file *File, err error) {
file = &File{
Path: path,
}
err = file.Load()
return
}
// Load (re)loads the PBO file.
func (f *File) Load() (err error) {
if f.file != nil {
f.Close()
}
if f.file, err = openPBO(f.Path); err != nil {
return
}
if err = f.file.validateFile(); err != nil {
return
}
if f.Headers, err = f.file.readHeaders(); err != nil {
return
}
if f.Files, err = f.file.readFileEntries(f); err != nil {
return
}
f.file.dataStart, err = f.file.Seek(0, io.SeekCurrent)
return
}
// CacheEnabled returns true if file caching is enabled.
func (f *File) CacheEnabled() bool { return f.file.cache != nil }
// ToggleCache enables/disables the file caching mechanism according to enable.
func (f *File) ToggleCache(enable bool) {
if enable {
if f.CacheEnabled() {
f.file.cache = make(map[string][]byte)
}
} else {
f.file.cache = nil
}
}
// ClearCache clears the file cache.
func (f *File) ClearCache() {
if f.file.cache != nil {
f.file.cache = make(map[string][]byte)
}
}
// Close closes the file stream and clears the file cache.
// You can't read any files after this anymore!
func (f *File) Close() {
f.file.Close()
f.file = nil
}