DEADSOFTWARE

changed license to GPLv3 only; sorry, no trust to FSF anymore
[d2df-sdl.git] / src / game / g_res_downloader.pas
index 9547335009a7296361e17b018196f7e6846c3278..04048b693c983c558af1c61168f3948eee2c5823 100644 (file)
@@ -1,3 +1,18 @@
+(* Copyright (C)  Doom 2D: Forever Developers
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License ONLY.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *)
+{$INCLUDE ../shared/a_modes.inc}
 unit g_res_downloader;
 
 interface
@@ -9,7 +24,7 @@ function g_Res_DownloadWAD(const FileName: string): string;
 
 implementation
 
-uses g_language;
+uses g_language, sfs, utils, wadreader, g_game;
 
 const DOWNLOAD_DIR = 'downloads';
 
@@ -23,15 +38,14 @@ begin
       repeat
         if (searchResult.Attr and faDirectory) = 0 then
         begin
-          if searchResult.Name = filename then
+          if StrEquCI1251(searchResult.Name, filename) then
           begin
             files.Add(dirName+'/'+filename);
             Exit;
           end;
         end
         else if (searchResult.Name <> '.') and (searchResult.Name <> '..') then
-          FindFiles(IncludeTrailingPathDelimiter(dirName)+searchResult.Name,
-                    filename, files);
+          FindFiles(IncludeTrailingPathDelimiter(dirName)+searchResult.Name, filename, files);
       until FindNext(searchResult) <> 0;
     finally
       FindClose(searchResult);
@@ -42,14 +56,21 @@ end;
 function CompareFileHash(const filename: string; const resMd5: TMD5Digest): Boolean;
 var
   gResHash: TMD5Digest;
+  fname: string;
 begin
-  gResHash := MD5File(filename);
+  fname := findDiskWad(filename);
+  if length(fname) = 0 then begin result := false; exit; end;
+  gResHash := MD5File(fname);
   Result := MD5Match(gResHash, resMd5);
 end;
 
 function CheckFileHash(const path, filename: string; const resMd5: TMD5Digest): Boolean;
+var
+  fname: string;
 begin
-  Result := FileExists(path + filename) and CompareFileHash(path + filename, resMd5);
+  fname := findDiskWad(path+filename);
+  if length(fname) = 0 then begin result := false; exit; end;
+  Result := FileExists(fname) and CompareFileHash(fname, resMd5);
 end;
 
 function g_Res_SearchSameWAD(const path, filename: string; const resMd5: TMD5Digest): string;
@@ -84,15 +105,14 @@ end;
 
 function SaveWAD(const path, filename: string; const data: array of Byte): string;
 var
-  resFile: TFileStream;
+  resFile: TStream;
+  dpt: string;
 begin
   try
-    Result := path + DOWNLOAD_DIR + '/' + filename;
-    if not DirectoryExists(path + DOWNLOAD_DIR) then
-    begin
-      CreateDir(path + DOWNLOAD_DIR);
-    end;
-    resFile := TFileStream.Create(Result, fmCreate);
+    result := path+DOWNLOAD_DIR+'/'+filename;
+    dpt := path+DOWNLOAD_DIR;
+    if not findFileCI(dpt, true) then CreateDir(dpt);
+    resFile := createDiskFile(result);
     resFile.WriteBuffer(data[0], Length(data));
     resFile.Free
   except
@@ -103,14 +123,15 @@ end;
 function g_Res_DownloadWAD(const FileName: string): string;
 var
   msgStream: TMemoryStream;
-  resStream: TFileStream;
+  resStream: TStream;
   mapData: TMapDataMsg;
   i: Integer;
   resData: TResDataMsg;
 begin
   SetLength(mapData.ExternalResources, 0);
   g_Console_Add(Format(_lc[I_NET_MAP_DL], [FileName]));
-  e_WriteLog('Downloading map `' + FileName + '` from server', MSG_NOTIFY);
+  e_WriteLog('Downloading map `' + FileName + '` from server', TMsgType.Notify);
+  g_Game_SetLoadingText(FileName + '...', 0, False);
   MC_SEND_MapRequest();
 
   msgStream := g_Net_Wait_Event(NET_MSG_MAP_RESPONSE);
@@ -118,7 +139,8 @@ begin
   begin
     mapData := MapDataFromMsgStream(msgStream);
     msgStream.Free;
-  end;
+  end else
+    mapData.FileSize := 0;
 
   for i := 0 to High(mapData.ExternalResources) do
   begin
@@ -129,15 +151,17 @@ begin
       g_Console_Add(Format(_lc[I_NET_WAD_DL],
                            [mapData.ExternalResources[i].Name]));
       e_WriteLog('Downloading Wad `' + mapData.ExternalResources[i].Name +
-                 '` from server', MSG_NOTIFY);
+                 '` from server', TMsgType.Notify);
+      g_Game_SetLoadingText(mapData.ExternalResources[i].Name + '...', 0, False);
       MC_SEND_ResRequest(mapData.ExternalResources[i].Name);
 
       msgStream := g_Net_Wait_Event(NET_MSG_RES_RESPONSE);
+      if msgStream = nil then
+        continue;
+
       resData := ResDataFromMsgStream(msgStream);
 
-      resStream := TFileStream.Create(GameDir+'/wads/'+
-                                      mapData.ExternalResources[i].Name,
-                                      fmCreate);
+      resStream := createDiskFile(GameDir+'/wads/'+mapData.ExternalResources[i].Name);
       resStream.WriteBuffer(resData.FileData[0], resData.FileSize);
 
       resData.FileData := nil;
@@ -147,6 +171,8 @@ begin
   end;
 
   Result := SaveWAD(MapsDir, ExtractFileName(FileName), mapData.FileData);
+  if mapData.FileSize = 0 then
+    DeleteFile(Result);
 end;
 
 end.