Swallow exceptions from bad DDS files

This commit is contained in:
Timothy Baldridge 2021-06-18 17:33:58 -06:00
parent 5a501226c3
commit 3703f3f558
3 changed files with 28 additions and 14 deletions

View File

@ -34,7 +34,7 @@ namespace Wabbajack.ImageHashing
PerceptualHash.Write(bw);
}
public static async Task<ImageState> FromImageStream(Stream stream, Extension ext, bool takeStreamOwnership = true)
public static async Task<ImageState?> FromImageStream(Stream stream, Extension ext, bool takeStreamOwnership = true)
{
var ms = new MemoryStream();
await stream.CopyToAsync(ms);
@ -57,6 +57,11 @@ namespace Wabbajack.ImageHashing
return img.ImageState();
}
catch (Exception ex)
{
Utils.Log($"Error getting ImageState: {ex}");
return null;
}
finally
{
img?.Dispose();

View File

@ -86,22 +86,31 @@ namespace Wabbajack.ImageHashing
public static async Task<PHash> FromStream(Stream stream, Extension ext, bool takeStreamOwnership = true)
{
var ms = new MemoryStream();
await stream.CopyToAsync(ms);
if (takeStreamOwnership) await stream.DisposeAsync();
try
{
var ms = new MemoryStream();
await stream.CopyToAsync(ms);
if (takeStreamOwnership) await stream.DisposeAsync();
DDSImage img;
if (ext == new Extension(".dds"))
img = DDSImage.FromDDSMemory(ms.GetBuffer());
else if (ext == new Extension(".tga"))
{
img = DDSImage.FromTGAMemory(ms.GetBuffer());
DDSImage img;
if (ext == new Extension(".dds"))
img = DDSImage.FromDDSMemory(ms.GetBuffer());
else if (ext == new Extension(".tga"))
{
img = DDSImage.FromTGAMemory(ms.GetBuffer());
}
else
{
throw new NotImplementedException("Only DDS and TGA files supported by PHash");
}
return img.PerceptionHash();
}
else
catch (Exception ex)
{
throw new NotImplementedException("Only DDS and TGA files supported by PHash");
Utils.Log($"Error getting PHASH {ex}");
return default;
}
return img.PerceptionHash();
}
public static async Task<PHash> FromFile(AbsolutePath path)

View File

@ -227,7 +227,7 @@ namespace Wabbajack.VirtualFileSystem
};
if (Consts.TextureExtensions.Contains(relPath.FileName.Extension))
self.ImageState = await ImageHashing.ImageState.FromImageStream(stream, relPath.FileName.Extension, false);
self.ImageState = await ImageState.FromImageStream(stream, relPath.FileName.Extension, false);
self.FillFullPath(depth);