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

Some more plugin stuff, lovely commit messages ikr

This commit is contained in:
SpoinkyNL 2018-01-08 19:10:59 +01:00
parent c761c880ed
commit 8760b7f838
16 changed files with 216 additions and 108 deletions

View File

@ -183,8 +183,8 @@
<ItemGroup> <ItemGroup>
<Compile Include="Constants.cs" /> <Compile Include="Constants.cs" />
<Compile Include="Exceptions\ArtemisCoreException.cs" /> <Compile Include="Exceptions\ArtemisCoreException.cs" />
<Compile Include="Exceptions\ArtemisModuleException.cs" /> <Compile Include="Exceptions\ArtemisPluginException.cs" />
<Compile Include="Models\ModuleInfo.cs" /> <Compile Include="Models\PluginInfo.cs" />
<Compile Include="Ninject\CoreModule.cs" /> <Compile Include="Ninject\CoreModule.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scripting.evaluator.cs" /> <Compile Include="Scripting.evaluator.cs" />
@ -194,7 +194,7 @@
<Compile Include="Services\Interfaces\IArtemisService.cs" /> <Compile Include="Services\Interfaces\IArtemisService.cs" />
<Compile Include="Services\Interfaces\ICoreService.cs" /> <Compile Include="Services\Interfaces\ICoreService.cs" />
<Compile Include="Services\Interfaces\IPluginService.cs" /> <Compile Include="Services\Interfaces\IPluginService.cs" />
<Compile Include="Events\ModuleEventArgs.cs" /> <Compile Include="Events\PluginEventArgs.cs" />
<Compile Include="Services\PluginService.cs" /> <Compile Include="Services\PluginService.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,9 +0,0 @@
using Artemis.Core.Plugins.Interfaces;
namespace Artemis.Core.Events
{
public class ModuleEventArgs : System.EventArgs
{
public IPlugin Plugin { get; set; }
}
}

View File

@ -0,0 +1,10 @@
using System;
using Artemis.Plugins.Interfaces;
namespace Artemis.Core.Events
{
public class PluginEventArgs : EventArgs
{
public IPlugin Plugin { get; set; }
}
}

View File

@ -1,25 +0,0 @@
using System;
using Artemis.Core.Models;
namespace Artemis.Core.Exceptions
{
public class ArtemisModuleException : Exception
{
public ArtemisModuleException(ModuleInfo moduleInfo)
{
ModuleInfo = moduleInfo;
}
public ArtemisModuleException(ModuleInfo moduleInfo, string message) : base(message)
{
ModuleInfo = moduleInfo;
}
public ArtemisModuleException(ModuleInfo moduleInfo, string message, Exception inner) : base(message, inner)
{
ModuleInfo = moduleInfo;
}
public ModuleInfo ModuleInfo { get; }
}
}

View File

@ -0,0 +1,25 @@
using System;
using Artemis.Core.Models;
namespace Artemis.Core.Exceptions
{
public class ArtemisPluginException : Exception
{
public ArtemisPluginException(PluginInfo pluginInfo)
{
PluginInfo = pluginInfo;
}
public ArtemisPluginException(PluginInfo pluginInfo, string message) : base(message)
{
PluginInfo = pluginInfo;
}
public ArtemisPluginException(PluginInfo pluginInfo, string message, Exception inner) : base(message, inner)
{
PluginInfo = pluginInfo;
}
public PluginInfo PluginInfo { get; }
}
}

View File

@ -1,17 +0,0 @@
using System.Collections.Generic;
using Artemis.Core.Plugins.Interfaces;
using Newtonsoft.Json;
namespace Artemis.Core.Models
{
public class ModuleInfo
{
public string Name { get; set; }
public string Version { get; set; }
public string MainFile { get; set; }
public IReadOnlyList<string> SubFiles { get; set; }
[JsonIgnore]
public IPlugin Plugin { get; set; }
}
}

View File

