mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 11:56:30 +00:00
Core - Implemented module priority
Settings UI - Module priority modification UI WIP
This commit is contained in:
1 parent
830ef33b0a
commit
d929589e1c
12 files changed
+416
-63
No files matched your search
@@ -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