1 (* Copyright (C) DooM 2D:Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE a_modes.inc}
19 {$DEFINE SFS_DFWAD_DEBUG}
20 {$DEFINE SFS_MAPDETECT_FX}
25 sfs
, xstreams
, Classes
;
29 SArray
= array of ShortString;
33 fFileName
: AnsiString; // empty: not opened
36 function getIsOpen (): Boolean;
37 function isMapResource (idx
: Integer): Boolean;
39 function GetResourceEx (name
: AnsiString; wantMap
: Boolean; var pData
: Pointer; var Len
: Integer): Boolean;
42 constructor Create ();
43 destructor Destroy (); override;
47 function ReadFile (FileName
: AnsiString): Boolean;
48 function ReadMemory (Data
: Pointer; Len
: LongWord): Boolean;
50 function GetResource (name
: AnsiString; var pData
: Pointer; var Len
: Integer): Boolean;
51 function GetMapResource (name
: AnsiString; var pData
: Pointer; var Len
: Integer): Boolean;
52 function GetMapResources (): SArray
;
54 // returns `nil` if file wasn't found
55 function openFileStream (name
: AnsiString): TStream
;
57 property isOpen
: Boolean read getIsOpen
;
61 function g_ExtractWadName (resourceStr
: AnsiString): AnsiString;
62 function g_ExtractWadNameNoPath (resourceStr
: AnsiString): AnsiString;
63 function g_ExtractFilePath (resourceStr
: AnsiString): AnsiString;
64 function g_ExtractFileName (resourceStr
: AnsiString): AnsiString; // without path
65 function g_ExtractFilePathName (resourceStr
: AnsiString): AnsiString;
67 // return fixed AnsiString or empty AnsiString
68 function findDiskWad (fname
: AnsiString): AnsiString;
72 wadoptDebug
: Boolean = false;
73 wadoptFast
: Boolean = false;
79 SysUtils
, e_log
, utils
, MAPDEF
;
82 function findDiskWad (fname
: AnsiString): AnsiString;
85 if not findFileCI(fname
) then
87 //e_WriteLog(Format('findDiskWad: error looking for [%s]', [fname]), MSG_NOTIFY);
88 if StrEquCI1251(ExtractFileExt(fname
), '.wad') then
90 fname
:= ChangeFileExt(fname
, '.pk3');
91 //e_WriteLog(Format(' looking for [%s]', [fname]), MSG_NOTIFY);
92 if not findFileCI(fname
) then
94 fname
:= ChangeFileExt(fname
, '.zip');
95 //e_WriteLog(Format(' looking for [%s]', [fname]), MSG_NOTIFY);
96 if not findFileCI(fname
) then exit
;
104 //e_WriteLog(Format('findDiskWad: FOUND [%s]', [fname]), MSG_NOTIFY);
109 function normSlashes (s
: AnsiString): AnsiString;
113 for f
:= 1 to length(s
) do if s
[f
] = '\' then s
[f
] := '/';
117 function g_ExtractWadNameNoPath (resourceStr
: AnsiString): AnsiString;
121 for f
:= length(resourceStr
) downto 1 do
123 if resourceStr
[f
] = ':' then
125 result
:= normSlashes(Copy(resourceStr
, 1, f
-1));
127 while (c
> 0) and (result
[c
] <> '/') do Dec(c
);
128 if c
> 0 then result
:= Copy(result
, c
+1, length(result
));
135 function g_ExtractWadName (resourceStr
: AnsiString): AnsiString;
139 for f
:= length(resourceStr
) downto 1 do
141 if resourceStr
[f
] = ':' then
143 result
:= normSlashes(Copy(resourceStr
, 1, f
-1));
150 function g_ExtractFilePath (resourceStr
: AnsiString): AnsiString;
152 f
, lastSlash
: Integer;
156 for f
:= length(resourceStr
) downto 1 do
158 if (lastSlash
< 0) and (resourceStr
[f
] = '\') or (resourceStr
[f
] = '/') then lastSlash
:= f
;
159 if resourceStr
[f
] = ':' then
161 if lastSlash
> 0 then
163 result
:= normSlashes(Copy(resourceStr
, f
, lastSlash
-f
));
164 while (length(result
) > 0) and (result
[1] = '/') do Delete(result
, 1, 1);
169 if lastSlash
> 0 then result
:= normSlashes(Copy(resourceStr
, 1, lastSlash
-1));
172 function g_ExtractFileName (resourceStr
: AnsiString): AnsiString; // without path
174 f
, lastSlash
: Integer;
178 for f
:= length(resourceStr
) downto 1 do
180 if (lastSlash
< 0) and (resourceStr
[f
] = '\') or (resourceStr
[f
] = '/') then lastSlash
:= f
;
181 if resourceStr
[f
] = ':' then
183 if lastSlash
> 0 then result
:= Copy(resourceStr
, lastSlash
+1, length(resourceStr
));
187 if lastSlash
> 0 then result
:= Copy(resourceStr
, lastSlash
+1, length(resourceStr
));
190 function g_ExtractFilePathName (resourceStr
: AnsiString): AnsiString;
195 for f
:= length(resourceStr
) downto 1 do
197 if resourceStr
[f
] = ':' then
199 result
:= normSlashes(Copy(resourceStr
, f
+1, length(resourceStr
)));
200 while (length(result
) > 0) and (result
[1] = '/') do Delete(result
, 1, 1);
204 result
:= normSlashes(resourceStr
);
205 while (length(result
) > 0) and (result
[1] = '/') do Delete(result
, 1, 1);
211 constructor TWADFile
.Create();
217 destructor TWADFile
.Destroy();
224 function TWADFile
.getIsOpen (): Boolean;
226 result
:= (fFileName
<> '');
230 procedure TWADFile
.FreeWAD();
232 if fIter
<> nil then FreeAndNil(fIter
);
233 //if fFileName <> '' then e_WriteLog(Format('TWADFile.ReadFile: [%s] closed', [fFileName]), MSG_NOTIFY);
238 //FIXME: detect text maps properly here
239 function TWADFile
.isMapResource (idx
: Integer): Boolean;
241 sign
: packed array [0..2] of Char;
245 if not isOpen
or (fIter
= nil) then exit
;
246 if (idx
< 0) or (idx
>= fIter
.Count
) then exit
;
248 fs
:= fIter
.volume
.OpenFileByIndex(idx
);
249 fs
.readBuffer(sign
, 3);
250 result
:= (sign
= MAP_SIGNATURE
);
251 if not result
then result
:= (sign
[0] = 'm') and (sign
[1] = 'a') and (sign
[2] = 'p');
253 if fs
<> nil then fs
.Free();
260 // returns `nil` if file wasn't found
261 function TWADFile
.openFileStream (name
: AnsiString): TStream
;
267 // backwards, due to possible similar names and such
268 for f
:= fIter
.Count
-1 downto 0 do
270 fi
:= fIter
.Files
[f
];
271 if fi
= nil then continue
;
272 if StrEquCI1251(fi
.name
, name
) then
275 result
:= fIter
.volume
.OpenFileByIndex(f
);
279 if (result
<> nil) then exit
;
285 function removeExt (s
: AnsiString): AnsiString;
290 while (i
> 1) and (s
[i
-1] <> '.') and (s
[i
-1] <> '/') do Dec(i
);
291 if (i
> 1) and (s
[i
-1] = '.') then
293 //writeln('[', s, '] -> [', Copy(s, 1, i-2), ']');
294 s
:= Copy(s
, 1, i
-2);
300 function TWADFile
.GetResourceEx (name
: AnsiString; wantMap
: Boolean; var pData
: Pointer; var Len
: Integer): Boolean;
302 f
, lastSlash
: Integer;
306 rpath
, rname
: AnsiString;
307 sign
: packed array [0..2] of Char;
311 if not isOpen
or (fIter
= nil) then Exit
;
312 rname
:= removeExt(name
);
313 if length(rname
) = 0 then Exit
; // just in case
315 for f
:= 1 to length(rname
) do
317 if rname
[f
] = '\' then rname
[f
] := '/';
318 if rname
[f
] = '/' then lastSlash
:= f
;
320 if lastSlash
> 0 then
322 rpath
:= Copy(rname
, 1, lastSlash
);
323 Delete(rname
, 1, lastSlash
);
329 // backwards, due to possible similar names and such
330 for f
:= fIter
.Count
-1 downto 0 do
332 fi
:= fIter
.Files
[f
];
333 if fi
= nil then continue
;
334 if StrEquCI1251(removeExt(fi
.name
), rname
) then
336 // i found her (maybe)
339 if length(fi
.path
) < length(rpath
) then continue
; // alas
340 if length(fi
.path
) = length(rpath
) then
342 if not StrEquCI1251(fi
.path
, rpath
) then continue
; // alas
346 if fi
.path
[length(fi
.path
)-length(rpath
)] <> '/' then continue
; // alas
347 if not StrEquCI1251(Copy(fi
.path
, length(fi
.path
)+1-length(rpath
), length(fi
.path
)), rpath
) then continue
; // alas
351 fs
:= fIter
.volume
.OpenFileByIndex(f
);
357 if wantMap
then continue
;
358 e_WriteLog(Format('DFWAD: can''t open file [%s] in [%s]', [name
, fFileName
]), MSG_WARNING
);
361 // if we want only maps, check if this is map
362 {$IFDEF SFS_MAPDETECT_FX}
366 {$IF DEFINED(D2D_NEW_MAP_READER_DBG)}
367 e_LogWritefln('DFWAD: checking for good map in wad [%s], file [%s] (#%d)', [fFileName
, fi
.fname
, f
]);
370 fs
.readBuffer(sign
, 3);
371 goodMap
:= (sign
= MAP_SIGNATURE
);
372 if not goodMap
then goodMap
:= (sign
[0] = 'm') and (sign
[1] = 'a') and (sign
[2] = 'p');
373 {$IF DEFINED(D2D_NEW_MAP_READER_DBG)}
375 e_LogWritefln(' GOOD map in wad [%s], file [%s] (#%d)', [fFileName
, fi
.fname
, f
])
377 e_LogWritefln(' BAD map in wad [%s], file [%s] (#%d)', [fFileName
, fi
.fname
, f
]);
383 {$IF DEFINED(D2D_NEW_MAP_READER_DBG)}
384 e_LogWritefln(' not a map in wad [%s], file [%s] (#%d)', [fFileName
, fi
.fname
, f
]);
392 Len
:= Integer(fs
.size
);
396 fs
.ReadBuffer(pData
^, Len
);
407 {$IFNDEF SFS_MAPDETECT_FX}
413 Move(pData
^, sign
, 3);
414 goodMap
:= (sign
= MAP_SIGNATURE
);
418 //e_WriteLog(Format(' not a map in wad [%s], file [%s] (#%d)', [fFileName, fi.fname, f]), MSG_NOTIFY);
427 {$IFDEF SFS_DFWAD_DEBUG}
429 e_WriteLog(Format('DFWAD: file [%s] FOUND in [%s]; size is %d bytes', [name
, fFileName
, Len
]), MSG_NOTIFY
);
434 e_WriteLog(Format('DFWAD: file [%s] not found in [%s]', [name
, fFileName
]), MSG_WARNING
);
437 function TWADFile
.GetResource (name
: AnsiString; var pData
: Pointer; var Len
: Integer): Boolean;
439 result
:= GetResourceEx(name
, false, pData
, Len
);
442 function TWADFile
.GetMapResource (name
: AnsiString; var pData
: Pointer; var Len
: Integer): Boolean;
444 result
:= GetResourceEx(name
, true, pData
, Len
);
447 function TWADFile
.GetMapResources (): SArray
;
454 if not isOpen
or (fIter
= nil) then Exit
;
455 for f
:= fIter
.Count
-1 downto 0 do
457 fi
:= fIter
.Files
[f
];
458 if fi
= nil then continue
;
459 if length(fi
.name
) = 0 then continue
;
460 {$IF DEFINED(D2D_NEW_MAP_READER)}
461 //e_LogWritefln('DFWAD: checking for map in wad [%s], file [%s] (#%d)', [fFileName, fi.fname, f]);
463 if isMapResource(f
) then
465 s
:= removeExt(fi
.name
);
469 if StrEquCI1251(result
[c
], s
) then break
;
474 SetLength(result
, Length(result
)+1);
475 result
[high(result
)] := removeExt(fi
.name
);
482 function TWADFile
.ReadFile (FileName
: AnsiString): Boolean;
489 //e_WriteLog(Format('TWADFile.ReadFile: [%s]', [FileName]), MSG_NOTIFY);
491 rfn
:= findDiskWad(FileName
);
492 if length(rfn
) = 0 then
494 e_WriteLog(Format('TWADFile.ReadFile: error looking for [%s]', [FileName
]), MSG_NOTIFY
);
497 {$IFDEF SFS_DFWAD_DEBUG}
498 if wadoptDebug
then e_WriteLog(Format('TWADFile.ReadFile: FOUND [%s]', [rfn
]), MSG_NOTIFY
);
504 if not SFSAddDataFile(rfn
, true) then exit
;
508 if not SFSAddDataFileTemp(rfn
, true) then exit
;
513 fIter
:= SFSFileList(rfn
);
514 if fIter
= nil then Exit
;
516 {$IFDEF SFS_DFWAD_DEBUG}
517 if wadoptDebug
then e_WriteLog(Format('TWADFile.ReadFile: [%s] opened', [fFileName
]), MSG_NOTIFY
);
524 uniqueCounter
: Integer = 0;
526 function TWADFile
.ReadMemory (Data
: Pointer; Len
: LongWord): Boolean;
535 if (Data
= nil) or (Len
= 0) then
537 e_WriteLog('TWADFile.ReadMemory: EMPTY SUBWAD!', MSG_WARNING
);
541 fn
:= Format(' -- memwad %d -- ', [uniqueCounter
]);
543 {$IFDEF SFS_DFWAD_DEBUG}
544 if wadoptDebug
then e_WriteLog(Format('TWADFile.ReadMemory: [%s]', [fn
]), MSG_NOTIFY
);
548 st
:= TSFSMemoryStreamRO
.Create(Data
, Len
);
549 if not SFSAddSubDataFile(fn
, st
, true) then
559 fIter
:= SFSFileList(fn
);
560 if fIter
= nil then Exit
;
563 {$IFDEF SFS_DFWAD_DEBUG}
564 if wadoptDebug
then e_WriteLog(Format('TWADFile.ReadMemory: [%s] opened', [fFileName
]), MSG_NOTIFY
);
568 for f := 0 to fIter.Count-1 do
570 fi := fIter.Files[f];
571 if fi = nil then continue;
572 st := fIter.volume.OpenFileByIndex(f);
575 e_WriteLog(Format('[%s]: [%s : %s] CAN''T OPEN', [fFileName, fi.path, fi.name]), MSG_NOTIFY);
579 e_WriteLog(Format('[%s]: [%s : %s] %u', [fFileName, fi.path, fi.name, st.size]), MSG_NOTIFY);
583 //fIter.volume.OpenFileByIndex(0);
591 sfsDiskDirs
:= '<exedir>/data'; //FIXME