mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-11 19:36:28 +00:00
Added some module architecture
This commit is contained in:
1 parent
ab3f2b1fd8
commit
f093520ce8
25 files changed
+1732
-59
No files matched your search
@@ -1,8 +1,30 @@
|
||||
using Artemis.Core.Services.Interfaces;
|
||||
using System.Threading.Tasks;
|
||||
using Artemis.Core.Services.Interfaces;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
{
|
||||
public class CoreService : ICoreService
|
||||
{
|
||||
private readonly IModuleService _moduleService;
|
||||
|
||||
public CoreService(IModuleService moduleService)
|
||||
{
|
||||
_moduleService = moduleService;
|
||||
Task.Run(Initialize);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_moduleService.Dispose();
|
||||
}
|
||||
|
||||
public bool IsInitialized { get; set; }
|
||||
|
||||
private async Task Initialize()
|
||||
{
|
||||
await _moduleService.LoadModules();
|
||||
|
||||
IsInitialized = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
namespace Artemis.Core.Services.Interfaces
|
||||
using System;
|
||||
|
||||
namespace Artemis.Core.Services.Interfaces
|
||||
{
|
||||
public interface ICoreService
|
||||
public interface ICoreService: IArtemisService, IDisposable
|
||||
{
|
||||
bool IsInitialized { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,16 @@
|
||||
namespace Artemis.Core.Services.Interfaces
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Artemis.Core.Events;
|
||||
using Artemis.Core.Modules.Interfaces;
|
||||
|
||||
namespace Artemis.Core.Services.Interfaces
|
||||
{
|
||||
public interface IModuleService
|
||||
public interface IModuleService : IArtemisService, IDisposable
|
||||
{
|
||||
Task LoadModules();
|
||||
Task ReloadModule(IModule module);
|
||||
|
||||
event EventHandler<ModuleEventArgs> ModuleLoaded;
|
||||
event EventHandler<ModuleEventArgs> ModuleReloaded;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,120 @@
|
||||
using Artemis.Core.Services.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Artemis.Core.Events;
|
||||
using Artemis.Core.Exceptions;
|
||||
using Artemis.Core.Models;
|
||||
using Artemis.Core.Modules.Interfaces;
|
||||
using Artemis.Core.Services.Interfaces;
|
||||
using CSScriptLibrary;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
{
|
||||
public class ModuleService : IModuleService
|
||||
{
|
||||
private readonly List<IModule> _modules;
|
||||
|
||||
public ModuleService()
|
||||
{
|
||||
_modules = new List<IModule>();
|
||||
|
||||
if (!Directory.Exists(Constants.DataFolder + "modules"))
|
||||
Directory.CreateDirectory(Constants.DataFolder + "modules");
|
||||
}
|
||||
|
||||
public bool LoadingModules { get; private set; }
|
||||
public ReadOnlyCollection<IModule> Modules => _modules.AsReadOnly();
|
||||
|
||||
/// <summary>
|
||||
/// Loads all installed modules. If modules already loaded this will reload them all
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task LoadModules()
|
||||
{
|
||||
if (LoadingModules)
|
||||
throw new ArtemisCoreException("Cannot load modules while a previous load hasn't been completed yet.");
|
||||
|
||||
OnStartedLoadingModules();
|
||||
|
||||
// Empty the list of modules
|
||||
_modules.Clear();
|
||||
// Iterate all module folders
|
||||
foreach (var directory in Directory.GetDirectories(Constants.DataFolder + "modules"))
|
||||
// Load each module
|
||||
_modules.Add(await LoadModuleFromFolder(directory));
|
||||
|
||||
|
||||
OnFinishedLoadedModules();
|
||||
}
|
||||
|
||||
public async Task ReloadModule(IModule module)
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
private async Task<IModule> LoadModuleFromFolder(string folder)
|
||||
{
|
||||
if (!folder.EndsWith("\\"))
|
||||
folder += "\\";
|
||||
if (!File.Exists(folder + "module.json"))
|
||||
throw new ArtemisModuleException(null, "Failed to load module, no module.json found in " + folder);
|
||||
|
||||
var moduleInfo = JsonConvert.DeserializeObject<ModuleInfo>(File.ReadAllText(folder + "module.json"));
|
||||
// Load the main module which will contain a class implementing IModule
|
||||
var module = await CSScript.Evaluator.LoadFileAsync<IModule>(folder + moduleInfo.MainFile);
|
||||
return module;
|
||||
}
|
||||
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when a single module has loaded
|
||||
/// </summary>
|
||||
public event EventHandler<ModuleEventArgs> ModuleLoaded;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when a single module has reloaded
|
||||
/// </summary>
|
||||
public event EventHandler<ModuleEventArgs> ModuleReloaded;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when loading all modules has started
|
||||
/// </summary>
|
||||
public event EventHandler StartedLoadingModules;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when loading all modules has finished
|
||||
/// </summary>
|
||||
public event EventHandler FinishedLoadedModules;
|
||||
|
||||
private void OnModuleLoaded(ModuleEventArgs e)
|
||||
{
|
||||
ModuleLoaded?.Invoke(this, e);
|
||||
}
|
||||
|
||||
private void OnModuleReloaded(ModuleEventArgs e)
|
||||
{
|
||||
ModuleReloaded?.Invoke(this, e);
|
||||
}
|
||||
|
||||
private void OnStartedLoadingModules()
|
||||
{
|
||||
LoadingModules = true;
|
||||
StartedLoadingModules?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
private void OnFinishedLoadedModules()
|
||||
{
|
||||
LoadingModules = false;
|
||||
FinishedLoadedModules?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user