Files
minecraft-modpack-updater/ModpackUpdater.Apps.Manager/Features/SharedFunctions.cs

111 lines
3.4 KiB
C#

using ModpackUpdater.Apps.Manager.Api.Model;
using ModpackUpdater.Apps.Manager.Features.Tools;
using ModpackUpdater.Apps.Manager.Ui;
using ModpackUpdater.Manager;
using Pilz.UI.Extensions;
using Telerik.WinControls.UI;
namespace ModpackUpdater.Apps.Manager.Features;
internal static class SharedFunctions
{
public static bool CheckActionHealthy(IMainApi api, params GridViewRowInfo[] selectedRows)
{
if (api.MainWindow is not MainForm mainForm
|| mainForm.Controls.Find("radGridView_Actions", true).FirstOrDefault() is not RadGridView gridView
|| mainForm.Controls.Find("radWaitingBar_Actions", true).FirstOrDefault() is not RadWaitingBar rwb)
return false;
rwb.StartWaiting();
rwb.ShowText = true;
var rowsCount = selectedRows.Length;
rwb.Text = "0 / " + rowsCount;
gridView.BeginUpdate();
var failed = false;
var msg = default(string);
var rows = new Dictionary<GridViewRowInfo, InstallAction>();
for (var i = 0; i < selectedRows.Length; i++)
{
var row = selectedRows[i];
if (row.Tag is InstallAction action)
{
Task.Run(async () =>
{
try
{
var factory = new ModpackFactory();
var result = await factory.ResolveSourceUrl(action);
failed = string.IsNullOrWhiteSpace(result);
}
catch (Exception ex)
{
msg = ex.Message;
}
}).Wait();
}
foreach (var c in row.Cells)
{
c.Style.CustomizeFill = true;
c.Style.BackColor = failed ? Color.IndianRed : Color.ForestGreen;
}
rwb.Text = $"{i} / {rowsCount}";
Application.DoEvents();
}
gridView.EndUpdate();
rwb.ShowText = false;
rwb.StopWaiting();
if (rowsCount == 1 && failed && !string.IsNullOrWhiteSpace(msg))
MessageBox.Show(msg);
return true;
}
public static bool CollectUpdates(IMainApi api, params InstallAction[] actions)
{
if (api.CurWorkspace?.UpdateInfos is null)
return false;
// Collect updates
var ucDialog = new UpdatesCollectorUi(actions);
if (!ucDialog.ShowDialog(api.MainWindow).IsOk() || ucDialog.CurrentUpdates is null)
return false;
// Collect versions with changes
var updates = new List<UpdatesCollectorUi.ModUpdateInfo>();
foreach (var update in ucDialog.CurrentUpdates.List)
{
if (update.Origin.SourceTag != update.AvailableVersions[update.NewVersion].Value)
updates.Add(update);
}
// Path install actions
foreach (var update in updates)
{
update.Origin.SourceTag = update.AvailableVersions[update.NewVersion].Value;
api.UpdateItem(update.Origin);
}
// Create update actions
var updateSet = new UpdateInfo();
foreach (var update in updates)
{
updateSet.Actions.Add(new()
{
InheritFrom = update.Origin.Id,
});
}
api.CurWorkspace.UpdateInfos.Updates.Insert(0, updateSet);
api.UpdateItem(updateSet);
return true;
}
}