using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Lidgren.Network; using RageCoop.Core; using System.Net; namespace RageCoop.Server.Scripting { public static class API { public static class Events { #region DELEGATES public delegate void EmptyEvent(); public delegate void PlayerConnect(int id,string name); public delegate void PlayerDisconnect(int id, string name); #endregion public static event EmptyEvent OnStop; public static event EventHandler OnChatMessage; public static event EventHandler OnPlayerHandshake; public static event PlayerConnect OnPlayerConnected; public static event PlayerDisconnect OnPlayerDisconnected; #region INVOKE internal static void InvokeOnStop() { OnStop?.Invoke(); } internal static void InvokeOnChatMessage(Packets.ChatMessage p,NetConnection con) { OnChatMessage?.Invoke(null,new ChatEventArgs() { Sender=Util.GetClientByNetID(con.RemoteUniqueIdentifier), Message=p.Message }); } internal static void InvokePlayerConnected(Client client) { OnPlayerConnected?.Invoke(client.Player.PedID,client.Player.Username); } internal static void InvokePlayerDisconnected(Client client) { OnPlayerDisconnected?.Invoke(client.Player.PedID, client.Player.Username); } internal static void InvokePlayerHandshake(HandshakeEventArgs args) { OnPlayerHandshake?.Invoke(null, args); } #endregion } #region FUNCTIONS /// /// Send a native call (Function.Call) to all players. /// Keys = int, float, bool, string and lvector3 /// /// The hash (Example: 0x25223CA6B4D20B7F = GET_CLOCK_HOURS) /// The arguments (Example: string = int, object = 5) public static void SendNativeCallToAll(GTA.Native.Hash hash, params object[] args) { try { if (Server.MainNetServer.ConnectionsCount == 0) { return; } if (args != null && args.Length == 0) { Program.Logger.Error($"[ServerScript->SendNativeCallToAll(ulong hash, params object[] args)]: args is not null!"); return; } Packets.NativeCall packet = new() { Hash = (ulong)hash, Args = new List(args) ?? new List() }; NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage(); packet.Pack(outgoingMessage); Server.MainNetServer.SendMessage(outgoingMessage, Server.MainNetServer.Connections, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Native); } catch (Exception e) { Program.Logger.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<"); } } /// /// Get a list of all Clients /// /// All clients as a dictionary indexed by NetID public static Dictionary GetAllClients() { return Server.Clients; } /// /// Get the client by its username /// /// The username to search for (non case-sensitive) /// The Client from this user or null public static Client GetClientByUsername(string username) { return Server.Clients.Values.FirstOrDefault(x => x.Player.Username.ToLower() == username.ToLower()); } /// /// Send a chat message to all players /// /// The chat message /// The username which send this message (default = "Server") /// The list of connections (players) who received this chat message public static void SendChatMessageToAll(string message, string username = "Server", List netHandleList = null) { try { if (Server.MainNetServer.ConnectionsCount == 0) { return; } List connections = netHandleList == null ? Server.MainNetServer.Connections : Server.MainNetServer.Connections.FindAll(c => netHandleList.Contains(c.RemoteUniqueIdentifier)); Server.SendChatMessage(username, message, connections); } catch (Exception e) { Program.Logger.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<"); } } /// /// Send CleanUpWorld to all players to delete all objects created by the server /// public static void SendCleanUpWorldToAll(List netHandleList = null) { if (Server.MainNetServer.ConnectionsCount == 0) { return; } List connections = netHandleList == null ? Server.MainNetServer.Connections : Server.MainNetServer.Connections.FindAll(c => netHandleList.Contains(c.RemoteUniqueIdentifier)); NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage(); outgoingMessage.Write((byte)PacketTypes.CleanUpWorld); Server.MainNetServer.SendMessage(outgoingMessage, connections, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Default); } /// /// Register a new command chat command (Example: "/test") /// /// The name of the command (Example: "test" for "/test") /// How to use this message (argsLength required!) /// The length of args (Example: "/message USERNAME MESSAGE" = 2) (usage required!) /// Create a new function! public static void RegisterCommand(string name, string usage, short argsLength, Action callback) { Server.RegisterCommand(name, usage, argsLength, callback); } /// /// Register a new command chat command (Example: "/test") /// /// The name of the command (Example: "test" for "/test") /// Create a new function! public static void RegisterCommand(string name, Action callback) { Server.RegisterCommand(name, callback); } /// /// Register a class of commands /// /// The name of your class with functions public static void RegisterCommands() { Server.RegisterCommands(); } /// /// Register a class of events /// /// The name of your class with functions public static void RegisterEvents() { Server.RegisterEvents(); } #endregion } }