From 16b695303583e0cbd6ec7c7b1f21a7d0cc7064d4 Mon Sep 17 00:00:00 2001 From: sorokya Date: Fri, 25 Mar 2022 17:21:55 +0100 Subject: [PATCH 01/11] Swapped ClientID to PlayerID Renamed AccountCreateID to SessionID Use SessionID for Account, Character, and Welcome packets instead of PlayerID --- EOLib/Domain/Account/AccountActions.cs | 4 ++-- EOLib/Domain/Login/ILoginRequestGrantedData.cs | 4 ++-- EOLib/Domain/Login/LoginActions.cs | 4 ++-- EOLib/Domain/Login/LoginRequestGrantedData.cs | 8 ++++---- EOLib/Domain/Login/PlayerInfoRepository.cs | 8 ++++---- EOLib/Domain/Protocol/IInitializationData.cs | 2 +- EOLib/Domain/Protocol/InitReply.cs | 17 +++++++++-------- .../Protocol/InitializationSuccessData.cs | 8 ++++---- .../Net/Connection/NetworkConnectionActions.cs | 11 +++++++++-- .../LoginRequestGrantedPacketTranslator.cs | 4 ++-- 10 files changed, 39 insertions(+), 31 deletions(-) diff --git a/EOLib/Domain/Account/AccountActions.cs b/EOLib/Domain/Account/AccountActions.cs index 8fb8be073..a7fcd5aee 100644 --- a/EOLib/Domain/Account/AccountActions.cs +++ b/EOLib/Domain/Account/AccountActions.cs @@ -71,7 +71,7 @@ public async Task CheckAccountNameWithServer(string accountName) var reply = (AccountReply)response.ReadShort(); if (reply >= AccountReply.OK_CodeRange) { - _playerInfoRepository.AccountCreateID = (ushort)reply; + _playerInfoRepository.SessionID = (short)reply; // Based on patch: https://github.com/eoserv/eoserv/commit/80dde6d4e7f440a93503aeec79f4a2f5931dc13d // Account may change sequence start depending on the eoserv build being used @@ -93,7 +93,7 @@ public async Task CheckAccountNameWithServer(string accountName) public async Task CreateAccount(ICreateAccountParameters parameters) { var createAccountPacket = new PacketBuilder(PacketFamily.Account, PacketAction.Create) - .AddShort((short)_playerInfoRepository.AccountCreateID) + .AddShort((short)_playerInfoRepository.SessionID) .AddByte(255) .AddBreakString(parameters.AccountName) .AddBreakString(parameters.Password) diff --git a/EOLib/Domain/Login/ILoginRequestGrantedData.cs b/EOLib/Domain/Login/ILoginRequestGrantedData.cs index c93735f07..035097f35 100644 --- a/EOLib/Domain/Login/ILoginRequestGrantedData.cs +++ b/EOLib/Domain/Login/ILoginRequestGrantedData.cs @@ -6,7 +6,7 @@ namespace EOLib.Domain.Login { public interface ILoginRequestGrantedData : ITranslatedData { - short PlayerID { get; } + short SessionID { get; } int CharacterID { get; } short MapID { get; } @@ -38,7 +38,7 @@ public interface ILoginRequestGrantedData : ITranslatedData short JailMap { get; } bool FirstTimePlayer { get; } - ILoginRequestGrantedData WithPlayerID(short playerID); + ILoginRequestGrantedData WithSessionID(short playerID); ILoginRequestGrantedData WithCharacterID(int characterID); ILoginRequestGrantedData WithMapID(short mapID); ILoginRequestGrantedData WithMapRID(IEnumerable mapRID); diff --git a/EOLib/Domain/Login/LoginActions.cs b/EOLib/Domain/Login/LoginActions.cs index 4a9572939..ad4e2fa1f 100644 --- a/EOLib/Domain/Login/LoginActions.cs +++ b/EOLib/Domain/Login/LoginActions.cs @@ -113,7 +113,7 @@ public async Task RequestCharacterLogin(ICharacter character) .WithAdminLevel(data.AdminLevel) .WithStats(data.CharacterStats); - _playerInfoRepository.PlayerID = data.PlayerID; + _playerInfoRepository.SessionID = data.SessionID; _playerInfoRepository.IsFirstTimePlayer = data.FirstTimePlayer; _currentMapStateRepository.CurrentMapID = data.MapID; @@ -133,7 +133,7 @@ public async Task RequestCharacterLogin(ICharacter character) public async Task CompleteCharacterLogin() { var packet = new PacketBuilder(PacketFamily.Welcome, PacketAction.Message) - .AddThree((ushort)_playerInfoRepository.PlayerID) + .AddThree(_playerInfoRepository.SessionID) .AddInt(_characterRepository.MainCharacter.ID) .Build(); diff --git a/EOLib/Domain/Login/LoginRequestGrantedData.cs b/EOLib/Domain/Login/LoginRequestGrantedData.cs index a7926fa7a..bc43bfbb1 100644 --- a/EOLib/Domain/Login/LoginRequestGrantedData.cs +++ b/EOLib/Domain/Login/LoginRequestGrantedData.cs @@ -7,7 +7,7 @@ namespace EOLib.Domain.Login { public class LoginRequestGrantedData : ILoginRequestGrantedData { - public short PlayerID { get; private set; } + public short SessionID { get; private set; } public int CharacterID { get; private set; } public short MapID { get; private set; } @@ -40,10 +40,10 @@ public class LoginRequestGrantedData : ILoginRequestGrantedData public short JailMap { get; private set; } public bool FirstTimePlayer { get; private set; } - public ILoginRequestGrantedData WithPlayerID(short playerID) + public ILoginRequestGrantedData WithSessionID(short sessionID) { var copy = MakeCopy(this); - copy.PlayerID = playerID; + copy.SessionID = sessionID; return copy; } @@ -219,7 +219,7 @@ private static LoginRequestGrantedData MakeCopy(LoginRequestGrantedData source) { return new LoginRequestGrantedData { - PlayerID = source.PlayerID, + SessionID = source.SessionID, CharacterID = source.CharacterID, MapID = source.MapID, diff --git a/EOLib/Domain/Login/PlayerInfoRepository.cs b/EOLib/Domain/Login/PlayerInfoRepository.cs index 023b8443c..a798f3fa8 100644 --- a/EOLib/Domain/Login/PlayerInfoRepository.cs +++ b/EOLib/Domain/Login/PlayerInfoRepository.cs @@ -10,7 +10,7 @@ public interface IPlayerInfoRepository short PlayerID { get; set; } - ushort AccountCreateID { get; set; } + short SessionID { get; set; } bool IsFirstTimePlayer { get; set; } @@ -25,7 +25,7 @@ public interface IPlayerInfoProvider short PlayerID { get; } - ushort AccountCreateID { get; set; } + short SessionID { get; set; } bool IsFirstTimePlayer { get; } @@ -41,7 +41,7 @@ public sealed class PlayerInfoRepository : IPlayerInfoRepository, IPlayerInfoPro public short PlayerID { get; set; } - public ushort AccountCreateID { get; set; } + public short SessionID { get; set; } public bool IsFirstTimePlayer { get; set; } @@ -52,7 +52,7 @@ public void ResetState() LoggedInAccountName = ""; PlayerPassword = ""; PlayerID = 0; - AccountCreateID = 0; + SessionID = 0; IsFirstTimePlayer = false; PlayerIsInGame = false; } diff --git a/EOLib/Domain/Protocol/IInitializationData.cs b/EOLib/Domain/Protocol/IInitializationData.cs index 034511b5e..6b09764eb 100644 --- a/EOLib/Domain/Protocol/IInitializationData.cs +++ b/EOLib/Domain/Protocol/IInitializationData.cs @@ -16,7 +16,7 @@ public enum InitializationDataKey SequenceByte2, SendMultiple, ReceiveMultiple, - ClientID, + PlayerID, HashResponse, //response: Out of Date RequiredVersionNumber, diff --git a/EOLib/Domain/Protocol/InitReply.cs b/EOLib/Domain/Protocol/InitReply.cs index fd03a946b..efc2c4775 100644 --- a/EOLib/Domain/Protocol/InitReply.cs +++ b/EOLib/Domain/Protocol/InitReply.cs @@ -5,14 +5,15 @@ public enum InitReply ClientOutOfDate = 1, Success = 2, BannedFromServer = 3, - MapFile = 4, - ItemFile = 5, - NpcFile = 6, - SpellFile = 7, - AllPlayersList = 8, - MapMutation = 9, - FriendPlayersList = 10, - ClassFile = 11, + WarpMap = 4, + MapFile = 5, + ItemFile = 6, + NpcFile = 7, + SpellFile = 8, + AllPlayersList = 9, + MapMutation = 10, + FriendPlayersList = 11, + ClassFile = 12, ErrorState = 0 } } diff --git a/EOLib/Domain/Protocol/InitializationSuccessData.cs b/EOLib/Domain/Protocol/InitializationSuccessData.cs index 62f05b7cc..e44b8a4b3 100644 --- a/EOLib/Domain/Protocol/InitializationSuccessData.cs +++ b/EOLib/Domain/Protocol/InitializationSuccessData.cs @@ -9,21 +9,21 @@ public class InitializationSuccessData : IInitializationData public int this[InitializationDataKey key] => GetValueHelper(key); private readonly byte _seq1, _seq2, _sendMulti, _recvMulti; - private readonly short _clientID; + private readonly short _playerID; private readonly int _hashResponse; public InitializationSuccessData(byte sequence1, byte sequence2, byte receiveMultiple, byte sendMultiple, - short clientID, + short playerID, int hashResponse) { _seq1 = sequence1; _seq2 = sequence2; _recvMulti = receiveMultiple; _sendMulti = sendMultiple; - _clientID = clientID; + _playerID = playerID; _hashResponse = hashResponse; } @@ -35,7 +35,7 @@ private int GetValueHelper(InitializationDataKey key) case InitializationDataKey.SequenceByte2: return _seq2; case InitializationDataKey.SendMultiple: return _sendMulti; case InitializationDataKey.ReceiveMultiple: return _recvMulti; - case InitializationDataKey.ClientID: return _clientID; + case InitializationDataKey.PlayerID: return _playerID; case InitializationDataKey.HashResponse: return _hashResponse; default: throw new ArgumentOutOfRangeException(nameof(key), key, null); } diff --git a/EOLib/Net/Connection/NetworkConnectionActions.cs b/EOLib/Net/Connection/NetworkConnectionActions.cs index d31938fea..77b0917b4 100644 --- a/EOLib/Net/Connection/NetworkConnectionActions.cs +++ b/EOLib/Net/Connection/NetworkConnectionActions.cs @@ -2,6 +2,7 @@ using System.Threading.Tasks; using AutomaticTypeMapper; using EOLib.Config; +using EOLib.Domain.Login; using EOLib.Domain.Protocol; using EOLib.Net.Communication; using EOLib.Net.PacketProcessing; @@ -21,6 +22,8 @@ public class NetworkConnectionActions : INetworkConnectionActions private readonly IPacketTranslator _initPacketTranslator; private readonly INetworkClientFactory _networkClientFactory; private readonly IPacketSendService _packetSendService; + private readonly IPlayerInfoRepository _playerInfoRepository; + public NetworkConnectionActions(INetworkClientRepository networkClientRepository, IConnectionStateRepository connectionStateRepository, @@ -30,7 +33,8 @@ public NetworkConnectionActions(INetworkClientRepository networkClientRepository IHDSerialNumberService hdSerialNumberService, IPacketTranslator initPacketTranslator, INetworkClientFactory networkClientFactory, - IPacketSendService packetSendService) + IPacketSendService packetSendService, + IPlayerInfoRepository playerInfoRepository) { _networkClientRepository = networkClientRepository; _connectionStateRepository = connectionStateRepository; @@ -41,6 +45,7 @@ public NetworkConnectionActions(INetworkClientRepository networkClientRepository _initPacketTranslator = initPacketTranslator; _networkClientFactory = networkClientFactory; _packetSendService = packetSendService; + _playerInfoRepository = playerInfoRepository; } public async Task ConnectToServer() @@ -104,10 +109,12 @@ public async Task BeginHandshake() public void CompleteHandshake(IInitializationData initializationData) { + _playerInfoRepository.PlayerID = (short)initializationData[InitializationDataKey.PlayerID]; + var packet = new PacketBuilder(PacketFamily.Connection, PacketAction.Accept) .AddShort((short)initializationData[InitializationDataKey.SendMultiple]) .AddShort((short)initializationData[InitializationDataKey.ReceiveMultiple]) - .AddShort((short)initializationData[InitializationDataKey.ClientID]) + .AddShort(_playerInfoRepository.PlayerID) .Build(); _packetSendService.SendPacket(packet); diff --git a/EOLib/Net/Translators/LoginRequestGrantedPacketTranslator.cs b/EOLib/Net/Translators/LoginRequestGrantedPacketTranslator.cs index fb6a87365..7d2c7b92a 100644 --- a/EOLib/Net/Translators/LoginRequestGrantedPacketTranslator.cs +++ b/EOLib/Net/Translators/LoginRequestGrantedPacketTranslator.cs @@ -16,7 +16,7 @@ public ILoginRequestGrantedData TranslatePacket(IPacket packet) if (reply != CharacterLoginReply.RequestGranted) throw new MalformedPacketException("Unexpected welcome response in packet: " + reply, packet); - var playerID = packet.ReadShort(); + var sessionID = packet.ReadShort(); var characterID = packet.ReadInt(); var mapID = packet.ReadShort(); @@ -111,7 +111,7 @@ public ILoginRequestGrantedData TranslatePacket(IPacket packet) throw new MalformedPacketException("Missing terminating 255 byte", packet); return new LoginRequestGrantedData() - .WithPlayerID(playerID) + .WithSessionID(sessionID) .WithCharacterID(characterID) .WithMapID(mapID) .WithMapRID(mapRid) From ba4226d3708c12ef9c1624d1eeaa879cd49bfe38 Mon Sep 17 00:00:00 2001 From: sorokya Date: Sat, 26 Mar 2022 08:25:13 +0100 Subject: [PATCH 02/11] implement session id for login actions, warping, and file request service --- EOLib/Domain/Login/LoginActions.cs | 2 +- EOLib/Net/FileTransfer/FileRequestActions.cs | 16 ++++----- EOLib/Net/FileTransfer/FileRequestService.cs | 34 +++++++++++-------- .../PacketHandlers/BeginPlayerWarpHandler.cs | 16 +++++---- 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/EOLib/Domain/Login/LoginActions.cs b/EOLib/Domain/Login/LoginActions.cs index ad4e2fa1f..45d36dda1 100644 --- a/EOLib/Domain/Login/LoginActions.cs +++ b/EOLib/Domain/Login/LoginActions.cs @@ -133,7 +133,7 @@ public async Task RequestCharacterLogin(ICharacter character) public async Task CompleteCharacterLogin() { var packet = new PacketBuilder(PacketFamily.Welcome, PacketAction.Message) - .AddThree(_playerInfoRepository.SessionID) + .AddThree((ushort)_playerInfoRepository.SessionID) .AddInt(_characterRepository.MainCharacter.ID) .Build(); diff --git a/EOLib/Net/FileTransfer/FileRequestActions.cs b/EOLib/Net/FileTransfer/FileRequestActions.cs index 9b7f08261..cbcd54154 100644 --- a/EOLib/Net/FileTransfer/FileRequestActions.cs +++ b/EOLib/Net/FileTransfer/FileRequestActions.cs @@ -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(InitFileType.Item, _playerInfoProvider.PlayerID); + var itemFile = await _fileRequestService.RequestFile(InitFileType.Item, _playerInfoProvider.SessionID); _pubFileSaveService.SaveFile(PubFileNameConstants.PathToEIFFile, itemFile, rewriteChecksum: false); _pubFileRepository.EIFFile = (EIFFile)itemFile; } public async Task GetNPCFileFromServer() { - var npcFile = await _fileRequestService.RequestFile(InitFileType.Npc, _playerInfoProvider.PlayerID); + var npcFile = await _fileRequestService.RequestFile(InitFileType.Npc, _playerInfoProvider.SessionID); _pubFileSaveService.SaveFile(PubFileNameConstants.PathToENFFile, npcFile, rewriteChecksum: false); _pubFileRepository.ENFFile = (ENFFile)npcFile; } public async Task GetSpellFileFromServer() { - var spellFile = await _fileRequestService.RequestFile(InitFileType.Spell, _playerInfoProvider.PlayerID); + var spellFile = await _fileRequestService.RequestFile(InitFileType.Spell, _playerInfoProvider.SessionID); _pubFileSaveService.SaveFile(PubFileNameConstants.PathToESFFile, spellFile, rewriteChecksum: false); _pubFileRepository.ESFFile = (ESFFile)spellFile; } public async Task GetClassFileFromServer() { - var classFile = await _fileRequestService.RequestFile(InitFileType.Class, _playerInfoProvider.PlayerID); + var classFile = await _fileRequestService.RequestFile(InitFileType.Class, _playerInfoProvider.SessionID); _pubFileSaveService.SaveFile(PubFileNameConstants.PathToECFFile, classFile, rewriteChecksum: false); _pubFileRepository.ECFFile = (ECFFile)classFile; } @@ -153,7 +153,7 @@ public interface IFileRequestActions Task GetMapFromServer(short mapID); - Task GetMapForWarp(short mapID); + Task GetMapForWarp(short mapID, short sessionID); Task GetItemFileFromServer(); diff --git a/EOLib/Net/FileTransfer/FileRequestService.cs b/EOLib/Net/FileTransfer/FileRequestService.cs index 573aae5a8..5b8ae1a1c 100644 --- a/EOLib/Net/FileTransfer/FileRequestService.cs +++ b/EOLib/Net/FileTransfer/FileRequestService.cs @@ -26,29 +26,33 @@ public FileRequestService(IPacketSendService packetSendService, _pubFileDeserializer = pubFileDeserializer; } - public async Task RequestMapFile(short mapID, short playerID) + public async Task 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 RequestMapFileForWarp(short mapID) + public async Task 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> RequestFile(InitFileType fileType, short playerID) + public async Task> RequestFile(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(); @@ -56,7 +60,7 @@ public async Task> RequestFile(InitFileType fileType, if (!PacketIsValid(response)) throw new EmptyPacketReceivedException(); - var responseFileType = (InitReply) response.ReadChar(); + var responseFileType = (InitReply) response.ReadByte(); var extraByte = response.ReadChar(); if (extraByte != 1) @@ -79,14 +83,14 @@ public async Task> RequestFile(InitFileType fileType, return _pubFileDeserializer.DeserializeFromByteArray(responseBytes, factory); } - private async Task GetMapFile(IPacket request, int mapID, bool isWarp) + private async Task 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); @@ -106,11 +110,11 @@ private static bool PacketIsValid(IPacket packet) public interface IFileRequestService { - Task RequestMapFile(short mapID, short playerID); + Task RequestMapFile(short mapID, short sessionID); - Task RequestMapFileForWarp(short mapID); + Task RequestMapFileForWarp(short mapID, short sessionID); - Task> RequestFile(InitFileType fileType, short playerID) + Task> RequestFile(InitFileType fileType, short sessionID) where TRecord : class, IPubRecord, new(); } } \ No newline at end of file diff --git a/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs b/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs index 990d9a711..c836f13fc 100644 --- a/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs +++ b/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs @@ -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 @@ -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; @@ -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); } From 2a700df757c7d57d66cfb1e9f48da639a99a49f2 Mon Sep 17 00:00:00 2001 From: sorokya Date: Sat, 26 Mar 2022 08:30:44 +0100 Subject: [PATCH 03/11] remove un-needed cast --- EOLib/Domain/Account/AccountActions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EOLib/Domain/Account/AccountActions.cs b/EOLib/Domain/Account/AccountActions.cs index a7fcd5aee..c3e7f2b0e 100644 --- a/EOLib/Domain/Account/AccountActions.cs +++ b/EOLib/Domain/Account/AccountActions.cs @@ -93,7 +93,7 @@ public async Task CheckAccountNameWithServer(string accountName) public async Task CreateAccount(ICreateAccountParameters parameters) { var createAccountPacket = new PacketBuilder(PacketFamily.Account, PacketAction.Create) - .AddShort((short)_playerInfoRepository.SessionID) + .AddShort(_playerInfoRepository.SessionID) .AddByte(255) .AddBreakString(parameters.AccountName) .AddBreakString(parameters.Password) From c7fe494b52bb3c3795011bda3d353db66f8a98c3 Mon Sep 17 00:00:00 2001 From: sorokya Date: Sat, 26 Mar 2022 08:37:17 +0100 Subject: [PATCH 04/11] Update Packet test fixture to use byte for InitReply rather than char --- EOLib.Test/Net/FileTransfer/FileRequestServiceTest.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/EOLib.Test/Net/FileTransfer/FileRequestServiceTest.cs b/EOLib.Test/Net/FileTransfer/FileRequestServiceTest.cs index 8c3fa7fc6..97072da30 100644 --- a/EOLib.Test/Net/FileTransfer/FileRequestServiceTest.cs +++ b/EOLib.Test/Net/FileTransfer/FileRequestServiceTest.cs @@ -168,7 +168,7 @@ private static byte[] CreateFilePacket(InitFileType type) { case InitFileType.Item: packetBuilder = packetBuilder - .AddChar((byte) InitReply.ItemFile).AddChar(1) //spacer + .AddByte((byte) InitReply.ItemFile).AddChar(1) //spacer .AddString("EIF").AddInt(1) //RID .AddShort(2) //Len .AddByte(1) //filler byte @@ -177,7 +177,7 @@ private static byte[] CreateFilePacket(InitFileType type) break; case InitFileType.Npc: packetBuilder = packetBuilder - .AddChar((byte) InitReply.NpcFile).AddChar(1) //spacer + .AddByte((byte) InitReply.NpcFile).AddChar(1) //spacer .AddString("ENF").AddInt(1) //RID .AddShort(2) //Len .AddByte(1) //filler byte @@ -186,7 +186,7 @@ private static byte[] CreateFilePacket(InitFileType type) break; case InitFileType.Spell: packetBuilder = packetBuilder - .AddChar((byte) InitReply.SpellFile).AddChar(1) //spacer + .AddByte((byte) InitReply.SpellFile).AddChar(1) //spacer .AddString("ESF").AddInt(1) //RID .AddShort(2) //Len .AddByte(1) //filler byte @@ -195,7 +195,7 @@ private static byte[] CreateFilePacket(InitFileType type) break; case InitFileType.Class: packetBuilder = packetBuilder - .AddChar((byte) InitReply.ClassFile).AddChar(1) //spacer + .AddByte((byte) InitReply.ClassFile).AddChar(1) //spacer .AddString("ECF").AddInt(1) //RID .AddShort(2) //Len .AddByte(1) //filler byte From 763a76cb25b9ce6f2527e8c6e0084a5250de868a Mon Sep 17 00:00:00 2001 From: sorokya Date: Sat, 26 Mar 2022 08:49:48 +0100 Subject: [PATCH 05/11] create overload for AddShort that accepts ushort --- EOLib/Domain/Account/AccountActions.cs | 2 +- EOLib/Net/Connection/NetworkConnectionActions.cs | 2 +- EOLib/Net/FileTransfer/FileRequestService.cs | 6 +++--- EOLib/Net/IPacketBuilder.cs | 2 ++ EOLib/Net/PacketBuilder.cs | 7 ++++++- EOLib/PacketHandlers/BeginPlayerWarpHandler.cs | 2 +- 6 files changed, 14 insertions(+), 7 deletions(-) diff --git a/EOLib/Domain/Account/AccountActions.cs b/EOLib/Domain/Account/AccountActions.cs index c3e7f2b0e..ab93c494c 100644 --- a/EOLib/Domain/Account/AccountActions.cs +++ b/EOLib/Domain/Account/AccountActions.cs @@ -93,7 +93,7 @@ public async Task CheckAccountNameWithServer(string accountName) public async Task CreateAccount(ICreateAccountParameters parameters) { var createAccountPacket = new PacketBuilder(PacketFamily.Account, PacketAction.Create) - .AddShort(_playerInfoRepository.SessionID) + .AddShort((ushort)_playerInfoRepository.SessionID) .AddByte(255) .AddBreakString(parameters.AccountName) .AddBreakString(parameters.Password) diff --git a/EOLib/Net/Connection/NetworkConnectionActions.cs b/EOLib/Net/Connection/NetworkConnectionActions.cs index 77b0917b4..5327cfebd 100644 --- a/EOLib/Net/Connection/NetworkConnectionActions.cs +++ b/EOLib/Net/Connection/NetworkConnectionActions.cs @@ -114,7 +114,7 @@ public void CompleteHandshake(IInitializationData initializationData) var packet = new PacketBuilder(PacketFamily.Connection, PacketAction.Accept) .AddShort((short)initializationData[InitializationDataKey.SendMultiple]) .AddShort((short)initializationData[InitializationDataKey.ReceiveMultiple]) - .AddShort(_playerInfoRepository.PlayerID) + .AddShort((ushort)_playerInfoRepository.PlayerID) .Build(); _packetSendService.SendPacket(packet); diff --git a/EOLib/Net/FileTransfer/FileRequestService.cs b/EOLib/Net/FileTransfer/FileRequestService.cs index 5b8ae1a1c..1575897fe 100644 --- a/EOLib/Net/FileTransfer/FileRequestService.cs +++ b/EOLib/Net/FileTransfer/FileRequestService.cs @@ -30,7 +30,7 @@ public async Task RequestMapFile(short mapID, short sessionID) { var request = new PacketBuilder(PacketFamily.Welcome, PacketAction.Agree) .AddChar((byte)InitFileType.Map) - .AddShort(sessionID) + .AddShort((ushort)sessionID) .AddShort(mapID) .Build(); @@ -41,7 +41,7 @@ public async Task RequestMapFileForWarp(short mapID, short sessionID) { var request = new PacketBuilder(PacketFamily.Warp, PacketAction.Take) .AddShort(mapID) - .AddShort(sessionID) + .AddShort((ushort)sessionID) .Build(); return await GetMapFile(request, mapID); @@ -52,7 +52,7 @@ public async Task> RequestFile(InitFileType fileType, { var request = new PacketBuilder(PacketFamily.Welcome, PacketAction.Agree) .AddChar((byte)fileType) - .AddShort(sessionID) + .AddShort((ushort)sessionID) .AddChar(1) // file id (for chunking oversize pub files) .Build(); diff --git a/EOLib/Net/IPacketBuilder.cs b/EOLib/Net/IPacketBuilder.cs index 78fa5dd86..b546873d6 100644 --- a/EOLib/Net/IPacketBuilder.cs +++ b/EOLib/Net/IPacketBuilder.cs @@ -22,6 +22,8 @@ public interface IPacketBuilder IPacketBuilder AddShort(short s); + IPacketBuilder AddShort(ushort s); + IPacketBuilder AddThree(int t); IPacketBuilder AddInt(int i); diff --git a/EOLib/Net/PacketBuilder.cs b/EOLib/Net/PacketBuilder.cs index 94e615e89..721527717 100644 --- a/EOLib/Net/PacketBuilder.cs +++ b/EOLib/Net/PacketBuilder.cs @@ -67,7 +67,12 @@ public IPacketBuilder AddChar(byte b) public IPacketBuilder AddShort(short s) { - return AddBytes(_encoderService.EncodeNumber((ushort)s, 2)); + return AddBytes(_encoderService.EncodeNumber(s, 2)); + } + + public IPacketBuilder AddShort(ushort s) + { + return AddBytes(_encoderService.EncodeNumber(s, 2)); } public IPacketBuilder AddThree(int t) diff --git a/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs b/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs index c836f13fc..b05ec4562 100644 --- a/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs +++ b/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs @@ -88,7 +88,7 @@ private void SendWarpAcceptToServer(short mapID, short sessionID) { var response = new PacketBuilder(PacketFamily.Warp, PacketAction.Accept) .AddShort(mapID) - .AddShort(sessionID) + .AddShort((ushort)sessionID) .Build(); _packetSendService.SendPacket(response); } From 3c7929d23c2c73c47daefcbabbf8c8c399e262b7 Mon Sep 17 00:00:00 2001 From: sorokya Date: Sat, 26 Mar 2022 21:38:50 +0100 Subject: [PATCH 06/11] Drop sessionID from player info repository Use sessionID locally in Account controller and Login controller Replace GetMapForWarp with just a single GetMapFromServer function --- EOBot/BotHelper.cs | 17 ++++----- EOLib/Domain/Account/AccountActions.cs | 8 ++--- EOLib/Domain/Login/LoginActions.cs | 12 +++---- EOLib/Domain/Login/PlayerInfoRepository.cs | 7 ---- EOLib/Net/FileTransfer/FileRequestActions.cs | 36 ++++++++----------- .../PacketHandlers/BeginPlayerWarpHandler.cs | 2 +- .../Controllers/AccountController.cs | 6 +++- EndlessClient/Controllers/LoginController.cs | 14 ++++---- 8 files changed, 46 insertions(+), 56 deletions(-) diff --git a/EOBot/BotHelper.cs b/EOBot/BotHelper.cs index 5dce10c21..705746460 100644 --- a/EOBot/BotHelper.cs +++ b/EOBot/BotHelper.cs @@ -28,7 +28,8 @@ public async Task CreateAccountAsync(string name, string password) { var accountActions = DependencyMaster.TypeRegistry[_botIndex].Resolve(); var accParams = new CreateAccountParameters(name, password, password, name, name, name + "@eobot.net"); - return await accountActions.CreateAccount(accParams); + var nameResult = await accountActions.CheckAccountNameWithServer(name); + return await accountActions.CreateAccount(accParams, (short)nameResult); } public async Task LoginToAccountAsync(string name, string password) @@ -56,7 +57,7 @@ public async Task LoginToCharacterAsync(string name) await CreateCharacterAsync(name); var character = characters.Characters.Single(x => string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase)); - await loginActions.RequestCharacterLogin(character); + var sessionID = await loginActions.RequestCharacterLogin(character); var unableToLoadMap = false; try @@ -71,21 +72,21 @@ public async Task LoginToCharacterAsync(string name) var fileRequestActions = DependencyMaster.TypeRegistry[_botIndex].Resolve(); if (unableToLoadMap || fileRequestActions.NeedsFileForLogin(InitFileType.Map, mapStateProvider.CurrentMapID)) - await fileRequestActions.GetMapFromServer(mapStateProvider.CurrentMapID); + await fileRequestActions.GetMapFromServer(mapStateProvider.CurrentMapID, sessionID); if (fileRequestActions.NeedsFileForLogin(InitFileType.Item)) - await fileRequestActions.GetItemFileFromServer(); + await fileRequestActions.GetItemFileFromServer(sessionID); if (fileRequestActions.NeedsFileForLogin(InitFileType.Npc)) - await fileRequestActions.GetNPCFileFromServer(); + await fileRequestActions.GetNPCFileFromServer(sessionID); if (fileRequestActions.NeedsFileForLogin(InitFileType.Spell)) - await fileRequestActions.GetSpellFileFromServer(); + await fileRequestActions.GetSpellFileFromServer(sessionID); if (fileRequestActions.NeedsFileForLogin(InitFileType.Class)) - await fileRequestActions.GetClassFileFromServer(); + await fileRequestActions.GetClassFileFromServer(sessionID); - await loginActions.CompleteCharacterLogin(); + await loginActions.CompleteCharacterLogin(sessionID); } public async Task ChangePasswordAsync(string name, string oldPass, string newPass) diff --git a/EOLib/Domain/Account/AccountActions.cs b/EOLib/Domain/Account/AccountActions.cs index ab93c494c..db9159747 100644 --- a/EOLib/Domain/Account/AccountActions.cs +++ b/EOLib/Domain/Account/AccountActions.cs @@ -71,8 +71,6 @@ public async Task CheckAccountNameWithServer(string accountName) var reply = (AccountReply)response.ReadShort(); if (reply >= AccountReply.OK_CodeRange) { - _playerInfoRepository.SessionID = (short)reply; - // Based on patch: https://github.com/eoserv/eoserv/commit/80dde6d4e7f440a93503aeec79f4a2f5931dc13d // Account may change sequence start depending on the eoserv build being used // Official software always updates sequence number @@ -90,10 +88,10 @@ public async Task CheckAccountNameWithServer(string accountName) return reply; } - public async Task CreateAccount(ICreateAccountParameters parameters) + public async Task CreateAccount(ICreateAccountParameters parameters, short sessionID) { var createAccountPacket = new PacketBuilder(PacketFamily.Account, PacketAction.Create) - .AddShort((ushort)_playerInfoRepository.SessionID) + .AddShort((ushort)sessionID) .AddByte(255) .AddBreakString(parameters.AccountName) .AddBreakString(parameters.Password) @@ -151,7 +149,7 @@ public interface IAccountActions Task CheckAccountNameWithServer(string accountName); - Task CreateAccount(ICreateAccountParameters parameters); + Task CreateAccount(ICreateAccountParameters parameters, short sessionID); Task ChangePassword(IChangePasswordParameters parameters); } diff --git a/EOLib/Domain/Login/LoginActions.cs b/EOLib/Domain/Login/LoginActions.cs index 45d36dda1..65f105a8a 100644 --- a/EOLib/Domain/Login/LoginActions.cs +++ b/EOLib/Domain/Login/LoginActions.cs @@ -89,7 +89,7 @@ public async Task LoginToServer(ILoginParameters parameters) return data.Response; } - public async Task RequestCharacterLogin(ICharacter character) + public async Task RequestCharacterLogin(ICharacter character) { var packet = new PacketBuilder(PacketFamily.Welcome, PacketAction.Request) .AddInt(character.ID) @@ -113,7 +113,6 @@ public async Task RequestCharacterLogin(ICharacter character) .WithAdminLevel(data.AdminLevel) .WithStats(data.CharacterStats); - _playerInfoRepository.SessionID = data.SessionID; _playerInfoRepository.IsFirstTimePlayer = data.FirstTimePlayer; _currentMapStateRepository.CurrentMapID = data.MapID; @@ -128,12 +127,13 @@ public async Task RequestCharacterLogin(ICharacter character) _loginFileChecksumRepository.ESFLength = data.EsfLen; _loginFileChecksumRepository.ECFChecksum = data.EcfRid; _loginFileChecksumRepository.ECFLength = data.EcfLen; + return data.SessionID; } - public async Task CompleteCharacterLogin() + public async Task CompleteCharacterLogin(short sessionID) { var packet = new PacketBuilder(PacketFamily.Welcome, PacketAction.Message) - .AddThree((ushort)_playerInfoRepository.SessionID) + .AddThree((ushort)sessionID) .AddInt(_characterRepository.MainCharacter.ID) .Build(); @@ -201,8 +201,8 @@ public interface ILoginActions Task LoginToServer(ILoginParameters parameters); - Task RequestCharacterLogin(ICharacter character); + Task RequestCharacterLogin(ICharacter character); - Task CompleteCharacterLogin(); + Task CompleteCharacterLogin(short sessionID); } } \ No newline at end of file diff --git a/EOLib/Domain/Login/PlayerInfoRepository.cs b/EOLib/Domain/Login/PlayerInfoRepository.cs index a798f3fa8..a3aba345d 100644 --- a/EOLib/Domain/Login/PlayerInfoRepository.cs +++ b/EOLib/Domain/Login/PlayerInfoRepository.cs @@ -10,8 +10,6 @@ public interface IPlayerInfoRepository short PlayerID { get; set; } - short SessionID { get; set; } - bool IsFirstTimePlayer { get; set; } bool PlayerIsInGame { get; set; } @@ -25,8 +23,6 @@ public interface IPlayerInfoProvider short PlayerID { get; } - short SessionID { get; set; } - bool IsFirstTimePlayer { get; } bool PlayerIsInGame { get; } @@ -41,8 +37,6 @@ public sealed class PlayerInfoRepository : IPlayerInfoRepository, IPlayerInfoPro public short PlayerID { get; set; } - public short SessionID { get; set; } - public bool IsFirstTimePlayer { get; set; } public bool PlayerIsInGame { get; set; } @@ -52,7 +46,6 @@ public void ResetState() LoggedInAccountName = ""; PlayerPassword = ""; PlayerID = 0; - SessionID = 0; IsFirstTimePlayer = false; PlayerIsInGame = false; } diff --git a/EOLib/Net/FileTransfer/FileRequestActions.cs b/EOLib/Net/FileTransfer/FileRequestActions.cs index cbcd54154..c99b54092 100644 --- a/EOLib/Net/FileTransfer/FileRequestActions.cs +++ b/EOLib/Net/FileTransfer/FileRequestActions.cs @@ -58,42 +58,36 @@ public bool NeedsMapForWarp(short mapID, byte[] mapRid, int fileSize) return NeedMap(mapID, expectedChecksum, fileSize); } - public async Task GetMapFromServer(short mapID) - { - var mapFile = await _fileRequestService.RequestMapFile(mapID, _playerInfoProvider.SessionID); - SaveAndCacheMapFile(mapID, mapFile); - } - - public async Task GetMapForWarp(short mapID, short sessionID) + public async Task GetMapFromServer(short mapID, short sessionID) { var mapFile = await _fileRequestService.RequestMapFileForWarp(mapID, sessionID); SaveAndCacheMapFile(mapID, mapFile); } - public async Task GetItemFileFromServer() + public async Task GetItemFileFromServer(short sessionID) { - var itemFile = await _fileRequestService.RequestFile(InitFileType.Item, _playerInfoProvider.SessionID); + var itemFile = await _fileRequestService.RequestFile(InitFileType.Item, sessionID); _pubFileSaveService.SaveFile(PubFileNameConstants.PathToEIFFile, itemFile, rewriteChecksum: false); _pubFileRepository.EIFFile = (EIFFile)itemFile; } - public async Task GetNPCFileFromServer() + public async Task GetNPCFileFromServer(short sessionID) { - var npcFile = await _fileRequestService.RequestFile(InitFileType.Npc, _playerInfoProvider.SessionID); + var npcFile = await _fileRequestService.RequestFile(InitFileType.Npc, sessionID); _pubFileSaveService.SaveFile(PubFileNameConstants.PathToENFFile, npcFile, rewriteChecksum: false); _pubFileRepository.ENFFile = (ENFFile)npcFile; } - public async Task GetSpellFileFromServer() + public async Task GetSpellFileFromServer(short sessionID) { - var spellFile = await _fileRequestService.RequestFile(InitFileType.Spell, _playerInfoProvider.SessionID); + var spellFile = await _fileRequestService.RequestFile(InitFileType.Spell, sessionID); _pubFileSaveService.SaveFile(PubFileNameConstants.PathToESFFile, spellFile, rewriteChecksum: false); _pubFileRepository.ESFFile = (ESFFile)spellFile; } - public async Task GetClassFileFromServer() + public async Task GetClassFileFromServer(short sessionID) { - var classFile = await _fileRequestService.RequestFile(InitFileType.Class, _playerInfoProvider.SessionID); + var classFile = await _fileRequestService.RequestFile(InitFileType.Class, sessionID); _pubFileSaveService.SaveFile(PubFileNameConstants.PathToECFFile, classFile, rewriteChecksum: false); _pubFileRepository.ECFFile = (ECFFile)classFile; } @@ -151,16 +145,14 @@ public interface IFileRequestActions bool NeedsMapForWarp(short mapID, byte[] mapRid, int fileSize); - Task GetMapFromServer(short mapID); - - Task GetMapForWarp(short mapID, short sessionID); + Task GetMapFromServer(short mapID, short sessionID); - Task GetItemFileFromServer(); + Task GetItemFileFromServer(short sessionID); - Task GetNPCFileFromServer(); + Task GetNPCFileFromServer(short sessionID); - Task GetSpellFileFromServer(); + Task GetSpellFileFromServer(short sessionID); - Task GetClassFileFromServer(); + Task GetClassFileFromServer(short sessionID); } } \ No newline at end of file diff --git a/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs b/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs index b05ec4562..dfc95d62d 100644 --- a/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs +++ b/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs @@ -72,7 +72,7 @@ public override bool HandlePacket(IPacket packet) catch (IOException) { mapIsDownloaded = false; } if (!mapIsDownloaded || _fileRequestActions.NeedsMapForWarp(mapID, mapRid, fileSize)) - _fileRequestActions.GetMapForWarp(mapID, sessionID).Wait(5000); + _fileRequestActions.GetMapFromServer(mapID, sessionID).Wait(5000); SendWarpAcceptToServer(mapID, sessionID); break; diff --git a/EndlessClient/Controllers/AccountController.cs b/EndlessClient/Controllers/AccountController.cs index de01103b6..e04587460 100644 --- a/EndlessClient/Controllers/AccountController.cs +++ b/EndlessClient/Controllers/AccountController.cs @@ -60,7 +60,11 @@ public async Task CreateAccount(ICreateAccountParameters createAccountParameters return; var createAccountOperation = _networkOperationFactory.CreateSafeBlockingOperation( - async () => await _accountActions.CreateAccount(createAccountParameters), + async () => + { + short sessionID = (short)nameResult; + return await _accountActions.CreateAccount(createAccountParameters, sessionID); + }, SetInitialStateAndShowError, SetInitialStateAndShowError); if (!await createAccountOperation.Invoke()) diff --git a/EndlessClient/Controllers/LoginController.cs b/EndlessClient/Controllers/LoginController.cs index 3c6e6a099..da2664eac 100644 --- a/EndlessClient/Controllers/LoginController.cs +++ b/EndlessClient/Controllers/LoginController.cs @@ -104,6 +104,8 @@ public async Task LoginToCharacter(ICharacter character) if (!await requestCharacterLoginOperation.Invoke()) return; + var sessionID = requestCharacterLoginOperation.Result; + var unableToLoadMap = false; try { @@ -127,7 +129,7 @@ public async Task LoginToCharacter(ICharacter character) if (unableToLoadMap || _fileRequestActions.NeedsFileForLogin(InitFileType.Map, _currentMapStateProvider.CurrentMapID)) { gameLoadingDialog.SetState(GameLoadingDialogState.Map); - if (!await SafeGetFile(async () => await _fileRequestActions.GetMapFromServer(_currentMapStateProvider.CurrentMapID))) + if (!await SafeGetFile(async () => await _fileRequestActions.GetMapFromServer(_currentMapStateProvider.CurrentMapID, sessionID))) return; await Task.Delay(1000); } @@ -135,7 +137,7 @@ public async Task LoginToCharacter(ICharacter character) if (_fileRequestActions.NeedsFileForLogin(InitFileType.Item)) { gameLoadingDialog.SetState(GameLoadingDialogState.Item); - if (!await SafeGetFile(_fileRequestActions.GetItemFileFromServer)) + if (!await SafeGetFile(async () => await _fileRequestActions.GetItemFileFromServer(sessionID))) return; await Task.Delay(1000); } @@ -143,7 +145,7 @@ public async Task LoginToCharacter(ICharacter character) if (_fileRequestActions.NeedsFileForLogin(InitFileType.Npc)) { gameLoadingDialog.SetState(GameLoadingDialogState.NPC); - if (!await SafeGetFile(_fileRequestActions.GetNPCFileFromServer)) + if (!await SafeGetFile(async () => await _fileRequestActions.GetNPCFileFromServer(sessionID))) return; await Task.Delay(1000); } @@ -151,7 +153,7 @@ public async Task LoginToCharacter(ICharacter character) if (_fileRequestActions.NeedsFileForLogin(InitFileType.Spell)) { gameLoadingDialog.SetState(GameLoadingDialogState.Spell); - if (!await SafeGetFile(_fileRequestActions.GetSpellFileFromServer)) + if (!await SafeGetFile(async () => await _fileRequestActions.GetSpellFileFromServer(sessionID))) return; await Task.Delay(1000); } @@ -159,7 +161,7 @@ public async Task LoginToCharacter(ICharacter character) if (_fileRequestActions.NeedsFileForLogin(InitFileType.Class)) { gameLoadingDialog.SetState(GameLoadingDialogState.Class); - if (!await SafeGetFile(_fileRequestActions.GetClassFileFromServer)) + if (!await SafeGetFile(async () => await _fileRequestActions.GetClassFileFromServer(sessionID))) return; await Task.Delay(1000); } @@ -167,7 +169,7 @@ public async Task LoginToCharacter(ICharacter character) gameLoadingDialog.SetState(GameLoadingDialogState.LoadingGame); var completeCharacterLoginOperation = _networkOperationFactory.CreateSafeBlockingOperation( - _loginActions.CompleteCharacterLogin, + (async () => await _loginActions.CompleteCharacterLogin(sessionID)), SetInitialStateAndShowError, SetInitialStateAndShowError); if (!await completeCharacterLoginOperation.Invoke()) From 546fe80f40644ea01fcc5f272259d4ef5f5dca5c Mon Sep 17 00:00:00 2001 From: sorokya Date: Sat, 26 Mar 2022 21:43:27 +0100 Subject: [PATCH 07/11] re-add warp specific map requester --- EOLib/Net/FileTransfer/FileRequestActions.cs | 10 +++++++++- EOLib/PacketHandlers/BeginPlayerWarpHandler.cs | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/EOLib/Net/FileTransfer/FileRequestActions.cs b/EOLib/Net/FileTransfer/FileRequestActions.cs index c99b54092..f67a23208 100644 --- a/EOLib/Net/FileTransfer/FileRequestActions.cs +++ b/EOLib/Net/FileTransfer/FileRequestActions.cs @@ -58,12 +58,18 @@ public bool NeedsMapForWarp(short mapID, byte[] mapRid, int fileSize) return NeedMap(mapID, expectedChecksum, fileSize); } - public async Task GetMapFromServer(short mapID, short sessionID) + public async Task GetMapForWarp(short mapID, short sessionID) { var mapFile = await _fileRequestService.RequestMapFileForWarp(mapID, sessionID); SaveAndCacheMapFile(mapID, mapFile); } + public async Task GetMapFromServer(short mapID, short sessionID) + { + var mapFile = await _fileRequestService.RequestMapFile(mapID, sessionID); + SaveAndCacheMapFile(mapID, mapFile); + } + public async Task GetItemFileFromServer(short sessionID) { var itemFile = await _fileRequestService.RequestFile(InitFileType.Item, sessionID); @@ -145,6 +151,8 @@ public interface IFileRequestActions bool NeedsMapForWarp(short mapID, byte[] mapRid, int fileSize); + Task GetMapForWarp(short mapID, short sessionID); + Task GetMapFromServer(short mapID, short sessionID); Task GetItemFileFromServer(short sessionID); diff --git a/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs b/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs index dfc95d62d..b05ec4562 100644 --- a/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs +++ b/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs @@ -72,7 +72,7 @@ public override bool HandlePacket(IPacket packet) catch (IOException) { mapIsDownloaded = false; } if (!mapIsDownloaded || _fileRequestActions.NeedsMapForWarp(mapID, mapRid, fileSize)) - _fileRequestActions.GetMapFromServer(mapID, sessionID).Wait(5000); + _fileRequestActions.GetMapForWarp(mapID, sessionID).Wait(5000); SendWarpAcceptToServer(mapID, sessionID); break; From c7cabc7edb76cfacc881b591ac41832435f2d50f Mon Sep 17 00:00:00 2001 From: sorokya Date: Sat, 26 Mar 2022 21:52:56 +0100 Subject: [PATCH 08/11] remove AddShort overload cast s to ushort before encoding number --- EOLib/Domain/Account/AccountActions.cs | 2 +- EOLib/Net/Connection/NetworkConnectionActions.cs | 2 +- EOLib/Net/FileTransfer/FileRequestService.cs | 6 +++--- EOLib/Net/IPacketBuilder.cs | 2 -- EOLib/Net/PacketBuilder.cs | 7 +------ EOLib/PacketHandlers/BeginPlayerWarpHandler.cs | 2 +- 6 files changed, 7 insertions(+), 14 deletions(-) diff --git a/EOLib/Domain/Account/AccountActions.cs b/EOLib/Domain/Account/AccountActions.cs index db9159747..e6e19a7bb 100644 --- a/EOLib/Domain/Account/AccountActions.cs +++ b/EOLib/Domain/Account/AccountActions.cs @@ -91,7 +91,7 @@ public async Task CheckAccountNameWithServer(string accountName) public async Task CreateAccount(ICreateAccountParameters parameters, short sessionID) { var createAccountPacket = new PacketBuilder(PacketFamily.Account, PacketAction.Create) - .AddShort((ushort)sessionID) + .AddShort(sessionID) .AddByte(255) .AddBreakString(parameters.AccountName) .AddBreakString(parameters.Password) diff --git a/EOLib/Net/Connection/NetworkConnectionActions.cs b/EOLib/Net/Connection/NetworkConnectionActions.cs index 5327cfebd..77b0917b4 100644 --- a/EOLib/Net/Connection/NetworkConnectionActions.cs +++ b/EOLib/Net/Connection/NetworkConnectionActions.cs @@ -114,7 +114,7 @@ public void CompleteHandshake(IInitializationData initializationData) var packet = new PacketBuilder(PacketFamily.Connection, PacketAction.Accept) .AddShort((short)initializationData[InitializationDataKey.SendMultiple]) .AddShort((short)initializationData[InitializationDataKey.ReceiveMultiple]) - .AddShort((ushort)_playerInfoRepository.PlayerID) + .AddShort(_playerInfoRepository.PlayerID) .Build(); _packetSendService.SendPacket(packet); diff --git a/EOLib/Net/FileTransfer/FileRequestService.cs b/EOLib/Net/FileTransfer/FileRequestService.cs index 1575897fe..5b8ae1a1c 100644 --- a/EOLib/Net/FileTransfer/FileRequestService.cs +++ b/EOLib/Net/FileTransfer/FileRequestService.cs @@ -30,7 +30,7 @@ public async Task RequestMapFile(short mapID, short sessionID) { var request = new PacketBuilder(PacketFamily.Welcome, PacketAction.Agree) .AddChar((byte)InitFileType.Map) - .AddShort((ushort)sessionID) + .AddShort(sessionID) .AddShort(mapID) .Build(); @@ -41,7 +41,7 @@ public async Task RequestMapFileForWarp(short mapID, short sessionID) { var request = new PacketBuilder(PacketFamily.Warp, PacketAction.Take) .AddShort(mapID) - .AddShort((ushort)sessionID) + .AddShort(sessionID) .Build(); return await GetMapFile(request, mapID); @@ -52,7 +52,7 @@ public async Task> RequestFile(InitFileType fileType, { var request = new PacketBuilder(PacketFamily.Welcome, PacketAction.Agree) .AddChar((byte)fileType) - .AddShort((ushort)sessionID) + .AddShort(sessionID) .AddChar(1) // file id (for chunking oversize pub files) .Build(); diff --git a/EOLib/Net/IPacketBuilder.cs b/EOLib/Net/IPacketBuilder.cs index b546873d6..78fa5dd86 100644 --- a/EOLib/Net/IPacketBuilder.cs +++ b/EOLib/Net/IPacketBuilder.cs @@ -22,8 +22,6 @@ public interface IPacketBuilder IPacketBuilder AddShort(short s); - IPacketBuilder AddShort(ushort s); - IPacketBuilder AddThree(int t); IPacketBuilder AddInt(int i); diff --git a/EOLib/Net/PacketBuilder.cs b/EOLib/Net/PacketBuilder.cs index 721527717..94e615e89 100644 --- a/EOLib/Net/PacketBuilder.cs +++ b/EOLib/Net/PacketBuilder.cs @@ -67,12 +67,7 @@ public IPacketBuilder AddChar(byte b) public IPacketBuilder AddShort(short s) { - return AddBytes(_encoderService.EncodeNumber(s, 2)); - } - - public IPacketBuilder AddShort(ushort s) - { - return AddBytes(_encoderService.EncodeNumber(s, 2)); + return AddBytes(_encoderService.EncodeNumber((ushort)s, 2)); } public IPacketBuilder AddThree(int t) diff --git a/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs b/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs index b05ec4562..c836f13fc 100644 --- a/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs +++ b/EOLib/PacketHandlers/BeginPlayerWarpHandler.cs @@ -88,7 +88,7 @@ private void SendWarpAcceptToServer(short mapID, short sessionID) { var response = new PacketBuilder(PacketFamily.Warp, PacketAction.Accept) .AddShort(mapID) - .AddShort((ushort)sessionID) + .AddShort(sessionID) .Build(); _packetSendService.SendPacket(response); } From 13011a389322f331e7b273d2bdaac2718b5f785d Mon Sep 17 00:00:00 2001 From: sorokya Date: Sun, 27 Mar 2022 11:48:17 +0200 Subject: [PATCH 09/11] remove extra async/await when requesting files from server --- EndlessClient/Controllers/LoginController.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/EndlessClient/Controllers/LoginController.cs b/EndlessClient/Controllers/LoginController.cs index da2664eac..b6edd3143 100644 --- a/EndlessClient/Controllers/LoginController.cs +++ b/EndlessClient/Controllers/LoginController.cs @@ -129,7 +129,7 @@ public async Task LoginToCharacter(ICharacter character) if (unableToLoadMap || _fileRequestActions.NeedsFileForLogin(InitFileType.Map, _currentMapStateProvider.CurrentMapID)) { gameLoadingDialog.SetState(GameLoadingDialogState.Map); - if (!await SafeGetFile(async () => await _fileRequestActions.GetMapFromServer(_currentMapStateProvider.CurrentMapID, sessionID))) + if (!await SafeGetFile(() => _fileRequestActions.GetMapFromServer(_currentMapStateProvider.CurrentMapID, sessionID))) return; await Task.Delay(1000); } @@ -137,7 +137,7 @@ public async Task LoginToCharacter(ICharacter character) if (_fileRequestActions.NeedsFileForLogin(InitFileType.Item)) { gameLoadingDialog.SetState(GameLoadingDialogState.Item); - if (!await SafeGetFile(async () => await _fileRequestActions.GetItemFileFromServer(sessionID))) + if (!await SafeGetFile(() => _fileRequestActions.GetItemFileFromServer(sessionID))) return; await Task.Delay(1000); } @@ -145,7 +145,7 @@ public async Task LoginToCharacter(ICharacter character) if (_fileRequestActions.NeedsFileForLogin(InitFileType.Npc)) { gameLoadingDialog.SetState(GameLoadingDialogState.NPC); - if (!await SafeGetFile(async () => await _fileRequestActions.GetNPCFileFromServer(sessionID))) + if (!await SafeGetFile(() => _fileRequestActions.GetNPCFileFromServer(sessionID))) return; await Task.Delay(1000); } @@ -153,7 +153,7 @@ public async Task LoginToCharacter(ICharacter character) if (_fileRequestActions.NeedsFileForLogin(InitFileType.Spell)) { gameLoadingDialog.SetState(GameLoadingDialogState.Spell); - if (!await SafeGetFile(async () => await _fileRequestActions.GetSpellFileFromServer(sessionID))) + if (!await SafeGetFile(() => _fileRequestActions.GetSpellFileFromServer(sessionID))) return; await Task.Delay(1000); } @@ -161,7 +161,7 @@ public async Task LoginToCharacter(ICharacter character) if (_fileRequestActions.NeedsFileForLogin(InitFileType.Class)) { gameLoadingDialog.SetState(GameLoadingDialogState.Class); - if (!await SafeGetFile(async () => await _fileRequestActions.GetClassFileFromServer(sessionID))) + if (!await SafeGetFile(() => _fileRequestActions.GetClassFileFromServer(sessionID))) return; await Task.Delay(1000); } From 23b4e495aef9b98d9abe4f768332d75f9b23f3ac Mon Sep 17 00:00:00 2001 From: sorokya Date: Sun, 27 Mar 2022 11:52:06 +0200 Subject: [PATCH 10/11] rename playerID->sessionID --- EOLib/Domain/Login/ILoginRequestGrantedData.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EOLib/Domain/Login/ILoginRequestGrantedData.cs b/EOLib/Domain/Login/ILoginRequestGrantedData.cs index 035097f35..b9058a2ed 100644 --- a/EOLib/Domain/Login/ILoginRequestGrantedData.cs +++ b/EOLib/Domain/Login/ILoginRequestGrantedData.cs @@ -38,7 +38,7 @@ public interface ILoginRequestGrantedData : ITranslatedData short JailMap { get; } bool FirstTimePlayer { get; } - ILoginRequestGrantedData WithSessionID(short playerID); + ILoginRequestGrantedData WithSessionID(short sessionID); ILoginRequestGrantedData WithCharacterID(int characterID); ILoginRequestGrantedData WithMapID(short mapID); ILoginRequestGrantedData WithMapRID(IEnumerable mapRID); From 08a9b2bc456fd04aa1325cdfc8c796a0f0fc7581 Mon Sep 17 00:00:00 2001 From: sorokya Date: Sun, 27 Mar 2022 12:04:01 +0200 Subject: [PATCH 11/11] Overwrite character id with player id when logging in --- EOLib/Domain/Login/LoginActions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EOLib/Domain/Login/LoginActions.cs b/EOLib/Domain/Login/LoginActions.cs index 65f105a8a..606b18b87 100644 --- a/EOLib/Domain/Login/LoginActions.cs +++ b/EOLib/Domain/Login/LoginActions.cs @@ -164,7 +164,7 @@ public async Task CompleteCharacterLogin(short sessionID) .WithNewStat(CharacterStat.MaxTP, mainCharacter.Stats[CharacterStat.MaxTP]); _characterRepository.MainCharacter = _characterRepository.MainCharacter - .WithID(mainCharacter.ID) + .WithID(_playerInfoRepository.PlayerID) .WithName(mainCharacter.Name) .WithMapID(mainCharacter.MapID) .WithGuildTag(mainCharacter.GuildTag)