X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fshared%2FWADEDITOR.pas;h=4434e45e7adde1ae8fc8501fae4a526763d44674;hb=97f895c7c328756a955090820189b86724968edc;hp=79f14b6364ab88ea17d7049f6feb39caa7ec3fa2;hpb=0bca3d7f2bd790a39f86ebe80f340a48af5f132b;p=d2df-sdl.git diff --git a/src/shared/WADEDITOR.pas b/src/shared/WADEDITOR.pas index 79f14b6..4434e45 100644 --- a/src/shared/WADEDITOR.pas +++ b/src/shared/WADEDITOR.pas @@ -1,6 +1,6 @@ unit WADEDITOR; -{.$DEFINE SFS_DWFAD_DEBUG} +{$DEFINE SFS_DWFAD_DEBUG} interface @@ -49,11 +49,49 @@ const procedure g_ProcessResourceStr (ResourceStr: String; var FileName, SectionName, ResourceName: String); overload; procedure g_ProcessResourceStr (ResourceStr: String; FileName, SectionName, ResourceName: PString); overload; +// return fixed string or empty string +function findDiskWad (fname: string): string; + implementation uses - SysUtils, Classes, BinEditor, e_log; + SysUtils, Classes, BinEditor, e_log, g_options; + + +function findDiskWad (fname: string): string; +var + path, rfn: string; +begin + result := ''; + path := ExtractFilePath(fname); + rfn := ExtractFileName(fname); + if not sfsFindFileCI(path, rfn) then + begin + //e_WriteLog(Format('TWADEditor_1.ReadFile: error looking for [%s] [%s]', [path, ExtractFileName(fname)]), MSG_NOTIFY); + if SFSStrEqu(ExtractFileExt(fname), '.wad') then + begin + rfn := ChangeFileExt(ExtractFileName(fname), '.pk3'); + //e_WriteLog(Format(' looking for [%s] [%s]', [path, rfn]), MSG_NOTIFY); + if not sfsFindFileCI(path, rfn) then + begin + //e_WriteLog(Format(' looking for [%s] [%s]', [path, rfn]), MSG_NOTIFY); + rfn := ChangeFileExt(ExtractFileName(fname), '.zip'); + if not sfsFindFileCI(path, rfn) then exit; + end; + end + else + begin + exit; + end; + //e_WriteLog(Format('TWADEditor_1.ReadFile: FOUND [%s]', [rfn]), MSG_NOTIFY); + end + else + begin + //if rfn <> ExtractFileName(FileName) then e_WriteLog(Format('TWADEditor_1.ReadFile: FOUND [%s]', [rfn]), MSG_NOTIFY); + end; + result := path+rfn; +end; procedure g_ProcessResourceStr (ResourceStr: String; var FileName, SectionName, ResourceName: String); @@ -138,22 +176,39 @@ begin end; +function removeExt (s: string): string; +var + i: Integer; +begin + i := length(s)+1; + while (i > 1) and (s[i-1] <> '.') and (s[i-1] <> '/') do Dec(i); + if (i > 1) and (s[i-1] = '.') then + begin + //writeln('[', s, '] -> [', Copy(s, 1, i-2), ']'); + s := Copy(s, 1, i-2); + end; + result := s; +end; + function TWADEditor_1.GetResource (Section, Resource: string; var pData: Pointer; var Len: Integer): Boolean; var f: Integer; fi: TSFSFileInfo; fs: TStream; + fpp: Pointer; //fn: string; begin Result := False; if not isOpen or (fIter = nil) then Exit; + if length(Resource) = 0 then Exit; // just in case if (length(Section) <> 0) and (Section[length(Section)] <> '/') then Section := Section+'/'; - for f := 0 to fIter.Count-1 do + // backwards, due to possible similar names and such + for f := fIter.Count-1 downto 0 do begin fi := fIter.Files[f]; if fi = nil then continue; - //e_WriteLog(Format('DFWAD: searching for [%s : %s] in [%s]; current is [%s : %s] (%d, %d)', [Section, Resource, fFileName, fi.path, fi.name, SFSStrEqu(fi.path, Section), SFSStrEqu(fi.name, Resource)]), MSG_NOTIFY); - if SFSStrEqu(fi.path, Section) and SFSStrEqu(fi.name, Resource) then + //e_WriteLog(Format('DFWAD: searching for [%s : %s] in [%s]; current is [%s : %s]', [Section, Resource, fFileName, fi.path, fi.name]), MSG_NOTIFY); + if {SFSStrEqu}SFSDFPathEqu(fi.path, Section) and SFSStrEqu(removeExt(fi.name), Resource) then begin // i found her! //fn := fFileName+'::'+fi.path+fi.name; @@ -170,10 +225,22 @@ begin end; Len := Integer(fs.size); GetMem(pData, Len); - fs.ReadBuffer(pData^, Len); - fs.Free; + fpp := pData; + try + fs.ReadBuffer(pData^, Len); + fpp := nil; + finally + if fpp <> nil then + begin + FreeMem(fpp); + pData := nil; + Len := 0; + end; + fs.Free; + end; result := true; {$IFDEF SFS_DWFAD_DEBUG} + if gSFSDebug then e_WriteLog(Format('DFWAD: file [%s%s] FOUND in [%s]; size is %d bytes', [Section, Resource, fFileName, Len]), MSG_NOTIFY); {$ENDIF} exit; @@ -195,10 +262,11 @@ begin begin fi := fIter.Files[f]; if fi = nil then continue; - if SFSStrEqu(fi.path, Section) then + if length(fi.name) = 0 then continue; + if {SFSStrEqu}SFSDFPathEqu(fi.path, Section) then begin SetLength(result, Length(result)+1); - result[high(result)] := fi.name; + result[high(result)] := removeExt(fi.name); end; end; end; @@ -207,36 +275,39 @@ end; function TWADEditor_1.ReadFile (FileName: string): Boolean; var rfn: string; + //f: Integer; + //fi: TSFSFileInfo; begin Result := False; //e_WriteLog(Format('TWADEditor_1.ReadFile: [%s]', [FileName]), MSG_NOTIFY); FreeWAD(); - rfn := FileName; - if not FileExists(rfn) then + rfn := findDiskWad(FileName); + if length(rfn) = 0 then begin - //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); - if (length(rfn) >= 4) and SFSStrEqu(Copy(rfn, length(rfn)-3, 4), '.wad') then - begin - rfn := Copy(rfn, 1, length(rfn)-4); - if FileExists(rfn+'.pk3') then rfn := rfn+'.pk3' - else if FileExists(rfn+'.zip') then rfn := rfn+'.zip' - else rfn := FileName; - {.$IFDEF SFS_DWFAD_DEBUG} - if FileExists(rfn) then e_WriteLog(Format('TWADEditor_1.ReadFile: FOUND [%s]', [rfn]), MSG_NOTIFY); - {.$ENDIF} - end; + e_WriteLog(Format('TWADEditor_1.ReadFile: error looking for [%s]', [FileName]), MSG_NOTIFY); + exit; end; - if not FileExists(rfn) then exit; {$IFDEF SFS_DWFAD_DEBUG} - e_WriteLog(Format('TWADEditor_1.ReadFile: FOUND [%s]', [rfn]), MSG_NOTIFY); + if gSFSDebug then e_WriteLog(Format('TWADEditor_1.ReadFile: FOUND [%s]', [rfn]), MSG_NOTIFY); {$ENDIF} // cache this wad - SFSAddDataFile(rfn); + try + if gSFSFastMode then + begin + if not SFSAddDataFile(rfn, true) then exit; + end + else + begin + if not SFSAddDataFileTemp(rfn, true) then exit; + end; + except + exit; + end; fIter := SFSFileList(rfn); if fIter = nil then Exit; fFileName := rfn; {$IFDEF SFS_DWFAD_DEBUG} - e_WriteLog(Format('TWADEditor_1.ReadFile: [%s] opened', [fFileName]), MSG_NOTIFY); + if gSFSDebug then e_WriteLog(Format('TWADEditor_1.ReadFile: [%s] opened', [fFileName]), MSG_NOTIFY); {$ENDIF} Result := True; end; @@ -247,7 +318,6 @@ var function TWADEditor_1.ReadMemory (Data: Pointer; Len: LongWord): Boolean; var - Signature: array[0..4] of Char; fn: string; st: TStream = nil; //f: Integer; @@ -269,7 +339,7 @@ begin try st := TSFSMemoryStreamRO.Create(Data, Len); - if not SFSAddSubDataFile(fn, st) then + if not SFSAddSubDataFile(fn, st, true) then begin st.Free; Exit;