Skip to content

Commit c3272ee

Browse files
committed
Merge branch 'feature/Mrb83/NetworkedVar' of https://github.com/Mrb83/MLAPI into feature/Mrb83/NetworkedVar
2 parents 31e337e + 8f476b5 commit c3272ee

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);
@@ -146,6 +165,7 @@ public void WriteDeltaToWriter(BitWriter writer)
146165
}
147166
}
148167

168+
/// <inheritdoc />
149169
public void WriteFieldToWriter(BitWriter writer)
150170
{
151171
writer.WriteUShort((ushort)dictionary.Count);
@@ -156,6 +176,7 @@ public void WriteFieldToWriter(BitWriter writer)
156176
}
157177
}
158178

179+
/// <inheritdoc />
159180
public bool CanClientWrite(uint clientId)
160181
{
161182
switch (Settings.WritePermission)
@@ -176,6 +197,7 @@ public bool CanClientWrite(uint clientId)
176197
return true;
177198
}
178199

200+
/// <inheritdoc />
179201
public bool CanClientRead(uint clientId)
180202
{
181203
switch (Settings.ReadPermission)
@@ -194,14 +216,18 @@ public bool CanClientRead(uint clientId)
194216
}
195217
return true;
196218
}
197-
219+
220+
/// <inheritdoc />
198221
public bool IsDirty()
199222
{
200-
return dirtyEvents.Count > 0;
223+
if (dirtyEvents.Count == 0) return false;
224+
if (Settings.SendOnChange) return true;
225+
if (NetworkingManager.singleton.NetworkTime - LastSyncedTime >= Settings.SendDelay) return true;
226+
return false;
201227
}
202228

203229

204-
230+
/// <inheritdoc />
205231
public TValue this[TKey key]
206232
{
207233
get
@@ -220,14 +246,19 @@ public TValue this[TKey key]
220246
}
221247
}
222248

249+
/// <inheritdoc />
223250
public ICollection<TKey> Keys => dictionary.Keys;
224251

252+
/// <inheritdoc />
225253
public ICollection<TValue> Values => dictionary.Values;
226254

255+
/// <inheritdoc />
227256
public int Count => dictionary.Count;
228257

258+
/// <inheritdoc />
229259
public bool IsReadOnly => dictionary.IsReadOnly;
230260

261+
/// <inheritdoc />
231262
public void Add(TKey key, TValue value)
232263
{
233264
dictionary.Add(key, value);
@@ -239,6 +270,7 @@ public void Add(TKey key, TValue value)
239270
});
240271
}
241272

273+
/// <inheritdoc />
242274
public void Add(KeyValuePair<TKey, TValue> item)
243275
{
244276
dictionary.Add(item);
@@ -250,6 +282,7 @@ public void Add(KeyValuePair<TKey, TValue> item)
250282
});
251283
}
252284

285+
/// <inheritdoc />
253286
public void Clear()
254287
{
255288
dictionary.Clear();
@@ -259,26 +292,31 @@ public void Clear()
259292
});
260293
}
261294

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

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

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

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

319+
/// <inheritdoc />
282320
public bool Remove(TKey key)
283321
{
284322
bool state = dictionary.Remove(key);
@@ -293,6 +331,7 @@ public bool Remove(TKey key)
293331
return state;
294332
}
295333

334+
/// <inheritdoc />
296335
public bool Remove(KeyValuePair<TKey, TValue> item)
297336
{
298337
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)