From 878eff002cb022298f652f920c81db4ea2d9ac5c Mon Sep 17 00:00:00 2001 From: erri120 Date: Sat, 4 Apr 2020 18:04:42 +0200 Subject: [PATCH] Updated zEditIntegration merges.json detection --- Wabbajack.Lib/zEditIntegration.cs | 78 +++++++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 8 deletions(-) diff --git a/Wabbajack.Lib/zEditIntegration.cs b/Wabbajack.Lib/zEditIntegration.cs index 54dedf18..ba88104c 100644 --- a/Wabbajack.Lib/zEditIntegration.cs +++ b/Wabbajack.Lib/zEditIntegration.cs @@ -1,5 +1,4 @@ using Alphaleonis.Win32.Filesystem; -using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -37,26 +36,78 @@ namespace Wabbajack.Lib public class IncludeZEditPatches : ACompilationStep { - private Dictionary _mergesIndexed; + private readonly Dictionary _mergesIndexed; + + private bool _disabled = true; public IncludeZEditPatches(ACompiler compiler) : base(compiler) { var zEditPath = FindzEditPath(compiler); var havezEdit = zEditPath != null; - Utils.Log(havezEdit ? $"Found zEdit at {zEditPath}" : $"zEdit not detected, disabling zEdit routines"); + Utils.Log(havezEdit ? $"Found zEdit at {zEditPath}" : "zEdit not detected, disabling zEdit routines"); if (!havezEdit) { _mergesIndexed = new Dictionary(); return; } - - var merges = Directory.EnumerateFiles(Path.Combine(zEditPath, "profiles"), + _mo2Compiler = (MO2Compiler) compiler; + + var settingsFiles = Directory.EnumerateFiles(Path.Combine(zEditPath, "profiles"), DirectoryEnumerationOptions.Files | DirectoryEnumerationOptions.Recursive) - .Where(f => f.EndsWith("\\merges.json")) - .SelectMany(f => f.FromJSON>()) - .GroupBy(f => (f.name, f.filename)); + .Where(f => f.EndsWith("settings.json")) + .Where(f => + { + var settings = f.FromJSON(); + + if (settings.modManager != "Mod Organizer 2") + { + Utils.Log($"zEdit settings file {f}: modManager is not Mod Organizer 2 but {settings.modManager}!"); + return false; + } + + if (settings.managerPath != _mo2Compiler.MO2Folder) + { + Utils.Log($"zEdit settings file {f}: managerPath is not {_mo2Compiler.MO2Folder} but {settings.managerPath}!"); + return false; + } + + if (settings.modsPath != Path.Combine(_mo2Compiler.MO2Folder, "mods")) + { + Utils.Log($"zEdit settings file {f}: modsPath is not {_mo2Compiler.MO2Folder}\\mods but {settings.modsPath}!"); + return false; + } + + if (settings.mergePath != Path.Combine(_mo2Compiler.MO2Folder, "mods")) + { + Utils.Log($"zEdit settings file {f}: modsPath is not {_mo2Compiler.MO2Folder}\\mods but {settings.modsPath}!"); + return false; + } + + return true; + }); + + if (!settingsFiles.Any()) + { + Utils.Log($"Found not acceptable settings.json file for zEdit!"); + return; + } + + var profileFolder = + settingsFiles.Where(x => File.Exists(Path.Combine(Path.GetDirectoryName(x), "merges.json")))?.Select(x => string.IsNullOrWhiteSpace(x) ? "" : Path.GetDirectoryName(x)).FirstOrDefault(); + + if (string.IsNullOrWhiteSpace(profileFolder)) + { + Utils.Log("Found no acceptable profiles folder for zEdit!"); + return; + } + + var mergeFile = Path.Combine(profileFolder, "merges.json"); + + Utils.Log($"Using merge file {mergeFile}"); + + var merges = mergeFile.FromJSON>().GroupBy(f => (f.name, f.filename)); merges.Where(m => m.Count() > 1) .Do(m => @@ -69,10 +120,13 @@ namespace Wabbajack.Lib merges.ToDictionary( m => Path.Combine(_mo2Compiler.MO2Folder, Consts.MO2ModFolderName, m.Key.name, m.Key.filename), m => m.First()); + + _disabled = false; } public override async ValueTask Run(RawSourceFile source) { + if (_disabled) return null; if (!_mergesIndexed.TryGetValue(source.AbsolutePath, out var merge)) return null; var result = source.EvolveTo(); result.Sources = merge.plugins.Select(f => @@ -138,6 +192,14 @@ namespace Wabbajack.Lib } } + public class zEditSettings + { + public string modManager; + public string managerPath; + public string modsPath; + public string mergePath; + } + public class zEditMerge { public string name;