Gonna move to server authority
This commit is contained in:
33
Assets/FirstGearGames/Scripts/Utilities/Network/Lookups.cs
Normal file
33
Assets/FirstGearGames/Scripts/Utilities/Network/Lookups.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using Mirror;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
|
||||
namespace FirstGearGames.Utilities.Networks
|
||||
{
|
||||
|
||||
|
||||
public static class Lookups
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the NetworkBehaviour for the specified NetworkIdentity and component index.
|
||||
/// </summary>
|
||||
/// <param name="componentIndex"></param>
|
||||
/// <returns></returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static NetworkBehaviour ReturnNetworkBehaviour(NetworkIdentity netIdentity, byte componentIndex)
|
||||
{
|
||||
if (netIdentity == null)
|
||||
return null;
|
||||
/* Networkbehaviours within the collection are the same order as compenent indexes.
|
||||
* I can save several iterations by simply grabbing the index from the networkbehaviours collection rather than iterating
|
||||
* it. */
|
||||
//A network behaviour was removed or added at runtime, component counts don't match up.
|
||||
if (componentIndex >= netIdentity.NetworkBehaviours.Length)
|
||||
return null;
|
||||
|
||||
return netIdentity.NetworkBehaviours[componentIndex];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f1a6decfc27380c41a81236da459c9a1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,36 @@
|
||||
//#if MIRROR_53_0_OR_NEWER
|
||||
|
||||
using Mirror;
|
||||
|
||||
namespace FirstGearGames.Utilities.Networks
|
||||
{
|
||||
|
||||
public static class MirrorBreaksProjectsEveryRelease_Serializers
|
||||
{
|
||||
public static void WriteBoolean(this NetworkWriter writer, bool value) => writer.WriteBool(value);
|
||||
public static bool ReadBoolean(this NetworkReader reader) => reader.ReadBool();
|
||||
|
||||
public static void WriteInt64(this NetworkWriter writer, long value) => writer.WriteLong(value);
|
||||
public static long ReadInt64(this NetworkReader reader) => reader.ReadLong();
|
||||
public static void WriteUInt64(this NetworkWriter writer, ulong value) => writer.WriteULong(value);
|
||||
public static ulong ReadUInt64(this NetworkReader reader) => reader.ReadULong();
|
||||
|
||||
public static void WriteInt32(this NetworkWriter writer, int value) => writer.WriteInt(value);
|
||||
public static int ReadInt32(this NetworkReader reader) => reader.ReadInt();
|
||||
public static void WriteUInt32(this NetworkWriter writer, uint value) => writer.WriteUInt(value);
|
||||
public static uint ReadUInt32(this NetworkReader reader) => reader.ReadUInt();
|
||||
|
||||
public static void WriteInt16(this NetworkWriter writer, short value) => writer.WriteShort(value);
|
||||
public static short ReadInt16(this NetworkReader reader) => reader.ReadShort();
|
||||
public static void WriteUInt16(this NetworkWriter writer, ushort value) => writer.WriteUShort(value);
|
||||
public static ushort ReadUInt16(this NetworkReader reader) => reader.ReadUShort();
|
||||
|
||||
public static void WriteSingle(this NetworkWriter writer, float value) => writer.WriteFloat(value);
|
||||
public static float ReadSingle(this NetworkReader reader) => reader.ReadFloat();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: edbcfa33993d4e0479acaeaebbedf609
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
174
Assets/FirstGearGames/Scripts/Utilities/Network/Platforms.cs
Normal file
174
Assets/FirstGearGames/Scripts/Utilities/Network/Platforms.cs
Normal file
@@ -0,0 +1,174 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
|
||||
|
||||
namespace FirstGearGames.Utilities.Networks
|
||||
{
|
||||
|
||||
public static class Platforms
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the NetworkId for a NetworkIdentity.
|
||||
/// </summary>
|
||||
/// <param name="ni"></param>
|
||||
/// <returns></returns>
|
||||
public static uint ReturnNetworkId(this NetworkIdentity ni)
|
||||
{
|
||||
return ni.netId;
|
||||
}
|
||||
/// <summary>
|
||||
/// Sends a message to the server.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="nm"></param>
|
||||
/// <param name="msg"></param>
|
||||
/// <param name="channel"></param>
|
||||
public static void ClientSend<T>(NetworkManager nm, T msg, int channel) where T : struct, NetworkMessage
|
||||
{
|
||||
NetworkClient.Send(msg, channel);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a message to all clients.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="nm"></param>
|
||||
/// <param name="msg"></param>
|
||||
/// <param name="channel"></param>
|
||||
public static void ServerSendToAll<T>(NetworkManager nm, T msg, int channel) where T : struct, NetworkMessage
|
||||
{
|
||||
NetworkServer.SendToAll(msg, channel, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if object has an owner.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool ReturnHasOwner(this NetworkBehaviour nb)
|
||||
{
|
||||
return (nb.connectionToClient != null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the networkId for the networkIdentity.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint ReturnNetId(this NetworkBehaviour nb)
|
||||
{
|
||||
return nb.netIdentity.netId;
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns current owner of this object.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static NetworkConnection ReturnOwner(this NetworkBehaviour nb)
|
||||
{
|
||||
return nb.connectionToClient;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if current client has authority.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool ReturnHasAuthority(this NetworkBehaviour nb)
|
||||
{
|
||||
return nb.hasAuthority;
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns if is server.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool ReturnIsServer(this NetworkBehaviour nb)
|
||||
{
|
||||
return nb.isServer;
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns if is server only.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool ReturnIsServerOnly(this NetworkBehaviour nb)
|
||||
{
|
||||
return nb.isServerOnly;
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns if is client.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool ReturnIsClient(this NetworkBehaviour nb)
|
||||
{
|
||||
return nb.isClient;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if client is active.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool ReturnServerActive(NetworkManager nm)
|
||||
{
|
||||
return NetworkServer.active;
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns if client is active.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool ReturnClientActive(NetworkManager nm)
|
||||
{
|
||||
return NetworkClient.active;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if a connection is ready.
|
||||
/// </summary>
|
||||
/// <param name="nc"></param>
|
||||
/// <returns></returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool IsReady(this NetworkConnection nc)
|
||||
{
|
||||
return nc.isReady;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns currently spawned NetworkIdentities.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Dictionary<uint, NetworkIdentity> ReturnSpawned(NetworkManager nm)
|
||||
{
|
||||
return NetworkIdentity.spawned;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets MTU values.
|
||||
/// </summary>
|
||||
/// <param name="reliable"></param>
|
||||
/// <param name="unreliable"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2796339eb81e2bd479d19627da52ff03
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
176
Assets/FirstGearGames/Scripts/Utilities/Network/Quaternions.cs
Normal file
176
Assets/FirstGearGames/Scripts/Utilities/Network/Quaternions.cs
Normal file
@@ -0,0 +1,176 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FirstGearGames.Utilities.Networks
|
||||
{
|
||||
public static class Quaternions
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Credit to this man for converting gaffer games c code to c#
|
||||
/// https://gist.github.com/fversnel/0497ad7ab3b81e0dc1dd
|
||||
/// </summary>
|
||||
private enum ComponentType : uint
|
||||
{
|
||||
X = 0,
|
||||
Y = 1,
|
||||
Z = 2,
|
||||
W = 3
|
||||
}
|
||||
|
||||
// note: 1.0f / sqrt(2)
|
||||
private const float Maximum = +1.0f / 1.414214f;
|
||||
|
||||
private const int BitsPerAxis = 10;
|
||||
private const int LargestComponentShift = BitsPerAxis * 3;
|
||||
private const int AShift = BitsPerAxis * 2;
|
||||
private const int BShift = BitsPerAxis * 1;
|
||||
private const int IntScale = (1 << (BitsPerAxis - 1)) - 1;
|
||||
private const int IntMask = (1 << BitsPerAxis) - 1;
|
||||
|
||||
internal static uint Compress(Quaternion quaternion)
|
||||
{
|
||||
float absX = Mathf.Abs(quaternion.x);
|
||||
float absY = Mathf.Abs(quaternion.y);
|
||||
float absZ = Mathf.Abs(quaternion.z);
|
||||
float absW = Mathf.Abs(quaternion.w);
|
||||
|
||||
ComponentType largestComponent = ComponentType.X;
|
||||
float largestAbs = absX;
|
||||
float largest = quaternion.x;
|
||||
|
||||
if (absY > largestAbs)
|
||||
{
|
||||
largestAbs = absY;
|
||||
largestComponent = ComponentType.Y;
|
||||
largest = quaternion.y;
|
||||
}
|
||||
if (absZ > largestAbs)
|
||||
{
|
||||
largestAbs = absZ;
|
||||
largestComponent = ComponentType.Z;
|
||||
largest = quaternion.z;
|
||||
}
|
||||
if (absW > largestAbs)
|
||||
{
|
||||
largestComponent = ComponentType.W;
|
||||
largest = quaternion.w;
|
||||
}
|
||||
|
||||
float a = 0;
|
||||
float b = 0;
|
||||
float c = 0;
|
||||
switch (largestComponent)
|
||||
{
|
||||
case ComponentType.X:
|
||||
a = quaternion.y;
|
||||
b = quaternion.z;
|
||||
c = quaternion.w;
|
||||
break;
|
||||
case ComponentType.Y:
|
||||
a = quaternion.x;
|
||||
b = quaternion.z;
|
||||
c = quaternion.w;
|
||||
break;
|
||||
case ComponentType.Z:
|
||||
a = quaternion.x;
|
||||
b = quaternion.y;
|
||||
c = quaternion.w;
|
||||
break;
|
||||
case ComponentType.W:
|
||||
a = quaternion.x;
|
||||
b = quaternion.y;
|
||||
c = quaternion.z;
|
||||
break;
|
||||
}
|
||||
|
||||
if (largest < 0)
|
||||
{
|
||||
a = -a;
|
||||
b = -b;
|
||||
c = -c;
|
||||
}
|
||||
|
||||
uint integerA = ScaleToUint(a);
|
||||
uint integerB = ScaleToUint(b);
|
||||
uint integerC = ScaleToUint(c);
|
||||
|
||||
return (((uint)largestComponent) << LargestComponentShift) | (integerA << AShift) | (integerB << BShift) | integerC;
|
||||
}
|
||||
|
||||
private static uint ScaleToUint(float v)
|
||||
{
|
||||
float normalized = v / Maximum;
|
||||
return (uint)Mathf.RoundToInt(normalized * IntScale) & IntMask;
|
||||
}
|
||||
|
||||
private static float ScaleToFloat(uint v)
|
||||
{
|
||||
float unscaled = v * Maximum / IntScale;
|
||||
|
||||
if (unscaled > Maximum)
|
||||
unscaled -= Maximum * 2;
|
||||
return unscaled;
|
||||
}
|
||||
|
||||
internal static Quaternion Decompress(uint compressed)
|
||||
{
|
||||
var largestComponentType = (ComponentType)(compressed >> LargestComponentShift);
|
||||
uint integerA = (compressed >> AShift) & IntMask;
|
||||
uint integerB = (compressed >> BShift) & IntMask;
|
||||
uint integerC = compressed & IntMask;
|
||||
|
||||
float a = ScaleToFloat(integerA);
|
||||
float b = ScaleToFloat(integerB);
|
||||
float c = ScaleToFloat(integerC);
|
||||
|
||||
Quaternion rotation;
|
||||
switch (largestComponentType)
|
||||
{
|
||||
case ComponentType.X:
|
||||
// (?) y z w
|
||||
rotation.y = a;
|
||||
rotation.z = b;
|
||||
rotation.w = c;
|
||||
rotation.x = Mathf.Sqrt(1 - rotation.y * rotation.y
|
||||
- rotation.z * rotation.z
|
||||
- rotation.w * rotation.w);
|
||||
break;
|
||||
case ComponentType.Y:
|
||||
// x (?) z w
|
||||
rotation.x = a;
|
||||
rotation.z = b;
|
||||
rotation.w = c;
|
||||
rotation.y = Mathf.Sqrt(1 - rotation.x * rotation.x
|
||||
- rotation.z * rotation.z
|
||||
- rotation.w * rotation.w);
|
||||
break;
|
||||
case ComponentType.Z:
|
||||
// x y (?) w
|
||||
rotation.x = a;
|
||||
rotation.y = b;
|
||||
rotation.w = c;
|
||||
rotation.z = Mathf.Sqrt(1 - rotation.x * rotation.x
|
||||
- rotation.y * rotation.y
|
||||
- rotation.w * rotation.w);
|
||||
break;
|
||||
case ComponentType.W:
|
||||
// x y z (?)
|
||||
rotation.x = a;
|
||||
rotation.y = b;
|
||||
rotation.z = c;
|
||||
rotation.w = Mathf.Sqrt(1 - rotation.x * rotation.x
|
||||
- rotation.y * rotation.y
|
||||
- rotation.z * rotation.z);
|
||||
break;
|
||||
default:
|
||||
// Should never happen!
|
||||
throw new ArgumentOutOfRangeException("Unknown rotation component type: " +
|
||||
largestComponentType);
|
||||
}
|
||||
|
||||
return rotation;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 610d5b62062eff04dac0100a5acb6f00
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user