Skip to content

Commit

Permalink
Feature/add map name to custom maps (#243)
Browse files Browse the repository at this point in the history
* add map name to download

* rename downloaded map with the map name

* remove .idea intellij file

* add helper function to create map file name

* use helper function

* Update DXMainClient/DXGUI/Multiplayer/GameLobby/CnCNetGameLobby.cs

Co-authored-by: Kerbiter <[email protected]>

* Update DXMainClient/Domain/Multiplayer/CnCNet/MapSharer.cs

Co-authored-by: Kerbiter <[email protected]>

Co-authored-by: Kerbiter <[email protected]>
  • Loading branch information
alexp8 and Metadorius authored Jul 12, 2021
1 parent 660bf3a commit ee1c3c6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.project
.idea/

# Visual Studion tiedostoja
*/[Ee]xported[Oo]bj/
Expand Down
18 changes: 14 additions & 4 deletions DXMainClient/DXGUI/Multiplayer/GameLobby/CnCNetGameLobby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ public CnCNetGameLobby(WindowManager windowManager, string iniName,
/// </summary>
private string lastMapSHA1;

/// <summary>
/// The map name of the latest selected map.
/// Used for map sharing.
/// </summary>
private string lastMapName;

/// <summary>
/// The game mode of the latest selected map.
/// Used for map sharing.
Expand Down Expand Up @@ -920,6 +926,7 @@ protected override void OnGameOptionChanged()
sb.Append(ProtocolVersion);
sb.Append(RandomSeed);
sb.Append(Convert.ToInt32(RemoveStartingLocations));
sb.Append(Map.Name);

channel.SendCTCPMessage(sb.ToString(), QueuedMessageType.GAME_SETTINGS_MESSAGE, 11);
}
Expand Down Expand Up @@ -973,11 +980,13 @@ private void ApplyGameOptions(string sender, string message)
AddNotice("The game host has changed ProtocolVersion to " + protocolVersion);
}

string mapName = parts[partIndex + 8];
GameMode currentGameMode = GameMode;
Map currentMap = Map;

lastGameMode = gameMode;
lastMapSHA1 = mapSHA1;
lastMapName = mapName;

GameMode = GameModes.Find(gm => gm.Name == gameMode);
if (GameMode == null)
Expand Down Expand Up @@ -1134,7 +1143,7 @@ private void MapSharingConfirmationPanel_MapDownloadConfirmed(object sender, Eve
Logger.Log("Map sharing confirmed.");
AddNotice("Attempting to download map.");
mapSharingConfirmationPanel.SetDownloadingStatus();
MapSharer.DownloadMap(lastMapSHA1, localGame);
MapSharer.DownloadMap(lastMapSHA1, localGame, lastMapName);
}

protected override void ChangeMap(GameMode gameMode, Map map)
Expand Down Expand Up @@ -1534,8 +1543,9 @@ private void MapSharer_MapDownloadComplete(object sender, SHA1EventArgs e) =>

private void MapSharer_HandleMapDownloadComplete(SHA1EventArgs e)
{
Logger.Log("Map " + e.SHA1 + " downloaded, parsing.");
string mapPath = "Maps/Custom/" + e.SHA1;
string mapFileName = MapSharer.GetMapFileName(e.SHA1, e.MapName);
Logger.Log("Map " + mapFileName + " downloaded, parsing.");
string mapPath = "Maps/Custom/" + mapFileName;
Map map = MapLoader.LoadCustomMap(mapPath, out string returnMessage);
if (map != null)
{
Expand Down Expand Up @@ -1681,7 +1691,7 @@ private void HandleMapDownloadRequest(string sender, string sha1)
if (lastMapSHA1 == sha1 && Map == null)
{
Logger.Log("The game host has uploaded the map into the database. Re-attempting download...");
MapSharer.DownloadMap(sha1, localGame);
MapSharer.DownloadMap(sha1, localGame, lastMapName);
}
}

Expand Down
31 changes: 21 additions & 10 deletions DXMainClient/Domain/Multiplayer/CnCNet/MapSharer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ private static string ExtractZipFile(string zipFile, string destDir)
return null;
}

