Skip to content

Commit

Permalink
fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
labbbirder committed Apr 1, 2024
1 parent ab79ef5 commit ffa1721
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 8 deletions.
75 changes: 71 additions & 4 deletions Editor/InjectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.TestTools;
using UnityEditorInternal;
using UnityEditor;
using UnityEditor.Compilation;
using UnityEngine.Assertions;
using System.Text;

namespace com.bbbirder.injection.editor
{
Expand Down Expand Up @@ -371,10 +371,77 @@ internal static Type GetUnderlyingType(this TypeReference td)
=> td.IsPrimitive ? Type.GetType(td.Name) : objType;

internal static string GetSignature(this MethodDefinition md)
=> $"{md.Name}({string.Join(",", md.Parameters.Select(p => p.ParameterType.FullName))})";
{
var builder = new StringBuilder();
builder.Append(md.Name);
if (md.HasGenericParameters)
{
builder.Append('`');
builder.Append(md.GenericParameters.Count);
}
builder.Append('(');

if (md.HasParameters)
{
var parameters = md.Parameters;
for (int i = 0; i < parameters.Count; i++)
{
ParameterDefinition parameterDefinition = parameters[i];
if (i > 0)
{
builder.Append(",");
}

AppendTypeFullName(builder, parameterDefinition.ParameterType);
}
}

builder.Append(')');
return builder.ToString();

static void AppendTypeFullName(StringBuilder builder, TypeReference type)
{
if (!string.IsNullOrEmpty(type.Namespace))
{
builder.Append(type.Namespace);
builder.Append("::");
}

var stack = new Stack<TypeReference>();
var declaringType = type;
while (null != declaringType)
{
stack.Push(declaringType);
declaringType = declaringType.DeclaringType;
}
while (stack.TryPop(out var nestedType)) AppendNestedType(builder, nestedType);
}
static void AppendNestedType(StringBuilder builder, TypeReference type)
{
builder.Append(type.Name);
if (type is GenericInstanceType gInst)
{
builder.Append('<');
var args = gInst.GenericArguments;
for (int i = 0; i < args.Count; i++)
{
if (i != 0)
{
builder.Append(',');
}
AppendTypeFullName(builder, args[i]);
}
builder.Append('>');
}
}
}

internal static MethodReference FindMethod(this TypeDefinition td, string methodSignature)
=> td.Module.ImportReference(td.Methods.FirstOrDefault(m => m.GetSignature().Equals(methodSignature)));
{
var method = td.Methods.FirstOrDefault(m => m.GetSignature().Equals(methodSignature));
if (method is null) return null;
return td.Module.ImportReference(method);
}

internal static MethodReference FindMethodByName(this TypeDefinition td, string methodName)
=> td.Module.ImportReference(td.Methods.FirstOrDefault(m => m.Name.Equals(methodName)));
Expand Down Expand Up @@ -492,7 +559,7 @@ internal static TypeDefinition CreateDelegateType(this ModuleDefinition assembly
return dt;
}


static Type voidType = typeof(void);
static Type objType = typeof(object);
static OpCode[] s_ldargs = new[] { OpCodes.Ldarg_0, OpCodes.Ldarg_1, OpCodes.Ldarg_2, OpCodes.Ldarg_3 };
Expand Down
66 changes: 63 additions & 3 deletions Runtime/FixHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Linq;
using System.IO;
using UnityEngine;
using com.bbbirder.injection;
using System.Text;

namespace com.bbbirder.injection
{
Expand Down Expand Up @@ -221,8 +221,68 @@ public static string GetAssemblyPath(this Assembly assembly)
return null;
}

public static string GetSignature(this MethodBase m)
=> $"{m.Name}({string.Join(",", m.GetParameters().Select(p => p.ParameterType.FullName))})";
public static string GetSignature(this MethodBase method)
{
var builder = new StringBuilder();
builder.Append(method.Name);
if (method.IsGenericMethod)
{
builder.Append('`');
builder.Append(method.GetGenericArguments().Length);
}
builder.Append('(');

var parameters = method.GetParameters();
for (int i = 0; i < parameters.Length; i++)
{
var parameterDefinition = parameters[i];
if (i > 0)
{
builder.Append(",");
}

AppendTypeFullName(builder, parameterDefinition.ParameterType);
}

builder.Append(')');
return builder.ToString();

static void AppendTypeFullName(StringBuilder builder, Type type)
{
if (!string.IsNullOrEmpty(type.Namespace))
{
builder.Append(type.Namespace);
builder.Append("::");
}

var stack = new Stack<Type>();
var declaringType = type;
while (null != declaringType)
{
stack.Push(declaringType);
declaringType = declaringType.DeclaringType;
}
while (stack.TryPop(out var name)) AppendNestedType(builder, name);
}
static void AppendNestedType(StringBuilder builder, Type type)
{
builder.Append(type.Name);
if (type.IsGenericType)
{
builder.Append('<');
var args = type.GenericTypeArguments;
for (int i = 0; i < args.Length; i++)
{
if (i != 0)
{
builder.Append(',');
}
AppendTypeFullName(builder, args[i]);
}
builder.Append('>');
}
}
}

static InjectionInfo[] m_allInjections;
public static InjectionInfo[] allInjections => m_allInjections ??= GetAllInjections();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "com.bbbirder.injection",
"displayName": "Unity Injection",
"description": "Unity注入模块,可以运行时改变被注入函数实现。",
"version": "1.3.29",
"version": "1.3.31",
"hideInEditor": false,
"author": "bbbirder <[email protected]>",
"dependencies": {
Expand Down

0 comments on commit ffa1721

Please sign in to comment.