1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Added built-in plugin copying

This commit is contained in:
Robert Beekman 2019-08-21 12:12:50 +02:00
parent cad98ae850
commit 254a8e311f
9 changed files with 95 additions and 11 deletions

View File

@ -113,6 +113,7 @@
<Compile Include="Constants.cs" />
<Compile Include="Events\DeviceEventArgs.cs" />
<Compile Include="Exceptions\ArtemisCoreException.cs" />
<Compile Include="Extensions\DirectoryInfoExtensions.cs" />
<Compile Include="Extensions\RgbColorExtensions.cs" />
<Compile Include="Extensions\RgbRectangleExtensions.cs" />
<Compile Include="Models\DataModelDescription.cs" />

View File

@ -0,0 +1,15 @@
using System.IO;
namespace Artemis.Core.Extensions
{
public static class DirectoryInfoExtensions
{
public static void CopyFilesRecursively(this DirectoryInfo source, DirectoryInfo target)
{
foreach (var dir in source.GetDirectories())
CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
foreach (var file in source.GetFiles())
file.CopyTo(Path.Combine(target.FullName, file.Name));
}
}
}

View File

@ -39,6 +39,7 @@ namespace Artemis.Core.Services
throw new ArtemisCoreException("Cannot initialize the core as it is already initialized.");
// Initialize the services
await Task.Run(() => _pluginService.CopyBuiltInPlugins());
await Task.Run(() => _pluginService.LoadPlugins());
OnInitialized();

View File

@ -13,6 +13,12 @@ namespace Artemis.Core.Services.Interfaces
/// </summary>
bool LoadingPlugins { get; }
/// <summary>
/// Copy built-in plugins from the executable directory to the plugins directory if the version is higher
/// (higher or equal if compiled as debug)
/// </summary>
void CopyBuiltInPlugins();
/// <summary>
/// Loads all installed plugins. If plugins already loaded this will reload them all
/// </summary>

View File