public static void DownloadMap(string sha1, string myGame)
public static void DownloadMap(string sha1, string myGame, string mapName)
{
lock (locker)
{
Expand All @@ -311,9 +311,10 @@ public static void DownloadMap(string sha1, string myGame)

if (MapDownloadQueue.Count == 1)
{
object[] details = new object[2];
object[] details = new object[3];
details[0] = sha1;
details[1] = myGame.ToLower();
details[2] = mapName;

ParameterizedThreadStart pts = new ParameterizedThreadStart(Download);
Thread thread = new Thread(pts);
Expand All @@ -327,34 +328,35 @@ private static void Download(object details)
object[] sha1AndGame = (object[])details;
string sha1 = (string)sha1AndGame[0];
string myGameId = (string)sha1AndGame[1];
string mapName = (string)sha1AndGame[2];

Logger.Log("MapSharer: Preparing to download map " + sha1);
Logger.Log("MapSharer: Preparing to download map " + sha1 + " with name: " + mapName);

bool success;

try
{
Logger.Log("MapSharer: MapDownloadStarted");
MapDownloadStarted?.Invoke(null, new SHA1EventArgs(sha1));
MapDownloadStarted?.Invoke(null, new SHA1EventArgs(sha1, mapName));
}
catch (Exception ex)
{
Logger.Log("MapSharer: ERROR " + ex.Message);
}

string mapPath = DownloadMain(sha1, myGameId, out success);
string mapPath = DownloadMain(sha1, myGameId, mapName, out success);

lock (locker)
{
if (success)
{
Logger.Log("MapSharer: Download of map " + sha1 + " completed succesfully.");
MapDownloadComplete?.Invoke(null, new SHA1EventArgs(sha1));
MapDownloadComplete?.Invoke(null, new SHA1EventArgs(sha1, mapName));
}
else
{
Logger.Log("MapSharer: Download of map " + sha1 + "failed! Reason: " + mapPath);
MapDownloadFailed?.Invoke(null, new SHA1EventArgs(sha1));
MapDownloadFailed?.Invoke(null, new SHA1EventArgs(sha1, mapName));
}

MapDownloadQueue.Remove(sha1);
Expand All @@ -363,20 +365,26 @@ private static void Download(object details)
{
Logger.Log("MapSharer: Continuing custom map downloads.");

object[] array = new object[2];
object[] array = new object[3];
array[0] = MapDownloadQueue[0];
array[1] = myGameId;
array[2] = mapName;

Download(array);
}
}
}

private static string DownloadMain(string sha1, string myGame, out bool success)
public static string GetMapFileName(string sha1, string mapName)
=> mapName + "_" + sha1;

private static string DownloadMain(string sha1, string myGame, string mapName, out bool success)
{
string customMapsDirectory = ProgramConstants.GamePath + "Maps/Custom/";

string destinationFilePath = customMapsDirectory + sha1 + ".zip";
string mapFileName = GetMapFileName(sha1, mapName);

string destinationFilePath = customMapsDirectory + mapFileName + ".zip";

try
{
Expand Down Expand Up @@ -420,6 +428,9 @@ private static string DownloadMain(string sha1, string myGame, out bool success)

string extractedFile = ExtractZipFile(destinationFilePath, customMapsDirectory);

string newFilename = customMapsDirectory + mapFileName + ".map";
File.Move(customMapsDirectory + extractedFile, newFilename);

if (String.IsNullOrEmpty(extractedFile))
{
success = false;
Expand Down
5 changes: 4 additions & 1 deletion DXMainClient/Domain/Multiplayer/CnCNet/SHA1EventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ namespace DTAClient.Domain.Multiplayer.CnCNet
{
public class SHA1EventArgs : EventArgs
{
public SHA1EventArgs(string sha1)
public SHA1EventArgs(string sha1, string mapName)
{
SHA1 = sha1;
MapName = mapName;
}

public string SHA1 { get; private set; }

public string MapName { get; private set; }
}
}

0 comments on commit ee1c3c6

Please sign in to comment.