Files
RAGECOOP-V/Client/Scripts/Menus/Sub/ServersMenu.cs

148 lines
5.2 KiB
C#
Raw Normal View History

2022-10-23 19:02:39 +08:00
using System;
2022-07-20 17:50:01 +08:00
using System.Collections.Generic;
2022-10-23 19:02:39 +08:00
using System.ComponentModel;
2022-07-20 17:50:01 +08:00
using System.Drawing;
using System.Net;
using System.Net.Http;
2022-05-31 23:12:32 -08:00
using System.Threading;
2022-10-23 19:02:39 +08:00
using GTA.UI;
using LemonUI.Menus;
using RageCoop.Client.Scripting;
using RageCoop.Core;
2022-05-31 19:46:40 -08:00
namespace RageCoop.Client.Menus
{
/// <summary>
2022-10-23 19:02:39 +08:00
/// Don't use it!
2022-05-31 19:46:40 -08:00
/// </summary>
2022-05-31 23:12:32 -08:00
internal static class ServersMenu
2022-05-31 19:46:40 -08:00
{
2022-05-31 23:12:32 -08:00
private static Thread GetServersThread;
2022-10-23 19:02:39 +08:00
2022-05-31 23:12:32 -08:00
internal static NativeMenu Menu = new NativeMenu("RAGECOOP", "Servers", "Go to the server list")
2022-05-31 19:46:40 -08:00
{
UseMouse = false,
2023-02-13 20:44:50 +08:00
Alignment = Settings.FlipMenu ? Alignment.Right : Alignment.Left
2022-05-31 19:46:40 -08:00
};
2022-10-23 19:02:39 +08:00
2022-05-31 23:12:32 -08:00
internal static NativeItem ResultItem = null;
2022-05-31 19:46:40 -08:00
/// <summary>
2022-10-23 19:02:39 +08:00
/// Don't use it!
2022-05-31 19:46:40 -08:00
/// </summary>
2022-05-31 23:12:32 -08:00
static ServersMenu()
2022-05-31 19:46:40 -08:00
{
2022-05-31 23:12:32 -08:00
Menu.Banner.Color = Color.FromArgb(225, 0, 0, 0);
Menu.Title.Color = Color.FromArgb(255, 165, 0);
2022-05-31 19:46:40 -08:00
2022-10-23 19:02:39 +08:00
Menu.Opening += (object sender, CancelEventArgs e) =>
2022-05-31 19:46:40 -08:00
{
2022-05-31 23:12:32 -08:00
CleanUpList();
Menu.Add(ResultItem = new NativeItem("Loading..."));
// Prevent freezing
GetServersThread = ThreadManager.CreateThread(() => GetAllServers(),"GetServers");
2022-05-31 19:46:40 -08:00
};
2022-10-23 19:02:39 +08:00
Menu.Closing += (object sender, CancelEventArgs e) => { CleanUpList(); };
2022-05-31 19:46:40 -08:00
}
2022-05-31 23:12:32 -08:00
private static void CleanUpList()
2022-05-31 19:46:40 -08:00
{
2022-05-31 23:12:32 -08:00
Menu.Clear();
2022-05-31 19:46:40 -08:00
ResultItem = null;
}
2022-05-31 23:12:32 -08:00
private static void GetAllServers()
2022-05-31 19:46:40 -08:00
{
2022-08-12 18:10:56 +08:00
List<ServerInfo> serverList = null;
2023-02-13 20:44:50 +08:00
var realUrl = Settings.MasterServer;
serverList = null;
2023-02-12 22:06:57 +08:00
try { serverList = JsonDeserialize<List<ServerInfo>>(DownloadString(realUrl)); }
2023-02-13 17:51:18 +08:00
catch (Exception ex) { Log.Error(ex); }
2022-07-20 17:50:01 +08:00
2022-05-31 23:12:32 -08:00
// Need to be processed in main thread
2022-10-09 11:15:09 +08:00
API.QueueAction(() =>
2022-05-31 19:46:40 -08:00
{
2022-06-01 17:55:38 +08:00
if (serverList == null)
{
ResultItem.Title = "Something went wrong!";
return;
}
2022-10-23 19:02:39 +08:00
2022-06-01 17:55:38 +08:00
if (serverList.Count == 0)
{
ResultItem.Title = "No server was found!";
return;
}
2022-10-23 19:02:39 +08:00
2022-05-31 23:12:32 -08:00
CleanUpList();
2022-08-12 18:10:56 +08:00
foreach (ServerInfo server in serverList)
2022-05-31 19:46:40 -08:00
{
2022-08-13 11:42:29 +08:00
string address = $"{server.address}:{server.port}";
2022-10-23 19:02:39 +08:00
NativeItem tmpItem =
new NativeItem($"[{server.country}] {server.name}",
$"~b~{address}~s~~n~~g~Version {server.version}.x~s~")
{ AltTitle = $"[{server.players}/{server.maxPlayers}]" };
2022-05-31 23:12:32 -08:00
tmpItem.Activated += (object sender, EventArgs e) =>
2022-05-31 19:46:40 -08:00
{
2022-05-31 23:12:32 -08:00
try
{
Menu.Visible = false;
2022-08-13 11:42:29 +08:00
if (server.useZT)
2022-08-12 20:40:50 +08:00
{
2022-09-06 21:46:35 +08:00
address = $"{server.ztAddress}:{server.port}";
2022-08-22 10:01:09 +08:00
Notification.Show($"~y~Joining ZeroTier network... {server.ztID}");
2022-09-06 21:46:35 +08:00
if (ZeroTierHelper.Join(server.ztID) == null)
2022-08-12 20:40:50 +08:00
{
throw new Exception("Failed to obtain ZeroTier network IP");
}
}
2022-10-23 19:02:39 +08:00
2022-09-06 21:46:35 +08:00
Networking.ToggleConnection(address, null, null, PublicKey.FromServerInfo(server));
2022-05-31 19:46:40 -08:00
#if !NON_INTERACTIVE
2022-05-31 23:12:32 -08:00
CoopMenu.ServerIpItem.AltTitle = address;
2022-05-31 19:46:40 -08:00
2022-05-31 23:12:32 -08:00
CoopMenu.Menu.Visible = true;
2022-05-31 19:46:40 -08:00
#endif
2023-02-13 20:44:50 +08:00
Settings.LastServerAddress = address;
2022-05-31 23:12:32 -08:00
Util.SaveSettings();
}
catch (Exception ex)
{
2022-08-12 20:40:50 +08:00
Notification.Show($"~r~{ex.Message}");
2022-08-13 11:42:29 +08:00
if (server.useZT)
{
2022-10-23 19:02:39 +08:00
Notification.Show(
$"Make sure ZeroTier is correctly installed, download it from https://www.zerotier.com/");
2022-08-13 11:42:29 +08:00
}
2022-05-31 23:12:32 -08:00
}
};
Menu.Add(tmpItem);
}
});
2022-05-31 19:46:40 -08:00
}
2022-10-23 19:02:39 +08:00
2022-06-03 16:28:02 +08:00
private static string DownloadString(string url)
{
try
{
// TLS only
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
var client = new HttpClient();
return client.GetStringAsync(url).GetAwaiter().GetResult();
2022-06-03 16:28:02 +08:00
}
catch (Exception ex)
{
2022-10-09 11:15:09 +08:00
API.QueueAction(() =>
2022-06-03 16:28:02 +08:00
{
ResultItem.Title = "Download failed!";
ResultItem.Description = ex.Message;
});
return "";
}
}
2022-05-31 19:46:40 -08:00
}
}