summaryrefslogtreecommitdiff
path: root/src/SMAPI.Toolkit/Utilities/PathUtilities.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI.Toolkit/Utilities/PathUtilities.cs')
-rw-r--r--src/SMAPI.Toolkit/Utilities/PathUtilities.cs45
1 files changed, 11 insertions, 34 deletions
diff --git a/src/SMAPI.Toolkit/Utilities/PathUtilities.cs b/src/SMAPI.Toolkit/Utilities/PathUtilities.cs
index 020ebc6d..2e9e5eac 100644
--- a/src/SMAPI.Toolkit/Utilities/PathUtilities.cs
+++ b/src/SMAPI.Toolkit/Utilities/PathUtilities.cs
@@ -1,5 +1,4 @@
using System;
-using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
@@ -27,7 +26,7 @@ namespace StardewModdingAPI.Toolkit.Utilities
public static readonly char PreferredPathSeparator = Path.DirectorySeparatorChar;
/// <summary>The preferred directory separator character in an asset key.</summary>
- public static readonly char PreferredAssetSeparator = PathUtilities.PreferredPathSeparator;
+ public static readonly char PreferredAssetSeparator = '/';
/*********
@@ -88,14 +87,18 @@ namespace StardewModdingAPI.Toolkit.Utilities
/// <summary>Get a directory or file path relative to a given source path. If no relative path is possible (e.g. the paths are on different drives), an absolute path is returned.</summary>
/// <param name="sourceDir">The source folder path.</param>
/// <param name="targetPath">The target folder or file path.</param>
- /// <remarks>
- ///
- /// NOTE: this is a heuristic implementation that works in the cases SMAPI needs it for, but it doesn't handle all edge cases (e.g. case-sensitivity on Linux, or traversing between UNC paths on Windows). This should be replaced with the more comprehensive <c>Path.GetRelativePath</c> if the game ever migrates to .NET Core.
- ///
- /// </remarks>
[Pure]
public static string GetRelativePath(string sourceDir, string targetPath)
{
+#if NET5_0
+ return Path.GetRelativePath(sourceDir, targetPath);
+#else
+ // NOTE:
+ // this is a heuristic implementation that works in the cases SMAPI needs it for, but it
+ // doesn't handle all edge cases (e.g. case-sensitivity on Linux, or traversing between
+ // UNC paths on Windows). SMAPI and mods will use the more robust .NET 5 version anyway
+ // though, this is only for compatibility with the mod build package.
+
// convert to URIs
Uri from = new Uri(sourceDir.TrimEnd(PathUtilities.PossiblePathSeparators) + "/");
Uri to = new Uri(targetPath.TrimEnd(PathUtilities.PossiblePathSeparators) + "/");
@@ -123,6 +126,7 @@ namespace StardewModdingAPI.Toolkit.Utilities
}
return relative;
+#endif
}
/// <summary>Get whether a path is relative and doesn't try to climb out of its containing folder (e.g. doesn't contain <c>../</c>).</summary>
@@ -145,32 +149,5 @@ namespace StardewModdingAPI.Toolkit.Utilities
{
return !Regex.IsMatch(str, "[^a-z0-9_.-]", RegexOptions.IgnoreCase);
}
-
- /// <summary>Get the paths which exceed the OS length limit.</summary>
- /// <param name="rootPath">The root path to search.</param>
- internal static IEnumerable<string> GetTooLongPaths(string rootPath)
- {
- if (!Directory.Exists(rootPath))
- return new string[0];
-
- return Directory
- .EnumerateFileSystemEntries(rootPath, "*.*", SearchOption.AllDirectories)
- .Where(PathUtilities.IsPathTooLong);
- }
-
- /// <summary>Get whether a file or directory path exceeds the OS path length limit.</summary>
- /// <param name="path">The path to test.</param>
- internal static bool IsPathTooLong(string path)
- {
- try
- {
- _ = Path.GetFullPath(path);
- return false;
- }
- catch (PathTooLongException)
- {
- return true;
- }
- }
}
}