Exemplo n.º 1
0
 /// <summary>
 /// Gets the method parameter value.
 /// </summary>
 /// <param name="parameter">Parameter reflexion into</param>
 /// <param name="index">Index of the parameter as seen by player</param>
 /// <param name="player">Player bound to</param>
 /// <param name="rawValues"></param>
 /// <returns>Parsed value or DBNull if could not be barsed</returns>
 private static object GetValue(ParameterInfo parameter, int index, Player player, List <string> rawValues)
 {
     if (index < rawValues.Count)
     {
         string rawValue = rawValues[index];
         object value    = GMUtils.TryParse(parameter.ParameterType, rawValue);
         if (value == null)
         {
             player.SendClientMessage(string.Concat(
                                          CommandToString(parameter), " : Unexpected value for ", parameter.Name), ChatLogFilters.CHATLOGFILTERS_CSR_TELL_RECEIVE);
             return(DBNull.Value);
         }
         return(value);
     }
     else if (parameter.DefaultValue != DBNull.Value)
     {
         return(parameter.DefaultValue);
     }
     else
     {
         player.SendClientMessage(string.Concat(
                                      CommandToString(parameter), " : ", parameter.Name, " is required"), ChatLogFilters.CHATLOGFILTERS_CSR_TELL_RECEIVE);
         return(DBNull.Value);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Invokes a command's method for given player and raw parameter values.
        /// </summary>
        /// <param name="method">Method to invoke</param>
        /// <param name="player">Player that invoked this command</param>
        /// <param name="rawValues">Raw values given by player</param>
        /// <returns>False if method returned false, true otherwise.</returns>
        private static bool Invoke(MethodInfo method, Player player, List <string> rawValues)
        {
            ParameterInfo[] parameters = method.GetParameters();
            object[]        values     = new object[parameters.Length];

            // First parameter is always player
            values[0] = player;

            // Second parameter can be target
            bool hasTargetParam = HasTargetParameter(parameters); // Second can be target
            int  startIndex     = hasTargetParam ? 2 : 1;

            if (hasTargetParam)
            {
                values[1] = GMUtils.GetTargetOrMe(player);
                if (!parameters[1].ParameterType.IsAssignableFrom(values[0].GetType()))
                {
                    player.SendClientMessage(string.Concat(
                                                 CommandToString(parameters[1]), " : You must target a ", parameters[1].ParameterType.Name.ToLowerInvariant()), ChatLogFilters.CHATLOGFILTERS_CSR_TELL_RECEIVE);
                    return(true);
                }
            }

            // Other parameters
            for (int i = startIndex; i < values.Length; i++)
            {
                values[i] = GetValue(parameters[i], i - startIndex, player, rawValues);
                if (values[i] == DBNull.Value)
                {
                    return(true);
                }
            }
            if (values.Length - startIndex < rawValues.Count)
            {
                player.SendClientMessage(string.Concat(
                                             CommandToString(parameters[1]), " : too many parameters"), ChatLogFilters.CHATLOGFILTERS_CSR_TELL_RECEIVE);
                return(true);
            }

            rawValues.Clear(); // why not

            try
            {
                object result = method.Invoke(null, values);
                if (result is bool)
                {
                    return((bool)result);
                }
                else
                {
                    return(true);
                }
            }
            catch (TargetInvocationException e)
            {
                Exception inner = e.InnerException;
                Log.Error("CommandsBuilder", string.Concat(CommandToString(parameters[0]), " - ", inner.ToString()));
                player.SendClientMessage(string.Concat(
                                             CommandToString(parameters[0]), " : ", inner.GetType().Name,
                                             string.IsNullOrEmpty(inner.Message) ? "": " - " + inner.Message), ChatLogFilters.CHATLOGFILTERS_CSR_TELL_RECEIVE);
                return(true);
            }
        }