add EnumUtil

This commit is contained in:
ousttrue
2018-04-07 17:23:30 +09:00
parent 3220a98e0c
commit 789b9b73e5
12 changed files with 95 additions and 76 deletions

View File

@@ -0,0 +1,21 @@
using NUnit.Framework;
using UnityEngine;
namespace VRM
{
public class EnumUtilTest
{
[Test]
public void EnumUtilTestSimplePasses()
{
Assert.AreEqual(default(HumanBodyBones), EnumUtil.TryParseOrDefault<HumanBodyBones>("xxx"));
#if UNITY_5_6_OR_NEWER
Assert.AreEqual(HumanBodyBones.UpperChest, EnumUtil.TryParseOrDefault<HumanBodyBones>("upperchest"));
#else
Assert.AreEqual(default(HumanBodyBones), EnumUtil.TryParseOrDefault<HumanBodyBones>("upperchest"));
#endif
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: d4a5f44a23cb4884b9667b622f262497
timeCreated: 1523088686
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -3,22 +3,25 @@ using System.Collections.Generic;
using VRM;
public class VRMBlendShapeKeyTest
namespace VRM
{
[Test]
public void KeyTest()
public class VRMBlendShapeKeyTest
{
var key = new BlendShapeKey("Blink", BlendShapePreset.Blink);
[Test]
public void KeyTest()
{
var key = new BlendShapeKey("Blink", BlendShapePreset.Blink);
Assert.AreEqual(key, new BlendShapeKey("blink"));
Assert.AreEqual(key, new BlendShapeKey(BlendShapePreset.Blink));
Assert.AreEqual(key, new BlendShapeKey("xxx", BlendShapePreset.Blink));
Assert.AreEqual(key, new BlendShapeKey("blink"));
Assert.AreEqual(key, new BlendShapeKey(BlendShapePreset.Blink));
Assert.AreEqual(key, new BlendShapeKey("xxx", BlendShapePreset.Blink));
var dict = new Dictionary<BlendShapeKey, float>();
dict[new BlendShapeKey("xxx", BlendShapePreset.Blink)] = 1.0f;
var dict = new Dictionary<BlendShapeKey, float>();
dict[new BlendShapeKey("xxx", BlendShapePreset.Blink)] = 1.0f;
Assert.IsTrue(dict.ContainsKey(new BlendShapeKey("blink")));
Assert.IsTrue(dict.ContainsKey(new BlendShapeKey(BlendShapePreset.Blink)));
Assert.IsTrue(dict.ContainsKey(new BlendShapeKey("xxx", BlendShapePreset.Blink)));
Assert.IsTrue(dict.ContainsKey(new BlendShapeKey("blink")));
Assert.IsTrue(dict.ContainsKey(new BlendShapeKey(BlendShapePreset.Blink)));
Assert.IsTrue(dict.ContainsKey(new BlendShapeKey("xxx", BlendShapePreset.Blink)));
}
}
}

20
Scripts/EnumUtil.cs Normal file
View File

@@ -0,0 +1,20 @@
using System;
namespace VRM
{
public static class EnumUtil
{
public static T TryParseOrDefault<T>(string src)where T: struct
{
try
{
return (T)Enum.Parse(typeof(T), src, true);
}
catch(Exception)
{
return default(T);
}
}
}
}

12
Scripts/EnumUtil.cs.meta Normal file
View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: da515e03d4b6ec244839077b79aad26a
timeCreated: 1523088440
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -125,7 +125,7 @@ namespace VRM
{
if (x.mesh == index)
{
return (FirstPersonFlag)Enum.Parse(typeof(FirstPersonFlag), x.firstPersonFlag, true);
return EnumUtil.TryParseOrDefault<FirstPersonFlag>(x.firstPersonFlag);
}
}

View File

@@ -305,11 +305,11 @@ namespace VRM
if (group != null)
{
asset.BlendShapeName = groupName;
asset.Preset = group.presetName.ToBlendShapePreset();
asset.Preset = EnumUtil.TryParseOrDefault<BlendShapePreset>(group.presetName);
if (asset.Preset == BlendShapePreset.Unknown)
{
// fallback
asset.Preset = group.name.ToBlendShapePreset();
asset.Preset = EnumUtil.TryParseOrDefault<BlendShapePreset>(group.name);
}
asset.Values = group.binds.Select(x =>
{
@@ -367,7 +367,7 @@ namespace VRM
.Where(x => x.Index != -1)
.Select(x =>
{
var humanBone = (HumanBodyBones)Enum.Parse(typeof(HumanBodyBones), x.Key.ToUpperCamelCase());
var humanBone = EnumUtil.TryParseOrDefault<HumanBodyBones>(x.Key);
var hb = new HumanBone
{
boneName = context.Nodes[x.Index].name,

View File

@@ -4,11 +4,11 @@ namespace VRM
public static class VRMVersion
{
public const int MAJOR = 0;
public const int MINOR = 18;
public const int MINOR = 19;
public const string VERSION = "0.18";
public const string VERSION = "0.19";
public const string DecrementMenuName = "VRM/Version(0.18) Decrement";
public const string IncrementMenuName = "VRM/Version(0.18) Increment";
public const string DecrementMenuName = "VRM/Version(0.19) Decrement";
public const string IncrementMenuName = "VRM/Version(0.19) Increment";
}
}

View File

@@ -70,29 +70,6 @@ namespace VRM
Blink_R,
}
public static class BlendShpaePresetExtensions
{
/*
static string ToCamel(this string src)
{
if (string.IsNullOrEmpty(src)) return string.Empty;
return src.Substring(0, 1).ToUpper() + src.Substring(1);
}
*/
public static BlendShapePreset ToBlendShapePreset(this string preset)
{
try
{
return (BlendShapePreset)Enum.Parse(typeof(BlendShapePreset), preset, true);
}
catch (Exception)
{
return default(BlendShapePreset);
}
}
}
[Serializable]
public class glTF_VRM_BlendShapeGroup : UniGLTF.JsonSerializableBase
{

View File

@@ -76,12 +76,7 @@ namespace VRM
public LookAtType lookAtType
{
get {
if (string.IsNullOrEmpty(lookAtTypeName))
{
// fallback
return LookAtType.Bone;
}
return (LookAtType)Enum.Parse(typeof(LookAtType), lookAtTypeName, true);
return EnumUtil.TryParseOrDefault<LookAtType>(lookAtTypeName);
}
set { lookAtTypeName = value.ToString(); }
}

View File

@@ -71,11 +71,11 @@ namespace VRM
{
public static VRMBone FromHumanBodyBone(this HumanBodyBones human)
{
return (VRMBone)Enum.Parse(typeof(VRMBone), human.ToString(), true);
return EnumUtil.TryParseOrDefault<VRMBone>(human.ToString());
}
public static HumanBodyBones ToHumanBodyBone(this VRMBone bone)
{
return (HumanBodyBones)Enum.Parse(typeof(HumanBodyBones), bone.ToString(), true);
return EnumUtil.TryParseOrDefault<HumanBodyBones>(bone.ToString());
}
}
@@ -91,7 +91,7 @@ namespace VRM
}
get
{
return (VRMBone)Enum.Parse(typeof(VRMBone), bone, true);
return EnumUtil.TryParseOrDefault<VRMBone>(bone);
}
}
public int node = -1;

View File

@@ -35,14 +35,7 @@ namespace VRM
{
static UssageLicense FromString(string src)
{
try
{
return (UssageLicense)Enum.Parse(typeof(UssageLicense), src, true);
}
catch (Exception)
{
return UssageLicense.Disallow;
}
return EnumUtil.TryParseOrDefault<UssageLicense>(src);
}
public string title;
public string version;
@@ -57,14 +50,7 @@ namespace VRM
{
get
{
try
{
return (AllowedUser)Enum.Parse(typeof(AllowedUser), allowedUserName, true);
}
catch (Exception)
{
return AllowedUser.OnlyAuthor;
}
return EnumUtil.TryParseOrDefault<AllowedUser>(allowedUserName);
}
set
{
@@ -102,14 +88,7 @@ namespace VRM
{
get
{
try
{
return (LicenseType)Enum.Parse(typeof(LicenseType), licenseName, true);
}
catch (Exception)
{
return default(LicenseType);
}
return EnumUtil.TryParseOrDefault<LicenseType>(licenseName);
}
set
{