wabbajack/Wabbajack.DTOs/Texture/PHash.cs

71 lines
1.3 KiB
C#
Raw Normal View History

2021-06-16 05:16:25 +00:00
using System.Data;
using System.IO;
2021-09-27 12:42:46 +00:00
using Wabbajack.Hashing.xxHash64;
2021-06-16 05:16:25 +00:00
2021-10-23 16:51:17 +00:00
namespace Wabbajack.DTOs.Texture;
public struct PHash
2021-06-16 05:16:25 +00:00
{
2021-10-23 16:51:17 +00:00
public const int SIZE = 40;
private readonly int _hash;
2021-06-16 05:16:25 +00:00
2021-10-23 16:51:17 +00:00
public byte[] Data { get; }
2021-06-17 23:09:03 +00:00
2021-10-23 16:51:17 +00:00
public PHash(byte[] data)
{
Data = data;
if (Data.Length != SIZE)
throw new DataException();
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
long h = 0;
h |= Data[0];
h <<= 8;
h |= Data[1];
h <<= 8;
h |= Data[2];
h <<= 8;
h |= Data[3];
h <<= 8;
_hash = (int) h;
}
2021-06-16 05:16:25 +00:00
2021-10-23 16:51:17 +00:00
public static PHash FromBase64(string base64)
{
var data = base64.FromBase64();
if (data.Length != SIZE)
throw new DataException();
return new PHash(data);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static PHash Read(BinaryReader br)
{
return new PHash(br.ReadBytes(SIZE));
}
2021-06-16 05:16:25 +00:00
2021-10-23 16:51:17 +00:00
public void Write(BinaryWriter br)
{
if (_hash == 0)
br.Write(new byte[SIZE]);
else
br.Write(Data);
}
2021-06-16 05:16:25 +00:00
2021-10-23 16:51:17 +00:00
public override string ToString()
{
return Data.ToBase64();
}
2021-06-16 05:16:25 +00:00
2021-10-23 16:51:17 +00:00
public override int GetHashCode()
{
long h = 0;
h |= Data[0];
h <<= 8;
h |= Data[1];
h <<= 8;
h |= Data[2];
h <<= 8;
h |= Data[3];
h <<= 8;
return (int) h;
2021-06-17 23:09:03 +00:00
}
2021-09-27 12:42:46 +00:00
}