Skip to content

Commit 18bd242

Browse files
authored
Merge branch 'CnCNet:develop' into develop
2 parents 055c75a + 1c099fa commit 18bd242

17 files changed

Lines changed: 682 additions & 154 deletions

ClientCore/Settings/UserINISettings.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ protected UserINISettings(IniFile iniFile)
150150
Difficulty = new IntSetting(iniFile, OPTIONS, "Difficulty", 1);
151151
ScrollDelay = new IntSetting(iniFile, OPTIONS, "ScrollDelay", 4);
152152
GameSpeed = new IntSetting(iniFile, OPTIONS, "GameSpeed", 1);
153-
PreloadMapPreviews = new BoolSetting(iniFile, VIDEO, "PreloadMapPreviews", false);
154153
ForceLowestDetailLevel = new BoolSetting(iniFile, VIDEO, "ForceLowestDetailLevel", false);
155154
MinimizeWindowsOnGameStart = new BoolSetting(iniFile, OPTIONS, "MinimizeWindowsOnGameStart", true);
156155
AutoRemoveUnderscoresFromName = new BoolSetting(iniFile, OPTIONS, "AutoRemoveUnderscoresFromName", true);
@@ -319,8 +318,6 @@ public void SetGameOptionFilterValue(string optionName, int? value)
319318

320319
public IntSetting ScrollDelay { get; private set; }
321320

322-
public BoolSetting PreloadMapPreviews { get; private set; }
323-
324321
public BoolSetting ForceLowestDetailLevel { get; private set; }
325322

326323
public BoolSetting MinimizeWindowsOnGameStart { get; private set; }

DXMainClient/DXGUI/Multiplayer/GameInformationPanel.cs

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using Rampastring.XNAUI;
1717
using Rampastring.XNAUI.XNAControls;
18+
using Image = SixLabors.ImageSharp.Image;
1819

1920
namespace DTAClient.DXGUI.Multiplayer
2021
{
@@ -51,8 +52,12 @@ public GameInformationPanel(WindowManager windowManager, MapLoader mapLoader, Ga
5152

5253
private GenericHostedGame game = null;
5354

54-
private bool disposeTextures = false;
55-
private Texture2D mapTexture = null;
55+
/// <summary>
56+
/// Indicates whether `mapPreviewTexture` needs to be disposed before loading the next texture. This is true if the current `mapPreviewTexture` was created from a map preview image and not assigned from the shared `noMapPreviewTexture`.
57+
/// </summary>
58+
private bool mapPreviewTextureNeedsDispose = false;
59+
private Texture2D mapPreviewTexture = null;
60+
5661
private Texture2D noMapPreviewTexture = null;
5762

5863
private Texture2D txLockedGame;
@@ -188,11 +193,10 @@ public void SetInfo(GenericHostedGame game)
188193

189194
if (!string.IsNullOrEmpty(game.MapHash) && mapLoader != null)
190195
{
191-
var mapEntry = mapLoader.GameModeMaps
192-
.Find(m => m.Map.SHA1.Equals(game.MapHash, StringComparison.OrdinalIgnoreCase));
196+
Map map = mapLoader.FindMapByHash(game.MapHash);
193197

194-
if (mapEntry != null)
195-
translatedMapName = mapEntry.Map.Name ?? mapEntry.Map.UntranslatedName;
198+
if (map != null)
199+
translatedMapName = map.Name ?? map.UntranslatedName;
196200
else if (!string.IsNullOrEmpty(game.Map))
197201
translatedMapName = game.Map; // fallback to broadcasted name
198202
}
@@ -247,22 +251,41 @@ public void SetInfo(GenericHostedGame game)
247251

248252
if (mapLoader != null && !string.IsNullOrEmpty(game.MapHash))
249253
{
250-
mapTexture = mapLoader.GameModeMaps
251-
.Find(m => m.Map.SHA1.Equals(game.MapHash, StringComparison.OrdinalIgnoreCase) &&
252-
m.Map.IsPreviewTextureCached())?.Map?.LoadPreviewTexture();
254+
Debug.Assert(!mapPreviewTextureNeedsDispose, "previous texture must be disposed before loading a new texture");
255+
256+
Map map = mapLoader.FindMapByHash(game.MapHash);
257+
258+
Image mapPreviewImage = map != null ? mapLoader.GetCachedPreviewImageFromMap(map, syncLoadOnCacheMiss: false) : null;
253259

254-
if (mapTexture == null && noMapPreviewTexture != null)
260+
if (mapPreviewImage != null)
261+
{
262+
mapPreviewTexture = AssetLoader.TextureFromImage(mapPreviewImage);
263+
mapPreviewTextureNeedsDispose = true;
264+
}
265+
else if (noMapPreviewTexture != null)
255266
{
256-
Debug.Assert(!noMapPreviewTexture.IsDisposed, "noMapPreviewTexture should not be disposed.");
257-
mapTexture = noMapPreviewTexture;
258-
disposeTextures = false;
267+
Debug.Assert(!noMapPreviewTexture.IsDisposed, "noMapPreviewTexture should never be disposed.");
268+
mapPreviewTexture = noMapPreviewTexture;
269+
mapPreviewTextureNeedsDispose = false;
259270
}
260271
else
261272
{
262-
disposeTextures = true;
273+
mapPreviewTexture = null;
274+
mapPreviewTextureNeedsDispose = false;
263275
}
264276
}
277+
else
278+
{
279+
if (mapPreviewTextureNeedsDispose &&
280+
mapPreviewTexture != null &&
281+
!mapPreviewTexture.IsDisposed)
282+
{
283+
mapPreviewTexture.Dispose();
284+
}
265285

286+
mapPreviewTexture = null;
287+
mapPreviewTextureNeedsDispose = false;
288+
}
266289
SetGameOptionsInfo(game);
267290
SetLegendInfo(game);
268291
}
@@ -480,11 +503,12 @@ public void ClearInfo()
480503
foreach (XNALabel label in lblPlayerNames)
481504
label.Visible = false;
482505

483-
if (mapTexture != null && disposeTextures)
506+
if (mapPreviewTexture != null && mapPreviewTextureNeedsDispose)
484507
{
485-
Debug.Assert(!mapTexture.IsDisposed, "mapTexture should not be disposed.");
486-
mapTexture.Dispose();
487-
mapTexture = null;
508+
Debug.Assert(!mapPreviewTexture.IsDisposed, "mapPreviewTexture should not be disposed before this call");
509+
mapPreviewTexture.Dispose();
510+
mapPreviewTexture = null;
511+
mapPreviewTextureNeedsDispose = false;
488512
}
489513
}
490514

