Skip to content

Commit 6181e7e

Browse files
authored
feat: INetworkMessage (#1187)
* FastBufferWriter implemented and tested (still need to add and update some xmldoc comments though) * A few additional tests to cover the last missed cases I can think of (aside from INetworkSerializable, which isn't ready to be tested yet due to lack of BufferSerializer) * FastBufferReader + tests * - More tests - Streamlined the implementations of bit-packing uints and ulongs. * - Removed NativeArray from FastBufferReader and FastBufferWriter, replacing with byte* allocated directly through UnsafeUtility.Malloc() (same function used under the hood by NativeArray). This is required in order to be able to store pointers to FastBufferReader and FastBufferWriter in order to be able to wrap them with BufferSerializer - NativeArray contains a managed variable in it that disallows taking a pointer to it. And since FBW and FBR are structs, pointers are the only way to wrap them "by reference" in another struct - ref fields aren't allowed even inside ref structs. * Added utility ref struct "Ref<T>" to more generically support wrapping values in other ref structs in a capture-by-reference style, updated BitReader and BitWriter to use that. Also aggressively inlining properties in FBW and FBR. * BufferSerializer and tests. * Removed unnecessary comment. * XMLDocs + cleanup for PR * Replaced possibly unaligned memory access with UnsafeUtility.MemCpy... it's a little slower, but apparently some platforms won't support unaligned memory access (e.g., WEBGL, ARM processors) and there's no compile time way to detect ARM processors since the bytecode is not processor-dependent... the cost of the runtime detection would be more expensive than the cost of just doing the memcpy. * Resurrected BytewiseUtil.FastCopyBytes as a faster alternative to UnsafeUtility.MemCpy for small values, while still supporting unaligned access. * Reverting an accidental change. * Removed files that got accidentally duplicated from before the rename. * Standards fixes * Removed accidentally added files. * Added BuildInfo.json to the .gitignore so I stop accidentally checking it in. * Addressed most of the review feedback. Still need to do a little more restructuring of some of the other tests. * standards.py --fix * standards.py --fix * Fixed incorrect namespaces. * -Fixed a couple of issues where growing a FastBufferWriter wouldn't work correctly (requesting beyond MaxCapacity and requesting more than double current capacity) -Added support for FastBufferReader to be used in a mode that doesn't copy the input buffer * Fix a test failure and better implementation of large growths * - Removed RefArray - Fixed incorrect text in a warning in FastBufferReader * First INetworkMessage WIP - not hooked up to anything yet. * Killed DelayUtil. * Hooked up MessagingSystem and converted ConnectionRequestMessage and ConnectionApprovedMessage. A couple of tests are known to be failing... this is to let the work start being divided up. * More converted messages * Finished converting all messages over and removed all the now-dead code. Several tests are failing... fixing them will be the next checkin. * Removed IMessageHandler as it's no longer needed after DelayUtil went away. * Fixed tests. * standards.py --fix * Corrected some incorrect xmldocs. * -Added some tests for serializing RPC parameters using FastBufferReader/FastBufferWriter extension methods -Fixed invalid IL code generated when using an extension method to serialize an array. * Fixed missing send queue information for the server on StartClient(). * Added ILPP compile-time check for INetworkMessage Receive() function. * Missed the meta file. * Changed generic function lookup in ILPP to use Cecil instead of relying on system types. * Changed scene management delivery type to ReliableFragmentedSequenced to support larger messages. * Fixed DontDestroyWithOwner throwing an exception on disconnect, fixed memory leak if the server forcibly disconnects a client * - Removed DynamicUnmanagedArray in favor of NativeList - Changed ulong[] to IEnumerable<ulong> in ClientRpcParams * standards.py --fix, plus some adjustments based on over-the-zoom-shoulder review with Fatih * Not sure how standards.py --fix missed this the first time. * increasing timeout in hopes it fixes test failure. * Restored ClientConnected on the ServerClientId in StartClient... seems we can't rely on the server connect event to come in before messages get sent to it? * Cherrypick from fast buffer reader/writer branch to fix accidentally reverted stuff. * Applied much review feedback. * Fixed snapshot stuff and also an outdated comment. * More feedback. * Fix standards check. * Fixed an edge case where the temp serialize buffer could be too large to copy into the main one. * Fixed metrics tests. * standards.py --fix * Renamed IBufferSerializerImplementation to IReaderWriter * Ok... actually renamed IBufferSerializerImplementation to IReaderWriter... * Missed a meta file * Fixed missing OnAfterSendMessage hook when sending to localhost. * Take 3 at a rename. * standards.py --fix
1 parent 268c7ec commit 6181e7e

File tree

195 files changed

+6186
-19273
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

195 files changed

+6186
-19273
lines changed

com.unity.netcode.gameobjects/Components/NetworkAnimator.cs

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public bool SetTrigger(int key)
7676
return TriggerParameters.Add(key);
7777
}
7878

79-
public void NetworkSerialize(NetworkSerializer serializer)
79+
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
8080
{
8181
SerializeIntParameters(serializer);
8282
SerializeFloatParameters(serializer);
@@ -85,28 +85,28 @@ public void NetworkSerialize(NetworkSerializer serializer)
8585
SerializeAnimatorLayerStates(serializer);
8686
}
8787

88-
private void SerializeAnimatorLayerStates(NetworkSerializer serializer)
88+
private void SerializeAnimatorLayerStates<T>(BufferSerializer<T> serializer) where T : IReaderWriter
8989
{
90-
int layerCount = serializer.IsReading ? 0 : LayerStates.Length;
91-
serializer.Serialize(ref layerCount);
90+
int layerCount = serializer.IsReader ? 0 : LayerStates.Length;
91+
serializer.SerializeValue(ref layerCount);
9292

93-
if (serializer.IsReading && LayerStates.Length != layerCount)
93+
if (serializer.IsReader && LayerStates.Length != layerCount)
9494
{
9595
LayerStates = new LayerState[layerCount];
9696
}
9797

9898
for (int paramIndex = 0; paramIndex < layerCount; paramIndex++)
9999
{
100-
var stateHash = serializer.IsReading ? 0 : LayerStates[paramIndex].StateHash;
101-
serializer.Serialize(ref stateHash);
100+
var stateHash = serializer.IsReader ? 0 : LayerStates[paramIndex].StateHash;
101+
serializer.SerializeValue(ref stateHash);
102102

103-
var layerWeight = serializer.IsReading ? 0 : LayerStates[paramIndex].LayerWeight;
104-
serializer.Serialize(ref layerWeight);
103+
var layerWeight = serializer.IsReader ? 0 : LayerStates[paramIndex].LayerWeight;
104+
serializer.SerializeValue(ref layerWeight);
105105

106-
var normalizedStateTime = serializer.IsReading ? 0 : LayerStates[paramIndex].NormalizedStateTime;
107-
serializer.Serialize(ref normalizedStateTime);
106+
var normalizedStateTime = serializer.IsReader ? 0 : LayerStates[paramIndex].NormalizedStateTime;
107+
serializer.SerializeValue(ref normalizedStateTime);
108108

109-
if (serializer.IsReading)
109+
if (serializer.IsReader)
110110
{
111111
LayerStates[paramIndex] = new LayerState()
112112
{
@@ -118,103 +118,103 @@ private void SerializeAnimatorLayerStates(NetworkSerializer serializer)
118118
}
119119
}
120120

121-
private void SerializeTriggerParameters(NetworkSerializer serializer)
121+
private void SerializeTriggerParameters<T>(BufferSerializer<T> serializer) where T : IReaderWriter
122122
{
123-
int paramCount = serializer.IsReading ? 0 : TriggerParameters.Count;
124-
serializer.Serialize(ref paramCount);
123+
int paramCount = serializer.IsReader ? 0 : TriggerParameters.Count;
124+
serializer.SerializeValue(ref paramCount);
125125

126-
var paramArray = serializer.IsReading ? new int[paramCount] : TriggerParameters.ToArray();
126+
var paramArray = serializer.IsReader ? new int[paramCount] : TriggerParameters.ToArray();
127127
for (int i = 0; i < paramCount; i++)
128128
{
129-
var paramId = serializer.IsReading ? 0 : paramArray[i];
130-
serializer.Serialize(ref paramId);
129+
var paramId = serializer.IsReader ? 0 : paramArray[i];
130+
serializer.SerializeValue(ref paramId);
131131

132-
if (serializer.IsReading)
132+
if (serializer.IsReader)
133133
{
134134
paramArray[i] = paramId;
135135
}
136136
}
137137

138-
if (serializer.IsReading)
138+
if (serializer.IsReader)
139139
{
140140
TriggerParameters = new HashSet<int>(paramArray);
141141
}
142142
}
143143

144-
private void SerializeBoolParameters(NetworkSerializer serializer)
144+
private void SerializeBoolParameters<T>(BufferSerializer<T> serializer) where T : IReaderWriter
145145
{
146-
int paramCount = serializer.IsReading ? 0 : BoolParameters.Count;
147-
serializer.Serialize(ref paramCount);
146+
int paramCount = serializer.IsReader ? 0 : BoolParameters.Count;
147+
serializer.SerializeValue(ref paramCount);
148148

149-
var paramArray = serializer.IsReading ? new KeyValuePair<int, bool>[paramCount] : BoolParameters.ToArray();
149+
var paramArray = serializer.IsReader ? new KeyValuePair<int, bool>[paramCount] : BoolParameters.ToArray();
150150
for (int paramIndex = 0; paramIndex < paramCount; paramIndex++)
151151
{
152-
var paramId = serializer.IsReading ? 0 : paramArray[paramIndex].Key;
153-
serializer.Serialize(ref paramId);
152+
var paramId = serializer.IsReader ? 0 : paramArray[paramIndex].Key;
153+
serializer.SerializeValue(ref paramId);
154154

155-
var paramBool = serializer.IsReading ? false : paramArray[paramIndex].Value;
156-
serializer.Serialize(ref paramBool);
155+
var paramBool = serializer.IsReader ? false : paramArray[paramIndex].Value;
156+
serializer.SerializeValue(ref paramBool);
157157

158-
if (serializer.IsReading)
158+
if (serializer.IsReader)
159159
{
160160
paramArray[paramIndex] = new KeyValuePair<int, bool>(paramId, paramBool);
161161
}
162162
}
163163

164-
if (serializer.IsReading)
164+
if (serializer.IsReader)
165165
{
166166
BoolParameters = paramArray.ToDictionary(pair => pair.Key, pair => pair.Value);
167167
}
168168
}
169169

170-
private void SerializeFloatParameters(NetworkSerializer serializer)
170+
private void SerializeFloatParameters<T>(BufferSerializer<T> serializer) where T : IReaderWriter
171171
{
172-
int paramCount = serializer.IsReading ? 0 : FloatParameters.Count;
173-
serializer.Serialize(ref paramCount);
172+
int paramCount = serializer.IsReader ? 0 : FloatParameters.Count;
173+
serializer.SerializeValue(ref paramCount);
174174

175-
var paramArray = serializer.IsReading ? new KeyValuePair<int, float>[paramCount] : FloatParameters.ToArray();
175+
var paramArray = serializer.IsReader ? new KeyValuePair<int, float>[paramCount] : FloatParameters.ToArray();
176176
for (int paramIndex = 0; paramIndex < paramCount; paramIndex++)
177177
{
178-
var paramId = serializer.IsReading ? 0 : paramArray[paramIndex].Key;
179-
serializer.Serialize(ref paramId);
178+
var paramId = serializer.IsReader ? 0 : paramArray[paramIndex].Key;
179+
serializer.SerializeValue(ref paramId);
180180

181-
var paramFloat = serializer.IsReading ? 0 : paramArray[paramIndex].Value;
182-
serializer.Serialize(ref paramFloat);
181+
var paramFloat = serializer.IsReader ? 0 : paramArray[paramIndex].Value;
182+
serializer.SerializeValue(ref paramFloat);
183183

184-
if (serializer.IsReading)
184+
if (serializer.IsReader)
185185
{
186186
paramArray[paramIndex] = new KeyValuePair<int, float>(paramId, paramFloat);
187187
}
188188
}
189189

190-
if (serializer.IsReading)
190+
if (serializer.IsReader)
191191
{
192192
FloatParameters = paramArray.ToDictionary(pair => pair.Key, pair => pair.Value);
193193
}
194194
}
195195

196-
private void SerializeIntParameters(NetworkSerializer serializer)
196+
private void SerializeIntParameters<T>(BufferSerializer<T> serializer) where T : IReaderWriter
197197
{
198-
int paramCount = serializer.IsReading ? 0 : IntParameters.Count;
199-
serializer.Serialize(ref paramCount);
198+
int paramCount = serializer.IsReader ? 0 : IntParameters.Count;
199+
serializer.SerializeValue(ref paramCount);
200200

201-
var paramArray = serializer.IsReading ? new KeyValuePair<int, int>[paramCount] : IntParameters.ToArray();
201+
var paramArray = serializer.IsReader ? new KeyValuePair<int, int>[paramCount] : IntParameters.ToArray();
202202

203203
for (int paramIndex = 0; paramIndex < paramCount; paramIndex++)
204204
{
205-
var paramId = serializer.IsReading ? 0 : paramArray[paramIndex].Key;
206-
serializer.Serialize(ref paramId);
205+
var paramId = serializer.IsReader ? 0 : paramArray[paramIndex].Key;
206+
serializer.SerializeValue(ref paramId);
207207

208-
var paramInt = serializer.IsReading ? 0 : paramArray[paramIndex].Value;
209-
serializer.Serialize(ref paramInt);
208+
var paramInt = serializer.IsReader ? 0 : paramArray[paramIndex].Value;
209+
serializer.SerializeValue(ref paramInt);
210210

211-
if (serializer.IsReading)
211+
if (serializer.IsReader)
212212
{
213213
paramArray[paramIndex] = new KeyValuePair<int, int>(paramId, paramInt);
214214
}
215215
}
216216

217-
if (serializer.IsReading)
217+
if (serializer.IsReader)
218218
{
219219
IntParameters = paramArray.ToDictionary(pair => pair.Key, pair => pair.Value);
220220
}

com.unity.netcode.gameobjects/Components/NetworkTransform.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,57 +169,57 @@ public Vector3 Scale
169169
}
170170
}
171171

172-
public void NetworkSerialize(NetworkSerializer serializer)
172+
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
173173
{
174-
serializer.Serialize(ref SentTime);
174+
serializer.SerializeValue(ref SentTime);
175175
// InLocalSpace + HasXXX Bits
176-
serializer.Serialize(ref Bitset);
176+
serializer.SerializeValue(ref Bitset);
177177
// Position Values
178178
if (HasPositionX)
179179
{
180-
serializer.Serialize(ref PositionX);
180+
serializer.SerializeValue(ref PositionX);
181181
}
182182

183183
if (HasPositionY)
184184
{
185-
serializer.Serialize(ref PositionY);
185+
serializer.SerializeValue(ref PositionY);
186186
}
187187

188188
if (HasPositionZ)
189189
{
190-
serializer.Serialize(ref PositionZ);
190+
serializer.SerializeValue(ref PositionZ);
191191
}
192192

193193
// RotAngle Values
194194
if (HasRotAngleX)
195195
{
196-
serializer.Serialize(ref RotAngleX);
196+
serializer.SerializeValue(ref RotAngleX);
197197
}
198198

199199
if (HasRotAngleY)
200200
{
201-
serializer.Serialize(ref RotAngleY);
201+
serializer.SerializeValue(ref RotAngleY);
202202
}
203203

204204
if (HasRotAngleZ)
205205
{
206-
serializer.Serialize(ref RotAngleZ);
206+
serializer.SerializeValue(ref RotAngleZ);
207207
}
208208

209209
// Scale Values
210210
if (HasScaleX)
211211
{
212-
serializer.Serialize(ref ScaleX);
212+
serializer.SerializeValue(ref ScaleX);
213213
}
214214

215215
if (HasScaleY)
216216
{
217-
serializer.Serialize(ref ScaleY);
217+
serializer.SerializeValue(ref ScaleY);
218218
}
219219

220220
if (HasScaleZ)
221221
{
222-
serializer.Serialize(ref ScaleZ);
222+
serializer.SerializeValue(ref ScaleZ);
223223
}
224224
}
225225
}

com.unity.netcode.gameobjects/Editor/CodeGen/CodeGenHelpers.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal static class CodeGenHelpers
1717
public const string RuntimeAssemblyName = "Unity.Netcode.Runtime";
1818

1919
public static readonly string NetworkBehaviour_FullName = typeof(NetworkBehaviour).FullName;
20+
public static readonly string INetworkMessage_FullName = typeof(INetworkMessage).FullName;
2021
public static readonly string ServerRpcAttribute_FullName = typeof(ServerRpcAttribute).FullName;
2122
public static readonly string ClientRpcAttribute_FullName = typeof(ClientRpcAttribute).FullName;
2223
public static readonly string ServerRpcParams_FullName = typeof(ServerRpcParams).FullName;
@@ -264,9 +265,9 @@ public static void AddError(this List<DiagnosticMessage> diagnostics, SequencePo
264265
});
265266
}
266267

267-
public static AssemblyDefinition AssemblyDefinitionFor(ICompiledAssembly compiledAssembly)
268+
public static AssemblyDefinition AssemblyDefinitionFor(ICompiledAssembly compiledAssembly, out PostProcessorAssemblyResolver assemblyResolver)
268269
{
269-
var assemblyResolver = new PostProcessorAssemblyResolver(compiledAssembly);
270+
assemblyResolver = new PostProcessorAssemblyResolver(compiledAssembly);
270271
var readerParameters = new ReaderParameters
271272
{
272273
SymbolStream = new MemoryStream(compiledAssembly.InMemoryAssembly.PdbData),

0 commit comments

Comments
 (0)