From 43b14f6831045e93e7350f453d8fbfcb8f0a8490 Mon Sep 17 00:00:00 2001 From: Shawn Kuang Date: Wed, 28 Sep 2022 17:42:41 -0700 Subject: [PATCH 1/2] Workaroud when `AssetDatabase.CreateFolder` failed With certain version of Unity, ex. 2022.2.0b8 or 2023.1.0a11, `AssetDatabase.CreateFolder()` can fail when the folder name is version number like `9.0.0`. In this case, the API does not return empty guid but a guid with zeroes. This change make sure that the folder can still be created using `Directory.CreateDirectory()`, which may not trigger AssetDatabase refresh in the older version of Unity. --- source/VersionHandlerImpl/src/FileUtils.cs | 31 ++++++++++++- .../unit_tests/src/FileUtilsTest.cs | 45 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/source/VersionHandlerImpl/src/FileUtils.cs b/source/VersionHandlerImpl/src/FileUtils.cs index 6988dd63..f4f4df3b 100644 --- a/source/VersionHandlerImpl/src/FileUtils.cs +++ b/source/VersionHandlerImpl/src/FileUtils.cs @@ -52,6 +52,11 @@ internal class FileUtils { private static Regex PACKAGES_PHYSICAL_PATH_REGEX = new Regex(@"^(Library[/\\]PackageCache[/\\])([^/\\]+)(@[^/\\]+)[/\\](.*)?$"); + /// + /// Regex to match invalid GUID like "00000000-0000-0000-0000-000000000000" or "00000000000000000000000000000000" + /// + private static Regex ZERO_GUID_REGEX = new Regex(@"^[0-]+$"); + /// /// Returns the project directory (e.g contains the Assets folder). /// @@ -618,6 +623,22 @@ public static RemoveAssetsResult RemoveAssets(IEnumerable filenames, return result; } + /// + /// Check if a guid returned from Unity API is valid. + /// + /// GUID returned from Unity API. + /// True if the guid is valid. + internal static bool IsValidGuid(string guidStr) { + if(String.IsNullOrEmpty(guidStr)) return false; + try { + var guid = new Guid(guidStr); + if (guid == Guid.Empty) return false; + } catch (FormatException e) { + return false; + } + return true; + } + /// /// Recursively create all parent folders given a path. /// @@ -632,7 +653,15 @@ public static bool CreateFolder(string path) { if (!CreateFolder(parentFolder)) { return false; } - return !String.IsNullOrEmpty(AssetDatabase.CreateFolder(parentFolder, di.Name)); + + // Try to use Unity API to create folder. However, some versions of Unity has issue to + // create folders with version number in it like '9.0.0'. In this case, instead of + // returnig empty guid, it can return guids with all zeroes. + if (IsValidGuid(AssetDatabase.CreateFolder(parentFolder, di.Name))) { + return true; + } + + return Directory.CreateDirectory(path) != null; } /// diff --git a/source/VersionHandlerImpl/unit_tests/src/FileUtilsTest.cs b/source/VersionHandlerImpl/unit_tests/src/FileUtilsTest.cs index dd7d7c7c..3aaa62f6 100644 --- a/source/VersionHandlerImpl/unit_tests/src/FileUtilsTest.cs +++ b/source/VersionHandlerImpl/unit_tests/src/FileUtilsTest.cs @@ -391,5 +391,50 @@ public void ReplaceBaseAssetsOrPackagesFolder() { "Foo/Bar", "Assets"), Is.EqualTo("Foo/Bar")); } + + /// + /// Test FileUtils.IsValidGuid() when it returns true + /// + [Test] + public void IsValidGuid_TrueCases() { + Assert.That( + FileUtils.IsValidGuid("4b7c4a82-79ca-4eb5-a154-5d78a3b3d3d7"), + Is.EqualTo(true)); + + Assert.That( + FileUtils.IsValidGuid("017885d9f22374a53844077ede0ccda6"), + Is.EqualTo(true)); + } + + /// + /// Test FileUtils.IsValidGuid() when it returns false + /// + [Test] + public void IsValidGuid_FalseCases() { + Assert.That( + FileUtils.IsValidGuid(""), + Is.EqualTo(false)); + Assert.That( + FileUtils.IsValidGuid(null), + Is.EqualTo(false)); + Assert.That( + FileUtils.IsValidGuid("00000000-0000-0000-0000-000000000000"), + Is.EqualTo(false)); + Assert.That( + FileUtils.IsValidGuid("00000000000000000000000000000000"), + Is.EqualTo(false)); + Assert.That( + FileUtils.IsValidGuid("g000000000000000000000000000000"), + Is.EqualTo(false)); + Assert.That( + FileUtils.IsValidGuid(" "), + Is.EqualTo(false)); + Assert.That( + FileUtils.IsValidGuid("12300000 0000 0000 0000 000000000000"), + Is.EqualTo(false)); + Assert.That( + FileUtils.IsValidGuid("12300000\n0000\n0000\n0000\n000000000000"), + Is.EqualTo(false)); + } } } From 26902f455612b9287320ae7823d415db33a38472 Mon Sep 17 00:00:00 2001 From: Shawn Kuang Date: Wed, 28 Sep 2022 17:51:28 -0700 Subject: [PATCH 2/2] remove unused regex --- source/VersionHandlerImpl/src/FileUtils.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/source/VersionHandlerImpl/src/FileUtils.cs b/source/VersionHandlerImpl/src/FileUtils.cs index f4f4df3b..5c1c9c2d 100644 --- a/source/VersionHandlerImpl/src/FileUtils.cs +++ b/source/VersionHandlerImpl/src/FileUtils.cs @@ -52,11 +52,6 @@ internal class FileUtils { private static Regex PACKAGES_PHYSICAL_PATH_REGEX = new Regex(@"^(Library[/\\]PackageCache[/\\])([^/\\]+)(@[^/\\]+)[/\\](.*)?$"); - /// - /// Regex to match invalid GUID like "00000000-0000-0000-0000-000000000000" or "00000000000000000000000000000000" - /// - private static Regex ZERO_GUID_REGEX = new Regex(@"^[0-]+$"); - /// /// Returns the project directory (e.g contains the Assets folder). ///