forked from peek6/UEcastoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathucas.go
94 lines (87 loc) · 2.82 KB
/
ucas.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
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
)
func (d *UTocData) unpackFile(fdata *GameFileMetaData, blockData *[][]byte, outDir string) error {
os.MkdirAll(outDir, 0700)
bdata := *blockData
outputData := []byte{}
for i := 0; i < len(bdata); i++ {
method := d.compressionMethods[fdata.compressionBlocks[i].CompressionMethod]
decomp := getDecompressionFunction(method)
if decomp == nil {
return errors.New(fmt.Sprintf("decompression method %s not known", method))
}
newData, err := decomp(&(bdata[i]), fdata.compressionBlocks[i].GetUncompressedSize())
if err != nil {
return err
}
outputData = append(outputData, (*newData)...)
}
// ensure path exists to the file
fpath := filepath.Clean(outDir + fdata.filepath)
directory := filepath.Dir(fpath)
os.MkdirAll(directory, 0700)
// write the actual data to the new file
err := os.WriteFile(fpath, outputData, 0644)
return err
}
func (d *UTocData) matchRegex(regex string) *[]GameFileMetaData {
filesToUnpack := []GameFileMetaData{}
for _, v := range d.files {
match, err := regexp.MatchString(regex, v.filepath)
if err != nil {
return &filesToUnpack
}
// exclude special "dependencies" file, as it's not meant to be directly unpacked
// for unpacking that file, have a look at the function to construct the manifest!
if match && v.filepath != DepFileName {
filesToUnpack = append(filesToUnpack, v)
}
}
return &filesToUnpack
}
func (d *UTocData) unpackUcasFiles(ucasPath string, outDir string, regex string) (filesUnpacked int, err error) {
outDir += d.mountPoint // adjust for mountpoint
filesUnpacked = 0
// read the file
openUcas, err := os.Open(ucasPath)
if err != nil {
return filesUnpacked, err
}
defer openUcas.Close()
filesToUnpack := *(d.matchRegex(regex))
// each "file" is built from compression blocks
// extract those compression blocks from the .ucas file and use those for unpacking
// Since there's one place where the .ucas file is actually read, it can act as a work divider.
// that may make it possible to make it run multithreaded in the future!
for _, v := range filesToUnpack {
var compressionblockData [][]byte
for _, b := range v.compressionBlocks {
_, err = openUcas.Seek(int64(b.GetOffset()), 0)
if err != nil {
return filesUnpacked, err
}
buf := make([]byte, b.GetCompressedSize())
readBytes, err := openUcas.Read(buf)
if err != nil {
return filesUnpacked, err
}
if uint32(readBytes) != b.GetCompressedSize() {
return filesUnpacked, errors.New("could not read the correct size")
}
compressionblockData = append(compressionblockData, buf)
}
// all separate blocks collected for file unpacking
err = d.unpackFile(&v, &compressionblockData, outDir)
if err != nil {
return filesUnpacked, err
}
filesUnpacked++
}
return len(filesToUnpack), nil
}