2022-09-12 23:06:14 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.CommandLine;
|
|
|
|
using System.CommandLine.Invocation;
|
2022-09-29 05:10:49 +00:00
|
|
|
using System.CommandLine.NamingConventionBinder;
|
2022-09-12 23:06:14 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.IO.Compression;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Reflection;
|
2022-10-07 20:53:55 +00:00
|
|
|
using System.Text;
|
2023-11-15 03:09:50 +00:00
|
|
|
using System.Threading;
|
2022-09-12 23:06:14 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Nettle;
|
2022-10-14 22:08:21 +00:00
|
|
|
using Wabbajack.CLI.Builder;
|
2022-09-12 23:06:14 +00:00
|
|
|
using Wabbajack.Common;
|
|
|
|
using Wabbajack.DTOs.Directives;
|
|
|
|
using Wabbajack.DTOs.JsonConverters;
|
|
|
|
using Wabbajack.Installer;
|
|
|
|
using Wabbajack.Paths;
|
|
|
|
using Wabbajack.Paths.IO;
|
|
|
|
|
|
|
|
namespace Wabbajack.CLI.Verbs;
|
|
|
|
|
2022-10-14 22:08:21 +00:00
|
|
|
public class ModlistReport
|
2022-09-12 23:06:14 +00:00
|
|
|
{
|
|
|
|
private readonly ILogger<ModlistReport> _logger;
|
|
|
|
private readonly DTOSerializer _dtos;
|
|
|
|
|
|
|
|
public ModlistReport(ILogger<ModlistReport> logger, DTOSerializer dtos)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_dtos = dtos;
|
|
|
|
}
|
2022-10-01 01:35:36 +00:00
|
|
|
|
|
|
|
public static VerbDefinition Definition = new("modlist-report",
|
|
|
|
"Generates a usage report for a Modlist file", new[]
|
|
|
|
{
|
|
|
|
new OptionDefinition(typeof(AbsolutePath), "i", "input", "Wabbajack file from which to generate a report")
|
|
|
|
});
|
2022-09-12 23:06:14 +00:00
|
|
|
|
|
|
|
private static async Task<string> ReportTemplate(object o)
|
|
|
|
{
|
2022-10-07 20:53:55 +00:00
|
|
|
var data = await (typeof(ModlistReport).Assembly.GetManifestResourceStream("Wabbajack.CLI.Resources.ModlistReport.html")!).ReadAllAsync();
|
|
|
|
var func = NettleEngine.GetCompiler().Compile(Encoding.UTF8.GetString(data));
|
2023-11-15 03:09:50 +00:00
|
|
|
return await func(o, CancellationToken.None);
|
2022-09-12 23:06:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<int> Run(AbsolutePath input)
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.LogInformation("Loading Modlist");
|
|
|
|
var modlist = await StandardInstaller.LoadFromFile(_dtos, input);
|
|
|
|
|
|
|
|
Dictionary<string, long> patchSizes;
|
|
|
|
using (var zip = new ZipArchive(input.Open(FileMode.Open, FileAccess.Read, FileShare.Read)))
|
|
|
|
{
|
|
|
|
patchSizes = zip.Entries.ToDictionary(e => e.Name, e => e.Length);
|
|
|
|
}
|
|
|
|
|
|
|
|
var archives = modlist.Archives.ToDictionary(a => a.Hash, a => a.Name);
|
|
|
|
var bsas = modlist.Directives.OfType<CreateBSA>().ToDictionary(bsa => bsa.TempID.ToString());
|
|
|
|
|
|
|
|
var inlinedData = modlist.Directives.OfType<InlineFile>()
|
|
|
|
.Select(e => new
|
|
|
|
{
|
|
|
|
To = e.To.ToString(),
|
|
|
|
Id = e.SourceDataID.ToString(),
|
|
|
|
SizeInt = e.Size,
|
|
|
|
Size = e.Size.ToFileSizeString()
|
|
|
|
}).ToArray();
|
|
|
|
|
|
|
|
string FixupTo(RelativePath path)
|
|
|
|
{
|
2022-10-24 23:28:03 +00:00
|
|
|
if (path.GetPart(0) != Consts.BSACreationDir.ToString()) return path.ToString();
|
2022-09-12 23:06:14 +00:00
|
|
|
var bsaId = path.GetPart(1);
|
|
|
|
|
|
|
|
if (!bsas.TryGetValue(bsaId, out var bsa))
|
|
|
|
{
|
|
|
|
return path.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
var relPath = RelativePath.FromParts(path.Parts[2..]);
|
|
|
|
return $"<i> {bsa.To} </i> | {relPath}";
|
|
|
|
}
|
|
|
|
|
|
|
|
var patchData = modlist.Directives.OfType<PatchedFromArchive>()
|
|
|
|
.Select(e => new
|
|
|
|
{
|
|
|
|
From = $"<i> {archives[e.ArchiveHashPath.Hash]} </i> | {string.Join(" | ", e.ArchiveHashPath.Parts.Select(e => e.ToString()))}",
|
|
|
|
To = FixupTo(e.To),
|
|
|
|
Id = e.PatchID.ToString(),
|
|
|
|
PatchSize = patchSizes[e.PatchID.ToString()].ToFileSizeString(),
|
|
|
|
PatchSizeInt = patchSizes[e.PatchID.ToString()],
|
|
|
|
FinalSize = e.Size.ToFileSizeString(),
|
|
|
|
}).ToArray();
|
|
|
|
|
|
|
|
var data = await ReportTemplate(new
|
|
|
|
{
|
|
|
|
Name = modlist.Name,
|
|
|
|
TotalInlinedSize = inlinedData.Sum(i => i.SizeInt).ToFileSizeString(),
|
|
|
|
InlinedData = inlinedData,
|
|
|
|
TotalPatchSize = patchData.Sum(i => i.PatchSizeInt).ToFileSizeString(),
|
|
|
|
PatchData = patchData,
|
|
|
|
WabbajackSize = input.Size().ToFileSizeString()
|
|
|
|
});
|
|
|
|
|
|
|
|
await input.WithExtension(Ext.Html).WriteAllTextAsync(data);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|