コード例 #1
0
 public void SetCurrentPath(MetadataPath path)
 {
     foreach (var provider in this._providers)
     {
         provider.SetCurrentPath(path);
     }
 }
コード例 #2
0
        public override int GetHashCode()
        {
            int hash = 1;

            if (TensorName.Length != 0)
            {
                hash ^= TensorName.GetHashCode();
            }
            if (MetadataPath.Length != 0)
            {
                hash ^= MetadataPath.GetHashCode();
            }
            if (BookmarksPath.Length != 0)
            {
                hash ^= BookmarksPath.GetHashCode();
            }
            hash ^= tensorShape_.GetHashCode();
            if (sprite_ != null)
            {
                hash ^= Sprite.GetHashCode();
            }
            if (TensorPath.Length != 0)
            {
                hash ^= TensorPath.GetHashCode();
            }
            if (_unknownFields != null)
            {
                hash ^= _unknownFields.GetHashCode();
            }
            return(hash);
        }
コード例 #3
0
        public async Task BuildAsync(params Assembly[] assemblies)
        {
            var createdModules = new List <ModuleInfo>();
            var modules        = assemblies.SelectMany(t => t.GetTypes())
                                 .Where(IsValidModuleDefinition)
                                 .ToImmutableArray();

            foreach (var module in modules)
            {
                var currentPath = new MetadataPath(module.FullName);
                this._metadataProvider.SetCurrentPath(currentPath);

                var group = this._metadataProvider.GetModuleValue(m => m.Group);

                if (group.IsEmpty())
                {
                    group = "";
                }

                var moduleInfo = await this._commandService.CreateModuleAsync(group,
                                                                              b => this.BuildModule(b, module.GetTypeInfo(), currentPath))
                                 .ConfigureAwait(false);

                createdModules.Add(moduleInfo);
            }

            if (createdModules.Count > 0)
            {
                this.Modules = this.Modules.Union(createdModules).ToImmutableList();
            }
        }
コード例 #4
0
        private void BuildModule(ModuleBuilder builder, TypeInfo moduleType, MetadataPath currentPath)
        {
            var attributes = moduleType.GetCustomAttributes()
                             .ToImmutableArray();

            var preconditions = attributes.Where(a => a is PreconditionAttribute);

            foreach (var precondition in preconditions)
            {
                builder.AddPrecondition(precondition as PreconditionAttribute);
            }

            builder.AddAttributes(attributes.Where(a => !(a is PreconditionAttribute)).ToArray());

            builder.WithName(this._metadataProvider.GetModuleValue(m => m.Name))
            .WithSummary(this._metadataProvider.GetModuleValue(m => m.Summary))
            .WithRemarks(this._metadataProvider.GetModuleValue(m => m.Remarks))
            .AddAliases(this._metadataProvider.GetModuleValue(m => m.Aliases) ?? new String[0]);

            if (builder.Name.IsEmpty())
            {
                builder.Name = moduleType.Name;
            }

            var commands = moduleType.DeclaredMethods.Where(IsValidCommandDefinition);

            foreach (var command in commands)
            {
                currentPath.CurrentCommand  = command.Name;
                currentPath.CurrentArgument = null;
                this._metadataProvider.SetCurrentPath(currentPath);

                this.BuildCommand(builder, moduleType, command, currentPath);
            }
        }
コード例 #5
0
ファイル: JsonProvider.cs プロジェクト: Nikey646/Kuuhaku
 public void SetCurrentPath(MetadataPath path)
 {
     this._metadataPath = path;
 }
コード例 #6
0
        private void BuildCommand(ModuleBuilder builder, TypeInfo moduleType, MethodInfo method,
                                  MetadataPath currentPath)
        {
            var name    = this._metadataProvider.GetCommandValue(c => c.Name);
            var command = this._metadataProvider.GetCommandValue(c => c.Command);

            var isDefault = command == null && !builder.Aliases[0].IsEmpty();

            // This command is not configured properly
            if (!isDefault && command.IsEmpty())
            {
                return;
            }

            async Task <IResult> ExecuteCommand(ICommandContext context, Object[] args, IServiceProvider services,
                                                CommandInfo cmd)
            {
                var instance = (IModule)services.GetRequiredService(moduleType);

                instance.SetContext(context as KuuhakuCommandContext);

                try
                {
                    instance.BeforeExecute(cmd);
                    var task = method.Invoke(instance, args) as Task ?? Task.CompletedTask;
                    if (task is Task <RuntimeResult> resultTask)
                    {
                        return(await resultTask);
                    }
                    await task;
                    return(ExecuteResult.FromSuccess());
                }
                finally
                {
                    instance.AfterExecute(cmd);
                    (instance as IDisposable)?.Dispose();
                    if (instance is IAsyncDisposable disposable)
                    {
                        await disposable.DisposeAsync();
                    }
                }
            }

            void CreateCommand(CommandBuilder builder)
            {
                var attributes = method.GetCustomAttributes()
                                 .ToImmutableArray();

                var preconditions = attributes.Where(a => a is PreconditionAttribute);

                foreach (var precondition in preconditions)
                {
                    builder.AddPrecondition(precondition as PreconditionAttribute);
                }

                builder.AddAttributes(attributes.Where(a => !(a is PreconditionAttribute)).ToArray());

                // TODO: Permissions
                // TODO: Ratelimiting
                // TODO: Generic Precondition Values?

                builder.WithPriority(this._metadataProvider.GetCommandValue(c => c.Priority))
                .WithSummary(this._metadataProvider.GetCommandValue(c => c.Summary))
                .WithRemarks(this._metadataProvider.GetCommandValue(c => c.Remarks))
                .AddAliases(this._metadataProvider.GetCommandValue(c => c.Aliases) ?? new String[0]);

                if (builder.Name.IsEmpty())
                {
                    builder.Name = method.Name.Replace("Async", "");
                }

                var parameters = method.GetParameters();

                for (var i = 0; i < parameters.Length; i++)
                {
                    currentPath.CurrentArgument = i;
                    this._metadataProvider.SetCurrentPath(currentPath);
                    this.BuildArgument(builder, parameters[i], (current: i, total: parameters.Length));
                }
            }

            var primaryAlias = isDefault
                ? ""
                : command.IsEmpty()
                    ? name
                    : command;

            builder.AddCommand(primaryAlias, ExecuteCommand, CreateCommand);
        }