forked from wabbajack-tools/wabbajack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbsolutePath.cs
207 lines (173 loc) · 5.97 KB
/
AbsolutePath.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Collections.Generic;
using System.Linq;
namespace Wabbajack.Paths;
public enum PathFormat : byte
{
Windows = 0,
Unix
}
public struct AbsolutePath : IPath, IComparable<AbsolutePath>, IEquatable<AbsolutePath>
{
public static readonly AbsolutePath Empty = "".ToAbsolutePath();
public PathFormat PathFormat { get; }
private int _hashCode = 0;
internal readonly string[] Parts;
public Extension Extension => Extension.FromPath(Parts[^1]);
public RelativePath FileName => new(Parts[^1..]);
internal AbsolutePath(string[] parts, PathFormat format)
{
Parts = parts;
PathFormat = format;
}
internal static readonly char[] StringSplits = {'/', '\\'};
private static AbsolutePath Parse(string path)
{
if (string.IsNullOrWhiteSpace(path)) return default;
var parts = path.Split(StringSplits, StringSplitOptions.RemoveEmptyEntries);
return new AbsolutePath(parts, DetectPathType(path));
}
private static readonly HashSet<char>
DriveLetters = new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
private static PathFormat DetectPathType(string path)
{
if (path.StartsWith("/"))
return PathFormat.Unix;
if (path.StartsWith(@"\\"))
return PathFormat.Windows;
if (path.Length >= 2 && DriveLetters.Contains(path[0]) && path[1] == ':')
return PathFormat.Windows;
throw new PathException($"Invalid Path format: {path}");
}
public AbsolutePath Parent
{
get
{
{
if (Parts.Length <= 1)
throw new PathException($"Path {this} does not have a parent folder");
var newParts = new string[Parts.Length - 1];
Array.Copy(Parts, newParts, newParts.Length);
return new AbsolutePath(newParts, PathFormat);
}
}
}
public int Depth => Parts?.Length ?? 0;
public AbsolutePath ReplaceExtension(Extension newExtension)
{
var paths = new string[Parts.Length];
Array.Copy(Parts, paths, paths.Length);
var oldName = paths[^1];
var newName = RelativePath.ReplaceExtension(oldName, newExtension);
paths[^1] = newName;
return new AbsolutePath(paths, PathFormat);
}
public static explicit operator AbsolutePath(string input)
{
return Parse(input);
}
public override string ToString()
{
if (Parts == default) return "";
if (PathFormat == PathFormat.Windows)
return string.Join('\\', Parts);
return '/' + string.Join('/', Parts);
}
public override int GetHashCode()
{
if (_hashCode != 0) return _hashCode;
var result = 0;
foreach (var part in Parts)
result = result ^ part.GetHashCode(StringComparison.CurrentCultureIgnoreCase);
_hashCode = result;
return _hashCode;
}
public override bool Equals(object? obj)
{
return obj is AbsolutePath path && Equals(path);
}
public int CompareTo(AbsolutePath other)
{
return ArrayExtensions.CompareString(Parts, other.Parts);
}
public bool Equals(AbsolutePath other)
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
if (other.Parts == null) return other.Parts == Parts;
if (Parts == null) return false;
if (Parts.Length != other.Parts.Length) return false;
for (var idx = 0; idx < Parts.Length; idx++)
if (!Parts[idx].Equals(other.Parts[idx], StringComparison.InvariantCultureIgnoreCase))
return false;
return true;
}
public RelativePath RelativeTo(AbsolutePath basePath)
{
if (!ArrayExtensions.AreEqual(basePath.Parts, 0, Parts, 0, basePath.Parts.Length))
throw new PathException($"{basePath} is not a base path of {this}");
var newParts = new string[Parts.Length - basePath.Parts.Length];
Array.Copy(Parts, basePath.Parts.Length, newParts, 0, newParts.Length);
return new RelativePath(newParts);
}
public bool InFolder(AbsolutePath parent)
{
return ArrayExtensions.AreEqual(parent.Parts, 0, Parts, 0, parent.Parts.Length);
}
public AbsolutePath Combine(params object[] paths)
{
var converted = paths.Select(p =>
{
return p switch
{
string s => (RelativePath) s,
RelativePath path => path,
_ => throw new PathException($"Cannot cast {p} of type {p.GetType()} to Path")
};
}).ToArray();
return Combine(converted);
}
public AbsolutePath Combine(params RelativePath[] paths)
{
var newLen = Parts.Length + paths.Sum(p => p.Parts.Length);
var newParts = new string[newLen];
Array.Copy(Parts, newParts, Parts.Length);
var toIdx = Parts.Length;
foreach (var p in paths)
{
Array.Copy(p.Parts, 0, newParts, toIdx, p.Parts.Length);
toIdx += p.Parts.Length;
}
return new AbsolutePath(newParts, PathFormat);
}
public static bool operator ==(AbsolutePath a, AbsolutePath b)
{
return a.Equals(b);
}
public static bool operator !=(AbsolutePath a, AbsolutePath b)
{
return !a.Equals(b);
}
public AbsolutePath WithExtension(Extension? ext)
{
var parts = new string[Parts.Length];
Array.Copy(Parts, parts, Parts.Length);
parts[^1] = parts[^1] + ext;
return new AbsolutePath(parts, PathFormat);
}
public AbsolutePath AppendToName(string append)
{
return Parent.Combine((FileName.WithoutExtension() + append).ToRelativePath()
.WithExtension(Extension));
}
public static AbsolutePath ConvertNoFailure(string value)
{
try
{
return (AbsolutePath) value;
}
catch (Exception ex)
{
return default;
}
}
}