Skip to content

Commit 8f476b5

Browse files
committed
Added XML comments and minor NetworkedVar changes
1 parent 8dd3842 commit 8f476b5

File tree

4 files changed

+215
-11
lines changed

4 files changed

+215
-11
lines changed

MLAPI/Data/NetworkedCollections/NetworkedDictionary.cs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
namespace MLAPI.Data.NetworkedCollections
99
{
10+
/// <summary>
11+
/// Event based networkedVar container for syncing Lists
12+
/// </summary>
13+
/// <typeparam name="TKey">The type for the dictionary keys</typeparam>
14+
/// <typeparam name="TValue">The type for the dctionary values</typeparam>
1015
public class NetworkedDictionary<TKey, TValue> : IDictionary<TKey, TValue>, INetworkedVar
1116
{
1217
internal struct NetworkedDictionaryEvent<TKey, TValue>
@@ -25,21 +30,31 @@ internal enum NetworkedListEventType
2530
internal TValue value;
2631
}
2732

28-
public NetworkedVarSettings Settings = new NetworkedVarSettings();
33+
/// <summary>
34+
/// Gets the last time the variable was synced
35+
/// </summary>
36+
public float LastSyncedTime { get; internal set; }
37+
/// <summary>
38+
/// The settings for this container
39+
/// </summary>
40+
public readonly NetworkedVarSettings Settings = new NetworkedVarSettings();
2941
private readonly IDictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>();
3042
private NetworkedBehaviour networkedBehaviour;
3143
private readonly List<NetworkedDictionaryEvent<TKey, TValue>> dirtyEvents = new List<NetworkedDictionaryEvent<TKey, TValue>>();
3244

45+
/// <inheritdoc />
3346
public void ResetDirty()
3447
{
3548
dirtyEvents.Clear();
3649
}
3750

51+
/// <inheritdoc />
3852
public string GetChannel()
3953
{
4054
return Settings.SendChannel;
4155
}
4256

57+
/// <inheritdoc />
4358
public void SetDeltaFromReader(BitReader reader)
4459
{
4560
ushort deltaCount = reader.ReadUShort();
@@ -83,6 +98,7 @@ public void SetDeltaFromReader(BitReader reader)
8398
}
8499
}
85100

101+
/// <inheritdoc />
86102
public void SetFieldFromReader(BitReader reader)
87103
{
88104
ushort entryCount = reader.ReadUShort();
@@ -94,16 +110,19 @@ public void SetFieldFromReader(BitReader reader)
94110
}
95111
}
96112

113+
/// <inheritdoc />
97114
public void SetNetworkedBehaviour(NetworkedBehaviour behaviour)
98115
{
99116
networkedBehaviour = behaviour;
100117
}
101118

119+
/// <inheritdoc />
102120
public bool TryGetValue(TKey key, out TValue value)
103121
{
104122
return dictionary.TryGetValue(key, out value);
105123
}
106124

125+
/// <inheritdoc />
107126
public void WriteDeltaToWriter(BitWriter writer)
108127
{
109128
writer.WriteUShort((ushort)dirtyEvents.Count);
@@ -147,6 +166,7 @@ public void WriteDeltaToWriter(BitWriter writer)
147166
}
148167
}
149168

169+
/// <inheritdoc />
150170
public void WriteFieldToWriter(BitWriter writer)
151171
{
152172
writer.WriteUShort((ushort)dictionary.Count);
@@ -157,6 +177,7 @@ public void WriteFieldToWriter(BitWriter writer)
157177
}
158178
}
159179

180+
/// <inheritdoc />
160181
public bool CanClientWrite(uint clientId)
161182
{
162183
switch (Settings.WritePermission)
@@ -177,6 +198,7 @@ public bool CanClientWrite(uint clientId)
177198
return true;
178199
}
179200

201+
/// <inheritdoc />
180202
public bool CanClientRead(uint clientId)
181203
{
182204
switch (Settings.ReadPermission)
@@ -195,14 +217,18 @@ public bool CanClientRead(uint clientId)
195217
}
196218
return true;
197219
}
198-
220+
221+
/// <inheritdoc />
199222
public bool IsDirty()
200223
{
201-
return dirtyEvents.Count > 0;
224+
if (dirtyEvents.Count == 0) return false;
225+
if (Settings.SendOnChange) return true;
226+
if (NetworkingManager.singleton.NetworkTime - LastSyncedTime >= Settings.SendDelay) return true;
227+
return false;
202228
}
203229

