Skip to content

Commit

Permalink
Request map info for new characters/npcs in the walk reply
Browse files Browse the repository at this point in the history
  • Loading branch information
sorokya committed Mar 28, 2022
1 parent 2803ca9 commit 278c31f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
63 changes: 60 additions & 3 deletions EOLib/PacketHandlers/MainPlayerWalkHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,46 @@
using EOLib.Domain.Login;
using EOLib.Domain.Map;
using EOLib.Net;
using EOLib.Net.Communication;
using EOLib.Net.Handlers;
using System.Collections.Generic;
using System.Linq;

namespace EOLib.PacketHandlers
{
[AutoMappedType]
public class MainPlayerWalkHandler : InGameOnlyPacketHandler
{
private readonly ICurrentMapStateRepository _currentMapStateRepository;
private readonly IPacketSendService _packetSendService;

public override PacketFamily Family => PacketFamily.Walk;
public override PacketAction Action => PacketAction.Reply;

public MainPlayerWalkHandler(IPlayerInfoProvider playerInfoProvider,
ICurrentMapStateRepository currentMapStateRepository)
ICurrentMapStateRepository currentMapStateRepository,
IPacketSendService packetSendService)
: base(playerInfoProvider)
{
_currentMapStateRepository = currentMapStateRepository;
_packetSendService = packetSendService;
}

public override bool HandlePacket(IPacket packet)
{
if (packet.ReadByte() != 255 || packet.ReadByte() != 255)
return false;
var playerIDs = new List<short>();
while (packet.PeekByte() != 0xFF)
{
playerIDs.Add(packet.ReadShort());
}
packet.ReadByte();

var npcIndexes = new List<byte>();
while (packet.PeekByte() != 0xFF)
{
npcIndexes.Add(packet.ReadChar());
}
packet.ReadByte();

var numberOfMapItems = packet.PeekEndString().Length / 9;
for (int i = 0; i < numberOfMapItems; ++i)
Expand All @@ -39,6 +56,46 @@ public override bool HandlePacket(IPacket packet)
_currentMapStateRepository.MapItems.Add(newItem);
}

var newPlayerIDs = playerIDs
.Where(id => !_currentMapStateRepository.Characters.ContainsKey(id))
.ToList();

var newNPCIndxes = npcIndexes
.Where(index => !_currentMapStateRepository.NPCs.Any((npc) => npc.Index == index))
.ToList();

if (newPlayerIDs.Count > 0 || newNPCIndxes.Count > 0)
{
IPacketBuilder builder = new PacketBuilder(PacketFamily.MapInfo, PacketAction.Request);

if (newPlayerIDs.Count > 0)
{
foreach (var playerId in newPlayerIDs)
{
builder = builder.AddShort(playerId);
}
}

if (newNPCIndxes.Count > 0)
{
builder.AddByte(0xFF);
foreach (var npcIndex in newNPCIndxes)
{
builder = builder.AddChar(npcIndex);
}
}

try
{
var request = builder.Build();
_packetSendService.SendPacket(request);
}
catch (NoDataSentException)
{
return false;
}
}

return true;
}
}
Expand Down
7 changes: 5 additions & 2 deletions EOLib/PacketHandlers/MapInfoHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ public class MapInfoHandler : InGameOnlyPacketHandler
public MapInfoHandler(IPlayerInfoProvider playerInfoProvider,
ICurrentMapStateRepository currentMapStateRepository,
ICharacterFromPacketFactory characterFromPacketFactory,
INPCFromPacketFactory npcFromPacketFactory
INPCFromPacketFactory npcFromPacketFactory,
IEIFFileProvider eifFileProvider
)
: base(playerInfoProvider)
{
_currentMapStateRepository = currentMapStateRepository;
_characterFromPacketFactory = characterFromPacketFactory;
_npcFromPacketFactory = npcFromPacketFactory;
_eifFileProvider = eifFileProvider;
}

public override bool HandlePacket(IPacket packet)
Expand All @@ -40,6 +42,7 @@ public override bool HandlePacket(IPacket packet)

if (packet.PeekByte() == 0xFF)
{
packet.ReadByte();
for (var i = 0; i < num_entities; i++)
{
var character = _characterFromPacketFactory.CreateCharacter(packet);
Expand All @@ -53,7 +56,7 @@ public override bool HandlePacket(IPacket packet)
}
}

while (packet.ReadPosition < packet.Length)
while (num_entities > 0 && packet.ReadPosition < packet.Length)
{
var npc = _npcFromPacketFactory.CreateNPC(packet);
_currentMapStateRepository.NPCs.RemoveWhere(n => n.Index == npc.Index);
Expand Down

0 comments on commit 278c31f

Please sign in to comment.