139 lines
5.0 KiB
C#
Raw Normal View History

2022-07-20 17:50:01 +08:00
using LemonUI.Menus;
2022-05-31 19:46:40 -08:00
using Newtonsoft.Json;
2022-07-20 17:50:01 +08:00
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Net;
2022-05-31 23:12:32 -08:00
using System.Threading;
2022-08-12 18:10:56 +08:00
using RageCoop.Core;
2022-08-12 20:40:50 +08:00
using GTA.UI;
2022-05-31 19:46:40 -08:00
namespace RageCoop.Client.Menus
{
/// <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
2022-07-20 17:50:01 +08:00
GetServersThread=new Thread(() => GetAllServers());
2022-05-31 23:12:32 -08:00
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
{
2022-08-12 18:10:56 +08:00
List<ServerInfo> serverList = null;
2022-07-11 12:02:46 +08:00
var realUrl = Main.Settings.MasterServer;
2022-08-12 18:10:56 +08:00
serverList = JsonConvert.DeserializeObject<List<ServerInfo>>(DownloadString(realUrl));
2022-07-20 17:50:01 +08:00
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-06-01 17:55:38 +08:00
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
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}";
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-08-13 11:42:29 +08:00
address=$"{server.ztAddress}:{server.port}";
Main.QueueAction(() => { Notification.Show($"~y~Joining ZeroTier network... {server.ztID}"); });
if (ZeroTierHelper.Join(server.ztID)==null)
2022-08-12 20:40:50 +08:00
{
throw new Exception("Failed to obtain ZeroTier network IP");
}
}
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)
{
2022-08-12 20:40:50 +08:00
Notification.Show($"~r~{ex.Message}");
2022-08-13 11:42:29 +08:00
if (server.useZT)
{
Notification.Show($"Make sure ZeroTier is correctly installed, download it from https://www.zerotier.com/");
}
2022-05-31 23:12:32 -08:00
}
};
Menu.Add(tmpItem);
}
});
2022-05-31 19:46:40 -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; };
WebClient client = new WebClient();
return client.DownloadString(url);
}
catch (Exception ex)
{
Main.QueueAction(() =>
{
ResultItem.Title = "Download failed!";
ResultItem.Description = ex.Message;
});
return "";
}
}
2022-05-31 19:46:40 -08:00
}
}