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;
|
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-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-05-22 15:55:26 +08:00
|
|
|
|
public static Client GetClientByID(long id)
|
2021-12-11 12:35:17 +01:00
|
|
|
|
{
|
2022-05-22 15:55:26 +08:00
|
|
|
|
Client result = Server.Clients.Find(x => x.ClientID == id);
|
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
|
|
|
|
{
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-22 15:55:26 +08:00
|
|
|
|
return Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == client.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-11-25 16:32:04 +01:00
|
|
|
|
return new(Server.MainNetServer.Connections.FindAll(e =>
|
|
|
|
|
{
|
2022-05-22 15:55:26 +08:00
|
|
|
|
Client client = Server.Clients.First(x => x.ClientID == 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 =>
|
|
|
|
|
{
|
2022-05-22 15:55:26 +08:00
|
|
|
|
Client client = Server.Clients.First(x => x.ClientID == 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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|