Untitled
Anonymous
plain_text
03/01/2026 8:36 PM
7.4 KB
15
Indexable
using UnityEngine;
using PacketTestApp;
using System.Collections.Generic;
public class Login : MonoBehaviour
{
[Header("Server Info")]
public string serverIP = "84.247.20.200";
public int authPort = 30001;
public int gamePort = 30101;
[Header("Static Account Info")]
public string staticUsername = "test";
public string staticPassword = "12345";
[Header("Character Selection")]
public byte characterIndex = 0;
private enum State
{
Idle,
WaitingForAuthPhase,
LoginSent,
ConnectingToGame,
WaitingForGamePhase,
Login2Sent,
CharacterSelectSent,
InGame
}
private State _state = State.Idle;
private Metin2Client _authClient;
private Metin2Client _gameClient;
private uint _loginKey = 0;
void Start()
{
Debug.Log("[Login] Script ready. Press the button to start.");
}
// Attach this to your Button's "On Click ()" in Inspector
public void OnLoginButtonClicked()
{
if (_state != State.Idle)
{
Debug.LogWarning("[Login] A login attempt is already in progress.");
return;
}
Debug.Log($"[Auth] Connecting to {serverIP}:{authPort} ...");
_authClient = new Metin2Client();
if (_authClient.Connect(serverIP, authPort))
{
Debug.Log("[Auth] Connected. Waiting for AUTH phase...");
_state = State.WaitingForAuthPhase;
}
else
{
Debug.LogError("[Auth] Connection failed! Check IP / Port.");
}
}
void Update()
{
switch (_state)
{
// ── AUTH SERVER ────────────────────────────────────────────
case State.WaitingForAuthPhase:
Poll(_authClient);
if (_authClient.Phase == "AUTH")
{
Debug.Log($"[Auth] Phase = AUTH. Sending Login3 for '{staticUsername}'...");
_authClient.SendLogin3(staticUsername, staticPassword);
_state = State.LoginSent;
}
break;
case State.LoginSent:
var authResult = Poll(_authClient);
if (authResult != null && authResult.ContainsKey("status"))
{
if ((string)authResult["status"] == "SUCCESS")
{
_loginKey = _authClient.LoginKey;
Debug.Log($"[Auth] Login SUCCESS! Key = {_loginKey}");
_authClient.Disconnect();
_state = State.ConnectingToGame;
}
else
{
Debug.LogError("[Auth] Login FAILED! Wrong credentials or server rejected.");
_authClient.Disconnect();
_state = State.Idle;
}
}
break;
// ── GAME SERVER ────────────────────────────────────────────
case State.ConnectingToGame:
Debug.Log($"[Game] Connecting to {serverIP}:{gamePort} ...");
_gameClient = new Metin2Client();
if (_gameClient.Connect(serverIP, gamePort))
{
Debug.Log("[Game] Connected. Waiting for LOGIN phase...");
_state = State.WaitingForGamePhase;
}
else
{
Debug.LogError("[Game] Connection failed!");
_state = State.Idle;
}
break;
case State.WaitingForGamePhase:
Poll(_gameClient);
if (_gameClient.Phase == "LOGIN")
{
// ✅ Program.cs ile birebir aynı sıra
Debug.Log("[Game] Phase = LOGIN. Sending Login2...");
_gameClient.SendLogin2(staticUsername, _loginKey);
Debug.Log("[Game] Enabling security keys...");
_gameClient.EnableGameSecurity();
_state = State.Login2Sent;
}
break;
case State.Login2Sent:
Poll(_gameClient);
if (_gameClient.Phase == "SELECT")
{
Debug.Log($"[Game] Phase = SELECT. Selecting character at index {characterIndex}...");
_gameClient.SendPlayerSelect(characterIndex);
_state = State.CharacterSelectSent;
}
break;
case State.CharacterSelectSent:
Poll(_gameClient);
if (_gameClient.Phase == "GAME" || _gameClient.Phase == "LOADING")
{
Debug.Log("[Game] Phase = GAME. Sending EnterGame...");
_gameClient.SendEnterGame();
_state = State.InGame;
Debug.Log(">>> FULLY IN GAME! <<<");
}
break;
case State.InGame:
Poll(_gameClient);
break;
}
}
private Dictionary<string, object> Poll(Metin2Client client)
{
byte[] data = client.Recv();
if (data != null)
return client.ProcessData(data);
return null;
}
void OnApplicationQuit()
{
_authClient?.Disconnect();
_gameClient?.Disconnect();
}
}
Editor is loading...
Leave a Comment