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, either version 3 of the License, or
6 * (at your option) any later version.
7 *
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.
12 *
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/>.
15 *)
16 {$INCLUDE a_modes.inc}
19 {$DEFINE SFS_DFWAD_DEBUG}
20 {$DEFINE SFS_MAPDETECT_FX}
22 interface
24 uses
25 Classes,
30 type
32 private
39 function GetResourceEx (name: AnsiString; wantMap: Boolean; var pData: Pointer; var Len: Integer; logError: Boolean=true): Boolean;
41 public
50 function GetResource (name: AnsiString; var pData: Pointer; var Len: Integer; logError: Boolean=true): Boolean;
51 function GetMapResource (name: AnsiString; var pData: Pointer; var Len: Integer; logError: Boolean=true): Boolean;
54 // returns `nil` if file wasn't found
68 var
73 implementation
75 uses
80 var
82 begin
88 var
90 begin
92 begin
94 begin
99 exit;
106 var
108 begin
110 begin
112 begin
114 exit;
121 var
123 begin
127 begin
130 begin
132 begin
136 exit;
143 var
145 begin
149 begin
152 begin
154 exit;
161 var
163 begin
166 begin
168 begin
171 exit;
180 { TWADFile }
182 begin
188 begin
195 begin
201 begin
203 //if fFileName <> '' then e_WriteLog(Format('TWADFile.ReadFile: [%s] closed', [fFileName]), MSG_NOTIFY);
208 //FIXME: detect text maps properly here
210 var
211 //sign: packed array [0..2] of Char;
213 begin
217 try
220 (*
221 fs.readBuffer(sign, 3);
222 result := (sign = MAP_SIGNATURE);
223 if not result then result := (sign[0] = 'm') and (sign[1] = 'a') and (sign[2] = 'p');
224 *)
225 except
228 exit;
234 // returns `nil` if file wasn't found
236 var
239 begin
241 // backwards, due to possible similar names and such
243 begin
247 begin
248 try
250 except
260 var
262 begin
266 begin
267 //writeln('[', s, '] -> [', Copy(s, 1, i-2), ']');
274 function TWADFile.GetResourceEx (name: AnsiString; wantMap: Boolean; var pData: Pointer; var Len: Integer; logError: Boolean=true): Boolean;
275 var
281 //sign: packed array [0..2] of Char;
283 {$IFNDEF SFS_MAPDETECT_FX}
285 {$ENDIF}
286 begin
293 begin
298 begin
301 end
302 else
303 begin
306 // backwards, due to possible similar names and such
308 begin
312 begin
313 // i found her (maybe)
315 begin
318 begin
320 end
321 else
322 begin
324 if not StrEquCI1251(Copy(fi.path, length(fi.path)+1-length(rpath), length(fi.path)), rpath) then continue; // alas
327 try
329 except
333 begin
335 if logError then e_WriteLog(Format('DFWAD: can''t open file [%s] in [%s]', [name, fFileName]), TMsgType.Warning);
336 break;
338 // if we want only maps, check if this is map
339 {$IFDEF SFS_MAPDETECT_FX}
341 begin
343 {$IF DEFINED(D2D_NEW_MAP_READER_DBG)}
344 e_LogWritefln('DFWAD: checking for good map in wad [%s], file [%s] (#%d)', [fFileName, fi.fname, f]);
345 {$ENDIF}
346 try
347 //fs.readBuffer(sign, 3);
348 //goodMap := (sign = MAP_SIGNATURE);
349 //if not goodMap then goodMap := (sign[0] = 'm') and (sign[1] = 'a') and (sign[2] = 'p');
351 {$IF DEFINED(D2D_NEW_MAP_READER_DBG)}
354 else
356 {$ENDIF}
357 except
360 begin
361 {$IF DEFINED(D2D_NEW_MAP_READER_DBG)}
363 {$ENDIF}
365 continue;
369 {$ENDIF}
373 try
376 finally
378 begin
385 {$IFNDEF SFS_MAPDETECT_FX}
387 begin
390 begin
391 //Move(pData^, sign, 3);
392 //goodMap := (sign = MAP_SIGNATURE);
394 try
396 except
402 begin
403 //e_WriteLog(Format(' not a map in wad [%s], file [%s] (#%d)', [fFileName, fi.fname, f]), MSG_NOTIFY);
407 continue;
410 {$ENDIF}
412 {$IFDEF SFS_DFWAD_DEBUG}
414 e_WriteLog(Format('DFWAD: file [%s] FOUND in [%s]; size is %d bytes', [name, fFileName, Len]), TMsgType.Notify);
415 {$ENDIF}
416 exit;
419 if logError then e_WriteLog(Format('DFWAD: file [%s] not found in [%s]', [name, fFileName]), TMsgType.Warning);
422 function TWADFile.GetResource (name: AnsiString; var pData: Pointer; var Len: Integer; logError: Boolean=true): Boolean;
423 begin
427 function TWADFile.GetMapResource (name: AnsiString; var pData: Pointer; var Len: Integer; logError: Boolean=true): Boolean;
428 begin
433 var
437 begin
441 begin
445 {$IF DEFINED(D2D_NEW_MAP_READER)}
446 //e_LogWritefln('DFWAD: checking for map in wad [%s], file [%s] (#%d)', [fFileName, fi.fname, f]);
447 {$ENDIF}
449 begin
453 begin
458 begin
468 var
470 //f: Integer;
471 //fi: TSFSFileInfo;
472 begin
474 //e_WriteLog(Format('TWADFile.ReadFile: [%s]', [FileName]), MSG_NOTIFY);
478 begin
480 exit;
482 {$IFDEF SFS_DFWAD_DEBUG}
483 if wadoptDebug then e_WriteLog(Format('TWADFile.ReadFile: FOUND [%s]', [rfn]), TMsgType.Notify);
484 {$ENDIF}
485 // cache this wad
486 try
488 begin
490 end
491 else
492 begin
495 except
496 exit;
501 {$IFDEF SFS_DFWAD_DEBUG}
502 if wadoptDebug then e_WriteLog(Format('TWADFile.ReadFile: [%s] opened', [fFileName]), TMsgType.Notify);
503 {$ENDIF}
508 var
512 var
515 //f: Integer;
516 //fi: TSFSFileInfo;
517 begin
521 begin
523 Exit;
528 {$IFDEF SFS_DFWAD_DEBUG}
530 {$ENDIF}
532 try
535 begin
537 Exit;
539 except
541 Exit;
548 {$IFDEF SFS_DFWAD_DEBUG}
549 if wadoptDebug then e_WriteLog(Format('TWADFile.ReadMemory: [%s] opened', [fFileName]), TMsgType.Notify);
550 {$ENDIF}
552 {
553 for f := 0 to fIter.Count-1 do
554 begin
555 fi := fIter.Files[f];
556 if fi = nil then continue;
557 st := fIter.volume.OpenFileByIndex(f);
558 if st = nil then
559 begin
560 e_WriteLog(Format('[%s]: [%s : %s] CAN''T OPEN', [fFileName, fi.path, fi.name]), MSG_NOTIFY);
561 end
562 else
563 begin
564 e_WriteLog(Format('[%s]: [%s : %s] %u', [fFileName, fi.path, fi.name, st.size]), MSG_NOTIFY);
565 st.Free;
566 end;
567 end;
568 //fIter.volume.OpenFileByIndex(0);
569 }
575 begin