Added Add Local plugin option

This commit is contained in:
2025-04-30 21:32:36 +03:00
parent 0c33422b5c
commit 2bd368dcce
4 changed files with 129 additions and 8 deletions

View File

@@ -91,6 +91,12 @@ public sealed class PluginManager : IPluginManager
{
pluginData.ListOfExecutableDependencies[dependency.Key] = dependency.Value;
}
if (installedPlugins.Any(plugin => plugin.PluginName == pluginData.PluginName))
{
_Logger.Log($"Plugin {pluginData.PluginName} already exists in the database. Updating...", this, LogType.Info);
installedPlugins.RemoveAll(p => p.PluginName == pluginData.PluginName);
}
installedPlugins.Add(pluginData);
await JsonManager.SaveToJsonFile(pluginDatabaseFile, installedPlugins);

View File

@@ -0,0 +1,103 @@
@page "/plugins/add-local"
@using System.ComponentModel.DataAnnotations
@using DiscordBotCore.Configuration
@using DiscordBotCore.Logging
@using DiscordBotCore.PluginManagement
@using DiscordBotCore.PluginManagement.Models
@inject NavigationManager Navigation
@inject IPluginManager PluginManager
@inject IConfiguration Configuration
@inject ILogger Logger
@rendermode InteractiveServer
<h3>Upload Plugin</h3>
<EditForm Model="@pluginModel" OnValidSubmit="HandleValidSubmit" FormName="pluginForm">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="form-group">
<label>Plugin Name:</label>
<InputText @bind-Value="pluginModel.Name" class="form-control" />
</div>
<div class="form-group">
<label>Plugin Version:</label>
<InputText @bind-Value="pluginModel.Version" class="form-control" />
@if (!IsValidVersion)
{
<div class="text-danger">Version must be in format X.X.X</div>
}
</div>
<div class="form-group">
<InputCheckbox @bind-Value="pluginModel.IsEnabled" /> Enable Plugin
</div>
<div class="form-group">
<label>Select DLL File:</label>
<InputFile OnChange="HandleFileSelected" />
@if (selectedFileName != null)
{
<div>Selected: @selectedFileName</div>
}
</div>
<button class="btn btn-primary" type="submit">Upload</button>
</EditForm>
@code {
private PluginUploadModel pluginModel = new();
private IBrowserFile? selectedFile;
private string? selectedFileName;
private bool IsValidVersion => System.Text.RegularExpressions.Regex.IsMatch(pluginModel.Version ?? "", @"^\d+\.\d+\.\d+$");
private async Task HandleFileSelected(InputFileChangeEventArgs e)
{
selectedFile = e.File;
selectedFileName = selectedFile.Name;
}
private async Task HandleValidSubmit()
{
if (!IsValidVersion || selectedFile is null || string.IsNullOrEmpty(selectedFileName))
{
return;
}
string? pluginsFolder = Configuration.Get<string>("PluginFolder");
if (string.IsNullOrEmpty(pluginsFolder))
{
Logger.Log("Plugins folder is not set in the configuration.", this, LogType.Error);
return;
}
string filePath = Path.Combine(pluginsFolder, selectedFileName);
await using var stream = selectedFile.OpenReadStream(maxAllowedSize: 10 * 1024 * 1024);
await using var fileStream = File.Create(filePath);
await stream.CopyToAsync(fileStream);
LocalPlugin plugin = new LocalPlugin(pluginModel.Name, pluginModel.Version, filePath, new(), true, pluginModel.IsEnabled);
await PluginManager.AppendPluginToDatabase(plugin);
Navigation.NavigateTo("/");
}
public class PluginUploadModel
{
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public string Version { get; set; } = string.Empty;
public bool IsEnabled { get; set; }
}
}

View File

@@ -6,6 +6,19 @@
@using DiscordBotCore.PluginManagement.Loading
@using DiscordBotCore.PluginManagement.Models
@inject IPluginManager PluginManager
@inject ILogger Logger
@inject IPluginLoader PluginLoader
@inject NavigationManager Navigation
<div class="d-flex justify-content-between align-items-center mb-3">
<button class="btn btn-success" @onclick="NavigateToAddPlugin">
<span class="bi me-1"></span> Add New Plugin
</button>
</div>
<h3>Installed Plugins</h3>
<table class="table">
<thead>
@@ -56,14 +69,6 @@
@code {
[Inject]
public IPluginManager PluginManager { get; set; }
[Inject]
public ILogger Logger { get; set; }
[Inject]
public IPluginLoader PluginLoader { get; set; }
private readonly List<InstalledPlugin> _installedPlugins = new List<InstalledPlugin>();
@@ -150,4 +155,10 @@
public string Version { get; set; }
public bool IsOfflineAdded { get; set; }
}
private void NavigateToAddPlugin()
{
Navigation.NavigateTo("/plugins/add-local");
}
}

View File

@@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DiscordBotCore.Database.Sqlite\DiscordBotCore.Database.Sqlite.csproj" />
<ProjectReference Include="..\DiscordBotCore.WebApplication\DiscordBotCore.WebApplication.csproj" />
</ItemGroup>