2019-11-03 15:26:51 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-11-17 15:00:33 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
2019-11-03 15:26:51 +00:00
|
|
|
|
using System.Linq;
|
2019-11-15 23:13:27 +00:00
|
|
|
|
using System.Reactive.Subjects;
|
2019-12-04 01:26:26 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2019-12-03 21:56:18 +00:00
|
|
|
|
using System.Threading;
|
2019-11-17 15:00:33 +00:00
|
|
|
|
using CommonMark;
|
2019-11-03 16:43:43 +00:00
|
|
|
|
using Wabbajack.Common;
|
2019-11-03 15:26:51 +00:00
|
|
|
|
using Wabbajack.Lib.CompilationSteps;
|
2019-11-17 15:00:33 +00:00
|
|
|
|
using Wabbajack.Lib.Downloaders;
|
|
|
|
|
using Wabbajack.Lib.ModListRegistry;
|
2019-11-15 13:06:34 +00:00
|
|
|
|
using Wabbajack.VirtualFileSystem;
|
2019-11-17 15:00:33 +00:00
|
|
|
|
using Directory = Alphaleonis.Win32.Filesystem.Directory;
|
|
|
|
|
using File = Alphaleonis.Win32.Filesystem.File;
|
|
|
|
|
using Path = Alphaleonis.Win32.Filesystem.Path;
|
2019-11-03 15:26:51 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Lib
|
|
|
|
|
{
|
2019-11-17 23:48:32 +00:00
|
|
|
|
public abstract class ACompiler : ABatchProcessor
|
2019-11-03 15:26:51 +00:00
|
|
|
|
{
|
2019-11-17 14:30:06 +00:00
|
|
|
|
public string ModListName, ModListAuthor, ModListDescription, ModListImage, ModListWebsite, ModListReadme;
|
2019-12-20 07:14:43 +00:00
|
|
|
|
public bool ReadmeIsWebsite;
|
2019-11-17 14:30:06 +00:00
|
|
|
|
public string WabbajackVersion;
|
|
|
|
|
|
2019-11-15 23:13:27 +00:00
|
|
|
|
protected static string _vfsCacheName = "vfs_compile_cache.bin";
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A stream of tuples of ("Update Title", 0.25) which represent the name of the current task
|
|
|
|
|
/// and the current progress.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IObservable<(string, float)> ProgressUpdates => _progressUpdates;
|
|
|
|
|
protected readonly Subject<(string, float)> _progressUpdates = new Subject<(string, float)>();
|
|
|
|
|
|
2019-11-24 00:30:51 +00:00
|
|
|
|
public abstract ModManager ModManager { get; }
|
2019-11-03 15:26:51 +00:00
|
|
|
|
|
2019-11-24 00:30:51 +00:00
|
|
|
|
public abstract string GamePath { get; }
|
2019-11-03 15:26:51 +00:00
|
|
|
|
|
2019-11-24 00:30:51 +00:00
|
|
|
|
public abstract string ModListOutputFolder { get; }
|
|
|
|
|
public abstract string ModListOutputFile { get; }
|
2019-11-03 16:43:43 +00:00
|
|
|
|
|
2019-11-17 23:48:32 +00:00
|
|
|
|
public bool ShowReportWhenFinished { get; set; } = true;
|
|
|
|
|
|
2019-12-03 21:13:29 +00:00
|
|
|
|
public ICollection<Archive> SelectedArchives = new List<Archive>();
|
2019-11-15 05:47:31 +00:00
|
|
|
|
public List<Directive> InstallDirectives = new List<Directive>();
|
2019-11-15 13:06:34 +00:00
|
|
|
|
public List<RawSourceFile> AllFiles = new List<RawSourceFile>();
|
2019-11-15 05:47:31 +00:00
|
|
|
|
public ModList ModList = new ModList();
|
2019-11-15 14:19:39 +00:00
|
|
|
|
|
2019-11-15 13:06:34 +00:00
|
|
|
|
public List<IndexedArchive> IndexedArchives = new List<IndexedArchive>();
|
|
|
|
|
public Dictionary<string, IEnumerable<VirtualFile>> IndexedFiles = new Dictionary<string, IEnumerable<VirtualFile>>();
|
2019-11-03 15:26:51 +00:00
|
|
|
|
|
2019-11-17 15:00:33 +00:00
|
|
|
|
public void Info(string msg)
|
|
|
|
|
{
|
|
|
|
|
Utils.Log(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Status(string msg)
|
|
|
|
|
{
|
|
|
|
|
Queue.Report(msg, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Error(string msg)
|
|
|
|
|
{
|
|
|
|
|
Utils.Log(msg);
|
|
|
|
|
throw new Exception(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal string IncludeFile(byte[] data)
|
|
|
|
|
{
|
|
|
|
|
var id = Guid.NewGuid().ToString();
|
|
|
|
|
File.WriteAllBytes(Path.Combine(ModListOutputFolder, id), data);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal string IncludeFile(string data)
|
|
|
|
|
{
|
|
|
|
|
var id = Guid.NewGuid().ToString();
|
|
|
|
|
File.WriteAllText(Path.Combine(ModListOutputFolder, id), data);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ExportModList()
|
|
|
|
|
{
|
|
|
|
|
Utils.Log($"Exporting ModList to : {ModListOutputFile}");
|
|
|
|
|
|
2019-12-03 04:56:06 +00:00
|
|
|
|
// Modify readme and modlist image to relative paths if they exist
|
2019-12-03 20:08:58 +00:00
|
|
|
|
if (File.Exists(ModListImage))
|
2019-12-03 04:56:06 +00:00
|
|
|
|
{
|
|
|
|
|
ModList.Image = "modlist-image.png";
|
|
|
|
|
}
|
2019-12-03 20:08:58 +00:00
|
|
|
|
if (File.Exists(ModListReadme))
|
2019-12-03 04:56:06 +00:00
|
|
|
|
{
|
2019-12-03 20:08:58 +00:00
|
|
|
|
var readme = new FileInfo(ModListReadme);
|
2019-12-03 04:56:06 +00:00
|
|
|
|
ModList.Readme = $"readme{readme.Extension}";
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-04 03:56:20 +00:00
|
|
|
|
ModList.ReadmeIsWebsite = ReadmeIsWebsite;
|
2019-12-20 07:14:43 +00:00
|
|
|
|
|
2019-12-20 20:01:01 +00:00
|
|
|
|
ModList.ToCERAS(Path.Combine(ModListOutputFolder, "modlist"), CerasConfig.Config);
|
2019-11-17 15:00:33 +00:00
|
|
|
|
|
|
|
|
|
if (File.Exists(ModListOutputFile))
|
|
|
|
|
File.Delete(ModListOutputFile);
|
|
|
|
|
|
|
|
|
|
using (var fs = new FileStream(ModListOutputFile, FileMode.Create))
|
|
|
|
|
{
|
|
|
|
|
using (var za = new ZipArchive(fs, ZipArchiveMode.Create))
|
|
|
|
|
{
|
|
|
|
|
Directory.EnumerateFiles(ModListOutputFolder, "*.*")
|
|
|
|
|
.DoProgress("Compressing ModList",
|
|
|
|
|
f =>
|
|
|
|
|
{
|
|
|
|
|
var ze = za.CreateEntry(Path.GetFileName(f));
|
|
|
|
|
using (var os = ze.Open())
|
|
|
|
|
using (var ins = File.OpenRead(f))
|
|
|
|
|
{
|
|
|
|
|
ins.CopyTo(os);
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-12-03 04:56:06 +00:00
|
|
|
|
|
|
|
|
|
// Copy in modimage
|
2019-12-03 20:08:58 +00:00
|
|
|
|
if (File.Exists(ModListImage))
|
2019-12-03 04:56:06 +00:00
|
|
|
|
{
|
|
|
|
|
var ze = za.CreateEntry(ModList.Image);
|
|
|
|
|
using (var os = ze.Open())
|
|
|
|
|
using (var ins = File.OpenRead(ModListImage))
|
|
|
|
|
{
|
|
|
|
|
ins.CopyTo(os);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Copy in readme
|
2019-12-03 20:08:58 +00:00
|
|
|
|
if (File.Exists(ModListReadme))
|
2019-12-03 04:56:06 +00:00
|
|
|
|
{
|
|
|
|
|
var ze = za.CreateEntry(ModList.Readme);
|
|
|
|
|
using (var os = ze.Open())
|
|
|
|
|
using (var ins = File.OpenRead(ModListReadme))
|
|
|
|
|
{
|
|
|
|
|
ins.CopyTo(os);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-17 15:00:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils.Log("Exporting ModList metadata");
|
2019-11-29 05:52:33 +00:00
|
|
|
|
var metadata = new DownloadMetadata
|
2019-11-17 15:00:33 +00:00
|
|
|
|
{
|
|
|
|
|
Size = File.GetSize(ModListOutputFile),
|
|
|
|
|
Hash = ModListOutputFile.FileHash(),
|
|
|
|
|
NumberOfArchives = ModList.Archives.Count,
|
|
|
|
|
SizeOfArchives = ModList.Archives.Sum(a => a.Size),
|
|
|
|
|
NumberOfInstalledFiles = ModList.Directives.Count,
|
|
|
|
|
SizeOfInstalledFiles = ModList.Directives.Sum(a => a.Size)
|
|
|
|
|
};
|
|
|
|
|
metadata.ToJSON(ModListOutputFile + ".meta.json");
|
2019-11-03 15:26:51 +00:00
|
|
|
|
|
2019-11-17 15:00:33 +00:00
|
|
|
|
|
|
|
|
|
Utils.Log("Removing ModList staging folder");
|
2019-11-23 17:37:24 +00:00
|
|
|
|
Utils.DeleteDirectory(ModListOutputFolder);
|
2019-11-17 15:00:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ShowReport()
|
|
|
|
|
{
|
2019-11-17 23:48:32 +00:00
|
|
|
|
if (!ShowReportWhenFinished) return;
|
2019-11-17 15:00:33 +00:00
|
|
|
|
|
|
|
|
|
var file = Path.GetTempFileName() + ".html";
|
|
|
|
|
File.WriteAllText(file, ModList.ReportHTML);
|
|
|
|
|
Process.Start(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void GenerateReport()
|
|
|
|
|
{
|
|
|
|
|
string css;
|
2019-11-29 23:56:56 +00:00
|
|
|
|
using (var cssStream = Utils.GetEmbeddedResourceStream("Wabbajack.Lib.css-min.css"))
|
2019-11-17 15:00:33 +00:00
|
|
|
|
{
|
|
|
|
|
using (var reader = new StreamReader(cssStream))
|
|
|
|
|
{
|
|
|
|
|
css = reader.ReadToEnd();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var fs = File.OpenWrite($"{ModList.Name}.md"))
|
|
|
|
|
{
|
|
|
|
|
fs.SetLength(0);
|
|
|
|
|
using (var reporter = new ReportBuilder(fs, ModListOutputFolder))
|
|
|
|
|
{
|
|
|
|
|
reporter.Build(this, ModList);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ModList.ReportHTML = "<style>" + css + "</style>"
|
|
|
|
|
+ CommonMarkConverter.Convert(File.ReadAllText($"{ModList.Name}.md"));
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 01:26:26 +00:00
|
|
|
|
public async Task GatherArchives()
|
2019-11-17 15:00:33 +00:00
|
|
|
|
{
|
|
|
|
|
Info("Building a list of archives based on the files required");
|
|
|
|
|
|
|
|
|
|
var shas = InstallDirectives.OfType<FromArchive>()
|
|
|
|
|
.Select(a => a.ArchiveHashPath[0])
|
|
|
|
|
.Distinct();
|
|
|
|
|
|
|
|
|
|
var archives = IndexedArchives.OrderByDescending(f => f.File.LastModified)
|
|
|
|
|
.GroupBy(f => f.File.Hash)
|
|
|
|
|
.ToDictionary(f => f.Key, f => f.First());
|
|
|
|
|
|
2019-12-04 01:26:26 +00:00
|
|
|
|
SelectedArchives = await shas.PMap(Queue, sha => ResolveArchive(sha, archives));
|
2019-11-17 15:00:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 05:29:17 +00:00
|
|
|
|
public async Task<Archive> ResolveArchive(string sha, IDictionary<string, IndexedArchive> archives)
|
2019-11-17 15:00:33 +00:00
|
|
|
|
{
|
|
|
|
|
if (archives.TryGetValue(sha, out var found))
|
|
|
|
|
{
|
2019-12-13 00:40:21 +00:00
|
|
|
|
return await ResolveArchive(found);
|
2019-12-12 23:24:27 +00:00
|
|
|
|
}
|
2019-11-17 15:00:33 +00:00
|
|
|
|
|
2019-12-12 23:24:27 +00:00
|
|
|
|
Error($"No match found for Archive sha: {sha} this shouldn't happen");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-13 00:40:21 +00:00
|
|
|
|
public async Task<Archive> ResolveArchive(IndexedArchive archive)
|
2019-12-12 23:24:27 +00:00
|
|
|
|
{
|
2019-12-22 18:14:48 +00:00
|
|
|
|
Utils.Status($"Checking link for {archive.Name}", alsoLog: true);
|
|
|
|
|
|
2019-12-12 23:24:27 +00:00
|
|
|
|
if (archive.IniData == null)
|
|
|
|
|
Error(
|
|
|
|
|
$"No download metadata found for {archive.Name}, please use MO2 to query info or add a .meta file and try again.");
|
2019-11-17 15:00:33 +00:00
|
|
|
|
|
2019-12-12 23:24:27 +00:00
|
|
|
|
var result = new Archive
|
|
|
|
|
{
|
2019-12-23 00:03:45 +00:00
|
|
|
|
State = await DownloadDispatcher.ResolveArchive(archive.IniData)
|
2019-12-12 23:24:27 +00:00
|
|
|
|
};
|
2019-11-17 15:00:33 +00:00
|
|
|
|
|
2019-12-12 23:24:27 +00:00
|
|
|
|
if (result.State == null)
|
|
|
|
|
Error($"{archive.Name} could not be handled by any of the downloaders");
|
2019-11-17 15:00:33 +00:00
|
|
|
|
|
2019-12-12 23:24:27 +00:00
|
|
|
|
result.Name = archive.Name;
|
|
|
|
|
result.Hash = archive.File.Hash;
|
|
|
|
|
result.Meta = archive.Meta;
|
|
|
|
|
result.Size = archive.File.Size;
|
2019-11-17 15:00:33 +00:00
|
|
|
|
|
2019-12-13 00:40:21 +00:00
|
|
|
|
if (result.State != null && !await result.State.Verify())
|
2019-12-12 23:24:27 +00:00
|
|
|
|
Error(
|
|
|
|
|
$"Unable to resolve link for {archive.Name}. If this is hosted on the Nexus the file may have been removed.");
|
2019-11-17 15:00:33 +00:00
|
|
|
|
|
2019-12-12 23:24:27 +00:00
|
|
|
|
return result;
|
2019-11-17 15:00:33 +00:00
|
|
|
|
}
|
2019-11-03 15:26:51 +00:00
|
|
|
|
|
2019-12-04 01:26:26 +00:00
|
|
|
|
public async Task<Directive> RunStack(IEnumerable<ICompilationStep> stack, RawSourceFile source)
|
2019-11-17 15:00:33 +00:00
|
|
|
|
{
|
|
|
|
|
Utils.Status($"Compiling {source.Path}");
|
|
|
|
|
foreach (var step in stack)
|
|
|
|
|
{
|
2019-12-04 01:26:26 +00:00
|
|
|
|
var result = await step.Run(source);
|
2019-11-17 15:00:33 +00:00
|
|
|
|
if (result != null) return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new InvalidDataException("Data fell out of the compilation stack");
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-03 15:26:51 +00:00
|
|
|
|
public abstract IEnumerable<ICompilationStep> GetStack();
|
|
|
|
|
public abstract IEnumerable<ICompilationStep> MakeStack();
|
|
|
|
|
}
|
|
|
|
|
}
|