com.wallstop-studios.unity-helpers 1.0.0-rc4

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 (238) hide show
  1. package/.gitattributes +63 -0
  2. package/CHANGELOG.md +0 -0
  3. package/CHANGELOG.md.meta +7 -0
  4. package/Editor/AnimationCreator.cs +218 -0
  5. package/Editor/AnimationCreator.cs.meta +11 -0
  6. package/Editor/AnimationEventEditor.cs +742 -0
  7. package/Editor/AnimationEventEditor.cs.meta +11 -0
  8. package/Editor/PrefabCheckWizard.cs +140 -0
  9. package/Editor/PrefabCheckWizard.cs.meta +11 -0
  10. package/Editor/WallstopStudios.UnityHelpers.Editor.asmdef +16 -0
  11. package/Editor/WallstopStudios.UnityHelpers.Editor.asmdef.meta +7 -0
  12. package/Editor.meta +8 -0
  13. package/LICENSE +21 -0
  14. package/LICENSE.md +7 -0
  15. package/LICENSE.md.meta +7 -0
  16. package/LICENSE.meta +7 -0
  17. package/README.md +2 -0
  18. package/README.md.meta +7 -0
  19. package/Runtime/Core/Attributes/AnimationEventAttribute.cs +102 -0
  20. package/Runtime/Core/Attributes/AnimationEventAttribute.cs.meta +11 -0
  21. package/Runtime/Core/Attributes/AutomaticallyFindAttribute.cs +43 -0
  22. package/Runtime/Core/Attributes/AutomaticallyFindAttribute.cs.meta +11 -0
  23. package/Runtime/Core/Attributes/ChildComponentAttribute.cs +85 -0
  24. package/Runtime/Core/Attributes/ChildComponentAttribute.cs.meta +11 -0
  25. package/Runtime/Core/Attributes/KSerializableAttribute.cs +23 -0
  26. package/Runtime/Core/Attributes/KSerializableAttribute.cs.meta +11 -0
  27. package/Runtime/Core/Attributes/NotNullAttribute.cs +33 -0
  28. package/Runtime/Core/Attributes/NotNullAttribute.cs.meta +11 -0
  29. package/Runtime/Core/Attributes/ParentComponent.cs +85 -0
  30. package/Runtime/Core/Attributes/ParentComponent.cs.meta +11 -0
  31. package/Runtime/Core/Attributes/ReadOnlyAttribute.cs +8 -0
  32. package/Runtime/Core/Attributes/ReadOnlyAttribute.cs.meta +11 -0
  33. package/Runtime/Core/Attributes/RelationalComponentExtensions.cs +14 -0
  34. package/Runtime/Core/Attributes/RelationalComponentExtensions.cs.meta +11 -0
  35. package/Runtime/Core/Attributes/SiblingComponentAttribute.cs +89 -0
  36. package/Runtime/Core/Attributes/SiblingComponentAttribute.cs.meta +11 -0
  37. package/Runtime/Core/Attributes/ValidateAssignmentAttribute.cs +66 -0
  38. package/Runtime/Core/Attributes/ValidateAssignmentAttribute.cs.meta +11 -0
  39. package/Runtime/Core/Attributes.meta +8 -0
  40. package/Runtime/Core/DataStructure/Adapters/FastVector2Int.cs +91 -0
  41. package/Runtime/Core/DataStructure/Adapters/FastVector2Int.cs.meta +11 -0
  42. package/Runtime/Core/DataStructure/Adapters/FastVector3Int.cs +180 -0
  43. package/Runtime/Core/DataStructure/Adapters/FastVector3Int.cs.meta +11 -0
  44. package/Runtime/Core/DataStructure/Adapters/KGuid.cs +274 -0
  45. package/Runtime/Core/DataStructure/Adapters/KGuid.cs.meta +11 -0
  46. package/Runtime/Core/DataStructure/Adapters/KVector2.cs +79 -0
  47. package/Runtime/Core/DataStructure/Adapters/KVector2.cs.meta +11 -0
  48. package/Runtime/Core/DataStructure/Adapters.meta +8 -0
  49. package/Runtime/Core/DataStructure/Circle.cs +50 -0
  50. package/Runtime/Core/DataStructure/Circle.cs.meta +11 -0
  51. package/Runtime/Core/DataStructure/CyclicBuffer.cs +130 -0
  52. package/Runtime/Core/DataStructure/CyclicBuffer.cs.meta +11 -0
  53. package/Runtime/Core/DataStructure/ISpatialTree.cs +58 -0
  54. package/Runtime/Core/DataStructure/ISpatialTree.cs.meta +11 -0
  55. package/Runtime/Core/DataStructure/KDTree.cs +186 -0
  56. package/Runtime/Core/DataStructure/KDTree.cs.meta +11 -0
  57. package/Runtime/Core/DataStructure/QuadTree.cs +184 -0
  58. package/Runtime/Core/DataStructure/QuadTree.cs.meta +11 -0
  59. package/Runtime/Core/DataStructure/RTree.cs +247 -0
  60. package/Runtime/Core/DataStructure/RTree.cs.meta +11 -0
  61. package/Runtime/Core/DataStructure/StringWrapper.cs +90 -0
  62. package/Runtime/Core/DataStructure/StringWrapper.cs.meta +11 -0
  63. package/Runtime/Core/DataStructure/TimedCache.cs +50 -0
  64. package/Runtime/Core/DataStructure/TimedCache.cs.meta +11 -0
  65. package/Runtime/Core/DataStructure.meta +8 -0
  66. package/Runtime/Core/Extension/AnimatorExtensions.cs +25 -0
  67. package/Runtime/Core/Extension/AnimatorExtensions.cs.meta +11 -0
  68. package/Runtime/Core/Extension/CircleExtensions.cs +25 -0
  69. package/Runtime/Core/Extension/CircleExtensions.cs.meta +11 -0
  70. package/Runtime/Core/Extension/ColorExtensions.cs +72 -0
  71. package/Runtime/Core/Extension/ColorExtensions.cs.meta +11 -0
  72. package/Runtime/Core/Extension/DictionaryExtensions.cs +173 -0
  73. package/Runtime/Core/Extension/DictionaryExtensions.cs.meta +11 -0
  74. package/Runtime/Core/Extension/DirectionExtensions.cs +210 -0
  75. package/Runtime/Core/Extension/DirectionExtensions.cs.meta +11 -0
  76. package/Runtime/Core/Extension/HashSetExtensions.cs +12 -0
  77. package/Runtime/Core/Extension/HashSetExtensions.cs.meta +11 -0
  78. package/Runtime/Core/Extension/IEnumerableExtensions.cs +109 -0
  79. package/Runtime/Core/Extension/IEnumerableExtensions.cs.meta +11 -0
  80. package/Runtime/Core/Extension/IListExtensions.cs +49 -0
  81. package/Runtime/Core/Extension/IListExtensions.cs.meta +11 -0
  82. package/Runtime/Core/Extension/LoggingExtensions.cs +196 -0
  83. package/Runtime/Core/Extension/LoggingExtensions.cs.meta +11 -0
  84. package/Runtime/Core/Extension/RandomExtensions.cs +110 -0
  85. package/Runtime/Core/Extension/RandomExtensions.cs.meta +11 -0
  86. package/Runtime/Core/Extension/StringExtensions.cs +76 -0
  87. package/Runtime/Core/Extension/StringExtensions.cs.meta +11 -0
  88. package/Runtime/Core/Extension/UnityExtensions.cs +1409 -0
  89. package/Runtime/Core/Extension/UnityExtensions.cs.meta +11 -0
  90. package/Runtime/Core/Extension.meta +8 -0
  91. package/Runtime/Core/Helper/AssignUtilities.cs +14 -0
  92. package/Runtime/Core/Helper/AssignUtilities.cs.meta +11 -0
  93. package/Runtime/Core/Helper/Enumerables.cs +17 -0
  94. package/Runtime/Core/Helper/Enumerables.cs.meta +11 -0
  95. package/Runtime/Core/Helper/Geometry.cs +26 -0
  96. package/Runtime/Core/Helper/Geometry.cs.meta +11 -0
  97. package/Runtime/Core/Helper/Helpers.cs +1092 -0
  98. package/Runtime/Core/Helper/Helpers.cs.meta +11 -0
  99. package/Runtime/Core/Helper/IterationHelpers.cs +32 -0
  100. package/Runtime/Core/Helper/IterationHelpers.cs.meta +11 -0
  101. package/Runtime/Core/Helper/LifetimeHelpers.cs +12 -0
  102. package/Runtime/Core/Helper/LifetimeHelpers.cs.meta +11 -0
  103. package/Runtime/Core/Helper/Objects.cs +447 -0
  104. package/Runtime/Core/Helper/Objects.cs.meta +11 -0
  105. package/Runtime/Core/Helper/SpriteHelpers.cs +53 -0
  106. package/Runtime/Core/Helper/SpriteHelpers.cs.meta +11 -0
  107. package/Runtime/Core/Helper/StringInList.cs +31 -0
  108. package/Runtime/Core/Helper/StringInList.cs.meta +11 -0
  109. package/Runtime/Core/Helper/WallMath.cs +75 -0
  110. package/Runtime/Core/Helper/WallMath.cs.meta +11 -0
  111. package/Runtime/Core/Helper.meta +8 -0
  112. package/Runtime/Core/Math/Line.cs +51 -0
  113. package/Runtime/Core/Math/Line.cs.meta +11 -0
  114. package/Runtime/Core/Math/Parabola.cs +44 -0
  115. package/Runtime/Core/Math/Parabola.cs.meta +11 -0
  116. package/Runtime/Core/Math/PointPolygonCheck.cs +25 -0
  117. package/Runtime/Core/Math/PointPolygonCheck.cs.meta +11 -0
  118. package/Runtime/Core/Math/Range.cs +56 -0
  119. package/Runtime/Core/Math/Range.cs.meta +11 -0
  120. package/Runtime/Core/Math/XXHash.cs +308 -0
  121. package/Runtime/Core/Math/XXHash.cs.meta +11 -0
  122. package/Runtime/Core/Math.meta +8 -0
  123. package/Runtime/Core/Model/Direction.cs +26 -0
  124. package/Runtime/Core/Model/Direction.cs.meta +11 -0
  125. package/Runtime/Core/Model.meta +8 -0
  126. package/Runtime/Core/OneOf/FastOneOf.cs +145 -0
  127. package/Runtime/Core/OneOf/FastOneOf.cs.meta +11 -0
  128. package/Runtime/Core/OneOf/None.cs +6 -0
  129. package/Runtime/Core/OneOf/None.cs.meta +11 -0
  130. package/Runtime/Core/OneOf.meta +8 -0
  131. package/Runtime/Core/Random/AbstractRandom.cs +537 -0
  132. package/Runtime/Core/Random/AbstractRandom.cs.meta +11 -0
  133. package/Runtime/Core/Random/IRandom.cs +141 -0
  134. package/Runtime/Core/Random/IRandom.cs.meta +11 -0
  135. package/Runtime/Core/Random/NativePcgRandom.cs +97 -0
  136. package/Runtime/Core/Random/NativePcgRandom.cs.meta +11 -0
  137. package/Runtime/Core/Random/PcgRandom.cs +142 -0
  138. package/Runtime/Core/Random/PcgRandom.cs.meta +11 -0
  139. package/Runtime/Core/Random/RandomState.cs +92 -0
  140. package/Runtime/Core/Random/RandomState.cs.meta +11 -0
  141. package/Runtime/Core/Random/RandomUtilities.cs +26 -0
  142. package/Runtime/Core/Random/RandomUtilities.cs.meta +11 -0
  143. package/Runtime/Core/Random/SquirrelRandom.cs +82 -0
  144. package/Runtime/Core/Random/SquirrelRandom.cs.meta +11 -0
  145. package/Runtime/Core/Random/SystemRandom.cs +110 -0
  146. package/Runtime/Core/Random/SystemRandom.cs.meta +11 -0
  147. package/Runtime/Core/Random/ThreadLocalRandom.cs +11 -0
  148. package/Runtime/Core/Random/ThreadLocalRandom.cs.meta +11 -0
  149. package/Runtime/Core/Random/UnityRandom.cs +24 -0
  150. package/Runtime/Core/Random/UnityRandom.cs.meta +11 -0
  151. package/Runtime/Core/Random/XorShiftRandom.cs +45 -0
  152. package/Runtime/Core/Random/XorShiftRandom.cs.meta +11 -0
  153. package/Runtime/Core/Random.meta +8 -0
  154. package/Runtime/Core/Serialization/JsonConverters/Vector2Converter.cs +32 -0
  155. package/Runtime/Core/Serialization/JsonConverters/Vector2Converter.cs.meta +11 -0
  156. package/Runtime/Core/Serialization/JsonConverters/Vector3Converter.cs +32 -0
  157. package/Runtime/Core/Serialization/JsonConverters/Vector3Converter.cs.meta +11 -0
  158. package/Runtime/Core/Serialization/JsonConverters.meta +8 -0
  159. package/Runtime/Core/Serialization/Serializer.cs +125 -0
  160. package/Runtime/Core/Serialization/Serializer.cs.meta +11 -0
  161. package/Runtime/Core/Serialization.meta +8 -0
  162. package/Runtime/Core/Threading/SingleThreadedThreadPool.cs +102 -0
  163. package/Runtime/Core/Threading/SingleThreadedThreadPool.cs.meta +11 -0
  164. package/Runtime/Core/Threading.meta +8 -0
  165. package/Runtime/Core.meta +8 -0
  166. package/Runtime/Protobuf-Net/System.Buffers.dll +0 -0
  167. package/Runtime/Protobuf-Net/System.Buffers.dll.meta +33 -0
  168. package/Runtime/Protobuf-Net/System.Collections.Immutable.dll +0 -0
  169. package/Runtime/Protobuf-Net/System.Collections.Immutable.dll.meta +33 -0
  170. package/Runtime/Protobuf-Net/System.Numerics.Vectors.dll +0 -0
  171. package/Runtime/Protobuf-Net/System.Numerics.Vectors.dll.meta +33 -0
  172. package/Runtime/Protobuf-Net/System.Runtime.CompilerServices.Unsafe.dll +0 -0
  173. package/Runtime/Protobuf-Net/System.Runtime.CompilerServices.Unsafe.dll.meta +33 -0
  174. package/Runtime/Protobuf-Net/protobuf-net.Core.dll +0 -0
  175. package/Runtime/Protobuf-Net/protobuf-net.Core.dll.meta +33 -0
  176. package/Runtime/Protobuf-Net/protobuf-net.dll +0 -0
  177. package/Runtime/Protobuf-Net/protobuf-net.dll.meta +33 -0
  178. package/Runtime/Protobuf-Net.meta +8 -0
  179. package/Runtime/Utils/AnimationEventEqualityComparer.cs +149 -0
  180. package/Runtime/Utils/AnimationEventEqualityComparer.cs.meta +11 -0
  181. package/Runtime/Utils/AnimatorEnumStateMachine.cs +80 -0
  182. package/Runtime/Utils/AnimatorEnumStateMachine.cs.meta +11 -0
  183. package/Runtime/Utils/Buffers.cs +32 -0
  184. package/Runtime/Utils/Buffers.cs.meta +11 -0
  185. package/Runtime/Utils/CircleLineRenderer.cs +122 -0
  186. package/Runtime/Utils/CircleLineRenderer.cs.meta +11 -0
  187. package/Runtime/Utils/Oscillator.cs +25 -0
  188. package/Runtime/Utils/Oscillator.cs.meta +11 -0
  189. package/Runtime/Utils/SetTextureImportData.cs +67 -0
  190. package/Runtime/Utils/SetTextureImportData.cs.meta +11 -0
  191. package/Runtime/Utils.meta +8 -0
  192. package/Runtime/WallstopStudios.UnityHelpers.asmdef +14 -0
  193. package/Runtime/WallstopStudios.UnityHelpers.asmdef.meta +7 -0
  194. package/Runtime.meta +8 -0
  195. package/Tests/Editor/WallstopStudios.UnityHelpers.Tests.Editor.asmdef +17 -0
  196. package/Tests/Editor/WallstopStudios.UnityHelpers.Tests.Editor.asmdef.meta +7 -0
  197. package/Tests/Editor.meta +8 -0
  198. package/Tests/Runtime/DataStructures/BalancedKDTreeTests.cs +14 -0
  199. package/Tests/Runtime/DataStructures/BalancedKDTreeTests.cs.meta +11 -0
  200. package/Tests/Runtime/DataStructures/QuadTreeTests.cs +14 -0
  201. package/Tests/Runtime/DataStructures/QuadTreeTests.cs.meta +11 -0
  202. package/Tests/Runtime/DataStructures/SpatialTreeTests.cs +106 -0
  203. package/Tests/Runtime/DataStructures/SpatialTreeTests.cs.meta +11 -0
  204. package/Tests/Runtime/DataStructures/UnbalancedKDTreeTests.cs +14 -0
  205. package/Tests/Runtime/DataStructures/UnbalancedKDTreeTests.cs.meta +11 -0
  206. package/Tests/Runtime/DataStructures.meta +8 -0
  207. package/Tests/Runtime/Performance/KDTreePerformanceTests.cs +14 -0
  208. package/Tests/Runtime/Performance/KDTreePerformanceTests.cs.meta +11 -0
  209. package/Tests/Runtime/Performance/QuadTreePerformanceTests.cs +14 -0
  210. package/Tests/Runtime/Performance/QuadTreePerformanceTests.cs.meta +11 -0
  211. package/Tests/Runtime/Performance/RandomPerformanceTests.cs +76 -0
  212. package/Tests/Runtime/Performance/RandomPerformanceTests.cs.meta +11 -0
  213. package/Tests/Runtime/Performance/SpatialTreePerformanceTest.cs +141 -0
  214. package/Tests/Runtime/Performance/SpatialTreePerformanceTest.cs.meta +11 -0
  215. package/Tests/Runtime/Performance/UnbalancedKDTreeTests.cs +14 -0
  216. package/Tests/Runtime/Performance/UnbalancedKDTreeTests.cs.meta +11 -0
  217. package/Tests/Runtime/Performance.meta +8 -0
  218. package/Tests/Runtime/Random/PcgRandomTests.cs +9 -0
  219. package/Tests/Runtime/Random/PcgRandomTests.cs.meta +11 -0
  220. package/Tests/Runtime/Random/RandomTestBase.cs +117 -0
  221. package/Tests/Runtime/Random/RandomTestBase.cs.meta +11 -0
  222. package/Tests/Runtime/Random/SquirrelRandomTests.cs +9 -0
  223. package/Tests/Runtime/Random/SquirrelRandomTests.cs.meta +11 -0
  224. package/Tests/Runtime/Random/SystemRandomTests.cs +10 -0
  225. package/Tests/Runtime/Random/SystemRandomTests.cs.meta +11 -0
  226. package/Tests/Runtime/Random/UnityRandomTests.cs +9 -0
  227. package/Tests/Runtime/Random/UnityRandomTests.cs.meta +11 -0
  228. package/Tests/Runtime/Random/XorShiftRandomTests.cs +9 -0
  229. package/Tests/Runtime/Random/XorShiftRandomTests.cs.meta +11 -0
  230. package/Tests/Runtime/Random.meta +8 -0
  231. package/Tests/Runtime/WallstopStudios.UnityHelpers.Tests.Runtime.asmdef +22 -0
  232. package/Tests/Runtime/WallstopStudios.UnityHelpers.Tests.Runtime.asmdef.meta +7 -0
  233. package/Tests/Runtime.meta +8 -0
  234. package/Tests.meta +8 -0
  235. package/Third Party Notices.md +1 -0
  236. package/Third Party Notices.md.meta +7 -0
  237. package/package.json +35 -0
  238. package/package.json.meta +7 -0
