-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathRomMod.cs
More file actions
296 lines (267 loc) · 11.5 KB
/
RomMod.cs
File metadata and controls
296 lines (267 loc) · 11.5 KB
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
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Reflection;
using SharpTune;
using SharpTuneCore;
using SharpTune.Properties;
namespace SharpTune.RomMod
{
public class RomMod
{
public const int Version = 9;
public const int AuthVersion = 10000 + Version;
/// <summary>
/// Determines which command to run.
/// </summary>
public static bool Run(AvailableDevices availableDevices, string[] args)
{
if (args.Length == 2 && args[0] == "help")
{
PrintHelp(args[1]);
return true;
}
else if (args.Length == 2 && args[0] == "dump")
{
return RomMod.TryDumpSRecordFile(args[1]);
}
else if (args.Length == 3 && args[0] == "test")
{
return RomMod.TryApply(args[1], args[2], true, false) || RomMod.TryApply(args[1], args[2], false, false);
}
else if (args.Length == 3 && args[0] == "apply")
{
return RomMod.TryApply(args[1], args[2], true, true);
}
else if (args.Length == 3 && args[0] == "applied")
{
return RomMod.TryApply(args[1], args[2], false, false);//TODO is this deprecated?
}
else if (args.Length == 3 && args[0] == "remove")
{
return RomMod.TryApply(args[1], args[2], false, true);
}
else if (args.Length == 4 && args[0] == "baseline")
{
return RomMod.TryGenerateBaseline(args[1], args[2], args[3]);
}
else if (args.Length == 4 && args[0] == "baselinedefine")
{
return RomMod.TryBaselineAndDefine(availableDevices, args[1], args[2], args[3],Settings.Default.EcuFlashDefRepoPath);
}
else if (args.Length == 4 && args[0] == "define")
{
return RomMod.TryDefine(availableDevices, args[1], args[2], args[3], Settings.Default.EcuFlashDefRepoPath);
}
else if (args.Length == 4 && args[0] == "hewbuild")
{
return RomMod.TryHewBuild(availableDevices, args[1], args[2], args[3], Settings.Default.EcuFlashDefRepoPath);
}
PrintHelp();
return false;
}
/// <summary>
/// Print generic usage instructions.
/// </summary>
private static void PrintHelp()
{
Trace.WriteLine("RomPatch Version " + RomMod.Version);
Trace.WriteLine("Commands:");
Trace.WriteLine("");
Trace.WriteLine("test - determine whether a patch is suitable for a ROM");
Trace.WriteLine("apply - apply a patch to a ROM file");
Trace.WriteLine("applied - determine whether a patch has been applied to a ROM");
Trace.WriteLine("remove - remove a patch from a ROM file");
Trace.WriteLine("dump - dump the contents of a patch file");
Trace.WriteLine("baseline - generate baseline data for a ROM and a partial patch");
Trace.WriteLine("define - define a patched rom");
Trace.WriteLine("hewbuild - perform hew build (baseline -> patch -> define)");
Trace.WriteLine("");
Trace.WriteLine("Use \"RomPatch help <command>\" to show help for that command.");
}
/// <summary>
/// Print usage instructions for a particular command.
/// </summary>
private static void PrintHelp(string command)
{
switch (command)
{
case "test":
Trace.WriteLine("RomPatch test <patchfilename> <romfilename>");
Trace.WriteLine("Determines whether the given patch file matches the given ROM file.");
break;
case "apply":
Trace.WriteLine("RomPatch apply <patchfilename> <romfilename>");
Trace.WriteLine("");
Trace.WriteLine("Verifies that a patch is suitable for the ROM file, then applies");
Trace.WriteLine("the patch to the ROM (or prints an error message).");
break;
case "applied":
Trace.WriteLine("RomPatch applied <patchfilename> <romfilename>");
Trace.WriteLine("Determines whether the given patch file was applied to the given ROM file.");
break;
case "remove":
Trace.WriteLine("RomPatch remove <patchfilename> <romfilename>");
Trace.WriteLine("");
Trace.WriteLine("Verifies that a patch was applied to the ROM file, then removes");
Trace.WriteLine("the patch from the ROM (or prints an error message).");
break;
case "dump":
Trace.WriteLine("RomPatch dump <filename>");
Trace.WriteLine("Dumps the contents of the give patch file.");
break;
case "baseline":
Trace.WriteLine("RomPatch baseline <patchfilename> <romfilename>");
Trace.WriteLine("Generates baseline SRecords for the given patch and ROM file.");
break;
case "help":
Trace.WriteLine("You just had to try that, didn't you?");
break;
}
}
private static bool TryApply(string patchPath, string romPath, bool apply, bool commit)
{
Mod currentMod = new Mod(patchPath);//TODO: KING KLUDGE!
return currentMod.TryCheckApplyMod(romPath, romPath, apply, commit);
}
/// <summary>
/// Dump the contents of an SRecord file. Mostly intended for development use.
/// </summary>
private static bool TryDumpSRecordFile(string path)
{
bool result = true;
SRecord record;
SRecordReader reader = new SRecordReader(path);
reader.Open();
BlobList list = new BlobList();
while (reader.TryReadNextRecord(out record))
{
if (!record.IsValid)
{
Trace.WriteLine(record.ToString());
result = false;
continue;
}
list.ProcessRecord(record);
Trace.WriteLine(record.ToString());
}
Trace.WriteLine("Aggregated:");
foreach (Blob blob in list.Blobs)
{
Trace.WriteLine(blob.ToString());
}
return result;
}
/// <summary>
/// Extract data from an unpatched ROM file, for inclusion in a patch file.
/// </summary>
private static bool TryGenerateBaseline(string patchPath, string romPath, string build)
{
using (Stream romStream = File.OpenRead(romPath))
{
Mod patcher = new Mod(patchPath, build);
if (!patcher.TryReadPatches())
return false;
return patcher.TryPrintBaselines(patchPath, romStream);
}
}
private static bool TryBaselineAndDefine(AvailableDevices ad, string patchPath, string romPath, string build, string defPath)
{
using (Stream romStream = File.OpenRead(romPath))
{
Mod patcher = new Mod(patchPath, build);
if (!patcher.TryReadPatches())
return false;
if (!patcher.TryDefinition(ad, defPath))
return false;
return patcher.TryPrintBaselines(patchPath,romStream);
}
}
public static bool TryDefine(AvailableDevices ad, string patchPath, string romPath, string bc, string defPath)
{
using (Stream romStream = File.OpenRead(romPath))
{
Mod mod = new Mod(patchPath, bc);
return mod.TryDefinition(ad, defPath);
}
}
public static bool TryHewBuild(AvailableDevices ad, string patchPath, string romPath, string bc, string defPath)
{
Mod mod = new Mod(patchPath, bc);
using (Stream romStream = File.OpenRead(romPath))
{
Trace.WriteLine("Attempting to read patches");
if (!mod.TryReadPatches())
{
PrintError("READING PATCH");
return false;
}
Trace.WriteLine("Attempting to baseline patches");
if (!mod.TryPrintBaselines(patchPath, romStream))
{
PrintError("GENERATING BASELINES");
return false;
}
}
// use baseline'd patch to create new mod
mod = new Mod(mod.ModIdent + ".patch");
File.Copy(romPath, "oem.bin", true);
Trace.WriteLine("Attempting to test patches");
if (!mod.TryCheckApplyMod(romPath, romPath, true, false)) //&& !mod.TryCheckApplyMod(romPath, romPath, false, false)) ;
{
PrintError("TESTING PATCH");
return false;
}
Trace.WriteLine("Attempting to apply patches");
if (!mod.TryCheckApplyMod(romPath, romPath, true, true))
{
PrintError("APPLYING PATCH");
return false;
}
Trace.WriteLine("Attempting to remove patches");
File.Copy(romPath, "reverted.bin", true);
if (!mod.TryCheckApplyMod("reverted.bin", "reverted.bin", false, true))
{
PrintError("REMOVING PATCH");
return false;
}
Trace.WriteLine("Attempting to verify patch removal");
if (!Utils.FileCompare("reverted.bin", "oem.bin"))
{
PrintError("VERIFYING REMOVED PATCH");
return false;
}
Trace.WriteLine("Attempting to define patch");
if (!mod.TryDefinition(ad, defPath))
{
PrintError("WRITING DEFINITIONS");
return false;
}
Trace.WriteLine("Attempting to copy patch to: "+ bc + "\\" + mod.InitialCalibrationId + "\\" + mod.FileName);
string d = bc + "\\" + mod.InitialCalibrationId + "\\";
Directory.CreateDirectory(d);
string c = d + mod.FileName;
File.Copy(mod.FilePath,c,true);
Trace.WriteLine("HEW BUILD SUCCESS!!");
return true;
}
public static void PrintError(string mess)
{
for (int i = 0; i < 5; i++)
Trace.WriteLine("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!ERROR " + mess + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
}
}