Plugins - Separated plugins and implementations as different principles

This commit is contained in:
SpoinkyNL committed 2020-11-09 20:31:54 +01:00
1 parent c0d0e421c7
commit 1e0dc1894d
53 files changed
+438 -410

No files matched your search

+3 -3
View File
@@ -133,8 +133,8 @@ namespace Artemis.Core.Services
private void UpdatePluginCache()
{
_modules = _pluginService.GetPluginsOfType<Module>().Where(p => p.Enabled).ToList();
_dataModelExpansions = _pluginService.GetPluginsOfType<BaseDataModelExpansion>().Where(p => p.Enabled).ToList();
_modules = _pluginService.GetPluginsOfType<Module>().Where(p => p.IsEnabled).ToList();
_dataModelExpansions = _pluginService.GetPluginsOfType<BaseDataModelExpansion>().Where(p => p.IsEnabled).ToList();
}
private void ConfigureJsonConvert()
@@ -162,7 +162,7 @@ namespace Artemis.Core.Services
lock (_dataModelExpansions)
{
// Update all active modules, check Enabled status because it may go false before before the _dataModelExpansions list is updated
foreach (BaseDataModelExpansion dataModelExpansion in _dataModelExpansions.Where(e => e.Enabled))
foreach (BaseDataModelExpansion dataModelExpansion in _dataModelExpansions.Where(e => e.IsEnabled))
dataModelExpansion.Update(args.DeltaTime);
}
@@ -46,22 +46,22 @@ namespace Artemis.Core.Services
/// <summary>
/// Enables the provided plugin
/// </summary>
/// <param name="plugin"></param>
/// <param name="pluginImplementation"></param>
/// <param name="isAutoEnable">If true, fails if there is a lock file present</param>
void EnablePlugin(Plugin plugin, bool isAutoEnable = false);
void EnablePlugin(PluginImplementation pluginImplementation, bool isAutoEnable = false);
/// <summary>
/// Disables the provided plugin
/// </summary>
/// <param name="plugin"></param>
void DisablePlugin(Plugin plugin);
/// <param name="pluginImplementation"></param>
void DisablePlugin(PluginImplementation pluginImplementation);
/// <summary>
/// Finds the plugin info related to the plugin
/// </summary>
/// <param name="plugin">The plugin you want to find the plugin info for</param>
/// <param name="pluginImplementation">The plugin you want to find the plugin info for</param>
/// <returns>The plugins PluginInfo</returns>
PluginInfo GetPluginInfo(Plugin plugin);
PluginInfo GetPluginInfo(PluginImplementation pluginImplementation);
/// <summary>
/// Gets the plugin info of all loaded plugins
@@ -70,31 +70,31 @@ namespace Artemis.Core.Services
List<PluginInfo> GetAllPluginInfo();
/// <summary>
/// Finds all enabled <see cref="Plugin" /> instances of <typeparamref name="T" />
/// Finds all enabled <see cref="PluginImplementation" /> instances of <typeparamref name="T" />
/// </summary>
/// <typeparam name="T">Either <see cref="Plugin" /> or a plugin type implementing <see cref="Plugin" /></typeparam>
/// <typeparam name="T">Either <see cref="PluginImplementation" /> or a plugin type implementing <see cref="PluginImplementation" /></typeparam>
/// <returns>Returns a list of plugin instances of <typeparamref name="T" /></returns>
List<T> GetPluginsOfType<T>() where T : Plugin;
List<T> GetPluginsOfType<T>() where T : PluginImplementation;
/// <summary>
/// Gets the plugin that provided the specified assembly
/// </summary>
/// <param name="assembly"></param>
/// <returns></returns>
Plugin GetPluginByAssembly(Assembly assembly);
PluginImplementation GetPluginByAssembly(Assembly assembly);
/// <summary>
/// Gets the plugin that defined the specified device
/// </summary>
/// <param name="device"></param>
/// <returns></returns>
Plugin GetPluginByDevice(IRGBDevice device);
PluginImplementation GetPluginByDevice(IRGBDevice device);
/// <summary>
/// Returns the plugin info of the current call stack
/// </summary>
/// <returns>If the current call stack contains a plugin, the plugin. Otherwise null</returns>
Plugin GetCallingPlugin();
PluginImplementation GetCallingPlugin();
#region Events
+1 -1
View File
@@ -103,7 +103,7 @@ namespace Artemis.Core.Services
{
lock (module)
{
bool shouldBeActivated = module.EvaluateActivationRequirements() && module.Enabled;
bool shouldBeActivated = module.EvaluateActivationRequirements() && module.IsEnabled;
if (shouldBeActivated && !module.IsActivated)
tasks.Add(ActivateModule(module));
else if (!shouldBeActivated && module.IsActivated)
+40 -40
View File
@@ -136,7 +136,7 @@ namespace Artemis.Core.Services
}
// Activate plugins after they are all loaded
foreach (PluginInfo pluginInfo in _plugins.Where(p => p.Enabled))
foreach (PluginInfo pluginInfo in _plugins.Where(p => p.IsEnabled))
{
try
{
@@ -180,7 +180,7 @@ namespace Artemis.Core.Services
pluginEntity = new PluginEntity {Id = pluginInfo.Guid, IsEnabled = true};
pluginInfo.PluginEntity = pluginEntity;
pluginInfo.Enabled = pluginEntity.IsEnabled;
pluginInfo.IsEnabled = pluginEntity.IsEnabled;
string mainFile = Path.Combine(pluginInfo.Directory.FullName, pluginInfo.Main);
if (!File.Exists(mainFile))
@@ -206,7 +206,7 @@ namespace Artemis.Core.Services
List<Type> pluginTypes;
try
{
pluginTypes = pluginInfo.Assembly.GetTypes().Where(t => typeof(Plugin).IsAssignableFrom(t)).ToList();
pluginTypes = pluginInfo.Assembly.GetTypes().Where(t => typeof(PluginImplementation).IsAssignableFrom(t)).ToList();
}
catch (ReflectionTypeLoadException e)
{
@@ -226,7 +226,7 @@ namespace Artemis.Core.Services
};
pluginInfo.Kernel = new ChildKernel(_kernel);
pluginInfo.Kernel.Load(new PluginModule(pluginInfo));
pluginInfo.Instance = (Plugin) pluginInfo.Kernel.Get(pluginType, constraint: null, parameters: parameters);
pluginInfo.Instance = (PluginImplementation) pluginInfo.Kernel.Get(pluginType, constraint: null, parameters: parameters);
pluginInfo.Instance.PluginInfo = pluginInfo;
}
catch (Exception e)
@@ -266,29 +266,29 @@ namespace Artemis.Core.Services
}
}
public void EnablePlugin(Plugin plugin, bool isAutoEnable = false)
public void EnablePlugin(PluginImplementation pluginImplementation, bool isAutoEnable = false)
{
_logger.Debug("Enabling plugin {pluginInfo}", plugin.PluginInfo);
OnPluginEnabling(new PluginEventArgs(plugin.PluginInfo));
_logger.Debug("Enabling plugin {pluginInfo}", pluginImplementation.PluginInfo);
OnPluginEnabling(new PluginEventArgs(pluginImplementation.PluginInfo));
lock (_plugins)
{
try
{
// A device provider may be queued for disable on next restart, this undoes that
if (plugin is DeviceProvider && plugin.Enabled && !plugin.PluginInfo.Enabled)
if (pluginImplementation is DeviceProvider && pluginImplementation.IsEnabled && !pluginImplementation.PluginInfo.IsEnabled)
{
plugin.PluginInfo.Enabled = true;
plugin.PluginInfo.ApplyToEntity();
_pluginRepository.SavePlugin(plugin.PluginInfo.PluginEntity);
pluginImplementation.PluginInfo.IsEnabled = true;
pluginImplementation.PluginInfo.ApplyToEntity();
_pluginRepository.SavePlugin(pluginImplementation.PluginInfo.PluginEntity);
return;
}
plugin.SetEnabled(true, isAutoEnable);
pluginImplementation.SetEnabled(true, isAutoEnable);
}
catch (Exception e)
{
_logger.Warning(new ArtemisPluginException(plugin.PluginInfo, "Exception during SetEnabled(true)", e), "Failed to enable plugin");
_logger.Warning(new ArtemisPluginException(pluginImplementation.PluginInfo, "Exception during SetEnabled(true)", e), "Failed to enable plugin");
throw;
}
finally
@@ -296,48 +296,48 @@ namespace Artemis.Core.Services
// 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;
pluginImplementation.PluginInfo.IsEnabled = true;
plugin.PluginInfo.ApplyToEntity();
_pluginRepository.SavePlugin(plugin.PluginInfo.PluginEntity);
pluginImplementation.PluginInfo.ApplyToEntity();
_pluginRepository.SavePlugin(pluginImplementation.PluginInfo.PluginEntity);
if (plugin.PluginInfo.Enabled)
_logger.Debug("Successfully enabled plugin {pluginInfo}", plugin.PluginInfo);
if (pluginImplementation.PluginInfo.IsEnabled)
_logger.Debug("Successfully enabled plugin {pluginInfo}", pluginImplementation.PluginInfo);
}
}
OnPluginEnabled(new PluginEventArgs(plugin.PluginInfo));
OnPluginEnabled(new PluginEventArgs(pluginImplementation.PluginInfo));
}
public void DisablePlugin(Plugin plugin)
public void DisablePlugin(PluginImplementation pluginImplementation)
{
lock (_plugins)
{
_logger.Debug("Disabling plugin {pluginInfo}", plugin.PluginInfo);
_logger.Debug("Disabling plugin {pluginInfo}", pluginImplementation.PluginInfo);
// Device providers cannot be disabled at runtime simply queue a disable for next restart
if (plugin is DeviceProvider)
if (pluginImplementation is DeviceProvider)
{
// Don't call SetEnabled(false) but simply update enabled state and save it
plugin.PluginInfo.Enabled = false;
plugin.PluginInfo.ApplyToEntity();
_pluginRepository.SavePlugin(plugin.PluginInfo.PluginEntity);
pluginImplementation.PluginInfo.IsEnabled = false;
pluginImplementation.PluginInfo.ApplyToEntity();
_pluginRepository.SavePlugin(pluginImplementation.PluginInfo.PluginEntity);
return;
}
plugin.SetEnabled(false);
plugin.PluginInfo.ApplyToEntity();
_pluginRepository.SavePlugin(plugin.PluginInfo.PluginEntity);
pluginImplementation.SetEnabled(false);
pluginImplementation.PluginInfo.ApplyToEntity();
_pluginRepository.SavePlugin(pluginImplementation.PluginInfo.PluginEntity);
_logger.Debug("Successfully disabled plugin {pluginInfo}", plugin.PluginInfo);
_logger.Debug("Successfully disabled plugin {pluginInfo}", pluginImplementation.PluginInfo);
}
OnPluginDisabled(new PluginEventArgs(plugin.PluginInfo));
OnPluginDisabled(new PluginEventArgs(pluginImplementation.PluginInfo));
}
public PluginInfo GetPluginInfo(Plugin plugin)
public PluginInfo GetPluginInfo(PluginImplementation pluginImplementation)
{
return _plugins.FirstOrDefault(p => p.Instance == plugin);
return _plugins.FirstOrDefault(p => p.Instance == pluginImplementation);
}
public List<PluginInfo> GetAllPluginInfo()
@@ -345,22 +345,22 @@ namespace Artemis.Core.Services
return new List<PluginInfo>(_plugins);
}
public List<T> GetPluginsOfType<T>() where T : Plugin
public List<T> GetPluginsOfType<T>() where T : PluginImplementation
{
return _plugins.Where(p => p.Enabled && p.Instance is T).Select(p => (T) p.Instance).ToList();
return _plugins.Where(p => p.IsEnabled && p.Instance is T).Select(p => (T) p.Instance).ToList();
}
public Plugin GetPluginByAssembly(Assembly assembly)
public PluginImplementation GetPluginByAssembly(Assembly assembly)
{
return _plugins.FirstOrDefault(p => p.Assembly == assembly)?.Instance;
}
public Plugin GetPluginByDevice(IRGBDevice rgbDevice)
public PluginImplementation GetPluginByDevice(IRGBDevice rgbDevice)
{
return GetPluginsOfType<DeviceProvider>().First(d => d.RgbDeviceProvider.Devices != null && d.RgbDeviceProvider.Devices.Contains(rgbDevice));
}
public Plugin GetCallingPlugin()
public PluginImplementation GetCallingPlugin()
{
StackTrace stackTrace = new StackTrace(); // get call stack
StackFrame[] stackFrames = stackTrace.GetFrames(); // get method calls (frames)
@@ -368,9 +368,9 @@ namespace Artemis.Core.Services
foreach (StackFrame stackFrame in stackFrames)
{
Assembly assembly = stackFrame.GetMethod().DeclaringType.Assembly;
Plugin plugin = GetPluginByAssembly(assembly);
if (plugin != null)
return plugin;
PluginImplementation pluginImplementation = GetPluginByAssembly(assembly);
if (pluginImplementation != null)
return pluginImplementation;
}
return null;
@@ -11,9 +11,9 @@ namespace Artemis.Core.Services
public DataModelService(IPluginService pluginService)
{
// Add data models of already loaded plugins
foreach (Module module in pluginService.GetPluginsOfType<Module>().Where(p => p.Enabled))
foreach (Module module in pluginService.GetPluginsOfType<Module>().Where(p => p.IsEnabled))
AddModuleDataModel(module);
foreach (BaseDataModelExpansion dataModelExpansion in pluginService.GetPluginsOfType<BaseDataModelExpansion>().Where(p => p.Enabled))
foreach (BaseDataModelExpansion dataModelExpansion in pluginService.GetPluginsOfType<BaseDataModelExpansion>().Where(p => p.IsEnabled))
AddDataModelExpansionDataModel(dataModelExpansion);
// Add data models of new plugins when they get enabled
@@ -44,9 +44,9 @@ namespace Artemis.Core.Services
return (T) DataModelStore.GetAll().FirstOrDefault(d => d.DataModel is T)?.DataModel;
}
public DataModel GetPluginDataModel(Plugin plugin)
public DataModel GetPluginDataModel(PluginImplementation pluginImplementation)
{
return DataModelStore.Get(plugin.PluginInfo.Guid)?.DataModel;
return DataModelStore.Get(pluginImplementation.PluginInfo.Guid)?.DataModel;
}
public DataModel GetPluginDataModel(Guid pluginGuid)
@@ -34,8 +34,8 @@ namespace Artemis.Core.Services
/// <summary>
/// If found, returns the data model of the provided plugin
/// </summary>
/// <param name="plugin">The plugin to find the data model of</param>
DataModel GetPluginDataModel(Plugin plugin);
/// <param name="pluginImplementation">The plugin to find the data model of</param>
DataModel GetPluginDataModel(PluginImplementation pluginImplementation);
/// <summary>
/// If found, returns the data model of the provided plugin GUID
@@ -44,8 +44,8 @@ namespace Artemis.Core.Services
// Add all current devices
foreach (IRGBDevice rgbDevice in _rgbService.LoadedDevices)
{
Plugin plugin = _pluginService.GetPluginByDevice(rgbDevice);
configuration.Devices.Add(new ArtemisDevice(rgbDevice, plugin, configuration));
PluginImplementation pluginImplementation = _pluginService.GetPluginByDevice(rgbDevice);
configuration.Devices.Add(new ArtemisDevice(rgbDevice, pluginImplementation, configuration));
}
lock (_surfaceConfigurations)
@@ -136,8 +136,8 @@ namespace Artemis.Core.Services
IRGBDevice device = _rgbService.Surface.Devices.FirstOrDefault(d => d.GetDeviceIdentifier() == position.DeviceIdentifier);
if (device != null)
{
Plugin plugin = _pluginService.GetPluginByDevice(device);
surfaceConfiguration.Devices.Add(new ArtemisDevice(device, plugin, surfaceConfiguration, position));
PluginImplementation pluginImplementation = _pluginService.GetPluginByDevice(device);
surfaceConfiguration.Devices.Add(new ArtemisDevice(device, pluginImplementation, surfaceConfiguration, position));
}
}
@@ -178,8 +178,8 @@ namespace Artemis.Core.Services
DeviceEntity existingDeviceConfig = surface.SurfaceEntity.DeviceEntities.FirstOrDefault(d => d.DeviceIdentifier == deviceIdentifier);
if (existingDeviceConfig != null)
{
Plugin plugin = _pluginService.GetPluginByDevice(rgbDevice);
device = new ArtemisDevice(rgbDevice, plugin, surface, existingDeviceConfig);
PluginImplementation pluginImplementation = _pluginService.GetPluginByDevice(rgbDevice);
device = new ArtemisDevice(rgbDevice, pluginImplementation, surface, existingDeviceConfig);
}
// Fall back on creating a new device
else
@@ -189,8 +189,8 @@ namespace Artemis.Core.Services
rgbDevice.DeviceInfo,
deviceIdentifier
);
Plugin plugin = _pluginService.GetPluginByDevice(rgbDevice);
device = new ArtemisDevice(rgbDevice, plugin, surface);
PluginImplementation pluginImplementation = _pluginService.GetPluginByDevice(rgbDevice);
device = new ArtemisDevice(rgbDevice, pluginImplementation, surface);
}
surface.Devices.Add(device);