Skip to content

Commit

Permalink
fix: avatar names with non-path-safe characters break builds
Browse files Browse the repository at this point in the history
Fixes: #18
  • Loading branch information
bdunderscore committed Sep 24, 2023
1 parent 40dcf08 commit f82fcdc
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

* Fixed build failures when avatar names contained non-path-safe characters (#18)

### Changed

### Removed
Expand Down
31 changes: 31 additions & 0 deletions Editor/API/BuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using nadena.dev.ndmf.reporting;
using nadena.dev.ndmf.runtime;
using nadena.dev.ndmf.util;
Expand Down Expand Up @@ -93,6 +94,9 @@ public BuildContext(GameObject obj, string assetRootPath)
{
// Ensure the target directory exists
Directory.CreateDirectory(assetRootPath);

var pathAvatarName = FilterAvatarName(avatarName);

var avatarPath = Path.Combine(assetRootPath, avatarName) + ".asset";
AssetDatabase.GenerateUniqueAssetPath(avatarPath);
AssetDatabase.CreateAsset(AssetContainer, avatarPath);
Expand All @@ -114,6 +118,33 @@ public BuildContext(GameObject obj, string assetRootPath)
sw.Stop();
}

private static readonly Regex WindowsReservedFileNames = new Regex(
"(CON|PRN|AUX|NUL|COM[0-9]|LPT[0-9])([.].*)?",
RegexOptions.IgnoreCase
);

private static readonly Regex WindowsReservedFileCharacters = new Regex(
"[<>:\"/\\\\|?*\x00-\x1f]",
RegexOptions.IgnoreCase
);

internal static string FilterAvatarName(string avatarName)
{
avatarName = WindowsReservedFileCharacters.Replace(avatarName, "_");

if (WindowsReservedFileNames.IsMatch(avatarName))
{
avatarName = "_" + avatarName;
}

if (string.IsNullOrEmpty(avatarName))
{
avatarName = Guid.NewGuid().ToString();
}

return avatarName;
}

public bool IsTemporaryAsset(UnityObject obj)
{
return !EditorUtility.IsPersistent(obj)
Expand Down
42 changes: 42 additions & 0 deletions UnitTests~/AvatarNameFilterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using nadena.dev.ndmf;
using NUnit.Framework;

namespace UnitTests
{
public class AvatarNameFilterTests
{
[Test]
public void TestAvatarNameFilter()
{
Assert.AreEqual(
"foo",
BuildContext.FilterAvatarName("foo")
);

Assert.AreEqual(
"_con",
BuildContext.FilterAvatarName("con")
);

Assert.AreEqual(
"_LPT4",
BuildContext.FilterAvatarName("LPT4")
);

Assert.AreEqual(
"_AUX.avatar",
BuildContext.FilterAvatarName("AUX.avatar")
);

Assert.AreEqual(
"foo_bar",
BuildContext.FilterAvatarName("foo/bar")
);

Assert.AreEqual(
"foo_bar_baz_quux",
BuildContext.FilterAvatarName("foo\\bar?baz*quux")
);
}
}
}
3 changes: 3 additions & 0 deletions UnitTests~/AvatarNameFilterTests.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f82fcdc

Please sign in to comment.