204230

205-
231+
/// <inheritdoc />
206232
public TValue this[TKey key]
207233
{
208234
get
@@ -221,14 +247,19 @@ public TValue this[TKey key]
221247
}
222248
}
223249

250+
/// <inheritdoc />
224251
public ICollection<TKey> Keys => dictionary.Keys;
225252

253+
/// <inheritdoc />
226254
public ICollection<TValue> Values => dictionary.Values;
227255

256+
/// <inheritdoc />
228257
public int Count => dictionary.Count;
229258

259+
/// <inheritdoc />
230260
public bool IsReadOnly => dictionary.IsReadOnly;
231261

262+
/// <inheritdoc />
232263
public void Add(TKey key, TValue value)
233264
{
234265
dictionary.Add(key, value);
@@ -240,6 +271,7 @@ public void Add(TKey key, TValue value)
240271
});
241272
}
242273

274+
/// <inheritdoc />
243275
public void Add(KeyValuePair<TKey, TValue> item)
244276
{
245277
dictionary.Add(item);
@@ -251,6 +283,7 @@ public void Add(KeyValuePair<TKey, TValue> item)
251283
});
252284
}
253285

286+
/// <inheritdoc />
254287
public void Clear()
255288
{
256289
dictionary.Clear();
@@ -260,26 +293,31 @@ public void Clear()
260293
});
261294
}
262295

296+
/// <inheritdoc />
263297
public bool Contains(KeyValuePair<TKey, TValue> item)
264298
{
265299
return dictionary.Contains(item);
266300
}
267301

302+
/// <inheritdoc />
268303
public bool ContainsKey(TKey key)
269304
{
270305
return dictionary.ContainsKey(key);
271306
}
272307

308+
/// <inheritdoc />
273309
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
274310
{
275311
dictionary.CopyTo(array, arrayIndex);
276312
}
277313

314+
/// <inheritdoc />
278315
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
279316
{
280317
return dictionary.GetEnumerator();
281318
}
282319

320+
/// <inheritdoc />
283321
public bool Remove(TKey key)
284322
{
285323
bool state = dictionary.Remove(key);
@@ -294,6 +332,7 @@ public bool Remove(TKey key)
294332
return state;
295333
}
296334

335+
/// <inheritdoc />
297336
public bool Remove(KeyValuePair<TKey, TValue> item)
298337
{
299338
bool state = dictionary.Remove(item);

MLAPI/Data/NetworkedCollections/NetworkedList.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
namespace MLAPI.Data.NetworkedCollections
88
{
9+
/// <summary>
10+
/// Event based networkedVar container for syncing Lists
11+
/// </summary>
12+
/// <typeparam name="T">The type for the list</typeparam>
913
public class NetworkedList<T> : IList<T>, INetworkedVar
1014
{
1115
internal struct NetworkedListEvent<T>
@@ -28,23 +32,37 @@ internal enum NetworkedListEventType
2832
private readonly IList<T> list = new List<T>();
2933
private List<NetworkedListEvent<T>> dirtyEvents = new List<NetworkedListEvent<T>>();
3034
private NetworkedBehaviour networkedBehaviour;
31-
public NetworkedVarSettings Settings = new NetworkedVarSettings();
35+
/// <summary>
36+
/// Gets the last time the variable was synced
37+
/// </summary>
38+
public float LastSyncedTime { get; internal set; }
39+
/// <summary>
40+
/// The settings for this container
41+
/// </summary>
42+
public readonly NetworkedVarSettings Settings = new NetworkedVarSettings();
3243

44+
/// <inheritdoc />
3345
public void ResetDirty()
3446
{
3547
dirtyEvents.Clear();
3648
}
3749

50+
/// <inheritdoc />
3851
public bool IsDirty()
3952
{
40-
return dirtyEvents.Count > 0;
53+
if (dirtyEvents.Count == 0) return false;
54+
if (Settings.SendOnChange) return true;
55+
if (NetworkingManager.singleton.NetworkTime - LastSyncedTime >= Settings.SendDelay) return true;
56+
return false;
4157
}
4258

59+
/// <inheritdoc />
4360
public string GetChannel()
4461
{
4562
return Settings.SendChannel;
4663
}
4764

65+
/// <inheritdoc />
4866
public bool CanClientWrite(uint clientId)
4967
{
5068
switch (Settings.WritePermission)
@@ -65,6 +83,7 @@ public bool CanClientWrite(uint clientId)
6583
return true;
6684
}
6785

86+
/// <inheritdoc />
6887
public bool CanClientRead(uint clientId)
6988
{
7089
switch (Settings.ReadPermission)
@@ -84,6 +103,7 @@ public bool CanClientRead(uint clientId)
84103
return true;
85104
}
86105

106+
/// <inheritdoc />
87107
public void WriteDeltaToWriter(BitWriter writer)
88108
{
89109
writer.WriteUShort((ushort)dirtyEvents.Count);
@@ -130,6 +150,7 @@ public void WriteDeltaToWriter(BitWriter writer)
130150
}
131151
}
132152

153+
/// <inheritdoc />
133154
public void WriteFieldToWriter(BitWriter writer)
134155
{
135156
writer.WriteUShort((ushort)list.Count);
@@ -139,6 +160,7 @@ public void WriteFieldToWriter(BitWriter writer)
139160
}
140161
}
141162

163+
/// <inheritdoc />
142164
public void SetFieldFromReader(BitReader reader)
143165
{
144166
ushort count = reader.ReadUShort();
@@ -148,6 +170,7 @@ public void SetFieldFromReader(BitReader reader)
148170
}
149171
}
150172

