using System.Collections.Generic; using System.ComponentModel; using System.Linq; namespace CoopClient { /// /// ? /// public static class COOPAPI { #region DELEGATES /// /// ? /// /// /// The Lidgren-Network net handle /// public delegate void ConnectEvent(bool connected, long from, string reason = null); /// /// ? /// /// /// The Lidgren-Network net handle /// public delegate void ChatMessage(string from, string message, CancelEventArgs args); /// /// ? /// /// The Lidgren-Network net handle /// /// /// public delegate void ModEvent(long from, string mod, byte customID, byte[] bytes); #endregion #region EVENTS /// /// ? /// public static event ConnectEvent OnConnection; /// /// ? /// public static event ChatMessage OnChatMessage; /// /// ? /// public static event ModEvent OnModPacketReceived; internal static void Connected() { OnConnection?.Invoke(true, GetLocalNetHandle()); } internal static void Disconnected(string reason) { OnConnection?.Invoke(false, GetLocalNetHandle(), reason); } internal static void Connected(long netHandle) { OnConnection?.Invoke(true, netHandle); } internal static void Disconnected(long netHandle) { OnConnection?.Invoke(false, netHandle); } internal static void ModPacketReceived(long from, string mod, byte customID, byte[] bytes) { OnModPacketReceived?.Invoke(from, mod, customID, bytes); } internal static bool ChatMessageReceived(string from, string message) { CancelEventArgs args = new CancelEventArgs(false); OnChatMessage?.Invoke(from, message, args); return args.Cancel; } #endregion /// /// Send a local chat message to this player /// /// Username of the player who sent this message /// The player's message public static void LocalChatMessage(string from, string message) { Main.MainChat.AddMessage(from, message); } /// /// ? /// public static void Connect(string serverAddress) { Main.MainNetworking.DisConnectFromServer(serverAddress); } /// /// ? /// public static void Disconnect() { Main.MainNetworking.DisConnectFromServer(null); } /// /// Check if the player is already on a server /// public static bool IsOnServer() { return Main.MainNetworking.IsOnServer(); } /// /// Get the local net handle from this Lidgren-Network client when connected to a server /// /// long public static long GetLocalNetHandle() { return Main.LocalNetHandle; } /// /// Get all connected player's as a Dictionary. /// Key = Lidgren-Network net handle /// Value = Character handle or null /// public static Dictionary GetAllPlayers() { Dictionary result = new Dictionary(); lock (Main.Players) { foreach (KeyValuePair player in Main.Players.Where(x => x.Key != Main.LocalNetHandle)) { result.Add(player.Key, player.Value.Character?.Handle); } } return result; } /// /// Get a player using their Lidgren Network net handle /// /// Lidgren-Network net handle public static Entities.EntitiesPlayer GetPlayer(long handle) { lock (Main.Players) { return Main.Players.ContainsKey(handle) ? Main.Players[handle] : null; } } /// /// Check if a GTACOOP:R menu is visible /// public static bool IsMenuVisible() { #if NON_INTERACTIVE return false; #else return Main.MainMenu.MenuPool.AreAnyVisible; #endif } /// /// Check if the GTACOOP:R chat is visible /// public static bool IsChatFocused() { return Main.MainChat.Focused; } /// /// Check if the GTACOOP:R list of players is visible /// public static bool IsPlayerListVisible() { return Util.GetTickCount64() - PlayerList.Pressed < 5000; } /// /// Get the version of GTACOOP:R /// public static string GetCurrentVersion() { return Main.CurrentVersion; } /// /// Send any data (bytes) to the server /// /// The name of this modification (script) /// The ID to know what the data is /// Your class, structure or whatever in bytes public static void SendDataToServer(string mod, byte customID, byte[] bytes) { Main.MainNetworking.SendModData(-1, mod, customID, bytes); } /// /// Send any data (bytes) to the all player /// /// The name of this modification (script) /// The ID to know what the data is /// Your class, structure or whatever in bytes public static void SendDataToAll(string mod, byte customID, byte[] bytes) { Main.MainNetworking.SendModData(0, mod, customID, bytes); } /// /// Send any data (bytes) to a player /// /// The Lidgren Network net handle that receives the data /// The name of this modification (script) /// The ID to know what the data is /// Your class, structure or whatever in bytes public static void SendDataToPlayer(long netHandle, string mod, byte customID, byte[] bytes) { Main.MainNetworking.SendModData(netHandle, mod, customID, bytes); } /// /// Get that player's local username /// public static string GetUsername() { return Main.MainSettings.Username; } /// /// Set a new username for this player /// /// The new username /// false if the player already joined a server or the username is null or empty otherwise true public static bool SetUsername(string username) { if (IsOnServer() || string.IsNullOrEmpty(username)) { return false; } Main.MainSettings.Username = username; return true; } /// /// Enable or disable sharing of NPCs with other players /// /// public static void SetShareNPCs(bool share) { Main.ShareNPCsWithPlayers = share; } /// /// Enable or disable the local traffic for this player /// /// public static void SetLocalTraffic(bool enable) { Main.DisableTraffic = !enable; } #if DEBUG /// /// ? /// /// public static void SetDebug(bool value) { Main.UseDebug = value; } #endif } }