1515
1616using Rampastring . XNAUI ;
1717using Rampastring . XNAUI . XNAControls ;
18+ using Image = SixLabors . ImageSharp . Image ;
1819
1920namespace 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 ) ;
0 commit comments