Esempio n. 1
0
        /// <summary>
        /// Invokes the named method on the corresponding behaviour on the server.
        /// When called on the server, the target method will be instantly invoked.
        /// </summary>
        /// <param name="methodName">The name of the method to call. The method should have the [Cmd] attribute.</param>
        /// <param name="args">The arguments to pass to the method.</param>
        protected void InvokeCMD(string methodName, params object[] args)
        {
            if (!NetObject.HasNetID)
            {
                JNet.Error($"Cannot call Cmd {methodName} because the object that {GetType().FullName} is on is not net spawned.");
                return;
            }

            MethodInfo method = GetCmd(methodName);

            if (method == null)
            {
                JNet.Error($"Could not find valid method with Cmd tag {methodName}. Check tags, name spelling and regenerate netcode!");
                return;
            }

            if (!IsServer && !IsClient)
            {
                JNet.Error($"Not on client or server, can't invoke cmd {methodName}");
                return;
            }

            if (!HasAuthority)
            {
                JNet.Error($"There is currently no authority over this object {name}. Must call from on a client that has object ownership, or on the server. Current owner: {NetObject.OwnerID}");
                return;
            }

            methodName = methodName.Trim().ToLower();
            byte methodID = CustomGeneratedBehaviour.GetMethodID(methodName);

            if (!JNet.IsServer)
            {
                CheckDeliveryMethod();

                NetOutgoingMessage msg = JNet.GetClient().CreateMessage(Internal.JDataType.RMC);
                msg.Write(NetObject.NetID);
                msg.Write(this.BehaviourID);
                msg.Write(methodID);

                bool worked = ArgsToMsg(method, args, msg);
                if (!worked)
                {
                    JNet.Error("Did not invoke Cmd due to error.");
                    return;
                }

                var client = JNet.GetClient();
                client.Send(msg, RMCDeliveryMethod, 24);
            }
            else
            {
                // Invoke directly.
                var m = CustomGeneratedBehaviour.GetCmd(methodName);
                m.Invoke(this, args);
            }
        }