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

Data model - More WIP stuff woo

This commit is contained in:
SpoinkyNL 2020-06-24 23:44:48 +02:00
parent 28bcfcc95a
commit 722f3d597a
2 changed files with 65 additions and 35 deletions

View File

@ -11,20 +11,6 @@ namespace Artemis.Core.Plugins.Abstract.DataModels
{
public abstract class DataModel
{
private static readonly List<Type> SupportedTypes = new List<Type>
{
typeof(bool),
typeof(byte),
typeof(decimal),
typeof(double),
typeof(float),
typeof(int),
typeof(long),
typeof(string),
typeof(SKColor),
typeof(SKPoint)
};
/// <summary>
/// Gets the plugin info this data model belongs to
/// </summary>
@ -62,31 +48,19 @@ namespace Artemis.Core.Plugins.Abstract.DataModels
foreach (var propertyInfo in GetType().GetProperties())
{
var dataModelPropertyAttribute = (DataModelPropertyAttribute) Attribute.GetCustomAttribute(propertyInfo, typeof(DataModelPropertyAttribute));
if (dataModelPropertyAttribute == null)
if (dataModelPropertyAttribute == null || !typeof(DataModel).IsAssignableFrom(propertyInfo.PropertyType))
continue;
// If the a nested datamodel create an instance and initialize it
if (typeof(DataModel).IsAssignableFrom(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}");
// If the property is a nested datamodel create an instance and initialize it
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();
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(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" +
$"Supported properties:\r\n{string.Join("\r\n", SupportedTypes.Select(t => $" - {t.Name}"))}");
}
propertyInfo.SetValue(this, instance);
}
Initialized = true;

View File

@ -0,0 +1,56 @@
using System;
using System.Linq;
using Artemis.Core.Attributes;
using Artemis.Core.Plugins.Abstract.DataModels;
using Artemis.UI.Exceptions;
using Stylet;
namespace Artemis.UI.DataModelVisualization
{
public class DataModelViewModel : PropertyChangedBase
{
public DataModelViewModel(DataModel dataModel)
{
if (!DataModel.Initialized)
throw new ArtemisUIException("Cannot create view model for data model that is not yet initialized");
DataModel = dataModel;
}
public DataModelViewModel(DataModel parent, DataModel dataModel)
{
if (!DataModel.Initialized)
throw new ArtemisUIException("Cannot create view model for data model that is not yet initialized");
Parent = parent;
DataModel = dataModel;
}
public DataModel DataModel { get; }
public DataModel Parent { get; set; }
private void PopulateProperties()
{
foreach (var propertyInfo in DataModel.GetType().GetProperties())
{
var dataModelPropertyAttribute = (DataModelPropertyAttribute) Attribute.GetCustomAttribute(propertyInfo, typeof(DataModelPropertyAttribute));
if (dataModelPropertyAttribute == null)
continue;
// For child data models create another data model view model
if (typeof(DataModel).IsAssignableFrom(propertyInfo.PropertyType))
{
}
// For primitives, create a property view model
else if (propertyInfo.PropertyType.IsPrimitive)
{
}
// For anything else check if it has any child primitives and if so create a property container view model
else if (propertyInfo.PropertyType.GetProperties().Any(p => p.PropertyType.IsPrimitive))
{
}
}
}
}
}