Skip to content

Commit e0bbf20

Browse files
committed
Removed HasReturnValue as you can use out as parameter (didn't know that, wow)
1 parent ed709d1 commit e0bbf20

6 files changed

Lines changed: 2 additions & 112 deletions

File tree

HookAttribute/HasReturnValueAttribute.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

HookAttribute/HookAttribute.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
<Reference Include="System.Xml" />
4242
</ItemGroup>
4343
<ItemGroup>
44-
<Compile Include="HasReturnValueAttribute.cs" />
4544
<Compile Include="AddMethodAttribute.cs" />
4645
<Compile Include="HookAttribute.cs" />
4746
<Compile Include="Properties\AssemblyInfo.cs" />

TestProject/Core.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ namespace TestProject
1111
public class Core
1212
{
1313
[HookAttribute.Hook("Injected.Method3")]
14-
[HasReturnValue]
1514
public static bool InjectedMethod_M(Injected instance)
1615
{
1716
return true;

UniversalUnityHooks/Attributes/HasReturnValueAttribute.cs

Lines changed: 0 additions & 59 deletions
This file was deleted.

UniversalUnityHooks/Attributes/HookAttributes.cs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

UniversalUnityHooks/UniversalUnityHooks.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
<Compile Include="Attributes.cs" />
8989
<Compile Include="Attributes\AddMethodAttribute.cs" />
9090
<Compile Include="Attributes\Attribute.cs" />
91-
<Compile Include="Attributes\HasReturnValueAttribute.cs" />
9291
<Compile Include="Cecil.cs" />
9392
<Compile Include="Attributes\HookAttributes.cs" />
9493
<Compile Include="Program.cs" />

0 commit comments

Comments
 (0)