GetPlayerController() private method

private GetPlayerController ( short playerControllerId, PlayerController &playerController ) : bool
playerControllerId short
playerController PlayerController
return bool
コード例 #1
0
        public static bool AddPlayer(NetworkConnection readyConn, short playerControllerId, MessageBase extraMessage)
        {
            if (playerControllerId < 0)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is negative");
                }
                return(false);
            }
            if (playerControllerId > 32)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is too high, max is " + 32);
                }
                return(false);
            }
            if (playerControllerId > 16 && LogFilter.logWarn)
            {
                Debug.LogWarning("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is unusually high");
            }
            while (playerControllerId >= s_LocalPlayers.Count)
            {
                s_LocalPlayers.Add(new PlayerController());
            }
            if (readyConn == null)
            {
                if (!s_IsReady)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
                    }
                    return(false);
                }
            }
            else
            {
                s_IsReady         = true;
                s_ReadyConnection = readyConn;
            }
            PlayerController playerController;

            if (s_ReadyConnection.GetPlayerController(playerControllerId, out playerController) && playerController.IsValid && playerController.gameObject != null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " already in use.");
                }
                return(false);
            }
            if (LogFilter.logDebug)
            {
                Debug.Log("ClientScene::AddPlayer() for ID " + playerControllerId + " called with connection [" + s_ReadyConnection + "]");
            }
            AddPlayerMessage addPlayerMessage = new AddPlayerMessage();

            addPlayerMessage.playerControllerId = playerControllerId;
            if (extraMessage != null)
            {
                NetworkWriter networkWriter = new NetworkWriter();
                extraMessage.Serialize(networkWriter);
                addPlayerMessage.msgData = networkWriter.ToArray();
                addPlayerMessage.msgSize = networkWriter.Position;
            }
            s_ReadyConnection.Send(37, addPlayerMessage);
            return(true);
        }
        // use this to implicitly become ready
        public static bool AddPlayer(NetworkConnection readyConn, short playerControllerId, MessageBase extraMessage)
        {
            if (playerControllerId < 0)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is negative");
                }
                return(false);
            }
            if (playerControllerId > PlayerController.MaxPlayersPerClient)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is too high, max is " + PlayerController.MaxPlayersPerClient);
                }
                return(false);
            }
            if (playerControllerId > PlayerController.MaxPlayersPerClient / 2)
            {
                if (LogFilter.logWarn)
                {
                    Debug.LogWarning("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is unusually high");
                }
            }

            // fill out local players array
            while (playerControllerId >= s_LocalPlayers.Count)
            {
                s_LocalPlayers.Add(new PlayerController());
            }

            // ensure valid ready connection
            if (readyConn == null)
            {
                if (!s_IsReady)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
                    }
                    return(false);
                }
            }
            else
            {
                s_IsReady         = true;
                s_ReadyConnection = readyConn;
            }

            PlayerController existingPlayerController;

            if (s_ReadyConnection.GetPlayerController(playerControllerId, out existingPlayerController))
            {
                if (existingPlayerController.IsValid && existingPlayerController.gameObject != null)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " already in use.");
                    }
                    return(false);
                }
            }

            if (LogFilter.logDebug)
            {
                Debug.Log("ClientScene::AddPlayer() for ID " + playerControllerId + " called with connection [" + s_ReadyConnection + "]");
            }

#if ENABLE_UNET_HOST_MIGRATION
            if (!hasMigrationPending())
            {
#endif
            var msg = new AddPlayerMessage();
            msg.playerControllerId = playerControllerId;
            if (extraMessage != null)
            {
                var writer = new NetworkWriter();
                extraMessage.Serialize(writer);
                msg.msgData = writer.ToArray();
                msg.msgSize = writer.Position;
            }
            s_ReadyConnection.Send(MsgType.AddPlayer, msg);
#if ENABLE_UNET_HOST_MIGRATION
        }

        else
        {
            return(SendReconnectMessage(extraMessage));
        }
#endif
            return(true);
        }
