Exemplo n.º 1
0
        public void SetTrigger(int hash)
        {
            var animMsg = new AnimationTriggerMessage();

            animMsg.netId = netId;
            animMsg.hash  = hash;

            if (hasAuthority && localPlayerAuthority)
            {
                if (NetworkClient.allClients.Count > 0)
                {
                    var client = ClientScene.readyConnection;
                    if (client != null)
                    {
                        client.Send((short)MsgType.AnimationTrigger, animMsg);
                    }
                }
                return;
            }

            if (isServer && !localPlayerAuthority)
            {
                NetworkServer.SendToReady(gameObject, (short)MsgType.AnimationTrigger, animMsg);
            }
        }
Exemplo n.º 2
0
        protected void SendRPCInternal(Type invokeClass, string rpcName, NetworkWriter writer, int channelId)
        {
            // this was in Weaver before
            if (!NetworkServer.active)
            {
                Debug.LogError("RPC Function " + rpcName + " called on Client.");
                return;
            }
            // This cannot use NetworkServer.active, as that is not specific to this object.
            if (!isServer)
            {
                Debug.LogWarning("ClientRpc " + rpcName + " called on un-spawned object: " + name);
                return;
            }

            // construct the message
            RpcMessage message = new RpcMessage
            {
                netId          = netId,
                componentIndex = ComponentIndex,
                functionHash   = (invokeClass + ":" + rpcName).GetStableHashCode(), // type+func so Inventory.RpcUse != Equipment.RpcUse
                payload        = writer.ToArray()
            };

            NetworkServer.SendToReady(netIdentity, message, channelId);
        }
Exemplo n.º 3
0
        internal static void OnAnimationTriggerServerMessage(NetworkMessage netMsg)
        {
            AnimationTriggerMessage msg = new AnimationTriggerMessage();

            netMsg.ReadMessage(msg);
            if (LogFilter.logDev)
            {
                Debug.Log("OnAnimationTriggerMessage for netId=" + msg.netId + " conn=" + netMsg.conn);
            }

            GameObject go = NetworkServer.FindLocalObject(msg.netId);

            if (go == null)
            {
                return;
            }
            NetworkAnimator animSync = go.GetComponent <NetworkAnimator>();

            if (animSync != null)
            {
                animSync.HandleAnimTriggerMsg(msg.hash);

                NetworkServer.SendToReady(go, (short)MsgType.AnimationTrigger, msg);
            }
        }
Exemplo n.º 4
0
        protected void SendRPCInternal(Type invokeClass, string rpcName, NetworkWriter writer, int channelId)
        {
            // this was in Weaver before
            if (!NetworkServer.active)
            {
                Debug.LogError("RPC Function " + rpcName + " called on Client.");
                return;
            }
            // This cannot use NetworkServer.active, as that is not specific to this object.
            if (!isServer)
            {
                Debug.LogWarning("ClientRpc " + rpcName + " called on un-spawned object: " + name);
                return;
            }

            // construct the message
            RpcMessage message = new RpcMessage
            {
                netId          = netId,
                componentIndex = ComponentIndex,
                functionHash   = GetMethodHash(invokeClass, rpcName), // type+func so Inventory.RpcUse != Equipment.RpcUse
                payload        = writer.ToArraySegment()              // segment to avoid reader allocations
            };

            NetworkServer.SendToReady(netIdentity, message, channelId);
#if MIRROR_PROFILING
            NetworkProfiler.RecordMessage(NetworkDirection.Outgoing, typeof(RpcMessage), $"{invokeClass.GetType()}.{rpcName}", 1);
#endif
        }
