2020-02-05 05:17:12 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
using System.Linq;
|
2019-12-04 01:26:26 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
using Alphaleonis.Win32.Filesystem;
|
2019-10-31 02:24:42 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
using Wabbajack.Common;
|
2019-11-15 13:06:34 +00:00
|
|
|
|
using Wabbajack.VirtualFileSystem;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Lib.CompilationSteps
|
|
|
|
|
{
|
|
|
|
|
public class IncludePatches : ACompilationStep
|
|
|
|
|
{
|
2020-03-25 22:30:43 +00:00
|
|
|
|
private readonly Dictionary<RelativePath, IGrouping<RelativePath, VirtualFile>> _indexed;
|
2020-04-09 18:14:05 +00:00
|
|
|
|
private VirtualFile? _bsa;
|
2020-03-25 22:30:43 +00:00
|
|
|
|
private Dictionary<RelativePath, VirtualFile> _indexedByName;
|
2020-03-28 18:22:53 +00:00
|
|
|
|
private MO2Compiler _mo2Compiler;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
|
2020-04-09 18:14:05 +00:00
|
|
|
|
public IncludePatches(ACompiler compiler, VirtualFile? constructingFromBSA = null) : base(compiler)
|
2019-10-30 12:29:06 +00:00
|
|
|
|
{
|
2020-02-05 05:17:12 +00:00
|
|
|
|
_bsa = constructingFromBSA;
|
2020-03-28 18:22:53 +00:00
|
|
|
|
_mo2Compiler = (MO2Compiler)compiler;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
_indexed = _compiler.IndexedFiles.Values
|
|
|
|
|
.SelectMany(f => f)
|
2020-03-25 22:30:43 +00:00
|
|
|
|
.GroupBy(f => f.Name.FileName)
|
2019-10-30 12:29:06 +00:00
|
|
|
|
.ToDictionary(f => f.Key);
|
2020-02-05 05:17:12 +00:00
|
|
|
|
_indexedByName = _indexed.Values
|
|
|
|
|
.SelectMany(s => s)
|
|
|
|
|
.Where(f => f.IsNative)
|
2020-03-25 22:30:43 +00:00
|
|
|
|
.ToDictionary(f => f.FullPath.FileName);
|
2019-10-30 12:29:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 18:14:05 +00:00
|
|
|
|
public override async ValueTask<Directive?> Run(RawSourceFile source)
|
2019-10-30 12:29:06 +00:00
|
|
|
|
{
|
2020-03-25 22:30:43 +00:00
|
|
|
|
var name = source.File.Name.FileName;
|
|
|
|
|
RelativePath nameWithoutExt = name;
|
|
|
|
|
if (name.Extension == Consts.MOHIDDEN)
|
|
|
|
|
nameWithoutExt = name.FileNameWithoutExtension;
|
2020-02-05 05:17:12 +00:00
|
|
|
|
|
2020-03-25 22:30:43 +00:00
|
|
|
|
if (!_indexed.TryGetValue(name, out var choices))
|
|
|
|
|
_indexed.TryGetValue(nameWithoutExt, out choices);
|
2019-10-30 12:29:06 +00:00
|
|
|
|
|
2020-04-09 18:14:05 +00:00
|
|
|
|
dynamic? modIni = null;
|
2020-04-22 20:58:50 +00:00
|
|
|
|
|
|
|
|
|
if (_bsa == null && source.File.IsNative && source.AbsolutePath.InFolder(_mo2Compiler.MO2ModsFolder))
|
|
|
|
|
((MO2Compiler)_compiler).ModInis.TryGetValue(ModForFile(source.AbsolutePath), out modIni);
|
|
|
|
|
else if (_bsa != null)
|
2020-02-05 05:17:12 +00:00
|
|
|
|
{
|
2020-04-22 20:58:50 +00:00
|
|
|
|
var bsaPath = _bsa.FullPath.Base;
|
|
|
|
|
((MO2Compiler)_compiler).ModInis.TryGetValue(ModForFile(bsaPath), out modIni);
|
2020-02-05 05:17:12 +00:00
|
|
|
|
}
|
2019-11-21 21:32:58 +00:00
|
|
|
|
|
2020-04-21 22:58:42 +00:00
|
|
|
|
var installationFile = (string?)modIni?.General?.installationFile;
|
2019-11-21 21:32:58 +00:00
|
|
|
|
|
2020-04-09 18:14:05 +00:00
|
|
|
|
VirtualFile? found = null;
|
2020-02-05 05:17:12 +00:00
|
|
|
|
|
|
|
|
|
// Find based on exact file name + ext
|
2020-04-21 22:58:42 +00:00
|
|
|
|
if (choices != null && installationFile != null)
|
2020-02-05 05:17:12 +00:00
|
|
|
|
{
|
2020-04-21 22:58:42 +00:00
|
|
|
|
var relName = (RelativePath)Path.GetFileName(installationFile);
|
2020-02-05 05:17:12 +00:00
|
|
|
|
found = choices.FirstOrDefault(
|
2020-04-21 22:58:42 +00:00
|
|
|
|
f => f.FilesInFullPath.First().Name.FileName == relName);
|
2020-02-05 05:17:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find based on file name only (not ext)
|
|
|
|
|
if (found == null && choices != null)
|
2019-11-21 21:32:58 +00:00
|
|
|
|
{
|
|
|
|
|
found = choices.OrderBy(f => f.NestingFactor)
|
|
|
|
|
.ThenByDescending(f => (f.FilesInFullPath.First() ?? f).LastModified)
|
|
|
|
|
.First();
|
|
|
|
|
}
|
2019-10-30 12:29:06 +00:00
|
|
|
|
|
2020-02-05 05:17:12 +00:00
|
|
|
|
// Find based on matchAll=<archivename> in [General] in meta.ini
|
2020-04-21 22:58:42 +00:00
|
|
|
|
var matchAllName = (string?)modIni?.General?.matchAll;
|
2020-02-05 05:17:12 +00:00
|
|
|
|
if (matchAllName != null)
|
|
|
|
|
{
|
2020-04-21 22:58:42 +00:00
|
|
|
|
var relName = (RelativePath)Path.GetFileName(matchAllName);
|
|
|
|
|
if (_indexedByName.TryGetValue(relName, out var arch))
|
2020-02-05 05:17:12 +00:00
|
|
|
|
{
|
|
|
|
|
// Just match some file in the archive based on the smallest delta difference
|
|
|
|
|
found = arch.ThisAndAllChildren
|
|
|
|
|
.OrderBy(o => Math.Abs(o.Size - source.File.Size))
|
|
|
|
|
.First();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (found == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2019-10-30 12:29:06 +00:00
|
|
|
|
var e = source.EvolveTo<PatchedFromArchive>();
|
2019-11-22 05:19:42 +00:00
|
|
|
|
e.FromHash = found.Hash;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
e.ArchiveHashPath = found.MakeRelativePaths();
|
|
|
|
|
e.To = source.Path;
|
|
|
|
|
e.Hash = source.File.Hash;
|
|
|
|
|
|
2020-04-04 04:02:53 +00:00
|
|
|
|
if (Utils.TryGetPatch(found.Hash, source.File.Hash, out var data))
|
2020-03-25 22:30:43 +00:00
|
|
|
|
e.PatchID = await _compiler.IncludeFile(data);
|
2019-10-30 12:29:06 +00:00
|
|
|
|
|
|
|
|
|
return e;
|
|
|
|
|
}
|
2019-10-31 02:24:42 +00:00
|
|
|
|
|
2020-03-28 18:22:53 +00:00
|
|
|
|
private AbsolutePath ModForFile(AbsolutePath file)
|
|
|
|
|
{
|
|
|
|
|
return file.RelativeTo(((MO2Compiler)_compiler).MO2ModsFolder).TopParent
|
|
|
|
|
.RelativeTo(((MO2Compiler)_compiler).MO2ModsFolder);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 02:24:42 +00:00
|
|
|
|
public override IState GetState()
|
|
|
|
|
{
|
|
|
|
|
return new State();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[JsonObject("IncludePatches")]
|
|
|
|
|
public class State : IState
|
|
|
|
|
{
|
2019-11-03 16:45:49 +00:00
|
|
|
|
public ICompilationStep CreateStep(ACompiler compiler)
|
2019-10-31 02:24:42 +00:00
|
|
|
|
{
|
|
|
|
|
return new IncludePatches(compiler);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-30 12:29:06 +00:00
|
|
|
|
}
|
2019-11-21 21:32:58 +00:00
|
|
|
|
}
|