wabbajack/Wabbajack.Paths/FullPath.cs

74 lines
2.1 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System;
using System.Linq;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Paths;
public readonly struct FullPath : IPath, IEquatable<FullPath>, IComparable<FullPath>
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
public readonly AbsolutePath Base;
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 : Base.Extension;
public RelativePath FileName => Parts.Length > 0 ? Parts[^1].FileName : Base.FileName;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public FullPath(AbsolutePath basePath, params RelativePath[] parts)
{
Base = basePath;
Parts = parts;
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public override string ToString()
{
return Base + "|" + 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(Base.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(FullPath other)
{
if (other.Parts.Length != Parts.Length) return false;
if (other.Base != Base) 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(FullPath other)
{
var init = Base.CompareTo(other.Base);
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 ==(FullPath a, FullPath 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 !=(FullPath a, FullPath b)
{
return !a.Equals(b);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
/// <summary>
/// Creates a new full path, with relativePath combined with the deepest leaf in the full path
/// </summary>
/// <param name="relativePath"></param>
/// <returns></returns>
public FullPath InSameFolder(RelativePath relativePath)
{
if (Parts.Length == 0) return new FullPath(Base.Parent.Combine(relativePath));
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var paths = new RelativePath[Parts.Length];
Parts.CopyTo(paths, 0);
paths[^1] = paths[^1].Parent.Combine(relativePath);
return new FullPath(Base, paths);
2021-09-27 12:42:46 +00:00
}
}