@@ -27,45 +27,13 @@ public static int InjectHooks(Dictionary<string, List<AttributeData>> attributes
2727 if ( ! attributes . ContainsKey ( nameof ( HookAttributes ) ) )
2828 return 0 ;
2929 var injectedCorrectly = 0 ;
30- var returnValueAttributes = new Dictionary < MethodDefinition , AttributeData > ( ) ;
31- returnValueAttributes = attributes [ nameof ( HasReturnValueAttribute ) ] . ToDictionary ( x => x . Method , y => y ) ;
3230 foreach ( var hook in attributes [ nameof ( HookAttributes ) ] )
3331 {
3432 hook . TargetData = Cecil . ConvertStringToClassAndMethod ( hook . Attribute . ConstructorArguments [ 0 ] . Value . ToString ( ) , assemblyDefinition ) ;
3533 if ( hook . TargetData == null )
3634 continue ;
37- if ( ! returnValueAttributes . ContainsKey ( hook . Method ) )
38- {
39- if ( Cecil . Inject ( hook . TargetData , hook ) )
40- ++ injectedCorrectly ;
41- continue ;
42- }
43- ConsoleHelper . WriteMessage ( $ "F1: { hook . Method . FullName } : { hook . Method . ReturnType . Name } | F2: { hook . TargetData . methodDefinition . FullName } : { hook . TargetData . methodDefinition . ReturnType . Name } ") ;
44- if ( hook . Method . ReturnType . Name != hook . TargetData . methodDefinition . ReturnType . Name )
45- {
46- ConsoleHelper . WriteError ( $ "Return types of { hook . Method . Name } and { hook . TargetData . methodDefinition . Name } do not match. They need to match to add the [HasReturnValue] attribute.") ;
47- continue ;
48- }
49- ConsoleHelper . WriteMessage ( $ "Mdef: { hook . TargetData . methodDefinition } | Tdef: { hook . TargetData . typeDefinition } ") ;
50- var il = hook . TargetData . methodDefinition . Body . GetILProcessor ( ) ;
51- var firstInstruction = il . Body . Instructions . First ( ) ;
52- var method = hook . TargetData . methodDefinition ;
53- Instruction lastInstruction = null ;
54- Instruction currInstruction = null ;
55- if ( ! method . IsStatic && ! method . DeclaringType . IsSealed )
56- {
57- currInstruction = il . Create ( OpCodes . Ldarg_0 ) ;
58- if ( firstInstruction != null )
59- il . InsertBefore ( firstInstruction , currInstruction ) ;
60- else
61- il . Append ( currInstruction ) ;
62- }
63- lastInstruction = currInstruction ;
64- currInstruction = il . Create ( OpCodes . Call , assemblyDefinition . MainModule . Import ( hook . Method ) ) ;
65- il . InsertAfter ( lastInstruction ?? firstInstruction , currInstruction ) ;
66- lastInstruction = currInstruction ;
67- il . InsertAfter ( lastInstruction , Instruction . Create ( OpCodes . Ret ) ) ;
68- injectedCorrectly += 2 ; // HookAttribute and HasReturnValue both succeeded, add two
35+ if ( Cecil . Inject ( hook . TargetData , hook ) )
36+ ++ injectedCorrectly ;
6937 }
7038 return injectedCorrectly ;
7139 }
0 commit comments