예제 #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
            });
예제 #2
0
        public void Setup()
        {
            serviceProvider = new ServiceCollection().BuildBase().BuildServiceProvider();

            fakeLogger         = serviceProvider.GetRequiredService <ILogger>();
            fakeManager        = serviceProvider.GetRequiredService <IManager>();
            fakeRConConnection = serviceProvider.GetRequiredService <IRConConnection>();
            fakeRConParser     = serviceProvider.GetRequiredService <IRConParser>();
            mockEventHandler   = serviceProvider.GetRequiredService <EventHandlerMock>();
            appConfig          = serviceProvider.GetRequiredService <ApplicationConfiguration>();

            var rconConnectionFactory = serviceProvider.GetRequiredService <IRConConnectionFactory>();

            A.CallTo(() => rconConnectionFactory.CreateConnection(A <string> .Ignored, A <int> .Ignored, A <string> .Ignored))
            .Returns(fakeRConConnection);

            A.CallTo(() => fakeRConParser.Configuration)
            .Returns(ConfigurationGenerators.CreateRConParserConfiguration(serviceProvider.GetRequiredService <IParserRegexFactory>()));

            A.CallTo(() => fakeManager.AddEvent(A <GameEvent> .Ignored))
            .Invokes((fakeCall) => mockEventHandler.HandleEvent(fakeManager, fakeCall.Arguments[0] as GameEvent));
        }
예제 #3
0
        public async Task <string[]> ExecuteCommandAsync(IRConConnection connection, string command)
        {
            var response = await connection.SendQueryAsync(StaticHelpers.QueryType.COMMAND, command);

            return(response.Skip(1).ToArray());
        }
예제 #4
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());
        }