public static bool MatchesSub(this IRelativeDirectoryPath dir, string name) { while (dir.HasParentDirectory) { if (dir.DirectoryName.Equals(name)) { return(true); } dir = dir.ParentDirectoryPath; } return(false); }
public static bool SubStartsWith(this IRelativeDirectoryPath dir, string name) { while (dir.HasParentDirectory) { if (dir.DirectoryName.StartsWith(name)) { return(true); } dir = dir.ParentDirectoryPath; } return(false); }
public static IRelativeDirectoryPath GetRoot(this IRelativeDirectoryPath dir) { if (!dir.HasParentDirectory) { return(dir); } while (dir.ParentDirectoryPath.HasParentDirectory) { dir = dir.ParentDirectoryPath; } return(dir); }
public void Test_GetChildWithName() { IAbsoluteDirectoryPath absoluteDirectoryPath = @"C:\Dir1\Dir2".ToAbsoluteDirectoryPath(); Assert.IsTrue(absoluteDirectoryPath.GetChildFileWithName("File.txt").ToString() == @"C:\Dir1\Dir2\File.txt"); Assert.IsTrue(absoluteDirectoryPath.GetChildDirectoryWithName("Dir3").ToString() == @"C:\Dir1\Dir2\Dir3"); IRelativeDirectoryPath relativeDirectoryPath = @"..\..\Dir1\Dir2".ToRelativeDirectoryPath(); Assert.IsTrue(relativeDirectoryPath.GetChildFileWithName("File.txt").ToString() == @"..\..\Dir1\Dir2\File.txt"); Assert.IsTrue(relativeDirectoryPath.GetChildDirectoryWithName("Dir3").ToString() == @"..\..\Dir1\Dir2\Dir3"); }
public static string Join(this IRelativeDirectoryPath relative) { var parts = new List <string>(); parts.Insert(0, relative.DirectoryName); while (relative.HasParentDirectory) { relative = relative.ParentDirectoryPath; parts.Insert(0, relative.DirectoryName); } return(Path.Combine(parts.ToArray())); }
public void Test_PathEquality() { // // RelativeDirectoryPath // IRelativeDirectoryPath relativeRelativeDirectoryPath1 = @"..\Dir1\Dir2".ToRelativeDirectoryPath(); IRelativeDirectoryPath relativeRelativeDirectoryPath2 = @"..\\dir1//DIR2/".ToRelativeDirectoryPath(); Assert.IsTrue(relativeRelativeDirectoryPath1.Equals(relativeRelativeDirectoryPath2)); relativeRelativeDirectoryPath1 = @"..\Dir1\Dir2".ToRelativeDirectoryPath(); relativeRelativeDirectoryPath2 = @".\Dir1\Dir2".ToRelativeDirectoryPath(); Assert.IsFalse(relativeRelativeDirectoryPath1.Equals(relativeRelativeDirectoryPath2)); relativeRelativeDirectoryPath1 = @"..\Dir1\Dir2".ToRelativeDirectoryPath(); relativeRelativeDirectoryPath2 = @"..\Dir1\Dir2\Dir3".ToRelativeDirectoryPath(); Assert.IsFalse(relativeRelativeDirectoryPath1.Equals(relativeRelativeDirectoryPath2)); relativeRelativeDirectoryPath1 = @"..\Dir1\Dir2".ToRelativeDirectoryPath(); relativeRelativeDirectoryPath2 = @"..\Dir1\Dir".ToRelativeDirectoryPath(); Assert.IsFalse(relativeRelativeDirectoryPath1.Equals(relativeRelativeDirectoryPath2)); // // AbsoluteDirectoryPath // IAbsoluteDirectoryPath absoluteAbsoluteDirectoryPath1 = @"C:\Dir1\Dir2".ToAbsoluteDirectoryPath(); IAbsoluteDirectoryPath absoluteAbsoluteDirectoryPath2 = @"C:\\dir1//Dir2\\".ToAbsoluteDirectoryPath(); Assert.IsTrue(absoluteAbsoluteDirectoryPath1.Equals(absoluteAbsoluteDirectoryPath2)); absoluteAbsoluteDirectoryPath1 = @"C:\Dir1\Dir2".ToAbsoluteDirectoryPath(); absoluteAbsoluteDirectoryPath2 = @"D:\Dir1\Dir2".ToAbsoluteDirectoryPath(); Assert.IsFalse(absoluteAbsoluteDirectoryPath1.Equals(absoluteAbsoluteDirectoryPath2)); absoluteAbsoluteDirectoryPath1 = @"C:\Dir1\Dir2".ToAbsoluteDirectoryPath(); absoluteAbsoluteDirectoryPath2 = @"C:\Dir1\Dir2\Dir2".ToAbsoluteDirectoryPath(); Assert.IsFalse(absoluteAbsoluteDirectoryPath1.Equals(absoluteAbsoluteDirectoryPath2)); absoluteAbsoluteDirectoryPath1 = @"C:\Dir1\Dir2".ToAbsoluteDirectoryPath(); absoluteAbsoluteDirectoryPath2 = @"C:\Dir1\Dir".ToAbsoluteDirectoryPath(); Assert.IsFalse(absoluteAbsoluteDirectoryPath1.Equals(absoluteAbsoluteDirectoryPath2)); // // Mix between AbsoluteDirectoryPath and RelativeDirectoryPath // relativeRelativeDirectoryPath1 = @"..\Dir1\Dir2".ToRelativeDirectoryPath(); absoluteAbsoluteDirectoryPath1 = @"C:\Dir1\Dir2".ToAbsoluteDirectoryPath(); Assert.IsFalse(absoluteAbsoluteDirectoryPath1.Equals(relativeRelativeDirectoryPath1)); }
public void Test_GetAbsolutePathPathWithError3() { IRelativeDirectoryPath relativeDirectoryPathTo = @"..\..\Dir1".ToRelativeDirectoryPath(); IAbsoluteDirectoryPath absoluteDirectoryPathFrom = @"C:\Dir1".ToAbsoluteDirectoryPath(); Assert.IsFalse(relativeDirectoryPathTo.CanGetAbsolutePathFrom(absoluteDirectoryPathFrom)); string failureReason; Assert.IsFalse(relativeDirectoryPathTo.CanGetAbsolutePathFrom(absoluteDirectoryPathFrom, out failureReason)); Assert.IsTrue(failureReason == @"Cannot resolve pathTo.TryGetAbsolutePath(pathFrom) because there are too many parent dirs in pathTo: PathFrom = ""C:\Dir1"" PathTo = ""..\..\Dir1"""); Assert.Throws(typeof(ArgumentException), delegate { relativeDirectoryPathTo.GetAbsolutePathFrom(absoluteDirectoryPathFrom); }); }
private static string GetRelativeDirectoryPath(IRelativeDirectoryPath relativeDirectoryPath) { if (!relativeDirectoryPath.HasParentDirectory) { return(relativeDirectoryPath.DirectoryName); } IRelativeDirectoryPath parentDirectory = relativeDirectoryPath.ParentDirectoryPath; string parentDirectoryPath = GetRelativeDirectoryPath(parentDirectory); if (string.IsNullOrEmpty(parentDirectoryPath)) { return(relativeDirectoryPath.DirectoryName); } return(GetRelativeDirectoryPath(parentDirectory) + '/' + relativeDirectoryPath.DirectoryName); }
private string?GetBasePathForAppending(int parentDirs) { // TODO: Can be optimized so that parent directory instances are not created. if (parentDirs == -1) { return(string.Empty); } IRelativeDirectoryPath currentDir = this; for (int i = 0; i < parentDirs; i++) { currentDir = currentDir.ParentDirectory; if (currentDir == null) { return(null); } } return(currentDir.PathDisplay); }
public static IRelativeDirectoryPath Combine(this IRelativeDirectoryPath dir, IRelativeDirectoryPath dir2) { return(Combine(dir, dir2, dir.RelativeRoute.GetDirectoryPath)); }
public static IRelativeFilePathExt Combine(this IRelativeDirectoryPath dir, IRelativeFilePathExt file) { return(CombineExt(dir, file, dir.RelativeRoute.GetFilePathWithExtension)); }
public static IRelativeFilePath Combine(this IRelativeDirectoryPath dir, IRelativeFilePath file) { return(Combine(dir, file, dir.RelativeRoute.GetFilePath)); }
public void Test_PathEquality() { // // RelativeFilePath // IRelativeFilePath relativeFilePath1 = @"..\Dir1\File.txt".ToRelativeFilePath(); IRelativeFilePath relativeFilePath2 = @"..\\dir1//file.TXT".ToRelativeFilePath(); Assert.IsTrue(relativeFilePath1.Equals(relativeFilePath2)); relativeFilePath1 = @"..\Dir1\File.txt".ToRelativeFilePath(); relativeFilePath2 = @".\Dir1\File.txt".ToRelativeFilePath(); Assert.IsFalse(relativeFilePath1.Equals(relativeFilePath2)); relativeFilePath1 = @"..\Dir1\File.txt".ToRelativeFilePath(); relativeFilePath2 = @"..\Dir1\Dir2\File.txt".ToRelativeFilePath(); Assert.IsFalse(relativeFilePath1.Equals(relativeFilePath2)); relativeFilePath1 = @"..\Dir1\File.txt".ToRelativeFilePath(); relativeFilePath2 = @"..\Dir1\File.tx".ToRelativeFilePath(); Assert.IsFalse(relativeFilePath1.Equals(relativeFilePath2)); // // AbsoluteFilePath // IAbsoluteFilePath absoluteAbsoluteFilePath1 = @"C:\Dir1\File.txt".ToAbsoluteFilePath(); IAbsoluteFilePath absoluteAbsoluteFilePath2 = @"C:\\dir1//file.TXT".ToAbsoluteFilePath(); Assert.IsTrue(absoluteAbsoluteFilePath1.Equals(absoluteAbsoluteFilePath2)); absoluteAbsoluteFilePath1 = @"C:\Dir1\File.txt".ToAbsoluteFilePath(); absoluteAbsoluteFilePath2 = @"D:\Dir1\File.txt".ToAbsoluteFilePath(); Assert.IsFalse(absoluteAbsoluteFilePath1.Equals(absoluteAbsoluteFilePath2)); absoluteAbsoluteFilePath1 = @"C:\Dir1\File.txt".ToAbsoluteFilePath(); absoluteAbsoluteFilePath2 = @"C:\Dir1\Dir2\File.txt".ToAbsoluteFilePath(); Assert.IsFalse(absoluteAbsoluteFilePath1.Equals(absoluteAbsoluteFilePath2)); absoluteAbsoluteFilePath1 = @"C:\Dir1\File.txt".ToAbsoluteFilePath(); absoluteAbsoluteFilePath2 = @"C:\Dir1\File.tx".ToAbsoluteFilePath(); Assert.IsFalse(absoluteAbsoluteFilePath1.Equals(absoluteAbsoluteFilePath2)); // // Mix between AbsoluteFilePath and RelativeFilePath // relativeFilePath1 = @"..\Dir1\File.txt".ToRelativeFilePath(); absoluteAbsoluteFilePath1 = @"C:\Dir1\File.txt".ToAbsoluteFilePath(); Assert.IsFalse(absoluteAbsoluteFilePath1.Equals(relativeFilePath1)); // // Mix between absoluteDirectoryPath and filePath // IAbsoluteDirectoryPath absoluteAbsoluteDirectoryPath1 = @"C:\Dir1\File".ToAbsoluteDirectoryPath(); absoluteAbsoluteFilePath1 = @"C:\Dir1\File".ToAbsoluteFilePath(); Assert.IsFalse(absoluteAbsoluteDirectoryPath1.Equals(absoluteAbsoluteFilePath1)); Assert.IsFalse(absoluteAbsoluteFilePath1.Equals(absoluteAbsoluteDirectoryPath1)); IRelativeDirectoryPath relativeRelativeDirectoryPath1 = @"..\Dir1\File".ToRelativeDirectoryPath(); relativeFilePath1 = @"..\Dir1\File".ToRelativeFilePath(); Assert.IsFalse(relativeRelativeDirectoryPath1.Equals(relativeFilePath1)); Assert.IsFalse(relativeFilePath1.Equals(relativeRelativeDirectoryPath1)); }
private void BuildFileContent(IAbsoluteDirectoryPath modPackagePath, IRelativeDirectoryPath relativeTargetPath, IRelativeFilePath relativePath, ICollection<RootRpf> list, ContentAction action, FileType type = FileType.Default) { if (_config.AudioPathsOnly && !IsAudioPath(relativeTargetPath)) return; var rpfFile = RpfFile.FromPath(relativeTargetPath.GetAbsolutePathFrom(_gameDir)); if (!rpfFile.ExternalRpfFile.Exists) { throw new Exception("Unable to find an RPF file: " + rpfFile.ExternalRpfFile); } IDirectory root = list.FirstOrDefault(x => x.FilePath.Equals(rpfFile.ExternalRpfFile)); if (root == null) { var rootRpf = new RootRpf(rpfFile.ExternalRpfFile); root = rootRpf; list.Add(rootRpf); } foreach (var p in rpfFile.PathParts) { if (p.EndsWith(".rpf")) { root = root.Contents.ContainsKey(p) ? (InnerRpf)root.Contents[p] : (InnerRpf)(root.Contents[p] = new InnerRpf()); } else { root = root.Contents.ContainsKey(p) ? (InnerDirectory)root.Contents[p] : (InnerDirectory)(root.Contents[p] = new InnerDirectory()); } } var file = relativePath.FileName; IFileContent f; if (file.EndsWith(".rpf")) f = root.Contents.ContainsKey(file) ? (InnerRpf)root.Contents[file] : (InnerRpf)(root.Contents[file] = new InnerRpf()); else f = root.Contents.ContainsKey(file) ? (InnerFile)root.Contents[file] : (InnerFile)(root.Contents[file] = new InnerFile()); f.FilePath = relativePath.GetAbsolutePathFrom(modPackagePath); f.Action = action; f.Type = type; }
///<summary> ///Try get a new <see cref="IAbsoluteDirectoryPath"/> object from this string. ///</summary> ///<returns><i>true</i> if <paramref name="pathString"/> is a valid relative directory path and as a consequence, the returned <paramref name="relativeDirectoryPath"/> is not null.</returns> ///<remarks>The path represented by this string doesn't need to exist for this operation to complete properly.</remarks> ///<param name="pathString">Represents the path.</param> ///<param name="relativeDirectoryPath">If this method returns <i>true</i>, this is the returned path object.</param> public static bool TryGetRelativeDirectoryPath(this string pathString, out IRelativeDirectoryPath relativeDirectoryPath) { string failureReasonUnused; return pathString.TryGetRelativeDirectoryPath(out relativeDirectoryPath, out failureReasonUnused); }
///<summary> ///Try get a new <see cref="IAbsoluteDirectoryPath"/> object from this string. ///</summary> ///<returns><i>true</i> if <paramref name="pathString"/> is a valid relative directory path and as a consequence, the returned <paramref name="relativeDirectoryPath"/> is not null.</returns> ///<remarks>The path represented by this string doesn't need to exist for this operation to complete properly.</remarks> ///<param name="pathString">Represents the path.</param> ///<param name="relativeDirectoryPath">If this method returns <i>true</i>, this is the returned path object.</param> ///<param name="failureReason">If this method returns <i>false</i>, this is the plain english description of the failure.</param> public static bool TryGetRelativeDirectoryPath(this string pathString, out IRelativeDirectoryPath relativeDirectoryPath, out string failureReason) { relativeDirectoryPath = null; if (pathString.IsPathStringNullOrEmpty(out failureReason)) { return false; } if (!pathString.IsValidRelativeDirectoryPath(out failureReason)) { return false; } relativeDirectoryPath = new RelativeDirectoryPath(pathString); return true; }
/// <summary> /// Try get a new <see cref="IAbsoluteDirectoryPath" /> object from this string. /// </summary> /// <returns> /// <i>true</i> if <paramref name="path" /> is a valid relative directory path and as a consequence, the returned /// <paramref name="relativePath" /> is not null. /// </returns> /// <remarks>The path represented by this string doesn't need to exist for this operation to complete properly.</remarks> /// <param name="path">Represents the path.</param> /// <param name="relativePath">If this method returns <i>true</i>, this is the returned path object.</param> public static bool TryGetRelativeDirectoryPath(this string path, out IRelativeDirectoryPath relativePath) { string failureMessage; return path.TryGetRelativeDirectoryPath(out relativePath, out failureMessage); }
/// <summary> /// Try get a new <see cref="IAbsoluteDirectoryPath" /> object from this string. /// </summary> /// <returns> /// <i>true</i> if <paramref name="path" /> is a valid relative directory path and as a consequence, the returned /// <paramref name="relativePath" /> is not null. /// </returns> /// <remarks>The path represented by this string doesn't need to exist for this operation to complete properly.</remarks> /// <param name="path">Represents the path.</param> /// <param name="relativePath">If this method returns <i>true</i>, this is the returned path object.</param> /// <param name="failureMessage">If this method returns <i>false</i>, this is the plain english description of the failure.</param> public static bool TryGetRelativeDirectoryPath(this string path, out IRelativeDirectoryPath relativePath, out string failureMessage) { relativePath = null; if (IsNullOrEmpty(() => path, out failureMessage)) { return false; } if (!path.IsValidRelativeDirectoryPath(out failureMessage)) { return false; } relativePath = new RelativeDirectoryPath(path); return true; }
public void Test_GetChildWithName_Error8() { IRelativeDirectoryPath relativeDirectoryPath = @"..\Dir1\Dir2".ToRelativeDirectoryPath(); //RelativeDirectoryPath.GetChildDirectoryWithName(null); }
public IRelativeDirectoryPath Combine(IRelativeDirectoryPath dir) => (IRelativeDirectoryPath)Combine(dir, nameof(dir), nameof(dir));
public static bool TryGetRelativeDirectoryPath( string path, out IRelativeDirectoryPath relativeDirectoryPath) { throw new NotImplementedException(); }