Skip to content

Commit

Permalink
feat: ClientScene uses log window
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpach committed Apr 26, 2020
1 parent 2e39e13 commit b3656a9
Showing 1 changed file with 47 additions and 45 deletions.
92 changes: 47 additions & 45 deletions Assets/Mirror/Runtime/ClientScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace Mirror
/// </summary>
public static class ClientScene
{
static readonly ILogger logger = LogFactory.GetLogger(typeof(ClientScene));

static bool isSpawnFinished;

/// <summary>
Expand Down Expand Up @@ -66,7 +68,7 @@ internal static void Shutdown()
// this is called from message handler for Owner message
internal static void InternalAddPlayer(NetworkIdentity identity)
{
if (LogFilter.Debug) Debug.LogWarning("ClientScene.InternalAddPlayer");
logger.Log("ClientScene.InternalAddPlayer");

// NOTE: It can be "normal" when changing scenes for the player to be destroyed and recreated.
// But, the player structures are not cleaned up, we'll just replace the old player
Expand All @@ -82,7 +84,7 @@ internal static void InternalAddPlayer(NetworkIdentity identity)
}
else
{
Debug.LogWarning("No ready connection found for setting player controller during InternalAddPlayer");
logger.LogWarning("No ready connection found for setting player controller during InternalAddPlayer");
}
}

Expand All @@ -103,17 +105,17 @@ public static bool AddPlayer(NetworkConnection readyConn)

if (!ready)
{
Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
logger.LogError("Must call AddPlayer() with a connection the first time to become ready.");
return false;
}

if (readyConnection.identity != null)
{
Debug.LogError("ClientScene.AddPlayer: a PlayerController was already added. Did you call AddPlayer twice?");
logger.LogError("ClientScene.AddPlayer: a PlayerController was already added. Did you call AddPlayer twice?");
return false;
}

if (LogFilter.Debug) Debug.Log("ClientScene.AddPlayer() called with connection [" + readyConnection + "]");
if (logger.LogEnabled()) logger.Log("ClientScene.AddPlayer() called with connection [" + readyConnection + "]");

readyConnection.Send(new AddPlayerMessage());
return true;
Expand All @@ -125,7 +127,7 @@ public static bool AddPlayer(NetworkConnection readyConn)
/// <returns>True if succcessful</returns>
public static bool RemovePlayer()
{
if (LogFilter.Debug) Debug.Log("ClientScene.RemovePlayer() called with connection [" + readyConnection + "]");
if (logger.LogEnabled()) logger.Log("ClientScene.RemovePlayer() called with connection [" + readyConnection + "]");

if (readyConnection.identity != null)
{
Expand All @@ -151,11 +153,11 @@ public static bool Ready(NetworkConnection conn)
{
if (ready)
{
Debug.LogError("A connection has already been set as ready. There can only be one.");
logger.LogError("A connection has already been set as ready. There can only be one.");
return false;
}

if (LogFilter.Debug) Debug.Log("ClientScene.Ready() called with connection [" + conn + "]");
if (logger.LogEnabled()) logger.Log("ClientScene.Ready() called with connection [" + conn + "]");

if (conn != null)
{
Expand All @@ -170,7 +172,7 @@ public static bool Ready(NetworkConnection conn)

return true;
}
Debug.LogError("Ready() called with invalid connection object: conn=null");
logger.LogError("Ready() called with invalid connection object: conn=null");
return false;
}

Expand Down Expand Up @@ -210,7 +212,7 @@ static NetworkIdentity SpawnSceneObject(ulong sceneId)
spawnableObjects.Remove(sceneId);
return identity;
}
Debug.LogWarning("Could not find scene object with sceneid:" + sceneId.ToString("X"));
logger.LogWarning("Could not find scene object with sceneid:" + sceneId.ToString("X"));
return null;
}

Expand Down Expand Up @@ -243,12 +245,12 @@ public static void RegisterPrefab(GameObject prefab, Guid newAssetId)
{
identity.assetId = newAssetId;

if (LogFilter.Debug) Debug.Log("Registering prefab '" + prefab.name + "' as asset:" + identity.assetId);
if (logger.LogEnabled()) logger.Log("Registering prefab '" + prefab.name + "' as asset:" + identity.assetId);
prefabs[identity.assetId] = prefab;
}
else
{
Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
logger.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
}
}