コード例 #3
0
        /// <summary>
        /// <para>This adds a player GameObject for this client. This causes an AddPlayer message to be sent to the server, and NetworkManager.OnServerAddPlayer is called. If an extra message was passed to AddPlayer, then OnServerAddPlayer will be called with a NetworkReader that contains the contents of the message.</para>
        /// </summary>
        /// <param name="readyConn">The connection to become ready for this client.</param>
        /// <param name="playerControllerId">The local player ID number.</param>
        /// <param name="extraMessage">An extra message object that can be passed to the server for this player.</param>
        /// <returns>
        /// <para>True if player was added.</para>
        /// </returns>
        public static bool AddPlayer(NetworkConnection readyConn, short playerControllerId, MessageBase extraMessage)
        {
            PlayerController controller;

            if (playerControllerId < 0)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is negative");
                }
                return(false);
            }
            if (playerControllerId > 0x20)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError(string.Concat(new object[] { "ClientScene::AddPlayer: playerControllerId of ", playerControllerId, " is too high, max is ", 0x20 }));
                }
                return(false);
            }
            if ((playerControllerId > 0x10) && LogFilter.logWarn)
            {
                Debug.LogWarning("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is unusually high");
            }
            while (playerControllerId >= s_LocalPlayers.Count)
            {
                s_LocalPlayers.Add(new PlayerController());
            }
            if (readyConn == null)
            {
                if (!s_IsReady)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
                    }
                    return(false);
                }
            }
            else
            {
                s_IsReady         = true;
                s_ReadyConnection = readyConn;
            }
            if (s_ReadyConnection.GetPlayerController(playerControllerId, out controller) && (controller.IsValid && (controller.gameObject != null)))
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " already in use.");
                }
                return(false);
            }
            if (LogFilter.logDebug)
            {
                Debug.Log(string.Concat(new object[] { "ClientScene::AddPlayer() for ID ", playerControllerId, " called with connection [", s_ReadyConnection, "]" }));
            }
            if (!hasMigrationPending())
            {
                AddPlayerMessage msg = new AddPlayerMessage {
                    playerControllerId = playerControllerId
                };
                if (extraMessage != null)
                {
                    NetworkWriter writer = new NetworkWriter();
                    extraMessage.Serialize(writer);
                    msg.msgData = writer.ToArray();
                    msg.msgSize = writer.Position;
                }
                s_ReadyConnection.Send(0x25, msg);
            }
            else
            {
                return(SendReconnectMessage(extraMessage));
            }
            return(true);
        }
コード例 #4
0
 internal bool InternalAddPlayerForConnection(NetworkConnection conn, GameObject playerGameObject, short playerControllerId)
 {
     NetworkIdentity view;
       if (!NetworkServer.GetNetworkIdentity(playerGameObject, out view))
       {
     if (LogFilter.logError)
       Debug.Log((object) ("AddPlayer: playerGameObject has no NetworkIdentity. Please add a NetworkIdentity to " + (object) playerGameObject));
     return false;
       }
       if (!this.CheckPlayerControllerIdForConnection(conn, playerControllerId))
     return false;
       PlayerController playerController1 = (PlayerController) null;
       GameObject gameObject = (GameObject) null;
       if (conn.GetPlayerController(playerControllerId, out playerController1))
     gameObject = playerController1.gameObject;
       if ((UnityEngine.Object) gameObject != (UnityEngine.Object) null)
       {
     if (LogFilter.logError)
       Debug.Log((object) ("AddPlayer: player object already exists for playerControllerId of " + (object) playerControllerId));
     return false;
       }
       PlayerController playerController2 = new PlayerController(playerGameObject, playerControllerId);
       conn.SetPlayerController(playerController2);
       view.m_PlayerId = playerController2.playerControllerId;
       view.SetConnectionToClient(conn);
       NetworkServer.SetClientReady(conn);
       if (this.SetupLocalPlayerForConnection(conn, view, playerController2))
     return true;
       if (LogFilter.logDebug)
       {
     object[] objArray = new object[4];
     int index1 = 0;
     string str1 = "Adding new playerGameObject object netId: ";
     objArray[index1] = (object) str1;
     int index2 = 1;
     // ISSUE: variable of a boxed type
     __Boxed<NetworkInstanceId> local1 = (ValueType) playerGameObject.GetComponent<NetworkIdentity>().netId;
     objArray[index2] = (object) local1;
     int index3 = 2;
     string str2 = " asset ID ";
     objArray[index3] = (object) str2;
     int index4 = 3;
     // ISSUE: variable of a boxed type
     __Boxed<NetworkHash128> local2 = (ValueType) playerGameObject.GetComponent<NetworkIdentity>().assetId;
     objArray[index4] = (object) local2;
     Debug.Log((object) string.Concat(objArray));
       }
       this.FinishPlayerForConnection(conn, view, playerGameObject);
       return true;
 }