@ -0,0 +1,40 @@
using Artemis.Plugins.Interfaces;
using Newtonsoft.Json;
namespace Artemis.Core.Models
{
public class PluginInfo
{
/// <summary>
/// The name of the plugin
/// </summary>
public string Name { get; set; }
/// <summary>
/// The version of the plugin
/// </summary>
public string Version { get; set; }
/// <summary>
/// The file implementing IPlugin, loaded on startup
/// </summary>
public string Main { get; set; }
/// <summary>
/// The file implementing IPluginViewModel, loaded when opened in the UI
/// </summary>
public string ViewModel { get; set; }
/// <summary>
/// The instantiated plugin, available after successful load
/// </summary>
[JsonIgnore]
public IPlugin Plugin { get; set; }
/// <summary>
/// Full path to the plugin's current folder
/// </summary>
[JsonIgnore]
public string Folder { get; set; }
}
}

View File

@ -22,7 +22,7 @@ namespace Artemis.Core.Services
private async Task Initialize() private async Task Initialize()
{ {
await _pluginService.LoadModules(); await _pluginService.LoadPlugins();
IsInitialized = true; IsInitialized = true;
} }

View File

@ -1,16 +1,44 @@
using System; using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks; using System.Threading.Tasks;
using Artemis.Core.Events; using Artemis.Core.Events;
using Artemis.Core.Models;
using Artemis.Plugins.Interfaces; using Artemis.Plugins.Interfaces;
namespace Artemis.Core.Services.Interfaces namespace Artemis.Core.Services.Interfaces
{ {
public interface IPluginService : IArtemisService, IDisposable public interface IPluginService : IArtemisService, IDisposable
{ {
Task LoadModules(); bool LoadingPlugins { get; }
Task ReloadModule(IPlugin plugin); ReadOnlyCollection<PluginInfo> Plugins { get; }
event EventHandler<ModuleEventArgs> ModuleLoaded; /// <summary>
event EventHandler<ModuleEventArgs> ModuleReloaded; /// Loads all installed plugins. If plugins already loaded this will reload them all
/// </summary>
/// <returns></returns>
Task LoadPlugins();
Task ReloadPlugin(PluginInfo pluginInfo);
Task<IPluginViewModel> GetPluginViewModel(PluginInfo pluginInfo);
/// <summary>
/// Occurs when a single plugin has loaded
/// </summary>
event EventHandler<PluginEventArgs> PluginLoaded;
/// <summary>
/// Occurs when a single plugin has reloaded
/// </summary>
event EventHandler<PluginEventArgs> PluginReloaded;
/// <summary>
/// Occurs when loading all plugins has started
/// </summary>
event EventHandler StartedLoadingPlugins;
/// <summary>
/// Occurs when loading all plugins has finished
/// </summary>
event EventHandler FinishedLoadedPlugins;
} }
} }

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.IO; using System.IO;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Artemis.Core.Events; using Artemis.Core.Events;
using Artemis.Core.Exceptions; using Artemis.Core.Exceptions;
@ -10,109 +11,127 @@ using Artemis.Core.Services.Interfaces;
using Artemis.Plugins.Interfaces; using Artemis.Plugins.Interfaces;
using CSScriptLibrary; using CSScriptLibrary;
using Newtonsoft.Json; using Newtonsoft.Json;
using Ninject;
namespace Artemis.Core.Services namespace Artemis.Core.Services
{ {
public class PluginService : IPluginService public class PluginService : IPluginService
{ {
private readonly List<IPlugin> _modules; private readonly IKernel _kernel;
private readonly List<PluginInfo> _plugins;
public PluginService() public PluginService(IKernel kernel)
{ {
_modules = new List<IPlugin>(); _kernel = kernel;
_plugins = new List<PluginInfo>();
if (!Directory.Exists(Constants.DataFolder + "modules")) if (!Directory.Exists(Constants.DataFolder + "plugins"))
Directory.CreateDirectory(Constants.DataFolder + "modules"); Directory.CreateDirectory(Constants.DataFolder + "plugins");
} }
public bool LoadingModules { get; private set; } public bool LoadingPlugins { get; private set; }
public ReadOnlyCollection<IPlugin> Modules => _modules.AsReadOnly(); public ReadOnlyCollection<PluginInfo> Plugins => _plugins.AsReadOnly();
/// <summary> /// <summary>
/// Loads all installed modules. If modules already loaded this will reload them all /// Loads all installed plugins. If plugins already loaded this will reload them all
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public async Task LoadModules() public async Task LoadPlugins()
{ {
if (LoadingModules) if (LoadingPlugins)
throw new ArtemisCoreException("Cannot load modules while a previous load hasn't been completed yet."); throw new ArtemisCoreException("Cannot load plugins while a previous load hasn't been completed yet.");
OnStartedLoadingModules(); OnStartedLoadingPlugins();
// Empty the list of modules // Empty the list of plugins
_modules.Clear(); _plugins.Clear();
// Iterate all module folders
foreach (var directory in Directory.GetDirectories(Constants.DataFolder + "modules"))
// Load each module
_modules.Add(await LoadModuleFromFolder(directory));
// Iterate all plugin folders and load each plugin
OnFinishedLoadedModules(); foreach (var directory in Directory.GetDirectories(Constants.DataFolder + "plugins"))
_plugins.Add(await LoadPluginFromFolder(directory));
OnFinishedLoadedPlugins();
} }
public async Task ReloadModule(IPlugin plugin) public async Task ReloadPlugin(PluginInfo pluginInfo)
{ {
} }
public async Task<IPluginViewModel> GetPluginViewModel(PluginInfo pluginInfo)
{
// Compile the ViewModel and get the type
var compile = await Task.Run(() => CSScript.LoadFile(pluginInfo.Folder + pluginInfo.ViewModel));
var vmType = compile.ExportedTypes.FirstOrDefault(t => typeof(IPluginViewModel).IsAssignableFrom(t));
if (vmType == null)
throw new ArtemisPluginException(pluginInfo, "Cannot locate a view model for this plugin.");
// Instantiate the ViewModel with Ninject
return (IPluginViewModel) _kernel.Get(vmType);
}
public void Dispose() public void Dispose()
{ {
} }
private async Task<IPlugin> LoadModuleFromFolder(string folder) private async Task<PluginInfo> LoadPluginFromFolder(string folder)
{ {
if (!folder.EndsWith("\\")) if (!folder.EndsWith("\\"))
folder += "\\"; folder += "\\";
if (!File.Exists(folder + "module.json")) if (!File.Exists(folder + "plugin.json"))
throw new ArtemisModuleException(null, "Failed to load module, no module.json found in " + folder); throw new ArtemisPluginException(null, "Failed to load plugin, no plugin.json found in " + folder);
var moduleInfo = JsonConvert.DeserializeObject<ModuleInfo>(File.ReadAllText(folder + "module.json")); var pluginInfo = JsonConvert.DeserializeObject<PluginInfo>(File.ReadAllText(folder + "plugin.json"));
// Load the main module which will contain a class implementing IModule pluginInfo.Folder = folder;
var module = await CSScript.Evaluator.LoadFileAsync<IPlugin>(folder + moduleInfo.MainFile);
return module; // Load the main plugin which will contain a class implementing IPlugin
var plugin = await CSScript.Evaluator.LoadFileAsync<IPlugin>(folder + pluginInfo.Main);
pluginInfo.Plugin = plugin;
return pluginInfo;
} }
#region Events #region Events
/// <summary> /// <summary>
/// Occurs when a single module has loaded /// Occurs when a single plugin has loaded
/// </summary> /// </summary>
public event EventHandler<ModuleEventArgs> ModuleLoaded; public event EventHandler<PluginEventArgs> PluginLoaded;
/// <summary> /// <summary>
/// Occurs when a single module has reloaded /// Occurs when a single plugin has reloaded
/// </summary> /// </summary>
public event EventHandler<ModuleEventArgs> ModuleReloaded; public event EventHandler<PluginEventArgs> PluginReloaded;
/// <summary> /// <summary>
/// Occurs when loading all modules has started /// Occurs when loading all plugins has started
/// </summary> /// </summary>
public event EventHandler StartedLoadingModules; public event EventHandler StartedLoadingPlugins;
/// <summary> /// <summary>
/// Occurs when loading all modules has finished /// Occurs when loading all plugins has finished
/// </summary> /// </summary>
public event EventHandler FinishedLoadedModules; public event EventHandler FinishedLoadedPlugins;
private void OnModuleLoaded(ModuleEventArgs e) private void OnPluginLoaded(PluginEventArgs e)
{ {
ModuleLoaded?.Invoke(this, e); PluginLoaded?.Invoke(this, e);
} }
private void OnModuleReloaded(ModuleEventArgs e) private void OnPluginReloaded(PluginEventArgs e)
{ {
ModuleReloaded?.Invoke(this, e); PluginReloaded?.Invoke(this, e);
} }
private void OnStartedLoadingModules() private void OnStartedLoadingPlugins()
{ {
LoadingModules = true; LoadingPlugins = true;
StartedLoadingModules?.Invoke(this, EventArgs.Empty); StartedLoadingPlugins?.Invoke(this, EventArgs.Empty);
} }
private void OnFinishedLoadedModules() private void OnFinishedLoadedPlugins()
{ {
LoadingModules = false; LoadingPlugins = false;
FinishedLoadedModules?.Invoke(this, EventArgs.Empty); FinishedLoadedPlugins?.Invoke(this, EventArgs.Empty);
} }
#endregion #endregion

View File

@ -30,6 +30,9 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Ninject, Version=3.3.4.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
<HintPath>..\packages\Ninject.3.3.4\lib\net45\Ninject.dll</HintPath>
</Reference>
<Reference Include="Stylet, Version=1.1.21.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="Stylet, Version=1.1.21.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Stylet.1.1.21\lib\net45\Stylet.dll</HintPath> <HintPath>..\packages\Stylet.1.1.21\lib\net45\Stylet.dll</HintPath>
</Reference> </Reference>

View File

@ -2,6 +2,5 @@
{ {
public interface IModule : IPlugin public interface IModule : IPlugin
{ {
IPluginViewModel GetMainViewModel();
} }
} }

View File

@ -1,6 +1,17 @@
namespace Artemis.Plugins.Interfaces using System.Threading.Tasks;
namespace Artemis.Plugins.Interfaces
{ {
public interface IPlugin public interface IPlugin
{ {
/// <summary>
/// Called when the plugin is loaded
/// </summary>
void LoadPlugin();
/// <summary>
/// Called when the plugin is unloaded
/// </summary>
void UnloadPlugin();
} }
} }

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Ninject" version="3.3.4" targetFramework="net46" />
<package id="Stylet" version="1.1.21" targetFramework="net46" /> <package id="Stylet" version="1.1.21" targetFramework="net46" />
</packages> </packages>

View File

@ -151,6 +151,10 @@
<Project>{9b811f9b-86b9-4771-87af-72bae7078a36}</Project> <Project>{9b811f9b-86b9-4771-87af-72bae7078a36}</Project>
<Name>Artemis.Core</Name> <Name>Artemis.Core</Name>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\Artemis.Plugins\Artemis.Plugins.csproj">
<Project>{cd23bc5e-57f0-46ce-a007-24d031146219}</Project>
<Name>Artemis.Plugins</Name>
</ProjectReference>
<ProjectReference Include="..\Artemis.Storage\Artemis.Storage.csproj"> <ProjectReference Include="..\Artemis.Storage\Artemis.Storage.csproj">
<Project>{e489e5e3-1a65-4af5-a1ea-f9805fd19a65}</Project> <Project>{e489e5e3-1a65-4af5-a1ea-f9805fd19a65}</Project>
<Name>Artemis.Storage</Name> <Name>Artemis.Storage</Name>

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using Artemis.Core.Services.Interfaces;
using Artemis.UI.ViewModels.Interfaces; using Artemis.UI.ViewModels.Interfaces;
using Stylet; using Stylet;
@ -7,6 +8,15 @@ namespace Artemis.UI.ViewModels
{ {
public class HomeViewModel : Screen, IHomeViewModel public class HomeViewModel : Screen, IHomeViewModel
{ {
private readonly IPluginService _pluginService;
public HomeViewModel(IPluginService pluginService)
{
_pluginService = pluginService;
_pluginService.FinishedLoadedPlugins += PluginServiceOnFinishedLoadedPlugins;
}
public string Title => "Home"; public string Title => "Home";
public void OpenUrl(string url) public void OpenUrl(string url)
@ -15,5 +25,15 @@ namespace Artemis.UI.ViewModels
if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute)) if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
Process.Start(url); Process.Start(url);
} }
/// <summary>
/// Populates the sidebar with plugins when they are finished loading
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
private void PluginServiceOnFinishedLoadedPlugins(object sender, EventArgs eventArgs)
{
throw new NotImplementedException();
}
} }
} }