Skip to content

Commit

Permalink
fix: fixing warning created from id generator
Browse files Browse the repository at this point in the history
using SerializedObject was causing warning when the editor first opened
using Undo should fix this
  • Loading branch information
James-Frowen committed Jul 16, 2022
1 parent 9427936 commit eaee748
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
21 changes: 21 additions & 0 deletions Assets/Mirage/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,27 @@ internal set
}
}

#if UNITY_EDITOR
/// <summary>
/// Gets PrefabHash avoiding runtime checks
/// <para>used by NetworkIdentityIdGenerator</para>
/// </summary>
internal int Editor_PrefabHash
{
get => _prefabHash;
set => _prefabHash = value;
}

/// <summary>
/// Gets SceneId avoiding runtime checks
/// <para>used by NetworkIdentityIdGenerator</para>
/// </summary>
internal ulong Editor_SceneId
{
get => _sceneId;
set => _sceneId = value;
}
#endif


[Header("Events")]
Expand Down
40 changes: 22 additions & 18 deletions Assets/Mirage/Runtime/NetworkIdentityIdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,53 +219,57 @@ private static int GetRandomUInt()
/// </summary>
private class IdentityWrapper
{
private const long ID_MASK = (long)0x0000_0000_FFFF_FFFFul;
private const long HASH_MASK = unchecked((long)0xFFFF_FFFF_0000_0000ul);
private const ulong ID_MASK = 0x0000_0000_FFFF_FFFFul;
private const ulong HASH_MASK = 0xFFFF_FFFF_0000_0000ul;
private readonly NetworkIdentity _identity;
private readonly SerializedObject _serializedObject;
private readonly SerializedProperty _prefabHashProp;
private readonly SerializedProperty _sceneIdProp;

public IdentityWrapper(NetworkIdentity identity)
{
if (identity == null) throw new ArgumentNullException(nameof(identity));

_identity = identity;

_serializedObject = new SerializedObject(identity);
_prefabHashProp = _serializedObject.FindProperty("_prefabHash");
_sceneIdProp = _serializedObject.FindProperty("_sceneId");
}

public int PrefabHash
{
get => _prefabHashProp.intValue;
get => _identity.Editor_PrefabHash;
set
{
_prefabHashProp.intValue = value;
_serializedObject.ApplyModifiedProperties();
if (PrefabHash == value)
return;

Undo.RecordObject(_identity, "Set PrefabHash");
_identity.Editor_PrefabHash = value;
}
}


public int SceneId
{
get => (int)(_sceneIdProp.intValue & ID_MASK);
get => (int)(_identity.Editor_SceneId & ID_MASK);
set
{
// just use get here so we get the correct part of id
if (SceneId == value)
return;

Undo.RecordObject(_identity, "Set SceneId");
// have to mask incoming number incase it is negative
_sceneIdProp.longValue = (_sceneIdProp.longValue & HASH_MASK) | (value & ID_MASK);
_serializedObject.ApplyModifiedProperties();
_identity.Editor_SceneId = (_identity.SceneId & HASH_MASK) | ((ulong)value & ID_MASK);
}
}

public int SceneHash
{
get => (int)((_sceneIdProp.intValue & HASH_MASK) >> 32);
get => (int)((_identity.Editor_SceneId & HASH_MASK) >> 32);
set
{
_sceneIdProp.longValue = (((long)value) << 32) | (_sceneIdProp.longValue & ID_MASK);
_serializedObject.ApplyModifiedProperties();
// just use get here so we get the correct part of id
if (SceneHash == value)
return;

Undo.RecordObject(_identity, "Set SceneHash");
_identity.Editor_SceneId = (((ulong)value) << 32) | (_identity.SceneId & ID_MASK);
}
}

Expand Down

0 comments on commit eaee748

Please sign in to comment.