Scripting - Core implementation (#629)

Scripting - Added plugin classes
Layers - Fix certain blend modes not working as intended
UI - Add customizable header per page
Profile editor - Hide the regular header
Profile editor - Added a new toolbar
This commit is contained in:
Robert Beekman authored and GitHub committed 2021-06-19 09:48:16 +02:00
1 parent d749ab5bbd
commit 048864fe78
47 files changed
+1599 -237

No files matched your search

@@ -61,6 +61,7 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=plugins_005Cprerequisites/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=plugins_005Cprerequisites_005Cprerequisiteaction/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=plugins_005Cprofiling/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=plugins_005Cscriptingproviders_005Cscripts/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=plugins_005Csettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=rgb_002Enet/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Ccolorquantizer/@EntryIndexedValue">True</s:Boolean>
+50 -4
View File
@@ -4,6 +4,7 @@ using System.Collections.ObjectModel;
using System.Linq;
using Artemis.Core.LayerBrushes;
using Artemis.Core.LayerEffects;
using Artemis.Core.ScriptingProviders;
using Artemis.Storage.Entities.Profile;
using Artemis.Storage.Entities.Profile.Abstract;
using RGB.NET.Core;
@@ -37,6 +38,9 @@ namespace Artemis.Core
Profile = Parent.Profile;
Name = name;
Suspended = false;
Scripts = new List<LayerScript>();
ScriptConfigurations = new List<ScriptConfiguration>();
_general = new LayerGeneralProperties();
_transform = new LayerTransformProperties();
@@ -60,6 +64,9 @@ namespace Artemis.Core
Profile = profile;
Parent = parent;
Scripts = new List<LayerScript>();
ScriptConfigurations = new List<ScriptConfiguration>();
_general = new LayerGeneralProperties();
_transform = new LayerTransformProperties();
@@ -75,6 +82,16 @@ namespace Artemis.Core
/// </summary>
public ReadOnlyCollection<ArtemisLed> Leds => _leds.AsReadOnly();
/// <summary>
/// Gets a collection of all active scripts assigned to this layer
/// </summary>
public List<LayerScript> Scripts { get; }
/// <summary>
/// Gets a collection of all script configurations assigned to this layer
/// </summary>
public List<ScriptConfiguration> ScriptConfigurations { get; }
/// <summary>
/// Defines the shape that is rendered by the <see cref="LayerBrush" />.
/// </summary>
@@ -142,8 +159,10 @@ namespace Artemis.Core
if (LayerBrush?.BaseProperties != null)
result.AddRange(LayerBrush.BaseProperties.GetAllLayerProperties());
foreach (BaseLayerEffect layerEffect in LayerEffects)
{
if (layerEffect.BaseProperties != null)
result.AddRange(layerEffect.BaseProperties.GetAllLayerProperties());
}
return result;
}
@@ -171,6 +190,9 @@ namespace Artemis.Core
Disposed = true;
while (Scripts.Count > 1)
Scripts[0].Dispose();
LayerBrushStore.LayerBrushAdded -= LayerBrushStoreOnLayerBrushAdded;
LayerBrushStore.LayerBrushRemoved -= LayerBrushStoreOnLayerBrushRemoved;
@@ -247,6 +269,11 @@ namespace Artemis.Core
ExpandedPropertyGroups.AddRange(LayerEntity.ExpandedPropertyGroups);
LoadRenderElement();
Adapter.Load();
foreach (ScriptConfiguration scriptConfiguration in ScriptConfigurations)
scriptConfiguration.Script?.Dispose();
ScriptConfigurations.Clear();
ScriptConfigurations.AddRange(LayerEntity.ScriptConfigurations.Select(e => new ScriptConfiguration(e)));
}
internal override void Save()
@@ -284,6 +311,13 @@ namespace Artemis.Core
// Adaption hints
Adapter.Save();
LayerEntity.ScriptConfigurations.Clear();
foreach (ScriptConfiguration scriptConfiguration in ScriptConfigurations)
{
scriptConfiguration.Save();
LayerEntity.ScriptConfigurations.Add(scriptConfiguration.Entity);
}
SaveRenderElement();
}
@@ -316,6 +350,9 @@ namespace Artemis.Core
if (Disposed)
throw new ObjectDisposedException("Layer");
foreach (LayerScript layerScript in Scripts)
layerScript.OnLayerUpdating(deltaTime);
UpdateDisplayCondition();
UpdateTimeline(deltaTime);
@@ -323,6 +360,9 @@ namespace Artemis.Core
Enable();
else if (Timeline.IsFinished)
Disable();
foreach (LayerScript layerScript in Scripts)
layerScript.OnLayerUpdated(deltaTime);
}
/// <inheritdoc />
@@ -473,19 +513,27 @@ namespace Artemis.Core
if (LayerBrush == null)
throw new ArtemisCoreException("The layer is not yet ready for rendering");
foreach (LayerScript layerScript in Scripts)
layerScript.OnLayerRendering(canvas, bounds, layerPaint);
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => !e.Suspended))
baseLayerEffect.PreProcess(canvas, bounds, layerPaint);
try
{
canvas.SaveLayer(layerPaint);
// If a brush is a bad boy and tries to color outside the lines, ensure that its clipped off
canvas.ClipPath(renderPath);
// Restore the blend mode before doing the actual render
layerPaint.BlendMode = SKBlendMode.SrcOver;
LayerBrush.InternalRender(canvas, bounds, layerPaint);
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => !e.Suspended))
baseLayerEffect.PostProcess(canvas, bounds, layerPaint);
foreach (LayerScript layerScript in Scripts)
layerScript.OnLayerRendered(canvas, bounds, layerPaint);
}
finally
@@ -500,9 +548,7 @@ namespace Artemis.Core
throw new ObjectDisposedException("Layer");
if (!Leds.Any())
{
Path = new SKPath();
}
else
{
SKPath path = new() {FillType = SKPathFillType.Winding};
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Artemis.Core.ScriptingProviders;
using Artemis.Storage.Entities.Profile;
namespace Artemis.Core
@@ -23,6 +24,16 @@ namespace Artemis.Core
/// </summary>
LayerPropertyGroup LayerPropertyGroup { get; }
/// <summary>
/// Gets a collection of all active scripts assigned to this layer property
/// </summary>
List<PropertyScript> Scripts { get; }
/// <summary>
/// Gets a collection of all script configurations assigned to this layer property
/// </summary>
public List<ScriptConfiguration> ScriptConfigurations { get; }
/// <summary>
/// Gets the unique path of the property on the layer
/// </summary>
@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Artemis.Core.ScriptingProviders;
using Artemis.Storage.Entities.Profile;
using Newtonsoft.Json;
@@ -30,6 +31,8 @@ namespace Artemis.Core
Path = null!;
Entity = null!;
PropertyDescription = null!;
Scripts = new List<PropertyScript>();
ScriptConfigurations = new List<ScriptConfiguration>();
CurrentValue = default!;
DefaultValue = default!;
@@ -55,15 +58,15 @@ namespace Artemis.Core
/// </param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_disposed = true;
_disposed = true;
foreach (IDataBinding dataBinding in _dataBindings)
dataBinding.Dispose();
while (Scripts.Count > 1)
Scripts[0].Dispose();
Disposed?.Invoke(this, EventArgs.Empty);
}
foreach (IDataBinding dataBinding in _dataBindings)
dataBinding.Dispose();
Disposed?.Invoke(this, EventArgs.Empty);
}
/// <summary>
@@ -150,6 +153,12 @@ namespace Artemis.Core
/// <inheritdoc />
public PropertyDescriptionAttribute PropertyDescription { get; internal set; }
/// <inheritdoc />
public List<PropertyScript> Scripts { get; }
/// <inheritdoc />
public List<ScriptConfiguration> ScriptConfigurations { get; }
/// <inheritdoc />
public string Path { get; private set; }
@@ -162,12 +171,18 @@ namespace Artemis.Core
if (_disposed)
throw new ObjectDisposedException("LayerProperty");
foreach (PropertyScript propertyScript in Scripts)
propertyScript.OnPropertyUpdating(timeline.Delta.TotalSeconds);
CurrentValue = BaseValue;
UpdateKeyframes(timeline);
UpdateDataBindings(timeline);
OnUpdated();
foreach (PropertyScript propertyScript in Scripts)
propertyScript.OnPropertyUpdated(timeline.Delta.TotalSeconds);
}
/// <inheritdoc />
@@ -747,6 +762,11 @@ namespace Artemis.Core
if (dataBinding != null)
_dataBindings.Add(dataBinding);
}
foreach (ScriptConfiguration scriptConfiguration in ScriptConfigurations)
scriptConfiguration.Script?.Dispose();
ScriptConfigurations.Clear();
ScriptConfigurations.AddRange(Entity.ScriptConfigurations.Select(e => new ScriptConfiguration(e)));
}
/// <summary>
@@ -768,6 +788,13 @@ namespace Artemis.Core
Entity.DataBindingEntities.Clear();
foreach (IDataBinding dataBinding in _dataBindings)
dataBinding.Save();
Entity.ScriptConfigurations.Clear();
foreach (ScriptConfiguration scriptConfiguration in ScriptConfigurations)
{
scriptConfiguration.Save();
Entity.ScriptConfigurations.Add(scriptConfiguration.Entity);
}
}
/// <summary>
+42 -1
View File
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Artemis.Core.ScriptingProviders;
using Artemis.Storage.Entities.Profile;
using SkiaSharp;
@@ -13,13 +14,15 @@ namespace Artemis.Core
{
private readonly object _lock = new();
private bool _isFreshImport;
internal Profile(ProfileConfiguration configuration, ProfileEntity profileEntity) : base(null!)
{
Configuration = configuration;
Profile = this;
ProfileEntity = profileEntity;
EntityId = profileEntity.Id;
Scripts = new List<ProfileScript>();
ScriptConfigurations = new List<ScriptConfiguration>();
UndoStack = new Stack<string>();
RedoStack = new Stack<string>();
@@ -32,6 +35,17 @@ namespace Artemis.Core
/// </summary>
public ProfileConfiguration Configuration { get; }
/// <summary>
/// Gets a collection of all active scripts assigned to this profile
/// </summary>
public List<ProfileScript> Scripts { get; }
/// <summary>
/// Gets a collection of all script configurations assigned to this profile
/// </summary>
public List<ScriptConfiguration> ScriptConfigurations { get; }
/// <summary>
/// Gets or sets a boolean indicating whether this profile is freshly imported i.e. no changes have been made to it
/// since import
@@ -62,8 +76,14 @@ namespace Artemis.Core
if (Disposed)
throw new ObjectDisposedException("Profile");
foreach (ProfileScript profileScript in Scripts)
profileScript.OnProfileUpdating(deltaTime);
foreach (ProfileElement profileElement in Children)
profileElement.Update(deltaTime);
foreach (ProfileScript profileScript in Scripts)
profileScript.OnProfileUpdated(deltaTime);
}
}
@@ -75,8 +95,14 @@ namespace Artemis.Core
if (Disposed)
throw new ObjectDisposedException("Profile");
foreach (ProfileScript profileScript in Scripts)
profileScript.OnProfileRendering(canvas, canvas.LocalClipBounds);
foreach (ProfileElement profileElement in Children)
profileElement.Render(canvas, basePosition);
foreach (ProfileScript profileScript in Scripts)
profileScript.OnProfileRendered(canvas, canvas.LocalClipBounds);
}
}
@@ -125,6 +151,9 @@ namespace Artemis.Core
if (!disposing)
return;
while (Scripts.Count > 1)
Scripts[0].Dispose();
foreach (ProfileElement profileElement in Children)
profileElement.Dispose();
ChildrenList.Clear();
@@ -157,6 +186,11 @@ namespace Artemis.Core
AddChild(new Folder(this, this, rootFolder));
}
}
foreach (ScriptConfiguration scriptConfiguration in ScriptConfigurations)
scriptConfiguration.Script?.Dispose();
ScriptConfigurations.Clear();
ScriptConfigurations.AddRange(ProfileEntity.ScriptConfigurations.Select(e => new ScriptConfiguration(e)));
}
internal override void Save()
@@ -176,6 +210,13 @@ namespace Artemis.Core
ProfileEntity.Layers.Clear();
ProfileEntity.Layers.AddRange(GetAllLayers().Select(f => f.LayerEntity));
ProfileEntity.ScriptConfigurations.Clear();
foreach (ScriptConfiguration scriptConfiguration in ScriptConfigurations)
{
scriptConfiguration.Save();
ProfileEntity.ScriptConfigurations.Add(scriptConfiguration.Entity);
}
}
}
}
@@ -0,0 +1,86 @@
using Artemis.Storage.Entities.General;
namespace Artemis.Core.ScriptingProviders
{
/// <summary>
/// Represents the configuration of a script
/// </summary>
public class ScriptConfiguration : CorePropertyChanged, IStorageModel
{
private string _scriptingProviderId;
private string _name;
private string? _scriptContent;
/// <summary>
/// Creates a new instance of the <see cref="ScriptConfiguration" /> class
/// </summary>
public ScriptConfiguration(ScriptingProvider provider, string name)
{
ScriptingProviderId = provider.Id;
Name = name;
Entity = new ScriptConfigurationEntity();
}
internal ScriptConfiguration(ScriptConfigurationEntity entity)
{
ScriptingProviderId = null!;
Name = null!;
Entity = entity;
Load();
}
/// <summary>
/// Gets or sets the ID of the scripting provider
/// </summary>
public string ScriptingProviderId
{
get => _scriptingProviderId;
set => SetAndNotify(ref _scriptingProviderId, value);
}
/// <summary>
/// Gets or sets the name of the script
/// </summary>
public string Name
{
get => _name;
set => SetAndNotify(ref _name, value);
}
/// <summary>
/// Gets or sets the script's content
/// </summary>
public string? ScriptContent
{
get => _scriptContent;
set => SetAndNotify(ref _scriptContent, value);
}
/// <summary>
/// If active, gets the script
/// </summary>
public Script? Script { get; internal set; }
internal ScriptConfigurationEntity Entity { get; }
#region Implementation of IStorageModel
/// <inheritdoc />
public void Load()
{
ScriptingProviderId = Entity.ScriptingProviderId;
ScriptContent = Entity.ScriptContent;
Name = Entity.Name;
}
/// <inheritdoc />
public void Save()
{
Entity.ScriptingProviderId = ScriptingProviderId;
Entity.ScriptContent = ScriptContent;
}
#endregion
}
}
@@ -0,0 +1,177 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Artemis.Core.ScriptingProviders
{
/// <summary>
/// Allows you to implement and register your own scripting provider.
/// </summary>
public abstract class ScriptingProvider<TGlobalScript, TProfileScript, TLayerScript, TPropertyScript> : ScriptingProvider
where TGlobalScript : GlobalScript
where TProfileScript : ProfileScript
where TLayerScript : LayerScript
where TPropertyScript : PropertyScript
{
/// <summary>
/// Called when the UI needs a script editor for a <see cref="GlobalScript" />
/// </summary>
/// <param name="script">The script the editor must edit</param>
public abstract IScriptEditorViewModel CreateGlobalScriptEditor(TGlobalScript script);
/// <summary>
/// Called when the UI needs a script editor for a <see cref="ProfileScript" />
/// </summary>
/// <param name="script">The script the editor must edit</param>
public abstract IScriptEditorViewModel CreateProfileScriptEditor(TProfileScript script);
/// <summary>
/// Called when the UI needs a script editor for a <see cref="LayerScript" />
/// </summary>
/// <param name="script">The script the editor must edit</param>
public abstract IScriptEditorViewModel CreateLayerScriptScriptEditor(TLayerScript script);
/// <summary>
/// Called when the UI needs a script editor for a <see cref="PropertyScript" />
/// </summary>
/// <param name="script">The script the editor must edit</param>
public abstract IScriptEditorViewModel CreatePropertyScriptEditor(TPropertyScript script);
#region Overrides of ScriptingProvider
/// <inheritdoc />
internal override Type GlobalScriptType => typeof(TGlobalScript);
/// <inheritdoc />
internal override Type ProfileScriptType => typeof(TProfileScript);
/// <inheritdoc />
internal override Type LayerScriptType => typeof(TLayerScript);
/// <inheritdoc />
internal override Type PropertyScriptType => typeof(TPropertyScript);
/// <summary>
/// Called when the UI needs a script editor for a <see cref="GlobalScript" />
/// </summary>
/// <param name="script">The script the editor must edit</param>
public override IScriptEditorViewModel CreateGlobalScriptEditor(GlobalScript script)
{
if (script == null) throw new ArgumentNullException(nameof(script));
if (script.GetType() != GlobalScriptType)
throw new ArtemisCoreException($"This scripting provider only supports global scripts of type {GlobalScriptType.Name}");
return CreateGlobalScriptEditor((TGlobalScript) script);
}
/// <summary>
/// Called when the UI needs a script editor for a <see cref="ProfileScript" />
/// </summary>
/// <param name="script">The script the editor must edit</param>
public override IScriptEditorViewModel CreateProfileScriptEditor(ProfileScript script)
{
if (script == null) throw new ArgumentNullException(nameof(script));
if (script.GetType() != ProfileScriptType)
throw new ArtemisCoreException($"This scripting provider only supports profile scripts of type {ProfileScriptType.Name}");
return CreateProfileScriptEditor((TProfileScript) script);
}
/// <summary>
/// Called when the UI needs a script editor for a <see cref="LayerScript" />
/// </summary>
/// <param name="script">The script the editor must edit</param>
public override IScriptEditorViewModel CreateLayerScriptScriptEditor(LayerScript script)
{
if (script == null) throw new ArgumentNullException(nameof(script));
if (script.GetType() != LayerScriptType)
throw new ArtemisCoreException($"This scripting provider only supports layer scripts of type {LayerScriptType.Name}");
return CreateLayerScriptScriptEditor((TLayerScript) script);
}
/// <summary>
/// Called when the UI needs a script editor for a <see cref="PropertyScript" />
/// </summary>
/// <param name="script">The script the editor must edit</param>
public override IScriptEditorViewModel CreatePropertyScriptEditor(PropertyScript script)
{
if (script == null) throw new ArgumentNullException(nameof(script));
if (script.GetType() != PropertyScriptType)
throw new ArtemisCoreException($"This scripting provider only supports property scripts of type {PropertyScriptType.Name}");
return CreatePropertyScriptEditor((TPropertyScript) script);
}
#endregion
#region Overrides of PluginFeature
/// <inheritdoc />
internal override void InternalDisable()
{
base.InternalDisable();
while (Scripts.Count > 0)
Scripts[0].Dispose();
}
#endregion
}
/// <summary>
/// Allows you to implement and register your own scripting provider.
/// <para>
/// Note: You can't implement this, implement
/// <see cref="ScriptingProvider{TProfileScript,TLayerScript,TPropertyScript,TGlobalScript}" /> instead.
/// </para>
/// </summary>
public abstract class ScriptingProvider : PluginFeature
{
internal abstract Type GlobalScriptType { get; }
internal abstract Type PropertyScriptType { get; }
internal abstract Type LayerScriptType { get; }
internal abstract Type ProfileScriptType { get; }
internal List<Script> InternalScripts { get; } = new();
/// <summary>
/// Gets a list of all active scripts belonging to this scripting provider
/// </summary>
public ReadOnlyCollection<Script> Scripts => InternalScripts.AsReadOnly();
/// <summary>
/// Called when the UI needs a script editor for a <see cref="GlobalScript" />
/// </summary>
/// <param name="script">The script the editor must edit</param>
public abstract IScriptEditorViewModel CreateGlobalScriptEditor(GlobalScript script);
/// <summary>
/// Called when the UI needs a script editor for a <see cref="ProfileScript" />
/// </summary>
/// <param name="script">The script the editor must edit</param>
public abstract IScriptEditorViewModel CreateProfileScriptEditor(ProfileScript script);
/// <summary>
/// Called when the UI needs a script editor for a <see cref="LayerScript" />
/// </summary>
/// <param name="script">The script the editor must edit</param>
public abstract IScriptEditorViewModel CreateLayerScriptScriptEditor(LayerScript script);
/// <summary>
/// Called when the UI needs a script editor for a <see cref="PropertyScript" />
/// </summary>
/// <param name="script">The script the editor must edit</param>
public abstract IScriptEditorViewModel CreatePropertyScriptEditor(PropertyScript script);
}
/// <summary>
/// Represents a view model containing a script editor
/// </summary>
public interface IScriptEditorViewModel
{
/// <summary>
/// Gets the script this editor is editing
/// </summary>
Script Script { get; }
}
}
@@ -0,0 +1,43 @@
using Artemis.Core.Services;
namespace Artemis.Core.ScriptingProviders
{
/// <summary>
/// Represents a script running globally
/// </summary>
public abstract class GlobalScript : Script
{
/// <inheritdoc />
protected GlobalScript(ScriptConfiguration configuration) : base(configuration)
{
}
internal ScriptingService ScriptingService { get; set; }
/// <summary>
/// Called whenever the Artemis Core is about to update
/// </summary>
/// <param name="deltaTime">Seconds passed since last update</param>
public virtual void OnCoreUpdating(double deltaTime)
{
}
/// <summary>
/// Called whenever the Artemis Core has been updated
/// </summary>
/// <param name="deltaTime">Seconds passed since last update</param>
public virtual void OnCoreUpdated(double deltaTime)
{
}
#region Overrides of Script
/// <inheritdoc />
internal override void InternalCleanup()
{
ScriptingService.InternalGlobalScripts.Remove(this);
}
#endregion
}
}
@@ -0,0 +1,70 @@
using SkiaSharp;
namespace Artemis.Core.ScriptingProviders
{
/// <summary>
/// Represents a script bound to a specific <see cref="Layer" /> processed by a <see cref="ScriptingProvider" />.
/// </summary>
public abstract class LayerScript : Script
{
/// <inheritdoc />
protected LayerScript(Layer layer, ScriptConfiguration configuration) : base(configuration)
{
Layer = layer;
}
/// <summary>
/// Gets the layer this script is bound to
/// </summary>
public Layer Layer { get; internal set; }
/// <summary>
/// Called whenever the layer is about to update
/// </summary>
/// <param name="deltaTime">Seconds passed since last update</param>
public virtual void OnLayerUpdating(double deltaTime)
{
}
/// <summary>
/// Called whenever the layer has been updated
/// </summary>
/// <param name="deltaTime">Seconds passed since last update</param>
public virtual void OnLayerUpdated(double deltaTime)
{
}
/// <summary>
/// Called whenever the layer is about to render
/// </summary>
/// <param name="canvas">The layer canvas</param>
/// <param name="bounds">The area to be filled, covers the shape</param>
/// <param name="paint">The paint to be used to fill the shape</param>
public virtual void OnLayerRendering(SKCanvas canvas, SKRect bounds, SKPaint paint)
{
}
/// <summary>
/// Called whenever the layer has been rendered
/// </summary>
/// <param name="canvas">The layer canvas</param>
/// <param name="bounds">The area to be filled, covers the shape</param>
/// <param name="paint">The paint to be used to fill the shape</param>
public virtual void OnLayerRendered(SKCanvas canvas, SKRect bounds, SKPaint paint)
{
}
#region Overrides of Script
/// <inheritdoc />
internal override void InternalCleanup()
{
lock (Layer.Scripts)
{
Layer.Scripts.Remove(this);
}
}
#endregion
}
}
@@ -0,0 +1,68 @@
using SkiaSharp;
namespace Artemis.Core.ScriptingProviders
{
/// <summary>
/// Represents a script bound to a specific <see cref="Profile" /> processed by a <see cref="ScriptingProvider" />.
/// </summary>
public abstract class ProfileScript : Script
{
/// <inheritdoc />
protected ProfileScript(Profile profile, ScriptConfiguration configuration) : base(configuration)
{
Profile = profile;
}
/// <summary>
/// Gets the profile this script is bound to
/// </summary>
public Profile Profile { get; }
/// <summary>
/// Called whenever the profile is about to update
/// </summary>
/// <param name="deltaTime">Seconds passed since last update</param>
public virtual void OnProfileUpdating(double deltaTime)
{
}
/// <summary>
/// Called whenever the profile has been updated
/// </summary>
/// <param name="deltaTime">Seconds passed since last update</param>
public virtual void OnProfileUpdated(double deltaTime)
{
}
/// <summary>
/// Called whenever the profile is about to render
/// </summary>
/// <param name="canvas">The profile canvas</param>
/// <param name="bounds">The area to be filled, covers the entire canvas</param>
public virtual void OnProfileRendering(SKCanvas canvas, SKRect bounds)
{
}
/// <summary>
/// Called whenever the profile has been rendered
/// </summary>
/// <param name="canvas">The profile canvas</param>
/// <param name="bounds">The area to be filled, covers the entire canvas</param>
public virtual void OnProfileRendered(SKCanvas canvas, SKRect bounds)
{
}
#region Overrides of Script
/// <inheritdoc />
internal override void InternalCleanup()
{
lock (Profile.Scripts)
{
Profile.Scripts.Remove(this);
}
}
#endregion
}
}
@@ -0,0 +1,49 @@
namespace Artemis.Core.ScriptingProviders
{
/// <summary>
/// Represents a script bound to a specific <see cref="LayerProperty{T}" /> processed by a
/// <see cref="ScriptingProvider" />.
/// </summary>
public abstract class PropertyScript : Script
{
/// <inheritdoc />
protected PropertyScript(ILayerProperty layerProperty, ScriptConfiguration configuration) : base(configuration)
{
LayerProperty = layerProperty;
}
/// <summary>
/// Gets the layer property this script is bound to
/// </summary>
public ILayerProperty LayerProperty { get; }
/// <summary>
/// Called whenever the property is about to update
/// </summary>
/// <param name="deltaTime">Seconds passed since last update</param>
public virtual void OnPropertyUpdating(double deltaTime)
{
}
/// <summary>
/// Called whenever the property has been updated
/// </summary>
/// <param name="deltaTime">Seconds passed since last update</param>
public virtual void OnPropertyUpdated(double deltaTime)
{
}
#region Overrides of Script
/// <inheritdoc />
internal override void InternalCleanup()
{
lock (LayerProperty.Scripts)
{
LayerProperty.Scripts.Remove(this);
}
}
#endregion
}
}
@@ -0,0 +1,103 @@
using System;
using System.ComponentModel;
namespace Artemis.Core.ScriptingProviders
{
/// <summary>
/// Represents a script processed by a <see cref="ScriptingProviders.ScriptingProvider" />.
/// </summary>
public abstract class Script : CorePropertyChanged, IDisposable
{
private ScriptingProvider _scriptingProvider = null!;
/// <summary>
/// The base constructor of any script
/// </summary>
/// <param name="configuration">The script configuration this script belongs to</param>
protected Script(ScriptConfiguration configuration)
{
if (configuration.Script != null)
throw new ArtemisCoreException("The provided script configuration already has an active script");
ScriptConfiguration = configuration;
ScriptConfiguration.Script = this;
ScriptConfiguration.PropertyChanged += ScriptConfigurationOnPropertyChanged;
}
/// <summary>
/// Gets the scripting provider this script belongs to
/// </summary>
public ScriptingProvider ScriptingProvider
{
get => _scriptingProvider;
internal set => SetAndNotify(ref _scriptingProvider, value);
}
/// <summary>
/// Gets the script configuration this script belongs to
/// </summary>
public ScriptConfiguration ScriptConfiguration { get; }
#region Event handlers
private void ScriptConfigurationOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(ScriptConfiguration.ScriptContent))
OnScriptContentChanged();
}
#endregion
#region IDisposable
/// <summary>
/// Releases the unmanaged resources used by the object and optionally releases the managed resources.
/// </summary>
/// <param name="disposing">
/// <see langword="true" /> to release both managed and unmanaged resources;
/// <see langword="false" /> to release only unmanaged resources.
/// </param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
}
}
/// <inheritdoc />
public void Dispose()
{
ScriptConfiguration.PropertyChanged -= ScriptConfigurationOnPropertyChanged;
ScriptConfiguration.Script = null;
ScriptingProvider.InternalScripts.Remove(this);
// Can't trust those pesky plugin devs!
InternalCleanup();
Dispose(true);
GC.SuppressFinalize(this);
}
internal abstract void InternalCleanup();
#endregion
#region Events
/// <summary>
/// Occurs when the contents of the script have changed
/// </summary>
public event EventHandler? ScriptContentChanged;
/// <summary>
/// Invokes the <see cref="ScriptContentChanged" /> event
/// </summary>
protected virtual void OnScriptContentChanged()
{
ScriptContentChanged?.Invoke(this, EventArgs.Empty);
}
#endregion
}
}
+12 -2
View File
@@ -5,6 +5,7 @@ using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Artemis.Core.Ninject;
using Artemis.Core.ScriptingProviders;
using Artemis.Storage;
using HidSharp;
using Ninject;
@@ -29,6 +30,7 @@ namespace Artemis.Core.Services
private readonly IPluginManagementService _pluginManagementService;
private readonly IProfileService _profileService;
private readonly IModuleService _moduleService;
private readonly IScriptingService _scriptingService;
private readonly IRgbService _rgbService;
private readonly List<Exception> _updateExceptions = new();
private DateTime _lastExceptionLog;
@@ -41,7 +43,8 @@ namespace Artemis.Core.Services
IPluginManagementService pluginManagementService,
IRgbService rgbService,
IProfileService profileService,
IModuleService moduleService)
IModuleService moduleService,
IScriptingService scriptingService)
{
Kernel = kernel;
Constants.CorePlugin.Kernel = kernel;
@@ -51,10 +54,11 @@ namespace Artemis.Core.Services
_rgbService = rgbService;
_profileService = profileService;
_moduleService = moduleService;
_scriptingService = scriptingService;
_loggingLevel = settingsService.GetSetting("Core.LoggingLevel", LogEventLevel.Debug);
_frameStopWatch = new Stopwatch();
StartupArguments = new List<string>();
_rgbService.IsRenderPaused = true;
_rgbService.Surface.Updating += SurfaceOnUpdating;
_loggingLevel.SettingChanged += (sender, args) => ApplyLoggingLevel();
@@ -107,6 +111,9 @@ namespace Artemis.Core.Services
{
_frameStopWatch.Restart();
foreach (GlobalScript script in _scriptingService.GlobalScripts)
script.OnCoreUpdating(args.DeltaTime);
_moduleService.UpdateActiveModules(args.DeltaTime);
SKTexture texture = _rgbService.OpenRender();
SKCanvas canvas = texture.Surface.Canvas;
@@ -126,6 +133,9 @@ namespace Artemis.Core.Services
canvas.Flush();
OnFrameRendered(new FrameRenderedEventArgs(texture, _rgbService.Surface));
foreach (GlobalScript script in _scriptingService.GlobalScripts)
script.OnCoreUpdated(args.DeltaTime);
}
catch (Exception e)
{
@@ -0,0 +1,219 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using Artemis.Core.ScriptingProviders;
using Ninject;
using Ninject.Parameters;
namespace Artemis.Core.Services
{
internal class ScriptingService : IScriptingService
{
private readonly IPluginManagementService _pluginManagementService;
private List<ScriptingProvider> _scriptingProviders;
public ScriptingService(IPluginManagementService pluginManagementService, IProfileService profileService)
{
_pluginManagementService = pluginManagementService;
InternalGlobalScripts = new List<GlobalScript>();
_pluginManagementService.PluginFeatureEnabled += PluginManagementServiceOnPluginFeatureToggled;
_pluginManagementService.PluginFeatureDisabled += PluginManagementServiceOnPluginFeatureToggled;
_scriptingProviders = _pluginManagementService.GetFeaturesOfType<ScriptingProvider>();
// No need to sub to Deactivated, scripts will deactivate themselves
profileService.ProfileActivated += ProfileServiceOnProfileActivated;
foreach (ProfileConfiguration profileConfiguration in profileService.ProfileConfigurations)
{
if (profileConfiguration.Profile != null)
InitializeProfileScripts(profileConfiguration.Profile);
}
}
internal List<GlobalScript> InternalGlobalScripts { get; }
private ConstructorArgument CreateScriptConstructorArgument(Type scriptType, object value)
{
// Limit to one constructor, there's no need to have more and it complicates things anyway
ConstructorInfo[] constructors = scriptType.GetConstructors();
if (constructors.Length != 1)
throw new ArtemisCoreException("Scripts must have exactly one constructor");
// Find the ScriptConfiguration parameter, it is required by the base constructor so its there for sure
ParameterInfo configurationParameter = constructors.First().GetParameters().First(p => value.GetType().IsAssignableFrom(p.ParameterType));
if (configurationParameter.Name == null)
throw new ArtemisCoreException($"Couldn't find a valid constructor argument on {scriptType.Name} with type {value.GetType().Name}");
return new ConstructorArgument(configurationParameter.Name, value);
}
private void PluginManagementServiceOnPluginFeatureToggled(object? sender, PluginFeatureEventArgs e)
{
_scriptingProviders = _pluginManagementService.GetFeaturesOfType<ScriptingProvider>();
}
private void ProfileServiceOnProfileActivated(object? sender, ProfileConfigurationEventArgs e)
{
if (e.ProfileConfiguration.Profile != null)
InitializeProfileScripts(e.ProfileConfiguration.Profile);
}
private void InitializeProfileScripts(Profile profile)
{
// Initialize the scripts on the profile
foreach (ScriptConfiguration scriptConfiguration in profile.ScriptConfigurations.Where(c => c.Script == null))
CreateScriptInstance(profile, scriptConfiguration);
foreach (Layer layer in profile.GetAllLayers())
{
// Initialize the scripts on the layers
foreach (ScriptConfiguration scriptConfiguration in layer.ScriptConfigurations.Where(c => c.Script == null))
CreateScriptInstance(layer, scriptConfiguration);
// Initialize the scripts on the layer properties of layers
foreach (ILayerProperty layerProperty in layer.GetAllLayerProperties())
{
foreach (ScriptConfiguration scriptConfiguration in layerProperty.ScriptConfigurations.Where(c => c.Script == null))
CreateScriptInstance(layerProperty, scriptConfiguration);
}
}
foreach (Folder folder in profile.GetAllFolders())
{
// Initialize the scripts on the layer properties of folders
foreach (ILayerProperty layerProperty in folder.GetAllLayerProperties())
{
foreach (ScriptConfiguration scriptConfiguration in layerProperty.ScriptConfigurations.Where(c => c.Script == null))
CreateScriptInstance(layerProperty, scriptConfiguration);
}
}
}
public ReadOnlyCollection<GlobalScript> GlobalScripts => InternalGlobalScripts.AsReadOnly();
public GlobalScript? CreateScriptInstance(ScriptConfiguration scriptConfiguration)
{
if (scriptConfiguration.Script != null)
throw new ArtemisCoreException("The provided script configuration already has an active script");
ScriptingProvider? provider = _scriptingProviders.FirstOrDefault(p => p.Id == scriptConfiguration.ScriptingProviderId);
if (provider == null)
return null;
GlobalScript script = (GlobalScript) provider.Plugin.Kernel!.Get(
provider.GlobalScriptType,
CreateScriptConstructorArgument(provider.GlobalScriptType, scriptConfiguration)
);
script.ScriptingProvider = provider;
script.ScriptingService = this;
provider.InternalScripts.Add(script);
InternalGlobalScripts.Add(script);
return script;
}
public ProfileScript? CreateScriptInstance(Profile profile, ScriptConfiguration scriptConfiguration)
{
if (scriptConfiguration.Script != null)
throw new ArtemisCoreException("The provided script configuration already has an active script");
ScriptingProvider? provider = _scriptingProviders.FirstOrDefault(p => p.Id == scriptConfiguration.ScriptingProviderId);
if (provider == null)
return null;
ProfileScript script = (ProfileScript) provider.Plugin.Kernel!.Get(
provider.ProfileScriptType,
CreateScriptConstructorArgument(provider.ProfileScriptType, profile),
CreateScriptConstructorArgument(provider.ProfileScriptType, scriptConfiguration)
);
script.ScriptingProvider = provider;
provider.InternalScripts.Add(script);
return script;
}
public LayerScript? CreateScriptInstance(Layer layer, ScriptConfiguration scriptConfiguration)
{
if (scriptConfiguration.Script != null)
throw new ArtemisCoreException("The provided script configuration already has an active script");
ScriptingProvider? provider = _scriptingProviders.FirstOrDefault(p => p.Id == scriptConfiguration.ScriptingProviderId);
if (provider == null)
return null;
LayerScript script = (LayerScript) provider.Plugin.Kernel!.Get(
provider.LayerScriptType,
CreateScriptConstructorArgument(provider.LayerScriptType, layer),
CreateScriptConstructorArgument(provider.LayerScriptType, scriptConfiguration)
);
script.ScriptingProvider = provider;
provider.InternalScripts.Add(script);
return script;
}
public PropertyScript? CreateScriptInstance(ILayerProperty layerProperty, ScriptConfiguration scriptConfiguration)
{
if (scriptConfiguration.Script != null)
throw new ArtemisCoreException("The provided script configuration already has an active script");
ScriptingProvider? provider = _scriptingProviders.FirstOrDefault(p => p.Id == scriptConfiguration.ScriptingProviderId);
if (provider == null)
return null;
PropertyScript script = (PropertyScript) provider.Plugin.Kernel!.Get(
provider.PropertyScriptType,
CreateScriptConstructorArgument(provider.PropertyScriptType, layerProperty),
CreateScriptConstructorArgument(provider.PropertyScriptType, scriptConfiguration)
);
script.ScriptingProvider = provider;
provider.InternalScripts.Add(script);
return script;
}
}
public interface IScriptingService : IArtemisService
{
/// <summary>
/// Gets a read only collection of all active global scripts
/// </summary>
ReadOnlyCollection<GlobalScript> GlobalScripts { get; }
/// <summary>
/// Attempts to create an instance of a global script configured in the provided <see cref="ScriptConfiguration" />
/// </summary>
/// <param name="scriptConfiguration">The script configuration of the script to instantiate</param>
/// <returns>An instance of the script if the script provider was found; otherwise <see langword="null" /></returns>
GlobalScript? CreateScriptInstance(ScriptConfiguration scriptConfiguration);
/// <summary>
/// Attempts to create an instance of a profile script configured in the provided <see cref="ScriptConfiguration" />
/// </summary>
/// <param name="profile">The profile the script is bound to</param>
/// <param name="scriptConfiguration">The script configuration of the script to instantiate</param>
/// <returns>An instance of the script if the script provider was found; otherwise <see langword="null" /></returns>
ProfileScript? CreateScriptInstance(Profile profile, ScriptConfiguration scriptConfiguration);
/// <summary>
/// Attempts to create an instance of a layer script configured in the provided <see cref="ScriptConfiguration" />
/// </summary>
/// <param name="layer">The layer the script is bound to</param>
/// <param name="scriptConfiguration">The script configuration of the script to instantiate</param>
/// <returns>An instance of the script if the script provider was found; otherwise <see langword="null" /></returns>
LayerScript? CreateScriptInstance(Layer layer, ScriptConfiguration scriptConfiguration);
/// <summary>
/// Attempts to create an instance of a layer property script configured in the provided
/// <see cref="ScriptConfiguration" />
/// </summary>
/// <param name="layerProperty">The layer property the script is bound to</param>
/// <param name="scriptConfiguration">The script configuration of the script to instantiate</param>
/// <returns>An instance of the script if the script provider was found; otherwise <see langword="null" /></returns>
PropertyScript? CreateScriptInstance(ILayerProperty layerProperty, ScriptConfiguration scriptConfiguration);
}
}
@@ -1,4 +1,5 @@
using System.Collections.ObjectModel;
using System;
using System.Collections.ObjectModel;
using Newtonsoft.Json;
using SkiaSharp;
@@ -158,5 +159,15 @@ namespace Artemis.Core.Services
/// </summary>
/// <param name="canvas"></param>
void RenderProfiles(SKCanvas canvas);
/// <summary>
/// Occurs whenever a profile has been activated
/// </summary>
public event EventHandler<ProfileConfigurationEventArgs>? ProfileActivated;
/// <summary>
/// Occurs whenever a profile has been deactivated
/// </summary>
public event EventHandler<ProfileConfigurationEventArgs>? ProfileDeactivated;
}
}
@@ -317,6 +317,8 @@ namespace Artemis.Core.Services
}
profileConfiguration.Profile = profile;
OnProfileActivated(new ProfileConfigurationEventArgs(profileConfiguration));
return profile;
}
@@ -330,6 +332,8 @@ namespace Artemis.Core.Services
Profile profile = profileConfiguration.Profile;
profileConfiguration.Profile = null;
profile.Dispose();
OnProfileDeactivated(new ProfileConfigurationEventArgs(profileConfiguration));
}
public void DeleteProfile(ProfileConfiguration profileConfiguration)
@@ -582,5 +586,22 @@ namespace Artemis.Core.Services
_profileRepository.Save(profile.ProfileEntity);
}
#region Events
public event EventHandler<ProfileConfigurationEventArgs>? ProfileActivated;
public event EventHandler<ProfileConfigurationEventArgs>? ProfileDeactivated;
protected virtual void OnProfileActivated(ProfileConfigurationEventArgs e)
{
ProfileActivated?.Invoke(this, e);
}
protected virtual void OnProfileDeactivated(ProfileConfigurationEventArgs e)
{
ProfileDeactivated?.Invoke(this, e);
}
#endregion
}
}