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

Display conditions - Added basic models

Display conditions - Added externally registerable condition operators
Display conditions - Added GreaterThan condition for testing
This commit is contained in:
Robert 2020-07-03 20:49:56 +02:00
parent 6ee06b2fc5
commit 55555ed6a8
12 changed files with 248 additions and 10 deletions

View File

@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Reflection;
using Artemis.Core.Plugins.Models;
namespace Artemis.Core

View File

@ -0,0 +1,14 @@
using System.Collections.Generic;
namespace Artemis.Core.Models.Profile.Conditions.Abstract
{
public abstract class LayerConditionPart
{
protected LayerConditionPart()
{
Children = new List<LayerConditionPart>();
}
public List<LayerConditionPart> Children { get; set; }
}
}

View File

@ -7,5 +7,6 @@ namespace Artemis.Core.Models.Profile.Conditions
public class LayerCondition
{
public Expression<Func<DataModel, bool>> ExpressionTree { get; set; }
public LayerConditionGroup RootGroup { get; set; }
}
}

View File

@ -0,0 +1,17 @@
using Artemis.Core.Models.Profile.Conditions.Abstract;
namespace Artemis.Core.Models.Profile.Conditions
{
public class LayerConditionGroup : LayerConditionPart
{
public BooleanOperator BooleanOperator { get; set; }
}
public enum BooleanOperator
{
And,
Or,
AndNot,
OrNot
}
}

View File

@ -0,0 +1,18 @@
using Artemis.Core.Models.Profile.Conditions.Abstract;
namespace Artemis.Core.Models.Profile.Conditions
{
public class LayerConditionListStatement : LayerConditionPart
{
public ListOperator ListOperator { get; set; }
}
public enum ListOperator
{
Any,
All,
None,
Count
}
}

View File

@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Artemis.Core.Plugins.Models;
using Artemis.Core.Services.Interfaces;
namespace Artemis.Core.Models.Profile.Conditions
{
public abstract class LayerConditionOperator
{
private IDataModelService _dataModelService;
private bool _registered;
/// <summary>
/// Gets the plugin info this condition operator belongs to
/// <para>Note: Not set until after registering</para>
/// </summary>
public PluginInfo PluginInfo { get; internal set; }
/// <summary>
/// Gets the types this operator supports
/// </summary>
public abstract IReadOnlyCollection<Type> CompatibleTypes { get; }
/// <summary>
/// Gets or sets the description of this logical operator
/// </summary>
public string Description { get; set; }
/// <summary>
/// Gets or sets the icon of this logical operator
/// </summary>
public string Icon { get; set; }
/// <summary>
/// Creates a binary expression comparing two types
/// </summary>
/// <param name="leftSideType">The type of parameter passed to the left side of the expression</param>
/// <param name="rightSideType">The type of parameter passed to the right side of the expression</param>
/// <returns></returns>
public abstract BinaryExpression CreateExpression(Type leftSideType, Type rightSideType);
internal void Register(PluginInfo pluginInfo, IDataModelService dataModelService)
{
if (_registered)
return;
PluginInfo = pluginInfo;
_dataModelService = dataModelService;
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)
{
// Profile editor service will call Unsubscribe
_dataModelService.RemoveConditionOperator(this);
}
}
}

View File

@ -0,0 +1,14 @@
using System;
using Artemis.Core.Models.Profile.Conditions.Abstract;
namespace Artemis.Core.Models.Profile.Conditions
{
public class LayerConditionStatement : LayerConditionPart
{
public Guid DataModelGuid { get; set; }
public string PropertyPath { get; set; }
public LayerConditionOperator Operator { get; set; }
public object Value { get; set; }
}
}

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace Artemis.Core.Models.Profile.Conditions.Operators
{
public class GreaterThanConditionOperator : LayerConditionOperator
{
public override IReadOnlyCollection<Type> CompatibleTypes => new List<Type>
{
typeof(sbyte),
typeof(byte),
typeof(short),
typeof(ushort),
typeof(int),
typeof(uint),
typeof(long),
typeof(ulong),
typeof(float),
typeof(double),
typeof(decimal)
};
public override BinaryExpression CreateExpression(Type leftSideType, Type rightSideType)
{
var leftSideParameter = Expression.Parameter(leftSideType, "a");
var rightSideParameter = Expression.Parameter(rightSideType, "b");
return Expression.GreaterThan(leftSideParameter, rightSideParameter);
}
}
}

View File

