Skip to content

Commit

Permalink
Merge pull request #148 from dwango/add_GenericCallUtility
Browse files Browse the repository at this point in the history
Added GenericCallUtility
  • Loading branch information
yutopp authored Jan 28, 2019
2 parents 2665da1 + 9da1b3f commit 25f7cb4
Show file tree
Hide file tree
Showing 12 changed files with 1,099 additions and 2 deletions.
43 changes: 43 additions & 0 deletions Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs
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 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.

9 changes: 9 additions & 0 deletions Assets/VRM/UniJSON/Scripts/GenericCallUtility.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 25f7cb4

Please sign in to comment.