Exemplo n.º 5
0
        void OnServerReadyToBeginMessage(NetworkMessage netMsg)
        {
            if (LogFilter.logDebug)
            {
                Debug.Log("NetworkLobbyManager OnServerReadyToBeginMessage");
            }
            netMsg.ReadMessage(s_ReadyToBeginMessage);

            PlayerController lobbyController;

            if (!Utils.GetPlayerController(netMsg.conn, (short)s_ReadyToBeginMessage.slotId, out lobbyController))
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("NetworkLobbyManager OnServerReadyToBeginMessage invalid playerControllerId " + s_ReadyToBeginMessage.slotId);
                }
                return;
            }

            // set this player ready
            var lobbyPlayer = lobbyController.gameObject.GetComponent <NetworkLobbyPlayer>();

            lobbyPlayer.readyToBegin = s_ReadyToBeginMessage.readyState;

            // tell every player that this player is ready
            var outMsg = new LobbyReadyToBeginMessage();

            outMsg.slotId     = lobbyPlayer.slot;
            outMsg.readyState = s_ReadyToBeginMessage.readyState;
            NetworkServer.SendToReady(null, UnityEngine.Networking.MsgType.LobbyReadyToBegin, outMsg);

            // maybe start the game
            CheckReadyToBegin();
        }
Exemplo n.º 6
0
        protected void SendRPCInternal(Type invokeClass, string rpcName, NetworkWriter writer, int channelId, bool excludeOwner)
        {
            // this was in Weaver before
            if (!NetworkServer.active)
            {
                logger.LogError("RPC Function " + rpcName + " called on Client.");
                return;
            }
            // This cannot use NetworkServer.active, as that is not specific to this object.
            if (!isServer)
            {
                logger.LogWarning("ClientRpc " + rpcName + " called on un-spawned object: " + name);
                return;
            }

            // construct the message
            RpcMessage message = new RpcMessage
            {
                netId          = netId,
                componentIndex = ComponentIndex,
                // type+func so Inventory.RpcUse != Equipment.RpcUse
                functionHash = RemoteCallHelper.GetMethodHash(invokeClass, rpcName),
                // segment to avoid reader allocations
                payload = writer.ToArraySegment()
            };

            // The public facing parameter is excludeOwner in [ClientRpc]
            // so we negate it here to logically align with SendToReady.
            bool includeOwner = !excludeOwner;

            NetworkServer.SendToReady(netIdentity, message, includeOwner, channelId);
        }
Exemplo n.º 7
0
        internal static void OnAnimationParametersServerMessage(NetworkMessage netMsg)
        {
            AnimationParametersMessage msg = new AnimationParametersMessage();

            netMsg.ReadMessage(msg);

            if (LogFilter.Debug)
            {
                Debug.Log("OnAnimationParametersMessage for netId=" + msg.netId + " conn=" + netMsg.conn);
            }

            GameObject go = NetworkServer.FindLocalObject(msg.netId);

            if (go == null)
            {
                return;
            }
            NetworkAnimator animSync = go.GetComponent <NetworkAnimator>();

            if (animSync != null)
            {
                NetworkReader reader = new NetworkReader(msg.parameters);
                animSync.HandleAnimParamsMsg(msg, reader);
                NetworkServer.SendToReady(go, (short)MsgType.AnimationParameters, msg);
            }
        }
Exemplo n.º 8
0
        void SendMsg(Operation op, int itemIndex, T item)
        {
            if (m_Behaviour == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("SyncList not initialized");
                }
                return;
            }

            var uv = m_Behaviour.GetComponent <NetworkIdentity>();

            if (uv == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("SyncList no NetworkIdentity");
                }
                return;
            }

            if (!uv.isServer)
            {
                // object is not spawned yet, so no need to send updates.
                return;
            }


            // construct and send message
            SyncListMessage message = new SyncListMessage();

            message.netId        = uv.netId;
            message.syncListHash = m_CmdHash;

            NetworkWriter writer = new NetworkWriter();

            writer.Write((byte)op);
            writer.WritePackedUInt32((uint)itemIndex);
            SerializeItem(writer, item);

            message.payload = writer.ToArray();

            NetworkServer.SendToReady(uv.gameObject, (short)MsgType.SyncList, message);

            // ensure it is invoked on host
            if (m_Behaviour.isServer && m_Behaviour.isClient && m_Callback != null)
            {
                m_Callback.Invoke(op, itemIndex);
            }
        }
