1 (* coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
2 * Understanding is not required. Only obedience.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 3 of the License ONLY.
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 ../shared/a_modes.inc}
17 {.$DEFINE FUI_WADREAD_DEBUG}
26 function fuiAddWad (const wadfile
: AnsiString): Boolean;
28 // returns `nil` if file wasn't found
29 function fuiOpenFile (const fname
: AnsiString): TStream
;
33 fuiDiskFirst
: Boolean = true;
42 // ////////////////////////////////////////////////////////////////////////// //
50 constructor Create (const awadname
: AnsiString);
51 destructor Destroy (); override;
53 // returns `nil` if file wasn't found
54 function openFile (const fname
: AnsiString): TStream
;
58 constructor TFUIWad
.Create (const awadname
: AnsiString);
59 {$IFDEF FUI_WADREAD_DEBUG}
64 if not SFSAddDataFile(awadname
, true) then raise Exception
.Create('cannot open wad');
66 iter
:= SFSFileList(awadname
);
67 {$IFDEF FUI_WADREAD_DEBUG}
70 writeln('==== ', awadname
, ' ====');
71 for f
:= 0 to iter
.Count
-1 do
73 if (iter
.Files
[f
] = nil) then continue
;
74 writeln(' ', f
, ': ', iter
.Files
[f
].path
, iter
.Files
[f
].name
);
82 destructor TFUIWad
.Destroy ();
90 function TFUIWad
.openFile (const fname
: AnsiString): TStream
;
96 if (iter
= nil) then exit
;
97 // backwards, due to possible similar names and such
98 for f
:= iter
.Count
-1 downto 0 do
101 if (fi
= nil) then continue
;
102 if (StrEquCI1251(fi
.path
+fi
.name
, fname
)) then
105 result
:= iter
.volume
.OpenFileByIndex(f
);
109 if (result
<> nil) then exit
;
116 // ////////////////////////////////////////////////////////////////////////// //
117 function getExeDataPath (): AnsiString;
119 result
:= getFilenamePath(ParamStr(0))+'data/';
123 // ////////////////////////////////////////////////////////////////////////// //
125 wadlist
: array of TFUIWad
= nil;
128 function fuiAddWad (const wadfile
: AnsiString): Boolean;
131 awadname
: AnsiString;
138 if (Length(wadfile
) = 0) then exit
;
140 if (Length(wadfile
) > 2) and (wadfile
[1] = '.') and ((wadfile
[2] = '/') or (wadfile
[2] = '\')) then
143 awadname
:= findDiskWad(awadname
);
144 if (Length(awadname
) = 0) then
146 writeln('WARNING: FlexUI WAD ''', wadfile
, ''' not found');
152 exepath
:= getExeDataPath();
153 awadname
:= exepath
+wadfile
;
154 awadname
:= findDiskWad(awadname
);
155 if (Length(awadname
) = 0) then
158 awadname
:= findDiskWad(awadname
);
159 if (Length(awadname
) = 0) then
161 writeln('WARNING: FlexUI WAD ''', exepath
+wadfile
, ''' not found');
167 // check if we already have this file opened
168 for f
:= 0 to High(wadlist
) do
171 if (strEquCI1251(awadname
, wad
.wadname
)) then
173 // i found her! move it to the bottom of the list, so it will be checked first
174 for c
:= f
+1 to High(wadlist
) do wadlist
[c
-1] := wadlist
[c
];
175 wadlist
[High(wadlist
)] := wad
;
180 // create new wad file
182 wad
:= TFUIWad
.Create(awadname
);
184 writeln('WARNING: error opening FlexUI WAD ''', wadfile
, '''');
188 SetLength(wadlist
, Length(wadlist
)+1);
189 wadlist
[High(wadlist
)] := wad
;
190 {$IFDEF FUI_WADREAD_DEBUG}writeln('FUI: added WAD: ''', wad
.wadname
, '''');{$ENDIF}
196 // ////////////////////////////////////////////////////////////////////////// //
197 // returns `nil` if file wasn't found
198 function tryDiskFile (const fname
: AnsiString): TStream
;
202 fn
:= getExeDataPath()+fname
;
204 result
:= openDiskFileRO(fn
);
205 {$IFDEF FUI_WADREAD_DEBUG}writeln('FUI: opened DISK file: ''', fn
, '''');{$ENDIF}
212 // returns `nil` if file wasn't found
213 function fuiOpenFile (const fname
: AnsiString): TStream
;
218 if (fuiDiskFirst
) then
220 result
:= tryDiskFile(fname
);
221 if (result
<> nil) then exit
;
224 for f
:= High(wadlist
) downto 0 do
226 result
:= wadlist
[f
].openFile(fname
);
227 if (result
<> nil) then
229 {$IFDEF FUI_WADREAD_DEBUG}writeln('FUI: opened WAD file: ''', fname
, ''' (from ''', wadlist
[f
].wadname
, ''')');{$ENDIF}
234 if (not fuiDiskFirst
) then
236 result
:= tryDiskFile(fname
);
237 if (result
<> nil) then exit
;
239 {$IFDEF FUI_WADREAD_DEBUG}writeln('FUI: file: ''', fname
, ''' NOT FOUND!');{$ENDIF}