-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from dwango/add_GenericCallUtility
Added GenericCallUtility
- Loading branch information
Showing
12 changed files
with
1,099 additions
and
2 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using UnityEngine; | ||
using NUnit.Framework; | ||
using System.Collections; | ||
using System; | ||
|
||
|
||
namespace UniJSON | ||
{ | ||
public class GenericCallUtilityTests | ||
{ | ||
class Sample | ||
{ | ||
public int Value | ||
{ | ||
get; | ||
private set; | ||
} | ||
|
||
public void Set(int value) | ||
{ | ||
Value = value; | ||
} | ||
} | ||
|
||
|
||
|
||
[Test] | ||
public void GenericCallUtilityTestsSimplePasses() | ||
{ | ||
var s = new Sample(); | ||
|
||
var mi = s.GetType().GetMethod("Set"); | ||
|
||
var invoke = (Action<Sample, int>)GenericInvokeCallFactory.Create<Sample, int>(mi); | ||
invoke(s, 1); | ||
Assert.AreEqual(1, s.Value); | ||
|
||
var exp = (Action<Sample, int>)GenericExpressionCallFactory.Create<Sample, int>(mi); | ||
exp(s, 2); | ||
Assert.AreEqual(2, s.Value); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
134 changes: 134 additions & 0 deletions
134
Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericExpressionCallFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
#if UNITY_EDITOR | ||
using UnityEditor; | ||
#endif | ||
|
||
|
||
namespace UniJSON | ||
{ | ||
public static partial class GenericExpressionCallFactory | ||
{ | ||
#if UNITY_EDITOR && VRM_DEVELOP | ||
const int ARGS = 6; | ||
const string GENERATE_PATH = "Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericExpressionCallFactory.g.cs"; | ||
|
||
static System.Collections.Generic.IEnumerable<string> GetArgs(string prefix, int n) | ||
{ | ||
for (int i = 0; i < n; ++i) | ||
{ | ||
yield return prefix + i; | ||
} | ||
} | ||
|
||
[MenuItem(VRM.VRMVersion.MENU + "/Generate GenericExpressionCallFactory")] | ||
static void Generate() | ||
{ | ||
var sb = new StringBuilder(); | ||
using (var w = new StringWriter(sb)) | ||
{ | ||
w.WriteLine(@" | ||
using System; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
namespace UniJSON | ||
{ | ||
public static partial class GenericExpressionCallFactory | ||
{ | ||
"); | ||
// Create | ||
for (int i = 1; i <= ARGS; ++i) | ||
{ | ||
var g = String.Join(", ", GetArgs("A", i).ToArray()); | ||
var a = String.Join(", ", GetArgs("a", i).ToArray()); | ||
|
||
var source = @" | ||
#if UNITY_5 | ||
public static Delegate Create<S, $0>(MethodInfo m) | ||
#else | ||
public static Action<S, $0> Create<S, $0>(MethodInfo m) | ||
#endif | ||
{ | ||
var self = Expression.Parameter(m.DeclaringType, m.Name); | ||
var args = m.GetParameters().Select(x => Expression.Parameter(x.ParameterType, x.Name)).ToArray(); | ||
var call = Expression.Call(self, m, args); | ||
return | ||
#if UNITY_5 | ||
#else | ||
(Action<S, $0>) | ||
#endif | ||
Expression.Lambda(call, new[] { self }.Concat(args).ToArray()).Compile(); | ||
} | ||
".Replace("$0", g).Replace("$1", a); | ||
|
||
w.WriteLine(source); | ||
} | ||
|
||
// CreateWithThis | ||
for (int i = 1; i <= ARGS; ++i) | ||
{ | ||
var g = String.Join(", ", GetArgs("A", i).ToArray()); | ||
|
||
var source = @" | ||
#if UNITY_5 | ||
public static Delegate CreateWithThis<S, $0>(MethodInfo m, S instance) | ||
#else | ||
public static Action<$0> CreateWithThis<S, $0>(MethodInfo m, S instance) | ||
#endif | ||
{ | ||
if (m.IsStatic) | ||
{ | ||
if (instance != null) | ||
{ | ||
throw new ArgumentException(); | ||
} | ||
} | ||
else | ||
{ | ||
if (instance == null) | ||
{ | ||
throw new ArgumentNullException(); | ||
} | ||
} | ||
var self = Expression.Constant(instance, typeof(S)); // thisを定数化 | ||
var args = m.GetParameters().Select(x => Expression.Parameter(x.ParameterType, x.Name)).ToArray(); | ||
MethodCallExpression call; | ||
if (m.IsStatic) | ||
{ | ||
call = Expression.Call(m, args); | ||
} | ||
else | ||
{ | ||
call = Expression.Call(self, m, args); | ||
} | ||
return | ||
#if UNITY_5 | ||
#else | ||
(Action<$0>) | ||
#endif | ||
Expression.Lambda(call, args).Compile(); | ||
} | ||
".Replace("$0", g); | ||
|
||
w.WriteLine(source); | ||
} | ||
|
||
w.WriteLine(@" | ||
} | ||
} | ||
"); | ||
} | ||
|
||
var path = UniGLTF.UnityPath.FromUnityPath(GENERATE_PATH); | ||
File.WriteAllText(path.FullPath, sb.ToString().Replace("\r\n", "\n")); | ||
path.ImportAsset(); | ||
} | ||
#endif | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericExpressionCallFactory.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.