Skip to content

Commit e0cdfc5

Browse files
committed
Made Networked collections server authoritive
1 parent 6f2c9d2 commit e0cdfc5

File tree

5 files changed

+96
-111
lines changed

5 files changed

+96
-111
lines changed

MLAPI/Data/NetworkedCollections/NetworkedDictionary.cs

Lines changed: 45 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -225,35 +225,17 @@ public void ReadDelta(Stream stream, bool keepDirtyDelta)
225225
}
226226

227227
/// <inheritdoc />
228-
public void ReadField(Stream stream, bool keepDirtyState)
228+
public void ReadField(Stream stream)
229229
{
230230
using (PooledBitReader reader = PooledBitReader.Get(stream))
231231
{
232-
if (keepDirtyState)
233-
{
234-
dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>()
235-
{
236-
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Clear
237-
});
238-
}
239-
240232
dictionary.Clear();
241233
ushort entryCount = reader.ReadUInt16Packed();
242234
for (int i = 0; i < entryCount; i++)
243235
{
244236
TKey key = (TKey)reader.ReadObjectPacked(typeof(TKey));
245237
TValue value = (TValue)reader.ReadObjectPacked(typeof(TValue));
246238
dictionary.Add(key, value);
247-
248-
if (keepDirtyState)
249-
{
250-
dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>()
251-
{
252-
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Add,
253-
key = key,
254-
value = value
255-
});
256-
}
257239
}
258240
}
259241
}
@@ -391,7 +373,9 @@ public TValue this[TKey key]
391373
}
392374
set
393375
{
394-
dictionary[key] = value;
376+
if (NetworkingManager.singleton.isServer)
377+
dictionary[key] = value;
378+
395379
NetworkedDictionaryEvent<TKey, TValue> dictionaryEvent = new NetworkedDictionaryEvent<TKey, TValue>()
396380
{
397381
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Value,
@@ -400,7 +384,7 @@ public TValue this[TKey key]
400384
};
401385
dirtyEvents.Add(dictionaryEvent);
402386

403-
if (OnDictionaryChanged != null)
387+
if (NetworkingManager.singleton.isServer && OnDictionaryChanged != null)
404388
OnDictionaryChanged(dictionaryEvent);
405389
}
406390
}
@@ -420,7 +404,9 @@ public TValue this[TKey key]
420404
/// <inheritdoc />
421405
public void Add(TKey key, TValue value)
422406
{
423-
dictionary.Add(key, value);
407+
if (NetworkingManager.singleton.isServer)
408+
dictionary.Add(key, value);
409+
424410
NetworkedDictionaryEvent<TKey, TValue> dictionaryEvent = new NetworkedDictionaryEvent<TKey, TValue>()
425411
{
426412
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Add,
@@ -429,14 +415,16 @@ public void Add(TKey key, TValue value)
429415
};
430416
dirtyEvents.Add(dictionaryEvent);
431417

432-
if (OnDictionaryChanged != null)
418+
if (NetworkingManager.singleton.isServer && OnDictionaryChanged != null)
433419
OnDictionaryChanged(dictionaryEvent);
434420
}
435421

436422
/// <inheritdoc />
437423
public void Add(KeyValuePair<TKey, TValue> item)
438424
{
439-
dictionary.Add(item);
425+
if (NetworkingManager.singleton.isServer)
426+
dictionary.Add(item);
427+
440428
NetworkedDictionaryEvent<TKey, TValue> dictionaryEvent = new NetworkedDictionaryEvent<TKey, TValue>()
441429
{
442430
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Add,
@@ -445,21 +433,23 @@ public void Add(KeyValuePair<TKey, TValue> item)
445433
};
446434
dirtyEvents.Add(dictionaryEvent);
447435

448-
if (OnDictionaryChanged != null)
436+
if (NetworkingManager.singleton.isServer && OnDictionaryChanged != null)
449437
OnDictionaryChanged(dictionaryEvent);
450438
}
451439

452440
/// <inheritdoc />
453441
public void Clear()
454442
{
455-
dictionary.Clear();
443+
if (NetworkingManager.singleton.isServer)
444+
dictionary.Clear();
445+
456446
NetworkedDictionaryEvent<TKey, TValue> dictionaryEvent = new NetworkedDictionaryEvent<TKey, TValue>()
457447
{
458448
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Clear
459449
};
460450
dirtyEvents.Add(dictionaryEvent);
461451

462-
if (OnDictionaryChanged != null)
452+
if (NetworkingManager.singleton.isServer && OnDictionaryChanged != null)
463453
OnDictionaryChanged(dictionaryEvent);
464454
}
465455

