mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-11 19:36:28 +00:00
Core - Implemented module priority
Settings UI - Module priority modification UI WIP
This commit is contained in:
1 parent
6a32ecc3a4
commit
04f2162bcd
12 files changed
+416
-63
No files matched your search
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user