diff --git a/src/SMAPI.Tests/Core/PathUtilitiesTests.cs b/src/SMAPI.Tests/Core/PathUtilitiesTests.cs
new file mode 100644
index 00000000..268ba504
--- /dev/null
+++ b/src/SMAPI.Tests/Core/PathUtilitiesTests.cs
@@ -0,0 +1,70 @@
+using NUnit.Framework;
+using StardewModdingAPI.Framework.Utilities;
+
+namespace StardewModdingAPI.Tests.Core
+{
+ /// Unit tests for .
+ [TestFixture]
+ public class PathUtilitiesTests
+ {
+ /*********
+ ** Unit tests
+ *********/
+ [Test(Description = "Assert that GetSegments returns the expected values.")]
+ [TestCase("", ExpectedResult = "")]
+ [TestCase("/", ExpectedResult = "")]
+ [TestCase("///", ExpectedResult = "")]
+ [TestCase("/usr/bin", ExpectedResult = "usr|bin")]
+ [TestCase("/usr//bin//", ExpectedResult = "usr|bin")]
+ [TestCase("/usr//bin//.././boop.exe", ExpectedResult = "usr|bin|..|.|boop.exe")]
+ [TestCase(@"C:", ExpectedResult = "C:")]
+ [TestCase(@"C:/boop", ExpectedResult = "C:|boop")]
+ [TestCase(@"C:\boop\/usr//bin//.././boop.exe", ExpectedResult = "C:|boop|usr|bin|..|.|boop.exe")]
+ public string GetSegments(string path)
+ {
+ return string.Join("|", PathUtilities.GetSegments(path));
+ }
+
+ [Test(Description = "Assert that NormalisePathSeparators returns the expected values.")]
+#if SMAPI_FOR_WINDOWS
+ [TestCase("", ExpectedResult = "")]
+ [TestCase("/", ExpectedResult = "")]
+ [TestCase("///", ExpectedResult = "")]
+ [TestCase("/usr/bin", ExpectedResult = @"usr\bin")]
+ [TestCase("/usr//bin//", ExpectedResult = @"usr\bin")]
+ [TestCase("/usr//bin//.././boop.exe", ExpectedResult = @"usr\bin\..\.\boop.exe")]
+ [TestCase("C:", ExpectedResult = "C:")]
+ [TestCase("C:/boop", ExpectedResult = @"C:\boop")]
+ [TestCase(@"C:\usr\bin//.././boop.exe", ExpectedResult = @"C:\usr\bin\..\.\boop.exe")]
+#else
+ [TestCase("", ExpectedResult = "")]
+ [TestCase("/", ExpectedResult = "/")]
+ [TestCase("///", ExpectedResult = "/")]
+ [TestCase("/usr/bin", ExpectedResult = "/usr/bin")]
+ [TestCase("/usr//bin//", ExpectedResult = "/usr/bin")]
+ [TestCase("/usr//bin//.././boop.exe", ExpectedResult = "/usr/bin/.././boop.exe")]
+ [TestCase("C:", ExpectedResult = "C:")]
+ [TestCase("C:/boop", ExpectedResult = "C:/boop")]
+ [TestCase(@"C:\usr\bin//.././boop.exe", ExpectedResult = "C:/usr/bin/.././boop.exe")]
+#endif
+ public string NormalisePathSeparators(string path)
+ {
+ return PathUtilities.NormalisePathSeparators(path);
+ }
+
+ [Test(Description = "Assert that GetRelativePath returns the expected values.")]
+#if SMAPI_FOR_WINDOWS
+ [TestCase(@"C:\", @"C:\", ExpectedResult = "./")]
+ [TestCase(@"C:\grandparent\parent\child", @"C:\grandparent\parent\sibling", ExpectedResult = @"..\sibling")]
+ [TestCase(@"C:\grandparent\parent\child", @"C:\cousin\file.exe", ExpectedResult = @"..\..\..\cousin\file.exe")]
+#else
+ [TestCase("/", "/", ExpectedResult = "./")]
+ [TestCase("/grandparent/parent/child", "/grandparent/parent/sibling", ExpectedResult = "../sibling")]
+ [TestCase("/grandparent/parent/child", "/cousin/file.exe", ExpectedResult = "../../../cousin/file.exe")]
+#endif
+ public string GetRelativePath(string sourceDir, string targetPath)
+ {
+ return PathUtilities.GetRelativePath(sourceDir, targetPath);
+ }
+ }
+}
diff --git a/src/SMAPI.Tests/StardewModdingAPI.Tests.csproj b/src/SMAPI.Tests/StardewModdingAPI.Tests.csproj
index 6e7fa1f0..e4b2c8c6 100644
--- a/src/SMAPI.Tests/StardewModdingAPI.Tests.csproj
+++ b/src/SMAPI.Tests/StardewModdingAPI.Tests.csproj
@@ -49,6 +49,7 @@
Properties\GlobalAssemblyInfo.cs
+
diff --git a/src/SMAPI/Framework/Content/ContentCache.cs b/src/SMAPI/Framework/Content/ContentCache.cs
index 4508e641..533da398 100644
--- a/src/SMAPI/Framework/Content/ContentCache.cs
+++ b/src/SMAPI/Framework/Content/ContentCache.cs
@@ -5,6 +5,7 @@ using System.Linq;
using Microsoft.Xna.Framework;
using StardewModdingAPI.Framework.ModLoading;
using StardewModdingAPI.Framework.Reflection;
+using StardewModdingAPI.Framework.Utilities;
using StardewValley;
namespace StardewModdingAPI.Framework.Content
@@ -18,12 +19,6 @@ namespace StardewModdingAPI.Framework.Content
/// The underlying asset cache.
private readonly IDictionary Cache;
- /// The possible directory separator characters in an asset key.
- private readonly char[] PossiblePathSeparators;
-
- /// The preferred directory separator chaeacter in an asset key.
- private readonly string PreferredPathSeparator;
-
/// Applies platform-specific asset key normalisation so it's consistent with the underlying cache.
private readonly Func NormaliseAssetNameForPlatform;
@@ -52,14 +47,10 @@ namespace StardewModdingAPI.Framework.Content
/// Construct an instance.
/// The underlying content manager whose cache to manage.
/// Simplifies access to private game code.
- /// The possible directory separator characters in an asset key.
- /// The preferred directory separator chaeacter in an asset key.
- public ContentCache(LocalizedContentManager contentManager, Reflector reflection, char[] possiblePathSeparators, string preferredPathSeparator)
+ public ContentCache(LocalizedContentManager contentManager, Reflector reflection)
{
// init
this.Cache = reflection.GetField>(contentManager, "loadedAssets").GetValue();
- this.PossiblePathSeparators = possiblePathSeparators;
- this.PreferredPathSeparator = preferredPathSeparator;
// get key normalisation logic
if (Constants.TargetPlatform == Platform.Windows)
@@ -90,11 +81,7 @@ namespace StardewModdingAPI.Framework.Content
[Pure]
public string NormalisePathSeparators(string path)
{
- string[] parts = path.Split(this.PossiblePathSeparators, StringSplitOptions.RemoveEmptyEntries);
- string normalised = string.Join(this.PreferredPathSeparator, parts);
- if (path.StartsWith(this.PreferredPathSeparator))
- normalised = this.PreferredPathSeparator + normalised; // keep root slash
- return normalised;
+ return PathUtilities.NormalisePathSeparators(path);
}
/// Normalise a cache key so it's consistent with the underlying cache.
diff --git a/src/SMAPI/Framework/ModHelpers/ContentHelper.cs b/src/SMAPI/Framework/ModHelpers/ContentHelper.cs
index 4a1d3853..7d8bec1e 100644
--- a/src/SMAPI/Framework/ModHelpers/ContentHelper.cs
+++ b/src/SMAPI/Framework/ModHelpers/ContentHelper.cs
@@ -8,6 +8,7 @@ using System.Linq;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI.Framework.Exceptions;
+using StardewModdingAPI.Framework.Utilities;
using StardewValley;
using xTile;
using xTile.Format;
@@ -238,7 +239,7 @@ namespace StardewModdingAPI.Framework.ModHelpers
string imageSource = tilesheet.ImageSource;
// validate tilesheet path
- if (Path.IsPathRooted(imageSource) || imageSource.Split(SContentManager.PossiblePathSeparators).Contains(".."))
+ if (Path.IsPathRooted(imageSource) || PathUtilities.GetSegments(imageSource).Contains(".."))
throw new ContentLoadException($"The '{imageSource}' tilesheet couldn't be loaded. Tilesheet paths must be a relative path without directory climbing (../).");
// get seasonal name (if applicable)
diff --git a/src/SMAPI/Framework/SContentManager.cs b/src/SMAPI/Framework/SContentManager.cs
index 463fea0b..fa51bd53 100644
--- a/src/SMAPI/Framework/SContentManager.cs
+++ b/src/SMAPI/Framework/SContentManager.cs
@@ -35,9 +35,6 @@ namespace StardewModdingAPI.Framework
/*********
** Properties
*********/
- /// The preferred directory separator chaeacter in an asset key.
- private static readonly string PreferredPathSeparator = Path.DirectorySeparatorChar.ToString();
-
/// Encapsulates monitoring and logging.
private readonly IMonitor Monitor;
@@ -75,9 +72,6 @@ namespace StardewModdingAPI.Framework
/// Interceptors which edit matching assets after they're loaded.
internal IDictionary> Editors { get; } = new Dictionary>();
- /// The possible directory separator characters in an asset key.
- internal static readonly char[] PossiblePathSeparators = new[] { '/', '\\', Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }.Distinct().ToArray();
-
/// The absolute path to the .
internal string FullRootDirectory => Path.Combine(Constants.ExecutionPath, this.RootDirectory);
@@ -100,7 +94,7 @@ namespace StardewModdingAPI.Framework
{
// init
this.Monitor = monitor ?? throw new ArgumentNullException(nameof(monitor));
- this.Cache = new ContentCache(this, reflection, SContentManager.PossiblePathSeparators, SContentManager.PreferredPathSeparator);
+ this.Cache = new ContentCache(this, reflection);
this.GetKeyLocale = reflection.GetMethod(this, "languageCode");
this.ModContentPrefix = this.GetAssetNameFromFilePath(Constants.ModPath);
@@ -399,15 +393,7 @@ namespace StardewModdingAPI.Framework
/// The target file path.
private string GetRelativePath(string targetPath)
{
- // convert to URIs
- Uri from = new Uri(this.FullRootDirectory + "/");
- Uri to = new Uri(targetPath + "/");
- if (from.Scheme != to.Scheme)
- throw new InvalidOperationException($"Can't get path for '{targetPath}' relative to '{this.FullRootDirectory}'.");
-
- // get relative path
- return Uri.UnescapeDataString(from.MakeRelativeUri(to).ToString())
- .Replace(Path.DirectorySeparatorChar == '/' ? '\\' : '/', Path.DirectorySeparatorChar); // use correct separator for platform
+ return PathUtilities.GetRelativePath(this.FullRootDirectory, targetPath);
}
/// Get the locale codes (like ja-JP) used in asset keys.
diff --git a/src/SMAPI/Framework/Utilities/PathUtilities.cs b/src/SMAPI/Framework/Utilities/PathUtilities.cs
new file mode 100644
index 00000000..4fa521f1
--- /dev/null
+++ b/src/SMAPI/Framework/Utilities/PathUtilities.cs
@@ -0,0 +1,59 @@
+using System;
+using System.Diagnostics.Contracts;
+using System.IO;
+using System.Linq;
+
+namespace StardewModdingAPI.Framework.Utilities
+{
+ /// Provides utilities for normalising file paths.
+ internal static class PathUtilities
+ {
+ /*********
+ ** Properties
+ *********/
+ /// The possible directory separator characters in a file path.
+ private static readonly char[] PossiblePathSeparators = new[] { '/', '\\', Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }.Distinct().ToArray();
+
+ /// The preferred directory separator chaeacter in an asset key.
+ private static readonly string PreferredPathSeparator = Path.DirectorySeparatorChar.ToString();
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// Get the segments from a path (e.g. /usr/bin/boop => usr, bin, and boop).
+ /// The path to split.
+ public static string[] GetSegments(string path)
+ {
+ return path.Split(PathUtilities.PossiblePathSeparators, StringSplitOptions.RemoveEmptyEntries);
+ }
+
+ /// Normalise path separators in a file path.
+ /// The file path to normalise.
+ [Pure]
+ public static string NormalisePathSeparators(string path)
+ {
+ string[] parts = PathUtilities.GetSegments(path);
+ string normalised = string.Join(PathUtilities.PreferredPathSeparator, parts);
+ if (path.StartsWith(PathUtilities.PreferredPathSeparator))
+ normalised = PathUtilities.PreferredPathSeparator + normalised; // keep root slash
+ return normalised;
+ }
+
+ /// Get a directory or file path relative to a given source path.
+ /// The source folder path.
+ /// The target folder or file path.
+ [Pure]
+ public static string GetRelativePath(string sourceDir, string targetPath)
+ {
+ // convert to URIs
+ Uri from = new Uri(sourceDir.TrimEnd(PathUtilities.PossiblePathSeparators) + "/");
+ Uri to = new Uri(targetPath.TrimEnd(PathUtilities.PossiblePathSeparators) + "/");
+ if (from.Scheme != to.Scheme)
+ throw new InvalidOperationException($"Can't get path for '{targetPath}' relative to '{sourceDir}'.");
+
+ // get relative path
+ return PathUtilities.NormalisePathSeparators(Uri.UnescapeDataString(from.MakeRelativeUri(to).ToString()));
+ }
+ }
+}
diff --git a/src/SMAPI/StardewModdingAPI.csproj b/src/SMAPI/StardewModdingAPI.csproj
index 562da60d..14ed1531 100644
--- a/src/SMAPI/StardewModdingAPI.csproj
+++ b/src/SMAPI/StardewModdingAPI.csproj
@@ -125,6 +125,7 @@
+