Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Additional documentation and release notes are available at [Multiplayer Documen

### Fixed

- Fixed issue where attempts to use `NetworkLog` when there is no `NetworkManager` instance would result in an exception. (#3917)

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,14 @@ private void OnTransformParentChanged()
NetworkManagerCheckForParent();
}

/// <summary>
/// For testing purposes when you need the singleton to be null
/// </summary>
internal static void ResetSingleton()
{
Singleton = null;
}

/// <summary>
/// Set this NetworkManager instance as the static NetworkManager singleton
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3647,7 +3647,8 @@ internal void OnNetworkBehaviourDestroyed(NetworkBehaviour networkBehaviour)
{
if (networkBehaviour.IsSpawned && IsSpawned)
{
if (NetworkManagerOwner.LogLevel <= LogLevel.Developer)
// Only log this warning if we are not shutting down.
if (!NetworkManagerOwner.ShutdownInProgress && NetworkManagerOwner.LogLevel <= LogLevel.Developer)
{
NetworkLog.LogWarning($"{nameof(NetworkBehaviour)}-{networkBehaviour.name} is being destroyed while {nameof(NetworkObject)}-{name} is still spawned! (could break state synchronization)");
}
Expand Down
13 changes: 10 additions & 3 deletions com.unity.netcode.gameobjects/Runtime/Logging/NetworkLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,21 @@ private static void LogServer(string message, LogType logType)
}
}

private const string k_HeaderStart = "Netcode";
private static string Header()
{
var networkManager = NetworkManagerOverride ??= NetworkManager.Singleton;
if (networkManager.DistributedAuthorityMode)
if (networkManager != null)
{
return "Session-Owner";
if (networkManager.DistributedAuthorityMode)
{
return $"{k_HeaderStart}-Session-Owner";
}
return $"{k_HeaderStart}-Server";
}
return "Netcode-Server";

// If NetworkManager no longer exists, then return the generic header
return k_HeaderStart;
}

internal static void LogInfoServerLocal(string message, ulong sender) => Debug.Log($"[{Header()} Sender={sender}] {message}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ public IEnumerator ChangeOwnershipWithoutObservers()
authorityInstance.ChangeOwnership(otherClient.LocalClientId);
var senderId = authority.LocalClientId;
var receiverId = otherClient.LocalClientId;
LogAssert.Expect(LogType.Warning, $"[Session-Owner Sender={senderId}] [Invalid Owner] Cannot send Ownership change as client-{receiverId} cannot see {authorityInstance.name}! Use NetworkShow first.");
LogAssert.Expect(LogType.Warning, $"[Netcode-Session-Owner Sender={senderId}] [Invalid Owner] Cannot send Ownership change as client-{receiverId} cannot see {authorityInstance.name}! Use NetworkShow first.");
Assert.True(authorityInstance.IsOwner, $"[Ownership Check] Client-{senderId} should still own this object!");

// Now re-add the client to the Observers list and try to change ownership
Expand Down
47 changes: 47 additions & 0 deletions com.unity.netcode.gameobjects/Tests/Runtime/NetworkLogTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine.TestTools;

namespace Unity.Netcode.RuntimeTests
{
/// <summary>
/// Validates edge cases with <see cref="NetworkLog"/>
/// </summary>
internal class NetworkLogTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 0;
private bool m_ServerStopped;

/// <summary>
/// Validates that if no <see cref="NetworkManager"/> exists,
/// you can still use NetworkLog with the caveat when one does
/// not exist it will only log locally.
/// (is topology agnostic)
/// </summary>
[UnityTest]
public IEnumerator UseNetworkLogWithNoNetworkManager()
{
m_ServerStopped = false;
var authority = GetAuthorityNetworkManager();
authority.OnServerStopped += OnServerStopped;
authority.Shutdown();
yield return WaitForConditionOrTimeOut(() => m_ServerStopped);
AssertOnTimeout($"Timed out waiting for {nameof(NetworkManager)} to stop!");
// Assure it is destroyed.
UnityEngine.Object.Destroy(authority);
authority = null;

// Clear out the singleton to assure NetworkLog has no references to a NetworkManager
NetworkManager.ResetSingleton();

// Validate you can use NetworkLog without any NetworkManager instance.
NetworkLog.LogInfoServer($"Test a message to the server with no {nameof(NetworkManager)}.");
// No exceptions thrown is considered passing.
}

private void OnServerStopped(bool obj)
{
m_ServerStopped = true;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.