-
Notifications
You must be signed in to change notification settings - Fork 0
/
mw_wadwriter.pas
208 lines (188 loc) · 4.96 KB
/
mw_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
197
198
199
200
201
202
203
204
205
206
207
//------------------------------------------------------------------------------
//
// MAD2WAD - Create a Doom WAD file from Mars, Hero or Tao MAD files
//
// 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:
// MAD writer
//
//------------------------------------------------------------------------------
// Site : https://sourceforge.net/projects/mars3d/
//------------------------------------------------------------------------------
unit mw_wadwriter;
interface
uses
SysUtils,
Classes;
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 fname: string);
procedure AddString(const lumpname: string; const data: string);
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
mw_types;
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
if (data = nil) or (size = 0) then
AddSeparator(lumpname)
else
begin
m := TMemoryStream.Create;
m.Write(data^, size);
lumps.AddObject(UpperCase(lumpname), m);
end;
end;
procedure TWadWriter.AddFile(const lumpname: string; const fname: string);
var
m: TMemoryStream;
f: TFileStream;
buf: array[0..8191] of byte;
numread: integer;
begin
if FileExists(fname) then
begin
f := TFileStream.Create(fname, fmOpenRead);
if f.Size = 0 then
begin
AddSeparator(lumpname);
f.Free;
exit;
end;
m := TMemoryStream.Create;
repeat
numread := f.Read(buf, SizeOf(buf));
if numread > 0 then
m.Write(buf, numread);
until numread <= 0;
lumps.AddObject(UpperCase(lumpname), m);
f.Free;
end
else
AddSeparator(lumpname);
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.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.Seek(0, soBeginning);
strm.Write(m.Memory^, m.Size);
end
else
la[i].size := 0;
la[i].name := stringtochar8(lumps.Strings[i]);
end;
ssize := strm.Position;
strm.Seek(p, soBeginning);
strm.Write(la^, lumps.Count * SizeOf(filelump_t));
FreeMem(la, lumps.Count * SizeOf(filelump_t));
strm.Seek(ssize, soBeginning);
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.