From a76aff7962cbd3531703c547b87c86b961db3791 Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Mon, 6 Jan 2020 17:24:33 -0700 Subject: [PATCH] Add override for Game Folder Files --- .../IgnoreGameFilesIfGameFolderFilesExist.cs | 46 +++++++++++++++++++ Wabbajack.Lib/MO2Compiler.cs | 1 + Wabbajack.Lib/Wabbajack.Lib.csproj | 1 + Wabbajack.Test/SanityTests.cs | 38 +++++++++++++++ Wabbajack.Test/TestUtils.cs | 29 ++++++++++++ 5 files changed, 115 insertions(+) create mode 100644 Wabbajack.Lib/CompilationSteps/IgnoreGameFilesIfGameFolderFilesExist.cs diff --git a/Wabbajack.Lib/CompilationSteps/IgnoreGameFilesIfGameFolderFilesExist.cs b/Wabbajack.Lib/CompilationSteps/IgnoreGameFilesIfGameFolderFilesExist.cs new file mode 100644 index 00000000..c2dc1cdb --- /dev/null +++ b/Wabbajack.Lib/CompilationSteps/IgnoreGameFilesIfGameFolderFilesExist.cs @@ -0,0 +1,46 @@ +using System.Threading.Tasks; +using Alphaleonis.Win32.Filesystem; +using Wabbajack.Common; + +namespace Wabbajack.Lib.CompilationSteps +{ + public class IgnoreGameFilesIfGameFolderFilesExist : ACompilationStep + { + private readonly bool _gameFolderFilesExists; + private readonly string _gameFolder; + + public IgnoreGameFilesIfGameFolderFilesExist(ACompiler compiler) : base(compiler) + { + _gameFolderFilesExists = Directory.Exists(Path.Combine(((MO2Compiler)compiler).MO2Folder, Consts.GameFolderFilesDir)); + _gameFolder = compiler.GamePath; + } + + public override async ValueTask Run(RawSourceFile source) + { + if (_gameFolderFilesExists) + { + if (source.AbsolutePath.IsInPath(_gameFolder)) + { + var result = source.EvolveTo(); + result.Reason = $"Ignoring game files because {Consts.GameFolderFilesDir} exists"; + return result; + } + } + + return null; + } + + public override IState GetState() + { + return new State(); + } + + public class State : IState + { + public ICompilationStep CreateStep(ACompiler compiler) + { + return new IgnoreGameFilesIfGameFolderFilesExist(compiler); + } + } + } +} diff --git a/Wabbajack.Lib/MO2Compiler.cs b/Wabbajack.Lib/MO2Compiler.cs index 0c02b076..679f1b20 100644 --- a/Wabbajack.Lib/MO2Compiler.cs +++ b/Wabbajack.Lib/MO2Compiler.cs @@ -496,6 +496,7 @@ namespace Wabbajack.Lib Utils.Log("Generating compilation stack"); return new List { + new IgnoreGameFilesIfGameFolderFilesExist(this), new IncludePropertyFiles(this), new IgnoreStartsWith(this,"logs\\"), new IgnoreStartsWith(this, "downloads\\"), diff --git a/Wabbajack.Lib/Wabbajack.Lib.csproj b/Wabbajack.Lib/Wabbajack.Lib.csproj index 8318f13e..d311effd 100644 --- a/Wabbajack.Lib/Wabbajack.Lib.csproj +++ b/Wabbajack.Lib/Wabbajack.Lib.csproj @@ -101,6 +101,7 @@ + diff --git a/Wabbajack.Test/SanityTests.cs b/Wabbajack.Test/SanityTests.cs index e2f9c732..053c28c6 100644 --- a/Wabbajack.Test/SanityTests.cs +++ b/Wabbajack.Test/SanityTests.cs @@ -32,6 +32,44 @@ namespace Wabbajack.Test utils.VerifyInstalledFile(mod, @"Data\scripts\test.pex"); } + + [TestMethod] + public async Task TestDirectMatchFromGameFolder() + { + + var profile = utils.AddProfile(); + var mod = utils.AddMod(); + var test_pex = utils.AddGameFile(@"enbstuff\test.pex", 10); + + utils.Configure(); + + utils.AddManualDownload( + new Dictionary {{"/baz/biz.pex", File.ReadAllBytes(test_pex)}}); + + await CompileAndInstall(profile); + + utils.VerifyInstalledGameFile(@"enbstuff\test.pex"); + } + + [TestMethod] + public async Task TestDirectMatchIsIgnoredWhenGameFolderFilesOverrideExists() + { + + var profile = utils.AddProfile(); + var mod = utils.AddMod(); + var test_pex = utils.AddGameFile(@"enbstuff\test.pex", 10); + + utils.Configure(); + + Directory.CreateDirectory(Path.Combine(utils.MO2Folder, Consts.GameFolderFilesDir)); + + utils.AddManualDownload( + new Dictionary {{"/baz/biz.pex", File.ReadAllBytes(test_pex)}}); + + await CompileAndInstall(profile); + + Assert.IsFalse(File.Exists(Path.Combine(utils.InstallFolder, Consts.GameFolderFilesDir, @"enbstuff\test.pex"))); + } [TestMethod] public async Task TestDuplicateFilesAreCopied() diff --git a/Wabbajack.Test/TestUtils.cs b/Wabbajack.Test/TestUtils.cs index d44b453b..1ad33df3 100644 --- a/Wabbajack.Test/TestUtils.cs +++ b/Wabbajack.Test/TestUtils.cs @@ -177,7 +177,26 @@ namespace Wabbajack.Test Assert.Fail($"Index {x} of {mod}\\{file} are not the same"); } } + + public void VerifyInstalledGameFile(string file) + { + var src = Path.Combine(GameFolder, file); + Assert.IsTrue(File.Exists(src), src); + var dest = Path.Combine(InstallFolder, Consts.GameFolderFilesDir, file); + Assert.IsTrue(File.Exists(dest), dest); + + var src_data = File.ReadAllBytes(src); + var dest_data = File.ReadAllBytes(dest); + + Assert.AreEqual(src_data.Length, dest_data.Length); + + for(int x = 0; x < src_data.Length; x++) + { + if (src_data[x] != dest_data[x]) + Assert.Fail($"Index {x} of {Consts.GameFolderFilesDir}\\{file} are not the same"); + } + } public string PathOfInstalledFile(string mod, string file) { return Path.Combine(InstallFolder, "mods", mod, file); @@ -218,5 +237,15 @@ namespace Wabbajack.Test } } } + + public string AddGameFile(string path, int i) + { + var full_path = Path.Combine(GameFolder, path); + var dir = Path.GetDirectoryName(full_path); + if (!Directory.Exists(dir)) + Directory.CreateDirectory(dir); + GenerateRandomFileData(full_path, i); + return full_path; + } } }