1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Display conditions - Implemented IDisposable

Display conditions - Listen to registration add/remove WIP
Data bindings - Implemented IDisposable
Data bindings - Listen to registration add/remove
This commit is contained in:
SpoinkyNL 2020-09-09 23:01:36 +02:00
parent 11de30318e
commit 1de6fefc2a
17 changed files with 461 additions and 227 deletions

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Artemis.Core.Services;
using Artemis.Storage.Entities.Profile.Abstract;
@ -7,7 +8,7 @@ namespace Artemis.Core
/// <summary>
/// An abstract class for display condition parts
/// </summary>
public abstract class DisplayConditionPart
public abstract class DisplayConditionPart : IDisposable
{
private readonly List<DisplayConditionPart> _children = new List<DisplayConditionPart>();
@ -62,5 +63,23 @@ namespace Artemis.Core
internal abstract void ApplyToEntity();
internal abstract DisplayConditionPartEntity GetEntity();
#region IDisposable
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}

View File

@ -11,6 +11,8 @@ namespace Artemis.Core
/// </summary>
public class DisplayConditionGroup : DisplayConditionPart
{
private bool _disposed;
/// <summary>
/// Creates a new instance of the <see cref="DisplayConditionGroup" /> class
/// </summary>
@ -55,6 +57,9 @@ namespace Artemis.Core
/// <inheritdoc />
public override bool Evaluate()
{
if (_disposed)
throw new ObjectDisposedException("DisplayConditionGroup");
// Empty groups are always true
if (Children.Count == 0)
return true;
@ -80,6 +85,9 @@ namespace Artemis.Core
/// <inheritdoc />
public override bool EvaluateObject(object target)
{
if (_disposed)
throw new ObjectDisposedException("DisplayConditionGroup");
// Empty groups are always true
if (Children.Count == 0)
return true;
@ -111,6 +119,19 @@ namespace Artemis.Core
{
return Entity;
}
#region IDisposable
protected override void Dispose(bool disposing)
{
_disposed = true;
foreach (var child in Children)
child.Dispose();
base.Dispose(disposing);
}
#endregion
}
public enum BooleanOperator

View File

@ -10,6 +10,8 @@ namespace Artemis.Core
{
public class DisplayConditionList : DisplayConditionPart
{
private bool _disposed;
public DisplayConditionList(DisplayConditionPart parent)
{
Parent = parent;
@ -37,6 +39,9 @@ namespace Artemis.Core
public override bool Evaluate()
{
if (_disposed)
throw new ObjectDisposedException("DisplayConditionList");
if (CompiledListAccessor == null)
return false;
@ -45,6 +50,9 @@ namespace Artemis.Core
public override bool EvaluateObject(object target)
{
if (_disposed)
throw new ObjectDisposedException("DisplayConditionList");
if (!Children.Any())
return false;
if (!(target is IList list))
@ -63,6 +71,9 @@ namespace Artemis.Core
public void UpdateList(DataModel dataModel, string path)
{
if (_disposed)
throw new ObjectDisposedException("DisplayConditionList");
if (dataModel != null && path == null)
throw new ArtemisCoreException("If a data model is provided, a path is also required");
if (dataModel == null && path != null)
@ -93,6 +104,9 @@ namespace Artemis.Core
public void CreateExpression()
{
if (_disposed)
throw new ObjectDisposedException("DisplayConditionList");
var parameter = Expression.Parameter(typeof(object), "listDataModel");
var accessor = ListPropertyPath.Split('.').Aggregate<string, Expression>(
Expression.Convert(parameter, ListDataModel.GetType()),
@ -101,12 +115,15 @@ namespace Artemis.Core
var lambda = Expression.Lambda<Func<object, IList>>(accessor, parameter);
CompiledListAccessor = lambda.Compile();
}
internal override void ApplyToEntity()
{
// Target list
Entity.ListDataModelGuid = ListDataModel?.PluginInfo?.Guid;
Entity.ListPropertyPath = ListPropertyPath;
if (ListDataModel != null)
{
Entity.ListDataModelGuid = ListDataModel.PluginInfo.Guid;
Entity.ListPropertyPath = ListPropertyPath;
}
// Operator
Entity.ListOperator = (int) ListOperator;
@ -125,6 +142,8 @@ namespace Artemis.Core
internal void Initialize()
{
DataModelStore.DataModelAdded += DataModelStoreOnDataModelAdded;
DataModelStore.DataModelRemoved += DataModelStoreOnDataModelRemoved;
if (Entity.ListDataModelGuid == null)
return;
@ -147,6 +166,49 @@ namespace Artemis.Core
AddChild(new DisplayConditionGroup(this));
}
}
#region Event handlers
private void DataModelStoreOnDataModelAdded(object sender, DataModelStoreEvent e)
{
var dataModel = e.Registration.DataModel;
if (dataModel.PluginInfo.Guid == Entity.ListDataModelGuid && dataModel.ContainsPath(Entity.ListPropertyPath))
{
ListDataModel = dataModel;
ListPropertyPath = Entity.ListPropertyPath;
CreateExpression();
}
}
private void DataModelStoreOnDataModelRemoved(object sender, DataModelStoreEvent e)
{
if (ListDataModel != e.Registration.DataModel)
return;
ListDataModel = null;
CompiledListAccessor = null;
}
#endregion
#region IDisposable
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
_disposed = true;
DataModelStore.DataModelAdded -= DataModelStoreOnDataModelAdded;
DataModelStore.DataModelRemoved -= DataModelStoreOnDataModelRemoved;
foreach (var child in Children)
child.Dispose();
base.Dispose(disposing);
}
#endregion
}
public enum ListOperator

View File

@ -248,6 +248,9 @@ namespace Artemis.Core
internal void Initialize()
{
DataModelStore.DataModelAdded += DataModelStoreOnDataModelAdded;
DataModelStore.DataModelRemoved += DataModelStoreOnDataModelRemoved;
// Left side
if (Entity.LeftDataModelGuid != null)
{
@ -424,5 +427,42 @@ namespace Artemis.Core
Expression.Property
);
}
#region Event handlers
private void DataModelStoreOnDataModelAdded(object sender, DataModelStoreEvent e)
{
var dataModel = e.Registration.DataModel;
if (dataModel.PluginInfo.Guid == Entity.LeftDataModelGuid && dataModel.ContainsPath(Entity.LeftPropertyPath))
UpdateLeftSide(dataModel, Entity.LeftPropertyPath);
if (dataModel.PluginInfo.Guid == Entity.RightDataModelGuid && dataModel.ContainsPath(Entity.RightPropertyPath))
UpdateRightSide(dataModel, Entity.RightPropertyPath);
}
private void DataModelStoreOnDataModelRemoved(object sender, DataModelStoreEvent e)
{
if (LeftDataModel == e.Registration.DataModel)
{
CompiledDynamicPredicate = null;
LeftDataModel = null;
}
if (RightDataModel == e.Registration.DataModel)
{
CompiledDynamicPredicate = null;
RightDataModel = null;
}
}
#endregion
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
DataModelStore.DataModelAdded -= DataModelStoreOnDataModelAdded;
DataModelStore.DataModelRemoved -= DataModelStoreOnDataModelRemoved;
base.Dispose(disposing);
}
}
}

View File

@ -4,7 +4,6 @@ using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Artemis.Core.DataModelExpansions;
using Artemis.Core.Services;
using Artemis.Storage.Entities.Profile.DataBindings;
namespace Artemis.Core
@ -17,6 +16,7 @@ namespace Artemis.Core
private TProperty _currentValue;
private TimeSpan _easingProgress;
private TProperty _previousValue;
private bool _disposed;
internal DataBinding(DataBindingRegistration<TLayerProperty, TProperty> dataBindingRegistration)
{
@ -25,6 +25,7 @@ namespace Artemis.Core
ApplyRegistration(dataBindingRegistration);
Save();
Initialize();
}
internal DataBinding(LayerProperty<TLayerProperty> layerProperty, DataBindingEntity entity)
@ -32,6 +33,8 @@ namespace Artemis.Core
LayerProperty = layerProperty;
Entity = entity;
// Load will add children so be initialized before that
Initialize();
Load();
}
@ -95,6 +98,9 @@ namespace Artemis.Core
/// <param name="deltaTime">The time in seconds that passed since the last update</param>
public void Update(double deltaTime)
{
if (_disposed)
throw new ObjectDisposedException("DataBinding");
// Data bindings cannot go back in time like brushes
deltaTime = Math.Max(0, deltaTime);
@ -103,11 +109,27 @@ namespace Artemis.Core
_easingProgress = EasingTime;
}
/// <inheritdoc />
public void Apply()
{
if (_disposed)
throw new ObjectDisposedException("DataBinding");
if (Converter == null)
return;
var value = GetValue(Converter.GetValue());
Converter.ApplyValue(GetValue(value));
}
/// <summary>
/// Adds a modifier to the data binding's <see cref="Modifiers" /> collection
/// </summary>
public void AddModifier(DataBindingModifier<TLayerProperty, TProperty> modifier)
{
if (_disposed)
throw new ObjectDisposedException("DataBinding");
if (!_modifiers.Contains(modifier))
{
modifier.DataBinding = this;
@ -122,6 +144,9 @@ namespace Artemis.Core
/// </summary>
public void RemoveModifier(DataBindingModifier<TLayerProperty, TProperty> modifier)
{
if (_disposed)
throw new ObjectDisposedException("DataBinding");
if (_modifiers.Contains(modifier))
{
modifier.DataBinding = null;
@ -138,6 +163,9 @@ namespace Artemis.Core
/// <param name="path">The path pointing to the source inside the data model</param>
public void UpdateSource(DataModel dataModel, string path)
{
if (_disposed)
throw new ObjectDisposedException("DataBinding");
if (dataModel != null && path == null)
throw new ArtemisCoreException("If a data model is provided, a path is also required");
if (dataModel == null && path != null)
@ -161,6 +189,9 @@ namespace Artemis.Core
/// <returns></returns>
public TProperty GetValue(TProperty baseValue)
{
if (_disposed)
throw new ObjectDisposedException("DataBinding");
if (CompiledTargetAccessor == null || Converter == null)
return baseValue;
@ -198,30 +229,18 @@ namespace Artemis.Core
return SourceDataModel?.GetTypeAtPath(SourcePropertyPath);
}
/// <inheritdoc />
public void Apply()
private void Initialize()
{
if (Converter == null)
return;
DataModelStore.DataModelAdded += DataModelStoreOnDataModelAdded;
DataModelStore.DataModelRemoved += DataModelStoreOnDataModelRemoved;
var value = GetValue(Converter.GetValue());
Converter.ApplyValue(GetValue(value));
}
/// <inheritdoc />
public void Initialize(IDataModelService dataModelService, IDataBindingService dataBindingService)
{
// Source
if (Entity.SourceDataModelGuid != null && SourceDataModel == null)
{
var dataModel = dataModelService.GetPluginDataModelByGuid(Entity.SourceDataModelGuid.Value);
var dataModel = DataModelStore.Get(Entity.SourceDataModelGuid.Value)?.DataModel;
if (dataModel != null && dataModel.ContainsPath(Entity.SourcePropertyPath))
UpdateSource(dataModel, Entity.SourcePropertyPath);
}
// Modifiers
foreach (var dataBindingModifier in Modifiers)
dataBindingModifier.Initialize(dataModelService, dataBindingService);
}
private void ResetEasing(TProperty value)
@ -283,6 +302,8 @@ namespace Artemis.Core
/// <inheritdoc />
public void Load()
{
if (_disposed)
throw new ObjectDisposedException("DataBinding");
// General
var registration = LayerProperty.GetDataBindingRegistration<TProperty>(Entity.TargetProperty);
ApplyRegistration(registration);
@ -301,6 +322,9 @@ namespace Artemis.Core
/// <inheritdoc />
public void Save()
{
if (_disposed)
throw new ObjectDisposedException("DataBinding");
if (!LayerProperty.Entity.DataBindingEntities.Contains(Entity))
LayerProperty.Entity.DataBindingEntities.Add(Entity);
@ -311,9 +335,12 @@ namespace Artemis.Core
Entity.EasingFunction = (int) EasingFunction;
// Data model
Entity.SourceDataModelGuid = SourceDataModel?.PluginInfo?.Guid;
Entity.SourcePropertyPath = SourcePropertyPath;
if (SourceDataModel != null)
{
Entity.SourceDataModelGuid = SourceDataModel.PluginInfo.Guid;
Entity.SourcePropertyPath = SourcePropertyPath;
}
// Modifiers
Entity.Modifiers.Clear();
foreach (var dataBindingModifier in Modifiers)
@ -322,6 +349,25 @@ namespace Artemis.Core
#endregion
#region Event handlers
private void DataModelStoreOnDataModelAdded(object sender, DataModelStoreEvent e)
{
var dataModel = e.Registration.DataModel;
if (dataModel.PluginInfo.Guid == Entity.SourceDataModelGuid && dataModel.ContainsPath(Entity.SourcePropertyPath))
UpdateSource(dataModel, Entity.SourcePropertyPath);
}
private void DataModelStoreOnDataModelRemoved(object sender, DataModelStoreEvent e)
{
if (SourceDataModel != e.Registration.DataModel)
return;
SourceDataModel = null;
CompiledTargetAccessor = null;
}
#endregion
#region Events
/// <summary>
@ -335,6 +381,18 @@ namespace Artemis.Core
}
#endregion
/// <inheritdoc />
public void Dispose()
{
_disposed = true;
DataModelStore.DataModelAdded -= DataModelStoreOnDataModelAdded;
DataModelStore.DataModelRemoved -= DataModelStoreOnDataModelRemoved;
foreach (var dataBindingModifier in Modifiers)
dataBindingModifier.Dispose();
}
}
/// <summary>

View File

@ -2,7 +2,6 @@
using System.Linq;
using System.Linq.Expressions;
using Artemis.Core.DataModelExpansions;
using Artemis.Core.Services;
using Artemis.Storage.Entities.Profile.DataBindings;
using Newtonsoft.Json;
@ -12,6 +11,7 @@ namespace Artemis.Core
public class DataBindingModifier<TLayerProperty, TProperty> : IDataBindingModifier
{
private DataBinding<TLayerProperty, TProperty> _dataBinding;
private bool _disposed;
/// <summary>
/// Creates a new instance of the <see cref="DataBindingModifier{TLayerProperty,TProperty}" /> class
@ -22,6 +22,7 @@ namespace Artemis.Core
ParameterType = parameterType;
Entity = new DataBindingModifierEntity();
Save();
Initialize();
}
internal DataBindingModifier(DataBinding<TLayerProperty, TProperty> dataBinding, DataBindingModifierEntity entity)
@ -29,6 +30,7 @@ namespace Artemis.Core
DataBinding = dataBinding;
Entity = entity;
Load();
Initialize();
}
/// <summary>
@ -82,6 +84,52 @@ namespace Artemis.Core
internal DataBindingModifierEntity Entity { get; set; }
/// <inheritdoc />
public void Save()
{
if (_disposed)
throw new ObjectDisposedException("DataBindingModifier");
if (!DataBinding.Entity.Modifiers.Contains(Entity))
DataBinding.Entity.Modifiers.Add(Entity);
// Modifier
if (ModifierType != null)
{
Entity.ModifierType = ModifierType.GetType().Name;
Entity.ModifierTypePluginGuid = ModifierType.PluginInfo.Guid;
}
// General
Entity.Order = Order;
Entity.ParameterType = (int) ParameterType;
// Parameter
if (ParameterDataModel != null)
{
Entity.ParameterDataModelGuid = ParameterDataModel.PluginInfo.Guid;
Entity.ParameterPropertyPath = ParameterPropertyPath;
}
Entity.ParameterStaticValue = JsonConvert.SerializeObject(ParameterStaticValue);
}
/// <inheritdoc />
public void Load()
{
if (_disposed)
throw new ObjectDisposedException("DataBindingModifier");
// Modifier type is done during Initialize
// General
Order = Entity.Order;
ParameterType = (ProfileRightSideType) Entity.ParameterType;
// Parameter is done during initialize
}
/// <summary>
/// Applies the modifier to the provided value
/// </summary>
@ -89,6 +137,9 @@ namespace Artemis.Core
/// <returns>The modified value</returns>
public object Apply(object currentValue)
{
if (_disposed)
throw new ObjectDisposedException("DataBindingModifier");
if (ModifierType == null)
return currentValue;
@ -113,6 +164,9 @@ namespace Artemis.Core
/// <param name="modifierType"></param>
public void UpdateModifierType(DataBindingModifierType modifierType)
{
if (_disposed)
throw new ObjectDisposedException("DataBindingModifier");
// Calling CreateExpression will clear compiled expressions
if (modifierType == null)
{
@ -139,6 +193,9 @@ namespace Artemis.Core
/// <param name="path">The path pointing to the parameter inside the data model</param>
public void UpdateParameter(DataModel dataModel, string path)
{
if (_disposed)
throw new ObjectDisposedException("DataBindingModifier");
if (dataModel != null && path == null)
throw new ArtemisCoreException("If a data model is provided, a path is also required");
if (dataModel == null && path != null)
@ -163,6 +220,9 @@ namespace Artemis.Core
/// <param name="staticValue">The static value to use as a parameter</param>
public void UpdateParameter(object staticValue)
{
if (_disposed)
throw new ObjectDisposedException("DataBindingModifier");
ParameterType = ProfileRightSideType.Static;
ParameterDataModel = null;
ParameterPropertyPath = null;
@ -183,12 +243,17 @@ namespace Artemis.Core
CreateExpression();
}
internal void Initialize(IDataModelService dataModelService, IDataBindingService dataBindingService)
private void Initialize()
{
DataBindingModifierTypeStore.DataBindingModifierAdded += DataBindingModifierTypeStoreOnDataBindingModifierAdded;
DataBindingModifierTypeStore.DataBindingModifierRemoved += DataBindingModifierTypeStoreOnDataBindingModifierRemoved;
DataModelStore.DataModelAdded += DataModelStoreOnDataModelAdded;
DataModelStore.DataModelRemoved += DataModelStoreOnDataModelRemoved;
// Modifier type
if (Entity.ModifierTypePluginGuid != null && ModifierType == null)
{
var modifierType = dataBindingService.GetModifierType(Entity.ModifierTypePluginGuid.Value, Entity.ModifierType);
var modifierType = DataBindingModifierTypeStore.Get(Entity.ModifierTypePluginGuid.Value, Entity.ModifierType)?.DataBindingModifierType;
if (modifierType != null)
UpdateModifierType(modifierType);
}
@ -196,7 +261,7 @@ namespace Artemis.Core
// Dynamic parameter
if (ParameterType == ProfileRightSideType.Dynamic && Entity.ParameterDataModelGuid != null && ParameterDataModel == null)
{
var dataModel = dataModelService.GetPluginDataModelByGuid(Entity.ParameterDataModelGuid.Value);
var dataModel = DataModelStore.Get(Entity.ParameterDataModelGuid.Value)?.DataModel;
if (dataModel != null && dataModel.ContainsPath(Entity.ParameterPropertyPath))
UpdateParameter(dataModel, Entity.ParameterPropertyPath);
}
@ -214,7 +279,7 @@ namespace Artemis.Core
// If deserialization fails, use the type's default
catch (JsonSerializationException e)
{
dataBindingService.LogModifierDeserializationFailure(GetType().Name, e);
DeserializationLogger.LogModifierDeserializationFailure(GetType().Name, e);
staticValue = Activator.CreateInstance(targetType);
}
@ -222,39 +287,6 @@ namespace Artemis.Core
}
}
/// <inheritdoc />
public void Save()
{
if (!DataBinding.Entity.Modifiers.Contains(Entity))
DataBinding.Entity.Modifiers.Add(Entity);
// Modifier
Entity.ModifierType = ModifierType?.GetType().Name;
Entity.ModifierTypePluginGuid = ModifierType?.PluginInfo.Guid;
// General
Entity.Order = Order;
Entity.ParameterType = (int) ParameterType;
// Parameter
Entity.ParameterDataModelGuid = ParameterDataModel?.PluginInfo.Guid;
Entity.ParameterPropertyPath = ParameterPropertyPath;
Entity.ParameterStaticValue = JsonConvert.SerializeObject(ParameterStaticValue);
}
/// <inheritdoc />
public void Load()
{
// Modifier type is done during Initialize
// General
Order = Entity.Order;
ParameterType = (ProfileRightSideType) Entity.ParameterType;
// Parameter is done during initialize
}
private void CreateExpression()
{
CompiledParameterAccessor = null;
@ -286,5 +318,51 @@ namespace Artemis.Core
Expression.Property
);
}
#region Event handlers
private void DataBindingModifierTypeStoreOnDataBindingModifierAdded(object sender, DataBindingModifierTypeStoreEvent e)
{
if (ModifierType != null)
return;
var modifierType = e.TypeRegistration.DataBindingModifierType;
if (modifierType.PluginInfo.Guid == Entity.ModifierTypePluginGuid && modifierType.GetType().Name == Entity.ModifierType)
UpdateModifierType(modifierType);
}
private void DataBindingModifierTypeStoreOnDataBindingModifierRemoved(object sender, DataBindingModifierTypeStoreEvent e)
{
if (e.TypeRegistration.DataBindingModifierType == ModifierType)
UpdateModifierType(null);
}
private void DataModelStoreOnDataModelAdded(object sender, DataModelStoreEvent e)
{
var dataModel = e.Registration.DataModel;
if (dataModel.PluginInfo.Guid == Entity.ParameterDataModelGuid && dataModel.ContainsPath(Entity.ParameterPropertyPath))
UpdateParameter(dataModel, Entity.ParameterPropertyPath);
}
private void DataModelStoreOnDataModelRemoved(object sender, DataModelStoreEvent e)
{
if (e.Registration.DataModel != ParameterDataModel)
return;
ParameterDataModel = null;
CompiledParameterAccessor = null;
}
#endregion
/// <inheritdoc />
public void Dispose()
{
_disposed = true;
DataBindingModifierTypeStore.DataBindingModifierAdded -= DataBindingModifierTypeStoreOnDataBindingModifierAdded;
DataBindingModifierTypeStore.DataBindingModifierRemoved -= DataBindingModifierTypeStoreOnDataBindingModifierRemoved;
DataModelStore.DataModelAdded -= DataModelStoreOnDataModelAdded;
DataModelStore.DataModelRemoved -= DataModelStoreOnDataModelRemoved;
}
}
}

View File

@ -1,20 +1,14 @@
using Artemis.Core.DataModelExpansions;
using Artemis.Core.Services;
using System;
using Artemis.Core.DataModelExpansions;
namespace Artemis.Core
{
/// <summary>
/// Represents a data binding that binds a certain <see cref="LayerProperty{T}" /> to a value inside a <see cref="DataModel" />
/// Represents a data binding that binds a certain <see cref="LayerProperty{T}" /> to a value inside a
/// <see cref="DataModel" />
/// </summary>
public interface IDataBinding : IStorageModel, IUpdateModel
public interface IDataBinding : IStorageModel, IUpdateModel, IDisposable
{
/// <summary>
/// (Re)initializes the data binding
/// </summary>
/// <param name="dataModelService"></param>
/// <param name="dataBindingService"></param>
void Initialize(IDataModelService dataModelService, IDataBindingService dataBindingService);
/// <summary>
/// Applies the data binding to the layer property
/// </summary>

View File

@ -1,9 +1,11 @@
namespace Artemis.Core
using System;
namespace Artemis.Core
{
/// <summary>
/// Modifies a data model value in a way defined by the modifier type
/// </summary>
public interface IDataBindingModifier : IStorageModel
public interface IDataBindingModifier : IStorageModel, IDisposable
{
}
}

View File

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Artemis.Core.Services;
namespace Artemis.Core
{
@ -10,9 +9,6 @@ namespace Artemis.Core
/// </summary>
public abstract class DataBindingModifierType
{
private IDataBindingService _dataBindingService;
private bool _registered;
/// <summary>
/// Gets the plugin info this data binding modifier belongs to
/// <para>Note: Not set until after registering</para>
@ -63,35 +59,5 @@ namespace Artemis.Core
/// </param>
/// <returns>The modified value, must be a value of a type contained in <see cref="CompatibleTypes" /></returns>
public abstract object Apply(object currentValue, object parameterValue);
internal void Register(PluginInfo pluginInfo, IDataBindingService dataBindingService)
{
if (_registered)
return;
PluginInfo = pluginInfo;
_dataBindingService = dataBindingService;
if (PluginInfo != Constants.CorePluginInfo)
PluginInfo.Instance.PluginDisabled += InstanceOnPluginDisabled;
_registered = true;
}
internal void Unsubscribe()
{
if (!_registered)
return;
if (PluginInfo != Constants.CorePluginInfo)
PluginInfo.Instance.PluginDisabled -= InstanceOnPluginDisabled;
_registered = false;
}
private void InstanceOnPluginDisabled(object sender, EventArgs e)
{
// The service will call Unsubscribe
_dataBindingService.RemoveModifierType(this);
}
}
}

View File

@ -1,4 +1,5 @@
using Artemis.Storage.Entities.Profile;
using System;
using Artemis.Storage.Entities.Profile;
namespace Artemis.Core
{
@ -9,7 +10,7 @@ namespace Artemis.Core
/// initialize these for you.
/// </para>
/// </summary>
public interface ILayerProperty : IStorageModel, IUpdateModel
public interface ILayerProperty : IStorageModel, IUpdateModel, IDisposable
{
/// <summary>
/// Initializes the layer property

View File

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Artemis.Core.Services;
using Artemis.Storage.Entities.Profile;
using Newtonsoft.Json;
@ -19,6 +18,8 @@ namespace Artemis.Core
/// <typeparam name="T">The type of property encapsulated in this layer property</typeparam>
public abstract class LayerProperty<T> : ILayerProperty
{
private bool _disposed;
/// <summary>
/// Creates a new instance of the <see cref="LayerProperty{T}" /> class
/// </summary>
@ -37,6 +38,9 @@ namespace Artemis.Core
/// </summary>
public void Update(double deltaTime)
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
CurrentValue = BaseValue;
UpdateKeyframes();
@ -136,6 +140,9 @@ namespace Artemis.Core
/// </param>
public void SetCurrentValue(T value, TimeSpan? time)
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
if (time == null || !KeyframesEnabled || !KeyframesSupported)
BaseValue = value;
else
@ -159,6 +166,9 @@ namespace Artemis.Core
/// </summary>
public void ApplyDefaultValue()
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
BaseValue = DefaultValue;
CurrentValue = DefaultValue;
}
@ -212,6 +222,9 @@ namespace Artemis.Core
/// <param name="keyframe">The keyframe to add</param>
public void AddKeyframe(LayerPropertyKeyframe<T> keyframe)
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
if (_keyframes.Contains(keyframe))
return;
@ -229,6 +242,9 @@ namespace Artemis.Core
/// <param name="keyframe">The keyframe to remove</param>
public LayerPropertyKeyframe<T> CopyKeyframe(LayerPropertyKeyframe<T> keyframe)
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
var newKeyframe = new LayerPropertyKeyframe<T>(
keyframe.Value,
keyframe.Position,
@ -246,6 +262,9 @@ namespace Artemis.Core
/// <param name="keyframe">The keyframe to remove</param>
public void RemoveKeyframe(LayerPropertyKeyframe<T> keyframe)
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
if (!_keyframes.Contains(keyframe))
return;
@ -260,6 +279,9 @@ namespace Artemis.Core
/// </summary>
public void ClearKeyframes()
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
var keyframes = new List<LayerPropertyKeyframe<T>>(_keyframes);
foreach (var layerPropertyKeyframe in keyframes)
RemoveKeyframe(layerPropertyKeyframe);
@ -313,6 +335,9 @@ namespace Artemis.Core
public DataBindingRegistration<T, TProperty> GetDataBindingRegistration<TProperty>(string propertyName)
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
var match = _dataBindingRegistrations.FirstOrDefault(r => r is DataBindingRegistration<T, TProperty> registration &&
registration.Property.Name == propertyName);
return (DataBindingRegistration<T, TProperty>) match;
@ -320,6 +345,9 @@ namespace Artemis.Core
public void RegisterDataBindingProperty<TProperty>(Expression<Func<T, TProperty>> propertyLambda, DataBindingConverter<T, TProperty> converter)
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
// If the lambda references to itself, use the property info of public new T CurrentValue
PropertyInfo propertyInfo;
string path = null;
@ -353,6 +381,9 @@ namespace Artemis.Core
/// <returns>The newly created data binding</returns>
public DataBinding<T, TProperty> EnableDataBinding<TProperty>(DataBindingRegistration<T, TProperty> dataBindingRegistration)
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
if (dataBindingRegistration.LayerProperty != this)
throw new ArtemisCoreException("Cannot enable a data binding using a data binding registration of a different layer property");
@ -368,6 +399,9 @@ namespace Artemis.Core
/// <param name="dataBinding">The data binding to remove</param>
public void DisableDataBinding<TProperty>(DataBinding<T, TProperty> dataBinding)
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
_dataBindings.Remove(dataBinding);
}
@ -380,12 +414,6 @@ namespace Artemis.Core
}
}
internal void InitializeDataBindings(IDataModelService dataModelService, IDataBindingService dataBindingService)
{
foreach (var dataBinding in _dataBindings)
dataBinding.Initialize(dataModelService, dataBindingService);
}
#endregion
#region Storage
@ -402,6 +430,9 @@ namespace Artemis.Core
/// <inheritdoc />
public void Initialize(RenderProfileElement profileElement, LayerPropertyGroup group, PropertyEntity entity, bool fromStorage, PropertyDescriptionAttribute description)
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
_isInitialized = true;
ProfileElement = profileElement ?? throw new ArgumentNullException(nameof(profileElement));
@ -409,13 +440,16 @@ namespace Artemis.Core
Entity = entity ?? throw new ArgumentNullException(nameof(entity));
PropertyDescription = description ?? throw new ArgumentNullException(nameof(description));
IsLoadedFromStorage = fromStorage;
LayerPropertyGroup.PropertyGroupUpdating += (sender, args) => Update(args.DeltaTime);
}
/// <inheritdoc />
public void Load()
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
if (!_isInitialized)
throw new ArtemisCoreException("Layer property is not yet initialized");
@ -467,6 +501,9 @@ namespace Artemis.Core
/// </summary>
public void Save()
{
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
if (!_isInitialized)
throw new ArtemisCoreException("Layer property is not yet initialized");
@ -550,5 +587,14 @@ namespace Artemis.Core
}
#endregion
/// <inheritdoc />
public void Dispose()
{
_disposed = true;
foreach (var dataBinding in _dataBindings)
dataBinding.Dispose();
}
}
}

View File

@ -6,7 +6,6 @@ using System.Reflection;
using Artemis.Core.LayerBrushes;
using Artemis.Core.LayerEffects;
using Artemis.Core.Properties;
using Artemis.Core.Services;
using Artemis.Storage.Entities.Profile;
namespace Artemis.Core
@ -15,6 +14,7 @@ namespace Artemis.Core
{
private readonly List<ILayerProperty> _layerProperties;
private readonly List<LayerPropertyGroup> _layerPropertyGroups;
private bool _disposed;
private bool _isHidden;
protected LayerPropertyGroup()
@ -23,6 +23,9 @@ namespace Artemis.Core
_layerPropertyGroups = new List<LayerPropertyGroup>();
}
/// <summary>
/// Gets the description of this group
/// </summary>
public PropertyGroupDescriptionAttribute GroupDescription { get; internal set; }
/// <summary>
@ -83,19 +86,30 @@ namespace Artemis.Core
/// </summary>
public ReadOnlyCollection<LayerPropertyGroup> LayerPropertyGroups => _layerPropertyGroups.AsReadOnly();
#region IDisposable
/// <inheritdoc />
public void Dispose()
{
_disposed = true;
DisableProperties();
foreach (var layerProperty in _layerProperties)
layerProperty.Dispose();
foreach (var layerPropertyGroup in _layerPropertyGroups)
layerPropertyGroup.Dispose();
}
#endregion
/// <summary>
/// Recursively gets all layer properties on this group and any subgroups
/// </summary>
public IReadOnlyCollection<ILayerProperty> GetAllLayerProperties()
{
if (_disposed)
throw new ObjectDisposedException("LayerPropertyGroup");
if (!PropertiesInitialized)
return new List<ILayerProperty>();
@ -240,7 +254,7 @@ namespace Artemis.Core
return entity;
}
#region Events
internal event EventHandler<LayerPropertyGroupUpdatingEventArgs> PropertyGroupUpdating;

View File

@ -34,7 +34,7 @@ namespace Artemis.Core
UndoStack = new Stack<string>();
RedoStack = new Stack<string>();
ApplyToProfile();
Load();
}
public ProfileModule Module { get; }
@ -86,7 +86,7 @@ namespace Artemis.Core
return (Folder) Children.Single();
}
public void ApplyToProfile()
internal override void Load()
{
if (_disposed)
throw new ObjectDisposedException("Profile");

View File

@ -12,6 +12,12 @@ namespace Artemis.Core
{
public abstract class RenderProfileElement : ProfileElement
{
protected RenderProfileElement()
{
LayerEffectStore.LayerEffectAdded += LayerEffectStoreOnLayerEffectAdded;
LayerEffectStore.LayerEffectRemoved += LayerEffectStoreOnLayerEffectRemoved;
}
internal void ApplyRenderElementDefaults()
{
MainSegmentLength = TimeSpan.FromSeconds(5);
@ -270,6 +276,7 @@ namespace Artemis.Core
}
}
internal void ActivateLayerEffect(BaseLayerEffect layerEffect)
{
_layerEffects.Add(layerEffect);
@ -285,6 +292,16 @@ namespace Artemis.Core
OnLayerEffectsUpdated();
}
private void LayerEffectStoreOnLayerEffectRemoved(object? sender, LayerEffectStoreEvent e)
{
throw new NotImplementedException();
}
private void LayerEffectStoreOnLayerEffectAdded(object? sender, LayerEffectStoreEvent e)
{
ActivateEffects();
}
#endregion
#region Conditions

View File

@ -17,21 +17,17 @@ namespace Artemis.Core.Services
private readonly ILogger _logger;
private readonly IPluginService _pluginService;
private readonly IProfileRepository _profileRepository;
private readonly IRenderElementService _renderElementService;
private readonly ISurfaceService _surfaceService;
internal ProfileService(ILogger logger, IPluginService pluginService, ISurfaceService surfaceService, IRenderElementService renderElementService, IProfileRepository profileRepository)
internal ProfileService(ILogger logger, IPluginService pluginService, ISurfaceService surfaceService, IProfileRepository profileRepository)
{
_logger = logger;
_pluginService = pluginService;
_surfaceService = surfaceService;
_renderElementService = renderElementService;
_profileRepository = profileRepository;
_surfaceService.ActiveSurfaceConfigurationSelected += OnActiveSurfaceConfigurationSelected;
_surfaceService.SurfaceConfigurationUpdated += OnSurfaceConfigurationUpdated;
_pluginService.PluginEnabled += OnPluginToggled;
_pluginService.PluginDisabled += OnPluginToggled;
}
public JsonSerializerSettings MementoSettings { get; set; } = new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.All};
@ -186,7 +182,7 @@ namespace Artemis.Core.Services
profile.RedoStack.Push(memento);
profile.ProfileEntity = JsonConvert.DeserializeObject<ProfileEntity>(top, MementoSettings);
profile.ApplyToProfile();
profile.Load();
InstantiateProfile(profile);
}
@ -210,7 +206,7 @@ namespace Artemis.Core.Services
profile.UndoStack.Push(memento);
profile.ProfileEntity = JsonConvert.DeserializeObject<ProfileEntity>(top, MementoSettings);
profile.ApplyToProfile();
profile.Load();
InstantiateProfile(profile);
_logger.Debug("Redo profile update - Success");
@ -221,9 +217,6 @@ namespace Artemis.Core.Services
public void InstantiateProfile(Profile profile)
{
profile.PopulateLeds(_surfaceService.ActiveSurface);
InitializeLayerProperties(profile);
InstantiateLayers(profile);
InstantiateFolders(profile);
}
public string ExportProfile(ProfileDescriptor profileDescriptor)
@ -268,67 +261,7 @@ namespace Artemis.Core.Services
_profileRepository.Save(profileEntity);
}
}
/// <summary>
/// Initializes the properties on the layers of the given profile
/// </summary>
/// <param name="profile"></param>
private void InitializeLayerProperties(Profile profile)
{
foreach (var layer in profile.GetAllLayers())
{
if (!layer.General.PropertiesInitialized)
layer.General.Initialize(layer, "General.", Constants.CorePluginInfo);
if (!layer.Transform.PropertiesInitialized)
layer.Transform.Initialize(layer, "Transform.", Constants.CorePluginInfo);
}
}
/// <summary>
/// Instantiates all plugin-related classes on the folders of the given profile
/// </summary>
private void InstantiateFolders(Profile profile)
{
foreach (var folder in profile.GetAllFolders())
{
// Instantiate effects
_renderElementService.InstantiateLayerEffects(folder);
// Remove effects of plugins that are disabled
var disabledEffects = new List<BaseLayerEffect>(folder.LayerEffects.Where(layerLayerEffect => !layerLayerEffect.PluginInfo.Enabled));
foreach (var layerLayerEffect in disabledEffects)
_renderElementService.RemoveLayerEffect(layerLayerEffect);
_renderElementService.InstantiateDisplayConditions(folder);
_renderElementService.InstantiateDataBindings(folder);
}
}
/// <summary>
/// Instantiates all plugin-related classes on the layers of the given profile
/// </summary>
private void InstantiateLayers(Profile profile)
{
foreach (var layer in profile.GetAllLayers())
{
// Instantiate brush
if (layer.LayerBrush == null)
_renderElementService.InstantiateLayerBrush(layer);
// Remove brush if plugin is disabled
else if (!layer.LayerBrush.PluginInfo.Enabled)
_renderElementService.DeactivateLayerBrush(layer);
// Instantiate effects
_renderElementService.InstantiateLayerEffects(layer);
// Remove effects of plugins that are disabled
var disabledEffects = new List<BaseLayerEffect>(layer.LayerEffects.Where(layerLayerEffect => !layerLayerEffect.PluginInfo.Enabled));
foreach (var layerLayerEffect in disabledEffects)
_renderElementService.RemoveLayerEffect(layerLayerEffect);
_renderElementService.InstantiateDisplayConditions(layer);
_renderElementService.InstantiateDataBindings(layer);
}
}
/// <summary>
/// Populates all missing LEDs on all currently active profiles
/// </summary>
@ -339,21 +272,7 @@ namespace Artemis.Core.Services
foreach (var profileModule in profileModules.Where(p => p.ActiveProfile != null).ToList())
profileModule.ActiveProfile.PopulateLeds(surface);
}
/// <summary>
/// Instantiates all missing plugin-related classes on the profile trees of all currently active profiles
/// </summary>
private void ActiveProfilesInstantiatePlugins()
{
var profileModules = _pluginService.GetPluginsOfType<ProfileModule>();
foreach (var profileModule in profileModules.Where(p => p.ActiveProfile != null).ToList())
{
InstantiateLayers(profileModule.ActiveProfile);
InstantiateFolders(profileModule.ActiveProfile);
}
}
#region Event handlers
private void OnActiveSurfaceConfigurationSelected(object sender, SurfaceConfigurationEventArgs e)
@ -366,15 +285,7 @@ namespace Artemis.Core.Services
if (e.Surface.IsActive)
ActiveProfilesPopulateLeds(e.Surface);
}
private void OnPluginToggled(object sender, PluginEventArgs e)
{
if (e.PluginInfo.Instance is LayerBrushProvider)
ActiveProfilesInstantiatePlugins();
if (e.PluginInfo.Instance is LayerEffectProvider)
ActiveProfilesInstantiatePlugins();
}
#endregion
}
}

