Skip to content

Commit ad55ee6

Browse files
authored
Workaroud when AssetDatabase.CreateFolder failed (#543)
* 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.
1 parent a83fcbf commit ad55ee6

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

Diff for: source/VersionHandlerImpl/src/FileUtils.cs

+25-1
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,22 @@ public static RemoveAssetsResult RemoveAssets(IEnumerable<string> filenames,
618618
return result;
619619
}
620620

621+
/// <summary>
622+
/// Check if a guid returned from Unity API is valid.
623+
/// </summary>
624+
/// <param name="guid">GUID returned from Unity API.</param>
625+
/// <returns>True if the guid is valid.</returns>
626+
internal static bool IsValidGuid(string guidStr) {
627+
if(String.IsNullOrEmpty(guidStr)) return false;
628+
try {
629+
var guid = new Guid(guidStr);
630+
if (guid == Guid.Empty) return false;
631+
} catch (FormatException e) {
632+
return false;
633+
}
634+
return true;
635+
}
636+
621637
/// <summary>
622638
/// Recursively create all parent folders given a path.
623639
/// </summary>
@@ -632,7 +648,15 @@ public static bool CreateFolder(string path) {
632648
if (!CreateFolder(parentFolder)) {
633649
return false;
634650
}
635-
return !String.IsNullOrEmpty(AssetDatabase.CreateFolder(parentFolder, di.Name));
651+
652+
// Try to use Unity API to create folder. However, some versions of Unity has issue to
653+
// create folders with version number in it like '9.0.0'. In this case, instead of
654+
// returnig empty guid, it can return guids with all zeroes.
655+
if (IsValidGuid(AssetDatabase.CreateFolder(parentFolder, di.Name))) {
656+
return true;
657+
}
658+
659+
return Directory.CreateDirectory(path) != null;
636660
}
637661

638662
/// <summary>

Diff for: source/VersionHandlerImpl/unit_tests/src/FileUtilsTest.cs

+45
Original file line numberDiff line numberDiff line change
@@ -391,5 +391,50 @@ public void ReplaceBaseAssetsOrPackagesFolder() {
391391
"Foo/Bar", "Assets"),
392392
Is.EqualTo("Foo/Bar"));
393393
}
394+
395+
/// <summary>
396+
/// Test FileUtils.IsValidGuid() when it returns true
397+
/// </summary>
398+
[Test]
399+
public void IsValidGuid_TrueCases() {
400+
Assert.That(
401+
FileUtils.IsValidGuid("4b7c4a82-79ca-4eb5-a154-5d78a3b3d3d7"),
402+
Is.EqualTo(true));
403+
404+
Assert.That(
405+
FileUtils.IsValidGuid("017885d9f22374a53844077ede0ccda6"),
406+
Is.EqualTo(true));
407+
}
408+
409+
/// <summary>
410+
/// Test FileUtils.IsValidGuid() when it returns false
411+
/// </summary>
412+
[Test]
413+
public void IsValidGuid_FalseCases() {
414+
Assert.That(
415+
FileUtils.IsValidGuid(""),
416+
Is.EqualTo(false));
417+
Assert.That(
418+
FileUtils.IsValidGuid(null),
419+
Is.EqualTo(false));
420+
Assert.That(
421+
FileUtils.IsValidGuid("00000000-0000-0000-0000-000000000000"),
422+
Is.EqualTo(false));
423+
Assert.That(
424+
FileUtils.IsValidGuid("00000000000000000000000000000000"),
425+
Is.EqualTo(false));
426+
Assert.That(
427+
FileUtils.IsValidGuid("g000000000000000000000000000000"),
428+
Is.EqualTo(false));
429+
Assert.That(
430+
FileUtils.IsValidGuid(" "),
431+
Is.EqualTo(false));
432+
Assert.That(
433+
FileUtils.IsValidGuid("12300000 0000 0000 0000 000000000000"),
434+
Is.EqualTo(false));
435+
Assert.That(
436+
FileUtils.IsValidGuid("12300000\n0000\n0000\n0000\n000000000000"),
437+
Is.EqualTo(false));
438+
}
394439
}
395440
}

0 commit comments

Comments
 (0)