2021-11-25 16:32:04 +01:00
|
|
|
|
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;
|
2022-04-10 14:34:55 +02:00
|
|
|
|
private float _currentLatency = 0f;
|
2022-01-01 15:58:14 +01:00
|
|
|
|
public float Latency
|
|
|
|
|
{
|
2022-04-10 14:34:55 +02:00
|
|
|
|
get => _currentLatency;
|
2022-05-22 15:55:26 +08:00
|
|
|
|
set
|
2022-01-01 15:58:14 +01:00
|
|
|
|
{
|
2022-04-10 14:34:55 +02: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;
|
2022-04-10 14:34:55 +02:00
|
|
|
|
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;
|
2021-09-26 21:19:26 +02:00
|
|
|
|
|
|
|
|
|
#region CUSTOMDATA FUNCTIONS
|
|
|
|
|
public void SetData<T>(string name, T data)
|
|
|
|
|
{
|
|
|
|
|
if (HasData(name))
|
|
|
|
|
{
|
2022-04-10 14:34:55 +02:00
|
|
|
|
_customData[name] = data;
|
2021-09-26 21:19:26 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-10 14:34:55 +02:00
|
|
|
|
_customData.Add(name, data);
|
2021-09-26 21:19:26 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasData(string name)
|
|
|
|
|
{
|
2022-04-10 14:34:55 +02:00
|
|
|
|
return _customData.ContainsKey(name);
|
2021-09-26 21:19:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T GetData<T>(string name)
|
|
|
|
|
{
|
2022-04-10 14:34:55 +02:00
|
|
|
|
return HasData(name) ? (T)_customData[name] : default;
|
2021-09-26 21:19:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveData(string name)
|
|
|
|
|
{
|
|
|
|
|
if (HasData(name))
|
|
|
|
|
{
|
2022-04-10 14:34:55 +02:00
|
|
|
|
_customData.Remove(name);
|
2021-09-26 21:19:26 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2021-08-26 17:01:32 +02:00
|
|
|
|
|
2021-11-25 16:32:04 +01: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")
|
|
|
|
|
{
|
2021-11-25 16:32:04 +01: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);
|
2021-11-25 16:32:04 +01:00
|
|
|
|
if (userConnection == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-23 22:03:01 +01:00
|
|
|
|
Server.SendChatMessage(from, message, userConnection);
|
2021-11-25 16:32:04 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
|
|
|
|
|
}
|
2021-08-26 17:01:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 03:25:00 +01:00
|
|
|
|
public void SendNativeCall(ulong hash, params object[] args)
|
2021-08-26 17:01:32 +02:00
|
|
|
|
{
|
2021-11-25 16:32:04 +01: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);
|
2021-11-25 16:32:04 +01:00
|
|
|
|
if (userConnection == null)
|
|
|
|
|
{
|
2022-05-22 15:55:26 +08:00
|
|
|
|
Logging.Error($"[Client->SendNativeCall(ulong hash, params object[] args)]: Connection \"{ClientID}\" not found!");
|
2021-11-25 16:32:04 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 03:25:00 +01:00
|
|
|
|
if (args != null && args.Length == 0)
|
2021-11-25 16:32:04 +01:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
Logging.Error($"[Client->SendNativeCall(ulong hash, Dictionary<string, object> args)]: Missing arguments!");
|
|
|
|
|
return;
|
2021-11-25 16:32:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-01 03:07:18 +01:00
|
|
|
|
Packets.NativeCall packet = new()
|
2021-11-25 16:32:04 +01:00
|
|
|
|
{
|
|
|
|
|
Hash = hash,
|
2021-12-26 03:25:00 +01:00
|
|
|
|
Args = new List<object>(args) ?? new List<object>(),
|
2021-11-25 16:32:04 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
|
2022-05-22 15:55:26 +08:00
|
|
|
|
packet.Pack(outgoingMessage);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
Server.MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Native);
|
2021-08-26 17:01:32 +02:00
|
|
|
|
}
|
2021-11-25 16:32:04 +01:00
|
|
|
|
catch (Exception e)
|
2021-08-26 17:01:32 +02:00
|
|
|
|
{
|
2021-11-25 16:32:04 +01: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
|
|
|
|
|
2021-12-26 03:25:00 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 03:25:00 +01:00
|
|
|
|
if (args != null && args.Length == 0)
|
2021-12-10 13:38:03 +01:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
Logging.Error($"[Client->SendNativeCall(ulong hash, Dictionary<string, object> args)]: Missing arguments!");
|
|
|
|
|
return;
|
2021-12-10 13:38:03 +01:00
|
|
|
|
}
|
2021-12-23 22:03:01 +01:00
|
|
|
|
|
2022-04-10 14:34:55 +02:00
|
|
|
|
long id = ++_callbacksCount;
|
2021-12-26 03:25:00 +01:00
|
|
|
|
Callbacks.Add(id, callback);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
|
|
|
|
|
byte returnTypeValue = 0x00;
|
|
|
|
|
if (returnType == typeof(int))
|
2021-12-10 13:38:03 +01:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
// NOTHING BECAUSE VALUE IS 0x00
|
2021-12-10 13:38:03 +01:00
|
|
|
|
}
|
2021-12-23 22:03:01 +01:00
|
|
|
|
else if (returnType == typeof(bool))
|
2021-12-10 13:38:03 +01:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
returnTypeValue = 0x01;
|
2021-12-10 13:38:03 +01:00
|
|
|
|
}
|
2021-12-23 22:03:01 +01:00
|
|
|
|
else if (returnType == typeof(float))
|
2021-12-10 13:38:03 +01:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
returnTypeValue = 0x02;
|
2021-12-10 13:38:03 +01:00
|
|
|
|
}
|
2021-12-23 22:03:01 +01:00
|
|
|
|
else if (returnType == typeof(string))
|
2021-12-10 13:38:03 +01:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
returnTypeValue = 0x03;
|
2021-12-10 13:38:03 +01:00
|
|
|
|
}
|
2021-12-23 22:03:01 +01:00
|
|
|
|
else if (returnType == typeof(LVector3))
|
2021-12-10 13:38:03 +01:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
returnTypeValue = 0x04;
|
2021-12-10 13:38:03 +01:00
|
|
|
|
}
|
2021-12-23 22:03:01 +01:00
|
|
|
|
else
|
2021-12-10 13:38:03 +01:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
Logging.Error($"[Client->SendNativeCall(ulong hash, Dictionary<string, object> args)]: Missing return type!");
|
|
|
|
|
return;
|
2021-12-10 13:38:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-23 22:03:01 +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,
|
2021-12-26 03:25:00 +01:00
|
|
|
|
Args = new List<object>(args) ?? new List<object>(),
|
2021-12-23 22:03:01 +01:00
|
|
|
|
ResultType = returnTypeValue,
|
|
|
|
|
ID = id
|
2022-05-22 15:55:26 +08:00
|
|
|
|
}.Pack(outgoingMessage);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
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} <<");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 03:25:00 +01:00
|
|
|
|
public void SendCleanUpWorld()
|
|
|
|
|
{
|
2022-05-22 15:55:26 +08:00
|
|
|
|
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == ClientID);
|
2021-12-26 03:25:00 +01:00
|
|
|
|
if (userConnection == null)
|
|
|
|
|
{
|
2022-05-22 15:55:26 +08:00
|
|
|
|
Logging.Error($"[Client->SendCleanUpWorld()]: Connection \"{ClientID}\" not found!");
|
2021-12-26 03:25:00 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
|
2022-01-01 03:07:18 +01:00
|
|
|
|
outgoingMessage.Write((byte)PacketTypes.CleanUpWorld);
|
2021-12-26 03:25:00 +01:00
|
|
|
|
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
|
|
|
|
{
|
2021-11-25 16:32:04 +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);
|
2021-11-25 16:32:04 +01:00
|
|
|
|
if (userConnection == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
|
2022-01-01 03:07:18 +01:00
|
|
|
|
new Packets.Mod()
|
2021-11-25 16:32:04 +01:00
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
NetHandle = 0,
|
2021-11-25 16:32:04 +01:00
|
|
|
|
Target = 0,
|
2022-01-01 03:07:18 +01:00
|
|
|
|
Name = modName,
|
2021-11-25 16:32:04 +01:00
|
|
|
|
CustomPacketID = customID,
|
|
|
|
|
Bytes = bytes
|
2022-05-22 15:55:26 +08:00
|
|
|
|
}.Pack(outgoingMessage);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
Server.MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Mod);
|
2021-11-25 16:32:04 +01:00
|
|
|
|
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 05:05:27 +02:00
|
|
|
|
|
2022-04-12 06:04:02 +02:00
|
|
|
|
public void SendTriggerEvent(string eventName, params object[] args)
|
2022-04-12 05:05:27 +02:00
|
|
|
|
{
|
2022-04-12 07:40:56 +02:00
|
|
|
|
if (!FilesReceived)
|
|
|
|
|
{
|
|
|
|
|
Logging.Warning($"Player \"{Player.Username}\" doesn't have all the files yet!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-12 05:05:27 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-05-22 15:55:26 +08:00
|
|
|
|
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == ClientID);
|
2022-04-12 05:05:27 +02:00
|
|
|
|
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);
|
2022-04-12 05:05:27 +02:00
|
|
|
|
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} <<");
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-25 16:32:04 +01:00
|
|
|
|
#endregion
|
2021-08-26 17:01:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|