Exemplo n.º 1
0
        public async Task <Dvar <T> > GetDvarAsync <T>(IRConConnection connection, string dvarName, T fallbackValue = default)
        {
            string[] lineSplit;

            try
            {
                lineSplit = await connection.SendQueryAsync(StaticHelpers.QueryType.GET_DVAR, dvarName);
            }
            catch
            {
                if (fallbackValue == null)
                {
                    throw;
                }

                lineSplit = new string[0];
            }

            string response = string.Join('\n', lineSplit).TrimEnd('\0');
            var    match    = Regex.Match(response, Configuration.Dvar.Pattern);

            if (response.Contains("Unknown command") ||
                !match.Success)
            {
                if (fallbackValue != null)
                {
                    return(new Dvar <T>()
                    {
                        Name = dvarName,
                        Value = fallbackValue
                    });
                }

                throw new DvarException(Utilities.CurrentLocalization.LocalizationIndex["SERVER_ERROR_DVAR"].FormatExt(dvarName));
            }

            string value        = match.Groups[Configuration.Dvar.GroupMapping[ParserRegex.GroupType.RConDvarValue]].Value;
            string defaultValue = match.Groups[Configuration.Dvar.GroupMapping[ParserRegex.GroupType.RConDvarDefaultValue]].Value;
            string latchedValue = match.Groups[Configuration.Dvar.GroupMapping[ParserRegex.GroupType.RConDvarLatchedValue]].Value;

            string removeTrailingColorCode(string input) => Regex.Replace(input, @"\^7$", "");

            value        = removeTrailingColorCode(value);
            defaultValue = removeTrailingColorCode(defaultValue);
            latchedValue = removeTrailingColorCode(latchedValue);

            return(new Dvar <T>()
            {
                Name = dvarName,
                Value = string.IsNullOrEmpty(value) ? default : (T)Convert.ChangeType(value, typeof(T)),
                        DefaultValue = string.IsNullOrEmpty(defaultValue) ? default : (T)Convert.ChangeType(defaultValue, typeof(T)),
                                       LatchedValue = string.IsNullOrEmpty(latchedValue) ? default : (T)Convert.ChangeType(latchedValue, typeof(T)),
                                                      Domain = match.Groups[Configuration.Dvar.GroupMapping[ParserRegex.GroupType.RConDvarDomain]].Value
            });
Exemplo n.º 2
0
        public async Task <string[]> ExecuteCommandAsync(IRConConnection connection, string command)
        {
            var response = await connection.SendQueryAsync(StaticHelpers.QueryType.COMMAND, command);

            return(response.Skip(1).ToArray());
        }
Exemplo n.º 3
0
        public async Task <string[]> ExecuteCommandAsync(IRConConnection connection, string command)
        {
            var response = await connection.SendQueryAsync(StaticHelpers.QueryType.COMMAND, command);

            return(response.Where(item => item != Configuration.CommandPrefixes.RConResponse).ToArray());
        }