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;
|
|
|
|
|
|
|
|
|
|
using Lidgren.Network;
|
2021-07-07 13:36:25 +02:00
|
|
|
|
|
|
|
|
|
namespace CoopServer
|
|
|
|
|
{
|
2021-12-03 20:41:44 +01:00
|
|
|
|
internal class Util
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
public static Client GetClientByNetHandle(long netHandle)
|
2021-12-11 12:35:17 +01:00
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
Client result = Server.Clients.Find(x => x.NetHandle == netHandle);
|
2021-12-11 12:35:17 +01:00
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
NetConnection localConn = Server.MainNetServer.Connections.Find(x => netHandle == 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
|
|
|
|
{
|
2021-12-11 12:35:17 +01:00
|
|
|
|
Client client = Server.Clients.Find(x => x.Player.Username.ToLower() == username.ToLower());
|
|
|
|
|
if (client == null)
|
2021-08-21 16:52:17 +02:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 22:39:15 +01:00
|
|
|
|
return Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == client.NetHandle);
|
2021-08-18 11:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return a list of all connections but not the local connection
|
|
|
|
|
public static List<NetConnection> FilterAllLocal(NetConnection local)
|
|
|
|
|
{
|
|
|
|
|
return new(Server.MainNetServer.Connections.Where(e => e != local));
|
|
|
|
|
}
|
|
|
|
|
public static List<NetConnection> FilterAllLocal(long local)
|
|
|
|
|
{
|
|
|
|
|
return new(Server.MainNetServer.Connections.Where(e => e.RemoteUniqueIdentifier != local));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return a list of players within range of ...
|
|
|
|
|
public static List<NetConnection> GetAllInRange(LVector3 position, float range)
|
|
|
|
|
{
|
2021-11-25 16:32:04 +01:00
|
|
|
|
return new(Server.MainNetServer.Connections.FindAll(e =>
|
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
Client client = Server.Clients.First(x => x.NetHandle == e.RemoteUniqueIdentifier);
|
2021-12-11 12:35:17 +01:00
|
|
|
|
return client != null && client.Player.IsInRangeOf(position, range);
|
2021-11-25 16:32:04 +01:00
|
|
|
|
}));
|
2021-08-18 11:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
// Return a list of players within range of ... but not the local one
|
|
|
|
|
public static List<NetConnection> GetAllInRange(LVector3 position, float range, NetConnection local)
|
|
|
|
|
{
|
2021-11-25 16:32:04 +01:00
|
|
|
|
return new(Server.MainNetServer.Connections.Where(e =>
|
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
Client client = Server.Clients.First(x => x.NetHandle == e.RemoteUniqueIdentifier);
|
2021-12-11 12:35:17 +01:00
|
|
|
|
return e != local && client != null && client.Player.IsInRangeOf(position, range);
|
2021-11-25 16:32:04 +01:00
|
|
|
|
}));
|
2021-08-18 11:47:59 +02: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))
|
|
|
|
|
{
|
2021-12-28 13:51:36 +01:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 13:51:36 +01: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
|
|
|
|
|
{
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|