Skip to content

Commit ec050a3

Browse files
Update inner exception throwing
1 parent 8eaade7 commit ec050a3

1 file changed

Lines changed: 25 additions & 9 deletions

File tree

src/Plugins/CSPlugin.cs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Reflection;
5+
using System.Runtime.ExceptionServices;
56
using Oxide.Pooling;
67
using HarmonyLib;
78

@@ -87,25 +88,33 @@ public CSPlugin()
8788

8889
// Find all hooks in the plugin and any base classes derived from CSPlugin
8990
Type type = GetType();
90-
List<Type> types = new List<Type> { type };
91+
List<Type> types = new()
92+
{
93+
type
94+
};
95+
9196
while (type != typeof(CSPlugin))
9297
{
9398
types.Add(type = type.BaseType);
9499
}
95100

96101
// Add hooks implemented in base classes before user implemented methods
97-
for (int i = types.Count - 1; i >= 0; i--)
102+
int typeCount = types.Count;
103+
for (int i = typeCount - 1; i >= 0; i--)
98104
{
99-
foreach (MethodInfo method in types[i].GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
105+
MethodInfo[] methods = types[i].GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
106+
int methodCount = methods.Length;
107+
for (int j = 0; j < methodCount; j++)
100108
{
101-
object[] attr = method.GetCustomAttributes(typeof(HookMethodAttribute), true);
102-
if (attr.Length < 1)
109+
MethodInfo method = methods[j];
110+
object[] customAttributes = method.GetCustomAttributes(typeof(HookMethodAttribute), true);
111+
if (customAttributes.Length < 1)
103112
{
104113
continue;
105114
}
106115

107-
HookMethodAttribute hookmethod = attr[0] as HookMethodAttribute;
108-
AddHookMethod(hookmethod?.Name, method);
116+
HookMethodAttribute hookMethodAttribute = (HookMethodAttribute)customAttributes[0];
117+
AddHookMethod(hookMethodAttribute?.Name, method);
109118
}
110119
}
111120
}
@@ -251,13 +260,20 @@ protected sealed override object OnCallHook(string name, object[] args)
251260
{
252261
returnvalue = InvokeMethod(h, hookArgs);
253262
}
254-
catch (TargetInvocationException ex)
263+
catch (TargetInvocationException exception)
255264
{
256265
if (pooledArray)
257266
{
258267
ObjectArrayPool.Return(hookArgs);
259268
}
260-
throw ex.InnerException ?? ex;
269+
270+
Exception? innerException = exception.InnerException;
271+
if (innerException != null)
272+
{
273+
ExceptionDispatchInfo.Capture(innerException).Throw();
274+
}
275+
276+
throw;
261277
}
262278

263279
if (received != h.Parameters.Length)

0 commit comments

Comments
 (0)