@@ -490,43 +480,45 @@ public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
490480
/// <inheritdoc />
491481
public bool Remove(TKey key)
492482
{
483+
if (NetworkingManager.singleton.isServer)
484+
dictionary.Remove(key);
485+
493486
TValue value;
494487
dictionary.TryGetValue(key, out value);
495-
bool state = dictionary.Remove(key);
496-
if (state)
488+
489+
NetworkedDictionaryEvent<TKey, TValue> dictionaryEvent = new NetworkedDictionaryEvent<TKey, TValue>()
497490
{
498-
NetworkedDictionaryEvent<TKey, TValue> dictionaryEvent = new NetworkedDictionaryEvent<TKey, TValue>()
499-
{
500-
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Remove,
501-
key = key,
502-
value = value
503-
};
504-
dirtyEvents.Add(dictionaryEvent);
491+
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Remove,
492+
key = key,
493+
value = value
494+
};
495+
dirtyEvents.Add(dictionaryEvent);
496+
497+
if (NetworkingManager.singleton.isServer && OnDictionaryChanged != null)
498+
OnDictionaryChanged(dictionaryEvent);
505499

506-
if (OnDictionaryChanged != null)
507-
OnDictionaryChanged(dictionaryEvent);
508-
}
509-
return state;
500+
return true;
510501
}
502+
511503

512504
/// <inheritdoc />
513505
public bool Remove(KeyValuePair<TKey, TValue> item)
514506
{
515-
bool state = dictionary.Remove(item);
516-
if (state)
507+
if (NetworkingManager.singleton.isServer)
508+
dictionary.Remove(item);
509+
510+
NetworkedDictionaryEvent<TKey, TValue> dictionaryEvent = new NetworkedDictionaryEvent<TKey, TValue>()
517511
{
518-
NetworkedDictionaryEvent<TKey, TValue> dictionaryEvent = new NetworkedDictionaryEvent<TKey, TValue>()
519-
{
520-
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.RemovePair,
521-
key = item.Key,
522-
value = item.Value
523-
};
524-
dirtyEvents.Add(dictionaryEvent);
512+
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.RemovePair,
513+
key = item.Key,
514+
value = item.Value
515+
};
516+
dirtyEvents.Add(dictionaryEvent);
525517

526-
if (OnDictionaryChanged != null)
527-
OnDictionaryChanged(dictionaryEvent);
528-
}
529-
return state;
518+
if (NetworkingManager.singleton.isServer && OnDictionaryChanged != null)
519+
OnDictionaryChanged(dictionaryEvent);
520+
521+
return true;
530522
}
531523

532524
IEnumerator IEnumerable.GetEnumerator()

MLAPI/Data/NetworkedCollections/NetworkedList.cs

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -197,33 +197,15 @@ public void WriteField(Stream stream)
197197
}
198198

199199
/// <inheritdoc />
200-
public void ReadField(Stream stream, bool keepDirtyState)
200+
public void ReadField(Stream stream)
201201
{
202202
using (PooledBitReader reader = PooledBitReader.Get(stream))
203203
{
204-
if (keepDirtyState)
205-
{
206-
dirtyEvents.Add(new NetworkedListEvent<T>()
207-
{
208-
eventType = NetworkedListEvent<T>.EventType.Clear
209-
});
210-
}
211-
212204
list.Clear();
213205
ushort count = reader.ReadUInt16Packed();
214206
for (int i = 0; i < count; i++)
215207
{
216208
list.Add((T)reader.ReadObjectPacked(typeof(T))); //BOX
217-
218-
if (keepDirtyState)
219-
{
220-
dirtyEvents.Add(new NetworkedListEvent<T>()
221-
{
222-
eventType = NetworkedListEvent<T>.EventType.Add,
223-
index = i,
224-
value = list[i]
225-
});
226-
}
227209
}
228210
}
229211
}
@@ -400,7 +382,9 @@ IEnumerator IEnumerable.GetEnumerator()
400382
/// <inheritdoc />
401383
public void Add(T item)
402384
{
403-
list.Add(item);
385+
if (NetworkingManager.singleton.isServer)
386+
list.Add(item);
387+
404388
NetworkedListEvent<T> listEvent = new NetworkedListEvent<T>()
405389
{
406390
eventType = NetworkedListEvent<T>.EventType.Add,
@@ -409,21 +393,23 @@ public void Add(T item)
409393
};
410394
dirtyEvents.Add(listEvent);
411395

412-
if (OnListChanged != null)
396+
if (NetworkingManager.singleton.isServer && OnListChanged != null)
413397
OnListChanged(listEvent);
414398
}
415399

416400
/// <inheritdoc />
417401
public void Clear()
418402
{
419-
list.Clear();
403+
if (NetworkingManager.singleton.isServer)
404+
list.Clear();
405+
420406
NetworkedListEvent<T> listEvent = new NetworkedListEvent<T>()
421407
{
422408
eventType = NetworkedListEvent<T>.EventType.Clear
423409
};
424410
dirtyEvents.Add(listEvent);
425411

426-
if (OnListChanged != null)
412+
if (NetworkingManager.singleton.isServer && OnListChanged != null)
427413
OnListChanged(listEvent);
428414
}
429415

