Skip to content

Commit b523b0d

Browse files
authored
Fixes for sharing/downloading maps (#865)
* Fix incorrect directory for downloaded maps Fix non-hosts being able to download maps Add better error handling * Add try catch to delete calls
1 parent 7e0cb41 commit b523b0d

File tree

2 files changed

+84
-42
lines changed

2 files changed

+84
-42
lines changed

DXMainClient/DXGUI/Multiplayer/GameLobby/CnCNetGameLobby.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Random random
106106
true, (s) => ShowTunnelSelectionWindow("Select tunnel server:".L10N("Client:Main:SelectTunnelServerCommand"))));
107107
AddChatBoxCommand(new ChatBoxCommand("DOWNLOADMAP",
108108
"Download a map from CNCNet's map server using a map ID and an optional filename.\nExample: \"/downloadmap MAPID [2] My Battle Map\"".L10N("Client:Main:DownloadMapCommandDescription"),
109-
false, DownloadMapByIdCommand));
109+
true, DownloadMapByIdCommand));
110110
}
111111

112112
public event EventHandler GameLeft;

DXMainClient/Domain/Multiplayer/CnCNet/MapSharer.cs

Lines changed: 83 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -374,71 +374,113 @@ public static string GetMapFileName(string sha1, string mapName)
374374

