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
59 // g_ExtractWadName C:\svr\shit.wad:\MAPS\MAP01 -> C:/svr/shit.wad
62 // g_ExtractWadNameNoPath C:\svr\shit.wad:\MAPS\MAP01 -> shit.wad
65 // g_ExtractFilePath C:\svr\shit.wad:\MAPS\MAP01 -> :/MAPS
68 // g_ExtractFileName C:\svr\shit.wad:\MAPS\MAP01 -> MAP01
71 // g_ExtractFilePathName C:\svr\shit.wad:\MAPS\MAP01 -> MAPS/MAP01
75 var
80 implementation
82 uses
87 var
89 begin
95 var
97 begin
99 begin
101 begin
106 exit;
113 var
115 begin
117 begin
119 begin
121 exit;
128 var
130 begin
134 begin
137 begin
139 begin
143 exit;
150 var
152 begin
156 begin
159 begin
161 exit;
168 var
170 begin
173 begin
175 begin
178 exit;
187 { TWADFile }
189 begin
195 begin
202 begin
208 begin
210 //if fFileName <> '' then e_WriteLog(Format('TWADFile.ReadFile: [%s] closed', [fFileName]), MSG_NOTIFY);
215 //FIXME: detect text maps properly here
217 var
218 //sign: packed array [0..2] of Char;
220 begin
224 try
227 (*
228 fs.readBuffer(sign, 3);
229 result := (sign = MAP_SIGNATURE);
230 if not result then result := (sign[0] = 'm') and (sign[1] = 'a') and (sign[2] = 'p');
231 *)
232 except
235 exit;
241 // returns `nil` if file wasn't found
243 var
246 begin
248 // backwards, due to possible similar names and such
250 begin
254 begin
255 try
257 except
267 var
269 begin
273 begin
274 //writeln('[', s, '] -> [', Copy(s, 1, i-2), ']');
281 function TWADFile.GetResourceEx (name: AnsiString; wantMap: Boolean; var pData: Pointer; var Len: Integer; logError: Boolean=true): Boolean;
282 var
288 //sign: packed array [0..2] of Char;
290 {$IFNDEF SFS_MAPDETECT_FX}
292 {$ENDIF}
293 begin
300 begin
305 begin
308 end
309 else
310 begin
313 // backwards, due to possible similar names and such
315 begin
319 begin
320 // i found her (maybe)
322 begin
325 begin
327 end
328 else
329 begin
331 if not StrEquCI1251(Copy(fi.path, length(fi.path)+1-length(rpath), length(fi.path)), rpath) then continue; // alas
334 try
336 except
340 begin
342 if logError then e_WriteLog(Format('DFWAD: can''t open file [%s] in [%s]', [name, fFileName]), TMsgType.Warning);
343 break;
345 // if we want only maps, check if this is map
346 {$IFDEF SFS_MAPDETECT_FX}
348 begin
350 {$IF DEFINED(D2D_NEW_MAP_READER_DBG)}
351 e_LogWritefln('DFWAD: checking for good map in wad [%s], file [%s] (#%d)', [fFileName, fi.fname, f]);
352 {$ENDIF}
353 try
354 //fs.readBuffer(sign, 3);
355 //goodMap := (sign = MAP_SIGNATURE);
356 //if not goodMap then goodMap := (sign[0] = 'm') and (sign[1] = 'a') and (sign[2] = 'p');
358 {$IF DEFINED(D2D_NEW_MAP_READER_DBG)}
361 else
363 {$ENDIF}
364 except
367 begin
368 {$IF DEFINED(D2D_NEW_MAP_READER_DBG)}
370 {$ENDIF}
372 continue;
376 {$ENDIF}
380 try
383 finally
385 begin
392 {$IFNDEF SFS_MAPDETECT_FX}
394 begin
397 begin
398 //Move(pData^, sign, 3);
399 //goodMap := (sign = MAP_SIGNATURE);
401 try
403 except
409 begin
410 //e_WriteLog(Format(' not a map in wad [%s], file [%s] (#%d)', [fFileName, fi.fname, f]), MSG_NOTIFY);
414 continue;
417 {$ENDIF}
419 {$IFDEF SFS_DFWAD_DEBUG}
421 e_WriteLog(Format('DFWAD: file [%s] FOUND in [%s]; size is %d bytes', [name, fFileName, Len]), TMsgType.Notify);
422 {$ENDIF}
423 exit;
426 if logError then e_WriteLog(Format('DFWAD: file [%s] not found in [%s]', [name, fFileName]), TMsgType.Warning);
429 function TWADFile.GetResource (name: AnsiString; var pData: Pointer; var Len: Integer; logError: Boolean=true): Boolean;
430 begin
434 function TWADFile.GetMapResource (name: AnsiString; var pData: Pointer; var Len: Integer; logError: Boolean=true): Boolean;
435 begin
440 var
444 begin
448 begin
452 {$IF DEFINED(D2D_NEW_MAP_READER)}
453 //e_LogWritefln('DFWAD: checking for map in wad [%s], file [%s] (#%d)', [fFileName, fi.fname, f]);
454 {$ENDIF}
456 begin
460 begin
465 begin
475 var
477 //f: Integer;
478 //fi: TSFSFileInfo;
479 begin
481 //e_WriteLog(Format('TWADFile.ReadFile: [%s]', [FileName]), MSG_NOTIFY);
485 begin
487 exit;
489 {$IFDEF SFS_DFWAD_DEBUG}
490 if wadoptDebug then e_WriteLog(Format('TWADFile.ReadFile: FOUND [%s]', [rfn]), TMsgType.Notify);
491 {$ENDIF}
492 // cache this wad
493 try
495 begin
497 end
498 else
499 begin
502 except
503 exit;
508 {$IFDEF SFS_DFWAD_DEBUG}
509 if wadoptDebug then e_WriteLog(Format('TWADFile.ReadFile: [%s] opened', [fFileName]), TMsgType.Notify);
510 {$ENDIF}
515 var
519 var
522 //f: Integer;
523 //fi: TSFSFileInfo;
524 begin
528 begin
530 Exit;
535 {$IFDEF SFS_DFWAD_DEBUG}
537 {$ENDIF}
539 try
542 begin
544 Exit;
546 except
548 Exit;
555 {$IFDEF SFS_DFWAD_DEBUG}
556 if wadoptDebug then e_WriteLog(Format('TWADFile.ReadMemory: [%s] opened', [fFileName]), TMsgType.Notify);
557 {$ENDIF}
559 {
560 for f := 0 to fIter.Count-1 do
561 begin
562 fi := fIter.Files[f];
563 if fi = nil then continue;
564 st := fIter.volume.OpenFileByIndex(f);
565 if st = nil then
566 begin
567 e_WriteLog(Format('[%s]: [%s : %s] CAN''T OPEN', [fFileName, fi.path, fi.name]), MSG_NOTIFY);
568 end
569 else
570 begin
571 e_WriteLog(Format('[%s]: [%s : %s] %u', [fFileName, fi.path, fi.name, st.size]), MSG_NOTIFY);
572 st.Free;
573 end;
574 end;
575 //fIter.volume.OpenFileByIndex(0);
576 }
582 begin