NetworkIdentity.OnSerializeSafely simplified: owner/observers 'written' changed from int to bool

This commit is contained in:
vis2k 2021-08-08 13:23:39 +08:00
parent adc7687807
commit a76fa59fee
5 changed files with 24 additions and 24 deletions

View File

@ -29,8 +29,8 @@ public struct NetworkIdentitySerialization
public NetworkWriter ownerWriter;
public NetworkWriter observersWriter;
// TODO there is probably a more simple way later
public int ownerWritten;
public int observersWritten;
public bool ownerWritten;
public bool observersWritten;
}
/// <summary>NetworkIdentity identifies objects across the network.</summary>
@ -871,10 +871,10 @@ bool OnSerializeSafely(NetworkBehaviour comp, NetworkWriter writer, bool initial
// check ownerWritten/observersWritten to know if anything was written
// We pass dirtyComponentsMask into this function so that we can check
// if any Components are dirty before creating writers
internal void OnSerializeAllSafely(bool initialState, NetworkWriter ownerWriter, out int ownerWritten, NetworkWriter observersWriter, out int observersWritten)
internal void OnSerializeAllSafely(bool initialState, NetworkWriter ownerWriter, out bool ownerWritten, NetworkWriter observersWriter, out bool observersWritten)
{
// clear 'written' variables
ownerWritten = observersWritten = 0;
ownerWritten = observersWritten = false;
// check if components are in byte.MaxRange just to be 100% sure
// that we avoid overflows
@ -903,7 +903,7 @@ internal void OnSerializeAllSafely(bool initialState, NetworkWriter ownerWriter,
// serialize into ownerWriter first
// (owner always gets everything!)
OnSerializeSafely(comp, ownerWriter, initialState);
++ownerWritten;
ownerWritten = true;
// copy into observersWriter too if SyncMode.Observers
// -> we copy instead of calling OnSerialize again because
@ -919,7 +919,7 @@ internal void OnSerializeAllSafely(bool initialState, NetworkWriter ownerWriter,
ArraySegment<byte> segment = ownerWriter.ToArraySegment();
int length = ownerWriter.Position - startPosition;
observersWriter.WriteBytes(segment.Array, startPosition, length);
++observersWritten;
observersWritten = true;
}
}
}

View File

@ -893,12 +893,12 @@ static ArraySegment<byte> CreateSpawnMessagePayload(bool isOwner, NetworkIdentit
// serialize all components with initialState = true
// (can be null if has none)
identity.OnSerializeAllSafely(true, ownerWriter, out int ownerWritten, observersWriter, out int observersWritten);
identity.OnSerializeAllSafely(true, ownerWriter, out bool ownerWritten, observersWriter, out bool observersWritten);
// convert to ArraySegment to avoid reader allocations
// (need to handle null case too)
ArraySegment<byte> ownerSegment = ownerWritten > 0 ? ownerWriter.ToArraySegment() : default;
ArraySegment<byte> observersSegment = observersWritten > 0 ? observersWriter.ToArraySegment() : default;
ArraySegment<byte> ownerSegment = ownerWritten ? ownerWriter.ToArraySegment() : default;
ArraySegment<byte> observersSegment = observersWritten ? observersWriter.ToArraySegment() : default;
// use owner segment if 'conn' owns this identity, otherwise
// use observers segment
@ -1490,14 +1490,14 @@ static NetworkWriter GetEntitySerializationForConnection(NetworkIdentity identit
if (owned)
{
// was it dirty / did we actually serialize anything?
if (serialization.ownerWritten > 0)
if (serialization.ownerWritten)
return serialization.ownerWriter;
}
// observers writer if not owned
else
{
// was it dirty / did we actually serialize anything?
if (serialization.observersWritten > 0)
if (serialization.observersWritten)
return serialization.observersWriter;
}

View File

@ -539,7 +539,7 @@ public void ApplyPayload_SendsDataToNetworkBehaviourDeserialize()
NetworkWriter ownerWriter = new NetworkWriter();
NetworkWriter observersWriter = new NetworkWriter();
serverIdentity.OnSerializeAllSafely(true, ownerWriter, out int ownerWritten, observersWriter, out int observersWritten);
serverIdentity.OnSerializeAllSafely(true, ownerWriter, out bool _, observersWriter, out bool _);
// check that Serialize was called
Assert.That(onSerializeCalled, Is.EqualTo(1));

View File

@ -758,14 +758,14 @@ public void OnSerializeAndDeserializeAllSafely()
NetworkWriter observersWriter = new NetworkWriter();
// error log because of the exception is expected
LogAssert.ignoreFailingMessages = true;
identity.OnSerializeAllSafely(true, ownerWriter, out int ownerWritten, observersWriter, out int observersWritten);
identity.OnSerializeAllSafely(true, ownerWriter, out bool ownerWritten, observersWriter, out bool observersWritten);
LogAssert.ignoreFailingMessages = false;
// owner should have written all components
Assert.That(ownerWritten, Is.EqualTo(3));
// owner should have written something
Assert.That(ownerWritten, Is.EqualTo(true));
// observers should have written only the observers components
Assert.That(observersWritten, Is.EqualTo(2));
// observers should have written something
Assert.That(observersWritten, Is.EqualTo(true));
// reset component values
comp1.value = 0;
@ -824,13 +824,13 @@ public void OnSerializeAllSafelyShouldNotLogErrorsForTooManyComponents()
NetworkWriter ownerWriter = new NetworkWriter();
NetworkWriter observersWriter = new NetworkWriter();
identity.OnSerializeAllSafely(true, ownerWriter, out int ownerWritten, observersWriter, out int observersWritten);
identity.OnSerializeAllSafely(true, ownerWriter, out bool ownerWritten, observersWriter, out bool observersWritten);
// Should still write with too mnay Components because NetworkBehavioursCache should handle the error
// Should still write with too mnany Components because NetworkBehavioursCache should handle the error
Assert.That(ownerWriter.Position, Is.GreaterThan(0));
Assert.That(observersWriter.Position, Is.GreaterThan(0));
Assert.That(ownerWritten, Is.GreaterThan(0));
Assert.That(observersWritten, Is.GreaterThan(0));
Assert.That(ownerWritten, Is.True);
Assert.That(observersWritten, Is.True);
}
[Test]
@ -874,7 +874,7 @@ public void OnDeserializeSafelyShouldDetectAndHandleDeSerializationMismatch()
// serialize
NetworkWriter ownerWriter = new NetworkWriter();
NetworkWriter observersWriter = new NetworkWriter();
identity.OnSerializeAllSafely(true, ownerWriter, out int ownerWritten, observersWriter, out int observersWritten);
identity.OnSerializeAllSafely(true, ownerWriter, out bool _, observersWriter, out bool _);
// reset component values
comp1.value = 0;

View File

@ -154,7 +154,7 @@ public void TestSynchronizingObjects()
NetworkWriter ownerWriter = new NetworkWriter();
// not really used in this Test
NetworkWriter observersWriter = new NetworkWriter();
identity1.OnSerializeAllSafely(true, ownerWriter, out int ownerWritten, observersWriter, out int observersWritten);
identity1.OnSerializeAllSafely(true, ownerWriter, out bool _, observersWriter, out bool _);
// set up a "client" object
CreateNetworked(out GameObject gameObject2, out NetworkIdentity identity2, out MockPlayer player2);
@ -417,7 +417,7 @@ public void TestSyncingAbstractNetworkBehaviour()
NetworkWriter ownerWriter = new NetworkWriter();
// not really used in this Test
NetworkWriter observersWriter = new NetworkWriter();
serverIdentity.OnSerializeAllSafely(true, ownerWriter, out int ownerWritten, observersWriter, out int observersWritten);
serverIdentity.OnSerializeAllSafely(true, ownerWriter, out bool _, observersWriter, out bool _);
// set up a "client" object
CreateNetworked(out _, out NetworkIdentity clientIdentity, out SyncVarAbstractNetworkBehaviour clientBehaviour);