8da2b385e77822c8b8254564f25dd420f993c973
3 {.$DEFINE SFS_DWFAD_DEBUG}
12 SArray
= array of ShortString;
14 TWADEditor_1
= class(TObject
)
16 fFileName
: string; // empty: not opened
19 function getIsOpen (): Boolean;
23 destructor Destroy(); override;
27 function ReadFile (FileName
: string): Boolean;
28 function ReadMemory (Data
: Pointer; Len
: LongWord): Boolean;
29 function GetResource (Section
, Resource
: string; var pData
: Pointer; var Len
: Integer): Boolean;
30 function GetResourcesList (Section
: string): SArray
;
32 property isOpen
: Boolean read getIsOpen
;
38 DFWAD_ERROR_WADNOTFOUND = -1;
39 DFWAD_ERROR_CANTOPENWAD = -2;
40 DFWAD_ERROR_RESOURCENOTFOUND = -3;
41 DFWAD_ERROR_FILENOTWAD = -4;
42 DFWAD_ERROR_WADNOTLOADED = -5;
43 DFWAD_ERROR_READRESOURCE = -6;
44 DFWAD_ERROR_READWAD = -7;
45 DFWAD_ERROR_WRONGVERSION = -8;
49 procedure g_ProcessResourceStr (ResourceStr
: String; var FileName
, SectionName
, ResourceName
: String); overload
;
50 procedure g_ProcessResourceStr (ResourceStr
: String; FileName
, SectionName
, ResourceName
: PString); overload
;
56 SysUtils
, Classes
, BinEditor
, e_log
;
59 procedure g_ProcessResourceStr (ResourceStr
: String; var FileName
, SectionName
, ResourceName
: String);
64 //e_WriteLog(Format('g_ProcessResourceStr0: [%s]', [ResourceStr]), MSG_NOTIFY);
65 for i
:= Length(ResourceStr
) downto 1 do
66 if ResourceStr
[i
] = ':' then
69 FileName
:= Copy(ResourceStr
, 1, i
-1);
71 for a
:= i
+1 to Length(ResourceStr
) do
72 if (ResourceStr
[a
] = '\') or (ResourceStr
[a
] = '/') then Break
;
74 ResourceName
:= Copy(ResourceStr
, a
+1, Length(ResourceStr
)-Abs(a
));
75 SectionName
:= Copy(ResourceStr
, i
+1, Length(ResourceStr
)-Length(ResourceName
)-Length(FileName
)-2);
79 procedure g_ProcessResourceStr (ResourceStr
: AnsiString; FileName
, SectionName
, ResourceName
: PAnsiString);
81 a
, i
, l1
, l2
: Integer;
84 //e_WriteLog(Format('g_ProcessResourceStr1: [%s]', [ResourceStr]), MSG_NOTIFY);
85 for i
:= Length(ResourceStr
) downto 1 do
86 if ResourceStr
[i
] = ':' then
89 if FileName
<> nil then
91 FileName
^ := Copy(ResourceStr
, 1, i
-1);
92 l1
:= Length(FileName
^);
97 for a
:= i
+1 to Length(ResourceStr
) do
98 if (ResourceStr
[a
] = '\') or (ResourceStr
[a
] = '/') then Break
;
100 if ResourceName
<> nil then
102 ResourceName
^ := Copy(ResourceStr
, a
+1, Length(ResourceStr
)-Abs(a
));
103 l2
:= Length(ResourceName
^);
108 if SectionName
<> nil then
109 SectionName
^ := Copy(ResourceStr
, i
+1, Length(ResourceStr
)-l2
-l1
-2);
114 constructor TWADEditor_1
.Create();
120 destructor TWADEditor_1
.Destroy();
127 function TWADEditor_1
.getIsOpen (): Boolean;
129 result
:= (fFileName
<> '');
133 procedure TWADEditor_1
.FreeWAD();
135 if fIter
<> nil then FreeAndNil(fIter
);
136 //if fFileName <> '' then e_WriteLog(Format('TWADEditor_1.ReadFile: [%s] closed', [fFileName]), MSG_NOTIFY);
141 function TWADEditor_1
.GetResource (Section
, Resource
: string; var pData
: Pointer; var Len
: Integer): Boolean;
149 if not isOpen
or (fIter
= nil) then Exit
;
150 if (length(Section
) <> 0) and (Section
[length(Section
)] <> '/') then Section
:= Section
+'/';
151 for f
:= 0 to fIter
.Count
-1 do
153 fi
:= fIter
.Files
[f
];
154 if fi
= nil then continue
;
155 //e_WriteLog(Format('DFWAD: searching for [%s : %s] in [%s]; current is [%s : %s] (%d, %d)', [Section, Resource, fFileName, fi.path, fi.name, SFSStrComp(fi.path, Section), SFSStrComp(fi.name, Resource)]), MSG_NOTIFY);
156 if (SFSStrComp(fi
.path
, Section
) = 0) and (SFSStrComp(fi
.name
, Resource
) = 0) then
159 //fn := fFileName+'::'+fi.path+fi.name;
160 //fs := SFSFileOpen(fn);
162 fs
:= fIter
.volume
.OpenFileByIndex(f
);
168 e_WriteLog(Format('DFWAD: can''t open file [%s%s] in [%s]', [Section
, Resource
, fFileName
]), MSG_WARNING
);
171 Len
:= Integer(fs
.size
);
173 fs
.ReadBuffer(pData
^, Len
);
176 {$IFDEF SFS_DWFAD_DEBUG}
177 e_WriteLog(Format('DFWAD: file [%s%s] FOUND in [%s]; size is %d bytes', [Section
, Resource
, fFileName
, Len
]), MSG_NOTIFY
);
182 e_WriteLog(Format('DFWAD: file [%s%s] not found in [%s]', [Section
, Resource
, fFileName
]), MSG_WARNING
);
186 function TWADEditor_1
.GetResourcesList (Section
: string): SArray
;
192 if not isOpen
or (fIter
= nil) then Exit
;
193 if (length(Section
) <> 0) and (Section
[length(Section
)] <> '/') then Section
:= Section
+'/';
194 for f
:= 0 to fIter
.Count
-1 do
196 fi
:= fIter
.Files
[f
];
197 if fi
= nil then continue
;
198 if SFSStrComp(fi
.path
, Section
) = 0 then
200 SetLength(result
, Length(result
)+1);
201 result
[high(result
)] := fi
.name
;
207 function TWADEditor_1
.ReadFile (FileName
: string): Boolean;
212 //e_WriteLog(Format('TWADEditor_1.ReadFile: [%s]', [FileName]), MSG_NOTIFY);
215 if not FileExists(rfn
) then
217 //if length(rfn) >= 4 then e_WriteLog(Format('XXXX TWADEditor_1.ReadFile: [%s] [%s]', [Copy(rfn, length(rfn)-3, 4), Copy(rfn, 1, length(rfn)-4)]), MSG_NOTIFY);
218 if (length(rfn
) >= 4) and (SFSStrComp(Copy(rfn
, length(rfn
)-3, 4), '.wad') = 0) then
220 rfn
:= Copy(rfn
, 1, length(rfn
)-4);
221 if FileExists(rfn
+'.pk3') then rfn
:= rfn
+'.pk3'
222 else if FileExists(rfn
+'.zip') then rfn
:= rfn
+'.zip'
223 else rfn
:= FileName
;
224 {.$IFDEF SFS_DWFAD_DEBUG}
225 if FileExists(rfn
) then e_WriteLog(Format('TWADEditor_1.ReadFile: FOUND [%s]', [rfn
]), MSG_NOTIFY
);
229 if not FileExists(rfn
) then exit
;
230 {$IFDEF SFS_DWFAD_DEBUG}
231 e_WriteLog(Format('TWADEditor_1.ReadFile: FOUND [%s]', [rfn
]), MSG_NOTIFY
);
235 fIter
:= SFSFileList(rfn
);
236 if fIter
= nil then Exit
;
238 {$IFDEF SFS_DWFAD_DEBUG}
239 e_WriteLog(Format('TWADEditor_1.ReadFile: [%s] opened', [fFileName
]), MSG_NOTIFY
);
246 uniqueCounter
: Integer = 0;
248 function TWADEditor_1
.ReadMemory (Data
: Pointer; Len
: LongWord): Boolean;
250 Signature
: array[0..4] of Char;
258 if (Data
= nil) or (Len
= 0) then
260 e_WriteLog('TWADEditor_1.ReadMemory: EMPTY SUBWAD!', MSG_WARNING
);
264 fn
:= Format(' -- memwad %d -- ', [uniqueCounter
]);
266 {$IFDEF SFS_DWFAD_DEBUG}
267 e_WriteLog(Format('TWADEditor_1.ReadMemory: [%s]', [fn
]), MSG_NOTIFY
);
271 st
:= TSFSMemoryStreamRO
.Create(Data
, Len
);
272 if not SFSAddSubDataFile(fn
, st
) then
282 fIter
:= SFSFileList(fn
);
283 if fIter
= nil then Exit
;
286 {$IFDEF SFS_DWFAD_DEBUG}
287 e_WriteLog(Format('TWADEditor_1.ReadMemory: [%s] opened', [fFileName
]), MSG_NOTIFY
);
291 for f := 0 to fIter.Count-1 do
293 fi := fIter.Files[f];
294 if fi = nil then continue;
295 st := fIter.volume.OpenFileByIndex(f);
298 e_WriteLog(Format('[%s]: [%s : %s] CAN''T OPEN', [fFileName, fi.path, fi.name]), MSG_NOTIFY);
302 e_WriteLog(Format('[%s]: [%s : %s] %u', [fFileName, fi.path, fi.name, st.size]), MSG_NOTIFY);
306 //fIter.volume.OpenFileByIndex(0);
314 sfsDiskDirs
:= '<exedir>/data'; //FIXME