Plugins - Adjusted namespaces

This commit is contained in:
SpoinkyNL committed 2020-08-21 22:32:46 +02:00
1 parent 625fcbafdd
commit e27f658cea
144 files changed
+267 -252

No files matched your search

@@ -0,0 +1,8 @@
using System;
namespace Artemis.Core.Plugins.DataModelExpansions.Attributes
{
public class DataModelIgnoreAttribute : Attribute
{
}
}
@@ -0,0 +1,43 @@
using System;
namespace Artemis.Core.Plugins.DataModelExpansions.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public class DataModelPropertyAttribute : Attribute
{
/// <summary>
/// Gets or sets the user-friendly name for this property, shown in the UI.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the user-friendly description for this property, shown in the UI.
/// </summary>
public string Description { get; set; }
/// <summary>
/// Gets or sets the an optional prefix to show before displaying elements in the UI.
/// </summary>
public string Prefix { get; set; }
/// <summary>
/// Gets or sets an optional affix to show behind displaying elements in the UI.
/// </summary>
public string Affix { get; set; }
/// <summary>
/// Gets or sets an optional maximum value, this value is not enforced but used for percentage calculations.
/// </summary>
public object MaxValue { get; set; }
/// <summary>
/// Gets or sets an optional minimum value, this value is not enforced but used for percentage calculations.
/// </summary>
public object MinValue { get; set; }
/// <summary>
/// Gets or sets whether this property resets the max depth of the data model, defaults to true
/// </summary>
public bool ResetsDepth { get; set; } = true;
}
}
@@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Artemis.Core.Plugins.DataModelExpansions.Attributes;
using Artemis.Core.Utilities;
namespace Artemis.Core.Plugins.DataModelExpansions
{
/// <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>
/// Hide the provided property using a lambda expression, e.g. HideProperty(dm => dm.TimeDataModel.CurrentTimeUTC)
/// </summary>
/// <param name="propertyLambda">A lambda expression pointing to the property to ignore</param>
public void HideProperty<TProperty>(Expression<Func<T, TProperty>> propertyLambda)
{
var propertyInfo = ReflectionUtilities.GetPropertyInfo(DataModel, propertyLambda);
if (!HiddenPropertiesList.Any(p => p.Equals(propertyInfo)))
HiddenPropertiesList.Add(propertyInfo);
}
/// <summary>
/// Stop hiding the provided property using a lambda expression, e.g. ShowProperty(dm => dm.TimeDataModel.CurrentTimeUTC)
/// </summary>
/// <param name="propertyLambda">A lambda expression pointing to the property to stop ignoring</param>
public void ShowProperty<TProperty>(Expression<Func<T, TProperty>> propertyLambda)
{
var propertyInfo = ReflectionUtilities.GetPropertyInfo(DataModel, propertyLambda);
HiddenPropertiesList.RemoveAll(p => p.Equals(propertyInfo));
}
internal override void InternalEnablePlugin()
{
DataModel = Activator.CreateInstance<T>();
DataModel.PluginInfo = PluginInfo;
DataModel.DataModelDescription = GetDataModelDescription();
base.InternalEnablePlugin();
}
internal override void InternalDisablePlugin()
{
DataModel = null;
base.InternalDisablePlugin();
}
}
/// <summary>
/// For internal use only, to implement your own layer property type, extend <see cref="BaseDataModelExpansion{T}" />
/// instead.
/// </summary>
public abstract class BaseDataModelExpansion : Plugin
{
protected readonly List<PropertyInfo> HiddenPropertiesList = new List<PropertyInfo>();
/// <summary>
/// Gets a list of all properties ignored at runtime using IgnoreProperty(x => x.y)
/// </summary>
public ReadOnlyCollection<PropertyInfo> HiddenProperties => HiddenPropertiesList.AsReadOnly();
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};
}
}
}
@@ -0,0 +1,134 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using Artemis.Core.Exceptions;
using Artemis.Core.Plugins.DataModelExpansions.Attributes;
using Artemis.Core.Plugins.Modules;
namespace Artemis.Core.Plugins.DataModelExpansions
{
public abstract class DataModel
{
/// <summary>
/// Gets the plugin info this data model belongs to
/// </summary>
[DataModelIgnore]
public PluginInfo PluginInfo { get; internal set; }
/// <summary>
/// Gets the <see cref="DataModelPropertyAttribute" /> describing this data model
/// </summary>
[DataModelIgnore]
public DataModelPropertyAttribute DataModelDescription { get; internal set; }
public bool ContainsPath(string path)
{
var parts = path.Split('.');
var current = GetType();
foreach (var part in parts)
{
var property = current?.GetProperty(part);
current = property?.PropertyType;
if (property == null)
return false;
}
return true;
}
public Type GetTypeAtPath(string path)
{
if (!ContainsPath(path))
return null;
var parts = path.Split('.');
var current = GetType();
Type result = null;
foreach (var part in parts)
{
var property = current.GetProperty(part);
current = property.PropertyType;
result = property.PropertyType;
}
return result;
}
public Type GetListTypeInPath(string path)
{
if (!ContainsPath(path))
return null;
var parts = path.Split('.');
var current = GetType();
var index = 0;
foreach (var part in parts)
{
// Only return a type if the path CONTAINS a list, not if it points TO a list
if (index == parts.Length - 1)
return null;
var property = current.GetProperty(part);
// For lists, look into the list type instead of the list itself
if (typeof(IList).IsAssignableFrom(property.PropertyType))
return property.PropertyType.GetGenericArguments()[0];
current = property.PropertyType;
index++;
}
return null;
}
public Type GetListTypeAtPath(string path)
{
if (!ContainsPath(path))
return null;
var child = GetTypeAtPath(path);
return child.GenericTypeArguments.Length > 0 ? child.GenericTypeArguments[0] : null;
}
public string GetListInnerPath(string path)
{
if (GetListTypeInPath(path) == null)
throw new ArtemisCoreException($"Cannot determine inner list path at {path} because it does not contain a list");
var parts = path.Split('.');
var current = GetType();
for (var index = 0; index < parts.Length; index++)
{
var part = parts[index];
var property = current.GetProperty(part);
if (typeof(IList).IsAssignableFrom(property.PropertyType))
return string.Join('.', parts.Skip(index + 1).ToList());
current = property.PropertyType;
}
return null;
}
/// <summary>
/// Returns a read-only list of all properties in this datamodel that are to be ignored
/// </summary>
/// <returns></returns>
public ReadOnlyCollection<PropertyInfo> GetHiddenProperties()
{
if (PluginInfo.Instance is ProfileModule profileModule)
return profileModule.HiddenProperties;
if (PluginInfo.Instance is BaseDataModelExpansion dataModelExpansion)
return dataModelExpansion.HiddenProperties;
return new List<PropertyInfo>().AsReadOnly();
}
}
}