375375
private static string DownloadMain(string sha1, string myGame, string mapName, out bool success)
376376
{
377-
string customMapsDirectory = SafePath.CombineDirectoryPath(ProgramConstants.GamePath, "Maps", "Custom");
378-
string mapFileName = GetMapFileName(sha1, mapName);
377+
try
378+
{
379+
string customMapsDirectory = SafePath.CombineDirectoryPath(ProgramConstants.GamePath, "Maps", "Custom");
380+
string mapFileName = GetMapFileName(sha1, mapName);
379381

380-
FileInfo destinationFile = SafePath.GetFile(customMapsDirectory, FormattableString.Invariant($"{mapFileName}.zip"));
382+
FileInfo destinationFile = SafePath.GetFile(customMapsDirectory, FormattableString.Invariant($"{mapFileName}.zip"));
381383

382-
// This string is up here so we can check that there isn't already a .map file for this download.
383-
// This prevents the client from crashing when trying to rename the unzipped file to a duplicate filename.
384-
FileInfo newFile = SafePath.GetFile(customMapsDirectory, FormattableString.Invariant($"{mapFileName}.{ClientConfiguration.Instance.MapFileExtension}"));
384+
// This string is up here so we can check that there isn't already a .map file for this download.
385+
// This prevents the client from crashing when trying to rename the unzipped file to a duplicate filename.
386+
FileInfo newFile = SafePath.GetFile(customMapsDirectory, FormattableString.Invariant($"{mapFileName}.{ClientConfiguration.Instance.MapFileExtension}"));
385387

386-
destinationFile.Delete();
387-
newFile.Delete();
388+
try
389+
{
390+
destinationFile.Delete();
391+
}
392+
catch (Exception ex)
393+
{
394+
Logger.Log($"MapSharer: Failed to delete existing zip file: {ex.Message}");
395+
}
388396

389-
using (WebClient webClient = new ExtendedWebClient(DOWNLOAD_TIMEOUT))
390-
{
391-
if (string.IsNullOrWhiteSpace(ClientConfiguration.Instance.CnCNetMapDBDownloadURL))
397+
try
398+
{
399+
newFile.Delete();
400+
}
401+
catch (Exception ex)
402+
{
403+
Logger.Log($"MapSharer: Failed to delete existing map file: {ex.Message}");
404+
}
405+
406+
using (WebClient webClient = new ExtendedWebClient(DOWNLOAD_TIMEOUT))
407+
{
408+
if (string.IsNullOrWhiteSpace(ClientConfiguration.Instance.CnCNetMapDBDownloadURL))
409+
{
410+
success = false;
411+
Logger.Log("MapSharer: Download URL is not configured.");
412+
return null;
413+
}
414+
415+
string url = string.Format(CultureInfo.InvariantCulture, "{0}/{1}/{2}.zip", ClientConfiguration.Instance.CnCNetMapDBDownloadURL, myGame, sha1);
416+
417+
try
418+
{
419+
Logger.Log($"MapSharer: Downloading URL: {url}");
420+
webClient.DownloadFile(url, destinationFile.FullName);
421+
}
422+
catch (Exception ex)
423+
{
424+
/* if (ex.Message.Contains("404"))
425+
{
426+
string messageToSend = "NOTICE " + ChannelName + " " + CTCPChar1 + CTCPChar2 + "READY 1" + CTCPChar2;
427+
CnCNetData.ConnectionBridge.SendMessage(messageToSend);
428+
}
429+
else
430+
{
431+
//GlobalVars.WriteLogfile(ex.StackTrace.ToString(), DateTime.Now.ToString("hh:mm:ss") + " DownloadMap: " + ex.Message + _DestFile);
432+
MessageBox.Show("Download failed:" + _DestFile);
433+
}*/
434+
success = false;
435+
return ex.Message;
436+
}
437+
}
438+
439+
destinationFile.Refresh();
440+
441+
if (!destinationFile.Exists)
392442
{
393443
success = false;
394-
Logger.Log("MapSharer: Download URL is not configured.");
395444
return null;
396445
}
397446

398-
string url = string.Format(CultureInfo.InvariantCulture, "{0}/{1}/{2}.zip", ClientConfiguration.Instance.CnCNetMapDBDownloadURL, myGame, sha1);
447+
string extractedFile;
399448

400449
try
401450
{
402-
Logger.Log($"MapSharer: Downloading URL: {url}");
403-
webClient.DownloadFile(url, destinationFile.FullName);
451+
extractedFile = ExtractZipFile(destinationFile.FullName, customMapsDirectory);
404452
}
405453
catch (Exception ex)
406454
{
407-
/* if (ex.Message.Contains("404"))
408-
{
409-
string messageToSend = "NOTICE " + ChannelName + " " + CTCPChar1 + CTCPChar2 + "READY 1" + CTCPChar2;
410-
CnCNetData.ConnectionBridge.SendMessage(messageToSend);
411-
}
412-
else
413-
{
414-
//GlobalVars.WriteLogfile(ex.StackTrace.ToString(), DateTime.Now.ToString("hh:mm:ss") + " DownloadMap: " + ex.Message + _DestFile);
415-
MessageBox.Show("Download failed:" + _DestFile);
416-
}*/
455+
Logger.Log($"MapSharer: Failed to extract map: {ex.Message}");
417456
success = false;
418457
return ex.Message;
419458
}
420-
}
421-
422-
destinationFile.Refresh();
423459

424-
if (!destinationFile.Exists)
425-
{
426-
success = false;
427-
return null;
428-
}
460+
if (String.IsNullOrEmpty(extractedFile))
461+
{
462+
success = false;
463+
return null;
464+
}
429465

430-
string extractedFile = ExtractZipFile(destinationFile.FullName, newFile.FullName);
466+
try
467+
{
468+
destinationFile.Delete();
469+
}
470+
catch (Exception ex)
471+
{
472+
Logger.Log($"MapSharer: Failed to delete zip file after extraction: {ex.Message}");
473+
}
431474

432-
if (String.IsNullOrEmpty(extractedFile))
475+
success = true;
476+
return extractedFile;
477+
}
478+
catch (Exception ex)
433479
{
480+
Logger.Log($"MapSharer: Map download failed with exception: {ex.Message}");
434481
success = false;
435-
return null;
482+
return ex.Message;
436483
}
437-
438-
destinationFile.Delete();
439-
440-
success = true;
441-
return extractedFile;
442484
}
443485

444486
class FileToUpload

0 commit comments

Comments
 (0)