Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update session id logic #150

Merged
merged 11 commits into from
Mar 27, 2022
17 changes: 9 additions & 8 deletions EOBot/BotHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public async Task<AccountReply> CreateAccountAsync(string name, string password)
{
var accountActions = DependencyMaster.TypeRegistry[_botIndex].Resolve<IAccountActions>();
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<LoginReply> LoginToAccountAsync(string name, string password)
Expand Down Expand Up @@ -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
Expand All @@ -71,21 +72,21 @@ public async Task LoginToCharacterAsync(string name)

var fileRequestActions = DependencyMaster.TypeRegistry[_botIndex].Resolve<IFileRequestActions>();
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<AccountReply> ChangePasswordAsync(string name, string oldPass, string newPass)
Expand Down
8 changes: 4 additions & 4 deletions EOLib.Test/Net/FileTransfer/FileRequestServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 3 additions & 5 deletions EOLib/Domain/Account/AccountActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ public async Task<AccountReply> CheckAccountNameWithServer(string accountName)
var reply = (AccountReply)response.ReadShort();
if (reply >= AccountReply.OK_CodeRange)
{
_playerInfoRepository.AccountCreateID = (ushort)reply;
ethanmoffat marked this conversation as resolved.
Show resolved Hide resolved

// 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
Expand All @@ -90,10 +88,10 @@ public async Task<AccountReply> CheckAccountNameWithServer(string accountName)
return reply;
}

public async Task<AccountReply> CreateAccount(ICreateAccountParameters parameters)
public async Task<AccountReply> CreateAccount(ICreateAccountParameters parameters, short sessionID)
{
var createAccountPacket = new PacketBuilder(PacketFamily.Account, PacketAction.Create)
.AddShort((short)_playerInfoRepository.AccountCreateID)
.AddShort(sessionID)
.AddByte(255)
.AddBreakString(parameters.AccountName)
.AddBreakString(parameters.Password)
Expand Down Expand Up @@ -151,7 +149,7 @@ public interface IAccountActions

Task<AccountReply> CheckAccountNameWithServer(string accountName);

Task<AccountReply> CreateAccount(ICreateAccountParameters parameters);
Task<AccountReply> CreateAccount(ICreateAccountParameters parameters, short sessionID);

Task<AccountReply> ChangePassword(IChangePasswordParameters parameters);
}
Expand Down
4 changes: 2 additions & 2 deletions EOLib/Domain/Login/ILoginRequestGrantedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace EOLib.Domain.Login
{
public interface ILoginRequestGrantedData : ITranslatedData
{
short PlayerID { get; }
short SessionID { get; }
int CharacterID { get; }

short MapID { get; }
Expand Down Expand Up @@ -38,7 +38,7 @@ public interface ILoginRequestGrantedData : ITranslatedData
short JailMap { get; }
bool FirstTimePlayer { get; }

ILoginRequestGrantedData WithPlayerID(short playerID);
ILoginRequestGrantedData WithSessionID(short sessionID);
ILoginRequestGrantedData WithCharacterID(int characterID);
ILoginRequestGrantedData WithMapID(short mapID);
ILoginRequestGrantedData WithMapRID(IEnumerable<byte> mapRID);
Expand Down
14 changes: 7 additions & 7 deletions EOLib/Domain/Login/LoginActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public async Task<LoginReply> LoginToServer(ILoginParameters parameters)
return data.Response;
}

public async Task RequestCharacterLogin(ICharacter character)
public async Task<short> RequestCharacterLogin(ICharacter character)
{
var packet = new PacketBuilder(PacketFamily.Welcome, PacketAction.Request)
.AddInt(character.ID)
Expand All @@ -113,7 +113,6 @@ public async Task RequestCharacterLogin(ICharacter character)
.WithAdminLevel(data.AdminLevel)
.WithStats(data.CharacterStats);

_playerInfoRepository.PlayerID = data.PlayerID;
_playerInfoRepository.IsFirstTimePlayer = data.FirstTimePlayer;
_currentMapStateRepository.CurrentMapID = data.MapID;

Expand All @@ -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<CharacterLoginReply> CompleteCharacterLogin()
public async Task<CharacterLoginReply> CompleteCharacterLogin(short sessionID)
{
var packet = new PacketBuilder(PacketFamily.Welcome, PacketAction.Message)
.AddThree((ushort)_playerInfoRepository.PlayerID)
.AddThree((ushort)sessionID)
.AddInt(_characterRepository.MainCharacter.ID)
.Build();

Expand Down Expand Up @@ -164,7 +164,7 @@ public async Task<CharacterLoginReply> CompleteCharacterLogin()
.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)
Expand Down Expand Up @@ -201,8 +201,8 @@ public interface ILoginActions

Task<LoginReply> LoginToServer(ILoginParameters parameters);

Task RequestCharacterLogin(ICharacter character);
Task<short> RequestCharacterLogin(ICharacter character);

Task<CharacterLoginReply> CompleteCharacterLogin();
Task<CharacterLoginReply> CompleteCharacterLogin(short sessionID);
}
}
8 changes: 4 additions & 4 deletions EOLib/Domain/Login/LoginRequestGrantedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions EOLib/Domain/Login/PlayerInfoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public interface IPlayerInfoRepository

short PlayerID { get; set; }
ethanmoffat marked this conversation as resolved.
Show resolved Hide resolved

ushort AccountCreateID { get; set; }

bool IsFirstTimePlayer { get; set; }

bool PlayerIsInGame { get; set; }
Expand All @@ -25,8 +23,6 @@ public interface IPlayerInfoProvider

short PlayerID { get; }

ushort AccountCreateID { get; set; }

bool IsFirstTimePlayer { get; }

bool PlayerIsInGame { get; }
Expand All @@ -41,8 +37,6 @@ public sealed class PlayerInfoRepository : IPlayerInfoRepository, IPlayerInfoPro

public short PlayerID { get; set; }

public ushort AccountCreateID { get; set; }

public bool IsFirstTimePlayer { get; set; }

public bool PlayerIsInGame { get; set; }
Expand All @@ -52,7 +46,6 @@ public void ResetState()
LoggedInAccountName = "";
PlayerPassword = "";
PlayerID = 0;
AccountCreateID = 0;
IsFirstTimePlayer = false;
PlayerIsInGame = false;
}
Expand Down
2 changes: 1 addition & 1 deletion EOLib/Domain/Protocol/IInitializationData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public enum InitializationDataKey
SequenceByte2,
SendMultiple,
ReceiveMultiple,
ClientID,
PlayerID,
HashResponse,
//response: Out of Date
RequiredVersionNumber,
Expand Down
17 changes: 9 additions & 8 deletions EOLib/Domain/Protocol/InitReply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ public enum InitReply
ClientOutOfDate = 1,
Success = 2,
BannedFromServer = 3,
MapFile = 4,
ethanmoffat marked this conversation as resolved.
Show resolved Hide resolved
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
}
}
8 changes: 4 additions & 4 deletions EOLib/Domain/Protocol/InitializationSuccessData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}
Expand Down
11 changes: 9 additions & 2 deletions EOLib/Net/Connection/NetworkConnectionActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +22,8 @@ public class NetworkConnectionActions : INetworkConnectionActions
private readonly IPacketTranslator<IInitializationData> _initPacketTranslator;
private readonly INetworkClientFactory _networkClientFactory;
private readonly IPacketSendService _packetSendService;
private readonly IPlayerInfoRepository _playerInfoRepository;


public NetworkConnectionActions(INetworkClientRepository networkClientRepository,
IConnectionStateRepository connectionStateRepository,
Expand All @@ -30,7 +33,8 @@ public NetworkConnectionActions(INetworkClientRepository networkClientRepository
IHDSerialNumberService hdSerialNumberService,
IPacketTranslator<IInitializationData> initPacketTranslator,
INetworkClientFactory networkClientFactory,
IPacketSendService packetSendService)
IPacketSendService packetSendService,
IPlayerInfoRepository playerInfoRepository)
{
_networkClientRepository = networkClientRepository;
_connectionStateRepository = connectionStateRepository;
Expand All @@ -41,6 +45,7 @@ public NetworkConnectionActions(INetworkClientRepository networkClientRepository
_initPacketTranslator = initPacketTranslator;
_networkClientFactory = networkClientFactory;
_packetSendService = packetSendService;
_playerInfoRepository = playerInfoRepository;
}

public async Task<ConnectResult> ConnectToServer()
Expand Down Expand Up @@ -104,10 +109,12 @@ public async Task<IInitializationData> 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);
Expand Down
Loading