-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
204 lines (171 loc) · 3.92 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
203
204
package main
import (
"archive/zip"
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
"path"
"path/filepath"
"strings"
)
var baseKey = flag.String("base", "chitin.key", "base game key file")
var name = flag.String("name", "sod-dlc", "name of the mod to load")
func main() {
flag.Parse()
if !promptForPermission(*name) {
log.Printf("Exiting without making any changes")
return
}
// Step 1: Backup chitin.key
err := backup(*baseKey)
if err != nil {
log.Fatal(err)
}
// Step 2: Load in chitin.key
baseKeyIn, err := os.Open(*baseKey)
if err != nil {
log.Fatal(err)
}
defer baseKeyIn.Close()
bk, err := OpenKEY(baseKeyIn, "")
if err != nil {
log.Fatal(err)
}
baseKeyIn.Close()
// Step 3: Load mod key, and attempt to extract the mod if possible
modKeyIn, err := os.Open(*name + ".key")
var modDlcName string
if os.IsNotExist(err) {
log.Printf("No %s.key found, attempting to extract %s", *name, *name)
modDlcName, err = attemptExtractMod(*name)
if err != nil {
log.Fatal(err)
}
modKeyIn, err = os.Open(*name + ".key")
} else {
log.Fatalf("%s.key found, have you already run this tool?", *name)
}
if err != nil {
log.Fatal(err)
}
mk, err := OpenKEY(modKeyIn, "")
if err != nil {
log.Fatal(err)
}
modKeyIn.Close()
// Step 4: Merge our mod key into our base key
err = bk.MergeWith(mk, *name)
if err != nil {
log.Fatal(err)
}
// Step 5: Re-create our base key
outKey, err := os.Create(*baseKey)
if err != nil {
log.Fatal(err)
}
bk.Write(outKey)
outKey.Close()
// Step 6: Rename our mod to disable it
disabledDlcName := strings.Replace(modDlcName, ".zip", ".disabled", 1)
err = os.Rename(modDlcName, disabledDlcName)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Conversion complete.\n")
}
func backup(name string) error {
in, err := os.Open(name)
if err != nil {
return err
}
out, err := os.Create(name + ".bak")
if err != nil {
return err
}
_, err = io.Copy(out, in)
return err
}
func searchForMod(name string) (string, error) {
paths := []string{filepath.Join("dlc", name+".zip"), name + ".zip"}
for _, p := range paths {
_, err := os.Stat(p)
if err == nil {
return p, nil
}
}
return "", fmt.Errorf("Unable to find dlc file: %s.zip", name)
}
func attemptExtractMod(name string) (string, error) {
modPath, err := searchForMod(name)
if err != nil {
return "", err
}
r, err := zip.OpenReader(modPath)
if err != nil {
return "", err
}
defer r.Close()
os.MkdirAll(name, os.ModeDir|0777)
log.Printf("Extracting files")
for _, f := range r.File {
if f.FileInfo().IsDir() {
os.MkdirAll(f.Name, f.Mode())
continue
}
rc, err := f.Open()
if err != nil {
return "", err
}
fname := f.Name
chunks := strings.Split(f.Name, "/")
if strings.ToLower(chunks[0]) == "data" {
chunks[0] = name
}
if strings.ToLower(chunks[0]) == "mod.key" {
chunks[0] = name + ".key"
}
fname = path.Join(chunks...)
out, err := os.Create(fname)
if err != nil {
rc.Close()
return "", err
}
_, err = io.Copy(out, rc)
if err != nil {
rc.Close()
return "", err
}
out.Close()
rc.Close()
fmt.Printf(".")
}
fmt.Printf("\n")
log.Printf("Finished extracting files")
return modPath, nil
}
var msg = `
The Plan:
1: Backup your chitin.key as chitin.key.bak
2: Load our chitin.key
3: Load our %s key file
If unsuccessful, attempt to extract dlc/mod.zip, and create %s.key
The data/ folder inside %s.zip will be placed inside the folder %s/
4: Merge our mod %s into the chitin.key
5: Overwrite our chitin.key
6: Rename dlc/%s.zip to dlc/%s.disabled
`
var prompt = "Continue? [y/n]"
func promptForPermission(name string) bool {
fmt.Printf(msg, name, name, name, name, name, name, name)
reader := bufio.NewReader(os.Stdin)
input := ""
for !(input == "Y" || input == "y" || input == "N" || input == "n") {
fmt.Printf(prompt)
input, _ = reader.ReadString('\n')
input = strings.TrimSpace(input)
}
return input == "Y" || input == "y"
}