|
14 | 14 | using Intersect.Network; |
15 | 15 | using Intersect.Network.Packets.Server; |
16 | 16 | using Microsoft.Extensions.Logging; |
| 17 | +using ApplicationContext = Intersect.Core.ApplicationContext; |
17 | 18 |
|
18 | 19 | namespace Intersect.Editor.Networking; |
19 | 20 |
|
@@ -272,21 +273,68 @@ public void HandlePacket(IPacketSender packetSender, MapPacket packet) |
272 | 273 | } |
273 | 274 | } |
274 | 275 |
|
275 | | - if (Globals.CurrentMap.Id == packet.MapId && Globals.MapGrid != null && Globals.MapGrid.Loaded) |
| 276 | + if (Globals.CurrentMap is not { } currentMap) |
276 | 277 | { |
277 | | - for (var y = Globals.CurrentMap.MapGridY + 1; y >= Globals.CurrentMap.MapGridY - 1; y--) |
| 278 | + throw new InvalidOperationException( |
| 279 | + $"Received packet for map {packet.MapId} but the current map was null" |
| 280 | + ); |
| 281 | + } |
| 282 | + |
| 283 | + if (Globals.MapGrid is not { } mapGrid) |
| 284 | + { |
| 285 | + ApplicationContext.CurrentContext.Logger.LogError( |
| 286 | + "Received packet for map {MapId} before the map grid was initialized", |
| 287 | + packet.MapId |
| 288 | + ); |
| 289 | + return; |
| 290 | + } |
| 291 | + |
| 292 | + if (!mapGrid.Loaded) |
| 293 | + { |
| 294 | + ApplicationContext.CurrentContext.Logger.LogError( |
| 295 | + "Received packet for map {MapId} before the map grid was loaded", |
| 296 | + packet.MapId |
| 297 | + ); |
| 298 | + return; |
| 299 | + } |
| 300 | + |
| 301 | + if (currentMap.Id != packet.MapId) |
| 302 | + { |
| 303 | + ApplicationContext.CurrentContext.Logger.LogError( |
| 304 | + "Received packet for map {MapId} but the current map is {CurrentMapId} ({CurrentMapName})", |
| 305 | + packet.MapId, |
| 306 | + currentMap.Id, |
| 307 | + currentMap.Name |
| 308 | + ); |
| 309 | + return; |
| 310 | + } |
| 311 | + |
| 312 | + for (var y = currentMap.MapGridY + 1; y >= currentMap.MapGridY - 1; y--) |
| 313 | + { |
| 314 | + if (y < 0 || y >= mapGrid.GridHeight) |
278 | 315 | { |
279 | | - for (var x = Globals.CurrentMap.MapGridX - 1; x <= Globals.CurrentMap.MapGridX + 1; x++) |
| 316 | + continue; |
| 317 | + } |
| 318 | + |
| 319 | + for (var x = currentMap.MapGridX - 1; x <= currentMap.MapGridX + 1; x++) |
| 320 | + { |
| 321 | + if (x < 0 || x >= mapGrid.GridWidth) |
280 | 322 | { |
281 | | - if (x >= 0 && x < Globals.MapGrid.GridWidth && y >= 0 && y < Globals.MapGrid.GridHeight) |
282 | | - { |
283 | | - var needMap = MapInstance.Get(Globals.MapGrid.Grid[x, y].MapId); |
284 | | - if (needMap == null && Globals.MapGrid.Grid[x, y].MapId != Guid.Empty) |
285 | | - { |
286 | | - PacketSender.SendNeedMap(Globals.MapGrid.Grid[x, y].MapId); |
287 | | - } |
288 | | - } |
| 323 | + continue; |
| 324 | + } |
| 325 | + |
| 326 | + var gridMapId = mapGrid.Grid[x, y].MapId; |
| 327 | + if (gridMapId == Guid.Empty) |
| 328 | + { |
| 329 | + continue; |
| 330 | + } |
| 331 | + |
| 332 | + if (!MapInstance.TryGet(gridMapId, out _)) |
| 333 | + { |
| 334 | + continue; |
289 | 335 | } |
| 336 | + |
| 337 | + PacketSender.SendNeedMap(gridMapId); |
290 | 338 | } |
291 | 339 | } |
292 | 340 | } |
|
0 commit comments