new plugin loader init
This commit is contained in:
@@ -21,6 +21,8 @@ using NLog;
|
|||||||
using Torch.API.WebAPI;
|
using Torch.API.WebAPI;
|
||||||
using Torch.Collections;
|
using Torch.Collections;
|
||||||
using Torch.Server.Annotations;
|
using Torch.Server.Annotations;
|
||||||
|
using Torch.Managers;
|
||||||
|
using Torch.API.Managers;
|
||||||
|
|
||||||
namespace Torch.Server.Views
|
namespace Torch.Server.Views
|
||||||
{
|
{
|
||||||
@@ -31,8 +33,11 @@ namespace Torch.Server.Views
|
|||||||
{
|
{
|
||||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
public MtObservableList<PluginItem> PluginsSource { get; set; } = new MtObservableList<PluginItem>();
|
||||||
public MtObservableList<PluginItem> Plugins { get; set; } = new MtObservableList<PluginItem>();
|
public MtObservableList<PluginItem> Plugins { get; set; } = new MtObservableList<PluginItem>();
|
||||||
public PluginItem CurrentItem { get; set; }
|
public PluginItem CurrentItem { get; set; }
|
||||||
|
public const string PLUGINS_SEARCH_TEXT = "Plugins search...";
|
||||||
|
private string PreviousSearchQuery = "";
|
||||||
|
|
||||||
private string _description = "Loading data from server, please wait..";
|
private string _description = "Loading data from server, please wait..";
|
||||||
public string CurrentDescription
|
public string CurrentDescription
|
||||||
@@ -45,18 +50,25 @@ namespace Torch.Server.Views
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public PluginBrowser()
|
public PluginBrowser(IPluginManager pluginManager)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
|
var installedPlugins = pluginManager.Plugins;
|
||||||
Task.Run(async () =>
|
Task.Run(async () =>
|
||||||
{
|
{
|
||||||
var res = await PluginQuery.Instance.QueryAll();
|
var res = await PluginQuery.Instance.QueryAll();
|
||||||
if (res == null)
|
if (res == null)
|
||||||
return;
|
return;
|
||||||
foreach (var item in res.Plugins)
|
foreach (var item in res.Plugins.OrderBy(i => i.Name)) {
|
||||||
|
if (installedPlugins.Keys.Contains(Guid.Parse(item.ID)))
|
||||||
|
item.Installed = true;
|
||||||
|
|
||||||
Plugins.Add(item);
|
Plugins.Add(item);
|
||||||
PluginsList.Dispatcher.Invoke(() => PluginsList.SelectedIndex = 0);
|
PluginsList.Dispatcher.Invoke(() => PluginsList.SelectedIndex = 0);
|
||||||
|
PluginsSource.Add(item);
|
||||||
|
}
|
||||||
|
CurrentDescription = "Please select a plugin...";
|
||||||
});
|
});
|
||||||
|
|
||||||
MarkdownFlow.CommandBindings.Add(new CommandBinding(NavigationCommands.GoToPage, (sender, e) => OpenUri((string)e.Parameter)));
|
MarkdownFlow.CommandBindings.Add(new CommandBinding(NavigationCommands.GoToPage, (sender, e) => OpenUri((string)e.Parameter)));
|
||||||
@@ -83,23 +95,49 @@ namespace Torch.Server.Views
|
|||||||
private void PluginsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
private void PluginsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||||
{
|
{
|
||||||
CurrentItem = (PluginItem)PluginsList.SelectedItem;
|
CurrentItem = (PluginItem)PluginsList.SelectedItem;
|
||||||
CurrentDescription = CurrentItem.Description;
|
if (CurrentItem != null) {
|
||||||
DownloadButton.IsEnabled = !string.IsNullOrEmpty(CurrentItem.LatestVersion);
|
CurrentDescription = CurrentItem.Description;
|
||||||
|
DownloadButton.IsEnabled = !string.IsNullOrEmpty(CurrentItem.LatestVersion);
|
||||||
|
UninstallButton.IsEnabled = !string.IsNullOrEmpty(CurrentItem.LatestVersion);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DownloadButton_OnClick(object sender, RoutedEventArgs e)
|
private void DownloadButton_OnClick(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
var item = CurrentItem;
|
var SelectedItems = PluginsList.SelectedItems;
|
||||||
TorchBase.Instance.Config.Plugins.Add(new Guid(item.ID));
|
|
||||||
|
foreach(PluginItem PluginItem in SelectedItems)
|
||||||
|
TorchBase.Instance.Config.Plugins.Add(new Guid(PluginItem.ID));
|
||||||
|
|
||||||
TorchBase.Instance.Config.Save();
|
TorchBase.Instance.Config.Save();
|
||||||
Task.Run(async () =>
|
Log.Info($"Started to download {SelectedItems.Count} plugin(s)");
|
||||||
{
|
|
||||||
var result = await PluginQuery.Instance.DownloadPlugin(item.ID);
|
PluginDownloader DownloadProgress = new PluginDownloader(SelectedItems);
|
||||||
MessageBox.Show(result ? "Plugin downloaded successfully! Please restart the server to load changes."
|
DownloadProgress.Show();
|
||||||
: "Plugin failed to download! See log for details.",
|
}
|
||||||
"Plugin Downloader",
|
|
||||||
MessageBoxButton.OK);
|
private void UninstallButton_OnClick(object sender, RoutedEventArgs e) {
|
||||||
});
|
var SelectedItems = PluginsList.SelectedItems;
|
||||||
|
if(SelectedItems.Cast<PluginItem>().Any(x => x.Installed == false)) {
|
||||||
|
MessageBox.Show($"Error! You have selected at least 1 plugin which isnt currently installed. Please de-select and try again!", "Uninstall Error", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var result = MessageBox.Show($"Are you sure you want to attempt uninstall of {SelectedItems.Count} plugin(s)?", "Uninstall Confirmation", MessageBoxButton.YesNo);
|
||||||
|
if (result == MessageBoxResult.Yes) {
|
||||||
|
foreach(PluginItem PluginItem in SelectedItems) {
|
||||||
|
if(TorchBase.Instance.Config.Plugins.Contains(Guid.Parse(PluginItem.ID))) {
|
||||||
|
TorchBase.Instance.Config.Plugins.Remove(Guid.Parse(PluginItem.ID));
|
||||||
|
|
||||||
|
string path = $"Plugins\\{PluginItem.Name}.zip";
|
||||||
|
|
||||||
|
if (File.Exists(path))
|
||||||
|
File.Delete(path);
|
||||||
|
|
||||||
|
Log.Info($"Uninstalled {PluginItem.Name}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MessageBox.Show($"Plugins removed... Please restart your server for changes to take effect.", "Uninstall Confirmation", MessageBoxButton.OK);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public event PropertyChangedEventHandler PropertyChanged;
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
@@ -109,6 +147,59 @@ namespace Torch.Server.Views
|
|||||||
{
|
{
|
||||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void TxtPluginsSearch_GotFocus(object sender, RoutedEventArgs e) {
|
||||||
|
if (txtPluginsSearch.Text == PLUGINS_SEARCH_TEXT) {
|
||||||
|
txtPluginsSearch.Clear();
|
||||||
|
txtPluginsSearch.Foreground = Brushes.Black;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void TxtPluginsSearch_LostFocus(object sender, RoutedEventArgs e) {
|
||||||
|
if(txtPluginsSearch.Text == "") {
|
||||||
|
txtPluginsSearch.Foreground = Brushes.Gray;
|
||||||
|
txtPluginsSearch.Text = PLUGINS_SEARCH_TEXT;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void TxtPluginsSearch_TextChanged(object sender, TextChangedEventArgs e) {
|
||||||
|
string SearchQueryString = txtPluginsSearch.Text;
|
||||||
|
|
||||||
|
if(SearchQueryString.Length < PreviousSearchQuery.Length) {
|
||||||
|
ResetSearchFilter();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (SearchQueryString != PLUGINS_SEARCH_TEXT && SearchQueryString != string.Empty) {
|
||||||
|
SearchPlugins(SearchQueryString);
|
||||||
|
} else {
|
||||||
|
ResetSearchFilter();
|
||||||
|
}
|
||||||
|
|
||||||
|
PreviousSearchQuery = SearchQueryString;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SearchPlugins(string SearchQueryString) {
|
||||||
|
foreach (var plugin in Plugins.Where(p => !p.Name.Contains(SearchQueryString, StringComparison.OrdinalIgnoreCase) &&
|
||||||
|
!p.Author.Contains(SearchQueryString, StringComparison.OrdinalIgnoreCase))) {
|
||||||
|
Plugins.Remove(plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var plugin in Plugins.Where(p => p.Name.Contains(SearchQueryString, StringComparison.OrdinalIgnoreCase) ||
|
||||||
|
p.Author.Contains(SearchQueryString, StringComparison.OrdinalIgnoreCase))) {
|
||||||
|
if (!Plugins.Contains(plugin))
|
||||||
|
Plugins.Add(plugin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ResetSearchFilter() {
|
||||||
|
Plugins.Clear();
|
||||||
|
foreach (var plugin in PluginsSource) {
|
||||||
|
if (!Plugins.Contains(plugin))
|
||||||
|
Plugins.Add(plugin);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user