wabbajack/Wabbajack.Lib/CompilationSteps/MatchSimilarTextures.cs

46 lines
1.5 KiB
C#
Raw Normal View History

2021-06-17 23:09:03 +00:00
using System.Linq;
using System.Threading.Tasks;
using Wabbajack.Common;
using Wabbajack.ImageHashing;
using Wabbajack.VirtualFileSystem;
namespace Wabbajack.Lib.CompilationSteps
{
public class MatchSimilarTextures : ACompilationStep
{
private ILookup<RelativePath, VirtualFile> _byName;
public MatchSimilarTextures(ACompiler compiler) : base(compiler)
{
_byName = _compiler.IndexedFiles.SelectMany(kv => kv.Value)
.Where(f => f.Name.FileName.Extension == DDS)
2021-06-27 14:05:45 +00:00
.Where(f => f.ImageState != null)
2021-06-17 23:09:03 +00:00
.ToLookup(f => f.Name.FileName.FileNameWithoutExtension);
}
private static Extension DDS = new(".dds");
public override async ValueTask<Directive?> Run(RawSourceFile source)
{
2021-06-29 03:02:01 +00:00
if (source.Path.Extension == DDS && source.File.ImageState != null)
2021-06-17 23:09:03 +00:00
{
var found = _byName[source.Path.FileNameWithoutExtension]
.Select(f => (f.ImageState.PerceptualHash.Similarity(source.File.ImageState.PerceptualHash), f))
.Where(f => f.Item1 >= 0.90f)
.OrderByDescending(f => f.Item1)
.FirstOrDefault();
if (found == default) return null;
var rv = source.EvolveTo<TransformedTexture>();
rv.ArchiveHashPath = found.f.MakeRelativePaths();
rv.ImageState = found.f.ImageState;
return rv;
}
return null;
}
}
}