wabbajack/Wabbajack.Hashing.xxHash64/HashRelativePath.cs

65 lines
1.7 KiB
C#
Raw Permalink Normal View History

2021-09-27 12:42:46 +00:00
using System;
using System.Linq;
using Wabbajack.Paths;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Hashing.xxHash64;
public readonly struct HashRelativePath : IPath, IEquatable<HashRelativePath>, IComparable<HashRelativePath>
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
public readonly Hash Hash;
public readonly RelativePath[] Parts;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public Extension Extension => Parts.Length > 0
? Parts[^1].Extension
: throw new InvalidOperationException("No path in HashRelativePath");
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public RelativePath FileName => Parts.Length > 0
? Parts[^1].FileName
: throw new InvalidOperationException("No path in HashRelativePath");
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public HashRelativePath(Hash basePath, params RelativePath[] parts)
{
Hash = basePath;
Parts = parts;
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public override string ToString()
{
return Hash + "|" + string.Join("|", Parts);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public override bool Equals(object? obj)
{
return obj is FullPath path && Equals(path);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public override int GetHashCode()
{
return Parts.Aggregate(Hash.GetHashCode(), (i, path) => i ^ path.GetHashCode());
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public bool Equals(HashRelativePath other)
{
if (other.Parts.Length != Parts.Length) return false;
if (other.Hash != Hash) return false;
return ArrayExtensions.AreEqual(Parts, 0, other.Parts, 0, Parts.Length);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public int CompareTo(HashRelativePath other)
{
var init = Hash.CompareTo(other.Hash);
if (init != 0) return init;
return ArrayExtensions.Compare(Parts, other.Parts);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static bool operator ==(HashRelativePath a, HashRelativePath b)
{
return a.Equals(b);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static bool operator !=(HashRelativePath a, HashRelativePath b)
{
return !a.Equals(b);
2021-09-27 12:42:46 +00:00
}
}