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;
|
2020-07-01 12:07:02 +00:00
|
|
|
|
using F23.StringSimilarity;
|
2019-10-31 02:24:42 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
using Wabbajack.Common;
|
2020-08-18 22:30:00 +00:00
|
|
|
|
using Wabbajack.Lib.Downloaders;
|
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-06-20 22:51:47 +00:00
|
|
|
|
private Dictionary<RelativePath, IEnumerable<VirtualFile>> _indexedByName;
|
2020-07-05 10:44:46 +00:00
|
|
|
|
private bool _isGenericGame;
|
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-10-19 00:15:22 +00:00
|
|
|
|
_compiler = 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-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);
|
2020-07-05 10:44:46 +00:00
|
|
|
|
|
2020-10-19 00:15:22 +00:00
|
|
|
|
_isGenericGame = _compiler.CompilingGame.IsGenericMO2Plugin;
|
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-07-05 10:44:46 +00:00
|
|
|
|
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);
|
2019-10-30 12:29:06 +00:00
|
|
|
|
|
2020-04-09 18:14:05 +00:00
|
|
|
|
dynamic? modIni = null;
|
2020-10-19 00:15:22 +00:00
|
|
|
|
|
|
|
|
|
if (_compiler is MO2Compiler)
|
2020-02-05 05:17:12 +00:00
|
|
|
|
{
|
2020-10-19 00:15:22 +00:00
|
|
|
|
if (_bsa == null && source.File.IsNative && source.AbsolutePath.InFolder(((MO2Compiler)_compiler).MO2ModsFolder))
|
|
|
|
|
((MO2Compiler)_compiler).ModInis.TryGetValue(ModForFile(source.AbsolutePath), out modIni);
|
|
|
|
|
else if (_bsa != null)
|
|
|
|
|
{
|
|
|
|
|
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-06-30 23:09:59 +00:00
|
|
|
|
VirtualFile[] found = {};
|
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-06-30 23:09:59 +00:00
|
|
|
|
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)
|
2020-06-30 23:09:59 +00:00
|
|
|
|
if (found.Length == 0 && choices != null)
|
2019-11-21 21:32:58 +00:00
|
|
|
|
{
|
2020-06-30 23:09:59 +00:00
|
|
|
|
found = choices.ToArray();
|
2019-11-21 21:32:58 +00:00
|
|
|
|
}
|
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-06-30 23:09:59 +00:00
|
|
|
|
if (matchAllName != null && found.Length == 0)
|
2020-02-05 05:17:12 +00:00
|
|
|
|
{
|
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
|
|
|
|
{
|
2020-07-01 12:07:02 +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()))
|
2020-07-01 12:07:02 +00:00
|
|
|
|
.Take(3)
|
|
|
|
|
.ToArray();
|
2020-02-05 05:17:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-30 23:09:59 +00:00
|
|
|
|
if (found.Length == 0)
|
2020-02-05 05:17:12 +00:00
|
|
|
|
return null;
|
|
|
|
|
|
2020-06-30 23:09:59 +00:00
|
|
|
|
|
2019-10-30 12:29:06 +00:00
|
|
|
|
var e = source.EvolveTo<PatchedFromArchive>();
|
|
|
|
|
|
2020-06-30 23:09:59 +00:00
|
|
|
|
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
|
|
|
|
{
|
2020-10-19 00:15:22 +00:00
|
|
|
|
var (_, bytes, file) = PickPatch(_compiler, patches);
|
2020-06-30 23:09:59 +00:00
|
|
|
|
e.FromHash = file.Hash;
|
|
|
|
|
e.ArchiveHashPath = file.MakeRelativePaths();
|
2021-01-15 14:00:58 +00:00
|
|
|
|
e.PatchID = await _compiler.IncludeFile(await bytes!.GetData());
|
2020-06-02 03:41:34 +00:00
|
|
|
|
}
|
2020-06-30 23:09:59 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
e.Choices = found;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-15 04:20:56 +00:00
|
|
|
|
if (source.File.IsNative && await VirusScanner.ShouldScan(source.File.AbsoluteName))
|
|
|
|
|
{
|
|
|
|
|
if (await ClientAPI.GetVirusScanResult(source.File.AbsoluteName) == VirusScanner.Result.Malware)
|
|
|
|
|
{
|
|
|
|
|
Utils.ErrorThrow(new Exception($"Executable file {source.File.AbsoluteName} ({source.File}) has been marked as malware."));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 12:29:06 +00:00
|
|
|
|
return e;
|
|
|
|
|
}
|
2019-10-31 02:24:42 +00:00
|
|
|
|
|
2021-01-15 14:00:58 +00:00
|
|
|
|
public static (bool, PatchCache.CacheEntry, VirtualFile) PickPatch(ACompiler compiler, IEnumerable<(bool foundHash, PatchCache.CacheEntry? data, VirtualFile file)> patches)
|
2020-08-18 22:30:00 +00:00
|
|
|
|
{
|
|
|
|
|
var ordered = patches
|
|
|
|
|
.Select(f => (f.foundHash, f.data!, f.file))
|
2021-01-15 14:00:58 +00:00
|
|
|
|
.OrderBy(f => f.Item2.PatchSize)
|
2020-08-18 22:30:00 +00:00
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
var primaryChoice = ordered.FirstOrDefault(itm =>
|
|
|
|
|
{
|
|
|
|
|
var baseHash = itm.file.TopParent.Hash;
|
|
|
|
|
|
|
|
|
|
// If this file doesn't come from a game use it
|
2020-10-18 13:16:27 +00:00
|
|
|
|
if (!compiler.GamesWithHashes.TryGetValue(baseHash, out var games))
|
2020-08-18 22:30:00 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
// Otherwise skip files that are not from the primary game
|
2020-10-18 13:16:27 +00:00
|
|
|
|
return games.Contains(compiler.CompilingGame.Game);
|
2020-08-18 22:30:00 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// If we didn't find a file from an archive or the primary game, use a secondary game file.
|
2021-01-15 14:00:58 +00:00
|
|
|
|
var result = primaryChoice != default ? primaryChoice : ordered.FirstOrDefault();
|
|
|
|
|
return result;
|
2020-08-18 22:30:00 +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-30 12:29:06 +00:00
|
|
|
|
}
|
2019-11-21 21:32:58 +00:00
|
|
|
|
}
|