@@ -0,0 +1,97 @@
1
+ namespace UnityHelpers.Core.Random
2
+ {
3
+ using System;
4
+
5
+ public struct NativePcgRandom
6
+ {
7
+ private const uint HalfwayUint = uint.MaxValue / 2;
8
+ private const double MagicDouble = 4.6566128752458E-10;
9
+ private const float MagicFloat = 5.960465E-008F;
10
+
11
+ private readonly ulong _increment;
12
+ private ulong _state;
13
+
14
+ public NativePcgRandom(Guid seed)
15
+ {
16
+ byte[] guidArray = seed.ToByteArray();
17
+ _state = BitConverter.ToUInt64(guidArray, 0);
18
+ _increment = BitConverter.ToUInt64(guidArray, sizeof(ulong));
19
+ }
20
+
21
+ public NativePcgRandom(int seed)
22
+ {
23
+ _increment = 6554638469UL;
24
+ _state = unchecked((ulong)seed);
25
+ _increment = NextUlong();
26
+ }
27
+
28
+ public uint NextUint()
29
+ {
30
+ unchecked
31
+ {
32
+ ulong oldState = _state;
33
+ _state = oldState * 6364136223846793005UL + _increment;
34
+ uint xorShifted = (uint)(((oldState >> 18) ^ oldState) >> 27);
35
+ int rot = (int)(oldState >> 59);
36
+ return (xorShifted >> rot) | (xorShifted << (-rot & 31));
37
+ }
38
+ }
39
+
40
+ public int Next(int max)
41
+ {
42
+ if (max <= 0)
43
+ {
44
+ throw new ArgumentException($"Max {max} cannot be less-than or equal-to 0");
45
+ }
46
+
47
+ return unchecked((int)NextUint(unchecked((uint)max)));
48
+ }
49
+
50
+ public uint NextUint(uint max)
51
+ {
52
+ /*
53
+ https://github.com/libevent/libevent/blob/3807a30b03ab42f2f503f2db62b1ef5876e2be80/arc4random.c#L531
54
+
55
+ http://cs.stackexchange.com/questions/570/generating-uniformly-distributed-random-numbers-using-a-coin
56
+ Generates a uniform random number within the bound, avoiding modulo bias
57
+ */
58
+ uint threshold = unchecked((uint)((0x100000000UL - max) % max));
59
+ while (true)
60
+ {
61
+ uint randomValue = NextUint();
62
+ if (threshold <= randomValue)
63
+ {
64
+ return randomValue % max;
65
+ }
66
+ }
67
+ }
68
+
69
+ public long NextLong()
70
+ {
71
+ uint upper = NextUint();
72
+ uint lower = NextUint();
73
+ // Mix things up a little
74
+ if (NextBool())
75
+ {
76
+ return unchecked((long)((ulong)upper << 32) | lower);
77
+ }
78
+ return unchecked((long)((ulong)lower << 32) | upper);
79
+ }
80
+
81
+ public ulong NextUlong()
82
+ {
83
+ return unchecked((ulong)NextLong());
84
+ }
85
+
86
+ public bool NextBool()
87
+ {
88
+ return NextUint() < HalfwayUint;
89
+ }
90
+
91
+ public float NextFloat()
92
+ {
93
+ uint floatAsInt = NextUint();
94
+ return (floatAsInt >> 8) * MagicFloat;
95
+ }
96
+ }
97
+ }
@@ -0,0 +1,11 @@
1
+ fileFormatVersion: 2
2
+ guid: 738be60c0f0a5ad428b38b79c266f408
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
@@ -0,0 +1,142 @@
1
+ namespace UnityHelpers.Core.Random
2
+ {
3
+ using System;
4
+ using System.Runtime.Serialization;
5
+
6
+ /// <summary>
7
+ /// Implementation based off of the reference PCG Random, found here: https://www.pcg-random.org/index.html
8
+ /// </summary>
9
+ [Serializable]
10
+ [DataContract]
11
+ public sealed class PcgRandom : AbstractRandom, IEquatable<PcgRandom>, IComparable, IComparable<PcgRandom>
12
+ {
13
+ public static IRandom Instance => ThreadLocalRandom<PcgRandom>.Instance;
14
+
15
+ [DataMember(Name = "Increment")]
16
+ internal readonly ulong _increment;
17
+ [DataMember(Name = "State")]
18
+ internal ulong _state;
19
+
20
+ public PcgRandom() : this(Guid.NewGuid())
21
+ {
22
+ }
23
+
24
+ public PcgRandom(Guid guid)
25
+ {
26
+ byte[] guidArray = guid.ToByteArray();
27
+ _state = BitConverter.ToUInt64(guidArray, 0);
28
+ _increment = BitConverter.ToUInt64(guidArray, sizeof(ulong));
29
+ }
30
+
31
+ public PcgRandom(RandomState randomState)
32
+ {
33
+ _state = randomState.State1;
34
+ _increment = randomState.State2;
35
+ _cachedGaussian = randomState.Gaussian;
36
+ }
37
+
38
+ public PcgRandom(ulong increment, ulong state)
39
+ {
40
+ _increment = increment;
41
+ _state = state;
42
+ }
43
+
44
+ public PcgRandom(long seed)
45
+ {
46
+ // Start with a nice prime
47
+ _increment = 6554638469UL;
48
+ _state = unchecked((ulong) seed);
49
+ _increment = NextUlong();
50
+ }
51
+
52
+ public override RandomState InternalState => new (_state, _increment, _cachedGaussian);
53
+
54
+ public override uint NextUint()
55
+ {
56
+ unchecked
57
+ {
58
+ ulong oldState = _state;
59
+ _state = oldState * 6364136223846793005UL + _increment;
60
+ uint xorShifted = (uint)(((oldState >> 18) ^ oldState) >> 27);
61
+ int rot = (int)(oldState >> 59);
62
+ return (xorShifted >> rot) | (xorShifted << (-rot & 31));
63
+ }
64
+ }
65
+
66
+ public bool Equals(PcgRandom other)
67
+ {
68
+ if (ReferenceEquals(other, null))
69
+ {
70
+ return false;
71
+ }
72
+
73
+ // ReSharper disable once CompareOfFloatsByEqualityOperator
74
+ return _increment == other._increment && _state == other._state && _cachedGaussian == other._cachedGaussian;
75
+ }
76
+
77
+ public int CompareTo(PcgRandom other)
78
+ {
79
+ if (ReferenceEquals(other, null))
80
+ {
81
+ return -1;
82
+ }
83
+
84
+ if (_increment == other._increment)
85
+ {
86
+ if (_state == other._state)
87
+ {
88
+ return 0;
89
+ }
90
+ if (_state < other._state)
91
+ {
92
+ return -1;
93
+ }
94
+
95
+ return 1;
96
+ }
97
+
98
+ if (_increment < other._increment)
99
+ {
100
+ return -1;
101
+ }
102
+
103
+ if (_cachedGaussian.HasValue != other._cachedGaussian.HasValue)
104
+ {
105
+ return _cachedGaussian.HasValue ? -1 : 1;
106
+ }
107
+
108
+ if (!_cachedGaussian.HasValue)
109
+ {
110
+ return 0;
111
+ }
112
+
113
+ // ReSharper disable once PossibleInvalidOperationException
114
+ return _cachedGaussian.Value.CompareTo(other._cachedGaussian.Value);
115
+ }
116
+
117
+ public override bool Equals(object other)
118
+ {
119
+ return Equals(other as PcgRandom);
120
+ }
121
+
122
+ public override int GetHashCode()
123
+ {
124
+ return _increment.GetHashCode();
125
+ }
126
+
127
+ public override string ToString()
128
+ {
129
+ return $"{{\"Increment\": {_increment}, \"State\": {_state}}}";
130
+ }
131
+
132
+ public int CompareTo(object other)
133
+ {
134
+ return CompareTo(other as PcgRandom);
135
+ }
136
+
137
+ public override IRandom Copy()
138
+ {
139
+ return new PcgRandom(InternalState);
140
+ }
141
+ }
142
+ }
@@ -0,0 +1,11 @@
1
+ fileFormatVersion: 2
2
+ guid: 5206bd9e6b6b7a24cb5a88faba0e4777
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
@@ -0,0 +1,92 @@
1
+ namespace UnityHelpers.Core.Random
2
+ {
3
+ using System;
4
+ using Extension;
5
+ using Helper;
6
+ using ProtoBuf;
7
+
8
+ [Serializable]
9
+ [ProtoContract]
10
+ public struct RandomState : IEquatable<RandomState>
11
+ {
12
+ public ulong State1 => _state1;
13
+ public ulong State2 => _state2;
14
+
15
+ public double? Gaussian
16
+ {
17
+ get
18
+ {
19
+ if (_hasGaussian)
20
+ {
21
+ return _gaussian;
22
+ }
23
+
24
+ return null;
25
+ }
26
+ }
27
+
28
+ [ProtoMember(1)]
29
+ private ulong _state1;
30
+ [ProtoMember(2)]
31
+ private ulong _state2;
32
+
33
+ [ProtoMember(3)]
34
+ private bool _hasGaussian;
35
+
36
+ [ProtoMember(4)]
37
+ private double _gaussian;
38
+
39
+ private int _hashCode;
40
+
41
+ public RandomState(ulong state1, ulong state2 = 0, double? gaussian = null)
42
+ {
43
+ _state1 = state1;
44
+ _state2 = state2;
45
+ _hasGaussian = gaussian.HasValue;
46
+ _gaussian = gaussian ?? 0;
47
+ _hashCode = Objects.ValueTypeHashCode(state1, state2, _hasGaussian, _gaussian);
48
+ }
49
+
50
+ public RandomState(Guid guid)
51
+ {
52
+ byte[] guidBytes = guid.ToByteArray();
53
+ _state1 = BitConverter.ToUInt64(guidBytes, 0);
54
+ _state2 = BitConverter.ToUInt64(guidBytes, sizeof(ulong));
55
+ _hasGaussian = false;
56
+ _gaussian = 0;
57
+ _hashCode = Objects.ValueTypeHashCode(_state1, _state2, _hasGaussian, _gaussian);
58
+ }
59
+
60
+ [ProtoAfterDeserialization]
61
+ private void OnProtoDeserialize()
62
+ {
63
+ _hashCode = Objects.ValueTypeHashCode(_state1, _state2, _hasGaussian, _gaussian);
64
+ }
65
+
66
+ public override bool Equals(object other)
67
+ {
68
+ return other is RandomState randomState && Equals(randomState);
69
+ }
70
+
71
+ public bool Equals(RandomState other)
72
+ {
73
+ // ReSharper disable once CompareOfFloatsByEqualityOperator
74
+ return _state1 == other._state1 && _state2 == other._state2 && _hasGaussian == other._hasGaussian && (!_hasGaussian || _gaussian == other._gaussian);
75
+ }
76
+
77
+ public override int GetHashCode()
78
+ {
79
+ if (_hashCode == 0)
80
+ {
81
+ return _hashCode = Objects.ValueTypeHashCode(_state1, _state2, _hasGaussian, _gaussian);
82
+ }
83
+
84
+ return _hashCode;
85
+ }
86
+
87
+ public override string ToString()
88
+ {
89
+ return this.ToJson();
90
+ }
91
+ }
92
+ }
@@ -0,0 +1,11 @@
1
+ fileFormatVersion: 2
2
+ guid: 41d80559b2118924ba94e768d005a4e6
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
@@ -0,0 +1,26 @@
1
+ namespace UnityHelpers.Core.Random
2
+ {
3
+ using UnityEngine;
4
+
5
+ public static class RandomUtilities
6
+ {
7
+ public static float GetRandomVariance(this IRandom random, float baseValue, float variance)
8
+ {
9
+ if (variance < 0.0f)
10
+ {
11
+ Debug.LogError("Variance cannot be negative");
12
+ return baseValue;
13
+ }
14
+
15
+ if (variance == 0.0f)
16
+ {
17
+ return baseValue;
18
+ }
19
+
20
+ float higher = variance / 2;
21
+ float lower = -higher;
22
+
23
+ return baseValue + random.NextFloat(lower, higher);
24
+ }
25
+ }
26
+ }
@@ -0,0 +1,11 @@
1
+ fileFormatVersion: 2
2
+ guid: 93d61d247b2c43638c798cd1cb955b0f
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
@@ -0,0 +1,82 @@
1
+ namespace UnityHelpers.Core.Random
2
+ {
3
+ using System;
4
+
5
+ // https://youtu.be/LWFzPP8ZbdU?t=2673
6
+ public sealed class SquirrelRandom : AbstractRandom
7
+ {
8
+ private const uint BitNoise1 = 0xB5297A4D;
9
+ private const uint BitNoise2 = 0x68E31DA4;
10
+ private const uint BitNoise3 = 0x1B56C4E9;
11
+ private const int LargePrime = 198491317;
12
+
13
+ public static readonly SquirrelRandom Instance = new();
14
+
15
+ private uint _position;
16
+
17
+ public SquirrelRandom() : this(Guid.NewGuid().GetHashCode())
18
+ {
19
+
20
+ }
21
+
22
+ public SquirrelRandom(int seed)
23
+ {
24
+ _position = unchecked((uint)seed);
25
+ }
26
+
27
+ public SquirrelRandom(RandomState randomState)
28
+ {
29
+ _position = unchecked((uint)randomState.State1);
30
+ _cachedGaussian = randomState.Gaussian;
31
+ }
32
+
33
+ public override RandomState InternalState => new(_position, gaussian: _cachedGaussian);
34
+
35
+ public override uint NextUint()
36
+ {
37
+ return _position = NextUintInternal(_position);
38
+ }
39
+
40
+ // Does not advance the RNG
41
+ public float NextNoise(int x, int y)
42
+ {
43
+ return NextNoise(x, y, _position);
44
+ }
45
+
46
+ public override IRandom Copy()
47
+ {
48
+ return new SquirrelRandom(InternalState);
49
+ }
50
+
51
+ private static uint NextUintInternal(uint seed)
52
+ {
53
+ uint result = seed;
54
+ result *= BitNoise1;
55
+ result ^= (result >> 8);
56
+ result += BitNoise2;
57
+ result ^= (result << 8);
58
+ result *= BitNoise3;
59
+ result ^= (result >> 8);
60
+ return result;
61
+ }
62
+
63
+ // https://youtu.be/LWFzPP8ZbdU?t=2906
64
+ private static float NextNoise(int x, uint seed)
65
+ {
66
+ uint result = unchecked((uint)x);
67
+ result *= BitNoise1;
68
+ result += seed;
69
+ result ^= (result >> 8);
70
+ result += BitNoise2;
71
+ result ^= (result << 8);
72
+ result *= BitNoise3;
73
+ result ^= (result >> 8);
74
+ return (result >> 8) * MagicFloat;
75
+ }
76
+
77
+ private static float NextNoise(int x, int y, uint seed)
78
+ {
79
+ return NextNoise(x + (LargePrime * y), seed);
80
+ }
81
+ }
82
+ }
@@ -0,0 +1,11 @@
1
+ fileFormatVersion: 2
2
+ guid: 3f2a41ab545b08e4b9bd2a8025bf839a
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
@@ -0,0 +1,110 @@
1
+ namespace UnityHelpers.Core.Random
2
+ {
3
+ using System;
4
+
5
+ /// <summary>
6
+ /// Implementation dependent upon .Net's Random class.
7
+ /// </summary>
8
+ public sealed class SystemRandom : AbstractRandom
9
+ {
10
+ /*
11
+ Copied from Random.cs source. Apparently it isn't guaranteed to be the
12
+ same across platforms and we depend on that.
13
+ */
14
+ private int inext;
15
+ private int inextp;
16
+ private readonly int[] SeedArray = new int[56];
17
+
18
+ public static IRandom Instance => ThreadLocalRandom<SystemRandom>.Instance;
19
+
20
+ public SystemRandom() : this (Environment.TickCount)
21
+ {
22
+ }
23
+
24
+ public SystemRandom(int seed)
25
+ {
26
+ int num1 = 161803398 - (seed == int.MinValue ? int.MaxValue : Math.Abs(seed));
27
+ this.SeedArray[55] = num1;
28
+ int num2 = 1;
29
+ for (int index1 = 1; index1 < 55; ++index1)
30
+ {
31
+ int index2 = 21 * index1 % 55;
32
+ this.SeedArray[index2] = num2;
33
+ num2 = num1 - num2;
34
+ if (num2 < 0)
35
+ num2 += int.MaxValue;
36
+ num1 = this.SeedArray[index2];
37
+ }
38
+ for (int index3 = 1; index3 < 5; ++index3)
39
+ {
40
+ for (int index4 = 1; index4 < 56; ++index4)
41
+ {
42
+ this.SeedArray[index4] -= this.SeedArray[1 + (index4 + 30) % 55];
43
+ if (this.SeedArray[index4] < 0)
44
+ this.SeedArray[index4] += int.MaxValue;
45
+ }
46
+ }
47
+ this.inext = 0;
48
+ this.inextp = 21;
49
+ }
50
+
51
+ public SystemRandom(RandomState randomState)
52
+ {
53
+ inext = unchecked((int)randomState.State1);
54
+ inextp = unchecked((int)randomState.State2);
55
+ _cachedGaussian = randomState.Gaussian;
56
+ }
57
+
58
+ public override RandomState InternalState => new(unchecked((ulong)inext), unchecked((ulong)inextp), _cachedGaussian);
59
+
60
+ public override uint NextUint()
61
+ {
62
+ int inext = this.inext;
63
+ int inextp = this.inextp;
64
+ int index1;
65
+ if ((index1 = inext + 1) >= 56)
66
+ index1 = 1;
67
+ int index2;
68
+ if ((index2 = inextp + 1) >= 56)
69
+ index2 = 1;
70
+ int num = this.SeedArray[index1] - this.SeedArray[index2];
71
+ if (num == int.MaxValue)
72
+ --num;
73
+ if (num < 0)
74
+ num += int.MaxValue;
75
+ this.SeedArray[index1] = num;
76
+ this.inext = index1;
77
+ this.inextp = index2;
78
+ return unchecked((uint) num);
79
+ }
80
+
81
+ public override double NextDouble()
82
+ {
83
+ double generated;
84
+ do
85
+ {
86
+ generated = unchecked((int)NextUint()) * 4.6566128752458E-10;
87
+ }
88
+ while (generated < 0 || 1 <= generated);
89
+
90
+ return generated;
91
+ }
92
+
93
+ public override float NextFloat()
94
+ {
95
+ return (float) NextDouble();
96
+ }
97
+
98
+ public override IRandom Copy()
99
+ {
100
+ SystemRandom copy = new(InternalState);
101
+
102
+ for (int i = 0; i < SeedArray.Length; ++i)
103
+ {
104
+ copy.SeedArray[i] = SeedArray[i];
105
+ }
106
+
107
+ return copy;
108
+ }
109
+ }
110
+ }
@@ -0,0 +1,11 @@
1
+ fileFormatVersion: 2
2
+ guid: d00769758e05b2f478ea85ce53650d44
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
@@ -0,0 +1,11 @@
1
+ namespace UnityHelpers.Core.Random
2
+ {
3
+ using System.Threading;
4
+
5
+ public static class ThreadLocalRandom<T> where T : IRandom, new()
6
+ {
7
+ private static readonly ThreadLocal<T> RandomCache = new ThreadLocal<T>(() => new T());
8
+
9
+ public static T Instance => RandomCache.Value;
10
+ }
11
+ }
@@ -0,0 +1,11 @@
1
+ fileFormatVersion: 2
2
+ guid: 0d9f5b15009b2ff48ba3d66fd8a7fc53
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
@@ -0,0 +1,24 @@
1
+ namespace UnityHelpers.Core.Random
2
+ {
3
+ using System;
4
+
5
+ public sealed class UnityRandom : AbstractRandom
6
+ {
7
+ public static readonly UnityRandom Instance = new UnityRandom();
8
+
9
+ private UnityRandom()
10
+ {
11
+ }
12
+
13
+ public override RandomState InternalState => throw new NotSupportedException("Unity Random does not expose its internal state");
14
+ public override uint NextUint()
15
+ {
16
+ return unchecked((uint)UnityEngine.Random.Range(int.MinValue, int.MaxValue));
17
+ }
18
+
19
+ public override IRandom Copy()
20
+ {
21
+ throw new NotSupportedException("Unity Random does not support copying / seeding");
22
+ }
23
+ }
24
+ }
@@ -0,0 +1,11 @@
1
+ fileFormatVersion: 2
2
+ guid: 8588ccb12c40bf24da31067ccfd067f9
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant: