Skip to content

Commit b8bdc89

Browse files
scorteanucosminMrBlue
authored andcommitted
Update various files
1 parent 426173b commit b8bdc89

4 files changed

Lines changed: 67 additions & 52 deletions

File tree

src/Libraries/Permission.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,8 @@ private void VerifyAndLoadUsersData()
155155

156156
user.Groups = new HashSet<string>(groups, StringComparer.OrdinalIgnoreCase);
157157

158-
if (result.ContainsKey(entry.Key))
158+
if (result.TryGetValue(entry.Key, out UserData? existing))
159159
{
160-
UserData existing = result[entry.Key];
161160
existing.Perms.UnionWith(user.Perms);
162161
existing.Groups.UnionWith(user.Groups);
163162

@@ -199,9 +198,8 @@ private void VerifyAndLoadGroupsData()
199198

200199
group.Perms = new HashSet<string>(permissions, StringComparer.OrdinalIgnoreCase);
201200

202-
if (result.ContainsKey(entry.Key))
201+
if (result.TryGetValue(entry.Key, out GroupData? existing))
203202
{
204-
GroupData existing = result[entry.Key];
205203
existing.Perms.UnionWith(group.Perms);
206204
changed = true;
207205

@@ -236,11 +234,10 @@ private Dictionary<string, GroupData> VerifyGroupData(Dictionary<string, GroupDa
236234

237235
group.Perms = new HashSet<string>(permissions, StringComparer.OrdinalIgnoreCase);
238236

239-
if (result.ContainsKey(entry.Key))
237+
if (result.TryGetValue(entry.Key, out GroupData? existing))
240238
{
241-
GroupData existing = result[entry.Key];
242239
// Get a matching entry.key from result.keys
243-
var existingKey = result.Keys.FirstOrDefault(x => x.Equals(entry.Key, StringComparison.OrdinalIgnoreCase));
240+
string? existingKey = result.Keys.FirstOrDefault(x => x.Equals(entry.Key, StringComparison.OrdinalIgnoreCase));
244241

245242
existing.Perms.UnionWith(group.Perms);
246243

src/Plugins/CSPlugin.cs

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ public override void HandleAddedToManager(PluginManager manager)
129129
base.HandleAddedToManager(manager);
130130

131131
// Subscribe us
132-
foreach (string hookname in Hooks.Keys)
132+
foreach (string hookName in Hooks.Keys)
133133
{
134-
Subscribe(hookname);
134+
Subscribe(hookName);
135135
}
136136

137137
try
@@ -149,8 +149,13 @@ public override void HandleAddedToManager(PluginManager manager)
149149
}
150150

151151
// Find all classes with the AutoPatch attribute and apply the patches
152-
foreach (Type nestedType in GetType().GetNestedTypes(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static))
152+
Type[] nestedTypes = GetType().GetNestedTypes(BindingFlags.DeclaredOnly | BindingFlags.Public |
153+
BindingFlags.NonPublic | BindingFlags.Static);
154+
155+
int nestedTypeCount = nestedTypes.Length;
156+
for (int i = 0; i < nestedTypeCount; i++)
153157
{
158+
Type nestedType = nestedTypes[i];
154159
object[] attr = nestedType.GetCustomAttributes(typeof(AutoPatchAttribute), false);
155160
if (attr.Length < 1)
156161
{
@@ -159,22 +164,26 @@ public override void HandleAddedToManager(PluginManager manager)
159164

160165
try
161166
{
162-
List<MethodInfo> harmonyMethods = HarmonyInstance.CreateClassProcessor(nestedType)?.Patch();
163-
167+
List<MethodInfo>? harmonyMethods = HarmonyInstance.CreateClassProcessor(nestedType)?.Patch();
164168
if (harmonyMethods == null || harmonyMethods.Count == 0)
165169
{
166-
Interface.Oxide.LogWarning($"[{Title}] AutoPatch attribute found on '{nestedType.Name}' but no HarmonyPatch methods found. Skipping.");
170+
Interface.Oxide.LogWarning(
171+
$"[{Title}] AutoPatch attribute found on '{nestedType.Name}' but no HarmonyPatch methods found. Skipping.");
167172
continue;
168173
}
169174

170-
foreach (MethodInfo method in harmonyMethods)
175+
int harmonyMethodCount = harmonyMethods.Count;
176+
for (int j = 0; j < harmonyMethodCount; j++)
171177
{
172-
Interface.Oxide.LogInfo($"[{Title}] Automatically Harmony patched '{method?.Name ?? "unknown"}' method. ({nestedType.Name})");
178+
MethodInfo harmonyMethod = harmonyMethods[j];
179+
Interface.Oxide.LogInfo(
180+
$"[{Title}] Automatically Harmony patched '{harmonyMethod?.Name ?? "unknown"}' method. ({nestedType.Name})");
173181
}
174182
}
175183
catch (Exception ex)
176184
{
177-
Interface.Oxide.LogException($"[{Title}] Failed to automatically Harmony patch '{nestedType.Name}'", ex);
185+
Interface.Oxide.LogException($"[{Title}] Failed to automatically Harmony patch '{nestedType.Name}'",
186+
ex);
178187
}
179188
}
180189
}
@@ -211,19 +220,22 @@ protected void AddHookMethod(string name, MethodInfo method)
211220
/// <returns></returns>
212221
protected sealed override object OnCallHook(string name, object[] args)
213222
{
214-
object returnvalue = null;
223+
object returnValue = null;
215224
bool pooledArray = false;
216225

217226
// Call all hooks that match the signature
218-
foreach (HookMethod h in FindHooks(name, args))
227+
List<HookMethod> hookMethods = FindHooks(name, args);
228+
int hookMethodCount = hookMethods.Count;
229+
for (int i = 0; i < hookMethodCount; i++)
219230
{
231+
HookMethod hookMethod = hookMethods[i];
220232
int received = args?.Length ?? 0;
221233
object[] hookArgs;
222234

223-
if (received != h.Parameters.Length)
235+
if (received != hookMethod.Parameters.Length)
224236
{
225237
// The call argument count is different to the declared callback methods argument count
226-
hookArgs = ObjectArrayPool.Take(h.Parameters.Length);
238+
hookArgs = ObjectArrayPool.Take(hookMethod.Parameters.Length);
227239
pooledArray = true;
228240

229241
if (received > 0 && hookArgs.Length > 0)
@@ -237,7 +249,7 @@ protected sealed override object OnCallHook(string name, object[] args)
237249
// Create additional parameters for arguments excluded in this hook call
238250
for (int n = received; n < hookArgs.Length; n++)
239251
{
240-
ParameterInfo parameter = h.Parameters[n];
252+
ParameterInfo parameter = hookMethod.Parameters[n];
241253
if (parameter.DefaultValue != null && parameter.DefaultValue != DBNull.Value)
242254
{
243255
// Use the default value that was provided by the method definition
@@ -258,7 +270,7 @@ protected sealed override object OnCallHook(string name, object[] args)
258270

259271
try
260272
{
261-
returnvalue = InvokeMethod(h, hookArgs);
273+
returnValue = InvokeMethod(hookMethod, hookArgs);
262274
}
263275
catch (TargetInvocationException exception)
264276
{
@@ -276,13 +288,13 @@ protected sealed override object OnCallHook(string name, object[] args)
276288
throw;
277289
}
278290

279-
if (received != h.Parameters.Length)
291+
if (received != hookMethod.Parameters.Length)
280292
{
281293
// A copy of the call arguments was used for this method call
282-
for (int n = 0; n < h.Parameters.Length; n++)
294+
for (int n = 0; n < hookMethod.Parameters.Length; n++)
283295
{
284296
// Copy output values for out and by reference arguments back to the calling args
285-
if (h.Parameters[n].IsOut || h.Parameters[n].ParameterType.IsByRef)
297+
if (hookMethod.Parameters[n].IsOut || hookMethod.Parameters[n].ParameterType.IsByRef)
286298
{
287299
args[n] = hookArgs[n];
288300
}
@@ -295,22 +307,23 @@ protected sealed override object OnCallHook(string name, object[] args)
295307
}
296308
}
297309

298-
return returnvalue;
310+
return returnValue;
299311
}
300312

301313
protected List<HookMethod> FindHooks(string name, object[] args)
302314
{
303315
// Get the full name of the hook `name(argument type 1, argument type 2, ..., argument type x)`
304316

305317
// Check the cache if we already found a match for this hook
306-
List<HookMethod> methods = HooksCache.GetHookMethod(name, args, out HookCache cache);
307-
if (methods != null)
318+
List<HookMethod> hookMethods = HooksCache.GetHookMethod(name, args, out HookCache cache);
319+
if (hookMethods != null)
308320
{
309-
return methods;
321+
return hookMethods;
310322
}
323+
311324
List<HookMethod> matches = new List<HookMethod>();
312325
// Get all hook methods that could match, return an empty list if none match
313-
if (!Hooks.TryGetValue(name, out methods))
326+
if (!Hooks.TryGetValue(name, out hookMethods))
314327
{
315328
return matches;
316329
}
@@ -319,13 +332,15 @@ protected List<HookMethod> FindHooks(string name, object[] args)
319332
HookMethod exactMatch = null;
320333
HookMethod overloadedMatch = null;
321334

322-
foreach (HookMethod h in methods)
335+
int hookMethodCount = hookMethods.Count;
336+
for (int i = 0; i < hookMethodCount; i++)
323337
{
338+
HookMethod hookMethod = hookMethods[i];
324339
// A base hook should always have a matching signature either directly or through inheritance
325340
// and should always be called as core functionality depends on it.
326-
if (h.IsBaseHook)
341+
if (hookMethod.IsBaseHook)
327342
{
328-
matches.Add(h);
343+
matches.Add(hookMethod);
329344
continue;
330345
}
331346

@@ -335,10 +350,10 @@ protected List<HookMethod> FindHooks(string name, object[] args)
335350

336351
bool pooledArray = false;
337352

338-
if (received != h.Parameters.Length)
353+
if (received != hookMethod.Parameters.Length)
339354
{
340355
// The call argument count is different to the declared callback methods argument count
341-
hookArgs = ObjectArrayPool.Take(h.Parameters.Length);
356+
hookArgs = ObjectArrayPool.Take(hookMethod.Parameters.Length);
342357
pooledArray = true;
343358

344359
if (received > 0 && hookArgs.Length > 0)
@@ -352,7 +367,7 @@ protected List<HookMethod> FindHooks(string name, object[] args)
352367
// Create additional parameters for arguments excluded in this hook call
353368
for (int n = received; n < hookArgs.Length; n++)
354369
{
355-
ParameterInfo parameter = h.Parameters[n];
370+
ParameterInfo parameter = hookMethod.Parameters[n];
356371
if (parameter.DefaultValue != null && parameter.DefaultValue != DBNull.Value)
357372
{
358373
// Use the default value that was provided by the method definition
@@ -371,16 +386,16 @@ protected List<HookMethod> FindHooks(string name, object[] args)
371386
hookArgs = args;
372387
}
373388

374-
if (h.HasMatchingSignature(hookArgs, out bool isExactMatch))
389+
if (hookMethod.HasMatchingSignature(hookArgs, out bool isExactMatch))
375390
{
376391
if (isExactMatch)
377392
{
378-
exactMatch = h;
393+
exactMatch = hookMethod;
379394
break;
380395
}
381396

382397
// Should we determine the level and call the closest overloaded match? Performance impact?
383-
overloadedMatch = h;
398+
overloadedMatch = hookMethod;
384399
}
385400

386401
if (pooledArray)

src/Plugins/HookCache.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public List<HookMethod> GetHookMethod(object[] args, int index, out HookCache ca
3030
}
3131

3232
HookCache nextCache;
33-
if (args[index] == null)
33+
object arg = args[index];
34+
if (arg == null)
3435
{
3536
if (!_cache.TryGetValue(NullKey, out nextCache))
3637
{
@@ -40,12 +41,14 @@ public List<HookMethod> GetHookMethod(object[] args, int index, out HookCache ca
4041
}
4142
else
4243
{
43-
if (!_cache.TryGetValue(args[index].GetType().FullName, out nextCache))
44+
string fullName = arg.GetType().FullName;
45+
if (!_cache.TryGetValue(fullName, out nextCache))
4446
{
4547
nextCache = new HookCache();
46-
_cache.Add(args[index].GetType().FullName, nextCache);
48+
_cache.Add(fullName, nextCache);
4749
}
4850
}
51+
4952
//Interface.Oxide.ServerConsole.AddMessage($"GetHookMethod {key} {index}");
5053
return nextCache.GetHookMethod(args, index + 1, out cache);
5154
}

src/Plugins/PluginManager.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,11 @@ public bool AddPlugin(Plugin plugin)
117117
/// <returns></returns>
118118
public bool RemovePlugin(Plugin plugin)
119119
{
120-
if (!loadedPlugins.ContainsKey(plugin.Name))
120+
if (!loadedPlugins.Remove(plugin.Name))
121121
{
122122
return false;
123123
}
124124

125-
loadedPlugins.Remove(plugin.Name);
126-
127125
lock (hookSubscriptions)
128126
{
129127
foreach (HookSubscriptions sub in hookSubscriptions.Values)
@@ -295,27 +293,29 @@ public object CallHook(string hook, params object[] args)
295293
continue;
296294
}
297295

298-
if (value.GetType().IsValueType)
296+
Type valueType = value.GetType();
297+
if (valueType.IsValueType)
299298
{
300-
if (!values[i].Equals(finalValue))
299+
if (!value.Equals(finalValue))
301300
{
302-
hookConflicts.Add($"{plugin.Name} - {value} ({value.GetType().Name})");
301+
hookConflicts.Add($"{plugin.Name} - {value} ({valueType.Name})");
303302
}
304303
}
305304
else
306305
{
307-
if (values[i] != finalValue)
306+
if (value != finalValue)
308307
{
309-
hookConflicts.Add($"{plugin.Name} - {value} ({value.GetType().Name})");
308+
hookConflicts.Add($"{plugin.Name} - {value} ({valueType.Name})");
310309
}
311310
}
312311
}
313312
if (hookConflicts.Count > 0)
314313
{
315314
hookConflicts.Add($"{finalPlugin.Name} ({finalValue} ({finalValue.GetType().Name}))");
316-
Logger.Write(LogType.Warning, "Calling hook {0} resulted in a conflict between the following plugins: {1}", hook, string.Join(", ", hookConflicts.ToArray()));
315+
Logger.Write(LogType.Warning, "Calling hook {0} resulted in a conflict between the following plugins: {1}", hook, hookConflicts.JoinValues(", "));
317316
}
318317
}
318+
319319
ObjectPool.Return(values);
320320
}
321321
finally
@@ -381,7 +381,7 @@ public object CallDeprecatedHook(string oldHook, string newHook, DateTime expire
381381
{
382382
// TODO: Add better handling
383383
lastDeprecatedWarningAt[oldHook] = now;
384-
Interface.Oxide.LogWarning($"'{subscriptions.Plugins[0].Name} v{subscriptions.Plugins[0].Version}' is using deprecated hook '{oldHook}', which will stop working on {expireDate.ToString("D")}. Please ask the author to update to '{newHook}'");
384+
Interface.Oxide.LogWarning($"'{subscriptions.Plugins[0].Name} v{subscriptions.Plugins[0].Version}' is using deprecated hook '{oldHook}', which will stop working on {expireDate:D}. Please ask the author to update to '{newHook}'");
385385
}
386386

387387
return CallHook(oldHook, args);

0 commit comments

Comments
 (0)