RAGECOOP-V/Server/Client.cs

263 lines
9.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2022-05-22 15:55:26 +08:00
using RageCoop.Core;
2021-08-26 17:01:32 +02:00
using Lidgren.Network;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Server
2021-08-26 17:01:32 +02:00
{
public class Client
{
2022-05-22 15:55:26 +08:00
public long ClientID = 0;
private float _currentLatency = 0f;
2022-01-01 15:58:14 +01:00
public float Latency
{
get => _currentLatency;
2022-05-22 15:55:26 +08:00
set
2022-01-01 15:58:14 +01:00
{
_currentLatency = value;
2022-01-01 15:58:14 +01:00
if ((value * 1000f) > Server.MainSettings.MaxLatency)
{
2022-05-22 15:55:26 +08:00
Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == ClientID)?.Disconnect($"Too high latency [{value * 1000f}/{(float)Server.MainSettings.MaxLatency}]");
2022-01-01 15:58:14 +01:00
}
}
}
2021-08-26 17:01:32 +02:00
public PlayerData Player;
private readonly Dictionary<string, object> _customData = new();
private long _callbacksCount = 0;
2022-05-22 15:55:26 +08:00
public readonly Dictionary<long, Action<object>> Callbacks = new();
public bool FilesReceived { get; set; } = false;
public bool FilesSent = false;
#region CUSTOMDATA FUNCTIONS
public void SetData<T>(string name, T data)
{
if (HasData(name))
{
_customData[name] = data;
}
else
{
_customData.Add(name, data);
}
}
public bool HasData(string name)
{
return _customData.ContainsKey(name);
}
public T GetData<T>(string name)
{
return HasData(name) ? (T)_customData[name] : default;
}
public void RemoveData(string name)
{
if (HasData(name))
{
_customData.Remove(name);
}
}
#endregion
2021-08-26 17:01:32 +02:00
#region FUNCTIONS
2021-12-11 14:00:22 +01:00
public void Kick(string reason)
{
2022-05-22 15:55:26 +08:00
Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == ClientID)?.Disconnect(reason);
2021-12-11 14:00:22 +01:00
}
2022-04-08 22:30:34 +02:00
public void Kick(string[] reason)
{
Kick(string.Join(" ", reason));
}
2021-08-26 17:01:32 +02:00
public void SendChatMessage(string message, string from = "Server")
{
try
2021-08-26 17:01:32 +02:00
{
2022-05-22 15:55:26 +08:00
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == ClientID);
if (userConnection == null)
{
return;
}
Server.SendChatMessage(from, message, userConnection);
}
catch (Exception e)
{
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
}
2021-08-26 17:01:32 +02:00
}
public void SendNativeCall(ulong hash, params object[] args)
2021-08-26 17:01:32 +02:00
{
try
2021-08-26 17:01:32 +02:00
{
2022-05-22 15:55:26 +08:00
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == ClientID);
if (userConnection == null)
{
2022-05-22 15:55:26 +08:00
Logging.Error($"[Client->SendNativeCall(ulong hash, params object[] args)]: Connection \"{ClientID}\" not found!");
return;
}
if (args != null && args.Length == 0)
{
Logging.Error($"[Client->SendNativeCall(ulong hash, Dictionary<string, object> args)]: Missing arguments!");
return;
}
2022-01-01 03:07:18 +01:00
Packets.NativeCall packet = new()
{
Hash = hash,
Args = new List<object>(args) ?? new List<object>(),
};
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
2022-05-22 15:55:26 +08:00
packet.Pack(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Native);
2021-08-26 17:01:32 +02:00
}
catch (Exception e)
2021-08-26 17:01:32 +02:00
{
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
}
2021-08-26 17:01:32 +02:00
}
2021-11-23 23:17:47 +01:00
public void SendNativeResponse(Action<object> callback, ulong hash, Type returnType, params object[] args)
2021-12-10 13:38:03 +01:00
{
try
{
2022-05-22 15:55:26 +08:00
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == ClientID);
2021-12-10 13:38:03 +01:00
if (userConnection == null)
{
2022-05-22 15:55:26 +08:00
Logging.Error($"[Client->SendNativeResponse(Action<object> callback, ulong hash, Type type, params object[] args)]: Connection \"{ClientID}\" not found!");
2021-12-10 13:38:03 +01:00
return;
}
if (args != null && args.Length == 0)
2021-12-10 13:38:03 +01:00
{
Logging.Error($"[Client->SendNativeCall(ulong hash, Dictionary<string, object> args)]: Missing arguments!");
return;
2021-12-10 13:38:03 +01:00
}
long id = ++_callbacksCount;
Callbacks.Add(id, callback);
byte returnTypeValue = 0x00;
if (returnType == typeof(int))
2021-12-10 13:38:03 +01:00
{
// NOTHING BECAUSE VALUE IS 0x00
2021-12-10 13:38:03 +01:00
}
else if (returnType == typeof(bool))
2021-12-10 13:38:03 +01:00
{
returnTypeValue = 0x01;
2021-12-10 13:38:03 +01:00
}
else if (returnType == typeof(float))
2021-12-10 13:38:03 +01:00
{
returnTypeValue = 0x02;
2021-12-10 13:38:03 +01:00
}
else if (returnType == typeof(string))
2021-12-10 13:38:03 +01:00
{
returnTypeValue = 0x03;
2021-12-10 13:38:03 +01:00
}
else if (returnType == typeof(LVector3))
2021-12-10 13:38:03 +01:00
{
returnTypeValue = 0x04;
2021-12-10 13:38:03 +01:00
}
else
2021-12-10 13:38:03 +01:00
{
Logging.Error($"[Client->SendNativeCall(ulong hash, Dictionary<string, object> args)]: Missing return type!");
return;
2021-12-10 13:38:03 +01:00
}
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
2022-01-01 03:07:18 +01:00
new Packets.NativeResponse()
2021-12-10 13:38:03 +01:00
{
Hash = hash,
Args = new List<object>(args) ?? new List<object>(),
ResultType = returnTypeValue,
ID = id
2022-05-22 15:55:26 +08:00
}.Pack(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Native);
2021-12-10 13:38:03 +01:00
}
catch (Exception e)
{
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
}
}
public void SendCleanUpWorld()
{
2022-05-22 15:55:26 +08:00
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == ClientID);
if (userConnection == null)
{
2022-05-22 15:55:26 +08:00
Logging.Error($"[Client->SendCleanUpWorld()]: Connection \"{ClientID}\" not found!");
return;
}
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
2022-01-01 03:07:18 +01:00
outgoingMessage.Write((byte)PacketTypes.CleanUpWorld);
Server.MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Default);
}
2022-01-01 03:07:18 +01:00
public void SendModPacket(string modName, byte customID, byte[] bytes)
2021-11-23 23:17:47 +01:00
{
try
2021-11-23 23:17:47 +01:00
{
2022-05-22 15:55:26 +08:00
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == ClientID);
if (userConnection == null)
{
return;
}
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
2022-01-01 03:07:18 +01:00
new Packets.Mod()
{
NetHandle = 0,
Target = 0,
2022-01-01 03:07:18 +01:00
Name = modName,
CustomPacketID = customID,
Bytes = bytes
2022-05-22 15:55:26 +08:00
}.Pack(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Mod);
Server.MainNetServer.FlushSendQueue();
}
catch (Exception e)
{
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
}
2021-11-23 23:17:47 +01:00
}
2022-04-12 06:04:02 +02:00
public void SendTriggerEvent(string eventName, params object[] args)
{
if (!FilesReceived)
{
Logging.Warning($"Player \"{Player.Username}\" doesn't have all the files yet!");
return;
}
try
{
2022-05-22 15:55:26 +08:00
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == ClientID);
if (userConnection == null)
{
return;
}
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
new Packets.ServerClientEvent()
{
EventName = eventName,
Args = new List<object>(args)
2022-05-22 15:55:26 +08:00
}.Pack(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Event);
Server.MainNetServer.FlushSendQueue();
}
catch (Exception e)
{
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
}
}
#endregion
2021-08-26 17:01:32 +02:00
}
}