RAGECOOP-V/Server/ServerScript.cs

268 lines
8.6 KiB
C#
Raw Normal View History

2021-08-15 07:54:25 +02:00
using System;
using System.Collections.Generic;
2021-08-18 12:47:36 +02:00
using System.ComponentModel;
2021-08-15 07:54:25 +02:00
using Lidgren.Network;
2021-08-14 21:49:23 +02:00
namespace CoopServer
{
2021-08-18 12:47:36 +02:00
public abstract class ServerScript
2021-08-15 07:54:25 +02:00
{
2021-08-20 17:28:13 +02:00
public API API { get; } = new();
2021-08-18 12:47:36 +02:00
}
public class API
{
#region DELEGATES
public delegate void ChatEvent(string username, string message, CancelEventArgs cancel);
public delegate void PlayerEvent(Entities.EntitiesPlayer player);
#endregion
#region EVENTS
public event EventHandler OnStart;
public event ChatEvent OnChatMessage;
public event PlayerEvent OnPlayerConnected;
public event PlayerEvent OnPlayerDisconnected;
2021-08-20 17:28:13 +02:00
public event PlayerEvent OnPlayerPositionUpdate;
2021-08-18 12:47:36 +02:00
internal void InvokeStart()
{
OnStart?.Invoke(this, EventArgs.Empty);
}
2021-08-15 07:54:25 +02:00
2021-08-20 17:28:13 +02:00
internal void InvokePlayerConnected(Entities.EntitiesPlayer player)
2021-08-15 07:54:25 +02:00
{
2021-08-18 12:47:36 +02:00
OnPlayerConnected?.Invoke(player);
2021-08-15 07:54:25 +02:00
}
2021-08-20 17:28:13 +02:00
internal void InvokePlayerDisconnected(Entities.EntitiesPlayer player)
2021-08-15 07:54:25 +02:00
{
2021-08-18 12:47:36 +02:00
OnPlayerDisconnected?.Invoke(player);
2021-08-18 11:47:59 +02:00
}
2021-08-15 07:54:25 +02:00
2021-08-18 12:47:36 +02:00
internal bool InvokeChatMessage(string username, string message)
2021-08-18 11:47:59 +02:00
{
2021-08-18 12:47:36 +02:00
var args = new CancelEventArgs(false);
OnChatMessage?.Invoke(username, message, args);
return args.Cancel;
2021-08-15 07:54:25 +02:00
}
2021-08-20 17:28:13 +02:00
internal void InvokePlayerPositionUpdate(Entities.EntitiesPlayer player)
{
OnPlayerPositionUpdate?.Invoke(player);
}
2021-08-18 12:47:36 +02:00
#endregion
2021-08-15 07:54:25 +02:00
2021-08-18 12:47:36 +02:00
#region FUNCTIONS
2021-08-20 17:28:13 +02:00
public static void SendNativeCallToAll(ulong hash, params object[] args)
2021-08-15 07:54:25 +02:00
{
2021-08-20 17:28:13 +02:00
List<NetConnection> connections = Server.MainNetServer.Connections;
if (connections.Count == 0)
{
return;
}
2021-08-18 11:47:59 +02:00
2021-08-20 17:28:13 +02:00
List<NativeArgument> arguments = new();
foreach (object arg in args)
2021-08-15 07:54:25 +02:00
{
2021-08-20 17:28:13 +02:00
Type typeOf = arg.GetType();
if (typeOf == typeof(int))
{
arguments.Add(new IntArgument() { Data = (int)arg });
}
else if (typeOf == typeof(bool))
{
arguments.Add(new BoolArgument() { Data = (bool)arg });
}
else if (typeOf == typeof(float))
{
arguments.Add(new FloatArgument() { Data = (float)arg });
}
else if (typeOf == typeof(LVector3))
{
arguments.Add(new LVector3Argument() { Data = (LVector3)arg });
}
else
{
Logging.Error("[ServerScript->SendNativeCallToAll(" + hash + ", params object[] args)]: Type of argument not found!");
return;
}
2021-08-15 07:54:25 +02:00
}
2021-08-20 17:28:13 +02:00
NativeCallPacket packet = new()
{
Hash = hash,
Args = arguments
};
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
packet.PacketToNetOutGoingMessage(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, Server.MainNetServer.Connections, NetDeliveryMethod.ReliableOrdered, 0);
2021-08-18 11:47:59 +02:00
}
2021-08-15 07:54:25 +02:00
2021-08-20 17:28:13 +02:00
public static void SendNativeCallToPlayer(string username, ulong hash, params object[] args)
2021-08-18 11:47:59 +02:00
{
2021-08-20 17:28:13 +02:00
NetConnection userConnection = Util.GetConnectionByUsername(username);
if (userConnection == null)
{
Logging.Warning("[ServerScript->SendNativeCallToPlayer(\"" + username + "\", \"" + hash + "\", params object[] args)]: User not found!");
return;
}
List<NativeArgument> arguments = new();
foreach (object arg in args)
2021-08-15 07:54:25 +02:00
{
2021-08-20 17:28:13 +02:00
Type typeOf = arg.GetType();
if (typeOf == typeof(int))
{
arguments.Add(new IntArgument() { Data = (int)arg });
}
else if (typeOf == typeof(bool))
{
arguments.Add(new BoolArgument() { Data = (bool)arg });
}
else if (typeOf == typeof(float))
{
arguments.Add(new FloatArgument() { Data = (float)arg });
}
else if (typeOf == typeof(LVector3))
{
arguments.Add(new LVector3Argument() { Data = (LVector3)arg });
}
else
{
Logging.Error("[ServerScript->SendNativeCallToAll(" + hash + ", params object[] args)]: Type of argument not found!");
return;
}
2021-08-15 07:54:25 +02:00
}
2021-08-20 17:28:13 +02:00
NativeCallPacket packet = new()
{
Hash = hash,
Args = arguments
};
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
packet.PacketToNetOutGoingMessage(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, 0);
}
public static List<long> GetAllConnections()
{
List<long> result = new();
Server.MainNetServer.Connections.ForEach(x => result.Add(x.RemoteUniqueIdentifier));
return result;
}
public static int GetAllPlayersCount()
{
return Server.Players.Count;
2021-08-18 11:47:59 +02:00
}
2021-08-15 07:54:25 +02:00
2021-08-18 11:47:59 +02:00
public static Dictionary<long, Entities.EntitiesPlayer> GetAllPlayers()
{
2021-08-20 17:28:13 +02:00
return Server.Players;
2021-08-18 11:47:59 +02:00
}
2021-08-15 07:54:25 +02:00
2021-08-18 11:47:59 +02:00
public static void KickPlayerByUsername(string username, string[] reason)
{
2021-08-20 17:28:13 +02:00
NetConnection userConnection = Util.GetConnectionByUsername(username);
if (userConnection == null)
2021-08-15 07:54:25 +02:00
{
2021-08-20 17:28:13 +02:00
Logging.Warning("[ServerScript->KickPlayerByUsername(\"" + username + "\", \"" + string.Join(" ", reason) + "\")]: User not found!");
return;
2021-08-15 07:54:25 +02:00
}
2021-08-20 17:28:13 +02:00
userConnection.Disconnect(string.Join(" ", reason));
2021-08-15 07:54:25 +02:00
}
2021-08-18 11:47:59 +02:00
public static void SendChatMessageToAll(string message, string username = "Server")
2021-08-14 21:49:23 +02:00
{
2021-08-18 11:47:59 +02:00
List<NetConnection> connections = Server.MainNetServer.Connections;
2021-08-20 17:28:13 +02:00
if (connections.Count == 0)
2021-08-15 07:54:25 +02:00
{
2021-08-20 17:28:13 +02:00
return;
2021-08-15 07:54:25 +02:00
}
2021-08-20 17:28:13 +02:00
ChatMessagePacket packet = new()
{
Username = username,
Message = message
};
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
packet.PacketToNetOutGoingMessage(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, Server.MainNetServer.Connections, NetDeliveryMethod.ReliableOrdered, 0);
2021-08-14 21:49:23 +02:00
}
2021-08-18 11:47:59 +02:00
public static void SendChatMessageToPlayer(string username, string message, string from = "Server")
2021-08-14 21:49:23 +02:00
{
2021-08-20 17:28:13 +02:00
NetConnection userConnection = Util.GetConnectionByUsername(username);
if (userConnection == null)
2021-08-15 07:54:25 +02:00
{
2021-08-20 17:28:13 +02:00
Logging.Warning("[ServerScript->SendChatMessageToPlayer(\"" + username + "\", \"" + message + "\", \"" + from + "\")]: User not found!");
return;
2021-08-15 07:54:25 +02:00
}
2021-08-20 17:28:13 +02:00
ChatMessagePacket packet = new()
{
Username = from,
Message = message
};
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
packet.PacketToNetOutGoingMessage(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, 0);
2021-08-14 21:49:23 +02:00
}
2021-08-18 11:47:59 +02:00
public static void RegisterCommand(string name, Action<CommandContext> callback)
2021-08-14 21:49:23 +02:00
{
2021-08-18 11:47:59 +02:00
Server.RegisterCommand(name, callback);
}
2021-08-14 21:49:23 +02:00
2021-08-18 11:47:59 +02:00
public static void RegisterCommands<T>()
{
Server.RegisterCommands<T>();
2021-08-14 21:49:23 +02:00
}
2021-08-18 12:47:36 +02:00
#endregion
2021-08-18 11:47:59 +02:00
}
public class Command
{
public string Name { get; set; }
}
2021-08-15 07:54:25 +02:00
2021-08-18 11:47:59 +02:00
[AttributeUsage(AttributeTargets.Method)]
public class CommandAttribute : Attribute
{
/// <summary>
/// Sets name of the command
/// </summary>
public string Name { get; set; }
public CommandAttribute(string name)
2021-08-15 07:54:25 +02:00
{
2021-08-18 11:47:59 +02:00
Name = name;
2021-08-15 07:54:25 +02:00
}
2021-08-14 21:49:23 +02:00
}
2021-08-18 11:47:59 +02:00
public class CommandContext
{
/// <summary>
/// Gets the client which executed the command
/// </summary>
public Entities.EntitiesPlayer Player { get; internal set; }
/// <summary>
/// Gets the chatdata associated with the command
/// </summary>
public string[] Args { get; internal set; }
}
2021-08-14 21:49:23 +02:00
}