128 lines
4.1 KiB
C#
Raw Normal View History

using System;
using System.IO;
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;
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-03 16:28:02 +08:00
WebClient client = new();
return client.DownloadString(url);
}
catch
{
return "";
}
}
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-04 18:09:42 +08:00
public static Client GetClientByNetID(long id)
2021-12-11 12:35:17 +01:00
{
2022-05-30 11:11:40 +08:00
Client result = null;
Server.Clients.TryGetValue(id,out result);
2021-12-11 12:35:17 +01:00
if (result == null)
{
2022-05-22 15:55:26 +08:00
NetConnection localConn = Server.MainNetServer.Connections.Find(x => id == x.RemoteUniqueIdentifier);
2021-12-11 12:35:17 +01:00
if (localConn != null)
{
localConn.Disconnect("No data found!");
}
return null;
}
return result;
}
2021-08-21 16:52:17 +02:00
public static NetConnection GetConnectionByUsername(string username)
2021-08-18 11:47:59 +02:00
{
2022-06-21 18:13:30 +08:00
Client client = Server.Clients.Values.ToList().Find(x => x.Username.ToLower() == username.ToLower());
2021-12-11 12:35:17 +01:00
if (client == null)
2021-08-21 16:52:17 +02:00
{
return null;
}
return Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == client.NetID);
2021-08-18 11:47:59 +02:00
}
*/
2021-08-18 11:47:59 +02:00
// Return a list of all connections but not the local connection
public static List<NetConnection> Exclude(this IEnumerable<NetConnection> connections,NetConnection toExclude)
2021-08-18 11:47:59 +02:00
{
return new(connections.Where(e => e != toExclude));
2021-08-18 11:47:59 +02:00
}
/*
2021-08-18 11:47:59 +02:00
public static List<NetConnection> FilterAllLocal(long local)
{
return new(Server.MainNetServer.Connections.Where(e => e.RemoteUniqueIdentifier != local));
}
*/
2021-07-07 13:36:25 +02:00
public static T Read<T>(string file) where T : new()
{
XmlSerializer ser = new(typeof(T));
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))
{
using (XmlReader stream = XmlReader.Create(path))
2021-07-07 13:36:25 +02:00
{
2021-08-14 21:49:23 +02:00
data = (T)ser.Deserialize(stream);
2021-07-07 13:36:25 +02:00
}
using (XmlWriter stream = XmlWriter.Create(path, settings))
2021-07-07 13:36:25 +02:00
{
2021-08-14 21:49:23 +02:00
ser.Serialize(stream, data);
2021-07-07 13:36:25 +02:00
}
}
else
{
using (XmlWriter stream = XmlWriter.Create(path, settings))
{
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
}
}
}