Expand All @@ -264,19 +266,19 @@ public static void RegisterPrefab(GameObject prefab)
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity)
{
if (LogFilter.Debug) Debug.Log("Registering prefab '" + prefab.name + "' as asset:" + identity.assetId);
if (logger.LogEnabled()) logger.Log("Registering prefab '" + prefab.name + "' as asset:" + identity.assetId);
prefabs[identity.assetId] = prefab;

NetworkIdentity[] identities = prefab.GetComponentsInChildren<NetworkIdentity>();
if (identities.Length > 1)
{
Debug.LogWarning("The prefab '" + prefab.name +
logger.LogWarning("The prefab '" + prefab.name +
"' has multiple NetworkIdentity components. There can only be one NetworkIdentity on a prefab, and it must be on the root object.");
}
}
else
{
Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
logger.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
}
}

Expand Down Expand Up @@ -308,23 +310,23 @@ public static void RegisterPrefab(GameObject prefab, SpawnHandlerDelegate spawnH
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity == null)
{
Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
logger.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
return;
}

if (spawnHandler == null || unspawnHandler == null)
{
Debug.LogError("RegisterPrefab custom spawn function null for " + identity.assetId);
logger.LogError("RegisterPrefab custom spawn function null for " + identity.assetId);
return;
}

if (identity.assetId == Guid.Empty)
{
Debug.LogError("RegisterPrefab game object " + prefab.name + " has no prefab. Use RegisterSpawnHandler() instead?");
logger.LogError("RegisterPrefab game object " + prefab.name + " has no prefab. Use RegisterSpawnHandler() instead?");
return;
}

if (LogFilter.Debug) Debug.Log("Registering custom prefab '" + prefab.name + "' as asset:" + identity.assetId + " " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName());
if (logger.LogEnabled()) logger.Log("Registering custom prefab '" + prefab.name + "' as asset:" + identity.assetId + " " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName());

