diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3acbe8c..0682f59 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,4 +1,3 @@
-
# Changelog
All notable changes to this project will be documented in this file.
@@ -7,6 +6,37 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).
+## [1.2.0] - 2022-11-21
+
+### Added
+
+- Added protected method `NetworkBehaviour.OnSynchronize` which is invoked during the initial `NetworkObject` synchronization process. This provides users the ability to include custom serialization information that will be applied to the `NetworkBehaviour` prior to the `NetworkObject` being spawned. (#2298)
+- Added support for different versions of the SDK to talk to each other in circumstances where changes permit it. Starting with this version and into future versions, patch versions should be compatible as long as the minor version is the same. (#2290)
+- Added `NetworkObject` auto-add helper and Multiplayer Tools install reminder settings to Project Settings. (#2285)
+- Added `public string DisconnectReason` getter to `NetworkManager` and `string Reason` to `ConnectionApprovalResponse`. Allows connection approval to communicate back a reason. Also added `public void DisconnectClient(ulong clientId, string reason)` allowing setting a disconnection reason, when explicitly disconnecting a client. (#2280)
+
+### Changed
+
+- Changed 3rd-party `XXHash` (32 & 64) implementation with an in-house reimplementation (#2310)
+- When `NetworkConfig.EnsureNetworkVariableLengthSafety` is disabled `NetworkVariable` fields do not write the additional `ushort` size value (_which helps to reduce the total synchronization message size_), but when enabled it still writes the additional `ushort` value. (#2298)
+- Optimized bandwidth usage by encoding most integer fields using variable-length encoding. (#2276)
+
+### Fixed
+
+- Fixed issue where `NetworkTransform` components nested under a parent with a `NetworkObject` component (i.e. network prefab) would not have their associated `GameObject`'s transform synchronized. (#2298)
+- Fixed issue where `NetworkObject`s that failed to instantiate could cause the entire synchronization pipeline to be disrupted/halted for a connecting client. (#2298)
+- Fixed issue where in-scene placed `NetworkObject`s nested under a `GameObject` would be added to the orphaned children list causing continual console warning log messages. (#2298)
+- Custom messages are now properly received by the local client when they're sent while running in host mode. (#2296)
+- Fixed issue where the host would receive more than one event completed notification when loading or unloading a scene only when no clients were connected. (#2292)
+- Fixed an issue in `UnityTransport` where an error would be logged if the 'Use Encryption' flag was enabled with a Relay configuration that used a secure protocol. (#2289)
+- Fixed issue where in-scene placed `NetworkObjects` were not honoring the `AutoObjectParentSync` property. (#2281)
+- Fixed the issue where `NetworkManager.OnClientConnectedCallback` was being invoked before in-scene placed `NetworkObject`s had been spawned when starting `NetworkManager` as a host. (#2277)
+- Creating a `FastBufferReader` with `Allocator.None` will not result in extra memory being allocated for the buffer (since it's owned externally in that scenario). (#2265)
+
+### Removed
+
+- Removed the `NetworkObject` auto-add and Multiplayer Tools install reminder settings from the Menu interface. (#2285)
+
## [1.1.0] - 2022-10-21
### Added
@@ -157,6 +187,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
- Removed `ClientNetworkTransform` from the package samples and moved to Boss Room's Utilities package which can be found [here](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/main/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/ClientAuthority/ClientNetworkTransform.cs) (#1912)
### Fixed
+
- Fixed issue where `NetworkSceneManager` did not synchronize despawned in-scene placed NetworkObjects. (#1898)
- Fixed `NetworkTransform` generating false positive rotation delta checks when rolling over between 0 and 360 degrees. (#1890)
- Fixed client throwing an exception if it has messages in the outbound queue when processing the `NetworkEvent.Disconnect` event and is using UTP. (#1884)
@@ -210,10 +241,12 @@ Additional documentation and release notes are available at [Multiplayer Documen
## [1.0.0-pre.6] - 2022-03-02
### Added
+
- NetworkAnimator now properly synchrhonizes all animation layers as well as runtime-adjusted weighting between them (#1765)
- Added first set of tests for NetworkAnimator - parameter syncing, trigger set / reset, override network animator (#1735)
### Fixed
+
- Fixed an issue where sometimes the first client to connect to the server could see messages from the server as coming from itself. (#1683)
- Fixed an issue where clients seemed to be able to send messages to ClientId 1, but these messages would actually still go to the server (id 0) instead of that client. (#1683)
- Improved clarity of error messaging when a client attempts to send a message to a destination other than the server, which isn't allowed. (#1683)
@@ -265,6 +298,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
- Removed `FixedQueue`, `StreamExtensions`, `TypeExtensions` (#1398)
### Fixed
+
- Fixed in-scene NetworkObjects that are moved into the DDOL scene not getting restored to their original active state (enabled/disabled) after a full scene transition (#1354)
- Fixed invalid IL code being generated when using `this` instead of `this ref` for the FastBufferReader/FastBufferWriter parameter of an extension method. (#1393)
- Fixed an issue where if you are running as a server (not host) the LoadEventCompleted and UnloadEventCompleted events would fire early by the NetworkSceneManager (#1379)
@@ -279,6 +313,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
- Fixed network tick value sometimes being duplicated or skipped. (#1614)
### Changed
+
- The SDK no longer limits message size to 64k. (The transport may still impose its own limits, but the SDK no longer does.) (#1384)
- Updated com.unity.collections to 1.1.0 (#1451)
- NetworkManager's GameObject is no longer allowed to be nested under one or more GameObject(s).(#1484)
diff --git a/Components/NetworkTransform.cs b/Components/NetworkTransform.cs
index f8d4dbd..d9e8393 100644
--- a/Components/NetworkTransform.cs
+++ b/Components/NetworkTransform.cs
@@ -451,6 +451,33 @@ namespace Unity.Netcode.Components
return m_LastSentState;
}
+ ///
+ /// This is invoked when a new client joins (server and client sides)
+ /// Server Side: Serializes as if we were teleporting (everything is sent via NetworkTransformState)
+ /// Client Side: Adds the interpolated state which applies the NetworkTransformState as well
+ ///
+ protected override void OnSynchronize(ref BufferSerializer serializer)
+ {
+ // We don't need to synchronize NetworkTransforms that are on the same
+ // GameObject as the NetworkObject.
+ if (NetworkObject.gameObject == gameObject)
+ {
+ return;
+ }
+ var synchronizationState = new NetworkTransformState();
+ if (serializer.IsWriter)
+ {
+ synchronizationState.IsTeleportingNextFrame = true;
+ ApplyTransformToNetworkStateWithInfo(ref synchronizationState, m_CachedNetworkManager.LocalTime.Time, transform);
+ synchronizationState.NetworkSerialize(serializer);
+ }
+ else
+ {
+ synchronizationState.NetworkSerialize(serializer);
+ AddInterpolatedState(synchronizationState);
+ }
+ }
+
///
/// This will try to send/commit the current transform delta states (if any)
///
diff --git a/Editor/CodeGen/INetworkMessageILPP.cs b/Editor/CodeGen/INetworkMessageILPP.cs
index 30b6f0b..9a1e8ac 100644
--- a/Editor/CodeGen/INetworkMessageILPP.cs
+++ b/Editor/CodeGen/INetworkMessageILPP.cs
@@ -102,15 +102,19 @@ namespace Unity.Netcode.Editor.CodeGen
private PostProcessorAssemblyResolver m_AssemblyResolver;
private MethodReference m_MessagingSystem_ReceiveMessage_MethodRef;
+ private MethodReference m_MessagingSystem_CreateMessageAndGetVersion_MethodRef;
private TypeReference m_MessagingSystem_MessageWithHandler_TypeRef;
private MethodReference m_MessagingSystem_MessageHandler_Constructor_TypeRef;
+ private MethodReference m_MessagingSystem_VersionGetter_Constructor_TypeRef;
private FieldReference m_ILPPMessageProvider___network_message_types_FieldRef;
private FieldReference m_MessagingSystem_MessageWithHandler_MessageType_FieldRef;
private FieldReference m_MessagingSystem_MessageWithHandler_Handler_FieldRef;
+ private FieldReference m_MessagingSystem_MessageWithHandler_GetVersion_FieldRef;
private MethodReference m_Type_GetTypeFromHandle_MethodRef;
private MethodReference m_List_Add_MethodRef;
private const string k_ReceiveMessageName = nameof(MessagingSystem.ReceiveMessage);
+ private const string k_CreateMessageAndGetVersionName = nameof(MessagingSystem.CreateMessageAndGetVersion);
private bool ImportReferences(ModuleDefinition moduleDefinition)
{
@@ -126,6 +130,7 @@ namespace Unity.Netcode.Editor.CodeGen
TypeDefinition listTypeDef = moduleDefinition.ImportReference(typeof(List<>)).Resolve();
TypeDefinition messageHandlerTypeDef = null;
+ TypeDefinition versionGetterTypeDef = null;
TypeDefinition messageWithHandlerTypeDef = null;
TypeDefinition ilppMessageProviderTypeDef = null;
TypeDefinition messagingSystemTypeDef = null;
@@ -137,6 +142,12 @@ namespace Unity.Netcode.Editor.CodeGen
continue;
}
+ if (versionGetterTypeDef == null && netcodeTypeDef.Name == nameof(MessagingSystem.VersionGetter))
+ {
+ versionGetterTypeDef = netcodeTypeDef;
+ continue;
+ }
+
if (messageWithHandlerTypeDef == null && netcodeTypeDef.Name == nameof(MessagingSystem.MessageWithHandler))
{
messageWithHandlerTypeDef = netcodeTypeDef;
@@ -157,6 +168,7 @@ namespace Unity.Netcode.Editor.CodeGen
}
m_MessagingSystem_MessageHandler_Constructor_TypeRef = moduleDefinition.ImportReference(messageHandlerTypeDef.GetConstructors().First());
+ m_MessagingSystem_VersionGetter_Constructor_TypeRef = moduleDefinition.ImportReference(versionGetterTypeDef.GetConstructors().First());
m_MessagingSystem_MessageWithHandler_TypeRef = moduleDefinition.ImportReference(messageWithHandlerTypeDef);
foreach (var fieldDef in messageWithHandlerTypeDef.Fields)
@@ -169,6 +181,9 @@ namespace Unity.Netcode.Editor.CodeGen
case nameof(MessagingSystem.MessageWithHandler.Handler):
m_MessagingSystem_MessageWithHandler_Handler_FieldRef = moduleDefinition.ImportReference(fieldDef);
break;
+ case nameof(MessagingSystem.MessageWithHandler.GetVersion):
+ m_MessagingSystem_MessageWithHandler_GetVersion_FieldRef = moduleDefinition.ImportReference(fieldDef);
+ break;
}
}
@@ -211,6 +226,9 @@ namespace Unity.Netcode.Editor.CodeGen
case k_ReceiveMessageName:
m_MessagingSystem_ReceiveMessage_MethodRef = moduleDefinition.ImportReference(methodDef);
break;
+ case k_CreateMessageAndGetVersionName:
+ m_MessagingSystem_CreateMessageAndGetVersion_MethodRef = moduleDefinition.ImportReference(methodDef);
+ break;
}
}
@@ -236,7 +254,7 @@ namespace Unity.Netcode.Editor.CodeGen
return staticCtorMethodDef;
}
- private void CreateInstructionsToRegisterType(ILProcessor processor, List instructions, TypeReference type, MethodReference receiveMethod)
+ private void CreateInstructionsToRegisterType(ILProcessor processor, List instructions, TypeReference type, MethodReference receiveMethod, MethodReference versionMethod)
{
// MessagingSystem.__network_message_types.Add(new MessagingSystem.MessageWithHandler{MessageType=typeof(type), Handler=type.Receive});
processor.Body.Variables.Add(new VariableDefinition(m_MessagingSystem_MessageWithHandler_TypeRef));
@@ -252,7 +270,7 @@ namespace Unity.Netcode.Editor.CodeGen
instructions.Add(processor.Create(OpCodes.Call, m_Type_GetTypeFromHandle_MethodRef));
instructions.Add(processor.Create(OpCodes.Stfld, m_MessagingSystem_MessageWithHandler_MessageType_FieldRef));
- // tmp.Handler = type.Receive
+ // tmp.Handler = MessageHandler.ReceveMessage
instructions.Add(processor.Create(OpCodes.Ldloca, messageWithHandlerLocIdx));
instructions.Add(processor.Create(OpCodes.Ldnull));
@@ -260,6 +278,15 @@ namespace Unity.Netcode.Editor.CodeGen
instructions.Add(processor.Create(OpCodes.Newobj, m_MessagingSystem_MessageHandler_Constructor_TypeRef));
instructions.Add(processor.Create(OpCodes.Stfld, m_MessagingSystem_MessageWithHandler_Handler_FieldRef));
+
+ // tmp.GetVersion = MessageHandler.CreateMessageAndGetVersion
+ instructions.Add(processor.Create(OpCodes.Ldloca, messageWithHandlerLocIdx));
+ instructions.Add(processor.Create(OpCodes.Ldnull));
+
+ instructions.Add(processor.Create(OpCodes.Ldftn, versionMethod));
+ instructions.Add(processor.Create(OpCodes.Newobj, m_MessagingSystem_VersionGetter_Constructor_TypeRef));
+ instructions.Add(processor.Create(OpCodes.Stfld, m_MessagingSystem_MessageWithHandler_GetVersion_FieldRef));
+
// ILPPMessageProvider.__network_message_types.Add(tmp);
instructions.Add(processor.Create(OpCodes.Ldloc, messageWithHandlerLocIdx));
instructions.Add(processor.Create(OpCodes.Callvirt, m_List_Add_MethodRef));
@@ -285,7 +312,9 @@ namespace Unity.Netcode.Editor.CodeGen
{
var receiveMethod = new GenericInstanceMethod(m_MessagingSystem_ReceiveMessage_MethodRef);
receiveMethod.GenericArguments.Add(type);
- CreateInstructionsToRegisterType(processor, instructions, type, receiveMethod);
+ var versionMethod = new GenericInstanceMethod(m_MessagingSystem_CreateMessageAndGetVersion_MethodRef);
+ versionMethod.GenericArguments.Add(type);
+ CreateInstructionsToRegisterType(processor, instructions, type, receiveMethod, versionMethod);
}
instructions.ForEach(instruction => processor.Body.Instructions.Insert(processor.Body.Instructions.Count - 1, instruction));
diff --git a/Editor/CodeGen/NetworkBehaviourILPP.cs b/Editor/CodeGen/NetworkBehaviourILPP.cs
index ce7d3bb..4c1fe20 100644
--- a/Editor/CodeGen/NetworkBehaviourILPP.cs
+++ b/Editor/CodeGen/NetworkBehaviourILPP.cs
@@ -139,6 +139,19 @@ namespace Unity.Netcode.Editor.CodeGen
return false;
}
+ private bool IsSpecialCaseType(TypeReference type)
+ {
+ foreach (var supportedType in SpecialCaseTypes)
+ {
+ if (type.FullName == supportedType.FullName)
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly)
{
foreach (var typeDefinition in assembly.MainModule.Types)
@@ -153,6 +166,11 @@ namespace Unity.Netcode.Editor.CodeGen
foreach (var type in m_WrappedNetworkVariableTypes)
{
+ if (IsSpecialCaseType(type))
+ {
+ continue;
+ }
+
// If a serializable type isn't found, FallbackSerializer will be used automatically, which will
// call into UserNetworkVariableSerialization, giving the user a chance to define their own serializaiton
// for types that aren't in our official supported types list.
@@ -257,6 +275,20 @@ namespace Unity.Netcode.Editor.CodeGen
private MethodReference m_NetworkVariableSerializationTypes_InitializeEqualityChecker_UnmanagedValueEquals_MethodRef;
private MethodReference m_NetworkVariableSerializationTypes_InitializeEqualityChecker_ManagedClassEquals_MethodRef;
+ private MethodReference m_BytePacker_WriteValueBitPacked_Short_MethodRef;
+ private MethodReference m_BytePacker_WriteValueBitPacked_UShort_MethodRef;
+ private MethodReference m_BytePacker_WriteValueBitPacked_Int_MethodRef;
+ private MethodReference m_BytePacker_WriteValueBitPacked_UInt_MethodRef;
+ private MethodReference m_BytePacker_WriteValueBitPacked_Long_MethodRef;
+ private MethodReference m_BytePacker_WriteValueBitPacked_ULong_MethodRef;
+
+ private MethodReference m_ByteUnpacker_ReadValueBitPacked_Short_MethodRef;
+ private MethodReference m_ByteUnpacker_ReadValueBitPacked_UShort_MethodRef;
+ private MethodReference m_ByteUnpacker_ReadValueBitPacked_Int_MethodRef;
+ private MethodReference m_ByteUnpacker_ReadValueBitPacked_UInt_MethodRef;
+ private MethodReference m_ByteUnpacker_ReadValueBitPacked_Long_MethodRef;
+ private MethodReference m_ByteUnpacker_ReadValueBitPacked_ULong_MethodRef;
+
private TypeReference m_FastBufferWriter_TypeRef;
private readonly Dictionary m_FastBufferWriter_WriteValue_MethodRefs = new Dictionary();
private readonly List m_FastBufferWriter_ExtensionMethodRefs = new List();
@@ -276,12 +308,13 @@ namespace Unity.Netcode.Editor.CodeGen
typeof(decimal),
typeof(double),
typeof(float),
- typeof(int),
+ // the following types have special handling
+ /*typeof(int),
typeof(uint),
typeof(long),
typeof(ulong),
typeof(short),
- typeof(ushort),
+ typeof(ushort),*/
typeof(Vector2),
typeof(Vector3),
typeof(Vector2Int),
@@ -293,6 +326,16 @@ namespace Unity.Netcode.Editor.CodeGen
typeof(Ray),
typeof(Ray2D)
};
+ internal static readonly Type[] SpecialCaseTypes = new[]
+ {
+ // the following types have special handling
+ typeof(int),
+ typeof(uint),
+ typeof(long),
+ typeof(ulong),
+ typeof(short),
+ typeof(ushort),
+ };
private const string k_Debug_LogError = nameof(Debug.LogError);
private const string k_NetworkManager_LocalClientId = nameof(NetworkManager.LocalClientId);
@@ -343,6 +386,8 @@ namespace Unity.Netcode.Editor.CodeGen
TypeDefinition fastBufferWriterTypeDef = null;
TypeDefinition fastBufferReaderTypeDef = null;
TypeDefinition networkVariableSerializationTypesTypeDef = null;
+ TypeDefinition bytePackerTypeDef = null;
+ TypeDefinition byteUnpackerTypeDef = null;
foreach (var netcodeTypeDef in m_NetcodeModule.GetAllTypes())
{
if (networkManagerTypeDef == null && netcodeTypeDef.Name == nameof(NetworkManager))
@@ -398,6 +443,18 @@ namespace Unity.Netcode.Editor.CodeGen
networkVariableSerializationTypesTypeDef = netcodeTypeDef;
continue;
}
+
+ if (bytePackerTypeDef == null && netcodeTypeDef.Name == nameof(BytePacker))
+ {
+ bytePackerTypeDef = netcodeTypeDef;
+ continue;
+ }
+
+ if (byteUnpackerTypeDef == null && netcodeTypeDef.Name == nameof(ByteUnpacker))
+ {
+ byteUnpackerTypeDef = netcodeTypeDef;
+ continue;
+ }
}
foreach (var methodDef in debugTypeDef.Methods)
@@ -652,6 +709,82 @@ namespace Unity.Netcode.Editor.CodeGen
}
}
+ foreach (var method in bytePackerTypeDef.Methods)
+ {
+ if (!method.IsStatic)
+ {
+ continue;
+ }
+
+ switch (method.Name)
+ {
+ case nameof(BytePacker.WriteValueBitPacked):
+ if (method.Parameters[1].ParameterType.FullName == typeof(short).FullName)
+ {
+ m_BytePacker_WriteValueBitPacked_Short_MethodRef = m_MainModule.ImportReference(method);
+ }
+ else if (method.Parameters[1].ParameterType.FullName == typeof(ushort).FullName)
+ {
+ m_BytePacker_WriteValueBitPacked_UShort_MethodRef = m_MainModule.ImportReference(method);
+ }
+ else if (method.Parameters[1].ParameterType.FullName == typeof(int).FullName)
+ {
+ m_BytePacker_WriteValueBitPacked_Int_MethodRef = m_MainModule.ImportReference(method);
+ }
+ else if (method.Parameters[1].ParameterType.FullName == typeof(uint).FullName)
+ {
+ m_BytePacker_WriteValueBitPacked_UInt_MethodRef = m_MainModule.ImportReference(method);
+ }
+ else if (method.Parameters[1].ParameterType.FullName == typeof(long).FullName)
+ {
+ m_BytePacker_WriteValueBitPacked_Long_MethodRef = m_MainModule.ImportReference(method);
+ }
+ else if (method.Parameters[1].ParameterType.FullName == typeof(ulong).FullName)
+ {
+ m_BytePacker_WriteValueBitPacked_ULong_MethodRef = m_MainModule.ImportReference(method);
+ }
+ break;
+ }
+ }
+
+ foreach (var method in byteUnpackerTypeDef.Methods)
+ {
+ if (!method.IsStatic)
+ {
+ continue;
+ }
+
+ switch (method.Name)
+ {
+ case nameof(ByteUnpacker.ReadValueBitPacked):
+ if (method.Parameters[1].ParameterType.FullName == typeof(short).MakeByRefType().FullName)
+ {
+ m_ByteUnpacker_ReadValueBitPacked_Short_MethodRef = m_MainModule.ImportReference(method);
+ }
+ else if (method.Parameters[1].ParameterType.FullName == typeof(ushort).MakeByRefType().FullName)
+ {
+ m_ByteUnpacker_ReadValueBitPacked_UShort_MethodRef = m_MainModule.ImportReference(method);
+ }
+ else if (method.Parameters[1].ParameterType.FullName == typeof(int).MakeByRefType().FullName)
+ {
+ m_ByteUnpacker_ReadValueBitPacked_Int_MethodRef = m_MainModule.ImportReference(method);
+ }
+ else if (method.Parameters[1].ParameterType.FullName == typeof(uint).MakeByRefType().FullName)
+ {
+ m_ByteUnpacker_ReadValueBitPacked_UInt_MethodRef = m_MainModule.ImportReference(method);
+ }
+ else if (method.Parameters[1].ParameterType.FullName == typeof(long).MakeByRefType().FullName)
+ {
+ m_ByteUnpacker_ReadValueBitPacked_Long_MethodRef = m_MainModule.ImportReference(method);
+ }
+ else if (method.Parameters[1].ParameterType.FullName == typeof(ulong).MakeByRefType().FullName)
+ {
+ m_ByteUnpacker_ReadValueBitPacked_ULong_MethodRef = m_MainModule.ImportReference(method);
+ }
+ break;
+ }
+ }
+
return true;
}
@@ -1008,6 +1141,36 @@ namespace Unity.Netcode.Editor.CodeGen
private bool GetWriteMethodForParameter(TypeReference paramType, out MethodReference methodRef)
{
+ if (paramType.FullName == typeof(short).FullName)
+ {
+ methodRef = m_BytePacker_WriteValueBitPacked_Short_MethodRef;
+ return true;
+ }
+ if (paramType.FullName == typeof(ushort).FullName)
+ {
+ methodRef = m_BytePacker_WriteValueBitPacked_UShort_MethodRef;
+ return true;
+ }
+ if (paramType.FullName == typeof(int).FullName)
+ {
+ methodRef = m_BytePacker_WriteValueBitPacked_Int_MethodRef;
+ return true;
+ }
+ if (paramType.FullName == typeof(uint).FullName)
+ {
+ methodRef = m_BytePacker_WriteValueBitPacked_UInt_MethodRef;
+ return true;
+ }
+ if (paramType.FullName == typeof(long).FullName)
+ {
+ methodRef = m_BytePacker_WriteValueBitPacked_Long_MethodRef;
+ return true;
+ }
+ if (paramType.FullName == typeof(ulong).FullName)
+ {
+ methodRef = m_BytePacker_WriteValueBitPacked_ULong_MethodRef;
+ return true;
+ }
var assemblyQualifiedName = paramType.FullName + ", " + paramType.Resolve().Module.Assembly.FullName;
var foundMethodRef = m_FastBufferWriter_WriteValue_MethodRefs.TryGetValue(assemblyQualifiedName, out methodRef);
@@ -1154,6 +1317,36 @@ namespace Unity.Netcode.Editor.CodeGen
private bool GetReadMethodForParameter(TypeReference paramType, out MethodReference methodRef)
{
+ if (paramType.FullName == typeof(short).FullName)
+ {
+ methodRef = m_ByteUnpacker_ReadValueBitPacked_Short_MethodRef;
+ return true;
+ }
+ if (paramType.FullName == typeof(ushort).FullName)
+ {
+ methodRef = m_ByteUnpacker_ReadValueBitPacked_UShort_MethodRef;
+ return true;
+ }
+ if (paramType.FullName == typeof(int).FullName)
+ {
+ methodRef = m_ByteUnpacker_ReadValueBitPacked_Int_MethodRef;
+ return true;
+ }
+ if (paramType.FullName == typeof(uint).FullName)
+ {
+ methodRef = m_ByteUnpacker_ReadValueBitPacked_UInt_MethodRef;
+ return true;
+ }
+ if (paramType.FullName == typeof(long).FullName)
+ {
+ methodRef = m_ByteUnpacker_ReadValueBitPacked_Long_MethodRef;
+ return true;
+ }
+ if (paramType.FullName == typeof(ulong).FullName)
+ {
+ methodRef = m_ByteUnpacker_ReadValueBitPacked_ULong_MethodRef;
+ return true;
+ }
var assemblyQualifiedName = paramType.FullName + ", " + paramType.Resolve().Module.Assembly.FullName;
var foundMethodRef = m_FastBufferReader_ReadValue_MethodRefs.TryGetValue(assemblyQualifiedName, out methodRef);
diff --git a/Runtime/Hashing/XXHash.meta b/Editor/Configuration.meta
similarity index 77%
rename from Runtime/Hashing/XXHash.meta
rename to Editor/Configuration.meta
index f1fa98a..1f445ad 100644
--- a/Runtime/Hashing/XXHash.meta
+++ b/Editor/Configuration.meta
@@ -1,5 +1,5 @@
fileFormatVersion: 2
-guid: 2c61e8fe9a68a486fbbc3128d233ded2
+guid: 52153943c346dd04e8712ab540ab9c22
folderAsset: yes
DefaultImporter:
externalObjects: {}
diff --git a/Editor/Configuration/NetcodeForGameObjectsSettings.cs b/Editor/Configuration/NetcodeForGameObjectsSettings.cs
new file mode 100644
index 0000000..f62b287
--- /dev/null
+++ b/Editor/Configuration/NetcodeForGameObjectsSettings.cs
@@ -0,0 +1,39 @@
+using UnityEditor;
+
+
+namespace Unity.Netcode.Editor.Configuration
+{
+ internal class NetcodeForGameObjectsSettings
+ {
+ internal const string AutoAddNetworkObjectIfNoneExists = "AutoAdd-NetworkObject-When-None-Exist";
+ internal const string InstallMultiplayerToolsTipDismissedPlayerPrefKey = "Netcode_Tip_InstallMPTools_Dismissed";
+
+ internal static int GetNetcodeInstallMultiplayerToolTips()
+ {
+ if (EditorPrefs.HasKey(InstallMultiplayerToolsTipDismissedPlayerPrefKey))
+ {
+ return EditorPrefs.GetInt(InstallMultiplayerToolsTipDismissedPlayerPrefKey);
+ }
+ return 0;
+ }
+
+ internal static void SetNetcodeInstallMultiplayerToolTips(int toolTipPrefSetting)
+ {
+ EditorPrefs.SetInt(InstallMultiplayerToolsTipDismissedPlayerPrefKey, toolTipPrefSetting);
+ }
+
+ internal static bool GetAutoAddNetworkObjectSetting()
+ {
+ if (EditorPrefs.HasKey(AutoAddNetworkObjectIfNoneExists))
+ {
+ return EditorPrefs.GetBool(AutoAddNetworkObjectIfNoneExists);
+ }
+ return false;
+ }
+
+ internal static void SetAutoAddNetworkObjectSetting(bool autoAddSetting)
+ {
+ EditorPrefs.SetBool(AutoAddNetworkObjectIfNoneExists, autoAddSetting);
+ }
+ }
+}
diff --git a/Runtime/Messaging/Messages/OrderingMessage.cs.meta b/Editor/Configuration/NetcodeForGameObjectsSettings.cs.meta
similarity index 83%
rename from Runtime/Messaging/Messages/OrderingMessage.cs.meta
rename to Editor/Configuration/NetcodeForGameObjectsSettings.cs.meta
index 3a8bc03..f1c3145 100644
--- a/Runtime/Messaging/Messages/OrderingMessage.cs.meta
+++ b/Editor/Configuration/NetcodeForGameObjectsSettings.cs.meta
@@ -1,5 +1,5 @@
fileFormatVersion: 2
-guid: 3ada9e8fd5bf94b1f9a6a21531c8a3ee
+guid: 2f9c9b10bc41a0e46ab71324dd0ac6e1
MonoImporter:
externalObjects: {}
serializedVersion: 2
diff --git a/Editor/Configuration/NetcodeSettingsProvider.cs b/Editor/Configuration/NetcodeSettingsProvider.cs
new file mode 100644
index 0000000..c0f27ad
--- /dev/null
+++ b/Editor/Configuration/NetcodeSettingsProvider.cs
@@ -0,0 +1,94 @@
+using UnityEditor;
+using UnityEngine;
+
+namespace Unity.Netcode.Editor.Configuration
+{
+ internal static class NetcodeSettingsProvider
+ {
+ [SettingsProvider]
+ public static SettingsProvider CreateNetcodeSettingsProvider()
+ {
+ // First parameter is the path in the Settings window.
+ // Second parameter is the scope of this setting: it only appears in the Settings window for the Project scope.
+ var provider = new SettingsProvider("Project/NetcodeForGameObjects", SettingsScope.Project)
+ {
+ label = "Netcode for GameObjects",
+ keywords = new[] { "netcode", "editor" },
+ guiHandler = OnGuiHandler,
+ };
+
+ return provider;
+ }
+
+ internal static NetcodeSettingsLabel NetworkObjectsSectionLabel = new NetcodeSettingsLabel("NetworkObject Helper Settings", 20);
+ internal static NetcodeSettingsToggle AutoAddNetworkObjectToggle = new NetcodeSettingsToggle("Auto-Add NetworkObjects", "When enabled, NetworkObjects are automatically added to GameObjects when NetworkBehaviours are added first.", 20);
+ internal static NetcodeSettingsLabel MultiplayerToolsLabel = new NetcodeSettingsLabel("Multiplayer Tools", 20);
+ internal static NetcodeSettingsToggle MultiplayerToolTipStatusToggle = new NetcodeSettingsToggle("Multiplayer Tools Install Reminder", "When enabled, the NetworkManager will display " +
+ "the notification to install the multiplayer tools package.", 20);
+
+ private static void OnGuiHandler(string obj)
+ {
+ var autoAddNetworkObjectSetting = NetcodeForGameObjectsSettings.GetAutoAddNetworkObjectSetting();
+ var multiplayerToolsTipStatus = NetcodeForGameObjectsSettings.GetNetcodeInstallMultiplayerToolTips() == 0;
+ EditorGUI.BeginChangeCheck();
+ NetworkObjectsSectionLabel.DrawLabel();
+ autoAddNetworkObjectSetting = AutoAddNetworkObjectToggle.DrawToggle(autoAddNetworkObjectSetting);
+ MultiplayerToolsLabel.DrawLabel();
+ multiplayerToolsTipStatus = MultiplayerToolTipStatusToggle.DrawToggle(multiplayerToolsTipStatus);
+ if (EditorGUI.EndChangeCheck())
+ {
+ NetcodeForGameObjectsSettings.SetAutoAddNetworkObjectSetting(autoAddNetworkObjectSetting);
+ NetcodeForGameObjectsSettings.SetNetcodeInstallMultiplayerToolTips(multiplayerToolsTipStatus ? 0 : 1);
+ }
+ }
+ }
+
+ internal class NetcodeSettingsLabel : NetcodeGUISettings
+ {
+ private string m_LabelContent;
+
+ public void DrawLabel()
+ {
+ EditorGUIUtility.labelWidth = m_LabelSize;
+ GUILayout.Label(m_LabelContent, EditorStyles.boldLabel, m_LayoutWidth);
+ }
+
+ public NetcodeSettingsLabel(string labelText, float layoutOffset = 0.0f)
+ {
+ m_LabelContent = labelText;
+ AdjustLableSize(labelText, layoutOffset);
+ }
+ }
+
+ internal class NetcodeSettingsToggle : NetcodeGUISettings
+ {
+ private GUIContent m_ToggleContent;
+
+ public bool DrawToggle(bool currentSetting)
+ {
+ EditorGUIUtility.labelWidth = m_LabelSize;
+ return EditorGUILayout.Toggle(m_ToggleContent, currentSetting, m_LayoutWidth);
+ }
+
+ public NetcodeSettingsToggle(string labelText, string toolTip, float layoutOffset)
+ {
+ AdjustLableSize(labelText, layoutOffset);
+ m_ToggleContent = new GUIContent(labelText, toolTip);
+ }
+ }
+
+ internal class NetcodeGUISettings
+ {
+ private const float k_MaxLabelWidth = 450f;
+ protected float m_LabelSize { get; private set; }
+
+ protected GUILayoutOption m_LayoutWidth { get; private set; }
+
+ protected void AdjustLableSize(string labelText, float offset = 0.0f)
+ {
+ m_LabelSize = Mathf.Min(k_MaxLabelWidth, EditorStyles.label.CalcSize(new GUIContent(labelText)).x);
+ m_LayoutWidth = GUILayout.Width(m_LabelSize + offset);
+ }
+ }
+
+}
diff --git a/Editor/Configuration/NetcodeSettingsProvider.cs.meta b/Editor/Configuration/NetcodeSettingsProvider.cs.meta
new file mode 100644
index 0000000..c133da2
--- /dev/null
+++ b/Editor/Configuration/NetcodeSettingsProvider.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 6b373a89fcbd41444a97ebd1798b326f
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Editor/NetworkBehaviourEditor.cs b/Editor/NetworkBehaviourEditor.cs
index 95e353a..744805d 100644
--- a/Editor/NetworkBehaviourEditor.cs
+++ b/Editor/NetworkBehaviourEditor.cs
@@ -3,9 +3,13 @@ using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEditor;
+using Unity.Netcode.Editor.Configuration;
namespace Unity.Netcode.Editor
{
+ ///
+ /// The for
+ ///
[CustomEditor(typeof(NetworkBehaviour), true)]
[CanEditMultipleObjects]
public class NetworkBehaviourEditor : UnityEditor.Editor
@@ -33,8 +37,8 @@ namespace Unity.Netcode.Editor
var ft = fields[i].FieldType;
if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(NetworkVariable<>) && !fields[i].IsDefined(typeof(HideInInspector), true))
{
- m_NetworkVariableNames.Add(fields[i].Name);
- m_NetworkVariableFields.Add(fields[i].Name, fields[i]);
+ m_NetworkVariableNames.Add(ObjectNames.NicifyVariableName(fields[i].Name));
+ m_NetworkVariableFields.Add(ObjectNames.NicifyVariableName(fields[i].Name), fields[i]);
}
}
}
@@ -230,8 +234,6 @@ namespace Unity.Netcode.Editor
CheckForNetworkObject((target as NetworkBehaviour).gameObject);
}
- internal const string AutoAddNetworkObjectIfNoneExists = "AutoAdd-NetworkObject-When-None-Exist";
-
///
/// Recursively finds the root parent of a
///
@@ -308,7 +310,7 @@ namespace Unity.Netcode.Editor
// and the user has already turned "Auto-Add NetworkObject" on when first notified about the requirement
// then just send a reminder to the user why the NetworkObject they just deleted seemingly "re-appeared"
// again.
- if (networkObjectRemoved && EditorPrefs.HasKey(AutoAddNetworkObjectIfNoneExists) && EditorPrefs.GetBool(AutoAddNetworkObjectIfNoneExists))
+ if (networkObjectRemoved && NetcodeForGameObjectsSettings.GetAutoAddNetworkObjectSetting())
{
Debug.LogWarning($"{gameObject.name} still has {nameof(NetworkBehaviour)}s and Auto-Add NetworkObjects is enabled. A NetworkObject is being added back to {gameObject.name}.");
Debug.Log($"To reset Auto-Add NetworkObjects: Select the Netcode->General->Reset Auto-Add NetworkObject menu item.");
@@ -317,7 +319,7 @@ namespace Unity.Netcode.Editor
// Notify and provide the option to add it one time, always add a NetworkObject, or do nothing and let the user manually add it
if (EditorUtility.DisplayDialog($"{nameof(NetworkBehaviour)}s require a {nameof(NetworkObject)}",
$"{gameObject.name} does not have a {nameof(NetworkObject)} component. Would you like to add one now?", "Yes", "No (manually add it)",
- DialogOptOutDecisionType.ForThisMachine, AutoAddNetworkObjectIfNoneExists))
+ DialogOptOutDecisionType.ForThisMachine, NetcodeForGameObjectsSettings.AutoAddNetworkObjectIfNoneExists))
{
gameObject.AddComponent();
var activeScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
@@ -327,20 +329,5 @@ namespace Unity.Netcode.Editor
}
}
}
-
- ///
- /// This allows users to reset the Auto-Add NetworkObject preference
- /// so the next time they add a NetworkBehaviour to a GameObject without
- /// a NetworkObject it will display the dialog box again and not
- /// automatically add a NetworkObject.
- ///
- [MenuItem("Netcode/General/Reset Auto-Add NetworkObject", false, 1)]
- private static void ResetMultiplayerToolsTipStatus()
- {
- if (EditorPrefs.HasKey(AutoAddNetworkObjectIfNoneExists))
- {
- EditorPrefs.SetBool(AutoAddNetworkObjectIfNoneExists, false);
- }
- }
}
}
diff --git a/Editor/NetworkManagerEditor.cs b/Editor/NetworkManagerEditor.cs
index debaca2..d79cd9c 100644
--- a/Editor/NetworkManagerEditor.cs
+++ b/Editor/NetworkManagerEditor.cs
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEditorInternal;
+using Unity.Netcode.Editor.Configuration;
namespace Unity.Netcode.Editor
{
@@ -14,7 +15,6 @@ namespace Unity.Netcode.Editor
[CanEditMultipleObjects]
public class NetworkManagerEditor : UnityEditor.Editor
{
- internal const string InstallMultiplayerToolsTipDismissedPlayerPrefKey = "Netcode_Tip_InstallMPTools_Dismissed";
private static GUIStyle s_CenteredWordWrappedLabelStyle;
private static GUIStyle s_HelpBoxStyle;
@@ -359,7 +359,7 @@ namespace Unity.Netcode.Editor
const string targetUrl = "https://docs-multiplayer.unity3d.com/netcode/current/tools/install-tools";
const string infoIconName = "console.infoicon";
- if (PlayerPrefs.GetInt(InstallMultiplayerToolsTipDismissedPlayerPrefKey, 0) != 0)
+ if (NetcodeForGameObjectsSettings.GetNetcodeInstallMultiplayerToolTips() != 0)
{
return;
}
@@ -405,7 +405,7 @@ namespace Unity.Netcode.Editor
GUILayout.FlexibleSpace();
if (GUILayout.Button(dismissButtonText, dismissButtonStyle, GUILayout.ExpandWidth(false)))
{
- PlayerPrefs.SetInt(InstallMultiplayerToolsTipDismissedPlayerPrefKey, 1);
+ NetcodeForGameObjectsSettings.SetNetcodeInstallMultiplayerToolTips(1);
}
EditorGUIUtility.AddCursorRect(GUILayoutUtility.GetLastRect(), MouseCursor.Link);
GUILayout.FlexibleSpace();
diff --git a/Editor/NetworkManagerHelper.cs b/Editor/NetworkManagerHelper.cs
index 4c4aabd..c7d13b0 100644
--- a/Editor/NetworkManagerHelper.cs
+++ b/Editor/NetworkManagerHelper.cs
@@ -65,7 +65,11 @@ namespace Unity.Netcode.Editor
var scenesList = EditorBuildSettings.scenes.ToList();
var activeScene = SceneManager.GetActiveScene();
var isSceneInBuildSettings = scenesList.Count((c) => c.path == activeScene.path) == 1;
+#if UNITY_2023_1_OR_NEWER
+ var networkManager = Object.FindFirstObjectByType();
+#else
var networkManager = Object.FindObjectOfType();
+#endif
if (!isSceneInBuildSettings && networkManager != null)
{
if (networkManager.NetworkConfig != null && networkManager.NetworkConfig.EnableSceneManagement)
diff --git a/Runtime/Core/NetworkBehaviour.cs b/Runtime/Core/NetworkBehaviour.cs
index 7f95289..1a43947 100644
--- a/Runtime/Core/NetworkBehaviour.cs
+++ b/Runtime/Core/NetworkBehaviour.cs
@@ -21,6 +21,7 @@ namespace Unity.Netcode
Client = 2
}
+
// NetworkBehaviourILPP will override this in derived classes to return the name of the concrete type
internal virtual string __getTypeName() => nameof(NetworkBehaviour);
@@ -286,7 +287,18 @@ namespace Unity.Netcode
/// Gets the NetworkManager that owns this NetworkBehaviour instance
/// See note around `NetworkObject` for how there is a chicken / egg problem when we are not initialized
///
- public NetworkManager NetworkManager => NetworkObject.NetworkManager;
+ public NetworkManager NetworkManager
+ {
+ get
+ {
+ if (NetworkObject?.NetworkManager != null)
+ {
+ return NetworkObject?.NetworkManager;
+ }
+
+ return NetworkManager.Singleton;
+ }
+ }
///
/// If a NetworkObject is assigned, it will return whether or not this NetworkObject
@@ -335,23 +347,29 @@ namespace Unity.Netcode
m_NetworkObject.NetworkManager.IsServer;
}
- ///
- /// Gets the NetworkObject that owns this NetworkBehaviour instance
/// TODO: this needs an overhaul. It's expensive, it's ja little naive in how it looks for networkObject in
/// its parent and worst, it creates a puzzle if you are a NetworkBehaviour wanting to see if you're live or not
/// (e.g. editor code). All you want to do is find out if NetworkManager is null, but to do that you
/// need NetworkObject, but if you try and grab NetworkObject and NetworkManager isn't up you'll get
/// the warning below. This is why IsBehaviourEditable had to be created. Matt was going to re-do
/// how NetworkObject works but it was close to the release and too risky to change
- ///
+ ///
+ /// Gets the NetworkObject that owns this NetworkBehaviour instance
///
public NetworkObject NetworkObject
{
get
{
- if (m_NetworkObject == null)
+ try
{
- m_NetworkObject = GetComponentInParent();
+ if (m_NetworkObject == null)
+ {
+ m_NetworkObject = GetComponentInParent();
+ }
+ }
+ catch (Exception)
+ {
+ return null;
}
// ShutdownInProgress check:
@@ -712,7 +730,7 @@ namespace Unity.Netcode
var tmpWriter = new FastBufferWriter(MessagingSystem.NON_FRAGMENTED_MESSAGE_MAX_SIZE, Allocator.Temp, MessagingSystem.FRAGMENTED_MESSAGE_MAX_SIZE);
using (tmpWriter)
{
- message.Serialize(tmpWriter);
+ message.Serialize(tmpWriter, message.Version);
}
}
else
@@ -745,6 +763,14 @@ namespace Unity.Netcode
}
}
+ ///
+ /// Synchronizes by setting only the NetworkVariable field values that the client has permission to read.
+ /// Note: This is only invoked when first synchronizing a NetworkBehaviour (i.e. late join or spawned NetworkObject)
+ ///
+ ///
+ /// When NetworkConfig.EnsureNetworkVariableLengthSafety is enabled each NetworkVariable field will be preceded
+ /// by the number of bytes written for that specific field.
+ ///
internal void WriteNetworkVariableData(FastBufferWriter writer, ulong targetClientId)
{
if (NetworkVariableFields.Count == 0)
@@ -754,27 +780,47 @@ namespace Unity.Netcode
for (int j = 0; j < NetworkVariableFields.Count; j++)
{
- bool canClientRead = NetworkVariableFields[j].CanClientRead(targetClientId);
- if (canClientRead)
+ if (NetworkVariableFields[j].CanClientRead(targetClientId))
{
- var writePos = writer.Position;
- writer.WriteValueSafe((ushort)0);
- var startPos = writer.Position;
- NetworkVariableFields[j].WriteField(writer);
- var size = writer.Position - startPos;
- writer.Seek(writePos);
- writer.WriteValueSafe((ushort)size);
- writer.Seek(startPos + size);
+ if (NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety)
+ {
+ var writePos = writer.Position;
+ // Note: This value can't be packed because we don't know how large it will be in advance
+ // we reserve space for it, then write the data, then come back and fill in the space
+ // to pack here, we'd have to write data to a temporary buffer and copy it in - which
+ // isn't worth possibly saving one byte if and only if the data is less than 63 bytes long...
+ // The way we do packing, any value > 63 in a ushort will use the full 2 bytes to represent.
+ writer.WriteValueSafe((ushort)0);
+ var startPos = writer.Position;
+ NetworkVariableFields[j].WriteField(writer);
+ var size = writer.Position - startPos;
+ writer.Seek(writePos);
+ writer.WriteValueSafe((ushort)size);
+ writer.Seek(startPos + size);
+ }
+ else
+ {
+ NetworkVariableFields[j].WriteField(writer);
+ }
}
- else
+ else // Only if EnsureNetworkVariableLengthSafety, otherwise just skip
+ if (NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety)
{
writer.WriteValueSafe((ushort)0);
}
}
}
- internal void SetNetworkVariableData(FastBufferReader reader)
+ ///
+ /// Synchronizes by setting only the NetworkVariable field values that the client has permission to read.
+ /// Note: This is only invoked when first synchronizing a NetworkBehaviour (i.e. late join or spawned NetworkObject)
+ ///
+ ///
+ /// When NetworkConfig.EnsureNetworkVariableLengthSafety is enabled each NetworkVariable field will be preceded
+ /// by the number of bytes written for that specific field.
+ ///
+ internal void SetNetworkVariableData(FastBufferReader reader, ulong clientId)
{
if (NetworkVariableFields.Count == 0)
{
@@ -783,13 +829,23 @@ namespace Unity.Netcode
for (int j = 0; j < NetworkVariableFields.Count; j++)
{
- reader.ReadValueSafe(out ushort varSize);
- if (varSize == 0)
+ var varSize = (ushort)0;
+ var readStartPos = 0;
+ if (NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety)
+ {
+ reader.ReadValueSafe(out varSize);
+ if (varSize == 0)
+ {
+ continue;
+ }
+ readStartPos = reader.Position;
+ }
+ else // If the client cannot read this field, then skip it
+ if (!NetworkVariableFields[j].CanClientRead(clientId))
{
continue;
}
- var readStartPos = reader.Position;
NetworkVariableFields[j].ReadField(reader);
if (NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety)
@@ -826,6 +882,138 @@ namespace Unity.Netcode
return NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(networkId, out NetworkObject networkObject) ? networkObject : null;
}
+ ///
+ /// Override this method if your derived NetworkBehaviour requires custom synchronization data.
+ /// Note: Use of this method is only for the initial client synchronization of NetworkBehaviours
+ /// and will increase the payload size for client synchronization and dynamically spawned
+ /// s.
+ ///
+ ///
+ /// When serializing (writing) this will be invoked during the client synchronization period and
+ /// when spawning new NetworkObjects.
+ /// When deserializing (reading), this will be invoked prior to the NetworkBehaviour's associated
+ /// NetworkObject being spawned.
+ ///
+ /// The serializer to use to read and write the data.
+ ///
+ /// Either BufferSerializerReader or BufferSerializerWriter, depending whether the serializer
+ /// is in read mode or write mode.
+ ///
+ protected virtual void OnSynchronize(ref BufferSerializer serializer) where T : IReaderWriter
+ {
+
+ }
+
+ ///
+ /// Internal method that determines if a NetworkBehaviour has additional synchronization data to
+ /// be synchronized when first instantiated prior to its associated NetworkObject being spawned.
+ ///
+ ///
+ /// This includes try-catch blocks to recover from exceptions that might occur and continue to
+ /// synchronize any remaining NetworkBehaviours.
+ ///
+ /// true if it wrote synchronization data and false if it did not
+ internal bool Synchronize(ref BufferSerializer serializer) where T : IReaderWriter
+ {
+ if (serializer.IsWriter)
+ {
+ // Get the writer to handle seeking and determining how many bytes were written
+ var writer = serializer.GetFastBufferWriter();
+ // Save our position before we attempt to write anything so we can seek back to it (i.e. error occurs)
+ var positionBeforeWrite = writer.Position;
+ writer.WriteValueSafe(NetworkBehaviourId);
+
+ // Save our position where we will write the final size being written so we can skip over it in the
+ // event an exception occurs when deserializing.
+ var sizePosition = writer.Position;
+ writer.WriteValueSafe((ushort)0);
+
+ // Save our position before synchronizing to determine how much was written
+ var positionBeforeSynchronize = writer.Position;
+ var threwException = false;
+ try
+ {
+ OnSynchronize(ref serializer);
+ }
+ catch (Exception ex)
+ {
+ threwException = true;
+ if (NetworkManager.LogLevel <= LogLevel.Normal)
+ {
+ NetworkLog.LogWarning($"{name} threw an exception during synchronization serialization, this {nameof(NetworkBehaviour)} is being skipped and will not be synchronized!");
+ if (NetworkManager.LogLevel == LogLevel.Developer)
+ {
+ NetworkLog.LogError($"{ex.Message}\n {ex.StackTrace}");
+ }
+ }
+ }
+ var finalPosition = writer.Position;
+
+ // If we wrote nothing then skip writing anything for this NetworkBehaviour
+ if (finalPosition == positionBeforeSynchronize || threwException)
+ {
+ writer.Seek(positionBeforeWrite);
+ return false;
+ }
+ else
+ {
+ // Write the number of bytes serialized to handle exceptions on the deserialization side
+ var bytesWritten = finalPosition - positionBeforeSynchronize;
+ writer.Seek(sizePosition);
+ writer.WriteValueSafe((ushort)bytesWritten);
+ writer.Seek(finalPosition);
+ }
+ return true;
+ }
+ else
+ {
+ var reader = serializer.GetFastBufferReader();
+ // We will always read the expected byte count
+ reader.ReadValueSafe(out ushort expectedBytesToRead);
+
+ // Save our position before we begin synchronization deserialization
+ var positionBeforeSynchronize = reader.Position;
+ var synchronizationError = false;
+ try
+ {
+ // Invoke synchronization
+ OnSynchronize(ref serializer);
+ }
+ catch (Exception ex)
+ {
+ if (NetworkManager.LogLevel <= LogLevel.Normal)
+ {
+ NetworkLog.LogWarning($"{name} threw an exception during synchronization deserialization, this {nameof(NetworkBehaviour)} is being skipped and will not be synchronized!");
+ if (NetworkManager.LogLevel == LogLevel.Developer)
+ {
+ NetworkLog.LogError($"{ex.Message}\n {ex.StackTrace}");
+ }
+ }
+ synchronizationError = true;
+ }
+
+ var totalBytesRead = reader.Position - positionBeforeSynchronize;
+ if (totalBytesRead != expectedBytesToRead)
+ {
+ if (NetworkManager.LogLevel <= LogLevel.Normal)
+ {
+ NetworkLog.LogWarning($"{name} read {totalBytesRead} bytes but was expected to read {expectedBytesToRead} bytes during synchronization deserialization! This {nameof(NetworkBehaviour)} is being skipped and will not be synchronized!");
+ }
+ synchronizationError = true;
+ }
+
+ // Skip over the entry if deserialization fails
+ if (synchronizationError)
+ {
+ var skipToPosition = positionBeforeSynchronize + expectedBytesToRead;
+ reader.Seek(skipToPosition);
+ return false;
+ }
+ return true;
+ }
+ }
+
+
///
/// Invoked when the the is attached to.
/// NOTE: If you override this, you will want to always invoke this base class version of this
diff --git a/Runtime/Core/NetworkManager.cs b/Runtime/Core/NetworkManager.cs
index 20794b4..a88d353 100644
--- a/Runtime/Core/NetworkManager.cs
+++ b/Runtime/Core/NetworkManager.cs
@@ -86,6 +86,12 @@ namespace Unity.Netcode
private bool m_ShuttingDown;
private bool m_StopProcessingMessages;
+ ///
+ /// When disconnected from the server, the server may send a reason. If a reason was sent, this property will
+ /// tell client code what the reason was. It should be queried after the OnClientDisconnectCallback is called
+ ///
+ public string DisconnectReason { get; internal set; }
+
private class NetworkManagerHooks : INetworkHooks
{
private NetworkManager m_NetworkManager;
@@ -443,6 +449,12 @@ namespace Unity.Netcode
/// If the Approval decision cannot be made immediately, the client code can set Pending to true, keep a reference to the ConnectionApprovalResponse object and write to it later. Client code must exercise care to setting all the members to the value it wants before marking Pending to false, to indicate completion. If the field is set as Pending = true, we'll monitor the object until it gets set to not pending anymore and use the parameters then.
///
public bool Pending;
+
+ ///
+ /// Optional reason. If Approved is false, this reason will be sent to the client so they know why they
+ /// were not approved.
+ ///
+ public string Reason;
}
///
@@ -889,6 +901,7 @@ namespace Unity.Netcode
return;
}
+ DisconnectReason = string.Empty;
IsApproved = false;
ComponentFactory.SetDefaults();
@@ -1181,6 +1194,11 @@ namespace Unity.Netcode
SpawnManager.ServerSpawnSceneObjectsOnStartSweep();
+ // This assures that any in-scene placed NetworkObject is spawned and
+ // any associated NetworkBehaviours' netcode related properties are
+ // set prior to invoking OnClientConnected.
+ InvokeOnClientConnectedCallback(LocalClientId);
+
OnServerStarted?.Invoke();
return true;
@@ -1341,6 +1359,7 @@ namespace Unity.Netcode
private void DisconnectRemoteClient(ulong clientId)
{
var transportId = ClientIdToTransportId(clientId);
+ MessagingSystem.ProcessSendQueues();
NetworkConfig.NetworkTransport.DisconnectRemoteClient(transportId);
}
@@ -1421,7 +1440,7 @@ namespace Unity.Netcode
}
}
- if (IsClient && IsConnectedClient)
+ if (IsClient && IsListening)
{
// Client only, send disconnect to server
NetworkConfig.NetworkTransport.DisconnectLocalClient();
@@ -1579,6 +1598,7 @@ namespace Unity.Netcode
} while (IsListening && networkEvent != NetworkEvent.Nothing);
MessagingSystem.ProcessIncomingMessageQueue();
+ MessagingSystem.CleanupDisconnectedClients();
#if DEVELOPMENT_BUILD || UNITY_EDITOR
s_TransportPoll.End();
@@ -1660,7 +1680,23 @@ namespace Unity.Netcode
ShouldSendConnectionData = NetworkConfig.ConnectionApproval,
ConnectionData = NetworkConfig.ConnectionData
};
+
+ message.MessageVersions = new NativeArray(MessagingSystem.MessageHandlers.Length, Allocator.Temp);
+ for (int index = 0; index < MessagingSystem.MessageHandlers.Length; index++)
+ {
+ if (MessagingSystem.MessageTypes[index] != null)
+ {
+ var type = MessagingSystem.MessageTypes[index];
+ message.MessageVersions[index] = new MessageVersionData
+ {
+ Hash = XXHash.Hash32(type.FullName),
+ Version = MessagingSystem.GetLocalVersion(type)
+ };
+ }
+ }
+
SendMessage(ref message, NetworkDelivery.ReliableSequenced, ServerClientId);
+ message.MessageVersions.Dispose();
}
private IEnumerator ApprovalTimeout(ulong clientId)
@@ -1821,7 +1857,18 @@ namespace Unity.Netcode
NetworkLog.LogInfo($"Disconnect Event From {clientId}");
}
- OnClientDisconnectCallback?.Invoke(clientId);
+ // Process the incoming message queue so that we get everything from the server disconnecting us
+ // or, if we are the server, so we got everything from that client.
+ MessagingSystem.ProcessIncomingMessageQueue();
+
+ try
+ {
+ OnClientDisconnectCallback?.Invoke(clientId);
+ }
+ catch (Exception exception)
+ {
+ Debug.LogException(exception);
+ }
if (IsServer)
{
@@ -1987,12 +2034,31 @@ namespace Unity.Netcode
///
/// The ClientId to disconnect
public void DisconnectClient(ulong clientId)
+ {
+ DisconnectClient(clientId, null);
+ }
+
+ ///
+ /// Disconnects the remote client.
+ ///
+ /// The ClientId to disconnect
+ /// Disconnection reason. If set, client will receive a DisconnectReasonMessage and have the
+ /// reason available in the NetworkManager.DisconnectReason property
+ public void DisconnectClient(ulong clientId, string reason)
{
if (!IsServer)
{
throw new NotServerException($"Only server can disconnect remote clients. Please use `{nameof(Shutdown)}()` instead.");
}
+ if (!string.IsNullOrEmpty(reason))
+ {
+ var disconnectReason = new DisconnectReasonMessage();
+ disconnectReason.Reason = reason;
+ SendMessage(ref disconnectReason, NetworkDelivery.Reliable, clientId);
+ }
+ MessagingSystem.ProcessSendQueues();
+
OnClientDisconnectFromServer(clientId);
DisconnectRemoteClient(clientId);
}
@@ -2137,14 +2203,11 @@ namespace Unity.Netcode
// Generate a SceneObject for the player object to spawn
var sceneObject = new NetworkObject.SceneObject
{
- Header = new NetworkObject.SceneObject.HeaderData
- {
- IsPlayerObject = true,
- OwnerClientId = ownerClientId,
- IsSceneObject = false,
- HasTransform = true,
- Hash = playerPrefabHash,
- },
+ OwnerClientId = ownerClientId,
+ IsPlayerObject = true,
+ IsSceneObject = false,
+ HasTransform = true,
+ Hash = playerPrefabHash,
TargetClientId = ownerClientId,
Transform = new NetworkObject.SceneObject.TransformData
{
@@ -2184,22 +2247,23 @@ namespace Unity.Netcode
}
}
- SendMessage(ref message, NetworkDelivery.ReliableFragmentedSequenced, ownerClientId);
-
+ message.MessageVersions = new NativeArray(MessagingSystem.MessageHandlers.Length, Allocator.Temp);
for (int index = 0; index < MessagingSystem.MessageHandlers.Length; index++)
{
if (MessagingSystem.MessageTypes[index] != null)
{
- var orderingMessage = new OrderingMessage
+ var type = MessagingSystem.MessageTypes[index];
+ message.MessageVersions[index] = new MessageVersionData
{
- Order = index,
- Hash = XXHash.Hash32(MessagingSystem.MessageTypes[index].FullName)
+ Hash = XXHash.Hash32(type.FullName),
+ Version = MessagingSystem.GetLocalVersion(type)
};
-
- SendMessage(ref orderingMessage, NetworkDelivery.ReliableFragmentedSequenced, ownerClientId);
}
}
+ SendMessage(ref message, NetworkDelivery.ReliableFragmentedSequenced, ownerClientId);
+ message.MessageVersions.Dispose();
+
// If scene management is enabled, then let NetworkSceneManager handle the initial scene and NetworkObject synchronization
if (!NetworkConfig.EnableSceneManagement)
{
@@ -2214,7 +2278,6 @@ namespace Unity.Netcode
{
LocalClient = client;
SpawnManager.UpdateObservedNetworkObjects(ownerClientId);
- InvokeOnClientConnectedCallback(ownerClientId);
}
if (!response.CreatePlayerObject || (response.PlayerPrefabHash == null && NetworkConfig.PlayerPrefab == null))
@@ -2227,6 +2290,15 @@ namespace Unity.Netcode
}
else
{
+ if (!string.IsNullOrEmpty(response.Reason))
+ {
+ var disconnectReason = new DisconnectReasonMessage();
+ disconnectReason.Reason = response.Reason;
+ SendMessage(ref disconnectReason, NetworkDelivery.Reliable, ownerClientId);
+
+ MessagingSystem.ProcessSendQueues();
+ }
+
PendingClients.Remove(ownerClientId);
DisconnectRemoteClient(ownerClientId);
}
@@ -2253,11 +2325,11 @@ namespace Unity.Netcode
{
ObjectInfo = ConnectedClients[clientId].PlayerObject.GetMessageSceneObject(clientPair.Key)
};
- message.ObjectInfo.Header.Hash = playerPrefabHash;
- message.ObjectInfo.Header.IsSceneObject = false;
- message.ObjectInfo.Header.HasParent = false;
- message.ObjectInfo.Header.IsPlayerObject = true;
- message.ObjectInfo.Header.OwnerClientId = clientId;
+ message.ObjectInfo.Hash = playerPrefabHash;
+ message.ObjectInfo.IsSceneObject = false;
+ message.ObjectInfo.HasParent = false;
+ message.ObjectInfo.IsPlayerObject = true;
+ message.ObjectInfo.OwnerClientId = clientId;
var size = SendMessage(ref message, NetworkDelivery.ReliableFragmentedSequenced, clientPair.Key);
NetworkMetrics.TrackObjectSpawnSent(clientPair.Key, ConnectedClients[clientId].PlayerObject, size);
}
diff --git a/Runtime/Core/NetworkObject.cs b/Runtime/Core/NetworkObject.cs
index 9d956f7..122b6f1 100644
--- a/Runtime/Core/NetworkObject.cs
+++ b/Runtime/Core/NetworkObject.cs
@@ -830,6 +830,9 @@ namespace Unity.Netcode
// parent and then re-parents the child under a GameObject with a NetworkObject component attached.
if (parentNetworkObject == null)
{
+ // If we are parented under a GameObject, go ahead and mark the world position stays as false
+ // so clients synchronize their transform in local space. (only for in-scene placed NetworkObjects)
+ m_CachedWorldPositionStays = false;
return true;
}
else // If the parent still isn't spawned add this to the orphaned children and return false
@@ -841,8 +844,11 @@ namespace Unity.Netcode
else
{
// If we made it this far, go ahead and set the network parenting values
- // with the default WorldPoisitonSays value
- SetNetworkParenting(parentNetworkObject.NetworkObjectId, true);
+ // with the WorldPoisitonSays value set to false
+ // Note: Since in-scene placed NetworkObjects are parented in the scene
+ // the default "assumption" is that children are parenting local space
+ // relative.
+ SetNetworkParenting(parentNetworkObject.NetworkObjectId, false);
// Set the cached parent
m_CachedParent = parentNetworkObject.transform;
@@ -1002,13 +1008,17 @@ namespace Unity.Netcode
}
}
}
- internal void SetNetworkVariableData(FastBufferReader reader)
+
+ ///
+ /// Only invoked during first synchronization of a NetworkObject (late join or newly spawned)
+ ///
+ internal void SetNetworkVariableData(FastBufferReader reader, ulong clientId)
{
for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
{
var behaviour = ChildNetworkBehaviours[i];
behaviour.InitializeVariables();
- behaviour.SetNetworkVariableData(reader);
+ behaviour.SetNetworkVariableData(reader, clientId);
}
}
@@ -1045,7 +1055,20 @@ namespace Unity.Netcode
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Error)
{
- NetworkLog.LogError($"Behaviour index was out of bounds. Did you mess up the order of your {nameof(NetworkBehaviour)}s?");
+ NetworkLog.LogError($"{nameof(NetworkBehaviour)} index {index} was out of bounds for {name}. NetworkBehaviours must be the same, and in the same order, between server and client.");
+ }
+
+ if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
+ {
+ var currentKnownChildren = new System.Text.StringBuilder();
+ currentKnownChildren.Append($"Known child {nameof(NetworkBehaviour)}s:");
+ for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
+ {
+ var childNetworkBehaviour = ChildNetworkBehaviours[i];
+ currentKnownChildren.Append($" [{i}] {childNetworkBehaviour.__getTypeName()}");
+ currentKnownChildren.Append(i < ChildNetworkBehaviours.Count - 1 ? "," : ".");
+ }
+ NetworkLog.LogInfo(currentKnownChildren.ToString());
}
return null;
@@ -1056,19 +1079,43 @@ namespace Unity.Netcode
internal struct SceneObject
{
- public struct HeaderData : INetworkSerializeByMemcpy
- {
- public ulong NetworkObjectId;
- public ulong OwnerClientId;
- public uint Hash;
+ private byte m_BitField;
+ public uint Hash;
+ public ulong NetworkObjectId;
+ public ulong OwnerClientId;
- public bool IsPlayerObject;
- public bool HasParent;
- public bool IsSceneObject;
- public bool HasTransform;
+ public bool IsPlayerObject
+ {
+ get => ByteUtility.GetBit(m_BitField, 0);
+ set => ByteUtility.SetBit(ref m_BitField, 0, value);
+ }
+ public bool HasParent
+ {
+ get => ByteUtility.GetBit(m_BitField, 1);
+ set => ByteUtility.SetBit(ref m_BitField, 1, value);
+ }
+ public bool IsSceneObject
+ {
+ get => ByteUtility.GetBit(m_BitField, 2);
+ set => ByteUtility.SetBit(ref m_BitField, 2, value);
+ }
+ public bool HasTransform
+ {
+ get => ByteUtility.GetBit(m_BitField, 3);
+ set => ByteUtility.SetBit(ref m_BitField, 3, value);
}
- public HeaderData Header;
+ public bool IsLatestParentSet
+ {
+ get => ByteUtility.GetBit(m_BitField, 4);
+ set => ByteUtility.SetBit(ref m_BitField, 4, value);
+ }
+
+ public bool WorldPositionStays
+ {
+ get => ByteUtility.GetBit(m_BitField, 5);
+ set => ByteUtility.SetBit(ref m_BitField, 5, value);
+ }
//If(Metadata.HasParent)
public ulong ParentObjectId;
@@ -1084,7 +1131,6 @@ namespace Unity.Netcode
public TransformData Transform;
//If(Metadata.IsReparented)
- public bool IsLatestParentSet;
//If(IsLatestParentSet)
public ulong? LatestParent;
@@ -1094,114 +1140,90 @@ namespace Unity.Netcode
public int NetworkSceneHandle;
- public bool WorldPositionStays;
- public unsafe void Serialize(FastBufferWriter writer)
+ public void Serialize(FastBufferWriter writer)
{
- var writeSize = sizeof(HeaderData);
- if (Header.HasParent)
+ writer.WriteValueSafe(m_BitField);
+ writer.WriteValueSafe(Hash);
+ BytePacker.WriteValueBitPacked(writer, NetworkObjectId);
+ BytePacker.WriteValueBitPacked(writer, OwnerClientId);
+
+ if (HasParent)
{
- writeSize += FastBufferWriter.GetWriteSize(ParentObjectId);
- writeSize += FastBufferWriter.GetWriteSize(WorldPositionStays);
- writeSize += FastBufferWriter.GetWriteSize(IsLatestParentSet);
- writeSize += IsLatestParentSet ? FastBufferWriter.GetWriteSize() : 0;
+ BytePacker.WriteValueBitPacked(writer, ParentObjectId);
+ if (IsLatestParentSet)
+ {
+ BytePacker.WriteValueBitPacked(writer, LatestParent.Value);
+ }
}
- writeSize += Header.HasTransform ? FastBufferWriter.GetWriteSize() : 0;
- writeSize += Header.IsSceneObject ? FastBufferWriter.GetWriteSize() : 0;
+
+ var writeSize = 0;
+ writeSize += HasTransform ? FastBufferWriter.GetWriteSize() : 0;
+ writeSize += IsSceneObject ? FastBufferWriter.GetWriteSize() : 0;
if (!writer.TryBeginWrite(writeSize))
{
throw new OverflowException("Could not serialize SceneObject: Out of buffer space.");
}
- writer.WriteValue(Header);
-
- if (Header.HasParent)
- {
- writer.WriteValue(ParentObjectId);
- writer.WriteValue(WorldPositionStays);
- writer.WriteValue(IsLatestParentSet);
- if (IsLatestParentSet)
- {
- writer.WriteValue(LatestParent.Value);
- }
- }
-
- if (Header.HasTransform)
+ if (HasTransform)
{
writer.WriteValue(Transform);
}
// In-Scene NetworkObjects are uniquely identified NetworkPrefabs defined by their
- // NetworkSceneHandle and GlobalObjectIdHash. Since each loaded scene has a unique
- // handle, it provides us with a unique and persistent "scene prefab asset" instance.
- // This is only set on in-scene placed NetworkObjects to reduce the over-all packet
- // sizes for dynamically spawned NetworkObjects.
- if (Header.IsSceneObject)
+ // NetworkSceneHandle and GlobalObjectIdHash. Client-side NetworkSceneManagers use
+ // this to locate their local instance of the in-scene placed NetworkObject instance.
+ // Only written for in-scene placed NetworkObjects.
+ if (IsSceneObject)
{
writer.WriteValue(OwnerObject.GetSceneOriginHandle());
}
- OwnerObject.WriteNetworkVariableData(writer, TargetClientId);
+ // Synchronize NetworkVariables and NetworkBehaviours
+ var bufferSerializer = new BufferSerializer(new BufferSerializerWriter(writer));
+ OwnerObject.SynchronizeNetworkBehaviours(ref bufferSerializer, TargetClientId);
}
- public unsafe void Deserialize(FastBufferReader reader)
+ public void Deserialize(FastBufferReader reader)
{
- if (!reader.TryBeginRead(sizeof(HeaderData)))
+ reader.ReadValueSafe(out m_BitField);
+ reader.ReadValueSafe(out Hash);
+ ByteUnpacker.ReadValueBitPacked(reader, out NetworkObjectId);
+ ByteUnpacker.ReadValueBitPacked(reader, out OwnerClientId);
+
+ if (HasParent)
{
- throw new OverflowException("Could not deserialize SceneObject: Out of buffer space.");
- }
- reader.ReadValue(out Header);
- var readSize = 0;
- if (Header.HasParent)
- {
- readSize += FastBufferWriter.GetWriteSize(ParentObjectId);
- readSize += FastBufferWriter.GetWriteSize(WorldPositionStays);
- readSize += FastBufferWriter.GetWriteSize(IsLatestParentSet);
- // We need to read at this point in order to get the IsLatestParentSet value
- if (!reader.TryBeginRead(readSize))
+ ByteUnpacker.ReadValueBitPacked(reader, out ParentObjectId);
+ if (IsLatestParentSet)
{
- throw new OverflowException("Could not deserialize SceneObject: Out of buffer space.");
+ ByteUnpacker.ReadValueBitPacked(reader, out ulong latestParent);
+ LatestParent = latestParent;
}
-
- // Read the initial parenting related properties
- reader.ReadValue(out ParentObjectId);
- reader.ReadValue(out WorldPositionStays);
- reader.ReadValue(out IsLatestParentSet);
-
- // Now calculate the remaining bytes to read
- readSize = 0;
- readSize += IsLatestParentSet ? FastBufferWriter.GetWriteSize() : 0;
}
- readSize += Header.HasTransform ? FastBufferWriter.GetWriteSize() : 0;
- readSize += Header.IsSceneObject ? FastBufferWriter.GetWriteSize() : 0;
+ var readSize = 0;
+ readSize += HasTransform ? FastBufferWriter.GetWriteSize() : 0;
+ readSize += IsSceneObject ? FastBufferWriter.GetWriteSize() : 0;
// Try to begin reading the remaining bytes
if (!reader.TryBeginRead(readSize))
{
- throw new OverflowException("Could not deserialize SceneObject: Out of buffer space.");
+ throw new OverflowException("Could not deserialize SceneObject: Reading past the end of the buffer");
}
- if (IsLatestParentSet)
- {
- reader.ReadValueSafe(out ulong latestParent);
- LatestParent = latestParent;
- }
-
- if (Header.HasTransform)
+ if (HasTransform)
{
reader.ReadValue(out Transform);
}
// In-Scene NetworkObjects are uniquely identified NetworkPrefabs defined by their
- // NetworkSceneHandle and GlobalObjectIdHash. Since each loaded scene has a unique
- // handle, it provides us with a unique and persistent "scene prefab asset" instance.
- // Client-side NetworkSceneManagers use this to locate their local instance of the
- // NetworkObject instance.
- if (Header.IsSceneObject)
+ // NetworkSceneHandle and GlobalObjectIdHash. Client-side NetworkSceneManagers use
+ // this to locate their local instance of the in-scene placed NetworkObject instance.
+ // Only read for in-scene placed NetworkObjects
+ if (IsSceneObject)
{
- reader.ReadValueSafe(out NetworkSceneHandle);
+ reader.ReadValue(out NetworkSceneHandle);
}
}
}
@@ -1214,18 +1236,87 @@ namespace Unity.Netcode
}
}
+ ///
+ /// Handles synchronizing NetworkVariables and custom synchronization data for NetworkBehaviours.
+ ///
+ ///
+ /// This is where we determine how much data is written after the associated NetworkObject in order to recover
+ /// from a failed instantiated NetworkObject without completely disrupting client synchronization.
+ ///
+ internal void SynchronizeNetworkBehaviours(ref BufferSerializer serializer, ulong targetClientId = 0) where T : IReaderWriter
+ {
+ if (serializer.IsWriter)
+ {
+ var writer = serializer.GetFastBufferWriter();
+ var positionBeforeSynchronizing = writer.Position;
+ writer.WriteValueSafe((ushort)0);
+ var sizeToSkipCalculationPosition = writer.Position;
+
+ // Synchronize NetworkVariables
+ WriteNetworkVariableData(writer, targetClientId);
+ // Reserve the NetworkBehaviour synchronization count position
+ var networkBehaviourCountPosition = writer.Position;
+ writer.WriteValueSafe((byte)0);
+
+ // Parse through all NetworkBehaviours and any that return true
+ // had additional synchronization data written.
+ // (See notes for reading/deserialization below)
+ var synchronizationCount = (byte)0;
+ foreach (var childBehaviour in ChildNetworkBehaviours)
+ {
+ if (childBehaviour.Synchronize(ref serializer))
+ {
+ synchronizationCount++;
+ }
+ }
+
+ var currentPosition = writer.Position;
+ // Write the total number of bytes written for NetworkVariable and NetworkBehaviour
+ // synchronization.
+ writer.Seek(positionBeforeSynchronizing);
+ // We want the size of everything after our size to skip calculation position
+ var size = (ushort)(currentPosition - sizeToSkipCalculationPosition);
+ writer.WriteValueSafe(size);
+ // Write the number of NetworkBehaviours synchronized
+ writer.Seek(networkBehaviourCountPosition);
+ writer.WriteValueSafe(synchronizationCount);
+ // seek back to the position after writing NetworkVariable and NetworkBehaviour
+ // synchronization data.
+ writer.Seek(currentPosition);
+ }
+ else
+ {
+ var reader = serializer.GetFastBufferReader();
+
+ reader.ReadValueSafe(out ushort sizeOfSynchronizationData);
+ var seekToEndOfSynchData = reader.Position + sizeOfSynchronizationData;
+ // Apply the network variable synchronization data
+ SetNetworkVariableData(reader, targetClientId);
+ // Read the number of NetworkBehaviours to synchronize
+ reader.ReadValueSafe(out byte numberSynchronized);
+ var networkBehaviourId = (ushort)0;
+
+ // If a NetworkBehaviour writes synchronization data, it will first
+ // write its NetworkBehaviourId so when deserializing the client-side
+ // can find the right NetworkBehaviour to deserialize the synchronization data.
+ for (int i = 0; i < numberSynchronized; i++)
+ {
+ serializer.SerializeValue(ref networkBehaviourId);
+ var networkBehaviour = GetNetworkBehaviourAtOrderIndex(networkBehaviourId);
+ networkBehaviour.Synchronize(ref serializer);
+ }
+ }
+ }
+
internal SceneObject GetMessageSceneObject(ulong targetClientId)
{
var obj = new SceneObject
{
- Header = new SceneObject.HeaderData
- {
- IsPlayerObject = IsPlayerObject,
- NetworkObjectId = NetworkObjectId,
- OwnerClientId = OwnerClientId,
- IsSceneObject = IsSceneObject ?? true,
- Hash = HostCheckForGlobalObjectIdHashOverride(),
- },
+ NetworkObjectId = NetworkObjectId,
+ OwnerClientId = OwnerClientId,
+ IsPlayerObject = IsPlayerObject,
+ IsSceneObject = IsSceneObject ?? true,
+ Hash = HostCheckForGlobalObjectIdHashOverride(),
OwnerObject = this,
TargetClientId = targetClientId
};
@@ -1235,11 +1326,18 @@ namespace Unity.Netcode
if (!AlwaysReplicateAsRoot && transform.parent != null)
{
parentNetworkObject = transform.parent.GetComponent();
+ // In-scene placed NetworkObjects parented under GameObjects with no NetworkObject
+ // should set the has parent flag and preserve the world position stays value
+ if (parentNetworkObject == null && obj.IsSceneObject)
+ {
+ obj.HasParent = true;
+ obj.WorldPositionStays = m_CachedWorldPositionStays;
+ }
}
if (parentNetworkObject != null)
{
- obj.Header.HasParent = true;
+ obj.HasParent = true;
obj.ParentObjectId = parentNetworkObject.NetworkObjectId;
obj.WorldPositionStays = m_CachedWorldPositionStays;
var latestParent = GetNetworkParenting();
@@ -1253,20 +1351,38 @@ namespace Unity.Netcode
if (IncludeTransformWhenSpawning == null || IncludeTransformWhenSpawning(OwnerClientId))
{
- obj.Header.HasTransform = true;
+ obj.HasTransform = true;
+
+ // We start with the default AutoObjectParentSync values to determine which transform space we will
+ // be synchronizing clients with.
+ var syncRotationPositionLocalSpaceRelative = obj.HasParent && !m_CachedWorldPositionStays;
+ var syncScaleLocalSpaceRelative = obj.HasParent && !m_CachedWorldPositionStays;
+
+ // If auto object synchronization is turned off
+ if (!AutoObjectParentSync)
+ {
+ // We always synchronize position and rotation world space relative
+ syncRotationPositionLocalSpaceRelative = false;
+ // Scale is special, it synchronizes local space relative if it has a
+ // parent since applying the world space scale under a parent with scale
+ // will result in the improper scale for the child
+ syncScaleLocalSpaceRelative = obj.HasParent;
+ }
+
+
obj.Transform = new SceneObject.TransformData
{
// If we are parented and we have the m_CachedWorldPositionStays disabled, then use local space
// values as opposed world space values.
- Position = parentNetworkObject && !m_CachedWorldPositionStays ? transform.localPosition : transform.position,
- Rotation = parentNetworkObject && !m_CachedWorldPositionStays ? transform.localRotation : transform.rotation,
+ Position = syncRotationPositionLocalSpaceRelative ? transform.localPosition : transform.position,
+ Rotation = syncRotationPositionLocalSpaceRelative ? transform.localRotation : transform.rotation,
// We only use the lossyScale if the NetworkObject has a parent. Multi-generation nested children scales can
// impact the final scale of the child NetworkObject in question. The solution is to use the lossy scale
// which can be thought of as "world space scale".
// More information:
// https://docs.unity3d.com/ScriptReference/Transform-lossyScale.html
- Scale = parentNetworkObject && !m_CachedWorldPositionStays ? transform.localScale : transform.lossyScale,
+ Scale = syncScaleLocalSpaceRelative ? transform.localScale : transform.lossyScale,
};
}
@@ -1278,10 +1394,10 @@ namespace Unity.Netcode
/// when the client is approved or during a scene transition
///
/// Deserialized scene object data
- /// reader for the NetworkVariable data
+ /// FastBufferReader for the NetworkVariable data
/// NetworkManager instance
/// optional to use NetworkObject deserialized
- internal static NetworkObject AddSceneObject(in SceneObject sceneObject, FastBufferReader variableData, NetworkManager networkManager)
+ internal static NetworkObject AddSceneObject(in SceneObject sceneObject, FastBufferReader reader, NetworkManager networkManager)
{
//Attempt to create a local NetworkObject
var networkObject = networkManager.SpawnManager.CreateLocalNetworkObject(sceneObject);
@@ -1289,18 +1405,36 @@ namespace Unity.Netcode
if (networkObject == null)
{
// Log the error that the NetworkObject failed to construct
- Debug.LogError($"Failed to spawn {nameof(NetworkObject)} for Hash {sceneObject.Header.Hash}.");
+ if (networkManager.LogLevel <= LogLevel.Normal)
+ {
+ NetworkLog.LogError($"Failed to spawn {nameof(NetworkObject)} for Hash {sceneObject.Hash}.");
+ }
- // If we failed to load this NetworkObject, then skip past the network variable data
- variableData.ReadValueSafe(out ushort varSize);
- variableData.Seek(variableData.Position + varSize);
+ try
+ {
+ // If we failed to load this NetworkObject, then skip past the Network Variable and (if any) synchronization data
+ reader.ReadValueSafe(out ushort networkBehaviourSynchronizationDataLength);
+ reader.Seek(reader.Position + networkBehaviourSynchronizationDataLength);
+ }
+ catch (Exception ex)
+ {
+ Debug.LogException(ex);
+ }
// We have nothing left to do here.
return null;
}
+ // This will get set again when the NetworkObject is spawned locally, but we set it here ahead of spawning
+ // in order to be able to determine which NetworkVariables the client will be allowed to read.
+ networkObject.OwnerClientId = sceneObject.OwnerClientId;
+
+ // Synchronize NetworkBehaviours
+ var bufferSerializer = new BufferSerializer(new BufferSerializerReader(reader));
+ networkObject.SynchronizeNetworkBehaviours(ref bufferSerializer, networkManager.LocalClientId);
+
// Spawn the NetworkObject
- networkManager.SpawnManager.SpawnNetworkObjectLocally(networkObject, sceneObject, variableData, false);
+ networkManager.SpawnManager.SpawnNetworkObjectLocally(networkObject, sceneObject, false);
return networkObject;
}
diff --git a/Runtime/Hashing/XXHash.cs b/Runtime/Hashing/XXHash.cs
new file mode 100644
index 0000000..615736c
--- /dev/null
+++ b/Runtime/Hashing/XXHash.cs
@@ -0,0 +1,248 @@
+using System;
+using System.Text;
+using System.Runtime.CompilerServices;
+
+namespace Unity.Netcode
+{
+ internal static class XXHash
+ {
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static unsafe uint Hash32(byte* input, int length, uint seed = 0)
+ {
+ unchecked
+ {
+ const uint prime1 = 2654435761u;
+ const uint prime2 = 2246822519u;
+ const uint prime3 = 3266489917u;
+ const uint prime4 = 0668265263u;
+ const uint prime5 = 0374761393u;
+
+ uint hash = seed + prime5;
+
+ if (length >= 16)
+ {
+ uint val0 = seed + prime1 + prime2;
+ uint val1 = seed + prime2;
+ uint val2 = seed + 0;
+ uint val3 = seed - prime1;
+
+ int count = length >> 4;
+ for (int i = 0; i < count; i++)
+ {
+ var pos0 = *(uint*)(input + 0);
+ var pos1 = *(uint*)(input + 4);
+ var pos2 = *(uint*)(input + 8);
+ var pos3 = *(uint*)(input + 12);
+
+ val0 += pos0 * prime2;
+ val0 = (val0 << 13) | (val0 >> (32 - 13));
+ val0 *= prime1;
+
+ val1 += pos1 * prime2;
+ val1 = (val1 << 13) | (val1 >> (32 - 13));
+ val1 *= prime1;
+
+ val2 += pos2 * prime2;
+ val2 = (val2 << 13) | (val2 >> (32 - 13));
+ val2 *= prime1;
+
+ val3 += pos3 * prime2;
+ val3 = (val3 << 13) | (val3 >> (32 - 13));
+ val3 *= prime1;
+
+ input += 16;
+ }
+
+ hash = ((val0 << 01) | (val0 >> (32 - 01))) +
+ ((val1 << 07) | (val1 >> (32 - 07))) +
+ ((val2 << 12) | (val2 >> (32 - 12))) +
+ ((val3 << 18) | (val3 >> (32 - 18)));
+ }
+
+ hash += (uint)length;
+
+ length &= 15;
+ while (length >= 4)
+ {
+ hash += *(uint*)input * prime3;
+ hash = ((hash << 17) | (hash >> (32 - 17))) * prime4;
+ input += 4;
+ length -= 4;
+ }
+ while (length > 0)
+ {
+ hash += *input * prime5;
+ hash = ((hash << 11) | (hash >> (32 - 11))) * prime1;
+ ++input;
+ --length;
+ }
+
+ hash ^= hash >> 15;
+ hash *= prime2;
+ hash ^= hash >> 13;
+ hash *= prime3;
+ hash ^= hash >> 16;
+
+ return hash;
+ }
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static unsafe ulong Hash64(byte* input, int length, uint seed = 0)
+ {
+ unchecked
+ {
+ const ulong prime1 = 11400714785074694791ul;
+ const ulong prime2 = 14029467366897019727ul;
+ const ulong prime3 = 01609587929392839161ul;
+ const ulong prime4 = 09650029242287828579ul;
+ const ulong prime5 = 02870177450012600261ul;
+
+ ulong hash = seed + prime5;
+
+ if (length >= 32)
+ {
+ ulong val0 = seed + prime1 + prime2;
+ ulong val1 = seed + prime2;
+ ulong val2 = seed + 0;
+ ulong val3 = seed - prime1;
+
+ int count = length >> 5;
+ for (int i = 0; i < count; i++)
+ {
+ var pos0 = *(ulong*)(input + 0);
+ var pos1 = *(ulong*)(input + 8);
+ var pos2 = *(ulong*)(input + 16);
+ var pos3 = *(ulong*)(input + 24);
+
+ val0 += pos0 * prime2;
+ val0 = (val0 << 31) | (val0 >> (64 - 31));
+ val0 *= prime1;
+
+ val1 += pos1 * prime2;
+ val1 = (val1 << 31) | (val1 >> (64 - 31));
+ val1 *= prime1;
+
+ val2 += pos2 * prime2;
+ val2 = (val2 << 31) | (val2 >> (64 - 31));
+ val2 *= prime1;
+
+ val3 += pos3 * prime2;
+ val3 = (val3 << 31) | (val3 >> (64 - 31));
+ val3 *= prime1;
+
+ input += 32;
+ }
+
+ hash = ((val0 << 01) | (val0 >> (64 - 01))) +
+ ((val1 << 07) | (val1 >> (64 - 07))) +
+ ((val2 << 12) | (val2 >> (64 - 12))) +
+ ((val3 << 18) | (val3 >> (64 - 18)));
+
+ val0 *= prime2;
+ val0 = (val0 << 31) | (val0 >> (64 - 31));
+ val0 *= prime1;
+ hash ^= val0;
+ hash = hash * prime1 + prime4;
+
+ val1 *= prime2;
+ val1 = (val1 << 31) | (val1 >> (64 - 31));
+ val1 *= prime1;
+ hash ^= val1;
+ hash = hash * prime1 + prime4;
+
+ val2 *= prime2;
+ val2 = (val2 << 31) | (val2 >> (64 - 31));
+ val2 *= prime1;
+ hash ^= val2;
+ hash = hash * prime1 + prime4;
+
+ val3 *= prime2;
+ val3 = (val3 << 31) | (val3 >> (64 - 31));
+ val3 *= prime1;
+ hash ^= val3;
+ hash = hash * prime1 + prime4;
+ }
+
+ hash += (ulong)length;
+
+ length &= 31;
+ while (length >= 8)
+ {
+ ulong lane = *(ulong*)input * prime2;
+ lane = ((lane << 31) | (lane >> (64 - 31))) * prime1;
+ hash ^= lane;
+ hash = ((hash << 27) | (hash >> (64 - 27))) * prime1 + prime4;
+ input += 8;
+ length -= 8;
+ }
+ if (length >= 4)
+ {
+ hash ^= *(uint*)input * prime1;
+ hash = ((hash << 23) | (hash >> (64 - 23))) * prime2 + prime3;
+ input += 4;
+ length -= 4;
+ }
+ while (length > 0)
+ {
+ hash ^= *input * prime5;
+ hash = ((hash << 11) | (hash >> (64 - 11))) * prime1;
+ ++input;
+ --length;
+ }
+
+ hash ^= hash >> 33;
+ hash *= prime2;
+ hash ^= hash >> 29;
+ hash *= prime3;
+ hash ^= hash >> 32;
+
+ return hash;
+ }
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static uint Hash32(this byte[] buffer)
+ {
+ int length = buffer.Length;
+ unsafe
+ {
+ fixed (byte* pointer = buffer)
+ {
+ return Hash32(pointer, length);
+ }
+ }
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static uint Hash32(this string text) => Hash32(Encoding.UTF8.GetBytes(text));
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static uint Hash32(this Type type) => Hash32(type.FullName);
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static uint Hash32() => Hash32(typeof(T));
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ulong Hash64(this byte[] buffer)
+ {
+ int length = buffer.Length;
+ unsafe
+ {
+ fixed (byte* pointer = buffer)
+ {
+ return Hash64(pointer, length);
+ }
+ }
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ulong Hash64(this string text) => Hash64(Encoding.UTF8.GetBytes(text));
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ulong Hash64(this Type type) => Hash64(type.FullName);
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ulong Hash64() => Hash64(typeof(T));
+ }
+}
diff --git a/Runtime/Hashing/XXHash/XXHash.cs.meta b/Runtime/Hashing/XXHash.cs.meta
similarity index 83%
rename from Runtime/Hashing/XXHash/XXHash.cs.meta
rename to Runtime/Hashing/XXHash.cs.meta
index 5c090bb..9715625 100644
--- a/Runtime/Hashing/XXHash/XXHash.cs.meta
+++ b/Runtime/Hashing/XXHash.cs.meta
@@ -1,5 +1,5 @@
fileFormatVersion: 2
-guid: b5aa7a49e9e694f148d810d34577546b
+guid: c3077af091aa443acbdea9d3e97727b0
MonoImporter:
externalObjects: {}
serializedVersion: 2
diff --git a/Runtime/Hashing/XXHash/LICENSE b/Runtime/Hashing/XXHash/LICENSE
deleted file mode 100644
index 6b55f78..0000000
--- a/Runtime/Hashing/XXHash/LICENSE
+++ /dev/null
@@ -1,21 +0,0 @@
-The MIT License (MIT)
-
-Copyright (c) 2015, 2016 Sedat Kapanoglu
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
diff --git a/Runtime/Hashing/XXHash/LICENSE.meta b/Runtime/Hashing/XXHash/LICENSE.meta
deleted file mode 100644
index c6b28aa..0000000
--- a/Runtime/Hashing/XXHash/LICENSE.meta
+++ /dev/null
@@ -1,7 +0,0 @@
-fileFormatVersion: 2
-guid: cf89ecbf6f9954c8ea6d0848b1e79d87
-DefaultImporter:
- externalObjects: {}
- userData:
- assetBundleName:
- assetBundleVariant:
diff --git a/Runtime/Hashing/XXHash/XXHash.cs b/Runtime/Hashing/XXHash/XXHash.cs
deleted file mode 100644
index cfcb9d6..0000000
--- a/Runtime/Hashing/XXHash/XXHash.cs
+++ /dev/null
@@ -1,318 +0,0 @@
-//
-// Copyright (c) 2015-2019 Sedat Kapanoglu
-// MIT License (see LICENSE file for details)
-//
-
-// @mfatihmar (Unity): Modified for Unity support
-
-using System.Text;
-using System.Runtime.CompilerServices;
-
-namespace Unity.Netcode
-{
- ///
- /// XXHash implementation.
- ///
- internal static class XXHash
- {
- private const ulong k_Prime64v1 = 11400714785074694791ul;
- private const ulong k_Prime64v2 = 14029467366897019727ul;
- private const ulong k_Prime64v3 = 1609587929392839161ul;
- private const ulong k_Prime64v4 = 9650029242287828579ul;
- private const ulong k_Prime64v5 = 2870177450012600261ul;
-
- private const uint k_Prime32v1 = 2654435761u;
- private const uint k_Prime32v2 = 2246822519u;
- private const uint k_Prime32v3 = 3266489917u;
- private const uint k_Prime32v4 = 668265263u;
- private const uint k_Prime32v5 = 374761393u;
-
- public static uint Hash32(string text) => Hash32(text, Encoding.UTF8);
- public static uint Hash32(string text, Encoding encoding) => Hash32(encoding.GetBytes(text));
- public static uint Hash32(byte[] buffer)
- {
- unsafe
- {
- fixed (byte* ptr = buffer)
- {
- return Hash32(ptr, buffer.Length);
- }
- }
- }
-
- ///
- /// Generate a 32-bit xxHash value.
- ///
- /// Input buffer.
- /// Input buffer length.
- /// Optional seed.
- /// 32-bit hash value.
- public static unsafe uint Hash32(byte* buffer, int bufferLength, uint seed = 0)
- {
- const int stripeLength = 16;
-
- int len = bufferLength;
- int remainingLen = len;
- uint acc;
-
- byte* pInput = buffer;
- if (len >= stripeLength)
- {
- uint acc1 = seed + k_Prime32v1 + k_Prime32v2;
- uint acc2 = seed + k_Prime32v2;
- uint acc3 = seed;
- uint acc4 = seed - k_Prime32v1;
-
- do
- {
- acc = processStripe32(ref pInput, ref acc1, ref acc2, ref acc3, ref acc4);
- remainingLen -= stripeLength;
- } while (remainingLen >= stripeLength);
- }
- else
- {
- acc = seed + k_Prime32v5;
- }
-
- acc += (uint)len;
- acc = processRemaining32(pInput, acc, remainingLen);
-
- return avalanche32(acc);
- }
-
- public static ulong Hash64(string text) => Hash64(text, Encoding.UTF8);
- public static ulong Hash64(string text, Encoding encoding) => Hash64(encoding.GetBytes(text));
- public static ulong Hash64(byte[] buffer)
- {
- unsafe
- {
- fixed (byte* ptr = buffer)
- {
- return Hash64(ptr, buffer.Length);
- }
- }
- }
-
- ///
- /// Generate a 64-bit xxHash value.
- ///
- /// Input buffer.
- /// Input buffer length.
- /// Optional seed.
- /// Computed 64-bit hash value.
- public static unsafe ulong Hash64(byte* buffer, int bufferLength, ulong seed = 0)
- {
- const int stripeLength = 32;
-
- int len = bufferLength;
- int remainingLen = len;
- ulong acc;
-
- byte* pInput = buffer;
- if (len >= stripeLength)
- {
- ulong acc1 = seed + k_Prime64v1 + k_Prime64v2;
- ulong acc2 = seed + k_Prime64v2;
- ulong acc3 = seed;
- ulong acc4 = seed - k_Prime64v1;
-
- do
- {
- acc = processStripe64(ref pInput, ref acc1, ref acc2, ref acc3, ref acc4);
- remainingLen -= stripeLength;
- } while (remainingLen >= stripeLength);
- }
- else
- {
- acc = seed + k_Prime64v5;
- }
-
- acc += (ulong)len;
- acc = processRemaining64(pInput, acc, remainingLen);
-
-
- return avalanche64(acc);
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static unsafe ulong processStripe64(
- ref byte* pInput,
- ref ulong acc1,
- ref ulong acc2,
- ref ulong acc3,
- ref ulong acc4)
- {
- processLane64(ref acc1, ref pInput);
- processLane64(ref acc2, ref pInput);
- processLane64(ref acc3, ref pInput);
- processLane64(ref acc4, ref pInput);
-
- ulong acc = Bits.RotateLeft(acc1, 1)
- + Bits.RotateLeft(acc2, 7)
- + Bits.RotateLeft(acc3, 12)
- + Bits.RotateLeft(acc4, 18);
-
- mergeAccumulator64(ref acc, acc1);
- mergeAccumulator64(ref acc, acc2);
- mergeAccumulator64(ref acc, acc3);
- mergeAccumulator64(ref acc, acc4);
- return acc;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static unsafe void processLane64(ref ulong accn, ref byte* pInput)
- {
- ulong lane = *(ulong*)pInput;
- accn = round64(accn, lane);
- pInput += 8;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static unsafe ulong processRemaining64(
- byte* pInput,
- ulong acc,
- int remainingLen)
- {
- for (ulong lane; remainingLen >= 8; remainingLen -= 8, pInput += 8)
- {
- lane = *(ulong*)pInput;
-
- acc ^= round64(0, lane);
- acc = Bits.RotateLeft(acc, 27) * k_Prime64v1;
- acc += k_Prime64v4;
- }
-
- for (uint lane32; remainingLen >= 4; remainingLen -= 4, pInput += 4)
- {
- lane32 = *(uint*)pInput;
-
- acc ^= lane32 * k_Prime64v1;
- acc = Bits.RotateLeft(acc, 23) * k_Prime64v2;
- acc += k_Prime64v3;
- }
-
- for (byte lane8; remainingLen >= 1; remainingLen--, pInput++)
- {
- lane8 = *pInput;
- acc ^= lane8 * k_Prime64v5;
- acc = Bits.RotateLeft(acc, 11) * k_Prime64v1;
- }
-
- return acc;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static ulong avalanche64(ulong acc)
- {
- acc ^= acc >> 33;
- acc *= k_Prime64v2;
- acc ^= acc >> 29;
- acc *= k_Prime64v3;
- acc ^= acc >> 32;
- return acc;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static ulong round64(ulong accn, ulong lane)
- {
- accn += lane * k_Prime64v2;
- return Bits.RotateLeft(accn, 31) * k_Prime64v1;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static void mergeAccumulator64(ref ulong acc, ulong accn)
- {
- acc ^= round64(0, accn);
- acc *= k_Prime64v1;
- acc += k_Prime64v4;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static unsafe uint processStripe32(
- ref byte* pInput,
- ref uint acc1,
- ref uint acc2,
- ref uint acc3,
- ref uint acc4)
- {
- processLane32(ref pInput, ref acc1);
- processLane32(ref pInput, ref acc2);
- processLane32(ref pInput, ref acc3);
- processLane32(ref pInput, ref acc4);
-
- return Bits.RotateLeft(acc1, 1)
- + Bits.RotateLeft(acc2, 7)
- + Bits.RotateLeft(acc3, 12)
- + Bits.RotateLeft(acc4, 18);
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static unsafe void processLane32(ref byte* pInput, ref uint accn)
- {
- uint lane = *(uint*)pInput;
- accn = round32(accn, lane);
- pInput += 4;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static unsafe uint processRemaining32(
- byte* pInput,
- uint acc,
- int remainingLen)
- {
- for (uint lane; remainingLen >= 4; remainingLen -= 4, pInput += 4)
- {
- lane = *(uint*)pInput;
- acc += lane * k_Prime32v3;
- acc = Bits.RotateLeft(acc, 17) * k_Prime32v4;
- }
-
- for (byte lane; remainingLen >= 1; remainingLen--, pInput++)
- {
- lane = *pInput;
- acc += lane * k_Prime32v5;
- acc = Bits.RotateLeft(acc, 11) * k_Prime32v1;
- }
-
- return acc;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static uint round32(uint accn, uint lane)
- {
- accn += lane * k_Prime32v2;
- accn = Bits.RotateLeft(accn, 13);
- accn *= k_Prime32v1;
- return accn;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static uint avalanche32(uint acc)
- {
- acc ^= acc >> 15;
- acc *= k_Prime32v2;
- acc ^= acc >> 13;
- acc *= k_Prime32v3;
- acc ^= acc >> 16;
- return acc;
- }
-
- ///
- /// Bit operations.
- ///
- private static class Bits
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static ulong RotateLeft(ulong value, int bits)
- {
- return (value << bits) | (value >> (64 - bits));
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static uint RotateLeft(uint value, int bits)
- {
- return (value << bits) | (value >> (32 - bits));
- }
- }
- }
-}
diff --git a/Runtime/Messaging/CustomMessageManager.cs b/Runtime/Messaging/CustomMessageManager.cs
index aa69ba7..573d645 100644
--- a/Runtime/Messaging/CustomMessageManager.cs
+++ b/Runtime/Messaging/CustomMessageManager.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using Unity.Collections;
namespace Unity.Netcode
{
@@ -68,9 +69,23 @@ namespace Unity.Netcode
if (clientIds == null)
{
- throw new ArgumentNullException("You must pass in a valid clientId List");
+ throw new ArgumentNullException(nameof(clientIds), "You must pass in a valid clientId List");
}
+ if (m_NetworkManager.IsHost)
+ {
+ for (var i = 0; i < clientIds.Count; ++i)
+ {
+ if (clientIds[i] == m_NetworkManager.LocalClientId)
+ {
+ InvokeUnnamedMessage(
+ m_NetworkManager.LocalClientId,
+ new FastBufferReader(messageBuffer, Allocator.None),
+ 0
+ );
+ }
+ }
+ }
var message = new UnnamedMessage
{
SendData = messageBuffer
@@ -92,6 +107,18 @@ namespace Unity.Netcode
/// The delivery type (QoS) to send data with
public void SendUnnamedMessage(ulong clientId, FastBufferWriter messageBuffer, NetworkDelivery networkDelivery = NetworkDelivery.ReliableSequenced)
{
+ if (m_NetworkManager.IsHost)
+ {
+ if (clientId == m_NetworkManager.LocalClientId)
+ {
+ InvokeUnnamedMessage(
+ m_NetworkManager.LocalClientId,
+ new FastBufferReader(messageBuffer, Allocator.None),
+ 0
+ );
+ return;
+ }
+ }
var message = new UnnamedMessage
{
SendData = messageBuffer
@@ -220,6 +247,20 @@ namespace Unity.Netcode
hash = XXHash.Hash64(messageName);
break;
}
+ if (m_NetworkManager.IsHost)
+ {
+ if (clientId == m_NetworkManager.LocalClientId)
+ {
+ InvokeNamedMessage(
+ hash,
+ m_NetworkManager.LocalClientId,
+ new FastBufferReader(messageStream, Allocator.None),
+ 0
+ );
+
+ return;
+ }
+ }
var message = new NamedMessage
{
@@ -251,7 +292,7 @@ namespace Unity.Netcode
if (clientIds == null)
{
- throw new ArgumentNullException("You must pass in a valid clientId List");
+ throw new ArgumentNullException(nameof(clientIds), "You must pass in a valid clientId List");
}
ulong hash = 0;
@@ -264,6 +305,21 @@ namespace Unity.Netcode
hash = XXHash.Hash64(messageName);
break;
}
+ if (m_NetworkManager.IsHost)
+ {
+ for (var i = 0; i < clientIds.Count; ++i)
+ {
+ if (clientIds[i] == m_NetworkManager.LocalClientId)
+ {
+ InvokeNamedMessage(
+ hash,
+ m_NetworkManager.LocalClientId,
+ new FastBufferReader(messageStream, Allocator.None),
+ 0
+ );
+ }
+ }
+ }
var message = new NamedMessage
{
Hash = hash,
diff --git a/Runtime/Messaging/DisconnectReasonMessage.cs b/Runtime/Messaging/DisconnectReasonMessage.cs
new file mode 100644
index 0000000..eb5d39a
--- /dev/null
+++ b/Runtime/Messaging/DisconnectReasonMessage.cs
@@ -0,0 +1,50 @@
+namespace Unity.Netcode
+{
+ internal struct DisconnectReasonMessage : INetworkMessage
+ {
+ public string Reason;
+
+ public int Version => 0;
+
+ public void Serialize(FastBufferWriter writer, int targetVersion)
+ {
+ string reasonSent = Reason;
+ if (reasonSent == null)
+ {
+ reasonSent = string.Empty;
+ }
+
+ // Since we don't send a ConnectionApprovedMessage, the version for this message is encded with the message
+ // itself. However, note that we HAVE received a ConnectionRequestMessage, so we DO have a valid targetVersion
+ // on this side of things - we just have to make sure the receiving side knows what version we sent it,
+ // since whoever has the higher version number is responsible for versioning and they may be the one
+ // with the higher version number.
+ BytePacker.WriteValueBitPacked(writer, Version);
+
+ if (writer.TryBeginWrite(FastBufferWriter.GetWriteSize(reasonSent)))
+ {
+ writer.WriteValue(reasonSent);
+ }
+ else
+ {
+ writer.WriteValueSafe(string.Empty);
+ NetworkLog.LogWarning(
+ "Disconnect reason didn't fit. Disconnected without sending a reason. Consider shortening the reason string.");
+ }
+ }
+
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
+ {
+ // Since we don't get a ConnectionApprovedMessage, the version for this message is encded with the message
+ // itself. This will override what we got from MessagingSystem... which will always be 0 here.
+ ByteUnpacker.ReadValueBitPacked(reader, out receivedMessageVersion);
+ reader.ReadValueSafe(out Reason);
+ return true;
+ }
+
+ public void Handle(ref NetworkContext context)
+ {
+ ((NetworkManager)context.SystemOwner).DisconnectReason = Reason;
+ }
+ };
+}
diff --git a/Runtime/Messaging/DisconnectReasonMessage.cs.meta b/Runtime/Messaging/DisconnectReasonMessage.cs.meta
new file mode 100644
index 0000000..87bae59
--- /dev/null
+++ b/Runtime/Messaging/DisconnectReasonMessage.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: d7742516058394f96999464f3ea32c71
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Runtime/Messaging/INetworkMessage.cs b/Runtime/Messaging/INetworkMessage.cs
index 1249081..c05587a 100644
--- a/Runtime/Messaging/INetworkMessage.cs
+++ b/Runtime/Messaging/INetworkMessage.cs
@@ -40,8 +40,9 @@ namespace Unity.Netcode
///
internal interface INetworkMessage
{
- void Serialize(FastBufferWriter writer);
- bool Deserialize(FastBufferReader reader, ref NetworkContext context);
+ void Serialize(FastBufferWriter writer, int targetVersion);
+ bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion);
void Handle(ref NetworkContext context);
+ int Version { get; }
}
}
diff --git a/Runtime/Messaging/Messages/ChangeOwnershipMessage.cs b/Runtime/Messaging/Messages/ChangeOwnershipMessage.cs
index 19f84bc..417347b 100644
--- a/Runtime/Messaging/Messages/ChangeOwnershipMessage.cs
+++ b/Runtime/Messaging/Messages/ChangeOwnershipMessage.cs
@@ -2,22 +2,26 @@ namespace Unity.Netcode
{
internal struct ChangeOwnershipMessage : INetworkMessage, INetworkSerializeByMemcpy
{
+ public int Version => 0;
+
public ulong NetworkObjectId;
public ulong OwnerClientId;
- public void Serialize(FastBufferWriter writer)
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
- writer.WriteValueSafe(this);
+ BytePacker.WriteValueBitPacked(writer, NetworkObjectId);
+ BytePacker.WriteValueBitPacked(writer, OwnerClientId);
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
var networkManager = (NetworkManager)context.SystemOwner;
if (!networkManager.IsClient)
{
return false;
}
- reader.ReadValueSafe(out this);
+ ByteUnpacker.ReadValueBitPacked(reader, out NetworkObjectId);
+ ByteUnpacker.ReadValueBitPacked(reader, out OwnerClientId);
if (!networkManager.SpawnManager.SpawnedObjects.ContainsKey(NetworkObjectId))
{
networkManager.DeferredMessageManager.DeferMessage(IDeferredMessageManager.TriggerType.OnSpawn, NetworkObjectId, reader, ref context);
diff --git a/Runtime/Messaging/Messages/ConnectionApprovedMessage.cs b/Runtime/Messaging/Messages/ConnectionApprovedMessage.cs
index 0a685a7..e0b3769 100644
--- a/Runtime/Messaging/Messages/ConnectionApprovedMessage.cs
+++ b/Runtime/Messaging/Messages/ConnectionApprovedMessage.cs
@@ -1,10 +1,12 @@
-using System;
using System.Collections.Generic;
+using Unity.Collections;
namespace Unity.Netcode
{
internal struct ConnectionApprovedMessage : INetworkMessage
{
+ public int Version => 0;
+
public ulong OwnerClientId;
public int NetworkTick;
@@ -13,14 +15,26 @@ namespace Unity.Netcode
private FastBufferReader m_ReceivedSceneObjectData;
- public void Serialize(FastBufferWriter writer)
+ public NativeArray MessageVersions;
+
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
- if (!writer.TryBeginWrite(sizeof(ulong) + sizeof(int) + sizeof(int)))
+ // ============================================================
+ // BEGIN FORBIDDEN SEGMENT
+ // DO NOT CHANGE THIS HEADER. Everything added to this message
+ // must go AFTER the message version header.
+ // ============================================================
+ BytePacker.WriteValueBitPacked(writer, MessageVersions.Length);
+ foreach (var messageVersion in MessageVersions)
{
- throw new OverflowException($"Not enough space in the write buffer to serialize {nameof(ConnectionApprovedMessage)}");
+ messageVersion.Serialize(writer);
}
- writer.WriteValue(OwnerClientId);
- writer.WriteValue(NetworkTick);
+ // ============================================================
+ // END FORBIDDEN SEGMENT
+ // ============================================================
+
+ BytePacker.WriteValueBitPacked(writer, OwnerClientId);
+ BytePacker.WriteValueBitPacked(writer, NetworkTick);
uint sceneObjectCount = 0;
if (SpawnedObjectsList != null)
@@ -39,17 +53,19 @@ namespace Unity.Netcode
++sceneObjectCount;
}
}
+
writer.Seek(pos);
- writer.WriteValue(sceneObjectCount);
+ // Can't pack this value because its space is reserved, so it needs to always use all the reserved space.
+ writer.WriteValueSafe(sceneObjectCount);
writer.Seek(writer.Length);
}
else
{
- writer.WriteValue(sceneObjectCount);
+ writer.WriteValueSafe(sceneObjectCount);
}
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
var networkManager = (NetworkManager)context.SystemOwner;
if (!networkManager.IsClient)
@@ -57,13 +73,36 @@ namespace Unity.Netcode
return false;
}
- if (!reader.TryBeginRead(sizeof(ulong) + sizeof(int) + sizeof(int)))
+ // ============================================================
+ // BEGIN FORBIDDEN SEGMENT
+ // DO NOT CHANGE THIS HEADER. Everything added to this message
+ // must go AFTER the message version header.
+ // ============================================================
+ ByteUnpacker.ReadValueBitPacked(reader, out int length);
+ var messageHashesInOrder = new NativeArray(length, Allocator.Temp);
+ for (var i = 0; i < length; ++i)
{
- throw new OverflowException($"Not enough space in the buffer to read {nameof(ConnectionApprovedMessage)}");
- }
+ var messageVersion = new MessageVersionData();
+ messageVersion.Deserialize(reader);
+ networkManager.MessagingSystem.SetVersion(context.SenderId, messageVersion.Hash, messageVersion.Version);
+ messageHashesInOrder[i] = messageVersion.Hash;
- reader.ReadValue(out OwnerClientId);
- reader.ReadValue(out NetworkTick);
+ // Update the received version since this message will always be passed version 0, due to the map not
+ // being initialized until just now.
+ var messageType = networkManager.MessagingSystem.GetMessageForHash(messageVersion.Hash);
+ if (messageType == typeof(ConnectionApprovedMessage))
+ {
+ receivedMessageVersion = messageVersion.Version;
+ }
+ }
+ networkManager.MessagingSystem.SetServerMessageOrder(messageHashesInOrder);
+ messageHashesInOrder.Dispose();
+ // ============================================================
+ // END FORBIDDEN SEGMENT
+ // ============================================================
+
+ ByteUnpacker.ReadValueBitPacked(reader, out OwnerClientId);
+ ByteUnpacker.ReadValueBitPacked(reader, out NetworkTick);
m_ReceivedSceneObjectData = reader;
return true;
}
@@ -85,7 +124,7 @@ namespace Unity.Netcode
if (!networkManager.NetworkConfig.EnableSceneManagement)
{
networkManager.SpawnManager.DestroySceneObjects();
- m_ReceivedSceneObjectData.ReadValue(out uint sceneObjectCount);
+ m_ReceivedSceneObjectData.ReadValueSafe(out uint sceneObjectCount);
// Deserializing NetworkVariable data is deferred from Receive() to Handle to avoid needing
// to create a list to hold the data. This is a breach of convention for performance reasons.
diff --git a/Runtime/Messaging/Messages/ConnectionRequestMessage.cs b/Runtime/Messaging/Messages/ConnectionRequestMessage.cs
index ee7965f..73e8b1f 100644
--- a/Runtime/Messaging/Messages/ConnectionRequestMessage.cs
+++ b/Runtime/Messaging/Messages/ConnectionRequestMessage.cs
@@ -1,15 +1,35 @@
+using Unity.Collections;
+
namespace Unity.Netcode
{
internal struct ConnectionRequestMessage : INetworkMessage
{
+ public int Version => 0;
+
public ulong ConfigHash;
public byte[] ConnectionData;
public bool ShouldSendConnectionData;
- public void Serialize(FastBufferWriter writer)
+ public NativeArray MessageVersions;
+
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
+ // ============================================================
+ // BEGIN FORBIDDEN SEGMENT
+ // DO NOT CHANGE THIS HEADER. Everything added to this message
+ // must go AFTER the message version header.
+ // ============================================================
+ BytePacker.WriteValueBitPacked(writer, MessageVersions.Length);
+ foreach (var messageVersion in MessageVersions)
+ {
+ messageVersion.Serialize(writer);
+ }
+ // ============================================================
+ // END FORBIDDEN SEGMENT
+ // ============================================================
+
if (ShouldSendConnectionData)
{
writer.WriteValueSafe(ConfigHash);
@@ -21,7 +41,7 @@ namespace Unity.Netcode
}
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
var networkManager = (NetworkManager)context.SystemOwner;
if (!networkManager.IsServer)
@@ -29,6 +49,30 @@ namespace Unity.Netcode
return false;
}
+ // ============================================================
+ // BEGIN FORBIDDEN SEGMENT
+ // DO NOT CHANGE THIS HEADER. Everything added to this message
+ // must go AFTER the message version header.
+ // ============================================================
+ ByteUnpacker.ReadValueBitPacked(reader, out int length);
+ for (var i = 0; i < length; ++i)
+ {
+ var messageVersion = new MessageVersionData();
+ messageVersion.Deserialize(reader);
+ networkManager.MessagingSystem.SetVersion(context.SenderId, messageVersion.Hash, messageVersion.Version);
+
+ // Update the received version since this message will always be passed version 0, due to the map not
+ // being initialized until just now.
+ var messageType = networkManager.MessagingSystem.GetMessageForHash(messageVersion.Hash);
+ if (messageType == typeof(ConnectionRequestMessage))
+ {
+ receivedMessageVersion = messageVersion.Version;
+ }
+ }
+ // ============================================================
+ // END FORBIDDEN SEGMENT
+ // ============================================================
+
if (networkManager.NetworkConfig.ConnectionApproval)
{
if (!reader.TryBeginRead(FastBufferWriter.GetWriteSize(ConfigHash) + FastBufferWriter.GetWriteSize()))
diff --git a/Runtime/Messaging/Messages/CreateObjectMessage.cs b/Runtime/Messaging/Messages/CreateObjectMessage.cs
index 547197d..defddd5 100644
--- a/Runtime/Messaging/Messages/CreateObjectMessage.cs
+++ b/Runtime/Messaging/Messages/CreateObjectMessage.cs
@@ -2,15 +2,17 @@ namespace Unity.Netcode
{
internal struct CreateObjectMessage : INetworkMessage
{
+ public int Version => 0;
+
public NetworkObject.SceneObject ObjectInfo;
private FastBufferReader m_ReceivedNetworkVariableData;
- public void Serialize(FastBufferWriter writer)
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
ObjectInfo.Serialize(writer);
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
var networkManager = (NetworkManager)context.SystemOwner;
if (!networkManager.IsClient)
@@ -21,7 +23,7 @@ namespace Unity.Netcode
ObjectInfo.Deserialize(reader);
if (!networkManager.NetworkConfig.ForceSamePrefabs && !networkManager.SpawnManager.HasPrefab(ObjectInfo))
{
- networkManager.DeferredMessageManager.DeferMessage(IDeferredMessageManager.TriggerType.OnAddPrefab, ObjectInfo.Header.Hash, reader, ref context);
+ networkManager.DeferredMessageManager.DeferMessage(IDeferredMessageManager.TriggerType.OnAddPrefab, ObjectInfo.Hash, reader, ref context);
return false;
}
m_ReceivedNetworkVariableData = reader;
diff --git a/Runtime/Messaging/Messages/DestroyObjectMessage.cs b/Runtime/Messaging/Messages/DestroyObjectMessage.cs
index 2730088..425cbe9 100644
--- a/Runtime/Messaging/Messages/DestroyObjectMessage.cs
+++ b/Runtime/Messaging/Messages/DestroyObjectMessage.cs
@@ -2,15 +2,18 @@ namespace Unity.Netcode
{
internal struct DestroyObjectMessage : INetworkMessage, INetworkSerializeByMemcpy
{
+ public int Version => 0;
+
public ulong NetworkObjectId;
public bool DestroyGameObject;
- public void Serialize(FastBufferWriter writer)
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
- writer.WriteValueSafe(this);
+ BytePacker.WriteValueBitPacked(writer, NetworkObjectId);
+ writer.WriteValueSafe(DestroyGameObject);
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
var networkManager = (NetworkManager)context.SystemOwner;
if (!networkManager.IsClient)
@@ -18,7 +21,8 @@ namespace Unity.Netcode
return false;
}
- reader.ReadValueSafe(out this);
+ ByteUnpacker.ReadValueBitPacked(reader, out NetworkObjectId);
+ reader.ReadValueSafe(out DestroyGameObject);
if (!networkManager.SpawnManager.SpawnedObjects.TryGetValue(NetworkObjectId, out var networkObject))
{
diff --git a/Runtime/Messaging/Messages/MessageMetadata.cs b/Runtime/Messaging/Messages/MessageMetadata.cs
new file mode 100644
index 0000000..9644280
--- /dev/null
+++ b/Runtime/Messaging/Messages/MessageMetadata.cs
@@ -0,0 +1,23 @@
+namespace Unity.Netcode
+{
+ ///
+ /// Conveys a version number on a remote node for the given message (identified by its hash)
+ ///
+ internal struct MessageVersionData
+ {
+ public uint Hash;
+ public int Version;
+
+ public void Serialize(FastBufferWriter writer)
+ {
+ writer.WriteValueSafe(Hash);
+ BytePacker.WriteValueBitPacked(writer, Version);
+ }
+
+ public void Deserialize(FastBufferReader reader)
+ {
+ reader.ReadValueSafe(out Hash);
+ ByteUnpacker.ReadValueBitPacked(reader, out Version);
+ }
+ }
+}
diff --git a/Runtime/Messaging/Messages/MessageMetadata.cs.meta b/Runtime/Messaging/Messages/MessageMetadata.cs.meta
new file mode 100644
index 0000000..d6dc086
--- /dev/null
+++ b/Runtime/Messaging/Messages/MessageMetadata.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 754d727b316b4263a2fa0d4c54fdad52
+timeCreated: 1666895514
\ No newline at end of file
diff --git a/Runtime/Messaging/Messages/NamedMessage.cs b/Runtime/Messaging/Messages/NamedMessage.cs
index 246f5aa..87ac914 100644
--- a/Runtime/Messaging/Messages/NamedMessage.cs
+++ b/Runtime/Messaging/Messages/NamedMessage.cs
@@ -2,18 +2,20 @@ namespace Unity.Netcode
{
internal struct NamedMessage : INetworkMessage
{
+ public int Version => 0;
+
public ulong Hash;
public FastBufferWriter SendData;
private FastBufferReader m_ReceiveData;
- public unsafe void Serialize(FastBufferWriter writer)
+ public unsafe void Serialize(FastBufferWriter writer, int targetVersion)
{
writer.WriteValueSafe(Hash);
writer.WriteBytesSafe(SendData.GetUnsafePtr(), SendData.Length);
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
reader.ReadValueSafe(out Hash);
m_ReceiveData = reader;
diff --git a/Runtime/Messaging/Messages/NetworkVariableDeltaMessage.cs b/Runtime/Messaging/Messages/NetworkVariableDeltaMessage.cs
index 2258821..ba3289a 100644
--- a/Runtime/Messaging/Messages/NetworkVariableDeltaMessage.cs
+++ b/Runtime/Messaging/Messages/NetworkVariableDeltaMessage.cs
@@ -12,6 +12,8 @@ namespace Unity.Netcode
///
internal struct NetworkVariableDeltaMessage : INetworkMessage
{
+ public int Version => 0;
+
public ulong NetworkObjectId;
public ushort NetworkBehaviourIndex;
@@ -21,15 +23,15 @@ namespace Unity.Netcode
private FastBufferReader m_ReceivedNetworkVariableData;
- public void Serialize(FastBufferWriter writer)
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
if (!writer.TryBeginWrite(FastBufferWriter.GetWriteSize(NetworkObjectId) + FastBufferWriter.GetWriteSize(NetworkBehaviourIndex)))
{
throw new OverflowException($"Not enough space in the buffer to write {nameof(NetworkVariableDeltaMessage)}");
}
- writer.WriteValue(NetworkObjectId);
- writer.WriteValue(NetworkBehaviourIndex);
+ BytePacker.WriteValueBitPacked(writer, NetworkObjectId);
+ BytePacker.WriteValueBitPacked(writer, NetworkBehaviourIndex);
for (int i = 0; i < NetworkBehaviour.NetworkVariableFields.Count; i++)
{
@@ -38,7 +40,7 @@ namespace Unity.Netcode
// This var does not belong to the currently iterating delivery group.
if (NetworkBehaviour.NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety)
{
- writer.WriteValueSafe((ushort)0);
+ BytePacker.WriteValueBitPacked(writer, (ushort)0);
}
else
{
@@ -66,7 +68,7 @@ namespace Unity.Netcode
{
if (!shouldWrite)
{
- BytePacker.WriteValueBitPacked(writer, 0);
+ BytePacker.WriteValueBitPacked(writer, (ushort)0);
}
}
else
@@ -110,15 +112,10 @@ namespace Unity.Netcode
}
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
- if (!reader.TryBeginRead(FastBufferWriter.GetWriteSize(NetworkObjectId) + FastBufferWriter.GetWriteSize(NetworkBehaviourIndex)))
- {
- throw new OverflowException($"Not enough data in the buffer to read {nameof(NetworkVariableDeltaMessage)}");
- }
-
- reader.ReadValue(out NetworkObjectId);
- reader.ReadValue(out NetworkBehaviourIndex);
+ ByteUnpacker.ReadValueBitPacked(reader, out NetworkObjectId);
+ ByteUnpacker.ReadValueBitPacked(reader, out NetworkBehaviourIndex);
m_ReceivedNetworkVariableData = reader;
diff --git a/Runtime/Messaging/Messages/OrderingMessage.cs b/Runtime/Messaging/Messages/OrderingMessage.cs
deleted file mode 100644
index 6651e0e..0000000
--- a/Runtime/Messaging/Messages/OrderingMessage.cs
+++ /dev/null
@@ -1,50 +0,0 @@
-using System;
-
-namespace Unity.Netcode
-{
- ///
- /// Upon connecting, the host sends a series of OrderingMessage to the client so that it can make sure both sides
- /// have the same message types in the same positions in
- /// - MessagingSystem.m_MessageHandlers
- /// - MessagingSystem.m_ReverseTypeMap
- /// even if one side has extra messages (compilation, version, patch, or platform differences, etc...)
- ///
- /// The ConnectionRequestedMessage, ConnectionApprovedMessage and OrderingMessage are prioritized at the beginning
- /// of the mapping, to guarantee they can be exchanged before the two sides share their ordering
- /// The sorting used in also stable so that even if MessageType names share hashes, it will work most of the time
- ///
- internal struct OrderingMessage : INetworkMessage
- {
- public int Order;
- public uint Hash;
-
- public void Serialize(FastBufferWriter writer)
- {
- if (!writer.TryBeginWrite(FastBufferWriter.GetWriteSize(Order) + FastBufferWriter.GetWriteSize(Hash)))
- {
- throw new OverflowException($"Not enough space in the buffer to write {nameof(OrderingMessage)}");
- }
-
- writer.WriteValue(Order);
- writer.WriteValue(Hash);
- }
-
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
- {
- if (!reader.TryBeginRead(FastBufferWriter.GetWriteSize(Order) + FastBufferWriter.GetWriteSize(Hash)))
- {
- throw new OverflowException($"Not enough data in the buffer to read {nameof(OrderingMessage)}");
- }
-
- reader.ReadValue(out Order);
- reader.ReadValue(out Hash);
-
- return true;
- }
-
- public void Handle(ref NetworkContext context)
- {
- ((NetworkManager)context.SystemOwner).MessagingSystem.ReorderMessage(Order, Hash);
- }
- }
-}
diff --git a/Runtime/Messaging/Messages/ParentSyncMessage.cs b/Runtime/Messaging/Messages/ParentSyncMessage.cs
index 0023b1a..c65c87b 100644
--- a/Runtime/Messaging/Messages/ParentSyncMessage.cs
+++ b/Runtime/Messaging/Messages/ParentSyncMessage.cs
@@ -4,18 +4,34 @@ namespace Unity.Netcode
{
internal struct ParentSyncMessage : INetworkMessage
{
+ public int Version => 0;
+
public ulong NetworkObjectId;
- public bool WorldPositionStays;
+ private byte m_BitField;
+
+ public bool WorldPositionStays
+ {
+ get => ByteUtility.GetBit(m_BitField, 0);
+ set => ByteUtility.SetBit(ref m_BitField, 0, value);
+ }
//If(Metadata.IsReparented)
- public bool IsLatestParentSet;
+ public bool IsLatestParentSet
+ {
+ get => ByteUtility.GetBit(m_BitField, 1);
+ set => ByteUtility.SetBit(ref m_BitField, 1, value);
+ }
//If(IsLatestParentSet)
public ulong? LatestParent;
// Is set when the parent should be removed (similar to IsReparented functionality but only for removing the parent)
- public bool RemoveParent;
+ public bool RemoveParent
+ {
+ get => ByteUtility.GetBit(m_BitField, 2);
+ set => ByteUtility.SetBit(ref m_BitField, 2, value);
+ }
// These additional properties are used to synchronize clients with the current position,
// rotation, and scale after parenting/de-parenting (world/local space relative). This
@@ -25,18 +41,15 @@ namespace Unity.Netcode
public Quaternion Rotation;
public Vector3 Scale;
- public void Serialize(FastBufferWriter writer)
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
- BytePacker.WriteValuePacked(writer, NetworkObjectId);
- writer.WriteValueSafe(RemoveParent);
- writer.WriteValueSafe(WorldPositionStays);
+ BytePacker.WriteValueBitPacked(writer, NetworkObjectId);
+ writer.WriteValueSafe(m_BitField);
if (!RemoveParent)
{
- writer.WriteValueSafe(IsLatestParentSet);
-
if (IsLatestParentSet)
{
- BytePacker.WriteValueBitPacked(writer, (ulong)LatestParent);
+ BytePacker.WriteValueBitPacked(writer, LatestParent.Value);
}
}
@@ -46,7 +59,7 @@ namespace Unity.Netcode
writer.WriteValueSafe(Scale);
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
var networkManager = (NetworkManager)context.SystemOwner;
if (!networkManager.IsClient)
@@ -54,13 +67,10 @@ namespace Unity.Netcode
return false;
}
- ByteUnpacker.ReadValuePacked(reader, out NetworkObjectId);
- reader.ReadValueSafe(out RemoveParent);
- reader.ReadValueSafe(out WorldPositionStays);
+ ByteUnpacker.ReadValueBitPacked(reader, out NetworkObjectId);
+ reader.ReadValueSafe(out m_BitField);
if (!RemoveParent)
{
- reader.ReadValueSafe(out IsLatestParentSet);
-
if (IsLatestParentSet)
{
ByteUnpacker.ReadValueBitPacked(reader, out ulong latestParent);
diff --git a/Runtime/Messaging/Messages/RpcMessages.cs b/Runtime/Messaging/Messages/RpcMessages.cs
index 1ab0492..21889c0 100644
--- a/Runtime/Messaging/Messages/RpcMessages.cs
+++ b/Runtime/Messaging/Messages/RpcMessages.cs
@@ -8,24 +8,17 @@ namespace Unity.Netcode
{
public static unsafe void Serialize(ref FastBufferWriter writer, ref RpcMetadata metadata, ref FastBufferWriter payload)
{
- if (!writer.TryBeginWrite(FastBufferWriter.GetWriteSize() + payload.Length))
- {
- throw new OverflowException("Not enough space in the buffer to store RPC data.");
- }
-
- writer.WriteValue(metadata);
- writer.WriteBytes(payload.GetUnsafePtr(), payload.Length);
+ BytePacker.WriteValueBitPacked(writer, metadata.NetworkObjectId);
+ BytePacker.WriteValueBitPacked(writer, metadata.NetworkBehaviourId);
+ BytePacker.WriteValueBitPacked(writer, metadata.NetworkRpcMethodId);
+ writer.WriteBytesSafe(payload.GetUnsafePtr(), payload.Length);
}
public static unsafe bool Deserialize(ref FastBufferReader reader, ref NetworkContext context, ref RpcMetadata metadata, ref FastBufferReader payload)
{
- int metadataSize = FastBufferWriter.GetWriteSize();
- if (!reader.TryBeginRead(metadataSize))
- {
- throw new InvalidOperationException("Not enough data in the buffer to read RPC meta.");
- }
-
- reader.ReadValue(out metadata);
+ ByteUnpacker.ReadValueBitPacked(reader, out metadata.NetworkObjectId);
+ ByteUnpacker.ReadValueBitPacked(reader, out metadata.NetworkBehaviourId);
+ ByteUnpacker.ReadValueBitPacked(reader, out metadata.NetworkRpcMethodId);
var networkManager = (NetworkManager)context.SystemOwner;
if (!networkManager.SpawnManager.SpawnedObjects.ContainsKey(metadata.NetworkObjectId))
@@ -46,7 +39,7 @@ namespace Unity.Netcode
return false;
}
- payload = new FastBufferReader(reader.GetUnsafePtr() + metadataSize, Allocator.None, reader.Length - metadataSize);
+ payload = new FastBufferReader(reader.GetUnsafePtrAtCurrentPosition(), Allocator.None, reader.Length - reader.Position);
#if DEVELOPMENT_BUILD || UNITY_EDITOR
if (NetworkManager.__rpc_name_table.TryGetValue(metadata.NetworkRpcMethodId, out var rpcMethodName))
@@ -92,17 +85,19 @@ namespace Unity.Netcode
internal struct ServerRpcMessage : INetworkMessage
{
+ public int Version => 0;
+
public RpcMetadata Metadata;
public FastBufferWriter WriteBuffer;
public FastBufferReader ReadBuffer;
- public unsafe void Serialize(FastBufferWriter writer)
+ public unsafe void Serialize(FastBufferWriter writer, int targetVersion)
{
RpcMessageHelpers.Serialize(ref writer, ref Metadata, ref WriteBuffer);
}
- public unsafe bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public unsafe bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
return RpcMessageHelpers.Deserialize(ref reader, ref context, ref Metadata, ref ReadBuffer);
}
@@ -125,17 +120,19 @@ namespace Unity.Netcode
internal struct ClientRpcMessage : INetworkMessage
{
+ public int Version => 0;
+
public RpcMetadata Metadata;
public FastBufferWriter WriteBuffer;
public FastBufferReader ReadBuffer;
- public void Serialize(FastBufferWriter writer)
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
RpcMessageHelpers.Serialize(ref writer, ref Metadata, ref WriteBuffer);
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
return RpcMessageHelpers.Deserialize(ref reader, ref context, ref Metadata, ref ReadBuffer);
}
diff --git a/Runtime/Messaging/Messages/SceneEventMessage.cs b/Runtime/Messaging/Messages/SceneEventMessage.cs
index 48be77d..4417569 100644
--- a/Runtime/Messaging/Messages/SceneEventMessage.cs
+++ b/Runtime/Messaging/Messages/SceneEventMessage.cs
@@ -4,16 +4,18 @@ namespace Unity.Netcode
// like most of the other messages when we have some more time and can come back and refactor this.
internal struct SceneEventMessage : INetworkMessage
{
+ public int Version => 0;
+
public SceneEventData EventData;
private FastBufferReader m_ReceivedData;
- public void Serialize(FastBufferWriter writer)
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
EventData.Serialize(writer);
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
m_ReceivedData = reader;
return true;
diff --git a/Runtime/Messaging/Messages/ServerLogMessage.cs b/Runtime/Messaging/Messages/ServerLogMessage.cs
index 8f0cde8..b12c008 100644
--- a/Runtime/Messaging/Messages/ServerLogMessage.cs
+++ b/Runtime/Messaging/Messages/ServerLogMessage.cs
@@ -2,6 +2,8 @@ namespace Unity.Netcode
{
internal struct ServerLogMessage : INetworkMessage
{
+ public int Version => 0;
+
public NetworkLog.LogType LogType;
// It'd be lovely to be able to replace this with FixedString or NativeArray...
// But it's not really practical. On the sending side, the user is likely to want
@@ -11,13 +13,13 @@ namespace Unity.Netcode
public string Message;
- public void Serialize(FastBufferWriter writer)
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
writer.WriteValueSafe(LogType);
BytePacker.WriteValuePacked(writer, Message);
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
var networkManager = (NetworkManager)context.SystemOwner;
if (networkManager.IsServer && networkManager.NetworkConfig.EnableNetworkLogs)
diff --git a/Runtime/Messaging/Messages/TimeSyncMessage.cs b/Runtime/Messaging/Messages/TimeSyncMessage.cs
index 613bb90..97b0fcc 100644
--- a/Runtime/Messaging/Messages/TimeSyncMessage.cs
+++ b/Runtime/Messaging/Messages/TimeSyncMessage.cs
@@ -2,21 +2,23 @@ namespace Unity.Netcode
{
internal struct TimeSyncMessage : INetworkMessage, INetworkSerializeByMemcpy
{
+ public int Version => 0;
+
public int Tick;
- public void Serialize(FastBufferWriter writer)
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
- writer.WriteValueSafe(this);
+ BytePacker.WriteValueBitPacked(writer, Tick);
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
var networkManager = (NetworkManager)context.SystemOwner;
if (!networkManager.IsClient)
{
return false;
}
- reader.ReadValueSafe(out this);
+ ByteUnpacker.ReadValueBitPacked(reader, out Tick);
return true;
}
diff --git a/Runtime/Messaging/Messages/UnnamedMessage.cs b/Runtime/Messaging/Messages/UnnamedMessage.cs
index b34d200..ccce67c 100644
--- a/Runtime/Messaging/Messages/UnnamedMessage.cs
+++ b/Runtime/Messaging/Messages/UnnamedMessage.cs
@@ -2,15 +2,17 @@ namespace Unity.Netcode
{
internal struct UnnamedMessage : INetworkMessage
{
+ public int Version => 0;
+
public FastBufferWriter SendData;
private FastBufferReader m_ReceivedData;
- public unsafe void Serialize(FastBufferWriter writer)
+ public unsafe void Serialize(FastBufferWriter writer, int targetVersion)
{
writer.WriteBytesSafe(SendData.GetUnsafePtr(), SendData.Length);
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
m_ReceivedData = reader;
return true;
diff --git a/Runtime/Messaging/MessagingSystem.cs b/Runtime/Messaging/MessagingSystem.cs
index 82917a1..6f5fca5 100644
--- a/Runtime/Messaging/MessagingSystem.cs
+++ b/Runtime/Messaging/MessagingSystem.cs
@@ -46,6 +46,7 @@ namespace Unity.Netcode
}
internal delegate void MessageHandler(FastBufferReader reader, ref NetworkContext context, MessagingSystem system);
+ internal delegate int VersionGetter();
private NativeList m_IncomingMessageQueue = new NativeList(16, Allocator.Persistent);
@@ -56,6 +57,11 @@ namespace Unity.Netcode
private Dictionary m_MessageTypes = new Dictionary();
private Dictionary> m_SendQueues = new Dictionary>();
+ // This is m_PerClientMessageVersion[clientId][messageType] = version
+ private Dictionary> m_PerClientMessageVersions = new Dictionary>();
+ private Dictionary m_MessagesByHash = new Dictionary();
+ private Dictionary m_LocalVersions = new Dictionary();
+
private List m_Hooks = new List();
private uint m_HighMessageType;
@@ -74,12 +80,13 @@ namespace Unity.Netcode
}
public const int NON_FRAGMENTED_MESSAGE_MAX_SIZE = 1300;
- public const int FRAGMENTED_MESSAGE_MAX_SIZE = BytePacker.BitPackedIntMax;
+ public const int FRAGMENTED_MESSAGE_MAX_SIZE = int.MaxValue;
internal struct MessageWithHandler
{
public Type MessageType;
public MessageHandler Handler;
+ public VersionGetter GetVersion;
}
internal List PrioritizeMessageOrder(List allowedTypes)
@@ -90,9 +97,8 @@ namespace Unity.Netcode
// Those are the messages that must be delivered in order to allow re-ordering the others later
foreach (var t in allowedTypes)
{
- if (t.MessageType.FullName == "Unity.Netcode.ConnectionRequestMessage" ||
- t.MessageType.FullName == "Unity.Netcode.ConnectionApprovedMessage" ||
- t.MessageType.FullName == "Unity.Netcode.OrderingMessage")
+ if (t.MessageType.FullName == typeof(ConnectionRequestMessage).FullName ||
+ t.MessageType.FullName == typeof(ConnectionApprovedMessage).FullName)
{
prioritizedTypes.Add(t);
}
@@ -100,9 +106,8 @@ namespace Unity.Netcode
foreach (var t in allowedTypes)
{
- if (t.MessageType.FullName != "Unity.Netcode.ConnectionRequestMessage" &&
- t.MessageType.FullName != "Unity.Netcode.ConnectionApprovedMessage" &&
- t.MessageType.FullName != "Unity.Netcode.OrderingMessage")
+ if (t.MessageType.FullName != typeof(ConnectionRequestMessage).FullName &&
+ t.MessageType.FullName != typeof(ConnectionApprovedMessage).FullName)
{
prioritizedTypes.Add(t);
}
@@ -189,7 +194,14 @@ namespace Unity.Netcode
m_MessageHandlers[m_HighMessageType] = messageWithHandler.Handler;
m_ReverseTypeMap[m_HighMessageType] = messageWithHandler.MessageType;
+ m_MessagesByHash[XXHash.Hash32(messageWithHandler.MessageType.FullName)] = messageWithHandler.MessageType;
m_MessageTypes[messageWithHandler.MessageType] = m_HighMessageType++;
+ m_LocalVersions[messageWithHandler.MessageType] = messageWithHandler.GetVersion();
+ }
+
+ public int GetLocalVersion(Type messageType)
+ {
+ return m_LocalVersions[messageType];
}
internal void HandleIncomingData(ulong clientId, ArraySegment data, float receiveTime)
@@ -270,68 +282,53 @@ namespace Unity.Netcode
return true;
}
- // Moves the handler for the type having hash `targetHash` to the `desiredOrder` position, in the handler list
- // This allows the server to tell the client which id it is using for which message and make sure the right
- // message is used when deserializing.
- internal void ReorderMessage(int desiredOrder, uint targetHash)
+ internal Type GetMessageForHash(uint messageHash)
{
- if (desiredOrder < 0)
+ if (!m_MessagesByHash.ContainsKey(messageHash))
{
- throw new ArgumentException("ReorderMessage desiredOrder must be positive");
+ return null;
}
+ return m_MessagesByHash[messageHash];
+ }
- if (desiredOrder < m_ReverseTypeMap.Length &&
- XXHash.Hash32(m_ReverseTypeMap[desiredOrder].FullName) == targetHash)
+ internal void SetVersion(ulong clientId, uint messageHash, int version)
+ {
+ if (!m_MessagesByHash.ContainsKey(messageHash))
{
- // matching positions and hashes. All good.
return;
}
+ var messageType = m_MessagesByHash[messageHash];
- Debug.Log($"Unexpected hash for {desiredOrder}");
-
- // Since the message at `desiredOrder` is not the expected one,
- // insert an empty placeholder and move the messages down
- var typesAsList = new List(m_ReverseTypeMap);
-
- typesAsList.Insert(desiredOrder, null);
- var handlersAsList = new List(m_MessageHandlers);
- handlersAsList.Insert(desiredOrder, null);
-
- // we added a dummy message, bump the end up
- m_HighMessageType++;
-
- // Here, we rely on the server telling us about all messages, in order.
- // So, we know the handlers before desiredOrder are correct.
- // We start at desiredOrder to not shift them when we insert.
- int position = desiredOrder;
- bool found = false;
- while (position < typesAsList.Count)
+ if (!m_PerClientMessageVersions.ContainsKey(clientId))
{
- if (typesAsList[position] != null &&
- XXHash.Hash32(typesAsList[position].FullName) == targetHash)
+ m_PerClientMessageVersions[clientId] = new Dictionary();
+ }
+
+ m_PerClientMessageVersions[clientId][messageType] = version;
+ }
+
+ internal void SetServerMessageOrder(NativeArray messagesInIdOrder)
+ {
+ var oldHandlers = m_MessageHandlers;
+ var oldTypes = m_MessageTypes;
+ m_ReverseTypeMap = new Type[messagesInIdOrder.Length];
+ m_MessageHandlers = new MessageHandler[messagesInIdOrder.Length];
+ m_MessageTypes = new Dictionary();
+
+ for (var i = 0; i < messagesInIdOrder.Length; ++i)
+ {
+ if (!m_MessagesByHash.ContainsKey(messagesInIdOrder[i]))
{
- found = true;
- break;
+ continue;
}
-
- position++;
+ var messageType = m_MessagesByHash[messagesInIdOrder[i]];
+ var oldId = oldTypes[messageType];
+ var handler = oldHandlers[oldId];
+ var newId = (uint)i;
+ m_MessageTypes[messageType] = newId;
+ m_MessageHandlers[newId] = handler;
+ m_ReverseTypeMap[newId] = messageType;
}
-
- if (found)
- {
- // Copy the handler and type to the right index
-
- typesAsList[desiredOrder] = typesAsList[position];
- handlersAsList[desiredOrder] = handlersAsList[position];
- typesAsList.RemoveAt(position);
- handlersAsList.RemoveAt(position);
-
- // we removed a copy after moving a message, reduce the high message index
- m_HighMessageType--;
- }
-
- m_ReverseTypeMap = typesAsList.ToArray();
- m_MessageHandlers = handlersAsList.ToArray();
}
public void HandleMessage(in MessageHeader header, FastBufferReader reader, ulong senderId, float timestamp, int serializedHeaderSize)
@@ -433,7 +430,7 @@ namespace Unity.Netcode
m_SendQueues.Remove(clientId);
}
- private unsafe void CleanupDisconnectedClient(ulong clientId)
+ private void CleanupDisconnectedClient(ulong clientId)
{
var queue = m_SendQueues[clientId];
for (var i = 0; i < queue.Length; ++i)
@@ -444,10 +441,67 @@ namespace Unity.Netcode
queue.Dispose();
}
+ internal void CleanupDisconnectedClients()
+ {
+ var removeList = new NativeList(Allocator.Temp);
+ foreach (var clientId in m_PerClientMessageVersions.Keys)
+ {
+ if (!m_SendQueues.ContainsKey(clientId))
+ {
+ removeList.Add(clientId);
+ }
+ }
+
+ foreach (var clientId in removeList)
+ {
+ m_PerClientMessageVersions.Remove(clientId);
+ }
+ }
+
+ public static int CreateMessageAndGetVersion() where T : INetworkMessage, new()
+ {
+ return new T().Version;
+ }
+
+ internal int GetMessageVersion(Type type, ulong clientId, bool forReceive = false)
+ {
+ if (!m_PerClientMessageVersions.TryGetValue(clientId, out var versionMap))
+ {
+ if (forReceive)
+ {
+ Debug.LogWarning($"Trying to receive {type.Name} from client {clientId} which is not in a connected state.");
+
+ }
+ else
+ {
+ Debug.LogWarning($"Trying to send {type.Name} to client {clientId} which is not in a connected state.");
+ }
+ return -1;
+ }
+ if (!versionMap.TryGetValue(type, out var messageVersion))
+ {
+ return -1;
+ }
+
+ return messageVersion;
+ }
+
public static void ReceiveMessage(FastBufferReader reader, ref NetworkContext context, MessagingSystem system) where T : INetworkMessage, new()
{
var message = new T();
- if (message.Deserialize(reader, ref context))
+ var messageVersion = 0;
+ // Special cases because these are the messages that carry the version info - thus the version info isn't
+ // populated yet when we get these. The first part of these messages always has to be the version data
+ // and can't change.
+ if (typeof(T) != typeof(ConnectionRequestMessage) && typeof(T) != typeof(ConnectionApprovedMessage) && typeof(T) != typeof(DisconnectReasonMessage))
+ {
+ messageVersion = system.GetMessageVersion(typeof(T), context.SenderId, true);
+ if (messageVersion < 0)
+ {
+ return;
+ }
+ }
+ if (message.Deserialize(reader, ref context, messageVersion))
{
for (var hookIdx = 0; hookIdx < system.m_Hooks.Count; ++hookIdx)
{
@@ -485,16 +539,47 @@ namespace Unity.Netcode
return 0;
}
- var maxSize = delivery == NetworkDelivery.ReliableFragmentedSequenced ? FRAGMENTED_MESSAGE_MAX_SIZE : NON_FRAGMENTED_MESSAGE_MAX_SIZE;
+ var largestSerializedSize = 0;
+ var sentMessageVersions = new NativeHashSet(clientIds.Count, Allocator.Temp);
+ for (var i = 0; i < clientIds.Count; ++i)
+ {
+ var messageVersion = 0;
+ // Special case because this is the message that carries the version info - thus the version info isn't
+ // populated yet when we get this. The first part of this message always has to be the version data
+ // and can't change.
+ if (typeof(TMessageType) != typeof(ConnectionRequestMessage))
+ {
+ messageVersion = GetMessageVersion(typeof(TMessageType), clientIds[i]);
+ if (messageVersion < 0)
+ {
+ // Client doesn't know this message exists, don't send it at all.
+ continue;
+ }
+ }
- using var tmpSerializer = new FastBufferWriter(NON_FRAGMENTED_MESSAGE_MAX_SIZE - FastBufferWriter.GetWriteSize(), Allocator.Temp, maxSize - FastBufferWriter.GetWriteSize());
+ if (sentMessageVersions.Contains(messageVersion))
+ {
+ continue;
+ }
- message.Serialize(tmpSerializer);
+ sentMessageVersions.Add(messageVersion);
- return SendPreSerializedMessage(tmpSerializer, maxSize, ref message, delivery, clientIds);
+ var maxSize = delivery == NetworkDelivery.ReliableFragmentedSequenced ? FRAGMENTED_MESSAGE_MAX_SIZE : NON_FRAGMENTED_MESSAGE_MAX_SIZE;
+
+ using var tmpSerializer = new FastBufferWriter(NON_FRAGMENTED_MESSAGE_MAX_SIZE - FastBufferWriter.GetWriteSize(), Allocator.Temp, maxSize - FastBufferWriter.GetWriteSize());
+
+ message.Serialize(tmpSerializer, messageVersion);
+
+ var size = SendPreSerializedMessage(tmpSerializer, maxSize, ref message, delivery, clientIds, messageVersion);
+ largestSerializedSize = size > largestSerializedSize ? size : largestSerializedSize;
+ }
+
+ sentMessageVersions.Dispose();
+
+ return largestSerializedSize;
}
- internal unsafe int SendPreSerializedMessage(in FastBufferWriter tmpSerializer, int maxSize, ref TMessageType message, NetworkDelivery delivery, in IReadOnlyList clientIds)
+ internal unsafe int SendPreSerializedMessage(in FastBufferWriter tmpSerializer, int maxSize, ref TMessageType message, NetworkDelivery delivery, in IReadOnlyList clientIds, int messageVersionFilter)
where TMessageType : INetworkMessage
{
using var headerSerializer = new FastBufferWriter(FastBufferWriter.GetWriteSize(), Allocator.Temp);
@@ -509,6 +594,25 @@ namespace Unity.Netcode
for (var i = 0; i < clientIds.Count; ++i)
{
+ var messageVersion = 0;
+ // Special case because this is the message that carries the version info - thus the version info isn't
+ // populated yet when we get this. The first part of this message always has to be the version data
+ // and can't change.
+ if (typeof(TMessageType) != typeof(ConnectionRequestMessage))
+ {
+ messageVersion = GetMessageVersion(typeof(TMessageType), clientIds[i]);
+ if (messageVersion < 0)
+ {
+ // Client doesn't know this message exists, don't send it at all.
+ continue;
+ }
+
+ if (messageVersion != messageVersionFilter)
+ {
+ continue;
+ }
+ }
+
var clientId = clientIds[i];
if (!CanSend(clientId, typeof(TMessageType), delivery))
@@ -559,8 +663,22 @@ namespace Unity.Netcode
internal unsafe int SendPreSerializedMessage(in FastBufferWriter tmpSerializer, int maxSize, ref TMessageType message, NetworkDelivery delivery, ulong clientId)
where TMessageType : INetworkMessage
{
+ var messageVersion = 0;
+ // Special case because this is the message that carries the version info - thus the version info isn't
+ // populated yet when we get this. The first part of this message always has to be the version data
+ // and can't change.
+ if (typeof(TMessageType) != typeof(ConnectionRequestMessage))
+ {
+ messageVersion = GetMessageVersion(typeof(TMessageType), clientId);
+ if (messageVersion < 0)
+ {
+ // Client doesn't know this message exists, don't send it at all.
+ return 0;
+ }
+ }
+
ulong* clientIds = stackalloc ulong[] { clientId };
- return SendPreSerializedMessage(tmpSerializer, maxSize, ref message, delivery, new PointerListWrapper(clientIds, 1));
+ return SendPreSerializedMessage(tmpSerializer, maxSize, ref message, delivery, new PointerListWrapper(clientIds, 1), messageVersion);
}
private struct PointerListWrapper : IReadOnlyList
diff --git a/Runtime/NetworkVariable/Collections/NetworkList.cs b/Runtime/NetworkVariable/Collections/NetworkList.cs
index e893db7..aa25390 100644
--- a/Runtime/NetworkVariable/Collections/NetworkList.cs
+++ b/Runtime/NetworkVariable/Collections/NetworkList.cs
@@ -413,7 +413,7 @@ namespace Unity.Netcode
///
public bool Contains(T item)
{
- int index = NativeArrayExtensions.IndexOf(m_List, item);
+ int index = m_List.IndexOf(item);
return index != -1;
}
@@ -426,7 +426,7 @@ namespace Unity.Netcode
throw new InvalidOperationException("Client is not allowed to write to this NetworkList");
}
- int index = NativeArrayExtensions.IndexOf(m_List, item);
+ int index = m_List.IndexOf(item);
if (index == -1)
{
return false;
diff --git a/Runtime/NetworkVariable/NetworkVariableSerialization.cs b/Runtime/NetworkVariable/NetworkVariableSerialization.cs
index 83c4de2..463f502 100644
--- a/Runtime/NetworkVariable/NetworkVariableSerialization.cs
+++ b/Runtime/NetworkVariable/NetworkVariableSerialization.cs
@@ -1,6 +1,8 @@
using System;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
+using UnityEditor;
+using UnityEngine;
namespace Unity.Netcode
{
@@ -20,6 +22,96 @@ namespace Unity.Netcode
public void Read(FastBufferReader reader, ref T value);
}
+ ///
+ /// Packing serializer for shorts
+ ///
+ internal class ShortSerializer : INetworkVariableSerializer
+ {
+ public void Write(FastBufferWriter writer, ref short value)
+ {
+ BytePacker.WriteValueBitPacked(writer, value);
+ }
+ public void Read(FastBufferReader reader, ref short value)
+ {
+ ByteUnpacker.ReadValueBitPacked(reader, out value);
+ }
+ }
+
+ ///
+ /// Packing serializer for shorts
+ ///
+ internal class UshortSerializer : INetworkVariableSerializer
+ {
+ public void Write(FastBufferWriter writer, ref ushort value)
+ {
+ BytePacker.WriteValueBitPacked(writer, value);
+ }
+ public void Read(FastBufferReader reader, ref ushort value)
+ {
+ ByteUnpacker.ReadValueBitPacked(reader, out value);
+ }
+ }
+
+ ///
+ /// Packing serializer for ints
+ ///
+ internal class IntSerializer : INetworkVariableSerializer
+ {
+ public void Write(FastBufferWriter writer, ref int value)
+ {
+ BytePacker.WriteValueBitPacked(writer, value);
+ }
+ public void Read(FastBufferReader reader, ref int value)
+ {
+ ByteUnpacker.ReadValueBitPacked(reader, out value);
+ }
+ }
+
+ ///
+ /// Packing serializer for ints
+ ///
+ internal class UintSerializer : INetworkVariableSerializer
+ {
+ public void Write(FastBufferWriter writer, ref uint value)
+ {
+ BytePacker.WriteValueBitPacked(writer, value);
+ }
+ public void Read(FastBufferReader reader, ref uint value)
+ {
+ ByteUnpacker.ReadValueBitPacked(reader, out value);
+ }
+ }
+
+ ///
+ /// Packing serializer for longs
+ ///
+ internal class LongSerializer : INetworkVariableSerializer
+ {
+ public void Write(FastBufferWriter writer, ref long value)
+ {
+ BytePacker.WriteValueBitPacked(writer, value);
+ }
+ public void Read(FastBufferReader reader, ref long value)
+ {
+ ByteUnpacker.ReadValueBitPacked(reader, out value);
+ }
+ }
+
+ ///
+ /// Packing serializer for longs
+ ///
+ internal class UlongSerializer : INetworkVariableSerializer
+ {
+ public void Write(FastBufferWriter writer, ref ulong value)
+ {
+ BytePacker.WriteValueBitPacked(writer, value);
+ }
+ public void Read(FastBufferReader reader, ref ulong value)
+ {
+ ByteUnpacker.ReadValueBitPacked(reader, out value);
+ }
+ }
+
///
/// Basic serializer for unmanaged types.
/// This covers primitives, built-in unity types, and IForceSerializeByMemcpy
@@ -188,6 +280,26 @@ namespace Unity.Netcode
///
public static class NetworkVariableSerializationTypes
{
+ [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
+#if UNITY_EDITOR
+ [InitializeOnLoadMethod]
+#endif
+ internal static void InitializeIntegerSerialization()
+ {
+ NetworkVariableSerialization.Serializer = new ShortSerializer();
+ NetworkVariableSerialization.AreEqual = NetworkVariableSerialization.ValueEquals;
+ NetworkVariableSerialization.Serializer = new UshortSerializer();
+ NetworkVariableSerialization.AreEqual = NetworkVariableSerialization.ValueEquals;
+ NetworkVariableSerialization.Serializer = new IntSerializer();
+ NetworkVariableSerialization.AreEqual = NetworkVariableSerialization.ValueEquals;
+ NetworkVariableSerialization.Serializer = new UintSerializer();
+ NetworkVariableSerialization.AreEqual = NetworkVariableSerialization.ValueEquals;
+ NetworkVariableSerialization.Serializer = new LongSerializer();
+ NetworkVariableSerialization.AreEqual = NetworkVariableSerialization.ValueEquals;
+ NetworkVariableSerialization.Serializer = new UlongSerializer();
+ NetworkVariableSerialization.AreEqual = NetworkVariableSerialization.ValueEquals;
+ }
+
///
/// Registeres an unmanaged type that will be serialized by a direct memcpy into a buffer
///
diff --git a/Runtime/SceneManagement/NetworkSceneManager.cs b/Runtime/SceneManagement/NetworkSceneManager.cs
index c91cee2..781326f 100644
--- a/Runtime/SceneManagement/NetworkSceneManager.cs
+++ b/Runtime/SceneManagement/NetworkSceneManager.cs
@@ -2018,7 +2018,11 @@ namespace Unity.Netcode
ScenePlacedObjects.Clear();
}
+#if UNITY_2023_1_OR_NEWER
+ var networkObjects = UnityEngine.Object.FindObjectsByType(FindObjectsSortMode.InstanceID);
+#else
var networkObjects = UnityEngine.Object.FindObjectsOfType();
+#endif
// Just add every NetworkObject found that isn't already in the list
// With additive scenes, we can have multiple in-scene placed NetworkObjects with the same GlobalObjectIdHash value
diff --git a/Runtime/SceneManagement/SceneEventData.cs b/Runtime/SceneManagement/SceneEventData.cs
index 222e437..f4c9927 100644
--- a/Runtime/SceneManagement/SceneEventData.cs
+++ b/Runtime/SceneManagement/SceneEventData.cs
@@ -269,7 +269,12 @@ namespace Unity.Netcode
{
m_DespawnedInSceneObjectsSync.Clear();
// Find all active and non-active in-scene placed NetworkObjects
+#if UNITY_2023_1_OR_NEWER
+ var inSceneNetworkObjects = UnityEngine.Object.FindObjectsByType(UnityEngine.FindObjectsInactive.Include, UnityEngine.FindObjectsSortMode.InstanceID).Where((c) => c.NetworkManager == m_NetworkManager);
+#else
var inSceneNetworkObjects = UnityEngine.Object.FindObjectsOfType(includeInactive: true).Where((c) => c.NetworkManager == m_NetworkManager);
+
+#endif
foreach (var sobj in inSceneNetworkObjects)
{
if (sobj.IsSceneObject.HasValue && sobj.IsSceneObject.Value && !sobj.IsSpawned)
@@ -380,7 +385,7 @@ namespace Unity.Netcode
writer.WriteValueSafe(SceneEventType);
// Write the scene loading mode
- writer.WriteValueSafe(LoadSceneMode);
+ writer.WriteValueSafe((byte)LoadSceneMode);
// Write the scene event progress Guid
if (SceneEventType != SceneEventType.Synchronize)
@@ -444,13 +449,13 @@ namespace Unity.Netcode
int totalBytes = 0;
// Write the number of NetworkObjects we are serializing
- BytePacker.WriteValuePacked(writer, m_NetworkObjectsSync.Count);
+ writer.WriteValueSafe(m_NetworkObjectsSync.Count);
+
// Serialize all NetworkObjects that are spawned
for (var i = 0; i < m_NetworkObjectsSync.Count; ++i)
{
var noStart = writer.Position;
var sceneObject = m_NetworkObjectsSync[i].GetMessageSceneObject(TargetClientId);
- BytePacker.WriteValuePacked(writer, m_NetworkObjectsSync[i].GetSceneOriginHandle());
sceneObject.Serialize(writer);
var noStop = writer.Position;
totalBytes += (int)(noStop - noStart);
@@ -462,8 +467,8 @@ namespace Unity.Netcode
for (var i = 0; i < m_DespawnedInSceneObjectsSync.Count; ++i)
{
var noStart = writer.Position;
- BytePacker.WriteValuePacked(writer, m_DespawnedInSceneObjectsSync[i].GetSceneOriginHandle());
- BytePacker.WriteValuePacked(writer, m_DespawnedInSceneObjectsSync[i].GlobalObjectIdHash);
+ writer.WriteValueSafe(m_DespawnedInSceneObjectsSync[i].GetSceneOriginHandle());
+ writer.WriteValueSafe(m_DespawnedInSceneObjectsSync[i].GlobalObjectIdHash);
var noStop = writer.Position;
totalBytes += (int)(noStop - noStart);
}
@@ -497,8 +502,6 @@ namespace Unity.Netcode
{
if (keyValuePairBySceneHandle.Value.Observers.Contains(TargetClientId))
{
- // Write our server relative scene handle for the NetworkObject being serialized
- writer.WriteValueSafe(keyValuePairBySceneHandle.Key);
// Serialize the NetworkObject
var sceneObject = keyValuePairBySceneHandle.Value.GetMessageSceneObject(TargetClientId);
sceneObject.Serialize(writer);
@@ -512,8 +515,8 @@ namespace Unity.Netcode
// Write the scene handle and GlobalObjectIdHash value
for (var i = 0; i < m_DespawnedInSceneObjectsSync.Count; ++i)
{
- BytePacker.WriteValuePacked(writer, m_DespawnedInSceneObjectsSync[i].GetSceneOriginHandle());
- BytePacker.WriteValuePacked(writer, m_DespawnedInSceneObjectsSync[i].GlobalObjectIdHash);
+ writer.WriteValueSafe(m_DespawnedInSceneObjectsSync[i].GetSceneOriginHandle());
+ writer.WriteValueSafe(m_DespawnedInSceneObjectsSync[i].GlobalObjectIdHash);
}
var tailPosition = writer.Position;
@@ -533,7 +536,8 @@ namespace Unity.Netcode
internal void Deserialize(FastBufferReader reader)
{
reader.ReadValueSafe(out SceneEventType);
- reader.ReadValueSafe(out LoadSceneMode);
+ reader.ReadValueSafe(out byte loadSceneMode);
+ LoadSceneMode = (LoadSceneMode)loadSceneMode;
if (SceneEventType != SceneEventType.Synchronize)
{
@@ -624,13 +628,15 @@ namespace Unity.Netcode
for (ushort i = 0; i < newObjectsCount; i++)
{
- InternalBuffer.ReadValueSafe(out int sceneHandle);
- // Set our relative scene to the NetworkObject
- m_NetworkManager.SceneManager.SetTheSceneBeingSynchronized(sceneHandle);
-
- // Deserialize the NetworkObject
var sceneObject = new NetworkObject.SceneObject();
sceneObject.Deserialize(InternalBuffer);
+
+ if (sceneObject.IsSceneObject)
+ {
+ // Set our relative scene to the NetworkObject
+ m_NetworkManager.SceneManager.SetTheSceneBeingSynchronized(sceneObject.NetworkSceneHandle);
+ }
+
NetworkObject.AddSceneObject(sceneObject, InternalBuffer, m_NetworkManager);
}
// Now deserialize the despawned in-scene placed NetworkObjects list (if any)
@@ -656,7 +662,11 @@ namespace Unity.Netcode
if (networkObjectsToRemove.Length > 0)
{
+#if UNITY_2023_1_OR_NEWER
+ var networkObjects = UnityEngine.Object.FindObjectsByType(UnityEngine.FindObjectsSortMode.InstanceID);
+#else
var networkObjects = UnityEngine.Object.FindObjectsOfType();
+#endif
var networkObjectIdToNetworkObject = new Dictionary();
foreach (var networkObject in networkObjects)
{
@@ -771,8 +781,8 @@ namespace Unity.Netcode
for (int i = 0; i < despawnedObjectsCount; i++)
{
// We just need to get the scene
- ByteUnpacker.ReadValuePacked(InternalBuffer, out int networkSceneHandle);
- ByteUnpacker.ReadValuePacked(InternalBuffer, out uint globalObjectIdHash);
+ InternalBuffer.ReadValueSafe(out int networkSceneHandle);
+ InternalBuffer.ReadValueSafe(out uint globalObjectIdHash);
var sceneRelativeNetworkObjects = new Dictionary();
if (!sceneCache.ContainsKey(networkSceneHandle))
{
@@ -784,8 +794,14 @@ namespace Unity.Netcode
var objectRelativeScene = m_NetworkManager.SceneManager.ScenesLoaded[localSceneHandle];
// Find all active and non-active in-scene placed NetworkObjects
+#if UNITY_2023_1_OR_NEWER
+ var inSceneNetworkObjects = UnityEngine.Object.FindObjectsByType(UnityEngine.FindObjectsInactive.Include, UnityEngine.FindObjectsSortMode.InstanceID).Where((c) =>
+ c.GetSceneOriginHandle() == localSceneHandle && (c.IsSceneObject != false)).ToList();
+#else
var inSceneNetworkObjects = UnityEngine.Object.FindObjectsOfType(includeInactive: true).Where((c) =>
c.GetSceneOriginHandle() == localSceneHandle && (c.IsSceneObject != false)).ToList();
+#endif
+
foreach (var inSceneObject in inSceneNetworkObjects)
{
@@ -847,24 +863,26 @@ namespace Unity.Netcode
try
{
// Process all spawned NetworkObjects for this network session
- ByteUnpacker.ReadValuePacked(InternalBuffer, out int newObjectsCount);
-
-
+ InternalBuffer.ReadValueSafe(out int newObjectsCount);
for (int i = 0; i < newObjectsCount; i++)
{
- // We want to make sure for each NetworkObject we have the appropriate scene selected as the scene that is
- // currently being synchronized. This assures in-scene placed NetworkObjects will use the right NetworkObject
- // from the list of populated
- ByteUnpacker.ReadValuePacked(InternalBuffer, out int handle);
- m_NetworkManager.SceneManager.SetTheSceneBeingSynchronized(handle);
-
var sceneObject = new NetworkObject.SceneObject();
sceneObject.Deserialize(InternalBuffer);
- var spawnedNetworkObject = NetworkObject.AddSceneObject(sceneObject, InternalBuffer, networkManager);
- if (!m_NetworkObjectsSync.Contains(spawnedNetworkObject))
+ // If the sceneObject is in-scene placed, then set the scene being synchronized
+ if (sceneObject.IsSceneObject)
{
- m_NetworkObjectsSync.Add(spawnedNetworkObject);
+ m_NetworkManager.SceneManager.SetTheSceneBeingSynchronized(sceneObject.NetworkSceneHandle);
+ }
+ var spawnedNetworkObject = NetworkObject.AddSceneObject(sceneObject, InternalBuffer, networkManager);
+
+ // If we failed to deserialize the NetowrkObject then don't add null to the list
+ if (spawnedNetworkObject != null)
+ {
+ if (!m_NetworkObjectsSync.Contains(spawnedNetworkObject))
+ {
+ m_NetworkObjectsSync.Add(spawnedNetworkObject);
+ }
}
}
diff --git a/Runtime/SceneManagement/SceneEventProgress.cs b/Runtime/SceneManagement/SceneEventProgress.cs
index bce5389..6ab998c 100644
--- a/Runtime/SceneManagement/SceneEventProgress.cs
+++ b/Runtime/SceneManagement/SceneEventProgress.cs
@@ -108,19 +108,37 @@ namespace Unity.Netcode
internal List GetClientsWithStatus(bool completedSceneEvent)
{
var clients = new List();
- foreach (var clientStatus in ClientsProcessingSceneEvent)
+ if (completedSceneEvent)
{
- if (clientStatus.Value == completedSceneEvent)
+ // If we are the host, then add the host-client to the list
+ // of clients that completed if the AsyncOperation is done.
+ if (m_NetworkManager.IsHost && m_AsyncOperation.isDone)
{
- clients.Add(clientStatus.Key);
+ clients.Add(m_NetworkManager.LocalClientId);
+ }
+
+ // Add all clients that completed the scene event
+ foreach (var clientStatus in ClientsProcessingSceneEvent)
+ {
+ if (clientStatus.Value == completedSceneEvent)
+ {
+ clients.Add(clientStatus.Key);
+ }
}
}
-
- // If we are getting the list of clients that have not completed the
- // scene event, then add any clients that disconnected during this
- // scene event.
- if (!completedSceneEvent)
+ else
{
+ // If we are the host, then add the host-client to the list
+ // of clients that did not complete if the AsyncOperation is
+ // not done.
+ if (m_NetworkManager.IsHost && !m_AsyncOperation.isDone)
+ {
+ clients.Add(m_NetworkManager.LocalClientId);
+ }
+
+ // If we are getting the list of clients that have not completed the
+ // scene event, then add any clients that disconnected during this
+ // scene event.
clients.AddRange(ClientsThatDisconnected);
}
return clients;
@@ -138,6 +156,11 @@ namespace Unity.Netcode
// Track the clients that were connected when we started this event
foreach (var connectedClientId in networkManager.ConnectedClientsIds)
{
+ // Ignore the host client
+ if (NetworkManager.ServerClientId == connectedClientId)
+ {
+ continue;
+ }
ClientsProcessingSceneEvent.Add(connectedClientId, false);
}
@@ -218,7 +241,10 @@ namespace Unity.Netcode
}
// Return the local scene event's AsyncOperation status
- return m_AsyncOperation.isDone;
+ // Note: Integration tests process scene loading through a queue
+ // and the AsyncOperation could not be assigned for several
+ // network tick periods. Return false if that is the case.
+ return m_AsyncOperation == null ? false : m_AsyncOperation.isDone;
}
///
diff --git a/Runtime/Serialization/BytePacker.cs b/Runtime/Serialization/BytePacker.cs
index 737faf7..58b7ea1 100644
--- a/Runtime/Serialization/BytePacker.cs
+++ b/Runtime/Serialization/BytePacker.cs
@@ -50,7 +50,7 @@ namespace Unity.Netcode
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteValuePacked(FastBufferWriter writer, float value)
{
- WriteUInt32Packed(writer, ToUint(value));
+ WriteValueBitPacked(writer, ToUint(value));
}
///
@@ -61,7 +61,7 @@ namespace Unity.Netcode
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteValuePacked(FastBufferWriter writer, double value)
{
- WriteUInt64Packed(writer, ToUlong(value));
+ WriteValueBitPacked(writer, ToUlong(value));
}
///
@@ -98,7 +98,7 @@ namespace Unity.Netcode
/// The writer to write to
/// Value to write
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void WriteValuePacked(FastBufferWriter writer, short value) => WriteUInt32Packed(writer, (ushort)Arithmetic.ZigZagEncode(value));
+ public static void WriteValuePacked(FastBufferWriter writer, short value) => WriteValueBitPacked(writer, value);
///
/// Write an unsigned short (UInt16) as a varint to the buffer.
@@ -109,7 +109,7 @@ namespace Unity.Netcode
/// The writer to write to
/// Value to write
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void WriteValuePacked(FastBufferWriter writer, ushort value) => WriteUInt32Packed(writer, value);
+ public static void WriteValuePacked(FastBufferWriter writer, ushort value) => WriteValueBitPacked(writer, value);
///
/// Write a two-byte character as a varint to the buffer.
@@ -120,7 +120,7 @@ namespace Unity.Netcode
/// The writer to write to
/// Value to write
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void WriteValuePacked(FastBufferWriter writer, char c) => WriteUInt32Packed(writer, c);
+ public static void WriteValuePacked(FastBufferWriter writer, char c) => WriteValueBitPacked(writer, c);
///
/// Write a signed int (Int32) as a ZigZag encoded varint to the buffer.
@@ -128,7 +128,7 @@ namespace Unity.Netcode
/// The writer to write to
/// Value to write
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void WriteValuePacked(FastBufferWriter writer, int value) => WriteUInt32Packed(writer, (uint)Arithmetic.ZigZagEncode(value));
+ public static void WriteValuePacked(FastBufferWriter writer, int value) => WriteValueBitPacked(writer, value);
///
/// Write an unsigned int (UInt32) to the buffer.
@@ -136,7 +136,7 @@ namespace Unity.Netcode
/// The writer to write to
/// Value to write
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void WriteValuePacked(FastBufferWriter writer, uint value) => WriteUInt32Packed(writer, value);
+ public static void WriteValuePacked(FastBufferWriter writer, uint value) => WriteValueBitPacked(writer, value);
///
/// Write an unsigned long (UInt64) to the buffer.
@@ -144,7 +144,7 @@ namespace Unity.Netcode
/// The writer to write to
/// Value to write
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void WriteValuePacked(FastBufferWriter writer, ulong value) => WriteUInt64Packed(writer, value);
+ public static void WriteValuePacked(FastBufferWriter writer, ulong value) => WriteValueBitPacked(writer, value);
///
/// Write a signed long (Int64) as a ZigZag encoded varint to the buffer.
@@ -152,7 +152,7 @@ namespace Unity.Netcode
/// The writer to write to
/// Value to write
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void WriteValuePacked(FastBufferWriter writer, long value) => WriteUInt64Packed(writer, Arithmetic.ZigZagEncode(value));
+ public static void WriteValuePacked(FastBufferWriter writer, long value) => WriteValueBitPacked(writer, value);
///
/// Convenience method that writes two packed Vector3 from the ray to the buffer
@@ -282,231 +282,183 @@ namespace Unity.Netcode
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueBitPacked(FastBufferWriter writer, T value) where T: unmanaged => writer.WriteValueSafe(value);
#else
-
///
- /// Maximum serializable value for a BitPacked ushort (minimum for unsigned is 0)
+ /// Obsolete value that no longer carries meaning. Do not use.
///
public const ushort BitPackedUshortMax = (1 << 15) - 1;
///
- /// Maximum serializable value for a BitPacked short
+ /// Obsolete value that no longer carries meaning. Do not use.
///
public const short BitPackedShortMax = (1 << 14) - 1;
///
- /// Minimum serializable value size for a BitPacked ushort
+ /// Obsolete value that no longer carries meaning. Do not use.
///
public const short BitPackedShortMin = -(1 << 14);
///
- /// Maximum serializable value for a BitPacked uint (minimum for unsigned is 0)
+ /// Obsolete value that no longer carries meaning. Do not use.
///
public const uint BitPackedUintMax = (1 << 30) - 1;
///
- /// Maximum serializable value for a BitPacked int
+ /// Obsolete value that no longer carries meaning. Do not use.
///
public const int BitPackedIntMax = (1 << 29) - 1;
///
- /// Minimum serializable value size for a BitPacked int
+ /// Obsolete value that no longer carries meaning. Do not use.
///
public const int BitPackedIntMin = -(1 << 29);
///
- /// Maximum serializable value for a BitPacked ulong (minimum for unsigned is 0)
+ /// Obsolete value that no longer carries meaning. Do not use.
///
public const ulong BitPackedULongMax = (1L << 61) - 1;
///
- /// Maximum serializable value for a BitPacked long
+ /// Obsolete value that no longer carries meaning. Do not use.
///
public const long BitPackedLongMax = (1L << 60) - 1;
///
- /// Minimum serializable value size for a BitPacked long
+ /// Obsolete value that no longer carries meaning. Do not use.
///
public const long BitPackedLongMin = -(1L << 60);
///
- /// Writes a 14-bit signed short to the buffer in a bit-encoded packed format.
- /// The first bit indicates whether the value is 1 byte or 2.
- /// The sign bit takes up another bit.
- /// That leaves 14 bits for the value.
- /// A value greater than 2^14-1 or less than -2^14 will throw an exception in editor and development builds.
- /// In release builds builds the exception is not thrown and the value is truncated by losing its two
- /// most significant bits after zig-zag encoding.
+ /// Writes a 16-bit signed short to the buffer in a bit-encoded packed format.
+ /// Zig-zag encoding is used to move the sign bit to the least significant bit, so that negative values
+ /// are still able to be compressed.
+ /// The first two bits indicate whether the value is 1, 2, or 3 bytes.
+ /// If the value uses 14 bits or less, the remaining 14 bits contain the value.
+ /// For performance, reasons, if the value is 15 bits or more, there will be six 0 bits, followed
+ /// by the original unmodified 16-bit value in the next 2 bytes.
///
/// The writer to write to
/// The value to pack
public static void WriteValueBitPacked(FastBufferWriter writer, short value) => WriteValueBitPacked(writer, (ushort)Arithmetic.ZigZagEncode(value));
///
- /// Writes a 15-bit unsigned short to the buffer in a bit-encoded packed format.
- /// The first bit indicates whether the value is 1 byte or 2.
- /// That leaves 15 bits for the value.
- /// A value greater than 2^15-1 will throw an exception in editor and development builds.
- /// In release builds builds the exception is not thrown and the value is truncated by losing its
- /// most significant bit.
+ /// Writes a 16-bit unsigned short to the buffer in a bit-encoded packed format.
+ /// The first two bits indicate whether the value is 1, 2, or 3 bytes.
+ /// If the value uses 14 bits or less, the remaining 14 bits contain the value.
+ /// For performance, reasons, if the value is 15 bits or more, there will be six 0 bits, followed
+ /// by the original unmodified 16-bit value in the next 2 bytes.
///
/// The writer to write to
/// The value to pack
public static void WriteValueBitPacked(FastBufferWriter writer, ushort value)
{
-#if DEVELOPMENT_BUILD || UNITY_EDITOR
- if (value >= BitPackedUshortMax)
+ if (value > (1 << 14) - 1)
{
- throw new ArgumentException("BitPacked ushorts must be <= 15 bits");
- }
-#endif
-
- if (value <= 0b0111_1111)
- {
- if (!writer.TryBeginWriteInternal(1))
+ if (!writer.TryBeginWriteInternal(3))
{
throw new OverflowException("Writing past the end of the buffer");
}
- writer.WriteByte((byte)(value << 1));
+ writer.WriteByte(3);
+ writer.WriteValue(value);
return;
}
- if (!writer.TryBeginWriteInternal(2))
- {
- throw new OverflowException("Writing past the end of the buffer");
- }
- writer.WriteValue((ushort)((value << 1) | 0b1));
- }
-
- ///
- /// Writes a 29-bit signed int to the buffer in a bit-encoded packed format.
- /// The first two bits indicate whether the value is 1, 2, 3, or 4 bytes.
- /// The sign bit takes up another bit.
- /// That leaves 29 bits for the value.
- /// A value greater than 2^29-1 or less than -2^29 will throw an exception in editor and development builds.
- /// In release builds builds the exception is not thrown and the value is truncated by losing its three
- /// most significant bits after zig-zag encoding.
- ///
- /// The writer to write to
- /// The value to pack
- public static void WriteValueBitPacked(FastBufferWriter writer, int value) => WriteValueBitPacked(writer, (uint)Arithmetic.ZigZagEncode(value));
-
- ///
- /// Writes a 30-bit unsigned int to the buffer in a bit-encoded packed format.
- /// The first two bits indicate whether the value is 1, 2, 3, or 4 bytes.
- /// That leaves 30 bits for the value.
- /// A value greater than 2^30-1 will throw an exception in editor and development builds.
- /// In release builds builds the exception is not thrown and the value is truncated by losing its two
- /// most significant bits.
- ///
- /// The writer to write to
- /// The value to pack
- public static void WriteValueBitPacked(FastBufferWriter writer, uint value)
- {
-#if DEVELOPMENT_BUILD || UNITY_EDITOR
- if (value > BitPackedUintMax)
- {
- throw new ArgumentException("BitPacked uints must be <= 30 bits");
- }
-#endif
value <<= 2;
var numBytes = BitCounter.GetUsedByteCount(value);
if (!writer.TryBeginWriteInternal(numBytes))
{
throw new OverflowException("Writing past the end of the buffer");
}
- writer.WritePartialValue(value | (uint)(numBytes - 1), numBytes);
+ writer.WritePartialValue(value | (ushort)(numBytes), numBytes);
}
///
- /// Writes a 60-bit signed long to the buffer in a bit-encoded packed format.
- /// The first three bits indicate whether the value is 1, 2, 3, 4, 5, 6, 7, or 8 bytes.
- /// The sign bit takes up another bit.
- /// That leaves 60 bits for the value.
- /// A value greater than 2^60-1 or less than -2^60 will throw an exception in editor and development builds.
- /// In release builds builds the exception is not thrown and the value is truncated by losing its four
- /// most significant bits after zig-zag encoding.
+ /// Writes a 32-bit signed int to the buffer in a bit-encoded packed format.
+ /// Zig-zag encoding is used to move the sign bit to the least significant bit, so that negative values
+ /// are still able to be compressed.
+ /// The first three bits indicate whether the value is 1, 2, 3, 4, or 5 bytes.
+ /// If the value uses 29 bits or less, the remaining 29 bits contain the value.
+ /// For performance, reasons, if the value is 30 bits or more, there will be five 0 bits, followed
+ /// by the original unmodified 32-bit value in the next 4 bytes.
///
/// The writer to write to
/// The value to pack
- public static void WriteValueBitPacked(FastBufferWriter writer, long value) => WriteValueBitPacked(writer, Arithmetic.ZigZagEncode(value));
+ public static void WriteValueBitPacked(FastBufferWriter writer, int value) => WriteValueBitPacked(writer, (uint)Arithmetic.ZigZagEncode(value));
///
- /// Writes a 61-bit unsigned long to the buffer in a bit-encoded packed format.
- /// The first three bits indicate whether the value is 1, 2, 3, 4, 5, 6, 7, or 8 bytes.
- /// That leaves 31 bits for the value.
- /// A value greater than 2^61-1 will throw an exception in editor and development builds.
- /// In release builds builds the exception is not thrown and the value is truncated by losing its three
- /// most significant bits.
+ /// Writes a 32-bit unsigned int to the buffer in a bit-encoded packed format.
+ /// The first three bits indicate whether the value is 1, 2, 3, 4, or 5 bytes.
+ /// If the value uses 29 bits or less, the remaining 29 bits contain the value.
+ /// For performance, reasons, if the value is 30 bits or more, there will be five 0 bits, followed
+ /// by the original unmodified 32-bit value in the next 4 bytes.
///
/// The writer to write to
/// The value to pack
- public static void WriteValueBitPacked(FastBufferWriter writer, ulong value)
+ public static void WriteValueBitPacked(FastBufferWriter writer, uint value)
{
-#if DEVELOPMENT_BUILD || UNITY_EDITOR
- if (value > BitPackedULongMax)
+ if (value > (1 << 29) - 1)
{
- throw new ArgumentException("BitPacked ulongs must be <= 61 bits");
+ if (!writer.TryBeginWriteInternal(5))
+ {
+ throw new OverflowException("Writing past the end of the buffer");
+ }
+ writer.WriteByte(5);
+ writer.WriteValue(value);
+ return;
}
-#endif
+
value <<= 3;
var numBytes = BitCounter.GetUsedByteCount(value);
if (!writer.TryBeginWriteInternal(numBytes))
{
throw new OverflowException("Writing past the end of the buffer");
}
- writer.WritePartialValue(value | (uint)(numBytes - 1), numBytes);
+ writer.WritePartialValue(value | (uint)(numBytes), numBytes);
+ }
+
+ ///
+ /// Writes a 64-bit signed long to the buffer in a bit-encoded packed format.
+ /// Zig-zag encoding is used to move the sign bit to the least significant bit, so that negative values
+ /// are still able to be compressed.
+ /// The first four bits indicate whether the value is 1, 2, 3, 4, 5, 6, 7, 8, or 9 bytes.
+ /// If the value uses 60 bits or less, the remaining 60 bits contain the value.
+ /// For performance, reasons, if the value is 61 bits or more, there will be four 0 bits, followed
+ /// by the original unmodified 64-bit value in the next 8 bytes.
+ ///
+ /// The writer to write to
+ /// The value to pack
+ public static void WriteValueBitPacked(FastBufferWriter writer, long value) => WriteValueBitPacked(writer, Arithmetic.ZigZagEncode(value));
+
+ ///
+ /// Writes a 64-bit unsigned long to the buffer in a bit-encoded packed format.
+ /// The first four bits indicate whether the value is 1, 2, 3, 4, 5, 6, 7, 8, or 9 bytes.
+ /// If the value uses 60 bits or less, the remaining 60 bits contain the value.
+ /// For performance, reasons, if the value is 61 bits or more, there will be four 0 bits, followed
+ /// by the original unmodified 64-bit value in the next 8 bytes.
+ ///
+ /// The writer to write to
+ /// The value to pack
+ public static void WriteValueBitPacked(FastBufferWriter writer, ulong value)
+ {
+ if (value > (1L << 60) - 1)
+ {
+ if (!writer.TryBeginWriteInternal(9))
+ {
+ throw new OverflowException("Writing past the end of the buffer");
+ }
+ writer.WriteByte(9);
+ writer.WriteValue(value);
+ return;
+ }
+
+ value <<= 4;
+ var numBytes = BitCounter.GetUsedByteCount(value);
+ if (!writer.TryBeginWriteInternal(numBytes))
+ {
+ throw new OverflowException("Writing past the end of the buffer");
+ }
+ writer.WritePartialValue(value | (uint)(numBytes), numBytes);
}
#endif
-
- private static void WriteUInt64Packed(FastBufferWriter writer, ulong value)
- {
- if (value <= 240)
- {
- writer.WriteByteSafe((byte)value);
- return;
- }
- if (value <= 2287)
- {
- writer.WriteByteSafe((byte)(((value - 240) >> 8) + 241));
- writer.WriteByteSafe((byte)(value - 240));
- return;
- }
- var writeBytes = BitCounter.GetUsedByteCount(value);
-
- if (!writer.TryBeginWriteInternal(writeBytes + 1))
- {
- throw new OverflowException("Writing past the end of the buffer");
- }
- writer.WriteByte((byte)(247 + writeBytes));
- writer.WritePartialValue(value, writeBytes);
- }
-
- // Looks like the same code as WriteUInt64Packed?
- // It's actually different because it will call the more efficient 32-bit version
- // of BytewiseUtility.GetUsedByteCount().
- private static void WriteUInt32Packed(FastBufferWriter writer, uint value)
- {
- if (value <= 240)
- {
- writer.WriteByteSafe((byte)value);
- return;
- }
- if (value <= 2287)
- {
- writer.WriteByteSafe((byte)(((value - 240) >> 8) + 241));
- writer.WriteByteSafe((byte)(value - 240));
- return;
- }
- var writeBytes = BitCounter.GetUsedByteCount(value);
-
- if (!writer.TryBeginWriteInternal(writeBytes + 1))
- {
- throw new OverflowException("Writing past the end of the buffer");
- }
- writer.WriteByte((byte)(247 + writeBytes));
- writer.WritePartialValue(value, writeBytes);
- }
-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe uint ToUint(T value) where T : unmanaged
{
diff --git a/Runtime/Serialization/ByteUnpacker.cs b/Runtime/Serialization/ByteUnpacker.cs
index a84a0d9..2d2cd88 100644
--- a/Runtime/Serialization/ByteUnpacker.cs
+++ b/Runtime/Serialization/ByteUnpacker.cs
@@ -11,7 +11,6 @@ namespace Unity.Netcode
///
public static class ByteUnpacker
{
-
#if UNITY_NETCODE_DEBUG_NO_PACKING
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -58,7 +57,7 @@ namespace Unity.Netcode
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ReadValuePacked(FastBufferReader reader, out float value)
{
- ReadUInt32Packed(reader, out uint asUInt);
+ ReadValueBitPacked(reader, out uint asUInt);
value = ToSingle(asUInt);
}
@@ -70,7 +69,7 @@ namespace Unity.Netcode
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ReadValuePacked(FastBufferReader reader, out double value)
{
- ReadUInt64Packed(reader, out ulong asULong);
+ ReadValueBitPacked(reader, out ulong asULong);
value = ToDouble(asULong);
}
@@ -109,11 +108,7 @@ namespace Unity.Netcode
/// The reader to read from
/// Value to read
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void ReadValuePacked(FastBufferReader reader, out short value)
- {
- ReadUInt32Packed(reader, out uint readValue);
- value = (short)Arithmetic.ZigZagDecode(readValue);
- }
+ public static void ReadValuePacked(FastBufferReader reader, out short value) => ReadValueBitPacked(reader, out value);
///
/// Read an unsigned short (UInt16) as a varint from the stream.
@@ -121,11 +116,7 @@ namespace Unity.Netcode
/// The reader to read from
/// Value to read
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void ReadValuePacked(FastBufferReader reader, out ushort value)
- {
- ReadUInt32Packed(reader, out uint readValue);
- value = (ushort)readValue;
- }
+ public static void ReadValuePacked(FastBufferReader reader, out ushort value) => ReadValueBitPacked(reader, out value);
///
/// Read a two-byte character as a varint from the stream.
@@ -135,7 +126,7 @@ namespace Unity.Netcode
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ReadValuePacked(FastBufferReader reader, out char c)
{
- ReadUInt32Packed(reader, out uint readValue);
+ ReadValueBitPacked(reader, out ushort readValue);
c = (char)readValue;
}
@@ -145,11 +136,7 @@ namespace Unity.Netcode
/// The reader to read from
/// Value to read
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void ReadValuePacked(FastBufferReader reader, out int value)
- {
- ReadUInt32Packed(reader, out uint readValue);
- value = (int)Arithmetic.ZigZagDecode(readValue);
- }
+ public static void ReadValuePacked(FastBufferReader reader, out int value) => ReadValueBitPacked(reader, out value);
///
/// Read an unsigned int (UInt32) from the stream.
@@ -157,7 +144,7 @@ namespace Unity.Netcode
/// The reader to read from
/// Value to read
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void ReadValuePacked(FastBufferReader reader, out uint value) => ReadUInt32Packed(reader, out value);
+ public static void ReadValuePacked(FastBufferReader reader, out uint value) => ReadValueBitPacked(reader, out value);
///
/// Read an unsigned long (UInt64) from the stream.
@@ -165,7 +152,7 @@ namespace Unity.Netcode
/// The reader to read from
/// Value to read
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void ReadValuePacked(FastBufferReader reader, out ulong value) => ReadUInt64Packed(reader, out value);
+ public static void ReadValuePacked(FastBufferReader reader, out ulong value) => ReadValueBitPacked(reader, out value);
///
/// Read a signed long (Int64) as a ZigZag encoded varint from the stream.
@@ -175,8 +162,7 @@ namespace Unity.Netcode
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ReadValuePacked(FastBufferReader reader, out long value)
{
- ReadUInt64Packed(reader, out ulong readValue);
- value = Arithmetic.ZigZagDecode(readValue);
+ ReadValueBitPacked(reader, out value);
}
///
@@ -341,7 +327,9 @@ namespace Unity.Netcode
ushort returnValue = 0;
byte* ptr = ((byte*)&returnValue);
byte* data = reader.GetUnsafePtrAtCurrentPosition();
- int numBytes = (data[0] & 0b1) + 1;
+ // Mask out the first two bits - they contain the total byte count
+ // (1, 2, or 3)
+ int numBytes = (data[0] & 0b11);
if (!reader.TryBeginReadInternal(numBytes))
{
throw new OverflowException("Reading past the end of the buffer");
@@ -350,17 +338,23 @@ namespace Unity.Netcode
switch (numBytes)
{
case 1:
- *ptr = *data;
+ ptr[0] = data[0];
break;
case 2:
- *ptr = *data;
- *(ptr + 1) = *(data + 1);
+ ptr[0] = data[0];
+ ptr[1] = data[1];
break;
+ case 3:
+ // First byte contains no data, it's just a marker. The data is in the remaining two bytes.
+ ptr[0] = data[1];
+ ptr[1] = data[2];
+ value = returnValue;
+ return;
default:
throw new InvalidOperationException("Could not read bit-packed value: impossible byte count");
}
- value = (ushort)(returnValue >> 1);
+ value = (ushort)(returnValue >> 2);
}
///
@@ -386,7 +380,8 @@ namespace Unity.Netcode
uint returnValue = 0;
byte* ptr = ((byte*)&returnValue);
byte* data = reader.GetUnsafePtrAtCurrentPosition();
- int numBytes = (data[0] & 0b11) + 1;
+ // Mask out the first three bits - they contain the total byte count (1-5)
+ int numBytes = (data[0] & 0b111);
if (!reader.TryBeginReadInternal(numBytes))
{
throw new OverflowException("Reading past the end of the buffer");
@@ -395,26 +390,34 @@ namespace Unity.Netcode
switch (numBytes)
{
case 1:
- *ptr = *data;
+ ptr[0] = data[0];
break;
case 2:
- *ptr = *data;
- *(ptr + 1) = *(data + 1);
+ ptr[0] = data[0];
+ ptr[1] = data[1];
break;
case 3:
- *ptr = *data;
- *(ptr + 1) = *(data + 1);
- *(ptr + 2) = *(data + 2);
+ ptr[0] = data[0];
+ ptr[1] = data[1];
+ ptr[2] = data[2];
break;
case 4:
- *ptr = *data;
- *(ptr + 1) = *(data + 1);
- *(ptr + 2) = *(data + 2);
- *(ptr + 3) = *(data + 3);
+ ptr[0] = data[0];
+ ptr[1] = data[1];
+ ptr[2] = data[2];
+ ptr[3] = data[3];
break;
+ case 5:
+ // First byte contains no data, it's just a marker. The data is in the remaining two bytes.
+ ptr[0] = data[1];
+ ptr[1] = data[2];
+ ptr[2] = data[3];
+ ptr[3] = data[4];
+ value = returnValue;
+ return;
}
- value = returnValue >> 2;
+ value = returnValue >> 3;
}
///
@@ -440,7 +443,8 @@ namespace Unity.Netcode
ulong returnValue = 0;
byte* ptr = ((byte*)&returnValue);
byte* data = reader.GetUnsafePtrAtCurrentPosition();
- int numBytes = (data[0] & 0b111) + 1;
+ // Mask out the first four bits - they contain the total byte count (1-9)
+ int numBytes = (data[0] & 0b1111);
if (!reader.TryBeginReadInternal(numBytes))
{
throw new OverflowException("Reading past the end of the buffer");
@@ -449,109 +453,74 @@ namespace Unity.Netcode
switch (numBytes)
{
case 1:
- *ptr = *data;
+ ptr[0] = data[0];
break;
case 2:
- *ptr = *data;
- *(ptr + 1) = *(data + 1);
+ ptr[0] = data[0];
+ ptr[1] = data[1];
break;
case 3:
- *ptr = *data;
- *(ptr + 1) = *(data + 1);
- *(ptr + 2) = *(data + 2);
+ ptr[0] = data[0];
+ ptr[1] = data[1];
+ ptr[2] = data[2];
break;
case 4:
- *ptr = *data;
- *(ptr + 1) = *(data + 1);
- *(ptr + 2) = *(data + 2);
- *(ptr + 3) = *(data + 3);
+ ptr[0] = data[0];
+ ptr[1] = data[1];
+ ptr[2] = data[2];
+ ptr[3] = data[3];
break;
case 5:
- *ptr = *data;
- *(ptr + 1) = *(data + 1);
- *(ptr + 2) = *(data + 2);
- *(ptr + 3) = *(data + 3);
- *(ptr + 4) = *(data + 4);
+ ptr[0] = data[0];
+ ptr[1] = data[1];
+ ptr[2] = data[2];
+ ptr[3] = data[3];
+ ptr[4] = data[4];
break;
case 6:
- *ptr = *data;
- *(ptr + 1) = *(data + 1);
- *(ptr + 2) = *(data + 2);
- *(ptr + 3) = *(data + 3);
- *(ptr + 4) = *(data + 4);
- *(ptr + 5) = *(data + 5);
+ ptr[0] = data[0];
+ ptr[1] = data[1];
+ ptr[2] = data[2];
+ ptr[3] = data[3];
+ ptr[4] = data[4];
+ ptr[5] = data[5];
break;
case 7:
- *ptr = *data;
- *(ptr + 1) = *(data + 1);
- *(ptr + 2) = *(data + 2);
- *(ptr + 3) = *(data + 3);
- *(ptr + 4) = *(data + 4);
- *(ptr + 5) = *(data + 5);
- *(ptr + 6) = *(data + 6);
+ ptr[0] = data[0];
+ ptr[1] = data[1];
+ ptr[2] = data[2];
+ ptr[3] = data[3];
+ ptr[4] = data[4];
+ ptr[5] = data[5];
+ ptr[6] = data[6];
break;
case 8:
- *ptr = *data;
- *(ptr + 1) = *(data + 1);
- *(ptr + 2) = *(data + 2);
- *(ptr + 3) = *(data + 3);
- *(ptr + 4) = *(data + 4);
- *(ptr + 5) = *(data + 5);
- *(ptr + 6) = *(data + 6);
- *(ptr + 7) = *(data + 7);
+ ptr[0] = data[0];
+ ptr[1] = data[1];
+ ptr[2] = data[2];
+ ptr[3] = data[3];
+ ptr[4] = data[4];
+ ptr[5] = data[5];
+ ptr[6] = data[6];
+ ptr[7] = data[7];
break;
+ case 9:
+ // First byte contains no data, it's just a marker. The data is in the remaining two bytes.
+ ptr[0] = data[1];
+ ptr[1] = data[2];
+ ptr[2] = data[3];
+ ptr[3] = data[4];
+ ptr[4] = data[5];
+ ptr[5] = data[6];
+ ptr[6] = data[7];
+ ptr[7] = data[8];
+ value = returnValue;
+ return;
}
- value = returnValue >> 3;
+ value = returnValue >> 4;
}
#endif
- private static void ReadUInt64Packed(FastBufferReader reader, out ulong value)
- {
- reader.ReadByteSafe(out byte firstByte);
- if (firstByte <= 240)
- {
- value = firstByte;
- return;
- }
-
- if (firstByte <= 248)
- {
- reader.ReadByteSafe(out byte secondByte);
- value = 240UL + ((firstByte - 241UL) << 8) + secondByte;
- return;
- }
-
- var numBytes = firstByte - 247;
- if (!reader.TryBeginReadInternal(numBytes))
- {
- throw new OverflowException("Reading past the end of the buffer");
- }
- reader.ReadPartialValue(out value, numBytes);
- }
-
- private static void ReadUInt32Packed(FastBufferReader reader, out uint value)
- {
- reader.ReadByteSafe(out byte firstByte);
- if (firstByte <= 240)
- {
- value = firstByte;
- return;
- }
-
- if (firstByte <= 248)
- {
- reader.ReadByteSafe(out byte secondByte);
- value = 240U + ((firstByte - 241U) << 8) + secondByte;
- return;
- }
-
- var numBytes = firstByte - 247;
- if (!reader.TryBeginReadInternal(numBytes))
- {
- throw new OverflowException("Reading past the end of the buffer");
- }
- reader.ReadPartialValue(out value, numBytes);
- }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe float ToSingle(T value) where T : unmanaged
diff --git a/Runtime/Serialization/ByteUtility.cs b/Runtime/Serialization/ByteUtility.cs
new file mode 100644
index 0000000..45ad1c7
--- /dev/null
+++ b/Runtime/Serialization/ByteUtility.cs
@@ -0,0 +1,58 @@
+using System.Runtime.CompilerServices;
+
+namespace Unity.Netcode
+{
+ internal class ByteUtility
+ {
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static unsafe byte ToByte(bool b) => *(byte*)&b;
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static bool GetBit(byte bitField, ushort bitPosition)
+ {
+ return (bitField & (1 << bitPosition)) != 0;
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static void SetBit(ref byte bitField, ushort bitPosition, bool value)
+ {
+ bitField = (byte)((bitField & ~(1 << bitPosition)) | (ToByte(value) << bitPosition));
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static bool GetBit(ushort bitField, ushort bitPosition)
+ {
+ return (bitField & (1 << bitPosition)) != 0;
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static void SetBit(ref ushort bitField, ushort bitPosition, bool value)
+ {
+ bitField = (ushort)((bitField & ~(1 << bitPosition)) | (ToByte(value) << bitPosition));
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static bool GetBit(uint bitField, ushort bitPosition)
+ {
+ return (bitField & (1 << bitPosition)) != 0;
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static void SetBit(ref uint bitField, ushort bitPosition, bool value)
+ {
+ bitField = (uint)((bitField & ~(1 << bitPosition)) | ((uint)ToByte(value) << bitPosition));
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static bool GetBit(ulong bitField, ushort bitPosition)
+ {
+ return (bitField & (ulong)(1 << bitPosition)) != 0;
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static void SetBit(ref ulong bitField, ushort bitPosition, bool value)
+ {
+ bitField = ((bitField & (ulong)~(1 << bitPosition)) | ((ulong)ToByte(value) << bitPosition));
+ }
+ }
+}
diff --git a/Runtime/Serialization/ByteUtility.cs.meta b/Runtime/Serialization/ByteUtility.cs.meta
new file mode 100644
index 0000000..972bc25
--- /dev/null
+++ b/Runtime/Serialization/ByteUtility.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 25bb0dd7157c423b8cfe0ecf06e15ae5
+timeCreated: 1666711082
\ No newline at end of file
diff --git a/Runtime/Serialization/FastBufferReader.cs b/Runtime/Serialization/FastBufferReader.cs
index e052c95..180311c 100644
--- a/Runtime/Serialization/FastBufferReader.cs
+++ b/Runtime/Serialization/FastBufferReader.cs
@@ -65,7 +65,7 @@ namespace Unity.Netcode
ReaderHandle* readerHandle = null;
if (copyAllocator == Allocator.None)
{
- readerHandle = (ReaderHandle*)UnsafeUtility.Malloc(sizeof(ReaderHandle) + length, UnsafeUtility.AlignOf(), internalAllocator);
+ readerHandle = (ReaderHandle*)UnsafeUtility.Malloc(sizeof(ReaderHandle), UnsafeUtility.AlignOf(), internalAllocator);
readerHandle->BufferPointer = buffer;
readerHandle->Position = offset;
}
diff --git a/Runtime/Spawning/NetworkSpawnManager.cs b/Runtime/Spawning/NetworkSpawnManager.cs
index 45c190b..b354ffe 100644
--- a/Runtime/Spawning/NetworkSpawnManager.cs
+++ b/Runtime/Spawning/NetworkSpawnManager.cs
@@ -270,6 +270,9 @@ namespace Unity.Netcode
networkObject.OwnerClientId = clientId;
+ networkObject.MarkVariablesDirty(true);
+ NetworkManager.BehaviourUpdater.AddForUpdate(networkObject);
+
// Server adds entries for all client ownership
UpdateOwnershipTable(networkObject, networkObject.OwnerClientId);
@@ -291,13 +294,13 @@ namespace Unity.Netcode
internal bool HasPrefab(NetworkObject.SceneObject sceneObject)
{
- if (!NetworkManager.NetworkConfig.EnableSceneManagement || !sceneObject.Header.IsSceneObject)
+ if (!NetworkManager.NetworkConfig.EnableSceneManagement || !sceneObject.IsSceneObject)
{
- if (NetworkManager.PrefabHandler.ContainsHandler(sceneObject.Header.Hash))
+ if (NetworkManager.PrefabHandler.ContainsHandler(sceneObject.Hash))
{
return true;
}
- if (NetworkManager.NetworkConfig.NetworkPrefabOverrideLinks.TryGetValue(sceneObject.Header.Hash, out var networkPrefab))
+ if (NetworkManager.NetworkConfig.NetworkPrefabOverrideLinks.TryGetValue(sceneObject.Hash, out var networkPrefab))
{
switch (networkPrefab.Override)
{
@@ -312,7 +315,7 @@ namespace Unity.Netcode
return false;
}
- var networkObject = NetworkManager.SceneManager.GetSceneRelativeInSceneNetworkObject(sceneObject.Header.Hash, sceneObject.NetworkSceneHandle);
+ var networkObject = NetworkManager.SceneManager.GetSceneRelativeInSceneNetworkObject(sceneObject.Hash, sceneObject.NetworkSceneHandle);
return networkObject != null;
}
@@ -326,22 +329,22 @@ namespace Unity.Netcode
internal NetworkObject CreateLocalNetworkObject(NetworkObject.SceneObject sceneObject)
{
NetworkObject networkObject = null;
- var globalObjectIdHash = sceneObject.Header.Hash;
- var position = sceneObject.Header.HasTransform ? sceneObject.Transform.Position : default;
- var rotation = sceneObject.Header.HasTransform ? sceneObject.Transform.Rotation : default;
- var scale = sceneObject.Header.HasTransform ? sceneObject.Transform.Scale : default;
- var parentNetworkId = sceneObject.Header.HasParent ? sceneObject.ParentObjectId : default;
- var worldPositionStays = sceneObject.Header.HasParent ? sceneObject.WorldPositionStays : true;
+ var globalObjectIdHash = sceneObject.Hash;
+ var position = sceneObject.HasTransform ? sceneObject.Transform.Position : default;
+ var rotation = sceneObject.HasTransform ? sceneObject.Transform.Rotation : default;
+ var scale = sceneObject.HasTransform ? sceneObject.Transform.Scale : default;
+ var parentNetworkId = sceneObject.HasParent ? sceneObject.ParentObjectId : default;
+ var worldPositionStays = (!sceneObject.HasParent) || sceneObject.WorldPositionStays;
var isSpawnedByPrefabHandler = false;
// If scene management is disabled or the NetworkObject was dynamically spawned
- if (!NetworkManager.NetworkConfig.EnableSceneManagement || !sceneObject.Header.IsSceneObject)
+ if (!NetworkManager.NetworkConfig.EnableSceneManagement || !sceneObject.IsSceneObject)
{
// If the prefab hash has a registered INetworkPrefabInstanceHandler derived class
if (NetworkManager.PrefabHandler.ContainsHandler(globalObjectIdHash))
{
// Let the handler spawn the NetworkObject
- networkObject = NetworkManager.PrefabHandler.HandleNetworkPrefabSpawn(globalObjectIdHash, sceneObject.Header.OwnerClientId, position, rotation);
+ networkObject = NetworkManager.PrefabHandler.HandleNetworkPrefabSpawn(globalObjectIdHash, sceneObject.OwnerClientId, position, rotation);
networkObject.NetworkManagerOwner = NetworkManager;
isSpawnedByPrefabHandler = true;
}
@@ -402,12 +405,12 @@ namespace Unity.Netcode
if (networkObject != null)
{
- // SPECIAL CASE:
+ // SPECIAL CASE FOR IN-SCENE PLACED: (only when the parent has a NetworkObject)
// This is a special case scenario where a late joining client has joined and loaded one or
// more scenes that contain nested in-scene placed NetworkObject children yet the server's
// synchronization information does not indicate the NetworkObject in question has a parent.
// Under this scenario, we want to remove the parent before spawning and setting the transform values.
- if (sceneObject.Header.IsSceneObject && !sceneObject.Header.HasParent && networkObject.transform.parent != null)
+ if (sceneObject.IsSceneObject && !sceneObject.HasParent && networkObject.transform.parent != null)
{
// if the in-scene placed NetworkObject has a parent NetworkObject but the synchronization information does not
// include parenting, then we need to force the removal of that parent
@@ -421,9 +424,11 @@ namespace Unity.Netcode
// Set the transform unless we were spawned by a prefab handler
// Note: prefab handlers are provided the position and rotation
// but it is up to the user to set those values
- if (sceneObject.Header.HasTransform && !isSpawnedByPrefabHandler)
+ if (sceneObject.HasTransform && !isSpawnedByPrefabHandler)
{
- if (worldPositionStays)
+ // If world position stays is true or we have auto object parent synchronization disabled
+ // then we want to apply the position and rotation values world space relative
+ if (worldPositionStays || !networkObject.AutoObjectParentSync)
{
networkObject.transform.position = position;
networkObject.transform.rotation = rotation;
@@ -441,22 +446,31 @@ namespace Unity.Netcode
// the network prefab used to represent the player.
// Note: not doing this would set the player's scale to zero since
// that is the default value of Vector3.
- if (!sceneObject.Header.IsPlayerObject)
+ if (!sceneObject.IsPlayerObject)
{
+ // Since scale is always applied to local space scale, we do the transform
+ // space logic during serialization such that it works out whether AutoObjectParentSync
+ // is enabled or not (see NetworkObject.SceneObject)
networkObject.transform.localScale = scale;
}
}
- if (sceneObject.Header.HasParent)
+ if (sceneObject.HasParent)
{
- // Go ahead and set network parenting properties
- networkObject.SetNetworkParenting(parentNetworkId, worldPositionStays);
+ // Go ahead and set network parenting properties, if the latest parent is not set then pass in null
+ // (we always want to set worldPositionStays)
+ ulong? parentId = null;
+ if (sceneObject.IsLatestParentSet)
+ {
+ parentId = parentNetworkId;
+ }
+ networkObject.SetNetworkParenting(parentId, worldPositionStays);
}
// Dynamically spawned NetworkObjects that occur during a LoadSceneMode.Single load scene event are migrated into the DDOL
// until the scene is loaded. They are then migrated back into the newly loaded and currently active scene.
- if (!sceneObject.Header.IsSceneObject && NetworkSceneManager.IsSpawnedObjectsPendingInDontDestroyOnLoad)
+ if (!sceneObject.IsSceneObject && NetworkSceneManager.IsSpawnedObjectsPendingInDontDestroyOnLoad)
{
UnityEngine.Object.DontDestroyOnLoad(networkObject.gameObject);
}
@@ -490,8 +504,7 @@ namespace Unity.Netcode
}
// Ran on both server and client
- internal void SpawnNetworkObjectLocally(NetworkObject networkObject, in NetworkObject.SceneObject sceneObject,
- FastBufferReader variableData, bool destroyWithScene)
+ internal void SpawnNetworkObjectLocally(NetworkObject networkObject, in NetworkObject.SceneObject sceneObject, bool destroyWithScene)
{
if (networkObject == null)
{
@@ -503,9 +516,7 @@ namespace Unity.Netcode
throw new SpawnStateException("Object is already spawned");
}
- networkObject.SetNetworkVariableData(variableData);
-
- SpawnNetworkObjectLocallyCommon(networkObject, sceneObject.Header.NetworkObjectId, sceneObject.Header.IsSceneObject, sceneObject.Header.IsPlayerObject, sceneObject.Header.OwnerClientId, destroyWithScene);
+ SpawnNetworkObjectLocallyCommon(networkObject, sceneObject.NetworkObjectId, sceneObject.IsSceneObject, sceneObject.IsPlayerObject, sceneObject.OwnerClientId, destroyWithScene);
}
private void SpawnNetworkObjectLocallyCommon(NetworkObject networkObject, ulong networkId, bool sceneObject, bool playerObject, ulong ownerClientId, bool destroyWithScene)
@@ -647,7 +658,11 @@ namespace Unity.Netcode
// Makes scene objects ready to be reused
internal void ServerResetShudownStateForSceneObjects()
{
+#if UNITY_2023_1_OR_NEWER
+ var networkObjects = UnityEngine.Object.FindObjectsByType(FindObjectsSortMode.InstanceID).Where((c) => c.IsSceneObject != null && c.IsSceneObject == true);
+#else
var networkObjects = UnityEngine.Object.FindObjectsOfType().Where((c) => c.IsSceneObject != null && c.IsSceneObject == true);
+#endif
foreach (var sobj in networkObjects)
{
sobj.IsSpawned = false;
@@ -678,7 +693,11 @@ namespace Unity.Netcode
internal void DespawnAndDestroyNetworkObjects()
{
+#if UNITY_2023_1_OR_NEWER
+ var networkObjects = UnityEngine.Object.FindObjectsByType(FindObjectsSortMode.InstanceID);
+#else
var networkObjects = UnityEngine.Object.FindObjectsOfType();
+#endif
for (int i = 0; i < networkObjects.Length; i++)
{
@@ -708,7 +727,11 @@ namespace Unity.Netcode
internal void DestroySceneObjects()
{
+#if UNITY_2023_1_OR_NEWER
+ var networkObjects = UnityEngine.Object.FindObjectsByType(FindObjectsSortMode.InstanceID);
+#else
var networkObjects = UnityEngine.Object.FindObjectsOfType();
+#endif
for (int i = 0; i < networkObjects.Length; i++)
{
@@ -735,7 +758,11 @@ namespace Unity.Netcode
internal void ServerSpawnSceneObjectsOnStartSweep()
{
+#if UNITY_2023_1_OR_NEWER
+ var networkObjects = UnityEngine.Object.FindObjectsByType(FindObjectsSortMode.InstanceID);
+#else
var networkObjects = UnityEngine.Object.FindObjectsOfType();
+#endif
var networkObjectsToSpawn = new List();
for (int i = 0; i < networkObjects.Length; i++)
diff --git a/Runtime/Transports/UTP/UnityTransport.cs b/Runtime/Transports/UTP/UnityTransport.cs
index f616b1d..84eeed7 100644
--- a/Runtime/Transports/UTP/UnityTransport.cs
+++ b/Runtime/Transports/UTP/UnityTransport.cs
@@ -10,8 +10,10 @@ using System.Collections.Generic;
using UnityEngine;
using NetcodeNetworkEvent = Unity.Netcode.NetworkEvent;
using TransportNetworkEvent = Unity.Networking.Transport.NetworkEvent;
+using Unity.Burst;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Collections;
+using Unity.Jobs;
using Unity.Networking.Transport;
using Unity.Networking.Transport.Relay;
using Unity.Networking.Transport.Utilities;
@@ -51,50 +53,48 @@ namespace Unity.Netcode.Transports.UTP
///
public static class ErrorUtilities
{
- private const string k_NetworkSuccess = "Success";
- private const string k_NetworkIdMismatch = "NetworkId is invalid, likely caused by stale connection {0}.";
- private const string k_NetworkVersionMismatch = "NetworkVersion is invalid, likely caused by stale connection {0}.";
- private const string k_NetworkStateMismatch = "Sending data while connecting on connection {0} is not allowed.";
- private const string k_NetworkPacketOverflow = "Unable to allocate packet due to buffer overflow.";
- private const string k_NetworkSendQueueFull = "Currently unable to queue packet as there is too many in-flight packets. This could be because the send queue size ('Max Send Queue Size') is too small.";
- private const string k_NetworkHeaderInvalid = "Invalid Unity Transport Protocol header.";
- private const string k_NetworkDriverParallelForErr = "The parallel network driver needs to process a single unique connection per job, processing a single connection multiple times in a parallel for is not supported.";
- private const string k_NetworkSendHandleInvalid = "Invalid NetworkInterface Send Handle. Likely caused by pipeline send data corruption.";
- private const string k_NetworkArgumentMismatch = "Invalid NetworkEndpoint Arguments.";
+ private static readonly FixedString128Bytes k_NetworkSuccess = "Success";
+ private static readonly FixedString128Bytes k_NetworkIdMismatch = "Invalid connection ID {0}.";
+ private static readonly FixedString128Bytes k_NetworkVersionMismatch = "Connection ID is invalid. Likely caused by sending on stale connection {0}.";
+ private static readonly FixedString128Bytes k_NetworkStateMismatch = "Connection state is invalid. Likely caused by sending on connection {0} which is stale or still connecting.";
+ private static readonly FixedString128Bytes k_NetworkPacketOverflow = "Packet is too large to be allocated by the transport.";
+ private static readonly FixedString128Bytes k_NetworkSendQueueFull = "Unable to queue packet in the transport. Likely caused by send queue size ('Max Send Queue Size') being too small.";
///
- /// Convert error code to human readable error message.
+ /// Convert a UTP error code to human-readable error message.
///
- /// Status code of the error
- /// Subject connection ID of the error
- /// Human readable error message.
+ /// UTP error code.
+ /// ID of the connection on which the error occurred.
+ /// Human-readable error message.
public static string ErrorToString(Networking.Transport.Error.StatusCode error, ulong connectionId)
{
- switch (error)
+ return ErrorToString((int)error, connectionId);
+ }
+
+ internal static string ErrorToString(int error, ulong connectionId)
+ {
+ return ErrorToFixedString(error, connectionId).ToString();
+ }
+
+ internal static FixedString128Bytes ErrorToFixedString(int error, ulong connectionId)
+ {
+ switch ((Networking.Transport.Error.StatusCode)error)
{
case Networking.Transport.Error.StatusCode.Success:
return k_NetworkSuccess;
case Networking.Transport.Error.StatusCode.NetworkIdMismatch:
- return string.Format(k_NetworkIdMismatch, connectionId);
+ return FixedString.Format(k_NetworkIdMismatch, connectionId);
case Networking.Transport.Error.StatusCode.NetworkVersionMismatch:
- return string.Format(k_NetworkVersionMismatch, connectionId);
+ return FixedString.Format(k_NetworkVersionMismatch, connectionId);
case Networking.Transport.Error.StatusCode.NetworkStateMismatch:
- return string.Format(k_NetworkStateMismatch, connectionId);
+ return FixedString.Format(k_NetworkStateMismatch, connectionId);
case Networking.Transport.Error.StatusCode.NetworkPacketOverflow:
return k_NetworkPacketOverflow;
case Networking.Transport.Error.StatusCode.NetworkSendQueueFull:
return k_NetworkSendQueueFull;
- case Networking.Transport.Error.StatusCode.NetworkHeaderInvalid:
- return k_NetworkHeaderInvalid;
- case Networking.Transport.Error.StatusCode.NetworkDriverParallelForErr:
- return k_NetworkDriverParallelForErr;
- case Networking.Transport.Error.StatusCode.NetworkSendHandleInvalid:
- return k_NetworkSendHandleInvalid;
- case Networking.Transport.Error.StatusCode.NetworkArgumentMismatch:
- return k_NetworkArgumentMismatch;
+ default:
+ return FixedString.Format("Unknown error code {0}.", error);
}
-
- return $"Unknown ErrorCode {Enum.GetName(typeof(Networking.Transport.Error.StatusCode), error)}";
}
}
@@ -676,53 +676,72 @@ namespace Unity.Netcode.Transports.UTP
}
}
+ [BurstCompile]
+ private struct SendBatchedMessagesJob : IJob
+ {
+ public NetworkDriver.Concurrent Driver;
+ public SendTarget Target;
+ public BatchedSendQueue Queue;
+ public NetworkPipeline ReliablePipeline;
+
+ public void Execute()
+ {
+ var clientId = Target.ClientId;
+ var connection = ParseClientId(clientId);
+ var pipeline = Target.NetworkPipeline;
+
+ while (!Queue.IsEmpty)
+ {
+ var result = Driver.BeginSend(pipeline, connection, out var writer);
+ if (result != (int)Networking.Transport.Error.StatusCode.Success)
+ {
+ Debug.LogError($"Error sending message: {ErrorUtilities.ErrorToFixedString(result, clientId)}");
+ return;
+ }
+
+ // We don't attempt to send entire payloads over the reliable pipeline. Instead we
+ // fragment it manually. This is safe and easy to do since the reliable pipeline
+ // basically implements a stream, so as long as we separate the different messages
+ // in the stream (the send queue does that automatically) we are sure they'll be
+ // reassembled properly at the other end. This allows us to lift the limit of ~44KB
+ // on reliable payloads (because of the reliable window size).
+ var written = pipeline == ReliablePipeline ? Queue.FillWriterWithBytes(ref writer) : Queue.FillWriterWithMessages(ref writer);
+
+ result = Driver.EndSend(writer);
+ if (result == written)
+ {
+ // Batched message was sent successfully. Remove it from the queue.
+ Queue.Consume(written);
+ }
+ else
+ {
+ // Some error occured. If it's just the UTP queue being full, then don't log
+ // anything since that's okay (the unsent message(s) are still in the queue
+ // and we'll retry sending them later). Otherwise log the error and remove the
+ // message from the queue (we don't want to resend it again since we'll likely
+ // just get the same error again).
+ if (result != (int)Networking.Transport.Error.StatusCode.NetworkSendQueueFull)
+ {
+ Debug.LogError($"Error sending the message: {ErrorUtilities.ErrorToFixedString(result, clientId)}");
+ Queue.Consume(written);
+ }
+
+ return;
+ }
+ }
+ }
+ }
+
// Send as many batched messages from the queue as possible.
private void SendBatchedMessages(SendTarget sendTarget, BatchedSendQueue queue)
{
- var clientId = sendTarget.ClientId;
- var connection = ParseClientId(clientId);
- var pipeline = sendTarget.NetworkPipeline;
-
- while (!queue.IsEmpty)
+ new SendBatchedMessagesJob
{
- var result = m_Driver.BeginSend(pipeline, connection, out var writer);
- if (result != (int)Networking.Transport.Error.StatusCode.Success)
- {
- Debug.LogError("Error sending the message: " +
- ErrorUtilities.ErrorToString((Networking.Transport.Error.StatusCode)result, clientId));
- return;
- }
-
- // We don't attempt to send entire payloads over the reliable pipeline. Instead we
- // fragment it manually. This is safe and easy to do since the reliable pipeline
- // basically implements a stream, so as long as we separate the different messages
- // in the stream (the send queue does that automatically) we are sure they'll be
- // reassembled properly at the other end. This allows us to lift the limit of ~44KB
- // on reliable payloads (because of the reliable window size).
- var written = pipeline == m_ReliableSequencedPipeline ? queue.FillWriterWithBytes(ref writer) : queue.FillWriterWithMessages(ref writer);
-
- result = m_Driver.EndSend(writer);
- if (result == written)
- {
- // Batched message was sent successfully. Remove it from the queue.
- queue.Consume(written);
- }
- else
- {
- // Some error occured. If it's just the UTP queue being full, then don't log
- // anything since that's okay (the unsent message(s) are still in the queue
- // and we'll retry sending the later). Otherwise log the error and remove the
- // message from the queue (we don't want to resend it again since we'll likely
- // just get the same error again).
- if (result != (int)Networking.Transport.Error.StatusCode.NetworkSendQueueFull)
- {
- Debug.LogError("Error sending the message: " + ErrorUtilities.ErrorToString((Networking.Transport.Error.StatusCode)result, clientId));
- queue.Consume(written);
- }
-
- return;
- }
- }
+ Driver = m_Driver.ToConcurrent(),
+ Target = sendTarget,
+ Queue = queue,
+ ReliablePipeline = m_ReliableSequencedPipeline
+ }.Run();
}
private bool AcceptConnection()
@@ -1441,7 +1460,7 @@ namespace Unity.Netcode.Transports.UTP
{
if (m_ProtocolType == ProtocolType.RelayUnityTransport)
{
- if (m_RelayServerData.IsSecure != 0)
+ if (m_RelayServerData.IsSecure == 0)
{
// log an error because we have mismatched configuration
Debug.LogError("Mismatched security configuration, between Relay and local NetworkManager settings");
@@ -1452,36 +1471,30 @@ namespace Unity.Netcode.Transports.UTP
}
else
{
- try
+ if (NetworkManager.IsServer)
{
- if (NetworkManager.IsServer)
+ if (String.IsNullOrEmpty(m_ServerCertificate) || String.IsNullOrEmpty(m_ServerPrivateKey))
{
- if (m_ServerCertificate.Length == 0 || m_ServerPrivateKey.Length == 0)
- {
- throw new Exception("In order to use encrypted communications, when hosting, you must set the server certificate and key.");
- }
- m_NetworkSettings.WithSecureServerParameters(m_ServerCertificate, m_ServerPrivateKey);
+ throw new Exception("In order to use encrypted communications, when hosting, you must set the server certificate and key.");
+ }
+
+ m_NetworkSettings.WithSecureServerParameters(m_ServerCertificate, m_ServerPrivateKey);
+ }
+ else
+ {
+ if (String.IsNullOrEmpty(m_ServerCommonName))
+ {
+ throw new Exception("In order to use encrypted communications, clients must set the server common name.");
+ }
+ else if (String.IsNullOrEmpty(m_ClientCaCertificate))
+ {
+ m_NetworkSettings.WithSecureClientParameters(m_ServerCommonName);
}
else
{
- if (m_ServerCommonName.Length == 0)
- {
- throw new Exception("In order to use encrypted communications, clients must set the server common name.");
- }
- else if (m_ClientCaCertificate == null)
- {
- m_NetworkSettings.WithSecureClientParameters(m_ServerCommonName);
- }
- else
- {
- m_NetworkSettings.WithSecureClientParameters(m_ClientCaCertificate, m_ServerCommonName);
- }
+ m_NetworkSettings.WithSecureClientParameters(m_ClientCaCertificate, m_ServerCommonName);
}
}
- catch(Exception e)
- {
- Debug.LogException(e, this);
- }
}
}
#endif
diff --git a/TestHelpers/Runtime/DebugNetworkHooks.cs b/TestHelpers/Runtime/DebugNetworkHooks.cs
new file mode 100644
index 0000000..480a1c0
--- /dev/null
+++ b/TestHelpers/Runtime/DebugNetworkHooks.cs
@@ -0,0 +1,63 @@
+using System;
+using UnityEngine;
+
+namespace Unity.Netcode.TestHelpers.Runtime
+{
+ internal class DebugNetworkHooks : INetworkHooks
+ {
+ public void OnBeforeSendMessage(ulong clientId, ref T message, NetworkDelivery delivery) where T : INetworkMessage
+ {
+ }
+
+ public void OnAfterSendMessage(ulong clientId, ref T message, NetworkDelivery delivery, int messageSizeBytes) where T : INetworkMessage
+ {
+ Debug.Log($"Sending message of type {typeof(T).Name} to {clientId} ({messageSizeBytes} bytes)");
+ }
+
+ public void OnBeforeReceiveMessage(ulong senderId, Type messageType, int messageSizeBytes)
+ {
+ Debug.Log($"Receiving message of type {messageType.Name} from {senderId}");
+ }
+
+ public void OnAfterReceiveMessage(ulong senderId, Type messageType, int messageSizeBytes)
+ {
+ }
+
+ public void OnBeforeSendBatch(ulong clientId, int messageCount, int batchSizeInBytes, NetworkDelivery delivery)
+ {
+ Debug.Log($"==> Sending a batch of {messageCount} messages ({batchSizeInBytes} bytes) to {clientId} ({delivery})");
+ }
+
+ public void OnAfterSendBatch(ulong clientId, int messageCount, int batchSizeInBytes, NetworkDelivery delivery)
+ {
+ }
+
+ public void OnBeforeReceiveBatch(ulong senderId, int messageCount, int batchSizeInBytes)
+ {
+ Debug.Log($"<== Receiving a batch of {messageCount} messages ({batchSizeInBytes} bytes) from {senderId}");
+ }
+
+ public void OnAfterReceiveBatch(ulong senderId, int messageCount, int batchSizeInBytes)
+ {
+ }
+
+ public bool OnVerifyCanSend(ulong destinationId, Type messageType, NetworkDelivery delivery)
+ {
+ return true;
+ }
+
+ public bool OnVerifyCanReceive(ulong senderId, Type messageType, FastBufferReader messageContent, ref NetworkContext context)
+ {
+ return true;
+ }
+
+ public void OnBeforeHandleMessage(ref T message, ref NetworkContext context) where T : INetworkMessage
+ {
+ Debug.Log($"Handling a message of type {typeof(T).Name}");
+ }
+
+ public void OnAfterHandleMessage(ref T message, ref NetworkContext context) where T : INetworkMessage
+ {
+ }
+ }
+}
diff --git a/TestHelpers/Runtime/DebugNetworkHooks.cs.meta b/TestHelpers/Runtime/DebugNetworkHooks.cs.meta
new file mode 100644
index 0000000..ed3cc37
--- /dev/null
+++ b/TestHelpers/Runtime/DebugNetworkHooks.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: b5f8098e713443eeba116a25de551cf8
+timeCreated: 1666626883
\ No newline at end of file
diff --git a/TestHelpers/Runtime/IntegrationTestSceneHandler.cs b/TestHelpers/Runtime/IntegrationTestSceneHandler.cs
index 15b6bc1..80e09c8 100644
--- a/TestHelpers/Runtime/IntegrationTestSceneHandler.cs
+++ b/TestHelpers/Runtime/IntegrationTestSceneHandler.cs
@@ -147,7 +147,12 @@ namespace Unity.Netcode.TestHelpers.Runtime
private static void ProcessInSceneObjects(Scene scene, NetworkManager networkManager)
{
// Get all in-scene placed NeworkObjects that were instantiated when this scene loaded
+#if UNITY_2023_1_OR_NEWER
+ var inSceneNetworkObjects = Object.FindObjectsByType(FindObjectsSortMode.InstanceID).Where((c) => c.IsSceneObject != false && c.GetSceneOriginHandle() == scene.handle);
+#else
var inSceneNetworkObjects = Object.FindObjectsOfType().Where((c) => c.IsSceneObject != false && c.GetSceneOriginHandle() == scene.handle);
+#endif
+
foreach (var sobj in inSceneNetworkObjects)
{
if (sobj.NetworkManagerOwner != networkManager)
diff --git a/TestHelpers/Runtime/MessageHooks.cs b/TestHelpers/Runtime/MessageHooks.cs
index 23f5f6c..9be56d9 100644
--- a/TestHelpers/Runtime/MessageHooks.cs
+++ b/TestHelpers/Runtime/MessageHooks.cs
@@ -44,7 +44,7 @@ namespace Unity.Netcode.TestHelpers.Runtime
public void OnAfterReceiveMessage(ulong senderId, Type messageType, int messageSizeBytes)
{
- if (!CurrentMessageHasTriggerdAHook && IsWaiting && (HandleCheck == null || HandleCheck.Invoke(messageType)))
+ if (!CurrentMessageHasTriggerdAHook && IsWaiting && ReceiptCheck != null && ReceiptCheck.Invoke(messageType))
{
IsWaiting = false;
CurrentMessageHasTriggerdAHook = true;
@@ -92,7 +92,7 @@ namespace Unity.Netcode.TestHelpers.Runtime
public void OnAfterHandleMessage(ref T message, ref NetworkContext context) where T : INetworkMessage
{
- if (!CurrentMessageHasTriggerdAHook && IsWaiting && (HandleCheck == null || HandleCheck.Invoke(message)))
+ if (!CurrentMessageHasTriggerdAHook && IsWaiting && HandleCheck != null && HandleCheck.Invoke(message))
{
IsWaiting = false;
CurrentMessageHasTriggerdAHook = true;
diff --git a/TestHelpers/Runtime/MessageHooksConditional.cs b/TestHelpers/Runtime/MessageHooksConditional.cs
index f16978e..80b2dd8 100644
--- a/TestHelpers/Runtime/MessageHooksConditional.cs
+++ b/TestHelpers/Runtime/MessageHooksConditional.cs
@@ -39,7 +39,10 @@ namespace Unity.Netcode.TestHelpers.Runtime
if (AllMessagesReceived)
{
- return AllMessagesReceived;
+ foreach (var entry in m_MessageHookEntries)
+ {
+ entry.RemoveHook();
+ }
}
return AllMessagesReceived;
@@ -110,6 +113,11 @@ namespace Unity.Netcode.TestHelpers.Runtime
Initialize();
}
+ internal void RemoveHook()
+ {
+ m_NetworkManager.MessagingSystem.Unhook(MessageHooks);
+ }
+
internal void AssignMessageType(Type type)
{
MessageType = type.Name;
diff --git a/TestHelpers/Runtime/NetcodeIntegrationTest.cs b/TestHelpers/Runtime/NetcodeIntegrationTest.cs
index c2f3dfe..9103f33 100644
--- a/TestHelpers/Runtime/NetcodeIntegrationTest.cs
+++ b/TestHelpers/Runtime/NetcodeIntegrationTest.cs
@@ -274,16 +274,6 @@ namespace Unity.Netcode.TestHelpers.Runtime
CreateServerAndClients(NumberOfClients);
}
- protected virtual void OnNewClientCreated(NetworkManager networkManager)
- {
-
- }
-
- protected virtual void OnNewClientStartedAndConnected(NetworkManager networkManager)
- {
-
- }
-
private void AddRemoveNetworkManager(NetworkManager networkManager, bool addNetworkManager)
{
var clientNetworkManagersList = new List(m_ClientNetworkManagers);
@@ -299,6 +289,37 @@ namespace Unity.Netcode.TestHelpers.Runtime
m_NumberOfClients = clientNetworkManagersList.Count;
}
+ ///
+ /// CreateAndStartNewClient Only
+ /// Invoked when the newly created client has been created
+ ///
+ protected virtual void OnNewClientCreated(NetworkManager networkManager)
+ {
+
+ }
+
+ ///
+ /// CreateAndStartNewClient Only
+ /// Invoked when the newly created client has been created and started
+ ///
+ protected virtual void OnNewClientStarted(NetworkManager networkManager)
+ {
+ }
+
+ ///
+ /// CreateAndStartNewClient Only
+ /// Invoked when the newly created client has been created, started, and connected
+ /// to the server-host.
+ ///
+ protected virtual void OnNewClientStartedAndConnected(NetworkManager networkManager)
+ {
+
+ }
+
+ ///
+ /// This will create, start, and connect a new client while in the middle of an
+ /// integration test.
+ ///
protected IEnumerator CreateAndStartNewClient()
{
var networkManager = NetcodeIntegrationTestHelpers.CreateNewClient(m_ClientNetworkManagers.Length);
@@ -309,9 +330,20 @@ namespace Unity.Netcode.TestHelpers.Runtime
OnNewClientCreated(networkManager);
NetcodeIntegrationTestHelpers.StartOneClient(networkManager);
+
+ if (LogAllMessages)
+ {
+ networkManager.MessagingSystem.Hook(new DebugNetworkHooks());
+ }
+
AddRemoveNetworkManager(networkManager, true);
+
+ OnNewClientStarted(networkManager);
+
// Wait for the new client to connect
yield return WaitForClientsConnectedOrTimeOut();
+
+ OnNewClientStartedAndConnected(networkManager);
if (s_GlobalTimeoutHelper.TimedOut)
{
AddRemoveNetworkManager(networkManager, false);
@@ -322,6 +354,9 @@ namespace Unity.Netcode.TestHelpers.Runtime
VerboseDebug($"[{networkManager.name}] Created and connected!");
}
+ ///
+ /// This will stop a client while in the middle of an integration test
+ ///
protected IEnumerator StopOneClient(NetworkManager networkManager, bool destroy = false)
{
NetcodeIntegrationTestHelpers.StopOneClient(networkManager, destroy);
@@ -401,8 +436,19 @@ namespace Unity.Netcode.TestHelpers.Runtime
networkManager.name = $"NetworkManager - Client - {networkManager.LocalClientId}";
Assert.NotNull(networkManager.LocalClient.PlayerObject, $"{nameof(StartServerAndClients)} detected that client {networkManager.LocalClientId} does not have an assigned player NetworkObject!");
+ // Go ahead and create an entry for this new client
+ if (!m_PlayerNetworkObjects.ContainsKey(networkManager.LocalClientId))
+ {
+ m_PlayerNetworkObjects.Add(networkManager.LocalClientId, new Dictionary());
+ }
+
+#if UNITY_2023_1_OR_NEWER
// Get all player instances for the current client NetworkManager instance
- var clientPlayerClones = Object.FindObjectsOfType().Where((c) => c.IsPlayerObject && c.OwnerClientId == networkManager.LocalClientId);
+ var clientPlayerClones = Object.FindObjectsByType(FindObjectsSortMode.None).Where((c) => c.IsPlayerObject && c.OwnerClientId == networkManager.LocalClientId).ToList();
+#else
+ // Get all player instances for the current client NetworkManager instance
+ var clientPlayerClones = Object.FindObjectsOfType().Where((c) => c.IsPlayerObject && c.OwnerClientId == networkManager.LocalClientId).ToList();
+#endif
// Add this player instance to each client player entry
foreach (var playerNetworkObject in clientPlayerClones)
{
@@ -416,6 +462,20 @@ namespace Unity.Netcode.TestHelpers.Runtime
m_PlayerNetworkObjects[playerNetworkObject.NetworkManager.LocalClientId].Add(networkManager.LocalClientId, playerNetworkObject);
}
}
+#if UNITY_2023_1_OR_NEWER
+ // For late joining clients, add the remaining (if any) cloned versions of each client's player
+ clientPlayerClones = Object.FindObjectsByType(FindObjectsSortMode.None).Where((c) => c.IsPlayerObject && c.NetworkManager == networkManager).ToList();
+#else
+ // For late joining clients, add the remaining (if any) cloned versions of each client's player
+ clientPlayerClones = Object.FindObjectsOfType().Where((c) => c.IsPlayerObject && c.NetworkManager == networkManager).ToList();
+#endif
+ foreach (var playerNetworkObject in clientPlayerClones)
+ {
+ if (!m_PlayerNetworkObjects[networkManager.LocalClientId].ContainsKey(playerNetworkObject.OwnerClientId))
+ {
+ m_PlayerNetworkObjects[networkManager.LocalClientId].Add(playerNetworkObject.OwnerClientId, playerNetworkObject);
+ }
+ }
}
protected void ClientNetworkManagerPostStartInit()
@@ -428,7 +488,11 @@ namespace Unity.Netcode.TestHelpers.Runtime
}
if (m_UseHost)
{
+#if UNITY_2023_1_OR_NEWER
+ var clientSideServerPlayerClones = Object.FindObjectsByType(FindObjectsSortMode.None).Where((c) => c.IsPlayerObject && c.OwnerClientId == m_ServerNetworkManager.LocalClientId);
+#else
var clientSideServerPlayerClones = Object.FindObjectsOfType().Where((c) => c.IsPlayerObject && c.OwnerClientId == m_ServerNetworkManager.LocalClientId);
+#endif
foreach (var playerNetworkObject in clientSideServerPlayerClones)
{
// When the server is not the host this needs to be done
@@ -444,6 +508,8 @@ namespace Unity.Netcode.TestHelpers.Runtime
}
}
+ protected virtual bool LogAllMessages => false;
+
///
/// This starts the server and clients as long as
/// returns true.
@@ -462,6 +528,11 @@ namespace Unity.Netcode.TestHelpers.Runtime
Assert.Fail("Failed to start instances");
}
+ if (LogAllMessages)
+ {
+ EnableMessageLogging();
+ }
+
RegisterSceneManagerHandler();
// Notification that the server and clients have been started
@@ -477,8 +548,13 @@ namespace Unity.Netcode.TestHelpers.Runtime
if (m_UseHost || m_ServerNetworkManager.IsHost)
{
+#if UNITY_2023_1_OR_NEWER
+ // Add the server player instance to all m_ClientSidePlayerNetworkObjects entries
+ var serverPlayerClones = Object.FindObjectsByType(FindObjectsSortMode.None).Where((c) => c.IsPlayerObject && c.OwnerClientId == m_ServerNetworkManager.LocalClientId);
+#else
// Add the server player instance to all m_ClientSidePlayerNetworkObjects entries
var serverPlayerClones = Object.FindObjectsOfType().Where((c) => c.IsPlayerObject && c.OwnerClientId == m_ServerNetworkManager.LocalClientId);
+#endif
foreach (var playerNetworkObject in serverPlayerClones)
{
if (!m_PlayerNetworkObjects.ContainsKey(playerNetworkObject.NetworkManager.LocalClientId))
@@ -668,7 +744,11 @@ namespace Unity.Netcode.TestHelpers.Runtime
///
protected void DestroySceneNetworkObjects()
{
+#if UNITY_2023_1_OR_NEWER
+ var networkObjects = Object.FindObjectsByType(FindObjectsSortMode.InstanceID);
+#else
var networkObjects = Object.FindObjectsOfType();
+#endif
foreach (var networkObject in networkObjects)
{
// This can sometimes be null depending upon order of operations
@@ -692,6 +772,18 @@ namespace Unity.Netcode.TestHelpers.Runtime
}
}
+ ///
+ /// For debugging purposes, this will turn on verbose logging of all messages and batches sent and received
+ ///
+ protected void EnableMessageLogging()
+ {
+ m_ServerNetworkManager.MessagingSystem.Hook(new DebugNetworkHooks());
+ foreach (var client in m_ClientNetworkManagers)
+ {
+ client.MessagingSystem.Hook(new DebugNetworkHooks());
+ }
+ }
+
///
/// Waits for the function condition to return true or it will time out.
/// This will operate at the current m_ServerNetworkManager.NetworkConfig.TickRate
diff --git a/Tests/Editor/Build/BuildTests.cs b/Tests/Editor/Build/BuildTests.cs
index c8cde89..de63ce1 100644
--- a/Tests/Editor/Build/BuildTests.cs
+++ b/Tests/Editor/Build/BuildTests.cs
@@ -12,6 +12,7 @@ namespace Unity.Netcode.EditorTests
public const string DefaultBuildScenePath = "Tests/Editor/Build/BuildTestScene.unity";
[Test]
+ [Ignore("Disabling this test on release/1.2.0 branch due to Burst failures caused when running with upm ci")]
public void BasicBuildTest()
{
var execAssembly = Assembly.GetExecutingAssembly();
diff --git a/Tests/Editor/DisconnectMessageTests.cs b/Tests/Editor/DisconnectMessageTests.cs
new file mode 100644
index 0000000..58f5502
--- /dev/null
+++ b/Tests/Editor/DisconnectMessageTests.cs
@@ -0,0 +1,56 @@
+using NUnit.Framework;
+using Unity.Collections;
+
+namespace Unity.Netcode.EditorTests
+{
+ public class DisconnectMessageTests
+ {
+ [Test]
+ public void EmptyDisconnectReason()
+ {
+ var networkContext = new NetworkContext();
+ var writer = new FastBufferWriter(20, Allocator.Temp, 20);
+ var msg = new DisconnectReasonMessage();
+ msg.Reason = string.Empty;
+ msg.Serialize(writer, msg.Version);
+
+ var fbr = new FastBufferReader(writer, Allocator.Temp);
+ var recvMsg = new DisconnectReasonMessage();
+ recvMsg.Deserialize(fbr, ref networkContext, msg.Version);
+
+ Assert.IsEmpty(recvMsg.Reason);
+ }
+
+ [Test]
+ public void DisconnectReason()
+ {
+ var networkContext = new NetworkContext();
+ var writer = new FastBufferWriter(20, Allocator.Temp, 20);
+ var msg = new DisconnectReasonMessage();
+ msg.Reason = "Foo";
+ msg.Serialize(writer, msg.Version);
+
+ var fbr = new FastBufferReader(writer, Allocator.Temp);
+ var recvMsg = new DisconnectReasonMessage();
+ recvMsg.Deserialize(fbr, ref networkContext, msg.Version);
+
+ Assert.AreEqual("Foo", recvMsg.Reason);
+ }
+
+ [Test]
+ public void DisconnectReasonTooLong()
+ {
+ var networkContext = new NetworkContext();
+ var writer = new FastBufferWriter(20, Allocator.Temp, 20);
+ var msg = new DisconnectReasonMessage();
+ msg.Reason = "ThisStringIsWayLongerThanTwentyBytes";
+ msg.Serialize(writer, msg.Version);
+
+ var fbr = new FastBufferReader(writer, Allocator.Temp);
+ var recvMsg = new DisconnectReasonMessage();
+ recvMsg.Deserialize(fbr, ref networkContext, msg.Version);
+
+ Assert.IsEmpty(recvMsg.Reason);
+ }
+ }
+}
diff --git a/Tests/Editor/DisconnectMessageTests.cs.meta b/Tests/Editor/DisconnectMessageTests.cs.meta
new file mode 100644
index 0000000..8cfb8e2
--- /dev/null
+++ b/Tests/Editor/DisconnectMessageTests.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 55a1355c62fe14a118253f8bbee7c3cf
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Tests/Editor/Messaging/MessageReceivingTests.cs b/Tests/Editor/Messaging/MessageReceivingTests.cs
index d8b6f37..11c65e7 100644
--- a/Tests/Editor/Messaging/MessageReceivingTests.cs
+++ b/Tests/Editor/Messaging/MessageReceivingTests.cs
@@ -18,12 +18,12 @@ namespace Unity.Netcode.EditorTests
public static bool Handled;
public static List DeserializedValues = new List();
- public void Serialize(FastBufferWriter writer)
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
writer.WriteValueSafe(this);
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
Deserialized = true;
reader.ReadValueSafe(out this);
@@ -35,6 +35,8 @@ namespace Unity.Netcode.EditorTests
Handled = true;
DeserializedValues.Add(this);
}
+
+ public int Version => 0;
}
private class TestMessageProvider : IMessageProvider
@@ -46,7 +48,8 @@ namespace Unity.Netcode.EditorTests
new MessagingSystem.MessageWithHandler
{
MessageType = typeof(TestMessage),
- Handler = MessagingSystem.ReceiveMessage
+ Handler = MessagingSystem.ReceiveMessage,
+ GetVersion = MessagingSystem.CreateMessageAndGetVersion
}
};
}
@@ -62,6 +65,7 @@ namespace Unity.Netcode.EditorTests
TestMessage.DeserializedValues.Clear();
m_MessagingSystem = new MessagingSystem(new NopMessageSender(), this, new TestMessageProvider());
+ m_MessagingSystem.SetVersion(0, XXHash.Hash32(typeof(TestMessage).FullName), 0);
}
[TearDown]
diff --git a/Tests/Editor/Messaging/MessageRegistrationTests.cs b/Tests/Editor/Messaging/MessageRegistrationTests.cs
index 38ca9b0..2f9d58c 100644
--- a/Tests/Editor/Messaging/MessageRegistrationTests.cs
+++ b/Tests/Editor/Messaging/MessageRegistrationTests.cs
@@ -1,4 +1,3 @@
-using System;
using System.Collections.Generic;
using NUnit.Framework;
@@ -11,12 +10,12 @@ namespace Unity.Netcode.EditorTests
public int A;
public int B;
public int C;
- public void Serialize(FastBufferWriter writer)
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
writer.WriteValue(this);
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
return true;
}
@@ -24,6 +23,8 @@ namespace Unity.Netcode.EditorTests
public void Handle(ref NetworkContext context)
{
}
+
+ public int Version => 0;
}
private struct TestMessageTwo : INetworkMessage, INetworkSerializeByMemcpy
@@ -31,12 +32,12 @@ namespace Unity.Netcode.EditorTests
public int A;
public int B;
public int C;
- public void Serialize(FastBufferWriter writer)
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
writer.WriteValue(this);
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
return true;
}
@@ -44,6 +45,8 @@ namespace Unity.Netcode.EditorTests
public void Handle(ref NetworkContext context)
{
}
+
+ public int Version => 0;
}
private class TestMessageProviderOne : IMessageProvider
{
@@ -54,12 +57,14 @@ namespace Unity.Netcode.EditorTests
new MessagingSystem.MessageWithHandler
{
MessageType = typeof(TestMessageOne),
- Handler = MessagingSystem.ReceiveMessage
+ Handler = MessagingSystem.ReceiveMessage,
+ GetVersion = MessagingSystem.CreateMessageAndGetVersion
},
new MessagingSystem.MessageWithHandler
{
MessageType = typeof(TestMessageTwo),
- Handler = MessagingSystem.ReceiveMessage
+ Handler = MessagingSystem.ReceiveMessage,
+ GetVersion = MessagingSystem.CreateMessageAndGetVersion
}
};
}
@@ -70,12 +75,12 @@ namespace Unity.Netcode.EditorTests
public int A;
public int B;
public int C;
- public void Serialize(FastBufferWriter writer)
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
writer.WriteValue(this);
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
return true;
}
@@ -83,6 +88,8 @@ namespace Unity.Netcode.EditorTests
public void Handle(ref NetworkContext context)
{
}
+
+ public int Version => 0;
}
private class TestMessageProviderTwo : IMessageProvider
{
@@ -93,7 +100,8 @@ namespace Unity.Netcode.EditorTests
new MessagingSystem.MessageWithHandler
{
MessageType = typeof(TestMessageThree),
- Handler = MessagingSystem.ReceiveMessage
+ Handler = MessagingSystem.ReceiveMessage,
+ GetVersion = MessagingSystem.CreateMessageAndGetVersion
}
};
}
@@ -103,12 +111,12 @@ namespace Unity.Netcode.EditorTests
public int A;
public int B;
public int C;
- public void Serialize(FastBufferWriter writer)
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
writer.WriteValue(this);
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
return true;
}
@@ -116,6 +124,8 @@ namespace Unity.Netcode.EditorTests
public void Handle(ref NetworkContext context)
{
}
+
+ public int Version => 0;
}
private class TestMessageProviderThree : IMessageProvider
{
@@ -126,7 +136,8 @@ namespace Unity.Netcode.EditorTests
new MessagingSystem.MessageWithHandler
{
MessageType = typeof(TestMessageFour),
- Handler = MessagingSystem.ReceiveMessage
+ Handler = MessagingSystem.ReceiveMessage,
+ GetVersion = MessagingSystem.CreateMessageAndGetVersion
}
};
}
@@ -183,11 +194,11 @@ namespace Unity.Netcode.EditorTests
internal class AAAEarlyLexicographicNetworkMessage : INetworkMessage
{
- public void Serialize(FastBufferWriter writer)
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
return true;
}
@@ -195,6 +206,8 @@ namespace Unity.Netcode.EditorTests
public void Handle(ref NetworkContext context)
{
}
+
+ public int Version => 0;
}
#pragma warning disable IDE1006
@@ -212,18 +225,19 @@ namespace Unity.Netcode.EditorTests
var messageWithHandler = new MessagingSystem.MessageWithHandler();
messageWithHandler.MessageType = typeof(zzzLateLexicographicNetworkMessage);
+ messageWithHandler.GetVersion = MessagingSystem.CreateMessageAndGetVersion;
listMessages.Add(messageWithHandler);
messageWithHandler.MessageType = typeof(ConnectionRequestMessage);
+ messageWithHandler.GetVersion = MessagingSystem.CreateMessageAndGetVersion;
listMessages.Add(messageWithHandler);
messageWithHandler.MessageType = typeof(ConnectionApprovedMessage);
- listMessages.Add(messageWithHandler);
-
- messageWithHandler.MessageType = typeof(OrderingMessage);
+ messageWithHandler.GetVersion = MessagingSystem.CreateMessageAndGetVersion;
listMessages.Add(messageWithHandler);
messageWithHandler.MessageType = typeof(AAAEarlyLexicographicNetworkMessage);
+ messageWithHandler.GetVersion = MessagingSystem.CreateMessageAndGetVersion;
listMessages.Add(messageWithHandler);
return listMessages;
@@ -237,65 +251,16 @@ namespace Unity.Netcode.EditorTests
var provider = new OrderingMessageProvider();
using var messagingSystem = new MessagingSystem(sender, null, provider);
- // the 3 priority messages should appear first, in lexicographic order
+ // the 2 priority messages should appear first, in lexicographic order
Assert.AreEqual(messagingSystem.MessageTypes[0], typeof(ConnectionApprovedMessage));
Assert.AreEqual(messagingSystem.MessageTypes[1], typeof(ConnectionRequestMessage));
- Assert.AreEqual(messagingSystem.MessageTypes[2], typeof(OrderingMessage));
// the other should follow after
- Assert.AreEqual(messagingSystem.MessageTypes[3], typeof(AAAEarlyLexicographicNetworkMessage));
- Assert.AreEqual(messagingSystem.MessageTypes[4], typeof(zzzLateLexicographicNetworkMessage));
+ Assert.AreEqual(messagingSystem.MessageTypes[2], typeof(AAAEarlyLexicographicNetworkMessage));
+ Assert.AreEqual(messagingSystem.MessageTypes[3], typeof(zzzLateLexicographicNetworkMessage));
// there should not be any extras
- Assert.AreEqual(messagingSystem.MessageHandlerCount, 5);
-
- // reorder the zzz one to position 3
- messagingSystem.ReorderMessage(3, XXHash.Hash32(typeof(zzzLateLexicographicNetworkMessage).FullName));
-
- // the 3 priority messages should still appear first, in lexicographic order
- Assert.AreEqual(messagingSystem.MessageTypes[0], typeof(ConnectionApprovedMessage));
- Assert.AreEqual(messagingSystem.MessageTypes[1], typeof(ConnectionRequestMessage));
- Assert.AreEqual(messagingSystem.MessageTypes[2], typeof(OrderingMessage));
-
- // the other should follow after, but reordered
- Assert.AreEqual(messagingSystem.MessageTypes[3], typeof(zzzLateLexicographicNetworkMessage));
- Assert.AreEqual(messagingSystem.MessageTypes[4], typeof(AAAEarlyLexicographicNetworkMessage));
-
- // there should still not be any extras
- Assert.AreEqual(messagingSystem.MessageHandlerCount, 5);
-
- // verify we get an exception when asking for an invalid position
- try
- {
- messagingSystem.ReorderMessage(-1, XXHash.Hash32(typeof(zzzLateLexicographicNetworkMessage).FullName));
- Assert.Fail();
- }
- catch (ArgumentException)
- {
- }
-
- // reorder the zzz one to position 3, again, to check nothing bad happens
- messagingSystem.ReorderMessage(3, XXHash.Hash32(typeof(zzzLateLexicographicNetworkMessage).FullName));
-
- // the two non-priority should not have moved
- Assert.AreEqual(messagingSystem.MessageTypes[3], typeof(zzzLateLexicographicNetworkMessage));
- Assert.AreEqual(messagingSystem.MessageTypes[4], typeof(AAAEarlyLexicographicNetworkMessage));
-
- // there should still not be any extras
- Assert.AreEqual(messagingSystem.MessageHandlerCount, 5);
-
- // 4242 is a random hash that should not match anything
- messagingSystem.ReorderMessage(3, 4242);
-
- // that should result in an extra entry
- Assert.AreEqual(messagingSystem.MessageHandlerCount, 6);
-
- // with a null handler
- Assert.AreEqual(messagingSystem.MessageHandlers[3], null);
-
- // and it should have bumped the previous messages down
- Assert.AreEqual(messagingSystem.MessageTypes[4], typeof(zzzLateLexicographicNetworkMessage));
- Assert.AreEqual(messagingSystem.MessageTypes[5], typeof(AAAEarlyLexicographicNetworkMessage));
+ Assert.AreEqual(messagingSystem.MessageHandlerCount, 4);
}
}
}
diff --git a/Tests/Editor/Messaging/MessageSendingTests.cs b/Tests/Editor/Messaging/MessageSendingTests.cs
index 5fce803..66f398a 100644
--- a/Tests/Editor/Messaging/MessageSendingTests.cs
+++ b/Tests/Editor/Messaging/MessageSendingTests.cs
@@ -18,13 +18,13 @@ namespace Unity.Netcode.EditorTests
public int B;
public int C;
public static bool Serialized;
- public void Serialize(FastBufferWriter writer)
+ public void Serialize(FastBufferWriter writer, int targetVersion)
{
Serialized = true;
writer.WriteValueSafe(this);
}
- public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
return true;
}
@@ -32,6 +32,8 @@ namespace Unity.Netcode.EditorTests
public void Handle(ref NetworkContext context)
{
}
+
+ public int Version => 0;
}
private class TestMessageSender : IMessageSender
@@ -66,7 +68,8 @@ namespace Unity.Netcode.EditorTests
new MessagingSystem.MessageWithHandler
{
MessageType = typeof(TestMessage),
- Handler = MessagingSystem.ReceiveMessage
+ Handler = MessagingSystem.ReceiveMessage,
+ GetVersion = MessagingSystem.CreateMessageAndGetVersion
}
};
// Track messages sent
@@ -88,6 +91,7 @@ namespace Unity.Netcode.EditorTests
m_TestMessageProvider = new TestMessageProvider();
m_MessagingSystem = new MessagingSystem(m_MessageSender, this, m_TestMessageProvider);
m_MessagingSystem.ClientConnected(0);
+ m_MessagingSystem.SetVersion(0, XXHash.Hash32(typeof(TestMessage).FullName), 0);
}
[TearDown]
@@ -256,7 +260,8 @@ namespace Unity.Netcode.EditorTests
new MessagingSystem.MessageWithHandler
{
MessageType = typeof(TestMessage),
- Handler = null
+ Handler = null,
+ GetVersion = MessagingSystem.CreateMessageAndGetVersion
}
};
}
diff --git a/Tests/Editor/Messaging/MessageVersioningTests.cs b/Tests/Editor/Messaging/MessageVersioningTests.cs
new file mode 100644
index 0000000..cbc7109
--- /dev/null
+++ b/Tests/Editor/Messaging/MessageVersioningTests.cs
@@ -0,0 +1,503 @@
+using System;
+using System.Collections.Generic;
+using NUnit.Framework;
+using NUnit.Framework.Internal;
+
+namespace Unity.Netcode.EditorTests
+{
+ public class MessageVersioningTests
+ {
+ public static int SentVersion;
+ public static int ReceivedVersion;
+
+ private const int k_DefaultB = 5;
+ private const int k_DefaultC = 10;
+ private const int k_DefaultD = 15;
+ private const long k_DefaultE = 20;
+
+ private struct VersionedTestMessage_v0 : INetworkMessage, INetworkSerializeByMemcpy
+ {
+ public int A;
+ public int B;
+ public int C;
+ public static bool Serialized;
+ public static bool Deserialized;
+ public static bool Handled;
+ public static List DeserializedValues = new List();
+
+ public void Serialize(FastBufferWriter writer, int targetVersion)
+ {
+ SentVersion = Version;
+ Serialized = true;
+ writer.WriteValueSafe(A);
+ writer.WriteValueSafe(B);
+ writer.WriteValueSafe(C);
+ }
+
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
+ {
+ ReceivedVersion = Version;
+ Deserialized = true;
+ reader.ReadValueSafe(out A);
+ reader.ReadValueSafe(out B);
+ reader.ReadValueSafe(out C);
+ return true;
+ }
+
+ public void Handle(ref NetworkContext context)
+ {
+ Handled = true;
+ DeserializedValues.Add(this);
+ }
+
+ public int Version => 0;
+ }
+
+ private struct VersionedTestMessage_v1 : INetworkMessage, INetworkSerializeByMemcpy
+ {
+ public int A;
+ public int B;
+ public int C;
+ public int D;
+ public static bool Serialized;
+ public static bool Deserialized;
+ public static bool Downgraded;
+ public static bool Upgraded;
+ public static bool Handled;
+ public static List DeserializedValues = new List();
+
+ public void Serialize(FastBufferWriter writer, int targetVersion)
+ {
+ if (targetVersion < Version)
+ {
+ Downgraded = true;
+ var v0 = new VersionedTestMessage_v0 { A = A, B = B, C = C };
+ v0.Serialize(writer, targetVersion);
+ return;
+ }
+ SentVersion = Version;
+ Serialized = true;
+ writer.WriteValueSafe(C);
+ writer.WriteValueSafe(D);
+ writer.WriteValueSafe(A);
+ writer.WriteValueSafe(B);
+ }
+
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
+ {
+ if (receivedMessageVersion < Version)
+ {
+ var v0 = new VersionedTestMessage_v0();
+ v0.Deserialize(reader, ref context, receivedMessageVersion);
+ A = v0.A;
+ B = v0.B;
+ C = v0.C;
+ D = k_DefaultD;
+ Upgraded = true;
+ return true;
+ }
+ ReceivedVersion = Version;
+ Deserialized = true;
+ reader.ReadValueSafe(out C);
+ reader.ReadValueSafe(out D);
+ reader.ReadValueSafe(out A);
+ reader.ReadValueSafe(out B);
+ return true;
+ }
+
+ public void Handle(ref NetworkContext context)
+ {
+ Handled = true;
+ DeserializedValues.Add(this);
+ }
+
+ public int Version => 1;
+ }
+
+ private struct VersionedTestMessage : INetworkMessage, INetworkSerializeByMemcpy
+ {
+ public int A;
+ public float D;
+ public long E;
+ public static bool Serialized;
+ public static bool Deserialized;
+ public static bool Downgraded;
+ public static bool Upgraded;
+ public static bool Handled;
+ public static List DeserializedValues = new List();
+
+ public void Serialize(FastBufferWriter writer, int targetVersion)
+ {
+ if (targetVersion < Version)
+ {
+ Downgraded = true;
+ var v1 = new VersionedTestMessage_v1 { A = A, B = k_DefaultB, C = k_DefaultC, D = (int)D };
+ v1.Serialize(writer, targetVersion);
+ return;
+ }
+ SentVersion = Version;
+ Serialized = true;
+ writer.WriteValueSafe(D);
+ writer.WriteValueSafe(A);
+ writer.WriteValueSafe(E);
+ }
+
+ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
+ {
+ if (receivedMessageVersion < Version)
+ {
+ var v1 = new VersionedTestMessage_v1();
+ v1.Deserialize(reader, ref context, receivedMessageVersion);
+ A = v1.A;
+ D = (float)v1.D;
+ E = k_DefaultE;
+ Upgraded = true;
+ return true;
+ }
+ ReceivedVersion = Version;
+ Deserialized = true;
+ reader.ReadValueSafe(out D);
+ reader.ReadValueSafe(out A);
+ reader.ReadValueSafe(out E);
+ return true;
+ }
+
+ public void Handle(ref NetworkContext context)
+ {
+ Handled = true;
+ DeserializedValues.Add(this);
+ }
+
+ public int Version => 2;
+ }
+
+ private class TestMessageProvider_v0 : IMessageProvider
+ {
+ public List GetMessages()
+ {
+ return new List
+ {
+ new MessagingSystem.MessageWithHandler
+ {
+ MessageType = typeof(VersionedTestMessage_v0),
+ Handler = MessagingSystem.ReceiveMessage,
+ GetVersion = MessagingSystem.CreateMessageAndGetVersion
+ }
+ };
+ }
+ }
+
+ private class TestMessageProvider_v1 : IMessageProvider
+ {
+ public List GetMessages()
+ {
+ return new List
+ {
+ new MessagingSystem.MessageWithHandler
+ {
+ MessageType = typeof(VersionedTestMessage_v1),
+ Handler = MessagingSystem.ReceiveMessage,
+ GetVersion = MessagingSystem.CreateMessageAndGetVersion
+ }
+ };
+ }
+ }
+
+ private class TestMessageProvider_v2 : IMessageProvider
+ {
+ public List GetMessages()
+ {
+ return new List
+ {
+ new MessagingSystem.MessageWithHandler
+ {
+ MessageType = typeof(VersionedTestMessage),
+ Handler = MessagingSystem.ReceiveMessage,
+ GetVersion = MessagingSystem.CreateMessageAndGetVersion
+ }
+ };
+ }
+ }
+
+ private class TestMessageSender : IMessageSender
+ {
+ public List MessageQueue = new List();
+
+ public void Send(ulong clientId, NetworkDelivery delivery, FastBufferWriter batchData)
+ {
+ MessageQueue.Add(batchData.ToArray());
+ }
+ }
+
+ private MessagingSystem m_MessagingSystem_v0;
+ private MessagingSystem m_MessagingSystem_v1;
+ private MessagingSystem m_MessagingSystem_v2;
+ private TestMessageSender m_MessageSender;
+
+ private void CreateFakeClients(MessagingSystem system, uint hash)
+ {
+ // Create three fake clients for each messaging system
+ // client 0 has version 0, client 1 has version 1, and client 2 has version 2
+ system.ClientConnected(0);
+ system.ClientConnected(1);
+ system.ClientConnected(2);
+ system.SetVersion(0, hash, 0);
+ system.SetVersion(1, hash, 1);
+ system.SetVersion(2, hash, 2);
+ }
+
+ [SetUp]
+ public void SetUp()
+ {
+ VersionedTestMessage_v0.Serialized = false;
+ VersionedTestMessage_v0.Deserialized = false;
+ VersionedTestMessage_v0.Handled = false;
+ VersionedTestMessage_v0.DeserializedValues.Clear();
+ VersionedTestMessage_v1.Serialized = false;
+ VersionedTestMessage_v1.Deserialized = false;
+ VersionedTestMessage_v1.Downgraded = false;
+ VersionedTestMessage_v1.Upgraded = false;
+ VersionedTestMessage_v1.Handled = false;
+ VersionedTestMessage_v1.DeserializedValues.Clear();
+ VersionedTestMessage.Serialized = false;
+ VersionedTestMessage.Deserialized = false;
+ VersionedTestMessage.Downgraded = false;
+ VersionedTestMessage.Upgraded = false;
+ VersionedTestMessage.Handled = false;
+ VersionedTestMessage.DeserializedValues.Clear();
+ m_MessageSender = new TestMessageSender();
+
+ m_MessagingSystem_v0 = new MessagingSystem(m_MessageSender, this, new TestMessageProvider_v0());
+ m_MessagingSystem_v1 = new MessagingSystem(m_MessageSender, this, new TestMessageProvider_v1());
+ m_MessagingSystem_v2 = new MessagingSystem(m_MessageSender, this, new TestMessageProvider_v2());
+
+ CreateFakeClients(m_MessagingSystem_v0, XXHash.Hash32(typeof(VersionedTestMessage_v0).FullName));
+ CreateFakeClients(m_MessagingSystem_v1, XXHash.Hash32(typeof(VersionedTestMessage_v1).FullName));
+ CreateFakeClients(m_MessagingSystem_v2, XXHash.Hash32(typeof(VersionedTestMessage).FullName));
+
+ // Make sure that all three messages got the same IDs...
+ Assert.AreEqual(
+ m_MessagingSystem_v0.GetMessageType(typeof(VersionedTestMessage_v0)),
+ m_MessagingSystem_v1.GetMessageType(typeof(VersionedTestMessage_v1)));
+ Assert.AreEqual(
+ m_MessagingSystem_v0.GetMessageType(typeof(VersionedTestMessage_v0)),
+ m_MessagingSystem_v2.GetMessageType(typeof(VersionedTestMessage)));
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ m_MessagingSystem_v0.Dispose();
+ m_MessagingSystem_v1.Dispose();
+ m_MessagingSystem_v2.Dispose();
+ }
+
+ private VersionedTestMessage_v0 GetMessage_v0()
+ {
+ var random = new Random();
+ return new VersionedTestMessage_v0
+ {
+ A = random.Next(),
+ B = random.Next(),
+ C = random.Next(),
+ };
+ }
+
+ private VersionedTestMessage_v1 GetMessage_v1()
+ {
+ var random = new Random();
+ return new VersionedTestMessage_v1
+ {
+ A = random.Next(),
+ B = random.Next(),
+ C = random.Next(),
+ D = random.Next(),
+ };
+ }
+
+ private VersionedTestMessage GetMessage_v2()
+ {
+ var random = new Random();
+ return new VersionedTestMessage
+ {
+ A = random.Next(),
+ D = (float)(random.NextDouble() * 10000),
+ E = ((long)random.Next() << 32) + random.Next()
+ };
+ }
+
+ public void CheckPostSendExpectations(int sourceLocalVersion, int remoteVersion)
+ {
+ Assert.AreEqual(Math.Min(sourceLocalVersion, remoteVersion) == 0, VersionedTestMessage_v0.Serialized);
+ Assert.AreEqual(Math.Min(sourceLocalVersion, remoteVersion) == 1, VersionedTestMessage_v1.Serialized);
+ Assert.AreEqual(Math.Min(sourceLocalVersion, remoteVersion) == 2, VersionedTestMessage.Serialized);
+ Assert.AreEqual(sourceLocalVersion >= 1 && remoteVersion < 1, VersionedTestMessage_v1.Downgraded);
+ Assert.AreEqual(sourceLocalVersion >= 2 && remoteVersion < 2, VersionedTestMessage.Downgraded);
+
+ Assert.AreEqual(1, m_MessageSender.MessageQueue.Count);
+ Assert.AreEqual(Math.Min(sourceLocalVersion, remoteVersion), SentVersion);
+ }
+
+ public void CheckPostReceiveExpectations(int sourceLocalVersion, int remoteVersion)
+ {
+ Assert.AreEqual(SentVersion == 0, VersionedTestMessage_v0.Deserialized);
+ Assert.AreEqual(SentVersion == 1, VersionedTestMessage_v1.Deserialized);
+ Assert.AreEqual(SentVersion == 2, VersionedTestMessage.Deserialized);
+ Assert.AreEqual(remoteVersion >= 1 && sourceLocalVersion < 1, VersionedTestMessage_v1.Upgraded);
+ Assert.AreEqual(remoteVersion >= 2 && sourceLocalVersion < 2, VersionedTestMessage.Upgraded);
+
+ Assert.AreEqual((remoteVersion == 0 ? 1 : 0), VersionedTestMessage_v0.DeserializedValues.Count);
+ Assert.AreEqual((remoteVersion == 1 ? 1 : 0), VersionedTestMessage_v1.DeserializedValues.Count);
+ Assert.AreEqual((remoteVersion == 2 ? 1 : 0), VersionedTestMessage.DeserializedValues.Count);
+
+ Assert.AreEqual(SentVersion, ReceivedVersion);
+ }
+
+ private void SendMessageWithVersions(T message, int fromVersion, int toVersion) where T : unmanaged, INetworkMessage
+ {
+ MessagingSystem sendSystem;
+ switch (fromVersion)
+ {
+ case 0: sendSystem = m_MessagingSystem_v0; break;
+ case 1: sendSystem = m_MessagingSystem_v1; break;
+ default: sendSystem = m_MessagingSystem_v2; break;
+ }
+ sendSystem.SendMessage(ref message, NetworkDelivery.Reliable, (ulong)toVersion);
+ sendSystem.ProcessSendQueues();
+ CheckPostSendExpectations(fromVersion, toVersion);
+
+ MessagingSystem receiveSystem;
+ switch (toVersion)
+ {
+ case 0: receiveSystem = m_MessagingSystem_v0; break;
+ case 1: receiveSystem = m_MessagingSystem_v1; break;
+ default: receiveSystem = m_MessagingSystem_v2; break;
+ }
+ receiveSystem.HandleIncomingData((ulong)fromVersion, new ArraySegment(m_MessageSender.MessageQueue[0]), 0.0f);
+ receiveSystem.ProcessIncomingMessageQueue();
+ CheckPostReceiveExpectations(fromVersion, toVersion);
+
+ m_MessageSender.MessageQueue.Clear();
+ }
+
+ [Test]
+ public void WhenSendingV0ToV0_DataIsReceivedCorrectly()
+ {
+ var message = GetMessage_v0();
+
+ SendMessageWithVersions(message, 0, 0);
+
+ var receivedMessage = VersionedTestMessage_v0.DeserializedValues[0];
+ Assert.AreEqual(message.A, receivedMessage.A);
+ Assert.AreEqual(message.B, receivedMessage.B);
+ Assert.AreEqual(message.C, receivedMessage.C);
+ }
+
+ [Test]
+ public void WhenSendingV0ToV1_DataIsReceivedCorrectly()
+ {
+ var message = GetMessage_v0();
+
+ SendMessageWithVersions(message, 0, 1);
+
+ var receivedMessage = VersionedTestMessage_v1.DeserializedValues[0];
+ Assert.AreEqual(message.A, receivedMessage.A);
+ Assert.AreEqual(message.B, receivedMessage.B);
+ Assert.AreEqual(message.C, receivedMessage.C);
+ Assert.AreEqual(k_DefaultD, receivedMessage.D);
+ }
+
+ [Test]
+ public void WhenSendingV0ToV2_DataIsReceivedCorrectly()
+ {
+ var message = GetMessage_v0();
+
+ SendMessageWithVersions(message, 0, 2);
+
+ var receivedMessage = VersionedTestMessage.DeserializedValues[0];
+ Assert.AreEqual(message.A, receivedMessage.A);
+ Assert.AreEqual((float)k_DefaultD, receivedMessage.D);
+ Assert.AreEqual(k_DefaultE, receivedMessage.E);
+ }
+
+ [Test]
+ public void WhenSendingV1ToV0_DataIsReceivedCorrectly()
+ {
+ var message = GetMessage_v1();
+
+ SendMessageWithVersions(message, 1, 0);
+
+ var receivedMessage = VersionedTestMessage_v0.DeserializedValues[0];
+ Assert.AreEqual(message.A, receivedMessage.A);
+ Assert.AreEqual(message.B, receivedMessage.B);
+ Assert.AreEqual(message.C, receivedMessage.C);
+ }
+
+ [Test]
+ public void WhenSendingV1ToV1_DataIsReceivedCorrectly()
+ {
+ var message = GetMessage_v1();
+
+ SendMessageWithVersions(message, 1, 1);
+
+ var receivedMessage = VersionedTestMessage_v1.DeserializedValues[0];
+ Assert.AreEqual(message.A, receivedMessage.A);
+ Assert.AreEqual(message.B, receivedMessage.B);
+ Assert.AreEqual(message.C, receivedMessage.C);
+ Assert.AreEqual(message.D, receivedMessage.D);
+ }
+
+ [Test]
+ public void WhenSendingV1ToV2_DataIsReceivedCorrectly()
+ {
+ var message = GetMessage_v1();
+
+ SendMessageWithVersions(message, 1, 2);
+
+ var receivedMessage = VersionedTestMessage.DeserializedValues[0];
+ Assert.AreEqual(message.A, receivedMessage.A);
+ Assert.AreEqual((float)message.D, receivedMessage.D);
+ Assert.AreEqual(k_DefaultE, receivedMessage.E);
+ }
+
+ [Test]
+ public void WhenSendingV2ToV0_DataIsReceivedCorrectly()
+ {
+ var message = GetMessage_v2();
+
+ SendMessageWithVersions(message, 2, 0);
+
+ var receivedMessage = VersionedTestMessage_v0.DeserializedValues[0];
+ Assert.AreEqual(message.A, receivedMessage.A);
+ Assert.AreEqual(k_DefaultB, receivedMessage.B);
+ Assert.AreEqual(k_DefaultC, receivedMessage.C);
+ }
+
+ [Test]
+ public void WhenSendingV2ToV1_DataIsReceivedCorrectly()
+ {
+ var message = GetMessage_v2();
+
+ SendMessageWithVersions(message, 2, 1);
+
+ var receivedMessage = VersionedTestMessage_v1.DeserializedValues[0];
+ Assert.AreEqual(message.A, receivedMessage.A);
+ Assert.AreEqual(k_DefaultB, receivedMessage.B);
+ Assert.AreEqual(k_DefaultC, receivedMessage.C);
+ Assert.AreEqual((int)message.D, receivedMessage.D);
+ }
+
+ [Test]
+ public void WhenSendingV2ToV2_DataIsReceivedCorrectly()
+ {
+ var message = GetMessage_v2();
+
+ SendMessageWithVersions(message, 2, 2);
+
+ var receivedMessage = VersionedTestMessage.DeserializedValues[0];
+ Assert.AreEqual(message.A, receivedMessage.A);
+ Assert.AreEqual(message.D, receivedMessage.D);
+ Assert.AreEqual(message.E, receivedMessage.E);
+ }
+ }
+}
diff --git a/Tests/Editor/Messaging/MessageVersioningTests.cs.meta b/Tests/Editor/Messaging/MessageVersioningTests.cs.meta
new file mode 100644
index 0000000..11b7f62
--- /dev/null
+++ b/Tests/Editor/Messaging/MessageVersioningTests.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: eac9a654aacb4faf91128c9ab6024543
+timeCreated: 1667326658
\ No newline at end of file
diff --git a/Tests/Editor/NetworkObjectTests.cs b/Tests/Editor/NetworkObjectTests.cs
index d887dcf..cb14dbf 100644
--- a/Tests/Editor/NetworkObjectTests.cs
+++ b/Tests/Editor/NetworkObjectTests.cs
@@ -1,3 +1,4 @@
+using System.Text.RegularExpressions;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
@@ -31,19 +32,17 @@ namespace Unity.Netcode.EditorTests
}
[Test]
- public void GetBehaviourIndexNone()
+ [TestCase(0)]
+ [TestCase(1)]
+ [TestCase(2)]
+ public void GetBehaviourIndexNone(int index)
{
var gameObject = new GameObject(nameof(GetBehaviourIndexNone));
var networkObject = gameObject.AddComponent();
- // TODO: Maybe not hardcode message?
- LogAssert.Expect(LogType.Error, $"[Netcode] Behaviour index was out of bounds. Did you mess up the order of your {nameof(NetworkBehaviour)}s?");
- LogAssert.Expect(LogType.Error, $"[Netcode] Behaviour index was out of bounds. Did you mess up the order of your {nameof(NetworkBehaviour)}s?");
- LogAssert.Expect(LogType.Error, $"[Netcode] Behaviour index was out of bounds. Did you mess up the order of your {nameof(NetworkBehaviour)}s?");
+ LogAssert.Expect(LogType.Error, new Regex(".*out of bounds.*"));
- Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(0), Is.Null);
- Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(1), Is.Null);
- Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(2), Is.Null);
+ Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex((ushort)index), Is.Null);
// Cleanup
Object.DestroyImmediate(gameObject);
@@ -56,13 +55,10 @@ namespace Unity.Netcode.EditorTests
var networkObject = gameObject.AddComponent();
var networkBehaviour = gameObject.AddComponent();
- // TODO: Maybe not hardcode message?
- LogAssert.Expect(LogType.Error, $"[Netcode] Behaviour index was out of bounds. Did you mess up the order of your {nameof(NetworkBehaviour)}s?");
- LogAssert.Expect(LogType.Error, $"[Netcode] Behaviour index was out of bounds. Did you mess up the order of your {nameof(NetworkBehaviour)}s?");
+ LogAssert.Expect(LogType.Error, new Regex(".*out of bounds.*"));
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(0), Is.EqualTo(networkBehaviour));
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(1), Is.Null);
- Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(2), Is.Null);
// Cleanup
Object.DestroyImmediate(gameObject);
diff --git a/Tests/Editor/Serialization/BytePackerTests.cs b/Tests/Editor/Serialization/BytePackerTests.cs
index c4e039b..3c072b4 100644
--- a/Tests/Editor/Serialization/BytePackerTests.cs
+++ b/Tests/Editor/Serialization/BytePackerTests.cs
@@ -76,116 +76,6 @@ namespace Unity.Netcode.EditorTests
#endregion
- private void CheckUnsignedPackedSize64(FastBufferWriter writer, ulong value)
- {
-
- if (value <= 240)
- {
- Assert.AreEqual(1, writer.Position);
- }
- else if (value <= 2287)
- {
- Assert.AreEqual(2, writer.Position);
- }
- else
- {
- Assert.AreEqual(BitCounter.GetUsedByteCount(value) + 1, writer.Position);
- }
- }
-
- private void CheckUnsignedPackedValue64(FastBufferWriter writer, ulong value)
- {
- var reader = new FastBufferReader(writer, Allocator.Temp);
- using (reader)
- {
- ByteUnpacker.ReadValuePacked(reader, out ulong readValue);
- Assert.AreEqual(readValue, value);
- }
- }
-
- private void CheckUnsignedPackedSize32(FastBufferWriter writer, uint value)
- {
-
- if (value <= 240)
- {
- Assert.AreEqual(1, writer.Position);
- }
- else if (value <= 2287)
- {
- Assert.AreEqual(2, writer.Position);
- }
- else
- {
- Assert.AreEqual(BitCounter.GetUsedByteCount(value) + 1, writer.Position);
- }
- }
-
- private void CheckUnsignedPackedValue32(FastBufferWriter writer, uint value)
- {
- var reader = new FastBufferReader(writer, Allocator.Temp);
- using (reader)
- {
- ByteUnpacker.ReadValuePacked(reader, out uint readValue);
- Assert.AreEqual(readValue, value);
- }
- }
-
- private void CheckSignedPackedSize64(FastBufferWriter writer, long value)
- {
- ulong asUlong = Arithmetic.ZigZagEncode(value);
-
- if (asUlong <= 240)
- {
- Assert.AreEqual(1, writer.Position);
- }
- else if (asUlong <= 2287)
- {
- Assert.AreEqual(2, writer.Position);
- }
- else
- {
- Assert.AreEqual(BitCounter.GetUsedByteCount(asUlong) + 1, writer.Position);
- }
- }
-
- private void CheckSignedPackedValue64(FastBufferWriter writer, long value)
- {
- var reader = new FastBufferReader(writer, Allocator.Temp);
- using (reader)
- {
- ByteUnpacker.ReadValuePacked(reader, out long readValue);
- Assert.AreEqual(readValue, value);
- }
- }
-
- private void CheckSignedPackedSize32(FastBufferWriter writer, int value)
- {
- ulong asUlong = Arithmetic.ZigZagEncode(value);
-
- if (asUlong <= 240)
- {
- Assert.AreEqual(1, writer.Position);
- }
- else if (asUlong <= 2287)
- {
- Assert.AreEqual(2, writer.Position);
- }
- else
- {
- Assert.AreEqual(BitCounter.GetUsedByteCount(asUlong) + 1, writer.Position);
- }
- }
-
- private void CheckSignedPackedValue32(FastBufferWriter writer, int value)
- {
- var reader = new FastBufferReader(writer, Allocator.Temp);
- using (reader)
- {
- ByteUnpacker.ReadValuePacked(reader, out int readValue);
- Assert.AreEqual(readValue, value);
- }
- }
-
private unsafe void VerifyBytewiseEquality(T value, T otherValue) where T : unmanaged
{
byte* asBytePointer = (byte*)&value;
@@ -229,164 +119,53 @@ namespace Unity.Netcode.EditorTests
}
}
-
- [Test]
- public void TestPacking64BitsUnsigned()
+ private int GetByteCount64Bits(ulong value)
{
- var writer = new FastBufferWriter(9, Allocator.Temp);
- using (writer)
+ if (value <= 0b0000_1111)
{
- writer.TryBeginWrite(9);
- ulong value = 0;
- BytePacker.WriteValuePacked(writer, value);
- Assert.AreEqual(1, writer.Position);
-
- for (var i = 0; i < 64; ++i)
- {
- value = 1UL << i;
- writer.Seek(0);
- writer.Truncate();
- BytePacker.WriteValuePacked(writer, value);
- CheckUnsignedPackedSize64(writer, value);
- CheckUnsignedPackedValue64(writer, value);
- for (var j = 0; j < 8; ++j)
- {
- value = (1UL << i) | (1UL << j);
- writer.Seek(0);
- writer.Truncate();
- BytePacker.WriteValuePacked(writer, value);
- CheckUnsignedPackedSize64(writer, value);
- CheckUnsignedPackedValue64(writer, value);
- }
- }
+ return 1;
}
+
+ if (value <= 0b0000_1111_1111_1111)
+ {
+ return 2;
+ }
+
+ if (value <= 0b0000_1111_1111_1111_1111_1111)
+ {
+ return 3;
+ }
+
+ if (value <= 0b0000_1111_1111_1111_1111_1111_1111_1111)
+ {
+ return 4;
+ }
+
+ if (value <= 0b0000_1111_1111_1111_1111_1111_1111_1111_1111_1111)
+ {
+ return 5;
+ }
+
+ if (value <= 0b0000_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111)
+ {
+ return 6;
+ }
+
+ if (value <= 0b0000_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111)
+ {
+ return 7;
+ }
+
+ if (value <= 0b0000_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111)
+ {
+ return 8;
+ }
+
+ return 9;
}
- [Test]
- public void TestPacking32BitsUnsigned()
- {
- var writer = new FastBufferWriter(9, Allocator.Temp);
-
- using (writer)
- {
- writer.TryBeginWrite(9);
- uint value = 0;
- BytePacker.WriteValuePacked(writer, value);
- Assert.AreEqual(1, writer.Position);
-
- for (var i = 0; i < 64; ++i)
- {
- value = 1U << i;
- writer.Seek(0);
- writer.Truncate();
- BytePacker.WriteValuePacked(writer, value);
- CheckUnsignedPackedSize32(writer, value);
- CheckUnsignedPackedValue32(writer, value);
- for (var j = 0; j < 8; ++j)
- {
- value = (1U << i) | (1U << j);
- writer.Seek(0);
- writer.Truncate();
- BytePacker.WriteValuePacked(writer, value);
- CheckUnsignedPackedSize32(writer, value);
- CheckUnsignedPackedValue32(writer, value);
- }
- }
- }
- }
-
- [Test]
- public void TestPacking64BitsSigned()
- {
- var writer = new FastBufferWriter(9, Allocator.Temp);
-
- using (writer)
- {
- writer.TryBeginWrite(9);
- long value = 0;
- BytePacker.WriteValuePacked(writer, value);
- Assert.AreEqual(1, writer.Position);
-
- for (var i = 0; i < 64; ++i)
- {
- value = 1L << i;
- writer.Seek(0);
- writer.Truncate();
- BytePacker.WriteValuePacked(writer, value);
- CheckSignedPackedSize64(writer, value);
- CheckSignedPackedValue64(writer, value);
-
- writer.Seek(0);
- writer.Truncate();
- BytePacker.WriteValuePacked(writer, -value);
- CheckSignedPackedSize64(writer, -value);
- CheckSignedPackedValue64(writer, -value);
- for (var j = 0; j < 8; ++j)
- {
- value = (1L << i) | (1L << j);
- writer.Seek(0);
- writer.Truncate();
- BytePacker.WriteValuePacked(writer, value);
- CheckSignedPackedSize64(writer, value);
- CheckSignedPackedValue64(writer, value);
-
- writer.Seek(0);
- writer.Truncate();
- BytePacker.WriteValuePacked(writer, -value);
- CheckSignedPackedSize64(writer, -value);
- CheckSignedPackedValue64(writer, -value);
- }
- }
- }
- }
-
- [Test]
- public void TestPacking32BitsSigned()
- {
- var writer = new FastBufferWriter(9, Allocator.Temp);
-
- using (writer)
- {
- writer.TryBeginWrite(5);
- int value = 0;
- BytePacker.WriteValuePacked(writer, value);
- Assert.AreEqual(1, writer.Position);
-
- for (var i = 0; i < 64; ++i)
- {
- value = 1 << i;
- writer.Seek(0);
- writer.Truncate();
- BytePacker.WriteValuePacked(writer, value);
- CheckSignedPackedSize32(writer, value);
- CheckSignedPackedValue32(writer, value);
-
- writer.Seek(0);
- writer.Truncate();
- BytePacker.WriteValuePacked(writer, -value);
- CheckSignedPackedSize32(writer, -value);
- CheckSignedPackedValue32(writer, -value);
- for (var j = 0; j < 8; ++j)
- {
- value = (1 << i) | (1 << j);
- writer.Seek(0);
- writer.Truncate();
- BytePacker.WriteValuePacked(writer, value);
- CheckSignedPackedSize32(writer, value);
- CheckSignedPackedValue32(writer, value);
-
- writer.Seek(0);
- writer.Truncate();
- BytePacker.WriteValuePacked(writer, -value);
- CheckSignedPackedSize32(writer, -value);
- CheckSignedPackedValue32(writer, -value);
- }
- }
- }
- }
-
- private int GetByteCount61Bits(ulong value)
+ private int GetByteCount32Bits(uint value)
{
if (value <= 0b0001_1111)
@@ -409,57 +188,25 @@ namespace Unity.Netcode.EditorTests
return 4;
}
- if (value <= 0b0001_1111_1111_1111_1111_1111_1111_1111_1111_1111)
- {
- return 5;
- }
-
- if (value <= 0b0001_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111)
- {
- return 6;
- }
-
- if (value <= 0b0001_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111)
- {
- return 7;
- }
-
- return 8;
+ return 5;
}
- private int GetByteCount30Bits(uint value)
+ private int GetByteCount16Bits(ushort value)
{
if (value <= 0b0011_1111)
{
return 1;
}
-
if (value <= 0b0011_1111_1111_1111)
{
return 2;
}
- if (value <= 0b0011_1111_1111_1111_1111_1111)
- {
- return 3;
- }
-
- return 4;
+ return 3;
}
- private int GetByteCount15Bits(ushort value)
- {
-
- if (value <= 0b0111_1111)
- {
- return 1;
- }
-
- return 2;
- }
-
- private ulong Get61BitEncodedValue(FastBufferWriter writer)
+ private ulong Get64BitEncodedValue(FastBufferWriter writer)
{
var reader = new FastBufferReader(writer, Allocator.Temp);
using (reader)
@@ -469,7 +216,7 @@ namespace Unity.Netcode.EditorTests
}
}
- private long Get60BitSignedEncodedValue(FastBufferWriter writer)
+ private long Get64BitSignedEncodedValue(FastBufferWriter writer)
{
var reader = new FastBufferReader(writer, Allocator.Temp);
using (reader)
@@ -479,7 +226,7 @@ namespace Unity.Netcode.EditorTests
}
}
- private uint Get30BitEncodedValue(FastBufferWriter writer)
+ private uint Get32BitEncodedValue(FastBufferWriter writer)
{
var reader = new FastBufferReader(writer, Allocator.Temp);
using (reader)
@@ -489,7 +236,7 @@ namespace Unity.Netcode.EditorTests
}
}
- private int Get29BitSignedEncodedValue(FastBufferWriter writer)
+ private int Get32BitSignedEncodedValue(FastBufferWriter writer)
{
var reader = new FastBufferReader(writer, Allocator.Temp);
using (reader)
@@ -499,7 +246,7 @@ namespace Unity.Netcode.EditorTests
}
}
- private ushort Get15BitEncodedValue(FastBufferWriter writer)
+ private ushort Get16BitEncodedValue(FastBufferWriter writer)
{
var reader = new FastBufferReader(writer, Allocator.Temp);
using (reader)
@@ -509,7 +256,7 @@ namespace Unity.Netcode.EditorTests
}
}
- private short Get14BitSignedEncodedValue(FastBufferWriter writer)
+ private short Get16BitSignedEncodedValue(FastBufferWriter writer)
{
var reader = new FastBufferReader(writer, Allocator.Temp);
using (reader)
@@ -520,7 +267,7 @@ namespace Unity.Netcode.EditorTests
}
[Test]
- public void TestBitPacking61BitsUnsigned()
+ public void TestBitPacking64BitsUnsigned()
{
var writer = new FastBufferWriter(9, Allocator.Temp);
@@ -530,18 +277,18 @@ namespace Unity.Netcode.EditorTests
ulong value = 0;
BytePacker.WriteValueBitPacked(writer, value);
Assert.AreEqual(1, writer.Position);
- Assert.AreEqual(0, writer.ToArray()[0] & 0b111);
- Assert.AreEqual(value, Get61BitEncodedValue(writer));
+ Assert.AreEqual(1, writer.ToArray()[0] & 0b1111);
+ Assert.AreEqual(value, Get64BitEncodedValue(writer));
- for (var i = 0; i < 61; ++i)
+ for (var i = 0; i < 64; ++i)
{
value = 1UL << i;
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount61Bits(value), writer.Position, $"Failed on {value} ({i})");
- Assert.AreEqual(GetByteCount61Bits(value) - 1, writer.ToArray()[0] & 0b111, $"Failed on {value} ({i})");
- Assert.AreEqual(value, Get61BitEncodedValue(writer));
+ Assert.AreEqual(GetByteCount64Bits(value), writer.Position, $"Failed on {value} ({i})");
+ Assert.AreEqual(GetByteCount64Bits(value), writer.ToArray()[0] & 0b1111, $"Failed on {value} ({i})");
+ Assert.AreEqual(value, Get64BitEncodedValue(writer));
for (var j = 0; j < 8; ++j)
{
@@ -549,18 +296,16 @@ namespace Unity.Netcode.EditorTests
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount61Bits(value), writer.Position, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(GetByteCount61Bits(value) - 1, writer.ToArray()[0] & 0b111, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(value, Get61BitEncodedValue(writer));
+ Assert.AreEqual(GetByteCount64Bits(value), writer.Position, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(GetByteCount64Bits(value), writer.ToArray()[0] & 0b1111, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(value, Get64BitEncodedValue(writer));
}
}
-
- Assert.Throws(() => { BytePacker.WriteValueBitPacked(writer, 1UL << 61); });
}
}
[Test]
- public void TestBitPacking60BitsSigned()
+ public void TestBitPacking64BitsSigned()
{
var writer = new FastBufferWriter(9, Allocator.Temp);
@@ -570,28 +315,28 @@ namespace Unity.Netcode.EditorTests
long value = 0;
BytePacker.WriteValueBitPacked(writer, value);
Assert.AreEqual(1, writer.Position);
- Assert.AreEqual(0, writer.ToArray()[0] & 0b111);
- Assert.AreEqual(value, Get60BitSignedEncodedValue(writer));
+ Assert.AreEqual(1, writer.ToArray()[0] & 0b1111);
+ Assert.AreEqual(value, Get64BitSignedEncodedValue(writer));
- for (var i = 0; i < 61; ++i)
+ for (var i = 0; i < 64; ++i)
{
value = 1U << i;
ulong zzvalue = Arithmetic.ZigZagEncode(value);
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount61Bits(zzvalue), writer.Position, $"Failed on {value} ({i})");
- Assert.AreEqual(GetByteCount61Bits(zzvalue) - 1, writer.ToArray()[0] & 0b111, $"Failed on {value} ({i})");
- Assert.AreEqual(value, Get60BitSignedEncodedValue(writer));
+ Assert.AreEqual(GetByteCount64Bits(zzvalue), writer.Position, $"Failed on {value} ({i})");
+ Assert.AreEqual(GetByteCount64Bits(zzvalue), writer.ToArray()[0] & 0b1111, $"Failed on {value} ({i})");
+ Assert.AreEqual(value, Get64BitSignedEncodedValue(writer));
value = -value;
zzvalue = Arithmetic.ZigZagEncode(value);
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount61Bits(zzvalue), writer.Position, $"Failed on {value} ({i})");
- Assert.AreEqual(GetByteCount61Bits(zzvalue) - 1, writer.ToArray()[0] & 0b111, $"Failed on {value} ({i})");
- Assert.AreEqual(value, Get60BitSignedEncodedValue(writer));
+ Assert.AreEqual(GetByteCount64Bits(zzvalue), writer.Position, $"Failed on {value} ({i})");
+ Assert.AreEqual(GetByteCount64Bits(zzvalue), writer.ToArray()[0] & 0b1111, $"Failed on {value} ({i})");
+ Assert.AreEqual(value, Get64BitSignedEncodedValue(writer));
for (var j = 0; j < 8; ++j)
{
@@ -600,27 +345,25 @@ namespace Unity.Netcode.EditorTests
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount61Bits(zzvalue), writer.Position, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(GetByteCount61Bits(zzvalue) - 1, writer.ToArray()[0] & 0b111, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(value, Get60BitSignedEncodedValue(writer));
+ Assert.AreEqual(GetByteCount64Bits(zzvalue), writer.Position, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(GetByteCount64Bits(zzvalue), writer.ToArray()[0] & 0b1111, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(value, Get64BitSignedEncodedValue(writer));
value = -value;
zzvalue = Arithmetic.ZigZagEncode(value);
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount61Bits(zzvalue), writer.Position, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(GetByteCount61Bits(zzvalue) - 1, writer.ToArray()[0] & 0b111, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(value, Get60BitSignedEncodedValue(writer));
+ Assert.AreEqual(GetByteCount64Bits(zzvalue), writer.Position, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(GetByteCount64Bits(zzvalue), writer.ToArray()[0] & 0b1111, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(value, Get64BitSignedEncodedValue(writer));
}
}
-
- Assert.Throws(() => { BytePacker.WriteValueBitPacked(writer, 1UL << 61); });
}
}
[Test]
- public void TestBitPacking30BitsUnsigned()
+ public void TestBitPacking32BitsUnsigned()
{
var writer = new FastBufferWriter(9, Allocator.Temp);
@@ -630,18 +373,18 @@ namespace Unity.Netcode.EditorTests
uint value = 0;
BytePacker.WriteValueBitPacked(writer, value);
Assert.AreEqual(1, writer.Position);
- Assert.AreEqual(0, writer.ToArray()[0] & 0b11);
- Assert.AreEqual(value, Get30BitEncodedValue(writer));
+ Assert.AreEqual(1, writer.ToArray()[0] & 0b111);
+ Assert.AreEqual(value, Get32BitEncodedValue(writer));
- for (var i = 0; i < 30; ++i)
+ for (var i = 0; i < 32; ++i)
{
value = 1U << i;
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount30Bits(value), writer.Position, $"Failed on {value} ({i})");
- Assert.AreEqual(GetByteCount30Bits(value) - 1, writer.ToArray()[0] & 0b11, $"Failed on {value} ({i})");
- Assert.AreEqual(value, Get30BitEncodedValue(writer));
+ Assert.AreEqual(GetByteCount32Bits(value), writer.Position, $"Failed on {value} ({i})");
+ Assert.AreEqual(GetByteCount32Bits(value), writer.ToArray()[0] & 0b111, $"Failed on {value} ({i})");
+ Assert.AreEqual(value, Get32BitEncodedValue(writer));
for (var j = 0; j < 8; ++j)
{
@@ -649,18 +392,16 @@ namespace Unity.Netcode.EditorTests
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount30Bits(value), writer.Position, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(GetByteCount30Bits(value) - 1, writer.ToArray()[0] & 0b11, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(value, Get30BitEncodedValue(writer));
+ Assert.AreEqual(GetByteCount32Bits(value), writer.Position, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(GetByteCount32Bits(value), writer.ToArray()[0] & 0b111, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(value, Get32BitEncodedValue(writer));
}
}
-
- Assert.Throws(() => { BytePacker.WriteValueBitPacked(writer, 1U << 30); });
}
}
[Test]
- public void TestBitPacking29BitsSigned()
+ public void TestBitPacking32BitsSigned()
{
var writer = new FastBufferWriter(9, Allocator.Temp);
@@ -670,28 +411,28 @@ namespace Unity.Netcode.EditorTests
int value = 0;
BytePacker.WriteValueBitPacked(writer, value);
Assert.AreEqual(1, writer.Position);
- Assert.AreEqual(0, writer.ToArray()[0] & 0b11);
- Assert.AreEqual(value, Get30BitEncodedValue(writer));
+ Assert.AreEqual(1, writer.ToArray()[0] & 0b111);
+ Assert.AreEqual(value, Get32BitEncodedValue(writer));
- for (var i = 0; i < 29; ++i)
+ for (var i = 0; i < 32; ++i)
{
value = 1 << i;
uint zzvalue = (uint)Arithmetic.ZigZagEncode(value);
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount30Bits(zzvalue), writer.Position, $"Failed on {value} ({i})");
- Assert.AreEqual(GetByteCount30Bits(zzvalue) - 1, writer.ToArray()[0] & 0b11, $"Failed on {value} ({i})");
- Assert.AreEqual(value, Get29BitSignedEncodedValue(writer));
+ Assert.AreEqual(GetByteCount32Bits(zzvalue), writer.Position, $"Failed on {value} ({i})");
+ Assert.AreEqual(GetByteCount32Bits(zzvalue), writer.ToArray()[0] & 0b111, $"Failed on {value} ({i})");
+ Assert.AreEqual(value, Get32BitSignedEncodedValue(writer));
value = -value;
zzvalue = (uint)Arithmetic.ZigZagEncode(value);
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount30Bits(zzvalue), writer.Position, $"Failed on {value} ({i})");
- Assert.AreEqual(GetByteCount30Bits(zzvalue) - 1, writer.ToArray()[0] & 0b11, $"Failed on {value} ({i})");
- Assert.AreEqual(value, Get29BitSignedEncodedValue(writer));
+ Assert.AreEqual(GetByteCount32Bits(zzvalue), writer.Position, $"Failed on {value} ({i})");
+ Assert.AreEqual(GetByteCount32Bits(zzvalue), writer.ToArray()[0] & 0b111, $"Failed on {value} ({i})");
+ Assert.AreEqual(value, Get32BitSignedEncodedValue(writer));
for (var j = 0; j < 8; ++j)
{
@@ -700,25 +441,25 @@ namespace Unity.Netcode.EditorTests
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount30Bits(zzvalue), writer.Position, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(GetByteCount30Bits(zzvalue) - 1, writer.ToArray()[0] & 0b11, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(value, Get29BitSignedEncodedValue(writer));
+ Assert.AreEqual(GetByteCount32Bits(zzvalue), writer.Position, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(GetByteCount32Bits(zzvalue), writer.ToArray()[0] & 0b111, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(value, Get32BitSignedEncodedValue(writer));
value = -value;
zzvalue = (uint)Arithmetic.ZigZagEncode(value);
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount30Bits(zzvalue), writer.Position, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(GetByteCount30Bits(zzvalue) - 1, writer.ToArray()[0] & 0b11, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(value, Get29BitSignedEncodedValue(writer));
+ Assert.AreEqual(GetByteCount32Bits(zzvalue), writer.Position, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(GetByteCount32Bits(zzvalue), writer.ToArray()[0] & 0b111, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(value, Get32BitSignedEncodedValue(writer));
}
}
}
}
[Test]
- public void TestBitPacking15BitsUnsigned()
+ public void TestBitPacking16BitsUnsigned()
{
var writer = new FastBufferWriter(9, Allocator.Temp);
@@ -728,18 +469,18 @@ namespace Unity.Netcode.EditorTests
ushort value = 0;
BytePacker.WriteValueBitPacked(writer, value);
Assert.AreEqual(1, writer.Position);
- Assert.AreEqual(0, writer.ToArray()[0] & 0b1);
- Assert.AreEqual(value, Get15BitEncodedValue(writer));
+ Assert.AreEqual(1, writer.ToArray()[0] & 0b11);
+ Assert.AreEqual(value, Get16BitEncodedValue(writer));
- for (var i = 0; i < 15; ++i)
+ for (var i = 0; i < 16; ++i)
{
value = (ushort)(1U << i);
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount15Bits(value), writer.Position, $"Failed on {value} ({i})");
- Assert.AreEqual(GetByteCount15Bits(value) - 1, writer.ToArray()[0] & 0b1, $"Failed on {value} ({i})");
- Assert.AreEqual(value, Get15BitEncodedValue(writer));
+ Assert.AreEqual(GetByteCount16Bits(value), writer.Position, $"Failed on {value} ({i})");
+ Assert.AreEqual(GetByteCount16Bits(value), writer.ToArray()[0] & 0b11, $"Failed on {value} ({i})");
+ Assert.AreEqual(value, Get16BitEncodedValue(writer));
for (var j = 0; j < 8; ++j)
{
@@ -747,17 +488,15 @@ namespace Unity.Netcode.EditorTests
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount15Bits(value), writer.Position, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(GetByteCount15Bits(value) - 1, writer.ToArray()[0] & 0b1, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(value, Get15BitEncodedValue(writer));
+ Assert.AreEqual(GetByteCount16Bits(value), writer.Position, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(GetByteCount16Bits(value), writer.ToArray()[0] & 0b11, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(value, Get16BitEncodedValue(writer));
}
}
-
- Assert.Throws(() => { BytePacker.WriteValueBitPacked(writer, (ushort)(1U << 15)); });
}
}
[Test]
- public void TestBitPacking14BitsSigned()
+ public void TestBitPacking16BitsSigned()
{
var writer = new FastBufferWriter(9, Allocator.Temp);
@@ -767,28 +506,28 @@ namespace Unity.Netcode.EditorTests
short value = 0;
BytePacker.WriteValueBitPacked(writer, value);
Assert.AreEqual(1, writer.Position);
- Assert.AreEqual(0, writer.ToArray()[0] & 0b1);
- Assert.AreEqual(value, Get15BitEncodedValue(writer));
+ Assert.AreEqual(1, writer.ToArray()[0] & 0b11);
+ Assert.AreEqual(value, Get16BitEncodedValue(writer));
- for (var i = 0; i < 14; ++i)
+ for (var i = 0; i < 16; ++i)
{
value = (short)(1 << i);
ushort zzvalue = (ushort)Arithmetic.ZigZagEncode(value);
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount15Bits(zzvalue), writer.Position, $"Failed on {value} ({i})");
- Assert.AreEqual(GetByteCount15Bits(zzvalue) - 1, writer.ToArray()[0] & 0b1, $"Failed on {value} ({i})");
- Assert.AreEqual(value, Get14BitSignedEncodedValue(writer));
+ Assert.AreEqual(GetByteCount16Bits(zzvalue), writer.Position, $"Failed on {value} ({i})");
+ Assert.AreEqual(GetByteCount16Bits(zzvalue), writer.ToArray()[0] & 0b11, $"Failed on {value} ({i})");
+ Assert.AreEqual(value, Get16BitSignedEncodedValue(writer));
value = (short)-value;
zzvalue = (ushort)Arithmetic.ZigZagEncode(value);
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount15Bits(zzvalue), writer.Position, $"Failed on {value} ({i})");
- Assert.AreEqual(GetByteCount15Bits(zzvalue) - 1, writer.ToArray()[0] & 0b1, $"Failed on {value} ({i})");
- Assert.AreEqual(value, Get14BitSignedEncodedValue(writer));
+ Assert.AreEqual(GetByteCount16Bits(zzvalue), writer.Position, $"Failed on {value} ({i})");
+ Assert.AreEqual(GetByteCount16Bits(zzvalue), writer.ToArray()[0] & 0b11, $"Failed on {value} ({i})");
+ Assert.AreEqual(value, Get16BitSignedEncodedValue(writer));
for (var j = 0; j < 8; ++j)
{
@@ -797,18 +536,18 @@ namespace Unity.Netcode.EditorTests
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount15Bits(zzvalue), writer.Position, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(GetByteCount15Bits(zzvalue) - 1, writer.ToArray()[0] & 0b1, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(value, Get14BitSignedEncodedValue(writer));
+ Assert.AreEqual(GetByteCount16Bits(zzvalue), writer.Position, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(GetByteCount16Bits(zzvalue), writer.ToArray()[0] & 0b11, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(value, Get16BitSignedEncodedValue(writer));
value = (short)-value;
zzvalue = (ushort)Arithmetic.ZigZagEncode(value);
writer.Seek(0);
writer.Truncate();
BytePacker.WriteValueBitPacked(writer, value);
- Assert.AreEqual(GetByteCount15Bits(zzvalue), writer.Position, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(GetByteCount15Bits(zzvalue) - 1, writer.ToArray()[0] & 0b1, $"Failed on {value} ({i}, {j})");
- Assert.AreEqual(value, Get14BitSignedEncodedValue(writer));
+ Assert.AreEqual(GetByteCount16Bits(zzvalue), writer.Position, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(GetByteCount16Bits(zzvalue), writer.ToArray()[0] & 0b11, $"Failed on {value} ({i}, {j})");
+ Assert.AreEqual(value, Get16BitSignedEncodedValue(writer));
}
}
}
diff --git a/Tests/Editor/Transports/UnityTransportTests.cs b/Tests/Editor/Transports/UnityTransportTests.cs
index fa53588..5303974 100644
--- a/Tests/Editor/Transports/UnityTransportTests.cs
+++ b/Tests/Editor/Transports/UnityTransportTests.cs
@@ -126,5 +126,40 @@ namespace Unity.Netcode.EditorTests
transport.Shutdown();
}
+
+#if UTP_TRANSPORT_2_0_ABOVE
+ [Test]
+ public void UnityTransport_EmptySecurityStringsShouldThrow([Values("", null)] string cert, [Values("", null)] string secret)
+ {
+ var supportingGO = new GameObject();
+ try
+ {
+ var networkManager = supportingGO.AddComponent(); // NM is required for UTP to work with certificates.
+ networkManager.NetworkConfig = new NetworkConfig();
+ UnityTransport transport = supportingGO.AddComponent();
+ networkManager.NetworkConfig.NetworkTransport = transport;
+ transport.Initialize();
+ transport.SetServerSecrets(serverCertificate: cert, serverPrivateKey: secret);
+
+ // Use encryption, but don't set certificate and check for exception
+ transport.UseEncryption = true;
+ Assert.Throws(() =>
+ {
+ networkManager.StartServer();
+ });
+ // Make sure StartServer failed
+ Assert.False(transport.NetworkDriver.IsCreated);
+ Assert.False(networkManager.IsServer);
+ Assert.False(networkManager.IsListening);
+ }
+ finally
+ {
+ if (supportingGO != null)
+ {
+ Object.DestroyImmediate(supportingGO);
+ }
+ }
+ }
+#endif
}
}
diff --git a/Tests/Editor/UI.meta b/Tests/Editor/UI.meta
deleted file mode 100644
index f22cc75..0000000
--- a/Tests/Editor/UI.meta
+++ /dev/null
@@ -1,3 +0,0 @@
-fileFormatVersion: 2
-guid: f672293e0efc41a6a7e930fd7ff14436
-timeCreated: 1631650280
\ No newline at end of file
diff --git a/Tests/Editor/UI/UITestHelpers.cs b/Tests/Editor/UI/UITestHelpers.cs
deleted file mode 100644
index 5eefc13..0000000
--- a/Tests/Editor/UI/UITestHelpers.cs
+++ /dev/null
@@ -1,15 +0,0 @@
-using Unity.Netcode.Editor;
-using UnityEditor;
-using UnityEngine;
-
-namespace Unity.Netcode.EditorTests
-{
- internal static class UITestHelpers
- {
- [MenuItem("Netcode/UI/Reset Multiplayer Tools Tip Status")]
- private static void ResetMultiplayerToolsTipStatus()
- {
- PlayerPrefs.DeleteKey(NetworkManagerEditor.InstallMultiplayerToolsTipDismissedPlayerPrefKey);
- }
- }
-}
diff --git a/Tests/Editor/UI/UITestHelpers.cs.meta b/Tests/Editor/UI/UITestHelpers.cs.meta
deleted file mode 100644
index c9addc4..0000000
--- a/Tests/Editor/UI/UITestHelpers.cs.meta
+++ /dev/null
@@ -1,3 +0,0 @@
-fileFormatVersion: 2
-guid: bde5fc3349494f77bebd0be12a6957e1
-timeCreated: 1631650292
\ No newline at end of file
diff --git a/Tests/Editor/XXHashTests.cs b/Tests/Editor/XXHashTests.cs
new file mode 100644
index 0000000..342a373
--- /dev/null
+++ b/Tests/Editor/XXHashTests.cs
@@ -0,0 +1,31 @@
+using NUnit.Framework;
+
+namespace Unity.Netcode.EditorTests
+{
+ public class XXHashTests
+ {
+ [Test]
+ public void TestXXHash32Short()
+ {
+ Assert.That("TestStuff".Hash32(), Is.EqualTo(0x64e10c4c));
+ }
+
+ [Test]
+ public void TestXXHash32Long()
+ {
+ Assert.That("TestingHashingWithLongStringValues".Hash32(), Is.EqualTo(0xba3d1783));
+ }
+
+ [Test]
+ public void TestXXHas64Short()
+ {
+ Assert.That("TestStuff".Hash64(), Is.EqualTo(0x4c3be8d82d14a5a9));
+ }
+
+ [Test]
+ public void TestXXHash64Long()
+ {
+ Assert.That("TestingHashingWithLongStringValues".Hash64(), Is.EqualTo(0x5b374f98b10bf246));
+ }
+ }
+}
diff --git a/Tests/Editor/XXHashTests.cs.meta b/Tests/Editor/XXHashTests.cs.meta
new file mode 100644
index 0000000..a7886b3
--- /dev/null
+++ b/Tests/Editor/XXHashTests.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: ce9bdc7200b66410286810307554534b
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Tests/Editor/com.unity.netcode.editortests.asmdef b/Tests/Editor/com.unity.netcode.editortests.asmdef
index 8bfcba5..7d2b613 100644
--- a/Tests/Editor/com.unity.netcode.editortests.asmdef
+++ b/Tests/Editor/com.unity.netcode.editortests.asmdef
@@ -32,6 +32,11 @@
"name": "Unity",
"expression": "(0,2022.2.0a5)",
"define": "UNITY_UNET_PRESENT"
+ },
+ {
+ "name": "com.unity.transport",
+ "expression": "2.0.0-exp",
+ "define": "UTP_TRANSPORT_2_0_ABOVE"
}
]
-}
\ No newline at end of file
+}
diff --git a/Tests/Runtime/AddNetworkPrefabTests.cs b/Tests/Runtime/AddNetworkPrefabTests.cs
index 5ed703b..57f1791 100644
--- a/Tests/Runtime/AddNetworkPrefabTests.cs
+++ b/Tests/Runtime/AddNetworkPrefabTests.cs
@@ -45,14 +45,18 @@ namespace Unity.Netcode.RuntimeTests
private EmptyComponent GetObjectForClient(ulong clientId)
{
- foreach (var component in Object.FindObjectsOfType())
+#if UNITY_2023_1_OR_NEWER
+ var emptyComponents = Object.FindObjectsByType(FindObjectsSortMode.InstanceID);
+#else
+ var emptyComponents = Object.FindObjectsOfType();
+#endif
+ foreach (var component in emptyComponents)
{
if (component.IsSpawned && component.NetworkManager.LocalClientId == clientId)
{
return component;
}
}
-
return null;
}
diff --git a/Tests/Runtime/DeferredMessagingTests.cs b/Tests/Runtime/DeferredMessagingTests.cs
index 6c63358..919195f 100644
--- a/Tests/Runtime/DeferredMessagingTests.cs
+++ b/Tests/Runtime/DeferredMessagingTests.cs
@@ -271,14 +271,19 @@ namespace Unity.Netcode.RuntimeTests
private T GetComponentForClient(ulong clientId) where T : NetworkBehaviour
{
- foreach (var component in Object.FindObjectsOfType())
+#if UNITY_2023_1_OR_NEWER
+ var componentsToFind = Object.FindObjectsByType(FindObjectsSortMode.InstanceID);
+#else
+ var componentsToFind = Object.FindObjectsOfType();
+#endif
+
+ foreach (var component in componentsToFind)
{
if (component.IsSpawned && component.NetworkManager.LocalClientId == clientId)
{
return component;
}
}
-
return null;
}
@@ -639,6 +644,8 @@ namespace Unity.Netcode.RuntimeTests
}
}
+ protected override bool LogAllMessages => true;
+
[UnityTest]
public IEnumerator WhenMultipleSpawnTriggeredMessagesAreDeferred_TheyAreAllProcessedOnSpawn()
{
@@ -658,7 +665,7 @@ namespace Unity.Netcode.RuntimeTests
serverObject.GetComponent().ChangeOwnership(m_ClientNetworkManagers[0].LocalClientId);
- yield return WaitForAllClientsToReceive();
+ yield return WaitForAllClientsToReceive();
foreach (var client in m_ClientNetworkManagers)
{
@@ -666,9 +673,9 @@ namespace Unity.Netcode.RuntimeTests
Assert.IsTrue(manager.DeferMessageCalled);
Assert.IsFalse(manager.ProcessTriggersCalled);
- Assert.AreEqual(3, manager.DeferredMessageCountTotal());
- Assert.AreEqual(3, manager.DeferredMessageCountForType(IDeferredMessageManager.TriggerType.OnSpawn));
- Assert.AreEqual(3, manager.DeferredMessageCountForKey(IDeferredMessageManager.TriggerType.OnSpawn, serverObject.GetComponent().NetworkObjectId));
+ Assert.AreEqual(4, manager.DeferredMessageCountTotal());
+ Assert.AreEqual(4, manager.DeferredMessageCountForType(IDeferredMessageManager.TriggerType.OnSpawn));
+ Assert.AreEqual(4, manager.DeferredMessageCountForKey(IDeferredMessageManager.TriggerType.OnSpawn, serverObject.GetComponent().NetworkObjectId));
Assert.AreEqual(0, manager.DeferredMessageCountForType(IDeferredMessageManager.TriggerType.OnAddPrefab));
AddPrefabsToClient(client);
}
@@ -751,7 +758,13 @@ namespace Unity.Netcode.RuntimeTests
{
var found1 = false;
var found2 = false;
- foreach (var component in Object.FindObjectsOfType())
+#if UNITY_2023_1_OR_NEWER
+ var deferredMessageTestRpcComponents = Object.FindObjectsByType(FindObjectsSortMode.None);
+#else
+ var deferredMessageTestRpcComponents = Object.FindObjectsOfType();
+#endif
+
+ foreach (var component in deferredMessageTestRpcComponents)
{
if (component.IsSpawned && component.NetworkManager.LocalClientId == client.LocalClientId)
{
@@ -794,7 +807,7 @@ namespace Unity.Netcode.RuntimeTests
serverObject.GetComponent().ChangeOwnership(m_ClientNetworkManagers[0].LocalClientId);
- yield return WaitForAllClientsToReceive();
+ yield return WaitForAllClientsToReceive();
// Validate messages are deferred and pending
foreach (var client in m_ClientNetworkManagers)
@@ -802,9 +815,11 @@ namespace Unity.Netcode.RuntimeTests
var manager = (TestDeferredMessageManager)client.DeferredMessageManager;
Assert.IsTrue(manager.DeferMessageCalled);
Assert.IsFalse(manager.ProcessTriggersCalled);
- Assert.AreEqual(4, manager.DeferredMessageCountTotal());
- Assert.AreEqual(3, manager.DeferredMessageCountForType(IDeferredMessageManager.TriggerType.OnSpawn));
- Assert.AreEqual(3, manager.DeferredMessageCountForKey(IDeferredMessageManager.TriggerType.OnSpawn, serverObject.GetComponent().NetworkObjectId));
+
+ Assert.AreEqual(5, manager.DeferredMessageCountTotal());
+
+ Assert.AreEqual(4, manager.DeferredMessageCountForType(IDeferredMessageManager.TriggerType.OnSpawn));
+ Assert.AreEqual(4, manager.DeferredMessageCountForKey(IDeferredMessageManager.TriggerType.OnSpawn, serverObject.GetComponent().NetworkObjectId));
Assert.AreEqual(1, manager.DeferredMessageCountForType(IDeferredMessageManager.TriggerType.OnAddPrefab));
Assert.AreEqual(1, manager.DeferredMessageCountForKey(IDeferredMessageManager.TriggerType.OnAddPrefab, serverObject.GetComponent().GlobalObjectIdHash));
AddPrefabsToClient(client);
diff --git a/Tests/Runtime/IntegrationTestExamples.cs b/Tests/Runtime/IntegrationTestExamples.cs
index d67030b..0556a1c 100644
--- a/Tests/Runtime/IntegrationTestExamples.cs
+++ b/Tests/Runtime/IntegrationTestExamples.cs
@@ -29,10 +29,15 @@ namespace Unity.Netcode.RuntimeTests
{
// Check the condition for this test and automatically handle varying processing
// environments and conditions
+#if UNITY_2023_1_OR_NEWER
+ yield return WaitForConditionOrTimeOut(() =>
+ Object.FindObjectsByType(FindObjectsSortMode.None).Where(
+ (c) => c.IsSpawned).Count() == 2);
+#else
yield return WaitForConditionOrTimeOut(() =>
Object.FindObjectsOfType().Where(
(c) => c.IsSpawned).Count() == 2);
-
+#endif
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Timed out waiting for instances " +
"to be detected!");
}
@@ -64,9 +69,15 @@ namespace Unity.Netcode.RuntimeTests
{
// Check the condition for this test and automatically handle varying processing
// environments and conditions
+#if UNITY_2023_1_OR_NEWER
+ yield return WaitForConditionOrTimeOut(() =>
+ Object.FindObjectsByType(FindObjectsSortMode.None).Where(
+ (c) => c.IsSpawned).Count() == 2);
+#else
yield return WaitForConditionOrTimeOut(() =>
Object.FindObjectsOfType().Where(
(c) => c.IsSpawned).Count() == 2);
+#endif
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Timed out waiting for instances " +
"to be detected!");
diff --git a/Tests/Runtime/Messaging/DisconnectReasonTests.cs b/Tests/Runtime/Messaging/DisconnectReasonTests.cs
new file mode 100644
index 0000000..cb19fd3
--- /dev/null
+++ b/Tests/Runtime/Messaging/DisconnectReasonTests.cs
@@ -0,0 +1,93 @@
+using System;
+using System.Collections;
+using System.Text.RegularExpressions;
+using NUnit.Framework;
+using UnityEngine;
+using UnityEngine.TestTools;
+using Unity.Netcode.TestHelpers.Runtime;
+
+namespace Unity.Netcode.RuntimeTests
+{
+ public class DisconnectReasonObject : NetworkBehaviour
+ {
+
+ }
+
+ public class DisconnectReasonTests : NetcodeIntegrationTest
+ {
+ protected override int NumberOfClients => 2;
+
+ private GameObject m_PrefabToSpawn;
+
+ protected override void OnServerAndClientsCreated()
+ {
+ m_PrefabToSpawn = CreateNetworkObjectPrefab("DisconnectReasonObject");
+ m_PrefabToSpawn.AddComponent();
+ }
+
+ private int m_DisconnectCount;
+ private bool m_ThrowOnDisconnect = false;
+
+ public void OnClientDisconnectCallback(ulong clientId)
+ {
+ m_DisconnectCount++;
+ if (m_ThrowOnDisconnect)
+ {
+ throw new SystemException("whatever");
+ }
+ }
+
+ [UnityTest]
+ public IEnumerator DisconnectReasonTest()
+ {
+ float startTime = Time.realtimeSinceStartup;
+ m_ThrowOnDisconnect = false;
+ m_DisconnectCount = 0;
+
+ // Add a callback for both clients, when they get disconnected
+ m_ClientNetworkManagers[0].OnClientDisconnectCallback += OnClientDisconnectCallback;
+ m_ClientNetworkManagers[1].OnClientDisconnectCallback += OnClientDisconnectCallback;
+
+ // Disconnect both clients, from the server
+ m_ServerNetworkManager.DisconnectClient(m_ClientNetworkManagers[0].LocalClientId, "Bogus reason 1");
+ m_ServerNetworkManager.DisconnectClient(m_ClientNetworkManagers[1].LocalClientId, "Bogus reason 2");
+
+ while (m_DisconnectCount < 2 && Time.realtimeSinceStartup < startTime + 10.0f)
+ {
+ yield return null;
+ }
+
+ Assert.AreEqual(m_ClientNetworkManagers[0].DisconnectReason, "Bogus reason 1");
+ Assert.AreEqual(m_ClientNetworkManagers[1].DisconnectReason, "Bogus reason 2");
+
+ Debug.Assert(m_DisconnectCount == 2);
+ }
+
+ [UnityTest]
+ public IEnumerator DisconnectExceptionTest()
+ {
+ m_ThrowOnDisconnect = true;
+ m_DisconnectCount = 0;
+ float startTime = Time.realtimeSinceStartup;
+
+ // Add a callback for first client, when they get disconnected
+ m_ClientNetworkManagers[0].OnClientDisconnectCallback += OnClientDisconnectCallback;
+ m_ClientNetworkManagers[1].OnClientDisconnectCallback += OnClientDisconnectCallback;
+
+ // Disconnect first client, from the server
+ LogAssert.Expect(LogType.Exception, new Regex(".*whatever.*"));
+ m_ServerNetworkManager.DisconnectClient(m_ClientNetworkManagers[0].LocalClientId);
+
+ // Disconnect second client, from the server
+ LogAssert.Expect(LogType.Exception, new Regex(".*whatever.*"));
+ m_ServerNetworkManager.DisconnectClient(m_ClientNetworkManagers[1].LocalClientId);
+
+ while (m_DisconnectCount < 2 && Time.realtimeSinceStartup < startTime + 10.0f)
+ {
+ yield return null;
+ }
+
+ Debug.Assert(m_DisconnectCount == 2);
+ }
+ }
+}
diff --git a/Tests/Runtime/Messaging/DisconnectReasonTests.cs.meta b/Tests/Runtime/Messaging/DisconnectReasonTests.cs.meta
new file mode 100644
index 0000000..26b12ef
--- /dev/null
+++ b/Tests/Runtime/Messaging/DisconnectReasonTests.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 93141fa15824f406b89dbc6f32c8910d
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Tests/Runtime/Messaging/NamedMessageTests.cs b/Tests/Runtime/Messaging/NamedMessageTests.cs
index f2cde70..b96c63d 100644
--- a/Tests/Runtime/Messaging/NamedMessageTests.cs
+++ b/Tests/Runtime/Messaging/NamedMessageTests.cs
@@ -3,7 +3,6 @@ using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using Unity.Collections;
-using UnityEngine;
using UnityEngine.TestTools;
using Unity.Netcode.TestHelpers.Runtime;
@@ -27,16 +26,6 @@ namespace Unity.Netcode.RuntimeTests
public IEnumerator NamedMessageIsReceivedOnClientWithContent()
{
var messageName = Guid.NewGuid().ToString();
- var messageContent = new ForceNetworkSerializeByMemcpy(Guid.NewGuid());
- var writer = new FastBufferWriter(1300, Allocator.Temp);
- using (writer)
- {
- writer.WriteValueSafe(messageContent);
- m_ServerNetworkManager.CustomMessagingManager.SendNamedMessage(
- messageName,
- FirstClient.LocalClientId,
- writer);
- }
ulong receivedMessageSender = 0;
var receivedMessageContent = new ForceNetworkSerializeByMemcpy(new Guid());
@@ -49,7 +38,49 @@ namespace Unity.Netcode.RuntimeTests
reader.ReadValueSafe(out receivedMessageContent);
});
- yield return new WaitForSeconds(0.2f);
+ var messageContent = new ForceNetworkSerializeByMemcpy(Guid.NewGuid());
+ var writer = new FastBufferWriter(1300, Allocator.Temp);
+ using (writer)
+ {
+ writer.WriteValueSafe(messageContent);
+ m_ServerNetworkManager.CustomMessagingManager.SendNamedMessage(
+ messageName,
+ FirstClient.LocalClientId,
+ writer);
+ }
+
+ yield return WaitForMessageReceived(new List { FirstClient });
+
+ Assert.AreEqual(messageContent.Value, receivedMessageContent.Value);
+ Assert.AreEqual(m_ServerNetworkManager.LocalClientId, receivedMessageSender);
+ }
+
+ [Test]
+ public void NamedMessageIsReceivedOnHostWithContent()
+ {
+ var messageName = Guid.NewGuid().ToString();
+
+ ulong receivedMessageSender = 0;
+ var receivedMessageContent = new ForceNetworkSerializeByMemcpy(new Guid());
+ m_ServerNetworkManager.CustomMessagingManager.RegisterNamedMessageHandler(
+ messageName,
+ (ulong sender, FastBufferReader reader) =>
+ {
+ receivedMessageSender = sender;
+
+ reader.ReadValueSafe(out receivedMessageContent);
+ });
+
+ var messageContent = new ForceNetworkSerializeByMemcpy(Guid.NewGuid());
+ var writer = new FastBufferWriter(1300, Allocator.Temp);
+ using (writer)
+ {
+ writer.WriteValueSafe(messageContent);
+ m_ServerNetworkManager.CustomMessagingManager.SendNamedMessage(
+ messageName,
+ m_ServerNetworkManager.LocalClientId,
+ writer);
+ }
Assert.AreEqual(messageContent.Value, receivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, receivedMessageSender);
@@ -59,16 +90,6 @@ namespace Unity.Netcode.RuntimeTests
public IEnumerator NamedMessageIsReceivedOnMultipleClientsWithContent()
{
var messageName = Guid.NewGuid().ToString();
- var messageContent = new ForceNetworkSerializeByMemcpy(Guid.NewGuid());
- var writer = new FastBufferWriter(1300, Allocator.Temp);
- using (writer)
- {
- writer.WriteValueSafe(messageContent);
- m_ServerNetworkManager.CustomMessagingManager.SendNamedMessage(
- messageName,
- new List { FirstClient.LocalClientId, SecondClient.LocalClientId },
- writer);
- }
ulong firstReceivedMessageSender = 0;
var firstReceivedMessageContent = new ForceNetworkSerializeByMemcpy(new Guid());
@@ -92,19 +113,78 @@ namespace Unity.Netcode.RuntimeTests
reader.ReadValueSafe(out secondReceivedMessageContent);
});
- yield return new WaitForSeconds(0.2f);
+ ulong thirdReceivedMessageSender = 0;
+ var thirdReceivedMessageContent = new ForceNetworkSerializeByMemcpy(new Guid());
+ m_ServerNetworkManager.CustomMessagingManager.RegisterNamedMessageHandler(
+ messageName,
+ (ulong sender, FastBufferReader reader) =>
+ {
+ thirdReceivedMessageSender = sender;
+
+ reader.ReadValueSafe(out thirdReceivedMessageContent);
+ });
+
+ var messageContent = new ForceNetworkSerializeByMemcpy(Guid.NewGuid());
+ var writer = new FastBufferWriter(1300, Allocator.Temp);
+ using (writer)
+ {
+ writer.WriteValueSafe(messageContent);
+ m_ServerNetworkManager.CustomMessagingManager.SendNamedMessage(
+ messageName,
+ new List { m_ServerNetworkManager.LocalClientId, FirstClient.LocalClientId, SecondClient.LocalClientId },
+ writer);
+ }
+
+ yield return WaitForMessageReceived(new List { FirstClient, SecondClient });
Assert.AreEqual(messageContent.Value, firstReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, firstReceivedMessageSender);
Assert.AreEqual(messageContent.Value, secondReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, secondReceivedMessageSender);
+
+ Assert.AreEqual(messageContent.Value, thirdReceivedMessageContent.Value);
+ Assert.AreEqual(m_ServerNetworkManager.LocalClientId, thirdReceivedMessageSender);
}
[UnityTest]
public IEnumerator WhenSendingNamedMessageToAll_AllClientsReceiveIt()
{
var messageName = Guid.NewGuid().ToString();
+
+ ulong firstReceivedMessageSender = 0;
+ var firstReceivedMessageContent = new ForceNetworkSerializeByMemcpy(new Guid());
+ FirstClient.CustomMessagingManager.RegisterNamedMessageHandler(
+ messageName,
+ (ulong sender, FastBufferReader reader) =>
+ {
+ firstReceivedMessageSender = sender;
+
+ reader.ReadValueSafe(out firstReceivedMessageContent);
+ });
+
+ ulong secondReceivedMessageSender = 0;
+ var secondReceivedMessageContent = new ForceNetworkSerializeByMemcpy(new Guid());
+ SecondClient.CustomMessagingManager.RegisterNamedMessageHandler(
+ messageName,
+ (ulong sender, FastBufferReader reader) =>
+ {
+ secondReceivedMessageSender = sender;
+
+ reader.ReadValueSafe(out secondReceivedMessageContent);
+ });
+
+ ulong thirdReceivedMessageSender = 0;
+ var thirdReceivedMessageContent = new ForceNetworkSerializeByMemcpy(new Guid());
+ m_ServerNetworkManager.CustomMessagingManager.RegisterNamedMessageHandler(
+ messageName,
+ (ulong sender, FastBufferReader reader) =>
+ {
+ thirdReceivedMessageSender = sender;
+
+ reader.ReadValueSafe(out thirdReceivedMessageContent);
+ });
+
var messageContent = new ForceNetworkSerializeByMemcpy(Guid.NewGuid());
var writer = new FastBufferWriter(1300, Allocator.Temp);
using (writer)
@@ -113,35 +193,16 @@ namespace Unity.Netcode.RuntimeTests
m_ServerNetworkManager.CustomMessagingManager.SendNamedMessageToAll(messageName, writer);
}
- ulong firstReceivedMessageSender = 0;
- var firstReceivedMessageContent = new ForceNetworkSerializeByMemcpy(new Guid());
- FirstClient.CustomMessagingManager.RegisterNamedMessageHandler(
- messageName,
- (ulong sender, FastBufferReader reader) =>
- {
- firstReceivedMessageSender = sender;
-
- reader.ReadValueSafe(out firstReceivedMessageContent);
- });
-
- ulong secondReceivedMessageSender = 0;
- var secondReceivedMessageContent = new ForceNetworkSerializeByMemcpy(new Guid());
- SecondClient.CustomMessagingManager.RegisterNamedMessageHandler(
- messageName,
- (ulong sender, FastBufferReader reader) =>
- {
- secondReceivedMessageSender = sender;
-
- reader.ReadValueSafe(out secondReceivedMessageContent);
- });
-
- yield return new WaitForSeconds(0.2f);
+ yield return WaitForMessageReceived(new List { FirstClient, SecondClient });
Assert.AreEqual(messageContent.Value, firstReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, firstReceivedMessageSender);
Assert.AreEqual(messageContent.Value, secondReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, secondReceivedMessageSender);
+
+ Assert.AreEqual(messageContent.Value, thirdReceivedMessageContent.Value);
+ Assert.AreEqual(m_ServerNetworkManager.LocalClientId, thirdReceivedMessageSender);
}
[Test]
diff --git a/Tests/Runtime/Messaging/UnnamedMessageTests.cs b/Tests/Runtime/Messaging/UnnamedMessageTests.cs
index 711e7cf..5c88696 100644
--- a/Tests/Runtime/Messaging/UnnamedMessageTests.cs
+++ b/Tests/Runtime/Messaging/UnnamedMessageTests.cs
@@ -3,7 +3,6 @@ using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using Unity.Collections;
-using UnityEngine;
using UnityEngine.TestTools;
using Unity.Netcode.TestHelpers.Runtime;
@@ -19,16 +18,6 @@ namespace Unity.Netcode.RuntimeTests
[UnityTest]
public IEnumerator UnnamedMessageIsReceivedOnClientWithContent()
{
- var messageContent = new ForceNetworkSerializeByMemcpy(Guid.NewGuid());
- var writer = new FastBufferWriter(1300, Allocator.Temp);
- using (writer)
- {
- writer.WriteValueSafe(messageContent);
- m_ServerNetworkManager.CustomMessagingManager.SendUnnamedMessage(
- FirstClient.LocalClientId,
- writer);
- }
-
ulong receivedMessageSender = 0;
var receivedMessageContent = new ForceNetworkSerializeByMemcpy(new Guid());
FirstClient.CustomMessagingManager.OnUnnamedMessage +=
@@ -39,7 +28,44 @@ namespace Unity.Netcode.RuntimeTests
reader.ReadValueSafe(out receivedMessageContent);
};
- yield return new WaitForSeconds(0.2f);
+ var messageContent = new ForceNetworkSerializeByMemcpy(Guid.NewGuid());
+ var writer = new FastBufferWriter(1300, Allocator.Temp);
+ using (writer)
+ {
+ writer.WriteValueSafe(messageContent);
+ m_ServerNetworkManager.CustomMessagingManager.SendUnnamedMessage(
+ FirstClient.LocalClientId,
+ writer);
+ }
+
+ yield return WaitForMessageReceived(new List { FirstClient });
+
+ Assert.AreEqual(messageContent.Value, receivedMessageContent.Value);
+ Assert.AreEqual(m_ServerNetworkManager.LocalClientId, receivedMessageSender);
+ }
+
+ [Test]
+ public void UnnamedMessageIsReceivedOnHostWithContent()
+ {
+ ulong receivedMessageSender = 0;
+ var receivedMessageContent = new ForceNetworkSerializeByMemcpy(new Guid());
+ m_ServerNetworkManager.CustomMessagingManager.OnUnnamedMessage +=
+ (ulong sender, FastBufferReader reader) =>
+ {
+ receivedMessageSender = sender;
+
+ reader.ReadValueSafe(out receivedMessageContent);
+ };
+
+ var messageContent = new ForceNetworkSerializeByMemcpy(Guid.NewGuid());
+ var writer = new FastBufferWriter(1300, Allocator.Temp);
+ using (writer)
+ {
+ writer.WriteValueSafe(messageContent);
+ m_ServerNetworkManager.CustomMessagingManager.SendUnnamedMessage(
+ m_ServerNetworkManager.LocalClientId,
+ writer);
+ }
Assert.AreEqual(messageContent.Value, receivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, receivedMessageSender);
@@ -48,16 +74,6 @@ namespace Unity.Netcode.RuntimeTests
[UnityTest]
public IEnumerator UnnamedMessageIsReceivedOnMultipleClientsWithContent()
{
- var messageContent = new ForceNetworkSerializeByMemcpy(Guid.NewGuid());
- var writer = new FastBufferWriter(1300, Allocator.Temp);
- using (writer)
- {
- writer.WriteValueSafe(messageContent);
- m_ServerNetworkManager.CustomMessagingManager.SendUnnamedMessage(
- new List { FirstClient.LocalClientId, SecondClient.LocalClientId },
- writer);
- }
-
ulong firstReceivedMessageSender = 0;
var firstReceivedMessageContent = new ForceNetworkSerializeByMemcpy(new Guid());
FirstClient.CustomMessagingManager.OnUnnamedMessage +=
@@ -78,18 +94,71 @@ namespace Unity.Netcode.RuntimeTests
reader.ReadValueSafe(out secondReceivedMessageContent);
};
- yield return new WaitForSeconds(0.2f);
+ ulong thirdReceivedMessageSender = 0;
+ var thirdReceivedMessageContent = new ForceNetworkSerializeByMemcpy(new Guid());
+ m_ServerNetworkManager.CustomMessagingManager.OnUnnamedMessage +=
+ (ulong sender, FastBufferReader reader) =>
+ {
+ thirdReceivedMessageSender = sender;
+
+ reader.ReadValueSafe(out thirdReceivedMessageContent);
+ };
+
+ var messageContent = new ForceNetworkSerializeByMemcpy(Guid.NewGuid());
+ var writer = new FastBufferWriter(1300, Allocator.Temp);
+ using (writer)
+ {
+ writer.WriteValueSafe(messageContent);
+ m_ServerNetworkManager.CustomMessagingManager.SendUnnamedMessage(
+ new List { m_ServerNetworkManager.LocalClientId, FirstClient.LocalClientId, SecondClient.LocalClientId },
+ writer);
+ }
+
+ yield return WaitForMessageReceived(new List { FirstClient, SecondClient });
Assert.AreEqual(messageContent.Value, firstReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, firstReceivedMessageSender);
Assert.AreEqual(messageContent.Value, secondReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, secondReceivedMessageSender);
+
+ Assert.AreEqual(messageContent.Value, thirdReceivedMessageContent.Value);
+ Assert.AreEqual(m_ServerNetworkManager.LocalClientId, thirdReceivedMessageSender);
}
[UnityTest]
public IEnumerator WhenSendingUnnamedMessageToAll_AllClientsReceiveIt()
{
+ ulong firstReceivedMessageSender = 0;
+ var firstReceivedMessageContent = new ForceNetworkSerializeByMemcpy(new Guid());
+ FirstClient.CustomMessagingManager.OnUnnamedMessage +=
+ (ulong sender, FastBufferReader reader) =>
+ {
+ firstReceivedMessageSender = sender;
+
+ reader.ReadValueSafe(out firstReceivedMessageContent);
+ };
+
+ ulong secondReceivedMessageSender = 0;
+ var secondReceivedMessageContent = new ForceNetworkSerializeByMemcpy(new Guid());
+ SecondClient.CustomMessagingManager.OnUnnamedMessage +=
+ (ulong sender, FastBufferReader reader) =>
+ {
+ secondReceivedMessageSender = sender;
+
+ reader.ReadValueSafe(out secondReceivedMessageContent);
+ };
+
+ ulong thirdReceivedMessageSender = 0;
+ var thirdReceivedMessageContent = new ForceNetworkSerializeByMemcpy