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

139 lines
4.5 KiB
C#
Raw Normal View History

2022-05-31 19:46:40 -08:00
using System;
using System.Net;
using System.Drawing;
using System.Collections.Generic;
using Newtonsoft.Json;
using LemonUI.Menus;
2022-05-31 23:12:32 -08:00
using System.Threading;
2022-05-31 19:46:40 -08:00
namespace RageCoop.Client.Menus
{
internal class ServerListClass
{
2022-05-31 23:12:32 -08:00
[JsonProperty("ip")]
public string IP { get; set; }
2022-05-31 19:46:40 -08:00
[JsonProperty("name")]
public string Name { get; set; }
2022-05-31 23:12:32 -08:00
2022-05-31 19:46:40 -08:00
[JsonProperty("version")]
public string Version { get; set; }
2022-05-31 23:12:32 -08:00
2022-05-31 19:46:40 -08:00
[JsonProperty("players")]
public int Players { get; set; }
2022-05-31 23:12:32 -08:00
2022-05-31 19:46:40 -08:00
[JsonProperty("maxPlayers")]
public int MaxPlayers { get; set; }
}
/// <summary>
/// Don't use it!
/// </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;
internal static NativeMenu Menu = new NativeMenu("RAGECOOP", "Servers", "Go to the server list")
2022-05-31 19:46:40 -08:00
{
UseMouse = false,
Alignment = Main.Settings.FlipMenu ? GTA.UI.Alignment.Right : GTA.UI.Alignment.Left
};
2022-05-31 23:12:32 -08:00
internal static NativeItem ResultItem = null;
2022-05-31 19:46:40 -08:00
/// <summary>
/// Don't use it!
/// </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-05-31 23:12:32 -08:00
Menu.Opening += (object sender, System.ComponentModel.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
if (GetServersThread!=null && GetServersThread.IsAlive) { GetServersThread?.Abort(); }
GetServersThread=new Thread(()=> GetAllServers());
GetServersThread.Start();
2022-05-31 19:46:40 -08:00
};
2022-05-31 23:12:32 -08:00
Menu.Closing += (object sender, System.ComponentModel.CancelEventArgs e) =>
2022-05-31 19:46:40 -08:00
{
CleanUpList();
};
}
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
{
List<ServerListClass> serverList = null;
try
{
// TLS only
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
WebClient client = new WebClient();
string data = client.DownloadString(Main.Settings.MasterServer);
serverList = JsonConvert.DeserializeObject<List<ServerListClass>>(data);
}
catch (Exception ex)
{
ResultItem.Title = "Download failed!";
ResultItem.Description = ex.Message;
return;
}
if (serverList == null)
{
ResultItem.Title = "Something went wrong!";
return;
}
if (serverList.Count == 0)
{
ResultItem.Title = "No server was found!";
return;
}
2022-05-31 23:12:32 -08:00
// Need to be processed in main thread
Main.QueueAction(() =>
2022-05-31 19:46:40 -08:00
{
2022-05-31 23:12:32 -08:00
CleanUpList();
foreach (ServerListClass server in serverList)
2022-05-31 19:46:40 -08:00
{
2022-05-31 23:12:32 -08:00
string address = server.IP;
NativeItem tmpItem = new NativeItem($"{server.Name}", $"~b~{address}~s~~n~~g~Version {server.Version}.x~s~") { AltTitle = $"[{server.Players}/{server.MaxPlayers}]" };
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-05-31 19:46:40 -08:00
2022-05-31 23:12:32 -08:00
Networking.ToggleConnection(address);
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
2022-05-31 23:12:32 -08:00
Main.Settings.LastServerAddress = address;
Util.SaveSettings();
}
catch (Exception ex)
{
GTA.UI.Notification.Show($"~r~{ex.Message}");
}
};
Menu.Add(tmpItem);
}
});
2022-05-31 19:46:40 -08:00
}
}
}