mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Core - Kill zombies after 2 seconds 🔫
Core - Fix an access violation on surface changes (when loading device providers/moving devices)
This commit is contained in:
parent
c08e4a49a3
commit
ca453f2052
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Artemis.Core.Ninject;
|
using Artemis.Core.Ninject;
|
||||||
using Artemis.Core.ScriptingProviders;
|
using Artemis.Core.ScriptingProviders;
|
||||||
@ -58,7 +59,6 @@ namespace Artemis.Core.Services
|
|||||||
_frameStopWatch = new Stopwatch();
|
_frameStopWatch = new Stopwatch();
|
||||||
StartupArguments = new List<string>();
|
StartupArguments = new List<string>();
|
||||||
|
|
||||||
_rgbService.IsRenderPaused = true;
|
|
||||||
_rgbService.Surface.Updating += SurfaceOnUpdating;
|
_rgbService.Surface.Updating += SurfaceOnUpdating;
|
||||||
_loggingLevel.SettingChanged += (sender, args) => ApplyLoggingLevel();
|
_loggingLevel.SettingChanged += (sender, args) => ApplyLoggingLevel();
|
||||||
}
|
}
|
||||||
@ -222,7 +222,7 @@ namespace Artemis.Core.Services
|
|||||||
_pluginManagementService.CopyBuiltInPlugins();
|
_pluginManagementService.CopyBuiltInPlugins();
|
||||||
_pluginManagementService.LoadPlugins(StartupArguments, IsElevated);
|
_pluginManagementService.LoadPlugins(StartupArguments, IsElevated);
|
||||||
|
|
||||||
_rgbService.IsRenderPaused = false;
|
_rgbService.SetRenderPaused(false);
|
||||||
OnInitialized();
|
OnInitialized();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -34,7 +34,7 @@ namespace Artemis.Core.Services
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets whether rendering should be paused
|
/// Gets or sets whether rendering should be paused
|
||||||
/// </summary>
|
/// </summary>
|
||||||
bool IsRenderPaused { get; set; }
|
bool IsRenderPaused { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a boolean indicating whether the render pipeline is open
|
/// Gets a boolean indicating whether the render pipeline is open
|
||||||
@ -138,6 +138,13 @@ namespace Artemis.Core.Services
|
|||||||
/// <param name="device">The device to disable</param>
|
/// <param name="device">The device to disable</param>
|
||||||
void DisableDevice(ArtemisDevice device);
|
void DisableDevice(ArtemisDevice device);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Pauses or resumes rendering, method won't return until the current frame finished rendering
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="paused"></param>
|
||||||
|
/// <returns><see langword="true"/> if the pause state was changed; otherwise <see langword="false"/>.</returns>
|
||||||
|
bool SetRenderPaused(bool paused);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Occurs when a single device was added
|
/// Occurs when a single device was added
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -27,7 +27,6 @@ namespace Artemis.Core.Services
|
|||||||
private readonly PluginSetting<int> _targetFrameRateSetting;
|
private readonly PluginSetting<int> _targetFrameRateSetting;
|
||||||
private readonly TextureBrush _textureBrush = new(ITexture.Empty) {CalculationMode = RenderMode.Absolute};
|
private readonly TextureBrush _textureBrush = new(ITexture.Empty) {CalculationMode = RenderMode.Absolute};
|
||||||
private Dictionary<Led, ArtemisLed> _ledMap;
|
private Dictionary<Led, ArtemisLed> _ledMap;
|
||||||
private bool _modifyingProviders;
|
|
||||||
private ListLedGroup? _surfaceLedGroup;
|
private ListLedGroup? _surfaceLedGroup;
|
||||||
private SKTexture? _texture;
|
private SKTexture? _texture;
|
||||||
|
|
||||||
@ -51,6 +50,7 @@ namespace Artemis.Core.Services
|
|||||||
_ledMap = new Dictionary<Led, ArtemisLed>();
|
_ledMap = new Dictionary<Led, ArtemisLed>();
|
||||||
|
|
||||||
UpdateTrigger = new TimerUpdateTrigger {UpdateFrequency = 1.0 / _targetFrameRateSetting.Value};
|
UpdateTrigger = new TimerUpdateTrigger {UpdateFrequency = 1.0 / _targetFrameRateSetting.Value};
|
||||||
|
SetRenderPaused(true);
|
||||||
Surface.RegisterUpdateTrigger(UpdateTrigger);
|
Surface.RegisterUpdateTrigger(UpdateTrigger);
|
||||||
|
|
||||||
Utilities.ShutdownRequested += UtilitiesOnShutdownRequested;
|
Utilities.ShutdownRequested += UtilitiesOnShutdownRequested;
|
||||||
@ -81,16 +81,14 @@ namespace Artemis.Core.Services
|
|||||||
|
|
||||||
private void UpdateLedGroup()
|
private void UpdateLedGroup()
|
||||||
{
|
{
|
||||||
lock (_devices)
|
bool changedRenderPaused = SetRenderPaused(true);
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (_modifyingProviders)
|
|
||||||
return;
|
|
||||||
|
|
||||||
_ledMap = new Dictionary<Led, ArtemisLed>(_devices.SelectMany(d => d.Leds).ToDictionary(l => l.RgbLed));
|
_ledMap = new Dictionary<Led, ArtemisLed>(_devices.SelectMany(d => d.Leds).ToDictionary(l => l.RgbLed));
|
||||||
|
|
||||||
if (_surfaceLedGroup == null)
|
if (_surfaceLedGroup == null)
|
||||||
{
|
{
|
||||||
_surfaceLedGroup = new ListLedGroup(Surface, LedMap.Select(l => l.Key)) {Brush = _textureBrush};
|
_surfaceLedGroup = new ListLedGroup(Surface, LedMap.Select(l => l.Key)) { Brush = _textureBrush };
|
||||||
OnLedsChanged();
|
OnLedsChanged();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -101,10 +99,16 @@ namespace Artemis.Core.Services
|
|||||||
_surfaceLedGroup.Detach();
|
_surfaceLedGroup.Detach();
|
||||||
|
|
||||||
// Apply the application wide brush and decorator
|
// Apply the application wide brush and decorator
|
||||||
_surfaceLedGroup = new ListLedGroup(Surface, LedMap.Select(l => l.Key)) {Brush = _textureBrush};
|
_surfaceLedGroup = new ListLedGroup(Surface, LedMap.Select(l => l.Key)) { Brush = _textureBrush };
|
||||||
OnLedsChanged();
|
OnLedsChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (changedRenderPaused)
|
||||||
|
SetRenderPaused(false);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void TargetFrameRateSettingOnSettingChanged(object? sender, EventArgs e)
|
private void TargetFrameRateSettingOnSettingChanged(object? sender, EventArgs e)
|
||||||
@ -141,95 +145,85 @@ namespace Artemis.Core.Services
|
|||||||
|
|
||||||
public void AddDeviceProvider(IRGBDeviceProvider deviceProvider)
|
public void AddDeviceProvider(IRGBDeviceProvider deviceProvider)
|
||||||
{
|
{
|
||||||
if (RenderOpen)
|
bool changedRenderPaused = SetRenderPaused(true);
|
||||||
throw new ArtemisCoreException("Cannot add a device provider while rendering");
|
|
||||||
|
|
||||||
lock (_devices)
|
try
|
||||||
{
|
{
|
||||||
try
|
List<ArtemisDevice> toRemove = _devices.Where(a => deviceProvider.Devices.Any(d => a.RgbDevice == d)).ToList();
|
||||||
|
Surface.Detach(toRemove.Select(d => d.RgbDevice));
|
||||||
|
foreach (ArtemisDevice device in toRemove)
|
||||||
|
RemoveDevice(device);
|
||||||
|
|
||||||
|
List<Exception> providerExceptions = new();
|
||||||
|
|
||||||
|
void DeviceProviderOnException(object? sender, ExceptionEventArgs e)
|
||||||
{
|
{
|
||||||
_modifyingProviders = true;
|
if (e.IsCritical)
|
||||||
|
providerExceptions.Add(e.Exception);
|
||||||
List<ArtemisDevice> toRemove = _devices.Where(a => deviceProvider.Devices.Any(d => a.RgbDevice == d)).ToList();
|
else
|
||||||
Surface.Detach(toRemove.Select(d => d.RgbDevice));
|
_logger.Warning(e.Exception, "Device provider {deviceProvider} threw non-critical exception", deviceProvider.GetType().Name);
|
||||||
foreach (ArtemisDevice device in toRemove)
|
|
||||||
RemoveDevice(device);
|
|
||||||
|
|
||||||
List<Exception> providerExceptions = new();
|
|
||||||
|
|
||||||
void DeviceProviderOnException(object? sender, ExceptionEventArgs e)
|
|
||||||
{
|
|
||||||
if (e.IsCritical)
|
|
||||||
providerExceptions.Add(e.Exception);
|
|
||||||
else
|
|
||||||
_logger.Warning(e.Exception, "Device provider {deviceProvider} threw non-critical exception", deviceProvider.GetType().Name);
|
|
||||||
}
|
|
||||||
|
|
||||||
deviceProvider.Exception += DeviceProviderOnException;
|
|
||||||
deviceProvider.Initialize();
|
|
||||||
Surface.Attach(deviceProvider.Devices);
|
|
||||||
deviceProvider.Exception -= DeviceProviderOnException;
|
|
||||||
if (providerExceptions.Count == 1)
|
|
||||||
throw new ArtemisPluginException("RGB.NET threw exception: " + providerExceptions.First().Message, providerExceptions.First());
|
|
||||||
if (providerExceptions.Count > 1)
|
|
||||||
throw new ArtemisPluginException("RGB.NET threw multiple exceptions", new AggregateException(providerExceptions));
|
|
||||||
|
|
||||||
if (!deviceProvider.Devices.Any())
|
|
||||||
{
|
|
||||||
_logger.Warning("Device provider {deviceProvider} has no devices", deviceProvider.GetType().Name);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (IRGBDevice rgbDevice in deviceProvider.Devices)
|
|
||||||
{
|
|
||||||
ArtemisDevice artemisDevice = GetArtemisDevice(rgbDevice);
|
|
||||||
AddDevice(artemisDevice);
|
|
||||||
_logger.Debug("Device provider {deviceProvider} added {deviceName}", deviceProvider.GetType().Name, rgbDevice.DeviceInfo.DeviceName);
|
|
||||||
}
|
|
||||||
|
|
||||||
_devices.Sort((a, b) => a.ZIndex - b.ZIndex);
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
|
||||||
|
deviceProvider.Exception += DeviceProviderOnException;
|
||||||
|
deviceProvider.Initialize();
|
||||||
|
Surface.Attach(deviceProvider.Devices);
|
||||||
|
deviceProvider.Exception -= DeviceProviderOnException;
|
||||||
|
if (providerExceptions.Count == 1)
|
||||||
|
throw new ArtemisPluginException("RGB.NET threw exception: " + providerExceptions.First().Message, providerExceptions.First());
|
||||||
|
if (providerExceptions.Count > 1)
|
||||||
|
throw new ArtemisPluginException("RGB.NET threw multiple exceptions", new AggregateException(providerExceptions));
|
||||||
|
|
||||||
|
if (!deviceProvider.Devices.Any())
|
||||||
{
|
{
|
||||||
_logger.Error(e, "Exception during device loading for device provider {deviceProvider}", deviceProvider.GetType().Name);
|
_logger.Warning("Device provider {deviceProvider} has no devices", deviceProvider.GetType().Name);
|
||||||
throw;
|
return;
|
||||||
}
|
}
|
||||||
finally
|
|
||||||
|
foreach (IRGBDevice rgbDevice in deviceProvider.Devices)
|
||||||
{
|
{
|
||||||
_modifyingProviders = false;
|
ArtemisDevice artemisDevice = GetArtemisDevice(rgbDevice);
|
||||||
UpdateLedGroup();
|
AddDevice(artemisDevice);
|
||||||
|
_logger.Debug("Device provider {deviceProvider} added {deviceName}", deviceProvider.GetType().Name, rgbDevice.DeviceInfo.DeviceName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_devices.Sort((a, b) => a.ZIndex - b.ZIndex);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.Error(e, "Exception during device loading for device provider {deviceProvider}", deviceProvider.GetType().Name);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
UpdateLedGroup();
|
||||||
|
if (changedRenderPaused)
|
||||||
|
SetRenderPaused(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveDeviceProvider(IRGBDeviceProvider deviceProvider)
|
public void RemoveDeviceProvider(IRGBDeviceProvider deviceProvider)
|
||||||
{
|
{
|
||||||
if (RenderOpen)
|
bool changedRenderPaused = SetRenderPaused(true);
|
||||||
throw new ArtemisCoreException("Cannot update the remove device provider while rendering");
|
|
||||||
|
|
||||||
lock (_devices)
|
try
|
||||||
{
|
{
|
||||||
try
|
List<ArtemisDevice> toRemove = _devices.Where(a => deviceProvider.Devices.Any(d => a.RgbDevice == d)).ToList();
|
||||||
{
|
Surface.Detach(toRemove.Select(d => d.RgbDevice));
|
||||||
_modifyingProviders = true;
|
foreach (ArtemisDevice device in toRemove)
|
||||||
|
RemoveDevice(device);
|
||||||
|
|
||||||
List<ArtemisDevice> toRemove = _devices.Where(a => deviceProvider.Devices.Any(d => a.RgbDevice == d)).ToList();
|
_devices.Sort((a, b) => a.ZIndex - b.ZIndex);
|
||||||
Surface.Detach(toRemove.Select(d => d.RgbDevice));
|
}
|
||||||
foreach (ArtemisDevice device in toRemove)
|
catch (Exception e)
|
||||||
RemoveDevice(device);
|
{
|
||||||
|
_logger.Error(e, "Exception during device removal for device provider {deviceProvider}", deviceProvider.GetType().Name);
|
||||||
_devices.Sort((a, b) => a.ZIndex - b.ZIndex);
|
throw;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
finally
|
||||||
{
|
{
|
||||||
_logger.Error(e, "Exception during device removal for device provider {deviceProvider}", deviceProvider.GetType().Name);
|
UpdateLedGroup();
|
||||||
throw;
|
if (changedRenderPaused)
|
||||||
}
|
SetRenderPaused(false);
|
||||||
finally
|
|
||||||
{
|
|
||||||
_modifyingProviders = false;
|
|
||||||
UpdateLedGroup();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -241,6 +235,24 @@ namespace Artemis.Core.Services
|
|||||||
Surface.Dispose();
|
Surface.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool SetRenderPaused(bool paused)
|
||||||
|
{
|
||||||
|
if (IsRenderPaused == paused)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (paused)
|
||||||
|
{
|
||||||
|
UpdateTrigger.Stop();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
UpdateTrigger.Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
IsRenderPaused = paused;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public event EventHandler<DeviceEventArgs>? DeviceAdded;
|
public event EventHandler<DeviceEventArgs>? DeviceAdded;
|
||||||
public event EventHandler<DeviceEventArgs>? DeviceRemoved;
|
public event EventHandler<DeviceEventArgs>? DeviceRemoved;
|
||||||
public event EventHandler? LedsChanged;
|
public event EventHandler? LedsChanged;
|
||||||
@ -326,12 +338,22 @@ namespace Artemis.Core.Services
|
|||||||
|
|
||||||
public void AutoArrangeDevices()
|
public void AutoArrangeDevices()
|
||||||
{
|
{
|
||||||
SurfaceArrangement surfaceArrangement = SurfaceArrangement.GetDefaultArrangement();
|
bool changedRenderPaused = SetRenderPaused(true);
|
||||||
surfaceArrangement.Arrange(_devices);
|
|
||||||
foreach (ArtemisDevice artemisDevice in _devices)
|
|
||||||
artemisDevice.ApplyDefaultCategories();
|
|
||||||
|
|
||||||
SaveDevices();
|
try
|
||||||
|
{
|
||||||
|
SurfaceArrangement surfaceArrangement = SurfaceArrangement.GetDefaultArrangement();
|
||||||
|
surfaceArrangement.Arrange(_devices);
|
||||||
|
foreach (ArtemisDevice artemisDevice in _devices)
|
||||||
|
artemisDevice.ApplyDefaultCategories();
|
||||||
|
|
||||||
|
SaveDevices();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (changedRenderPaused)
|
||||||
|
SetRenderPaused(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArtemisLayout? ApplyBestDeviceLayout(ArtemisDevice device)
|
public ArtemisLayout? ApplyBestDeviceLayout(ArtemisDevice device)
|
||||||
|
|||||||
@ -92,7 +92,10 @@ namespace Artemis.UI
|
|||||||
using HttpClient client = new();
|
using HttpClient client = new();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
HttpResponseMessage httpResponseMessage = client.Send(new HttpRequestMessage(HttpMethod.Post, url + "remote/bring-to-foreground"));
|
CancellationTokenSource cts = new();
|
||||||
|
cts.CancelAfter(2000);
|
||||||
|
|
||||||
|
HttpResponseMessage httpResponseMessage = client.Send(new HttpRequestMessage(HttpMethod.Post, url + "remote/bring-to-foreground"), cts.Token);
|
||||||
httpResponseMessage.EnsureSuccessStatusCode();
|
httpResponseMessage.EnsureSuccessStatusCode();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,14 +31,14 @@ namespace Artemis.UI.Screens.StartupWizard.Steps
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void OnActivate()
|
protected override void OnActivate()
|
||||||
{
|
{
|
||||||
_rgbService.IsRenderPaused = true;
|
_rgbService.SetRenderPaused(true);
|
||||||
base.OnActivate();
|
base.OnActivate();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void OnDeactivate()
|
protected override void OnDeactivate()
|
||||||
{
|
{
|
||||||
_rgbService.IsRenderPaused = false;
|
_rgbService.SetRenderPaused(false);
|
||||||
base.OnDeactivate();
|
base.OnDeactivate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -333,7 +333,7 @@ namespace Artemis.UI.Screens.SurfaceEditor
|
|||||||
SurfaceDeviceViewModel device = HitTestUtilities.GetHitViewModels<SurfaceDeviceViewModel>((Visual) sender, selectedRect).FirstOrDefault();
|
SurfaceDeviceViewModel device = HitTestUtilities.GetHitViewModels<SurfaceDeviceViewModel>((Visual) sender, selectedRect).FirstOrDefault();
|
||||||
if (device != null)
|
if (device != null)
|
||||||
{
|
{
|
||||||
_rgbService.IsRenderPaused = true;
|
_rgbService.SetRenderPaused(true);
|
||||||
_mouseDragStatus = MouseDragStatus.Dragging;
|
_mouseDragStatus = MouseDragStatus.Dragging;
|
||||||
// If the device is not selected, deselect others and select only this one (if shift not held)
|
// If the device is not selected, deselect others and select only this one (if shift not held)
|
||||||
if (device.SelectionStatus != SelectionStatus.Selected)
|
if (device.SelectionStatus != SelectionStatus.Selected)
|
||||||
@ -378,7 +378,7 @@ namespace Artemis.UI.Screens.SurfaceEditor
|
|||||||
}
|
}
|
||||||
|
|
||||||
_mouseDragStatus = MouseDragStatus.None;
|
_mouseDragStatus = MouseDragStatus.None;
|
||||||
_rgbService.IsRenderPaused = false;
|
_rgbService.SetRenderPaused(false);
|
||||||
ApplySurfaceSelection();
|
ApplySurfaceSelection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user