|
5 | 5 | using System.IO;
|
6 | 6 | using System.Linq;
|
7 | 7 | using UnityEngine;
|
8 |
| -using UnityEngine.TestTools; |
9 | 8 | using UnityEditorInternal;
|
10 | 9 | using UnityEditor;
|
11 | 10 | using UnityEditor.Compilation;
|
12 | 11 | using UnityEngine.Assertions;
|
| 12 | +using System.Text; |
13 | 13 |
|
14 | 14 | namespace com.bbbirder.injection.editor
|
15 | 15 | {
|
@@ -371,10 +371,77 @@ internal static Type GetUnderlyingType(this TypeReference td)
|
371 | 371 | => td.IsPrimitive ? Type.GetType(td.Name) : objType;
|
372 | 372 |
|
373 | 373 | internal static string GetSignature(this MethodDefinition md)
|
374 |
| - => $"{md.Name}({string.Join(",", md.Parameters.Select(p => p.ParameterType.FullName))})"; |
| 374 | + { |
| 375 | + var builder = new StringBuilder(); |
| 376 | + builder.Append(md.Name); |
| 377 | + if (md.HasGenericParameters) |
| 378 | + { |
| 379 | + builder.Append('`'); |
| 380 | + builder.Append(md.GenericParameters.Count); |
| 381 | + } |
| 382 | + builder.Append('('); |
| 383 | + |
| 384 | + if (md.HasParameters) |
| 385 | + { |
| 386 | + var parameters = md.Parameters; |
| 387 | + for (int i = 0; i < parameters.Count; i++) |
| 388 | + { |
| 389 | + ParameterDefinition parameterDefinition = parameters[i]; |
| 390 | + if (i > 0) |
| 391 | + { |
| 392 | + builder.Append(","); |
| 393 | + } |
| 394 | + |
| 395 | + AppendTypeFullName(builder, parameterDefinition.ParameterType); |
| 396 | + } |
| 397 | + } |
| 398 | + |
| 399 | + builder.Append(')'); |
| 400 | + return builder.ToString(); |
| 401 | + |
| 402 | + static void AppendTypeFullName(StringBuilder builder, TypeReference type) |
| 403 | + { |
| 404 | + if (!string.IsNullOrEmpty(type.Namespace)) |
| 405 | + { |
| 406 | + builder.Append(type.Namespace); |
| 407 | + builder.Append("::"); |
| 408 | + } |
| 409 | + |
| 410 | + var stack = new Stack<TypeReference>(); |
| 411 | + var declaringType = type; |
| 412 | + while (null != declaringType) |
| 413 | + { |
| 414 | + stack.Push(declaringType); |
| 415 | + declaringType = declaringType.DeclaringType; |
| 416 | + } |
| 417 | + while (stack.TryPop(out var nestedType)) AppendNestedType(builder, nestedType); |
| 418 | + } |
| 419 | + static void AppendNestedType(StringBuilder builder, TypeReference type) |
| 420 | + { |
| 421 | + builder.Append(type.Name); |
| 422 | + if (type is GenericInstanceType gInst) |
| 423 | + { |
| 424 | + builder.Append('<'); |
| 425 | + var args = gInst.GenericArguments; |
| 426 | + for (int i = 0; i < args.Count; i++) |
| 427 | + { |
| 428 | + if (i != 0) |
| 429 | + { |
| 430 | + builder.Append(','); |
| 431 | + } |
| 432 | + AppendTypeFullName(builder, args[i]); |
| 433 | + } |
| 434 | + builder.Append('>'); |
| 435 | + } |
| 436 | + } |
| 437 | + } |
375 | 438 |
|
376 | 439 | internal static MethodReference FindMethod(this TypeDefinition td, string methodSignature)
|
377 |
| - => td.Module.ImportReference(td.Methods.FirstOrDefault(m => m.GetSignature().Equals(methodSignature))); |
| 440 | + { |
| 441 | + var method = td.Methods.FirstOrDefault(m => m.GetSignature().Equals(methodSignature)); |
| 442 | + if (method is null) return null; |
| 443 | + return td.Module.ImportReference(method); |
| 444 | + } |
378 | 445 |
|
379 | 446 | internal static MethodReference FindMethodByName(this TypeDefinition td, string methodName)
|
380 | 447 | => td.Module.ImportReference(td.Methods.FirstOrDefault(m => m.Name.Equals(methodName)));
|
@@ -492,7 +559,7 @@ internal static TypeDefinition CreateDelegateType(this ModuleDefinition assembly
|
492 | 559 | return dt;
|
493 | 560 | }
|
494 | 561 |
|
495 |
| - |
| 562 | + |
496 | 563 | static Type voidType = typeof(void);
|
497 | 564 | static Type objType = typeof(object);
|
498 | 565 | static OpCode[] s_ldargs = new[] { OpCodes.Ldarg_0, OpCodes.Ldarg_1, OpCodes.Ldarg_2, OpCodes.Ldarg_3 };
|
|
0 commit comments