-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathNetworkArray.cs
More file actions
112 lines (91 loc) · 3.21 KB
/
NetworkArray.cs
File metadata and controls
112 lines (91 loc) · 3.21 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
// Copyright (c) Meta Platforms, Inc. and affiliates.
using System;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Netcode;
namespace Meta.Multiplayer.Networking
{
/// <summary>
/// Event based NetworkVariable container for syncing Arrays
/// </summary>
/// <typeparam name="T">The type for the array</typeparam>
public class NetworkArray<T> : NetworkVariableBase where T : unmanaged, IEquatable<T>
{
private NativeList<T> m_list = new(0, Allocator.Persistent);
/// <summary>
/// Delegate type for array changed event
/// </summary>
public delegate void OnValuesChangedDelegate(NativeArray<T> newValue);
/// <summary>
/// The callback to be invoked when the array gets changed
/// </summary>
public OnValuesChangedDelegate OnValuesChanged;
public NetworkArray() { }
public NetworkArray(
NetworkVariableReadPermission readPerm = DefaultReadPerm,
NetworkVariableWritePermission writePerm = DefaultWritePerm)
: base(readPerm, writePerm)
{
}
/// <inheritdoc />
public override void WriteDelta(FastBufferWriter writer)
{
if (IsDirty())
{
WriteField(writer);
}
}
/// <inheritdoc />
public override unsafe void WriteField(FastBufferWriter writer)
{
var length = (ushort)m_list.Length;
writer.WriteValueSafe(length);
if (length != 0)
{
var pointer = m_list.GetUnsafeReadOnlyPtr();
writer.WriteBytesSafe((byte*)pointer, length * sizeof(T));
}
}
/// <inheritdoc />
public override unsafe void ReadField(FastBufferReader reader)
{
reader.ReadValueSafe(out ushort length);
m_list.Clear();
m_list.ResizeUninitialized(length);
if (length != 0)
{
var pointer = m_list.GetUnsafeReadOnlyPtr();
reader.ReadBytesSafe((byte*)pointer, length * sizeof(T));
}
}
/// <inheritdoc />
public override unsafe void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
{
ReadField(reader);
if (keepDirtyDelta)
{
SetDirty(true);
}
OnValuesChanged?.Invoke(m_list);
}
public int LastModifiedTick =>
// todo: implement proper network tick for NetworkArray
NetworkTickSystem.NoTick;
public unsafe void ModifyValues(Action<NativeList<T>> modifyValues)
{
if (NetworkManager.Singleton != null && !CanClientWrite(NetworkManager.Singleton.LocalClientId))
{
throw new InvalidOperationException("Client is not allowed to write to this NetworkVariable");
}
SetDirty(true);
modifyValues(m_list);
OnValuesChanged?.Invoke(m_list);
}
public override void Dispose()
{
m_list.Dispose();
base.Dispose();
}
public NativeArray<T> Values => m_list;
}
}