com.wallstop-studios.unity-helpers 2.0.0-rc44 → 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.
@@ -4,19 +4,19 @@
4
4
 
5
5
  public static class FormattingHelpers
6
6
  {
7
+ private static readonly string[] ByteSizes = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
8
+
7
9
  public static string FormatBytes(long bytes)
8
10
  {
9
- string[] sizes = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
11
+ bytes = Math.Max(0L, bytes);
10
12
  double len = bytes;
11
13
  int order = 0;
12
14
 
13
- bytes = Math.Max(0, bytes);
14
-
15
15
  const int byteInChunk = 1024;
16
16
  while (byteInChunk <= len)
17
17
  {
18
18
  len /= byteInChunk;
19
- if (order < sizes.Length - 1)
19
+ if (order < ByteSizes.Length - 1)
20
20
  {
21
21
  ++order;
22
22
  }
@@ -26,7 +26,7 @@
26
26
  }
27
27
  }
28
28
 
29
- return $"{len:0.##} {sizes[order]}";
29
+ return $"{len:0.##} {ByteSizes[order]}";
30
30
  }
31
31
  }
32
32
  }
@@ -7,8 +7,8 @@
7
7
 
8
8
  public static class Objects
9
9
  {
10
- private const int HashBase = 839;
11
- private const int HashMultiplier = 4021;
10
+ private const int HashBase = 5556137;
11
+ private const int HashMultiplier = 95785853;
12
12
 
13
13
  public static T FromWeakReference<T>(WeakReference weakReference)
14
14
  where T : class
@@ -20,6 +20,7 @@
20
20
  public static Array CreateArray(Type type, int length)
21
21
  {
22
22
  return ArrayCreators
23
+ // ReSharper disable once ConvertClosureToMethodGroup
23
24
  .GetOrAdd(type, elementType => GetArrayCreator(elementType))
24
25
  .Invoke(length);
25
26
  }
@@ -28,6 +29,7 @@
28
29
  public static IList CreateList(Type elementType, int length)
29
30
  {
30
31
  return ListWithCapacityCreators
32
+ // ReSharper disable once ConvertClosureToMethodGroup
31
33
  .GetOrAdd(elementType, type => GetListWithCapacityCreator(type))
32
34
  .Invoke(length);
33
35
  }
@@ -35,6 +37,7 @@
35
37
  [MethodImpl(MethodImplOptions.AggressiveInlining)]
36
38
  public static IList CreateList(Type elementType)
37
39
  {
40
+ // ReSharper disable once ConvertClosureToMethodGroup
38
41
  return ListCreators.GetOrAdd(elementType, type => GetListCreator(type)).Invoke();
39
42
  }
40
43
 
@@ -44,7 +47,7 @@
44
47
  return field.GetValue;
45
48
  #else
46
49
  DynamicMethod dynamicMethod = new(
47
- $"Get{(field.DeclaringType?.Name ?? string.Empty)}{field.Name}",
50
+ $"Get{field.DeclaringType.Name}{field.Name}",
48
51
  typeof(object),
49
52
  new[] { typeof(object) },
50
53
  field.DeclaringType,
@@ -72,6 +75,41 @@
72
75
  #endif
73
76
  }
74
77
 
78
+ public static Func<object> GetStaticFieldGetter(FieldInfo field)
79
+ {
80
+ if (!field.IsStatic)
81
+ {
82
+ throw new ArgumentException(nameof(field));
83
+ }
84
+
85
+ #if WEB_GL
86
+ return () => field.GetValue(null);
87
+ #else
88
+ DynamicMethod dynamicMethod = new(
89
+ $"Get{field.DeclaringType.Name}{field.Name}",
90
+ typeof(object),
91
+ Type.EmptyTypes, // No parameters for static fields
92
+ field.DeclaringType,
93
+ true
94
+ );
95
+
96
+ ILGenerator il = dynamicMethod.GetILGenerator();
97
+
98
+ // Load the static field
99
+ il.Emit(OpCodes.Ldsfld, field);
100
+
101
+ // If the field's type is a value type, box it.
102
+ if (field.FieldType.IsValueType)
103
+ {
104
+ il.Emit(OpCodes.Box, field.FieldType);
105
+ }
106
+
107
+ il.Emit(OpCodes.Ret);
108
+
109
+ return (Func<object>)dynamicMethod.CreateDelegate(typeof(Func<object>));
110
+ #endif
111
+ }
112
+
75
113
  public static Func<TInstance, TValue> GetFieldGetter<TInstance, TValue>(FieldInfo field)
76
114
  {
77
115
  #if WEB_GL
@@ -142,6 +180,59 @@
142
180
  #endif
143
181
  }
144
182
 
183
+ public static Func<TValue> GetStaticFieldGetter<TValue>(FieldInfo field)
184
+ {
185
+ if (!field.IsStatic)
186
+ {
187
+ throw new ArgumentException(nameof(field));
188
+ }
189
+
190
+ #if WEB_GL
191
+ return Getter;
192
+ TValue Getter()
193
+ {
194
+ return (TValue)field.GetValue(null);
195
+ }
196
+ #else
197
+ DynamicMethod dynamicMethod = new(
198
+ $"GetGenericStatic{field.DeclaringType.Name}{field.Name}",
199
+ typeof(TValue),
200
+ Type.EmptyTypes, // no parameters needed for static fields
201
+ field.DeclaringType,
202
+ true
203
+ );
204
+
205
+ ILGenerator il = dynamicMethod.GetILGenerator();
206
+
207
+ // Load the static field.
208
+ il.Emit(OpCodes.Ldsfld, field);
209
+
210
+ // Handle conversion from the field type to TValue.
211
+ if (field.FieldType.IsValueType)
212
+ {
213
+ if (!typeof(TValue).IsValueType)
214
+ {
215
+ il.Emit(OpCodes.Box, field.FieldType);
216
+ }
217
+ }
218
+ else
219
+ {
220
+ if (typeof(TValue).IsValueType)
221
+ {
222
+ il.Emit(OpCodes.Unbox_Any, typeof(TValue));
223
+ }
224
+ else if (typeof(TValue) != field.FieldType)
225
+ {
226
+ il.Emit(OpCodes.Castclass, typeof(TValue));
227
+ }
228
+ }
229
+
230
+ il.Emit(OpCodes.Ret);
231
+
232
+ return (Func<TValue>)dynamicMethod.CreateDelegate(typeof(Func<TValue>));
233
+ #endif
234
+ }
235
+
145
236
  public static FieldSetter<TInstance, TValue> GetFieldSetter<TInstance, TValue>(
146
237
  FieldInfo field
147
238
  )
@@ -184,6 +275,36 @@
184
275
  #endif
185
276
  }
