forked from treellama/weland
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wadfile.cs
320 lines (268 loc) · 8.2 KB
/
Wadfile.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
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
using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
namespace Weland {
enum WadfileVersion {
PreEntryPoint,
HasDirectoryEntry,
SupportsOverlays,
HasInfinityStuff
}
enum WadfileDataVersion {
Marathon,
MarathonTwo
}
public class Wadfile {
const int headerSize = 128;
public static uint Chunk(string s) {
return (uint) ((((byte) s[0]) << 24) | (((byte) s[1]) << 16) | (((byte) s[2]) << 8) | ((byte) s[3]));
}
public short Version {
get {
return version;
}
}
short version;
public short DataVersion;
public string Filename;
const int maxFilename = 64;
public uint Checksum {
get {
return checksum;
}
}
uint checksum;
int directoryOffset;
public int WadCount {
get {
return Directory.Count;
}
}
protected short applicationSpecificDirectoryDataSize;
short entryHeaderSize;
short directoryEntryBaseSize;
public uint ParentChecksum;
public class BadMapException : Exception {
public BadMapException()
{
}
public BadMapException(string message) : base(message)
{
}
public BadMapException(string message, Exception inner) : base(message, inner)
{
}
}
public class DirectoryEntry {
internal const short BaseSize = 10;
public Dictionary<uint, byte[]> Chunks = new Dictionary<uint, byte[]> ();
internal const short HeaderSize = 16;
internal int Offset;
internal short Index;
public int Size {
get {
int total = 0;
foreach (var kvp in Chunks) {
total += kvp.Value.Length + HeaderSize;
}
return total;
}
}
internal void LoadEntry(BinaryReaderBE reader) {
Offset = reader.ReadInt32();
reader.ReadInt32(); // size
Index = reader.ReadInt16();
}
internal void LoadChunks(BinaryReaderBE reader) {
long position = reader.BaseStream.Position;
int nextOffset;
do {
uint tag = reader.ReadUInt32();
nextOffset = reader.ReadInt32();
int length = reader.ReadInt32();
reader.ReadInt32(); // offset
Chunks[tag] = reader.ReadBytes(length);
if (nextOffset > 0)
reader.BaseStream.Seek(position + nextOffset, SeekOrigin.Begin);
} while (nextOffset > 0);
}
internal void SaveEntry(BinaryWriterBE writer) {
writer.Write(Offset);
writer.Write((int) Size);
writer.Write(Index);
}
internal void SaveChunks(BinaryWriterBE writer, uint[] tagOrder) {
// build a list of tags to write in order
HashSet<uint> Used = new HashSet<uint>();
List<uint> Tags = new List<uint>();
foreach (uint tag in tagOrder) {
if (Chunks.ContainsKey(tag)) {
Tags.Add(tag);
Used.Add(tag);
}
}
foreach (var kvp in Chunks) {
if (!Used.Contains(kvp.Key)) {
Tags.Add(kvp.Key);
Used.Add(kvp.Key);
}
}
int offset = 0;
foreach (uint tag in Tags) {
writer.Write(tag);
if (tag == Tags[Tags.Count - 1]) {
writer.Write((uint) 0);
} else {
writer.Write((int) offset + HeaderSize + Chunks[tag].Length);
}
writer.Write((int) Chunks[tag].Length);
writer.Write((int) 0);
writer.Write(Chunks[tag]);
offset += Chunks[tag].Length + HeaderSize;
}
}
public DirectoryEntry Clone() {
DirectoryEntry clone = (DirectoryEntry) MemberwiseClone();
clone.Chunks = new Dictionary<uint, byte[]>();
foreach (var kvp in Chunks) {
clone.Chunks[kvp.Key] = (byte[]) kvp.Value.Clone();
}
return clone;
}
}
bool MacBinaryHeader(byte[] header) {
if (header[0] != 0 || header[1] > 64 || header[74] != 0 || header[123] > 0x81)
return false;
ushort crc = 0;
for (int i = 0; i < 124; ++i) {
ushort data = (ushort) (header[i] << 8);
for (int j = 0; j < 8; ++j) {
if (((data ^ crc) & 0x8000) == 0x8000)
crc = (ushort) ((crc << 1) ^ 0x1021);
else
crc <<= 1;
data <<= 1;
}
}
if (crc != ((header[124] << 8) | header[125]))
return false;
return true;
}
public SortedDictionary<int, DirectoryEntry> Directory = new SortedDictionary<int, DirectoryEntry> ();
protected virtual void LoadApplicationSpecificDirectoryData(BinaryReaderBE reader, int index) {
reader.ReadBytes(applicationSpecificDirectoryDataSize);
}
protected virtual void SaveApplicationSpecificDirectoryData(BinaryWriterBE writer, int index) {
}
protected virtual void SetApplicationSpecificDirectoryDataSize() {
applicationSpecificDirectoryDataSize = 0;
}
protected virtual uint[] GetTagOrder() {
return new uint[] { };
}
public virtual void Load(string filename) {
BinaryReaderBE reader = new BinaryReaderBE(File.Open(filename, FileMode.Open));
try {
// is it MacBinary?
int fork_start = 0;
if (MacBinaryHeader(reader.ReadBytes(128))) {
fork_start = 128;
}
reader.BaseStream.Seek(fork_start, SeekOrigin.Begin);
// read the header
version = reader.ReadInt16();
DataVersion = reader.ReadInt16();
Filename = reader.ReadMacString(maxFilename);
checksum = reader.ReadUInt32();
directoryOffset = reader.ReadInt32();
short wadCount = reader.ReadInt16();
applicationSpecificDirectoryDataSize = reader.ReadInt16();
entryHeaderSize = reader.ReadInt16();
directoryEntryBaseSize = reader.ReadInt16();
// sanity check the map
if (Version < 2 || entryHeaderSize != 16 || directoryEntryBaseSize != 10) {
throw new BadMapException("Only Marathon 2 and higher maps are supported");
}
ParentChecksum = reader.ReadUInt32();
reader.ReadBytes(2 * 20); // unused
// load the directory
reader.BaseStream.Seek(directoryOffset + fork_start, SeekOrigin.Begin);
for (int i = 0; i < wadCount; ++i) {
DirectoryEntry entry = new DirectoryEntry();
entry.LoadEntry(reader);
Directory[entry.Index] = entry;
LoadApplicationSpecificDirectoryData(reader, entry.Index);
}
// load all the wads(!)
foreach (KeyValuePair<int, DirectoryEntry> kvp in Directory) {
reader.BaseStream.Seek(kvp.Value.Offset + fork_start, SeekOrigin.Begin);
kvp.Value.LoadChunks(reader);
}
} finally {
reader.Close();
}
}
public void Save(string filename) {
using (FileStream fs = File.Open(filename, FileMode.OpenOrCreate, FileAccess.Write)) {
CrcStream crcStream = new CrcStream(fs);
BinaryWriterBE writer = new BinaryWriterBE(crcStream);
// set up the header
if (Directory.Count == 1) {
version = (short) WadfileVersion.SupportsOverlays;
} else {
version = (short) WadfileVersion.HasInfinityStuff;
}
DataVersion = (short) WadfileDataVersion.MarathonTwo;
checksum = 0;
directoryOffset = headerSize;
foreach (var kvp in Directory) {
kvp.Value.Offset = directoryOffset;
kvp.Value.Index = (short) kvp.Key;
directoryOffset += kvp.Value.Size;
}
SetApplicationSpecificDirectoryDataSize();
entryHeaderSize = DirectoryEntry.HeaderSize;
directoryEntryBaseSize = DirectoryEntry.BaseSize;
ParentChecksum = 0;
// write the header
writer.Write(version);
writer.Write(DataVersion);
writer.WriteMacString(filename.Split(new char[] {'.'})[0], maxFilename);
writer.Write(checksum);
writer.Write(directoryOffset);
writer.Write((short) Directory.Count);
writer.Write(applicationSpecificDirectoryDataSize);
writer.Write(entryHeaderSize);
writer.Write(directoryEntryBaseSize);
writer.Write(ParentChecksum);
writer.Write(new byte[2 * 20]);
// write wads
foreach (var kvp in Directory) {
kvp.Value.SaveChunks(writer, GetTagOrder());
}
// write directory
foreach (var kvp in Directory) {
kvp.Value.SaveEntry(writer);
SaveApplicationSpecificDirectoryData(writer, kvp.Value.Index);
}
// fix the checksum!
checksum = crcStream.GetCRC();
fs.Seek(68, SeekOrigin.Begin);
writer.Write(checksum);
}
}
static public void Main(string[] args) {
if (args.Length == 2) {
Wadfile wadfile = new Wadfile();
wadfile.Load(args[0]);
Wadfile export = new Wadfile();
export.Directory[0] = wadfile.Directory[0];
export.Save(args[1]);
Console.WriteLine("DirectoryOffset: {0}", export.directoryOffset);
} else {
Console.WriteLine("Test usage: wadfile.exe <wadfile> <export>");
}
}
}
}