Esempio n. 1
0
        public CommandService(CommandServiceConfig config)
        {
            _caseSensitive  = config.CaseSensitiveCommands;
            _separatorChar  = config.SeparatorChar;
            _defaultRunMode = config.DefaultRunMode;

            _moduleLock      = new SemaphoreSlim(1, 1);
            _typedModuleDefs = new ConcurrentDictionary <Type, ModuleInfo>();
            _moduleDefs      = new HashSet <ModuleInfo>();
            _map             = new CommandMap(this);
            _typeReaders     = new ConcurrentDictionary <Type, ConcurrentDictionary <Type, TypeReader> >();

            _defaultTypeReaders = new ConcurrentDictionary <Type, TypeReader>();
            foreach (var type in PrimitiveParsers.SupportedTypes)
            {
                _defaultTypeReaders[type] = PrimitiveTypeReader.Create(type);
            }

            var entityTypeReaders = ImmutableList.CreateBuilder <Tuple <Type, Type> >();

            entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IMessage), typeof(MessageTypeReader <>)));
            entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IChannel), typeof(ChannelTypeReader <>)));
            entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IRole), typeof(RoleTypeReader <>)));
            entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IUser), typeof(UserTypeReader <>)));
            _entityTypeReaders = entityTypeReaders.ToImmutable();
        }
Esempio n. 2
0
        /// <summary>
        ///     Initializes a new <see cref="CommandService"/> class with the provided configuration.
        /// </summary>
        /// <param name="config">The configuration class.</param>
        /// <exception cref="InvalidOperationException">
        /// The <see cref="RunMode"/> cannot be set to <see cref="RunMode.Default"/>.
        /// </exception>
        public CommandService(CommandServiceConfig config)
        {
            _caseSensitive         = config.CaseSensitiveCommands;
            _throwOnError          = config.ThrowOnError;
            _ignoreExtraArgs       = config.IgnoreExtraArgs;
            _separatorChar         = config.SeparatorChar;
            _defaultRunMode        = config.DefaultRunMode;
            _quotationMarkAliasMap = (config.QuotationMarkAliasMap ?? new Dictionary <char, char>()).ToImmutableDictionary();
            if (_defaultRunMode == RunMode.Default)
            {
                throw new InvalidOperationException("The default run mode cannot be set to Default.");
            }

            _logManager          = new LogManager(config.LogLevel);
            _logManager.Message += async msg => await _logEvent.InvokeAsync(msg).ConfigureAwait(false);

            _cmdLogger = _logManager.CreateLogger("Command");

            _moduleLock      = new SemaphoreSlim(1, 1);
            _typedModuleDefs = new ConcurrentDictionary <Type, ModuleInfo>();
            _moduleDefs      = new HashSet <ModuleInfo>();
            _map             = new CommandMap(this);
            _typeReaders     = new ConcurrentDictionary <Type, ConcurrentDictionary <Type, TypeReader> >();

            _defaultTypeReaders = new ConcurrentDictionary <Type, TypeReader>();
            foreach (var type in PrimitiveParsers.SupportedTypes)
            {
                _defaultTypeReaders[type] = PrimitiveTypeReader.Create(type);
                _defaultTypeReaders[typeof(Nullable <>).MakeGenericType(type)] = NullableTypeReader.Create(type, _defaultTypeReaders[type]);
            }

            var tsreader = new TimeSpanTypeReader();

            _defaultTypeReaders[typeof(TimeSpan)]  = tsreader;
            _defaultTypeReaders[typeof(TimeSpan?)] = NullableTypeReader.Create(typeof(TimeSpan), tsreader);

            _defaultTypeReaders[typeof(string)] =
                new PrimitiveTypeReader <string>((string x, out string y) => { y = x; return(true); }, 0);

            var entityTypeReaders = ImmutableList.CreateBuilder <Tuple <Type, Type> >();

            entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IMessage), typeof(MessageTypeReader <>)));
            entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IChannel), typeof(ChannelTypeReader <>)));
            entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IRole), typeof(RoleTypeReader <>)));
            entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IUser), typeof(UserTypeReader <>)));
            _entityTypeReaders = entityTypeReaders.ToImmutable();
        }
Esempio n. 3
0
        public CommandService(CommandServiceConfig config)
        {
            _caseSensitive  = config.CaseSensitiveCommands;
            _throwOnError   = config.ThrowOnError;
            _separatorChar  = config.SeparatorChar;
            _defaultRunMode = config.DefaultRunMode;
            if (_defaultRunMode == RunMode.Default)
            {
                throw new InvalidOperationException("The default run mode cannot be set to Default.");
            }

            _logManager          = new LogManager(config.LogLevel);
            _logManager.Message += async msg => await _logEvent.InvokeAsync(msg).ConfigureAwait(false);

            _cmdLogger = _logManager.CreateLogger("Command");

            _moduleLock      = new SemaphoreSlim(1, 1);
            _typedModuleDefs = new ConcurrentDictionary <Type, ModuleInfo>();
            _moduleDefs      = new HashSet <ModuleInfo>();
            _map             = new CommandMap(this);
            _typeReaders     = new ConcurrentDictionary <Type, ConcurrentDictionary <Type, TypeReader> >();

            _defaultTypeReaders = new ConcurrentDictionary <Type, TypeReader>();
            foreach (var type in PrimitiveParsers.SupportedTypes)
            {
                _defaultTypeReaders[type] = PrimitiveTypeReader.Create(type);
            }

            var entityTypeReaders = ImmutableList.CreateBuilder <Tuple <Type, Type> >();

            entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IMessage), typeof(MessageTypeReader <>)));
            entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IChannel), typeof(ChannelTypeReader <>)));
            entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IRole), typeof(RoleTypeReader <>)));
            entityTypeReaders.Add(new Tuple <Type, Type>(typeof(IUser), typeof(UserTypeReader <>)));
            _entityTypeReaders = entityTypeReaders.ToImmutable();
        }