com.wallstop-studios.unity-helpers 2.0.0-rc43 → 2.0.0-rc45

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.
Files changed (31) hide show
  1. package/README.md +16 -0
  2. package/Runtime/Core/Helper/FormattingHelpers.cs +32 -0
  3. package/Runtime/Core/Helper/FormattingHelpers.cs.meta +3 -0
  4. package/Runtime/Core/Helper/Objects.cs +2 -2
  5. package/Runtime/Core/Helper/ReflectionHelpers.cs +156 -1
  6. package/Runtime/Core/Helper/SpriteHelpers.cs +7 -52
  7. package/Runtime/Core/Helper/WallMath.cs +5 -5
  8. package/Runtime/Core/Random/AbstractRandom.cs +6 -5
  9. package/Runtime/Core/Random/DotNetRandom.cs +2 -0
  10. package/Runtime/Core/Random/IRandom.cs +1 -0
  11. package/Runtime/Core/Random/LinearCongruentialGenerator.cs +49 -0
  12. package/Runtime/Core/Random/LinearCongruentialGenerator.cs.meta +3 -0
  13. package/Runtime/Core/Random/PcgRandom.cs +1 -1
  14. package/Runtime/Core/Random/RomuDuo.cs +1 -1
  15. package/Runtime/Core/Random/SplitMix64.cs +1 -1
  16. package/Runtime/Core/Random/SystemRandom.cs +1 -1
  17. package/Runtime/Core/Random/WyRandom.cs +1 -1
  18. package/Runtime/Core/Random/XorShiftRandom.cs +13 -8
  19. package/Runtime/Core/Random/XorShiroRandom.cs +2 -0
  20. package/Runtime/UI/LayeredImage.cs +2 -2
  21. package/Runtime/Utils/SpriteRendererMetadata.cs +104 -42
  22. package/Tests/Runtime/Helper/ArrayConverterTests.cs +3 -3
  23. package/Tests/Runtime/Helper/FormattingHelperTests.cs +129 -0
  24. package/Tests/Runtime/Helper/FormattingHelperTests.cs.meta +3 -0
  25. package/Tests/Runtime/Helper/ObjectHelperTests.cs +2 -2
  26. package/Tests/Runtime/Helper/ReflectionHelperTests.cs +356 -35
  27. package/Tests/Runtime/Helper/WallMathTests.cs +4 -4
  28. package/Tests/Runtime/Performance/RandomPerformanceTests.cs +21 -3
  29. package/Tests/Runtime/Random/LinearCongruentialGeneratorTests.cs +12 -0
  30. package/Tests/Runtime/Random/LinearCongruentialGeneratorTests.cs.meta +3 -0
  31. package/package.json +1 -1
@@ -11,6 +11,8 @@
11
11
  [DisallowMultipleComponent]
12
12
  public sealed class SpriteRendererMetadata : MonoBehaviour
13
13
  {
14
+ private bool Enabled => enabled && gameObject.activeInHierarchy;
15
+
14
16
  private readonly List<(Component component, Color color)> _colorStack = new();
15
17
  private readonly List<(Component component, Material material)> _materialStack = new();
16
18
 
@@ -25,9 +27,10 @@
25
27
 
26
28
  public Material CurrentMaterial => _materialStack[^1].material;
27
29
 
28
- public IEnumerable<Material> Materials => _materialStack.Select(entry => entry.material);
30
+ public IEnumerable<Material> Materials =>
31
+ _materialStack.Select(entry => entry.material).Reverse();
29
32
 
30
- public IEnumerable<Color> Colors => _colorStack.Select(entry => entry.color);
33
+ public IEnumerable<Color> Colors => _colorStack.Select(entry => entry.color).Reverse();
31
34
 
32
35
  [SiblingComponent]
33
36
  [SerializeField]
@@ -42,7 +45,7 @@
42
45
  return;
43
46
  }
44
47
 
45
- if (!force && !enabled)
48
+ if (!force && !Enabled)
46
49
  {
47
50
  return;
48
51
  }
