wabbajack/Wabbajack.Lib/CompilationSteps/IncludePatches.cs

127 lines
4.7 KiB
C#
Raw Normal View History

2020-02-05 05:17:12 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Alphaleonis.Win32.Filesystem;
using F23.StringSimilarity;
using Newtonsoft.Json;
using Wabbajack.Common;
2019-11-15 13:06:34 +00:00
using Wabbajack.VirtualFileSystem;
namespace Wabbajack.Lib.CompilationSteps
{
public class IncludePatches : ACompilationStep
{
2020-03-25 22:30:43 +00:00
private readonly Dictionary<RelativePath, IGrouping<RelativePath, VirtualFile>> _indexed;
private VirtualFile? _bsa;
2020-06-20 22:51:47 +00:00
private Dictionary<RelativePath, IEnumerable<VirtualFile>> _indexedByName;
2020-03-28 18:22:53 +00:00
private MO2Compiler _mo2Compiler;
private bool _isGenericGame;
public IncludePatches(ACompiler compiler, VirtualFile? constructingFromBSA = null) : base(compiler)
{
2020-02-05 05:17:12 +00:00
_bsa = constructingFromBSA;
2020-03-28 18:22:53 +00:00
_mo2Compiler = (MO2Compiler)compiler;
_indexed = _compiler.IndexedFiles.Values
.SelectMany(f => f)
2020-03-25 22:30:43 +00:00
.GroupBy(f => f.Name.FileName)
.ToDictionary(f => f.Key);
2020-02-05 05:17:12 +00:00
_indexedByName = _indexed.Values
.SelectMany(s => s)
.Where(f => f.IsNative)
2020-07-10 22:59:39 +00:00
.GroupBy(f => f.Name.FileName)
2020-06-20 22:51:47 +00:00
.ToDictionary(f => f.Key, f => (IEnumerable<VirtualFile>)f);
_isGenericGame = _mo2Compiler.CompilingGame.IsGenericMO2Plugin;
}
public override async ValueTask<Directive?> Run(RawSourceFile source)
{
if (_isGenericGame)
{
if (source.Path.StartsWith(Consts.GameFolderFilesDir))
return null;
}
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);
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
var installationFile = (string?)modIni?.General?.installationFile;
2019-11-21 21:32:58 +00:00
VirtualFile[] found = {};
2020-02-05 05:17:12 +00:00
// Find based on exact file name + ext
if (choices != null && installationFile != null)
2020-02-05 05:17:12 +00:00
{
var relName = (RelativePath)Path.GetFileName(installationFile);
found = choices.Where(f => f.FilesInFullPath.First().Name.FileName == relName).ToArray();
2020-02-05 05:17:12 +00:00
}
// Find based on file name only (not ext)
if (found.Length == 0 && choices != null)
2019-11-21 21:32:58 +00:00
{
found = choices.ToArray();
2019-11-21 21:32:58 +00:00
}
2020-02-05 05:17:12 +00:00
// Find based on matchAll=<archivename> in [General] in meta.ini
var matchAllName = (string?)modIni?.General?.matchAll;
if (matchAllName != null && found.Length == 0)
2020-02-05 05:17:12 +00:00
{
var relName = (RelativePath)Path.GetFileName(matchAllName);
if (_indexedByName.TryGetValue(relName, out var arch))
2020-02-05 05:17:12 +00:00
{
var dist = new Levenshtein();
found = arch.SelectMany(a => a.ThisAndAllChildren)
2020-07-10 22:59:39 +00:00
.OrderBy(a => dist.Distance(a.Name.FileName.ToString(), source.File.Name.FileName.ToString()))
.Take(3)
.ToArray();
2020-02-05 05:17:12 +00:00
}
}
if (found.Length == 0)
2020-02-05 05:17:12 +00:00
return null;
var e = source.EvolveTo<PatchedFromArchive>();
var patches = found.Select(c => (Utils.TryGetPatch(c.Hash, source.File.Hash, out var data), data, c))
.ToArray();
if (patches.All(p => p.Item1))
2020-06-02 03:41:34 +00:00
{
var (_, bytes, file) = patches.OrderBy(f => f.data!.Length).First();
e.FromHash = file.Hash;
e.ArchiveHashPath = file.MakeRelativePaths();
e.PatchID = await _compiler.IncludeFile(bytes!);
2020-06-02 03:41:34 +00:00
}
else
{
e.Choices = found;
}
return e;
}
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-11-21 21:32:58 +00:00
}