@@ -494,34 +518,34 @@ public override void Draw(GameTime gameTime)
494518
{
495519
base.Draw(gameTime);
496520

497-
if (game != null && mapTexture != null)
521+
if (game != null && mapPreviewTexture != null)
498522
RenderMapPreview();
499523
}
500524
}
501525

502526
private void RenderMapPreview()
503527
{
504528
// Calculate map preview area based on right half of ClientRectangle
505-
double xRatio = (ClientRectangle.Width / 2 - mapPreviewHorizontalMargin) / (double)mapTexture.Width;
506-
double yRatio = (ClientRectangle.Height - mapPreviewVerticalMargin) / (double)mapTexture.Height;
529+
double xRatio = (ClientRectangle.Width / 2 - mapPreviewHorizontalMargin) / (double)mapPreviewTexture.Width;
530+
double yRatio = (ClientRectangle.Height - mapPreviewVerticalMargin) / (double)mapPreviewTexture.Height;
507531

508532
double ratio = Math.Min(xRatio, yRatio); // Choose the smaller ratio for scaling
509-
int textureWidth = (int)(mapTexture.Width * ratio);
510-
int textureHeight = (int)(mapTexture.Height * ratio);
533+
int textureWidth = (int)(mapPreviewTexture.Width * ratio);
534+
int textureHeight = (int)(mapPreviewTexture.Height * ratio);
511535

512536
// Apply max height constraint
513537
if (textureHeight > maxPreviewHeight)
514538
{
515-
ratio = maxPreviewHeight / (double)mapTexture.Height;
539+
ratio = maxPreviewHeight / (double)mapPreviewTexture.Height;
516540
textureHeight = maxPreviewHeight;
517-
textureWidth = (int)(mapTexture.Width * ratio); // Recalculate width to maintain aspect ratio
541+
textureWidth = (int)(mapPreviewTexture.Width * ratio); // Recalculate width to maintain aspect ratio
518542
}
519543

520544
int texturePositionX = rightColumnPositionX + (ClientRectangle.Width / 2 - textureWidth) / 2; // Center in the right column
521545
int texturePositionY = mapPreviewPositionY;
522546

523547
DrawTexture(
524-
mapTexture,
548+
mapPreviewTexture,
525549
new Rectangle(texturePositionX, texturePositionY, textureWidth, textureHeight),
526550
Color.White
527551
);

DXMainClient/DXGUI/Multiplayer/GameListBox.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using ClientCore;
66
using ClientCore.Enums;
77
using ClientCore.Extensions;
8+
89
using DTAClient.Domain.Multiplayer;
910
using DTAClient.Domain.Multiplayer.CnCNet;
1011
using DTAClient.DXGUI.Multiplayer.GameLobby;
@@ -40,7 +41,7 @@ public GameListBox(WindowManager windowManager, MapLoader mapLoader,
4041
SkillLevelOptions = ClientConfiguration.Instance.SkillLevelOptions.Split(',');
4142
}
4243

43-
private List<Texture2D?> txSkillLevelIcons = new();
44+
private List<Texture2D?> txSkillLevelIcons = new();
4445

4546
private int loadedGameTextWidth;
4647

@@ -126,6 +127,9 @@ public void AddGame(GenericHostedGame game)
126127
{
127128
HostedGames.Add(game);
128129

130+
// Early notify the map preview cache
131+
mapLoader.PrefetchCachedPreviewImageFromMap(mapLoader.FindMapByHash(game.MapHash));
132+
129133
Refresh();
130134
}
131135

@@ -207,7 +211,7 @@ private void InitSkillLevelIcons()
207211
for (int i = 0; i < SkillLevelOptions.Length; i++)
208212
{
209213
string fileName = $"skillLevel{i}.png";
210-
214+
211215
txSkillLevelIcons.Add(AssetLoader.AssetExists(fileName)
212216
? AssetLoader.LoadTexture(fileName)
213217
: null);

DXMainClient/DXGUI/Multiplayer/GameLobby/CnCNetGameLobby.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ private void ApplyGameOptions(string sender, string message)
12951295
lastMapSHA1 = mapSHA1;
12961296
lastMapName = mapName;
12971297

1298-
GameModeMap = GameModeMaps.Find(gmm => gmm.GameMode.Name == gameMode && gmm.Map.SHA1 == mapSHA1);
1298+
GameModeMap = GameModeMaps.FirstOrDefault(gmm => gmm.GameMode.Name == gameMode && gmm.Map.SHA1 == mapSHA1);
12991299
if (GameModeMap == null)
13001300
{
13011301
ChangeMap(null);
@@ -1949,7 +1949,7 @@ private void MapLoader_MapChanged(object sender, MapChangedEventArgs e)
19491949

19501950
AddNotice($"Map {e.Map.Name} loaded successfully.");
19511951

1952-
GameModeMap = GameModeMaps.Find(gmm => gmm.Map.SHA1 == e.Map.SHA1);
1952+
GameModeMap = GameModeMaps.FirstOrDefault(gmm => gmm.Map.SHA1 == e.Map.SHA1);
19531953
ChangeMap(GameModeMap);
19541954

19551955
if (isFromChatCommand)
@@ -2185,7 +2185,7 @@ private void DownloadMapByIdCommand(string parameters)
21852185
sha1 = sha1.Replace("?", "");
21862186

21872187
// See if the user already has this map, with any filename, before attempting to download it.
2188-
GameModeMap loadedMap = GameModeMaps.Find(gmm => gmm.Map.SHA1 == sha1);
2188+
GameModeMap loadedMap = GameModeMaps.FirstOrDefault(gmm => gmm.Map.SHA1 == sha1);
21892189

21902190
if (loadedMap != null)
21912191
{

DXMainClient/DXGUI/Multiplayer/GameLobby/GameLobbyBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public List<IGameSessionSetting> GetBroadcastableSettings()
110110
/// The list of multiplayer game mode maps.
111111
/// Each is an instance of a map for a specific game mode.
112112
/// </summary>
113-
protected GameModeMapCollection GameModeMaps => MapLoader.GameModeMaps;
113+
protected IReadOnlyGameModeMapCollection GameModeMaps => MapLoader.GameModeMaps;
114114

115115
protected GameModeMapFilter gameModeMapFilter;
116116

@@ -873,7 +873,7 @@ private void DeleteSelectedMap(XNAMessageBox messageBox)
873873
if (GameMode.Maps.Count == 0)
874874
{
875875
// this will trigger another GameMode to be selected
876-
GameModeMap = GameModeMaps.Find(gm => gm.GameMode.Maps.Count > 0);
876+
GameModeMap = GameModeMaps.FirstOrDefault(gm => gm.GameMode.Maps.Count > 0);
877877
}
878878
else
879879
{
@@ -936,7 +936,7 @@ private void PickRandomMap()
936936

937937
int randomValue = random.Next(0, maps.Count);
938938
bool isFavoriteMapsSelected = IsFavoriteMapsSelected();
939-
GameModeMap = GameModeMaps.Find(gmm => (gmm.GameMode == GameMode || gmm.IsFavorite && isFavoriteMapsSelected) && gmm.Map == maps[randomValue]);
939+
GameModeMap = GameModeMaps.FirstOrDefault(gmm => (gmm.GameMode == GameMode || gmm.IsFavorite && isFavoriteMapsSelected) && gmm.Map == maps[randomValue]);
940940
Logger.Log("PickRandomMap: Rolled " + randomValue + " out of " + maps.Count + ". Picked map: " + Map.Name);
941941

942942
ChangeMap(GameModeMap);

DXMainClient/DXGUI/Multiplayer/GameLobby/LANGameLobby.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ private void HandleGameOptionsMessage(string data)
10261026
string mapSHA1 = parts[parts.Length - (GAME_OPTION_SPECIAL_FLAG_COUNT - 1)];
10271027
string gameMode = parts[parts.Length - (GAME_OPTION_SPECIAL_FLAG_COUNT - 2)];
10281028

1029-
GameModeMap gameModeMap = GameModeMaps.Find(gmm => gmm.GameMode.Name == gameMode && gmm.Map.SHA1 == mapSHA1);
1029+
GameModeMap gameModeMap = GameModeMaps.FirstOrDefault(gmm => gmm.GameMode.Name == gameMode && gmm.Map.SHA1 == mapSHA1);
10301030

10311031
if (gameModeMap == null)
10321032
{

0 commit comments

Comments
 (0)