@ -1,11 +1,15 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Artemis.Core.Events;
using Artemis.Core.Exceptions;
using Artemis.Core.Models.Profile.Conditions;
using Artemis.Core.Models.Profile.Conditions.Operators;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.Abstract.DataModels;
using Artemis.Core.Plugins.Exceptions;
using Artemis.Core.Plugins.Models;
using Artemis.Core.Services.Interfaces;
namespace Artemis.Core.Services
@ -17,15 +21,19 @@ namespace Artemis.Core.Services
{
private readonly List<DataModel> _dataModelExpansions;
private readonly IPluginService _pluginService;
private readonly List<LayerConditionOperator> _registeredConditionOperators;
internal DataModelService(IPluginService pluginService)
{
_pluginService = pluginService;
_dataModelExpansions = new List<DataModel>();
_registeredConditionOperators = new List<LayerConditionOperator>();
_pluginService.PluginEnabled += PluginServiceOnPluginEnabled;
_pluginService.PluginDisabled += PluginServiceOnPluginDisabled;
RegisterBuiltInConditionOperators();
foreach (var module in _pluginService.GetPluginsOfType<Module>().Where(m => m.InternalExpandsMainDataModel))
AddModuleDataModel(module);
foreach (var dataModelExpansion in _pluginService.GetPluginsOfType<BaseDataModelExpansion>())
@ -73,6 +81,52 @@ namespace Artemis.Core.Services
return null;
}
public void RegisterConditionOperator(PluginInfo pluginInfo, LayerConditionOperator layerConditionOperator)
{
if (pluginInfo == null)
throw new ArgumentNullException(nameof(pluginInfo));
if (layerConditionOperator == null)
throw new ArgumentNullException(nameof(layerConditionOperator));
lock (_registeredConditionOperators)
{
if (_registeredConditionOperators.Contains(layerConditionOperator))
return;
layerConditionOperator.Register(pluginInfo, this);
_registeredConditionOperators.Add(layerConditionOperator);
}
}
public void RemoveConditionOperator(LayerConditionOperator layerConditionOperator)
{
if (layerConditionOperator == null)
throw new ArgumentNullException(nameof(layerConditionOperator));
lock (_registeredConditionOperators)
{
if (!_registeredConditionOperators.Contains(layerConditionOperator))
return;
layerConditionOperator.Unsubscribe();
_registeredConditionOperators.Remove(layerConditionOperator);
}
}
public List<LayerConditionOperator> GetCompatibleConditionOperators(Type type)
{
lock (_registeredConditionOperators)
{
return _registeredConditionOperators.Where(c => c.CompatibleTypes.Contains(type)).ToList();
}
}
private void RegisterBuiltInConditionOperators()
{
RegisterConditionOperator(Constants.CorePluginInfo, new GreaterThanConditionOperator());
}
private void PluginServiceOnPluginEnabled(object sender, PluginEventArgs e)
{
if (e.PluginInfo.Instance is Module module && module.InternalExpandsMainDataModel)
@ -85,7 +139,7 @@ namespace Artemis.Core.Services
{
if (dataModelExpansion.InternalDataModel.DataModelDescription == null)
throw new ArtemisPluginException(dataModelExpansion.PluginInfo, "Data model expansion overrides GetDataModelDescription but returned null");
AddExpansion(dataModelExpansion.InternalDataModel);
}
@ -93,7 +147,7 @@ namespace Artemis.Core.Services
{
if (module.InternalDataModel.DataModelDescription == null)
throw new ArtemisPluginException(module.PluginInfo, "Module overrides GetDataModelDescription but returned null");
AddExpansion(module.InternalDataModel);
}

View File

@ -1,7 +1,9 @@
using System.Collections.ObjectModel;
using Artemis.Core.Models;
using Artemis.Core.Annotations;
using Artemis.Core.Models.Profile.Conditions;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.Abstract.DataModels;
using Artemis.Core.Plugins.Models;
namespace Artemis.Core.Services.Interfaces
{
@ -22,9 +24,22 @@ namespace Artemis.Core.Services.Interfaces
void RemoveExpansion(DataModel baseDataModelExpansion);
/// <summary>
/// If found, returns the data model of the provided plugin
/// If found, returns the data model of the provided plugin
/// </summary>
/// <param name="plugin">Should be a module with a data model or a data model expansion</param>
DataModel GetPluginDataModel(Plugin plugin);
/// <summary>
/// Registers a new condition operator for use in layer conditions
/// </summary>
/// <param name="pluginInfo">The PluginInfo of the plugin this condition operator belongs to</param>
/// <param name="layerConditionOperator">The condition operator to register</param>
void RegisterConditionOperator([NotNull] PluginInfo pluginInfo, [NotNull] LayerConditionOperator layerConditionOperator);
/// <summary>
/// Removes a condition operator so it is no longer available for use in layer conditions
/// </summary>
/// <param name="layerConditionOperator">The layer condition operator to remove</param>
void RemoveConditionOperator([NotNull] LayerConditionOperator layerConditionOperator);
}
}

View File

@ -57,7 +57,8 @@ namespace Artemis.UI.Shared.Services
if (existing != null)
{
if (existing.PluginInfo != pluginInfo)
throw new ArtemisPluginException($"Cannot register property editor for type {supportedType.Name} because an editor was already registered by {pluginInfo.Name}");
throw new ArtemisPluginException($"Cannot register data model input for type {supportedType.Name} " +
$"because an editor was already registered by {pluginInfo.Name}");
return existing;
}
@ -78,7 +79,8 @@ namespace Artemis.UI.Shared.Services
if (existing != null)
{
if (existing.PluginInfo != pluginInfo)
throw new ArtemisPluginException($"Cannot register property editor for type {supportedType.Name} because an editor was already registered by {pluginInfo.Name}");
throw new ArtemisPluginException($"Cannot register data model display for type {supportedType.Name} " +
$"because an editor was already registered by {pluginInfo.Name}");
return existing;
}

View File

@ -102,8 +102,8 @@ namespace Artemis.UI.Screens.Settings.Tabs.Plugins
if (PluginInfo.Icon != null)
{
var parsedIcon = Enum.TryParse<PackIconKind>(PluginInfo.Icon, true, out var iconEnum);
if (parsedIcon == false)
return PackIconKind.QuestionMarkCircle;
if (parsedIcon)
return iconEnum;
}
switch (Plugin)