#undef DEBUG
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using RageCoop.Core;
namespace RageCoop.Client
{
///
/// ?
///
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;
public static void Connected()
{
OnConnection?.Invoke(true, GetPlayerID());
}
public static void Disconnected(string reason)
{
OnConnection?.Invoke(false, GetPlayerID(), reason);
}
public static void Connected(long netHandle)
{
OnConnection?.Invoke(true, netHandle);
}
public static void Disconnected(long netHandle)
{
OnConnection?.Invoke(false, netHandle);
}
public static void ModPacketReceived(long from, string mod, byte customID, byte[] bytes)
{
OnModPacketReceived?.Invoke(from, mod, customID, bytes);
}
public 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);
}
///
/// Connect to any server
///
/// The server address to connect. Example: 127.0.0.1:4499
public static void Connect(string serverAddress)
{
Networking.DisConnectFromServer(serverAddress);
}
///
/// ?
///
public static void Disconnect()
{
Networking.DisConnectFromServer(null);
}
///
/// Check if the player is already on a server
///
public static bool IsOnServer()
{
return Networking.IsOnServer;
}
///
/// Get the local player's ID
///
/// PlayerID
public static long GetPlayerID()
{
return Main.LocalPlayerID;
}
/*
///
/// Get a player using their Lidgren Network net handle
///
/// Lidgren-Network net handle
public static CharacterEntity GetPed(int ID)
{
lock (Main.Characters)
{
return Main.Characters.ContainsKey(ID) ? Main.Characters[ID] : null;
}
}
*/
///
/// Check if a RAGECOOP menu is visible
///
public static bool IsMenuVisible()
{
#if NON_INTERACTIVE
return false;
#else
return Main.MainMenu.MenuPool.AreAnyVisible;
#endif
}
///
/// Check if the RAGECOOP chat is visible
///
public static bool IsChatFocused()
{
return Main.MainChat.Focused;
}
///
/// Check if the RAGECOOP list of players is visible
///
public static bool IsPlayerListVisible()
{
return Util.GetTickCount64() - PlayerList.Pressed < 5000;
}
///
/// Get the version of RAGECOOP
///
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 modName, byte customID, byte[] bytes)
{
Networking.SendModData(-1, modName, 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 modName, byte customID, byte[] bytes)
{
Networking.SendModData(0, modName, 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 modName, byte customID, byte[] bytes)
{
Networking.SendModData(netHandle, modName, customID, bytes);
}
///
/// Get that player's local username
///
public static string GetUsername()
{
return Main.Settings.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.Settings.Username = username;
return true;
}
///
/// Enable or disable the local traffic for this player
///
/// true to disable traffic
public static void SetLocalTraffic(bool enable)
{
Main.Settings.DisableTraffic = !enable;
}
///
/// Sets the alignment for the player list, if set to true it will align left,
/// otherwise it will align right
///
/// true to move the player list to the left
public static void SetPlayerListLeftAlign(bool leftAlign)
{
PlayerList.LeftAlign = leftAlign;
}
#if DEBUG
///
/// ?
///
///
public static void SetDebug(bool value)
{
Main.UseDebug = value;
}
#endif
}
}