2021-07-10 09:41:17 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2021-12-28 13:51:36 +01:00
|
|
|
|
using System.Xml;
|
2021-07-07 13:36:25 +02:00
|
|
|
|
using System.Xml.Serialization;
|
2021-08-18 11:47:59 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Collections.Generic;
|
2022-05-22 15:55:26 +08:00
|
|
|
|
using RageCoop.Core;
|
2021-08-18 11:47:59 +02:00
|
|
|
|
using Lidgren.Network;
|
2022-06-03 16:28:02 +08:00
|
|
|
|
using System.Net;
|
2022-06-11 18:41:10 +08:00
|
|
|
|
using System.Net.Sockets;
|
2021-07-07 13:36:25 +02:00
|
|
|
|
|
2022-05-22 15:55:26 +08:00
|
|
|
|
namespace RageCoop.Server
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2022-05-22 15:55:26 +08:00
|
|
|
|
static partial class Util
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2022-06-03 16:28:02 +08:00
|
|
|
|
|
|
|
|
|
public static string DownloadString(string url)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// TLS only
|
|
|
|
|
ServicePointManager.Expect100Continue = true;
|
|
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12;
|
|
|
|
|
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
2022-06-11 18:41:10 +08:00
|
|
|
|
|
2022-06-03 16:28:02 +08:00
|
|
|
|
WebClient client = new();
|
|
|
|
|
return client.DownloadString(url);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-12 05:05:27 +02:00
|
|
|
|
public static (byte, byte[]) GetBytesFromObject(object obj)
|
|
|
|
|
{
|
|
|
|
|
return obj switch
|
|
|
|
|
{
|
|
|
|
|
byte _ => (0x01, BitConverter.GetBytes((byte)obj)),
|
|
|
|
|
short _ => (0x02, BitConverter.GetBytes((short)obj)),
|
|
|
|
|
ushort _ => (0x03, BitConverter.GetBytes((ushort)obj)),
|
|
|
|
|
int _ => (0x04, BitConverter.GetBytes((int)obj)),
|
|
|
|
|
uint _ => (0x05, BitConverter.GetBytes((uint)obj)),
|
|
|
|
|
long _ => (0x06, BitConverter.GetBytes((long)obj)),
|
|
|
|
|
ulong _ => (0x07, BitConverter.GetBytes((ulong)obj)),
|
|
|
|
|
float _ => (0x08, BitConverter.GetBytes((float)obj)),
|
|
|
|
|
bool _ => (0x09, BitConverter.GetBytes((bool)obj)),
|
|
|
|
|
_ => (0x0, null),
|
|
|
|
|
};
|
|
|
|
|
}
|
2022-06-06 17:37:11 +08:00
|
|
|
|
public static List<NetConnection> Exclude(this IEnumerable<NetConnection> connections,NetConnection toExclude)
|
2021-08-18 11:47:59 +02:00
|
|
|
|
{
|
2022-06-06 17:37:11 +08:00
|
|
|
|
return new(connections.Where(e => e != toExclude));
|
2021-08-18 11:47:59 +02:00
|
|
|
|
}
|
2022-07-10 16:13:08 +08:00
|
|
|
|
|
2021-07-07 13:36:25 +02:00
|
|
|
|
public static T Read<T>(string file) where T : new()
|
|
|
|
|
{
|
|
|
|
|
XmlSerializer ser = new(typeof(T));
|
|
|
|
|
|
2021-12-28 13:51:36 +01:00
|
|
|
|
XmlWriterSettings settings = new()
|
|
|
|
|
{
|
|
|
|
|
Indent = true,
|
|
|
|
|
IndentChars = ("\t"),
|
|
|
|
|
OmitXmlDeclaration = true
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-12 01:47:01 +02:00
|
|
|
|
string path = AppContext.BaseDirectory + file;
|
2021-08-14 21:49:23 +02:00
|
|
|
|
T data;
|
2021-07-07 13:36:25 +02:00
|
|
|
|
|
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
{
|
2022-06-27 14:42:57 +08:00
|
|
|
|
try
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2022-06-27 14:42:57 +08:00
|
|
|
|
using (XmlReader stream = XmlReader.Create(path))
|
|
|
|
|
{
|
|
|
|
|
data = (T)ser.Deserialize(stream);
|
|
|
|
|
}
|
2021-07-07 13:36:25 +02:00
|
|
|
|
|
2022-06-27 14:42:57 +08:00
|
|
|
|
using (XmlWriter stream = XmlWriter.Create(path, settings))
|
|
|
|
|
{
|
|
|
|
|
ser.Serialize(stream, data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2022-06-27 14:42:57 +08:00
|
|
|
|
using (XmlWriter stream = XmlWriter.Create(path, settings))
|
|
|
|
|
{
|
|
|
|
|
ser.Serialize(stream, data = new T());
|
|
|
|
|
}
|
2021-07-07 13:36:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-12-28 13:51:36 +01:00
|
|
|
|
using (XmlWriter stream = XmlWriter.Create(path, settings))
|
2021-12-10 11:31:36 +01:00
|
|
|
|
{
|
|
|
|
|
ser.Serialize(stream, data = new T());
|
|
|
|
|
}
|
2021-07-07 13:36:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-14 21:49:23 +02:00
|
|
|
|
return data;
|
2021-07-07 13:36:25 +02:00
|
|
|
|
}
|
2022-07-10 16:13:08 +08:00
|
|
|
|
|
|
|
|
|
public static T Next<T>(this T[] values)
|
|
|
|
|
{
|
|
|
|
|
return values[new Random().Next(values.Length-1)];
|
|
|
|
|
}
|
2021-07-07 13:36:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|