com.wallstop-studios.unity-helpers 2.0.0-rc79.8 → 2.0.0-rc79.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -407,9 +407,79 @@ namespace WallstopStudios.UnityHelpers.Core.Random
407
407
  {
408
408
  return NextOf(list);
409
409
  }
410
-
411
410
  int index = Next(collection.Count);
412
- return collection.ElementAt(index);
411
+
412
+ switch (collection)
413
+ {
414
+ case HashSet<T> hashSet:
415
+ {
416
+ int i = 0;
417
+ foreach (T element in hashSet)
418
+ {
419
+ if (index == i++)
420
+ {
421
+ return element;
422
+ }
423
+ }
424
+ throw new ArgumentException(nameof(collection));
425
+ }
426
+ case SortedSet<T> sortedSet:
427
+ {
428
+ int i = 0;
429
+ foreach (T element in sortedSet)
430
+ {
431
+ if (index == i++)
432
+ {
433
+ return element;
434
+ }
435
+ }
436
+
437
+ throw new ArgumentException(nameof(collection));
438
+ }
439
+ case LinkedList<T> linkedList:
440
+ {
441
+ int i = 0;
442
+ foreach (T element in linkedList)
443
+ {
444
+ if (index == i++)
445
+ {
446
+ return element;
447
+ }
448
+ }
449
+
450
+ throw new ArgumentException(nameof(collection));
451
+ }
452
+ case Queue<T> queue:
453
+ {
454
+ int i = 0;
455
+ foreach (T element in queue)
456
+ {
457
+ if (index == i++)
458
+ {
459
+ return element;
460
+ }
461
+ }
462
+
463
+ throw new ArgumentException(nameof(collection));
464
+ }
465
+ case Stack<T> stack:
466
+ {
467
+ int i = 0;
468
+ foreach (T element in stack)
469
+ {
470
+ if (index == i++)
471
+ {
472
+ return element;
473
+ }
474
+ }
475
+
476
+ throw new ArgumentException(nameof(collection));
477
+ }
478
+ default:
479
+ {
480
+ return collection.ElementAt(index);
481
+ }
482
+ }
413
483
  }
414
484
 
415
485
  public T NextOf<T>(IReadOnlyList<T> list)
@@ -5,6 +5,7 @@ namespace WallstopStudios.UnityHelpers.Utils
5
5
  using System.Reflection;
6
6
  using System.Text;
7
7
  using UnityEngine;
8
+ using WallstopStudios.UnityHelpers.Core.Helper;
8
9
 
9
10
  public static class Buffers
10
11
  {
@@ -38,10 +39,16 @@ namespace WallstopStudios.UnityHelpers.Utils
38
39
  public static class WallstopGenericPool<T>
39
40
  where T : new()
40
41
  {
42
+ public static Action<T> clearAction;
43
+
41
44
  private static readonly List<T> _pool = new();
42
- private static readonly Action<T> _clearAction = GetClearAction();
43
45
  private static readonly Action<T> _onDispose = Release;
44
46
 
47
+ static WallstopGenericPool()
48
+ {
49
+ clearAction = GetClearAction();
50
+ }
51
+
45
52
  public static PooledResource<T> Get()
46
53
  {
47
54
  if (_pool.Count == 0)
@@ -57,26 +64,34 @@ namespace WallstopStudios.UnityHelpers.Utils
57
64
 
58
65
  private static Action<T> GetClearAction()
59
66
  {
60
- Type type = typeof(T);
61
- foreach (
62
- MethodInfo method in type.GetMethods(BindingFlags.Instance | BindingFlags.Public)
63
- )
67
+ try
64
68
  {
65
- if (
66
- string.Equals(method.Name, "Clear", StringComparison.Ordinal)
67
- && method.GetParameters().Length == 0
69
+ Type type = typeof(T);
70
+ foreach (
71
+ MethodInfo method in type.GetMethods(
72
+ BindingFlags.Instance | BindingFlags.Public
73
+ )
68
74
  )
69
75
  {
70
- return (Action<T>)Delegate.CreateDelegate(typeof(Action<T>), method);
76
+ if (
77
+ string.Equals(method.Name, "Clear", StringComparison.Ordinal)
78
+ && method.GetParameters().Length == 0
79
+ )
80
+ {
81
+ return (Action<T>)Delegate.CreateDelegate(typeof(Action<T>), method);
82
+ }
71
83
  }
72
84
  }
73
-
74
- return _ => { };
85
+ catch
86
+ {
87
+ // Swallow
88
+ }
89
+ return null;
75
90
  }
76
91
 
77
92
  private static void Release(T resource)
78
93
  {
79
- _clearAction.Invoke(resource);
94
+ clearAction?.Invoke(resource);
80
95
  _pool.Add(resource);
81
96
  }
82
97
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.wallstop-studios.unity-helpers",
3
- "version": "2.0.0-rc79.8",
3
+ "version": "2.0.0-rc79.9",
4
4
  "displayName": "Unity Helpers",
5
5
  "description": "Various Unity Helper Library",
6
6
  "dependencies": {},
@@ -52,3 +52,4 @@
52
52
 
53
53
 
54
54
 
55
+