forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkVariable.cs
More file actions
585 lines (482 loc) · 19.1 KB
/
NetworkVariable.cs
File metadata and controls
585 lines (482 loc) · 19.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
using MLAPI.Serialization.Pooled;
using MLAPI.Transports;
namespace MLAPI.NetworkVariable
{
/// <summary>
/// A variable that can be synchronized over the network.
/// </summary>
[Serializable]
public class NetworkVariable<T> : INetworkVariable
{
/// <summary>
/// The settings for this var
/// </summary>
public readonly NetworkVariableSettings Settings = new NetworkVariableSettings();
/// <summary>
/// The last time the variable was written to locally
/// </summary>
public ushort LocalTick { get; internal set; }
/// <summary>
/// The last time the variable was written to remotely. Uses the remote timescale
/// </summary>
public ushort RemoteTick { get; internal set; }
/// <summary>
/// Delegate type for value changed event
/// </summary>
/// <param name="previousValue">The value before the change</param>
/// <param name="newValue">The new value</param>
public delegate void OnValueChangedDelegate(T previousValue, T newValue);
/// <summary>
/// The callback to be invoked when the value gets changed
/// </summary>
public OnValueChangedDelegate OnValueChanged;
private NetworkBehaviour m_NetworkBehaviour;
/// <summary>
/// Creates a NetworkVariable with the default value and settings
/// </summary>
public NetworkVariable() { }
/// <summary>
/// Creates a NetworkVariable with the default value and custom settings
/// </summary>
/// <param name="settings">The settings to use for the NetworkVariable</param>
public NetworkVariable(NetworkVariableSettings settings)
{
Settings = settings;
}
/// <summary>
/// Creates a NetworkVariable with a custom value and custom settings
/// </summary>
/// <param name="settings">The settings to use for the NetworkVariable</param>
/// <param name="value">The initial value to use for the NetworkVariable</param>
public NetworkVariable(NetworkVariableSettings settings, T value)
{
Settings = settings;
m_InternalValue = value;
}
/// <summary>
/// Creates a NetworkVariable with a custom value and the default settings
/// </summary>
/// <param name="value">The initial value to use for the NetworkVariable</param>
public NetworkVariable(T value)
{
m_InternalValue = value;
}
[SerializeField]
private T m_InternalValue;
/// <summary>
/// The value of the NetworkVariable container
/// </summary>
public T Value
{
get => m_InternalValue;
set
{
if (EqualityComparer<T>.Default.Equals(m_InternalValue, value)) return;
// Setter is assumed to be called locally, by game code.
// When used by the host, it is its responsibility to set the RemoteTick
RemoteTick = NetworkTickSystem.NoTick;
m_IsDirty = true;
T previousValue = m_InternalValue;
m_InternalValue = value;
OnValueChanged?.Invoke(previousValue, m_InternalValue);
}
}
private bool m_IsDirty = false;
/// <summary>
/// Sets whether or not the variable needs to be delta synced
/// </summary>
public void SetDirty(bool isDirty)
{
m_IsDirty = isDirty;
}
/// <inheritdoc />
public bool IsDirty()
{
return m_IsDirty;
}
/// <inheritdoc />
public void ResetDirty()
{
m_IsDirty = false;
}
/// <inheritdoc />
public bool CanClientRead(ulong clientId)
{
switch (Settings.ReadPermission)
{
case NetworkVariablePermission.Everyone:
return true;
case NetworkVariablePermission.ServerOnly:
return false;
case NetworkVariablePermission.OwnerOnly:
return m_NetworkBehaviour.OwnerClientId == clientId;
case NetworkVariablePermission.Custom:
{
if (Settings.ReadPermissionCallback == null) return false;
return Settings.ReadPermissionCallback(clientId);
}
}
return true;
}
/// <summary>
/// Writes the variable to the writer
/// </summary>
/// <param name="stream">The stream to write the value to</param>
public void WriteDelta(Stream stream)
{
WriteField(stream);
}
/// <inheritdoc />
public bool CanClientWrite(ulong clientId)
{
switch (Settings.WritePermission)
{
case NetworkVariablePermission.Everyone:
return true;
case NetworkVariablePermission.ServerOnly:
return false;
case NetworkVariablePermission.OwnerOnly:
return m_NetworkBehaviour.OwnerClientId == clientId;
case NetworkVariablePermission.Custom:
{
if (Settings.WritePermissionCallback == null) return false;
return Settings.WritePermissionCallback(clientId);
}
}
return true;
}
/// <summary>
/// Reads value from the reader and applies it
/// </summary>
/// <param name="stream">The stream to read the value from</param>
/// <param name="keepDirtyDelta">Whether or not the container should keep the dirty delta, or mark the delta as consumed</param>
public void ReadDelta(Stream stream, bool keepDirtyDelta, ushort localTick, ushort remoteTick)
{
// todo: This allows the host-returned value to be set back to an old value
// this will need to be adjusted to check if we're have a most recent value
LocalTick = localTick;
RemoteTick = remoteTick;
using (var reader = PooledNetworkReader.Get(stream))
{
T previousValue = m_InternalValue;
m_InternalValue = (T)reader.ReadObjectPacked(typeof(T));
if (keepDirtyDelta) m_IsDirty = true;
OnValueChanged?.Invoke(previousValue, m_InternalValue);
}
}
/// <inheritdoc />
public void SetNetworkBehaviour(NetworkBehaviour behaviour)
{
m_NetworkBehaviour = behaviour;
}
/// <inheritdoc />
public void ReadField(Stream stream, ushort localTick, ushort remoteTick)
{
ReadDelta(stream, false, localTick, remoteTick);
}
/// <inheritdoc />
public void WriteField(Stream stream)
{
// Store the local tick at which this NetworkVariable was modified
LocalTick = NetworkBehaviour.CurrentTick;
using (var writer = PooledNetworkWriter.Get(stream))
{
writer.WriteObjectPacked(m_InternalValue); //BOX
}
}
/// <inheritdoc />
public NetworkChannel GetChannel()
{
return Settings.SendNetworkChannel;
}
}
/// <summary>
/// A NetworkVariable that holds strings and support serialization
/// </summary>
[Serializable]
public class NetworkVariableString : NetworkVariable<string>
{
/// <inheritdoc />
public NetworkVariableString() : base(string.Empty) { }
/// <inheritdoc />
public NetworkVariableString(NetworkVariableSettings settings) : base(settings, string.Empty) { }
/// <inheritdoc />
public NetworkVariableString(string value) : base(value) { }
/// <inheritdoc />
public NetworkVariableString(NetworkVariableSettings settings, string value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds bools and support serialization
/// </summary>
[Serializable]
public class NetworkVariableBool : NetworkVariable<bool>
{
/// <inheritdoc />
public NetworkVariableBool() { }
/// <inheritdoc />
public NetworkVariableBool(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableBool(bool value) : base(value) { }
/// <inheritdoc />
public NetworkVariableBool(NetworkVariableSettings settings, bool value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds bytes and support serialization
/// </summary>
[Serializable]
public class NetworkVariableByte : NetworkVariable<byte>
{
/// <inheritdoc />
public NetworkVariableByte() { }
/// <inheritdoc />
public NetworkVariableByte(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableByte(byte value) : base(value) { }
/// <inheritdoc />
public NetworkVariableByte(NetworkVariableSettings settings, byte value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds sbytes and support serialization
/// </summary>
[Serializable]
public class NetworkVariableSByte : NetworkVariable<sbyte>
{
/// <inheritdoc />
public NetworkVariableSByte() { }
/// <inheritdoc />
public NetworkVariableSByte(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableSByte(sbyte value) : base(value) { }
/// <inheritdoc />
public NetworkVariableSByte(NetworkVariableSettings settings, sbyte value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds ushorts and support serialization
/// </summary>
[Serializable]
public class NetworkVariableUShort : NetworkVariable<ushort>
{
/// <inheritdoc />
public NetworkVariableUShort() { }
/// <inheritdoc />
public NetworkVariableUShort(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableUShort(ushort value) : base(value) { }
/// <inheritdoc />
public NetworkVariableUShort(NetworkVariableSettings settings, ushort value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds shorts and support serialization
/// </summary>
[Serializable]
public class NetworkVariableShort : NetworkVariable<short>
{
/// <inheritdoc />
public NetworkVariableShort() { }
/// <inheritdoc />
public NetworkVariableShort(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableShort(short value) : base(value) { }
/// <inheritdoc />
public NetworkVariableShort(NetworkVariableSettings settings, short value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds uints and support serialization
/// </summary>
[Serializable]
public class NetworkVariableUInt : NetworkVariable<uint>
{
/// <inheritdoc />
public NetworkVariableUInt() { }
/// <inheritdoc />
public NetworkVariableUInt(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableUInt(uint value) : base(value) { }
/// <inheritdoc />
public NetworkVariableUInt(NetworkVariableSettings settings, uint value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds ints and support serialization
/// </summary>
[Serializable]
public class NetworkVariableInt : NetworkVariable<int>
{
/// <inheritdoc />
public NetworkVariableInt() { }
/// <inheritdoc />
public NetworkVariableInt(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableInt(int value) : base(value) { }
/// <inheritdoc />
public NetworkVariableInt(NetworkVariableSettings settings, int value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds ulongs and support serialization
/// </summary>
[Serializable]
public class NetworkVariableULong : NetworkVariable<ulong>
{
/// <inheritdoc />
public NetworkVariableULong() { }
/// <inheritdoc />
public NetworkVariableULong(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableULong(ulong value) : base(value) { }
/// <inheritdoc />
public NetworkVariableULong(NetworkVariableSettings settings, ulong value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds longs and support serialization
/// </summary>
[Serializable]
public class NetworkVariableLong : NetworkVariable<long>
{
/// <inheritdoc />
public NetworkVariableLong() { }
/// <inheritdoc />
public NetworkVariableLong(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableLong(long value) : base(value) { }
/// <inheritdoc />
public NetworkVariableLong(NetworkVariableSettings settings, long value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds floats and support serialization
/// </summary>
[Serializable]
public class NetworkVariableFloat : NetworkVariable<float>
{
/// <inheritdoc />
public NetworkVariableFloat() { }
/// <inheritdoc />
public NetworkVariableFloat(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableFloat(float value) : base(value) { }
/// <inheritdoc />
public NetworkVariableFloat(NetworkVariableSettings settings, float value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds doubles and support serialization
/// </summary>
[Serializable]
public class NetworkVariableDouble : NetworkVariable<double>
{
/// <inheritdoc />
public NetworkVariableDouble() { }
/// <inheritdoc />
public NetworkVariableDouble(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableDouble(double value) : base(value) { }
/// <inheritdoc />
public NetworkVariableDouble(NetworkVariableSettings settings, double value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds vector2s and support serialization
/// </summary>
[Serializable]
public class NetworkVariableVector2 : NetworkVariable<Vector2>
{
/// <inheritdoc />
public NetworkVariableVector2() { }
/// <inheritdoc />
public NetworkVariableVector2(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableVector2(Vector2 value) : base(value) { }
/// <inheritdoc />
public NetworkVariableVector2(NetworkVariableSettings settings, Vector2 value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds vector3s and support serialization
/// </summary>
[Serializable]
public class NetworkVariableVector3 : NetworkVariable<Vector3>
{
/// <inheritdoc />
public NetworkVariableVector3() { }
/// <inheritdoc />
public NetworkVariableVector3(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableVector3(Vector3 value) : base(value) { }
/// <inheritdoc />
public NetworkVariableVector3(NetworkVariableSettings settings, Vector3 value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds vector4s and support serialization
/// </summary>
[Serializable]
public class NetworkVariableVector4 : NetworkVariable<Vector4>
{
/// <inheritdoc />
public NetworkVariableVector4() { }
/// <inheritdoc />
public NetworkVariableVector4(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableVector4(Vector4 value) : base(value) { }
/// <inheritdoc />
public NetworkVariableVector4(NetworkVariableSettings settings, Vector4 value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds colors and support serialization
/// </summary>
[Serializable]
public class NetworkVariableColor : NetworkVariable<Color>
{
/// <inheritdoc />
public NetworkVariableColor() { }
/// <inheritdoc />
public NetworkVariableColor(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableColor(Color value) : base(value) { }
/// <inheritdoc />
public NetworkVariableColor(NetworkVariableSettings settings, Color value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds color32s and support serialization
/// </summary>
[Serializable]
public class NetworkVariableColor32 : NetworkVariable<Color32>
{
/// <inheritdoc />
public NetworkVariableColor32() { }
/// <inheritdoc />
public NetworkVariableColor32(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableColor32(Color32 value) : base(value) { }
/// <inheritdoc />
public NetworkVariableColor32(NetworkVariableSettings settings, Color32 value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds rays and support serialization
/// </summary>
[Serializable]
public class NetworkVariableRay : NetworkVariable<Ray>
{
/// <inheritdoc />
public NetworkVariableRay() { }
/// <inheritdoc />
public NetworkVariableRay(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableRay(Ray value) : base(value) { }
/// <inheritdoc />
public NetworkVariableRay(NetworkVariableSettings settings, Ray value) : base(settings, value) { }
}
/// <summary>
/// A NetworkVariable that holds quaternions and support serialization
/// </summary>
[Serializable]
public class NetworkVariableQuaternion : NetworkVariable<Quaternion>
{
/// <inheritdoc />
public NetworkVariableQuaternion() { }
/// <inheritdoc />
public NetworkVariableQuaternion(NetworkVariableSettings settings) : base(settings) { }
/// <inheritdoc />
public NetworkVariableQuaternion(Quaternion value) : base(value) { }
/// <inheritdoc />
public NetworkVariableQuaternion(NetworkVariableSettings settings, Quaternion value) : base(settings, value) { }
}
}