Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,6 @@ protected virtual bool OnIsServerAuthoritative()
private int[] m_TransitionHash;
private int[] m_AnimationHash;
private float[] m_LayerWeights;
private static byte[] s_EmptyArray = new byte[] { };
private List<int> m_ParametersToUpdate;
private RpcParams m_RpcParams;
private IGroupRpcTarget m_TargetGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2221,13 +2221,11 @@ private bool CheckForStateChange(ref NetworkTransformState networkState, bool is
// values are applied.
var hasParentNetworkObject = false;

var parentNetworkObject = (NetworkObject)null;

// If the NetworkObject belonging to this NetworkTransform instance has a parent
// (i.e. this handles nested NetworkTransforms under a parent at some layer above)
if (NetworkObject.transform.parent != null)
{
parentNetworkObject = NetworkObject.transform.parent.GetComponent<NetworkObject>();
var parentNetworkObject = NetworkObject.transform.parent.GetComponent<NetworkObject>();

// In-scene placed NetworkObjects parented under a GameObject with no
// NetworkObject preserve their lossyScale when synchronizing.
Expand Down Expand Up @@ -4800,6 +4798,21 @@ public NetworkTransformTickRegistration(NetworkManager networkManager)
}
}
private static int s_TickSynchPosition;

#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad()
{
CurrentTick = 0;
TrackStateUpdateId = false;
AssignDefaultInterpolationType = false;
DefaultInterpolationType = default;
s_NetworkTickRegistration = new Dictionary<NetworkManager, NetworkTransformTickRegistration>();
InterpolationBufferTickOffset = 0;
s_TickSynchPosition = 0;
}
#endif

private int m_NextTickSync;

internal void RegisterForTickSynchronization()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public static class QuaternionCompressor

// Used to store the absolute value of the 4 quaternion elements
private static Quaternion s_QuatAbsValues = Quaternion.identity;
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad() => s_QuatAbsValues = Quaternion.identity;
#endif

/// <summary>
/// Compresses a Quaternion into an unsigned integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public struct ContactEventHandlerInfo
/// </summary>
public bool ProvideNonRigidBodyContactEvents;
/// <summary>
/// When set to true, the <see cref="RigidbodyContactEventManager"/> will prioritize invoking <see cref="IContactEventHandler.ContactEvent(ulong, Vector3, Rigidbody, Vector3, bool, Vector3)"/> <br /></br>
/// When set to true, the <see cref="RigidbodyContactEventManager"/> will prioritize invoking <see cref="IContactEventHandler.ContactEvent(ulong, Vector3, Rigidbody, Vector3, bool, Vector3)"/> <br />
/// if it is the 2nd colliding body in the contact pair being processed. With distributed authority, setting this value to true when a <see cref="NetworkObject"/> is owned by the local client <br />
/// will assure <see cref="IContactEventHandler.ContactEvent(ulong, Vector3, Rigidbody, Vector3, bool, Vector3)"/> is only invoked on the authoritative side.
/// </summary>
Expand Down Expand Up @@ -76,6 +76,10 @@ public interface IContactEventHandlerWithInfo : IContactEventHandler
public class RigidbodyContactEventManager : MonoBehaviour
{
public static RigidbodyContactEventManager Instance { get; private set; }
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad() => Instance = null;
#endif

private struct JobResultStruct
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,32 @@ private set
}
private static CommandLineOptions s_Instance;

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void RuntimeInitializeOnLoad() => Instance = new CommandLineOptions();

// Contains the current application instance domain's command line arguments
internal static List<string> CommandLineArguments = new List<string>();
private static List<string> s_CommandLineArguments = new List<string>(Environment.GetCommandLineArgs());

// Invoked upon application start, after scene load
[RuntimeInitializeOnLoadMethod]
private static void ParseCommandLineArguments()
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad()
{
Instance = new CommandLineOptions();
s_Instance = new CommandLineOptions();
// Get all the command line arguments to be parsed later and/or modified
// prior to being parsed (for testing purposes).
CommandLineArguments = new List<string>(Environment.GetCommandLineArgs());
s_CommandLineArguments = new List<string>(Environment.GetCommandLineArgs());
}
#endif

