1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Core - Implemented module priority

Settings UI - Module priority modification UI WIP
This commit is contained in:
Robert 2020-08-18 19:06:19 +02:00
parent 6a32ecc3a4
commit 04f2162bcd
12 changed files with 416 additions and 63 deletions

View File

@ -6,6 +6,7 @@ using Artemis.Core.Plugins.Abstract.DataModels;
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
using Artemis.Core.Plugins.Abstract.ViewModels;
using Artemis.Core.Plugins.ModuleActivationRequirements;
using Artemis.Storage.Entities.Module;
using SkiaSharp;
namespace Artemis.Core.Plugins.Abstract
@ -68,12 +69,6 @@ namespace Artemis.Core.Plugins.Abstract
/// </summary>
public abstract class Module : Plugin
{
protected Module()
{
ActivationRequirements = new List<IModuleActivationRequirement>();
ActivationRequirementMode = ActivationRequirementType.Any;
}
/// <summary>
/// The modules display name that's shown in the menu
/// </summary>
@ -95,16 +90,33 @@ namespace Artemis.Core.Plugins.Abstract
/// A list of activation requirements
/// <para>Note: if empty the module is always activated</para>
/// </summary>
public List<IModuleActivationRequirement> ActivationRequirements { get; }
public List<IModuleActivationRequirement> ActivationRequirements { get; } = new List<IModuleActivationRequirement>();
/// <summary>
/// Gets or sets the activation requirement mode, defaults to <see cref="ActivationRequirementType.Any" />
/// </summary>
public ActivationRequirementType ActivationRequirementMode { get; set; }
public ActivationRequirementType ActivationRequirementMode { get; set; } = ActivationRequirementType.Any;
/// <summary>
/// Gets or sets the default priority category for this module, defaults to
/// <see cref="ModulePriorityCategory.Normal" />
/// </summary>
public ModulePriorityCategory DefaultPriorityCategory { get; set; } = ModulePriorityCategory.Normal;
/// <summary>
/// Gets or sets the current priority category of this module
/// </summary>
public ModulePriorityCategory PriorityCategory { get; set; }
/// <summary>
/// Gets or sets the current priority of this module within its priority category
/// </summary>
public int Priority { get; set; }
internal DataModel InternalDataModel { get; set; }
internal bool InternalExpandsMainDataModel { get; set; }
internal ModuleSettingsEntity Entity { get; set; }
/// <summary>
/// Called each frame when the module must update
@ -170,6 +182,16 @@ namespace Artemis.Core.Plugins.Abstract
IsActivated = false;
ModuleDeactivated();
}
internal void ApplyToEntity()
{
if (Entity == null)
Entity = new ModuleSettingsEntity();
Entity.PluginGuid = PluginInfo.Guid;
Entity.PriorityCategory = (int) PriorityCategory;
Entity.Priority = Priority;
}
}
public enum ActivationRequirementType
@ -184,4 +206,22 @@ namespace Artemis.Core.Plugins.Abstract
/// </summary>
All
}
public enum ModulePriorityCategory
{
/// <summary>
/// Indicates a normal render priority
/// </summary>
Normal,
/// <summary>
/// Indicates that the module renders for a specific application/game, rendering on top of normal modules
/// </summary>
Application,
/// <summary>
/// Indicates that the module renders an overlay, always rendering on top
/// </summary>
Overlay
}
}

View File

