217 lines
7.6 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;
using GTA.Math;
using RageCoop.Core.Scripting.Events;
2021-08-26 17:01:32 +02:00
2022-05-22 15:55:26 +08:00
namespace RageCoop.Server
2021-08-26 17:01:32 +02:00
{
public class Client
{
public long NetID = 0;
2022-06-04 18:09:42 +08:00
internal NetConnection Connection { get; set; }
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 IsReady { get; internal set; }=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
2022-06-04 18:09:42 +08:00
public void Kick(string reason="You has been kicked!")
2021-12-11 14:00:22 +01:00
{
2022-06-04 18:09:42 +08:00
Connection?.Disconnect(reason);
2021-12-11 14:00:22 +01:00
}
2022-06-04 18:09:42 +08:00
public void Kick(params string[] reasons)
2022-04-08 22:30:34 +02:00
{
2022-06-04 18:09:42 +08:00
Kick(string.Join(" ", reasons));
2022-04-08 22:30:34 +02:00
}
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
{
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetID);
if (userConnection == null)
{
return;
}
Server.SendChatMessage(from, message, userConnection);
}
catch (Exception e)
{
2022-05-31 19:35:01 -08:00
Program.Logger.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
{
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetID);
if (userConnection == null)
{
2022-05-31 19:35:01 -08:00
Program.Logger.Error($"[Client->SendNativeCall(ulong hash, params object[] args)]: Connection \"{NetID}\" not found!");
return;
}
if (args != null && args.Length == 0)
{
2022-05-31 19:35:01 -08:00
Program.Logger.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
{
2022-05-31 19:35:01 -08:00
Program.Logger.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
{
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetID);
2021-12-10 13:38:03 +01:00
if (userConnection == null)
{
2022-05-31 19:35:01 -08:00
Program.Logger.Error($"[Client->SendNativeResponse(Action<object> callback, ulong hash, Type type, params object[] args)]: Connection \"{NetID}\" 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
{
2022-05-31 19:35:01 -08:00
Program.Logger.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(Vector3))
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
{
2022-05-31 19:35:01 -08:00
Program.Logger.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)
{
2022-05-31 19:35:01 -08:00
Program.Logger.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
2021-12-10 13:38:03 +01:00
}
}
public void SendCleanUpWorld()
{
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetID);
if (userConnection == null)
{
2022-05-31 19:35:01 -08:00
Program.Logger.Error($"[Client->SendCleanUpWorld()]: Connection \"{NetID}\" 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);
}
public void SendCustomEvent(int id,byte[] data)
{
if (!IsReady)
{
Program.Logger.Warning($"Player \"{Player.Username}\" is not ready!");
return;
}
try
{
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
new Packets.CustomEvent()
{
Hash=id,
Data=data
2022-05-22 15:55:26 +08:00
}.Pack(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, Connection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Event);
}
catch (Exception ex)
{
Program.Logger.Error(ex);
}
}
#endregion
2021-08-26 17:01:32 +02:00
}
}