/// <summary>
/// Returns the value of an argument or null if there the argument is not present
/// Returns the value of an argument or null if the argument is not present
/// </summary>
/// <param name="arg">The name of the argument</param>
/// <returns><see cref="string"/>Value of the command line argument passed in.</returns>
public string GetArg(string arg)
{
var argIndex = CommandLineArguments.IndexOf(arg);
if (argIndex >= 0 && argIndex < CommandLineArguments.Count - 1)
var argIndex = s_CommandLineArguments.IndexOf(arg);
if (argIndex >= 0 && argIndex < s_CommandLineArguments.Count - 1)
{
return CommandLineArguments[argIndex + 1];
return s_CommandLineArguments[argIndex + 1];
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEngine;
#endif

namespace Unity.Netcode
{
Expand All @@ -14,6 +17,10 @@ internal static class ComponentFactory
internal delegate object CreateObjectDelegate(NetworkManager networkManager);

private static Dictionary<Type, CreateObjectDelegate> s_Delegates = new Dictionary<Type, CreateObjectDelegate>();
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad() => s_Delegates = new Dictionary<Type, CreateObjectDelegate>();
#endif

/// <summary>
/// Instantiates an instance of a given interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1333,8 +1333,6 @@ internal void NetworkVariableUpdate(ulong targetClientId, bool forceSend = false
}
}

internal static bool LogSentVariableUpdateMessage;

private bool CouldHaveDirtyNetworkVariables()
{
// TODO: There should be a better way by reading one dirty variable vs. 'n'
Expand Down
13 changes: 13 additions & 0 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,19 @@ internal abstract class NetcodeAnalytics
internal delegate void ResetNetworkManagerDelegate(NetworkManager manager);

internal static ResetNetworkManagerDelegate OnNetworkManagerReset;
//We already are in an #if UNITY_ENGINE def
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad()
{
Singleton = null;
OnInstantiated = null;
OnDestroying = null;
OnSingletonReady = null;
OnNetworkManagerReset = null;
IsDistributedAuthority = false;
s_SerializedType = new List<Type>();
DisableNotOptimizedSerializedType = false;
}

private void Reset()
{
Expand Down
9 changes: 9 additions & 0 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,11 @@ internal void SetIsDestroying()
IsDestroying = true;
}

private void OnDisable()
{
SceneManager.activeSceneChanged -= CurrentlyActiveSceneChanged;
}

private void OnDestroy()
{
// Apply the is destroying flag
Expand Down Expand Up @@ -2506,6 +2511,10 @@ private void OnTransformParentChanged()
// If you couldn't find your parent, we put you into OrphanChildren set and every time we spawn another NetworkObject locally due to replication,
// we call CheckOrphanChildren() method and quickly iterate over OrphanChildren set and see if we can reparent/adopt one.
internal static HashSet<NetworkObject> OrphanChildren = new HashSet<NetworkObject>();
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad() => OrphanChildren = new HashSet<NetworkObject>();
#endif

internal bool ApplyNetworkParenting(bool removeParent = false, bool ignoreNotSpawned = false, bool orphanedChildPass = false, bool enableNotification = true)
{
Expand Down
10 changes: 10 additions & 0 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkUpdateLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,16 @@ public static PlayerLoopSystem CreateLoopSystem()
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void Initialize()
{
#if UNITY_EDITOR
s_UpdateSystem_Sets = new Dictionary<NetworkUpdateStage, HashSet<INetworkUpdateSystem>>();
s_UpdateSystem_Arrays = new Dictionary<NetworkUpdateStage, INetworkUpdateSystem[]>();
foreach (NetworkUpdateStage updateStage in Enum.GetValues(typeof(NetworkUpdateStage)))
{
s_UpdateSystem_Sets.Add(updateStage, new HashSet<INetworkUpdateSystem>());
s_UpdateSystem_Arrays.Add(updateStage, new INetworkUpdateSystem[k_UpdateSystem_InitialArrayCapacity]);
}
UpdateStage = default;
#endif
UnregisterLoopSystems();
RegisterLoopSystems();
}
Expand Down
4 changes: 4 additions & 0 deletions com.unity.netcode.gameobjects/Runtime/Logging/NetworkLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public static class NetworkLog
public static void LogErrorServer(string message) => LogServer(message, LogType.Error);

internal static NetworkManager NetworkManagerOverride;
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad() => NetworkManagerOverride = null;
#endif

private static void LogServer(string message, LogType logType)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections.Generic;
using Unity.Collections;
#if UNITY_EDITOR
using UnityEngine;
#endif

namespace Unity.Netcode
{
Expand Down Expand Up @@ -96,6 +99,10 @@ public virtual unsafe void CleanupStaleTriggers()
/// Used for testing purposes
/// </summary>
internal static bool IncludeMessageType = true;
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad() => IncludeMessageType = true;
#endif

private string GetWarningMessage(IDeferredNetworkMessageManager.TriggerType triggerType, ulong key, TriggerInfo triggerInfo, float spawnTimeout)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
#endif

Expand Down Expand Up @@ -53,6 +54,10 @@ internal struct ILPPMessageProvider : INetworkMessageProvider

// Enable this for integration tests that need no message types defined
internal static bool IntegrationTestNoMessages;
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad() => IntegrationTestNoMessages = false;
#endif

/// <summary>
/// Returns a table of message type to NetworkMessageTypes enum value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

namespace Unity.Netcode
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEditor;
using UnityEngine;

internal static class MessageDelivery
Expand All @@ -15,17 +14,21 @@ internal static class MessageDelivery
/// when sending the message via public API.
/// - Skip the time sync messages since it has always used unreliable network delivery.
/// </summary>
private static HashSet<NetworkMessageTypes> s_SkipMessageTypes = new HashSet<NetworkMessageTypes>(){
private static readonly HashSet<NetworkMessageTypes> k_SkipMessageTypes = new HashSet<NetworkMessageTypes>(){
NetworkMessageTypes.NamedMessage, NetworkMessageTypes.Unnamed};

[RuntimeInitializeOnLoadMethod]
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void OnApplicationStart()
{
#if UNITY_EDITOR
s_MessageToDelivery = new Dictionary<NetworkMessageTypes, NetworkDelivery>();
s_MessageToMessageType = new Dictionary<Type, NetworkMessageTypes>();
#endif
UpdateMessageTypes();
}

/// <summary>
/// FIrst pass at providing an easier path to configuring the network
/// First pass at providing an easier path to configuring the network
/// delivery type for the message type.
/// TODO: Once <see cref="NetworkMessageManager"/> coalesces all reliable messages
/// and/or organizes by a more unified order of operation tracking built into the
Expand All @@ -40,7 +43,7 @@ private static void UpdateMessageTypes()
foreach (var messageTypeObject in networkMessageTypes)
{
var messageType = (NetworkMessageTypes)messageTypeObject;
if (s_SkipMessageTypes.Contains(messageType))
if (k_SkipMessageTypes.Contains(messageType))
{
continue;
}
Expand Down Expand Up @@ -71,18 +74,10 @@ private static void UpdateMessageTypes()
MessageDeliveryType<TimeSyncMessage>.Initialize();
}

#if UNITY_EDITOR
[InitializeOnLoadMethod]
[InitializeOnEnterPlayMode]
private static void OnEnterPlayMode()
{
UpdateMessageTypes();
}
#endif
internal static NetworkDelivery GetDelivery(Type type)
{
// Return the default if not registered or null
if (type == null || s_SkipMessageTypes.Contains(s_MessageToMessageType[type]))
if (type == null || k_SkipMessageTypes.Contains(s_MessageToMessageType[type]))
{
return NetworkDelivery.ReliableFragmentedSequenced;
}
Expand All @@ -91,7 +86,7 @@ internal static NetworkDelivery GetDelivery(Type type)

internal static NetworkDelivery GetDelivery(NetworkMessageTypes messageType)
{
if (s_SkipMessageTypes.Contains(messageType))
if (k_SkipMessageTypes.Contains(messageType))
{
throw new Exception($"{messageType} is not registered in the message type to network delivery map!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public InvalidMessageStructureException(string issue) : base(issue)
internal class NetworkMessageManager : IDisposable
{
public bool StopProcessing = false;
private static Type s_ConnectionApprovedType = typeof(ConnectionApprovedMessage);
private static Type s_ConnectionRequestType = typeof(ConnectionRequestMessage);
private static Type s_DisconnectReasonType = typeof(DisconnectReasonMessage);
private static readonly Type k_ConnectionApprovedType = typeof(ConnectionApprovedMessage);
private static readonly Type k_ConnectionRequestType = typeof(ConnectionRequestMessage);
private static readonly Type k_DisconnectReasonType = typeof(DisconnectReasonMessage);

private struct ReceiveQueueItem
{
Expand Down Expand Up @@ -149,8 +149,6 @@ public NetworkMessageManager(INetworkMessageSender sender, object owner, INetwor
}
}

internal static bool EnableMessageOrderConsoleLog = false;

public void Dispose()
{
if (m_Disposed)
Expand Down Expand Up @@ -549,7 +547,7 @@ internal int GetMessageVersion(Type type, ulong clientId, bool forReceive = fals
// 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 (messageType != s_ConnectionRequestType && messageType != s_ConnectionApprovedType && messageType != s_DisconnectReasonType && context.SenderId != manager.m_LocalClientId)
if (messageType != k_ConnectionRequestType && messageType != k_ConnectionApprovedType && messageType != k_DisconnectReasonType && context.SenderId != manager.m_LocalClientId)
{
messageVersion = manager.GetMessageVersion(messageType, context.SenderId, true);
if (messageVersion < 0)
Expand Down Expand Up @@ -603,7 +601,7 @@ internal int SendMessage<TMessageType, TClientIdListType>(ref TMessageType messa
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) != s_ConnectionRequestType)
if (typeof(TMessageType) != k_ConnectionRequestType)
{
messageVersion = GetMessageVersion(typeof(TMessageType), clientIds[i]);
if (messageVersion < 0)
Expand Down Expand Up @@ -657,7 +655,7 @@ internal unsafe int SendPreSerializedMessage<TMessageType>(in FastBufferWriter t

// 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) != s_ConnectionRequestType)
if (typeof(TMessageType) != k_ConnectionRequestType)
{
var messageVersion = GetMessageVersion(typeof(TMessageType), clientIds[i]);
if (messageVersion < 0)
Expand Down Expand Up @@ -741,7 +739,7 @@ internal unsafe int SendPreSerializedMessage<TMessageType>(in FastBufferWriter t
// 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) != s_ConnectionRequestType)
if (typeof(TMessageType) != k_ConnectionRequestType)
{
messageVersion = GetMessageVersion(typeof(TMessageType), clientId);
if (messageVersion < 0)
Expand Down
Loading