Working on module/plugin system
This commit is contained in:
@@ -1,74 +0,0 @@
|
||||
using System.Text.Json;
|
||||
using MoonCore.Helpers;
|
||||
using Moonlight.ApiServer.Models;
|
||||
|
||||
namespace Moonlight.ApiServer.Services;
|
||||
|
||||
public class ModuleService
|
||||
{
|
||||
private readonly ILogger<ModuleService> Logger;
|
||||
private readonly Dictionary<string, ModuleModel> ModuleMeta = new();
|
||||
private static string ModulePath = PathBuilder.Dir("storage", "modules");
|
||||
|
||||
public ModuleModel[] Modules => ModuleMeta.Values.ToArray();
|
||||
|
||||
public ModuleService(ILogger<ModuleService> logger)
|
||||
{
|
||||
Logger = logger;
|
||||
}
|
||||
|
||||
public void Load()
|
||||
{
|
||||
Logger.LogInformation("Loading modules");
|
||||
|
||||
Directory.CreateDirectory(ModulePath);
|
||||
|
||||
foreach (var moduleDirectory in Directory.EnumerateDirectories(ModulePath))
|
||||
{
|
||||
var metaPath = PathBuilder.File(moduleDirectory, "meta.json");
|
||||
|
||||
if (!File.Exists(metaPath))
|
||||
{
|
||||
Logger.LogWarning("No meta.json found in {folder}", moduleDirectory);
|
||||
continue;
|
||||
}
|
||||
|
||||
ModuleModel moduleModel;
|
||||
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(metaPath);
|
||||
moduleModel = JsonSerializer.Deserialize<ModuleModel>(json)!;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.LogError("An error occured while loading meta.json in {folder}: {e}", moduleDirectory, e);
|
||||
continue;
|
||||
}
|
||||
|
||||
ModuleMeta.Add(moduleDirectory, moduleModel);
|
||||
}
|
||||
|
||||
Logger.LogInformation("Loaded {count} modules", ModuleMeta.Count);
|
||||
}
|
||||
|
||||
public string[] GetModuleDlls(string moduleName, string section)
|
||||
{
|
||||
if (ModuleMeta.All(x => x.Value.Name != moduleName))
|
||||
throw new ArgumentException($"No module with the name '{moduleName}' found");
|
||||
|
||||
var moduleKvp = ModuleMeta
|
||||
.FirstOrDefault(x => x.Value.Name == moduleName);
|
||||
|
||||
var module = moduleKvp.Value;
|
||||
|
||||
if (!module.Modules.ContainsKey(section))
|
||||
return [];
|
||||
|
||||
var modulePaths = module.Modules[section].Select(
|
||||
dllName => PathBuilder.File(ModulePath, moduleKvp.Key, "bin", section, dllName)
|
||||
).Where(File.Exists).ToArray();
|
||||
|
||||
return modulePaths;
|
||||
}
|
||||
}
|
||||
75
Moonlight.ApiServer/Services/PluginService.cs
Normal file
75
Moonlight.ApiServer/Services/PluginService.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System.Text.Json;
|
||||
using MoonCore.Helpers;
|
||||
using Moonlight.ApiServer.Models;
|
||||
|
||||
namespace Moonlight.ApiServer.Services;
|
||||
|
||||
public class PluginService
|
||||
{
|
||||
private readonly Dictionary<string, PluginMeta> Plugins = new();
|
||||
private readonly ILogger<PluginService> Logger;
|
||||
|
||||
private static string PluginsPath = PathBuilder.Dir("storage", "plugins");
|
||||
|
||||
public PluginService(ILogger<PluginService> logger)
|
||||
{
|
||||
Logger = logger;
|
||||
}
|
||||
|
||||
public async Task Load()
|
||||
{
|
||||
Logger.LogInformation("Loading plugins...");
|
||||
|
||||
foreach (var pluginPath in Directory.EnumerateDirectories(PluginsPath))
|
||||
{
|
||||
var metaPath = PathBuilder.File(PluginsPath, "meta.json");
|
||||
|
||||
if (!File.Exists(metaPath))
|
||||
{
|
||||
Logger.LogWarning("Ignoring folder '{name}'. No meta.json found", pluginPath);
|
||||
continue;
|
||||
}
|
||||
|
||||
var metaContent = await File.ReadAllTextAsync(metaPath);
|
||||
PluginMeta meta;
|
||||
|
||||
try
|
||||
{
|
||||
meta = JsonSerializer.Deserialize<PluginMeta>(metaContent)
|
||||
?? throw new JsonException();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.LogCritical("Unable to deserialize meta.json: {e}", e);
|
||||
continue;
|
||||
}
|
||||
|
||||
Plugins.Add(pluginPath, meta);
|
||||
}
|
||||
}
|
||||
|
||||
public Task<Dictionary<string, string[]>> GetBinariesBySection(string section)
|
||||
{
|
||||
var bins = Plugins
|
||||
.Values
|
||||
.Where(x => x.Binaries.ContainsKey(section))
|
||||
.ToDictionary(x => x.Id, x => x.Binaries[section]);
|
||||
|
||||
return Task.FromResult(bins);
|
||||
}
|
||||
|
||||
public Task<Stream?> GetBinaryStream(string plugin, string section, string fileName)
|
||||
{
|
||||
if (Plugins.All(x => x.Value.Id != plugin))
|
||||
return Task.FromResult<Stream?>(null);
|
||||
|
||||
var pluginData = Plugins.First(x => x.Value.Id == plugin);
|
||||
var binaryPath = PathBuilder.File(pluginData.Key, section, fileName);
|
||||
|
||||
if (!File.Exists(binaryPath))
|
||||
return Task.FromResult<Stream?>(null);
|
||||
|
||||
var fs = File.OpenRead(binaryPath);
|
||||
return Task.FromResult<Stream?>(fs);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user