fishnet installed
This commit is contained in:
53
Assets/FishNet/Runtime/Utility/Performance/ByteArrayPool.cs
Normal file
53
Assets/FishNet/Runtime/Utility/Performance/ByteArrayPool.cs
Normal file
@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace FishNet.Utility.Performance
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves and stores byte arrays using a pooling system.
|
||||
/// </summary>
|
||||
public static class ByteArrayPool
|
||||
{
|
||||
/// <summary>
|
||||
/// Stored byte arrays.
|
||||
/// </summary>
|
||||
private static Queue<byte[]> _byteArrays = new Queue<byte[]>();
|
||||
|
||||
/// <summary>
|
||||
/// Returns a byte array which will be of at lesat minimum length. The returned array must manually be stored.
|
||||
/// </summary>
|
||||
public static byte[] Retrieve(int minimumLength)
|
||||
{
|
||||
byte[] result = null;
|
||||
|
||||
if (_byteArrays.Count > 0)
|
||||
result = _byteArrays.Dequeue();
|
||||
|
||||
int doubleMinimumLength = (minimumLength * 2);
|
||||
if (result == null)
|
||||
result = new byte[doubleMinimumLength];
|
||||
else if (result.Length < minimumLength)
|
||||
Array.Resize(ref result, doubleMinimumLength);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores a byte array for re-use.
|
||||
/// </summary>
|
||||
public static void Store(byte[] buffer)
|
||||
{
|
||||
/* Holy cow that's a lot of buffered
|
||||
* buffers. This wouldn't happen under normal
|
||||
* circumstances but if the user is stress
|
||||
* testing connections in one executable perhaps. */
|
||||
if (_byteArrays.Count > 300)
|
||||
return;
|
||||
_byteArrays.Enqueue(buffer);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c7620a5e6fedc18408f8f04821b35bbd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
203
Assets/FishNet/Runtime/Utility/Performance/DefaultObjectPool.cs
Normal file
203
Assets/FishNet/Runtime/Utility/Performance/DefaultObjectPool.cs
Normal file
@ -0,0 +1,203 @@
|
||||
using FishNet.Managing.Object;
|
||||
using FishNet.Object;
|
||||
using FishNet.Utility.Extension;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FishNet.Utility.Performance
|
||||
{
|
||||
|
||||
|
||||
public class DefaultObjectPool : ObjectPool
|
||||
{
|
||||
#region Public.
|
||||
/// <summary>
|
||||
/// Cache for pooled NetworkObjects.
|
||||
/// </summary>
|
||||
public IReadOnlyCollection<Dictionary<int, Stack<NetworkObject>>> Cache => _cache;
|
||||
private List<Dictionary<int, Stack<NetworkObject>>> _cache = new List<Dictionary<int, Stack<NetworkObject>>>();
|
||||
#endregion
|
||||
|
||||
#region Serialized.
|
||||
/// <summary>
|
||||
/// True if to use object pooling.
|
||||
/// </summary>
|
||||
[Tooltip("True if to use object pooling.")]
|
||||
[SerializeField]
|
||||
private bool _enabled = true;
|
||||
#endregion
|
||||
|
||||
#region Private.
|
||||
/// <summary>
|
||||
/// Current count of the cache collection.
|
||||
/// </summary>
|
||||
private int _cacheCount = 0;
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Returns an object that has been stored with a collectionId of 0. A new object will be created if no stored objects are available.
|
||||
/// </summary>
|
||||
/// <param name="prefabId">PrefabId of the object to return.</param>
|
||||
/// <param name="asServer">True if being called on the server side.</param>
|
||||
/// <returns></returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override NetworkObject RetrieveObject(int prefabId, bool asServer)
|
||||
{
|
||||
return RetrieveObject(prefabId, 0, asServer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an object that has been stored. A new object will be created if no stored objects are available.
|
||||
/// </summary>
|
||||
/// <param name="prefabId">PrefabId of the object to return.</param>
|
||||
/// <param name="collectionId">CollectionId of the prefab.</param>
|
||||
/// <param name="asServer">True if being called on the server side.</param>
|
||||
/// <returns></returns>
|
||||
public override NetworkObject RetrieveObject(int prefabId, ushort collectionId, bool asServer)
|
||||
{
|
||||
PrefabObjects po = base.NetworkManager.GetPrefabObjects<PrefabObjects>(collectionId, false);
|
||||
//Quick exit/normal retrieval when not using pooling.
|
||||
if (!_enabled)
|
||||
{
|
||||
NetworkObject prefab = po.GetObject(asServer, prefabId);
|
||||
return Instantiate(prefab);
|
||||
}
|
||||
|
||||
Stack<NetworkObject> cache = GetOrCreateCache(collectionId, prefabId);
|
||||
NetworkObject nob;
|
||||
//Iterate until nob is populated just in case cache entries have been destroyed.
|
||||
do
|
||||
{
|
||||
if (cache.Count == 0)
|
||||
{
|
||||
NetworkObject prefab = po.GetObject(asServer, prefabId);
|
||||
/* A null nob should never be returned from spawnables. This means something
|
||||
* else broke, likely unrelated to the object pool. */
|
||||
nob = Instantiate(prefab);
|
||||
//Can break instantly since we know nob is not null.
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
nob = cache.Pop();
|
||||
}
|
||||
|
||||
} while (nob == null);
|
||||
|
||||
nob.gameObject.SetActive(true);
|
||||
return nob;
|
||||
}
|
||||
/// <summary>
|
||||
/// Stores an object into the pool.
|
||||
/// </summary>
|
||||
/// <param name="instantiated">Object to store.</param>
|
||||
/// <param name="asServer">True if being called on the server side.</param>
|
||||
/// <returns></returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void StoreObject(NetworkObject instantiated, bool asServer)
|
||||
{
|
||||
//Pooling is not enabled.
|
||||
if (!_enabled)
|
||||
{
|
||||
Destroy(instantiated.gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
instantiated.gameObject.SetActive(false);
|
||||
instantiated.ResetForObjectPool();
|
||||
Stack<NetworkObject> cache = GetOrCreateCache(instantiated.SpawnableCollectionId, instantiated.PrefabId);
|
||||
cache.Push(instantiated);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Instantiates a number of objects and adds them to the pool.
|
||||
/// </summary>
|
||||
/// <param name="prefab">Prefab to cache.</param>
|
||||
/// <param name="count">Quantity to spawn.</param>
|
||||
/// <param name="asServer">True if storing prefabs for the server collection. This is only applicable when using DualPrefabObjects.</param>
|
||||
public void CacheObjects(NetworkObject prefab, int count, bool asServer)
|
||||
{
|
||||
if (!_enabled)
|
||||
return;
|
||||
if (count <= 0)
|
||||
return;
|
||||
if (prefab == null)
|
||||
return;
|
||||
if (prefab.PrefabId == NetworkObject.UNSET_PREFABID_VALUE)
|
||||
{
|
||||
InstanceFinder.NetworkManager.LogError($"Pefab {prefab.name} has an invalid prefabId and cannot be cached.");
|
||||
return;
|
||||
}
|
||||
|
||||
Stack<NetworkObject> cache = GetOrCreateCache(prefab.SpawnableCollectionId, prefab.PrefabId);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
NetworkObject nob = Instantiate(prefab);
|
||||
nob.gameObject.SetActive(false);
|
||||
cache.Push(nob);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears pools for all collectionIds
|
||||
/// </summary>
|
||||
public void ClearPool()
|
||||
{
|
||||
int count = _cache.Count;
|
||||
for (int i = 0; i < count; i++)
|
||||
ClearPool(i);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears a pool for collectionId.
|
||||
/// </summary>
|
||||
/// <param name="collectionId">CollectionId to clear for.</param>
|
||||
public void ClearPool(int collectionId)
|
||||
{
|
||||
if (collectionId >= _cacheCount)
|
||||
return;
|
||||
|
||||
Dictionary<int, Stack<NetworkObject>> dict = _cache[collectionId];
|
||||
//Convert to a list from the stack so we do not modify the stack directly.
|
||||
ListCache<NetworkObject> nobCache = ListCaches.GetNetworkObjectCache();
|
||||
foreach (Stack<NetworkObject> item in dict.Values)
|
||||
{
|
||||
while (item.Count > 0)
|
||||
nobCache.AddValue(item.Pop());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets a cache for an id or creates one if does not exist.
|
||||
/// </summary>
|
||||
/// <param name="prefabId"></param>
|
||||
/// <returns></returns>
|
||||
private Stack<NetworkObject> GetOrCreateCache(int collectionId, int prefabId)
|
||||
{
|
||||
if (collectionId >= _cacheCount)
|
||||
{
|
||||
//Add more to the cache.
|
||||
while (_cache.Count <= collectionId)
|
||||
{
|
||||
Dictionary<int, Stack<NetworkObject>> dict = new Dictionary<int, Stack<NetworkObject>>();
|
||||
_cache.Add(dict);
|
||||
}
|
||||
_cacheCount = collectionId;
|
||||
}
|
||||
|
||||
Dictionary<int, Stack<NetworkObject>> dictionary = _cache[collectionId];
|
||||
Stack<NetworkObject> cache;
|
||||
//No cache for prefabId yet, make one.
|
||||
if (!dictionary.TryGetValueIL2CPP(prefabId, out cache))
|
||||
{
|
||||
cache = new Stack<NetworkObject>();
|
||||
dictionary[prefabId] = cache;
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cb13d174096685549b1d6a94d726ff7d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
341
Assets/FishNet/Runtime/Utility/Performance/ListCache.cs
Normal file
341
Assets/FishNet/Runtime/Utility/Performance/ListCache.cs
Normal file
@ -0,0 +1,341 @@
|
||||
using FishNet.Connection;
|
||||
using FishNet.Managing;
|
||||
using FishNet.Object;
|
||||
using FishNet.Serializing.Helping;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FishNet.Utility.Performance
|
||||
{
|
||||
/// <summary>
|
||||
/// Various ListCache instances that may be used on the Unity thread.
|
||||
/// </summary>
|
||||
public static class ListCaches
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Cache collection for NetworkObjects.
|
||||
/// </summary>
|
||||
private static Stack<ListCache<NetworkObject>> _networkObjectCaches = new Stack<ListCache<NetworkObject>>();
|
||||
/// <summary>
|
||||
/// Cache collection for NetworkObjects.
|
||||
/// </summary>
|
||||
private static Stack<ListCache<NetworkBehaviour>> _networkBehaviourCaches = new Stack<ListCache<NetworkBehaviour>>();
|
||||
/// <summary>
|
||||
/// Cache collection for NetworkObjects.
|
||||
/// </summary>
|
||||
private static Stack<ListCache<Transform>> _transformCaches = new Stack<ListCache<Transform>>();
|
||||
/// <summary>
|
||||
/// Cache collection for NetworkConnections.
|
||||
/// </summary>
|
||||
private static Stack<ListCache<NetworkConnection>> _networkConnectionCaches = new Stack<ListCache<NetworkConnection>>();
|
||||
/// <summary>
|
||||
/// Cache collection for ints.
|
||||
/// </summary>
|
||||
private static Stack<ListCache<int>> _intCaches = new Stack<ListCache<int>>();
|
||||
|
||||
|
||||
#region GetCache.
|
||||
/// <summary>
|
||||
/// Returns a NetworkObject cache. Use StoreCache to return the cache.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static ListCache<NetworkObject> GetNetworkObjectCache()
|
||||
{
|
||||
ListCache<NetworkObject> result;
|
||||
if (_networkObjectCaches.Count == 0)
|
||||
result = new ListCache<NetworkObject>();
|
||||
else
|
||||
result = _networkObjectCaches.Pop();
|
||||
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns a NetworkConnection cache. Use StoreCache to return the cache.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static ListCache<NetworkConnection> GetNetworkConnectionCache()
|
||||
{
|
||||
ListCache<NetworkConnection> result;
|
||||
if (_networkConnectionCaches.Count == 0)
|
||||
result = new ListCache<NetworkConnection>();
|
||||
else
|
||||
result = _networkConnectionCaches.Pop();
|
||||
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns a Transform cache. Use StoreCache to return the cache.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static ListCache<Transform> GetTransformCache()
|
||||
{
|
||||
ListCache<Transform> result;
|
||||
if (_transformCaches.Count == 0)
|
||||
result = new ListCache<Transform>();
|
||||
else
|
||||
result = _transformCaches.Pop();
|
||||
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns a NetworkBehaviour cache. Use StoreCache to return the cache.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static ListCache<NetworkBehaviour> GetNetworkBehaviourCache()
|
||||
{
|
||||
ListCache<NetworkBehaviour> result;
|
||||
if (_networkBehaviourCaches.Count == 0)
|
||||
result = new ListCache<NetworkBehaviour>();
|
||||
else
|
||||
result = _networkBehaviourCaches.Pop();
|
||||
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns an int cache. Use StoreCache to return the cache.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static ListCache<int> GetIntCache()
|
||||
{
|
||||
ListCache<int> result;
|
||||
if (_intCaches.Count == 0)
|
||||
result = new ListCache<int>();
|
||||
else
|
||||
result = _intCaches.Pop();
|
||||
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region StoreCache.
|
||||
/// <summary>
|
||||
/// Stores a NetworkObject cache.
|
||||
/// </summary>
|
||||
/// <param name="cache"></param>
|
||||
public static void StoreCache(ListCache<NetworkObject> cache)
|
||||
{
|
||||
cache.Reset();
|
||||
_networkObjectCaches.Push(cache);
|
||||
}
|
||||
/// <summary>
|
||||
/// Stores a NetworkConnection cache.
|
||||
/// </summary>
|
||||
/// <param name="cache"></param>
|
||||
public static void StoreCache(ListCache<NetworkConnection> cache)
|
||||
{
|
||||
cache.Reset();
|
||||
_networkConnectionCaches.Push(cache);
|
||||
}
|
||||
/// <summary>
|
||||
/// Stores a Transform cache.
|
||||
/// </summary>
|
||||
/// <param name="cache"></param>
|
||||
public static void StoreCache(ListCache<Transform> cache)
|
||||
{
|
||||
cache.Reset();
|
||||
_transformCaches.Push(cache);
|
||||
}
|
||||
/// <summary>
|
||||
/// Stores a NetworkBehaviour cache.
|
||||
/// </summary>
|
||||
/// <param name="cache"></param>
|
||||
public static void StoreCache(ListCache<NetworkBehaviour> cache)
|
||||
{
|
||||
cache.Reset();
|
||||
_networkBehaviourCaches.Push(cache);
|
||||
}
|
||||
/// <summary>
|
||||
/// Stores an int cache.
|
||||
/// </summary>
|
||||
/// <param name="cache"></param>
|
||||
public static void StoreCache(ListCache<int> cache)
|
||||
{
|
||||
cache.Reset();
|
||||
_intCaches.Push(cache);
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a reusable cache of T which auto expands.
|
||||
/// </summary>
|
||||
public class ListCache<T>
|
||||
{
|
||||
#region Public.
|
||||
/// <summary>
|
||||
/// Collection cache is for.
|
||||
/// </summary>
|
||||
public List<T> Collection = new List<T>();
|
||||
/// <summary>
|
||||
/// Entries currently written.
|
||||
/// </summary>
|
||||
public int Written => Collection.Count;
|
||||
#endregion
|
||||
|
||||
#region Private.
|
||||
/// <summary>
|
||||
/// Cache for type.
|
||||
/// </summary>
|
||||
private Stack<T> _cache = new Stack<T>();
|
||||
#endregion
|
||||
|
||||
public ListCache()
|
||||
{
|
||||
Collection = new List<T>();
|
||||
}
|
||||
public ListCache(int capacity)
|
||||
{
|
||||
Collection = new List<T>(capacity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns T from cache when possible, or creates a new object when not.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private T Retrieve()
|
||||
{
|
||||
if (_cache.Count > 0)
|
||||
return _cache.Pop();
|
||||
else
|
||||
return Activator.CreateInstance<T>();
|
||||
}
|
||||
/// <summary>
|
||||
/// Stores value into the cache.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
private void Store(T value)
|
||||
{
|
||||
_cache.Push(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new value to Collection and returns it.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
public T AddReference()
|
||||
{
|
||||
T next = Retrieve();
|
||||
Collection.Add(next);
|
||||
return next;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts an bject into Collection and returns it.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
public T InsertReference(int index)
|
||||
{
|
||||
//Would just be at the end anyway.
|
||||
if (index >= Collection.Count)
|
||||
return AddReference();
|
||||
|
||||
T next = Retrieve();
|
||||
Collection.Insert(index, next);
|
||||
return next;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds value to Collection.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
public void AddValue(T value)
|
||||
{
|
||||
Collection.Add(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts value into Collection.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
|
||||
public void InsertValue(int index, T value)
|
||||
{
|
||||
//Would just be at the end anyway.
|
||||
if (index >= Collection.Count)
|
||||
AddValue(value);
|
||||
else
|
||||
Collection.Insert(index, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds values to Collection.
|
||||
/// </summary>
|
||||
/// <param name="values"></param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AddValues(ListCache<T> values)
|
||||
{
|
||||
int w = values.Written;
|
||||
List<T> c = values.Collection;
|
||||
for (int i = 0; i < w; i++)
|
||||
AddValue(c[i]);
|
||||
}
|
||||
/// <summary>
|
||||
/// Adds values to Collection.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AddValues(T[] values)
|
||||
{
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
AddValue(values[i]);
|
||||
}
|
||||
/// <summary>
|
||||
/// Adds values to Collection.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AddValues(List<T> values)
|
||||
{
|
||||
for (int i = 0; i < values.Count; i++)
|
||||
AddValue(values[i]);
|
||||
}
|
||||
/// <summary>
|
||||
/// Adds values to Collection.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AddValues(HashSet<T> values)
|
||||
{
|
||||
foreach (T item in values)
|
||||
AddValue(item);
|
||||
}
|
||||
/// <summary>
|
||||
/// Adds values to Collection.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AddValues(ISet<T> values)
|
||||
{
|
||||
foreach (T item in values)
|
||||
AddValue(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds values to Collection.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AddValues(IReadOnlyCollection<T> values)
|
||||
{
|
||||
foreach (T item in values)
|
||||
AddValue(item);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Resets cache.
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
foreach (T item in Collection)
|
||||
Store(item);
|
||||
Collection.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
11
Assets/FishNet/Runtime/Utility/Performance/ListCache.cs.meta
Normal file
11
Assets/FishNet/Runtime/Utility/Performance/ListCache.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 488b0788adfd9ee43977abd5d0280124
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
47
Assets/FishNet/Runtime/Utility/Performance/ObjectPool.cs
Normal file
47
Assets/FishNet/Runtime/Utility/Performance/ObjectPool.cs
Normal file
@ -0,0 +1,47 @@
|
||||
using FishNet.Managing;
|
||||
using FishNet.Object;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FishNet.Utility.Performance
|
||||
{
|
||||
|
||||
public abstract class ObjectPool : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// NetworkManager this ObjectPool belongs to.
|
||||
/// </summary>
|
||||
protected NetworkManager NetworkManager {get; private set;}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes this script for use.
|
||||
/// </summary>
|
||||
public virtual void InitializeOnce(NetworkManager nm)
|
||||
{
|
||||
NetworkManager = nm;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an object that has been stored using collectioNid of 0. A new object will be created if no stored objects are available.
|
||||
/// </summary>
|
||||
/// <param name="prefabId">PrefabId of the object to return.</param>
|
||||
/// <param name="asServer">True if being called on the server side.</param>
|
||||
/// <returns></returns>
|
||||
public abstract NetworkObject RetrieveObject(int prefabId, bool asServer);
|
||||
/// <summary>
|
||||
/// Returns an object that has been stored. A new object will be created if no stored objects are available.
|
||||
/// </summary>
|
||||
/// <param name="prefabId">PrefabId of the object to return.</param>
|
||||
/// <param name="asServer">True if being called on the server side.</param>
|
||||
/// <returns></returns>
|
||||
public virtual NetworkObject RetrieveObject(int prefabId, ushort collectionId, bool asServer) => null;
|
||||
/// <summary>
|
||||
/// Stores an object into the pool.
|
||||
/// </summary>
|
||||
/// <param name="instantiated">Object to store.</param>
|
||||
/// <param name="asServer">True if being called on the server side.</param>
|
||||
/// <returns></returns>
|
||||
public abstract void StoreObject(NetworkObject instantiated, bool asServer);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6ec7d855ffa7afc45b619b84ddbda27c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
339
Assets/FishNet/Runtime/Utility/Performance/RingBuffer.cs
Normal file
339
Assets/FishNet/Runtime/Utility/Performance/RingBuffer.cs
Normal file
@ -0,0 +1,339 @@
|
||||
using FishNet.Documenting;
|
||||
using FishNet.Managing;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FishNet.Utility
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Writes values to a collection of a set size, overwriting old values as needed.
|
||||
/// </summary>
|
||||
public class RingBuffer<T>
|
||||
{
|
||||
#region Types.
|
||||
/// <summary>
|
||||
/// Custom enumerator to prevent garbage collection.
|
||||
/// </summary>
|
||||
[APIExclude]
|
||||
public struct Enumerator : IEnumerator<T>
|
||||
{
|
||||
#region Public.
|
||||
/// <summary>
|
||||
/// Current entry in the enumerator.
|
||||
/// </summary>
|
||||
public T Current { get; private set; }
|
||||
/// <summary>
|
||||
/// Actual index of the last enumerated value.
|
||||
/// </summary>
|
||||
public int ActualIndex
|
||||
{
|
||||
get
|
||||
{
|
||||
int total = (_startIndex + (_read - 1));
|
||||
int capacity = _rollingCollection.Capacity;
|
||||
if (total >= capacity)
|
||||
total -= capacity;
|
||||
|
||||
return total;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Simulated index of the last enumerated value.
|
||||
/// </summary>
|
||||
public int SimulatedIndex => (_read - 1);
|
||||
#endregion
|
||||
|
||||
#region Private.
|
||||
/// <summary>
|
||||
/// RollingCollection to use.
|
||||
/// </summary>
|
||||
private RingBuffer<T> _rollingCollection;
|
||||
/// <summary>
|
||||
/// Collection to iterate.
|
||||
/// </summary>
|
||||
private readonly T[] _collection;
|
||||
/// <summary>
|
||||
/// Number of entries read during the enumeration.
|
||||
/// </summary>
|
||||
private int _read;
|
||||
/// <summary>
|
||||
/// Start index of enumerations.
|
||||
/// </summary>
|
||||
private int _startIndex;
|
||||
#endregion
|
||||
|
||||
public Enumerator(RingBuffer<T> c)
|
||||
{
|
||||
_read = 0;
|
||||
_startIndex = 0;
|
||||
_rollingCollection = c;
|
||||
_collection = c.Collection;
|
||||
Current = default;
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
int written = _rollingCollection.Count;
|
||||
if (_read >= written)
|
||||
{
|
||||
ResetRead();
|
||||
return false;
|
||||
}
|
||||
|
||||
int index = (_startIndex + _read);
|
||||
int capacity = _rollingCollection.Capacity;
|
||||
if (index >= capacity)
|
||||
index -= capacity;
|
||||
Current = _collection[index];
|
||||
|
||||
_read++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a new start index to begin reading at.
|
||||
/// </summary>
|
||||
public void SetStartIndex(int index)
|
||||
{
|
||||
_startIndex = index;
|
||||
ResetRead();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sets a new start index to begin reading at.
|
||||
/// </summary>
|
||||
public void AddStartIndex(int value)
|
||||
{
|
||||
_startIndex += value;
|
||||
|
||||
int cap = _rollingCollection.Capacity;
|
||||
if (_startIndex > cap)
|
||||
_startIndex -= cap;
|
||||
else if (_startIndex < 0)
|
||||
_startIndex += cap;
|
||||
|
||||
ResetRead();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets number of entries read during the enumeration.
|
||||
/// </summary>
|
||||
public void ResetRead()
|
||||
{
|
||||
_read = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets read count.
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
_startIndex = 0;
|
||||
ResetRead();
|
||||
}
|
||||
|
||||
object IEnumerator.Current => Current;
|
||||
public void Dispose() { }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public.
|
||||
/// <summary>
|
||||
/// Current write index of the collection.
|
||||
/// </summary>
|
||||
public int WriteIndex { get; private set; }
|
||||
/// <summary>
|
||||
/// Number of entries currently written.
|
||||
/// </summary>
|
||||
public int Count => _written;
|
||||
/// <summary>
|
||||
/// Maximum size of the collection.
|
||||
/// </summary>
|
||||
public int Capacity => Collection.Length;
|
||||
/// <summary>
|
||||
/// Collection being used.
|
||||
/// </summary>
|
||||
public T[] Collection = new T[0];
|
||||
/// <summary>
|
||||
/// True if initialized.
|
||||
/// </summary>
|
||||
public bool Initialized { get; private set; }
|
||||
#endregion
|
||||
|
||||
#region Private.
|
||||
/// <summary>
|
||||
/// Number of entries written. This will never go beyond the capacity but will be less until capacity is filled.
|
||||
/// </summary>
|
||||
private int _written;
|
||||
/// <summary>
|
||||
/// Enumerator for the collection.
|
||||
/// </summary>
|
||||
private Enumerator _enumerator;
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the collection at length.
|
||||
/// </summary>
|
||||
/// <param name="capacity">Size to initialize the collection as. This cannot be changed after initialized.</param>
|
||||
public void Initialize(int capacity)
|
||||
{
|
||||
if (capacity <= 0)
|
||||
{
|
||||
NetworkManager.StaticLogError($"Collection length must be larger than 0.");
|
||||
return;
|
||||
}
|
||||
|
||||
Collection = new T[capacity];
|
||||
_enumerator = new Enumerator(this);
|
||||
Initialized = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the collection to default values and resets indexing.
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
for (int i = 0; i < Collection.Length; i++)
|
||||
Collection[i] = default;
|
||||
|
||||
Reset();
|
||||
}
|
||||
/// <summary>
|
||||
/// Resets the collection without clearing.
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
_written = 0;
|
||||
WriteIndex = 0;
|
||||
_enumerator.Reset();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an entry to the collection, returning a replaced entry.
|
||||
/// </summary>
|
||||
/// <param name="data">Data to add.</param>
|
||||
/// <returns>Replaced entry. Value will be default if no entry was replaced.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public T Add(T data)
|
||||
{
|
||||
if (!IsInitializedWithError())
|
||||
return default;
|
||||
|
||||
int capacity = Capacity;
|
||||
T current = Collection[WriteIndex];
|
||||
Collection[WriteIndex] = data;
|
||||
|
||||
WriteIndex++;
|
||||
_written++;
|
||||
//If index would exceed next iteration reset it.
|
||||
if (WriteIndex >= capacity)
|
||||
WriteIndex = 0;
|
||||
|
||||
/* If written has exceeded capacity
|
||||
* then the start index needs to be moved
|
||||
* to adjust for overwritten values. */
|
||||
if (_written > capacity)
|
||||
{
|
||||
_written = capacity;
|
||||
_enumerator.SetStartIndex(WriteIndex);
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns value in actual index as it relates to simulated index.
|
||||
/// </summary>
|
||||
/// <param name="simulatedIndex">Simulated index to return. A value of 0 would return the first simulated index in the collection.</param>
|
||||
/// <returns></returns>
|
||||
public T this[int simulatedIndex]
|
||||
{
|
||||
get
|
||||
{
|
||||
int offset = (Capacity - _written) + simulatedIndex + WriteIndex;
|
||||
if (offset >= Capacity)
|
||||
offset -= Capacity;
|
||||
return Collection[offset];
|
||||
}
|
||||
set
|
||||
{
|
||||
int offset = (Capacity - _written) + simulatedIndex + WriteIndex;
|
||||
if (offset >= Capacity)
|
||||
offset -= Capacity;
|
||||
Collection[offset] = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns Enumerator for the collection.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Enumerator GetEnumerator()
|
||||
{
|
||||
if (!IsInitializedWithError())
|
||||
return default;
|
||||
|
||||
_enumerator.ResetRead();
|
||||
return _enumerator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes values from the simulated start of the collection.
|
||||
/// </summary>
|
||||
/// <param name="fromStart">True to remove from the start, false to remove from the end.</param>
|
||||
/// <param name="length">Number of entries to remove.</param>
|
||||
public void RemoveRange(bool fromStart, int length)
|
||||
{
|
||||
if (length == 0)
|
||||
return;
|
||||
if (length < 0)
|
||||
{
|
||||
NetworkManager.StaticLogError($"Negative values cannot be removed.");
|
||||
return;
|
||||
}
|
||||
//Full reset if value is at or more than written.
|
||||
if (length >= _written)
|
||||
{
|
||||
Reset();
|
||||
return;
|
||||
}
|
||||
|
||||
_written -= length;
|
||||
if (fromStart)
|
||||
{
|
||||
_enumerator.AddStartIndex(length);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
WriteIndex -= length;
|
||||
if (WriteIndex < 0)
|
||||
WriteIndex += Capacity;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if initialized and errors if not.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private bool IsInitializedWithError()
|
||||
{
|
||||
if (!Initialized)
|
||||
{
|
||||
NetworkManager.StaticLogError($"RingBuffer has not yet been initialized.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a066c51d748a04546875bd7d43118837
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
39
Assets/FishNet/Runtime/Utility/Performance/Transforms.cs
Normal file
39
Assets/FishNet/Runtime/Utility/Performance/Transforms.cs
Normal file
@ -0,0 +1,39 @@
|
||||
using FishNet.Object;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FishNet.Utility.Performance
|
||||
{
|
||||
|
||||
public static class GetNonAlloc
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
private static List<Transform> _transformList = new List<Transform>();
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
private static List<NetworkBehaviour> _networkBehavioursList = new List<NetworkBehaviour>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets all NetworkBehaviours on a transform.
|
||||
/// </summary>
|
||||
public static List<NetworkBehaviour> GetNetworkBehaviours(this Transform t)
|
||||
{
|
||||
t.GetComponents(_networkBehavioursList);
|
||||
return _networkBehavioursList;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all transforms on transform and it's children.
|
||||
/// </summary>
|
||||
public static List<Transform> GetTransformsInChildrenNonAlloc(this Transform t, bool includeInactive = false)
|
||||
{
|
||||
t.GetComponentsInChildren<Transform>(includeInactive, _transformList);
|
||||
return _transformList;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7d0740f919077254c8ffb131b9587407
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user