From 1ee439c7c160de2a39ab51642aa9b6c4c4b96729 Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Thu, 21 Nov 2019 14:32:58 -0700 Subject: [PATCH] Better selection of patch file sources --- Wabbajack.Common/Utils.cs | 13 +++++++++++++ .../CompilationSteps/IncludePatches.cs | 17 ++++++++++++++--- Wabbajack.Lib/MO2Compiler.cs | 9 +++++++++ Wabbajack.Test/SanityTests.cs | 10 +++++++++- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/Wabbajack.Common/Utils.cs b/Wabbajack.Common/Utils.cs index c997b93e..995af435 100644 --- a/Wabbajack.Common/Utils.cs +++ b/Wabbajack.Common/Utils.cs @@ -420,6 +420,19 @@ namespace Wabbajack.Common } } + /// + /// A combination of .Select(func).Where(v => v != default). So select and filter default values. + /// + /// + /// + /// + /// + /// + public static IEnumerable Keep(this IEnumerable coll, Func func) where TOut : IComparable + { + return coll.Select(func).Where(v => v.CompareTo(default) != 0); + } + public static byte[] ReadAll(this Stream ins) { using (var ms = new MemoryStream()) diff --git a/Wabbajack.Lib/CompilationSteps/IncludePatches.cs b/Wabbajack.Lib/CompilationSteps/IncludePatches.cs index 761047cd..469b5080 100644 --- a/Wabbajack.Lib/CompilationSteps/IncludePatches.cs +++ b/Wabbajack.Lib/CompilationSteps/IncludePatches.cs @@ -21,10 +21,21 @@ namespace Wabbajack.Lib.CompilationSteps public override Directive Run(RawSourceFile source) { - if (!_indexed.TryGetValue(Path.GetFileName(source.File.Name.ToLower()), out var value)) + if (!_indexed.TryGetValue(Path.GetFileName(source.File.Name.ToLower()), out var choices)) return null; - var found = value.OrderByDescending(f => (f.FilesInFullPath.First() ?? f).LastModified).First(); + var mod_ini = ((MO2Compiler)_compiler).ModMetas.FirstOrDefault(f => source.AbsolutePath.StartsWith(f.Key)); + var installationFile = mod_ini.Value?.General?.installationFile; + + var found = choices.FirstOrDefault( + f => Path.GetFileName(f.FilesInFullPath.First().Name) == installationFile); + + if (found == null) + { + found = choices.OrderBy(f => f.NestingFactor) + .ThenByDescending(f => (f.FilesInFullPath.First() ?? f).LastModified) + .First(); + } var e = source.EvolveTo(); e.ArchiveHashPath = found.MakeRelativePaths(); @@ -53,4 +64,4 @@ namespace Wabbajack.Lib.CompilationSteps } } } -} \ No newline at end of file +} diff --git a/Wabbajack.Lib/MO2Compiler.cs b/Wabbajack.Lib/MO2Compiler.cs index bd16c1a6..406b983a 100644 --- a/Wabbajack.Lib/MO2Compiler.cs +++ b/Wabbajack.Lib/MO2Compiler.cs @@ -23,6 +23,7 @@ namespace Wabbajack.Lib public string MO2Folder; public string MO2Profile; + public Dictionary ModMetas { get; set; } public MO2Compiler(string mo2Folder) { @@ -142,6 +143,13 @@ namespace Wabbajack.Lib }) .ToList(); + ModMetas = Directory.EnumerateDirectories(Path.Combine(MO2Folder, "mods")) + .Keep(f => + { + var path = Path.Combine(f, "meta.ini"); + return File.Exists(path) ? (f, path.LoadIniFile()) : default; + }).ToDictionary(f => f.f + "\\", v => v.Item2); + IndexedFiles = IndexedArchives.SelectMany(f => f.File.ThisAndAllChildren) .OrderBy(f => f.NestingFactor) .GroupBy(f => f.Hash) @@ -254,6 +262,7 @@ namespace Wabbajack.Lib return true; } + private void IncludeArchiveMetadata() { Utils.Log($"Including {SelectedArchives.Count} .meta files for downloads"); diff --git a/Wabbajack.Test/SanityTests.cs b/Wabbajack.Test/SanityTests.cs index b3f66c52..a7407301 100644 --- a/Wabbajack.Test/SanityTests.cs +++ b/Wabbajack.Test/SanityTests.cs @@ -156,12 +156,20 @@ namespace Wabbajack.Test var profile = utils.AddProfile(); var mod = utils.AddMod(); var ini = utils.AddModFile(mod, @"foo.ini", 10); + var meta = utils.AddModFile(mod, "meta.ini"); + utils.Configure(); - utils.AddManualDownload( + var archive = utils.AddManualDownload( new Dictionary { { "/baz/foo.ini", File.ReadAllBytes(ini) } }); + File.WriteAllLines(meta, new[] + { + "[General]", + $"installationFile={archive}", + }); + // Modify after creating mod archive in the downloads folder File.WriteAllText(ini, "Wabbajack, Wabbajack, Wabbajack!");