173+
/// <inheritdoc />
151174
public void SetDeltaFromReader(BitReader reader)
152175
{
153176
ushort deltaCount = reader.ReadUShort();
@@ -194,11 +217,13 @@ public void SetDeltaFromReader(BitReader reader)
194217
}
195218
}
196219

220+
/// <inheritdoc />
197221
public void SetNetworkedBehaviour(NetworkedBehaviour behaviour)
198222
{
199223
networkedBehaviour = behaviour;
200224
}
201225

226+
/// <inheritdoc />
202227
public IEnumerator<T> GetEnumerator()
203228
{
204229
return list.GetEnumerator();
@@ -209,6 +234,7 @@ IEnumerator IEnumerable.GetEnumerator()
209234
return ((IEnumerable) list).GetEnumerator();
210235
}
211236

237+
/// <inheritdoc />
212238
public void Add(T item)
213239
{
214240
list.Add(item);
@@ -219,6 +245,7 @@ public void Add(T item)
219245
});
220246
}
221247

248+
/// <inheritdoc />
222249
public void Clear()
223250
{
224251
list.Clear();
@@ -228,16 +255,19 @@ public void Clear()
228255
});
229256
}
230257

258+
/// <inheritdoc />
231259
public bool Contains(T item)
232260
{
233261
return list.Contains(item);
234262
}
235263

264+
/// <inheritdoc />
236265
public void CopyTo(T[] array, int arrayIndex)
237266
{
238267
list.CopyTo(array, arrayIndex);
239268
}
240269

270+
/// <inheritdoc />
241271
public bool Remove(T item)
242272
{
243273
bool state = list.Remove(item);
@@ -252,15 +282,19 @@ public bool Remove(T item)
252282
return state;
253283
}
254284

285+
/// <inheritdoc />
255286
public int Count => list.Count;
256287

288+
/// <inheritdoc />
257289
public bool IsReadOnly => list.IsReadOnly;
258290

291+
/// <inheritdoc />
259292
public int IndexOf(T item)
260293
{
261294
return list.IndexOf(item);
262295
}
263296

297+
/// <inheritdoc />
264298
public void Insert(int index, T item)
265299
{
266300
list.Insert(index, item);
@@ -272,6 +306,7 @@ public void Insert(int index, T item)
272306
});
273307
}
274308

309+
/// <inheritdoc />
275310
public void RemoveAt(int index)
276311
{
277312
list.RemoveAt(index);
@@ -282,6 +317,7 @@ public void RemoveAt(int index)
282317
});
283318
}
284319

320+
/// <inheritdoc />
285321
public T this[int index]
286322
{
287323
get

0 commit comments

Comments
 (0)