From 5d001f3aa9adeba74e6a405ae1173e6e594d37a4 Mon Sep 17 00:00:00 2001 From: zeromus Date: Thu, 1 Oct 2020 03:12:42 -0400 Subject: [PATCH] put MultiDiskBundler's GetRelativePath() in PathExtensions.cs. but I didn't end up using it... --- .../MultiDiskBundler/MultiDiskBundler.cs | 32 +------------------ .../Extensions/PathExtensions.cs | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.cs b/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.cs index f44c4b43be..a16029c246 100644 --- a/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.cs +++ b/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.cs @@ -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 } } - /// running on Windows host, and unmanaged call failed - /// running on Windows host, and either path is not a regular file or directory - /// Algorithm for Windows taken from https://stackoverflow.com/a/485516/7467292 - 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(); diff --git a/src/BizHawk.Common/Extensions/PathExtensions.cs b/src/BizHawk.Common/Extensions/PathExtensions.cs index 8c09d34e87..6c644d89cd 100644 --- a/src/BizHawk.Common/Extensions/PathExtensions.cs +++ b/src/BizHawk.Common/Extensions/PathExtensions.cs @@ -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; } + /// running on Windows host, and unmanaged call failed + /// running on Windows host, and either path is not a regular file or directory + /// Algorithm for Windows taken from https://stackoverflow.com/a/485516/7467292 + 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"); + } + /// the absolute path equivalent to which contains %exe% (expanded) as a prefix /// /// returned string omits trailing slash