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
{
public long ID = 0 ;
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-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-11-25 16:32:04 +01:00
Server . MainNetServer . Connections . Find ( x = > x . RemoteUniqueIdentifier = = ID ) ? . 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 )
{
Server . MainNetServer . Connections . Find ( x = > x . RemoteUniqueIdentifier = = ID ) ? . Disconnect ( 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
{
2021-11-25 16:32:04 +01:00
NetConnection userConnection = Server . MainNetServer . Connections . Find ( x = > x . RemoteUniqueIdentifier = = ID ) ;
if ( userConnection = = null )
{
return ;
}
ChatMessagePacket packet = new ( )
{
Username = from ,
Message = message
} ;
NetOutgoingMessage outgoingMessage = Server . MainNetServer . CreateMessage ( ) ;
packet . PacketToNetOutGoingMessage ( outgoingMessage ) ;
2021-12-13 22:41:07 +01:00
Server . MainNetServer . SendMessage ( outgoingMessage , userConnection , NetDeliveryMethod . ReliableOrdered , ( int ) ConnectionChannel . Chat ) ;
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
}
public void SendNativeCall ( ulong hash , params object [ ] args )
{
2021-11-25 16:32:04 +01:00
try
2021-08-26 17:01:32 +02:00
{
2021-11-25 16:32:04 +01:00
NetConnection userConnection = Server . MainNetServer . Connections . Find ( x = > x . RemoteUniqueIdentifier = = ID ) ;
if ( userConnection = = null )
{
2021-12-10 16:25:59 +01:00
Logging . Error ( $"[Client->SendNativeCall(ulong hash, params object[] args)]: Connection \" { ID } \ " not found!" ) ;
2021-11-25 16:32:04 +01:00
return ;
}
2021-12-11 14:12:38 +01:00
List < NativeArgument > arguments = null ;
if ( args ! = null & & args . Length > 0 )
2021-11-25 16:32:04 +01:00
{
2021-12-11 14:12:38 +01:00
arguments = Util . ParseNativeArguments ( args ) ;
if ( arguments = = null )
{
Logging . Error ( $"[Client->SendNativeCall(ulong hash, params object[] args)]: Missing arguments!" ) ;
return ;
}
2021-11-25 16:32:04 +01:00
}
2021-12-11 14:12:38 +01:00
2021-11-25 16:32:04 +01:00
NativeCallPacket packet = new ( )
{
Hash = hash ,
Args = arguments
} ;
NetOutgoingMessage outgoingMessage = Server . MainNetServer . CreateMessage ( ) ;
packet . PacketToNetOutGoingMessage ( outgoingMessage ) ;
2021-12-13 22:41:07 +01:00
Server . MainNetServer . SendMessage ( outgoingMessage , userConnection , NetDeliveryMethod . ReliableOrdered , ( int ) 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-10 13:38:03 +01:00
public void SendNativeResponse ( Action < object > callback , ulong hash , Type type , params object [ ] args )
{
try
{
NetConnection userConnection = Server . MainNetServer . Connections . Find ( x = > x . RemoteUniqueIdentifier = = ID ) ;
if ( userConnection = = null )
{
2021-12-10 16:25:59 +01:00
Logging . Error ( $"[Client->SendNativeResponse(Action<object> callback, ulong hash, Type type, params object[] args)]: Connection \" { ID } \ " not found!" ) ;
2021-12-10 13:38:03 +01:00
return ;
}
NativeArgument returnType = null ;
Type typeOf = type ;
if ( typeOf = = typeof ( int ) )
{
returnType = new IntArgument ( ) ;
}
else if ( typeOf = = typeof ( bool ) )
{
returnType = new BoolArgument ( ) ;
}
else if ( typeOf = = typeof ( float ) )
{
returnType = new FloatArgument ( ) ;
}
else if ( typeOf = = typeof ( string ) )
{
returnType = new StringArgument ( ) ;
}
else if ( typeOf = = typeof ( LVector3 ) )
{
returnType = new LVector3Argument ( ) ;
}
else
{
2021-12-10 16:25:59 +01:00
Logging . Error ( $"[Client->SendNativeResponse(Action<object> callback, ulong hash, Type type, params object[] args)]: Argument does not exist!" ) ;
2021-12-10 13:38:03 +01:00
return ;
}
2021-12-11 14:12:38 +01:00
List < NativeArgument > arguments = null ;
if ( args ! = null & & args . Length > 0 )
2021-12-10 13:38:03 +01:00
{
2021-12-11 14:12:38 +01:00
arguments = Util . ParseNativeArguments ( args ) ;
if ( arguments = = null )
{
Logging . Error ( $"[Client->SendNativeResponse(Action<object> callback, ulong hash, Type type, params object[] args)]: One or more arguments do not exist!" ) ;
return ;
}
2021-12-10 13:38:03 +01:00
}
long id = 0 ;
Callbacks . Add ( id = Environment . TickCount64 , callback ) ;
NativeResponsePacket packet = new ( )
{
Hash = hash ,
Args = arguments ,
Type = returnType ,
ID = id
} ;
NetOutgoingMessage outgoingMessage = Server . MainNetServer . CreateMessage ( ) ;
packet . PacketToNetOutGoingMessage ( outgoingMessage ) ;
2021-12-13 22:41:07 +01:00
Server . MainNetServer . SendMessage ( outgoingMessage , userConnection , NetDeliveryMethod . ReliableOrdered , ( int ) 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-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-11-25 16:32:04 +01:00
NetConnection userConnection = Server . MainNetServer . Connections . Find ( x = > x . RemoteUniqueIdentifier = = ID ) ;
if ( userConnection = = null )
{
return ;
}
NetOutgoingMessage outgoingMessage = Server . MainNetServer . CreateMessage ( ) ;
new ModPacket ( )
{
ID = 0 ,
Target = 0 ,
Mod = mod ,
CustomPacketID = customID ,
Bytes = bytes
} . PacketToNetOutGoingMessage ( outgoingMessage ) ;
2021-12-13 22:41:07 +01:00
Server . MainNetServer . SendMessage ( outgoingMessage , userConnection , NetDeliveryMethod . ReliableOrdered , ( int ) 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
}
}