DEADSOFTWARE

network: it should find correct files on POSIX now (and substitute extensions too)
[d2df-sdl.git] / src / shared / WADEDITOR.pas
index de670694b81fe44207506b355c7351b3921ea86c..d41acfaa8a83a67b96b9d2f3f1ff5ab0cbf7345d 100644 (file)
@@ -49,6 +49,9 @@ 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
 
@@ -56,6 +59,41 @@ uses
   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);
 var
   a, i: Integer;
@@ -157,6 +195,7 @@ var
   f: Integer;
   fi: TSFSFileInfo;
   fs: TStream;
+  fpp: Pointer;
   //fn: string;
 begin
   Result := False;
@@ -168,8 +207,8 @@ begin
   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}SFSDFPathEqu(fi.path, Section) and SFSStrEqu(removeExt(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(fi.path, Section) and SFSStrEqu(removeExt(fi.name), Resource) then
     begin
       // i found her!
       //fn := fFileName+'::'+fi.path+fi.name;
@@ -186,8 +225,19 @@ 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
@@ -213,7 +263,7 @@ begin
     fi := fIter.Files[f];
     if fi = nil then continue;
     if length(fi.name) = 0 then continue;
-    if {SFSStrEqu}SFSDFPathEqu(fi.path, Section) then
+    if SFSStrEqu(fi.path, Section) then
     begin
       SetLength(result, Length(result)+1);
       result[high(result)] := removeExt(fi.name);
@@ -231,44 +281,34 @@ 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 gSFSDebug then
-        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}
-  if gSFSDebug then
-    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}
-  if gSFSDebug then
-    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}
-  {
-    for f := 0 to fIter.Count-1 do
-    begin
-      fi := fIter.Files[f];
-      if fi = nil then continue;
-      e_WriteLog(Format('[%s]: [%s : %s] %u', [fFileName, fi.path, fi.name, fi.size]), MSG_NOTIFY);
-    end;
-  }
   Result := True;
 end;
 
@@ -299,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;