1 (* Copyright (C) Doom 2D: Forever Developers
2 *
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, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE a_modes.inc}
18 {$DEFINE SFS_DFWAD_DEBUG}
19 {$DEFINE SFS_MAPDETECT_FX}
21 interface
23 uses
24 Classes,
29 type
31 private
38 function GetResourceEx (name: AnsiString; wantMap: Boolean; var pData: Pointer; var Len: Integer; logError: Boolean=true): Boolean;
40 public
49 function GetResource (name: AnsiString; var pData: Pointer; var Len: Integer; logError: Boolean=true): Boolean;
50 function GetMapResource (name: AnsiString; var pData: Pointer; var Len: Integer; logError: Boolean=true): Boolean;
53 // returns `nil` if file wasn't found
67 var
72 implementation
74 uses
79 var
81 begin
87 var
89 begin
91 begin
93 begin
98 exit;
105 var
107 begin
109 begin
111 begin
113 exit;
120 var
122 begin
126 begin
129 begin
131 begin
135 exit;
142 var
144 begin
148 begin
151 begin
153 exit;
160 var
162 begin
165 begin
167 begin
170 exit;
179 { TWADFile }
181 begin
187 begin
194 begin
200 begin
202 //if fFileName <> '' then e_WriteLog(Format('TWADFile.ReadFile: [%s] closed', [fFileName]), MSG_NOTIFY);
207 //FIXME: detect text maps properly here
209 var
210 //sign: packed array [0..2] of Char;
212 begin
216 try
219 (*
220 fs.readBuffer(sign, 3);
221 result := (sign = MAP_SIGNATURE);
222 if not result then result := (sign[0] = 'm') and (sign[1] = 'a') and (sign[2] = 'p');
223 *)
224 except
227 exit;
233 // returns `nil` if file wasn't found
235 var
238 begin
240 // backwards, due to possible similar names and such
242 begin
246 begin
247 try
249 except
259 var
261 begin
265 begin
266 //writeln('[', s, '] -> [', Copy(s, 1, i-2), ']');
273 function TWADFile.GetResourceEx (name: AnsiString; wantMap: Boolean; var pData: Pointer; var Len: Integer; logError: Boolean=true): Boolean;
274 var
280 //sign: packed array [0..2] of Char;
282 {$IFNDEF SFS_MAPDETECT_FX}
284 {$ENDIF}
285 begin
292 begin
297 begin
300 end
301 else
302 begin
305 // backwards, due to possible similar names and such
307 begin
311 begin
312 // i found her (maybe)
314 begin
317 begin
319 end
320 else
321 begin
323 if not StrEquCI1251(Copy(fi.path, length(fi.path)+1-length(rpath), length(fi.path)), rpath) then continue; // alas
326 try
328 except
332 begin
334 if logError then e_WriteLog(Format('DFWAD: can''t open file [%s] in [%s]', [name, fFileName]), TMsgType.Warning);
335 break;
337 // if we want only maps, check if this is map
338 {$IFDEF SFS_MAPDETECT_FX}
340 begin
342 {$IF DEFINED(D2D_NEW_MAP_READER_DBG)}
343 e_LogWritefln('DFWAD: checking for good map in wad [%s], file [%s] (#%d)', [fFileName, fi.fname, f]);
344 {$ENDIF}
345 try
346 //fs.readBuffer(sign, 3);
347 //goodMap := (sign = MAP_SIGNATURE);
348 //if not goodMap then goodMap := (sign[0] = 'm') and (sign[1] = 'a') and (sign[2] = 'p');
350 {$IF DEFINED(D2D_NEW_MAP_READER_DBG)}
353 else
355 {$ENDIF}
356 except
359 begin
360 {$IF DEFINED(D2D_NEW_MAP_READER_DBG)}
362 {$ENDIF}
364 continue;
368 {$ENDIF}
372 try
375 finally
377 begin
384 {$IFNDEF SFS_MAPDETECT_FX}
386 begin
389 begin
390 //Move(pData^, sign, 3);
391 //goodMap := (sign = MAP_SIGNATURE);
393 try
395 except
401 begin
402 //e_WriteLog(Format(' not a map in wad [%s], file [%s] (#%d)', [fFileName, fi.fname, f]), MSG_NOTIFY);
406 continue;
409 {$ENDIF}
411 {$IFDEF SFS_DFWAD_DEBUG}
413 e_WriteLog(Format('DFWAD: file [%s] FOUND in [%s]; size is %d bytes', [name, fFileName, Len]), TMsgType.Notify);
414 {$ENDIF}
415 exit;
418 if logError then e_WriteLog(Format('DFWAD: file [%s] not found in [%s]', [name, fFileName]), TMsgType.Warning);
421 function TWADFile.GetResource (name: AnsiString; var pData: Pointer; var Len: Integer; logError: Boolean=true): Boolean;
422 begin
426 function TWADFile.GetMapResource (name: AnsiString; var pData: Pointer; var Len: Integer; logError: Boolean=true): Boolean;
427 begin
432 var
436 begin
440 begin
444 {$IF DEFINED(D2D_NEW_MAP_READER)}
445 //e_LogWritefln('DFWAD: checking for map in wad [%s], file [%s] (#%d)', [fFileName, fi.fname, f]);
446 {$ENDIF}
448 begin
452 begin
457 begin
467 var
469 //f: Integer;
470 //fi: TSFSFileInfo;
471 begin
473 //e_WriteLog(Format('TWADFile.ReadFile: [%s]', [FileName]), MSG_NOTIFY);
477 begin
479 exit;
481 {$IFDEF SFS_DFWAD_DEBUG}
482 if wadoptDebug then e_WriteLog(Format('TWADFile.ReadFile: FOUND [%s]', [rfn]), TMsgType.Notify);
483 {$ENDIF}
484 // cache this wad
485 try
487 begin
489 end
490 else
491 begin
494 except
495 exit;
500 {$IFDEF SFS_DFWAD_DEBUG}
501 if wadoptDebug then e_WriteLog(Format('TWADFile.ReadFile: [%s] opened', [fFileName]), TMsgType.Notify);
502 {$ENDIF}
507 var
511 var
514 //f: Integer;
515 //fi: TSFSFileInfo;
516 begin
520 begin
522 Exit;
527 {$IFDEF SFS_DFWAD_DEBUG}
529 {$ENDIF}
531 try
534 begin
536 Exit;
538 except
540 Exit;
547 {$IFDEF SFS_DFWAD_DEBUG}
548 if wadoptDebug then e_WriteLog(Format('TWADFile.ReadMemory: [%s] opened', [fFileName]), TMsgType.Notify);
549 {$ENDIF}
551 {
552 for f := 0 to fIter.Count-1 do
553 begin
554 fi := fIter.Files[f];
555 if fi = nil then continue;
556 st := fIter.volume.OpenFileByIndex(f);
557 if st = nil then
558 begin
559 e_WriteLog(Format('[%s]: [%s : %s] CAN''T OPEN', [fFileName, fi.path, fi.name]), MSG_NOTIFY);
560 end
561 else
562 begin
563 e_WriteLog(Format('[%s]: [%s : %s] %u', [fFileName, fi.path, fi.name, st.size]), MSG_NOTIFY);
564 st.Free;
565 end;
566 end;
567 //fIter.volume.OpenFileByIndex(0);
568 }
574 begin