@@ -64,7 +67,7 @@
64
67
  return;
65
68
  }
66
69
 
67
- if (!force && !enabled)
70
+ if (!force && !Enabled)
68
71
  {
69
72
  return;
70
73
  }
@@ -82,15 +85,17 @@
82
85
 
83
86
  public bool TryGetColor(Component component, out Color color)
84
87
  {
85
- int index = _colorStack.FindIndex(value => value.component == component);
86
- if (index < 0)
88
+ foreach ((Component component, Color color) entry in _colorStack)
87
89
  {
88
- color = default;
89
- return false;
90
+ if (entry.component == component)
91
+ {
92
+ color = entry.color;
93
+ return true;
94
+ }
90
95
  }
91
96
 
92
- color = _colorStack[index].color;
93
- return true;
97
+ color = default;
98
+ return false;
94
99
  }
95
100
 
96
101
  /// <summary>
@@ -98,7 +103,7 @@
98
103
  /// </summary>
99
104
  /// <param name="component">Component that owns the material.</param>
100
105
  /// <param name="material">Material to use.</param>
101
- /// <param name="force">If true, overrides the enable check.</param>
106
+ /// <param name="force">If true, overrides the enabled check.</param>
102
107
  /// <returns>The instanced material, if possible.</returns>
103
108
  public Material PushMaterial(Component component, Material material, bool force = false)
104
109
  {
@@ -107,7 +112,7 @@
107
112
  return null;
108
113
  }
109
114
 
110
- if (!force && !enabled)
115
+ if (!force && !Enabled)
111
116
  {
112
117
  return null;
113
118
  }
@@ -135,7 +140,7 @@
135
140
  /// </summary>
136
141
  /// <param name="component">Component that owns the material.</param>
137
142
  /// <param name="material">Material to use.</param>
138
- /// <param name="force">If true, overrides the enable check.</param>
143
+ /// <param name="force">If true, overrides the enabled check.</param>
139
144
  /// <returns>The instanced material, if possible.</returns>
140
145
  public Material PushBackMaterial(Component component, Material material, bool force = false)
141
146
  {
@@ -144,7 +149,7 @@
144
149
  return null;
145
150
  }
146
151
 
147
- if (!force && !enabled)
152
+ if (!force && !Enabled)
148
153
  {
149
154
  return null;
150
155
  }
@@ -186,14 +191,17 @@
186
191
 
187
192
  public bool TryGetMaterial(Component component, out Material material)
188
193
  {
189
- int index = _materialStack.FindIndex(value => value.component == component);
190
- if (index < 0)
194
+ foreach ((Component component, Material material) entry in _materialStack)
191
195
  {
192
- material = default;
193
- return false;
196
+ if (entry.component == component)
197
+ {
198
+ material = entry.material;
199
+ return true;
200
+ }
194
201
  }
195
- material = _materialStack[index].material;
196
- return true;
202
+
203
+ material = default;
204
+ return false;
197
205
  }
198
206
 
199
207
  private void Awake()
@@ -204,9 +212,15 @@
204
212
  }
205
213
 
206
214
  InternalPushColor(this, _spriteRenderer.color);
207
- _colorStackCache.AddRange(_colorStack);
215
+ foreach ((Component component, Color color) entry in _colorStack)
216
+ {
217
+ _colorStackCache.Add(entry);
218
+ }
208
219
  _ = InternalPushMaterial(this, _spriteRenderer.material);
209
- _materialStackCache.AddRange(_materialStack);
220
+ foreach ((Component component, Material material) entry in _materialStack)
221
+ {
222
+ _materialStackCache.Add(entry);
223
+ }
210
224
  }
211
225
 
212
226
  private void OnEnable()
@@ -219,13 +233,20 @@
219
233
  }
220
234
 
221
235
  _colorStack.Clear();
