RAGECOOP-V/Server/Util.cs

112 lines
3.8 KiB
C#
Raw Normal View History

using System;
using System.IO;
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
{
class Util
{
2021-08-21 16:52:17 +02:00
public static List<NativeArgument> ParseNativeArguments(params object[] args)
2021-08-18 11:47:59 +02:00
{
2021-08-21 16:52:17 +02:00
List<NativeArgument> result = new();
foreach (object arg in args)
2021-08-18 11:47:59 +02:00
{
2021-08-21 16:52:17 +02:00
Type typeOf = arg.GetType();
if (typeOf == typeof(int))
{
result.Add(new IntArgument() { Data = (int)arg });
}
else if (typeOf == typeof(bool))
{
result.Add(new BoolArgument() { Data = (bool)arg });
}
else if (typeOf == typeof(float))
{
result.Add(new FloatArgument() { Data = (float)arg });
}
else if (typeOf == typeof(string))
{
result.Add(new StringArgument() { Data = (string)arg });
}
else if (typeOf == typeof(LVector3))
{
result.Add(new LVector3Argument() { Data = (LVector3)arg });
}
else
{
Logging.Error("[Util->ParseNativeArguments(params object[] args)]: Type of argument not found!");
return null;
}
2021-08-18 11:47:59 +02:00
}
2021-08-21 16:52:17 +02:00
return result;
2021-08-18 11:47:59 +02:00
}
2021-08-21 16:52:17 +02:00
public static NetConnection GetConnectionByUsername(string username)
2021-08-18 11:47:59 +02:00
{
2021-08-26 17:01:32 +02:00
long clientID;
if ((clientID = Server.Clients.FirstOrDefault(x => x.Player.Username == username).ID) == default)
2021-08-21 16:52:17 +02:00
{
return null;
}
2021-08-26 17:01:32 +02:00
return Server.MainNetServer.Connections.FirstOrDefault(x => x.RemoteUniqueIdentifier == clientID);
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-08-26 17:01:32 +02:00
return new(Server.MainNetServer.Connections.FindAll(e => Server.Clients.First(x => x.ID == e.RemoteUniqueIdentifier).Player.IsInRangeOf(position, range)));
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-08-26 17:01:32 +02:00
return new(Server.MainNetServer.Connections.Where(e => e != local && Server.Clients.First(x => x.ID == e.RemoteUniqueIdentifier).Player.IsInRangeOf(position, range)));
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-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 (FileStream stream = File.OpenRead(path))
{
2021-08-14 21:49:23 +02:00
data = (T)ser.Deserialize(stream);
2021-07-07 13:36:25 +02:00
}
using (FileStream stream = new(path, File.Exists(path) ? FileMode.Truncate : FileMode.Create, FileAccess.ReadWrite))
{
2021-08-14 21:49:23 +02:00
ser.Serialize(stream, data);
2021-07-07 13:36:25 +02:00
}
}
else
{
using FileStream stream = File.OpenWrite(path);
2021-08-14 21:49:23 +02: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
}
}
}