Implemented a basic plugin system
This commit is contained in:
12
Moonlight/App/Helpers/ComponentHelper.cs
Normal file
12
Moonlight/App/Helpers/ComponentHelper.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using Microsoft.AspNetCore.Components;
|
||||
|
||||
namespace Moonlight.App.Helpers;
|
||||
|
||||
public class ComponentHelper
|
||||
{
|
||||
public static RenderFragment FromType(Type type) => builder =>
|
||||
{
|
||||
builder.OpenComponent(0, type);
|
||||
builder.CloseComponent();
|
||||
};
|
||||
}
|
||||
15
Moonlight/App/Plugin/MoonlightPlugin.cs
Normal file
15
Moonlight/App/Plugin/MoonlightPlugin.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Moonlight.App.Plugin.UI;
|
||||
using Moonlight.App.Plugin.UI.Servers;
|
||||
using Moonlight.App.Plugin.UI.Webspaces;
|
||||
|
||||
namespace Moonlight.App.Plugin;
|
||||
|
||||
public abstract class MoonlightPlugin
|
||||
{
|
||||
public string Name { get; set; } = "N/A";
|
||||
public string Author { get; set; } = "N/A";
|
||||
public string Version { get; set; } = "N/A";
|
||||
|
||||
public Func<ServerPageContext, Task>? OnBuildServerPage { get; set; }
|
||||
public Func<WebspacePageContext, Task>? OnBuildWebspacePage { get; set; }
|
||||
}
|
||||
11
Moonlight/App/Plugin/UI/Servers/ServerPageContext.cs
Normal file
11
Moonlight/App/Plugin/UI/Servers/ServerPageContext.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using Moonlight.App.Database.Entities;
|
||||
|
||||
namespace Moonlight.App.Plugin.UI.Servers;
|
||||
|
||||
public class ServerPageContext
|
||||
{
|
||||
public List<ServerTab> Tabs { get; set; } = new();
|
||||
public List<ServerSetting> Settings { get; set; } = new();
|
||||
public Server Server { get; set; }
|
||||
public User User { get; set; }
|
||||
}
|
||||
9
Moonlight/App/Plugin/UI/Servers/ServerSetting.cs
Normal file
9
Moonlight/App/Plugin/UI/Servers/ServerSetting.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Microsoft.AspNetCore.Components;
|
||||
|
||||
namespace Moonlight.App.Plugin.UI.Servers;
|
||||
|
||||
public class ServerSetting
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public RenderFragment Component { get; set; }
|
||||
}
|
||||
11
Moonlight/App/Plugin/UI/Servers/ServerTab.cs
Normal file
11
Moonlight/App/Plugin/UI/Servers/ServerTab.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using Microsoft.AspNetCore.Components;
|
||||
|
||||
namespace Moonlight.App.Plugin.UI.Servers;
|
||||
|
||||
public class ServerTab
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Route { get; set; }
|
||||
public string Icon { get; set; }
|
||||
public RenderFragment Component { get; set; }
|
||||
}
|
||||
10
Moonlight/App/Plugin/UI/Webspaces/WebspacePageContext.cs
Normal file
10
Moonlight/App/Plugin/UI/Webspaces/WebspacePageContext.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Moonlight.App.Database.Entities;
|
||||
|
||||
namespace Moonlight.App.Plugin.UI.Webspaces;
|
||||
|
||||
public class WebspacePageContext
|
||||
{
|
||||
public List<WebspaceTab> Tabs { get; set; } = new();
|
||||
public User User { get; set; }
|
||||
public WebSpace WebSpace { get; set; }
|
||||
}
|
||||
10
Moonlight/App/Plugin/UI/Webspaces/WebspaceTab.cs
Normal file
10
Moonlight/App/Plugin/UI/Webspaces/WebspaceTab.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Microsoft.AspNetCore.Components;
|
||||
|
||||
namespace Moonlight.App.Plugin.UI.Webspaces;
|
||||
|
||||
public class WebspaceTab
|
||||
{
|
||||
public string Name { get; set; } = "N/A";
|
||||
public string Route { get; set; } = "/";
|
||||
public RenderFragment Component { get; set; }
|
||||
}
|
||||
69
Moonlight/App/Services/PluginService.cs
Normal file
69
Moonlight/App/Services/PluginService.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System.Reflection;
|
||||
using Moonlight.App.Database.Entities;
|
||||
using Moonlight.App.Helpers;
|
||||
using Moonlight.App.Plugin;
|
||||
using Moonlight.App.Plugin.UI;
|
||||
using Moonlight.App.Plugin.UI.Servers;
|
||||
using Moonlight.App.Plugin.UI.Webspaces;
|
||||
|
||||
namespace Moonlight.App.Services;
|
||||
|
||||
public class PluginService
|
||||
{
|
||||
public List<MoonlightPlugin> Plugins { get; set; }
|
||||
|
||||
public PluginService()
|
||||
{
|
||||
LoadPlugins();
|
||||
}
|
||||
|
||||
private void LoadPlugins()
|
||||
{
|
||||
Plugins = new();
|
||||
|
||||
var pluginType = typeof(MoonlightPlugin);
|
||||
|
||||
foreach (var pluginFile in Directory.EnumerateFiles(
|
||||
PathBuilder.Dir(Directory.GetCurrentDirectory(), "storage", "plugins"))
|
||||
.Where(x => x.EndsWith(".dll")))
|
||||
{
|
||||
var assembly = Assembly.LoadFile(pluginFile);
|
||||
|
||||
foreach (var type in assembly.GetTypes())
|
||||
{
|
||||
if (type.IsSubclassOf(pluginType))
|
||||
{
|
||||
var plugin = (Activator.CreateInstance(type) as MoonlightPlugin)!;
|
||||
|
||||
Logger.Info($"Loaded plugin '{plugin.Name}' ({plugin.Version}) by {plugin.Author}");
|
||||
|
||||
Plugins.Add(plugin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Logger.Info($"Loaded {Plugins.Count} plugins");
|
||||
}
|
||||
|
||||
public async Task<ServerPageContext> BuildServerPage(ServerPageContext context)
|
||||
{
|
||||
foreach (var plugin in Plugins)
|
||||
{
|
||||
if (plugin.OnBuildServerPage != null)
|
||||
await plugin.OnBuildServerPage.Invoke(context);
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
public async Task<WebspacePageContext> BuildWebspacePage(WebspacePageContext context)
|
||||
{
|
||||
foreach (var plugin in Plugins)
|
||||
{
|
||||
if (plugin.OnBuildWebspacePage != null)
|
||||
await plugin.OnBuildWebspacePage.Invoke(context);
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user