Skip to content

Commit 6f2c9d2

Browse files
committed
Fixed client to server networked var syncing
1 parent e592899 commit 6f2c9d2

File tree

5 files changed

+173
-22
lines changed

5 files changed

+173
-22
lines changed

MLAPI/Data/NetworkedCollections/NetworkedDictionary.cs

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public string GetChannel()
8686
}
8787

8888
/// <inheritdoc />
89-
public void ReadDelta(Stream stream)
89+
public void ReadDelta(Stream stream, bool keepDirtyDelta)
9090
{
9191
using (PooledBitReader reader = PooledBitReader.Get(stream))
9292
{
@@ -103,11 +103,24 @@ public void ReadDelta(Stream stream)
103103
dictionary.Add(key, value);
104104

105105
if (OnDictionaryChanged != null)
106-
OnDictionaryChanged(new NetworkedDictionaryEvent<TKey, TValue> {
106+
{
107+
OnDictionaryChanged(new NetworkedDictionaryEvent<TKey, TValue>
108+
{
109+
eventType = eventType,
110+
key = key,
111+
value = value
112+
});
113+
}
114+
115+
if (keepDirtyDelta)
116+
{
117+
dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>()
118+
{
107119
eventType = eventType,
108120
key = key,
109121
value = value
110122
});
123+
}
111124
}
112125
break;
113126
case NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Remove:
@@ -118,11 +131,24 @@ public void ReadDelta(Stream stream)
118131
dictionary.Remove(key);
119132

120133
if (OnDictionaryChanged != null)
121-
OnDictionaryChanged(new NetworkedDictionaryEvent<TKey, TValue> {
134+
{
135+
OnDictionaryChanged(new NetworkedDictionaryEvent<TKey, TValue>
136+
{
137+
eventType = eventType,
138+
key = key,
139+
value = value
140+
});
141+
}
142+
143+
if (keepDirtyDelta)
144+
{
145+
dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>()
146+
{
122147
eventType = eventType,
123148
key = key,
124149
value = value
125150
});
151+
}
126152
}
127153
break;
128154
case NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.RemovePair:
@@ -132,11 +158,24 @@ public void ReadDelta(Stream stream)
132158
dictionary.Remove(new KeyValuePair<TKey, TValue>(key, value));
133159

134160
if (OnDictionaryChanged != null)
135-
OnDictionaryChanged(new NetworkedDictionaryEvent<TKey, TValue> {
161+
{
162+
OnDictionaryChanged(new NetworkedDictionaryEvent<TKey, TValue>
163+
{
136164
eventType = eventType,
137165
key = key,
138166
value = value
139167
});
168+
}
169+
170+
if (keepDirtyDelta)
171+
{
172+
dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>()
173+
{
174+
eventType = eventType,
175+
key = key,
176+
value = value
177+
});
178+
}
140179
}
141180
break;
142181
case NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Clear:
@@ -158,11 +197,24 @@ public void ReadDelta(Stream stream)
158197
dictionary[key] = value;
159198

160199
if (OnDictionaryChanged != null)
161-
OnDictionaryChanged(new NetworkedDictionaryEvent<TKey, TValue> {
200+
{
201+
OnDictionaryChanged(new NetworkedDictionaryEvent<TKey, TValue>
202+
{
203+
eventType = eventType,
204+
key = key,
205+
value = value
206+
});
207+
}
208+
209+
if (keepDirtyDelta)
210+
{
211+
dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>()
212+
{
162213
eventType = eventType,
163214
key = key,
164215
value = value
165216
});
217+
}
166218
}
167219
break;
168220
default:
@@ -173,17 +225,35 @@ public void ReadDelta(Stream stream)
173225
}
174226

175227
/// <inheritdoc />
176-
public void ReadField(Stream stream)
228+
public void ReadField(Stream stream, bool keepDirtyState)
177229
{
178230
using (PooledBitReader reader = PooledBitReader.Get(stream))
179231
{
232+
if (keepDirtyState)
233+
{
234+
dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>()
235+
{
236+
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Clear
237+
});
238+
}
239+
180240
dictionary.Clear();
181241
ushort entryCount = reader.ReadUInt16Packed();
182242
for (int i = 0; i < entryCount; i++)
183243
{
184244
TKey key = (TKey)reader.ReadObjectPacked(typeof(TKey));
185245
TValue value = (TValue)reader.ReadObjectPacked(typeof(TValue));
186246
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+
}
187257
}
188258
}
189259
}

MLAPI/Data/NetworkedCollections/NetworkedList.cs

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,21 +197,39 @@ public void WriteField(Stream stream)
197197
}
198198

199199
/// <inheritdoc />
200-
public void ReadField(Stream stream)
200+
public void ReadField(Stream stream, bool keepDirtyState)
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+
204212
list.Clear();
205213
ushort count = reader.ReadUInt16Packed();
206214
for (int i = 0; i < count; i++)
207215
{
208216
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+
}
209227
}
210228
}
211229
}
212230

