Migrations - Added migration to segments

Layout - Fixed rotation not always applying (workaround)
Conditions - Added string operators
This commit is contained in:
SpoinkyNL committed 2020-07-30 00:49:23 +02:00
1 parent dab11cb3e7
commit 385a30bec7
17 files changed
+317 -22

No files matched your search

@@ -34,6 +34,36 @@ namespace Artemis.Core.Models.Profile.Conditions
/// </summary>
public abstract string Icon { get; }
/// <summary>
/// Gets or sets whether this condition operator supports a right side, defaults to true
/// </summary>
public bool SupportsRightSide { get; protected set; } = true;
public bool SupportsType(Type type)
{
if (type == null)
return true;
return CompatibleTypes.Any(t => t.IsCastableFrom(type));
}
/// <summary>
/// Creates a binary expression comparing two types
/// </summary>
/// <param name="leftSide">The parameter on the left side of the expression</param>
/// <param name="rightSide">The parameter on the right side of the expression</param>
/// <returns></returns>
public abstract BinaryExpression CreateExpression(Expression leftSide, Expression rightSide);
/// <summary>
/// Returns an expression that checks the given expression for null
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
protected Expression CreateNullCheckExpression(Expression expression)
{
return Expression.NotEqual(expression, Expression.Constant(null));
}
internal void Register(PluginInfo pluginInfo, IDataModelService dataModelService)
{
if (_registered)
@@ -63,20 +93,5 @@ namespace Artemis.Core.Models.Profile.Conditions
// Profile editor service will call Unsubscribe
_dataModelService.RemoveConditionOperator(this);
}
public bool SupportsType(Type type)
{
if (type == null)
return true;
return CompatibleTypes.Any(t => t.IsCastableFrom(type));
}
/// <summary>
/// Creates a binary expression comparing two types
/// </summary>
/// <param name="leftSide">The parameter on the left side of the expression</param>
/// <param name="rightSide">The parameter on the right side of the expression</param>
/// <returns></returns>
public abstract BinaryExpression CreateExpression(Expression leftSide, Expression rightSide);
}
}
@@ -304,7 +304,11 @@ namespace Artemis.Core.Models.Profile.Conditions
if (leftSideAccessor.Type.IsValueType && RightStaticValue == null)
return;
var rightSideConstant = Expression.Constant(RightStaticValue);
// If the right side value is null, the constant type cannot be inferred and must be provided manually
var rightSideConstant = RightStaticValue != null
? Expression.Constant(RightStaticValue)
: Expression.Constant(null, leftSideAccessor.Type);
var conditionExpression = Operator.CreateExpression(leftSideAccessor, rightSideConstant);
StaticConditionLambda = Expression.Lambda<Func<DataModel, bool>>(conditionExpression, leftSideParameter);
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace Artemis.Core.Models.Profile.Conditions.Operators
{
public class StringContainsConditionOperator : DisplayConditionOperator
{
private readonly MethodInfo _toLower;
private readonly MethodInfo _contains;
public StringContainsConditionOperator()
{
_toLower = typeof(string).GetMethod("ToLower", new Type[] { });
_contains = typeof(string).GetMethod("Contains", new[] {typeof(string) });
}
public override IReadOnlyCollection<Type> CompatibleTypes => new List<Type> {typeof(string)};
public override string Description => "Contains";
public override string Icon => "Contain";
public override BinaryExpression CreateExpression(Expression leftSide, Expression rightSide)
{
return Expression.Equal(Expression.Call(Expression.Call(leftSide, _toLower), _contains, Expression.Call(rightSide, _toLower)), Expression.Constant(true));
}
}
}
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace Artemis.Core.Models.Profile.Conditions.Operators
{
public class StringEndsWithConditionOperator : DisplayConditionOperator
{
private readonly MethodInfo _toLower;
private readonly MethodInfo _endsWith;
public StringEndsWithConditionOperator()
{
_toLower = typeof(string).GetMethod("ToLower", new Type[] { });
_endsWith = typeof(string).GetMethod("EndsWith", new[] { typeof(string) });
}
public override IReadOnlyCollection<Type> CompatibleTypes => new List<Type> { typeof(string) };
public override string Description => "Ends with";
public override string Icon => "ContainEnd";
public override BinaryExpression CreateExpression(Expression leftSide, Expression rightSide)
{
return Expression.Equal(Expression.Call(Expression.Call(leftSide, _toLower), _endsWith, Expression.Call(rightSide, _toLower)), Expression.Constant(true));
}
}
}
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace Artemis.Core.Models.Profile.Conditions.Operators
{
public class StringEqualsConditionOperator : DisplayConditionOperator
{
private readonly MethodInfo _toLower;
public StringEqualsConditionOperator()
{
_toLower = typeof(string).GetMethod("ToLower", new Type[] { });
}
public override IReadOnlyCollection<Type> CompatibleTypes => new List<Type> {typeof(string)};
public override string Description => "Equals";
public override string Icon => "Equal";
public override BinaryExpression CreateExpression(Expression leftSide, Expression rightSide)
{
return Expression.Equal(Expression.Call(leftSide, _toLower), Expression.Call(rightSide, _toLower));
}
}
}
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace Artemis.Core.Models.Profile.Conditions.Operators
{
public class StringNotContainsConditionOperator : DisplayConditionOperator
{
private readonly MethodInfo _toLower;
private readonly MethodInfo _contains;
public StringNotContainsConditionOperator()
{
_toLower = typeof(string).GetMethod("ToLower", new Type[] { });
_contains = typeof(string).GetMethod("Contains", new[] { typeof(string) });
}
public override IReadOnlyCollection<Type> CompatibleTypes => new List<Type> { typeof(string) };
public override string Description => "Does not contain";
public override string Icon => "FormatStrikethrough";
public override BinaryExpression CreateExpression(Expression leftSide, Expression rightSide)
{
return Expression.Equal(Expression.Call(Expression.Call(leftSide, _toLower), _contains, Expression.Call(rightSide, _toLower)), Expression.Constant(false));
}
}
}
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace Artemis.Core.Models.Profile.Conditions.Operators
{
public class StringNotEqualConditionOperator : DisplayConditionOperator
{
private readonly MethodInfo _toLower;
public StringNotEqualConditionOperator()
{
_toLower = typeof(string).GetMethod("ToLower", new Type[] { });
}
public override IReadOnlyCollection<Type> CompatibleTypes => new List<Type> {typeof(string)};
public override string Description => "Does not equal";
public override string Icon => "NotEqualVariant";
public override BinaryExpression CreateExpression(Expression leftSide, Expression rightSide)
{
return Expression.NotEqual(Expression.Call(leftSide, _toLower), Expression.Call(rightSide, _toLower));
}
}
}
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace Artemis.Core.Models.Profile.Conditions.Operators
{
public class StringNullConditionOperator : DisplayConditionOperator
{
public StringNullConditionOperator()
{
SupportsRightSide = false;
}
public override IReadOnlyCollection<Type> CompatibleTypes => new List<Type> {typeof(string)};
public override string Description => "Is null";
public override string Icon => "Null";
public override BinaryExpression CreateExpression(Expression leftSide, Expression rightSide)
{
return Expression.Equal(leftSide, Expression.Constant(null, leftSide.Type));
}
}
}
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace Artemis.Core.Models.Profile.Conditions.Operators
{
public class StringStartsWithConditionOperator : DisplayConditionOperator
{
private readonly MethodInfo _toLower;
private readonly MethodInfo _startsWith;
public StringStartsWithConditionOperator()
{
_toLower = typeof(string).GetMethod("ToLower", new Type[] { });
_startsWith = typeof(string).GetMethod("StartsWith", new[] { typeof(string) });
}
public override IReadOnlyCollection<Type> CompatibleTypes => new List<Type> { typeof(string) };
public override string Description => "Starts with";
public override string Icon => "ContainStart";
public override BinaryExpression CreateExpression(Expression leftSide, Expression rightSide)
{
return Expression.Equal(Expression.Call(Expression.Call(leftSide, _toLower), _startsWith, Expression.Call(rightSide, _toLower)), Expression.Constant(true));
}
}
}
@@ -134,10 +134,14 @@ namespace Artemis.Core.Models.Surface
internal void ApplyToRgbDevice()
{
RgbDevice.Location = new Point(DeviceEntity.X, DeviceEntity.Y);
RgbDevice.Rotation = DeviceEntity.Rotation;
RgbDevice.Scale = DeviceEntity.Scale;
// Workaround for device rotation not applying
if (DeviceEntity.X == 0 && DeviceEntity.Y == 0)
RgbDevice.Location = new Point(1, 1);
RgbDevice.Location = new Point(DeviceEntity.X, DeviceEntity.Y);
CalculateRenderProperties();
OnDeviceUpdated();
}