|
2 | 2 | using System; |
3 | 3 | using System.Collections.Generic; |
4 | 4 | using System.Reflection; |
| 5 | +using System.Runtime.ExceptionServices; |
5 | 6 | using Oxide.Pooling; |
6 | 7 | using HarmonyLib; |
7 | 8 |
|
@@ -87,25 +88,33 @@ public CSPlugin() |
87 | 88 |
|
88 | 89 | // Find all hooks in the plugin and any base classes derived from CSPlugin |
89 | 90 | Type type = GetType(); |
90 | | - List<Type> types = new List<Type> { type }; |
| 91 | + List<Type> types = new() |
| 92 | + { |
| 93 | + type |
| 94 | + }; |
| 95 | + |
91 | 96 | while (type != typeof(CSPlugin)) |
92 | 97 | { |
93 | 98 | types.Add(type = type.BaseType); |
94 | 99 | } |
95 | 100 |
|
96 | 101 | // 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--) |
98 | 104 | { |
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++) |
100 | 108 | { |
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) |
103 | 112 | { |
104 | 113 | continue; |
105 | 114 | } |
106 | 115 |
|
107 | | - HookMethodAttribute hookmethod = attr[0] as HookMethodAttribute; |
108 | | - AddHookMethod(hookmethod?.Name, method); |
| 116 | + HookMethodAttribute hookMethodAttribute = (HookMethodAttribute)customAttributes[0]; |
| 117 | + AddHookMethod(hookMethodAttribute?.Name, method); |
109 | 118 | } |
110 | 119 | } |
111 | 120 | } |
@@ -251,13 +260,20 @@ protected sealed override object OnCallHook(string name, object[] args) |
251 | 260 | { |
252 | 261 | returnvalue = InvokeMethod(h, hookArgs); |
253 | 262 | } |
254 | | - catch (TargetInvocationException ex) |
| 263 | + catch (TargetInvocationException exception) |
255 | 264 | { |
256 | 265 | if (pooledArray) |
257 | 266 | { |
258 | 267 | ObjectArrayPool.Return(hookArgs); |
259 | 268 | } |
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; |
261 | 277 | } |
262 | 278 |
|
263 | 279 | if (received != h.Parameters.Length) |
|
0 commit comments