@@ -442,20 +428,20 @@ public void CopyTo(T[] array, int arrayIndex)
442428
/// <inheritdoc />
443429
public bool Remove(T item)
444430
{
445-
bool state = list.Remove(item);
446-
if (state)
431+
if (NetworkingManager.singleton.isServer)
432+
list.Remove(item);
433+
434+
NetworkedListEvent<T> listEvent = new NetworkedListEvent<T>()
447435
{
448-
NetworkedListEvent<T> listEvent = new NetworkedListEvent<T>()
449-
{
450-
eventType = NetworkedListEvent<T>.EventType.Remove,
451-
value = item
452-
};
453-
dirtyEvents.Add(listEvent);
436+
eventType = NetworkedListEvent<T>.EventType.Remove,
437+
value = item
438+
};
439+
dirtyEvents.Add(listEvent);
454440

455-
if (OnListChanged != null)
456-
OnListChanged(listEvent);
457-
}
458-
return state;
441+
if (NetworkingManager.singleton.isServer && OnListChanged != null)
442+
OnListChanged(listEvent);
443+
444+
return true;
459445
}
460446

461447
/// <inheritdoc />
@@ -473,7 +459,9 @@ public int IndexOf(T item)
473459
/// <inheritdoc />
474460
public void Insert(int index, T item)
475461
{
476-
list.Insert(index, item);
462+
if (NetworkingManager.singleton.isServer)
463+
list.Insert(index, item);
464+
477465
NetworkedListEvent<T> listEvent = new NetworkedListEvent<T>()
478466
{
479467
eventType = NetworkedListEvent<T>.EventType.Insert,
@@ -482,24 +470,24 @@ public void Insert(int index, T item)
482470
};
483471
dirtyEvents.Add(listEvent);
484472

485-
if (OnListChanged != null)
473+
if (NetworkingManager.singleton.isServer && OnListChanged != null)
486474
OnListChanged(listEvent);
487475
}
488476

489477
/// <inheritdoc />
490478
public void RemoveAt(int index)
491479
{
492-
T value = list[index];
493-
list.RemoveAt(index);
480+
if (NetworkingManager.singleton.isServer)
481+
list.RemoveAt(index);
482+
494483
NetworkedListEvent<T> listEvent = new NetworkedListEvent<T>()
495484
{
496485
eventType = NetworkedListEvent<T>.EventType.RemoveAt,
497-
index = index,
498-
value = value
486+
index = index
499487
};
500488
dirtyEvents.Add(listEvent);
501489

502-
if (OnListChanged != null)
490+
if (NetworkingManager.singleton.isServer && OnListChanged != null)
503491
OnListChanged(listEvent);
504492
}
505493

@@ -513,7 +501,9 @@ public T this[int index]
513501
}
514502
set
515503
{
516-
list[index] = value;
504+
if (NetworkingManager.singleton.isServer)
505+
list[index] = value;
506+
517507
NetworkedListEvent<T> listEvent = new NetworkedListEvent<T>()
518508
{
519509
eventType = NetworkedListEvent<T>.EventType.Value,
@@ -522,7 +512,7 @@ public T this[int index]
522512
};
523513
dirtyEvents.Add(listEvent);
524514

525-
if (OnListChanged != null)
515+
if (NetworkingManager.singleton.isServer && OnListChanged != null)
526516
OnListChanged(listEvent);
527517
}
528518
}

MLAPI/Data/NetworkedVar.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,28 +166,31 @@ public bool CanClientWrite(uint clientId)
166166
/// Reads value from the reader and applies it
167167
/// </summary>
168168
/// <param name="stream">The stream to read the value from</param>
169-
public void ReadDelta(Stream stream, bool keepDirtyDelta) => ReadField(stream, keepDirtyDelta); //The NetworkedVar is built for simple data types and has no delta.
170-
171-
/// <inheritdoc />
172-
public void SetNetworkedBehaviour(NetworkedBehaviour behaviour)
173-
{
174-
networkedBehaviour = behaviour;
175-
}
176-
177-
/// <inheritdoc />
178-
public void ReadField(Stream stream, bool keepDirtyState)
169+
public void ReadDelta(Stream stream, bool keepDirtyDelta)
179170
{
180171
using (PooledBitReader reader = PooledBitReader.Get(stream))
181172
{
182173
T previousValue = InternalValue;
183174
InternalValue = (T)reader.ReadObjectPacked((typeof(T)));
184175

185-
if (keepDirtyState) isDirty = true;
176+
if (keepDirtyDelta) isDirty = true;
186177

187178
if (OnValueChanged != null)
188179
OnValueChanged(previousValue, InternalValue);
189180
}
190181
}
182+
183+
/// <inheritdoc />
184+
public void SetNetworkedBehaviour(NetworkedBehaviour behaviour)
185+
{
186+
networkedBehaviour = behaviour;
187+
}
188+
189+
/// <inheritdoc />
190+
public void ReadField(Stream stream)
191+
{
192+
ReadDelta(stream, false);
193+
}
191194

192195
/// <inheritdoc />
193196
public void WriteField(Stream stream)

0 commit comments

Comments
 (0)