Files
crystall-punk-14/Content.Client/Launcher/LauncherConnecting.cs

109 lines
3.2 KiB
C#
Raw Normal View History

2021-04-19 09:52:40 +02:00
using System;
using Robust.Client;
using Robust.Client.UserInterface;
using Robust.Shared.IoC;
using Robust.Shared.Network;
2021-06-09 22:19:39 +02:00
namespace Content.Client.Launcher
{
public sealed class LauncherConnecting : Robust.Client.State.State
{
[Dependency] private readonly IUserInterfaceManager _userInterfaceManager = default!;
[Dependency] private readonly IClientNetManager _clientNetManager = default!;
[Dependency] private readonly IGameController _gameController = default!;
[Dependency] private readonly IBaseClient _baseClient = default!;
2021-04-19 09:52:40 +02:00
private LauncherConnectingGui? _control;
2021-04-19 09:52:40 +02:00
private Page _currentPage;
private string? _connectFailReason;
2021-04-19 09:52:40 +02:00
public string? Address => _gameController.LaunchState.Ss14Address ?? _gameController.LaunchState.ConnectAddress;
2021-04-19 09:52:40 +02:00
public string? ConnectFailReason
{
get => _connectFailReason;
private set
{
2021-04-19 09:52:40 +02:00
_connectFailReason = value;
ConnectFailReasonChanged?.Invoke(value);
}
}
public string? LastDisconnectReason => _baseClient.LastDisconnectReason;
2021-04-19 09:52:40 +02:00
public Page CurrentPage
{
get => _currentPage;
private set
{
_currentPage = value;
PageChanged?.Invoke(value);
}
}
2021-02-21 12:38:56 +01:00
2021-04-19 09:52:40 +02:00
public ClientConnectionState ConnectionState => _clientNetManager.ClientConnectState;
2021-02-21 12:38:56 +01:00
2021-04-19 09:52:40 +02:00
public event Action<Page>? PageChanged;
public event Action<string?>? ConnectFailReasonChanged;
public event Action<ClientConnectionState>? ConnectionStateChanged;
2021-04-19 09:52:40 +02:00
public override void Startup()
{
_control = new LauncherConnectingGui(this);
_userInterfaceManager.StateRoot.AddChild(_control);
_clientNetManager.ConnectFailed += OnConnectFailed;
_clientNetManager.ClientConnectStateChanged += OnConnectStateChanged;
2021-04-19 09:52:40 +02:00
CurrentPage = Page.Connecting;
}
public override void Shutdown()
{
_control?.Dispose();
_clientNetManager.ConnectFailed -= OnConnectFailed;
_clientNetManager.ClientConnectStateChanged -= OnConnectStateChanged;
}
private void OnConnectFailed(object? _, NetConnectFailArgs args)
{
ConnectFailReason = args.Reason;
CurrentPage = Page.ConnectFailed;
}
private void OnConnectStateChanged(ClientConnectionState state)
{
ConnectionStateChanged?.Invoke(state);
}
2021-04-19 09:52:40 +02:00
public void RetryConnect()
{
2021-04-19 09:52:40 +02:00
if (_gameController.LaunchState.ConnectEndpoint != null)
{
2021-04-19 09:52:40 +02:00
_baseClient.ConnectToServer(_gameController.LaunchState.ConnectEndpoint);
CurrentPage = Page.Connecting;
}
}
2021-04-19 09:52:40 +02:00
public void Exit()
{
2021-04-19 09:52:40 +02:00
_gameController.Shutdown("Exit button pressed");
}
2021-04-19 09:52:40 +02:00
public void SetDisconnected()
{
2021-04-19 09:52:40 +02:00
CurrentPage = Page.Disconnected;
}
2021-04-19 09:52:40 +02:00
public enum Page : byte
{
Connecting,
ConnectFailed,
Disconnected,
}
}
}