Skip to content

Commit 7481c72

Browse files
alex-vazquez-unity3dEvergreen
authored andcommitted
SRP Core utils - Make sure the folder separator is the same for all the platforms.
1 parent afb06fe commit 7481c72

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

Packages/com.unity.render-pipelines.core/Runtime/Utilities/CoreUtils.cs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,32 +1845,48 @@ public static IEnumerable<T> LoadAllAssets<T>(string extension = "asset", bool a
18451845
}
18461846
}
18471847

1848+
const char k_DirectorySeparatorChar = '/';
1849+
18481850
/// <summary>
18491851
/// Create any missing folders in the file path given.
18501852
/// </summary>
1851-
/// <param name="filePath">File or folder (ending with '/') path to ensure existence of each subfolder in. </param>
1853+
/// <param name="filePath">File or folder (ending with '/') path to ensure existence of each subfolder in.</param>
18521854
public static void EnsureFolderTreeInAssetFilePath(string filePath)
18531855
{
1854-
var path = filePath.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar);
1855-
if (!path.StartsWith("Assets" + Path.DirectorySeparatorChar, StringComparison.CurrentCultureIgnoreCase))
1856-
throw new ArgumentException($"Path should start with \"Assets/\". Got {filePath}.", filePath);
1856+
if (string.IsNullOrEmpty(filePath))
1857+
return;
1858+
1859+
// Normalize to forward slashes (Unity standard)
1860+
var path = filePath.Replace('\\', k_DirectorySeparatorChar);
1861+
1862+
if (!path.StartsWith("Assets/", StringComparison.Ordinal))
1863+
throw new ArgumentException($"Path should start with \"Assets/\". Got {filePath}.", nameof(filePath));
18571864

18581865
var folderPath = Path.GetDirectoryName(path);
18591866

1860-
if (!UnityEditor.AssetDatabase.IsValidFolder(folderPath))
1867+
if (string.IsNullOrEmpty(folderPath))
1868+
return;
1869+
1870+
// GetDirectoryName may reintroduce backslashes on Windows
1871+
folderPath = folderPath.Replace('\\', k_DirectorySeparatorChar);
1872+
1873+
// Early exit if folder already exists
1874+
if (AssetDatabase.IsValidFolder(folderPath))
1875+
return;
1876+
1877+
var folderNames = folderPath.Split(k_DirectorySeparatorChar);
1878+
string currentPath = "Assets";
1879+
1880+
for (int i = 1; i < folderNames.Length; ++i)
18611881
{
1862-
var folderNames = folderPath.Split(Path.DirectorySeparatorChar);
1863-
string rootPath = "";
1864-
foreach (var folderName in folderNames)
1865-
{
1866-
var newPath = rootPath + folderName;
1867-
if (!UnityEditor.AssetDatabase.IsValidFolder(newPath))
1868-
UnityEditor.AssetDatabase.CreateFolder(rootPath.TrimEnd(Path.DirectorySeparatorChar), folderName);
1869-
rootPath = newPath + Path.DirectorySeparatorChar;
1870-
}
1882+
string nextPath = currentPath + k_DirectorySeparatorChar + folderNames[i];
1883+
if (!UnityEditor.AssetDatabase.IsValidFolder(nextPath))
1884+
UnityEditor.AssetDatabase.CreateFolder(currentPath, folderNames[i]);
1885+
currentPath = nextPath;
18711886
}
18721887
}
18731888

1889+
18741890
/// <summary>
18751891
/// Returns the icon for the given type if it has an IconAttribute.
18761892
/// </summary>

Packages/com.unity.render-pipelines.core/Tests/Editor/CoreUtils.Tests.EnsureFolderTreeInAssetFilePath.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Text;
34
using NUnit.Framework;
45
using UnityEngine.Rendering;
56

@@ -23,7 +24,13 @@ partial class CoreUtilsTests
2324
[TestCase("Assets/TestFolder/Folder#Test/SubFolder/", TestName = "Folder name containing a hash character")]
2425
[TestCase("Assets/TestFolder/Folder@Test/SubFolder/File.txt", TestName = "File name containing @ character")]
2526
[TestCase("Assets/TestFolder/Folder\\SubFolder/File", TestName = "File without extension")]
26-
27+
[TestCase("Assets/TestFolder//DoubleSlash/File.txt", TestName = "Path with double slashes")]
28+
[TestCase("Assets/TestFolder///TripleSlash/File.txt", TestName = "Path with triple slashes")]
29+
[TestCase("Assets/TestFolder/Mixed\\Separators/File.txt", TestName = "Mixed forward and backslashes")]
30+
[TestCase("Assets/TestFolder/A/B/C/D/E/F/G/H/I/J/Deep.txt", TestName = "Deep nested path (10 levels)")]
31+
[TestCase("Assets/TestFolder/Unicode文件夹/File.txt", TestName = "Unicode characters in folder name")]
32+
[TestCase("Assets/TestFolder/Émojis😀/File.txt", TestName = "Emoji in folder name")]
33+
[TestCase("Assets/TestFolder/VeryLongFolderNameThatExceedsNormalLength123456789012345678901234567890/File.txt", TestName = "Very long folder name")]
2734
public void EnsureFolderTreeInAssetFilePath(string path)
2835
{
2936
string folderPath = Path.GetDirectoryName(path);
@@ -34,15 +41,31 @@ public void EnsureFolderTreeInAssetFilePath(string path)
3441
[Test]
3542
[TestCase("Assets", TestName = "Just Assets and not Assets/")]
3643
[TestCase("NotAssetsFolder/TestFolder/", TestName = "FilePath does not start with Assets/")]
44+
[TestCase("assets/TestFolder/", TestName = "Lowercase assets (case sensitivity)")]
45+
[TestCase("ASSETS/TestFolder/", TestName = "Uppercase ASSETS")]
46+
[TestCase("FileName.txt", TestName = "FileName.txt")]
47+
[TestCase("C:\\Filename.txt", TestName = "C:\\Filename.txt")]
3748
public void EnsureFolderTreeInAssetFilePathThrows(string folderPath)
3849
{
3950
Assert.Throws<ArgumentException>(() => CoreUtils.EnsureFolderTreeInAssetFilePath(folderPath));
4051
}
4152

53+
[Test]
54+
public void EnsureFolderTreeInAssetFilePath_NullOrEmpty_DoesNotThrow()
55+
{
56+
Assert.DoesNotThrow(() => CoreUtils.EnsureFolderTreeInAssetFilePath(null));
57+
Assert.DoesNotThrow(() => CoreUtils.EnsureFolderTreeInAssetFilePath(""));
58+
Assert.DoesNotThrow(() => CoreUtils.EnsureFolderTreeInAssetFilePath(string.Empty));
59+
}
60+
4261
[TearDown]
4362
public void TearDown()
4463
{
45-
AssetDatabase.DeleteAsset("Assets/TestFolder");
64+
if (AssetDatabase.IsValidFolder("Assets/TestFolder"))
65+
{
66+
AssetDatabase.DeleteAsset("Assets/TestFolder");
67+
}
68+
AssetDatabase.Refresh();
4669
}
4770
}
4871
}

0 commit comments

Comments
 (0)