/
frostract.go
500 lines (448 loc) · 15.5 KB
/
frostract.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package main
import (
"bufio"
"bytes"
"compress/gzip"
"encoding/binary"
"encoding/hex"
"flag"
"io"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/Mystik738/frostexture"
"github.com/Mystik738/frostlang"
"github.com/aviddiviner/go-murmur"
log "github.com/sirupsen/logrus"
)
const bytesPerFile = 29
const oldBytesPerFile = 17
const fphookFileName = "FPHook.log"
func main() {
log.SetLevel(log.InfoLevel)
overwrite := flag.Bool("o", false, "Overwrite existing files")
help := flag.Bool("h", false, "Display this help")
dds := flag.Bool("d", false, "Skip repairing dds files. Setting this flag will also skip conversion to png")
png := flag.Bool("p", false, "Skip converting dds to png")
pre121 := flag.Bool("v", false, "Idx is from before version 1.2.1")
langToJSON := flag.Bool("j", false, "Skip converting lang files to json")
compress := flag.Bool("c", false, "Instead of extracting, compress existing subdirectories and idx files into new idx and dat files")
files := flag.String("f", "", "File(s) to extract or directories to compress")
flag.Parse()
if !*compress {
Frostract(*overwrite, *help, *dds, *png, *pre121, *langToJSON, *files)
} else {
Frostpress(*overwrite, *pre121, *files)
}
}
// Frostpress compresses a directory into a dat and .idx file
func Frostpress(overwrite, pre121 bool, filespec string) {
parentDir, err := os.Getwd()
checkError(err)
//Get all directories that are children of the current directory
if filespec == "" {
filespec = "*"
}
files, err := filepath.Glob(filespec)
checkError(err)
var dirs []string
for _, file := range files {
info, err := os.Stat(file)
checkError(err)
if info.IsDir() {
dirs = append(dirs, file)
log.Debugf("Adding %v", info.Name())
}
}
//get our hashes
hashToFileName := readFPHook(true)
//For each directory, compress
for _, dir := range dirs {
//If there are json files, convert them back to the lang files
frostlang.ConvertJSONToLang(dir, overwrite)
var files []string
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
//We can ignore the custom json, dds, and png files that this utility writes
if filepath.Ext(path) == ".json" || filepath.Ext(path) == ".dds" || filepath.Ext(path) == ".png" {
return nil
}
files = append(files, info.Name())
return nil
})
log.Infof("Compressing %v directory", dir)
idxFileName := filepath.Join(parentDir, dir+".idx")
newIdxFileName := filepath.Join(parentDir, dir+"_new.idx")
datName := filepath.Join(parentDir, dir+"_new.dat")
//We don't want to overwrite existing files unless the overwrite flag is set
if _, err := os.Stat(newIdxFileName); overwrite || err != nil {
} else {
log.Fatalf("%v already exists", newIdxFileName)
}
if _, err := os.Stat(datName); overwrite || err != nil {
} else {
log.Fatalf("%v already exists", datName)
}
//We should keep the dat file in the same order as the original, but do we need to?
content, err := os.ReadFile(idxFileName)
checkError(err)
content = content[11:]
offsetToHash := make(map[uint64]string)
for i := 0; i < len(content); i += bytesPerFile {
//Get our information out of the idx file
hash := content[i : i+4]
offsetToHash[binary.LittleEndian.Uint64(content[i+20:i+28])] = hex.EncodeToString(hash)
}
log.Debugf("OffsetsToHash: %v", offsetToHash)
offsetOrder := make(map[uint64]string, 0)
offsets := make([]uint64, 0)
//Need to find original dat order
for i := 0; i < len(content); i += bytesPerFile {
hash := hex.EncodeToString(content[i : i+4])
offset := binary.LittleEndian.Uint64(content[i+20 : i+28])
offsets = append(offsets, offset)
offsetOrder[offset] = hash
}
sort.Slice(offsets, func(i, j int) bool { return offsets[i] < offsets[j] })
log.Debugf("Offsets: %v", offsets)
log.Debugf("OffsetsOrder: %v", offsetOrder)
orderedHashes := make([][]byte, 0)
for _, offset := range offsets {
decoded, err := hex.DecodeString(offsetOrder[offset])
checkError(err)
orderedHashes = append(orderedHashes, decoded)
}
offset := 0
hashInfo := make(map[string][]byte, 0)
for _, hash := range orderedHashes {
file, ok := hashToFileName[hex.EncodeToString(hash)]
if !ok {
file = hex.EncodeToString(hash)
}
fileName := filepath.Join(parentDir, dir, file)
tmpFileName := filepath.Join(parentDir, dir, file+".gz")
info, err := os.Stat(datName)
checkError(err)
//Write to a temporary gz file. Without this, gzip doesn't close the file correctly.
data, err := os.ReadFile(fileName)
checkError(err)
tmp, err := os.OpenFile(tmpFileName, os.O_WRONLY|os.O_CREATE, 0660)
checkError(err)
gzdat, err := gzip.NewWriterLevel(tmp, gzip.DefaultCompression)
checkError(err)
gzdat.Header.OS = byte(0)
fw := bufio.NewWriter(gzdat)
fw.Write(data)
uncompressed := len(data)
fw.Flush()
gzdat.Close()
tmp.Close()
//Read from our temp file and move the data into the .dat
data, err = os.ReadFile(tmpFileName)
checkError(err)
dat, err := os.OpenFile(datName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
checkError(err)
datSizeBefore := info.Size()
//Frostpunk doesn't have checksum and size footer in the dat file
dat.Write(data[:len(data)-8])
dat.Close()
os.Remove(tmpFileName)
//Get our compressed size
datinfo, err := os.Stat(datName)
checkError(err)
compressed := datinfo.Size() - datSizeBefore
//store the file information in a map
stringHash := hex.EncodeToString(hash)
log.Debugf("%v %v %v %v\n", stringHash, offset, compressed, uncompressed)
writeByte := hash
hashInfo[stringHash] = writeByte
writeCompressed := make([]byte, 8)
binary.LittleEndian.PutUint64(writeCompressed, uint64(compressed))
hashInfo[stringHash] = append(hashInfo[stringHash], writeCompressed...)
writeUncompressed := make([]byte, 8)
binary.LittleEndian.PutUint64(writeUncompressed, uint64(uncompressed))
hashInfo[stringHash] = append(hashInfo[stringHash], writeUncompressed...)
writeOffset := make([]byte, 8)
binary.LittleEndian.PutUint64(writeOffset, uint64(offset))
hashInfo[stringHash] = append(hashInfo[stringHash], writeOffset...)
offset += int(compressed)
}
//idx written in original order
idx, err := os.Create(newIdxFileName)
checkError(err)
defer idx.Close()
//write our header information
idxHeader := []byte{0x00, 0x02, 0x01}
checkError(err)
idx.Write(idxHeader)
headerContent := make([]byte, 8)
binary.LittleEndian.PutUint64(headerContent, uint64(len(files)))
idx.Write(headerContent)
for i := 0; i < len(content); i += bytesPerFile {
hash := hex.EncodeToString(content[i : i+4])
isCompressed := content[i+28]
hashInfo[hash] = append(hashInfo[hash], isCompressed)
idx.Write(hashInfo[hash])
}
}
}
func Frostract(overwrite, help, dds, png, pre121, langToJSON bool, filespec string) {
log.SetLevel(log.InfoLevel)
if help {
helptext := `frostract is a utility for extracting files from Frostpunk idx and dat archives. Requires magick to be installed to convert dds to png. FPHook.log must be in same directory for filename lookup.
Usage: frostract [flags] [options]
Flags:
-h Display this help
-d Skip repairing dds files. Setting this flag will also skip conversion to png
-p Skip converting dds to png
-j Skip converting lang files to json
-o Overwrite existing files
-v Idx is from before version 1.2.1
-c Instead of extracting, compress existing subdirectories and idx files into new idx and dat files
Options:
-f <filespec> Extract specific idx files or compress specific directories
`
log.Info(helptext)
} else {
startTime := time.Now()
dir, err := os.Getwd()
//This is more a heuristic value to increase efficiency rather than an exact science
concurrency := 8
checkError(err)
log.Debugf("Working directory is %v", dir)
//Make our hashmap
hashToFileName := readFPHook(true)
//Walk our current path to get idx files
if filespec == "" {
filespec = "*.idx"
}
files, err := filepath.Glob(filespec)
checkError(err)
var matchingFiles []string
for _, file := range files {
info, err := os.Stat(file)
checkError(err)
if !info.IsDir() && filepath.Ext(file) == ".idx" {
log.Debugf("Adding %v", file)
matchingFiles = append(matchingFiles, file)
}
}
for _, idxFileName := range matchingFiles {
log.Infof("Extracting %v", idxFileName)
parentDirName := filepath.Dir(idxFileName)
baseName := filepath.Base(idxFileName)
stemName := baseName[:len(baseName)-len(filepath.Ext(baseName))]
datFileName := filepath.Join(parentDirName, stemName+".dat")
if _, err := os.Stat(stemName); os.IsNotExist(err) {
wdirStat, err := os.Stat(dir)
checkError(err)
err = os.Mkdir(stemName, wdirStat.Mode())
checkError(err)
}
//Read in the whole idx file so we can determine number of files in archive
content, err := os.ReadFile(idxFileName)
checkError(err)
//Just open a pointer to the dat file so we can read subsections later
datFile, err := os.Open(datFileName)
checkError(err)
defer datFile.Close()
//Metadata
//All .idx files start with 00 02 01
//Then uint64 for number of files
log.Infof("%v has %v files", idxFileName, binary.LittleEndian.Uint64(content[3:11]))
content = content[11:]
//Set up our concurrency to wait for the number of files
sem := make(chan bool, concurrency)
//indice over the idx file, jumping an entry's number of bytes at a time
if pre121 && len(content)%oldBytesPerFile == 0 {
for i := 0; i < len(content); i += oldBytesPerFile {
//Get our information out of the idx file
hash := content[i : i+4]
compressed := binary.LittleEndian.Uint32(content[i+4 : i+8])
uncompressed := binary.LittleEndian.Uint32(content[i+8 : i+12])
offset := binary.LittleEndian.Uint32(content[i+12 : i+16])
isCompressed := content[i+16] == 1
//Since the files are independent, we can run through them concurrently
sem <- true
go func(hash []byte, uncompressed uint32, compressed uint32, offset uint32, isCompressed bool) {
defer func() { <-sem }()
fileName := hex.EncodeToString(hash)
if _, ok := hashToFileName[fileName]; ok {
fileName = hashToFileName[fileName]
}
filePath := filepath.Join(stemName, fileName)
if _, err := os.Stat(filePath); overwrite || err != nil {
outFile, err := os.Create(filePath)
checkError(err)
bytesToRead := compressed
//If the file is compressed, decompress using gzip
if isCompressed {
bytesRead := make([]byte, bytesToRead)
datFile.ReadAt(bytesRead, int64(offset))
b := bytes.NewReader(bytesRead)
r, err := gzip.NewReader(b)
log.Debug(fileName)
if err == nil {
io.Copy(outFile, r)
r.Close()
} else {
outFile.Write(bytesRead)
}
} else { //else just write out
bytesRead := make([]byte, bytesToRead)
datFile.ReadAt(bytesRead, int64(offset))
outFile.Write(bytesRead)
}
err = outFile.Close()
checkError(err)
}
}(hash, compressed, uncompressed, offset, isCompressed)
}
} else if len(content)%bytesPerFile == 0 {
for i := 0; i < len(content); i += bytesPerFile {
//Get our information out of the idx file
hash := content[i : i+4]
compressed := binary.LittleEndian.Uint64(content[i+4 : i+12])
uncompressed := binary.LittleEndian.Uint64(content[i+12 : i+20])
offset := binary.LittleEndian.Uint64(content[i+20 : i+28])
isCompressed := content[i+28] == 1
log.Debugf("%v %v %v %v %v\n", hex.EncodeToString(hash), offset, compressed, uncompressed, isCompressed)
//Since the files are independent, we can run through them concurrently
sem <- true
go func(hash []byte, uncompressed uint64, compressed uint64, offset uint64, isCompressed bool) {
defer func() { <-sem }()
fileName := hex.EncodeToString(hash)
if _, ok := hashToFileName[fileName]; ok {
fileName = hashToFileName[fileName]
}
filePath := filepath.Join(stemName, fileName)
if _, err := os.Stat(filePath); overwrite || err != nil {
outFile, err := os.Create(filePath)
checkError(err)
bytesToRead := compressed
bytesRead := make([]byte, bytesToRead)
datFile.ReadAt(bytesRead, int64(offset))
//If the file is compressed, decompress using gzip
if isCompressed {
b := bytes.NewReader(bytesRead)
r, err := gzip.NewReader(b)
//Some files are 0 or 4 bytes, so they don't uncompress
if err != nil {
outFile.Write(bytesRead)
} else {
io.Copy(outFile, r)
r.Close()
}
} else { //else just
outFile.Write(bytesRead)
}
err = outFile.Close()
checkError(err)
}
}(hash, compressed, uncompressed, offset, isCompressed)
}
} else {
log.Error("Content size doesn't match data type")
return
}
//wait for our concurrency before finishing
for i := 0; i < cap(sem); i++ {
sem <- true
}
}
if !langToJSON {
log.Info("Converting Lang files to JSON")
frostlang.ConvertLangToJSON(dir+"/localizations/", overwrite)
}
if !dds {
if !png {
log.Info("Repairing DDS files and creating PNGs")
frostexture.ConvertToDDSandPNG(dir+"/textures-s3/", overwrite, true)
} else {
log.Info("Repairing DDS files")
frostexture.ConvertToDDSandPNG(dir+"/textures-s3/", overwrite, false)
}
}
log.Info("Process ran in ", time.Since(startTime))
}
}
// Simple function to panic if there's an error
func checkError(err error) {
if err != nil {
log.Panic(err)
panic(err)
}
}
// Read in FPHook to a map
func readFPHook(hashToFile bool) map[string]string {
var err error
dir, err := os.Getwd()
checkError(err)
// first, look for FPHook in the working directory
fphookFilePath := filepath.Join(dir, fphookFileName)
_, err = os.Stat(fphookFilePath)
if os.IsNotExist(err) {
// next, look for FPHook in the directory with the frostract binary
// TODO handle symlinks
var execFileName string
execFileName, err = os.Executable()
checkError(err)
fphookFilePath = filepath.Join(filepath.Dir(execFileName), fphookFileName)
_, err = os.Stat(fphookFilePath)
}
fpmap := make(map[string]string)
lineCounts := make(map[string]int)
if !os.IsNotExist(err) {
//Get our FPHook file to get hashes
f, err := os.Open(fphookFilePath)
checkError(err)
r := bufio.NewReader(f)
lineCount := 0
for {
line, isPrefix, err := r.ReadLine()
lineCount++
if err != nil || isPrefix {
break
}
splits := strings.Split(string(line), "/")
fileName := splits[len(splits)-1]
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, murmur.MurmurHash2([]byte(line), 0))
if hashToFile {
fpmap[hex.EncodeToString(b)] = fileName
} else {
fpmap[fileName] = hex.EncodeToString(b)
}
if _, ok := lineCounts[string(line)]; ok {
lineCounts[string(line)]++
} else {
lineCounts[string(line)] = 1
}
}
f.Close()
//This is just to trim out duplicates, maybe flag to turn off
if len(lineCounts) < lineCount {
// TODO handle FPHook file is not writable
f, err = os.Create(filepath.Join(dir, fphookFileName))
checkError(err)
keys := make([]string, 0, len(lineCounts))
for k := range lineCounts {
keys = append(keys, k)
}
sort.Strings(keys)
for _, line := range keys {
f.Write([]byte(line))
f.Write([]byte("\r\n"))
}
f.Close()
}
f.Close()
} else {
log.Warn("FPHook.log does not exist in current directory, cannot adjust filenames")
}
return fpmap
}