Skip to content

Commit

Permalink
fix: NetworkBehaviour.SyncVarGameObjectEqual made protected again so …
Browse files Browse the repository at this point in the history
…that Weaver finds it again
  • Loading branch information
miwarnec committed Mar 9, 2020
1 parent 76dd0c8 commit 165a1dd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
4 changes: 3 additions & 1 deletion Assets/Mirror/Runtime/NetworkBehaviour.cs
Expand Up @@ -469,8 +469,10 @@ public static CmdDelegate GetDelegate(int cmdHash)
#region Helpers

// helper function for [SyncVar] GameObjects.
// IMPORTANT: keep as 'protected', not 'internal', otherwise Weaver
// can't resolve it
[EditorBrowsable(EditorBrowsableState.Never)]
internal bool SyncVarGameObjectEqual(GameObject newGameObject, uint netIdField)
protected bool SyncVarGameObjectEqual(GameObject newGameObject, uint netIdField)
{
uint newNetId = 0;
if (newGameObject != null)
Expand Down
32 changes: 24 additions & 8 deletions Assets/Mirror/Tests/Editor/NetworkBehaviourTests.cs
Expand Up @@ -13,6 +13,14 @@ class EmptyBehaviour : NetworkBehaviour
{
}

class SyncVarGameObjectEqualExposedBehaviour : NetworkBehaviour
{
public bool SyncVarGameObjectEqualExposed(GameObject newGameObject, uint netIdField)
{
return SyncVarGameObjectEqual(newGameObject, netIdField);
}
}

// we need to inherit from networkbehaviour to test protected functions
public class NetworkBehaviourSendCommandInternalComponent : NetworkBehaviour
{
Expand Down Expand Up @@ -781,7 +789,8 @@ public void SyncVarGameObjectEqualZeroNetIdNullIsTrue()
// better to return false here.
// => we possibly return false so that resync doesn't happen when
// GO disappears? or not?
bool result = emptyBehaviour.SyncVarGameObjectEqual(null, identity.netId);
SyncVarGameObjectEqualExposedBehaviour comp = gameObject.AddComponent<SyncVarGameObjectEqualExposedBehaviour>();
bool result = comp.SyncVarGameObjectEqualExposed(null, identity.netId);
Assert.That(result, Is.True);
}

Expand All @@ -793,7 +802,8 @@ public void SyncVarGameObjectEqualNull()
identity.netId = 42;

// null should return false
bool result = emptyBehaviour.SyncVarGameObjectEqual(null, identity.netId);
SyncVarGameObjectEqualExposedBehaviour comp = gameObject.AddComponent<SyncVarGameObjectEqualExposedBehaviour>();
bool result = comp.SyncVarGameObjectEqualExposed(null, identity.netId);
Assert.That(result, Is.False);
}

Expand All @@ -808,7 +818,8 @@ public void SyncVarGameObjectEqualZeroNetIdAndGOWithoutIdentityComponentIsTrue()
// => we possibly return false so that resync doesn't happen when
// GO disappears? or not?
GameObject go = new GameObject();
bool result = emptyBehaviour.SyncVarGameObjectEqual(go, identity.netId);
SyncVarGameObjectEqualExposedBehaviour comp = go.AddComponent<SyncVarGameObjectEqualExposedBehaviour>();
bool result = comp.SyncVarGameObjectEqualExposed(go, identity.netId);
Assert.That(result, Is.True);
}

Expand All @@ -821,7 +832,8 @@ public void SyncVarGameObjectEqualWithoutIdentityComponent()

// gameobject without networkidentity component should return false
GameObject go = new GameObject();
bool result = emptyBehaviour.SyncVarGameObjectEqual(go, identity.netId);
SyncVarGameObjectEqualExposedBehaviour comp = go.AddComponent<SyncVarGameObjectEqualExposedBehaviour>();
bool result = comp.SyncVarGameObjectEqualExposed(go, identity.netId);
Assert.That(result, Is.False);

// clean up
Expand All @@ -838,8 +850,9 @@ public void SyncVarGameObjectEqualValidGOWithDifferentNetId()
// gameobject with valid networkidentity and netid that is different
GameObject go = new GameObject();
NetworkIdentity ni = go.AddComponent<NetworkIdentity>();
SyncVarGameObjectEqualExposedBehaviour comp = go.AddComponent<SyncVarGameObjectEqualExposedBehaviour>();
ni.netId = 43;
bool result = emptyBehaviour.SyncVarGameObjectEqual(go, identity.netId);
bool result = comp.SyncVarGameObjectEqualExposed(go, identity.netId);
Assert.That(result, Is.False);

// clean up
Expand All @@ -856,8 +869,9 @@ public void SyncVarGameObjectEqualValidGOWithSameNetId()
// gameobject with valid networkidentity and netid that is different
GameObject go = new GameObject();
NetworkIdentity ni = go.AddComponent<NetworkIdentity>();
SyncVarGameObjectEqualExposedBehaviour comp = go.AddComponent<SyncVarGameObjectEqualExposedBehaviour>();
ni.netId = 42;
bool result = emptyBehaviour.SyncVarGameObjectEqual(go, identity.netId);
bool result = comp.SyncVarGameObjectEqualExposed(go, identity.netId);
Assert.That(result, Is.True);

// clean up
Expand All @@ -874,8 +888,9 @@ public void SyncVarGameObjectEqualUnspawnedGO()
// gameobject with valid networkidentity and 0 netid that is unspawned
GameObject go = new GameObject();
go.AddComponent<NetworkIdentity>();
SyncVarGameObjectEqualExposedBehaviour comp = go.AddComponent<SyncVarGameObjectEqualExposedBehaviour>();
LogAssert.Expect(LogType.Warning, "SetSyncVarGameObject GameObject " + go + " has a zero netId. Maybe it is not spawned yet?");
bool result = emptyBehaviour.SyncVarGameObjectEqual(go, identity.netId);
bool result = comp.SyncVarGameObjectEqualExposed(go, identity.netId);
Assert.That(result, Is.False);

// clean up
Expand All @@ -889,8 +904,9 @@ public void SyncVarGameObjectEqualUnspawnedGOZeroNetIdIsTrue()
// unspawned go and identity.netid==0 returns true (=equal)
GameObject go = new GameObject();
go.AddComponent<NetworkIdentity>();
SyncVarGameObjectEqualExposedBehaviour comp = go.AddComponent<SyncVarGameObjectEqualExposedBehaviour>();
LogAssert.Expect(LogType.Warning, "SetSyncVarGameObject GameObject " + go + " has a zero netId. Maybe it is not spawned yet?");
bool result = emptyBehaviour.SyncVarGameObjectEqual(go, identity.netId);
bool result = comp.SyncVarGameObjectEqualExposed(go, identity.netId);
Assert.That(result, Is.True);

// clean up
Expand Down

0 comments on commit 165a1dd

Please sign in to comment.