Data model - Added data model visualization view model

Debugger - Split debugger into different tabs
Debugger - Added data model debugger
This commit is contained in:
SpoinkyNL committed 2020-06-29 00:22:16 +02:00
1 parent 291a343428
commit 796c0dc671
33 files changed
+565 -236

No files matched your search

@@ -12,13 +12,13 @@ namespace Artemis.Core.Extensions
file.CopyTo(Path.Combine(target.FullName, file.Name));
}
public static void RecursiveDelete(this DirectoryInfo baseDir)
public static void DeleteRecursively(this DirectoryInfo baseDir)
{
if (!baseDir.Exists)
return;
foreach (var dir in baseDir.EnumerateDirectories())
RecursiveDelete(dir);
DeleteRecursively(dir);
var files = baseDir.GetFiles();
foreach (var file in files)
{
@@ -12,6 +12,11 @@ namespace Artemis.Core.Extensions
return type.BaseType?.GetGenericTypeDefinition() == genericType;
}
public static bool IsStruct(this Type source)
{
return source.IsValueType && !source.IsPrimitive && !source.IsEnum;
}
public static bool IsNumber(this object value)
{
return value is sbyte
@@ -1,4 +1,5 @@
using Artemis.Core.Plugins.Abstract.DataModels;
using System;
using Artemis.Core.Plugins.Abstract.DataModels;
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
namespace Artemis.Core.Plugins.Abstract
@@ -16,6 +17,20 @@ namespace Artemis.Core.Plugins.Abstract
get => (T) InternalDataModel;
internal set => InternalDataModel = value;
}
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>
@@ -8,11 +8,13 @@ namespace Artemis.Core.Plugins.Abstract.DataModels
/// <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; }
}
}
+13 -8
View File
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Artemis.Core.Models.Surface;
using Artemis.Core.Plugins.Abstract.DataModels;
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
@@ -44,9 +45,18 @@ namespace Artemis.Core.Plugins.Abstract
return new DataModelPropertyAttribute {Name = PluginInfo.Name, Description = PluginInfo.Description};
}
internal override DataModelPropertyAttribute InternalGetDataModelDescription()
internal override void InternalEnablePlugin()
{
return GetDataModelDescription();
DataModel = Activator.CreateInstance<T>();
DataModel.PluginInfo = PluginInfo;
DataModel.DataModelDescription = GetDataModelDescription();
base.InternalEnablePlugin();
}
internal override void InternalDisablePlugin()
{
DataModel = null;
base.InternalDisablePlugin();
}
}
@@ -91,10 +101,5 @@ namespace Artemis.Core.Plugins.Abstract
/// </summary>
/// <returns></returns>
public abstract IEnumerable<ModuleViewModel> GetViewModels();
internal virtual DataModelPropertyAttribute InternalGetDataModelDescription()
{
return null;
}
}
}
+35 -11
View File
@@ -1,6 +1,8 @@
using System;
using Artemis.Core.Plugins.Abstract.ViewModels;
using Artemis.Core.Plugins.Exceptions;
using Artemis.Core.Plugins.Models;
using Castle.Core.Internal;
namespace Artemis.Core.Plugins.Abstract
{
@@ -9,7 +11,7 @@ namespace Artemis.Core.Plugins.Abstract
/// </summary>
public abstract class Plugin : IDisposable
{
public PluginInfo PluginInfo { get; internal set; }
public PluginInfo PluginInfo { get; internal set; }
/// <summary>
/// Gets whether the plugin is enabled
@@ -47,26 +49,39 @@ namespace Artemis.Core.Plugins.Abstract
return null;
}
internal void SetEnabled(bool enable)
internal void SetEnabled(bool enable, bool isAutoEnable = false)
{
if (enable && !Enabled)
{
Enabled = true;
PluginInfo.Enabled = true;
// If enable failed, put it back in a disabled state
try
{
EnablePlugin();
if (isAutoEnable && PluginInfo.GetLockFileCreated())
{
// Don't wrap existing lock exceptions, simply rethrow them
if (PluginInfo.LoadException is ArtemisPluginLockException)
throw PluginInfo.LoadException;
throw new ArtemisPluginLockException(PluginInfo.LoadException);
}
Enabled = true;
PluginInfo.Enabled = true;
PluginInfo.CreateLockFile();
InternalEnablePlugin();
OnPluginEnabled();
PluginInfo.LoadException = null;
}
catch
// If enable failed, put it back in a disabled state
catch (Exception e)
{
Enabled = false;
PluginInfo.Enabled = false;
PluginInfo.LoadException = e;
throw;
}
OnPluginEnabled();
PluginInfo.DeleteLockFile();
}
else if (!enable && Enabled)
{
@@ -74,12 +89,21 @@ namespace Artemis.Core.Plugins.Abstract
PluginInfo.Enabled = false;
// Even if disable failed, still leave it in a disabled state to avoid more issues
DisablePlugin();
InternalDisablePlugin();
OnPluginDisabled();
}
}
internal virtual void InternalEnablePlugin()
{
EnablePlugin();
}
internal virtual void InternalDisablePlugin()
{
DisablePlugin();
}
#region Events
public event EventHandler PluginEnabled;
@@ -4,6 +4,7 @@ using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Surface;
using Artemis.Core.Plugins.Abstract.DataModels;
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
using Artemis.Core.Plugins.Models;
using SkiaSharp;
namespace Artemis.Core.Plugins.Abstract
@@ -18,7 +19,7 @@ namespace Artemis.Core.Plugins.Abstract
/// </summary>
public T DataModel
{
get => (T)InternalDataModel;
get => (T) InternalDataModel;
internal set => InternalDataModel = value;
}
@@ -42,12 +43,21 @@ namespace Artemis.Core.Plugins.Abstract
/// <returns></returns>
public virtual DataModelPropertyAttribute GetDataModelDescription()
{
return new DataModelPropertyAttribute { Name = PluginInfo.Name, Description = PluginInfo.Description };
return new DataModelPropertyAttribute {Name = PluginInfo.Name, Description = PluginInfo.Description};
}
internal override void InternalEnablePlugin()
{
DataModel = Activator.CreateInstance<T>();
DataModel.PluginInfo = PluginInfo;
DataModel.DataModelDescription = GetDataModelDescription();
base.InternalEnablePlugin();
}
internal override DataModelPropertyAttribute InternalGetDataModelDescription()
internal override void InternalDisablePlugin()
{
return GetDataModelDescription();
DataModel = null;
base.InternalDisablePlugin();
}
}
@@ -0,0 +1,18 @@
using System;
namespace Artemis.Core.Plugins.Exceptions
{
public class ArtemisPluginLockException : Exception
{
public ArtemisPluginLockException(Exception innerException) : base(CreateExceptionMessage(innerException), innerException)
{
}
private static string CreateExceptionMessage(Exception innerException)
{
return innerException != null
? "Found a lock file, skipping load, see inner exception for last known exception."
: "Found a lock file, skipping load.";
}
}
}
+27 -11
View File
@@ -12,16 +12,17 @@ namespace Artemis.Core.Plugins.Models
[JsonObject(MemberSerialization.OptIn)]
public class PluginInfo : PropertyChangedBase
{
private Guid _guid;
private string _name;
private string _description;
private string _icon;
private Version _version;
private string _main;
private DirectoryInfo _directory;
private Plugin _instance;
private bool _enabled;
private Guid _guid;
private string _icon;
private Plugin _instance;
private bool _lastEnableSuccessful;
private Exception _loadException;
private string _main;
private string _name;
private Version _version;
internal PluginInfo()
{
@@ -117,12 +118,12 @@ namespace Artemis.Core.Plugins.Models
}
/// <summary>
/// Indicates whether the last time the plugin loaded, it loaded correctly
/// Gets the exception thrown while loading
/// </summary>
public bool LastEnableSuccessful
public Exception LoadException
{
get => _lastEnableSuccessful;
internal set => SetAndNotify(ref _lastEnableSuccessful, value);
get => _loadException;
internal set => SetAndNotify(ref _loadException, value);
}
/// <summary>
@@ -149,7 +150,22 @@ namespace Artemis.Core.Plugins.Models
{
PluginEntity.Id = Guid;
PluginEntity.IsEnabled = Enabled;
PluginEntity.LastEnableSuccessful = LastEnableSuccessful;
}
internal void CreateLockFile()
{
File.Create(Path.Combine(Directory.FullName, "artemis.lock")).Close();
}
internal void DeleteLockFile()
{
if (GetLockFileCreated())
File.Delete(Path.Combine(Directory.FullName, "artemis.lock"));
}
internal bool GetLockFileCreated()
{
return File.Exists(Path.Combine(Directory.FullName, "artemis.lock"));
}
}
}
+3 -2
View File
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Artemis.Core.Events;
using Artemis.Core.Exceptions;
@@ -107,8 +108,8 @@ namespace Artemis.Core.Services
private void UpdatePluginCache()
{
_modules = _pluginService.GetPluginsOfType<Module>();
_dataModelExpansions = _pluginService.GetPluginsOfType<BaseDataModelExpansion>();
_modules = _pluginService.GetPluginsOfType<Module>().Where(p => p.Enabled).ToList();
_dataModelExpansions = _pluginService.GetPluginsOfType<BaseDataModelExpansion>().Where(p => p.Enabled).ToList();
}
private void ConfigureJsonConvert()
+23 -22
View File
@@ -3,10 +3,8 @@ 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;
@@ -17,8 +15,8 @@ namespace Artemis.Core.Services
/// </summary>
public class DataModelService : IDataModelService
{
private readonly IPluginService _pluginService;
private readonly List<DataModel> _dataModelExpansions;
private readonly IPluginService _pluginService;
internal DataModelService(IPluginService pluginService)
{
@@ -27,6 +25,11 @@ namespace Artemis.Core.Services
_pluginService.PluginEnabled += PluginServiceOnPluginEnabled;
_pluginService.PluginDisabled += PluginServiceOnPluginDisabled;
foreach (var module in _pluginService.GetPluginsOfType<Module>().Where(m => m.InternalExpandsMainDataModel))
AddModuleDataModel(module);
foreach (var dataModelExpansion in _pluginService.GetPluginsOfType<BaseDataModelExpansion>())
AddDataModelExpansionDataModel(dataModelExpansion);
}
public ReadOnlyCollection<DataModel> DataModelExpansions
@@ -64,27 +67,25 @@ namespace Artemis.Core.Services
private void PluginServiceOnPluginEnabled(object sender, PluginEventArgs e)
{
if (e.PluginInfo.Instance is Module module && module.InternalExpandsMainDataModel)
{
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");
}
_dataModelExpansions.Add(module.InternalDataModel);
}
AddModuleDataModel(module);
else if (e.PluginInfo.Instance is BaseDataModelExpansion dataModelExpansion)
{
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");
}
AddDataModelExpansionDataModel(dataModelExpansion);
}
_dataModelExpansions.Add(dataModelExpansion.InternalDataModel);
}
private void AddDataModelExpansionDataModel(BaseDataModelExpansion dataModelExpansion)
{
if (dataModelExpansion.InternalDataModel.DataModelDescription == null)
throw new ArtemisPluginException(dataModelExpansion.PluginInfo, "Data model expansion overrides GetDataModelDescription but returned null");
AddExpansion(dataModelExpansion.InternalDataModel);
}
private void AddModuleDataModel(Module module)
{
if (module.InternalDataModel.DataModelDescription == null)
throw new ArtemisPluginException(module.PluginInfo, "Module overrides GetDataModelDescription but returned null");
AddExpansion(module.InternalDataModel);
}
private void PluginServiceOnPluginDisabled(object sender, PluginEventArgs e)
@@ -46,7 +46,8 @@ namespace Artemis.Core.Services.Interfaces
/// Enables the provided plugin
/// </summary>
/// <param name="plugin"></param>
void EnablePlugin(Plugin plugin);
/// <param name="isAutoEnable">If true, fails if there is a lock file present</param>
void EnablePlugin(Plugin plugin, bool isAutoEnable = false);
/// <summary>
/// Disables the provided plugin
+15 -26
View File
@@ -148,22 +148,14 @@ namespace Artemis.Core.Services
// Activate plugins after they are all loaded
foreach (var pluginInfo in _plugins.Where(p => p.Enabled))
{
if (!pluginInfo.LastEnableSuccessful)
{
pluginInfo.Enabled = false;
_logger.Warning("Plugin failed to load last time, disabling it now to avoid instability. Plugin info: {pluginInfo}", pluginInfo);
continue;
}
try
{
EnablePlugin(pluginInfo.Instance);
EnablePlugin(pluginInfo.Instance, true);
}
catch (Exception)
{
// ignored, logged in EnablePlugin
}
}
LoadingPlugins = false;
@@ -204,11 +196,10 @@ namespace Artemis.Core.Services
var pluginEntity = _pluginRepository.GetPluginByGuid(pluginInfo.Guid);
if (pluginEntity == null)
pluginEntity = new PluginEntity {Id = pluginInfo.Guid, IsEnabled = true, LastEnableSuccessful = true};
pluginEntity = new PluginEntity {Id = pluginInfo.Guid, IsEnabled = true};
pluginInfo.PluginEntity = pluginEntity;
pluginInfo.Enabled = pluginEntity.IsEnabled;
pluginInfo.LastEnableSuccessful = pluginEntity.LastEnableSuccessful;
var mainFile = Path.Combine(pluginInfo.Directory.FullName, pluginInfo.Main);
if (!File.Exists(mainFile))
@@ -294,20 +285,15 @@ namespace Artemis.Core.Services
}
}
public void EnablePlugin(Plugin plugin)
public void EnablePlugin(Plugin plugin, bool isAutoEnable = false)
{
lock (_plugins)
{
_logger.Debug("Enabling plugin {pluginInfo}", plugin.PluginInfo);
plugin.PluginInfo.LastEnableSuccessful = false;
plugin.PluginInfo.ApplyToEntity();
_pluginRepository.SavePlugin(plugin.PluginInfo.PluginEntity);
try
{
plugin.SetEnabled(true);
plugin.SetEnabled(true, isAutoEnable);
}
catch (Exception e)
{
@@ -316,16 +302,16 @@ namespace Artemis.Core.Services
}
finally
{
// We got this far so the plugin enabled and we didn't crash horribly, yay
if (plugin.PluginInfo.Enabled)
{
plugin.PluginInfo.LastEnableSuccessful = true;
plugin.PluginInfo.ApplyToEntity();
// On an auto-enable, ensure PluginInfo.Enabled is true even if enable failed, that way a failure on auto-enable does
// not affect the user's settings
if (isAutoEnable)
plugin.PluginInfo.Enabled = true;
_pluginRepository.SavePlugin(plugin.PluginInfo.PluginEntity);
plugin.PluginInfo.ApplyToEntity();
_pluginRepository.SavePlugin(plugin.PluginInfo.PluginEntity);
if (plugin.PluginInfo.Enabled)
_logger.Debug("Successfully enabled plugin {pluginInfo}", plugin.PluginInfo);
}
}
}
@@ -398,13 +384,16 @@ namespace Artemis.Core.Services
private static void CopyBuiltInPlugin(DirectoryInfo builtInPluginDirectory)
{
var pluginDirectory = new DirectoryInfo(Path.Combine(Constants.DataFolder, "plugins", builtInPluginDirectory.Name));
var createLockFile = File.Exists(Path.Combine(pluginDirectory.FullName, "artemis.lock"));
// Remove the old directory if it exists
if (Directory.Exists(pluginDirectory.FullName))
pluginDirectory.RecursiveDelete();
pluginDirectory.DeleteRecursively();
Directory.CreateDirectory(pluginDirectory.FullName);
builtInPluginDirectory.CopyFilesRecursively(pluginDirectory);
if (createLockFile)
File.Create(Path.Combine(pluginDirectory.FullName, "artemis.lock")).Close();
}
#region Events