-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmbdcps2.go
334 lines (315 loc) · 11.2 KB
/
mbdcps2.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
package main
import (
"archive/zip"
_ "embed"
"flag"
"fmt"
"os"
"path/filepath"
"github.com/MBDesu/mbdcps2/Resources"
"github.com/MBDesu/mbdcps2/cps2crypt"
"github.com/MBDesu/mbdcps2/cps2rom"
"github.com/MBDesu/mbdcps2/tui"
file_utils "github.com/MBDesu/mbdcps2/utils"
)
// Ideal Workflow
//
// Decrypt `.zip` to `.bin`, edit hex, encrypt to `.zip`, diff with clean `.zip` to generate `.mra` patches
//
// Then encrypt should take a `.bin` and a `.zip` as input, adding the missing files back to the `.zip`
//
// | Mode | Flag | Priority | Input File Format | Output File Format | ROM set name |
// | ---------------- | :--: | :------: | :---------------: | :----------------: | :----------: |
// | Concat | c | 5 | .zip | .bin | Required |
// | Decrypt | d | 1 | .zip | .bin | Required |
// | Encrypt | e | 2 | .bin+.zip | .zip | Required |
// | Generate .mra | m | 4 | .zip | .mra | Required |
// | Patch | p | 3 | .zip | .zip | Required |
// | Decode gfx | g | 6 | .zip | .bin | Required |
//
// | Argument | Flag | Required With |
// | :-------------- | :--: | :-----------------: |
// | Output filepath | o | N/A |
// | Input zip | z | c, d, g, m, p |
// | Input bin | b | e |
// | ROM set name | n | c, d, e, g, m, p |
// | Input diff zip | x | m |
type Flags struct {
isConcatMode bool
isDecryptMode bool
isEncryptMode bool
isGuiMode bool
isPatchMode bool
isMraMode bool
isSwapMode bool
romSetName string
binFilepath string
outputFilepath string
zipFilepath string
diffZipFilepath string
mraFilepath string
}
var flags Flags
func parseFlags() {
binFile := flag.String("b", "", Resources.Strings.Flag["binFileDesc"])
concatMode := flag.Bool("c", false, Resources.Strings.Flag["concatModeDesc"])
decryptMode := flag.Bool("d", false, Resources.Strings.Flag["decryptModeDesc"])
encryptMode := flag.Bool("e", false, Resources.Strings.Flag["encryptModeDesc"])
diffMode := flag.Bool("m", false, Resources.Strings.Flag["diffModeDesc"])
guiMode := flag.Bool("g", false, Resources.Strings.Flag["guiModeDesc"])
swapMode := flag.Bool("w", false, Resources.Strings.Flag["swapModeDesc"])
romName := flag.String("n", "", Resources.Strings.Flag["romSetNameDesc"])
outputFile := flag.String("o", "", Resources.Strings.Flag["outputFileDesc"])
patchMode := flag.Bool("p", false, Resources.Strings.Flag["patchModeDesc"])
mraFile := flag.String("r", "", Resources.Strings.Flag["mraFileDesc"])
diffZipFile := flag.String("x", "", Resources.Strings.Flag["diffZipDesc"])
zipFile := flag.String("z", "", Resources.Strings.Flag["zipFileDesc"])
flag.Parse()
flags = Flags{*concatMode, *decryptMode, *encryptMode, *guiMode, *patchMode, *diffMode, *swapMode, *romName, *binFile, *outputFile, *zipFile, *diffZipFile, *mraFile}
validateFlags()
}
func validateFlags() {
if flags.isGuiMode {
return
}
zipFileRequired := flags.isDecryptMode || flags.isEncryptMode || flags.isMraMode || flags.isPatchMode || flags.isConcatMode
if zipFileRequired && flags.zipFilepath == "" {
flag.Usage()
throw(Resources.Strings.Error["noRomFile"])
}
binFileRequired := flags.isEncryptMode || flags.isSwapMode
if binFileRequired && flags.binFilepath == "" {
flag.Usage()
throw(Resources.Strings.Error["noBinFile"])
}
romSetNameRequired := flags.isDecryptMode || flags.isEncryptMode || flags.isMraMode || flags.isPatchMode || flags.isConcatMode
if romSetNameRequired && flags.romSetName == "" {
flag.Usage()
throw(Resources.Strings.Error["noRomSetName"])
}
diffZipFileRequired := flags.isMraMode
if diffZipFileRequired && flags.diffZipFilepath == "" {
flag.Usage()
throw(Resources.Strings.Error["noDiffRomFile"])
}
mraFileRequired := flags.isPatchMode
if mraFileRequired && flags.mraFilepath == "" {
flag.Usage()
throw(Resources.Strings.Error["noMraFile"])
}
}
func throw(errorString string) {
fmt.Println(Resources.LogText.Red(Resources.LogText.Bold("[!]")) + " " + errorString)
os.Exit(1)
}
func check(err error) {
if err != nil {
throw(err.Error())
}
}
func gui() {
command := tui.StartTui()
switch *command.Name {
case "Decrypt":
decrypt(command.RomSetName, command.ZipFilepath)
case "Encrypt":
encrypt(command.RomSetName, command.ZipFilepath, command.BinFilepath)
case "Patch":
patch(command.RomSetName, command.ZipFilepath, command.MraFilepath)
case "Diff":
diff(command.RomSetName, command.ZipFilepath, command.DiffZipFilepath)
}
}
func concat() {
if flags.outputFilepath == "" {
flags.outputFilepath = flags.romSetName + ".bin"
}
romZipFile, romDef, err := cps2rom.ParseRomZip(flags.zipFilepath, flags.romSetName)
check(err)
defer romZipFile.Close()
romBinary, err := cps2rom.ProcessRegionFromZip(romZipFile, romDef.Maincpu)
check(err)
f, err := file_utils.CreateFile(flags.outputFilepath)
check(err)
f.Write(romBinary)
f.Close()
}
// func decodeGfx() {
// if flags.outputFilepath == "" || flags.outputFilepath == flags.romSetName+".bin" {
// flags.outputFilepath = flags.romSetName + "_gfx.bin"
// }
// romZipFile, romDef, err := cps2rom.ParseRomZip(flags.zipFilepath, flags.romSetName)
// check(err)
// defer romZipFile.Close()
// gfxBinary, err := cps2rom.ProcessRegionFromZip(romZipFile, romDef.Gfx)
// check(err)
// err = file_utils.WriteBytesToFile(flags.outputFilepath, gfxBinary)
// check(err)
// Resources.Logger.Done(fmt.Sprintf("Decoded graphics written to %s!", flags.outputFilepath))
// }
func decrypt(args ...*string) {
if len(args) > 0 {
flags.romSetName = *args[0]
flags.zipFilepath = *args[1]
}
if flags.outputFilepath == "" {
flags.outputFilepath = flags.romSetName + ".bin"
}
romZipFile, romDef, err := cps2rom.ParseRomZip(flags.zipFilepath, flags.romSetName)
check(err)
defer romZipFile.Close()
romBinary, err := cps2rom.ProcessRegionFromZip(romZipFile, romDef.Maincpu)
check(err)
decryptedRomBinary, err := cps2crypt.Crypt(cps2crypt.Decrypt, *romDef, romZipFile, romBinary)
check(err)
err = file_utils.WriteBytesToFile(flags.outputFilepath, decryptedRomBinary)
check(err)
Resources.Logger.Done(fmt.Sprintf("Decrypted ROM written to %s!", flags.outputFilepath))
}
func encrypt(args ...*string) {
if len(args) > 0 {
flags.romSetName = *args[0]
flags.zipFilepath = *args[1]
flags.binFilepath = *args[2]
}
if flags.outputFilepath == "" {
flags.outputFilepath = flags.romSetName + ".zip"
}
romZipFile, romDef, err := cps2rom.ParseRomZip(flags.zipFilepath, flags.romSetName)
check(err)
decryptedRomBinary, err := file_utils.GetFileContents(flags.binFilepath)
check(err)
defer romZipFile.Close()
encryptedRegion, err := cps2crypt.Crypt(cps2crypt.Encrypt, *romDef, romZipFile, decryptedRomBinary)
check(err)
err = cps2rom.SplitRegionToFiles(romDef.Maincpu, encryptedRegion, flags.outputFilepath+"_enc")
check(err)
encryptedRegionZip, err := zip.OpenReader(flags.outputFilepath + "_enc")
check(err)
defer encryptedRegionZip.Close()
err = cps2rom.WriteModifiedRegionToZip(flags.outputFilepath, romZipFile, encryptedRegionZip, romDef.Maincpu)
check(err)
err = file_utils.DeleteFile(flags.outputFilepath + "_enc")
check(err)
Resources.Logger.Done(fmt.Sprintf("Encrypted ROM written to %s!", flags.outputFilepath))
}
func patch(args ...*string) {
if len(args) > 0 {
flags.romSetName = *args[0]
flags.zipFilepath = *args[1]
flags.mraFilepath = *args[2]
}
if flags.outputFilepath == "" {
flags.outputFilepath = flags.romSetName + ".zip"
}
romZipFile, romDef, err := cps2rom.ParseRomZip(flags.zipFilepath, flags.romSetName)
check(err)
mraFile, err := file_utils.GetFileContents(flags.mraFilepath)
check(err)
mra, err := cps2rom.ParseMra(mraFile)
check(err)
fileContentMap, err := file_utils.UnzipFilesToFilenameContentMap(romZipFile)
check(err)
Resources.Logger.Warn("Patching ROM...")
romRegions := []struct {
regionName string
region cps2rom.RomRegion
}{{"maincpu", romDef.Maincpu}, {"audiocpu", romDef.Audiocpu}, {"qsound", romDef.Qsound}, {"gfx", romDef.Gfx}}
baseOffset := 0
for _, region := range romRegions {
Resources.Logger.Info(fmt.Sprintf("%s (+0x%06x):", region.regionName, baseOffset))
err = cps2rom.PatchRomRegionWithMra(romZipFile, *mra, region.region, fileContentMap, baseOffset, flags.outputFilepath)
if region.regionName == "audiocpu" {
baseOffset += 0x40000
} else {
baseOffset += region.region.Size
}
}
check(err)
Resources.Logger.Done("Done patching ROM!")
Resources.Logger.Warn("Writing files to .zip...")
f, err := file_utils.CreateFile(flags.outputFilepath)
w := *zip.NewWriter(f)
for file, content := range fileContentMap {
x, err := w.Create(file)
check(err)
_, err = x.Write(content)
check(err)
}
w.Close()
Resources.Logger.Done(fmt.Sprintf("Patched ROM written to %s!", flags.outputFilepath))
}
func diff(args ...*string) {
if len(args) > 0 {
flags.romSetName = *args[0]
flags.zipFilepath = *args[1]
flags.diffZipFilepath = *args[2]
}
if flags.outputFilepath == "" {
flags.outputFilepath = flags.romSetName + ".mra"
}
var patches []cps2rom.RomPatch
firstRom, romDef, err := cps2rom.ParseRomZip(flags.zipFilepath, flags.romSetName)
check(err)
secondRom, _, err := cps2rom.ParseRomZip(flags.diffZipFilepath, flags.romSetName)
check(err)
romRegions := []struct {
regionName string
region cps2rom.RomRegion
}{{"maincpu", romDef.Maincpu}, {"audiocpu", romDef.Audiocpu}, {"qsound", romDef.Qsound}, {"gfx", romDef.Gfx}}
baseOffset := 0
Resources.Logger.Warn("Diffing ROMs...")
for _, region := range romRegions {
Resources.Logger.Info(fmt.Sprintf("%s (+0x%06x):", region.regionName, baseOffset))
regionPatches, err := cps2rom.DiffRomRegion(baseOffset, region.region, firstRom, secondRom)
check(err)
patches = append(patches, *regionPatches...)
if region.regionName == "audiocpu" {
baseOffset += 0x40000
} else {
baseOffset += region.region.Size
}
}
patchStrings := cps2rom.GenerateMraPatches(&patches)
patchFile, err := file_utils.CreateFile(flags.outputFilepath)
check(err)
_, err = patchFile.WriteString(Resources.Strings.Info["mraHeader"])
check(err)
for _, patch := range patchStrings {
_, err = patchFile.WriteString(patch)
check(err)
}
defer patchFile.Close()
Resources.Logger.Done(fmt.Sprintf("Patches written to %s!", flags.outputFilepath))
}
func swap() {
if flags.outputFilepath == "" {
flags.outputFilepath = filepath.Join(filepath.Dir(flags.binFilepath), filepath.Base(flags.binFilepath)+"_swap")
}
swappedBinBytes, err := file_utils.SwapFileBytes(flags.binFilepath)
check(err)
err = file_utils.WriteBytesToFile(flags.outputFilepath, swappedBinBytes)
check(err)
Resources.Logger.Done(fmt.Sprintf("Swapped bytes written to %s!", flags.outputFilepath))
}
func main() {
err := cps2rom.ParseRoms()
check(err)
parseFlags()
if flags.isGuiMode {
gui()
} else if flags.isDecryptMode {
decrypt()
} else if flags.isEncryptMode {
encrypt()
} else if flags.isPatchMode {
patch()
} else if flags.isMraMode {
diff()
} else if flags.isConcatMode {
concat()
} else if flags.isSwapMode {
swap()
}
os.Exit(0)
}