fix: #3341 SyncLists can now be modified before spawning again

This commit is contained in:
vis2k 2023-01-04 00:53:50 +01:00
parent 02218bda13
commit 8570afab94
2 changed files with 19 additions and 6 deletions

View File

@ -232,17 +232,25 @@ protected void InitSyncObject(SyncObject syncObject)
// carefully check each mode separately to ensure correct results.
// fixes: https://github.com/MirrorNetworking/Mirror/issues/3342
// normally we would check isServer / isClient here.
// users may add to SyncLists before the object was spawned.
// isServer / isClient would still be false.
// so we need to check NetworkServer/Client.active here instead.
// host mode: any ServerToClient and any local client owned
if (isServer && isClient) return syncDirection == SyncDirection.ServerToClient || isOwned;
if (NetworkServer.active && NetworkClient.active)
return syncDirection == SyncDirection.ServerToClient || isOwned;
// server only: any ServerToClient
if (isServer) return syncDirection == SyncDirection.ServerToClient;
if (NetworkServer.active)
return syncDirection == SyncDirection.ServerToClient;
// client only: only ClientToServer and owned
if (isClient) return syncDirection == SyncDirection.ClientToServer && isOwned;
if (NetworkClient.active)
return syncDirection == SyncDirection.ClientToServer && isOwned;
// undefined behaviour should throw to make it very obvious
throw new Exception("InitSyncObject: IsWritable: neither isServer nor isClient are true.");
throw new Exception("InitSyncObject: IsWritable: neither NetworkServer nor NetworkClient are active.");
};
// when do we record changes:
@ -266,8 +274,10 @@ protected void InitSyncObject(SyncObject syncObject)
// client only: only ClientToServer and owned
if (isClient) return syncDirection == SyncDirection.ClientToServer && isOwned;
// undefined behaviour should throw to make it very obvious
throw new Exception("InitSyncObject: IsRecording: neither isServer nor isClient are true.");
// users may add to SyncLists before the object was spawned.
// isServer / isClient would still be false.
// in that case, allow modifying but don't record changes yet.
return false;
};
}

View File

@ -794,6 +794,9 @@ public void GetSyncVarNetworkIdentityOnClientNull()
[Test]
public void SerializeAndDeserializeObjectsAll()
{
NetworkServer.Listen(1);
ConnectHostClientBlockingAuthenticatedAndReady();
CreateNetworked(out GameObject _, out NetworkIdentity _, out NetworkBehaviourWithSyncVarsAndCollections comp);
comp.netIdentity.isServer = true;