@@ -226,7 +226,12 @@ public static void Render()
226226
227227 if ( map != null )
228228 {
229- lock ( map . MapLock )
229+ if ( ! map . Lock . TryAcquireLock ( out var lockRef ) )
230+ {
231+ throw new InvalidOperationException ( "Failed to acquire map instance lock" ) ;
232+ }
233+
234+ using ( lockRef )
230235 {
231236 //Draw this map
232237 DrawMap (
@@ -265,7 +270,12 @@ public static void Render()
265270 continue ;
266271 }
267272
268- lock ( map . MapLock )
273+ if ( ! map . Lock . TryAcquireLock ( out var lockRef ) )
274+ {
275+ throw new InvalidOperationException ( "Failed to acquire map instance lock" ) ;
276+ }
277+
278+ using ( lockRef )
269279 {
270280 DrawMapAttributes (
271281 map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY ,
@@ -294,15 +304,22 @@ public static void Render()
294304 if ( x >= 0 && x < Globals . MapGrid . GridWidth && y >= 0 && y < Globals . MapGrid . GridHeight )
295305 {
296306 var map = MapInstance . Get ( Globals . MapGrid . Grid [ x , y ] . MapId ) ;
297- if ( map ! = null )
307+ if ( map = = null )
298308 {
299- lock ( map . MapLock )
300- {
301- DrawMapEvents (
302- map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY ,
303- false , null
304- ) ;
305- }
309+ continue ;
310+ }
311+
312+ if ( ! map . Lock . TryAcquireLock ( out var lockRef ) )
313+ {
314+ throw new InvalidOperationException ( "Failed to acquire map instance lock" ) ;
315+ }
316+
317+ using ( lockRef )
318+ {
319+ DrawMapEvents (
320+ map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY ,
321+ false , null
322+ ) ;
306323 }
307324 }
308325 }
@@ -316,16 +333,23 @@ public static void Render()
316333 if ( x >= 0 && x < Globals . MapGrid . GridWidth && y >= 0 && y < Globals . MapGrid . GridHeight )
317334 {
318335 var map = MapInstance . Get ( Globals . MapGrid . Grid [ x , y ] . MapId ) ;
319- if ( map ! = null )
336+ if ( map = = null )
320337 {
321- lock ( map . MapLock )
322- {
323- //Draw this map
324- DrawMap (
325- map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY ,
326- false , 1 , null
327- ) ;
328- }
338+ continue ;
339+ }
340+
341+ if ( ! map . Lock . TryAcquireLock ( out var lockRef ) )
342+ {
343+ throw new InvalidOperationException ( "Failed to acquire map instance lock" ) ;
344+ }
345+
346+ using ( lockRef )
347+ {
348+ //Draw this map
349+ DrawMap (
350+ map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY ,
351+ false , 1 , null
352+ ) ;
329353 }
330354 }
331355 }
@@ -339,15 +363,22 @@ public static void Render()
339363 if ( x >= 0 && x < Globals . MapGrid . GridWidth && y >= 0 && y < Globals . MapGrid . GridHeight )
340364 {
341365 var map = MapInstance . Get ( Globals . MapGrid . Grid [ x , y ] . MapId ) ;
342- if ( map ! = null )
366+ if ( map = = null )
343367 {
344- lock ( map . MapLock )
345- {
346- DrawMapAttributes (
347- map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY ,
348- false , null , true , false
349- ) ;
350- }
368+ continue ;
369+ }
370+
371+ if ( ! map . Lock . TryAcquireLock ( out var lockRef ) )
372+ {
373+ throw new InvalidOperationException ( "Failed to acquire map instance lock" ) ;
374+ }
375+
376+ using ( lockRef )
377+ {
378+ DrawMapAttributes (
379+ map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY ,
380+ false , null , true , false
381+ ) ;
351382 }
352383 }
353384 }
@@ -574,9 +605,14 @@ RenderTarget2D renderTarget2D
574605 tmpMap = TilePreviewStruct ;
575606 if ( TilePreviewUpdated || TilePreviewStruct == null )
576607 {
577- if ( Globals . CurrentMap != null )
608+ if ( Globals . CurrentMap is { } currentMap )
578609 {
579- lock ( Globals . CurrentMap . MapLock )
610+ if ( ! currentMap . Lock . TryAcquireLock ( out var lockRef ) )
611+ {
612+ throw new InvalidOperationException ( "Failed to acquire map instance lock" ) ;
613+ }
614+
615+ using ( lockRef )
580616 {
581617 TilePreviewStruct = new MapInstance ( Globals . CurrentMap ) ;
582618 }
@@ -1651,7 +1687,12 @@ public static Bitmap ScreenShotMap()
16511687 continue ;
16521688 }
16531689
1654- lock ( map . MapLock )
1690+ if ( ! map . Lock . TryAcquireLock ( out var lockRef ) )
1691+ {
1692+ throw new InvalidOperationException ( "Failed to acquire map instance lock" ) ;
1693+ }
1694+
1695+ using ( lockRef )
16551696 {
16561697 //Draw this map
16571698 DrawMap (
@@ -1670,25 +1711,32 @@ public static Bitmap ScreenShotMap()
16701711 if ( x >= 0 && x < Globals . MapGrid . GridWidth && y >= 0 && y < Globals . MapGrid . GridHeight )
16711712 {
16721713 var map = MapInstance . Get ( Globals . MapGrid . Grid [ x , y ] . MapId ) ;
1673- if ( map ! = null )
1714+ if ( map = = null )
16741715 {
1675- lock ( map . MapLock )
1676- {
1677- DrawMapAttributes (
1678- map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY , true ,
1679- sScreenShotRenderTexture , false , false
1680- ) ;
1716+ continue ;
1717+ }
16811718
1682- DrawMapAttributes (
1683- map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY , true ,
1684- sScreenShotRenderTexture , false , true
1685- ) ;
1719+ if ( ! map . Lock . TryAcquireLock ( out var lockRef ) )
1720+ {
1721+ throw new InvalidOperationException ( "Failed to acquire map instance lock" ) ;
1722+ }
16861723
1687- DrawMapAttributes (
1688- map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY , true ,
1689- sScreenShotRenderTexture , true , true
1690- ) ;
1691- }
1724+ using ( lockRef )
1725+ {
1726+ DrawMapAttributes (
1727+ map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY , true ,
1728+ sScreenShotRenderTexture , false , false
1729+ ) ;
1730+
1731+ DrawMapAttributes (
1732+ map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY , true ,
1733+ sScreenShotRenderTexture , false , true
1734+ ) ;
1735+
1736+ DrawMapAttributes (
1737+ map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY , true ,
1738+ sScreenShotRenderTexture , true , true
1739+ ) ;
16921740 }
16931741 }
16941742 }
@@ -1702,15 +1750,22 @@ public static Bitmap ScreenShotMap()
17021750 if ( x >= 0 && x < Globals . MapGrid . GridWidth && y >= 0 && y < Globals . MapGrid . GridHeight )
17031751 {
17041752 var map = MapInstance . Get ( Globals . MapGrid . Grid [ x , y ] . MapId ) ;
1705- if ( map ! = null )
1753+ if ( map = = null )
17061754 {
1707- lock ( map . MapLock )
1708- {
1709- DrawMapEvents (
1710- map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY ,
1711- true , sScreenShotRenderTexture
1712- ) ;
1713- }
1755+ continue ;
1756+ }
1757+
1758+ if ( ! map . Lock . TryAcquireLock ( out var lockRef ) )
1759+ {
1760+ throw new InvalidOperationException ( "Failed to acquire map instance lock" ) ;
1761+ }
1762+
1763+ using ( lockRef )
1764+ {
1765+ DrawMapEvents (
1766+ map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY ,
1767+ true , sScreenShotRenderTexture
1768+ ) ;
17141769 }
17151770 }
17161771 }
@@ -1724,16 +1779,23 @@ public static Bitmap ScreenShotMap()
17241779 if ( x >= 0 && x < Globals . MapGrid . GridWidth && y >= 0 && y < Globals . MapGrid . GridHeight )
17251780 {
17261781 var map = MapInstance . Get ( Globals . MapGrid . Grid [ x , y ] . MapId ) ;
1727- if ( map ! = null )
1782+ if ( map = = null )
17281783 {
1729- lock ( map . MapLock )
1730- {
1731- //Draw this map
1732- DrawMap (
1733- map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY , true , 1 ,
1734- sScreenShotRenderTexture
1735- ) ;
1736- }
1784+ continue ;
1785+ }
1786+
1787+ if ( ! map . Lock . TryAcquireLock ( out var lockRef ) )
1788+ {
1789+ throw new InvalidOperationException ( "Failed to acquire map instance lock" ) ;
1790+ }
1791+
1792+ using ( lockRef )
1793+ {
1794+ //Draw this map
1795+ DrawMap (
1796+ map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY , true , 1 ,
1797+ sScreenShotRenderTexture
1798+ ) ;
17371799 }
17381800 }
17391801 }
@@ -1747,23 +1809,35 @@ public static Bitmap ScreenShotMap()
17471809 if ( x >= 0 && x < Globals . MapGrid . GridWidth && y >= 0 && y < Globals . MapGrid . GridHeight )
17481810 {
17491811 var map = MapInstance . Get ( Globals . MapGrid . Grid ? [ x , y ] ? . MapId ?? Guid . Empty ) ;
1750- if ( map ! = null )
1812+ if ( map = = null )
17511813 {
1752- lock ( map . MapLock )
1753- {
1754- DrawMapAttributes (
1755- map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY , true ,
1756- sScreenShotRenderTexture , true , false
1757- ) ;
1758- }
1814+ continue ;
1815+ }
1816+
1817+ if ( ! map . Lock . TryAcquireLock ( out var lockRef ) )
1818+ {
1819+ throw new InvalidOperationException ( "Failed to acquire map instance lock" ) ;
1820+ }
1821+
1822+ using ( lockRef )
1823+ {
1824+ DrawMapAttributes (
1825+ map , x - Globals . CurrentMap . MapGridX , y - Globals . CurrentMap . MapGridY , true ,
1826+ sScreenShotRenderTexture , true , false
1827+ ) ;
17591828 }
17601829 }
17611830 }
17621831 }
17631832 }
17641833 else
17651834 {
1766- lock ( Globals . CurrentMap . MapLock )
1835+ if ( ! Globals . CurrentMap . Lock . TryAcquireLock ( out var lockRef ) )
1836+ {
1837+ throw new InvalidOperationException ( "Failed to acquire map instance lock" ) ;
1838+ }
1839+
1840+ using ( lockRef )
17671841 {
17681842 //Draw this map
17691843 DrawMap ( Globals . CurrentMap , 0 , 0 , true , 0 , sScreenShotRenderTexture ) ;
0 commit comments