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
+3
-2
@@ -1,4 +1,5 @@
|
||||
using System.Diagnostics;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Artemis.Core.Extensions;
|
||||
@@ -39,7 +40,7 @@ namespace Artemis.Core.Plugins.Modules.ActivationRequirements
|
||||
|
||||
var processes = ProcessName != null ? Process.GetProcessesByName(ProcessName).Where(p => !p.HasExited) : Process.GetProcesses().Where(p => !p.HasExited);
|
||||
return Location != null
|
||||
? processes.Any(p => Path.GetDirectoryName(p.GetProcessFilename()) == Location)
|
||||
? processes.Any(p => string.Equals(Path.GetDirectoryName(p.GetProcessFilename()), Location, StringComparison.CurrentCultureIgnoreCase))
|
||||
: processes.Any();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,6 +79,12 @@ namespace Artemis.Core.Plugins.Modules
|
||||
/// </summary>
|
||||
public string DisplayIcon { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A path to an image to use as the modules display icon that's shown in the menu.
|
||||
/// <para>If set, takes precedence over <see cref="DisplayIcon" /></para>
|
||||
/// </summary>
|
||||
public string DisplayIconPath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether this module is activated. A module can only be active while its <see cref="ActivationRequirements" />
|
||||
/// are met
|
||||
@@ -138,14 +144,22 @@ namespace Artemis.Core.Plugins.Modules
|
||||
public abstract void Render(double deltaTime, ArtemisSurface surface, SKCanvas canvas, SKImageInfo canvasInfo);
|
||||
|
||||
/// <summary>
|
||||
/// Called when the <see cref="ActivationRequirements" /> are met
|
||||
/// Called when the <see cref="ActivationRequirements" /> are met or during an override
|
||||
/// </summary>
|
||||
public abstract void ModuleActivated();
|
||||
/// <param name="isOverride">
|
||||
/// If true, the activation was due to an override. This usually means the module was activated
|
||||
/// by the profile editor
|
||||
/// </param>
|
||||
public abstract void ModuleActivated(bool isOverride);
|
||||
|
||||
/// <summary>
|
||||
/// Called when the <see cref="ActivationRequirements" /> are no longer met
|
||||
/// Called when the <see cref="ActivationRequirements" /> are no longer met or during an override
|
||||
/// </summary>
|
||||
public abstract void ModuleDeactivated();
|
||||
/// <param name="isOverride">
|
||||
/// If true, the deactivation was due to an override. This usually means the module was deactivated
|
||||
/// by the profile editor
|
||||
/// </param>
|
||||
public abstract void ModuleDeactivated(bool isOverride);
|
||||
|
||||
/// <summary>
|
||||
/// Evaluates the activation requirements following the <see cref="ActivationRequirementMode" /> and returns the result
|
||||
@@ -163,22 +177,22 @@ namespace Artemis.Core.Plugins.Modules
|
||||
return false;
|
||||
}
|
||||
|
||||
internal virtual void Activate()
|
||||
internal virtual void Activate(bool isOverride)
|
||||
{
|
||||
if (IsActivated)
|
||||
return;
|
||||
|
||||
ModuleActivated();
|
||||
ModuleActivated(isOverride);
|
||||
IsActivated = true;
|
||||
}
|
||||
|
||||
internal virtual void Deactivate()
|
||||
internal virtual void Deactivate(bool isOverride)
|
||||
{
|
||||
if (!IsActivated)
|
||||
return;
|
||||
|
||||
IsActivated = false;
|
||||
ModuleDeactivated();
|
||||
ModuleDeactivated(isOverride);
|
||||
}
|
||||
|
||||
internal void ApplyToEntity()
|
||||
|
||||
@@ -193,9 +193,9 @@ namespace Artemis.Core.Plugins.Modules
|
||||
/// </summary>
|
||||
public bool AnimatingProfileChange { get; private set; }
|
||||
|
||||
internal override void Deactivate()
|
||||
internal override void Deactivate(bool isOverride)
|
||||
{
|
||||
base.Deactivate();
|
||||
base.Deactivate(isOverride);
|
||||
|
||||
var profile = ActiveProfile;
|
||||
ActiveProfile = null;
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -79,12 +79,12 @@ namespace Artemis.Core.Utilities
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void ModuleActivated()
|
||||
public override void ModuleActivated(bool isOverride)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void ModuleDeactivated()
|
||||
public override void ModuleDeactivated(bool isOverride)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user