-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathse_wadreader.pas
232 lines (209 loc) · 5.75 KB
/
se_wadreader.pas
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
//------------------------------------------------------------------------------
//
// SpeedEd: GLSpeed map Editor utilities
// Copyright (C) 2021-2022 by Jim Valavanis
//
// 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 2
// 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.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// DESCRIPTION:
// WAD Reader
//
//------------------------------------------------------------------------------
// E-Mail: jimmyvalavanis@yahoo.gr
// Site : https://sourceforge.net/projects/speed-game/
//------------------------------------------------------------------------------
unit se_wadreader;
interface
uses
Classes, SysUtils, se_wad;
type
TWadReader = class(TObject)
private
h: wadinfo_t;
la: Pfilelump_tArray;
ss: TStream;
ffilename: string;
public
constructor Create; virtual;
destructor Destroy; override;
procedure Clear; virtual;
procedure OpenWadFile(const aname: string);
procedure LoadFromStream(const strm: TStream);
function EntryAsString(const id: integer): string; overload;
function EntryAsString(const aname: string): string; overload;
function ReadEntry(const id: integer; var buf: pointer; var bufsize: integer): boolean; overload;
function ReadEntry(const aname: string; var buf: pointer; var bufsize: integer): boolean; overload;
function EntryName(const id: integer): string;
function EntryId(const aname: string): integer;
function EntryInfo(const id: integer): Pfilelump_t; overload;
function EntryInfo(const aname: string): Pfilelump_t; overload;
function NumEntries: integer;
property FileName: string read ffilename;
end;
implementation
constructor TWadReader.Create;
begin
h.identification := 0;
h.numlumps := 0;
h.infotableofs := 0;
la := nil;
ss := nil;
ffilename := '';
Inherited;
end;
destructor TWadReader.Destroy;
begin
Clear;
Inherited;
end;
procedure TWadReader.Clear;
begin
if h.numlumps > 0 then
begin
FreeMem(la, h.numlumps * SizeOf(filelump_t));
h.identification := 0;
h.numlumps := 0;
h.infotableofs := 0;
la := nil;
ffilename := '';
end
else
begin
h.identification := 0;
h.infotableofs := 0;
end;
if ss <> nil then
begin
ss.Free;
ss := nil;
end;
end;
procedure TWadReader.OpenWadFile(const aname: string);
begin
if aname = '' then
Exit;
Clear;
if not FileExists(aname) then
Exit;
ss := TFileStream.Create(aname, fmOpenRead or fmShareDenyWrite);
ss.Read(h, SizeOf(wadinfo_t));
if (h.numlumps > 0) and (h.infotableofs < ss.Size) and ((h.identification = IWAD) or (h.identification = PWAD)) then
begin
ss.Position := h.infotableofs;
GetMem(la, h.numlumps * SizeOf(filelump_t));
ss.Read(la^, h.numlumps * SizeOf(filelump_t));
ffilename := aname;
end;
end;
procedure TWadReader.LoadFromStream(const strm: TStream);
begin
Clear;
ss := TMemoryStream.Create;
ss.CopyFrom(strm, strm.Size);
ss.Position := 0;
if ss.Size > SizeOf(wadinfo_t) then
begin
ss.Read(h, SizeOf(wadinfo_t));
if (h.numlumps > 0) and (h.infotableofs < ss.Size) and ((h.identification = IWAD) or (h.identification = PWAD)) then
begin
ss.Position := h.infotableofs;
GetMem(la, h.numlumps * SizeOf(filelump_t));
ss.Read(la^, h.numlumps * SizeOf(filelump_t));
ffilename := '';
end;
end;
end;
function TWadReader.EntryAsString(const id: integer): string;
begin
if (ss <> nil) and (id >= 0) and (id < h.numlumps) then
begin
SetLength(Result, la[id].size);
ss.Position := la[id].filepos;
ss.Read((@Result[1])^, la[id].size);
end
else
Result := '';
end;
function TWadReader.EntryAsString(const aname: string): string;
var
id: integer;
begin
id := EntryId(aname);
if id >= 0 then
Result := EntryAsString(id)
else
Result := '';
end;
function TWadReader.ReadEntry(const id: integer; var buf: pointer; var bufsize: integer): boolean;
begin
if (ss <> nil) and (id >= 0) and (id < h.numlumps) then
begin
ss.Position := la[id].filepos;
bufsize := la[id].size;
GetMem(buf, bufsize);
ss.Read(buf^, bufsize);
Result := true;
end
else
Result := False;
end;
function TWadReader.ReadEntry(const aname: string; var buf: pointer; var bufsize: integer): boolean;
var
id: integer;
begin
id := EntryId(aname);
if id >= 0 then
Result := ReadEntry(id, buf, bufsize)
else
Result := False;
end;
function TWadReader.EntryName(const id: integer): string;
begin
if (id >= 0) and (id < h.numlumps) then
Result := char8tostring(la[id].name)
else
Result := '';
end;
function TWadReader.EntryId(const aname: string): integer;
var
i: integer;
uname: string;
begin
uname := UpperCase(aname);
for i := h.numlumps - 1 downto 0 do
if UpperCase(char8tostring(la[i].name)) = uname then
begin
Result := i;
Exit;
end;
Result := -1;
end;
function TWadReader.EntryInfo(const id: integer): Pfilelump_t;
begin
if (id >= 0) and (id < h.numlumps) then
Result := @la[id]
else
Result := nil;
end;
function TWadReader.EntryInfo(const aname: string): Pfilelump_t;
begin
Result := EntryInfo(EntryId(aname));
end;
function TWadReader.NumEntries: integer;
begin
Result := h.numlumps;
end;
end.