Skip to content

Commit c966cea

Browse files
authored
chore(1377): batch map request packets from client (AscensionGameDev#2023)
* fix: fringe crash * chore(1377): batch map request packets from client
1 parent ad0368a commit c966cea

File tree

6 files changed

+81
-27
lines changed

6 files changed

+81
-27
lines changed

Intersect (Core)/Logging/Output/FileOutput.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,30 @@ private void InternalWrite(
111111
params object[] args
112112
)
113113
{
114-
if (LogLevel < logLevel)
114+
try
115115
{
116-
return;
117-
}
118-
119-
var line = configuration.Formatter.Format(
120-
configuration, logLevel, DateTime.UtcNow, exception, format, args
121-
);
116+
if (LogLevel < logLevel)
117+
{
118+
return;
119+
}
122120

123-
Writer.Write(line);
124-
Writer.Write(Spacer);
125-
Writer.Flush();
121+
var line = configuration.Formatter.Format(
122+
configuration,
123+
logLevel,
124+
DateTime.UtcNow,
125+
exception,
126+
format,
127+
args
128+
);
129+
130+
Writer.Write(line);
131+
Writer.Write(Spacer);
132+
Writer.Flush();
133+
}
134+
catch (Exception exceptionWhileWriting)
135+
{
136+
Console.WriteLine(exceptionWhileWriting);
137+
}
126138
}
127139

128140
~FileOutput()

Intersect (Core)/Network/Packets/Client/NeedMapPacket.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ public NeedMapPacket()
1111
{
1212
}
1313

14-
public NeedMapPacket(Guid mapId)
14+
public NeedMapPacket(params Guid[] mapIds)
1515
{
16-
MapId = mapId;
16+
MapIds = new List<Guid>(mapIds);
1717
}
1818

1919
[Key(0)]
20-
public Guid MapId { get; set; }
20+
public List<Guid> MapIds { get; set; }
2121

2222
}
2323

Intersect.Client/Entities/Player.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,10 +2327,10 @@ public void FetchNewMaps()
23272327
return;
23282328
}
23292329

2330-
if (Maps.MapInstance.Get(Globals.Me.MapId) != null)
2330+
if (Maps.MapInstance.TryGet(Globals.Me.MapId, out var mapInstance))
23312331
{
2332-
var gridX = Maps.MapInstance.Get(Globals.Me.MapId).GridX;
2333-
var gridY = Maps.MapInstance.Get(Globals.Me.MapId).GridY;
2332+
var gridX = mapInstance.GridX;
2333+
var gridY = mapInstance.GridY;
23342334
for (var x = gridX - 1; x <= gridX + 1; x++)
23352335
{
23362336
for (var y = gridY - 1; y <= gridY + 1; y++)

Intersect.Client/Maps/MapInstance.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,9 +1370,17 @@ public void Dispose(bool prep = true, bool killentities = true)
13701370

13711371
public static bool MapNotRequested(Guid mapId) => !MapRequests.ContainsKey(mapId) || MapRequests[mapId] < Timing.Global.Milliseconds;
13721372

1373-
public static void UpdateMapRequestTime(Guid mapId)
1373+
public static void UpdateMapRequestTime(params Guid[] mapIds)
13741374
{
1375-
MapRequests[mapId] = Timing.Global.Milliseconds + 2000;
1375+
foreach (var mapId in mapIds)
1376+
{
1377+
if (mapId == default)
1378+
{
1379+
continue;
1380+
}
1381+
1382+
MapRequests[mapId] = Timing.Global.Milliseconds + 2000;
1383+
}
13761384
}
13771385
}
13781386

Intersect.Client/Networking/PacketSender.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,44 @@ public static void SendLogout(bool characterSelect = false)
3030
Network.SendPacket(new LogoutPacket(characterSelect));
3131
}
3232

33-
public static void SendNeedMap(Guid mapId)
33+
public static void SendNeedMap(params Guid[] mapIds)
3434
{
35-
if (mapId == default || MapInstance.Get(mapId) != null || !MapInstance.MapNotRequested(mapId))
35+
var validMapIds = mapIds.Where(
36+
mapId => mapId != default && MapInstance.TryGet(mapId, out _) && MapInstance.MapNotRequested(mapId)
37+
)
38+
.ToArray();
39+
if (validMapIds.Length < 1)
3640
{
3741
return;
3842
}
39-
Network.SendPacket(new NeedMapPacket(mapId));
40-
MapInstance.UpdateMapRequestTime(mapId);
43+
44+
Network.SendPacket(new NeedMapPacket(validMapIds));
45+
MapInstance.UpdateMapRequestTime(validMapIds);
46+
}
47+
48+
public static void SendNeedMapForGrid(MapInstance? mapInstance)
49+
{
50+
if (mapInstance == default && !MapInstance.TryGet(Globals.Me.MapId, out mapInstance))
51+
{
52+
return;
53+
}
54+
55+
var gridX = mapInstance.GridX;
56+
var gridY = mapInstance.GridY;
57+
var minX = Math.Max(0, gridX - 1);
58+
var minY = Math.Max(0, gridY - 1);
59+
var maxX = Math.Max(Globals.MapGridWidth, gridX + 2);
60+
var maxY = Math.Max(Globals.MapGridHeight, gridY + 2);
61+
for (int x = minX; x < maxX; x++)
62+
{
63+
for (int y = minY; y < maxY; y++)
64+
{
65+
if (Globals.MapGrid[x, y] != Guid.Empty)
66+
{
67+
PacketSender.SendNeedMap(Globals.MapGrid[x, y]);
68+
}
69+
}
70+
}
4171
}
4272

4373
public static void SendMove()

Intersect.Server.Core/Networking/PacketHandler.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -659,13 +659,17 @@ public void HandlePacket(Client client, LogoutPacket packet)
659659
public void HandlePacket(Client client, NeedMapPacket packet)
660660
{
661661
var player = client?.Entity;
662-
var map = MapController.Get(packet.MapId);
663-
if (map != null)
662+
foreach (var mapId in packet.MapIds)
664663
{
665-
PacketSender.SendMap(client, packet.MapId);
666-
if (player != null && packet.MapId == player.MapId)
664+
if (!MapController.TryGet(mapId, out var mapController))
667665
{
668-
PacketSender.SendMapGrid(client, map.MapGrid);
666+
continue;
667+
}
668+
669+
PacketSender.SendMap(client, mapId);
670+
if (player != null && mapId == player.MapId)
671+
{
672+
PacketSender.SendMapGrid(client, mapController.MapGrid);
669673
}
670674
}
671675
}

0 commit comments

Comments
 (0)