Skip to content

Commit efd3627

Browse files
committed
Merge master
2 parents dd2f48b + 336bed4 commit efd3627

File tree

2 files changed

+48
-23
lines changed

2 files changed

+48
-23
lines changed

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,21 @@ internal void NetworkedVarInit()
258258
channelMappedVarIndexes[firstLevelIndex[channel]].Add(i);
259259
}
260260
}
261-
261+
262+
private readonly List<int> networkedVarIndexesToReset = new List<int>();
263+
private readonly HashSet<int> networkedVarIndexesToResetSet = new HashSet<int>();
262264
internal void NetworkedVarUpdate()
263265
{
264266
if (!networkedVarInit)
265267
NetworkedVarInit();
266268
//TODO: Do this efficiently.
267269

270+
if (!CouldHaveDirtyVars())
271+
return;
272+
273+
networkedVarIndexesToReset.Clear();
274+
networkedVarIndexesToResetSet.Clear();
275+
268276
for (int i = 0; i < NetworkingManager.singleton.ConnectedClientsList.Count; i++)
269277
{
270278
//This iterates over every "channel group".
@@ -278,6 +286,7 @@ internal void NetworkedVarUpdate()
278286
writer.WriteUInt16Packed(networkedObject.GetOrderIndex(this));
279287

280288
uint clientId = NetworkingManager.singleton.ConnectedClientsList[i].ClientId;
289+
bool writtenAny = false;
281290
for (int k = 0; k < networkedVarFields.Count; k++)
282291
{
283292
if (!channelMappedVarIndexes[j].Contains(k))
@@ -291,19 +300,42 @@ internal void NetworkedVarUpdate()
291300
writer.WriteBool(isDirty);
292301
if (isDirty && (!isServer || networkedVarFields[k].CanClientRead(clientId)))
293302
{
303+
writtenAny = true;
294304
networkedVarFields[k].WriteDelta(stream);
295-
networkedVarFields[k].ResetDirty();
305+
if (!networkedVarIndexesToResetSet.Contains(k))
306+
{
307+
networkedVarIndexesToResetSet.Add(k);
308+
networkedVarIndexesToReset.Add(k);
309+
}
296310
}
297311
}
298-
299-
if (isServer)
300-
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_NETWORKED_VAR_DELTA, channelsForVarGroups[j], stream, new InternalSecuritySendOptions(false, false));
301-
else
302-
InternalMessageHandler.Send(NetworkingManager.singleton.ServerClientId, MLAPIConstants.MLAPI_NETWORKED_VAR_DELTA, channelsForVarGroups[j], stream, new InternalSecuritySendOptions(false, false));
312+
313+
if (writtenAny)
314+
{
315+
if (isServer)
316+
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_NETWORKED_VAR_DELTA, channelsForVarGroups[j], stream, new InternalSecuritySendOptions(false, false));
317+
else
318+
InternalMessageHandler.Send(NetworkingManager.singleton.ServerClientId, MLAPIConstants.MLAPI_NETWORKED_VAR_DELTA, channelsForVarGroups[j], stream, new InternalSecuritySendOptions(false, false));
319+
}
303320
}
304321
}
305322
}
306323
}
324+
325+
for (int i = 0; i < networkedVarIndexesToReset.Count; i++)
326+
{
327+
networkedVarFields[networkedVarIndexesToReset[i]].ResetDirty();
328+
}
329+
}
330+
331+
private bool CouldHaveDirtyVars()
332+
{
333+
for (int i = 0; i < networkedVarFields.Count; i++)
334+
{
335+
if (networkedVarFields[i].IsDirty())
336+
return true;
337+
}
338+
return false;
307339
}
308340

309341
internal void HandleNetworkedVarDeltas(Stream stream, uint clientId)

MLAPI/NetworkingManagerComponents/Binary/BitStream.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@ public BitStream(int capacity) : this(capacity, initialGrowthFactor) { }
4242
/// </summary>
4343
public BitStream() : this(initialCapacity, initialGrowthFactor) { }
4444

45-
/// <summary>
46-
/// A stream that supports writing data smaller than a single byte. This stream also has a built-in compression algorithm that can (optionally) be used to write compressed data.
47-
/// </summary>
48-
/// <param name="target">The buffer containing initial data</param>
49-
/// <param name="offset">The offset where the data begins</param>
50-
/// <param name="count">The amount of bytes to copy from the initial data buffer</param>
51-
public BitStream(byte[] target, int offset, int count) : this(count)
52-
{
53-
Buffer.BlockCopy(target, offset, this.target, 0, count);
54-
Resizable = false;
55-
}
56-
5745
/// <summary>
5846
/// A stream that supports writing data smaller than a single byte. This stream also has a built-in compression algorithm that can (optionally) be used to write compressed data.
5947
/// NOTE: when using a pre-allocated buffer, the stream will not grow!
@@ -78,9 +66,14 @@ public BitStream(byte[] target)
7866
public float GrowthFactor { set { _growthFactor = value <= 1 ? 1.5f : value; } get { return _growthFactor; } }
7967

8068
/// <summary>
81-
/// Whether or not data can be read from the stream.
69+
/// Whether or not stream supports reading. (Always true)
70+
/// </summary>
71+
public override bool CanRead => true;
72+
73+
/// <summary>
74+
/// Wheter or not or there is any data to be read from the stream.
8275
/// </summary>
83-
public override bool CanRead => Position < target.LongLength;
76+
public bool HasDataToRead => Position < Length;
8477

8578
/// <summary>
8679
/// Whether or not seeking is supported by this stream. (Always true)
@@ -167,14 +160,14 @@ private byte ReadByteMisaligned()
167160
/// Read a byte from the buffer. This takes into account possible byte misalignment.
168161
/// </summary>
169162
/// <returns>A byte from the buffer or, if a byte can't be read, -1.</returns>
170-
public override int ReadByte() => CanRead ? BitAligned ? ReadByteAligned() : ReadByteMisaligned() : -1;
163+
public override int ReadByte() => HasDataToRead ? BitAligned ? ReadByteAligned() : ReadByteMisaligned() : -1;
171164

172165
/// <summary>
173166
/// Peeks a byte without advancing the position
174167
/// </summary>
175168
/// <returns>The peeked byte</returns>
176169
public int PeekByte() =>
177-
CanRead ?
170+
HasDataToRead ?
178171
BitAligned ?
179172
target[Position] :
180173
(byte)((target[(int)Position] >> (int)(BitPosition & 7)) | (target[(int)(BitPosition + 8) >> 3] << (8 - (int)(BitPosition & 7)))) :

0 commit comments

Comments
 (0)