Skip to content

Commit 590df37

Browse files
svc-reach-platform-supportEvergreen
authored andcommitted
[Port] [6000.4] SRP Core utils - Make sure the folder separator is the same for all the platforms.
1 parent 4573144 commit 590df37

2 files changed

Lines changed: 55 additions & 16 deletions

File tree

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

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

1841+
const char k_DirectorySeparatorChar = '/';
1842+
18411843
/// <summary>
18421844
/// Create any missing folders in the file path given.
18431845
/// </summary>
1844-
/// <param name="filePath">File or folder (ending with '/') path to ensure existence of each subfolder in. </param>
1846+
/// <param name="filePath">File or folder (ending with '/') path to ensure existence of each subfolder in.</param>
18451847
public static void EnsureFolderTreeInAssetFilePath(string filePath)
18461848
{
1847-
var path = filePath.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar);
1848-
if (!path.StartsWith("Assets" + Path.DirectorySeparatorChar, StringComparison.CurrentCultureIgnoreCase))
1849-
throw new ArgumentException($"Path should start with \"Assets/\". Got {filePath}.", filePath);
1849+
if (string.IsNullOrEmpty(filePath))
1850+
return;
1851+
1852+
// Normalize to forward slashes (Unity standard)
1853+
var path = filePath.Replace('\\', k_DirectorySeparatorChar);
1854+
1855+
if (!path.StartsWith("Assets/", StringComparison.Ordinal))
1856+
throw new ArgumentException($"Path should start with \"Assets/\". Got {filePath}.", nameof(filePath));
18501857

18511858
var folderPath = Path.GetDirectoryName(path);
18521859

1853-
if (!UnityEditor.AssetDatabase.IsValidFolder(folderPath))
1860+
if (string.IsNullOrEmpty(folderPath))
1861+
return;
1862+
1863+
// GetDirectoryName may reintroduce backslashes on Windows
1864+
folderPath = folderPath.Replace('\\', k_DirectorySeparatorChar);
1865+
1866+
// Early exit if folder already exists
1867+
if (AssetDatabase.IsValidFolder(folderPath))
1868+
return;
1869+
1870+
var folderNames = folderPath.Split(k_DirectorySeparatorChar);
1871+
string currentPath = "Assets";
1872+
1873+
for (int i = 1; i < folderNames.Length; ++i)
18541874
{
1855-
var folderNames = folderPath.Split(Path.DirectorySeparatorChar);
1856-
string rootPath = "";
1857-
foreach (var folderName in folderNames)
1858-
{
1859-
var newPath = rootPath + folderName;
1860-
if (!UnityEditor.AssetDatabase.IsValidFolder(newPath))
1861-
UnityEditor.AssetDatabase.CreateFolder(rootPath.TrimEnd(Path.DirectorySeparatorChar), folderName);
1862-
rootPath = newPath + Path.DirectorySeparatorChar;
1863-
}
1875+
string nextPath = currentPath + k_DirectorySeparatorChar + folderNames[i];
1876+
if (!UnityEditor.AssetDatabase.IsValidFolder(nextPath))
1877+
UnityEditor.AssetDatabase.CreateFolder(currentPath, folderNames[i]);
1878+
currentPath = nextPath;
18641879
}
18651880
}
18661881

1882+
18671883
/// <summary>
18681884
/// Returns the icon for the given type if it has an IconAttribute.
18691885
/// </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)