186
277
 
278
+ public static Action<TValue> GetStaticFieldSetter<TValue>(FieldInfo field)
279
+ {
280
+ if (!field.IsStatic)
281
+ {
282
+ throw new ArgumentException(nameof(field));
283
+ }
284
+ #if WEB_GL
285
+ return Setter;
286
+ void Setter(TValue newValue)
287
+ {
288
+ field.SetValue(null, newValue);
289
+ }
290
+ #else
291
+ DynamicMethod dynamicMethod = new(
292
+ $"SetFieldGenericStatic{field.DeclaringType.Name}{field.Name}",
293
+ typeof(void),
294
+ new[] { typeof(TValue) },
295
+ field.Module,
296
+ true
297
+ );
298
+
299
+ ILGenerator il = dynamicMethod.GetILGenerator();
300
+ il.Emit(OpCodes.Ldarg_0);
301
+ il.Emit(OpCodes.Stsfld, field);
302
+ il.Emit(OpCodes.Ret);
303
+
304
+ return (Action<TValue>)dynamicMethod.CreateDelegate(typeof(Action<TValue>));
305
+ #endif
306
+ }
307
+
187
308
  public static Action<object, object> GetFieldSetter(FieldInfo field)
188
309
  {
189
310
  #if WEB_GL
@@ -217,6 +338,40 @@
217
338
  #endif
218
339
  }
219
340
 
341
+ public static Action<object> GetStaticFieldSetter(FieldInfo field)
342
+ {
343
+ if (!field.IsStatic)
344
+ {
345
+ throw new ArgumentException(nameof(field));
346
+ }
347
+ #if WEB_GL
348
+ return value => field.SetValue(null, value);
349
+ #else
350
+ DynamicMethod dynamicMethod = new(
351
+ $"SetFieldStatic{field.DeclaringType.Name}{field.Name}",
352
+ null,
353
+ new[] { typeof(object) },
354
+ field.DeclaringType.Module,
355
+ true
356
+ );
357
+
358
+ ILGenerator il = dynamicMethod.GetILGenerator();
359
+
360
+ // Load the new value (argument 0)
361
+ il.Emit(OpCodes.Ldarg_0);
362
+ // Convert the object to the field's type (unbox or cast as needed)
363
+ il.Emit(
364
+ field.FieldType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass,
365
+ field.FieldType
366
+ );
367
+ // Set the static field
368
+ il.Emit(OpCodes.Stsfld, field);
369
+ il.Emit(OpCodes.Ret);
370
+
371
+ return (Action<object>)dynamicMethod.CreateDelegate(typeof(Action<object>));
372
+ #endif
373
+ }
374
+
220
375
  public static Func<int, Array> GetArrayCreator(Type elementType)
