put MultiDiskBundler's GetRelativePath() in PathExtensions.cs. but I didn't end up using it...

This commit is contained in:
zeromus 2020-10-01 03:12:42 -04:00
parent 99f9b9b447
commit 5d001f3aa9
2 changed files with 33 additions and 31 deletions

View File

@ -214,7 +214,7 @@ namespace BizHawk.Client.EmuHawk
new XElement("LoadAssets",
names.Select(n => new XElement(
"Asset",
new XAttribute("FileName", GetRelativePath(basePath, n))
new XAttribute("FileName", PathExtensions.GetRelativePath(basePath, n))
))
)
);
@ -270,36 +270,6 @@ namespace BizHawk.Client.EmuHawk
}
}
/// <exception cref="ArgumentException">running on Windows host, and unmanaged call failed</exception>
/// <exception cref="FileNotFoundException">running on Windows host, and either path is not a regular file or directory</exception>
/// <remarks>Algorithm for Windows taken from https://stackoverflow.com/a/485516/7467292</remarks>
public static string GetRelativePath(string fromPath, string toPath)
{
if (OSTailoredCode.IsUnixHost) return fromPath.MakeRelativeTo(toPath);
//TODO merge this with the Windows implementation in PathExtensions.MakeRelativeTo
static FileAttributes GetPathAttribute(string path1)
{
var di = new DirectoryInfo(path1.Split('|').First());
if (di.Exists)
{
return FileAttributes.Directory;
}
var fi = new FileInfo(path1.Split('|').First());
if (fi.Exists)
{
return FileAttributes.Normal;
}
throw new FileNotFoundException();
}
var path = new StringBuilder(260 /* = MAX_PATH */);
return Win32Imports.PathRelativePathTo(path, fromPath, GetPathAttribute(fromPath), toPath, GetPathAttribute(toPath))
? path.ToString()
: throw new ArgumentException("Paths must have a common prefix");
}
private void SystemDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
Recalculate();

View File

@ -1,6 +1,8 @@
using System;
using System.Text;
using System.IO;
using System.Reflection;
using System.Linq;
using BizHawk.Common.StringExtensions;
@ -45,6 +47,36 @@ namespace BizHawk.Common.PathExtensions
return false;
}
/// <exception cref="ArgumentException">running on Windows host, and unmanaged call failed</exception>
/// <exception cref="FileNotFoundException">running on Windows host, and either path is not a regular file or directory</exception>
/// <remarks>Algorithm for Windows taken from https://stackoverflow.com/a/485516/7467292</remarks>
public static string GetRelativePath(string fromPath, string toPath)
{
if (OSTailoredCode.IsUnixHost) return fromPath.MakeRelativeTo(toPath);
//TODO merge this with the Windows implementation in PathExtensions.MakeRelativeTo
static FileAttributes GetPathAttribute(string path1)
{
var di = new DirectoryInfo(path1.Split('|').First());
if (di.Exists)
{
return FileAttributes.Directory;
}
var fi = new FileInfo(path1.Split('|').First());
if (fi.Exists)
{
return FileAttributes.Normal;
}
throw new FileNotFoundException();
}
var path = new StringBuilder(260 /* = MAX_PATH */);
return Win32Imports.PathRelativePathTo(path, fromPath, GetPathAttribute(fromPath), toPath, GetPathAttribute(toPath))
? path.ToString()
: throw new ArgumentException("Paths must have a common prefix");
}
/// <returns>the absolute path equivalent to <paramref name="path"/> which contains <c>%exe%</c> (expanded) as a prefix</returns>
/// <remarks>
/// returned string omits trailing slash<br/>