1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-12 17:48:31 +00:00

Merge branch 'Development' into SomeFixes

This commit is contained in:
Darth Affe 2023-04-11 00:27:06 +02:00
commit f6433af4b5
9 changed files with 85 additions and 3 deletions

View File

@ -85,6 +85,8 @@ public abstract class AbstractRGBDevice<TDeviceInfo> : Placeable, IRGBDevice<TDe
{
this.DeviceInfo = deviceInfo;
this.UpdateQueue = updateQueue;
UpdateQueue.AddReferencingObject(this);
}
#endregion
@ -157,7 +159,13 @@ public abstract class AbstractRGBDevice<TDeviceInfo> : Placeable, IRGBDevice<TDe
/// <inheritdoc />
public virtual void Dispose()
{
try { UpdateQueue.Dispose(); } catch { /* :( */ }
try
{
UpdateQueue.RemoveReferencingObject(this);
if (!UpdateQueue.HasActiveReferences())
UpdateQueue.Dispose();
}
catch { /* :( */ }
try { LedMapping.Clear(); } catch { /* this really shouldn't happen */ }
IdGenerator.ResetCounter(GetType().Assembly);

View File

@ -0,0 +1,6 @@
namespace RGB.NET.Core;
public static class ReferenceCountingExtension
{
public static bool HasActiveReferences(this IReferenceCounting target) => target.ActiveReferenceCount > 0;
}

View File

@ -0,0 +1,41 @@
using System.Collections.Generic;
namespace RGB.NET.Core;
public abstract class AbstractReferenceCounting : IReferenceCounting
{
#region Properties & Fields
private readonly HashSet<object> _referencingObjects = new();
/// <inheritdoc />
public int ActiveReferenceCount
{
get
{
lock (_referencingObjects)
return _referencingObjects.Count;
}
}
#endregion
#region Methods
/// <inheritdoc />
public void AddReferencingObject(object obj)
{
lock (_referencingObjects)
if (!_referencingObjects.Contains(obj))
_referencingObjects.Add(obj);
}
/// <inheritdoc />
public void RemoveReferencingObject(object obj)
{
lock (_referencingObjects)
_referencingObjects.Remove(obj);
}
#endregion
}

View File

@ -0,0 +1,21 @@
namespace RGB.NET.Core;
public interface IReferenceCounting
{
/// <summary>
/// Gets the amount of currently registered referencing objects.
/// </summary>
public int ActiveReferenceCount { get; }
/// <summary>
/// Adds the given object to the list of referencing objects.
/// </summary>
/// <param name="obj">The object to add.</param>
public void AddReferencingObject(object obj);
/// <summary>
/// Removes the given object from the list of referencing objects.
/// </summary>
/// <param name="obj">The object to remove.</param>
public void RemoveReferencingObject(object obj);
}

View File

@ -16,6 +16,7 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=helper/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=ids/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=leds/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=misc/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=mvvm/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=positioning/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=rendering/@EntryIndexedValue">True</s:Boolean>

View File

@ -8,7 +8,7 @@ namespace RGB.NET.Core;
/// </summary>
/// <typeparam name="TIdentifier">The identifier used to identify the data processed by this queue.</typeparam>
/// <typeparam name="TData">The type of the data processed by this queue.</typeparam>
public interface IUpdateQueue<TIdentifier, TData> : IDisposable
public interface IUpdateQueue<TIdentifier, TData> : IReferenceCounting, IDisposable
where TIdentifier : notnull
{
/// <summary>

View File

@ -10,7 +10,7 @@ namespace RGB.NET.Core;
/// </summary>
/// <typeparam name="TIdentifier">The type of the key used to identify some data.</typeparam>
/// <typeparam name="TData">The type of the data.</typeparam>
public abstract class UpdateQueue<TIdentifier, TData> : IUpdateQueue<TIdentifier, TData>
public abstract class UpdateQueue<TIdentifier, TData> : AbstractReferenceCounting, IUpdateQueue<TIdentifier, TData>
where TIdentifier : notnull
{
#region Properties & Fields

View File

@ -13,6 +13,8 @@ public class CorsairDeviceUpdateQueue : UpdateQueue
{
#region Properties & Fields
private bool _isDisposed = false;
private readonly _CorsairDeviceInfo _device;
private readonly nint _colorPtr;
@ -42,6 +44,7 @@ public class CorsairDeviceUpdateQueue : UpdateQueue
{
try
{
if (_isDisposed) throw new ObjectDisposedException(nameof(CorsairDeviceUpdateQueue));
if (!_CUESDK.IsConnected) return false;
Span<_CorsairLedColor> colors = new((void*)_colorPtr, dataSet.Length);
@ -71,6 +74,7 @@ public class CorsairDeviceUpdateQueue : UpdateQueue
{
base.Dispose();
_isDisposed = true;
Marshal.FreeHGlobal(_colorPtr);
GC.SuppressFinalize(this);

View File

@ -108,6 +108,7 @@ public class RazerDeviceProvider : AbstractRGBDeviceProvider
{ 0x0266, RGBDeviceType.Keyboard, "Huntsman V2", LedMappings.Keyboard, RazerEndpointType.Keyboard },
{ 0x026C, RGBDeviceType.Keyboard, "Huntsman V2", LedMappings.Keyboard, RazerEndpointType.Keyboard },
{ 0x028D, RGBDeviceType.Keyboard, "BlackWidow V4", LedMappings.Keyboard, RazerEndpointType.Keyboard },
{ 0x02A1, RGBDeviceType.Keyboard, "Ornata V3", LedMappings.Keyboard, RazerEndpointType.Keyboard },
// Mice
{ 0x0013, RGBDeviceType.Mouse, "Orochi 2011", LedMappings.Mouse, RazerEndpointType.Mouse },