Exemplo n.º 9
0
 void SendMessage(short type, MessageBase msg)
 {
     if (isServer)
     {
         NetworkServer.SendToReady(gameObject, type, msg);
     }
     else
     {
         if (ClientScene.readyConnection != null)
         {
             ClientScene.readyConnection.Send(type, msg);
         }
     }
 }
Exemplo n.º 10
0
        // invoked by unity runtime immediately after the regular "Update()" function.
        internal void UNetUpdate()
        {
            // serialize all the dirty components and send (if any were dirty)
            byte[] payload = OnSerializeAllSafely(false);
            if (payload != null)
            {
                // construct message and send
                UpdateVarsMessage message = new UpdateVarsMessage();
                message.netId   = netId;
                message.payload = payload;

                NetworkServer.SendToReady(gameObject, (short)MsgType.UpdateVars, message);
            }
        }
Exemplo n.º 11
0
        // invoked by unity runtime immediately after the regular "Update()" function.
        internal void UNetUpdate()
        {
            // serialize all the dirty components and send (if any were dirty)
            NetworkWriter writer = new NetworkWriter();

            if (OnSerializeAllSafely(m_NetworkBehaviours, writer, false))
            {
                // construct message and send
                UpdateVarsMessage message = new UpdateVarsMessage();
                message.netId   = netId;
                message.payload = writer.ToArray();

                NetworkServer.SendToReady(gameObject, (short)MsgType.UpdateVars, message);
            }
        }
Exemplo n.º 12
0
        protected void SendEventInternal(Type invokeClass, string eventName, NetworkWriter writer, int channelId)
        {
            if (!NetworkServer.active)
            {
                Debug.LogWarning("SendEvent no server?");
                return;
            }

            // construct the message
            SyncEventMessage message = new SyncEventMessage();

            message.netId          = netId;
            message.componentIndex = ComponentIndex;
            message.functionHash   = (invokeClass + ":" + eventName).GetStableHashCode(); // type+func so Inventory.RpcUse != Equipment.RpcUse
            message.payload        = writer.ToArray();

            NetworkServer.SendToReady(netIdentity, (short)MsgType.SyncEvent, message, channelId);
        }
Exemplo n.º 13
0
        protected void SendEventInternal(int eventHash, NetworkWriter writer, int channelId, string eventName)
        {
            if (!NetworkServer.active)
            {
                Debug.LogWarning("SendEvent no server?");
                return;
            }

            // construct the message
            SyncEventMessage message = new SyncEventMessage();

            message.netId          = netId;
            message.componentIndex = ComponentIndex;
            message.eventHash      = eventHash;
            message.payload        = writer.ToArray();

            NetworkServer.SendToReady(gameObject, (short)MsgType.SyncEvent, message, channelId);
        }
Exemplo n.º 14
0
        protected void SendEventInternal(Type invokeClass, string eventName, NetworkWriter writer, int channelId)
        {
            if (!NetworkServer.active)
            {
                Debug.LogWarning("SendEvent no server?");
                return;
            }

            // construct the message
            SyncEventMessage message = new SyncEventMessage
            {
                netId          = netId,
                componentIndex = ComponentIndex,
                functionHash   = GetMethodHash(invokeClass, eventName),    // type+func so Inventory.RpcUse != Equipment.RpcUse
                payload        = new ArraySegment <byte>(writer.ToArray()) // segment to avoid reader allocations
            };

            NetworkServer.SendToReady(netIdentity, message, channelId);
        }
