Datamodel - WIP stuff, need this on a diff. PC

This commit is contained in:
SpoinkyNL committed 2020-06-23 22:49:27 +02:00
1 parent 920aea6695
commit 2b7a507725
23 files changed
+392 -161

No files matched your search

@@ -10,13 +10,25 @@ namespace Artemis.Core.Models.Profile.Colors
{
public class ColorGradient : INotifyPropertyChanged
{
private float _rotation;
public ColorGradient()
{
Stops = new BindableCollection<ColorGradientStop>();
}
public BindableCollection<ColorGradientStop> Stops { get; }
public float Rotation { get; set; }
public float Rotation
{
get => _rotation;
set
{
if (value.Equals(_rotation)) return;
_rotation = value;
OnPropertyChanged();
}
}
public SKColor[] GetColorsArray()
{
@@ -7,14 +7,36 @@ namespace Artemis.Core.Models.Profile.Colors
{
public class ColorGradientStop : INotifyPropertyChanged
{
private SKColor _color;
private float _position;
public ColorGradientStop(SKColor color, float position)
{
Color = color;
Position = position;
}
public SKColor Color { get; set; }
public float Position { get; set; }
public SKColor Color
{
get => _color;
set
{
if (value.Equals(_color)) return;
_color = value;
OnPropertyChanged();
}
}
public float Position
{
get => _position;
set
{
if (value.Equals(_position)) return;
_position = value;
OnPropertyChanged();
}
}
#region PropertyChanged
@@ -13,8 +13,7 @@ namespace Artemis.Core.Models.Profile.LayerProperties
/// Represents a property on a layer. Properties are saved in storage and can optionally be modified from the UI.
/// <para>
/// Note: You cannot initialize layer properties yourself. If properly placed and annotated, the Artemis core will
/// initialize
/// these for you.
/// initialize these for you.
/// </para>
/// </summary>
/// <typeparam name="T">The type of property encapsulated in this layer property</typeparam>
@@ -44,7 +44,7 @@ namespace Artemis.Core.Models.Profile
public LayerPropertyGroup Parent { get; internal set; }
/// <summary>
/// Gets whether this property group's properties are all initialized
/// Gets whether this property groups properties are all initialized
/// </summary>
public bool PropertiesInitialized { get; private set; }
@@ -0,0 +1,40 @@
using Artemis.Core.Plugins.Abstract.DataModels;
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
namespace Artemis.Core.Plugins.Abstract
{
/// <summary>
/// Allows you to expand the application-wide datamodel
/// </summary>
public abstract class BaseDataModelExpansion<T> : BaseDataModelExpansion where T : DataModel
{
/// <summary>
/// The data model driving this module
/// </summary>
public T DataModel
{
get => (T) InternalDataModel;
internal set => InternalDataModel = value;
}
}
/// <summary>
/// For internal use only, to implement your own layer property type, extend <see cref="BaseDataModelExpansion{T}" />
/// instead.
/// </summary>
public abstract class BaseDataModelExpansion : Plugin
{
internal DataModel InternalDataModel { get; set; }
public abstract void Update(double deltaTime);
/// <summary>
/// Override to provide your own data model description. By default this returns a description matching your plugin
/// name and description
/// </summary>
/// <returns></returns>
public virtual DataModelPropertyAttribute GetDataModelDescription()
{
return new DataModelPropertyAttribute {Name = PluginInfo.Name, Description = PluginInfo.Description};
}
}
}
@@ -1,11 +0,0 @@
namespace Artemis.Core.Plugins.Abstract
{
/// <inheritdoc />
/// <summary>
/// Allows you to expand the application-wide datamodel
/// </summary>
public abstract class DataModelExpansion : Plugin
{
public abstract void Update(double deltaTime);
}
}
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Artemis.Core.Exceptions;
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
using Artemis.Core.Plugins.Exceptions;
using Artemis.Core.Plugins.Models;
using SkiaSharp;
namespace Artemis.Core.Plugins.Abstract.DataModels
@@ -23,39 +25,63 @@ namespace Artemis.Core.Plugins.Abstract.DataModels
typeof(SKPoint)
};
protected DataModel(Module module)
{
Module = module;
Validate();
}
public Module Module { get; }
/// <summary>
/// Gets the plugin info this data model belongs to
/// </summary>
public PluginInfo PluginInfo { get; internal set; }
/// <summary>
/// Recursively validates the current datamodel, ensuring all properties annotated with
/// <see cref="DataModelPropertyAttribute" /> are of supported types.
/// Gets whether this data model is initialized
/// </summary>
/// <returns></returns>
public bool Validate()
public bool Initialized { get; private set; }
/// <summary>
/// Gets the <see cref="DataModelPropertyAttribute" /> describing this data model
/// </summary>
public DataModelPropertyAttribute DataModelDescription { get; internal set; }
/// <summary>
/// If found on this type, returns the <see cref="DataModelPropertyAttribute" /> for the provided property name
/// </summary>
/// <param name="propertyName">The name of the property on to look for</param>
public DataModelPropertyAttribute GetPropertyAttribute(string propertyName)
{
return ValidateType(GetType());
var propertyInfo = GetType().GetProperty(propertyName);
if (propertyInfo == null)
return null;
return (DataModelPropertyAttribute) Attribute.GetCustomAttribute(propertyInfo, typeof(DataModelPropertyAttribute));
}
private bool ValidateType(Type type)
internal void Initialize()
{
foreach (var propertyInfo in type.GetProperties())
// Doubt this will happen but let's make sure
if (Initialized)
throw new ArtemisCoreException("Data model already initialized, wut");
foreach (var propertyInfo in GetType().GetProperties())
{
var dataModelPropertyAttribute = (DataModelPropertyAttribute) Attribute.GetCustomAttribute(propertyInfo, typeof(DataModelPropertyAttribute));
if (dataModelPropertyAttribute == null)
continue;
// If the a nested datamodel, ensure the properties on there are valid
// If the a nested datamodel create an instance and initialize it
if (typeof(DataModel).IsAssignableFrom(propertyInfo.PropertyType))
ValidateType(propertyInfo.PropertyType);
{
var instance = (DataModel) Activator.CreateInstance(propertyInfo.PropertyType, true);
if (instance == null)
throw new ArtemisCoreException($"Failed to create instance of child datamodel at {propertyInfo.Name}");
instance.PluginInfo = PluginInfo;
instance.DataModelDescription = dataModelPropertyAttribute;
instance.Initialize();
propertyInfo.SetValue(this, instance);
}
else if (!SupportedTypes.Contains(propertyInfo.PropertyType))
{
// Show a useful error for plugin devs
throw new ArtemisPluginException(Module.PluginInfo,
throw new ArtemisPluginException(PluginInfo,
$"Plugin datamodel contains property of unsupported type {propertyInfo.PropertyType.Name}. \r\n\r\n" +
$"Property name: {propertyInfo.Name}\r\n" +
$"Property declared on: {propertyInfo.DeclaringType?.Name ?? "-"} \r\n\r\n" +
@@ -63,7 +89,7 @@ namespace Artemis.Core.Plugins.Abstract.DataModels
}
}
return true;
Initialized = true;
}
}
}
+54 -12
View File
@@ -1,17 +1,65 @@
using System.Collections.Generic;
using Artemis.Core.Models.Surface;
using Artemis.Core.Plugins.Abstract.DataModels;
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
using Artemis.Core.Plugins.Abstract.ViewModels;
using SkiaSharp;
namespace Artemis.Core.Plugins.Abstract
{
/// <inheritdoc />
/// <summary>
/// Allows you to add support for new games/applications while utilizing your own data model
/// </summary>
public abstract class Module<T> : Module where T : DataModel
{
/// <summary>
/// The data model driving this module
/// </summary>
public T DataModel
{
get => (T) InternalDataModel;
internal set => InternalDataModel = value;
}
/// <summary>
/// Gets or sets whether this module must also expand the main data model
/// <para>
/// Note: If expanding the main data model is all you want your plugin to do, create a
/// <see cref="BaseDataModelExpansion" /> plugin instead.
/// </para>
/// </summary>
public bool ExpandsDataModel
{
get => InternalExpandsMainDataModel;
set => InternalExpandsMainDataModel = value;
}
/// <summary>
/// Override to provide your own data model description. By default this returns a description matching your plugin
/// name and description
/// </summary>
/// <returns></returns>
public virtual DataModelPropertyAttribute GetDataModelDescription()
{
return new DataModelPropertyAttribute {Name = PluginInfo.Name, Description = PluginInfo.Description};
}
internal override DataModelPropertyAttribute InternalGetDataModelDescription()
{
return GetDataModelDescription();
}
}
/// <summary>
/// Allows you to add support for new games/applications
/// </summary>
public abstract class Module : Plugin
{
internal DataModel InternalDataModel { get; set; }
internal bool InternalExpandsMainDataModel { get; set; }
/// <summary>
/// The modules display name that's shown in the menu
/// </summary>
@@ -23,17 +71,6 @@ namespace Artemis.Core.Plugins.Abstract
/// </summary>
public string DisplayIcon { get; set; }
/// <summary>
/// The optional datamodel driving this module
/// </summary>
public DataModel DataModel { get; set; }
/// <summary>
/// Whether or not this module expands upon the main data model. If set to true any data in main data model can be
/// accessed by profiles in this module
/// </summary>
public bool ExpandsMainDataModel { get; protected set; }
/// <summary>
/// Called each frame when the module must update
/// </summary>
@@ -54,5 +91,10 @@ namespace Artemis.Core.Plugins.Abstract
/// </summary>
/// <returns></returns>
public abstract IEnumerable<ModuleViewModel> GetViewModels();
internal virtual DataModelPropertyAttribute InternalGetDataModelDescription()
{
return null;
}
}
}
@@ -4,7 +4,6 @@ using Artemis.Core.Plugins.Models;
namespace Artemis.Core.Plugins.Abstract
{
/// <inheritdoc />
/// <summary>
/// This is the base plugin type, use the other interfaces such as Module to create plugins
/// </summary>
@@ -2,10 +2,58 @@
using Artemis.Core.Exceptions;
using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Surface;
using Artemis.Core.Plugins.Abstract.DataModels;
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
using SkiaSharp;
namespace Artemis.Core.Plugins.Abstract
{
/// <summary>
/// Allows you to add support for new games/applications while utilizing Artemis' profile engine and your own data model
/// </summary>
public abstract class ProfileModule<T> : ProfileModule where T : DataModel
{
/// <summary>
/// The data model driving this module
/// </summary>
public T DataModel
{
get => (T)InternalDataModel;
internal set => InternalDataModel = value;
}
/// <summary>
/// Gets or sets whether this module must also expand the main data model
/// <para>
/// Note: If expanding the main data model is all you want your plugin to do, create a
/// <see cref="BaseDataModelExpansion" /> plugin instead.
/// </para>
/// </summary>
public bool ExpandsDataModel
{
get => InternalExpandsMainDataModel;
set => InternalExpandsMainDataModel = value;
}
/// <summary>
/// Override to provide your own data model description. By default this returns a description matching your plugin
/// name and description
/// </summary>
/// <returns></returns>
public virtual DataModelPropertyAttribute GetDataModelDescription()
{
return new DataModelPropertyAttribute { Name = PluginInfo.Name, Description = PluginInfo.Description };
}
internal override DataModelPropertyAttribute InternalGetDataModelDescription()
{
return GetDataModelDescription();
}
}
/// <summary>
/// Allows you to add support for new games/applications while utilizing Artemis' profile engine
/// </summary>
public abstract class ProfileModule : Module
{
public Profile ActiveProfile { get; private set; }
+22 -7
View File
@@ -6,6 +6,7 @@ using Artemis.Core.Events;
using Artemis.Core.Exceptions;
using Artemis.Core.JsonConverters;
using Artemis.Core.Ninject;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.Models;
using Artemis.Core.Services.Interfaces;
using Artemis.Core.Services.Storage.Interfaces;
@@ -24,13 +25,14 @@ namespace Artemis.Core.Services
/// </summary>
public class CoreService : ICoreService
{
private readonly Stopwatch _frameStopWatch;
private readonly ILogger _logger;
private readonly PluginSetting<LogEventLevel> _loggingLevel;
private readonly IPluginService _pluginService;
private readonly IProfileService _profileService;
private readonly IRgbService _rgbService;
private readonly ISurfaceService _surfaceService;
private readonly Stopwatch _frameStopWatch;
private List<BaseDataModelExpansion> _dataModelExpansions;
private List<Module> _modules;
// ReSharper disable once UnusedParameter.Local - Storage migration service is injected early to ensure it runs before anything else
@@ -48,9 +50,9 @@ namespace Artemis.Core.Services
_rgbService.Surface.Updated += SurfaceOnUpdated;
_loggingLevel.SettingChanged += (sender, args) => ApplyLoggingLevel();
_modules = _pluginService.GetPluginsOfType<Module>();
_pluginService.PluginEnabled += (sender, args) => _modules = _pluginService.GetPluginsOfType<Module>();
_pluginService.PluginDisabled += (sender, args) => _modules = _pluginService.GetPluginsOfType<Module>();
_pluginService.PluginEnabled += (sender, args) => UpdatePluginCache();
_pluginService.PluginDisabled += (sender, args) => UpdatePluginCache();
UpdatePluginCache();
_frameStopWatch = new Stopwatch();
@@ -58,7 +60,7 @@ namespace Artemis.Core.Services
}
public TimeSpan FrameTime { get; private set; }
public bool ModuleUpdatingDisabled { get; set; }
public bool PluginUpdatingDisabled { get; set; }
public bool ModuleRenderingDisabled { get; set; }
public void Dispose()
@@ -75,7 +77,7 @@ namespace Artemis.Core.Services
throw new ArtemisCoreException("Cannot initialize the core as it is already initialized.");
var versionAttribute = typeof(CoreService).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
_logger.Information("Initializing Artemis Core version {version}", versionAttribute?.InformationalVersion);
_logger.Information("Initializing Artemis Core version {version}", versionAttribute?.InformationalVersion);
ApplyLoggingLevel();
// Initialize the services
@@ -103,6 +105,12 @@ namespace Artemis.Core.Services
FrameRendered?.Invoke(this, e);
}
private void UpdatePluginCache()
{
_modules = _pluginService.GetPluginsOfType<Module>();
_dataModelExpansions = _pluginService.GetPluginsOfType<BaseDataModelExpansion>();
}
private void ConfigureJsonConvert()
{
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
@@ -125,8 +133,15 @@ namespace Artemis.Core.Services
try
{
_frameStopWatch.Restart();
if (!ModuleUpdatingDisabled && _modules != null)
if (!PluginUpdatingDisabled)
{
lock (_dataModelExpansions)
{
// Update all active modules
foreach (var dataModelExpansion in _dataModelExpansions)
dataModelExpansion.Update(args.DeltaTime);
}
lock (_modules)
{
// Update all active modules
@@ -0,0 +1,112 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Artemis.Core.Events;
using Artemis.Core.Exceptions;
using Artemis.Core.Models;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.Abstract.DataModels;
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
using Artemis.Core.Plugins.Exceptions;
using Artemis.Core.Services.Interfaces;
namespace Artemis.Core.Services
{
/// <summary>
/// Provides access to the main data model
/// </summary>
public class DataModelService : IDataModelService
{
private readonly IPluginService _pluginService;
private readonly List<DataModel> _dataModelExpansions;
internal DataModelService(IPluginService pluginService)
{
_pluginService = pluginService;
_dataModelExpansions = new List<DataModel>();
_pluginService.PluginEnabled += PluginServiceOnPluginEnabled;
_pluginService.PluginDisabled += PluginServiceOnPluginDisabled;
}
public ReadOnlyCollection<DataModel> DataModelExpansions
{
get
{
lock (_dataModelExpansions)
{
return new List<DataModel>(_dataModelExpansions).AsReadOnly();
}
}
}
public void AddExpansion(DataModel dataModelExpansion)
{
lock (_dataModelExpansions)
{
_dataModelExpansions.Add(dataModelExpansion);
// TODO SpoinkyNL 3-3-2018: Initialize the expansion and fire an event
}
}
public void RemoveExpansion(DataModel dataModelExpansion)
{
lock (_dataModelExpansions)
{
if (!_dataModelExpansions.Contains(dataModelExpansion))
throw new ArtemisCoreException("Cannot remove a data model expansion that wasn't previously added.");
// TODO SpoinkyNL 3-3-2018: Dispose the expansion and fire an event
_dataModelExpansions.Remove(dataModelExpansion);
}
}
public DataModelDescription GetMainDataModelDescription()
{
var dataModelDescription = new DataModelDescription();
return dataModelDescription;
}
private void PluginServiceOnPluginEnabled(object sender, PluginEventArgs e)
{
if (e.PluginInfo.Instance is Module module && module.InternalExpandsMainDataModel)
{
if (!module.InternalDataModel.Initialized)
{
module.InternalDataModel.DataModelDescription = module.InternalGetDataModelDescription();
if (module.InternalDataModel.DataModelDescription == null)
throw new ArtemisPluginException(module.PluginInfo, "Module overrides GetDataModelDescription but returned null");
module.InternalDataModel.Initialize();
}
_dataModelExpansions.Add(module.InternalDataModel);
}
else if (e.PluginInfo.Instance is BaseDataModelExpansion dataModelExpansion)
{
if (!dataModelExpansion.InternalDataModel.Initialized)
{
dataModelExpansion.InternalDataModel.DataModelDescription = dataModelExpansion.GetDataModelDescription();
if (dataModelExpansion.InternalDataModel.DataModelDescription == null)
throw new ArtemisPluginException(dataModelExpansion.PluginInfo, "Data model expansion overrides GetDataModelDescription but returned null");
dataModelExpansion.InternalDataModel.Initialize();
}
_dataModelExpansions.Add(dataModelExpansion.InternalDataModel);
}
}
private void PluginServiceOnPluginDisabled(object sender, PluginEventArgs e)
{
// Remove all data models related to the plugin
lock (_dataModelExpansions)
{
var toRemove = _dataModelExpansions.Where(d => d.PluginInfo == e.PluginInfo).ToList();
foreach (var dataModel in toRemove)
_dataModelExpansions.Remove(dataModel);
}
}
}
}
@@ -18,7 +18,7 @@ namespace Artemis.Core.Services.Interfaces
/// <summary>
/// Gets or sets whether modules are updated each frame by calling their Update method
/// </summary>
bool ModuleUpdatingDisabled { get; set; }
bool PluginUpdatingDisabled { get; set; }
/// <summary>
/// Gets or sets whether modules are rendered each frame by calling their Render method
@@ -1,27 +1,21 @@
using Artemis.Core.Models;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.Abstract.DataModels;
namespace Artemis.Core.Services.Interfaces
{
public interface IMainDataModelService : IArtemisService
public interface IDataModelService : IArtemisService
{
/// <summary>
/// Called each frame when the main data model must update
/// </summary>
/// <param name="deltaTime">Time since the last update</param>
void Update(double deltaTime);
/// <summary>
/// Add an expansion to the datamodel to be available for use after the next update
/// </summary>
/// <param name="dataModelExpansion"></param>
void AddExpansion(DataModelExpansion dataModelExpansion);
/// <param name="baseDataModelExpansion"></param>
void AddExpansion(DataModel baseDataModelExpansion);
/// <summary>
/// Remove a previously added expansion so that it is no longer available and updated
/// </summary>
/// <param name="dataModelExpansion"></param>
void RemoveExpansion(DataModelExpansion dataModelExpansion);
/// <param name="baseDataModelExpansion"></param>
void RemoveExpansion(DataModel baseDataModelExpansion);
/// <summary>
/// Generates a data model description for the main datamodel including all it's expansions
@@ -1,71 +0,0 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Artemis.Core.Exceptions;
using Artemis.Core.Models;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Services.Interfaces;
namespace Artemis.Core.Services
{
/// <summary>
/// Provides access to the main data model
/// </summary>
public class MainDataModelService : IMainDataModelService
{
private readonly List<DataModelExpansion> _dataModelExpansions;
internal MainDataModelService()
{
_dataModelExpansions = new List<DataModelExpansion>();
}
public ReadOnlyCollection<DataModelExpansion> DataModelExpansions
{
get
{
lock (_dataModelExpansions)
{
return _dataModelExpansions.AsReadOnly();
}
}
}
public void Update(double deltaTime)
{
lock (_dataModelExpansions)
{
// Update all expansions
foreach (var expansion in _dataModelExpansions)
expansion.Update(deltaTime);
}
}
public void AddExpansion(DataModelExpansion dataModelExpansion)
{
lock (_dataModelExpansions)
{
_dataModelExpansions.Add(dataModelExpansion);
// TODO SpoinkyNL 3-3-2018: Initialize the expansion and fire an event
}
}
public void RemoveExpansion(DataModelExpansion dataModelExpansion)
{
lock (_dataModelExpansions)
{
if (!_dataModelExpansions.Contains(dataModelExpansion))
throw new ArtemisCoreException("Cannot remove a data model expansion that wasn't previously added.");
// TODO SpoinkyNL 3-3-2018: Dispose the expansion and fire an event
_dataModelExpansions.Remove(dataModelExpansion);
}
}
public DataModelDescription GetMainDataModelDescription()
{
var dataModelDescription = new DataModelDescription();
return dataModelDescription;
}
}
}