コード例 #5
0
        // use this to implicitly become ready
        public static bool AddPlayer(NetworkConnection readyConn, short playerControllerId, MessageBase extraMessage)
        {
            if (playerControllerId < 0)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is negative");
                }
                return(false);
            }
            if (playerControllerId > PlayerController.MaxPlayersPerClient)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is too high, max is " + PlayerController.MaxPlayersPerClient);
                }
                return(false);
            }
            if (playerControllerId > PlayerController.MaxPlayersPerClient / 2)
            {
                if (LogFilter.logWarn)
                {
                    Debug.LogWarning("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is unusually high");
                }
            }

            // fill out local players array
            while (playerControllerId >= s_LocalPlayers.Count)
            {
                s_LocalPlayers.Add(new PlayerController());
            }

            // ensure valid ready connection
            if (readyConn == null)
            {
                if (!s_IsReady)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
                    }
                    return(false);
                }
            }
            else
            {
                s_IsReady         = true;
                s_ReadyConnection = readyConn;
            }

            PlayerController existingPlayerController;

            if (s_ReadyConnection.GetPlayerController(playerControllerId, out existingPlayerController))
            {
                if (existingPlayerController.IsValid && existingPlayerController.gameObject != null)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " already in use.");
                    }
                    return(false);
                }
            }

            if (LogFilter.logDebug)
            {
                Debug.Log("ClientScene::AddPlayer() for ID " + playerControllerId + " called with connection [" + s_ReadyConnection + "]");
            }

#if ENABLE_UNET_HOST_MIGRATION
            if (s_ReconnectId == ReconnectIdInvalid)
            {
#endif
            var msg = new AddPlayerMessage();
            msg.playerControllerId = playerControllerId;
            if (extraMessage != null)
            {
                var writer = new NetworkWriter();
                extraMessage.Serialize(writer);
                msg.msgData = writer.ToArray();
                msg.msgSize = writer.Position;
            }
            s_ReadyConnection.Send(MsgType.AddPlayer, msg);
#if ENABLE_UNET_HOST_MIGRATION
        }

        else
        {
            if (LogFilter.logDebug)
            {
                Debug.Log("ClientScene::AddPlayer reconnect " + s_ReconnectId);
            }

            if (s_Peers == null)
            {
                SetReconnectId(ReconnectIdInvalid, null);
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: reconnecting, but no peers.");
                }
                return(false);
            }

            // reconnect all the players
            for (int i = 0; i < s_Peers.Length; i++)
            {
                var peer = s_Peers[i];
                if (peer.playerIds == null)
                {
                    // this coluld be empty if this peer had no players
                    continue;
                }
                if (peer.connectionId == s_ReconnectId)
                {
                    for (int pid = 0; pid < peer.playerIds.Length; pid++)
                    {
                        var msg = new ReconnectMessage();
                        msg.oldConnectionId    = s_ReconnectId;
                        msg.netId              = peer.playerIds[pid].netId;
                        msg.playerControllerId = peer.playerIds[pid].playerControllerId;
                        if (extraMessage != null)
                        {
                            var writer = new NetworkWriter();
                            extraMessage.Serialize(writer);
                            msg.msgData = writer.ToArray();
                            msg.msgSize = writer.Position;
                        }

                        s_ReadyConnection.Send(MsgType.ReconnectPlayer, msg);
                    }
                }
            }
            // this should only be done once.
            SetReconnectId(ReconnectIdInvalid, null);
        }
