347 lines
14 KiB
C#
347 lines
14 KiB
C#
using UnityEngine;
|
|
using Unity.Netcode;
|
|
|
|
namespace Northbound
|
|
{
|
|
/// <summary>
|
|
/// Validates NetworkManager configuration before starting server
|
|
/// Checks for common issues that prevent server from starting
|
|
/// </summary>
|
|
public class NetworkManagerValidator : MonoBehaviour
|
|
{
|
|
[Header("Validation Settings")]
|
|
[SerializeField] private bool validateOnStart = true;
|
|
[SerializeField] private bool validateOnEnable = true;
|
|
[SerializeField] private bool showDetailedLogs = true;
|
|
|
|
[Header("Auto-Fix")]
|
|
[SerializeField] private bool autoFixIfPossible = true;
|
|
|
|
private NetworkManager _networkManager;
|
|
private bool _hasValidated = false;
|
|
|
|
private void Awake()
|
|
{
|
|
StartCoroutine(InitializeDelayed());
|
|
}
|
|
|
|
private System.Collections.IEnumerator InitializeDelayed()
|
|
{
|
|
Debug.Log("[NetworkValidator] Waiting for NetworkManager to initialize...");
|
|
|
|
int attempts = 0;
|
|
while (_networkManager == null && attempts < 50)
|
|
{
|
|
_networkManager = NetworkManager.Singleton;
|
|
if (_networkManager == null)
|
|
{
|
|
yield return new WaitForSeconds(0.1f);
|
|
attempts++;
|
|
}
|
|
}
|
|
|
|
if (_networkManager == null)
|
|
{
|
|
Debug.LogError("<color=red>[NetworkValidator] ❌ CRITICAL: NetworkManager not found after 50 attempts!</color>");
|
|
Debug.LogError("[NetworkValidator] Ensure NetworkManager is in scene and has 'NetworkManager' component");
|
|
yield break;
|
|
}
|
|
|
|
Debug.Log("[NetworkValidator] NetworkManager found!");
|
|
|
|
if (validateOnAwake)
|
|
{
|
|
yield return new WaitForSeconds(0.5f);
|
|
ValidateConfiguration();
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (validateOnStart && !_hasValidated)
|
|
{
|
|
ValidateConfiguration();
|
|
}
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (validateOnEnable && !_hasValidated)
|
|
{
|
|
ValidateConfiguration();
|
|
}
|
|
}
|
|
|
|
[ContextMenu("Validate NetworkManager Configuration")]
|
|
public void ValidateConfiguration()
|
|
{
|
|
Debug.Log("<color=yellow>=== NETWORKMANAGER VALIDATION ===</color>");
|
|
|
|
if (_networkManager == null)
|
|
{
|
|
Debug.LogError("<color=red>[NetworkValidator] ❌ NetworkManager is NULL!</color>");
|
|
Debug.LogError("[NetworkValidator] Cannot validate configuration");
|
|
return;
|
|
}
|
|
|
|
_hasValidated = true;
|
|
|
|
bool allValid = true;
|
|
|
|
allValid &= CheckPlayerPrefab();
|
|
allValid &= CheckNetworkConfig();
|
|
allValid &= CheckTransport();
|
|
allValid &= CheckRequiredComponents();
|
|
|
|
Debug.Log("");
|
|
if (allValid)
|
|
{
|
|
Debug.Log("<color=green>[NetworkValidator] ✅ ALL CHECKS PASSED - NetworkManager is ready!</color>");
|
|
Debug.Log("[NetworkValidator] Server should start successfully");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("<color=red>[NetworkValidator] ❌ VALIDATION FAILED - Server may NOT start!</color>");
|
|
Debug.LogError("[NetworkValidator] Please fix the issues above");
|
|
}
|
|
|
|
Debug.Log("=================================");
|
|
}
|
|
|
|
private bool CheckPlayerPrefab()
|
|
{
|
|
Debug.Log("<color=cyan>[NetworkValidator] 1. Checking Player Prefab...</color>");
|
|
|
|
if (_networkManager.NetworkConfig == null)
|
|
{
|
|
Debug.LogError("<color=red>[NetworkValidator] ❌ NetworkConfig is NULL!</color>");
|
|
return false;
|
|
}
|
|
|
|
GameObject playerPrefab = _networkManager.NetworkConfig.PlayerPrefab;
|
|
|
|
if (playerPrefab == null)
|
|
{
|
|
Debug.LogError("<color=red>[NetworkValidator] ❌ Player Prefab is NOT assigned!</color>");
|
|
Debug.LogError("<color=red>[NetworkValidator] This is the #1 cause of servers not starting!</color>");
|
|
Debug.LogError("[NetworkValidator] FIX:");
|
|
Debug.LogError("1. Select NetworkManager in scene");
|
|
Debug.LogError("2. Inspector → Network Config → Player Prefab");
|
|
Debug.LogError("3. Drag your Player Prefab from Project window");
|
|
Debug.LogError("4. Assign to Player Prefab field");
|
|
return false;
|
|
}
|
|
|
|
Debug.Log("<color=green>[NetworkValidator] ✅ Player Prefab assigned: " + playerPrefab.name + "</color>");
|
|
|
|
NetworkObject networkObject = playerPrefab.GetComponent<NetworkObject>();
|
|
if (networkObject == null)
|
|
{
|
|
Debug.LogWarning("<color=yellow>[NetworkValidator] ⚠ Player Prefab has NO NetworkObject!</color>");
|
|
Debug.LogWarning("[NetworkValidator] FIX: Add NetworkObject component to Player Prefab");
|
|
return false;
|
|
}
|
|
|
|
Debug.Log("<color=green>[NetworkValidator] ✅ Player Prefab has NetworkObject</color>");
|
|
return true;
|
|
}
|
|
|
|
private bool CheckNetworkConfig()
|
|
{
|
|
Debug.Log("<color=cyan>[NetworkValidator] 2. Checking Network Config...</color>");
|
|
|
|
if (_networkManager.NetworkConfig == null)
|
|
{
|
|
Debug.LogError("<color=red>[NetworkValidator] ❌ NetworkConfig is NULL!</color>");
|
|
return false;
|
|
}
|
|
|
|
bool configValid = true;
|
|
|
|
bool connectionApproval = _networkManager.NetworkConfig.ConnectionApproval;
|
|
if (connectionApproval)
|
|
{
|
|
Debug.Log("<color=green>[NetworkValidator] ✅ Connection Approval: ENABLED</color>");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("<color=yellow>[NetworkValidator] ⚠ Connection Approval: DISABLED</color>");
|
|
Debug.LogWarning("[NetworkValidator] All connections will be auto-approved");
|
|
}
|
|
|
|
bool sceneManagement = _networkManager.NetworkConfig.EnableSceneManagement;
|
|
if (sceneManagement)
|
|
{
|
|
Debug.Log("<color=green>[NetworkValidator] ✅ Scene Management: ENABLED</color>");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("<color=yellow>[NetworkValidator] ⚠ Scene Management: DISABLED</color>");
|
|
}
|
|
|
|
float clientBuffer = _networkManager.NetworkConfig.ClientConnectionBufferTimeout;
|
|
Debug.Log("[NetworkValidator] Client Connection Buffer: " + clientBuffer + "s");
|
|
|
|
return configValid;
|
|
}
|
|
|
|
private bool CheckTransport()
|
|
{
|
|
Debug.Log("<color=cyan>[NetworkValidator] 3. Checking Transport...</color>");
|
|
|
|
var transport = _networkManager.GetComponent<Unity.Netcode.Transports.UTP.UnityTransport>();
|
|
|
|
if (transport == null)
|
|
{
|
|
Debug.LogError("<color=red>[NetworkValidator] ❌ UnityTransport not found!</color>");
|
|
Debug.LogError("[NetworkValidator] FIX: Add Unity Transport component to NetworkManager");
|
|
return false;
|
|
}
|
|
|
|
Debug.Log("<color=green>[NetworkValidator] ✅ Unity Transport exists</color>");
|
|
|
|
ushort port = transport.ConnectionData.Port;
|
|
Debug.Log("[NetworkValidator] Transport Port: " + port);
|
|
|
|
if (port <= 0)
|
|
{
|
|
Debug.LogWarning("<color=yellow>[NetworkValidator] ⚠ Transport Port is not set (0)</color>");
|
|
Debug.LogWarning("[NetworkValidator] FIX: Set port to 40445 in Unity Transport component");
|
|
return false;
|
|
}
|
|
|
|
string address = transport.ConnectionData.Address;
|
|
Debug.Log("[NetworkValidator] Transport Address: " + address);
|
|
|
|
if (address == "127.0.0.1")
|
|
{
|
|
Debug.LogError("<color=red>[NetworkValidator] ❌ Transport Address is 127.0.0.1!</color>");
|
|
Debug.LogError("<color=red>[NetworkValidator] This blocks ALL remote connections!</color>");
|
|
Debug.LogError("[NetworkValidator] FIX: Set to 0.0.0.0");
|
|
return false;
|
|
}
|
|
|
|
if (address == "0.0.0.0")
|
|
{
|
|
Debug.Log("<color=green>[NetworkValidator] ✅ Transport Address: 0.0.0.0 (accepts all connections)</color>");
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private bool CheckRequiredComponents()
|
|
{
|
|
Debug.Log("<color=cyan>[NetworkValidator] 4. Checking Required Components...</color>");
|
|
|
|
bool allPresent = true;
|
|
|
|
NetworkConnectionHandler connectionHandler = FindObjectOfType<NetworkConnectionHandler>();
|
|
if (connectionHandler != null)
|
|
{
|
|
Debug.Log("<color=green>[NetworkValidator] ✅ NetworkConnectionHandler found</color>");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("<color=yellow>[NetworkValidator] ⚠ NetworkConnectionHandler NOT found</color>");
|
|
Debug.LogWarning("[NetworkValidator] FIX: Add NetworkConnectionHandler to scene");
|
|
Debug.LogWarning("[NetworkValidator] Required for manual player spawning");
|
|
}
|
|
|
|
PlayerSpawnPoint[] spawnPoints = FindObjectsByType<PlayerSpawnPoint>(FindObjectsSortMode.None);
|
|
if (spawnPoints.Length > 0)
|
|
{
|
|
Debug.Log("<color=green>[NetworkValidator] ✅ Found " + spawnPoints.Length + " Player Spawn Point(s)</color>");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("<color=yellow>[NetworkValidator] ⚠ No Player Spawn Points found</color>");
|
|
Debug.LogWarning("[NetworkValidator] FIX: Add PlayerSpawnPoint component to empty GameObject(s)");
|
|
}
|
|
|
|
AutoHost autoHost = FindObjectOfType<AutoHost>();
|
|
if (autoHost != null && autoHost.enabled)
|
|
{
|
|
Debug.LogWarning("<color=yellow>[NetworkValidator] ⚠ AutoHost is enabled!</color>");
|
|
Debug.LogWarning("[NetworkValidator] This might interfere with manual host startup");
|
|
Debug.LogWarning("[NetworkValidator] Consider disabling AutoHost");
|
|
}
|
|
|
|
return allPresent;
|
|
}
|
|
|
|
[ContextMenu("Generate Fix Report")]
|
|
public void GenerateFixReport()
|
|
{
|
|
Debug.Log("=== NETWORKMANAGER FIX REPORT ===");
|
|
Debug.Log("");
|
|
Debug.Log("PROBLEM: PortListenerTest works, but Unity Netcode doesn't");
|
|
Debug.Log("");
|
|
Debug.Log("MOST LIKELY CAUSE:");
|
|
Debug.LogError("1. ❌ Player Prefab is NOT assigned (90% probability)");
|
|
Debug.LogWarning("2. ⚠ NetworkManager has initialization order issue");
|
|
Debug.LogWarning("3. ⚠ Transport not configured correctly");
|
|
Debug.LogWarning("4. ⚠ AutoHost interfering");
|
|
Debug.Log("");
|
|
Debug.Log("QUICK FIX:");
|
|
Debug.Log("1. Select NetworkManager in scene");
|
|
Debug.Log("2. Inspector → Network Config → Player Prefab");
|
|
Debug.Log("3. Drag your Player Prefab from Project window");
|
|
Debug.Log("4. Drop into Player Prefab field");
|
|
Debug.Log("5. Save scene");
|
|
Debug.Log("6. Start Host");
|
|
Debug.Log("7. Test on yougetsignal IMMEDIATELY");
|
|
Debug.Log("");
|
|
Debug.Log("COMPLETE FIX:");
|
|
Debug.Log("1. Right-click this component → 'Validate NetworkManager Configuration'");
|
|
Debug.Log("2. Fix any errors shown");
|
|
Debug.Log("3. Add ForceTransportBinding");
|
|
Debug.Log("4. Right-click → 'Force Then Start Host'");
|
|
Debug.Log("5. Add ContinuousPortMonitor");
|
|
Debug.Log("6. Right-click → 'Start Port Monitoring'");
|
|
Debug.Log("7. Watch Console for port status");
|
|
Debug.Log("=================================");
|
|
}
|
|
|
|
[ContextMenu("Check If Server Can Start")]
|
|
public void CheckIfServerCanStart()
|
|
{
|
|
Debug.Log("=== CAN SERVER START? ===");
|
|
|
|
bool canStart = true;
|
|
|
|
if (_networkManager == null)
|
|
{
|
|
Debug.LogError("❌ NO - NetworkManager is NULL");
|
|
canStart = false;
|
|
}
|
|
else
|
|
{
|
|
if (_networkManager.NetworkConfig?.PlayerPrefab == null)
|
|
{
|
|
Debug.LogError("❌ NO - Player Prefab NOT assigned");
|
|
canStart = false;
|
|
}
|
|
|
|
if (_networkManager.GetComponent<Unity.Netcode.Transports.UTP.UnityTransport>() == null)
|
|
{
|
|
Debug.LogError("❌ NO - UnityTransport NOT found");
|
|
canStart = false;
|
|
}
|
|
}
|
|
|
|
if (canStart)
|
|
{
|
|
Debug.Log("<color=green>✅ YES - Server should be able to start</color>");
|
|
Debug.Log("If port still closed after starting:");
|
|
Debug.Log("1. Check Console for errors");
|
|
Debug.Log("2. Verify Port Forwarding is working");
|
|
Debug.Log("3. Verify Firewall is allowing port");
|
|
}
|
|
|
|
Debug.Log("========================");
|
|
}
|
|
|
|
private bool validateOnAwake = true;
|
|
}
|
|
}
|