NetworkIdentity.OnSerializeSafely: remove unnecessary ownerWritten, observersWritten parameters

This commit is contained in:
vis2k 2021-08-08 13:59:09 +08:00
parent 6ce8dfcc23
commit e28d7e903a
5 changed files with 13 additions and 25 deletions

View File

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

View File

@ -893,7 +893,7 @@ static ArraySegment<byte> CreateSpawnMessagePayload(bool isOwner, NetworkIdentit
// serialize all components with initialState = true // serialize all components with initialState = true
// (can be null if has none) // (can be null if has none)
identity.OnSerializeAllSafely(true, ownerWriter, out bool _, observersWriter, out bool _); identity.OnSerializeAllSafely(true, ownerWriter, observersWriter);
// convert to ArraySegment to avoid reader allocations // convert to ArraySegment to avoid reader allocations
// if nothing was written, .ToArraySegment returns an empty segment. // if nothing was written, .ToArraySegment returns an empty segment.
@ -1490,14 +1490,14 @@ static NetworkWriter GetEntitySerializationForConnection(NetworkIdentity identit
if (owned) if (owned)
{ {
// was it dirty / did we actually serialize anything? // was it dirty / did we actually serialize anything?
if (serialization.ownerWritten) if (serialization.ownerWriter.Position > 0)
return serialization.ownerWriter; return serialization.ownerWriter;
} }
// observers writer if not owned // observers writer if not owned
else else
{ {
// was it dirty / did we actually serialize anything? // was it dirty / did we actually serialize anything?
if (serialization.observersWritten) if (serialization.observersWriter.Position > 0)
return serialization.observersWriter; return serialization.observersWriter;
} }

View File

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

View File

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

View File

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