Ejemplo n.º 1
0
        public void TargetSubmitChoice(Mirror.NetworkConnection target, int type, bool succeeded)
        {
            if (!succeeded)
            {
                Debug.Log("Item grab did not succeed");
                return;
            }

            Local.SubmitChoice(type);
        }
Ejemplo n.º 2
0
 // for users to apply settings from their room player object to their in-game player object
 /// <summary>
 /// This is called on the server when it is told that a client has finished switching from the room scene to a game player scene.
 /// <para>When switching from the room, the room-player is replaced with a game-player object. This callback function gives an opportunity to apply state from the room-player to the game-player object.</para>
 /// </summary>
 /// <param name="conn">The connection of the player</param>
 /// <param name="roomPlayer">The room player object.</param>
 /// <param name="gamePlayer">The game player object.</param>
 /// <returns>False to not allow this player to replace the room player.</returns>
 public virtual bool OnRoomServerSceneLoadedForPlayer(NetworkConnection conn, GameObject roomPlayer, GameObject gamePlayer)
 {
     return(true);
 }
Ejemplo n.º 3
0
 internal void SetConnectionToClient(NetworkConnection conn)
 {
     m_ConnectionToClient = conn;
 }
Ejemplo n.º 4
0
 public static void SetClientNotReady(NetworkConnection conn)
 {
     InternalSetClientNotReady(conn);
 }
