From 0ed46c09104b823a5784690fa50e7022efc12c8f Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 7 Mar 2021 18:04:48 -0500 Subject: add descriptive error for PathTooLongException which crashes SMAPI or the installer --- src/SMAPI.Toolkit/Utilities/PathUtilities.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/SMAPI.Toolkit/Utilities') diff --git a/src/SMAPI.Toolkit/Utilities/PathUtilities.cs b/src/SMAPI.Toolkit/Utilities/PathUtilities.cs index c9fb6213..a394edba 100644 --- a/src/SMAPI.Toolkit/Utilities/PathUtilities.cs +++ b/src/SMAPI.Toolkit/Utilities/PathUtilities.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics.Contracts; using System.IO; using System.Linq; @@ -133,5 +134,29 @@ namespace StardewModdingAPI.Toolkit.Utilities { return !Regex.IsMatch(str, "[^a-z0-9_.-]", RegexOptions.IgnoreCase); } + + /// Get the paths which exceed the OS length limit. + /// The root path to search. + internal static IEnumerable GetTooLongPaths(string rootPath) + { + return Directory + .EnumerateFileSystemEntries(rootPath, "*.*", SearchOption.AllDirectories) + .Where(PathUtilities.IsPathTooLong); + } + + /// Get whether a file or directory path exceeds the OS path length limit. + /// The path to test. + internal static bool IsPathTooLong(string path) + { + try + { + _ = Path.GetFullPath(path); + return false; + } + catch (PathTooLongException) + { + return true; + } + } } } -- cgit