/
DOOMWADCorrupter.cs
260 lines (243 loc) · 8.19 KB
/
DOOMWADCorrupter.cs
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
using arookas.Collections;
using arookas.IO.Binary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
namespace arookas {
class DOOMWADCorrupter {
static aCommandLine cmd;
static CorrupterOptions options;
static Random rnd;
static readonly string sSeparator = new String('=', 79);
static readonly string[] sZDOOMLumps = {
"ALTHUDCF", "ANIMDEFS", "CVARINFO", "DECALDEF", "DECORATE", "DEHACKED", "DEHSUPP", "DMXGUS",
"FSGLOBAL", "FONTDEFS", "GAMEINFO", "GLDEFS", "KEYCONF", "LANGUAGE", "LOADACS", "LOCKDEFS",
"MAPINFO", "MENUDEF", "MODELDEF", "MUSINFO", "PALVERS", "SBARINFO", "SCRIPTS", "SECRETS",
"SNDINFO", "SNDSEQ", "TEAMINFO", "TERRAIN", "TEXTCOLO", "TEXTURES", "VOXELDEF", "XHAIRS",
"X11R6RGB", "ZMAPINFO", "ANIMATED", "BEHAVIOR", "GENMIDI", "SNDCURVE", "SWITCHES",
};
static void Main(string[] arguments) {
Message("doomwadcorrupter v{0} arookas", new Version(0, 1, 12));
Separator();
if (arguments == null || arguments.Length < 2) {
Message("Usage: doomwadcorrupter <input.wad> <output.wad> [options]");
Message();
Message("Options:");
Message(" -start <value>");
Message(" -end <value>");
Message(" -inc <value>");
Message(" -mode <type> [<value>]");
Message(" -skip <filter> [<filter> [...]]");
Message(" -only <filter> [<filter> [...]]");
Message(" -zdoom");
Message();
Message("For more detailed instructions, refer to the official repo page.");
Pause();
Exit(false);
}
var inputWAD = arguments[0];
var outputWAD = arguments[1];
cmd = new aCommandLine(arguments.Skip(2).ToArray());
options = new CorrupterOptions(cmd);
DisplayOptions(inputWAD, outputWAD);
int lumpCount;
var lumpsCorrupted = 0;
var lumpsSkipped = 0;
var bytesCorrupted = 0;
rnd = new Random((uint)options.CorruptSeed);
var timeTaken = Stopwatch.StartNew();
using (var instream = OpenWAD(inputWAD)) {
var reader = new aBinaryReader(instream, Endianness.Little, Encoding.ASCII);
// header
var wadType = reader.ReadString(4);
if (wadType != "IWAD" && wadType != "PWAD") {
Error("Input file is not a DOOM WAD.");
}
lumpCount = reader.ReadS32();
var directoryOffset = reader.ReadS32();
// directory
reader.Goto(directoryOffset);
var lumps = aCollection.Initialize(lumpCount, () => new Lump(reader));
using (var outstream = CreateWAD(outputWAD)) {
var writer = new aBinaryWriter(outstream, Endianness.Little, Encoding.ASCII);
// header
writer.WriteString(wadType);
writer.WriteS32(lumpCount);
writer.WriteS32(directoryOffset);
// data
var corruptBuff = new byte[options.Increment];
var startBuff = new byte[options.Start];
var ns = LumpNamespace.Global;
foreach (var lump in lumps) {
reader.Goto(lump.Start);
writer.Goto(lump.Start);
CheckNamespaceMarker(lump, ref ns);
if (options.Filter.IsCorruptable(lump.Name, ns) && !(options.ZDOOM && IsZDOOMLump(lump.Name))) {
++lumpsCorrupted;
var i = options.Start;
var end = options.End ?? lump.Length;
if (i > 0) {
var count = (int)System.Math.Min(lump.Length, i);
reader.Read(startBuff, count);
writer.Write8s(startBuff, count);
}
while (i < lump.Length && i < end) {
Status("Corrupting '{0}'... (0x{1:X8} / 0x{2:X8})", lump.Name, i, lump.Length);
var count = (int)System.Math.Min(lump.Length - i, options.Increment);
reader.Read(corruptBuff, count);
CorruptByte(ref corruptBuff[0], options.CorruptMode, options.CorruptValue);
writer.Write8s(corruptBuff, count);
++bytesCorrupted;
i += count;
}
}
else {
++lumpsSkipped;
writer.Write8s(reader.Read8s(lump.Length));
}
}
// directory
writer.Goto(directoryOffset);
foreach (var lump in lumps) {
Status("Writing lump directory for '{0}'...", lump.Name);
lump.ToStream(writer);
}
}
}
timeTaken.Stop();
Status("Finished corrupting.");
Message();
Separator();
Message(" Files : {0}", lumpCount);
Message(" Files corrupted : {0}", lumpsCorrupted);
Message(" Files skipped : {0}", lumpsSkipped);
Message("Bytes mercilessly sacrificed : {0}", bytesCorrupted);
Message(" Time taken : {0}", timeTaken.Elapsed.ToString("g"));
Message(" Finished at : {0}", DateTime.Now.ToString("HH:mm:ss tt"));
Pause();
}
static void DisplayOptions(string inputWAD, string outputWAD) {
Message(" Input WAD : {0}", Path.GetFileName(inputWAD));
Message(" Output WAD : {0}", Path.GetFileName(outputWAD));
Message(" Start : {0}", options.Start);
if (options.End != null) {
Message(" End : {0}", options.End);
}
Message(" Increment : {0}", options.Increment);
if (options.CorruptMode == CorruptMode.Random) {
Message(" Mode : {0} ({1})", options.CorruptMode, options.CorruptSeed);
}
else {
Message(" Mode : {0} {1}", options.CorruptMode, options.CorruptValue);
}
Message("Filters:");
Message(options.Filter.ToString());
if (options.ZDOOM) {
Message("** Skip G/ZDOOM lumps enabled **");
}
Separator();
}
static void CheckNamespaceMarker(Lump lump, ref LumpNamespace ns) {
switch (lump.Name) {
case "F_START":
case "FF_START": {
ns = LumpNamespace.Flats;
break;
}
case "S_START":
case "SS_START": {
ns = LumpNamespace.Sprites;
break;
}
case "P_START":
case "PP_START": {
ns = LumpNamespace.Patches;
break;
}
case "F_END":
case "FF_END":
case "S_END":
case "SS_END":
case "P_END":
case "PP_END": {
ns = LumpNamespace.Global;
break;
}
}
}
static void CorruptByte(ref byte b, CorruptMode mode, byte value) {
switch (mode) {
case CorruptMode.Add: b += value; break;
case CorruptMode.Sub: b -= value; break;
case CorruptMode.Mul: b *= value; break;
case CorruptMode.Div: b /= value; break;
case CorruptMode.Mod: b %= value; break;
case CorruptMode.OR: b |= value; break;
case CorruptMode.XOR: b ^= value; break;
case CorruptMode.AND: b &= value; break;
case CorruptMode.NOT: b = (byte)~b; break;
case CorruptMode.LSH: b <<= value; break;
case CorruptMode.RSH: b >>= value; break;
case CorruptMode.ROL: b = (byte)((b << value) | (b >> (8 - value))); break;
case CorruptMode.ROR: b = (byte)((b >> value) | (b << (8 - value))); break;
case CorruptMode.Replace: b = value; break;
case CorruptMode.Random: b = (byte)rnd.NextInt32(); break;
}
}
static FileStream OpenWAD(string path) {
try {
return File.OpenRead(path);
}
catch {
Error("Failed to open the WAD file '{0}'. Check to make sure the file exists and is not already in use.", path);
return null;
}
}
static FileStream CreateWAD(string path) {
try {
return File.Create(path);
}
catch {
Error("Failed to create WAD file '{0}'. Check to make sure the program has access to the given path.", path);
return null;
}
}
static bool IsZDOOMLump(string name) { return sZDOOMLumps.Any(i => name.StartsWith(i)); }
public static void Separator() {
Message(sSeparator);
}
public static void Message() {
Console.WriteLine();
}
public static void Message(string format, params object[] args) {
Console.WriteLine(format, args);
}
public static void Warning(string format, params object[] args) {
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("WARNING: ");
Message(format, args);
Console.ResetColor();
}
public static void Error(string format, params object[] args) {
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("ERROR: ");
Message(format, args);
Console.ResetColor();
Pause();
Exit(true);
}
public static void Status(string format, params object[] args) {
Console.Write("\r{0,-79}", String.Format(format, args));
}
public static void Pause() {
Console.ReadKey();
}
public static void Exit(bool error) {
Environment.Exit(error ? 1 : 0);
}
}
}