#undef DEBUG
using System.Collections.Generic;
using System;
using System.Linq;
using RageCoop.Core;
using GTA;
namespace RageCoop.Client.Scripting
{
///
///
///
public class CustomEventReceivedArgs : EventArgs
{
///
/// The event hash
///
public int Hash { get; set; }
///
/// Supported types: byte, short, ushort, int, uint, long, ulong, float, bool, string, Vector3, Quaternion
///
public List Args { get; set; }
}
///
/// Provides vital functionality to interact with RAGECOOP
///
public static class API
{
#region INTERNAL
internal static Dictionary>> CustomEventHandlers = new Dictionary>>();
#endregion
///
/// Client configuration, this will conflict with server-side config.
///
public static class Config
{
///
/// Get or set local player's username, set won't be effective if already connected to a server.
///
public static string Username
{
get { return Main.Settings.Username; }
set
{
if (IsOnServer || string.IsNullOrEmpty(value))
{
return;
}
Main.Settings.Username = value;
}
}
///
/// Enable automatic respawn for this player.
///
public static bool EnableAutoRespawn { get; set; } = true;
///
/// Get or set player's blip color
///
public static BlipColor BlipColor { get; set; } = BlipColor.White;
///
/// Get or set player's blip sprite
///
public static BlipSprite BlipSprite { get; set; } = BlipSprite.Standard;
///
/// Get or set scale of player's blip
///
public static float BlipScale { get; set; } = 1;
}
///
/// Base events for RageCoop
///
public static class Events
{
#region DELEGATES
///
///
///
public delegate void EmptyEvent();
///
///
///
///
///
public delegate void CustomEvent(int hash, List args);
#endregion
///
/// The local player is dead
///
public static event EmptyEvent OnPlayerDied;
///
/// A local vehicle is spawned
///
public static event EventHandler OnVehicleSpawned;
///
/// A local vehicle is deleted
///
public static event EventHandler OnVehicleDeleted;
///
/// A local ped is spawned
///
public static event EventHandler OnPedSpawned;
///
/// A local ped is deleted
///
public static event EventHandler OnPedDeleted;
///
/// This is equivalent of .
///
public static event EmptyEvent OnTick;
#region INVOKE
internal static void InvokeVehicleSpawned(SyncedVehicle v) { OnVehicleSpawned?.Invoke(null, v); }
internal static void InvokeVehicleDeleted(SyncedVehicle v) { OnVehicleDeleted?.Invoke(null, v); }
internal static void InvokePedSpawned(SyncedPed p) { OnPedSpawned?.Invoke(null, p); }
internal static void InvokePedDeleted(SyncedPed p) { OnPedDeleted?.Invoke(null, p); }
internal static void InvokePlayerDied() { OnPlayerDied?.Invoke(); }
internal static void InvokeTick() { OnTick?.Invoke(); }
internal static void InvokeCustomEventReceived(Packets.CustomEvent p)
{
var args = new CustomEventReceivedArgs() { Hash=p.Hash, Args=p.Args};
// Main.Logger.Debug($"CustomEvent:\n"+args.Args.DumpWithType());
List> handlers;
if (CustomEventHandlers.TryGetValue(p.Hash, out handlers))
{
handlers.ForEach((x) => { x.Invoke(args); });
}
}
#endregion
internal static void ClearHandlers()
{
OnPlayerDied=null;
OnTick=null;
OnPedDeleted=null;
OnPedSpawned=null;
OnVehicleDeleted=null;
OnVehicleSpawned=null;
}
}
#region FUNCTIONS
///
/// Send a local chat message to this player
///
/// Name of the sender
/// The player's message
public static void LocalChatMessage(string from, string message)
{
Main.MainChat.AddMessage(from, message);
}
///
/// Get a that RAGECOOP is currently using.
///
///
public static Logger Logger
{
get
{
return Main.Logger;
}
}
///
/// Queue an action to be executed on next tick.
///
///
public static void QueueAction(Action a)
{
Main.QueueAction(a);
}
///
/// Disconnect from the server
///
public static void Disconnect()
{
Networking.ToggleConnection(null);
}
///
/// Check if the player is already on a server
///
public static bool IsOnServer
{
get { return Networking.IsOnServer; }
}
///
/// Get the local player's ID
///
/// PlayerID
public static int LocalPlayerID
{
get { return Main.LocalPlayerID; }
}
///
/// Check if a RAGECOOP menu is visible
///
public static bool IsMenuVisible
{
get { return Menus.CoopMenu.MenuPool.AreAnyVisible; }
}
///
/// Check if the RAGECOOP chat is visible
///
public static bool IsChatFocused
{
get { return Main.MainChat.Focused; }
}
///
/// Check if the RAGECOOP list of players is visible
///
public static bool IsPlayerListVisible
{
get { return Util.GetTickCount64() - PlayerList.Pressed < 5000; }
}
///
/// Get the version of RAGECOOP
///
public static string CurrentVersion
{
get { return Main.CurrentVersion; }
}
///
/// Send an event and data to the server.
///
/// An unique identifier of the event
/// The objects conataing your data, see for a list of supported types
public static void SendCustomEvent(int eventHash, List args)
{
var p = new Packets.CustomEvent()
{
Args=args,
Hash=eventHash
};
Networking.Send(p, ConnectionChannel.Event, Lidgren.Network.NetDeliveryMethod.ReliableOrdered);
}
///
/// Send an event and data to the server.
///
///
///
public static void SendCustomEvent(int eventHash,params object[] args)
{
var p = new Packets.CustomEvent()
{
Args=new List(args),
Hash=eventHash
};
Networking.Send(p, ConnectionChannel.Event, Lidgren.Network.NetDeliveryMethod.ReliableOrdered);
}
///
/// Register an handler to the specifed event hash, one event can have multiple handlers. This will be invoked from backgound thread, use in the handler to dispatch code to script thread.
///
/// 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.
public static void RegisterCustomEventHandler(int hash, Action handler)
{
List> handlers;
lock (CustomEventHandlers)
{
if (!CustomEventHandlers.TryGetValue(hash, out handlers))
{
CustomEventHandlers.Add(hash, handlers = new List>());
}
handlers.Add(handler);
}
}
#endregion
}
}