Profiles - Fixed conditions triggering on profile load, closes #520

UI - Added logging startup argument, closes #532
Plugins - Added import button, closes #521
This commit is contained in:
SpoinkyNL committed 2021-02-03 19:21:04 +01:00
1 parent 1c10eb8aa4
commit f343b3d0f8
10 files changed
+134 -14

No files matched your search

+22 -2
View File
@@ -168,8 +168,28 @@ namespace Artemis.Core.Services
private void ApplyLoggingLevel()
{
_logger.Information("Setting logging level to {loggingLevel}", _loggingLevel.Value);
LoggerProvider.LoggingLevelSwitch.MinimumLevel = _loggingLevel.Value;
string? argument = StartupArguments.FirstOrDefault(a => a.StartsWith("--logging"));
if (argument != null)
{
// Parse the provided log level
string[] parts = argument.Split('=');
if (parts.Length == 2 && Enum.TryParse(typeof(LogEventLevel), parts[1], true, out object? logLevelArgument))
{
_logger.Information("Setting logging level to {loggingLevel} from startup argument", (LogEventLevel) logLevelArgument!);
LoggerProvider.LoggingLevelSwitch.MinimumLevel = (LogEventLevel) logLevelArgument;
}
else
{
_logger.Warning("Failed to set log level from startup argument {argument}", argument);
_logger.Information("Setting logging level to {loggingLevel}", _loggingLevel.Value);
LoggerProvider.LoggingLevelSwitch.MinimumLevel = _loggingLevel.Value;
}
}
else
{
_logger.Information("Setting logging level to {loggingLevel}", _loggingLevel.Value);
LoggerProvider.LoggingLevelSwitch.MinimumLevel = _loggingLevel.Value;
}
}
private void SurfaceOnUpdating(UpdatingEventArgs args)
@@ -63,6 +63,13 @@ namespace Artemis.Core.Services
/// <param name="saveState">Whether or not to save the new enabled state</param>
void DisablePlugin(Plugin plugin, bool saveState);
/// <summary>
/// Imports the plugin contained in the provided ZIP file
/// </summary>
/// <param name="fileName">The full path to the ZIP file that contains the plugin</param>
/// <returns>The resulting plugin</returns>
Plugin ImportPlugin(string fileName);
/// <summary>
/// Enables the provided plugin feature
/// </summary>
@@ -273,7 +273,7 @@ namespace Artemis.Core.Services
FileInfo[] fileInfos = directory.GetFiles();
if (!fileInfos.Any(f => string.Equals(f.Name, plugin.Info.Main, StringComparison.InvariantCulture)))
throw new ArtemisPluginException(plugin, "Plugin main entry casing mismatch at " + plugin.Info.Main);
// Load the plugin, all types implementing Plugin and register them with DI
plugin.PluginLoader = PluginLoader.CreateFromAssemblyFile(mainFile!, configure =>
{
@@ -437,6 +437,55 @@ namespace Artemis.Core.Services
OnPluginDisabled(new PluginEventArgs(plugin));
}
/// <inheritdoc />
public Plugin ImportPlugin(string fileName)
{
DirectoryInfo pluginDirectory = new(Path.Combine(Constants.DataFolder, "plugins"));
// Find the metadata file in the zip
using ZipArchive archive = ZipFile.OpenRead(fileName);
ZipArchiveEntry? metaDataFileEntry = archive.Entries.FirstOrDefault(e => e.Name == "plugin.json");
if (metaDataFileEntry == null)
throw new ArtemisPluginException("Couldn't find a plugin.json in " + fileName);
using StreamReader reader = new(metaDataFileEntry.Open());
PluginInfo pluginInfo = CoreJson.DeserializeObject<PluginInfo>(reader.ReadToEnd())!;
if (!pluginInfo.Main.EndsWith(".dll"))
throw new ArtemisPluginException("Main entry in plugin.json must point to a .dll file" + fileName);
Plugin? existing = _plugins.FirstOrDefault(p => p.Guid == pluginInfo.Guid);
if (existing != null)
throw new ArtemisPluginException($"A plugin with the same GUID is already loaded: {existing.Info}");
string targetDirectory = pluginInfo.Main.Split(".dll")[0].Replace("/", "").Replace("\\", "");
string uniqueTargetDirectory = targetDirectory;
int attempt = 2;
// Find a unique folder
while (pluginDirectory.EnumerateDirectories().Any(d => d.Name == uniqueTargetDirectory))
{
uniqueTargetDirectory = targetDirectory + "-" + attempt;
attempt++;
}
// Extract everything in the same archive directory to the unique plugin directory
DirectoryInfo directoryInfo = new(Path.Combine(pluginDirectory.FullName, uniqueTargetDirectory));
Directory.CreateDirectory(directoryInfo.FullName);
string metaDataDirectory = metaDataFileEntry.FullName.Replace(metaDataFileEntry.Name, "");
foreach (ZipArchiveEntry zipArchiveEntry in archive.Entries)
{
if (zipArchiveEntry.FullName.StartsWith(metaDataDirectory))
{
string target = Path.Combine(directoryInfo.FullName, zipArchiveEntry.FullName.Remove(0, metaDataDirectory.Length));
zipArchiveEntry.ExtractToFile(target);
}
}
// Load the newly extracted plugin and return the result
return LoadPlugin(directoryInfo);
}
#endregion
#region Features