Skip to content

Commit

Permalink
EmuHawk: (mono) fix multi-disk bundler by removing the managed Win32 …
Browse files Browse the repository at this point in the history
…shlwapi.dll calls - TASEmulators#1430
  • Loading branch information
Asnivor committed Jan 16, 2019
1 parent f01f0fa commit ebd94a8
Showing 1 changed file with 47 additions and 12 deletions.
59 changes: 47 additions & 12 deletions BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,21 +280,56 @@ private void BrowseBtn_Click(object sender, EventArgs e)
// http://stackoverflow.com/questions/275689/how-to-get-relative-path-from-absolute-path
public static string GetRelativePath(string fromPath, string toPath)
{
Win32.FileAttributes fromAttr = GetPathAttribute(fromPath);
Win32.FileAttributes toAttr = GetPathAttribute(toPath);

var path = new StringBuilder(260); // MAX_PATH
if (Win32.PathRelativePathTo(
path,
fromPath,
fromAttr,
toPath,
toAttr) == false)
if (!BizHawk.Common.PlatformLinkedLibSingleton.RunningOnUnix)
{
throw new ArgumentException("Paths must have a common prefix");
Win32.FileAttributes fromAttr = GetPathAttribute(fromPath);
Win32.FileAttributes toAttr = GetPathAttribute(toPath);

var path = new StringBuilder(260); // MAX_PATH
if (Win32.PathRelativePathTo(
path,
fromPath,
fromAttr,
toPath,
toAttr) == false)
{
throw new ArgumentException("Paths must have a common prefix");
}

return path.ToString();
}
else
{
// mono doesnt know about shlwapi.dll that the code above tries to call
// it is available in WINE, but since this is the only call to that function,
// it's probably easier to get the relative path a different way
// this may actually work on windows as well - so maybe can replace the above when we move to .netcore
var dirSepChar = Path.DirectorySeparatorChar;
string from = !fromPath.EndsWith(dirSepChar.ToString())
? fromPath + dirSepChar
: fromPath;
string to = !toPath.EndsWith(dirSepChar.ToString())
? toPath + dirSepChar
: toPath;

Uri fromUri = new Uri(from);
Uri toUri = new Uri(to);

if (fromUri.Scheme != toUri.Scheme)
{
return toPath;
}

Uri relativeUri = fromUri.MakeRelativeUri(toUri);
string relativePath = Uri.UnescapeDataString(relativeUri.ToString());

if (string.Equals(toUri.Scheme, Uri.UriSchemeFile, StringComparison.OrdinalIgnoreCase))
{
relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
}

return path.ToString();
return relativePath.TrimEnd(dirSepChar);
}
}

private static Win32.FileAttributes GetPathAttribute(string path)
Expand Down

0 comments on commit ebd94a8

Please sign in to comment.