Exemple #1
0
        public static string BuildToString(string command, IEnumerable <CommandParameter> parameter, IEnumerable <CommandOption> options)
        {
            if (string.IsNullOrWhiteSpace(command))
            {
                throw new ArgumentNullException(nameof(command));
            }
            if (!CommandMatch.IsMatch(command))
            {
                throw new ArgumentException("Invalid command characters", nameof(command));
            }

            var strb = new StringBuilder(Ts3String.Escape(command));

            foreach (var param in parameter)
            {
                strb.Append(' ').Append(param.QueryString);
            }

            foreach (var option in options)
            {
                strb.Append(option.Value);
            }

            return(strb.ToString());
        }
Exemple #2
0
 public ParameterConverter(string value)
 {
     QueryValue = Ts3String.Escape(value);
 }
 [DebuggerStepThrough] public static string Serialize(string value)
 {
     return(Ts3String.Escape(value));
 }
Exemple #4
0
 public static string DeserializeString(string v) => Ts3String.Unescape(v);
        /// <summary>Builds the command from its parameters and returns the query-like command.</summary>
        /// <param name="command">The command name.</param>
        /// <param name="parameter">The parameter to be added to this command.</param>
        /// <returns>The formatted query-like command.</returns>
        /// <exception cref="ArgumentException">When a command is null or not valid.</exception>
        /// <exception cref="ArgumentOutOfRangeException">When multiple <see cref="CommandMultiParameter"/> are added but have different array lengths.</exception>
        public static string BuildToString(string command, IEnumerable <ICommandPart> parameter)
        {
            if (string.IsNullOrWhiteSpace(command))
            {
                throw new ArgumentNullException(nameof(command));
            }
            if (!CommandMatch.IsMatch(command))
            {
                throw new ArgumentException("Invalid command characters", nameof(command));
            }

            var strb = new StringBuilder(Ts3String.Escape(command));
            List <CommandMultiParameter> multiParamList = null;
            List <CommandOption>         optionList     = null;

            foreach (var param in parameter)
            {
                switch (param.Type)
                {
                case CommandPartType.SingleParameter:
                    var singleParam = (CommandParameter)param;
                    strb.Append(' ').Append(singleParam.Key).Append('=').Append(singleParam.Value);
                    break;

                case CommandPartType.MultiParameter:
                    if (multiParamList is null)
                    {
                        multiParamList = new List <CommandMultiParameter>();
                    }
                    multiParamList.Add((CommandMultiParameter)param);
                    break;

                case CommandPartType.Option:
                    if (optionList is null)
                    {
                        optionList = new List <CommandOption>();
                    }
                    optionList.Add((CommandOption)param);
                    break;

                default:
                    throw Util.UnhandledDefault(param.Type);
                }
            }

            if (multiParamList != null)
            {
                // Safety check
                int matrixLength = multiParamList[0].Values.Length;
                foreach (var param in multiParamList)
                {
                    if (param.Values.Length != matrixLength)
                    {
                        throw new ArgumentOutOfRangeException(nameof(parameter), "All multiparam key-value pairs must have the same length");
                    }
                }

                for (int i = 0; i < matrixLength; i++)
                {
                    foreach (var param in multiParamList)
                    {
                        strb.Append(' ').Append(param.Key).Append('=').Append(param.Values[i]);
                    }
                    strb.Append('|');
                }

                strb.Length--;
            }

            if (optionList != null)
            {
                foreach (var option in optionList)
                {
                    strb.Append(option.Value);
                }
            }

            return(strb.ToString());
        }