Profiles - Added system that displays when a layer is causing errors

This commit is contained in:
Robert committed 2021-07-11 19:32:27 +02:00
1 parent 3b7e226885
commit 5b6fc9eec7
18 files changed
+457 -95

No files matched your search

+92
View File
@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
namespace Artemis.Core
{
/// <summary>
/// Provides a default implementation for models that can have a broken state
/// </summary>
public abstract class BreakableModel : CorePropertyChanged, IBreakableModel
{
private string? _brokenState;
private Exception? _brokenStateException;
/// <summary>
/// Invokes the <see cref="BrokenStateChanged" /> event
/// </summary>
protected virtual void OnBrokenStateChanged()
{
BrokenStateChanged?.Invoke(this, EventArgs.Empty);
}
/// <inheritdoc />
public abstract string BrokenDisplayName { get; }
/// <summary>
/// Gets or sets the broken state of this breakable model, if <see langword="null" /> this model is not broken.
/// <para>Note: If setting this manually you are also responsible for invoking <see cref="BrokenStateChanged" /></para>
/// </summary>
public string? BrokenState
{
get => _brokenState;
set => SetAndNotify(ref _brokenState, value);
}
/// <summary>
/// Gets or sets the exception that caused the broken state
/// <para>Note: If setting this manually you are also responsible for invoking <see cref="BrokenStateChanged" /></para>
/// </summary>
public Exception? BrokenStateException
{
get => _brokenStateException;
set => SetAndNotify(ref _brokenStateException, value);
}
/// <inheritdoc />
public bool TryOrBreak(Action action, string breakMessage)
{
try
{
action();
ClearBrokenState(breakMessage);
return true;
}
catch (Exception e)
{
SetBrokenState(breakMessage, e);
return false;
}
}
/// <inheritdoc />
public void SetBrokenState(string state, Exception? exception)
{
BrokenState = state ?? throw new ArgumentNullException(nameof(state));
BrokenStateException = exception;
OnBrokenStateChanged();
}
/// <inheritdoc />
public void ClearBrokenState(string state)
{
if (state == null) throw new ArgumentNullException(nameof(state));
if (BrokenState == null)
return;
if (BrokenState != state) return;
BrokenState = null;
BrokenStateException = null;
OnBrokenStateChanged();
}
/// <inheritdoc />
public virtual IEnumerable<IBreakableModel> GetBrokenHierarchy()
{
if (BrokenState != null)
yield return this;
}
/// <inheritdoc />
public event EventHandler? BrokenStateChanged;
}
}
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
namespace Artemis.Core
{
/// <summary>
/// Represents a model that can have a broken state
/// </summary>
public interface IBreakableModel
{
/// <summary>
/// Gets the display name of this breakable model
/// </summary>
string BrokenDisplayName { get; }
/// <summary>
/// Gets or sets the broken state of this breakable model, if <see langword="null" /> this model is not broken.
/// </summary>
string? BrokenState { get; set; }
/// <summary>
/// Gets or sets the exception that caused the broken state
/// </summary>
Exception? BrokenStateException { get; set; }
/// <summary>
/// Try to execute the provided action. If the action succeeded the broken state is cleared if it matches
/// <see paramref="breakMessage" />, if the action throws an exception <see cref="BrokenState" /> and
/// <see cref="BrokenStateException" /> are set accordingly.
/// </summary>
/// <param name="action">The action to attempt to execute</param>
/// <param name="breakMessage">The message to clear on succeed or set on failure (exception)</param>
/// <returns><see langword="true" /> if the action succeeded; otherwise <see langword="false" />.</returns>
bool TryOrBreak(Action action, string breakMessage);
/// <summary>
/// Sets the broken state to the provided state and optional exception.
/// </summary>
/// <param name="state">The state to set the broken state to</param>
/// <param name="exception">The exception that caused the broken state</param>
public void SetBrokenState(string state, Exception? exception);
/// <summary>
/// Clears the broken state and exception if <see cref="BrokenState" /> equals <see paramref="state"></see>.
/// </summary>
/// <param name="state"></param>
public void ClearBrokenState(string state);
/// <summary>
/// Returns a list containing all broken models, including self and any children
/// </summary>
IEnumerable<IBreakableModel> GetBrokenHierarchy();
/// <summary>
/// Occurs when the broken state of this model changes
/// </summary>
event EventHandler BrokenStateChanged;
}
}
+14 -11
View File
@@ -183,18 +183,15 @@ namespace Artemis.Core
lock (Timeline)
{
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => !e.Suspended))
{
baseLayerEffect.BaseProperties?.Update(Timeline);
baseLayerEffect.Update(Timeline.Delta.TotalSeconds);
}
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => !e.Suspended))
baseLayerEffect.InternalUpdate(Timeline);
SKPaint layerPaint = new() {FilterQuality = SKFilterQuality.Low};
try
{
SKRectI rendererBounds = SKRectI.Create(0, 0, Bounds.Width, Bounds.Height);
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => !e.Suspended))
baseLayerEffect.PreProcess(canvas, rendererBounds, layerPaint);
baseLayerEffect.InternalPreProcess(canvas, rendererBounds, layerPaint);
// No point rendering if the alpha was set to zero by one of the effects
if (layerPaint.Color.Alpha == 0)
@@ -208,7 +205,7 @@ namespace Artemis.Core
Children[index].Render(canvas, new SKPointI(Bounds.Left, Bounds.Top));
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => !e.Suspended))
baseLayerEffect.PostProcess(canvas, rendererBounds, layerPaint);
baseLayerEffect.InternalPostProcess(canvas, rendererBounds, layerPaint);
}
finally
{
@@ -228,8 +225,6 @@ namespace Artemis.Core
if (Enabled)
return;
Debug.WriteLine($"Enabling {this}");
foreach (BaseLayerEffect baseLayerEffect in LayerEffects)
baseLayerEffect.InternalEnable();
@@ -248,8 +243,6 @@ namespace Artemis.Core
if (!Enabled)
return;
Debug.WriteLine($"Disabled {this}");
foreach (BaseLayerEffect baseLayerEffect in LayerEffects)
baseLayerEffect.InternalDisable();
@@ -344,5 +337,15 @@ namespace Artemis.Core
{
RenderPropertiesUpdated?.Invoke(this, EventArgs.Empty);
}
#region Overrides of BreakableModel
/// <inheritdoc />
public override IEnumerable<IBreakableModel> GetBrokenHierarchy()
{
return LayerEffects.Where(e => e.BrokenState != null);
}
#endregion
}
}
+48 -25
View File
@@ -206,6 +206,7 @@ namespace Artemis.Core
GetType().GetProperty(nameof(Transform))!,
typeof(PropertyGroupDescriptionAttribute)
)!;
General.GroupDescription = (PropertyGroupDescriptionAttribute) generalAttribute;
General.Initialize(this, "General.", Constants.CorePluginFeature);
Transform.GroupDescription = (PropertyGroupDescriptionAttribute) transformAttribute;
@@ -288,7 +289,7 @@ namespace Artemis.Core
// Adaption hints
Adapter.Save();
SaveRenderElement();
}
@@ -320,7 +321,7 @@ namespace Artemis.Core
{
if (Disposed)
throw new ObjectDisposedException("Layer");
UpdateDisplayCondition();
UpdateTimeline(deltaTime);
@@ -366,9 +367,17 @@ namespace Artemis.Core
if (Enabled)
return;
LayerBrush?.InternalEnable();
foreach (BaseLayerEffect baseLayerEffect in LayerEffects)
baseLayerEffect.InternalEnable();
bool tryOrBreak = TryOrBreak(() => LayerBrush?.InternalEnable(), "Failed to enable layer brush");
if (!tryOrBreak)
return;
tryOrBreak = TryOrBreak(() =>
{
foreach (BaseLayerEffect baseLayerEffect in LayerEffects)
baseLayerEffect.InternalEnable();
}, "Failed to enable one or more effects");
if (!tryOrBreak)
return;
Enabled = true;
}
@@ -393,17 +402,10 @@ namespace Artemis.Core
General.Update(timeline);
Transform.Update(timeline);
if (LayerBrush != null)
{
LayerBrush.BaseProperties?.Update(timeline);
LayerBrush.Update(timeline.Delta.TotalSeconds);
}
LayerBrush?.InternalUpdate(timeline);
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => !e.Suspended))
{
baseLayerEffect.BaseProperties?.Update(timeline);
baseLayerEffect.Update(timeline.Delta.TotalSeconds);
}
baseLayerEffect.InternalUpdate(timeline);
}
private void RenderTimeline(Timeline timeline, SKCanvas canvas, SKPointI basePosition)
@@ -477,9 +479,9 @@ namespace Artemis.Core
{
if (LayerBrush == null)
throw new ArtemisCoreException("The layer is not yet ready for rendering");
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => !e.Suspended))
baseLayerEffect.PreProcess(canvas, bounds, layerPaint);
baseLayerEffect.InternalPreProcess(canvas, bounds, layerPaint);
try
{
@@ -492,7 +494,7 @@ namespace Artemis.Core
LayerBrush.InternalRender(canvas, bounds, layerPaint);
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => !e.Suspended))
baseLayerEffect.PostProcess(canvas, bounds, layerPaint);
baseLayerEffect.InternalPostProcess(canvas, bounds, layerPaint);
}
finally
@@ -722,16 +724,24 @@ namespace Artemis.Core
internal void ActivateLayerBrush()
{
LayerBrushReference? current = General.BrushReference.CurrentValue;
if (current == null)
return;
try
{
LayerBrushReference? current = General.BrushReference.CurrentValue;
if (current == null)
return;
LayerBrushDescriptor? descriptor = current.LayerBrushProviderId != null && current.BrushType != null
? LayerBrushStore.Get(current.LayerBrushProviderId, current.BrushType)?.LayerBrushDescriptor
: null;
descriptor?.CreateInstance(this);
LayerBrushDescriptor? descriptor = current.LayerBrushProviderId != null && current.BrushType != null
? LayerBrushStore.Get(current.LayerBrushProviderId, current.BrushType)?.LayerBrushDescriptor
: null;
descriptor?.CreateInstance(this);
OnLayerBrushUpdated();
OnLayerBrushUpdated();
ClearBrokenState("Failed to initialize layer brush");
}
catch (Exception e)
{
SetBrokenState("Failed to initialize layer brush", e);
}
}
internal void DeactivateLayerBrush()
@@ -747,6 +757,19 @@ namespace Artemis.Core
}
#endregion
#region Overrides of BreakableModel
/// <inheritdoc />
public override IEnumerable<IBreakableModel> GetBrokenHierarchy()
{
if (LayerBrush?.BrokenState != null)
yield return LayerBrush;
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => e.BrokenState != null))
yield return baseLayerEffect;
}
#endregion
}
/// <summary>
@@ -27,6 +27,7 @@ namespace Artemis.Core
UndoStack = new MaxStack<string>(20);
RedoStack = new MaxStack<string>(20);
Exceptions = new List<Exception>();
Load();
}
@@ -77,6 +78,7 @@ namespace Artemis.Core
internal MaxStack<string> UndoStack { get; set; }
internal MaxStack<string> RedoStack { get; set; }
internal List<Exception> Exceptions { get; }
/// <inheritdoc />
public override void Update(double deltaTime)
@@ -113,6 +115,13 @@ namespace Artemis.Core
foreach (ProfileScript profileScript in Scripts)
profileScript.OnProfileRendered(canvas, canvas.LocalClipBounds);
if (!Exceptions.Any())
return;
List<Exception> exceptions = new(Exceptions);
Exceptions.Clear();
throw new AggregateException($"One or more exceptions while rendering profile {Name}", exceptions);
}
}
@@ -238,5 +247,18 @@ namespace Artemis.Core
ProfileEntity.ScriptConfigurations.Add(scriptConfiguration.Entity);
}
}
#region Overrides of BreakableModel
/// <inheritdoc />
public override IEnumerable<IBreakableModel> GetBrokenHierarchy()
{
foreach (IBreakableModel breakableModel in GetAllFolders().SelectMany(folders => folders.GetBrokenHierarchy()))
yield return breakableModel;
foreach (IBreakableModel breakableModel in GetAllLayers().SelectMany(layer => layer.GetBrokenHierarchy()))
yield return breakableModel;
}
#endregion
}
}
@@ -9,7 +9,7 @@ namespace Artemis.Core
/// <summary>
/// Represents an element of a <see cref="Profile" />
/// </summary>
public abstract class ProfileElement : CorePropertyChanged, IDisposable
public abstract class ProfileElement : BreakableModel, IDisposable
{
private Guid _entityId;
private string? _name;
@@ -25,7 +25,7 @@ namespace Artemis.Core
_profile = profile;
ChildrenList = new List<ProfileElement>();
}
/// <summary>
/// Gets the unique ID of this profile element
/// </summary>
@@ -121,6 +121,13 @@ namespace Artemis.Core
return $"{nameof(EntityId)}: {EntityId}, {nameof(Order)}: {Order}, {nameof(Name)}: {Name}";
}
#region Overrides of BreakableModel
/// <inheritdoc />
public override string BrokenDisplayName => Name ?? GetType().Name;
#endregion
#region Hierarchy
/// <summary>
@@ -234,7 +241,7 @@ namespace Artemis.Core
internal abstract void Save();
#endregion
#region IDisposable
/// <inheritdoc />