Skip to content

Commit d90c85d

Browse files
committed
fix: update editor to use new map lock
1 parent 4598207 commit d90c85d

File tree

3 files changed

+187
-80
lines changed

3 files changed

+187
-80
lines changed

Intersect.Editor/Core/Graphics.cs

Lines changed: 146 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

Intersect.Editor/Maps/MapInstance.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,25 @@ public partial class MapInstance : MapBase, IGameObject<Guid, MapInstance>
2626

2727
public MapInstance(Guid id) : base(id)
2828
{
29-
lock (MapLock)
29+
if (!Lock.TryAcquireLock(out var lockRef))
30+
{
31+
throw new InvalidOperationException("Failed to acquire map instance lock");
32+
}
33+
34+
using (lockRef)
3035
{
3136
Autotiles = new MapAutotiles(this);
3237
}
3338
}
3439

3540
public MapInstance(MapBase mapStruct) : base(mapStruct)
3641
{
37-
lock (MapLock)
42+
if (!Lock.TryAcquireLock(out var lockRef))
43+
{
44+
throw new InvalidOperationException("Failed to acquire map instance lock");
45+
}
46+
47+
using (lockRef)
3848
{
3949
Autotiles = new MapAutotiles(this);
4050
if (typeof(MapInstance) == mapStruct.GetType())
@@ -56,7 +66,12 @@ public MapInstance(MapBase mapStruct) : base(mapStruct)
5666

5767
public void Load(string mapJson, bool import = false, bool clearEvents = true)
5868
{
59-
lock (MapLock)
69+
if (!Lock.TryAcquireLock(out var lockRef))
70+
{
71+
throw new InvalidOperationException("Failed to acquire map instance lock");
72+
}
73+
74+
using (lockRef)
6075
{
6176
var up = Up;
6277
var down = Down;
@@ -88,7 +103,12 @@ public void Load(string mapJson, bool import = false, bool clearEvents = true)
88103

89104
public void LoadTileData(byte[] packet)
90105
{
91-
lock (MapLock)
106+
if (!Lock.TryAcquireLock(out var lockRef))
107+
{
108+
throw new InvalidOperationException("Failed to acquire map instance lock");
109+
}
110+
111+
using (lockRef)
92112
{
93113
Layers = JsonConvert.DeserializeObject<Dictionary<string, Tile[,]>>(LZ4.UnPickleString(packet), mJsonSerializerSettings);
94114
foreach (var layer in Options.Instance.MapOpts.Layers.All)
@@ -271,7 +291,12 @@ public void Update()
271291

272292
public void InitAutotiles()
273293
{
274-
lock (MapLock)
294+
if (!Lock.TryAcquireLock(out var lockRef))
295+
{
296+
throw new InvalidOperationException("Failed to acquire map instance lock");
297+
}
298+
299+
using (lockRef)
275300
{
276301
Autotiles.InitAutotiles(GenerateAutotileGrid());
277302
}

0 commit comments

Comments
 (0)