Skip to content

Commit

Permalink
Merge pull request #12 from BUTR/dev
Browse files Browse the repository at this point in the history
v1.0.6
  • Loading branch information
Aragas authored Aug 30, 2020
2 parents 49a4bfb + 899d9ed commit 1f7771c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!--Development Variables-->
<PropertyGroup>
<!--Module Version-->
<Version>1.0.5</Version>
<Version>1.0.6</Version>
<!--Harmony Version-->
<HarmonyVersion>2.0.2</HarmonyVersion>
<!--Current Bannerlord Stable Version-->
Expand Down
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
---------------------------------------------------------------------------------------------------
Version: 1.0.6
Game Versions: e1.4.3,e1.5.0,e1.5.1
* Added new Delegate extensions in AccessTools2
* Added new ConstructorInfo extensions in SymbolExtensions2
---------------------------------------------------------------------------------------------------
Version: 1.0.5
Game Versions: e1.4.3,e1.5.0,e1.5.1
* Added an empty AppDomainManager to prevent Fixed Launcher issues
Expand Down
76 changes: 55 additions & 21 deletions src/Bannerlord.ButterLib/Common/Helpers/AccessTools2.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

using static HarmonyLib.AccessTools;
Expand All @@ -8,61 +11,92 @@ namespace Bannerlord.ButterLib.Common.Helpers
/// <summary>An extension of Harmony's helper class for reflection related functions</summary>
public static class AccessTools2
{
/// <summary>Gets the delegate for a method by searching the type and all its super types</summary>
/// <summary>Allows to use object as Delegate's instance type.</summary>
/// <param name="type">The type where the method is declared</param>
/// <param name="method">The name of the method (case sensitive)</param>
/// <returns>A delegate or null when type/name is null or when the method cannot be found</returns>
public static TDelegate? GetDelegate<TDelegate>(Type type, string method) where TDelegate : Delegate
public static TDelegate? GetDelegateObjectInstance<TDelegate>(Type type, string method) where TDelegate : Delegate
{
var miOriginal = Method(type, method);
return miOriginal != null ? GetDelegate<TDelegate>(miOriginal) : null;
return miOriginal != null ? GetDelegateObjectInstance<TDelegate>(miOriginal) : null;
}

/// <summary>Gets the delegate for a method by searching the type and all its super types</summary>
/// <param name="instance">The instance where the method is declared</param>
/// <summary>Gets the delegate for a directly declared method</summary>
/// <param name="type">The type where the method is declared</param>
/// <param name="method">The name of the method (case sensitive)</param>
/// <returns>A delegate or null when type/name is null or when the method cannot be found</returns>
public static TDelegate? GetDelegate<TDelegate, TInstance>(TInstance instance, string method) where TDelegate : Delegate where TInstance : notnull
public static TDelegate? GetDeclaredDelegateObjectInstance<TDelegate>(Type type, string method) where TDelegate : Delegate
{
var miOriginal = Method(instance.GetType(), method);
return miOriginal == null ? null : GetDelegate<TDelegate, TInstance>(instance, miOriginal);
var miOriginal = DeclaredMethod(type, method);
return miOriginal != null ? GetDelegateObjectInstance<TDelegate>(miOriginal) : null;
}

/// <summary>Gets the delegate for a directly declared method</summary>
/// <summary>Allows to use object as Delegate's instance type.</summary>
/// <param name="methodInfo">The method's <see cref="MethodInfo"/></param>
/// <returns>A delegate or null when the method is null</returns>
public static TDelegate? GetDelegateObjectInstance<TDelegate>(MethodInfo methodInfo) where TDelegate : Delegate
{
if (methodInfo == null) return null;

var instance = Expression.Parameter(typeof(object), "instance");
var parameters = methodInfo.GetParameters().Select((t2, i) => Expression.Parameter(t2.ParameterType, $"p{i}")).ToList();

var body = Expression.Call(
Expression.Convert(instance, methodInfo.DeclaringType!),
methodInfo,
parameters);
return Expression.Lambda<TDelegate>(body, new List<ParameterExpression> { instance }.Concat(parameters)).Compile();
}


/// <summary>Gets the delegate for a method by searching the type and all its super types</summary>
/// <param name="type">The type where the method is declared</param>
/// <param name="method">The name of the method (case sensitive)</param>
/// <returns>A delegate or null when type/name is null or when the method cannot be found</returns>
public static TDelegate? GetDeclaredDelegate<TDelegate>(Type type, string method) where TDelegate : Delegate
public static TDelegate? GetDelegate<TDelegate>(Type type, string method) where TDelegate : Delegate
{
var miOriginal = DeclaredMethod(type, method);
var miOriginal = Method(type, method);
return miOriginal != null ? GetDelegate<TDelegate>(miOriginal) : null;
}

/// <summary>Gets the delegate for a directly declared method</summary>
/// <param name="instance">The instance where the method is declared</param>
/// <param name="type">The type where the method is declared</param>
/// <param name="method">The name of the method (case sensitive)</param>
/// <returns>A delegate or null when type/name is null or when the method cannot be found</returns>
public static TDelegate? GetDeclaredDelegate<TDelegate, TInstance>(TInstance instance, string method) where TDelegate : Delegate where TInstance : notnull
public static TDelegate? GetDeclaredDelegate<TDelegate>(Type type, string method) where TDelegate : Delegate
{
var miOriginal = DeclaredMethod(instance.GetType(), method);
return miOriginal == null ? null : GetDelegate<TDelegate, TInstance>(instance, miOriginal);
var miOriginal = DeclaredMethod(type, method);
return miOriginal != null ? GetDelegate<TDelegate>(miOriginal) : null;
}

/// <summary>Gets the delegate for a method</summary>
/// <param name="methodInfo">The method's <see cref="MethodInfo"/></param>
/// <returns>A delegate or null when type/name is null or when the method cannot be found</returns>
/// <returns>A delegate or null when the method is null</returns>
public static TDelegate? GetDelegate<TDelegate>(MethodInfo methodInfo) where TDelegate : Delegate
{
if (methodInfo == null) return null;
return Delegate.CreateDelegate(typeof(TDelegate), methodInfo) as TDelegate;
}

/// <summary>Gets the delegate for a method</summary>

/// <summary>Gets the delegate for a method by searching the type and all its super types</summary>
/// <param name="instance">The instance where the method is declared</param>
/// <param name="methodInfo">The method's <see cref="MethodInfo"/></param>
/// <param name="method">The name of the method (case sensitive)</param>
/// <returns>A delegate or null when type/name is null or when the method cannot be found</returns>
public static TDelegate? GetDelegate<TDelegate>(object instance, MethodInfo methodInfo) where TDelegate : Delegate
public static TDelegate? GetDelegate<TDelegate, TInstance>(TInstance instance, string method) where TDelegate : Delegate where TInstance : notnull
{
return Delegate.CreateDelegate(typeof(TDelegate), instance, methodInfo.Name) as TDelegate;
var miOriginal = Method(instance.GetType(), method);
return miOriginal == null ? null : GetDelegate<TDelegate, TInstance>(instance, miOriginal);
}

/// <summary>Gets the delegate for a directly declared method</summary>
/// <param name="instance">The instance where the method is declared</param>
/// <param name="method">The name of the method (case sensitive)</param>
/// <returns>A delegate or null when type/name is null or when the method cannot be found</returns>
public static TDelegate? GetDeclaredDelegate<TDelegate, TInstance>(TInstance instance, string method) where TDelegate : Delegate where TInstance : notnull
{
var miOriginal = DeclaredMethod(instance.GetType(), method);
return miOriginal == null ? null : GetDelegate<TDelegate, TInstance>(instance, miOriginal);
}

/// <summary>Gets the delegate for a method</summary>
Expand All @@ -71,7 +105,7 @@ public static class AccessTools2
/// <returns>A delegate or null when type/name is null or when the method cannot be found</returns>
public static TDelegate? GetDelegate<TDelegate, TInstance>(TInstance instance, MethodInfo methodInfo) where TDelegate : Delegate where TInstance : notnull
{
return GetDelegate<TDelegate>(instance, methodInfo);
return Delegate.CreateDelegate(typeof(TDelegate), instance, methodInfo.Name) as TDelegate;
}
}
}
16 changes: 16 additions & 0 deletions src/Bannerlord.ButterLib/Common/Helpers/SymbolExtensions2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ namespace Bannerlord.ButterLib.Common.Helpers
{
public static class SymbolExtensions2
{
public static ConstructorInfo GetConstructorInfo<T>(Expression<Func<T>> expression)
{
return GetConstructorInfo((LambdaExpression)expression);
}
public static ConstructorInfo GetConstructorInfo<T, TResult>(Expression<Func<T, TResult>> expression)
{
return GetConstructorInfo((LambdaExpression)expression);
}
public static ConstructorInfo GetConstructorInfo(LambdaExpression expression)
{
if (!(expression.Body is NewExpression body) || body.Constructor is null)
throw new ArgumentException("Invalid Expression. Expression should consist of a Field return only.");

return body.Constructor;
}

public static FieldInfo GetFieldInfo<T>(Expression<Func<T>> expression)
{
return GetFieldInfo((LambdaExpression)expression);
Expand Down

0 comments on commit 1f7771c

Please sign in to comment.