2021-09-27 12:42:46 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
using System.Linq;
|
2019-12-04 01:26:26 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2020-07-01 12:07:02 +00:00
|
|
|
|
using F23.StringSimilarity;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
using IniParser.Model;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
using Wabbajack.Common;
|
2021-10-23 16:51:17 +00:00
|
|
|
|
using Wabbajack.Compiler.PatchCache;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
using Wabbajack.DTOs;
|
|
|
|
|
using Wabbajack.DTOs.Directives;
|
|
|
|
|
using Wabbajack.Paths;
|
|
|
|
|
using Wabbajack.VFS;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
namespace Wabbajack.Compiler.CompilationSteps;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public class IncludePatches : ACompilationStep
|
|
|
|
|
{
|
|
|
|
|
private readonly Dictionary<RelativePath, IGrouping<RelativePath, VirtualFile>> _indexed;
|
|
|
|
|
private readonly VirtualFile? _bsa;
|
|
|
|
|
private readonly Dictionary<RelativePath, IEnumerable<VirtualFile>> _indexedByName;
|
|
|
|
|
private readonly bool _isGenericGame;
|
2020-07-05 10:44:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public IncludePatches(ACompiler compiler, VirtualFile? constructingFromBSA = null) : base(compiler)
|
|
|
|
|
{
|
|
|
|
|
_bsa = constructingFromBSA;
|
|
|
|
|
_compiler = compiler;
|
|
|
|
|
_indexed = _compiler.IndexedFiles.Values
|
|
|
|
|
.SelectMany(f => f)
|
|
|
|
|
.GroupBy(f => f.Name.FileName)
|
|
|
|
|
.ToDictionary(f => f.Key);
|
|
|
|
|
_indexedByName = _indexed.Values
|
|
|
|
|
.SelectMany(s => s)
|
|
|
|
|
.Where(f => f.IsNative)
|
|
|
|
|
.GroupBy(f => f.Name.FileName)
|
|
|
|
|
.ToDictionary(f => f.Key, f => (IEnumerable<VirtualFile>) f);
|
|
|
|
|
|
|
|
|
|
_isGenericGame = _compiler._settings.Game.MetaData().IsGenericMO2Plugin;
|
|
|
|
|
}
|
2020-02-05 05:17:12 +00:00
|
|
|
|
|
2022-10-04 04:43:21 +00:00
|
|
|
|
private IncludePatches(ACompiler compiler,
|
|
|
|
|
VirtualFile bsa,
|
|
|
|
|
Dictionary<RelativePath, IEnumerable<VirtualFile>> indexedByName,
|
|
|
|
|
Dictionary<RelativePath, IGrouping<RelativePath, VirtualFile>> indexed) : base(compiler)
|
|
|
|
|
{
|
|
|
|
|
_bsa = bsa;
|
|
|
|
|
_indexedByName = indexedByName;
|
|
|
|
|
_indexed = indexed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IncludePatches WithBSA(VirtualFile constructingFromBSA)
|
|
|
|
|
{
|
|
|
|
|
return new IncludePatches(_compiler, constructingFromBSA, _indexedByName, _indexed);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public override async ValueTask<Directive?> Run(RawSourceFile source)
|
|
|
|
|
{
|
|
|
|
|
if (_isGenericGame)
|
|
|
|
|
if (source.Path.InFolder(Consts.GameFolderFilesDir))
|
|
|
|
|
return null;
|
2019-10-30 12:29:06 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
var name = source.File.Name.FileName;
|
|
|
|
|
var nameWithoutExt = name;
|
|
|
|
|
if (name.Extension == Ext.Mohidden)
|
|
|
|
|
nameWithoutExt = name.WithoutExtension();
|
2020-10-19 00:15:22 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
if (!_indexed.TryGetValue(name, out var choices))
|
|
|
|
|
_indexed.TryGetValue(nameWithoutExt, out choices);
|
2019-11-21 21:32:58 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
IniData? modIni = null;
|
2019-11-21 21:32:58 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
if (_compiler is MO2Compiler)
|
|
|
|
|
{
|
|
|
|
|
if (_bsa == null && source.File.IsNative &&
|
|
|
|
|
source.AbsolutePath.InFolder(((MO2Compiler) _compiler).MO2ModsFolder))
|
2020-02-05 05:17:12 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
((MO2Compiler) _compiler).ModInis.TryGetValue(ModForFile(source.AbsolutePath), out modIni);
|
2020-02-05 05:17:12 +00:00
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
else if (_bsa != null)
|
2019-11-21 21:32:58 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
var bsaPath = _bsa.FullPath.Base;
|
2022-08-02 20:09:47 +00:00
|
|
|
|
var modPath = ModForFile(bsaPath);
|
|
|
|
|
if (modPath != default)
|
|
|
|
|
((MO2Compiler) _compiler).ModInis.TryGetValue(modPath, out modIni);
|
2019-11-21 21:32:58 +00:00
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|
2019-10-30 12:29:06 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
var installationFile = modIni?["General"]["installationFile"];
|
2020-02-05 05:17:12 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
VirtualFile[] found = { };
|
2020-02-05 05:17:12 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
// Find based on exact file name + ext
|
|
|
|
|
if (choices != null && installationFile != null)
|
|
|
|
|
{
|
|
|
|
|
var relName = (RelativePath) Path.GetFileName(installationFile);
|
|
|
|
|
found = choices.Where(f => f.FilesInFullPath.First().Name.FileName == relName).ToArray();
|
|
|
|
|
}
|
2019-10-30 12:29:06 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
// Find based on file name only (not ext)
|
|
|
|
|
if (found.Length == 0 && choices != null) found = choices.ToArray();
|
2020-06-30 23:09:59 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
// Find based on matchAll=<archivename> in [General] in meta.ini
|
|
|
|
|
var matchAllName = modIni?["General"]?["matchAll"];
|
|
|
|
|
if (matchAllName != null && found.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
var relName = (RelativePath) Path.GetFileName(matchAllName);
|
|
|
|
|
if (_indexedByName.TryGetValue(relName, out var arch))
|
2020-06-30 23:09:59 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
var dist = new Levenshtein();
|
|
|
|
|
found = arch.SelectMany(a => a.ThisAndAllChildren)
|
|
|
|
|
.OrderBy(a => dist.Distance(a.Name.FileName.ToString(), source.File.Name.FileName.ToString()))
|
|
|
|
|
.Take(3)
|
|
|
|
|
.ToArray();
|
2020-07-15 04:20:56 +00:00
|
|
|
|
}
|
2019-10-30 12:29:06 +00:00
|
|
|
|
}
|
2019-10-31 02:24:42 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
if (found.Length == 0)
|
|
|
|
|
return null;
|
2020-08-18 22:30:00 +00:00
|
|
|
|
|
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
var e = source.EvolveTo<PatchedFromArchive>();
|
|
|
|
|
|
|
|
|
|
var patches = await found
|
|
|
|
|
.SelectAsync(async c => (await _compiler._patchCache.GetPatch(c.Hash, source.File.Hash), c))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
if (patches.All(p => p.Item1 != null))
|
2020-03-28 18:22:53 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
var (patch, file) = PickPatch(_compiler, patches);
|
|
|
|
|
e.FromHash = file.Hash;
|
|
|
|
|
e.ArchiveHashPath = file.MakeRelativePaths();
|
|
|
|
|
e.PatchID = await _compiler.IncludeFile(await _compiler._patchCache.GetData(patch));
|
2020-03-28 18:22:53 +00:00
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_compiler._patchOptions[e] = found;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static (CacheEntry, VirtualFile) PickPatch(ACompiler compiler,
|
|
|
|
|
IEnumerable<(CacheEntry? data, VirtualFile file)> patches)
|
|
|
|
|
{
|
|
|
|
|
var ordered = patches
|
|
|
|
|
.Select(f => (f.data!, f.file))
|
|
|
|
|
.OrderBy(f => f.Item1.PatchSize)
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
var primaryChoice = ordered.FirstOrDefault(itm =>
|
|
|
|
|
{
|
|
|
|
|
var baseHash = itm.file.TopParent.Hash;
|
|
|
|
|
|
|
|
|
|
// If this file doesn't come from a game use it
|
|
|
|
|
if (!compiler.GamesWithHashes.TryGetValue(baseHash, out var games))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
// Otherwise skip files that are not from the primary game
|
|
|
|
|
return games.Contains(compiler._settings.Game);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// If we didn't find a file from an archive or the primary game, use a secondary game file.
|
|
|
|
|
var result = primaryChoice != default ? primaryChoice : ordered.FirstOrDefault();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AbsolutePath ModForFile(AbsolutePath file)
|
|
|
|
|
{
|
2022-08-02 20:09:47 +00:00
|
|
|
|
if (!file.InFolder(((MO2Compiler) _compiler).MO2ModsFolder))
|
|
|
|
|
return default;
|
2021-10-23 16:51:17 +00:00
|
|
|
|
return file.RelativeTo(((MO2Compiler) _compiler).MO2ModsFolder).TopParent
|
|
|
|
|
.RelativeTo(((MO2Compiler) _compiler).MO2ModsFolder);
|
2019-10-30 12:29:06 +00:00
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|