-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpk3_wadreader.pas
179 lines (161 loc) · 4.19 KB
/
pk3_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
//------------------------------------------------------------------------------
//
// PK3Entry
// PK3Entry is a tool to encapsulate multiple PK3 files inside a WAD file
// The output WAD file contains long filename aliases (PK3ENTRY lump)
// Copyright (C) 2019 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.
//
//------------------------------------------------------------------------------
// E-Mail: jimmyvalavanis@yahoo.gr
// Site : https://sourceforge.net/projects/pk3entry/
//------------------------------------------------------------------------------
unit pk3_wadreader;
interface
uses
Classes,
pk3_utils,
pk3_wad;
type
TWadReader = class(TObject)
private
h: header_t;
la: lumparray_p;
fs: TFileStream;
fonprogress: progress_t;
protected
procedure Progress(const pct: double);
public
constructor Create; virtual;
destructor Destroy; override;
procedure Clear; virtual;
procedure OpenWadFile(const aname: string);
function EntryAsString(const id: integer): string; overload;
function EntryAsString(const aname: string): string; overload;
function EntryName(const id: integer): string;
function EntryId(const aname: string): integer;
function NumEntries: integer;
end;
implementation
uses
SysUtils,
pk3_system;
constructor TWadReader.Create;
begin
h.identification := 0;
h.numlumps := 0;
h.infotableofs := 0;
la := nil;
fs := nil;
fonprogress := nil;
Inherited;
end;
destructor TWadReader.Destroy;
begin
Clear;
Inherited;
end;
procedure TWadReader.Clear;
begin
if h.numlumps > 0 then
begin
FreeMem(la, h.numlumps * SizeOf(lump_t));
h.identification := 0;
h.numlumps := 0;
h.infotableofs := 0;
la := nil;
end
else
begin
h.identification := 0;
h.infotableofs := 0;
end;
if fs <> nil then
begin
fs.Free;
fs := nil;
end;
end;
procedure TWadReader.Progress(const pct: double);
begin
if Assigned(fonprogress) then
fonprogress(pct);
end;
procedure TWadReader.OpenWadFile(const aname: string);
begin
if aname = '' then
Exit;
I_Verboseln('Opening WAD file ' + aname);
Clear;
fs := TFileStream.Create(aname, fmOpenRead);
fs.Read(h, SizeOf(header_t));
if (h.numlumps > 0) and (h.infotableofs < fs.Size) and ((h.identification = IWAD) or (h.identification = PWAD)) then
begin
fs.Seek(h.infotableofs, soFromBeginning);
GetMem(la, h.numlumps * SizeOf(lump_t));
fs.Read(la^, h.numlumps * SizeOf(lump_t));
end
else
I_Verboseln('Invalid WAD file ' + aname);
end;
function TWadReader.EntryAsString(const id: integer): string;
begin
if (fs <> nil) and (id >= 0) and (id < h.numlumps) then
begin
SetLength(Result, la[id].size);
fs.Position := la[id].filepos;
fs.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.EntryName(const id: integer): string;
begin
if (id >= 0) and (id < h.numlumps) then
Result := char8_to_string(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 char8_to_string(la[i].name) = uname then
begin
Result := i;
Exit;
end;
Result := -1;
end;
function TWadReader.NumEntries: integer;
begin
Result := h.numlumps;
end;
end.