RAGECOOP-V/Client/COOPAPI.cs

193 lines
5.4 KiB
C#
Raw Normal View History

2022-05-22 15:55:26 +08:00
#undef DEBUG
using System.Collections.Generic;
2021-09-28 16:51:16 +02:00
using System.ComponentModel;
2021-09-30 23:35:42 +02:00
using System.Linq;
2022-05-22 15:55:26 +08:00
using RageCoop.Core;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Client
{
/// <summary>
/// ?
/// </summary>
public static class COOPAPI
{
2021-09-27 19:10:51 +02:00
#region DELEGATES
/// <summary>
/// ?
/// </summary>
2021-12-14 22:51:09 +01:00
/// <param name="connected"></param>
/// <param name="from">The player's id</param>
2021-12-14 22:51:09 +01:00
/// <param name="reason"></param>
public delegate void ConnectEvent(bool connected, int from, string reason = null);
/// <summary>
/// ?
/// </summary>
2021-12-14 22:51:09 +01:00
/// <param name="from"></param>
/// <param name="message">The Lidgren-Network net handle</param>
/// <param name="args"></param>
public delegate void ChatMessage(string from, string message, CancelEventArgs args);
/// <summary>
/// ?
/// </summary>
2021-12-14 22:51:09 +01:00
/// <param name="from">The Lidgren-Network net handle</param>
/// <param name="mod"></param>
/// <param name="customID"></param>
/// <param name="bytes"></param>
2021-09-29 14:34:22 +02:00
public delegate void ModEvent(long from, string mod, byte customID, byte[] bytes);
2021-09-27 19:10:51 +02:00
#endregion
#region EVENTS
/// <summary>
/// ?
/// </summary>
public static event ConnectEvent OnConnection;
/// <summary>
/// ?
/// </summary>
public static event ChatMessage OnChatMessage;
2022-05-22 15:55:26 +08:00
public static void Connected()
{
2022-05-22 15:55:26 +08:00
OnConnection?.Invoke(true, GetPlayerID());
}
2022-05-22 15:55:26 +08:00
public static void Disconnected(string reason)
{
2022-05-22 15:55:26 +08:00
OnConnection?.Invoke(false, GetPlayerID(), reason);
}
public static void Connected(int playerID)
{
OnConnection?.Invoke(true, playerID);
}
public static void Disconnected(int playerID)
{
OnConnection?.Invoke(false, playerID);
}
2022-05-22 15:55:26 +08:00
public static bool ChatMessageReceived(string from, string message)
{
CancelEventArgs args = new CancelEventArgs(false);
OnChatMessage?.Invoke(from, message, args);
return args.Cancel;
}
2021-09-27 19:10:51 +02:00
#endregion
/// <summary>
/// Send a local chat message to this player
/// </summary>
/// <param name="from">Username of the player who sent this message</param>
/// <param name="message">The player's message</param>
public static void LocalChatMessage(string from, string message)
{
Main.MainChat.AddMessage(from, message);
}
/// <summary>
/// Connect to any server
/// </summary>
/// <param name="serverAddress">The server address to connect. Example: 127.0.0.1:4499</param>
2021-09-27 19:10:51 +02:00
public static void Connect(string serverAddress)
{
2022-05-31 23:12:32 -08:00
Networking.ToggleConnection(serverAddress);
}
/// <summary>
/// ?
/// </summary>
2021-09-29 14:34:22 +02:00
public static void Disconnect()
{
2022-05-31 23:12:32 -08:00
Networking.ToggleConnection(null);
2021-09-29 14:34:22 +02:00
}
/// <summary>
2021-12-16 14:52:48 +01:00
/// Check if the player is already on a server
/// </summary>
public static bool IsOnServer()
{
return Networking.IsOnServer;
}
/// <summary>
2022-05-22 15:55:26 +08:00
/// Get the local player's ID
/// </summary>
2022-05-22 15:55:26 +08:00
/// <returns>PlayerID</returns>
public static int GetPlayerID()
2021-09-29 14:34:22 +02:00
{
return Main.LocalPlayerID;
2021-09-29 14:34:22 +02:00
}
/// <summary>
2022-04-06 02:18:24 +02:00
/// Check if a RAGECOOP menu is visible
/// </summary>
2021-09-28 16:51:16 +02:00
public static bool IsMenuVisible()
{
2021-09-29 14:34:22 +02:00
#if NON_INTERACTIVE
return false;
#else
return Menus.CoopMenu.MenuPool.AreAnyVisible;
2021-09-29 14:34:22 +02:00
#endif
2021-09-28 16:51:16 +02:00
}
/// <summary>
2022-04-06 02:18:24 +02:00
/// Check if the RAGECOOP chat is visible
/// </summary>
2021-09-28 16:51:16 +02:00
public static bool IsChatFocused()
{
return Main.MainChat.Focused;
}
/// <summary>
2022-04-06 02:18:24 +02:00
/// Check if the RAGECOOP list of players is visible
/// </summary>
2021-09-28 16:51:16 +02:00
public static bool IsPlayerListVisible()
{
return Util.GetTickCount64() - PlayerList.Pressed < 5000;
2021-09-28 16:51:16 +02:00
}
/// <summary>
2022-04-06 02:18:24 +02:00
/// Get the version of RAGECOOP
/// </summary>
2021-09-28 16:51:16 +02:00
public static string GetCurrentVersion()
{
return Main.CurrentVersion;
}
/// <summary>
/// Get that player's local username
/// </summary>
public static string GetUsername()
{
2022-05-22 15:55:26 +08:00
return Main.Settings.Username;
}
/// <summary>
/// Set a new username for this player
/// </summary>
/// <param name="username">The new username</param>
/// <returns>false if the player already joined a server or the username is null or empty otherwise true</returns>
public static bool SetUsername(string username)
{
if (IsOnServer() || string.IsNullOrEmpty(username))
{
return false;
}
2022-05-22 15:55:26 +08:00
Main.Settings.Username = username;
return true;
}
/// <summary>
/// Get or set the client's settings.
/// </summary>
/// <returns>The client's settings, you should NEVER change settings without notifying the player.</returns>
public static Settings Settings()
{
return Main.Settings;
}
}
}