diff --git a/l2-unity/Assets/Scripts/Game/Manager/GameManager.cs b/l2-unity/Assets/Scripts/Game/Manager/GameManager.cs index 65aebd190..2389e1ae1 100644 --- a/l2-unity/Assets/Scripts/Game/Manager/GameManager.cs +++ b/l2-unity/Assets/Scripts/Game/Manager/GameManager.cs @@ -3,19 +3,20 @@ using UnityEngine; using static ServerListPacket; -public class GameManager : MonoBehaviour -{ +public class GameManager : MonoBehaviour { + [SerializeField] private int _protocolVersion = 1; [SerializeField] private GameState _gameState = GameState.LOGIN_SCREEN; private bool _gameReady = false; - public GameState GameState { - get { return _gameState; } + public GameState GameState { + get { return _gameState; } set { _gameState = value; Debug.Log($"Game state is now {_gameState}."); } } public bool GameReady { get { return _gameReady; } set { _gameReady = value; } } + public int ProtocolVersion { get { return _protocolVersion; } } private static GameManager _instance; public static GameManager Instance { get { return _instance; } } @@ -74,6 +75,14 @@ public void OnLoginServerAuthAllowed() { L2LoginUI.Instance.ShowLicenseWindow(); } + public void OnLoginServerPlayOk() { + GameState = GameState.READY_TO_CONNECT; + } + + public void OnConnectingToGameServer() { + GameState = GameState.CONNECTING_TO_GAMESERVER; + } + public void OnReceivedServerList(byte lastServer, List serverData, Dictionary charsOnServers) { GameState = GameState.SERVER_LIST; @@ -119,10 +128,10 @@ public void OnRelogin() { } public void OnDisconnect() { - if(GameState > GameState.CHAR_CREATION) { + if (GameState > GameState.CHAR_CREATION) { MusicManager.Instance.Clear(); SceneLoader.Instance.LoadMenu(); - } else if(GameState > GameState.LOGIN_SCREEN) { + } else if(GameState > GameState.LOGIN_SCREEN && GameState != GameState.READY_TO_CONNECT) { OnRelogin(); } } diff --git a/l2-unity/Assets/Scripts/Game/Manager/GameState.cs b/l2-unity/Assets/Scripts/Game/Manager/GameState.cs index 672077b41..fd317c937 100644 --- a/l2-unity/Assets/Scripts/Game/Manager/GameState.cs +++ b/l2-unity/Assets/Scripts/Game/Manager/GameState.cs @@ -6,8 +6,12 @@ public enum GameState : byte { LOGIN_SCREEN = 0, LOGIN_CONNECTED = 1, READING_LICENSE = 2, - SERVER_LIST = 3, - CHAR_SELECT = 4, - CHAR_CREATION = 5, - IN_GAME = 6 + SERVER_LIST = 3, + READY_TO_CONNECT = 4, + CONNECTING_TO_GAMESERVER = 5, + GAMESERVER_CONNECTED = 6, + GAMESERVER_AUTHED = 7, + CHAR_SELECT = 8, + CHAR_CREATION = 9, + IN_GAME = 10 } \ No newline at end of file diff --git a/l2-unity/Assets/Scripts/Networking/ClientLibrary/AsynchronousClient.cs b/l2-unity/Assets/Scripts/Networking/ClientLibrary/AsynchronousClient.cs index bbc045bd6..7f25b3be2 100644 --- a/l2-unity/Assets/Scripts/Networking/ClientLibrary/AsynchronousClient.cs +++ b/l2-unity/Assets/Scripts/Networking/ClientLibrary/AsynchronousClient.cs @@ -15,46 +15,23 @@ public class AsynchronousClient { private ServerPacketHandler _serverPacketHandler; private DefaultClient _client; - // Crypt - public static byte[] STATIC_BLOWFISH_KEY = { - (byte) 0x6b, - (byte) 0x60, - (byte) 0xcb, - (byte) 0x5b, - (byte) 0x82, - (byte) 0xce, - (byte) 0x90, - (byte) 0xb1, - (byte) 0xcc, - (byte) 0x2b, - (byte) 0x6c, - (byte) 0x55, - (byte) 0x6c, - (byte) 0x6c, - (byte) 0x6c, - (byte) 0x6c - }; private bool _initPacket = true; private RSACrypt _rsa; private byte[] _blowfishKey; private BlowfishEngine _decryptBlowfish; private BlowfishEngine _encryptBlowfish; - private int _sessionKey1; - private int _sessionKey2; - public bool InitPacket { get { return _initPacket; } set { _initPacket = value; } } public RSACrypt RSACrypt { get { return _rsa; } } public BlowfishEngine DecryptBlowFish { get { return _decryptBlowfish; } } public BlowfishEngine EncryptBlowFish { get { return _encryptBlowfish; } } - public int SessionKey1 { get { return _sessionKey1; } set { _sessionKey1 = value; } } - public int SessionKey2 { get { return _sessionKey2; } set { _sessionKey2 = value; } } + public byte[] BlowfishKey { get { return _blowfishKey; } } public int Ping { get; set; } - public AsynchronousClient(string ip, int port, DefaultClient client, ClientPacketHandler clientPacketHandler, ServerPacketHandler serverPacketHandler) { - SetBlowFishKey(STATIC_BLOWFISH_KEY); + public AsynchronousClient(string ip, int port, DefaultClient client, ClientPacketHandler clientPacketHandler, + ServerPacketHandler serverPacketHandler, bool enableInitPacket) { _ipAddress = ip; _port = port; _clientPacketHandler = clientPacketHandler; @@ -62,7 +39,7 @@ public AsynchronousClient(string ip, int port, DefaultClient client, ClientPacke _clientPacketHandler.SetClient(this); _serverPacketHandler.SetClient(this, _clientPacketHandler); _client = client; - + _initPacket = enableInitPacket; } public void SetBlowFishKey(byte[] blowfishKey) { diff --git a/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ClientPackets/PingPacket.cs b/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ClientPackets/PingPacket.cs index 4c38b1265..98c574673 100644 --- a/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ClientPackets/PingPacket.cs +++ b/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ClientPackets/PingPacket.cs @@ -1,5 +1,6 @@ public class PingPacket : ClientPacket { public PingPacket() : base((byte)GameClientPacketType.Ping) { SetData(new byte[] { (byte)GameClientPacketType.Ping, 0x02}); + BuildPacket(); } } \ No newline at end of file diff --git a/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ClientPackets/ProtocolVersionPacket.cs b/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ClientPackets/ProtocolVersionPacket.cs new file mode 100644 index 000000000..8c7d5b474 --- /dev/null +++ b/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ClientPackets/ProtocolVersionPacket.cs @@ -0,0 +1,12 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class ProtocolVersionPacket : ClientPacket +{ + public ProtocolVersionPacket(int version) : base((byte)GameClientPacketType.ProtocolVersion) { + WriteI(version); + + BuildPacket(); + } +} diff --git a/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ServerPackets/AuhRespondePacket.cs.meta b/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ClientPackets/ProtocolVersionPacket.cs.meta similarity index 83% rename from l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ServerPackets/AuhRespondePacket.cs.meta rename to l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ClientPackets/ProtocolVersionPacket.cs.meta index f117a991f..b1999dc4a 100644 --- a/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ServerPackets/AuhRespondePacket.cs.meta +++ b/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ClientPackets/ProtocolVersionPacket.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 628c7fd56ea31e04fa5ce9ec5f6b1be5 +guid: b5fc134b114442a43b6101574aedeb40 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/GameClientPacketType.cs b/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/GameClientPacketType.cs index 746b86647..ff3a5ceb2 100644 --- a/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/GameClientPacketType.cs +++ b/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/GameClientPacketType.cs @@ -1,14 +1,15 @@ public enum GameClientPacketType : byte { Ping = 0x00, - AuthRequest = 0x01, - SendMessage = 0x02, - RequestMove = 0x03, - LoadWorld = 0x04, - RequestRotate = 0x05, - RequestAnim = 0x06, - RequestAttack = 0x07, - RequestMoveDirection = 0x08, - RequestSetTarget = 0x09, - RequestAutoAttack = 0x0A + ProtocolVersion = 0x01, + AuthRequest = 0x02, + SendMessage = 0x03, + RequestMove = 0x04, + LoadWorld = 0x05, + RequestRotate = 0x06, + RequestAnim = 0x07, + RequestAttack = 0x08, + RequestMoveDirection = 0x09, + RequestSetTarget = 0x0A, + RequestAutoAttack = 0x0B } diff --git a/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/GameServerPacketType.cs b/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/GameServerPacketType.cs index 5c7b91000..a7a332f3e 100644 --- a/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/GameServerPacketType.cs +++ b/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/GameServerPacketType.cs @@ -1,22 +1,23 @@ public enum GameServerPacketType : byte { - Ping = 0, - AuthResponse = 1, - MessagePacket = 2, - SystemMessage = 3, - PlayerInfo = 4, - ObjectPosition = 5, - RemoveObject = 6, - ObjectRotation = 7, - ObjectAnimation = 8, - ApplyDamage = 9, - NpcInfo = 0x0A, - ObjectMoveTo = 0x0B, - UserInfo = 0x0C, - ObjectMoveDirection = 0x0D, - GameTime = 0x0E, - EntitySetTarget = 0x0F, - AutoAttackStart = 0x10, - AutoAttackStop = 0x11, - ActionFailed = 0x12 + Ping = 0x00, + Key = 0x01, + CharSelectionInfo = 0x02, + MessagePacket = 0x03, + SystemMessage = 0x04, + PlayerInfo = 0x05, + ObjectPosition = 0x06, + RemoveObject = 0x07, + ObjectRotation = 0x08, + ObjectAnimation = 0x09, + ApplyDamage = 0x0A, + NpcInfo = 0x0B, + ObjectMoveTo = 0x0C, + UserInfo = 0x0D, + ObjectMoveDirection = 0x0E, + GameTime = 0x0F, + EntitySetTarget = 0x10, + AutoAttackStart = 0x11, + AutoAttackStop = 0x12, + ActionFailed = 0x13 } diff --git a/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ServerPackets/AuhRespondePacket.cs b/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ServerPackets/AuhRespondePacket.cs deleted file mode 100644 index 4e07cf862..000000000 --- a/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ServerPackets/AuhRespondePacket.cs +++ /dev/null @@ -1,24 +0,0 @@ -using UnityEngine; -using System; - -public enum AuthResponse { - ALLOW, - ALREADY_CONNECTED, - INVALID_USERNAME -} - -public class AuthResponsePacket : ServerPacket { - public AuthResponse Response { get; private set; } - - public AuthResponsePacket(byte[] d) : base(d) { - Parse(); - } - - public override void Parse() { - try { - Response = (AuthResponse)ReadB(); - } catch(Exception e) { - Debug.LogError(e); - } - } -} \ No newline at end of file diff --git a/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ServerPackets/KeyPacket.cs b/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ServerPackets/KeyPacket.cs new file mode 100644 index 000000000..b75611fad --- /dev/null +++ b/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ServerPackets/KeyPacket.cs @@ -0,0 +1,18 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class KeyPacket : ServerPacket { + public byte[] BlowFishKey { get; private set; } + public bool AuthAllowed { get; private set; } + + public KeyPacket(byte[] d) : base(d) { + Parse(); + } + + public override void Parse() { + byte blowFishKeyLength = ReadB(); + BlowFishKey = ReadB(blowFishKeyLength); + AuthAllowed = ReadB() == 1; + } +} diff --git a/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ServerPackets/KeyPacket.cs.meta b/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ServerPackets/KeyPacket.cs.meta new file mode 100644 index 000000000..911023b23 --- /dev/null +++ b/l2-unity/Assets/Scripts/Networking/ClientLibrary/Packet/Gameserver/ServerPackets/KeyPacket.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4b018231bd658624bb03c73ce42ba462 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/Gameserver/GameClientPacketHandler.cs b/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/Gameserver/GameClientPacketHandler.cs index ced34d175..fc6b5c868 100644 --- a/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/Gameserver/GameClientPacketHandler.cs +++ b/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/Gameserver/GameClientPacketHandler.cs @@ -8,9 +8,9 @@ public void SendPing() { SendPacket(packet); } - public void SendAuth(string username) { - //AuthRequestPacket packet = new AuthRequestPacket(username); - //SendPacket(packet); + public void SendProtocolVersion() { + ProtocolVersionPacket packet = new ProtocolVersionPacket(GameManager.Instance.ProtocolVersion); + SendPacket(packet); } public void SendMessage(string message) { @@ -66,6 +66,10 @@ public override void SendPacket(ClientPacket packet) { } } + if (_client.BlowfishKey != null) { + EncryptPacket(packet); + } + _client.SendPacket(packet); } } diff --git a/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/Gameserver/GameServerPacketHandler.cs b/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/Gameserver/GameServerPacketHandler.cs index 712a9da5d..710ba3ab2 100644 --- a/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/Gameserver/GameServerPacketHandler.cs +++ b/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/Gameserver/GameServerPacketHandler.cs @@ -14,8 +14,11 @@ public override void HandlePacket(byte[] data) { case GameServerPacketType.Ping: OnPingReceive(); break; - case GameServerPacketType.AuthResponse: - OnAuthReceive(data); + case GameServerPacketType.Key: + OnKeyReceive(data); + break; + case GameServerPacketType.CharSelectionInfo: + OnCharSelectionInfoReceive(data); break; case GameServerPacketType.MessagePacket: OnMessageReceive(data); @@ -95,23 +98,19 @@ private void OnPingReceive() { }, _tokenSource.Token); } - private void OnAuthReceive(byte[] data) { - AuthResponsePacket packet = new AuthResponsePacket(data); - AuthResponse response = packet.Response; + private void OnKeyReceive(byte[] data) { + KeyPacket packet = new KeyPacket(data); - switch(response) { - case AuthResponse.ALLOW: - _eventProcessor.QueueEvent(() => GameClient.Instance.OnAuthAllowed()); - break; - case AuthResponse.ALREADY_CONNECTED: - Debug.Log("User already connected."); - _client.Disconnect(); - break; - case AuthResponse.INVALID_USERNAME: - Debug.Log("Incorrect user name."); - _client.Disconnect(); - break; + if(packet.AuthAllowed) { + _client.Disconnect(); + return; } + + GameClient.Instance.SetBlowFishKey(packet.BlowFishKey); + } + + private void OnCharSelectionInfoReceive(byte[] data) { + } private void OnMessageReceive(byte[] data) { diff --git a/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/LoginServer/LoginClientPacketHandler.cs b/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/LoginServer/LoginClientPacketHandler.cs index 0dada88cd..247e6a49a 100644 --- a/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/LoginServer/LoginClientPacketHandler.cs +++ b/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/LoginServer/LoginClientPacketHandler.cs @@ -54,13 +54,13 @@ public void SendAuth() { } public void SendRequestServerList() { - RequestServerListPacket packet = new RequestServerListPacket(_client.SessionKey1, _client.SessionKey2); + RequestServerListPacket packet = new RequestServerListPacket(LoginClient.Instance.SessionKey1, LoginClient.Instance.SessionKey2); SendPacket(packet); } public void SendRequestServerLogin(int serverId) { - RequestServerLoginPacket packet = new RequestServerLoginPacket(serverId, _client.SessionKey1, _client.SessionKey2); + RequestServerLoginPacket packet = new RequestServerLoginPacket(serverId, LoginClient.Instance.SessionKey1, LoginClient.Instance.SessionKey2); SendPacket(packet); } diff --git a/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/LoginServer/LoginServerPacketHandler.cs b/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/LoginServer/LoginServerPacketHandler.cs index 36e160a80..20524b194 100644 --- a/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/LoginServer/LoginServerPacketHandler.cs +++ b/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/LoginServer/LoginServerPacketHandler.cs @@ -74,7 +74,8 @@ private void OnInitReceive(byte[] data) { byte[] blowfishKey = packet.BlowfishKey; _client.SetRSAKey(rsaKey); - _client.SetBlowFishKey(blowfishKey); + + LoginClient.Instance.SetBlowFishKey(blowfishKey); _client.InitPacket = false; @@ -104,8 +105,11 @@ private void OnAccountKicked(byte[] data) { private void OnLoginOk(byte[] data) { LoginOkPacket packet = new LoginOkPacket(data); - _client.SessionKey1 = packet.SessionKey1; - _client.SessionKey2 = packet.SessionKey2; + LoginClient.Instance.SessionKey1 = packet.SessionKey1; + LoginClient.Instance.SessionKey2 = packet.SessionKey2; + + GameClient.Instance.SessionKey1 = packet.SessionKey1; + GameClient.Instance.SessionKey2 = packet.SessionKey2; EventProcessor.Instance.QueueEvent(() => LoginClient.Instance.OnAuthAllowed()); } @@ -129,10 +133,11 @@ private void OnPlayFail(byte[] data) { private void OnPlayOk(byte[] data) { LoginOkPacket packet = new LoginOkPacket(data); - GameClient.Instance.SessionKey1 = packet.SessionKey1; - GameClient.Instance.SessionKey2 = packet.SessionKey2; + GameClient.Instance.PlayKey1 = packet.SessionKey1; + GameClient.Instance.PlayKey2 = packet.SessionKey2; Debug.Log("Server select allowed."); - // EventProcessor.Instance.QueueEvent(() => LoginClient.Instance.OnAuthAllowed()); + + EventProcessor.Instance.QueueEvent(() => LoginClient.Instance.OnPlayOk()); } } diff --git a/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/ServerPacketHandler.cs b/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/ServerPacketHandler.cs index cd1a13a03..ecb6bb191 100644 --- a/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/ServerPacketHandler.cs +++ b/l2-unity/Assets/Scripts/Networking/ClientLibrary/PacketHandler/ServerPacketHandler.cs @@ -20,12 +20,14 @@ public void SetClient(AsynchronousClient client, ClientPacketHandler clientPacke public async Task HandlePacketAsync(byte[] data, bool init) { await Task.Run(() => { - data = DecryptPacket(data); - - if (init) { - if(!DecodeXOR(data)) { - Debug.LogError("Packet XOR could not be decoded."); - return; + if (_client.BlowfishKey != null) { + data = DecryptPacket(data); + + if (init) { + if (!DecodeXOR(data)) { + Debug.LogError("Packet XOR could not be decoded."); + return; + } } } diff --git a/l2-unity/Assets/Scripts/Networking/DefaultClient.cs b/l2-unity/Assets/Scripts/Networking/DefaultClient.cs index 5f9434023..bf907e0f7 100644 --- a/l2-unity/Assets/Scripts/Networking/DefaultClient.cs +++ b/l2-unity/Assets/Scripts/Networking/DefaultClient.cs @@ -8,8 +8,12 @@ public abstract class DefaultClient : MonoBehaviour { [SerializeField] protected int _serverPort = 11000; [SerializeField] protected AsynchronousClient _client; [SerializeField] protected int _connectionTimeoutMs = 10000; + [SerializeField] protected bool _connected = false; [SerializeField] protected bool _logReceivedPackets = true; [SerializeField] protected bool _logSentPackets = true; + [SerializeField] protected int _sessionKey1; + [SerializeField] protected int _sessionKey2; + [SerializeField] protected byte[] _blowfishKey; private bool _connecting = false; public bool LogReceivedPackets { get { return _logReceivedPackets; } } @@ -17,6 +21,9 @@ public abstract class DefaultClient : MonoBehaviour { public int ConnectionTimeoutMs { get { return _connectionTimeoutMs; } } public string ServerIp { get { return _serverIp; } set { _serverIp = value; } } public int ServerPort { get { return _serverPort; } set { _serverPort = value; } } + public int SessionKey1 { get { return _sessionKey1; } set { _sessionKey1 = value; } } + public int SessionKey2 { get { return _sessionKey2; } set { _sessionKey2 = value; } } + public byte[] BlowFishKey { get { return _blowfishKey; } } private void Start() { if(World.Instance != null && World.Instance.OfflineMode) { @@ -25,15 +32,15 @@ private void Start() { } public async void Connect() { + _connected = false; if(_connecting) { return; } - _connecting = true; - - CreateAsyncClient(); + WhileConnecting(); + bool connected = await Task.Run(_client.Connect); if(connected) { _connecting = false; @@ -42,12 +49,24 @@ public async void Connect() { } } + public void SetBlowFishKey(byte[] blowfishKey) { + _client.SetBlowFishKey(blowfishKey); + _blowfishKey = blowfishKey; + } + + protected virtual void WhileConnecting() { + _connecting = true; + } + protected abstract void CreateAsyncClient(); - protected abstract void OnConnectionSuccess(); + protected virtual void OnConnectionSuccess() { + _connected = true; + } public virtual void OnConnectionFailed() { _connecting = false; + _connected = false; } public abstract void OnAuthAllowed(); @@ -57,13 +76,14 @@ public int GetPing() { } public void Disconnect() { + _connected = false; + if (_client != null) { _client.Disconnect(); } } public virtual void OnDisconnect() { - Debug.Log("Disconnected"); _client = null; GameManager.Instance.OnDisconnect(); } diff --git a/l2-unity/Assets/Scripts/Networking/GameClient.cs b/l2-unity/Assets/Scripts/Networking/GameClient.cs index 30094be4e..0d0ef4e21 100644 --- a/l2-unity/Assets/Scripts/Networking/GameClient.cs +++ b/l2-unity/Assets/Scripts/Networking/GameClient.cs @@ -5,10 +5,15 @@ public class GameClient : DefaultClient { [SerializeField] protected Entity _currentPlayer; + [SerializeField] protected int _serverId; + [SerializeField] private int _playKey1; + [SerializeField] private int _playKey2; public string CurrentPlayer { get { return _currentPlayer.Identity.Name; } } - public int SessionKey1 { get { return _client.SessionKey1; } set { _client.SessionKey1 = value; } } - public int SessionKey2 { get { return _client.SessionKey2; } set { _client.SessionKey2 = value; } } + public int ServerId { get { return _serverId; } set { _serverId = value; } } + public int PlayKey1 { get { return _playKey1; } set { _playKey1 = value; } } + public int PlayKey2 { get { return _playKey2; } set { _playKey2 = value; } } + private GameClientPacketHandler clientPacketHandler; private GameServerPacketHandler serverPacketHandler; @@ -32,12 +37,21 @@ protected override void CreateAsyncClient() { clientPacketHandler = new GameClientPacketHandler(); serverPacketHandler = new GameServerPacketHandler(); - _client = new AsynchronousClient(_serverIp, _serverPort, this, clientPacketHandler, serverPacketHandler); + _client = new AsynchronousClient(_serverIp, _serverPort, this, clientPacketHandler, serverPacketHandler, false); + } + + protected override void WhileConnecting() { + base.WhileConnecting(); + + GameManager.Instance.OnConnectingToGameServer(); } protected override void OnConnectionSuccess() { - clientPacketHandler.SendPing(); - clientPacketHandler.SendAuth("1234"); + base.OnConnectionSuccess(); + + Debug.Log("Connected to GameServer"); + + clientPacketHandler.SendProtocolVersion(); } public override void OnConnectionFailed() { @@ -45,11 +59,14 @@ public override void OnConnectionFailed() { } public override void OnAuthAllowed() { - Debug.Log("Connected"); + Debug.Log("Authed to GameServer"); + GameManager.Instance.OnAuthAllowed(); } public override void OnDisconnect() { base.OnDisconnect(); + + Debug.Log("Disconnected from GameServer."); } } diff --git a/l2-unity/Assets/Scripts/Networking/LoginClient.cs b/l2-unity/Assets/Scripts/Networking/LoginClient.cs index 237820ced..b0c3266a0 100644 --- a/l2-unity/Assets/Scripts/Networking/LoginClient.cs +++ b/l2-unity/Assets/Scripts/Networking/LoginClient.cs @@ -5,6 +5,26 @@ using static ServerListPacket; public class LoginClient : DefaultClient { + // Crypt + public static byte[] STATIC_BLOWFISH_KEY = { + (byte) 0x6b, + (byte) 0x60, + (byte) 0xcb, + (byte) 0x5b, + (byte) 0x82, + (byte) 0xce, + (byte) 0x90, + (byte) 0xb1, + (byte) 0xcc, + (byte) 0x2b, + (byte) 0x6c, + (byte) 0x55, + (byte) 0x6c, + (byte) 0x6c, + (byte) 0x6c, + (byte) 0x6c + }; + [SerializeField] protected string _account; [SerializeField] protected string _password; @@ -12,7 +32,6 @@ public class LoginClient : DefaultClient { public string Account { get { return _account; } set { _account = value; } } public string Password { get { return _password; } set { _password = value; } } - private LoginClientPacketHandler clientPacketHandler; private LoginServerPacketHandler serverPacketHandler; @@ -35,10 +54,19 @@ protected override void CreateAsyncClient() { clientPacketHandler = new LoginClientPacketHandler(); serverPacketHandler = new LoginServerPacketHandler(); - _client = new AsynchronousClient(_serverIp, _serverPort, this, clientPacketHandler, serverPacketHandler); + _client = new AsynchronousClient(_serverIp, _serverPort, this, clientPacketHandler, serverPacketHandler, true); + } + + protected override void WhileConnecting() { + base.WhileConnecting(); + SetBlowFishKey(STATIC_BLOWFISH_KEY); } protected override void OnConnectionSuccess() { + base.OnConnectionSuccess(); + + Debug.Log("Connected to LoginServer"); + GameManager.Instance.OnLoginServerConnected(); } @@ -47,9 +75,16 @@ public override void OnConnectionFailed() { } public override void OnAuthAllowed() { + Debug.Log("Authed to LoginServer"); GameManager.Instance.OnLoginServerAuthAllowed(); } + public void OnPlayOk() { + GameManager.Instance.OnLoginServerPlayOk(); + + Disconnect(); + } + public void OnServerListReceived(byte lastServer, List serverData, Dictionary charsOnServers) { GameManager.Instance.OnReceivedServerList(lastServer, serverData, charsOnServers); } @@ -60,5 +95,11 @@ public void OnServerSelected(int serverId) { public override void OnDisconnect() { base.OnDisconnect(); + + if (GameManager.Instance.GameState == GameState.READY_TO_CONNECT) { + GameClient.Instance.Connect(); + } + + Debug.Log("Disconnected from LoginServer."); } } diff --git a/l2-unity/Assets/Scripts/UI/Login/ServerSelectWindow.cs b/l2-unity/Assets/Scripts/UI/Login/ServerSelectWindow.cs index fd987cf65..eba6945b6 100644 --- a/l2-unity/Assets/Scripts/UI/Login/ServerSelectWindow.cs +++ b/l2-unity/Assets/Scripts/UI/Login/ServerSelectWindow.cs @@ -120,7 +120,7 @@ public void SelectServer(int rowId) { return; } - _selectedServerId = _serverData[rowId].serverId; + SetServerId(_serverData[rowId].serverId); Debug.Log("Server selected: " + _selectedServerId); @@ -198,7 +198,13 @@ private void ResetWindow() { _serverData.Clear(); _serverData = null; } - _selectedServerId = -1; + + SetServerId(-1); + } + + private void SetServerId(int id) { + _selectedServerId = id; + GameClient.Instance.ServerId = id; } private void ConfirmButtonPressed() {