222
- _colorStack.Add(_colorStackCache[0]);
236
+ if (0 < _colorStackCache.Count)
237
+ {
238
+ _colorStack.Add(_colorStackCache[0]);
239
+ }
240
+
223
241
  List<(Component component, Color color)> colorBuffer = Buffers<(
224
242
  Component component,
225
243
  Color color
226
244
  )>.List;
227
245
  colorBuffer.Clear();
228
- colorBuffer.AddRange(_colorStackCache);
246
+ foreach ((Component component, Color color) entry in _colorStackCache)
247
+ {
248
+ colorBuffer.Add(entry);
249
+ }
229
250
  for (int i = 1; i < colorBuffer.Count; ++i)
230
251
  {
231
252
  (Component component, Color color) entry = colorBuffer[i];
@@ -233,13 +254,20 @@
233
254
  }
234
255
 
235
256
  _materialStack.Clear();
236
- _materialStack.Add(_materialStackCache[0]);
257
+ if (0 < _materialStackCache.Count)
258
+ {
259
+ _materialStack.Add(_materialStackCache[0]);
260
+ }
261
+
237
262
  List<(Component component, Material material)> materialBuffer = Buffers<(
238
263
  Component component,
239
264
  Material material
240
265
  )>.List;
241
266
  materialBuffer.Clear();
242
- materialBuffer.AddRange(_materialStackCache);
267
+ foreach ((Component component, Material material) entry in _materialStackCache)
268
+ {
269
+ materialBuffer.Add(entry);
270
+ }
243
271
  for (int i = 1; i < materialBuffer.Count; ++i)
244
272
  {
245
273
  (Component component, Material material) entry = materialBuffer[i];
@@ -254,21 +282,30 @@
254
282
  Color color
255
283
  )>.List;
256
284
  colorBuffer.Clear();
257
- colorBuffer.AddRange(_colorStack);
285
+ foreach ((Component component, Color color) entry in _colorStack)
286
+ {
287
+ colorBuffer.Add(entry);
288
+ }
258
289
  for (int i = colorBuffer.Count - 1; 1 <= i; --i)
259
290
  {
260
291
  PopColor(colorBuffer[i].component);
261
292
  }
262
293
 
263
294
  _colorStackCache.Clear();
264
- _colorStackCache.AddRange(colorBuffer);
295
+ foreach ((Component component, Color color) entry in colorBuffer)
296
+ {
297
+ _colorStackCache.Add(entry);
298
+ }
265
299
 
266
300
  List<(Component component, Material material)> materialBuffer = Buffers<(
267
301
  Component component,
268
302
  Material material
269
303
  )>.List;
270
304
  materialBuffer.Clear();
271
- materialBuffer.AddRange(_materialStack);
305
+ foreach ((Component component, Material material) entry in _materialStack)
306
+ {
307
+ materialBuffer.Add(entry);
308
+ }
272
309
 
273
310
  for (int i = materialBuffer.Count - 1; 1 <= i; --i)
274
311
  {
@@ -276,7 +313,10 @@
276
313
  }
277
314
 
278
315
  _materialStackCache.Clear();
279
- _materialStackCache.AddRange(materialBuffer);
316
+ foreach ((Component component, Material material) entry in materialBuffer)
317
+ {
318
+ _materialStackCache.Add(entry);
319
+ }
280
320
  }
281
321
 
282
322
  private void RemoveColor(Component component)
@@ -286,12 +326,23 @@
286
326
  return;
287
327
  }
288
328
 
289
- _ = _colorStack.RemoveAll(existingComponent =>
290
- existingComponent.component == component || existingComponent.component == null
291
- );
292
- _ = _colorStackCache.RemoveAll(existingComponent =>
293
- existingComponent.component == component || existingComponent.component == null
294
- );
329
+ for (int i = _colorStack.Count - 1; 0 <= i; --i)
330
+ {
331
+ (Component component, Color color) stackEntry = _colorStack[i];
332
+ if (stackEntry.component == component || stackEntry.component == null)
333
+ {
334
+ _colorStack.RemoveAt(i);
335
+ }
336
+ }
337
+
338
+ for (int i = _colorStackCache.Count - 1; 0 <= i; --i)
339
+ {
340
+ (Component component, Color color) stackEntry = _colorStackCache[i];
341
+ if (stackEntry.component == component || stackEntry.component == null)
342
+ {
343
+ _colorStackCache.RemoveAt(i);
344
+ }
345
+ }
295
346
  }
