This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (103 loc) · 2.24 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
package main
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"fmt"
"io"
"os"
)
func check(e error) {
if e != nil {
panic(e)
}
}
type Entry struct {
data []byte
}
type Header struct {
magic []byte
ver uint32
len uint32
}
func main() {
if len(os.Args) < 3 {
panic("Not enough arguments. Usage: ./merge <list of files separated with space> <target file>")
}
numFiles := len(os.Args) - 2 // os.Args[0] is path to this program, the last item is the target file
files := make([]string, numFiles)
for i := 0; i < numFiles; i++ {
files[i] = os.Args[i+1]
}
fmt.Println("Reading caches...")
hdr := Header{}
entries := make(map[string]Entry)
first := true
for i := 0; i < len(files); i++ {
fmt.Println(files[i], " ")
fd, err := os.Open(files[i])
check(err)
magicNum := make([]byte, 4)
fd.Read(magicNum)
sMagicNum := string(magicNum)
if sMagicNum == "DXVK" {
fmt.Println("| Magic number OK")
} else {
panic("Invalid magic number")
}
ver := make([]byte, 4)
fd.Read(ver)
nVer := binary.LittleEndian.Uint32(ver)
fmt.Println("| Version: ", nVer)
eSize := make([]byte, 4)
fd.Read(eSize)
neSize := binary.LittleEndian.Uint32(eSize)
fmt.Println("| Entry length: ", neSize)
cnt := 0
if first == false {
if hdr.len != neSize {
panic("Entry length mismatch!")
}
if hdr.ver != nVer {
panic("Version mismatch!")
}
}
hdr.magic = magicNum
hdr.len = neSize
hdr.ver = nVer
first = false
for {
entry := make([]byte, neSize)
_, err := fd.Read(entry)
if err == io.EOF {
break
} else {
check(err)
e := Entry{entry}
h := sha256.Sum256(entry)
slice := h[:]
hash := base64.StdEncoding.EncodeToString(slice)
entries[hash] = e
cnt++
}
}
fmt.Println("| Loaded ", cnt, " entries")
fd.Close()
}
fmt.Println("Merging...")
fd, err := os.OpenFile(os.Args[len(os.Args)-1], os.O_CREATE|os.O_RDWR, 0644)
check(err)
fd.Write(hdr.magic)
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, hdr.ver)
binary.Write(buf, binary.LittleEndian, hdr.len)
fd.Write(buf.Bytes())
entriesCnt := 0
for _,v := range entries {
fd.Write(v.data)
entriesCnt++
}
fmt.Println("Written ", entriesCnt, " entries")
defer fd.Close()
}