Skip to content

Commit

Permalink
fix: adding a check to make sure asset id isn't created from empty st…
Browse files Browse the repository at this point in the history
…ring
  • Loading branch information
James-Frowen committed Nov 7, 2021
1 parent bc5da6a commit 26a5bbc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Assets/Mirage/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ public int PrefabHash
{
#if UNITY_EDITOR
// This is important because sometimes OnValidate does not run (like when adding view to prefab with no child links)
if (_prefabHash == 0)
// also check for hash that is empty string, if it is, reset its ID to its real path
if (_prefabHash == 0 || _prefabHash == StringHash.EmptyString)
SetupIDs();
#endif
return _prefabHash;
Expand Down Expand Up @@ -416,6 +417,11 @@ void OnValidate()

void AssignAssetID(string path)
{
if (string.IsNullOrEmpty(path))
{
return;
}

_prefabHash = path.GetStableHashCode();
}

Expand Down
4 changes: 3 additions & 1 deletion Assets/Mirage/Runtime/StringHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace Mirage
{
public static class StringHash
{
public const int EmptyString = 23;

/// <summary>
/// Gets a hash for a string. This hash will be the same on all platforms
/// </summary>
Expand All @@ -12,7 +14,7 @@ public static int GetStableHashCode(this string text)
{
unchecked
{
int hash = 23;
int hash = EmptyString;
foreach (char c in text)
hash = hash * 31 + c;
return hash;
Expand Down

0 comments on commit 26a5bbc

Please sign in to comment.