310 lines
10 KiB
C#
310 lines
10 KiB
C#
using ModpackUpdater.Apps.Manager.Api.Model;
|
|
using ModpackUpdater.Apps.Manager.LangRes;
|
|
using ModpackUpdater.Apps.Manager.Ui;
|
|
using ModpackUpdater.Manager;
|
|
using OfficeOpenXml;
|
|
using Pilz.UI.WinForms.Extensions;
|
|
using System.Text;
|
|
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].Key)
|
|
updates.Add(update);
|
|
}
|
|
|
|
// Path install actions
|
|
foreach (var update in updates)
|
|
{
|
|
update.Origin.SourceTag = update.AvailableVersions[update.NewVersion].Key;
|
|
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)
|
|
{
|
|
try
|
|
{
|
|
Task.Run(async () =>
|
|
{
|
|
action.SourceUrl = await factory.ResolveSourceUrl(action);
|
|
}).Wait();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// Fail silently
|
|
}
|
|
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();
|
|
}
|
|
|
|
public static string GenerateChangelog(InstallInfos installInfos, UpdateInfo updateInfos)
|
|
{
|
|
var log = new StringBuilder();
|
|
|
|
foreach (var action in updateInfos.Actions.OrderBy(n => n.Type))
|
|
{
|
|
// Create copy
|
|
var copy = new UpdateAction();
|
|
ModpackInstaller.DuplicateTo(action, copy);
|
|
|
|
// Resolve inherit
|
|
if (!string.IsNullOrWhiteSpace(copy.InheritFrom))
|
|
ModpackInstaller.ResolveInherit(copy, installInfos);
|
|
|
|
if (string.IsNullOrWhiteSpace(copy.Name) || copy.Type != UpdateActionType.Update)
|
|
continue;
|
|
|
|
// Append bullet
|
|
log.Append('-');
|
|
|
|
// Append action indicator
|
|
log.Append(' ');
|
|
log.Append("⚒️");
|
|
|
|
// Append name
|
|
log.Append(' ');
|
|
log.Append("**");
|
|
log.Append(copy.Name);
|
|
log.Append("**");
|
|
|
|
// Append new version
|
|
if (!string.IsNullOrWhiteSpace(copy.SourceTag))
|
|
{
|
|
log.Append(' ');
|
|
log.Append('`');
|
|
log.Append(copy.SourceTag);
|
|
log.Append('`');
|
|
}
|
|
|
|
// Append new line
|
|
log.AppendLine();
|
|
}
|
|
|
|
return log.ToString().TrimEnd();
|
|
}
|
|
|
|
public static string GenerateModlistAsMarkdown(InstallInfos installInfos)
|
|
{
|
|
var sb = new StringBuilder();
|
|
sb.Append("|" + ActionsListLangRes.Col_Name);
|
|
sb.Append("|" + ActionsListLangRes.Col_SrcTag);
|
|
sb.Append("|" + ActionsListLangRes.Col_Side);
|
|
sb.Append("|" + ActionsListLangRes.Col_SrcType);
|
|
sb.Append("|" + ActionsListLangRes.Col_SrcOwner);
|
|
sb.Append("|" + ActionsListLangRes.Col_SrcName);
|
|
sb.AppendLine("|");
|
|
sb.AppendLine("|---|---|---|---|---|---|");
|
|
|
|
// Rows
|
|
foreach (var action in installInfos.Actions.OrderBy(n => n.Name))
|
|
{
|
|
if (action.IsExtra || action.IsZip || string.IsNullOrWhiteSpace(action.Id) || !action.Id.StartsWith("mod:"))
|
|
continue;
|
|
|
|
if (string.IsNullOrWhiteSpace(action.Website))
|
|
sb.Append($"|{action.Name}");
|
|
else
|
|
sb.Append($"|[{action.Name}]({action.Website})");
|
|
|
|
if (string.IsNullOrWhiteSpace(action.SourceUrl))
|
|
sb.Append($"|{action.SourceTag}");
|
|
else
|
|
sb.Append($"|[{action.SourceTag}]({action.GetSourceUrl(installInfos.Version)})");
|
|
|
|
sb.Append($"|{action.Side.ToString()}");
|
|
sb.Append($"|{action.SourceType}");
|
|
sb.Append($"|{action.SourceOwner}");
|
|
sb.Append($"|{action.SourceName}");
|
|
|
|
sb.AppendLine("|");
|
|
}
|
|
|
|
return sb.ToString().TrimEnd();
|
|
}
|
|
|
|
public static ExcelPackage? GenerateModlistAsExcel(InstallInfos installInfos)
|
|
{
|
|
var pkg = new ExcelPackage();
|
|
var ws = pkg.Workbook.Worksheets.Add(string.Format(GeneralLangRes.ModlistForVersionX, installInfos.Version));
|
|
var cr = 1;
|
|
var cc = 1;
|
|
|
|
// Header
|
|
ws.Cells[cr, cc++].Value = ActionsListLangRes.Col_Name;
|
|
ws.Cells[cr, cc++].Value = ActionsListLangRes.Col_SrcTag;
|
|
ws.Cells[cr, cc++].Value = ActionsListLangRes.Col_Side;
|
|
ws.Cells[cr, cc++].Value = ActionsListLangRes.Col_SrcType;
|
|
ws.Cells[cr, cc++].Value = ActionsListLangRes.Col_SrcOwner;
|
|
ws.Cells[cr, cc++].Value = ActionsListLangRes.Col_SrcName;
|
|
cr += 1;
|
|
cc = 1;
|
|
|
|
// Rows
|
|
foreach (var action in installInfos.Actions.OrderBy(n => n.Name))
|
|
{
|
|
if (action.IsExtra || action.IsZip || string.IsNullOrWhiteSpace(action.Id) || !action.Id.StartsWith("mod:"))
|
|
continue;
|
|
|
|
var cellName = ws.Cells[cr, cc++];
|
|
cellName.Value = action.Name;
|
|
if (!string.IsNullOrWhiteSpace(action.Website))
|
|
cellName.SetHyperlink(new Uri(action.Website));
|
|
|
|
var cellTag = ws.Cells[cr, cc++];
|
|
cellTag.Value = string.IsNullOrWhiteSpace(action.SourceTag) ? "direct link" : action.SourceTag;
|
|
if (!string.IsNullOrWhiteSpace(action.SourceUrl))
|
|
cellTag.SetHyperlink(new Uri(action.GetSourceUrl(installInfos.Version)));
|
|
|
|
ws.Cells[cr, cc++].Value = action.Side.ToString();
|
|
ws.Cells[cr, cc++].Value = action.SourceType;
|
|
ws.Cells[cr, cc++].Value = action.SourceOwner;
|
|
ws.Cells[cr, cc++].Value = action.SourceName;
|
|
|
|
cr += 1;
|
|
cc = 1;
|
|
}
|
|
|
|
// Styling
|
|
cc = 1;
|
|
ws.Column(cc++).Width = 30;
|
|
ws.Column(cc++).Width = 20;
|
|
ws.Column(cc++).Width = 10;
|
|
ws.Column(cc++).Width = 20;
|
|
ws.Column(cc++).Width = 20;
|
|
ws.Column(cc++).Width = 30;
|
|
var tableDef = ws.Tables.Add(ws.Cells[1, 1, cr - 1, cc - 1], "Table");
|
|
tableDef.TableStyle = OfficeOpenXml.Table.TableStyles.Medium16;
|
|
tableDef.ShowHeader = true;
|
|
|
|
return pkg;
|
|
}
|
|
}
|