@ -5,6 +5,7 @@ using System.Linq;
using AppDomainToolkit;
using Artemis.Core.Events;
using Artemis.Core.Exceptions;
using Artemis.Core.Extensions;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.Exceptions;
using Artemis.Core.Plugins.Models;
@ -30,6 +31,7 @@ namespace Artemis.Core.Services
_kernel = kernel;
_plugins = new List<PluginInfo>();
// Ensure the plugins directory exists
if (!Directory.Exists(Constants.DataFolder + "plugins"))
Directory.CreateDirectory(Constants.DataFolder + "plugins");
}
@ -37,6 +39,54 @@ namespace Artemis.Core.Services
/// <inheritdoc />
public bool LoadingPlugins { get; private set; }
/// <inheritdoc />
public void CopyBuiltInPlugins()
{
var pluginDirectory = new DirectoryInfo(Path.Combine(Constants.DataFolder, "plugins"));
// Iterate built-in plugins
var varBuiltInPluginDirectory = new DirectoryInfo(Path.Combine(Directory.GetCurrentDirectory(), "Plugins"));
foreach (var subDirectory in varBuiltInPluginDirectory.EnumerateDirectories())
{
// Load the metadata
var builtInMetadataFile = Path.Combine(subDirectory.FullName, "plugin.json");
if (!File.Exists(builtInMetadataFile))
throw new ArtemisPluginException("Couldn't find the built-in plugins metadata file at " + builtInMetadataFile);
var builtInPluginInfo = JsonConvert.DeserializeObject<PluginInfo>(File.ReadAllText(builtInMetadataFile));
// Find the matching plugin in the plugin folder
var match = pluginDirectory.EnumerateDirectories().FirstOrDefault(d => d.Name == subDirectory.Name);
if (match == null)
CopyBuiltInPlugin(subDirectory);
else
{
var metadataFile = Path.Combine(match.FullName, "plugin.json");
if (!File.Exists(metadataFile))
CopyBuiltInPlugin(subDirectory);
else
{
try
{
// Compare versions, copy if the same when debugging
var pluginInfo = JsonConvert.DeserializeObject<PluginInfo>(File.ReadAllText(builtInMetadataFile));
#if DEBUG
if (builtInPluginInfo.Version >= pluginInfo.Version)
CopyBuiltInPlugin(subDirectory);
#else
if (builtInPluginInfo.Version > pluginInfo.Version)
CopyBuiltInPlugin(subDirectory);
#endif
}
catch (Exception e)
{
throw new ArtemisPluginException("Failed read plugin metadata needed to install built-in plugin", e);
}
}
}
}
}
/// <inheritdoc />
public void LoadPlugins()
{
@ -54,8 +104,8 @@ namespace Artemis.Core.Services
_childKernel = new ChildKernel(_kernel);
// Load the plugin assemblies into the plugin context
var directory = new DirectoryInfo(Path.Combine(Constants.DataFolder, "plugins"));
foreach (var subDirectory in directory.EnumerateDirectories())
var pluginDirectory = new DirectoryInfo(Path.Combine(Constants.DataFolder, "plugins"));
foreach (var subDirectory in pluginDirectory.EnumerateDirectories())
{
try
{
@ -236,6 +286,17 @@ namespace Artemis.Core.Services
UnloadPlugins();
}
private static void CopyBuiltInPlugin(DirectoryInfo builtInPluginDirectory)
{
var pluginDirectory = new DirectoryInfo(Path.Combine(Constants.DataFolder, "plugins", builtInPluginDirectory.Name));
// Remove the old directory if it exists
if (Directory.Exists(pluginDirectory.FullName))
Directory.Delete(pluginDirectory.FullName, true);
Directory.CreateDirectory(pluginDirectory.FullName);
builtInPluginDirectory.CopyFilesRecursively(pluginDirectory);
}
#region Events

View File

@ -73,7 +73,7 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>(robocopy $(TargetDir) %25ProgramData%25\Artemis\plugins\$(ProjectName) /E /NFL /NDL /NJH /NJS /nc /ns /np) ^&amp; IF %25ERRORLEVEL%25 LEQ 4 exit /B 0</PostBuildEvent>
<PostBuildEvent>(robocopy $(TargetDir) $(SolutionDir)\Artemis.UI\$(OutDir)\Plugins\$(ProjectName) /E /NFL /NDL /NJH /NJS /nc /ns /np) ^&amp; IF %25ERRORLEVEL%25 LEQ 4 exit /B 0</PostBuildEvent>
</PropertyGroup>
<Import Project="..\packages\RGB.NET.Resources.Corsair.0.3.0.234\build\RGB.NET.Resources.Corsair.targets" Condition="Exists('..\packages\RGB.NET.Resources.Corsair.0.3.0.234\build\RGB.NET.Resources.Corsair.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">

View File

@ -86,6 +86,6 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>(robocopy $(TargetDir) %25ProgramData%25\Artemis\plugins\$(ProjectName) /E /NFL /NDL /NJH /NJS /nc /ns /np) ^&amp; IF %25ERRORLEVEL%25 LEQ 4 exit /B 0</PostBuildEvent>
<PostBuildEvent>(robocopy $(TargetDir) $(SolutionDir)\Artemis.UI\$(OutDir)\Plugins\$(ProjectName) /E /NFL /NDL /NJH /NJS /nc /ns /np) ^&amp; IF %25ERRORLEVEL%25 LEQ 4 exit /B 0</PostBuildEvent>
</PropertyGroup>
</Project>

View File

@ -95,6 +95,6 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>(robocopy $(TargetDir) %25ProgramData%25\Artemis\plugins\$(ProjectName) /E /NFL /NDL /NJH /NJS /nc /ns /np) ^&amp; IF %25ERRORLEVEL%25 LEQ 4 exit /B 0</PostBuildEvent>
<PostBuildEvent>(robocopy $(TargetDir) $(SolutionDir)\Artemis.UI\$(OutDir)\Plugins\$(ProjectName) /E /NFL /NDL /NJH /NJS /nc /ns /np) ^&amp; IF %25ERRORLEVEL%25 LEQ 4 exit /B 0</PostBuildEvent>
</PropertyGroup>
</Project>

View File

@ -1,5 +1,5 @@
using System.IO;
using System.Reflection;
using System;
using System.IO;
using Artemis.Storage.Entities;
using Microsoft.EntityFrameworkCore;
using SQLitePCL;
@ -14,12 +14,12 @@ namespace Artemis.Storage
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// var dbLocation = @"C:\Repos\Artemis\src\Artemis.Storage\Storage.db";
// ReSharper disable once RedundantAssignment - Used if not debugging
var dbLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Artemis\\Storage.db");
#if DEBUG
var dbLocation = Path.GetFullPath(Path.Combine(Assembly.GetEntryAssembly().Location, @"..\..\..\..\Artemis.Storage\Storage.db"));
#else
var dbLocation = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Artemis\\Storage.db";
dbLocation = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), @"..\..\..\..\Artemis.Storage\Storage.db"));
#endif
optionsBuilder.UseSqlite("Data Source=" + dbLocation);
// Requires Microsoft.Data.Sqlite in the startup project