2020-03-23 12:57:18 +00:00
|
|
|
|
using System;
|
2020-03-25 23:15:19 +00:00
|
|
|
|
using System.Collections;
|
2020-03-23 12:57:18 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Alphaleonis.Win32.Filesystem;
|
|
|
|
|
using Directory = System.IO.Directory;
|
|
|
|
|
using File = Alphaleonis.Win32.Filesystem.File;
|
|
|
|
|
using FileInfo = Alphaleonis.Win32.Filesystem.FileInfo;
|
|
|
|
|
using Path = Alphaleonis.Win32.Filesystem.Path;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Common
|
|
|
|
|
{
|
2020-03-24 12:21:19 +00:00
|
|
|
|
public interface IPath
|
2020-03-23 12:57:18 +00:00
|
|
|
|
{
|
2020-03-25 12:47:25 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get the final file name, for c:\bar\baz this is `baz` for c:\bar.zip this is `bar.zip`
|
|
|
|
|
/// for `bar.zip` this is `bar.zip`
|
|
|
|
|
/// </summary>
|
|
|
|
|
public RelativePath FileName { get; }
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-27 03:10:23 +00:00
|
|
|
|
public struct AbsolutePath : IPath, IComparable<AbsolutePath>
|
2020-03-23 12:57:18 +00:00
|
|
|
|
{
|
|
|
|
|
#region ObjectEquality
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
|
|
|
|
private bool Equals(AbsolutePath other)
|
2020-03-23 12:57:18 +00:00
|
|
|
|
{
|
|
|
|
|
return _path == other._path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
|
|
|
|
if (ReferenceEquals(null, obj))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ReferenceEquals(this, obj))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 12:47:25 +00:00
|
|
|
|
if (obj.GetType() != GetType())
|
2020-03-23 12:57:18 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 12:47:25 +00:00
|
|
|
|
return Equals((AbsolutePath)obj);
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
2020-03-25 12:47:25 +00:00
|
|
|
|
return _path != null ? _path.GetHashCode() : 0;
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 03:10:23 +00:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return _path;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
private readonly string _path;
|
|
|
|
|
|
|
|
|
|
public AbsolutePath(string path)
|
|
|
|
|
{
|
|
|
|
|
_path = path.ToLowerInvariant().Replace("/", "\\").TrimEnd('\\');
|
2020-03-25 12:47:25 +00:00
|
|
|
|
Extension = new Extension(Path.GetExtension(_path));
|
2020-03-23 12:57:18 +00:00
|
|
|
|
ValidateAbsolutePath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AbsolutePath(string path, bool skipValidation)
|
|
|
|
|
{
|
|
|
|
|
_path = path.ToLowerInvariant().Replace("/", "\\").TrimEnd('\\');
|
2020-03-25 12:47:25 +00:00
|
|
|
|
Extension = Extension.FromPath(path);
|
|
|
|
|
if (!skipValidation)
|
|
|
|
|
{
|
2020-03-23 12:57:18 +00:00
|
|
|
|
ValidateAbsolutePath();
|
2020-03-25 12:47:25 +00:00
|
|
|
|
}
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AbsolutePath(AbsolutePath path)
|
|
|
|
|
{
|
|
|
|
|
_path = path._path;
|
2020-03-25 12:47:25 +00:00
|
|
|
|
Extension = path.Extension;
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ValidateAbsolutePath()
|
|
|
|
|
{
|
2020-03-25 12:47:25 +00:00
|
|
|
|
if (Path.IsPathRooted(_path))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new InvalidDataException("Absolute path must be absolute");
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
2020-03-24 12:21:19 +00:00
|
|
|
|
|
2020-03-25 12:47:25 +00:00
|
|
|
|
public Extension Extension { get; }
|
2020-03-23 12:57:18 +00:00
|
|
|
|
|
|
|
|
|
public FileStream OpenRead()
|
|
|
|
|
{
|
|
|
|
|
return File.OpenRead(_path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public FileStream Create()
|
|
|
|
|
{
|
|
|
|
|
return File.Create(_path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public FileStream OpenWrite()
|
|
|
|
|
{
|
|
|
|
|
return File.OpenWrite(_path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task WriteAllTextAsync(string text)
|
|
|
|
|
{
|
|
|
|
|
await using var fs = File.Create(_path);
|
|
|
|
|
await fs.WriteAsync(Encoding.UTF8.GetBytes(text));
|
|
|
|
|
}
|
2020-03-26 21:15:44 +00:00
|
|
|
|
|
|
|
|
|
public void WriteAllText(string text)
|
|
|
|
|
{
|
|
|
|
|
using var fs = File.Create(_path);
|
|
|
|
|
fs.Write(Encoding.UTF8.GetBytes(text));
|
|
|
|
|
}
|
2020-03-23 12:57:18 +00:00
|
|
|
|
|
|
|
|
|
public bool Exists => File.Exists(_path) || Directory.Exists(_path);
|
|
|
|
|
public bool IsFile => File.Exists(_path);
|
|
|
|
|
public bool IsDirectory => Directory.Exists(_path);
|
2020-03-23 23:03:26 +00:00
|
|
|
|
|
2020-03-28 04:33:26 +00:00
|
|
|
|
public async Task DeleteDirectory()
|
2020-03-23 23:03:26 +00:00
|
|
|
|
{
|
2020-03-25 12:47:25 +00:00
|
|
|
|
if (IsDirectory)
|
|
|
|
|
{
|
2020-03-28 04:33:26 +00:00
|
|
|
|
await Utils.DeleteDirectory(this);
|
2020-03-25 12:47:25 +00:00
|
|
|
|
}
|
2020-03-23 23:03:26 +00:00
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
|
|
|
|
public long Size => new FileInfo(_path).Length;
|
2020-03-23 12:57:18 +00:00
|
|
|
|
|
2020-03-28 13:33:39 +00:00
|
|
|
|
public DateTime LastModified
|
|
|
|
|
{
|
|
|
|
|
get => File.GetLastWriteTime(_path);
|
|
|
|
|
set => File.SetLastWriteTime(_path, value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public DateTime LastModifiedUtc => File.GetLastWriteTimeUtc(_path);
|
|
|
|
|
public AbsolutePath Parent => (AbsolutePath)Path.GetDirectoryName(_path);
|
|
|
|
|
public RelativePath FileName => (RelativePath)Path.GetFileName(_path);
|
2020-03-25 12:47:25 +00:00
|
|
|
|
public RelativePath FileNameWithoutExtension => (RelativePath)Path.GetFileNameWithoutExtension(_path);
|
2020-03-27 03:33:24 +00:00
|
|
|
|
public bool IsEmptyDirectory => IsDirectory && !EnumerateFiles().Any();
|
2020-03-23 12:57:18 +00:00
|
|
|
|
|
2020-03-28 13:33:39 +00:00
|
|
|
|
public bool IsReadOnly
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return new FileInfo(_path).IsReadOnly;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
new FileInfo(_path).IsReadOnly = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 18:22:53 +00:00
|
|
|
|
public static AbsolutePath EntryPoint => ((AbsolutePath)Assembly.GetEntryAssembly().Location).Parent;
|
|
|
|
|
|
2020-03-24 02:46:30 +00:00
|
|
|
|
/// <summary>
|
2020-03-25 12:47:25 +00:00
|
|
|
|
/// Moves this file to the specified location
|
2020-03-24 02:46:30 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="otherPath"></param>
|
|
|
|
|
/// <param name="overwrite">Replace the destination file if it exists</param>
|
|
|
|
|
public void MoveTo(AbsolutePath otherPath, bool overwrite = false)
|
2020-03-23 12:57:18 +00:00
|
|
|
|
{
|
|
|
|
|
File.Move(_path, otherPath._path, overwrite ? MoveOptions.ReplaceExisting : MoveOptions.None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RelativePath RelativeTo(AbsolutePath p)
|
|
|
|
|
{
|
2020-03-25 12:47:25 +00:00
|
|
|
|
if (_path.Substring(0, p._path.Length + 1) != p._path + "\\")
|
|
|
|
|
{
|
2020-03-23 12:57:18 +00:00
|
|
|
|
throw new InvalidDataException("Not a parent path");
|
2020-03-25 12:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
return new RelativePath(_path.Substring(p._path.Length + 1));
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public async Task<string> ReadAllTextAsync()
|
|
|
|
|
{
|
|
|
|
|
await using var fs = File.OpenRead(_path);
|
|
|
|
|
return Encoding.UTF8.GetString(await fs.ReadAllAsync());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-03-25 12:47:25 +00:00
|
|
|
|
/// Assuming the path is a folder, enumerate all the files in the folder
|
2020-03-23 12:57:18 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="recursive">if true, also returns files in sub-folders</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public IEnumerable<AbsolutePath> EnumerateFiles(bool recursive = true)
|
|
|
|
|
{
|
2020-03-28 18:22:53 +00:00
|
|
|
|
if (!IsDirectory) return new AbsolutePath[0];
|
2020-03-23 12:57:18 +00:00
|
|
|
|
return Directory
|
|
|
|
|
.EnumerateFiles(_path, "*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
|
|
|
|
|
.Select(path => new AbsolutePath(path, true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Operators
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public static explicit operator string(AbsolutePath path)
|
|
|
|
|
{
|
|
|
|
|
return path._path;
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public static explicit operator AbsolutePath(string path)
|
|
|
|
|
{
|
|
|
|
|
return !Path.IsPathRooted(path) ? ((RelativePath)path).RelativeToEntryPoint() : new AbsolutePath(path);
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public static bool operator ==(AbsolutePath a, AbsolutePath b)
|
|
|
|
|
{
|
|
|
|
|
return a._path == b._path;
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public static bool operator !=(AbsolutePath a, AbsolutePath b)
|
|
|
|
|
{
|
|
|
|
|
return a._path != b._path;
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public void CreateDirectory()
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(_path);
|
|
|
|
|
}
|
2020-03-23 23:03:26 +00:00
|
|
|
|
|
|
|
|
|
public void Delete()
|
|
|
|
|
{
|
|
|
|
|
if (IsFile)
|
2020-03-25 12:47:25 +00:00
|
|
|
|
{
|
2020-03-23 23:03:26 +00:00
|
|
|
|
File.Delete(_path);
|
2020-03-25 12:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 22:30:43 +00:00
|
|
|
|
public bool InFolder(AbsolutePath folder)
|
2020-03-25 12:47:25 +00:00
|
|
|
|
{
|
2020-03-25 22:30:43 +00:00
|
|
|
|
return _path.StartsWith(folder._path + Path.DirectorySeparator);
|
2020-03-25 12:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<byte[]> ReadAllBytesAsync()
|
|
|
|
|
{
|
|
|
|
|
await using var f = OpenRead();
|
|
|
|
|
return await f.ReadAllAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AbsolutePath WithExtension(Extension hashFileExtension)
|
|
|
|
|
{
|
2020-03-26 23:02:15 +00:00
|
|
|
|
return new AbsolutePath(_path + (string)hashFileExtension, true);
|
2020-03-25 12:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AbsolutePath ReplaceExtension(Extension extension)
|
|
|
|
|
{
|
|
|
|
|
return new AbsolutePath(
|
|
|
|
|
Path.Combine(Path.GetDirectoryName(_path), Path.GetFileNameWithoutExtension(_path) + (string)extension),
|
|
|
|
|
true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AbsolutePath AppendToName(AbsolutePath bsa, string toAppend)
|
|
|
|
|
{
|
|
|
|
|
return new AbsolutePath(
|
|
|
|
|
Path.Combine(Path.GetDirectoryName(_path),
|
|
|
|
|
Path.GetFileNameWithoutExtension(_path) + toAppend + (string)Extension));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AbsolutePath Combine(params RelativePath[] paths)
|
|
|
|
|
{
|
2020-03-28 18:22:53 +00:00
|
|
|
|
return new AbsolutePath(Path.Combine(paths.Select(s => (string)s).Where(s => s != null).Cons(_path).ToArray()));
|
2020-03-25 12:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AbsolutePath Combine(params string[] paths)
|
|
|
|
|
{
|
2020-03-28 18:22:53 +00:00
|
|
|
|
|
2020-03-25 12:47:25 +00:00
|
|
|
|
return new AbsolutePath(Path.Combine(paths.Cons(_path).ToArray()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> ReadAllLines()
|
|
|
|
|
{
|
|
|
|
|
return File.ReadAllLines(_path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WriteAllBytes(byte[] data)
|
|
|
|
|
{
|
|
|
|
|
using var fs = Create();
|
|
|
|
|
fs.Write(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task WriteAllBytesAsync(byte[] data)
|
|
|
|
|
{
|
|
|
|
|
await using var fs = Create();
|
|
|
|
|
await fs.WriteAsync(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AppendAllText(string text)
|
|
|
|
|
{
|
|
|
|
|
File.AppendAllText(_path, text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CopyTo(AbsolutePath dest, bool useMove = false)
|
|
|
|
|
{
|
|
|
|
|
if (useMove)
|
|
|
|
|
{
|
2020-03-28 02:54:14 +00:00
|
|
|
|
File.Move(_path, dest._path, MoveOptions.ReplaceExisting);
|
2020-03-25 12:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
File.Copy(_path, dest._path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<string>> ReadAllLinesAsync()
|
|
|
|
|
{
|
|
|
|
|
return (await ReadAllTextAsync()).Split(new[] {'\n', '\r'}, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] ReadAllBytes()
|
|
|
|
|
{
|
|
|
|
|
return File.ReadAllBytes(_path);
|
2020-03-23 23:03:26 +00:00
|
|
|
|
}
|
2020-03-25 22:30:43 +00:00
|
|
|
|
|
|
|
|
|
public static AbsolutePath GetCurrentDirectory()
|
|
|
|
|
{
|
|
|
|
|
return new AbsolutePath(Directory.GetCurrentDirectory());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task CopyToAsync(AbsolutePath destFile)
|
|
|
|
|
{
|
|
|
|
|
await using var src = OpenRead();
|
|
|
|
|
await using var dest = destFile.Create();
|
|
|
|
|
await src.CopyToAsync(dest);
|
|
|
|
|
}
|
2020-03-25 23:15:19 +00:00
|
|
|
|
|
|
|
|
|
public IEnumerable<AbsolutePath> EnumerateDirectories(bool recursive = true)
|
|
|
|
|
{
|
|
|
|
|
return Directory.EnumerateDirectories(_path, "*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
|
|
|
|
|
.Select(p => (AbsolutePath)p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task WriteAllLinesAsync(params string[] strings)
|
|
|
|
|
{
|
2020-03-28 02:54:14 +00:00
|
|
|
|
await WriteAllTextAsync(string.Join("\r\n",strings));
|
2020-03-25 23:15:19 +00:00
|
|
|
|
}
|
2020-03-26 21:15:44 +00:00
|
|
|
|
|
2020-03-28 02:54:14 +00:00
|
|
|
|
public void WriteAllLines(params string[] strings)
|
2020-03-26 21:15:44 +00:00
|
|
|
|
{
|
|
|
|
|
WriteAllText(string.Join("\n",strings));
|
|
|
|
|
}
|
2020-03-27 03:10:23 +00:00
|
|
|
|
|
|
|
|
|
public int CompareTo(AbsolutePath other)
|
|
|
|
|
{
|
|
|
|
|
return string.Compare(_path, other._path, StringComparison.Ordinal);
|
|
|
|
|
}
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 23:02:15 +00:00
|
|
|
|
public struct RelativePath : IPath, IEquatable<RelativePath>, IComparable<RelativePath>
|
2020-03-23 12:57:18 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly string _path;
|
|
|
|
|
|
|
|
|
|
public RelativePath(string path)
|
|
|
|
|
{
|
2020-03-28 18:22:53 +00:00
|
|
|
|
var trimmed = path.ToLowerInvariant().Replace("/", "\\").Trim('\\');
|
|
|
|
|
if (string.IsNullOrEmpty(trimmed))
|
|
|
|
|
{
|
|
|
|
|
_path = null;
|
|
|
|
|
Extension = default;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_path = trimmed;
|
|
|
|
|
Extension = new Extension(Path.GetExtension(path));
|
|
|
|
|
}
|
2020-03-23 12:57:18 +00:00
|
|
|
|
Validate();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 12:47:25 +00:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return _path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Extension Extension { get; }
|
|
|
|
|
|
2020-03-24 21:42:28 +00:00
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
2020-03-25 12:47:25 +00:00
|
|
|
|
return _path != null ? _path.GetHashCode() : 0;
|
2020-03-24 21:42:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public static RelativePath RandomFileName()
|
|
|
|
|
{
|
|
|
|
|
return (RelativePath)Guid.NewGuid().ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Validate()
|
|
|
|
|
{
|
|
|
|
|
if (Path.IsPathRooted(_path))
|
2020-03-25 12:47:25 +00:00
|
|
|
|
{
|
2020-03-23 12:57:18 +00:00
|
|
|
|
throw new InvalidDataException("Cannot create relative path from absolute path string");
|
2020-03-25 12:47:25 +00:00
|
|
|
|
}
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AbsolutePath RelativeTo(AbsolutePath abs)
|
|
|
|
|
{
|
2020-03-28 18:22:53 +00:00
|
|
|
|
return _path == null ? abs : new AbsolutePath(Path.Combine((string)abs, _path));
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AbsolutePath RelativeToEntryPoint()
|
|
|
|
|
{
|
|
|
|
|
return RelativeTo(((AbsolutePath)Assembly.GetEntryAssembly().Location).Parent);
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public AbsolutePath RelativeToWorkingDirectory()
|
|
|
|
|
{
|
|
|
|
|
return RelativeTo((AbsolutePath)Directory.GetCurrentDirectory());
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public static explicit operator string(RelativePath path)
|
|
|
|
|
{
|
|
|
|
|
return path._path;
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public static explicit operator RelativePath(string path)
|
|
|
|
|
{
|
|
|
|
|
return new RelativePath(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AbsolutePath RelativeToSystemDirectory()
|
|
|
|
|
{
|
|
|
|
|
return RelativeTo((AbsolutePath)Environment.SystemDirectory);
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public RelativePath Parent => (RelativePath)Path.GetDirectoryName(_path);
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-24 12:21:19 +00:00
|
|
|
|
public RelativePath FileName => new RelativePath(Path.GetFileName(_path));
|
2020-03-24 21:42:28 +00:00
|
|
|
|
|
2020-03-25 22:30:43 +00:00
|
|
|
|
public RelativePath FileNameWithoutExtension => (RelativePath)Path.GetFileNameWithoutExtension(_path);
|
2020-03-28 18:22:53 +00:00
|
|
|
|
|
|
|
|
|
public RelativePath TopParent
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var curr = this;
|
|
|
|
|
|
|
|
|
|
while (curr.Parent != default)
|
|
|
|
|
curr = curr.Parent;
|
|
|
|
|
|
|
|
|
|
return curr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 22:30:43 +00:00
|
|
|
|
|
2020-03-24 21:42:28 +00:00
|
|
|
|
public bool Equals(RelativePath other)
|
|
|
|
|
{
|
|
|
|
|
return _path == other._path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
|
|
|
|
return obj is RelativePath other && Equals(other);
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-24 21:42:28 +00:00
|
|
|
|
public static bool operator ==(RelativePath a, RelativePath b)
|
|
|
|
|
{
|
|
|
|
|
return a._path == b._path;
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-24 21:42:28 +00:00
|
|
|
|
public static bool operator !=(RelativePath a, RelativePath b)
|
|
|
|
|
{
|
|
|
|
|
return !(a == b);
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
|
|
|
|
public bool StartsWith(string s)
|
|
|
|
|
{
|
|
|
|
|
return _path.StartsWith(s);
|
|
|
|
|
}
|
2020-03-25 22:30:43 +00:00
|
|
|
|
|
|
|
|
|
public bool StartsWith(RelativePath s)
|
|
|
|
|
{
|
|
|
|
|
return _path.StartsWith(s._path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RelativePath Combine(params RelativePath[] paths )
|
|
|
|
|
{
|
|
|
|
|
return (RelativePath)Path.Combine(paths.Select(p => (string)p).Cons(_path).ToArray());
|
|
|
|
|
}
|
2020-03-26 23:02:15 +00:00
|
|
|
|
|
|
|
|
|
public int CompareTo(RelativePath other)
|
|
|
|
|
{
|
|
|
|
|
return string.Compare(_path, other._path, StringComparison.Ordinal);
|
|
|
|
|
}
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 23:03:26 +00:00
|
|
|
|
public static partial class Utils
|
|
|
|
|
{
|
|
|
|
|
public static RelativePath ToPath(this string str)
|
|
|
|
|
{
|
|
|
|
|
return (RelativePath)str;
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 23:03:26 +00:00
|
|
|
|
public static AbsolutePath RelativeTo(this string str, AbsolutePath path)
|
|
|
|
|
{
|
|
|
|
|
return ((RelativePath)str).RelativeTo(path);
|
|
|
|
|
}
|
2020-03-24 12:21:19 +00:00
|
|
|
|
|
|
|
|
|
public static void Write(this BinaryWriter wtr, IPath path)
|
|
|
|
|
{
|
|
|
|
|
wtr.Write(path is AbsolutePath);
|
|
|
|
|
if (path is AbsolutePath)
|
2020-03-25 12:47:25 +00:00
|
|
|
|
{
|
2020-03-24 12:21:19 +00:00
|
|
|
|
wtr.Write((AbsolutePath)path);
|
2020-03-25 12:47:25 +00:00
|
|
|
|
}
|
2020-03-24 12:21:19 +00:00
|
|
|
|
else
|
2020-03-25 12:47:25 +00:00
|
|
|
|
{
|
2020-03-24 12:21:19 +00:00
|
|
|
|
wtr.Write((RelativePath)path);
|
2020-03-25 12:47:25 +00:00
|
|
|
|
}
|
2020-03-24 12:21:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Write(this BinaryWriter wtr, AbsolutePath path)
|
|
|
|
|
{
|
|
|
|
|
wtr.Write((string)path);
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-24 12:21:19 +00:00
|
|
|
|
public static void Write(this BinaryWriter wtr, RelativePath path)
|
|
|
|
|
{
|
|
|
|
|
wtr.Write((string)path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IPath ReadIPath(this BinaryReader rdr)
|
|
|
|
|
{
|
|
|
|
|
if (rdr.ReadBoolean())
|
2020-03-25 12:47:25 +00:00
|
|
|
|
{
|
2020-03-24 12:21:19 +00:00
|
|
|
|
return rdr.ReadAbsolutePath();
|
2020-03-25 12:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 12:21:19 +00:00
|
|
|
|
return rdr.ReadRelativePath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static AbsolutePath ReadAbsolutePath(this BinaryReader rdr)
|
|
|
|
|
{
|
|
|
|
|
return new AbsolutePath(rdr.ReadString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static RelativePath ReadRelativePath(this BinaryReader rdr)
|
|
|
|
|
{
|
|
|
|
|
return new RelativePath(rdr.ReadString());
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 18:22:53 +00:00
|
|
|
|
|
2020-03-24 12:21:19 +00:00
|
|
|
|
public static T[] Add<T>(this T[] arr, T itm)
|
|
|
|
|
{
|
|
|
|
|
var newArr = new T[arr.Length + 1];
|
|
|
|
|
Array.Copy(arr, 0, newArr, 0, arr.Length);
|
|
|
|
|
newArr[arr.Length] = itm;
|
|
|
|
|
return newArr;
|
|
|
|
|
}
|
2020-03-23 23:03:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 12:21:19 +00:00
|
|
|
|
public struct Extension
|
2020-03-23 12:57:18 +00:00
|
|
|
|
{
|
2020-03-25 12:47:25 +00:00
|
|
|
|
public static Extension None = new Extension("", false);
|
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
#region ObjectEquality
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
|
|
|
|
private bool Equals(Extension other)
|
2020-03-23 12:57:18 +00:00
|
|
|
|
{
|
|
|
|
|
return _extension == other._extension;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
|
|
|
|
if (ReferenceEquals(null, obj))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ReferenceEquals(this, obj))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 12:47:25 +00:00
|
|
|
|
if (obj.GetType() != GetType())
|
2020-03-23 12:57:18 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 12:47:25 +00:00
|
|
|
|
return Equals((Extension)obj);
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 02:54:14 +00:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return _extension;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
2020-03-25 12:47:25 +00:00
|
|
|
|
return _extension != null ? _extension.GetHashCode() : 0;
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
private readonly string _extension;
|
|
|
|
|
|
|
|
|
|
public Extension(string extension)
|
|
|
|
|
{
|
2020-03-24 12:21:19 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(extension))
|
|
|
|
|
{
|
|
|
|
|
_extension = None._extension;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
_extension = string.Intern(extension);
|
|
|
|
|
Validate();
|
|
|
|
|
}
|
2020-03-24 12:21:19 +00:00
|
|
|
|
|
|
|
|
|
private Extension(string extension, bool validate)
|
|
|
|
|
{
|
|
|
|
|
_extension = string.Intern(extension);
|
2020-03-25 12:47:25 +00:00
|
|
|
|
if (validate)
|
|
|
|
|
{
|
|
|
|
|
Validate();
|
|
|
|
|
}
|
2020-03-24 12:21:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Extension(Extension other)
|
|
|
|
|
{
|
|
|
|
|
_extension = other._extension;
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
private void Validate()
|
|
|
|
|
{
|
|
|
|
|
if (!_extension.StartsWith("."))
|
2020-03-25 12:47:25 +00:00
|
|
|
|
{
|
|
|
|
|
throw new InvalidDataException("Extensions must start with '.'");
|
|
|
|
|
}
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public static explicit operator string(Extension path)
|
|
|
|
|
{
|
|
|
|
|
return path._extension;
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public static explicit operator Extension(string path)
|
|
|
|
|
{
|
|
|
|
|
return new Extension(path);
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public static bool operator ==(Extension a, Extension b)
|
|
|
|
|
{
|
|
|
|
|
// Super fast comparison because extensions are interned
|
2020-03-25 12:47:25 +00:00
|
|
|
|
if ((object)a == null && (object)b == null)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((object)a == null || (object)b == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
return ReferenceEquals(a._extension, b._extension);
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public static bool operator !=(Extension a, Extension b)
|
|
|
|
|
{
|
2020-03-24 02:58:39 +00:00
|
|
|
|
return !(a == b);
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
2020-03-24 12:21:19 +00:00
|
|
|
|
|
|
|
|
|
public static Extension FromPath(string path)
|
|
|
|
|
{
|
|
|
|
|
var ext = Path.GetExtension(path);
|
|
|
|
|
return !string.IsNullOrWhiteSpace(ext) ? new Extension(ext) : None;
|
|
|
|
|
}
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 12:28:03 +00:00
|
|
|
|
public struct HashRelativePath : IEquatable<HashRelativePath>
|
2020-03-23 12:57:18 +00:00
|
|
|
|
{
|
2020-03-24 12:21:19 +00:00
|
|
|
|
private static RelativePath[] EMPTY_PATH;
|
|
|
|
|
public Hash BaseHash { get; }
|
|
|
|
|
public RelativePath[] Paths { get; }
|
|
|
|
|
|
|
|
|
|
static HashRelativePath()
|
|
|
|
|
{
|
|
|
|
|
EMPTY_PATH = new RelativePath[0];
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-24 12:21:19 +00:00
|
|
|
|
public HashRelativePath(Hash baseHash, params RelativePath[] paths)
|
|
|
|
|
{
|
|
|
|
|
BaseHash = baseHash;
|
|
|
|
|
Paths = paths;
|
|
|
|
|
}
|
2020-03-23 12:57:18 +00:00
|
|
|
|
|
2020-03-26 12:28:03 +00:00
|
|
|
|
public override string ToString()
|
2020-03-23 12:57:18 +00:00
|
|
|
|
{
|
|
|
|
|
return string.Join("|", Paths.Select(t => t.ToString()).Cons(BaseHash.ToString()));
|
|
|
|
|
}
|
|
|
|
|
public static bool operator ==(HashRelativePath a, HashRelativePath b)
|
|
|
|
|
{
|
2020-03-26 12:28:03 +00:00
|
|
|
|
if (a.BaseHash != b.BaseHash || a.Paths.Length != b.Paths.Length)
|
2020-03-25 12:47:25 +00:00
|
|
|
|
{
|
2020-03-23 12:57:18 +00:00
|
|
|
|
return false;
|
2020-03-25 12:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var idx = 0; idx < a.Paths.Length; idx += 1)
|
|
|
|
|
{
|
2020-03-23 12:57:18 +00:00
|
|
|
|
if (a.Paths[idx] != b.Paths[idx])
|
2020-03-25 12:47:25 +00:00
|
|
|
|
{
|
2020-03-23 12:57:18 +00:00
|
|
|
|
return false;
|
2020-03-25 12:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-23 12:57:18 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public static bool operator !=(HashRelativePath a, HashRelativePath b)
|
|
|
|
|
{
|
|
|
|
|
return !(a == b);
|
|
|
|
|
}
|
2020-03-26 12:28:03 +00:00
|
|
|
|
|
|
|
|
|
public bool Equals(HashRelativePath other)
|
|
|
|
|
{
|
|
|
|
|
return this == other;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
|
|
|
|
return obj is HashRelativePath other && Equals(other);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
return HashCode.Combine(BaseHash, Paths);
|
|
|
|
|
}
|
2020-03-26 21:31:25 +00:00
|
|
|
|
|
|
|
|
|
public static HashRelativePath FromStrings(string hash, params string[] paths)
|
|
|
|
|
{
|
|
|
|
|
return new HashRelativePath(Hash.FromBase64(hash), paths.Select(p => (RelativePath)p).ToArray());
|
|
|
|
|
}
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-25 22:30:43 +00:00
|
|
|
|
public struct FullPath : IEquatable<FullPath>, IPath
|
2020-03-23 12:57:18 +00:00
|
|
|
|
{
|
|
|
|
|
public AbsolutePath Base { get; }
|
|
|
|
|
public RelativePath[] Paths { get; }
|
|
|
|
|
|
2020-03-24 21:42:28 +00:00
|
|
|
|
private readonly int _hash;
|
|
|
|
|
|
2020-03-26 12:28:03 +00:00
|
|
|
|
public FullPath(AbsolutePath basePath, params RelativePath[] paths)
|
2020-03-23 12:57:18 +00:00
|
|
|
|
{
|
|
|
|
|
Base = basePath;
|
2020-03-24 12:21:19 +00:00
|
|
|
|
Paths = paths;
|
2020-03-24 21:42:28 +00:00
|
|
|
|
_hash = Base.GetHashCode();
|
|
|
|
|
foreach (var itm in Paths)
|
2020-03-25 12:47:25 +00:00
|
|
|
|
{
|
2020-03-24 21:42:28 +00:00
|
|
|
|
_hash ^= itm.GetHashCode();
|
2020-03-25 12:47:25 +00:00
|
|
|
|
}
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 21:42:28 +00:00
|
|
|
|
public override string ToString()
|
2020-03-23 12:57:18 +00:00
|
|
|
|
{
|
2020-03-24 21:42:28 +00:00
|
|
|
|
return string.Join("|", Paths.Select(t => (string)t).Cons((string)Base));
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
2020-03-24 21:42:28 +00:00
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
return _hash;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public static bool operator ==(FullPath a, FullPath b)
|
|
|
|
|
{
|
2020-03-28 02:54:14 +00:00
|
|
|
|
if (a.Paths == null || b.Paths == null) return false;
|
2020-03-24 21:42:28 +00:00
|
|
|
|
if (a.Base != b.Base || a.Paths.Length != b.Paths.Length)
|
2020-03-25 12:47:25 +00:00
|
|
|
|
{
|
2020-03-23 12:57:18 +00:00
|
|
|
|
return false;
|
2020-03-25 12:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var idx = 0; idx < a.Paths.Length; idx += 1)
|
|
|
|
|
{
|
2020-03-23 12:57:18 +00:00
|
|
|
|
if (a.Paths[idx] != b.Paths[idx])
|
2020-03-25 12:47:25 +00:00
|
|
|
|
{
|
2020-03-23 12:57:18 +00:00
|
|
|
|
return false;
|
2020-03-25 12:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-23 12:57:18 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-03-25 12:47:25 +00:00
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public static bool operator !=(FullPath a, FullPath b)
|
|
|
|
|
{
|
|
|
|
|
return !(a == b);
|
|
|
|
|
}
|
2020-03-24 21:42:28 +00:00
|
|
|
|
|
|
|
|
|
public bool Equals(FullPath other)
|
|
|
|
|
{
|
|
|
|
|
return this == other;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
|
|
|
|
return obj is FullPath other && Equals(other);
|
|
|
|
|
}
|
2020-03-25 22:30:43 +00:00
|
|
|
|
|
|
|
|
|
public RelativePath FileName => Paths.Length == 0 ? Base.FileName : Paths.Last().FileName;
|
2020-03-23 12:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|