Exemplo n.º 15
0
        protected void SendRPCInternal(Type invokeClass, string rpcName, NetworkWriter writer, int channelId)
        {
            // This cannot use NetworkServer.active, as that is not specific to this object.
            if (!isServer)
            {
                Debug.LogWarning("ClientRpc " + rpcName + " called on un-spawned object: " + name);
                return;
            }

            // construct the message
            RpcMessage message = new RpcMessage();

            message.netId          = netId;
            message.componentIndex = ComponentIndex;
            message.functionHash   = (invokeClass + ":" + rpcName).GetStableHashCode(); // type+func so Inventory.RpcUse != Equipment.RpcUse
            message.payload        = writer.ToArray();

            NetworkServer.SendToReady(netIdentity, (short)MsgType.Rpc, message, channelId);
        }
Exemplo n.º 16
0
        protected void SendRPCInternal(int rpcHash, NetworkWriter writer, int channelId, string rpcName)
        {
            // This cannot use NetworkServer.active, as that is not specific to this object.
            if (!isServer)
            {
                Debug.LogWarning("ClientRpc call on un-spawned object");
                return;
            }

            // construct the message
            RpcMessage message = new RpcMessage();

            message.netId          = netId;
            message.componentIndex = ComponentIndex;
            message.rpcHash        = rpcHash;
            message.payload        = writer.ToArray();

            NetworkServer.SendToReady(gameObject, (short)MsgType.Rpc, message, channelId);
        }
Exemplo n.º 17
0
        // invoked by unity runtime immediately after the regular "Update()" function.
        internal void UNetUpdate()
        {
            // SendToReady sends to all observers. no need to serialize if we
            // don't have any.
            if (observers == null || observers.Count == 0)
            {
                return;
            }

            // serialize all the dirty components and send (if any were dirty)
            byte[] payload = OnSerializeAllSafely(false);
            if (payload != null)
            {
                // construct message and send
                UpdateVarsMessage message = new UpdateVarsMessage();
                message.netId   = netId;
                message.payload = payload;

                NetworkServer.SendToReady(this, (short)MsgType.UpdateVars, message);
            }
        }
Exemplo n.º 18
0
        protected void SendEventInternal(Type invokeClass, string eventName, NetworkWriter writer, int channelId)
        {
            if (!NetworkServer.active)
            {
                Debug.LogWarning("SendEvent no server?");
                return;
            }

            // construct the message
            SyncEventMessage message = new SyncEventMessage
            {
                netId          = netId,
                componentIndex = ComponentIndex,
                functionHash   = GetMethodHash(invokeClass, eventName), // type+func so Inventory.RpcUse != Equipment.RpcUse
                payload        = writer.ToArraySegment()                // segment to avoid reader allocations
            };

            NetworkServer.SendToReady(netIdentity, message, channelId);
#if MIRROR_PROFILING
            NetworkProfiler.RecordMessage(NetworkDirection.Outgoing, typeof(SyncEventMessage), $"{invokeClass.GetType()}.{eventName}", 1);
#endif
        }
Exemplo n.º 19
0
        public override void OnServerRemovePlayer(NetworkConnection conn, PlayerController player)
        {
            var  playerControllerId = player.playerControllerId;
            byte slot = player.gameObject.GetComponent <NetworkLobbyPlayer>().slot;

            lobbySlots[slot] = null;
            base.OnServerRemovePlayer(conn, player);

            for (int i = 0; i < lobbySlots.Length; i++)
            {
                var lobbyPlayer = lobbySlots[i];
                if (lobbyPlayer != null)
                {
                    lobbyPlayer.GetComponent <NetworkLobbyPlayer>().readyToBegin = false;

                    s_LobbyReadyToBeginMessage.slotId     = lobbyPlayer.slot;
                    s_LobbyReadyToBeginMessage.readyState = false;
                    NetworkServer.SendToReady(null, UnityEngine.Networking.MsgType.LobbyReadyToBegin, s_LobbyReadyToBeginMessage);
                }
            }

            OnLobbyServerPlayerRemoved(conn, playerControllerId);
        }