@ -39,11 +39,10 @@ namespace Artemis.Core.Services
private List<BaseDataModelExpansion> _dataModelExpansions;
private List<Module> _modules;
private IntroAnimation _introAnimation;
private DateTime _lastModuleActivationUpdate;
// ReSharper disable once UnusedParameter.Local - Storage migration service is injected early to ensure it runs before anything else
internal CoreService(ILogger logger, StorageMigrationService _, ISettingsService settingsService, IPluginService pluginService,
IRgbService rgbService, ISurfaceService surfaceService, IProfileService profileService)
IRgbService rgbService, ISurfaceService surfaceService, IProfileService profileService, IModuleService moduleService)
{
_logger = logger;
_pluginService = pluginService;
@ -169,11 +168,6 @@ namespace Artemis.Core.Services
try
{
_frameStopWatch.Restart();
// Only run the module activation update every 2 seconds
if (DateTime.Now - _lastModuleActivationUpdate > TimeSpan.FromSeconds(2))
ModuleActivationUpdate();
lock (_dataModelExpansions)
{
// Update all active modules
@ -184,7 +178,7 @@ namespace Artemis.Core.Services
List<Module> modules;
lock (_modules)
{
modules = _modules.Where(m => m.IsActivated).ToList();
modules = _modules.Where(m => m.IsActivated).OrderByDescending(m => m.PriorityCategory).ThenByDescending(m => m.Priority).ToList();
}
// Update all active modules
@ -223,33 +217,6 @@ namespace Artemis.Core.Services
}
}
private void ModuleActivationUpdate()
{
_lastModuleActivationUpdate = DateTime.Now;
var stopwatch = new Stopwatch();
stopwatch.Start();
lock (_modules)
{
foreach (var module in _modules)
{
var shouldBeActivated = module.EvaluateActivationRequirements();
if (shouldBeActivated && !module.IsActivated)
{
module.Activate();
// If this is a profile module, activate the last active profile after module activation
if (module is ProfileModule profileModule)
_profileService.ActivateLastProfile(profileModule);
}
else if (!shouldBeActivated && module.IsActivated)
module.Deactivate();
}
}
stopwatch.Stop();
if (stopwatch.ElapsedMilliseconds > 100)
_logger.Warning("Activation requirements evaluation took too long: {moduleCount} module(s) in {elapsed}", _modules.Count, stopwatch.Elapsed);
}
private void SurfaceOnUpdated(UpdatedEventArgs args)
{
if (_rgbService.IsRenderPaused)

View File

@ -0,0 +1,131 @@
using System.Diagnostics;
using System.Linq;
using System.Timers;
using Artemis.Core.Events;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Services.Interfaces;
using Artemis.Core.Services.Storage.Interfaces;
using Artemis.Storage.Repositories.Interfaces;
using Serilog;
namespace Artemis.Core.Services
{
public class ModuleService : IModuleService
{
private readonly ILogger _logger;
private readonly IModuleRepository _moduleRepository;
private readonly IPluginService _pluginService;
private readonly IProfileService _profileService;
public ModuleService(ILogger logger, IModuleRepository moduleRepository, IPluginService pluginService, IProfileService profileService)
{
_logger = logger;
_moduleRepository = moduleRepository;
_pluginService = pluginService;
_profileService = profileService;
_pluginService.PluginEnabled += PluginServiceOnPluginEnabled;
var activationUpdateTimer = new Timer(2000);
activationUpdateTimer.Elapsed += ActivationUpdateTimerOnElapsed;
activationUpdateTimer.Start();
PopulatePriorities();
}
private void ActivationUpdateTimerOnElapsed(object sender, ElapsedEventArgs e)
{
UpdateModuleActivation();
}
public void UpdateModuleActivation()
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var modules = _pluginService.GetPluginsOfType<Module>().ToList();
foreach (var module in modules)
{
var shouldBeActivated = module.EvaluateActivationRequirements();
if (shouldBeActivated && !module.IsActivated)
{
module.Activate();
// If this is a profile module, activate the last active profile after module activation
if (module is ProfileModule profileModule)
_profileService.ActivateLastProfile(profileModule);
}
else if (!shouldBeActivated && module.IsActivated)
module.Deactivate();
}
stopwatch.Stop();
if (stopwatch.ElapsedMilliseconds > 100)
_logger.Warning("Activation requirements evaluation took too long: {moduleCount} module(s) in {elapsed}", modules.Count, stopwatch.Elapsed);
}
public void PopulatePriorities()
{
var modules = _pluginService.GetPluginsOfType<Module>().ToList();
var moduleEntities = _moduleRepository.GetAll();
foreach (var module in modules)
{
var entity = moduleEntities.FirstOrDefault(e => e.PluginGuid == module.PluginInfo.Guid);
if (entity != null)
{
module.Entity = entity;
module.PriorityCategory = (ModulePriorityCategory) entity.PriorityCategory;
module.Priority = entity.Priority;
}
}
}
public void UpdateModulePriority(Module module, ModulePriorityCategory category, int priority)
{
var modules = _pluginService.GetPluginsOfType<Module>().Where(m => m.PriorityCategory == category).OrderBy(m => m.Priority).ToList();
if (modules.Contains(module))
modules.Remove(module);
if (modules.Count == 0)
priority = 1;
else if (priority < 1)
priority = 1;
else if (priority > modules.Count)
priority = modules.Count;
module.PriorityCategory = category;
modules.Insert(priority - 1, module);
for (var index = 0; index < modules.Count; index++)
{
var categoryModule = modules[index];
categoryModule.Priority = index + 1;
categoryModule.ApplyToEntity();
_moduleRepository.Save(categoryModule.Entity);
}
}
private void PluginServiceOnPluginEnabled(object sender, PluginEventArgs e)
{
if (e.PluginInfo.Instance is Module module)
InitialiseOrApplyPriority(module);
}
private void InitialiseOrApplyPriority(Module module)
{
var entity = _moduleRepository.GetByPluginGuid(module.PluginInfo.Guid);
if (entity != null)
{
module.Entity = entity;
module.PriorityCategory = (ModulePriorityCategory) entity.PriorityCategory;
module.Priority = entity.Priority;
}
else
UpdateModulePriority(module, module.DefaultPriorityCategory, 1);
}
}
public interface IModuleService : IArtemisService
{
}
}

View File

@ -0,0 +1,11 @@
using System;
namespace Artemis.Storage.Entities.Module
{
public class ModuleSettingsEntity
{
public Guid PluginGuid { get; set; }
public int PriorityCategory { get; set; }
public int Priority { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using Artemis.Storage.Entities.Module;
namespace Artemis.Storage.Repositories.Interfaces
{
public interface IModuleRepository : IRepository
{
void Add(ModuleSettingsEntity moduleSettingsEntity);
ModuleSettingsEntity GetByPluginGuid(Guid guid);
List<ModuleSettingsEntity> GetAll();
List<ModuleSettingsEntity> GetByCategory(int category);
void Save(ModuleSettingsEntity moduleSettingsEntity);
}
}

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using Artemis.Storage.Entities.Module;
using Artemis.Storage.Repositories.Interfaces;
using LiteDB;
namespace Artemis.Storage.Repositories
{
public class ModuleRepository : IModuleRepository
{
private readonly LiteRepository _repository;
internal ModuleRepository(LiteRepository repository)
{
_repository = repository;
_repository.Database.GetCollection<ModuleSettingsEntity>().EnsureIndex(s => s.PluginGuid);
}
public void Add(ModuleSettingsEntity moduleSettingsEntity)
{
_repository.Insert(moduleSettingsEntity);
}
public ModuleSettingsEntity GetByPluginGuid(Guid guid)
{
return _repository.FirstOrDefault<ModuleSettingsEntity>(s => s.PluginGuid == guid);
}
public List<ModuleSettingsEntity> GetAll()
{
return _repository.Query<ModuleSettingsEntity>().ToList();
}
public List<ModuleSettingsEntity> GetByCategory(int category)
{
return _repository.Query<ModuleSettingsEntity>().Where(s => s.PriorityCategory == category).ToList();
}
public void Save(ModuleSettingsEntity moduleSettingsEntity)
{
_repository.Upsert(moduleSettingsEntity);
}
}
}

View File

@ -32,14 +32,10 @@ namespace Artemis.UI.Ninject.Factories
ModuleRootViewModel Create(Module module);
}
public interface IPluginSettingsVmFactory : IVmFactory
public interface ISettingsVmFactory : IVmFactory
{
PluginSettingsViewModel Create(Plugin plugin);
}
public interface IDeviceSettingsVmFactory : IVmFactory
{
DeviceSettingsViewModel Create(ArtemisDevice device);
PluginSettingsViewModel CreatePluginSettingsViewModel(Plugin plugin);
DeviceSettingsViewModel CreateDeviceSettingsViewModel(ArtemisDevice device);
}
public interface IDeviceDebugVmFactory : IVmFactory

View File

@ -28,12 +28,12 @@
</StackPanel.ContextMenu>
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<materialDesign:PackIcon Grid.Column="0" Kind="Layers" Width="16" VerticalAlignment="Center"/>
<materialDesign:PackIcon Grid.Column="0" Kind="Layers" Width="16" VerticalAlignment="Center" />
<materialDesign:PackIcon Grid.Column="1"
VerticalAlignment="Center"
Kind="{Binding Layer.LayerBrush.Descriptor.Icon}"

View File

@ -271,6 +271,15 @@
</StackPanel>
</ScrollViewer>
</TabItem>
<TabItem Header="MODULE PRIORITY" TextElement.Foreground="{DynamicResource MaterialDesignBody}">
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
<StackPanel Margin="15" MaxWidth="800">
<ContentControl xaml:View.Model="{Binding ModuleOrderViewModel}"></ContentControl>
</StackPanel>
</ScrollViewer>
</TabItem>
<TabItem Header="PLUGINS" TextElement.Foreground="{DynamicResource MaterialDesignBody}">
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" Margin="0 12 0 0">
<DockPanel Margin="15" MaxWidth="1230" HorizontalAlignment="Center">

View File

@ -11,6 +11,7 @@ using Artemis.Core.Services.Storage.Interfaces;
using Artemis.Core.Utilities;
using Artemis.UI.Ninject.Factories;
using Artemis.UI.Screens.Settings.Tabs.Devices;
using Artemis.UI.Screens.Settings.Tabs.Modules;
using Artemis.UI.Screens.Settings.Tabs.Plugins;
using Artemis.UI.Services.Interfaces;
using Artemis.UI.Shared.Services.Interfaces;
@ -22,12 +23,12 @@ namespace Artemis.UI.Screens.Settings
{
public class SettingsViewModel : MainScreenViewModel
{
public ModuleOrderViewModel ModuleOrderViewModel { get; }
private readonly IDebugService _debugService;
private readonly IDeviceSettingsVmFactory _deviceSettingsVmFactory;
private readonly IDialogService _dialogService;
private readonly IPluginService _pluginService;
private readonly ISettingsService _settingsService;
private readonly IPluginSettingsVmFactory _pluginSettingsVmFactory;
private readonly ISettingsVmFactory _settingsVmFactory;
private readonly ISurfaceService _surfaceService;
private List<Tuple<string, int>> _targetFrameRates;
private List<Tuple<string, double>> _renderScales;
@ -35,9 +36,15 @@ namespace Artemis.UI.Screens.Settings
private BindableCollection<DeviceSettingsViewModel> _deviceSettingsViewModels;
private BindableCollection<PluginSettingsViewModel> _plugins;
public SettingsViewModel(ISurfaceService surfaceService, IPluginService pluginService, IDialogService dialogService, IDebugService debugService,
ISettingsService settingsService, IPluginSettingsVmFactory pluginSettingsVmFactory, IDeviceSettingsVmFactory deviceSettingsVmFactory)
public SettingsViewModel(ISurfaceService surfaceService,
IPluginService pluginService,
IDialogService dialogService,
IDebugService debugService,
ISettingsService settingsService,
ISettingsVmFactory settingsVmFactory,
ModuleOrderViewModel moduleOrderViewModel)
{
ModuleOrderViewModel = moduleOrderViewModel;
DisplayName = "Settings";
_surfaceService = surfaceService;
@ -45,8 +52,7 @@ namespace Artemis.UI.Screens.Settings
_dialogService = dialogService;
_debugService = debugService;
_settingsService = settingsService;
_pluginSettingsVmFactory = pluginSettingsVmFactory;
_deviceSettingsVmFactory = deviceSettingsVmFactory;
_settingsVmFactory = settingsVmFactory;
DeviceSettingsViewModels = new BindableCollection<DeviceSettingsViewModel>();
Plugins = new BindableCollection<PluginSettingsViewModel>();
@ -225,10 +231,10 @@ namespace Artemis.UI.Screens.Settings
Task.Run(ApplyAutorun);
DeviceSettingsViewModels.Clear();
DeviceSettingsViewModels.AddRange(_surfaceService.ActiveSurface.Devices.Select(d => _deviceSettingsVmFactory.Create(d)));
DeviceSettingsViewModels.AddRange(_surfaceService.ActiveSurface.Devices.Select(d => _settingsVmFactory.CreateDeviceSettingsViewModel(d)));
Plugins.Clear();
Plugins.AddRange(_pluginService.GetAllPluginInfo().Select(p => _pluginSettingsVmFactory.Create(p.Instance)));
Plugins.AddRange(_pluginService.GetAllPluginInfo().Select(p => _settingsVmFactory.CreatePluginSettingsViewModel(p.Instance)));
base.OnInitialActivate();
}

View File

@ -0,0 +1,123 @@
<UserControl x:Class="Artemis.UI.Screens.Settings.Tabs.Modules.ModuleOrderView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Artemis.UI.Screens.Settings.Tabs.Modules"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<!-- Profile editor settings -->
<StackPanel>
<Grid Margin="0 15">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Style="{StaticResource MaterialDesignHeadline5TextBlock}">Overlays</TextBlock>
<TextBlock Grid.Column="1"
Style="{StaticResource MaterialDesignCaptionTextBlock}"
VerticalAlignment="Bottom"
HorizontalAlignment="Right"
Margin="10 0">
Modules that should always render on top
</TextBlock>
</Grid>
<materialDesign:Card materialDesign:ShadowAssist.ShadowDepth="Depth1" VerticalAlignment="Stretch" Margin="0,0,5,0">
<StackPanel Margin="15">
<ListBox>
<TextBlock>
Plain
</TextBlock>
<TextBlock>
Old
</TextBlock>
<TextBlock>
ListBox
</TextBlock>
<TextBlock>
Full of junk
</TextBlock>
</ListBox>
</StackPanel>
</materialDesign:Card>
</StackPanel>
<StackPanel Grid.Row="1">
<Grid Margin="0 15">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Style="{StaticResource MaterialDesignHeadline5TextBlock}">Applications/games</TextBlock>
<TextBlock Grid.Column="1"
Style="{StaticResource MaterialDesignCaptionTextBlock}"
VerticalAlignment="Bottom"
HorizontalAlignment="Right"
Margin="10 0">
Modules that are related to specific applications or games
</TextBlock>
</Grid>
<materialDesign:Card materialDesign:ShadowAssist.ShadowDepth="Depth1" VerticalAlignment="Stretch" Margin="0,0,5,0">
<StackPanel Margin="15">
<ListBox>
<TextBlock>
Plain
</TextBlock>
<TextBlock>
Old
</TextBlock>
<TextBlock>
ListBox
</TextBlock>
<TextBlock>
Full of junk
</TextBlock>
</ListBox>
</StackPanel>
</materialDesign:Card>
</StackPanel>
<StackPanel Grid.Row="2">
<Grid Margin="0 15">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Style="{StaticResource MaterialDesignHeadline5TextBlock}">Normal</TextBlock>
<TextBlock Grid.Column="1"
Style="{StaticResource MaterialDesignCaptionTextBlock}"
VerticalAlignment="Bottom"
HorizontalAlignment="Right"
Margin="10 0">
Regular modules that are always active in the background
</TextBlock>
</Grid>
<materialDesign:Card materialDesign:ShadowAssist.ShadowDepth="Depth1" VerticalAlignment="Stretch" Margin="0,0,5,0">
<StackPanel Margin="15">
<ListBox>
<TextBlock>
Plain
</TextBlock>
<TextBlock>
Old
</TextBlock>
<TextBlock>
ListBox
</TextBlock>
<TextBlock>
Full of junk
</TextBlock>
</ListBox>
</StackPanel>
</materialDesign:Card>
</StackPanel>
</Grid>
</UserControl>

View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
using Stylet;
namespace Artemis.UI.Screens.Settings.Tabs.Modules
{
public class ModuleOrderViewModel : PropertyChangedBase
{
}
}