mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-11 19:36:28 +00:00
Modules - Added enable override mechanism
Profile modules - Added animated module enable/disable
This commit is contained in:
1 parent
73e992bbb7
commit
ed479abebc
21 files changed
+442
-122
No files matched your search
@@ -175,8 +175,8 @@ namespace Artemis.Core.Services
|
||||
lock (_modules)
|
||||
{
|
||||
modules = _modules.Where(m => m.IsActivated || m.InternalExpandsMainDataModel)
|
||||
.OrderByDescending(m => m.PriorityCategory)
|
||||
.ThenByDescending(m => m.Priority)
|
||||
.OrderBy(m => m.PriorityCategory)
|
||||
.ThenBy(m => m.Priority)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
using System.Threading.Tasks;
|
||||
using Artemis.Core.Plugins.Modules;
|
||||
|
||||
namespace Artemis.Core.Services.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// A service providing module activation functionality
|
||||
/// </summary>
|
||||
public interface IModuleService : IArtemisService
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets whether an override is currently being applied
|
||||
/// </summary>
|
||||
bool ApplyingOverride { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current active module override. If set, all other modules are deactivated and only the
|
||||
/// <see cref="ActiveModuleOverride" /> is active.
|
||||
/// </summary>
|
||||
Module ActiveModuleOverride { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Changes the current <see cref="ActiveModuleOverride" /> and deactivates all other modules
|
||||
/// </summary>
|
||||
/// <param name="overrideModule"></param>
|
||||
Task SetActiveModuleOverride(Module overrideModule);
|
||||
|
||||
/// <summary>
|
||||
/// Evaluates every enabled module's activation requirements and activates/deactivates modules accordingly
|
||||
/// </summary>
|
||||
Task UpdateModuleActivation();
|
||||
|
||||
/// <summary>
|
||||
/// Updates the priority and priority category of the given module
|
||||
/// </summary>
|
||||
/// <param name="module">The module to update</param>
|
||||
/// <param name="category">The new priority category of the module</param>
|
||||
/// <param name="priority">The new priority of the module</param>
|
||||
void UpdateModulePriority(Module module, ModulePriorityCategory category, int priority);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,9 @@ using RGB.NET.Core;
|
||||
|
||||
namespace Artemis.Core.Services.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// A service providing plugin management
|
||||
/// </summary>
|
||||
public interface IPluginService : IArtemisService, IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
using System.Diagnostics;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Timers;
|
||||
using Artemis.Core.Events;
|
||||
using Artemis.Core.Plugins.Modules;
|
||||
@@ -7,6 +11,7 @@ using Artemis.Core.Services.Interfaces;
|
||||
using Artemis.Core.Services.Storage.Interfaces;
|
||||
using Artemis.Storage.Repositories.Interfaces;
|
||||
using Serilog;
|
||||
using Timer = System.Timers.Timer;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
{
|
||||
@@ -26,58 +31,85 @@ namespace Artemis.Core.Services
|
||||
_pluginService.PluginEnabled += PluginServiceOnPluginEnabled;
|
||||
|
||||
var activationUpdateTimer = new Timer(2000);
|
||||
activationUpdateTimer.Elapsed += ActivationUpdateTimerOnElapsed;
|
||||
activationUpdateTimer.Start();
|
||||
activationUpdateTimer.Elapsed += ActivationUpdateTimerOnElapsed;
|
||||
|
||||
PopulatePriorities();
|
||||
}
|
||||
|
||||
private void ActivationUpdateTimerOnElapsed(object sender, ElapsedEventArgs e)
|
||||
public bool ApplyingOverride { get; private set; }
|
||||
public Module ActiveModuleOverride { get; private set; }
|
||||
|
||||
public async Task SetActiveModuleOverride(Module overrideModule)
|
||||
{
|
||||
UpdateModuleActivation();
|
||||
if (ActiveModuleOverride == overrideModule)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
// Not the cleanest way but locks don't work async and I cba with a mutex
|
||||
while (ApplyingOverride)
|
||||
await Task.Delay(50);
|
||||
|
||||
ApplyingOverride = true;
|
||||
ActiveModuleOverride = overrideModule;
|
||||
|
||||
// If set to null, resume regular activation
|
||||
if (ActiveModuleOverride == null)
|
||||
{
|
||||
await UpdateModuleActivation();
|
||||
_logger.Information("Cleared active module override");
|
||||
return;
|
||||
}
|
||||
|
||||
// If a module was provided, activate it and deactivate everything else
|
||||
var modules = _pluginService.GetPluginsOfType<Module>().ToList();
|
||||
var deactivationTasks = new List<Task>();
|
||||
foreach (var module in modules)
|
||||
{
|
||||
if (module != ActiveModuleOverride)
|
||||
deactivationTasks.Add(DeactivateModule(module, true));
|
||||
}
|
||||
|
||||
await Task.WhenAll(deactivationTasks);
|
||||
|
||||
if (!ActiveModuleOverride.IsActivated)
|
||||
await ActivateModule(ActiveModuleOverride, true);
|
||||
|
||||
_logger.Information($"Set active module override to {ActiveModuleOverride.DisplayName}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
ApplyingOverride = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateModuleActivation()
|
||||
public async Task UpdateModuleActivation()
|
||||
{
|
||||
if (ActiveModuleOverride != null)
|
||||
return;
|
||||
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
|
||||
var modules = _pluginService.GetPluginsOfType<Module>().ToList();
|
||||
var tasks = new List<Task>();
|
||||
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);
|
||||
}
|
||||
tasks.Add(ActivateModule(module, false));
|
||||
else if (!shouldBeActivated && module.IsActivated)
|
||||
module.Deactivate();
|
||||
tasks.Add(DeactivateModule(module, false));
|
||||
}
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
|
||||
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();
|
||||
@@ -105,6 +137,46 @@ namespace Artemis.Core.Services
|
||||
}
|
||||
}
|
||||
|
||||
private async void ActivationUpdateTimerOnElapsed(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
await UpdateModuleActivation();
|
||||
}
|
||||
|
||||
private async Task ActivateModule(Module module, bool isOverride)
|
||||
{
|
||||
module.Activate(isOverride);
|
||||
|
||||
// If this is a profile module, activate the last active profile after module activation
|
||||
if (module is ProfileModule profileModule)
|
||||
await _profileService.ActivateLastProfileAnimated(profileModule);
|
||||
}
|
||||
|
||||
private async Task DeactivateModule(Module module, bool isOverride)
|
||||
{
|
||||
// If this is a profile module, activate the last active profile after module activation
|
||||
if (module.IsActivated && module is ProfileModule profileModule)
|
||||
await profileModule.ChangeActiveProfileAnimated(null, null);
|
||||
|
||||
module.Deactivate(isOverride);
|
||||
}
|
||||
|
||||
private 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void PluginServiceOnPluginEnabled(object sender, PluginEventArgs e)
|
||||
{
|
||||
if (e.PluginInfo.Instance is Module module)
|
||||
@@ -124,8 +196,4 @@ namespace Artemis.Core.Services
|
||||
UpdateModulePriority(module, module.DefaultPriorityCategory, 1);
|
||||
}
|
||||
}
|
||||
|
||||
public interface IModuleService : IArtemisService
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -51,6 +51,14 @@ namespace Artemis.Core.Services.Storage.Interfaces
|
||||
/// <param name="profileModule"></param>
|
||||
void ActivateLastProfile(ProfileModule profileModule);
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously activates the last profile of the given profile module using a fade animation
|
||||
/// </summary>
|
||||
/// <param name="profileModule"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
Task ActivateLastProfileAnimated(ProfileModule profileModule);
|
||||
|
||||
/// <summary>
|
||||
/// Activates the profile described in the given <see cref="ProfileDescriptor" /> with the currently active surface
|
||||
/// </summary>
|
||||
|
||||
@@ -64,6 +64,13 @@ namespace Artemis.Core.Services.Storage
|
||||
ActivateProfile(activeProfile);
|
||||
}
|
||||
|
||||
public async Task ActivateLastProfileAnimated(ProfileModule profileModule)
|
||||
{
|
||||
var activeProfile = GetLastActiveProfile(profileModule);
|
||||
if (activeProfile != null)
|
||||
await ActivateProfileAnimated(activeProfile);
|
||||
}
|
||||
|
||||
public Profile ActivateProfile(ProfileDescriptor profileDescriptor)
|
||||
{
|
||||
if (profileDescriptor.ProfileModule.ActiveProfile?.EntityId == profileDescriptor.Id)
|
||||
@@ -94,12 +101,25 @@ namespace Artemis.Core.Services.Storage
|
||||
var profile = new Profile(profileDescriptor.ProfileModule, profileEntity);
|
||||
InstantiateProfile(profile);
|
||||
|
||||
void ActivatingProfileSurfaceUpdate(object sender, SurfaceConfigurationEventArgs e) => profile.PopulateLeds(e.Surface);
|
||||
void ActivatingProfilePluginToggle(object sender, PluginEventArgs e) => InstantiateProfile(profile);
|
||||
|
||||
// This could happen during activation so subscribe to it
|
||||
_pluginService.PluginEnabled += ActivatingProfilePluginToggle;
|
||||
_pluginService.PluginDisabled += ActivatingProfilePluginToggle;
|
||||
_surfaceService.SurfaceConfigurationUpdated += ActivatingProfileSurfaceUpdate;
|
||||
|
||||
await profileDescriptor.ProfileModule.ChangeActiveProfileAnimated(profile, _surfaceService.ActiveSurface);
|
||||
SaveActiveProfile(profileDescriptor.ProfileModule);
|
||||
|
||||
_pluginService.PluginEnabled -= ActivatingProfilePluginToggle;
|
||||
_pluginService.PluginDisabled -= ActivatingProfilePluginToggle;
|
||||
_surfaceService.SurfaceConfigurationUpdated -= ActivatingProfileSurfaceUpdate;
|
||||
|
||||
return profile;
|
||||
}
|
||||
|
||||
|
||||
public void ClearActiveProfile(ProfileModule module)
|
||||
{
|
||||
module.ChangeActiveProfile(null, _surfaceService.ActiveSurface);
|
||||
@@ -234,6 +254,9 @@ namespace Artemis.Core.Services.Storage
|
||||
|
||||
private void SaveActiveProfile(ProfileModule module)
|
||||
{
|
||||
if (module.ActiveProfile == null)
|
||||
return;
|
||||
|
||||
var profileEntities = _profileRepository.GetByPluginGuid(module.PluginInfo.Guid);
|
||||
foreach (var profileEntity in profileEntities)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user