From 996bc91c93b3532085df51ac5424f2f76f6cb08e Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 7 Sep 2022 15:04:23 +0900 Subject: [PATCH] Rename CacheEnum to CachedEnum --- .../Runtime/UniGLTF/Util/CachedEnum.meta | 3 + .../UniGLTF/Util/CachedEnum/CachedEnum.cs | 46 +++++++++++++++ .../CachedEnum.cs.meta} | 0 .../UniGLTF/Util/CachedEnum/CachedEnumType.cs | 59 +++++++++++++++++++ .../Util/CachedEnum/CachedEnumType.cs.meta | 3 + Assets/UniGLTF/Tests/UniGLTF/CacheEnumTest.cs | 9 +-- .../Runtime/BlendShape/BlendShapeAvatar.cs | 2 +- .../VRM/Runtime/FirstPerson/VRMFirstPerson.cs | 2 +- .../Runtime/Format/glTF_VRM_FirstPerson.cs | 2 +- .../VRM/Runtime/Format/glTF_VRM_Humanoid.cs | 2 +- Assets/VRM/Runtime/Format/glTF_VRM_Meta.cs | 6 +- Assets/VRM/Runtime/IO/VRMImporterContext.cs | 4 +- 12 files changed, 122 insertions(+), 16 deletions(-) create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum.meta create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnum.cs rename Assets/UniGLTF/Runtime/UniGLTF/Util/{CacheEnum.cs.meta => CachedEnum/CachedEnum.cs.meta} (100%) create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnumType.cs create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnumType.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum.meta b/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum.meta new file mode 100644 index 000000000..a178bfff3 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: fa5415f8a69d46508e9e8fe6ff0ea820 +timeCreated: 1662528504 \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnum.cs b/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnum.cs new file mode 100644 index 000000000..7cad86e75 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnum.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections; + +namespace UniGLTF +{ + public static class CachedEnum + { + public static T Parse(string name, bool ignoreCase = false) where T : struct, Enum + { + if (ignoreCase) + { + return CachedEnumType.ParseIgnoreCase(name); + } + else + { + return CachedEnumType.Parse(name); + } + } + + public static T TryParseOrDefault(string name, bool ignoreCase = false, T defaultValue = default) + where T : struct, Enum + { + try + { + if (ignoreCase) + { + return CachedEnumType.ParseIgnoreCase(name); + } + else + { + return CachedEnumType.Parse(name); + } + } + catch + { + return defaultValue; + } + } + + public static T[] GetValues() where T : struct, Enum + { + return CachedEnumType.Values; + } + } +} + diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Util/CacheEnum.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnum.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/Util/CacheEnum.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnum.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnumType.cs b/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnumType.cs new file mode 100644 index 000000000..e3983140d --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnumType.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; + +namespace UniGLTF +{ + internal static class CachedEnumType where T : struct, Enum + { + private static Dictionary _values = new Dictionary(); + private static Dictionary _ignoreCaseValues = new Dictionary(StringComparer.OrdinalIgnoreCase); + private static T[] _allValues; + + public static T[] Values + { + get + { + if (_allValues == null) + { + _allValues = Enum.GetValues(typeof(T)) as T[]; + } + + return _allValues; + } + } + + public static T ParseIgnoreCase(string name) + { + if(_ignoreCaseValues.TryGetValue(name, out var value)) + { + return value; + } + else + { + T result; + value = Enum.TryParse(name, true, out result) + ? result + : throw new ArgumentException(nameof(result)); + _ignoreCaseValues.Add(name, value); + return value; + } + } + + public static T Parse(string name) + { + if(_values.TryGetValue(name, out var value)) + { + return value; + } + else + { + T result; + value = Enum.TryParse(name, false, out result) + ? result + : throw new ArgumentException(nameof(result)); + _values.Add(name, value); + return value; + } + } + } +} \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnumType.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnumType.cs.meta new file mode 100644 index 000000000..3d3336cf6 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnumType.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 439fd6a452234eeca5105e5591e8f9aa +timeCreated: 1662528509 \ No newline at end of file diff --git a/Assets/UniGLTF/Tests/UniGLTF/CacheEnumTest.cs b/Assets/UniGLTF/Tests/UniGLTF/CacheEnumTest.cs index a1e3a2ac0..ccaba22b4 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/CacheEnumTest.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/CacheEnumTest.cs @@ -9,13 +9,8 @@ namespace UniGLTF [Test] public void CacheEnumTestSimplePasses() { - Assert.AreEqual(default(HumanBodyBones), CacheEnum.TryParseOrDefault("xxx")); - -#if UNITY_5_6_OR_NEWER - Assert.AreEqual(HumanBodyBones.UpperChest, CacheEnum.TryParseOrDefault("upperchest", true)); -#else - Assert.AreEqual(default(HumanBodyBones), CacheEnum.TryParseOrDefault("upperchest")); -#endif + Assert.AreEqual(default(HumanBodyBones), CachedEnum.TryParseOrDefault("xxx")); + Assert.AreEqual(HumanBodyBones.UpperChest, CachedEnum.TryParseOrDefault("upperchest", true)); } } } diff --git a/Assets/VRM/Runtime/BlendShape/BlendShapeAvatar.cs b/Assets/VRM/Runtime/BlendShape/BlendShapeAvatar.cs index a5a416569..00d1055a3 100644 --- a/Assets/VRM/Runtime/BlendShape/BlendShapeAvatar.cs +++ b/Assets/VRM/Runtime/BlendShape/BlendShapeAvatar.cs @@ -61,7 +61,7 @@ namespace VRM /// public void CreateDefaultPreset() { - var presets = CacheEnum.GetValues(); + var presets = CachedEnum.GetValues(); foreach (var preset in presets) { diff --git a/Assets/VRM/Runtime/FirstPerson/VRMFirstPerson.cs b/Assets/VRM/Runtime/FirstPerson/VRMFirstPerson.cs index 1b600df6b..61dfbd4dc 100644 --- a/Assets/VRM/Runtime/FirstPerson/VRMFirstPerson.cs +++ b/Assets/VRM/Runtime/FirstPerson/VRMFirstPerson.cs @@ -126,7 +126,7 @@ namespace VRM { if (x.mesh == index) { - return CacheEnum.TryParseOrDefault(x.firstPersonFlag, true); + return CachedEnum.TryParseOrDefault(x.firstPersonFlag, true); } } diff --git a/Assets/VRM/Runtime/Format/glTF_VRM_FirstPerson.cs b/Assets/VRM/Runtime/Format/glTF_VRM_FirstPerson.cs index f1ab7e968..4a65522ce 100644 --- a/Assets/VRM/Runtime/Format/glTF_VRM_FirstPerson.cs +++ b/Assets/VRM/Runtime/Format/glTF_VRM_FirstPerson.cs @@ -72,7 +72,7 @@ namespace VRM { get { - return CacheEnum.TryParseOrDefault(lookAtTypeName, true); + return CachedEnum.TryParseOrDefault(lookAtTypeName, true); } set { lookAtTypeName = value.ToString(); } } diff --git a/Assets/VRM/Runtime/Format/glTF_VRM_Humanoid.cs b/Assets/VRM/Runtime/Format/glTF_VRM_Humanoid.cs index 27e1051f7..ec0ee3c65 100644 --- a/Assets/VRM/Runtime/Format/glTF_VRM_Humanoid.cs +++ b/Assets/VRM/Runtime/Format/glTF_VRM_Humanoid.cs @@ -139,7 +139,7 @@ namespace VRM } get { - return CacheEnum.Parse(bone, true); + return CachedEnum.Parse(bone, true); } } diff --git a/Assets/VRM/Runtime/Format/glTF_VRM_Meta.cs b/Assets/VRM/Runtime/Format/glTF_VRM_Meta.cs index c1036ebc7..f8f6380ae 100644 --- a/Assets/VRM/Runtime/Format/glTF_VRM_Meta.cs +++ b/Assets/VRM/Runtime/Format/glTF_VRM_Meta.cs @@ -36,7 +36,7 @@ namespace VRM { static UssageLicense FromString(string src) { - return CacheEnum.TryParseOrDefault(src, true); + return CachedEnum.TryParseOrDefault(src, true); } [JsonSchema(Description = "Title of VRM model")] @@ -69,7 +69,7 @@ namespace VRM { get { - return CacheEnum.TryParseOrDefault(allowedUserName, true); + return CachedEnum.TryParseOrDefault(allowedUserName, true); } set { @@ -135,7 +135,7 @@ namespace VRM { get { - return CacheEnum.TryParseOrDefault(licenseName, true); + return CachedEnum.TryParseOrDefault(licenseName, true); } set { diff --git a/Assets/VRM/Runtime/IO/VRMImporterContext.cs b/Assets/VRM/Runtime/IO/VRMImporterContext.cs index f48a19309..f56edfdf6 100644 --- a/Assets/VRM/Runtime/IO/VRMImporterContext.cs +++ b/Assets/VRM/Runtime/IO/VRMImporterContext.cs @@ -147,12 +147,12 @@ namespace VRM if (group != null) { asset.BlendShapeName = groupName; - asset.Preset = CacheEnum.TryParseOrDefault(group.presetName, true); + asset.Preset = CachedEnum.TryParseOrDefault(group.presetName, true); asset.IsBinary = group.isBinary; if (asset.Preset == BlendShapePreset.Unknown) { // fallback - asset.Preset = CacheEnum.TryParseOrDefault(group.name, true); + asset.Preset = CachedEnum.TryParseOrDefault(group.name, true); } asset.Values = group.binds.Select(x => {