Datamodel - Simplified some things

This commit is contained in:
Robert committed 2020-06-26 23:05:30 +02:00
1 parent 9b09942bda
commit 291a343428
10 files changed
+111 -116

No files matched your search

@@ -1,9 +0,0 @@
using System;
namespace Artemis.Core.Attributes
{
public class DataModelPropertyAttribute : Attribute
{
public string DisplayName { get; set; }
}
}
@@ -1,6 +0,0 @@
namespace Artemis.Core.Models
{
public class DataModelDescription
{
}
}
@@ -0,0 +1,8 @@
using System;
namespace Artemis.Core.Plugins.Abstract.DataModels.Attributes
{
public class DataModelIgnoreAttribute : Attribute
{
}
}
@@ -1,11 +1,5 @@
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.Abstract.DataModels.Attributes;
using Artemis.Core.Plugins.Models;
using SkiaSharp;
namespace Artemis.Core.Plugins.Abstract.DataModels
{
@@ -16,54 +10,9 @@ namespace Artemis.Core.Plugins.Abstract.DataModels
/// </summary>
public PluginInfo PluginInfo { get; internal set; }
/// <summary>
/// Gets whether this data model is initialized
/// </summary>
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)
{
var propertyInfo = GetType().GetProperty(propertyName);
if (propertyInfo == null)
return null;
return (DataModelPropertyAttribute) Attribute.GetCustomAttribute(propertyInfo, typeof(DataModelPropertyAttribute));
}
internal void Initialize()
{
// 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 || !typeof(DataModel).IsAssignableFrom(propertyInfo.PropertyType))
continue;
// 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();
propertyInfo.SetValue(this, instance);
}
Initialized = true;
}
}
}
+2 -13
View File
@@ -61,37 +61,26 @@ namespace Artemis.Core.Services
}
}
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)
if (module.InternalDataModel.DataModelDescription == null)
{
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)
if (dataModelExpansion.InternalDataModel.DataModelDescription == null)
{
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);
@@ -1,10 +1,13 @@
using Artemis.Core.Models;
using System.Collections.ObjectModel;
using Artemis.Core.Models;
using Artemis.Core.Plugins.Abstract.DataModels;
namespace Artemis.Core.Services.Interfaces
{
public interface IDataModelService : IArtemisService
{
ReadOnlyCollection<DataModel> DataModelExpansions { get; }
/// <summary>
/// Add an expansion to the datamodel to be available for use after the next update
/// </summary>
@@ -16,11 +19,5 @@ namespace Artemis.Core.Services.Interfaces
/// </summary>
/// <param name="baseDataModelExpansion"></param>
void RemoveExpansion(DataModel baseDataModelExpansion);
/// <summary>
/// Generates a data model description for the main datamodel including all it's expansions
/// </summary>
/// <returns>The generated data model description</returns>
DataModelDescription GetMainDataModelDescription();
}
}