RAGECOOP-V/Server/ServerScript.cs

177 lines
5.1 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-22 13:59:15 +02:00
using System.Linq;
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);
2021-08-26 17:01:32 +02:00
public delegate void PlayerEvent(Client client);
2021-09-30 23:35:42 +02:00
public delegate void ModEvent(long from, long target, string mod, byte customID, byte[] bytes, CancelEventArgs args);
2021-08-18 12:47:36 +02:00
#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-09-29 14:34:22 +02:00
public event ModEvent OnModPacketReceived;
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-26 17:01:32 +02:00
internal void InvokePlayerConnected(Client client)
2021-08-15 07:54:25 +02:00
{
2021-08-26 17:01:32 +02:00
OnPlayerConnected?.Invoke(client);
2021-08-15 07:54:25 +02:00
}
2021-08-26 17:01:32 +02:00
internal void InvokePlayerDisconnected(Client client)
2021-08-15 07:54:25 +02:00
{
2021-08-26 17:01:32 +02:00
OnPlayerDisconnected?.Invoke(client);
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
{
CancelEventArgs args = new(false);
2021-08-18 12:47:36 +02:00
OnChatMessage?.Invoke(username, message, args);
return args.Cancel;
2021-08-15 07:54:25 +02:00
}
2021-08-20 17:28:13 +02:00
2021-08-26 17:01:32 +02:00
internal void InvokePlayerPositionUpdate(PlayerData playerData)
2021-08-20 17:28:13 +02:00
{
2021-08-26 17:01:32 +02:00
OnPlayerPositionUpdate?.Invoke(Server.Clients.First(x => x.Player.Username == playerData.Username));
2021-08-20 17:28:13 +02:00
}
2021-09-29 14:34:22 +02:00
2021-09-30 23:35:42 +02:00
internal bool InvokeModPacketReceived(long from, long target, string mod, byte customID, byte[] bytes)
2021-09-29 14:34:22 +02:00
{
CancelEventArgs args = new(false);
2021-09-30 23:35:42 +02:00
OnModPacketReceived?.Invoke(from, target, mod, customID, bytes, args);
2021-09-29 14:34:22 +02:00
return args.Cancel;
}
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-26 17:01:32 +02:00
if (Server.MainNetServer.ConnectionsCount == 0)
2021-08-20 17:28:13 +02:00
{
return;
}
2021-08-18 11:47:59 +02:00
2021-08-26 17:01:32 +02:00
List<NativeArgument> arguments;
if ((arguments = Util.ParseNativeArguments(args)) == null)
2021-08-15 07:54:25 +02:00
{
2021-08-21 16:52:17 +02:00
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 List<long> GetAllConnections()
{
List<long> result = new();
Server.MainNetServer.Connections.ForEach(x => result.Add(x.RemoteUniqueIdentifier));
return result;
}
2021-08-26 17:01:32 +02:00
public static int GetAllClientsCount()
2021-08-18 11:47:59 +02:00
{
2021-08-26 17:01:32 +02:00
return Server.Clients.Count;
2021-08-18 11:47:59 +02:00
}
2021-08-15 07:54:25 +02:00
2021-08-26 17:01:32 +02:00
public static List<Client> GetAllClients()
2021-08-22 13:59:15 +02:00
{
2021-08-26 17:01:32 +02:00
return Server.Clients;
2021-08-22 13:59:15 +02:00
}
2021-08-26 17:01:32 +02:00
public static Client GetClientByUsername(string username)
2021-08-18 11:47:59 +02:00
{
2021-08-26 17:01:32 +02:00
return Server.Clients.FirstOrDefault(x => x.Player.Username == username);
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-26 17:01:32 +02:00
if (Server.MainNetServer.ConnectionsCount == 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 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>
2021-08-26 17:01:32 +02:00
public Client Client { get; internal set; }
2021-08-18 11:47:59 +02:00
/// <summary>
/// Gets the chatdata associated with the command
/// </summary>
public string[] Args { get; internal set; }
}
2021-08-14 21:49:23 +02:00
}