213231
/// <inheritdoc />
214-
public void ReadDelta(Stream stream)
232+
public void ReadDelta(Stream stream, bool keepDirtyDelta)
215233
{
216234
using (PooledBitReader reader = PooledBitReader.Get(stream))
217235
{
@@ -226,12 +244,24 @@ public void ReadDelta(Stream stream)
226244
list.Add((T)reader.ReadObjectPacked(typeof(T))); //BOX
227245

228246
if (OnListChanged != null)
229-
OnListChanged(new NetworkedListEvent<T>
247+
{
248+
OnListChanged(new NetworkedListEvent<T>
230249
{
231250
eventType = eventType,
232-
index = list.Count-1,
251+
index = list.Count - 1,
233252
value = list[list.Count - 1]
234253
});
254+
}
255+
256+
if (keepDirtyDelta)
257+
{
258+
dirtyEvents.Add(new NetworkedListEvent<T>()
259+
{
260+
eventType = eventType,
261+
index = list.Count - 1,
262+
value = list[list.Count - 1]
263+
});
264+
}
235265
}
236266
break;
237267
case NetworkedListEvent<T>.EventType.Insert:
@@ -240,12 +270,24 @@ public void ReadDelta(Stream stream)
240270
list.Insert(index, (T)reader.ReadObjectPacked(typeof(T))); //BOX
241271

242272
if (OnListChanged != null)
243-
OnListChanged(new NetworkedListEvent<T>
273+
{
274+
OnListChanged(new NetworkedListEvent<T>
275+
{
276+
eventType = eventType,
277+
index = index,
278+
value = list[index]
279+
});
280+
}
281+
282+
if (keepDirtyDelta)
283+
{
284+
dirtyEvents.Add(new NetworkedListEvent<T>()
244285
{
245286
eventType = eventType,
246287
index = index,
247288
value = list[index]
248289
});
290+
}
249291
}
250292
break;
251293
case NetworkedListEvent<T>.EventType.Remove:
@@ -255,12 +297,24 @@ public void ReadDelta(Stream stream)
255297
list.RemoveAt(index);
256298

257299
if (OnListChanged != null)
258-
OnListChanged(new NetworkedListEvent<T>
300+
{
301+
OnListChanged(new NetworkedListEvent<T>
259302
{
260303
eventType = eventType,
261304
index = index,
262305
value = value
263306
});
307+
}
308+
309+
if (keepDirtyDelta)
310+
{
311+
dirtyEvents.Add(new NetworkedListEvent<T>()
312+
{
313+
eventType = eventType,
314+
index = index,
315+
value = value
316+
});
317+
}
264318
}
265319
break;
266320
case NetworkedListEvent<T>.EventType.RemoveAt:
@@ -270,12 +324,24 @@ public void ReadDelta(Stream stream)
270324
list.RemoveAt(index);
271325

272326
if (OnListChanged != null)
273-
OnListChanged(new NetworkedListEvent<T>
327+
{
328+
OnListChanged(new NetworkedListEvent<T>
329+
{
330+
eventType = eventType,
331+
index = index,
332+
value = value
333+
});
334+
}
335+
336+
if (keepDirtyDelta)
337+
{
338+
dirtyEvents.Add(new NetworkedListEvent<T>()
274339
{
275340
eventType = eventType,
276341
index = index,
277342
value = value
278343
});
344+
}
279345
}
280346
break;
281347
case NetworkedListEvent<T>.EventType.Value:
@@ -285,12 +351,14 @@ public void ReadDelta(Stream stream)
285351
if (index < list.Count) list[index] = value;
286352

287353
if (OnListChanged != null)
288-
OnListChanged(new NetworkedListEvent<T>
354+
{
355+
OnListChanged(new NetworkedListEvent<T>
289356
{
290357
eventType = eventType,
291358
index = index,
292359
value = value
293360
});
361+
}
294362
}
295363
break;
296364
case NetworkedListEvent<T>.EventType.Clear:
@@ -299,10 +367,12 @@ public void ReadDelta(Stream stream)
299367
list.Clear();
300368

301369
if (OnListChanged != null)
302-
OnListChanged(new NetworkedListEvent<T>
370+
{
371+
OnListChanged(new NetworkedListEvent<T>
303372
{
304373
eventType = eventType,
305374
});
375+
}
306376
}
307377
break;
308378
}

MLAPI/Data/NetworkedVar.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ 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) => ReadField(stream); //The NetworkedVar is built for simple data types and has no delta.
169+
public void ReadDelta(Stream stream, bool keepDirtyDelta) => ReadField(stream, keepDirtyDelta); //The NetworkedVar is built for simple data types and has no delta.
170170

171171
/// <inheritdoc />
172172
public void SetNetworkedBehaviour(NetworkedBehaviour behaviour)
@@ -175,12 +175,15 @@ public void SetNetworkedBehaviour(NetworkedBehaviour behaviour)
175175
}
176176

177177
/// <inheritdoc />
178-
public void ReadField(Stream stream)
178+
public void ReadField(Stream stream, bool keepDirtyState)
179179
{
180180
using (PooledBitReader reader = PooledBitReader.Get(stream))
181181
{
182182
T previousValue = InternalValue;
183183
InternalValue = (T)reader.ReadObjectPacked((typeof(T)));
184+
185+
if (keepDirtyState) isDirty = true;
186+
184187
if (OnValueChanged != null)
185188
OnValueChanged(previousValue, InternalValue);
186189
}

MLAPI/Data/NetworkedVarMeta.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public interface INetworkedVar
4848
/// Reads the complete state from the reader and applies it
4949
/// </summary>
5050
/// <param name="stream">The stream to read the state from</param>
51-
void ReadField(Stream stream);
51+
void ReadField(Stream stream, bool keepDirtyState);
5252
/// <summary>
5353
/// Reads delta from the reader and applies them to the internal value
5454
/// </summary>
5555
/// <param name="stream">The stream to read the delta from</param>
56-
void ReadDelta(Stream stream);
56+
void ReadDelta(Stream stream, bool keepDirtyDelta);
5757
/// <summary>
5858
/// Sets NetworkedBehaviour the container belongs to.
5959
/// </summary>

0 commit comments

Comments
 (0)