using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Lidgren.Network; using RageCoop.Core; using RageCoop.Core.Scripting; using System.Reflection; using System.Net; namespace RageCoop.Server.Scripting { public class APIEvents { #region INTERNAL internal Dictionary>> CustomEventHandlers = new(); #endregion public event EventHandler OnChatMessage; public event EventHandler OnPlayerHandshake; /// /// Will be invoked when a player is connected, but this player might not be ready yet(client resources not loaded), using is recommended. /// public event EventHandler OnPlayerConnected; /// /// Will be invoked after the client connected and all resources(if any) have been loaded. /// public event EventHandler OnPlayerReady; public event EventHandler OnPlayerDisconnected; /// /// Will be invoked before registered handlers /// public event EventHandler OnCommandReceived; /// /// Invoked everytime a player's main ped has been updated /// public event EventHandler OnPlayerUpdate; internal void ClearHandlers() { OnChatMessage=null; OnPlayerHandshake=null; OnPlayerConnected=null; OnPlayerReady=null; OnPlayerDisconnected=null; // OnCustomEventReceived=null; OnCommandReceived=null; OnPlayerUpdate=null; } #region INVOKE internal void InvokeOnChatMessage(Packets.ChatMessage p, Client sender) { OnChatMessage?.Invoke(this, new ChatEventArgs() { Sender=sender, Message=p.Message }); } internal void InvokePlayerConnected(Client client) { OnPlayerConnected?.Invoke(this,client); } internal void InvokePlayerReady(Client client) { OnPlayerReady?.Invoke(this, client); } internal void InvokePlayerDisconnected(Client client) { OnPlayerDisconnected?.Invoke(this,client); } internal void InvokePlayerHandshake(HandshakeEventArgs args) { OnPlayerHandshake?.Invoke(this, args); } internal void InvokeCustomEventReceived(Packets.CustomEvent p, Client sender) { var args = new CustomEventReceivedArgs() { Hash=p.Hash, Args=p.Args, Sender=sender }; List> handlers; if (CustomEventHandlers.TryGetValue(p.Hash, out handlers)) { handlers.ForEach((x) => { x.Invoke(args); }); } } internal bool InvokeOnCommandReceived(string cname, string[] cargs, Client sender) { var args = new OnCommandEventArgs() { Name=cname, Args=cargs, Sender=sender }; OnCommandReceived?.Invoke(this, args); return args.Cancel; } internal void InvokePlayerUpdate(Client client) { OnPlayerUpdate?.Invoke(this, client); } #endregion } public class API { private readonly Server Server; internal API(Server server) { Server=server; } public APIEvents Events { get; set; }=new APIEvents(); #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 void SendNativeCallToAll(GTA.Native.Hash hash, params object[] args) { try { if (Server.MainNetServer.ConnectionsCount == 0) { return; } if (args != null && args.Length == 0) { Server.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) { Server.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 Dictionary GetAllClients() { return new(Server.Clients); } /// /// Get the client by its username /// /// The username to search for (non case-sensitive) /// The Client from this user or null public Client GetClientByUsername(string username) { return Server.Clients.Values.FirstOrDefault(x => x.Username.ToLower() == username.ToLower()); } /// /// Send a chat message to all players /// /// The chat message /// The username which send this message (default = "Server") public void SendChatMessage(string message, List targets = null, string username = "Server") { try { if (Server.MainNetServer.ConnectionsCount == 0) { return; } targets ??= new(Server.Clients.Values); foreach(Client client in targets) { Server.SendChatMessage(username, message, client.Connection); } } catch (Exception e) { Server.Logger?.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<"); } } public void SendChatMessage(string message, Client target, string username = "Server") { try { Server.SendChatMessage(username, message, target.Connection); } catch (Exception e) { Server.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 void SendCleanUpWorldToAll(List clients = null) { SendCustomEvent(CustomEvents.CleanUpWorld,null,clients); } /// /// 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 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 void RegisterCommand(string name, Action callback) { Server.RegisterCommand(name, callback); } /// /// Register all commands in a static class /// /// Your static class with commands public void RegisterCommands() { Server.RegisterCommands(); } /// /// Register all commands inside an class instance /// /// The instance of type containing the commands public void RegisterCommands(object obj) { IEnumerable commands = obj.GetType().GetMethods().Where(method => method.GetCustomAttributes(typeof(Command), false).Any()); foreach (MethodInfo method in commands) { Command attribute = method.GetCustomAttribute(true); RegisterCommand(attribute.Name, attribute.Usage, attribute.ArgsLength, (ctx) => { method.Invoke(obj, new object[] { ctx }); }); } } /// /// Send an event and data to the specified clients. Use if you want to send event to individual client. /// /// An unique identifier of the event /// The objects conataing your data, supported types: byte, short, ushort, int, uint, long, ulong, float, bool, string. /// The target clients to send. Leave it null to send to all clients public void SendCustomEvent(int eventHash,List args=null,List targets=null) { targets ??= new(Server.Clients.Values); var p = new Packets.CustomEvent() { Args=args, Hash=eventHash }; foreach(var c in targets) { Server.Send(p,c,ConnectionChannel.Event,NetDeliveryMethod.ReliableOrdered); } } /// /// Register an handler to the specifed event hash, one event can have multiple handlers. /// /// An unique identifier of the event, you can hash your event name with /// An handler to be invoked when the event is received from the server. This will be invoked from main thread. public void RegisterCustomEventHandler(int hash,Action handler) { List> handlers; lock (Events.CustomEventHandlers) { if (!Events.CustomEventHandlers.TryGetValue(hash,out handlers)) { Events.CustomEventHandlers.Add(hash, handlers = new List>()); } handlers.Add(handler); } } public void RegisterCustomEventHandler(string name, Action handler) { RegisterCustomEventHandler(CustomEvents.Hash(name), handler); } public Logger GetLogger() { return Server.Logger; } #endregion } }