296
347
 
297
348
  private void RemoveMaterial(Component component)
@@ -301,12 +352,23 @@
301
352
  return;
302
353
  }
303
354
 
304
- _ = _materialStack.RemoveAll(existingComponent =>
305
- existingComponent.component == component || existingComponent.component == null
306
- );
307
- _ = _materialStackCache.RemoveAll(existingComponent =>
308
- existingComponent.component == component || existingComponent.component == null
309
- );
355
+ for (int i = _materialStack.Count - 1; 0 <= i; --i)
356
+ {
357
+ (Component component, Material material) stackEntry = _materialStack[i];
358
+ if (stackEntry.component == component || stackEntry.component == null)
359
+ {
360
+ _materialStack.RemoveAt(i);
361
+ }
362
+ }
363
+
364
+ for (int i = _materialStackCache.Count - 1; 0 <= i; --i)
365
+ {
366
+ (Component component, Material material) stackEntry = _materialStackCache[i];
367
+ if (stackEntry.component == component || stackEntry.component == null)
368
+ {
369
+ _materialStackCache.RemoveAt(i);
370
+ }
371
+ }
310
372
  }
311
373
  }
312
374
  }
@@ -1,9 +1,9 @@
1
- namespace UnityHelpers.Tests.Tests.Runtime.Helper
1
+ namespace UnityHelpers.Tests.Helper
2
2
  {
3
3
  using System.Linq;
4
- using Core.Helper;
5
- using Core.Random;
6
4
  using NUnit.Framework;
5
+ using UnityHelpers.Core.Helper;
6
+ using UnityHelpers.Core.Random;
7
7
 
8
8
  public sealed class ArrayConverterTests
9
9
  {
@@ -0,0 +1,129 @@
1
+ namespace UnityHelpers.Tests.Helper
2
+ {
3
+ using NUnit.Framework;
4
+ using UnityHelpers.Core.Helper;
5
+ using UnityHelpers.Core.Random;
6
+
7
+ public sealed class FormattingHelperTests
8
+ {
9
+ private const int NumTries = 1_000;
10
+
11
+ [Test]
12
+ public void FormatNegative()
13
+ {
14
+ for (int i = 0; i < NumTries; ++i)
15
+ {
16
+ long bytes = PRNG.Instance.NextLong(long.MinValue, 0);
17
+ const string expected = "0 B";
18
+ string found = FormattingHelpers.FormatBytes(bytes);
19
+ Assert.AreEqual(expected, found, $"{bytes} failed to convert");
20
+ }
21
+ }
22
+
23
+ [Test]
24
+ public void FormatZeroBytes()
25
+ {
26
+ long bytes = 0L;
27
+ const string expected = "0 B";
28
+ string found = FormattingHelpers.FormatBytes(bytes);
29
+ Assert.AreEqual(expected, found);
30
+ }
31
+
32
+ [Test]
33
+ public void FormatBytes()
34
+ {
35
+ for (int i = 0; i < NumTries; ++i)
36
+ {
37
+ long bytes = PRNG.Instance.NextLong(1024L);
38
+ string expected = $"{bytes} B";
39
+ string found = FormattingHelpers.FormatBytes(bytes);
40
+ Assert.AreEqual(expected, found);
41
+ }
42
+ }
43
+
44
+ [Test]
45
+ public void FormatKiloBytes()
46
+ {
47
+ for (int i = 0; i < NumTries; ++i)
48
+ {
49
+ long bytes = PRNG.Instance.NextLong(1024L, 1024L * 1024L);
50
+ string expected = $"{(bytes / 1024.0):0.##} KB";
51
+ string found = FormattingHelpers.FormatBytes(bytes);
52
+ Assert.AreEqual(expected, found);
53
+ }
54
+ }
55
+
56
+ [Test]
57
+ public void FormatMegaBytes()
58
+ {
59
+ for (int i = 0; i < NumTries; ++i)
60
+ {
61
+ long bytes = PRNG.Instance.NextLong(1024L * 1024L, 1024L * 1024L * 1024L);
62
+ string expected = $"{(bytes / 1024.0 / 1024.0):0.##} MB";
63
+ string found = FormattingHelpers.FormatBytes(bytes);
64
+ Assert.AreEqual(expected, found);
65
+ }
66
+ }
67
+
68
+ [Test]
69
+ public void FormatGigaBytes()
70
+ {
71
+ for (int i = 0; i < NumTries; ++i)
72
+ {
73
+ long bytes = PRNG.Instance.NextLong(
74
+ 1024L * 1024L * 1024L,
75
+ 1024L * 1024L * 1024L * 1024L
76
+ );
77
+ string expected = $"{(bytes / 1024.0 / 1024.0 / 1024.0):0.##} GB";
78
+ string found = FormattingHelpers.FormatBytes(bytes);
79
+ Assert.AreEqual(expected, found);
80
+ }
81
+ }
82
+
83
+ [Test]
84
+ public void FormatTeraBytes()
85
+ {
86
+ for (int i = 0; i < NumTries; ++i)
87
+ {
88
+ long bytes = PRNG.Instance.NextLong(
89
+ 1024L * 1024L * 1024L * 1024L,
90
+ 1024L * 1024L * 1024L * 1024L * 1024L
91
+ );
92
+ string expected = $"{(bytes / 1024.0 / 1024.0 / 1024.0 / 1024.0):0.##} TB";
93
+ string found = FormattingHelpers.FormatBytes(bytes);
94
+ Assert.AreEqual(expected, found);
95
+ }
96
+ }
97
+
98
+ [Test]
99
+ public void FormatPetaBytes()
100
+ {
101
+ for (int i = 0; i < NumTries; ++i)
102
+ {
103
+ long bytes = PRNG.Instance.NextLong(
104
+ 1024L * 1024L * 1024L * 1024L * 1024L,
105
+ 1024L * 1024L * 1024L * 1024L * 1024L * 1024L
106
+ );
107
+ string expected = $"{(bytes / 1024.0 / 1024.0 / 1024.0 / 1024.0 / 1024.0):0.##} PB";
108
+ string found = FormattingHelpers.FormatBytes(bytes);
109
+ Assert.AreEqual(expected, found);
110
+ }
111
+ }
112
+
113
+ [Test]
114
+ public void FormatExaBytes()
115
+ {
116
+ for (int i = 0; i < NumTries; ++i)
117
+ {
118
+ long bytes = PRNG.Instance.NextLong(
119
+ 1024L * 1024L * 1024L * 1024L * 1024L * 1024L,
120
+ long.MaxValue
121
+ );
122
+ string expected =
123
+ $"{(bytes / 1024.0 / 1024.0 / 1024.0 / 1024.0 / 1024.0 / 1024.0):0.##} EB";
124
+ string found = FormattingHelpers.FormatBytes(bytes);
125
+ Assert.AreEqual(expected, found);
126
+ }
127
+ }
128
+ }
129
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 67b3a1cde9694bcb8a7bfbdb9bd477f6
3
+ timeCreated: 1742758560
@@ -1,11 +1,11 @@
1
- namespace UnityHelpers.Tests.Tests.Runtime.Helper
1
+ namespace UnityHelpers.Tests.Helper
2
2
  {
3
3
  using System.Collections;
4
- using Core.Helper;
5
4
  using JetBrains.Annotations;
6
5
  using NUnit.Framework;
7
6
  using UnityEngine;
8
7
  using UnityEngine.TestTools;
8
+ using UnityHelpers.Core.Helper;
9
9
 
10
10
  [UsedImplicitly]
11
11
  public sealed class ObjectHelperComponent : MonoBehaviour { }