Ejemplo n.º 5
0
 // used during dispose after disconnect
 internal void ClearClientOwner()
 {
     m_ClientAuthorityOwner = null;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Callback used by the visibility system to determine if an observer (player) can see this object.
 /// <para>If this function returns true, the network connection will be added as an observer.</para>
 /// </summary>
 /// <param name="conn">Network connection of a player.</param>
 /// <returns>True if the player can see this object.</returns>
 public abstract bool OnCheckObserver(NetworkConnection conn);
Ejemplo n.º 7
0
 public static bool ReplacePlayerForConnection(NetworkConnection conn, GameObject player)
 {
     return InternalReplacePlayerForConnection(conn, player);
 }
Ejemplo n.º 8
0
 // use this to implicitly become ready
 public static bool AddPlayer(NetworkConnection readyConn) => AddPlayer(readyConn, null);
Ejemplo n.º 9
0
 /// <summary>
 /// Callback used by the visibility system to determine if an observer (player) can see this object.
 /// <para>If this function returns true, the network connection will be added as an observer.</para>
 /// </summary>
 /// <param name="conn">Network connection of a player.</param>
 /// <returns>True if the player can see this object.</returns>
 public virtual bool OnCheckObserver(NetworkConnection conn)
 {
     return(true);
 }
Ejemplo n.º 10
0
        internal static void OnSpawnPrefab(NetworkConnection conn, SpawnPrefabMessage msg)
        {
            if (msg.assetId == Guid.Empty)
            {
                Debug.LogError("OnObjSpawn netId: " + msg.netId + " has invalid asset Id");
                return;
            }
            if (LogFilter.Debug)
            {
                Debug.Log("Client spawn handler instantiating [netId:" + msg.netId + " asset ID:" + msg.assetId + " pos:" + msg.position + "]");
            }

            if (NetworkIdentity.spawned.TryGetValue(msg.netId, out NetworkIdentity localObject) && localObject != null)
            {
                // this object already exists (was in the scene), just apply the update to existing object
                localObject.Reset();
                ApplySpawnPayload(localObject, msg.position, msg.rotation, msg.scale, msg.payload, msg.netId);
                return;
            }

            if (GetPrefab(msg.assetId, out GameObject prefab))
            {
                GameObject obj = Object.Instantiate(prefab, msg.position, msg.rotation);
                if (LogFilter.Debug)
                {
                    Debug.Log("Client spawn handler instantiating [netId:" + msg.netId + " asset ID:" + msg.assetId + " pos:" + msg.position + " rotation: " + msg.rotation + "]");
                }

                localObject = obj.GetComponent <NetworkIdentity>();
                if (localObject == null)
                {
                    Debug.LogError("Client object spawned for " + msg.assetId + " does not have a NetworkIdentity");
                    return;
                }
                localObject.Reset();
                ApplySpawnPayload(localObject, msg.position, msg.rotation, msg.scale, msg.payload, msg.netId);
            }
            // lookup registered factory for type:
            else if (spawnHandlers.TryGetValue(msg.assetId, out SpawnDelegate handler))
            {
                GameObject obj = handler(msg.position, msg.assetId);
                if (obj == null)
                {
                    Debug.LogWarning("Client spawn handler for " + msg.assetId + " returned null");
                    return;
                }
                localObject = obj.GetComponent <NetworkIdentity>();
                if (localObject == null)
                {
                    Debug.LogError("Client object spawned for " + msg.assetId + " does not have a network identity");
                    return;
                }
                localObject.Reset();
                localObject.assetId = msg.assetId;
                ApplySpawnPayload(localObject, msg.position, msg.rotation, msg.scale, msg.payload, msg.netId);
            }
            else
            {
                Debug.LogError("Failed to spawn server object, did you forget to add it to the NetworkManager? assetId=" + msg.assetId + " netId=" + msg.netId);
            }
        }
Ejemplo n.º 11
0
 internal static void OnObjectDestroy(NetworkConnection conn, ObjectDestroyMessage msg)
 {
     DestroyObject(msg.netId);
 }
Ejemplo n.º 12
0
 internal static void OnObjectHide(NetworkConnection _, ObjectHideMessage msg)
 {
     DestroyObject(msg.netId);
 }
Ejemplo n.º 13
0
 /// <summary>
 /// This is called on the client when the client is finished loading a new networked scene.
 /// </summary>
 /// <param name="conn">The connection that finished loading a new networked scene.</param>
 public virtual void OnRoomClientSceneChanged(NetworkConnection conn)
 {
 }
Ejemplo n.º 14
0
 /// <summary>
 /// This is called on the client when disconnected from a server.
 /// </summary>
 /// <param name="conn">The connection that disconnected.</param>
 public virtual void OnRoomClientDisconnect(NetworkConnection conn)
 {
 }
 /// <summary>
 /// Called on client from OnClientAuthenticateInternal when a client needs to authenticate
 /// </summary>
 /// <param name="conn">Connection of the client.</param>
 public abstract void OnClientAuthenticate(NetworkConnection conn);
Ejemplo n.º 16
0
        internal static void SendSpawnMessage(NetworkIdentity identity, NetworkConnection conn)
        {
            if (identity.serverOnly)
            {
                return;
            }

            if (LogFilter.Debug)
            {
                Debug.Log("Server SendSpawnMessage: name=" + identity.name + " sceneId=" + identity.sceneId.ToString("X") + " netid=" + identity.netId);                  // for easier debugging
            }
            // 'identity' is a prefab that should be spawned
            if (identity.sceneId == 0)
            {
                SpawnPrefabMessage msg = new SpawnPrefabMessage
                {
                    netId    = identity.netId,
                    assetId  = identity.assetId,
                    position = identity.transform.position,
                    rotation = identity.transform.rotation,
                    scale    = identity.transform.localScale,

                    // serialize all components with initialState = true
                    payload = identity.OnSerializeAllSafely(true)
                };

                // conn is != null when spawning it for a client
                if (conn != null)
                {
                    conn.Send(msg);
                }
                // conn is == null when spawning it for the local player
                else
                {
                    SendToReady(identity, msg);
                }
            }
            // 'identity' is a scene object that should be spawned again
            else
            {
                SpawnSceneObjectMessage msg = new SpawnSceneObjectMessage
                {
                    netId    = identity.netId,
                    sceneId  = identity.sceneId,
                    position = identity.transform.position,
                    rotation = identity.transform.rotation,
                    scale    = identity.transform.localScale,

                    // include synch data
                    payload = identity.OnSerializeAllSafely(true)
                };

                // conn is != null when spawning it for a client
                if (conn != null)
                {
                    conn.Send(msg);
                }
                // conn is == null when spawning it for the local player
                else
                {
                    SendToReady(identity, msg);
                }
            }
        }
 public void ClientAccept(NetworkConnection conn)
 {
     OnClientAuthenticated.Invoke(conn);
 }
Ejemplo n.º 18
0
 static void OnData(NetworkConnection conn, ArraySegment <byte> data)
 {
     conn.TransportReceive(data);
 }
Ejemplo n.º 19
0
 static void OnData(NetworkConnection conn, byte[] data)
 {
     conn.TransportReceive(data);
 }
Ejemplo n.º 20
0
 public static bool AddPlayerForConnection(NetworkConnection conn, GameObject player)
 {
     return(InternalAddPlayerForConnection(conn, player));
 }
Ejemplo n.º 21
0
 internal static void ShowForConnection(NetworkIdentity identity, NetworkConnection conn)
 {
     if (conn.isReady)
         SendSpawnMessage(identity, conn);
 }
Ejemplo n.º 22
0
        public static void SetClientReady(NetworkConnection conn)
        {
            if (LogFilter.Debug)
            {
                Debug.Log("SetClientReadyInternal for conn:" + conn.connectionId);
            }

            if (conn.isReady)
            {
                if (LogFilter.Debug)
                {
                    Debug.Log("SetClientReady conn " + conn.connectionId + " already ready");
                }
                return;
            }

            if (conn.playerController == null)
            {
                // this is now allowed
                if (LogFilter.Debug)
                {
                    Debug.LogWarning("Ready with no player object");
                }
            }

            conn.isReady = true;

            if (conn is ULocalConnectionToClient localConnection)
            {
                if (LogFilter.Debug)
                {
                    Debug.Log("NetworkServer Ready handling ULocalConnectionToClient");
                }

                // Setup spawned objects for local player
                // Only handle the local objects for the first player (no need to redo it when doing more local players)
                // and don't handle player objects here, they were done above
                foreach (NetworkIdentity identity in NetworkIdentity.spawned.Values)
                {
                    // Need to call OnStartClient directly here, as it's already been added to the local object dictionary
                    // in the above SetLocalPlayer call
                    if (identity.gameObject != null)
                    {
                        bool visible = identity.OnCheckObserver(conn);
                        if (visible)
                        {
                            identity.AddObserver(conn);
                        }
                        if (!identity.isClient)
                        {
                            if (LogFilter.Debug)
                            {
                                Debug.Log("LocalClient.SetSpawnObject calling OnStartClient");
                            }
                            identity.OnStartClient();
                        }
                    }
                }
                return;
            }

            // Spawn/update all current server objects
            if (LogFilter.Debug)
            {
                Debug.Log("Spawning " + NetworkIdentity.spawned.Count + " objects for conn " + conn.connectionId);
            }

            conn.Send(new ObjectSpawnStartedMessage());

            foreach (NetworkIdentity identity in NetworkIdentity.spawned.Values)
            {
                if (identity == null)
                {
                    Debug.LogWarning("Invalid object found in server local object list (null NetworkIdentity).");
                    continue;
                }
                if (!identity.gameObject.activeSelf)
                {
                    continue;
                }

                if (LogFilter.Debug)
                {
                    Debug.Log("Sending spawn message for current server objects name='" + identity.name + "' netId=" + identity.netId);
                }

                bool visible = identity.OnCheckObserver(conn);
                if (visible)
                {
                    identity.AddObserver(conn);
                }
            }

            conn.Send(new ObjectSpawnFinishedMessage());
        }
Ejemplo n.º 23
0
 // default ready handler.
 static void OnClientReadyMessage(NetworkConnection conn, ReadyMessage msg)
 {
     if (LogFilter.Debug) Debug.Log("Default handler for ready message from " + conn);
     SetClientReady(conn);
 }
 /// <summary>
 /// Called on server from OnServerAuthenticateInternal when a client needs to authenticate
 /// </summary>
 /// <param name="conn">Connection to client.</param>
 public abstract void OnServerAuthenticate(NetworkConnection conn);
Ejemplo n.º 25
0
 internal void SetConnectionToServer(NetworkConnection conn)
 {
     m_ConnectionToServer = conn;
 }
 protected void ServerAccept(NetworkConnection conn)
 {
     OnServerAuthenticated.Invoke(conn);
 }
Ejemplo n.º 27
0
        public void RebuildObservers(bool initialize)
        {
            if (observers == null)
            {
                return;
            }

            bool changed = false;
            bool result  = false;
            HashSet <NetworkConnection> newObservers = new HashSet <NetworkConnection>();
            HashSet <NetworkConnection> oldObservers = new HashSet <NetworkConnection>(observers.Values);

            foreach (NetworkBehaviour comp in NetworkBehaviours)
            {
                result |= comp.OnRebuildObservers(newObservers, initialize);
            }
            if (!result)
            {
                // none of the behaviours rebuilt our observers, use built-in rebuild method
                if (initialize)
                {
                    foreach (KeyValuePair <int, NetworkConnection> kvp in NetworkServer.connections)
                    {
                        NetworkConnection conn = kvp.Value;
                        if (conn.isReady)
                        {
                            AddObserver(conn);
                        }
                    }

                    if (NetworkServer.localConnection != null && NetworkServer.localConnection.isReady)
                    {
                        AddObserver(NetworkServer.localConnection);
                    }
                }
                return;
            }

            // apply changes from rebuild
            foreach (NetworkConnection conn in newObservers)
            {
                if (conn == null)
                {
                    continue;
                }

                if (!conn.isReady)
                {
                    Debug.LogWarning("Observer is not ready for " + gameObject + " " + conn);
                    continue;
                }

                if (initialize || !oldObservers.Contains(conn))
                {
                    // new observer
                    conn.AddToVisList(this);
                    if (LogFilter.Debug)
                    {
                        Debug.Log("New Observer for " + gameObject + " " + conn);
                    }
                    changed = true;
                }
            }

            foreach (NetworkConnection conn in oldObservers)
            {
                if (!newObservers.Contains(conn))
                {
                    // removed observer
                    conn.RemoveFromVisList(this, false);
                    if (LogFilter.Debug)
                    {
                        Debug.Log("Removed Observer for " + gameObject + " " + conn);
                    }
                    changed = true;
                }
            }

            // special case for local client.
            if (initialize)
            {
                if (!newObservers.Contains(NetworkServer.localConnection))
                {
                    OnSetLocalVisibility(false);
                }
            }

            if (changed)
            {
                observers = newObservers.ToDictionary(conn => conn.connectionId, conn => conn);
            }
        }
 protected void ServerReject(NetworkConnection conn)
 {
     conn.Disconnect();
 }
    /// <summary>
    /// This is the mirror network message handler for moderator requests. It just decides what server action to perform based on what type of request it recieves
    /// </summary>
    /// <param name="conn"></param>
    /// <param name="request"></param>
    public static void FulfillModeratorRequest(Mirror.NetworkConnection conn, ClientConsole.ModeratorRequest request)
    {
        // If server can verify that the connection that sent the request is a moderator, then perform their request
        if (Config.IdPresent((((PlayerData)conn.authenticationData).id), modsPath))
        {
            if (request.id == 0)
            {
                Debug.Log("[Server] Recieved moderator request: " + ((PlayerData)conn.authenticationData).steamName + " requested to " + RequestTypeToString(request.requestType) + " " + request.name);
            }
            else
            {
                Debug.Log("[Server] Recieved moderator request: " + ((PlayerData)conn.authenticationData).steamName + " requested to " + RequestTypeToString(request.requestType) + " " + request.id);
            }



            switch (request.requestType)
            {
            case ClientConsole.ModeratorRequestType.ban:

                if (request.id == 0)
                {
                    ServerActions.Ban(request.name);
                }
                else
                {
                    ServerActions.Ban(request.id);
                }

                break;

            case ClientConsole.ModeratorRequestType.kick:

                if (request.id == 0)
                {
                    ServerActions.Kick(request.name);
                }
                else
                {
                    ServerActions.Kick(request.id);
                }

                break;

            case ClientConsole.ModeratorRequestType.mod:

                if (request.id == 0)
                {
                    ServerActions.Mod(request.name);
                }
                else
                {
                    ServerActions.Mod(request.id);
                }

                break;

            case ClientConsole.ModeratorRequestType.unban:

                if (request.id == 0)
                {
                    ServerActions.UnBan(request.name);
                }
                else
                {
                    ServerActions.UnBan(request.id);
                }

                break;

            case ClientConsole.ModeratorRequestType.unmod:

                if (request.id == 0)
                {
                    ServerActions.Unmod(request.name);
                }
                else
                {
                    ServerActions.Unmod(request.id);
                }

                break;

            default:
                break;
            }
        }
    }
Ejemplo n.º 30
0
 /// <summary>
 /// This allows customization of the creation of the GamePlayer object on the server.
 /// <para>This is only called for subsequent GamePlay scenes after the first one.</para>
 /// <para>See <see cref="OnRoomServerCreateGamePlayer(NetworkConnection, GameObject)"/> to customize the player object for the initial GamePlay scene.</para>
 /// </summary>
 /// <param name="conn">The connection the player object is for.</param>
 public virtual void OnRoomServerAddPlayer(NetworkConnection conn)
 {
     base.OnServerAddPlayer(conn);
 }