Skip to content

Commit

Permalink
chore: remove dead code and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunderscore committed Nov 28, 2024
1 parent acdfd4c commit 7867747
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 66 deletions.
34 changes: 0 additions & 34 deletions Editor/API/BuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,40 +170,6 @@ public SerializationScope OpenSerializationScope()
return new SerializationScope(AssetSaver);
}

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
);

private static readonly Regex StripLeadingTrailingWhitespace = new Regex(
"^[\\s]*((?=\\S).*\\S)[\\s]*$"
);

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

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

var match = StripLeadingTrailingWhitespace.Match(avatarName);
if (match.Success)
{
avatarName = match.Groups[1].Value;
} else {
avatarName = Guid.NewGuid().ToString();
}

return avatarName;
}

public bool IsTemporaryAsset(UnityObject obj)
{
return AssetSaver.IsTemporaryAsset(obj);
Expand Down
24 changes: 5 additions & 19 deletions Editor/API/Serialization/AssetSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using nadena.dev.ndmf.runtime;
Expand Down Expand Up @@ -43,18 +44,8 @@ internal AssetSaver(string generatedAssetsRoot, string avatarName, int assetsPer
// Ensure directory exists recursively
if (!AssetDatabase.IsValidFolder(subAssetPath))
{
var parts = subAssetPath.Split('/');
var currentPath = parts[0];

for (var i = 1; i < parts.Length; i++)
{
if (!AssetDatabase.IsValidFolder(currentPath + "/" + parts[i]))
{
AssetDatabase.CreateFolder(currentPath, parts[i]);
}

currentPath += "/" + parts[i];
}
Directory.CreateDirectory(subAssetPath);
AssetDatabase.Refresh();
}

var rootAssetPath = AssetDatabase.GenerateUniqueAssetPath(rootPath + "/" + avatarName + ".asset");
Expand Down Expand Up @@ -137,7 +128,7 @@ public bool IsTemporaryAsset(UnityEngine.Object? obj)
"^[\\s]*((?=\\S).*\\S)[\\s]*$"
);

internal static string FilterAssetName(string assetName, string fallbackName)
internal static string FilterAssetName(string assetName, string? fallbackName = null)
{
assetName = WindowsReservedFileCharacters.Replace(assetName, "_");

Expand All @@ -151,12 +142,7 @@ internal static string FilterAssetName(string assetName, string fallbackName)
{
assetName = match.Groups[1].Value;
} else {
assetName = Guid.NewGuid().ToString();
}

if (string.IsNullOrWhiteSpace(assetName))
{
assetName = fallbackName;
assetName = fallbackName ?? Guid.NewGuid().ToString();
}

return assetName;
Expand Down
26 changes: 16 additions & 10 deletions UnitTests~/AvatarNameFilterTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using nadena.dev.ndmf;
using NUnit.Framework;
using UnityEditor;

namespace UnitTests
{
Expand All @@ -11,51 +12,56 @@ public void TestAvatarNameFilter()
{
Assert.AreEqual(
"foo",
BuildContext.FilterAvatarName("foo")
AssetSaver.FilterAssetName("foo")
);

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

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

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

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

Assert.AreEqual(
"foo_bar_baz_quux",
BuildContext.FilterAvatarName("foo\\bar?baz*quux")
AssetSaver.FilterAssetName("foo\\bar?baz*quux")
);

Assert.AreEqual(
"foo",
BuildContext.FilterAvatarName(" foo")
AssetSaver.FilterAssetName(" foo")
);

Assert.AreEqual(
"foo",
BuildContext.FilterAvatarName("foo ")
AssetSaver.FilterAssetName("foo ")
);
Assert.AreEqual(
"f",
BuildContext.FilterAvatarName(" f ")
AssetSaver.FilterAssetName(" f ")
);

Assert.AreEqual(
Guid.NewGuid().ToString().Length,
BuildContext.FilterAvatarName(" ").Length
AssetSaver.FilterAssetName(" ").Length
);

Assert.AreEqual(
"fallback",
AssetSaver.FilterAssetName(" ", "fallback")
);
}
}
Expand Down
10 changes: 7 additions & 3 deletions UnitTests~/SerializationSweepTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ public void testSerialization()
bc.Serialize();

var path = AssetDatabase.GetAssetPath(testScriptable1);
var pathParts = path.Split('/');
Assert.AreEqual("_assets", pathParts[^2]);
var avatarRootPath = string.Join("/", pathParts[..^2]);

Assert.IsFalse(string.IsNullOrEmpty(path));
Assert.AreEqual(path, AssetDatabase.GetAssetPath(testScriptable2));
Assert.AreEqual(path, AssetDatabase.GetAssetPath(testScriptable3));
Assert.AreEqual(path, AssetDatabase.GetAssetPath(testScriptable4));
Assert.IsTrue(AssetDatabase.GetAssetPath(testScriptable2).StartsWith(avatarRootPath));
Assert.IsTrue(AssetDatabase.GetAssetPath(testScriptable3).StartsWith(avatarRootPath));
Assert.IsTrue(AssetDatabase.GetAssetPath(testScriptable4).StartsWith(avatarRootPath));

Assert.IsTrue(string.IsNullOrEmpty(AssetDatabase.GetAssetPath(testComponent)));
Assert.IsTrue(string.IsNullOrEmpty(AssetDatabase.GetAssetPath(root)));
Expand Down

0 comments on commit 7867747

Please sign in to comment.