Files
minecraft-modpack-updater/ModpackUpdater.Apps.Manager/Ui/UpdatesCollectorUi.cs
Pascal f5596ab0ba manager: allow parent path annotations
- Allows unlimited amount of `..\` or `../` at the start of DestPath string.
- Each of then will go back one folder.
- Similar to how Linux works or Visual Studio project styles.
- This allows us to install/update lwjgl3ify in PrismLauncher.
2025-06-27 09:10:18 +02:00

140 lines
4.3 KiB
C#

using ModpackUpdater.Apps.Manager.Api.Model;
using ModpackUpdater.Manager;
using Telerik.WinControls.UI;
namespace ModpackUpdater.Apps.Manager.Ui;
public partial class UpdatesCollectorUi : RadForm
{
public record ModUpdateInfo(KeyValuePair<string, string>[] AvailableVersions, InstallAction Origin)
{
public int NewVersion { get; set; } = 0;
}
public record ModUpdates(List<ModUpdateInfo> List);
private readonly IWorkspace workspace;
private readonly ModpackFactory factory = new();
private readonly InstallAction[] actions;
public ModUpdates? CurrentUpdates { get; private set; }
public ModUpdateInfo? SelectedUpdate => radListView_Updates.SelectedItem?.Value as ModUpdateInfo;
public int SelectedVersion => radListView_VersionTags.SelectedIndex;
public UpdatesCollectorUi(IWorkspace workspace, params InstallAction[] actions)
{
this.workspace = workspace;
this.actions = actions;
InitializeComponent();
radListView_Updates.AutoSizeColumnsMode = ListViewAutoSizeColumnsMode.Fill;
radListView_VersionTags.AutoSizeColumnsMode = ListViewAutoSizeColumnsMode.Fill;
}
private async Task<ModUpdates> FindUpdates()
{
var list = new List<ModUpdateInfo>();
foreach (var action in actions)
{
var updates = await factory.FindUpdates(action, workspace.ModpackConfig?.MinecraftVersion, workspace.ModpackConfig?.ModLoader ?? ModLoader.Any);
BeginInvoke(() =>
{
radProgressBar1.Value1 += 1;
radProgressBar1.Text = $"{radProgressBar1.Value1} / {radProgressBar1.Maximum}";
});
if (updates == null || updates.Length == 0 || updates[0].Value == action.SourceTag)
continue;
list.Add(new(updates, action));
}
return new ModUpdates(list);
}
private void LoadUpdates(ModUpdates updates)
{
radListView_Updates.BeginUpdate();
radListView_Updates.Items.Clear();
foreach (var update in updates.List)
{
var item = new ListViewDataItem(update);
UpdateUpdatesItem(item);
radListView_Updates.Items.Add(item);
}
radListView_Updates.EndUpdate();
}
private static void UpdateUpdatesItem(ListViewDataItem? item)
{
if (item?.Value is not ModUpdateInfo updates)
return;
item[0] = updates.Origin.Name;
item[1] = updates.Origin.SourceTag;
item[2] = updates.AvailableVersions[updates.NewVersion].Value;
}
private void LoadVersions(ModUpdateInfo updates)
{
radListView_VersionTags.BeginUpdate();
radListView_VersionTags.Items.Clear();
foreach (var kvp in updates.AvailableVersions)
{
var item = new ListViewDataItem();
item[0] = kvp.Key;
item[1] = kvp.Value;
radListView_VersionTags.Items.Add(item);
}
radListView_VersionTags.SelectedIndex = updates.NewVersion;
radListView_VersionTags.EndUpdate();
}
private async void UpdatesCollectorUi_Shown(object sender, EventArgs e)
{
radWaitingBar1.StartWaiting();
radProgressBar1.Value1 = 0;
radProgressBar1.Maximum = actions.Length;
radProgressBar1.Text = null;
radProgressBar1.Visible = true;
CurrentUpdates = await FindUpdates();
LoadUpdates(CurrentUpdates);
radProgressBar1.Visible = false;
radWaitingBar1.StopWaiting();
}
private void RadListView_Updates_SelectedItemChanged(object sender, EventArgs e)
{
if (SelectedUpdate is ModUpdateInfo updates)
LoadVersions(updates);
}
private void RadListView_VersionTags_SelectedIndexChanged(object sender, EventArgs e)
{
var newIndex = SelectedVersion;
if (newIndex != -1 && SelectedUpdate is ModUpdateInfo updates)
{
updates.NewVersion = newIndex;
UpdateUpdatesItem(radListView_Updates.SelectedItem);
}
}
private void RadListView_Updates_ItemRemoved(object sender, ListViewItemEventArgs e)
{
if (e.Item?.Value is ModUpdateInfo update && CurrentUpdates is not null && CurrentUpdates.List.Contains(update))
CurrentUpdates.List.Remove(update);
}
}