-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
202 lines (172 loc) · 5.54 KB
/
main.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
// Command tf2-vpkunpack unpacks or initializes a new vpk for modification and
// repacking.
package main
import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
"github.com/pg9182/tf2vpk/internal"
"github.com/pg9182/tf2vpk/vpkutil"
"github.com/spf13/pflag"
)
var (
ResolveOpen = vpkutil.NewCLIResolveOpen(pflag.CommandLine, 1, true)
VPKFlagsExplicit = pflag.Bool("vpkflags-explicit", false, "Do not optimize vpkflags for inheritance; generate one line for each file")
VPKIgnoreEmpty = pflag.Bool("vpkignore-no-default", false, "Do not add default vpkignore entries")
Threads = pflag.IntP("threads", "j", runtime.NumCPU(), "The number of decompression threads to use while verifying checksums (0 to only decompress chunks as they are read) (defaults to the number of cores)")
IncludeExclude = vpkutil.NewCLIIncludeExclude(pflag.CommandLine)
Help = pflag.Bool("help", false, "Show this help message")
)
func main() {
pflag.Parse()
args := pflag.Args()
if len(args) == 0 || !ResolveOpen.ArgCheck() || *Help {
fmt.Fprintf(os.Stderr, "usage: %s [options] empty_output_path %s\n\noptions:\n%s", os.Args[0], ResolveOpen.ArgHelp(), pflag.CommandLine.FlagUsages())
if !*Help {
os.Exit(2)
}
return
}
if *Threads < 0 {
*Threads = 0
}
if *Threads > runtime.NumCPU() {
runtime.GOMAXPROCS(*Threads)
}
vpkOut := args[0]
if len(args) == 1 {
fmt.Printf("initializing new vpk in %q\n", vpkOut)
} else {
fmt.Printf("unpacking vpk to %q\n", vpkOut)
}
_, r, err := ResolveOpen.ResolveOpen()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if *VPKFlagsExplicit && r != nil {
fmt.Printf("... generating .vpkflags (without inheritance)\n")
} else {
fmt.Printf("... generating .vpkflags\n")
}
var vpkFlags vpkutil.VPKFlags
if r != nil {
if *VPKFlagsExplicit {
if err := vpkFlags.GenerateExplicit(r.Root); err != nil {
fmt.Fprintf(os.Stderr, "error: generate vpkflags without inheritance: %v\n", err)
os.Exit(1)
}
} else {
if err := vpkFlags.Generate(r.Root); err != nil {
fmt.Fprintf(os.Stderr, "error: generate vpkflags: %v\n", err)
os.Exit(1)
}
}
if err := vpkFlags.Test(r.Root); err != nil {
fmt.Println(vpkFlags.String())
panic(fmt.Errorf("BUG: test generated vpkflags: %w", err))
}
}
if *VPKIgnoreEmpty {
fmt.Printf("... generating .vpkignore (without default entries)\n")
} else {
fmt.Printf("... generating .vpkignore\n")
}
var vpkIgnore vpkutil.VPKIgnore
if !*VPKIgnoreEmpty {
vpkIgnore.AddDefault()
}
if r != nil {
if err := vpkIgnore.AddAutoExclusions(r.Root); err != nil {
fmt.Fprintf(os.Stderr, "error: generate vpkignore: %v\n", err)
os.Exit(1)
}
}
fmt.Printf("... creating output directory\n")
if err := os.Mkdir(vpkOut, 0777); err != nil && !errors.Is(err, fs.ErrExist) {
fmt.Fprintf(os.Stderr, "error: create output directory: %v\n", err)
os.Exit(1)
}
if dis, err := os.ReadDir(vpkOut); err != nil {
fmt.Fprintf(os.Stderr, "error: list output directory: %v\n", err)
os.Exit(1)
} else {
for _, di := range dis {
if !vpkIgnore.Match(di.Name()) {
fmt.Fprintf(os.Stderr, "error: output directory must not exist or be empty (other than ignored files), found %q\n", di.Name())
os.Exit(1)
}
}
}
fmt.Printf("... saving .vpkflags\n")
if err := os.WriteFile(filepath.Join(vpkOut, ".vpkflags"), []byte(vpkFlags.String()), 0666); err != nil {
fmt.Fprintf(os.Stderr, "error: write .vpkflags: %v\n", err)
os.Exit(1)
}
fmt.Printf("... saving .vpkignore\n")
if err := os.WriteFile(filepath.Join(vpkOut, ".vpkignore"), []byte(vpkIgnore.String()), 0666); err != nil {
fmt.Fprintf(os.Stderr, "error: write .vpkignore: %v\n", err)
os.Exit(1)
}
fmt.Println()
var excludedCount int
if r != nil {
for i, f := range r.Root.File {
if skip, err := IncludeExclude.Skip(f); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
} else if skip {
excludedCount++
fmt.Printf("[%4d/%4d] %s (excluded)\n", i+1, len(r.Root.File), f.Path)
continue
}
var uncompressed uint64
for _, c := range f.Chunk {
uncompressed += c.UncompressedSize
}
fmt.Printf("[%4d/%4d] %s (%s)\n", i+1, len(r.Root.File), f.Path, internal.FormatBytesSI(int64(uncompressed)))
outPath := filepath.Join(vpkOut, filepath.FromSlash(f.Path))
if err := os.MkdirAll(filepath.Dir(outPath), 0777); err != nil {
fmt.Fprintf(os.Stderr, "error: create %q: %v\n", outPath, err)
os.Exit(1)
}
tf, err := os.CreateTemp(vpkOut, ".vpk*")
if err != nil {
fmt.Fprintf(os.Stderr, "error: create temp file: %v\n", err)
os.Exit(1)
}
defer tf.Close()
fr, err := r.OpenFileParallel(f, *Threads)
if err != nil {
os.Remove(tf.Name())
fmt.Fprintf(os.Stderr, "error: read vpk file %q: %v\n", f.Path, err)
os.Exit(1)
}
if _, err := io.Copy(tf, fr); err != nil {
os.Remove(tf.Name())
fmt.Fprintf(os.Stderr, "error: extract vpk file %q: %v\n", f.Path, err)
os.Exit(1)
}
if err := tf.Close(); err != nil {
os.Remove(tf.Name())
fmt.Fprintf(os.Stderr, "error: extract vpk file %q: %v\n", f.Path, err)
os.Exit(1)
}
if err := os.Rename(tf.Name(), outPath); err != nil {
fmt.Fprintf(os.Stderr, "error: extract vpk file %q: rename temp file: %v\n", f.Path, err)
os.Exit(1)
}
// TODO: maybe extract files in parallel instead of using a parallel reader, might be faster for small files
}
}
fmt.Println()
if excludedCount != 0 {
fmt.Printf("success (%d files excluded by command-line filter)\n", excludedCount)
} else {
fmt.Printf("success\n")
}
}