-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWADReader.cs
More file actions
188 lines (163 loc) · 5.44 KB
/
WADReader.cs
File metadata and controls
188 lines (163 loc) · 5.44 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
using System;
using System.IO;
using System.Text;
using Microsoft.Xna.Framework;
namespace Doom;
public class WADReader : IDisposable
{
private readonly FileStream _fileStream;
public WADReader(string wadPath)
{
if (!File.Exists(wadPath))
{
throw new FileNotFoundException("WAD file not found", wadPath);
}
_fileStream = new FileStream(wadPath, System.IO.FileMode.Open);
WADHeader = ReadHeader();
Directory = ReadDirectory();
}
public WADHeader WADHeader { get; init; }
public LumpInfo[] Directory { get; init; }
private WADHeader ReadHeader()
{
var identification = ReadString(WADHeader.IdentificationPosition, WADHeader.IdentificationLength);
var numLumps = ReadInt(WADHeader.NumLumpsPosition);
var infoTableOffset = ReadInt(WADHeader.InfoTableOffsetPosition);
return new WADHeader(identification, numLumps, infoTableOffset);
}
private LumpInfo[] ReadDirectory()
{
var directory = new LumpInfo[WADHeader.NumLumps];
for (var i = 0; i < WADHeader.NumLumps; i++)
{
var offset = WADHeader.InfoTableOffset + i * 16;
var lumpOffset = ReadInt(offset);
var lumpSize = ReadInt(offset + 4);
var lumpName = ReadString(offset + 8, 8);
directory[i] = new LumpInfo(lumpOffset, lumpSize, lumpName);
}
return directory;
}
public Sidedef ReadSidedef(int offset) =>
new Sidedef
{
XOffset = ReadShort(offset),
YOffset = ReadShort(offset + 2),
UpperTexture = ReadString(offset + 4, 8),
LowerTexture = ReadString(offset + 12, 8),
MiddleTexture = ReadString(offset + 20, 8),
SectorId = ReadUShort(offset + 28)
};
public Sector ReadSector(int offset) =>
new Sector
{
FloorHeight = ReadShort(offset),
CeilingHeight = ReadShort(offset + 2),
FloorTexture = ReadString(offset + 4, 8),
CeilingTexture = ReadString(offset + 12, 8),
LightLevel = ReadUShort(offset + 20),
Type = ReadUShort(offset + 22),
Tag = ReadUShort(offset + 24)
};
public Thing ReadThing(int offset) =>
new Thing
{
Position = new Vector2(ReadShort(offset), ReadShort(offset + 2)),
Angle = ReadUShort(offset + 4),
Type = ReadUShort(offset + 6),
Flags = ReadUShort(offset + 8)
};
public Seg ReadSeg(int offset) =>
new Seg
{
StartVertexId = ReadShort(offset),
EndVertexId = ReadShort(offset + 2),
Angle = ReadShort(offset + 4),
LinedefId = ReadShort(offset + 6),
Direction = ReadShort(offset + 8),
Offset = ReadShort(offset + 10)
};
public SubSector ReadSubSector(int offset) =>
new SubSector
{
SegCount = ReadShort(offset),
FirstSeg = ReadShort(offset + 2)
};
public Node ReadNode(int offset) =>
new Node
{
PartitionX = ReadShort(offset),
PartitionY = ReadShort(offset + 2),
DeltaPartitionX = ReadShort(offset + 4),
DeltaPartitionY = ReadShort(offset + 6),
FrontBoundingBox = new BoundingBox
{
Top = ReadShort(offset + 8),
Bottom = ReadShort(offset + 10),
Left = ReadShort(offset + 12),
Right = ReadShort(offset + 14)
},
BackBoundingBox = new BoundingBox
{
Top = ReadShort(offset + 16),
Bottom = ReadShort(offset + 18),
Left = ReadShort(offset + 20),
Right = ReadShort(offset + 22)
},
FrontChild = ReadUShort(offset + 24),
BackChild = ReadUShort(offset + 26)
};
public Linedef ReadLinedef(int offset) =>
new Linedef
{
StartVertexId = ReadUShort(offset),
EndVertexId = ReadUShort(offset + 2),
Flags = ReadUShort(offset + 4),
LineType = ReadUShort(offset + 6),
SectorTag = ReadUShort(offset + 8),
FrontSidedefId = ReadUShort(offset + 10),
BackSidedefId = ReadUShort(offset + 12)
};
public Vector2 ReadVertex(int offset) =>
new Vector2(ReadShort(offset), ReadShort(offset + 2));
public byte ReadByte(int offset)
{
var bytes = ReadBytes(offset, 1);
return bytes[0];
}
public short ReadShort(int offset)
{
var bytes = ReadBytes(offset, 2);
return BitConverter.ToInt16(bytes);
}
public ushort ReadUShort(int offset)
{
var bytes = ReadBytes(offset, 2);
return BitConverter.ToUInt16(bytes);
}
public int ReadInt(int offset)
{
var bytes = ReadBytes(offset, 4);
return BitConverter.ToInt32(bytes);
}
public string ReadString(int offset, int count)
{
var bytes = ReadBytes(offset, count);
return Encoding.ASCII.GetString(bytes);
}
private byte[] ReadBytes(int offset, int count)
{
var bytes = new byte[count];
_fileStream.Seek(offset, SeekOrigin.Begin);
_fileStream.Read(bytes, 0, count);
return bytes;
}
public void Close()
{
_fileStream.Close();
}
public void Dispose()
{
Close();
}
}