2019-04-23 18:58:43 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using SanAndreasUnity.Utilities;
|
|
|
|
|
using SanAndreasUnity.Net;
|
2019-11-12 13:43:22 +00:00
|
|
|
|
using System.Linq;
|
2021-01-27 00:34:53 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2019-04-23 18:58:43 +00:00
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.UI
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class JoinGameWindow : PauseMenuWindow
|
|
|
|
|
{
|
2019-07-26 19:58:30 +00:00
|
|
|
|
[SerializeField] string m_ipStr = "127.0.0.1";
|
2019-06-25 21:04:30 +00:00
|
|
|
|
string m_portStr = NetManager.defaultListenPortNumber.ToString();
|
2019-04-23 18:58:43 +00:00
|
|
|
|
|
2021-02-27 16:30:55 +00:00
|
|
|
|
string[] m_tabNames = new string[]{"Internet", "LAN", "Direct" };
|
|
|
|
|
private const int InternetTabIndex = 0, LanTabIndex = 1, DirectTabIndex = 2;
|
|
|
|
|
|
|
|
|
|
int m_currentTabIndex = InternetTabIndex;
|
2019-11-12 13:43:22 +00:00
|
|
|
|
|
|
|
|
|
Mirror.NetworkDiscoveryHUD m_netDiscoveryHUD;
|
2021-01-26 21:46:14 +00:00
|
|
|
|
|
|
|
|
|
private List<ServerInfo> _serversFromMasterServer = new List<ServerInfo>();
|
|
|
|
|
private Vector2 _masterServerScrollViewPos;
|
|
|
|
|
bool _isRefreshingMasterServerList = false;
|
2021-01-28 14:01:22 +00:00
|
|
|
|
MessageBox _masterServerErrorMessageBox;
|
2021-02-27 17:07:52 +00:00
|
|
|
|
bool m_refreshedMasterServerWhenOpened = false;
|
2021-01-28 14:01:22 +00:00
|
|
|
|
|
2019-11-12 13:43:22 +00:00
|
|
|
|
|
2021-01-12 21:06:38 +00:00
|
|
|
|
JoinGameWindow()
|
2019-04-23 18:58:43 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// set default parameters
|
|
|
|
|
|
|
|
|
|
this.windowName = "Join Game";
|
|
|
|
|
this.useScrollView = true;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 00:29:00 +00:00
|
|
|
|
void Start ()
|
2019-04-23 18:58:43 +00:00
|
|
|
|
{
|
|
|
|
|
// adjust rect
|
2021-01-28 14:47:56 +00:00
|
|
|
|
float width = Mathf.Min(900, Screen.width * 0.9f);
|
|
|
|
|
float height = width * 9f / 16f;
|
|
|
|
|
this.windowRect = GUIUtils.GetCenteredRect(new Vector2(width, height));
|
2019-11-12 13:43:22 +00:00
|
|
|
|
|
|
|
|
|
m_netDiscoveryHUD = Mirror.NetworkManager.singleton.GetComponentOrThrow<Mirror.NetworkDiscoveryHUD>();
|
|
|
|
|
m_netDiscoveryHUD.connectAction = this.ConnectFromDiscovery;
|
|
|
|
|
m_netDiscoveryHUD.drawGUI = false;
|
2021-01-27 00:29:00 +00:00
|
|
|
|
}
|
2019-04-23 18:58:43 +00:00
|
|
|
|
|
2019-04-24 22:31:23 +00:00
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
if (PauseMenu.IsOpened)
|
|
|
|
|
this.IsOpened = false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-23 18:58:43 +00:00
|
|
|
|
|
2021-02-27 17:07:52 +00:00
|
|
|
|
protected override void OnWindowOpened()
|
|
|
|
|
{
|
|
|
|
|
if (m_refreshedMasterServerWhenOpened)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_refreshedMasterServerWhenOpened = true;
|
|
|
|
|
|
|
|
|
|
#pragma warning disable 4014
|
|
|
|
|
RefreshMasterServersButtonPressed();
|
|
|
|
|
#pragma warning restore 4014
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-23 18:58:43 +00:00
|
|
|
|
protected override void OnWindowGUI ()
|
|
|
|
|
{
|
|
|
|
|
|
2019-11-12 13:43:22 +00:00
|
|
|
|
m_currentTabIndex = GUIUtils.TabsControl(m_currentTabIndex, m_tabNames);
|
2019-04-23 18:58:43 +00:00
|
|
|
|
|
2019-11-12 13:43:22 +00:00
|
|
|
|
GUILayout.Space(20);
|
|
|
|
|
|
2021-02-27 16:30:55 +00:00
|
|
|
|
if (DirectTabIndex == m_currentTabIndex)
|
2019-11-12 13:43:22 +00:00
|
|
|
|
{
|
|
|
|
|
GUILayout.Label ("IP:");
|
|
|
|
|
m_ipStr = GUILayout.TextField(m_ipStr, GUILayout.Width(200));
|
|
|
|
|
|
|
|
|
|
GUILayout.Label ("Port:");
|
|
|
|
|
m_portStr = GUILayout.TextField(m_portStr, GUILayout.Width(100));
|
|
|
|
|
}
|
2021-02-27 16:30:55 +00:00
|
|
|
|
else if (LanTabIndex == m_currentTabIndex)
|
2019-11-12 13:43:22 +00:00
|
|
|
|
{
|
|
|
|
|
m_netDiscoveryHUD.width = (int) this.WindowSize.x - 30;
|
|
|
|
|
m_netDiscoveryHUD.DisplayServers();
|
|
|
|
|
}
|
2021-02-27 16:30:55 +00:00
|
|
|
|
else if (InternetTabIndex == m_currentTabIndex)
|
2021-01-12 21:06:38 +00:00
|
|
|
|
{
|
2021-01-26 22:01:20 +00:00
|
|
|
|
int availableWidth = (int) this.WindowSize.x - 50;
|
2021-01-26 21:46:14 +00:00
|
|
|
|
float[] widthPercentages = new float[] { 0.35f, 0.25f, 0.4f };
|
|
|
|
|
string[] columnNames = new string[] {"Name", "Players", "IP" };
|
|
|
|
|
|
2021-01-12 21:06:38 +00:00
|
|
|
|
// header
|
|
|
|
|
GUILayout.BeginHorizontal();
|
2021-01-26 21:46:14 +00:00
|
|
|
|
for (int i = 0; i < columnNames.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
GUILayout.Button(columnNames[i], GUILayout.Width(availableWidth * widthPercentages[i]));
|
|
|
|
|
}
|
2021-01-12 21:06:38 +00:00
|
|
|
|
GUILayout.EndHorizontal();
|
|
|
|
|
|
2021-01-26 21:46:14 +00:00
|
|
|
|
_masterServerScrollViewPos = GUILayout.BeginScrollView(_masterServerScrollViewPos);
|
2021-01-12 21:06:38 +00:00
|
|
|
|
|
2021-01-26 21:46:14 +00:00
|
|
|
|
foreach (ServerInfo info in _serversFromMasterServer)
|
2021-01-12 21:06:38 +00:00
|
|
|
|
{
|
|
|
|
|
GUILayout.BeginHorizontal();
|
|
|
|
|
|
2021-01-28 12:01:39 +00:00
|
|
|
|
GUILayout.Label(info.Name, GUIUtils.CenteredLabelStyle, GUILayout.Width(availableWidth * widthPercentages[0]));
|
2021-01-28 12:07:28 +00:00
|
|
|
|
|
|
|
|
|
GUILayout.Label($"{info.NumPlayersOnline}/{info.MaxPlayers}", GUIUtils.CenteredLabelStyle, GUILayout.Width(availableWidth * widthPercentages[1]));
|
|
|
|
|
|
|
|
|
|
GUI.enabled = ! NetStatus.IsClientActive();
|
|
|
|
|
if (GUILayout.Button($"{info.IP}:{info.Port}", GUILayout.Width(availableWidth * widthPercentages[2])))
|
2021-01-26 21:46:14 +00:00
|
|
|
|
ConnectToServerFromMasterServer(info);
|
2021-01-28 12:07:28 +00:00
|
|
|
|
GUI.enabled = true;
|
2021-01-26 21:46:14 +00:00
|
|
|
|
|
2021-01-12 21:06:38 +00:00
|
|
|
|
GUILayout.EndHorizontal();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GUILayout.EndScrollView();
|
|
|
|
|
}
|
2019-04-23 18:58:43 +00:00
|
|
|
|
|
2019-07-28 17:40:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnWindowGUIAfterContent()
|
|
|
|
|
{
|
|
|
|
|
|
2019-04-23 23:00:59 +00:00
|
|
|
|
// label with status
|
|
|
|
|
string strStatus = "Disconnected";
|
|
|
|
|
if (NetStatus.IsClientConnecting())
|
|
|
|
|
{
|
|
|
|
|
strStatus = "Connecting.";
|
|
|
|
|
for (int i = 0; i < ((int)Time.realtimeSinceStartup) % 3; i++)
|
|
|
|
|
strStatus += ".";
|
|
|
|
|
}
|
|
|
|
|
else if (NetStatus.IsClientConnected())
|
|
|
|
|
{
|
|
|
|
|
strStatus = "Connected";
|
|
|
|
|
}
|
|
|
|
|
GUILayout.Label("Status: " + strStatus);
|
|
|
|
|
|
2021-02-27 16:30:55 +00:00
|
|
|
|
// button for connecting/disconnecting/refreshing
|
2019-04-23 23:00:59 +00:00
|
|
|
|
|
|
|
|
|
string buttonText = "Connect";
|
2019-11-12 13:43:22 +00:00
|
|
|
|
System.Action buttonAction = this.ConnectDirectly;
|
2019-04-23 23:00:59 +00:00
|
|
|
|
if (NetStatus.IsClientConnecting())
|
|
|
|
|
{
|
|
|
|
|
buttonText = "Disconnect";
|
|
|
|
|
buttonAction = this.Disconnect;
|
|
|
|
|
}
|
|
|
|
|
else if (NetStatus.IsClientConnected())
|
|
|
|
|
{
|
|
|
|
|
GUI.enabled = false;
|
|
|
|
|
buttonText = "Connected";
|
|
|
|
|
buttonAction = () => {};
|
|
|
|
|
}
|
2019-11-12 13:43:22 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2021-02-27 16:30:55 +00:00
|
|
|
|
if (LanTabIndex == m_currentTabIndex)
|
2019-11-12 13:43:22 +00:00
|
|
|
|
{
|
|
|
|
|
GUI.enabled = ! m_netDiscoveryHUD.IsRefreshing;
|
|
|
|
|
buttonText = m_netDiscoveryHUD.IsRefreshing ? ( "Refreshing." + new string('.', (int) ((Time.time * 2) % 3)) ) : "Refresh LAN";
|
|
|
|
|
buttonAction = () => m_netDiscoveryHUD.Refresh();
|
|
|
|
|
}
|
2021-02-27 16:30:55 +00:00
|
|
|
|
else if (InternetTabIndex == m_currentTabIndex)
|
2021-01-12 21:06:38 +00:00
|
|
|
|
{
|
2021-01-26 21:46:14 +00:00
|
|
|
|
GUI.enabled = !_isRefreshingMasterServerList;
|
|
|
|
|
buttonText = _isRefreshingMasterServerList ? ( "Refreshing." + new string('.', (int) ((Time.time * 2) % 3)) ) : "Refresh servers";
|
2021-01-27 00:34:53 +00:00
|
|
|
|
buttonAction = async () => await RefreshMasterServersButtonPressed();
|
2021-01-12 21:06:38 +00:00
|
|
|
|
}
|
2019-11-12 13:43:22 +00:00
|
|
|
|
}
|
2019-04-23 23:00:59 +00:00
|
|
|
|
|
2019-07-28 22:26:56 +00:00
|
|
|
|
if (GUIUtils.ButtonWithCalculatedSize(buttonText, 80, 30))
|
2019-04-23 23:00:59 +00:00
|
|
|
|
buttonAction();
|
2019-04-23 18:58:43 +00:00
|
|
|
|
|
2021-01-28 12:13:01 +00:00
|
|
|
|
GUI.enabled = true;
|
|
|
|
|
|
2019-04-23 18:58:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 00:34:53 +00:00
|
|
|
|
async Task RefreshMasterServersButtonPressed()
|
|
|
|
|
{
|
|
|
|
|
_isRefreshingMasterServerList = true;
|
|
|
|
|
_serversFromMasterServer = new List<ServerInfo>();
|
2021-01-27 00:51:12 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_serversFromMasterServer = await MasterServerClient.Instance.GetAllServers();
|
|
|
|
|
}
|
|
|
|
|
catch (System.Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogException(ex);
|
2021-01-28 14:01:22 +00:00
|
|
|
|
|
|
|
|
|
if (_masterServerErrorMessageBox != null)
|
|
|
|
|
_masterServerErrorMessageBox.DestroyWindow();
|
|
|
|
|
_masterServerErrorMessageBox = MessageBox.Show("Error refreshing master server", ex.ToString());
|
2021-01-27 00:51:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 00:34:53 +00:00
|
|
|
|
_isRefreshingMasterServerList = false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-12 13:43:22 +00:00
|
|
|
|
void ConnectDirectly()
|
|
|
|
|
{
|
|
|
|
|
this.Connect(m_ipStr, ushort.Parse(m_portStr));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConnectFromDiscovery(Mirror.NetworkDiscovery.DiscoveryInfo info)
|
|
|
|
|
{
|
|
|
|
|
this.Connect(info.EndPoint.Address.ToString(), ushort.Parse( info.KeyValuePairs[Mirror.NetworkDiscovery.kPortKey] ));
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-26 21:46:14 +00:00
|
|
|
|
void ConnectToServerFromMasterServer(ServerInfo serverInfo)
|
|
|
|
|
{
|
|
|
|
|
Connect(serverInfo.IP, (ushort) serverInfo.Port);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-12 13:43:22 +00:00
|
|
|
|
void Connect(string ip, ushort port)
|
2019-04-23 18:58:43 +00:00
|
|
|
|
{
|
2019-04-23 19:04:48 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2019-11-12 13:43:22 +00:00
|
|
|
|
NetManager.StartClient(ip, port);
|
2019-04-23 19:04:48 +00:00
|
|
|
|
}
|
|
|
|
|
catch (System.Exception ex)
|
|
|
|
|
{
|
2019-06-25 21:09:34 +00:00
|
|
|
|
Debug.LogException(ex);
|
2019-04-23 19:04:48 +00:00
|
|
|
|
MessageBox.Show("Error", ex.ToString());
|
|
|
|
|
}
|
2019-04-23 18:58:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-23 23:00:59 +00:00
|
|
|
|
void Disconnect()
|
|
|
|
|
{
|
|
|
|
|
NetManager.StopNetwork();
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-23 18:58:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|