View File

@ -37,21 +37,26 @@ namespace Artemis.Plugins.LayerBrushes.Color.PropertyGroups
protected override void EnableProperties()
{
GradientType.BaseValueChanged += OnBaseValueChanged;
GradientType.BaseValueChanged += GradientTypeOnBaseValueChanged;
if (ProfileElement is Layer layer)
layer.General.ResizeMode.BaseValueChanged += OnBaseValueChanged;
layer.General.ResizeMode.BaseValueChanged += ResizeModeOnBaseValueChanged;
UpdateVisibility();
}
protected override void DisableProperties()
{
GradientType.BaseValueChanged -= OnBaseValueChanged;
GradientType.BaseValueChanged -= GradientTypeOnBaseValueChanged;
if (ProfileElement is Layer layer)
layer.General.ResizeMode.BaseValueChanged -= OnBaseValueChanged;
layer.General.ResizeMode.BaseValueChanged -= ResizeModeOnBaseValueChanged;
}
private void OnBaseValueChanged(object sender, LayerPropertyEventArgs e)
private void GradientTypeOnBaseValueChanged(object sender, LayerPropertyEventArgs<ColorType> e)
{
UpdateVisibility();
}
private void ResizeModeOnBaseValueChanged(object sender, LayerPropertyEventArgs<LayerResizeMode> e)
{
UpdateVisibility();
}

View File

@ -18,7 +18,7 @@ namespace Artemis.Plugins.LayerBrushes.Color.PropertyGroups
protected override void EnableProperties()
{
if (ProfileElement is Layer layer)
layer.General.ResizeMode.BaseValueChanged += OnBaseValueChanged;
layer.General.ResizeMode.BaseValueChanged += ResizeModeOnBaseValueChanged;
UpdateVisibility();
}
@ -26,10 +26,10 @@ namespace Artemis.Plugins.LayerBrushes.Color.PropertyGroups
protected override void DisableProperties()
{
if (ProfileElement is Layer layer)
layer.General.ResizeMode.BaseValueChanged -= OnBaseValueChanged;
layer.General.ResizeMode.BaseValueChanged -= ResizeModeOnBaseValueChanged;
}
private void OnBaseValueChanged(object sender, LayerPropertyEventArgs e)
private void ResizeModeOnBaseValueChanged(object sender, LayerPropertyEventArgs<LayerResizeMode> e)
{
UpdateVisibility();
}