Skip to content

Commit

Permalink
Fix crash on empty map (CnCNet#456)
Browse files Browse the repository at this point in the history
* Fix crash on empty map

* Apply code style changes

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

---------

Co-authored-by: Kerbiter <[email protected]>
  • Loading branch information
SadPencil and Metadorius committed Oct 4, 2024
1 parent 44f72f5 commit 3c56b69
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 34 deletions.
12 changes: 7 additions & 5 deletions DXMainClient/DXGUI/Multiplayer/CnCNet/CnCNetLobby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,13 @@ private bool HostedGameMatches(GenericHostedGame hg)

string textUpper = tbGameSearch?.Text?.ToUpperInvariant();

string translatedGameMode = hg.GameMode.L10N($"INI:GameModes:{hg.GameMode}:UIName", notify: false);
string translatedGameMode = string.IsNullOrEmpty(hg.GameMode)
? "Unknown".L10N("Client:Main:Unknown")
: hg.GameMode.L10N($"INI:GameModes:{hg.GameMode}:UIName", notify: false);

string translatedMapName = mapLoader.TranslatedMapNames.ContainsKey(hg.Map)
? mapLoader.TranslatedMapNames[hg.Map]
: null;
string translatedMapName = string.IsNullOrEmpty(hg.Map)
? "Unknown".L10N("Client:Main:Unknown") : mapLoader.TranslatedMapNames.ContainsKey(hg.Map)
? mapLoader.TranslatedMapNames[hg.Map] : null;

return
string.IsNullOrWhiteSpace(tbGameSearch?.Text) ||
Expand Down Expand Up @@ -1463,7 +1465,7 @@ private void GameBroadcastChannel_CTCPReceived(object sender, ChannelCTCPEventAr
return;

string msg = e.Message.Substring(5); // Cut out GAME part
string[] splitMessage = msg.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
string[] splitMessage = msg.Split(new char[] { ';' });

if (splitMessage.Length != 11)
{
Expand Down
17 changes: 9 additions & 8 deletions DXMainClient/DXGUI/Multiplayer/GameInformationPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,19 @@ public override void Initialize()

public void SetInfo(GenericHostedGame game)
{
string gameModeName = game.GameMode.L10N($"INI:GameModes:{game.GameMode}:UIName", notify: false);
// we don't have the ID of a map here
string translatedMapName = string.IsNullOrEmpty(game.Map)
? "Unknown".L10N("Client:Main:Unknown") : mapLoader.TranslatedMapNames.ContainsKey(game.Map)
? mapLoader.TranslatedMapNames[game.Map] : game.Map;

string translatedGameModeName = string.IsNullOrEmpty(game.GameMode)
? "Unknown".L10N("Client:Main:Unknown") : game.GameMode.L10N($"INI:GameModes:{game.GameMode}:UIName", notify: false);

lblGameMode.Text = Renderer.GetStringWithLimitedWidth("Game mode:".L10N("Client:Main:GameInfoGameMode") + " " + Renderer.GetSafeString(gameModeName, lblGameMode.FontIndex),
lblGameMode.Text = Renderer.GetStringWithLimitedWidth("Game mode:".L10N("Client:Main:GameInfoGameMode") + " " + Renderer.GetSafeString(translatedGameModeName, lblGameMode.FontIndex),
lblGameMode.FontIndex, Width - lblGameMode.X * 2);
lblGameMode.Visible = true;

// we don't have the ID of a map here
string mapName = mapLoader.TranslatedMapNames.ContainsKey(game.Map)
? mapLoader.TranslatedMapNames[game.Map]
: game.Map;

lblMap.Text = Renderer.GetStringWithLimitedWidth("Map:".L10N("Client:Main:GameInfoMap") + " " + Renderer.GetSafeString(mapName, lblMap.FontIndex),
lblMap.Text = Renderer.GetStringWithLimitedWidth("Map:".L10N("Client:Main:GameInfoMap") + " " + Renderer.GetSafeString(translatedMapName, lblMap.FontIndex),
lblMap.FontIndex, Width - lblMap.X * 2);
lblMap.Visible = true;

Expand Down
26 changes: 13 additions & 13 deletions DXMainClient/DXGUI/Multiplayer/GameLobby/CnCNetGameLobby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -996,15 +996,15 @@ protected override void OnGameOptionChanged()
foreach (GameLobbyDropDown dd in DropDowns)
sb.Append(dd.SelectedIndex);

sb.Append(Convert.ToInt32(Map.Official));
sb.Append(Map.SHA1);
sb.Append(GameMode.Name);
sb.Append(Convert.ToInt32(Map?.Official ?? false));
sb.Append(Map?.SHA1 ?? string.Empty);
sb.Append(GameMode?.Name ?? string.Empty);
sb.Append(FrameSendRate);
sb.Append(MaxAhead);
sb.Append(ProtocolVersion);
sb.Append(RandomSeed);
sb.Append(Convert.ToInt32(RemoveStartingLocations));
sb.Append(Map.UntranslatedName);
sb.Append(Map?.UntranslatedName ?? string.Empty);

channel.SendCTCPMessage(sb.ToString(), QueuedMessageType.GAME_SETTINGS_MESSAGE, 11);
}
Expand Down Expand Up @@ -1070,10 +1070,13 @@ private void ApplyGameOptions(string sender, string message)
{
ChangeMap(null);

if (!isMapOfficial)
RequestMap(mapSHA1);
else
ShowOfficialMapMissingMessage(mapSHA1);
if (!string.IsNullOrEmpty(mapSHA1))
{
if (!isMapOfficial)
RequestMap(mapSHA1);
else
ShowOfficialMapMissingMessage(mapSHA1);
}
}
else if (GameModeMap != currentGameModeMap)
{
Expand Down Expand Up @@ -1890,9 +1893,6 @@ private void BroadcastGame()
if (ProgramConstants.IsInGame && broadcastChannel.Users.Count > 500)
return;

if (GameMode == null || Map == null)
return;

StringBuilder sb = new StringBuilder("GAME ");
sb.Append(ProgramConstants.CNCNET_PROTOCOL_REVISION);
sb.Append(";");
Expand Down Expand Up @@ -1921,9 +1921,9 @@ private void BroadcastGame()

sb.Remove(sb.Length - 1, 1);
sb.Append(";");
sb.Append(Map.UntranslatedName);
sb.Append(Map?.UntranslatedName ?? string.Empty);
sb.Append(";");
sb.Append(GameMode.UntranslatedUIName);
sb.Append(GameMode?.UntranslatedUIName ?? string.Empty);
sb.Append(";");
sb.Append(tunnelHandler.CurrentTunnel.Address + ":" + tunnelHandler.CurrentTunnel.Port);
sb.Append(";");
Expand Down
2 changes: 2 additions & 0 deletions DXMainClient/DXGUI/Multiplayer/GameLobby/GameLobbyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2081,6 +2081,8 @@ protected virtual void ChangeMap(GameModeMap gameModeMap)

MapPreviewBox.GameModeMap = null;

OnGameOptionChanged();

return;
}

Expand Down
14 changes: 8 additions & 6 deletions DXMainClient/DXGUI/Multiplayer/GameLobby/LANGameLobby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@ protected override void OnGameOptionChanged()
}

sb.Append(RandomSeed);
sb.Append(Map.SHA1);
sb.Append(GameMode.Name);
sb.Append(Map?.SHA1 ?? string.Empty);
sb.Append(GameMode?.Name ?? string.Empty);
sb.Append(FrameSendRate);
sb.Append(Convert.ToInt32(RemoveStartingLocations));

Expand Down Expand Up @@ -743,8 +743,8 @@ private void BroadcastGame()
sb.Append(ProgramConstants.LAN_PROTOCOL_REVISION);
sb.Append(ProgramConstants.GAME_VERSION);
sb.Append(localGame);
sb.Append(Map.UntranslatedName);
sb.Append(GameMode.UntranslatedUIName);
sb.Append(Map?.UntranslatedName ?? string.Empty);
sb.Append(GameMode?.UntranslatedUIName ?? string.Empty);
sb.Append(0); // LoadedGameID
var sbPlayers = new StringBuilder();
Players.ForEach(p => sbPlayers.Append(p.Name + ","));
Expand Down Expand Up @@ -991,9 +991,11 @@ private void HandleGameOptionsMessage(string data)

if (gameModeMap == null)
{
AddNotice("The game host has selected a map that doesn't exist on your installation.".L10N("Client:Main:MapNotExist") +
"The host needs to change the map or you won't be able to play.".L10N("Client:Main:HostNeedChangeMapForYou"));
ChangeMap(null);
if (!string.IsNullOrEmpty(mapSHA1))
AddNotice("The game host has selected a map that doesn't exist on your installation.".L10N("Client:Main:MapNotExist") + " " +
"The host needs to change the map or you won't be able to play.".L10N("Client:Main:HostNeedChangeMapForYou"));

return;
}

Expand Down
3 changes: 1 addition & 2 deletions DXMainClient/DXGUI/Multiplayer/LANLobby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,7 @@ private void HandleNetworkMessage(string data, IPEndPoint endPoint)
string command = commandAndParams[0];

string[] parameters = data.Substring(command.Length + 1).Split(
new char[] { ProgramConstants.LAN_DATA_SEPARATOR },
StringSplitOptions.RemoveEmptyEntries);
new char[] { ProgramConstants.LAN_DATA_SEPARATOR });

LANLobbyUser user = players.Find(p => p.EndPoint.Equals(endPoint));

Expand Down

0 comments on commit 3c56b69

Please sign in to comment.