Skip to content

Commit

Permalink
implement session id for login actions, warping, and file request ser…
Browse files Browse the repository at this point in the history
…vice
  • Loading branch information
sorokya committed Mar 27, 2022
1 parent 16b6953 commit ba4226d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
2 changes: 1 addition & 1 deletion EOLib/Domain/Login/LoginActions.cs
Expand Up @@ -133,7 +133,7 @@ public async Task RequestCharacterLogin(ICharacter character)
public async Task<CharacterLoginReply> CompleteCharacterLogin()
{
var packet = new PacketBuilder(PacketFamily.Welcome, PacketAction.Message)
.AddThree(_playerInfoRepository.SessionID)
.AddThree((ushort)_playerInfoRepository.SessionID)
.AddInt(_characterRepository.MainCharacter.ID)
.Build();

Expand Down
16 changes: 8 additions & 8 deletions EOLib/Net/FileTransfer/FileRequestActions.cs
Expand Up @@ -60,40 +60,40 @@ public bool NeedsMapForWarp(short mapID, byte[] mapRid, int fileSize)

public async Task GetMapFromServer(short mapID)
{
var mapFile = await _fileRequestService.RequestMapFile(mapID, _playerInfoProvider.PlayerID);
var mapFile = await _fileRequestService.RequestMapFile(mapID, _playerInfoProvider.SessionID);
SaveAndCacheMapFile(mapID, mapFile);
}

public async Task GetMapForWarp(short mapID)
public async Task GetMapForWarp(short mapID, short sessionID)
{
var mapFile = await _fileRequestService.RequestMapFileForWarp(mapID);
var mapFile = await _fileRequestService.RequestMapFileForWarp(mapID, sessionID);
SaveAndCacheMapFile(mapID, mapFile);
}

public async Task GetItemFileFromServer()
{
var itemFile = await _fileRequestService.RequestFile<EIFRecord>(InitFileType.Item, _playerInfoProvider.PlayerID);
var itemFile = await _fileRequestService.RequestFile<EIFRecord>(InitFileType.Item, _playerInfoProvider.SessionID);
_pubFileSaveService.SaveFile(PubFileNameConstants.PathToEIFFile, itemFile, rewriteChecksum: false);
_pubFileRepository.EIFFile = (EIFFile)itemFile;
}

public async Task GetNPCFileFromServer()
{
var npcFile = await _fileRequestService.RequestFile<ENFRecord>(InitFileType.Npc, _playerInfoProvider.PlayerID);
var npcFile = await _fileRequestService.RequestFile<ENFRecord>(InitFileType.Npc, _playerInfoProvider.SessionID);
_pubFileSaveService.SaveFile(PubFileNameConstants.PathToENFFile, npcFile, rewriteChecksum: false);
_pubFileRepository.ENFFile = (ENFFile)npcFile;
}

public async Task GetSpellFileFromServer()
{
var spellFile = await _fileRequestService.RequestFile<ESFRecord>(InitFileType.Spell, _playerInfoProvider.PlayerID);
var spellFile = await _fileRequestService.RequestFile<ESFRecord>(InitFileType.Spell, _playerInfoProvider.SessionID);
_pubFileSaveService.SaveFile(PubFileNameConstants.PathToESFFile, spellFile, rewriteChecksum: false);
_pubFileRepository.ESFFile = (ESFFile)spellFile;
}

public async Task GetClassFileFromServer()
{
var classFile = await _fileRequestService.RequestFile<ECFRecord>(InitFileType.Class, _playerInfoProvider.PlayerID);
var classFile = await _fileRequestService.RequestFile<ECFRecord>(InitFileType.Class, _playerInfoProvider.SessionID);
_pubFileSaveService.SaveFile(PubFileNameConstants.PathToECFFile, classFile, rewriteChecksum: false);
_pubFileRepository.ECFFile = (ECFFile)classFile;
}
Expand Down Expand Up @@ -153,7 +153,7 @@ public interface IFileRequestActions

Task GetMapFromServer(short mapID);

Task GetMapForWarp(short mapID);
Task GetMapForWarp(short mapID, short sessionID);

Task GetItemFileFromServer();

Expand Down
34 changes: 19 additions & 15 deletions EOLib/Net/FileTransfer/FileRequestService.cs
Expand Up @@ -26,37 +26,41 @@ public class FileRequestService : IFileRequestService
_pubFileDeserializer = pubFileDeserializer;
}

public async Task<IMapFile> RequestMapFile(short mapID, short playerID)
public async Task<IMapFile> RequestMapFile(short mapID, short sessionID)
{
var request = new PacketBuilder(PacketFamily.Welcome, PacketAction.Agree)
.AddChar((byte)InitFileType.Map)
.AddShort(playerID)
.AddShort(sessionID)
.AddShort(mapID)
.Build();

return await GetMapFile(request, mapID, false);
return await GetMapFile(request, mapID);
}

