diff --git a/DXMainClient/DXGUI/Multiplayer/GameInformationPanel.cs b/DXMainClient/DXGUI/Multiplayer/GameInformationPanel.cs index 895e1a18c..e8be3c255 100644 --- a/DXMainClient/DXGUI/Multiplayer/GameInformationPanel.cs +++ b/DXMainClient/DXGUI/Multiplayer/GameInformationPanel.cs @@ -1,13 +1,16 @@ -using Rampastring.XNAUI.XNAControls; -using Rampastring.XNAUI; -using Microsoft.Xna.Framework; -using DTAClient.Domain.Multiplayer; +using System; +using System.Diagnostics; + +using ClientCore; using ClientCore.Extensions; + +using DTAClient.Domain.Multiplayer; + +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using Rampastring.Tools; -using System.Net.NetworkInformation; -using System; -using ClientCore; + +using Rampastring.XNAUI; +using Rampastring.XNAUI.XNAControls; namespace DTAClient.DXGUI.Multiplayer { @@ -39,7 +42,9 @@ public GameInformationPanel(WindowManager windowManager, MapLoader mapLoader) private XNALabel[] lblPlayerNames; private GenericHostedGame game = null; - private Texture2D mapTexture; + + private bool disposeTextures = false; + private Texture2D mapTexture = null; private Texture2D noMapPreviewTexture = null; private const int leftColumnPositionX = 10; @@ -65,7 +70,7 @@ public override void Initialize() lblGameInformation.FontIndex = 1; lblGameInformation.Text = "GAME INFORMATION".L10N("Client:Main:GameInfo"); - if(AssetLoader.AssetExists("noMapPreview.png")) + if (AssetLoader.AssetExists("noMapPreview.png")) noMapPreviewTexture = AssetLoader.LoadTexture("noMapPreview.png"); rightColumnPositionX = Width / 2 - columnMargin; @@ -189,9 +194,17 @@ public void SetInfo(GenericHostedGame game) if (mapLoader != null) { - mapTexture = mapLoader.GameModeMaps.Find(m => m.Map.Name == game.Map)?.Map.LoadPreviewTexture(); + mapTexture = mapLoader.GameModeMaps.Find(m => m.Map.Name == game.Map && m.Map.IsPreviewTextureCached())?.Map?.LoadPreviewTexture(); if (mapTexture == null && noMapPreviewTexture != null) + { + Debug.Assert(!noMapPreviewTexture.IsDisposed, "noMapPreviewTexture should not be disposed."); mapTexture = noMapPreviewTexture; + disposeTextures = false; + } + else + { + disposeTextures = true; + } } } @@ -208,8 +221,12 @@ public void ClearInfo() foreach (XNALabel label in lblPlayerNames) label.Visible = false; - mapTexture?.Dispose(); - mapTexture = null; + if (mapTexture != null && disposeTextures) + { + Debug.Assert(!mapTexture.IsDisposed, "mapTexture should not be disposed."); + mapTexture.Dispose(); + mapTexture = null; + } } public override void Draw(GameTime gameTime) diff --git a/DXMainClient/Domain/Multiplayer/Map.cs b/DXMainClient/Domain/Multiplayer/Map.cs index 1393dae6f..7e1b4cffd 100644 --- a/DXMainClient/Domain/Multiplayer/Map.cs +++ b/DXMainClient/Domain/Multiplayer/Map.cs @@ -680,6 +680,9 @@ private void ParseSpawnIniOptions(IniFile forcedOptionsIni, string spawnIniOptio } } + public bool IsPreviewTextureCached() => + SafePath.GetFile(ProgramConstants.GamePath, PreviewPath).Exists; + /// /// Loads and returns the map preview texture. ///