#endif
            return(true);
        }
コード例 #6
0
 internal bool InternalReplacePlayerForConnection(NetworkConnection conn, GameObject playerGameObject, short playerControllerId)
 {
     NetworkIdentity identity;
     PlayerController controller;
     if (!GetNetworkIdentity(playerGameObject, out identity))
     {
         if (LogFilter.logError)
         {
             Debug.LogError("ReplacePlayer: playerGameObject has no NetworkIdentity. Please add a NetworkIdentity to " + playerGameObject);
         }
         return false;
     }
     if (!CheckPlayerControllerIdForConnection(conn, playerControllerId))
     {
         return false;
     }
     if (LogFilter.logDev)
     {
         Debug.Log("NetworkServer ReplacePlayer");
     }
     if (conn.GetPlayerController(playerControllerId, out controller))
     {
         controller.unetView.SetNotLocalPlayer();
         controller.unetView.ClearClientOwner();
     }
     PlayerController player = new PlayerController(playerGameObject, playerControllerId);
     conn.SetPlayerController(player);
     identity.SetConnectionToClient(conn, player.playerControllerId);
     if (LogFilter.logDev)
     {
         Debug.Log("NetworkServer ReplacePlayer setup local");
     }
     if (!this.SetupLocalPlayerForConnection(conn, identity, player))
     {
         if (LogFilter.logDebug)
         {
             Debug.Log(string.Concat(new object[] { "Replacing playerGameObject object netId: ", playerGameObject.GetComponent<NetworkIdentity>().netId, " asset ID ", playerGameObject.GetComponent<NetworkIdentity>().assetId }));
         }
         FinishPlayerForConnection(conn, identity, playerGameObject);
         if (identity.localPlayerAuthority)
         {
             identity.SetClientOwner(conn);
         }
     }
     return true;
 }
コード例 #7
0
 internal bool InternalAddPlayerForConnection(NetworkConnection conn, GameObject playerGameObject, short playerControllerId)
 {
     NetworkIdentity identity;
     if (!GetNetworkIdentity(playerGameObject, out identity))
     {
         if (LogFilter.logError)
         {
             Debug.Log("AddPlayer: playerGameObject has no NetworkIdentity. Please add a NetworkIdentity to " + playerGameObject);
         }
         return false;
     }
     if (!CheckPlayerControllerIdForConnection(conn, playerControllerId))
     {
         return false;
     }
     PlayerController playerController = null;
     GameObject gameObject = null;
     if (conn.GetPlayerController(playerControllerId, out playerController))
     {
         gameObject = playerController.gameObject;
     }
     if (gameObject != null)
     {
         if (LogFilter.logError)
         {
             Debug.Log("AddPlayer: player object already exists for playerControllerId of " + playerControllerId);
         }
         return false;
     }
     PlayerController player = new PlayerController(playerGameObject, playerControllerId);
     conn.SetPlayerController(player);
     identity.SetConnectionToClient(conn, player.playerControllerId);
     SetClientReady(conn);
     if (!this.SetupLocalPlayerForConnection(conn, identity, player))
     {
         if (LogFilter.logDebug)
         {
             Debug.Log(string.Concat(new object[] { "Adding new playerGameObject object netId: ", playerGameObject.GetComponent<NetworkIdentity>().netId, " asset ID ", playerGameObject.GetComponent<NetworkIdentity>().assetId }));
         }
         FinishPlayerForConnection(conn, identity, playerGameObject);
         if (identity.localPlayerAuthority)
         {
             identity.SetClientOwner(conn);
         }
     }
     return true;
 }