2021-11-25 16:32:04 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-08-26 17:01:32 +02:00
|
|
|
|
|
|
|
|
|
using Lidgren.Network;
|
|
|
|
|
|
|
|
|
|
namespace CoopServer
|
|
|
|
|
{
|
|
|
|
|
public class Client
|
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
public long NetHandle = 0;
|
2021-08-26 17:01:32 +02:00
|
|
|
|
public float Latency = 0.0f;
|
|
|
|
|
public PlayerData Player;
|
2021-09-26 21:19:26 +02:00
|
|
|
|
private readonly Dictionary<string, object> CustomData = new();
|
2021-12-26 03:25:00 +01:00
|
|
|
|
private long CallbacksCount = 0;
|
2021-12-10 13:38:03 +01:00
|
|
|
|
internal readonly Dictionary<long, Action<object>> Callbacks = new();
|
2021-09-26 21:19:26 +02:00
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2021-11-25 16:32:04 +01:00
|
|
|
|
#region FUNCTIONS
|
2021-08-26 17:01:32 +02:00
|
|
|
|
public void Kick(string[] reason)
|
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetHandle)?.Disconnect(string.Join(" ", reason));
|
2021-08-26 17:01:32 +02:00
|
|
|
|
}
|
2021-12-11 14:00:22 +01:00
|
|
|
|
public void Kick(string reason)
|
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetHandle)?.Disconnect(reason);
|
2021-12-11 14:00:22 +01:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetHandle);
|
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
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetHandle);
|
2021-11-25 16:32:04 +01:00
|
|
|
|
if (userConnection == null)
|
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
Logging.Error($"[Client->SendNativeCall(ulong hash, params object[] args)]: Connection \"{NetHandle}\" 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NativeCallPacket packet = new()
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
packet.PacketToNetOutGoingMessage(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
|
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetHandle);
|
2021-12-10 13:38:03 +01:00
|
|
|
|
if (userConnection == null)
|
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
Logging.Error($"[Client->SendNativeResponse(Action<object> callback, ulong hash, Type type, params object[] args)]: Connection \"{NetHandle}\" 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
|
|
|
|
|
2021-12-26 03:25:00 +01:00
|
|
|
|
long id = ++CallbacksCount;
|
|
|
|
|
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();
|
|
|
|
|
new NativeResponsePacket()
|
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
|
|
|
|
|
}.PacketToNetOutGoingMessage(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} <<");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 03:25:00 +01:00
|
|
|
|
public void SendCleanUpWorld()
|
|
|
|
|
{
|
|
|
|
|
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetHandle);
|
|
|
|
|
if (userConnection == null)
|
|
|
|
|
{
|
|
|
|
|
Logging.Error($"[Client->SendCleanUpWorld()]: Connection \"{NetHandle}\" not found!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
|
|
|
|
|
outgoingMessage.Write((byte)PacketTypes.CleanUpWorldPacket);
|
|
|
|
|
Server.MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Default);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 23:17:47 +01:00
|
|
|
|
public void SendModPacket(string mod, byte customID, byte[] bytes)
|
|
|
|
|
{
|
2021-11-25 16:32:04 +01:00
|
|
|
|
try
|
2021-11-23 23:17:47 +01:00
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetHandle);
|
2021-11-25 16:32:04 +01:00
|
|
|
|
if (userConnection == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
|
|
|
|
|
new ModPacket()
|
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
NetHandle = 0,
|
2021-11-25 16:32:04 +01:00
|
|
|
|
Target = 0,
|
|
|
|
|
Mod = mod,
|
|
|
|
|
CustomPacketID = customID,
|
|
|
|
|
Bytes = bytes
|
|
|
|
|
}.PacketToNetOutGoingMessage(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
|
|
|
|
}
|
2021-11-25 16:32:04 +01:00
|
|
|
|
#endregion
|
2021-08-26 17:01:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|