mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-07-19 16:51:55 -05:00
Rename CacheEnum to CachedEnum
This commit is contained in:
parent
da04ce7379
commit
996bc91c93
3
Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum.meta
Normal file
3
Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: fa5415f8a69d46508e9e8fe6ff0ea820
|
||||
timeCreated: 1662528504
|
||||
46
Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnum.cs
Normal file
46
Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnum.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
public static class CachedEnum
|
||||
{
|
||||
public static T Parse<T>(string name, bool ignoreCase = false) where T : struct, Enum
|
||||
{
|
||||
if (ignoreCase)
|
||||
{
|
||||
return CachedEnumType<T>.ParseIgnoreCase(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
return CachedEnumType<T>.Parse(name);
|
||||
}
|
||||
}
|
||||
|
||||
public static T TryParseOrDefault<T>(string name, bool ignoreCase = false, T defaultValue = default)
|
||||
where T : struct, Enum
|
||||
{
|
||||
try
|
||||
{
|
||||
if (ignoreCase)
|
||||
{
|
||||
return CachedEnumType<T>.ParseIgnoreCase(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
return CachedEnumType<T>.Parse(name);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public static T[] GetValues<T>() where T : struct, Enum
|
||||
{
|
||||
return CachedEnumType<T>.Values;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
internal static class CachedEnumType<T> where T : struct, Enum
|
||||
{
|
||||
private static Dictionary<string, T> _values = new Dictionary<string, T>();
|
||||
private static Dictionary<string, T> _ignoreCaseValues = new Dictionary<string, T>(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<T>(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<T>(name, false, out result)
|
||||
? result
|
||||
: throw new ArgumentException(nameof(result));
|
||||
_values.Add(name, value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 439fd6a452234eeca5105e5591e8f9aa
|
||||
timeCreated: 1662528509
|
||||
|
|
@ -9,13 +9,8 @@ namespace UniGLTF
|
|||
[Test]
|
||||
public void CacheEnumTestSimplePasses()
|
||||
{
|
||||
Assert.AreEqual(default(HumanBodyBones), CacheEnum.TryParseOrDefault<HumanBodyBones>("xxx"));
|
||||
|
||||
#if UNITY_5_6_OR_NEWER
|
||||
Assert.AreEqual(HumanBodyBones.UpperChest, CacheEnum.TryParseOrDefault<HumanBodyBones>("upperchest", true));
|
||||
#else
|
||||
Assert.AreEqual(default(HumanBodyBones), CacheEnum.TryParseOrDefault<HumanBodyBones>("upperchest"));
|
||||
#endif
|
||||
Assert.AreEqual(default(HumanBodyBones), CachedEnum.TryParseOrDefault<HumanBodyBones>("xxx"));
|
||||
Assert.AreEqual(HumanBodyBones.UpperChest, CachedEnum.TryParseOrDefault<HumanBodyBones>("upperchest", true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ namespace VRM
|
|||
/// </summary>
|
||||
public void CreateDefaultPreset()
|
||||
{
|
||||
var presets = CacheEnum.GetValues<BlendShapePreset>();
|
||||
var presets = CachedEnum.GetValues<BlendShapePreset>();
|
||||
|
||||
foreach (var preset in presets)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -126,7 +126,7 @@ namespace VRM
|
|||
{
|
||||
if (x.mesh == index)
|
||||
{
|
||||
return CacheEnum.TryParseOrDefault<FirstPersonFlag>(x.firstPersonFlag, true);
|
||||
return CachedEnum.TryParseOrDefault<FirstPersonFlag>(x.firstPersonFlag, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@ namespace VRM
|
|||
{
|
||||
get
|
||||
{
|
||||
return CacheEnum.TryParseOrDefault<LookAtType>(lookAtTypeName, true);
|
||||
return CachedEnum.TryParseOrDefault<LookAtType>(lookAtTypeName, true);
|
||||
}
|
||||
set { lookAtTypeName = value.ToString(); }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -139,7 +139,7 @@ namespace VRM
|
|||
}
|
||||
get
|
||||
{
|
||||
return CacheEnum.Parse<VRMBone>(bone, true);
|
||||
return CachedEnum.Parse<VRMBone>(bone, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ namespace VRM
|
|||
{
|
||||
static UssageLicense FromString(string src)
|
||||
{
|
||||
return CacheEnum.TryParseOrDefault<UssageLicense>(src, true);
|
||||
return CachedEnum.TryParseOrDefault<UssageLicense>(src, true);
|
||||
}
|
||||
|
||||
[JsonSchema(Description = "Title of VRM model")]
|
||||
|
|
@ -69,7 +69,7 @@ namespace VRM
|
|||
{
|
||||
get
|
||||
{
|
||||
return CacheEnum.TryParseOrDefault<AllowedUser>(allowedUserName, true);
|
||||
return CachedEnum.TryParseOrDefault<AllowedUser>(allowedUserName, true);
|
||||
}
|
||||
set
|
||||
{
|
||||
|
|
@ -135,7 +135,7 @@ namespace VRM
|
|||
{
|
||||
get
|
||||
{
|
||||
return CacheEnum.TryParseOrDefault<LicenseType>(licenseName, true);
|
||||
return CachedEnum.TryParseOrDefault<LicenseType>(licenseName, true);
|
||||
}
|
||||
set
|
||||
{
|
||||
|
|
|
|||
|
|
@ -147,12 +147,12 @@ namespace VRM
|
|||
if (group != null)
|
||||
{
|
||||
asset.BlendShapeName = groupName;
|
||||
asset.Preset = CacheEnum.TryParseOrDefault<BlendShapePreset>(group.presetName, true);
|
||||
asset.Preset = CachedEnum.TryParseOrDefault<BlendShapePreset>(group.presetName, true);
|
||||
asset.IsBinary = group.isBinary;
|
||||
if (asset.Preset == BlendShapePreset.Unknown)
|
||||
{
|
||||
// fallback
|
||||
asset.Preset = CacheEnum.TryParseOrDefault<BlendShapePreset>(group.name, true);
|
||||
asset.Preset = CachedEnum.TryParseOrDefault<BlendShapePreset>(group.name, true);
|
||||
}
|
||||
asset.Values = group.binds.Select(x =>
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user