158 lines
4.9 KiB
C#
158 lines
4.9 KiB
C#
using ModpackUpdater.Apps.Manager.Api.Model;
|
|
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 factory = new ModpackFactory();
|
|
|
|
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 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(api.CurWorkspace, 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;
|
|
}
|
|
|
|
public static void FindNewDirectLinks(IMainApi api, params InstallAction[] actions)
|
|
{
|
|
var mainForm = api.MainWindow as MainForm;
|
|
var gridView = mainForm?.Controls.Find("radGridView_Actions", true).FirstOrDefault() as RadGridView;
|
|
var rwb = mainForm?.Controls.Find("radWaitingBar_Actions", true).FirstOrDefault() as RadWaitingBar;
|
|
var factory = new ModpackFactory();
|
|
|
|
rwb?.StartWaiting();
|
|
gridView?.BeginUpdate();
|
|
|
|
foreach (var action in actions)
|
|
{
|
|
if (action.SourceType != SourceType.DirectLink)
|
|
{
|
|
Task.Run(async () =>
|
|
{
|
|
action.SourceUrl = await factory.ResolveSourceUrl(action);
|
|
}).Wait();
|
|
api.UpdateItem(action);
|
|
}
|
|
}
|
|
|
|
gridView?.EndUpdate();
|
|
rwb?.StopWaiting();
|
|
}
|
|
|
|
public static void ClearDirectLinks(IMainApi api, params InstallAction[] actions)
|
|
{
|
|
var mainForm = api.MainWindow as MainForm;
|
|
var gridView = mainForm?.Controls.Find("radGridView_Actions", true).FirstOrDefault() as RadGridView;
|
|
var rwb = mainForm?.Controls.Find("radWaitingBar_Actions", true).FirstOrDefault() as RadWaitingBar;
|
|
|
|
rwb?.StartWaiting();
|
|
gridView?.BeginUpdate();
|
|
|
|
foreach (var action in actions)
|
|
{
|
|
if (action.SourceType != SourceType.DirectLink)
|
|
{
|
|
action.SourceUrl = null;
|
|
api.UpdateItem(action);
|
|
}
|
|
}
|
|
|
|
gridView?.EndUpdate();
|
|
rwb?.StopWaiting();
|
|
}
|
|
}
|