Пример #1
0
        private void OnCommandSourcesChanged()
        {
            AsyncHelper.RunSync(async() =>
            {
                await m_CommandSources.DisposeAllAsync();

                try
                {
                    m_CommandSources = m_Options.Value.CreateCommandSources(m_ServiceProvider);
                }
                catch (ObjectDisposedException)
                {
                    // https://github.com/openmod/OpenMod/issues/61
                    m_CommandSources = new List <ICommandSource>();
                }

                var commands = new List <ICommandRegistration>();
                foreach (var sources in m_CommandSources)
                {
                    commands.AddRange(await sources.GetCommandsAsync());
                }

                foreach (var registration in commands)
                {
                    var permission = m_CommandPermissionBuilder.GetPermission(registration, commands);

                    m_PermissionRegistry.RegisterPermission(registration.Component,
                                                            permission,
                                                            description: $"Grants access to the {registration.Id} command.",
                                                            defaultGrant: PermissionGrantResult.Default);

                    if (registration.PermissionRegistrations == null)
                    {
                        continue;
                    }

                    foreach (var permissionRegistration in registration.PermissionRegistrations)
                    {
                        m_PermissionRegistry.RegisterPermission(permissionRegistration.Owner,
                                                                $"{permission}.{permissionRegistration.Permission}",
                                                                permissionRegistration.Description,
                                                                permissionRegistration.DefaultGrant);
                    }
                }

                var commandsData = await m_CommandDataStore.GetRegisteredCommandsAsync() ?? new RegisteredCommandsData();
                commandsData.Commands ??= new List <RegisteredCommandData>();

                foreach (var command in commands
                         .Where(d => !commandsData.Commands.Any(c => c.Id.Equals(d.Id, StringComparison.OrdinalIgnoreCase))))
                {
                    commandsData.Commands.Add(CreateDefaultCommandData(command));
                }

                await m_CommandDataStore.SetRegisteredCommandsAsync(commandsData);
            });
        }
Пример #2
0
        public async Task InvalidateAsync()
        {
            await m_CommandSources.DisposeAllAsync();

            if (m_Runtime.IsDisposing)
            {
                return;
            }

            m_CommandSources = m_Options.Value.CreateCommandSources(m_ServiceProvider);

            if (m_CommandSources.Count == 0)
            {
                m_Logger.LogDebug("InvalidateAsync: failed because no command sources were found; this is normal on booting.");
                return;
            }

            var commands = new List <ICommandRegistration>();

            foreach (var sources in m_CommandSources)
            {
                commands.AddRange(await sources.GetCommandsAsync());
            }

            foreach (var registration in commands)
            {
                var permission = m_CommandPermissionBuilder.GetPermission(registration, commands).Split(':')[1];

                m_PermissionRegistry.RegisterPermission(registration.Component,
                                                        permission,
                                                        description: $"Grants access to the {registration.Id} command.",
                                                        defaultGrant: PermissionGrantResult.Default);

                if (registration.PermissionRegistrations == null)
                {
                    continue;
                }

                foreach (var permissionRegistration in registration.PermissionRegistrations)
                {
                    m_PermissionRegistry.RegisterPermission(permissionRegistration.Owner,
                                                            $"{permission}.{permissionRegistration.Permission}",
                                                            permissionRegistration.Description,
                                                            permissionRegistration.DefaultGrant);
                }
            }

            var commandsData = await m_CommandDataStore.GetRegisteredCommandsAsync();

            if (commandsData?.Commands == null)
            {
                throw new Exception("Failed to register commands: command data was null");
            }

            foreach (var command in commands
                     .Where(d => !commandsData.Commands.Any(c =>
                                                            c.Id?.Equals(d.Id, StringComparison.OrdinalIgnoreCase) ?? false)))
            {
                commandsData.Commands.Add(CreateDefaultCommandData(command));
            }

            if (commandsData.Commands.Count == 0)
            {
                throw new Exception("Failed to register commands: command data was empty.");
            }

            await m_CommandDataStore.SetRegisteredCommandsAsync(commandsData);

            m_Logger.LogDebug($"Reloaded {commands.Count} commands.");
        }