-
Notifications
You must be signed in to change notification settings - Fork 0
/
ff_wadwriter.pas
197 lines (176 loc) · 4.81 KB
/
ff_wadwriter.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
//------------------------------------------------------------------------------
//
// DD_FONT: Doom Font Creator
// 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 Writer
//
//------------------------------------------------------------------------------
// E-Mail: jimmyvalavanis@yahoo.gr
// Site : https://sourceforge.net/projects/dd-font/
//------------------------------------------------------------------------------
unit ff_wadwriter;
interface
uses
Classes, SysUtils;
type
TWadWriter = class(TObject)
private
lumps: TStringList;
public
constructor Create; virtual;
destructor Destroy; override;
procedure Clear; virtual;
procedure AddData(const lumpname: string; const data: pointer; const size: integer);
procedure AddFile(const lumpname: string; const datafname: string);
procedure AddString(const lumpname: string; const data: string);
procedure AddStringList(const lumpname: string; const lst: TStringList);
procedure AddSeparator(const lumpname: string);
procedure SaveToStream(const strm: TStream);
procedure SaveToFile(const fname: string);
end;
function AddDataToWAD(const wad: TWADWriter; const lumpname, data: string): boolean;
implementation
uses
ff_wad;
constructor TWadWriter.Create;
begin
lumps := TStringList.Create;
Inherited;
end;
destructor TWadWriter.Destroy;
var
i: integer;
begin
for i := 0 to lumps.Count - 1 do
if lumps.Objects[i] <> nil then
lumps.Objects[i].Free;
lumps.Free;
Inherited;
end;
procedure TWadWriter.Clear;
var
i: integer;
begin
for i := 0 to lumps.Count - 1 do
if lumps.Objects[i] <> nil then
lumps.Objects[i].Free;
lumps.Clear;
end;
procedure TWadWriter.AddData(const lumpname: string; const data: pointer; const size: integer);
var
m: TMemoryStream;
begin
m := TMemoryStream.Create;
m.Write(data^, size);
lumps.AddObject(UpperCase(lumpname), m);
end;
procedure TWadWriter.AddFile(const lumpname: string; const datafname: string);
var
fs: TFileStream;
m: TMemoryStream;
begin
if FileExists(datafname) then
begin
fs := TFileStream.Create(datafname, fmOpenRead);
m := TMemoryStream.Create;
m.CopyFrom(fs, 0);
lumps.AddObject(UpperCase(lumpname), m);
fs.Free;
end;
end;
procedure TWadWriter.AddString(const lumpname: string; const data: string);
var
m: TMemoryStream;
i: integer;
begin
m := TMemoryStream.Create;
for i := 1 to Length(data) do
m.Write(data[i], SizeOf(char));
lumps.AddObject(UpperCase(lumpname), m);
end;
procedure TWadWriter.AddStringList(const lumpname: string; const lst: TStringList);
var
stmp: string;
begin
stmp := lst.Text;
AddString(lumpname, stmp);
end;
procedure TWadWriter.AddSeparator(const lumpname: string);
begin
lumps.Add(UpperCase(lumpname));
end;
procedure TWadWriter.SaveToStream(const strm: TStream);
var
h: wadinfo_t;
la: Pfilelump_tArray;
i: integer;
p, ssize: integer;
m: TMemoryStream;
begin
p := strm.Position;
h.identification := PWAD;
h.numlumps := lumps.Count;
h.infotableofs := p + SizeOf(wadinfo_t);
strm.Write(h, SizeOf(h));
p := strm.Position;
GetMem(la, lumps.Count * SizeOf(filelump_t));
strm.Write(la^, lumps.Count * SizeOf(filelump_t));
for i := 0 to lumps.Count - 1 do
begin
la[i].filepos := strm.Position;
m := lumps.Objects[i] as TMemoryStream;
if m <> nil then
begin
la[i].size := m.Size;
m.Position := 0;
strm.Write(m.Memory^, m.Size);
end
else
la[i].size := 0;
la[i].name := stringtochar8(lumps.Strings[i]);
end;
ssize := strm.Position;
strm.Position := p;
strm.Write(la^, lumps.Count * SizeOf(filelump_t));
FreeMem(la, lumps.Count * SizeOf(filelump_t));
strm.Position := ssize;
end;
procedure TWadWriter.SaveToFile(const fname: string);
var
fs: TFileStream;
begin
fs := TFileStream.Create(fname, fmCreate);
try
SaveToStream(fs);
finally
fs.Free;
end;
end;
function AddDataToWAD(const wad: TWADWriter; const lumpname, data: string): boolean;
begin
if wad <> nil then
begin
wad.AddString(lumpname, data);
Result := True;
end
else
Result := False;
end;
end.