diff --git a/Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs b/Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs index 793ce922d..ee51bbc8c 100644 --- a/Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs +++ b/Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs @@ -20,6 +20,11 @@ namespace UniJSON { Value = value; } + + public int Get(int _) + { + return Value; + } } @@ -29,15 +34,27 @@ namespace UniJSON { var s = new Sample(); - var mi = s.GetType().GetMethod("Set"); - var invoke = GenericInvokeCallFactory.CreateAction(mi); - invoke(s, 1); - Assert.AreEqual(1, s.Value); + { + var mi = s.GetType().GetMethod("Set"); + var action = GenericInvokeCallFactory.OpenAction(mi); + action(s, 1); + Assert.AreEqual(1, s.Value); + } - var exp = GenericExpressionCallFactory.Create(mi); - exp(s, 2); - Assert.AreEqual(2, s.Value); + { + var mi = s.GetType().GetMethod("Get"); + var func = GenericInvokeCallFactory.OpenFunc(mi); + var value = func(s, 1); + Assert.AreEqual(1, s.Value); + } + + { + var mi = s.GetType().GetMethod("Set"); + var action = GenericExpressionCallFactory.Create(mi); + action(s, 2); + Assert.AreEqual(2, s.Value); + } } } } diff --git a/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs b/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs index 16350f3fc..473dee4d3 100644 --- a/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs +++ b/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs @@ -84,7 +84,7 @@ namespace UniJSON if (typeof(T) == typeof(object) && t.GetType() != typeof(object)) { var mi = FormatterExtensionsSerializer.GetMethod("SerializeObject"); - return GenericInvokeCallFactory.CreateAction(mi); + return GenericInvokeCallFactory.StaticAction(mi); } try @@ -93,7 +93,7 @@ namespace UniJSON var mi = typeof(IFormatter).GetMethod("Value", new Type[] { t }); if (mi != null) { - return GenericInvokeCallFactory.CreateAction(mi); + return GenericInvokeCallFactory.OpenAction(mi); } } catch (AmbiguousMatchException) @@ -111,7 +111,7 @@ namespace UniJSON if (idictionary != null) { var mi = FormatterExtensionsSerializer.GetMethod("SerializeDictionary"); - return GenericInvokeCallFactory.CreateAction(mi); + return GenericInvokeCallFactory.StaticAction(mi); } } @@ -120,7 +120,7 @@ namespace UniJSON if (t == typeof(object[])) { var mi = FormatterExtensionsSerializer.GetMethod("SerializeObjectArray"); - return GenericInvokeCallFactory.CreateAction(mi); + return GenericInvokeCallFactory.StaticAction(mi); } } @@ -134,7 +134,7 @@ namespace UniJSON { var g = FormatterExtensionsSerializer.GetMethod("SerializeArray"); var mi = g.MakeGenericMethod(ienumerable.GetGenericArguments()); - return GenericInvokeCallFactory.CreateAction(mi); + return GenericInvokeCallFactory.StaticAction(mi); } } diff --git a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs index 01c968fe7..290cfb91c 100644 --- a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs +++ b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs @@ -1,15 +1,23 @@ using System; +using System.Reflection; +#if UNITY_EDITOR && VRM_DEVELOP using System.IO; using System.Linq; -using System.Reflection; using System.Text; -#if UNITY_EDITOR using UnityEditor; #endif namespace UniJSON { + /// + /// MethodInfoからDelegateを作成する + /// + /// * StaticAction/Func StaticMethod呼び出し + /// * OpenAction/Func 第1引数にthisを受けるメソッド呼び出し + /// * BindAction/Func thisを内部に保持したメソッド呼び出し + /// + /// public static partial class GenericInvokeCallFactory { #if UNITY_EDITOR && VRM_DEVELOP @@ -42,79 +50,151 @@ namespace UniJSON { "); - // CreateWithThis - w.WriteLine("//////////// CreateAction"); - - // Create - for (int i = 1; i <= ARGS && i< NET35MAX; ++i) - { - var g = String.Join(", ", GetArgs("A", i).ToArray()); - var a = String.Join(", ", GetArgs("a", i).ToArray()); - - - - var source = @" - public static Action CreateAction(MethodInfo m) - { - Action callback = null; - if (m.IsStatic) - { - callback = (s, $1) => - { - m.Invoke(null, new object[] { s, $1 }); - }; - } - else - { - callback = (s, $1) => - { - m.Invoke(s, new object[] { $1 }); - }; - } - return callback; - } -".Replace("$0", g).Replace("$1", a); - - w.WriteLine(source); - - } - - - // CreateWithThis - w.WriteLine("//////////// CreateWithThis"); - for (int i = 1; i <= ARGS && i<= NET35MAX; ++i) + // StaticAction + w.WriteLine("//////////// StaticAction"); + for (int i = 1; i <= ARGS && i <= NET35MAX; ++i) { var g = String.Join(", ", GetArgs("A", i).ToArray()); var a = String.Join(", ", GetArgs("a", i).ToArray()); var source = @" - public static Action<$0> CreateActionBindThis(MethodInfo m, S instance) + public static Action<$0> StaticAction<$0>(MethodInfo m) { - if (m.IsStatic) + if (!m.IsStatic) { - if (instance != null) - { - throw new ArgumentException(); - } - } - else - { - if (instance == null) - { - throw new ArgumentNullException(); - } + throw new ArgumentException(string.Format(""{0} is not static"", m)); } - // ToDo: CreateDelegate - Action<$0> callback= - ($1) => { - m.Invoke(instance, new object[]{ $1 }); + return ($1) => + { + m.Invoke(null, new object[] { $1 }); }; - return callback; } ".Replace("$0", g).Replace("$1", a); w.WriteLine(source); + + } + + // OpenAction + w.WriteLine("//////////// OpenAction"); + for (int i = 1; i <= ARGS && i < NET35MAX; ++i) + { + var g = String.Join(", ", GetArgs("A", i).ToArray()); + var a = String.Join(", ", GetArgs("a", i).ToArray()); + + var source = @" + public static Action OpenAction(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format(""{0} is static"", m)); + } + + return (s, $1) => + { + m.Invoke(s, new object[] { $1 }); + }; + } +".Replace("$0", g).Replace("$1", a); + + w.WriteLine(source); + + } + + // BindAction + w.WriteLine("//////////// BindAction"); + for (int i = 1; i <= ARGS && i <= NET35MAX; ++i) + { + var g = String.Join(", ", GetArgs("A", i).ToArray()); + var a = String.Join(", ", GetArgs("a", i).ToArray()); + + var source = @" + public static Action<$0> BindAction(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format(""{0} is static"", m)); + } + + return ($1) => + { + m.Invoke(instance, new object[] { $1 }); + }; + } +".Replace("$0", g).Replace("$1", a); + + w.WriteLine(source); + + } + + // StaticFunc + w.WriteLine("//////////// StaticFunc"); + for (int i = 1; i <= ARGS && i <= NET35MAX; ++i) + { + var g = String.Join(", ", GetArgs("A", i).ToArray()); + var a = String.Join(", ", GetArgs("a", i).ToArray()); + + var source = @" + public static Func<$0, T> StaticFunc<$0, T>(MethodInfo m) + { + if (!m.IsStatic) + { + throw new ArgumentException(string.Format(""{0} is not static"", m)); + } + + return ($1) => (T)m.Invoke(null, new object[] { $1 }); + } +".Replace("$0", g).Replace("$1", a); + + w.WriteLine(source); + + } + + // OpenFunc + w.WriteLine("//////////// OpenFunc"); + for (int i = 1; i <= ARGS && i < NET35MAX; ++i) + { + var g = String.Join(", ", GetArgs("A", i).ToArray()); + var a = String.Join(", ", GetArgs("a", i).ToArray()); + + var source = @" + public static Func OpenFunc(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format(""{0} is static"", m)); + } + + return (s, $1) => (T)m.Invoke(s, new object[] { $1 }); + } +".Replace("$0", g).Replace("$1", a); + + w.WriteLine(source); + + } + + // BindFunc + w.WriteLine("//////////// BindFunc"); + for (int i = 1; i <= ARGS && i <= NET35MAX; ++i) + { + var g = String.Join(", ", GetArgs("A", i).ToArray()); + var a = String.Join(", ", GetArgs("a", i).ToArray()); + + var source = @" + public static Func<$0, T> BindFunc(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format(""{0} is static"", m)); + } + + return ($1) => (T)m.Invoke(instance, new object[] { $1 }); + } +".Replace("$0", g).Replace("$1", a); + + w.WriteLine(source); + } @@ -130,30 +210,77 @@ namespace UniJSON } #endif - #region zero arguments - public static Action CreateAction(MethodInfo m) + #region Action without arguments + public static Action StaticAction(MethodInfo m) { + if (!m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is not static", m)); + } + + return () => + { + m.Invoke(null, new object[] { }); + }; + } + + public static Action OpenAction(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + return (s) => { m.Invoke(s, new object[] { }); }; } - public static Func CreateFunc(MethodInfo m) + public static Action BindAction(MethodInfo m, S instance) { - return (s) => + if (m.IsStatic) { - return (T)m.Invoke(s, new object[] { }); - }; - } + throw new ArgumentException(string.Format("{0} is static", m)); + } - public static Action CreateActionBindThis(MethodInfo m, S instance) - { return () => { m.Invoke(instance, new object[] { }); }; } #endregion + + #region Func without arguments + public static Func StaticFunc(MethodInfo m) + { + if (!m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is not static", m)); + } + + return () => (T)m.Invoke(null, new object[] { }); + } + + public static Func OpenFunc(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (s) => (T)m.Invoke(s, new object[] { }); + } + + public static Func BindFunc(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return () => (T)m.Invoke(instance, new object[] { }); + } + #endregion } } diff --git a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs index 6b55983c2..209b49be0 100644 --- a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs +++ b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs @@ -8,173 +8,285 @@ namespace UniJSON public static partial class GenericInvokeCallFactory { -//////////// CreateAction +//////////// StaticAction - public static Action CreateAction(MethodInfo m) + public static Action StaticAction(MethodInfo m) { - Action callback = null; - if (m.IsStatic) + if (!m.IsStatic) { - callback = (s, a0) => - { - m.Invoke(null, new object[] { s, a0 }); - }; - } - else - { - callback = (s, a0) => - { - m.Invoke(s, new object[] { a0 }); - }; - } - return callback; - } - - - public static Action CreateAction(MethodInfo m) - { - Action callback = null; - if (m.IsStatic) - { - callback = (s, a0, a1) => - { - m.Invoke(null, new object[] { s, a0, a1 }); - }; - } - else - { - callback = (s, a0, a1) => - { - m.Invoke(s, new object[] { a0, a1 }); - }; - } - return callback; - } - - - public static Action CreateAction(MethodInfo m) - { - Action callback = null; - if (m.IsStatic) - { - callback = (s, a0, a1, a2) => - { - m.Invoke(null, new object[] { s, a0, a1, a2 }); - }; - } - else - { - callback = (s, a0, a1, a2) => - { - m.Invoke(s, new object[] { a0, a1, a2 }); - }; - } - return callback; - } - -//////////// CreateWithThis - - public static Action CreateActionBindThis(MethodInfo m, S instance) - { - if (m.IsStatic) - { - if (instance != null) - { - throw new ArgumentException(); - } - } - else - { - if (instance == null) - { - throw new ArgumentNullException(); - } + throw new ArgumentException(string.Format("{0} is not static", m)); } - // ToDo: CreateDelegate - Action callback= - (a0) => { - m.Invoke(instance, new object[]{ a0 }); + return (a0) => + { + m.Invoke(null, new object[] { a0 }); }; - return callback; } - public static Action CreateActionBindThis(MethodInfo m, S instance) + public static Action StaticAction(MethodInfo m) { - if (m.IsStatic) + if (!m.IsStatic) { - if (instance != null) - { - throw new ArgumentException(); - } - } - else - { - if (instance == null) - { - throw new ArgumentNullException(); - } + throw new ArgumentException(string.Format("{0} is not static", m)); } - // ToDo: CreateDelegate - Action callback= - (a0, a1) => { - m.Invoke(instance, new object[]{ a0, a1 }); + return (a0, a1) => + { + m.Invoke(null, new object[] { a0, a1 }); }; - return callback; } - public static Action CreateActionBindThis(MethodInfo m, S instance) + public static Action StaticAction(MethodInfo m) { - if (m.IsStatic) + if (!m.IsStatic) { - if (instance != null) - { - throw new ArgumentException(); - } - } - else - { - if (instance == null) - { - throw new ArgumentNullException(); - } + throw new ArgumentException(string.Format("{0} is not static", m)); } - // ToDo: CreateDelegate - Action callback= - (a0, a1, a2) => { - m.Invoke(instance, new object[]{ a0, a1, a2 }); + return (a0, a1, a2) => + { + m.Invoke(null, new object[] { a0, a1, a2 }); }; - return callback; } - public static Action CreateActionBindThis(MethodInfo m, S instance) + public static Action StaticAction(MethodInfo m) + { + if (!m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is not static", m)); + } + + return (a0, a1, a2, a3) => + { + m.Invoke(null, new object[] { a0, a1, a2, a3 }); + }; + } + +//////////// OpenAction + + public static Action OpenAction(MethodInfo m) { if (m.IsStatic) { - if (instance != null) - { - throw new ArgumentException(); - } - } - else - { - if (instance == null) - { - throw new ArgumentNullException(); - } + throw new ArgumentException(string.Format("{0} is static", m)); } - // ToDo: CreateDelegate - Action callback= - (a0, a1, a2, a3) => { - m.Invoke(instance, new object[]{ a0, a1, a2, a3 }); + return (s, a0) => + { + m.Invoke(s, new object[] { a0 }); }; - return callback; + } + + + public static Action OpenAction(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (s, a0, a1) => + { + m.Invoke(s, new object[] { a0, a1 }); + }; + } + + + public static Action OpenAction(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (s, a0, a1, a2) => + { + m.Invoke(s, new object[] { a0, a1, a2 }); + }; + } + +//////////// BindAction + + public static Action BindAction(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (a0) => + { + m.Invoke(instance, new object[] { a0 }); + }; + } + + + public static Action BindAction(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (a0, a1) => + { + m.Invoke(instance, new object[] { a0, a1 }); + }; + } + + + public static Action BindAction(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (a0, a1, a2) => + { + m.Invoke(instance, new object[] { a0, a1, a2 }); + }; + } + + + public static Action BindAction(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (a0, a1, a2, a3) => + { + m.Invoke(instance, new object[] { a0, a1, a2, a3 }); + }; + } + +//////////// StaticFunc + + public static Func StaticFunc(MethodInfo m) + { + if (!m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is not static", m)); + } + + return (a0) => (T)m.Invoke(null, new object[] { a0 }); + } + + + public static Func StaticFunc(MethodInfo m) + { + if (!m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is not static", m)); + } + + return (a0, a1) => (T)m.Invoke(null, new object[] { a0, a1 }); + } + + + public static Func StaticFunc(MethodInfo m) + { + if (!m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is not static", m)); + } + + return (a0, a1, a2) => (T)m.Invoke(null, new object[] { a0, a1, a2 }); + } + + + public static Func StaticFunc(MethodInfo m) + { + if (!m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is not static", m)); + } + + return (a0, a1, a2, a3) => (T)m.Invoke(null, new object[] { a0, a1, a2, a3 }); + } + +//////////// OpenFunc + + public static Func OpenFunc(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (s, a0) => (T)m.Invoke(s, new object[] { a0 }); + } + + + public static Func OpenFunc(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (s, a0, a1) => (T)m.Invoke(s, new object[] { a0, a1 }); + } + + + public static Func OpenFunc(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (s, a0, a1, a2) => (T)m.Invoke(s, new object[] { a0, a1, a2 }); + } + +//////////// BindFunc + + public static Func BindFunc(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (a0) => (T)m.Invoke(instance, new object[] { a0 }); + } + + + public static Func BindFunc(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (a0, a1) => (T)m.Invoke(instance, new object[] { a0, a1 }); + } + + + public static Func BindFunc(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (a0, a1, a2) => (T)m.Invoke(instance, new object[] { a0, a1, a2 }); + } + + + public static Func BindFunc(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (a0, a1, a2, a3) => (T)m.Invoke(instance, new object[] { a0, a1, a2, a3 }); } diff --git a/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonArrayValidator.cs b/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonArrayValidator.cs index 492c33031..e9ea2c8eb 100644 --- a/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonArrayValidator.cs +++ b/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonArrayValidator.cs @@ -264,7 +264,7 @@ namespace UniJSON IFormatter, JsonSchemaValidationContext, T>) - GenericInvokeCallFactory.CreateAction< + GenericInvokeCallFactory.StaticAction< IJsonSchemaValidator, IFormatter, JsonSchemaValidationContext,