221
376
  {
222
377
  #if WEB_GL
@@ -3,13 +3,12 @@
3
3
  using Extension;
4
4
  using UnityEditor;
5
5
  using UnityEngine;
6
- using Utils;
7
6
 
8
7
  public static class SpriteHelpers
9
8
  {
10
9
  public static void MakeReadable(this Texture2D texture)
11
10
  {
12
- if (texture.isReadable)
11
+ if (texture == null || texture.isReadable)
13
12
  {
14
13
  return;
15
14
  }
@@ -29,58 +28,14 @@
29
28
  return;
30
29
  }
31
30
 
32
- tImporter.isReadable = true;
33
- EditorUtility.SetDirty(tImporter);
34
- tImporter.SaveAndReimport();
35
- EditorUtility.SetDirty(texture);
36
- #endif
37
- }
38
-
39
- public static void SetSpritePivot(string fullSpritePath, Vector2 pivot)
40
- {
41
- #if UNITY_EDITOR
42
- SetSpritePivot(AssetImporter.GetAtPath(fullSpritePath) as TextureImporter, pivot);
43
- #endif
44
- }
45
-
46
- public static void SetSpritePivot(Sprite sprite, Vector2 pivot)
47
- {
48
- #if UNITY_EDITOR
49
- SetSpritePivot(
50
- AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(sprite)) as TextureImporter,
51
- pivot
52
- );
53
- #endif
54
- }
55
-
56
- #if UNITY_EDITOR
57
- public static void SetSpritePivot(TextureImporter textureImporter, Vector2 pivot)
58
- {
59
- if (textureImporter == null)
31
+ if (!tImporter.isReadable)
60
32
  {
61
- return;
33
+ tImporter.isReadable = true;
34
+ EditorUtility.SetDirty(tImporter);
35
+ tImporter.SaveAndReimport();
36
+ EditorUtility.SetDirty(texture);
62
37
  }
63
-
64
- TextureImporterSettings textureImportSettings = new TextureImporterSettings();
65
- textureImporter.ReadTextureSettings(textureImportSettings);
66
- textureImportSettings.spriteAlignment = (int)SpriteAlignment.Custom;
67
- textureImportSettings.wrapMode = TextureWrapMode.Clamp;
68
- textureImportSettings.filterMode = FilterMode.Trilinear;
69
- textureImporter.SetTextureSettings(textureImportSettings);
70
-
71
- TextureImporterPlatformSettings importerSettings = new TextureImporterPlatformSettings
72
- {
73
- resizeAlgorithm = TextureResizeAlgorithm.Bilinear,
74
- maxTextureSize = SetTextureImportData.RegularTextureSize,
75
- textureCompression = TextureImporterCompression.Compressed,
76
- format = TextureImporterFormat.Automatic,
77
- };
78
-
79
- textureImporter.SetPlatformTextureSettings(importerSettings);
80
- textureImporter.isReadable = true;
81
- textureImporter.spritePivot = pivot;
82
- textureImporter.SaveAndReimport();
83
- }
84
38
  #endif
39
+ }
85
40
  }
86
41
  }
@@ -125,12 +125,12 @@
125
125
  if (0 < direction.x)
126
126
  {
127
127
  float t2 = (max.x - center.x) / direction.x;
128
- tMax = Math.Min(tMax, t2);
128
+ tMax = Mathf.Min(tMax, t2);
129
129
  }
130
130
  else
131
131
  {
132
132
  float t1 = (min.x - center.x) / direction.x;
133
- tMax = Math.Min(tMax, t1);
133
+ tMax = Mathf.Min(tMax, t1);
134
134
  }
135
135
  }
136
136
 
@@ -139,12 +139,12 @@
139
139
  if (direction.y > 0)
140
140
  {
141
141
  float t2 = (max.y - center.y) / direction.y;
142
- tMax = Math.Min(tMax, t2);
142
+ tMax = Mathf.Min(tMax, t2);
143
143
  }
144
144
  else
145
145
  {
146
146
  float t1 = (min.y - center.y) / direction.y;
147
- tMax = Math.Min(tMax, t1);
147
+ tMax = Mathf.Min(tMax, t1);
148
148
  }
149
149
  }
150
150
 
@@ -160,7 +160,7 @@
160
160
 
161
161
  public static bool Approximately(this float lhs, float rhs, float tolerance = 0.045f)
162
162
  {
163
- return Math.Abs(lhs - rhs) <= tolerance;
163
+ return Mathf.Abs(lhs - rhs) <= tolerance;
164
164
  }
165
165
  }
166
166
  }