public async Task<IMapFile> RequestMapFileForWarp(short mapID)
public async Task<IMapFile> RequestMapFileForWarp(short mapID, short sessionID)
{
var request = new PacketBuilder(PacketFamily.Warp, PacketAction.Take).Build();
return await GetMapFile(request, mapID, true);
var request = new PacketBuilder(PacketFamily.Warp, PacketAction.Take)
.AddShort(mapID)
.AddShort(sessionID)
.Build();

return await GetMapFile(request, mapID);
}

public async Task<IPubFile<TRecord>> RequestFile<TRecord>(InitFileType fileType, short playerID)
public async Task<IPubFile<TRecord>> RequestFile<TRecord>(InitFileType fileType, short sessionID)
where TRecord : class, IPubRecord, new()
{
var request = new PacketBuilder(PacketFamily.Welcome, PacketAction.Agree)
.AddChar((byte)fileType)
.AddShort(playerID)
.AddShort(sessionID)
.AddChar(1) // file id (for chunking oversize pub files)
.Build();

var response = await _packetSendService.SendEncodedPacketAndWaitAsync(request);
if (!PacketIsValid(response))
throw new EmptyPacketReceivedException();

var responseFileType = (InitReply) response.ReadChar();
var responseFileType = (InitReply) response.ReadByte();

var extraByte = response.ReadChar();
if (extraByte != 1)
Expand All @@ -79,14 +83,14 @@ public async Task<IPubFile<TRecord>> RequestFile<TRecord>(InitFileType fileType,
return _pubFileDeserializer.DeserializeFromByteArray(responseBytes, factory);
}

private async Task<IMapFile> GetMapFile(IPacket request, int mapID, bool isWarp)
private async Task<IMapFile> GetMapFile(IPacket request, int mapID)
{
var response = await _packetSendService.SendEncodedPacketAndWaitAsync(request);
if (!PacketIsValid(response))
throw new EmptyPacketReceivedException();

var fileType = (InitReply)(isWarp ? response.ReadByte() : response.ReadChar());
if (fileType != InitReply.MapFile)
var fileType = (InitReply)response.ReadByte();
if (fileType != InitReply.MapFile && fileType != InitReply.WarpMap)
throw new MalformedPacketException("Invalid file type " + fileType + " when requesting a map file", response);

var fileData = response.ReadBytes(response.Length - response.ReadPosition);
Expand All @@ -106,11 +110,11 @@ private static bool PacketIsValid(IPacket packet)

public interface IFileRequestService
{
Task<IMapFile> RequestMapFile(short mapID, short playerID);
Task<IMapFile> RequestMapFile(short mapID, short sessionID);

Task<IMapFile> RequestMapFileForWarp(short mapID);
Task<IMapFile> RequestMapFileForWarp(short mapID, short sessionID);

Task<IPubFile<TRecord>> RequestFile<TRecord>(InitFileType fileType, short playerID)
Task<IPubFile<TRecord>> RequestFile<TRecord>(InitFileType fileType, short sessionID)
where TRecord : class, IPubRecord, new();
}
}
16 changes: 10 additions & 6 deletions EOLib/PacketHandlers/BeginPlayerWarpHandler.cs
Expand Up @@ -50,15 +50,18 @@ public override bool HandlePacket(IPacket packet)
_mapStateRepository.MapWarpState = WarpState.WarpStarted;

var warpType = packet.ReadChar();
var mapID = packet.ReadShort();
short sessionID;
switch (warpType)
{
case WARP_SAME_MAP:
SendWarpAcceptToServer(packet);
sessionID = packet.ReadShort();
SendWarpAcceptToServer(mapID, sessionID);
break;
case WARP_NEW_MAP:
var mapID = packet.ReadShort();
var mapRid = packet.ReadBytes(4).ToArray();
var fileSize = packet.ReadThree();
sessionID = packet.ReadShort();

var mapIsDownloaded = true;
try
Expand All @@ -69,9 +72,9 @@ public override bool HandlePacket(IPacket packet)
catch (IOException) { mapIsDownloaded = false; }

if (!mapIsDownloaded || _fileRequestActions.NeedsMapForWarp(mapID, mapRid, fileSize))
_fileRequestActions.GetMapForWarp(mapID).Wait(5000);
_fileRequestActions.GetMapForWarp(mapID, sessionID).Wait(5000);

SendWarpAcceptToServer(packet);
SendWarpAcceptToServer(mapID, sessionID);
break;
default:
_mapStateRepository.MapWarpState = WarpState.None;
Expand All @@ -81,10 +84,11 @@ public override bool HandlePacket(IPacket packet)
return true;
}

private void SendWarpAcceptToServer(IPacket packet)
private void SendWarpAcceptToServer(short mapID, short sessionID)
{
var response = new PacketBuilder(PacketFamily.Warp, PacketAction.Accept)
.AddShort(packet.ReadShort())
.AddShort(mapID)
.AddShort(sessionID)
.Build();
_packetSendService.SendPacket(response);
}
Expand Down

0 comments on commit ba4226d

Please sign in to comment.