[Add] FishNet
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
using FishNet.Documenting;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using GameKit.Dependencies.Utilities;
|
||||
using System.Text;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
using FishNet.Object;
|
||||
|
||||
namespace FishNet.Managing.Object
|
||||
{
|
||||
[APIExclude]
|
||||
// [CreateAssetMenu(fileName = "New DefaultPrefabObjects", menuName = "FishNet/Spawnable Prefabs/Default Prefab Objects")]
|
||||
public class DefaultPrefabObjects : SinglePrefabObjects
|
||||
{
|
||||
/// <summary>
|
||||
/// Used for version rebuilding.
|
||||
/// </summary>
|
||||
private StringBuilder _stringBuilder = new();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sets asset path hashes for prefabs starting at index, or if missing.
|
||||
/// </summary
|
||||
/// <return>Returns true if one or more NetworkObjects were updated.</return>
|
||||
internal bool SetAssetPathHashes(int index)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
bool dirtied = false;
|
||||
int count = base.GetObjectCount();
|
||||
|
||||
if (count == 0)
|
||||
return false;
|
||||
|
||||
if (index < 0 || index >= count)
|
||||
{
|
||||
Debug.LogError($"Index {index} is out of range when trying to set asset path hashes. Collection length is {count}. Defaulf prefabs may need to be rebuilt.");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
NetworkObject n = Prefabs[i];
|
||||
if (i < index)
|
||||
continue;
|
||||
|
||||
string pathAndName = $"{AssetDatabase.GetAssetPath(n.gameObject)}{n.gameObject.name}".Trim().ToLowerInvariant();
|
||||
|
||||
_stringBuilder.Clear();
|
||||
foreach (char c in pathAndName)
|
||||
{
|
||||
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))
|
||||
_stringBuilder.Append(c);
|
||||
}
|
||||
|
||||
ulong hashcode = _stringBuilder.ToString().GetStableHashU64();
|
||||
// Already set.
|
||||
if (n.AssetPathHash == hashcode)
|
||||
continue;
|
||||
|
||||
n.SetAssetPathHash(hashcode);
|
||||
EditorUtility.SetDirty(n);
|
||||
dirtied = true;
|
||||
}
|
||||
|
||||
//Check for conflicts.
|
||||
Dictionary<ulong, string> hashesAndPaths = new();
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
NetworkObject n = Prefabs[i];
|
||||
|
||||
string pathAndName = $"{AssetDatabase.GetAssetPath(n.gameObject)}{n.gameObject.name}";
|
||||
|
||||
if (hashesAndPaths.TryGetValueIL2CPP(n.AssetPathHash, out string path))
|
||||
{
|
||||
Debug.LogError($"Assets {pathAndName} and {path} have the same assetPath hash of {n.AssetPathHash}. Please modify the prefab name of either to resolve.");
|
||||
dirtied = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
hashesAndPaths.Add(n.AssetPathHash, pathAndName);
|
||||
}
|
||||
}
|
||||
|
||||
return dirtied;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sorts prefabs by name and path hashcode.
|
||||
/// </summary>
|
||||
internal void Sort()
|
||||
{
|
||||
if (base.GetObjectCount() == 0)
|
||||
return;
|
||||
|
||||
Dictionary<ulong, NetworkObject> hashcodesAndNobs = new();
|
||||
List<ulong> hashcodes = new();
|
||||
|
||||
bool error = false;
|
||||
foreach (NetworkObject n in Prefabs)
|
||||
{
|
||||
hashcodes.Add(n.AssetPathHash);
|
||||
// If hashcode is 0 something is wrong
|
||||
if (n.AssetPathHash == 0)
|
||||
{
|
||||
error = true;
|
||||
Debug.LogError($"AssetPathHash is not set for GameObject {n.name}.");
|
||||
}
|
||||
hashcodesAndNobs.Add(n.AssetPathHash, n);
|
||||
}
|
||||
// An error occured, no reason to continue.
|
||||
if (error)
|
||||
{
|
||||
Debug.LogError($"One or more NetworkObject prefabs did not have their AssetPathHash set. This usually occurs when a prefab cannot be saved. Check the specified prefabs for missing scripts or serialization errors and correct them, then use Fish-Networking -> Refresh Default Prefabs.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Once all hashes have been made re-add them to prefabs sorted.
|
||||
hashcodes.Sort();
|
||||
// Build to a new list using sorted hashcodes.
|
||||
List<NetworkObject> sortedNobs = new();
|
||||
foreach (ulong hc in hashcodes)
|
||||
sortedNobs.Add(hashcodesAndNobs[hc]);
|
||||
|
||||
base.Clear();
|
||||
base.AddObjects(sortedNobs, checkForDuplicates: false, initializeAdded: false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3ad70174b079c2f4ebc7931d3dd1af6f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 207815
|
||||
packageName: 'FishNet: Networking Evolved'
|
||||
packageVersion: 4.6.22R
|
||||
assetPath: Assets/FishNet/Runtime/Managing/Object/PrefabObjects/DefaultPrefabObjects.cs
|
||||
uploadId: 866910
|
||||
@@ -0,0 +1,130 @@
|
||||
using FishNet.Documenting;
|
||||
using FishNet.Object;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FishNet.Managing.Object
|
||||
{
|
||||
// document
|
||||
[APIExclude]
|
||||
[CreateAssetMenu(fileName = "New DualPrefabObjects", menuName = "FishNet/Spawnable Prefabs/Dual Prefab Objects")]
|
||||
public class DualPrefabObjects : PrefabObjects
|
||||
{
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
[Tooltip("Prefabs which may be spawned.")]
|
||||
[SerializeField]
|
||||
private List<DualPrefab> _prefabs = new();
|
||||
/// <summary>
|
||||
/// Prefabs which may be spawned.
|
||||
/// </summary>
|
||||
public IReadOnlyList<DualPrefab> Prefabs => _prefabs;
|
||||
|
||||
public override void Clear()
|
||||
{
|
||||
_prefabs.Clear();
|
||||
}
|
||||
|
||||
public override int GetObjectCount()
|
||||
{
|
||||
return _prefabs.Count;
|
||||
}
|
||||
|
||||
public override NetworkObject GetObject(bool asServer, int id)
|
||||
{
|
||||
if (id < 0 || id >= _prefabs.Count)
|
||||
{
|
||||
NetworkManagerExtensions.LogError($"PrefabId {id} is out of range.");
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
DualPrefab dp = _prefabs[id];
|
||||
NetworkObject nob = asServer ? dp.Server : dp.Client;
|
||||
if (nob == null)
|
||||
{
|
||||
string lookupSide = asServer ? "server" : "client";
|
||||
NetworkManagerExtensions.LogError($"Prefab for {lookupSide} on id {id} is null ");
|
||||
}
|
||||
|
||||
return nob;
|
||||
}
|
||||
}
|
||||
|
||||
public override void RemoveNull()
|
||||
{
|
||||
for (int i = 0; i < _prefabs.Count; i++)
|
||||
{
|
||||
if (_prefabs[i].Server == null || _prefabs[i].Client == null)
|
||||
{
|
||||
_prefabs.RemoveAt(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void AddObject(DualPrefab dualPrefab, bool checkForDuplicates = false, bool initializeAdded = true)
|
||||
{
|
||||
AddObjects(new DualPrefab[] { dualPrefab }, checkForDuplicates, initializeAdded);
|
||||
}
|
||||
|
||||
public override void AddObjects(List<DualPrefab> dualPrefabs, bool checkForDuplicates = false, bool initializeAdded = true)
|
||||
{
|
||||
AddObjects(dualPrefabs.ToArray(), checkForDuplicates, initializeAdded);
|
||||
}
|
||||
|
||||
public override void AddObjects(DualPrefab[] dualPrefabs, bool checkForDuplicates = false, bool initializeAdded = true)
|
||||
{
|
||||
if (!checkForDuplicates)
|
||||
{
|
||||
_prefabs.AddRange(dualPrefabs);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (DualPrefab dp in dualPrefabs)
|
||||
AddUniqueNetworkObjects(dp);
|
||||
}
|
||||
|
||||
if (initializeAdded && Application.isPlaying)
|
||||
InitializePrefabRange(0);
|
||||
}
|
||||
|
||||
private void AddUniqueNetworkObjects(DualPrefab dp)
|
||||
{
|
||||
for (int i = 0; i < _prefabs.Count; i++)
|
||||
{
|
||||
if (_prefabs[i].Server == dp.Server && _prefabs[i].Client == dp.Client)
|
||||
return;
|
||||
}
|
||||
|
||||
_prefabs.Add(dp);
|
||||
}
|
||||
|
||||
public override void InitializePrefabRange(int startIndex)
|
||||
{
|
||||
for (int i = startIndex; i < _prefabs.Count; i++)
|
||||
{
|
||||
ManagedObjects.InitializePrefab(_prefabs[i].Server, i, CollectionId);
|
||||
ManagedObjects.InitializePrefab(_prefabs[i].Client, i, CollectionId);
|
||||
}
|
||||
}
|
||||
|
||||
#region Unused.
|
||||
public override void AddObject(NetworkObject networkObject, bool checkForDuplicates = false, bool initializeAdded = true)
|
||||
{
|
||||
NetworkManagerExtensions.LogError($"Single prefabs are not supported with DualPrefabObjects. Make a SinglePrefabObjects asset instead.");
|
||||
}
|
||||
|
||||
public override void AddObjects(List<NetworkObject> networkObjects, bool checkForDuplicates = false, bool initializeAdded = true)
|
||||
{
|
||||
NetworkManagerExtensions.LogError($"Single prefabs are not supported with DualPrefabObjects. Make a SinglePrefabObjects asset instead.");
|
||||
}
|
||||
|
||||
public override void AddObjects(NetworkObject[] networkObjects, bool checkForDuplicates = false, bool initializeAdded = true)
|
||||
{
|
||||
NetworkManagerExtensions.LogError($"Single prefabs are not supported with DualPrefabObjects. Make a SinglePrefabObjects asset instead.");
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e4b890523e001c74a9a2bf0d6340e5f7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 207815
|
||||
packageName: 'FishNet: Networking Evolved'
|
||||
packageVersion: 4.6.22R
|
||||
assetPath: Assets/FishNet/Runtime/Managing/Object/PrefabObjects/DualPrefabObjects.cs
|
||||
uploadId: 866910
|
||||
@@ -0,0 +1,34 @@
|
||||
using FishNet.Documenting;
|
||||
using FishNet.Object;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FishNet.Managing.Object
|
||||
{
|
||||
// document
|
||||
[APIExclude]
|
||||
public abstract class PrefabObjects : ScriptableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// CollectionId for this PrefabObjects.
|
||||
/// </summary>
|
||||
public ushort CollectionId { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Sets CollectionIdValue.
|
||||
/// </summary>
|
||||
internal void SetCollectionId(ushort id) => CollectionId = id;
|
||||
|
||||
public abstract void Clear();
|
||||
public abstract int GetObjectCount();
|
||||
public abstract NetworkObject GetObject(bool asServer, int id);
|
||||
public abstract void RemoveNull();
|
||||
public abstract void AddObject(NetworkObject networkObject, bool checkForDuplicates = false, bool initializeAdded = true);
|
||||
public abstract void AddObjects(List<NetworkObject> networkObjects, bool checkForDuplicates = false, bool initializeAdded = true);
|
||||
public abstract void AddObjects(NetworkObject[] networkObjects, bool checkForDuplicates = false, bool initializeAdded = true);
|
||||
public abstract void AddObject(DualPrefab dualPrefab, bool checkForDuplicates = false, bool initializeAdded = true);
|
||||
public abstract void AddObjects(List<DualPrefab> dualPrefab, bool checkForDuplicates = false, bool initializeAdded = true);
|
||||
public abstract void AddObjects(DualPrefab[] dualPrefab, bool checkForDuplicates = false, bool initializeAdded = true);
|
||||
public abstract void InitializePrefabRange(int startIndex);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c5a7beb0d6ee75a4fb1f058eb3e2640a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 207815
|
||||
packageName: 'FishNet: Networking Evolved'
|
||||
packageVersion: 4.6.22R
|
||||
assetPath: Assets/FishNet/Runtime/Managing/Object/PrefabObjects/PrefabObjects.cs
|
||||
uploadId: 866910
|
||||
@@ -0,0 +1,125 @@
|
||||
using FishNet.Documenting;
|
||||
using FishNet.Object;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FishNet.Managing.Object
|
||||
{
|
||||
// document
|
||||
[APIExclude]
|
||||
[CreateAssetMenu(fileName = "New SinglePrefabObjects", menuName = "FishNet/Spawnable Prefabs/Single Prefab Objects")]
|
||||
public class SinglePrefabObjects : PrefabObjects
|
||||
{
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
[Tooltip("Prefabs which may be spawned.")]
|
||||
[SerializeField]
|
||||
private List<NetworkObject> _prefabs = new();
|
||||
/// <summary>
|
||||
/// Prefabs which may be spawned.
|
||||
/// </summary>
|
||||
public IReadOnlyList<NetworkObject> Prefabs => _prefabs;
|
||||
|
||||
public override void Clear()
|
||||
{
|
||||
_prefabs.Clear();
|
||||
}
|
||||
|
||||
public override int GetObjectCount()
|
||||
{
|
||||
return _prefabs.Count;
|
||||
}
|
||||
|
||||
public override NetworkObject GetObject(bool asServer, int id)
|
||||
{
|
||||
if (id < 0 || id >= _prefabs.Count)
|
||||
{
|
||||
NetworkManagerExtensions.LogError($"PrefabId {id} is out of range.");
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
NetworkObject nob = _prefabs[id];
|
||||
if (nob == null)
|
||||
NetworkManagerExtensions.LogError($"Prefab on id {id} is null.");
|
||||
|
||||
return nob;
|
||||
}
|
||||
}
|
||||
|
||||
public override void RemoveNull()
|
||||
{
|
||||
for (int i = 0; i < _prefabs.Count; i++)
|
||||
{
|
||||
if (_prefabs[i] == null)
|
||||
{
|
||||
_prefabs.RemoveAt(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void AddObject(NetworkObject networkObject, bool checkForDuplicates = false, bool initializeAdded = true)
|
||||
{
|
||||
if (!checkForDuplicates)
|
||||
_prefabs.Add(networkObject);
|
||||
else
|
||||
AddUniqueNetworkObject(networkObject);
|
||||
|
||||
if (initializeAdded && Application.isPlaying)
|
||||
InitializePrefabRange(0);
|
||||
}
|
||||
|
||||
public override void AddObjects(List<NetworkObject> networkObjects, bool checkForDuplicates = false, bool initializeAdded = true)
|
||||
{
|
||||
if (!checkForDuplicates)
|
||||
{
|
||||
_prefabs.AddRange(networkObjects);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (NetworkObject nob in networkObjects)
|
||||
AddUniqueNetworkObject(nob);
|
||||
}
|
||||
|
||||
if (initializeAdded && Application.isPlaying)
|
||||
InitializePrefabRange(0);
|
||||
}
|
||||
|
||||
public override void AddObjects(NetworkObject[] networkObjects, bool checkForDuplicates = false, bool initializeAdded = true)
|
||||
{
|
||||
AddObjects(networkObjects.ToList(), checkForDuplicates, initializeAdded);
|
||||
}
|
||||
|
||||
private void AddUniqueNetworkObject(NetworkObject nob)
|
||||
{
|
||||
if (!_prefabs.Contains(nob))
|
||||
_prefabs.Add(nob);
|
||||
}
|
||||
|
||||
public override void InitializePrefabRange(int startIndex)
|
||||
{
|
||||
for (int i = startIndex; i < _prefabs.Count; i++)
|
||||
ManagedObjects.InitializePrefab(_prefabs[i], i, CollectionId);
|
||||
}
|
||||
|
||||
#region Unused.
|
||||
public override void AddObject(DualPrefab dualPrefab, bool checkForDuplicates = false, bool initializeAdded = true)
|
||||
{
|
||||
NetworkManagerExtensions.LogError($"Dual prefabs are not supported with SinglePrefabObjects. Make a DualPrefabObjects asset instead.");
|
||||
}
|
||||
|
||||
public override void AddObjects(List<DualPrefab> dualPrefab, bool checkForDuplicates = false, bool initializeAdded = true)
|
||||
{
|
||||
NetworkManagerExtensions.LogError($"Dual prefabs are not supported with SinglePrefabObjects. Make a DualPrefabObjects asset instead.");
|
||||
}
|
||||
|
||||
public override void AddObjects(DualPrefab[] dualPrefab, bool checkForDuplicates = false, bool initializeAdded = true)
|
||||
{
|
||||
NetworkManagerExtensions.LogError($"Dual prefabs are not supported with SinglePrefabObjects. Make a DualPrefabObjects asset instead.");
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4489d77032a81ef42b0067acf2737d4d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 207815
|
||||
packageName: 'FishNet: Networking Evolved'
|
||||
packageVersion: 4.6.22R
|
||||
assetPath: Assets/FishNet/Runtime/Managing/Object/PrefabObjects/SinglePrefabObjects.cs
|
||||
uploadId: 866910
|
||||
Reference in New Issue
Block a user