@@ -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 { }
@@ -1,30 +1,31 @@
1
- namespace UnityHelpers.Tests.Tests.Runtime.Helper
1
+ namespace UnityHelpers.Tests.Helper
2
2
  {
3
3
  using System;
4
4
  using System.Collections;
5
5
  using System.Collections.Generic;
6
- using Core.Helper;
6
+ using System.Reflection;
7
7
  using NUnit.Framework;
8
+ using UnityHelpers.Core.Helper;
9
+ using UnityHelpers.Core.Random;
8
10
 
9
11
  public struct TestStruct
10
12
  {
13
+ public static int staticIntValue;
11
14
  public int intValue;
12
15
  }
13
16
 
14
17
  public sealed class TestClass
15
18
  {
19
+ public static int staticIntValue;
16
20
  public int intValue;
17
21
  }
18
22
 
19
23
  public sealed class ReflectionHelperTests
20
24
  {
21
- internal const int NumTries = 1_000;
25
+ private const int NumTries = 1_000;
22
26
 
23
- private readonly Random _random = new();
24
-
25
- // TODO: Test on static fields
26
27
  [Test]
27
- public void GetFieldGetterClass()
28
+ public void GetFieldGetterClassMemberField()
28
29
  {
29
30
  TestClass testClass = new();
30
31
  Func<object, object> classGetter = ReflectionHelpers.GetFieldGetter(
@@ -33,14 +34,59 @@
33
34
  Assert.AreEqual(testClass.intValue, classGetter(testClass));
34
35
  for (int i = 0; i < NumTries; ++i)
35
36
  {
36
- testClass.intValue = _random.Next(int.MinValue, int.MaxValue);
37
+ testClass.intValue = PRNG.Instance.Next(int.MinValue, int.MaxValue);
37
38
  Assert.AreEqual(testClass.intValue, classGetter(testClass));
38
39
  }
39
40
  }
40
41
 
41
- // TODO: Test on static fields
42
42
  [Test]
43
- public void GetFieldGetterStruct()
43
+ public void GetFieldGetterClassStaticField()
44
+ {
45
+ TestClass testClass = new();
46
+ Func<object, object> classGetter = ReflectionHelpers.GetFieldGetter(
47
+ typeof(TestClass).GetField(
48
+ nameof(TestClass.staticIntValue),
49
+ BindingFlags.Static | BindingFlags.Public
50
+ )
51
+ );
52
+ Assert.AreEqual(TestClass.staticIntValue, classGetter(testClass));
53
+ for (int i = 0; i < NumTries; ++i)
54
+ {
55
+ TestClass.staticIntValue = PRNG.Instance.Next(int.MinValue, int.MaxValue);
56
+ Assert.AreEqual(TestClass.staticIntValue, classGetter(testClass));
57
+ }
58
+ }
59
+
60
+ [Test]
61
+ public void GetStaticFieldGetterThrowsOnNonStaticField()
62
+ {
63
+ Assert.Throws<ArgumentException>(
64
+ () =>
65
+ ReflectionHelpers.GetStaticFieldGetter(
66
+ typeof(TestClass).GetField(nameof(TestClass.intValue))
67
+ )
68
+ );
69
+ }
70
+
71
+ [Test]
72
+ public void GetStaticFieldGetterClassStaticField()
73
+ {
74
+ Func<object> classGetter = ReflectionHelpers.GetStaticFieldGetter(
75
+ typeof(TestClass).GetField(
76
+ nameof(TestClass.staticIntValue),
77
+ BindingFlags.Static | BindingFlags.Public
78
+ )
79
+ );
80
+ Assert.AreEqual(TestClass.staticIntValue, classGetter());
81
+ for (int i = 0; i < NumTries; ++i)
82
+ {
83
+ TestClass.staticIntValue = PRNG.Instance.Next(int.MinValue, int.MaxValue);
84
+ Assert.AreEqual(TestClass.staticIntValue, classGetter());
85
+ }
86
+ }
87
+
88
+ [Test]
89
+ public void GetFieldGetterStructMemberField()
44
90
  {
45
91
  TestStruct testStruct = new();
46
92
  Func<object, object> structGetter = ReflectionHelpers.GetFieldGetter(
@@ -49,14 +95,48 @@
49
95
  Assert.AreEqual(testStruct.intValue, structGetter(testStruct));
50
96
  for (int i = 0; i < NumTries; ++i)
51
97
  {
52
- testStruct.intValue = _random.Next(int.MinValue, int.MaxValue);
98
+ testStruct.intValue = PRNG.Instance.Next(int.MinValue, int.MaxValue);
53
99
  Assert.AreEqual(testStruct.intValue, structGetter(testStruct));
54
100
  }
55
101
  }
56
102
 
57
- // TODO: Test on static fields
58
103
  [Test]
59
- public void GetFieldSetterClass()
104
+ public void GetFieldGetterStructStaticField()
105
+ {
106
+ TestStruct testStruct = new();
107
+ Func<object, object> structGetter = ReflectionHelpers.GetFieldGetter(
108
+ typeof(TestStruct).GetField(
109
+ nameof(TestStruct.staticIntValue),
110
+ BindingFlags.Static | BindingFlags.Public
111
+ )
112
+ );
113
+ Assert.AreEqual(TestStruct.staticIntValue, structGetter(testStruct));
114
+ for (int i = 0; i < NumTries; ++i)
115
+ {
116
+ TestStruct.staticIntValue = PRNG.Instance.Next(int.MinValue, int.MaxValue);
117
+ Assert.AreEqual(TestStruct.staticIntValue, structGetter(testStruct));
118
+ }
119
+ }
120
+
121
+ [Test]
122
+ public void GetStaticFieldGetterStructStaticField()
123
+ {
124
+ Func<object> structGetter = ReflectionHelpers.GetStaticFieldGetter(
125
+ typeof(TestStruct).GetField(
126
+ nameof(TestStruct.staticIntValue),
127
+ BindingFlags.Static | BindingFlags.Public
128
+ )
129
+ );
130
+ Assert.AreEqual(TestStruct.staticIntValue, structGetter());
131
+ for (int i = 0; i < NumTries; ++i)
132
+ {
133
+ TestStruct.staticIntValue = PRNG.Instance.Next(int.MinValue, int.MaxValue);
134
+ Assert.AreEqual(TestStruct.staticIntValue, structGetter());
135
+ }
136
+ }
137
+
138
+ [Test]
139
+ public void GetFieldSetterClassMemberField()
60
140
  {
61
141
  TestClass testClass = new();
62
142
  Action<object, object> structSetter = ReflectionHelpers.GetFieldSetter(
@@ -64,15 +144,60 @@
64
144
  );
65
145
  for (int i = 0; i < NumTries; ++i)
66
146
  {
67
- int expected = _random.Next(int.MinValue, int.MaxValue);
147
+ int expected = PRNG.Instance.Next(int.MinValue, int.MaxValue);
68
148
  structSetter(testClass, expected);
69
149
  Assert.AreEqual(expected, testClass.intValue);
70
150
  }
71
151
  }
72
152
 
73
- // TODO: Test on static fields
74
153
  [Test]
75
- public void GetFieldSetterStruct()
154
+ public void GetFieldSetterClassStaticField()
155
+ {
156
+ TestClass testClass = new();
157
+ Action<object, object> structSetter = ReflectionHelpers.GetFieldSetter(
158
+ typeof(TestClass).GetField(
159
+ nameof(TestClass.staticIntValue),
160
+ BindingFlags.Static | BindingFlags.Public
161
+ )
162
+ );
163
+ for (int i = 0; i < NumTries; ++i)
164
+ {
165
+ int expected = PRNG.Instance.Next(int.MinValue, int.MaxValue);
166
+ structSetter(testClass, expected);
167
+ Assert.AreEqual(expected, TestClass.staticIntValue);
168
+ }
169
+ }
170
+
171
+ [Test]
172
+ public void GetStaticFieldSetterThrowsOnNonStaticField()
173
+ {
174
+ Assert.Throws<ArgumentException>(
175
+ () =>
176
+ ReflectionHelpers.GetStaticFieldSetter(
177
+ typeof(TestClass).GetField(nameof(TestClass.intValue))
178
+ )
179
+ );
180
+ }
181
+
182
+ [Test]
183
+ public void GetStaticFieldSetterClassStaticField()
184
+ {
185
+ Action<object> structSetter = ReflectionHelpers.GetStaticFieldSetter(
186
+ typeof(TestClass).GetField(
187
+ nameof(TestClass.staticIntValue),
188
+ BindingFlags.Static | BindingFlags.Public
189
+ )
190
+ );
191
+ for (int i = 0; i < NumTries; ++i)
192
+ {
193
+ int expected = PRNG.Instance.Next(int.MinValue, int.MaxValue);
194
+ structSetter(expected);
195
+ Assert.AreEqual(expected, TestClass.staticIntValue);
196
+ }
197
+ }
198
+
199
+ [Test]
200
+ public void GetFieldSetterStructMemberField()
76
201
  {
77
202
  // Need boxing
78
203
  object testStruct = new TestStruct();
@@ -81,15 +206,50 @@
81
206
  );
82
207
  for (int i = 0; i < NumTries; ++i)
83
208
  {
84
- int expected = _random.Next(int.MinValue, int.MaxValue);
209
+ int expected = PRNG.Instance.Next(int.MinValue, int.MaxValue);
85
210
  structSetter(testStruct, expected);
86
211
  Assert.AreEqual(expected, ((TestStruct)testStruct).intValue);
87
212
  }
88
213
  }
89
214
 
90
- // TODO: Test on static fields
91
215
  [Test]
92
- public void GetFieldSetterClassGeneric()
216
+ public void GetFieldSetterStructStaticField()
217
+ {
218
+ // Need boxing
219
+ object testStruct = new TestStruct();
220
+ Action<object, object> structSetter = ReflectionHelpers.GetFieldSetter(
221
+ typeof(TestStruct).GetField(
222
+ nameof(TestStruct.staticIntValue),
223
+ BindingFlags.Static | BindingFlags.Public
224
+ )
225
+ );
226
+ for (int i = 0; i < NumTries; ++i)
227
+ {
228
+ int expected = PRNG.Instance.Next(int.MinValue, int.MaxValue);
229
+ structSetter(testStruct, expected);
230
+ Assert.AreEqual(expected, TestStruct.staticIntValue);
231
+ }
232
+ }
233
+
234
+ [Test]
235
+ public void GetStaticFieldSetterStructStaticField()
236
+ {
237
+ Action<object> structSetter = ReflectionHelpers.GetStaticFieldSetter(
238
+ typeof(TestStruct).GetField(
239
+ nameof(TestStruct.staticIntValue),
240
+ BindingFlags.Static | BindingFlags.Public
241
+ )
242
+ );
243
+ for (int i = 0; i < NumTries; ++i)
244
+ {
245
+ int expected = PRNG.Instance.Next(int.MinValue, int.MaxValue);
246
+ structSetter(expected);
247
+ Assert.AreEqual(expected, TestStruct.staticIntValue);
248
+ }
249
+ }
250
+
251
+ [Test]
252
+ public void GetFieldSetterClassGenericMemberField()
93
253
  {
94
254
  TestClass testClass = new();
95
255
  FieldSetter<TestClass, int> classSetter = ReflectionHelpers.GetFieldSetter<
@@ -98,15 +258,63 @@
98
258
  >(typeof(TestClass).GetField(nameof(TestClass.intValue)));
99
259
  for (int i = 0; i < NumTries; ++i)
100
260
  {
101
- int expected = _random.Next(int.MinValue, int.MaxValue);
261
+ int expected = PRNG.Instance.Next(int.MinValue, int.MaxValue);
102
262
  classSetter(ref testClass, expected);
103
263
  Assert.AreEqual(expected, testClass.intValue);
104
264
  }
105
265
  }
106
266
 
107
- // TODO: Test on static fields
108
267
  [Test]
109
- public void GetFieldSetterStructGeneric()
268
+ public void GetFieldSetterClassGenericStaticField()
269
+ {
270
+ TestClass testClass = new();
271
+ FieldSetter<TestClass, int> classSetter = ReflectionHelpers.GetFieldSetter<
272
+ TestClass,
273
+ int
274
+ >(
275
+ typeof(TestClass).GetField(
276
+ nameof(TestClass.staticIntValue),
277
+ BindingFlags.Static | BindingFlags.Public
278
+ )
279
+ );
280
+ for (int i = 0; i < NumTries; ++i)
281
+ {
282
+ int expected = PRNG.Instance.Next(int.MinValue, int.MaxValue);
283
+ classSetter(ref testClass, expected);
284
+ Assert.AreEqual(expected, TestClass.staticIntValue);
285
+ }
286
+ }
287
+
288
+ [Test]
289
+ public void GetStaticFieldSetterGenericThrowsOnNonStaticField()
290
+ {
291
+ Assert.Throws<ArgumentException>(
292
+ () =>
293
+ ReflectionHelpers.GetStaticFieldSetter<int>(
294
+ typeof(TestClass).GetField(nameof(TestClass.intValue))
295
+ )
296
+ );
297
+ }
298
+
299
+ [Test]
300
+ public void GetStaticFieldSetterClassGenericStaticField()
301
+ {
302
+ Action<int> classSetter = ReflectionHelpers.GetStaticFieldSetter<int>(
303
+ typeof(TestClass).GetField(
304
+ nameof(TestClass.staticIntValue),
305
+ BindingFlags.Static | BindingFlags.Public
306
+ )
307
+ );
308
+ for (int i = 0; i < NumTries; ++i)
309
+ {
310
+ int expected = PRNG.Instance.Next(int.MinValue, int.MaxValue);
311
+ classSetter(expected);
312
+ Assert.AreEqual(expected, TestClass.staticIntValue);
313
+ }
314
+ }
315
+
316
+ [Test]
317
+ public void GetFieldSetterStructGenericMemberField()
110
318
  {
111
319
  TestStruct testStruct = new();
112
320
  FieldSetter<TestStruct, int> structSetter = ReflectionHelpers.GetFieldSetter<
@@ -115,15 +323,52 @@
115
323
  >(typeof(TestStruct).GetField(nameof(TestStruct.intValue)));
116
324
  for (int i = 0; i < NumTries; ++i)
117
325
  {
118
- int expected = _random.Next(int.MinValue, int.MaxValue);
326
+ int expected = PRNG.Instance.Next(int.MinValue, int.MaxValue);
119
327
  structSetter(ref testStruct, expected);
120
328
  Assert.AreEqual(expected, testStruct.intValue);
121
329
  }
122
330
  }
123
331
 
124
- // TODO: Test on static fields
125
332
  [Test]
126
- public void GetFieldGetterClassGeneric()
333
+ public void GetFieldSetterStructGenericStaticField()
334
+ {
335
+ TestStruct testStruct = new();
336
+ FieldSetter<TestStruct, int> structSetter = ReflectionHelpers.GetFieldSetter<
337
+ TestStruct,
338
+ int
339
+ >(
340
+ typeof(TestStruct).GetField(
341
+ nameof(TestStruct.staticIntValue),
342
+ BindingFlags.Static | BindingFlags.Public
343
+ )
344
+ );
345
+ for (int i = 0; i < NumTries; ++i)
346
+ {
347
+ int expected = PRNG.Instance.Next(int.MinValue, int.MaxValue);
348
+ structSetter(ref testStruct, expected);
349
+ Assert.AreEqual(expected, TestStruct.staticIntValue);
350
+ }
351
+ }
352
+
353
+ [Test]
354
+ public void GetStaticFieldSetterStructGenericStaticField()
355
+ {
356
+ Action<int> structSetter = ReflectionHelpers.GetStaticFieldSetter<int>(
357
+ typeof(TestStruct).GetField(
358
+ nameof(TestStruct.staticIntValue),
359
+ BindingFlags.Static | BindingFlags.Public
360
+ )
361
+ );
362
+ for (int i = 0; i < NumTries; ++i)
363
+ {
364
+ int expected = PRNG.Instance.Next(int.MinValue, int.MaxValue);
365
+ structSetter(expected);
366
+ Assert.AreEqual(expected, TestStruct.staticIntValue);
367
+ }
368
+ }
369
+
370
+ [Test]
371
+ public void GetFieldGetterClassGenericMemberField()
127
372
  {
128
373
  TestClass testClass = new();
129
374
  Func<TestClass, int> classGetter = ReflectionHelpers.GetFieldGetter<TestClass, int>(
@@ -131,14 +376,57 @@
131
376
  );
132
377
  for (int i = 0; i < NumTries; ++i)
133
378
  {
134
- testClass.intValue = _random.Next(int.MinValue, int.MaxValue);
379
+ testClass.intValue = PRNG.Instance.Next(int.MinValue, int.MaxValue);
135
380
  Assert.AreEqual(testClass.intValue, classGetter(testClass));
136
381
  }
137
382
  }
138
383
 
139
- // TODO: Test on static fields
140
384
  [Test]
141
- public void GetFieldGetterStructGeneric()
385
+ public void GetFieldGetterClassGenericStaticField()
386
+ {
387
+ TestClass testClass = new();
388
+ Func<TestClass, int> classGetter = ReflectionHelpers.GetFieldGetter<TestClass, int>(
389
+ typeof(TestClass).GetField(
390
+ nameof(TestClass.staticIntValue),
391
+ BindingFlags.Static | BindingFlags.Public
392
+ )
393
+ );
394
+ for (int i = 0; i < NumTries; ++i)
395
+ {
396
+ TestClass.staticIntValue = PRNG.Instance.Next(int.MinValue, int.MaxValue);
397
+ Assert.AreEqual(TestClass.staticIntValue, classGetter(testClass));
398
+ }
399
+ }
400
+
401
+ [Test]
402
+ public void GetStaticFieldGetterGenericThrowsOnNonStaticField()
403
+ {
404
+ Assert.Throws<ArgumentException>(
405
+ () =>
406
+ ReflectionHelpers.GetStaticFieldGetter<int>(
407
+ typeof(TestClass).GetField(nameof(TestClass.intValue))
408
+ )
409
+ );
410
+ }
411
+
412
+ [Test]
413
+ public void GetStaticFieldGetterClassGenericStaticField()
414
+ {
415
+ Func<int> classGetter = ReflectionHelpers.GetStaticFieldGetter<int>(
416
+ typeof(TestClass).GetField(
417
+ nameof(TestClass.staticIntValue),
418
+ BindingFlags.Static | BindingFlags.Public
419
+ )
420
+ );
421
+ for (int i = 0; i < NumTries; ++i)
422
+ {
423
+ TestClass.staticIntValue = PRNG.Instance.Next(int.MinValue, int.MaxValue);
424
+ Assert.AreEqual(TestClass.staticIntValue, classGetter());
425
+ }
426
+ }
427
+
428
+ [Test]
429
+ public void GetFieldGetterStructGenericMemberField()
142
430
  {
143
431
  TestStruct testStruct = new();
144
432
  Func<TestStruct, int> structSetter = ReflectionHelpers.GetFieldGetter<TestStruct, int>(
@@ -146,17 +434,50 @@
146
434
  );
147
435
  for (int i = 0; i < NumTries; ++i)
148
436
  {
149
- testStruct.intValue = _random.Next(int.MinValue, int.MaxValue);
437
+ testStruct.intValue = PRNG.Instance.Next(int.MinValue, int.MaxValue);
150
438
  Assert.AreEqual(testStruct.intValue, structSetter(testStruct));
151
439
  }
152
440
  }
153
441
 
442
+ [Test]
443
+ public void GetFieldGetterStructGenericStaticField()
444
+ {
445
+ TestStruct testStruct = new();
446
+ Func<TestStruct, int> structSetter = ReflectionHelpers.GetFieldGetter<TestStruct, int>(
447
+ typeof(TestStruct).GetField(
448
+ nameof(TestStruct.staticIntValue),
449
+ BindingFlags.Static | BindingFlags.Public
450
+ )
451
+ );
452
+ for (int i = 0; i < NumTries; ++i)
453
+ {
454
+ TestStruct.staticIntValue = PRNG.Instance.Next(int.MinValue, int.MaxValue);
455
+ Assert.AreEqual(TestStruct.staticIntValue, structSetter(testStruct));
456
+ }
457
+ }
458
+
459
+ [Test]
460
+ public void GetStaticFieldGetterStructGenericStaticField()
461
+ {
462
+ Func<int> structSetter = ReflectionHelpers.GetStaticFieldGetter<int>(
463
+ typeof(TestStruct).GetField(
464
+ nameof(TestStruct.staticIntValue),
465
+ BindingFlags.Static | BindingFlags.Public
466
+ )
467
+ );
468
+ for (int i = 0; i < NumTries; ++i)
469
+ {
470
+ TestStruct.staticIntValue = PRNG.Instance.Next(int.MinValue, int.MaxValue);
471
+ Assert.AreEqual(TestStruct.staticIntValue, structSetter());
472
+ }
473
+ }
474
+
154
475
  [Test]
155
476
  public void ArrayCreator()
156
477
  {
157
478
  for (int i = 0; i < NumTries; ++i)
158
479
  {
159
- int count = _random.Next(1_000);
480
+ int count = PRNG.Instance.Next(1_000);
160
481
  Array created = ReflectionHelpers.CreateArray(typeof(int), count);
161
482
  Assert.AreEqual(count, created.Length);
162
483
  Assert.IsTrue(created is int[]);
@@ -174,11 +495,11 @@
174
495
  Assert.AreEqual(0, created.Count);
175
496
  Assert.IsTrue(created is List<int>);
176
497
  List<int> typedCreated = (List<int>)created;
177
- int count = _random.Next(50);
498
+ int count = PRNG.Instance.Next(50);
178
499
  List<int> expected = new();
179
500
  for (int j = 0; j < count; ++j)
180
501
  {
181
- int element = _random.Next();
502
+ int element = PRNG.Instance.Next();
182
503
  created.Add(element);
183
504
  expected.Add(element);
184
505
  Assert.AreEqual(j + 1, created.Count);
@@ -192,18 +513,18 @@
192
513
  {
193
514
  for (int i = 0; i < NumTries; ++i)
194
515
  {
195
- int capacity = _random.Next(1_000);
516
+ int capacity = PRNG.Instance.Next(1_000);
196
517
  IList created = ReflectionHelpers.CreateList(typeof(int), capacity);
197
518
  Assert.AreEqual(0, created.Count);
198
519
  Assert.IsTrue(created is List<int>);
199
520
  List<int> typedCreated = (List<int>)created;
200
521
  Assert.AreEqual(capacity, typedCreated.Capacity);
201
522
 
202
- int count = _random.Next(50);
523
+ int count = PRNG.Instance.Next(50);
203
524
  List<int> expected = new();
204
525
  for (int j = 0; j < count; ++j)
205
526
  {
206
- int element = _random.Next();
527
+ int element = PRNG.Instance.Next();
207
528
  created.Add(element);
208
529
  expected.Add(element);
209
530
  Assert.AreEqual(j + 1, created.Count);
@@ -1,10 +1,10 @@
1
- namespace UnityHelpers.Tests.Tests.Runtime.Helper
1
+ namespace UnityHelpers.Tests.Helper
2
2
  {
3
- using Core.Extension;
4
- using Core.Helper;
5
- using Core.Random;
6
3
  using NUnit.Framework;
7
4
  using UnityEngine;
5
+ using UnityHelpers.Core.Extension;
6
+ using UnityHelpers.Core.Helper;
7
+ using UnityHelpers.Core.Random;
8
8
 
9
9
  public sealed class WallMathTests
10
10
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.wallstop-studios.unity-helpers",
3
- "version": "2.0.0-rc44",
3
+ "version": "2.0.0-rc45",
4
4
  "displayName": "Unity Helpers",
5
5
  "description": "Various Unity Helper Library",
6
6
  "dependencies": {},