spawnHandlers[identity.assetId] = spawnHandler;
unspawnHandlers[identity.assetId] = unspawnHandler;
Expand All @@ -339,7 +341,7 @@ public static void UnregisterPrefab(GameObject prefab)
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity == null)
{
Debug.LogError("Could not unregister '" + prefab.name + "' since it contains no NetworkIdentity component");
logger.LogError("Could not unregister '" + prefab.name + "' since it contains no NetworkIdentity component");
return;
}
spawnHandlers.Remove(identity.assetId);
Expand Down Expand Up @@ -369,11 +371,11 @@ public static void RegisterSpawnHandler(Guid assetId, SpawnHandlerDelegate spawn
{
if (spawnHandler == null || unspawnHandler == null)
{
Debug.LogError("RegisterSpawnHandler custom spawn function null for " + assetId);
logger.LogError("RegisterSpawnHandler custom spawn function null for " + assetId);
return;
}

if (LogFilter.Debug) Debug.Log("RegisterSpawnHandler asset '" + assetId + "' " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName());
if (logger.LogEnabled()) logger.Log("RegisterSpawnHandler asset '" + assetId + "' " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName());

spawnHandlers[assetId] = spawnHandler;
unspawnHandlers[assetId] = unspawnHandler;
Expand Down Expand Up @@ -481,10 +483,10 @@ internal static void OnSpawn(SpawnMessage msg)
{
if (msg.assetId == Guid.Empty && msg.sceneId == 0)
{
Debug.LogError("OnObjSpawn netId: " + msg.netId + " has invalid asset Id");
logger.LogError("OnObjSpawn netId: " + msg.netId + " has invalid asset Id");
return;
}
if (LogFilter.Debug) Debug.Log($"Client spawn handler instantiating netId={msg.netId} assetID={msg.assetId} sceneId={msg.sceneId} pos={msg.position}");
if (logger.LogEnabled()) logger.Log($"Client spawn handler instantiating netId={msg.netId} assetID={msg.assetId} sceneId={msg.sceneId} pos={msg.position}");

// was the object already spawned?
NetworkIdentity identity = GetExistingObject(msg.netId);
Expand All @@ -496,7 +498,7 @@ internal static void OnSpawn(SpawnMessage msg)

if (identity == null)
{
Debug.LogError($"Could not spawn assetId={msg.assetId} scene={msg.sceneId} netId={msg.netId}");
logger.LogError($"Could not spawn assetId={msg.assetId} scene={msg.sceneId} netId={msg.netId}");
return;
}

Expand All @@ -514,9 +516,9 @@ static NetworkIdentity SpawnPrefab(SpawnMessage msg)
if (GetPrefab(msg.assetId, out GameObject prefab))
{
GameObject obj = Object.Instantiate(prefab, msg.position, msg.rotation);
if (LogFilter.Debug)
if (logger.LogEnabled())
{
Debug.Log("Client spawn handler instantiating [netId:" + msg.netId + " asset ID:" + msg.assetId + " pos:" + msg.position + " rotation: " + msg.rotation + "]");
logger.Log("Client spawn handler instantiating [netId:" + msg.netId + " asset ID:" + msg.assetId + " pos:" + msg.position + " rotation: " + msg.rotation + "]");
}

return obj.GetComponent<NetworkIdentity>();
Expand All @@ -526,12 +528,12 @@ static NetworkIdentity SpawnPrefab(SpawnMessage msg)
GameObject obj = handler(msg);
if (obj == null)
{
Debug.LogWarning("Client spawn handler for " + msg.assetId + " returned null");
logger.LogWarning("Client spawn handler for " + msg.assetId + " returned null");
return null;
}
return obj.GetComponent<NetworkIdentity>();
}
Debug.LogError("Failed to spawn server object, did you forget to add it to the NetworkManager? assetId=" + msg.assetId + " netId=" + msg.netId);
logger.LogError("Failed to spawn server object, did you forget to add it to the NetworkManager? assetId=" + msg.assetId + " netId=" + msg.netId);
return null;
}

Expand All @@ -540,31 +542,31 @@ static NetworkIdentity SpawnSceneObject(SpawnMessage msg)
NetworkIdentity spawnedId = SpawnSceneObject(msg.sceneId);
if (spawnedId == null)
{
Debug.LogError("Spawn scene object not found for " + msg.sceneId.ToString("X") + " SpawnableObjects.Count=" + spawnableObjects.Count);
logger.LogError("Spawn scene object not found for " + msg.sceneId.ToString("X") + " SpawnableObjects.Count=" + spawnableObjects.Count);

// dump the whole spawnable objects dict for easier debugging
if (LogFilter.Debug)
if (logger.LogEnabled())
{
foreach (KeyValuePair<ulong, NetworkIdentity> kvp in spawnableObjects)
Debug.Log("Spawnable: SceneId=" + kvp.Key + " name=" + kvp.Value.name);
logger.Log("Spawnable: SceneId=" + kvp.Key + " name=" + kvp.Value.name);
}
}

if (LogFilter.Debug) Debug.Log("Client spawn for [netId:" + msg.netId + "] [sceneId:" + msg.sceneId + "] obj:" + spawnedId);
if (logger.LogEnabled()) logger.Log("Client spawn for [netId:" + msg.netId + "] [sceneId:" + msg.sceneId + "] obj:" + spawnedId);
return spawnedId;
}

internal static void OnObjectSpawnStarted(ObjectSpawnStartedMessage _)
{
if (LogFilter.Debug) Debug.Log("SpawnStarted");
if (logger.LogEnabled()) logger.Log("SpawnStarted");

PrepareToSpawnSceneObjects();
isSpawnFinished = false;
}

internal static void OnObjectSpawnFinished(ObjectSpawnFinishedMessage _)
{
if (LogFilter.Debug) Debug.Log("SpawnFinished");
logger.Log("SpawnFinished");

// paul: Initialize the objects in the same order as they were initialized
// in the server. This is important if spawned objects
Expand All @@ -590,7 +592,7 @@ internal static void OnObjectDestroy(ObjectDestroyMessage msg)

static void DestroyObject(uint netId)
{
if (LogFilter.Debug) Debug.Log("ClientScene.OnObjDestroy netId:" + netId);
if (logger.LogEnabled()) logger.Log("ClientScene.OnObjDestroy netId:" + netId);

if (NetworkIdentity.spawned.TryGetValue(netId, out NetworkIdentity localObject) && localObject != null)
{
Expand All @@ -615,20 +617,20 @@ static void DestroyObject(uint netId)
}
else
{
if (LogFilter.Debug) Debug.LogWarning("Did not find target for destroy message for " + netId);
if (logger.LogEnabled()) logger.LogWarning("Did not find target for destroy message for " + netId);
}
}

internal static void OnHostClientObjectDestroy(ObjectDestroyMessage msg)
{
if (LogFilter.Debug) Debug.Log("ClientScene.OnLocalObjectObjDestroy netId:" + msg.netId);
if (logger.LogEnabled()) logger.Log("ClientScene.OnLocalObjectObjDestroy netId:" + msg.netId);

NetworkIdentity.spawned.Remove(msg.netId);
}

internal static void OnHostClientObjectHide(ObjectHideMessage msg)
{
if (LogFilter.Debug) Debug.Log("ClientScene::OnLocalObjectObjHide netId:" + msg.netId);
if (logger.LogEnabled()) logger.Log("ClientScene::OnLocalObjectObjHide netId:" + msg.netId);

if (NetworkIdentity.spawned.TryGetValue(msg.netId, out NetworkIdentity localObject) && localObject != null)
{
Expand All @@ -653,7 +655,7 @@ internal static void OnHostClientSpawn(SpawnMessage msg)

internal static void OnUpdateVarsMessage(UpdateVarsMessage msg)
{
if (LogFilter.Debug) Debug.Log("ClientScene.OnUpdateVarsMessage " + msg.netId);
if (logger.LogEnabled()) logger.Log("ClientScene.OnUpdateVarsMessage " + msg.netId);

if (NetworkIdentity.spawned.TryGetValue(msg.netId, out NetworkIdentity localObject) && localObject != null)
{
Expand All @@ -662,13 +664,13 @@ internal static void OnUpdateVarsMessage(UpdateVarsMessage msg)
}
else
{
Debug.LogWarning("Did not find target for sync message for " + msg.netId + " . Note: this can be completely normal because UDP messages may arrive out of order, so this message might have arrived after a Destroy message.");
logger.LogWarning("Did not find target for sync message for " + msg.netId + " . Note: this can be completely normal because UDP messages may arrive out of order, so this message might have arrived after a Destroy message.");
}
}

internal static void OnRPCMessage(RpcMessage msg)
{
if (LogFilter.Debug) Debug.Log("ClientScene.OnRPCMessage hash:" + msg.functionHash + " netId:" + msg.netId);
if (logger.LogEnabled()) logger.Log("ClientScene.OnRPCMessage hash:" + msg.functionHash + " netId:" + msg.netId);

if (NetworkIdentity.spawned.TryGetValue(msg.netId, out NetworkIdentity identity))
{
Expand All @@ -679,7 +681,7 @@ internal static void OnRPCMessage(RpcMessage msg)

internal static void OnSyncEventMessage(SyncEventMessage msg)
{
if (LogFilter.Debug) Debug.Log("ClientScene.OnSyncEventMessage " + msg.netId);
if (logger.LogEnabled()) logger.Log("ClientScene.OnSyncEventMessage " + msg.netId);

if (NetworkIdentity.spawned.TryGetValue(msg.netId, out NetworkIdentity identity))
{
Expand All @@ -688,7 +690,7 @@ internal static void OnSyncEventMessage(SyncEventMessage msg)
}
else
{
Debug.LogWarning("Did not find target for SyncEvent message for " + msg.netId);
logger.LogWarning("Did not find target for SyncEvent message for " + msg.netId);
}
}

Expand All @@ -700,7 +702,7 @@ static void CheckForLocalPlayer(NetworkIdentity identity)
identity.connectionToServer = readyConnection;
identity.OnStartLocalPlayer();

if (LogFilter.Debug) Debug.Log("ClientScene.OnOwnerMessage - player=" + identity.name);
if (logger.LogEnabled()) logger.Log("ClientScene.OnOwnerMessage - player=" + identity.name);
}
}
}
Expand Down

0 comments on commit b3656a9

Please sign in to comment.