DEADSOFTWARE

fixed small memory leak in archive reader
[d2df-sdl.git] / src / shared / WADEDITOR.pas
index 0c3357b58c6cf7f37df37ca984c69ddc402f4da7..0a80f51d4d43091f3be64900719c3180a498e588 100644 (file)
@@ -138,23 +138,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}SFSDFPathEqu(fi.path, Section) and SFSStrEqu(fi.name, Resource) then
+    if {SFSStrEqu}SFSDFPathEqu(fi.path, Section) and SFSStrEqu(removeExt(fi.name), Resource) then
     begin
       // i found her!
       //fn := fFileName+'::'+fi.path+fi.name;
@@ -171,8 +187,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
@@ -201,7 +228,7 @@ begin
     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;
@@ -224,7 +251,7 @@ begin
     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 if FileExists(rfn+'.zip') then rfn := rfn+'.zip'
       else rfn := FileName;
       {.$IFDEF SFS_DWFAD_DEBUG}
       if gSFSDebug then