using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using Mirror;
namespace FirstGearGames.Utilities.Networks
{
public static class Platforms
{
///
/// Returns the NetworkId for a NetworkIdentity.
///
///
///
public static uint ReturnNetworkId(this NetworkIdentity ni)
{
return ni.netId;
}
///
/// Sends a message to the server.
///
///
///
///
///
public static void ClientSend(NetworkManager nm, T msg, int channel) where T : struct, NetworkMessage
{
NetworkClient.Send(msg, channel);
}
///
/// Sends a message to all clients.
///
///
///
///
///
public static void ServerSendToAll(NetworkManager nm, T msg, int channel) where T : struct, NetworkMessage
{
NetworkServer.SendToAll(msg, channel, true);
}
///
/// Returns true if object has an owner.
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ReturnHasOwner(this NetworkBehaviour nb)
{
return (nb.connectionToClient != null);
}
///
/// Returns the networkId for the networkIdentity.
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ReturnNetId(this NetworkBehaviour nb)
{
return nb.netIdentity.netId;
}
///
/// Returns current owner of this object.
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NetworkConnection ReturnOwner(this NetworkBehaviour nb)
{
return nb.connectionToClient;
}
///
/// Returns if current client has authority.
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ReturnHasAuthority(this NetworkBehaviour nb)
{
return nb.hasAuthority;
}
///
/// Returns if is server.
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ReturnIsServer(this NetworkBehaviour nb)
{
return nb.isServer;
}
///
/// Returns if is server only.
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ReturnIsServerOnly(this NetworkBehaviour nb)
{
return nb.isServerOnly;
}
///
/// Returns if is client.
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ReturnIsClient(this NetworkBehaviour nb)
{
return nb.isClient;
}
///
/// Returns if client is active.
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ReturnServerActive(NetworkManager nm)
{
return NetworkServer.active;
}
///
/// Returns if client is active.
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ReturnClientActive(NetworkManager nm)
{
return NetworkClient.active;
}
///
/// Returns if a connection is ready.
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsReady(this NetworkConnection nc)
{
return nc.isReady;
}
///
/// Returns currently spawned NetworkIdentities.
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Dictionary ReturnSpawned(NetworkManager nm)
{
return NetworkIdentity.spawned;
}
///
/// Sets MTU values.
///
///
///
///
public static void SetMTU(ref int reliable, ref int unreliable, int maxPacketSize)
{
if (Transport.activeTransport != null)
{
reliable = Mathf.Min(maxPacketSize, Transport.activeTransport.GetMaxPacketSize(0));
unreliable = Mathf.Min(maxPacketSize, Transport.activeTransport.GetMaxPacketSize(1));
}
//If packet sizes are not calculated then use max.
if (reliable == -1)
reliable = maxPacketSize;
if (unreliable == -1)
unreliable = maxPacketSize;
}
}
}