diff --git a/Assets/BetterDataStructures/Runtime/Properties/ReadOnlyReactiveProperty.cs b/Assets/BetterDataStructures/Runtime/Properties/ReadOnlyReactiveProperty.cs index 27c31bc..1655840 100644 --- a/Assets/BetterDataStructures/Runtime/Properties/ReadOnlyReactiveProperty.cs +++ b/Assets/BetterDataStructures/Runtime/Properties/ReadOnlyReactiveProperty.cs @@ -22,6 +22,11 @@ public sealed class ReadOnlyReactiveProperty /// The ReactiveProperty to encapsulate in a read-only manner. public ReadOnlyReactiveProperty(ReactiveProperty source) { + if (source == null) + { + throw new ArgumentException(nameof(source)); + } + _source = source; } @@ -43,4 +48,4 @@ public ReadOnlyReactiveProperty(ReactiveProperty source) /// The callback action to unsubscribe. public void Unsubscribe(Action action) => _source.Unsubscribe(action); } -} +} \ No newline at end of file diff --git a/Assets/BetterDataStructures/Runtime/Ranges/RangeExtensions.cs b/Assets/BetterDataStructures/Runtime/Ranges/RangeExtensions.cs index 5ab484c..e2a8036 100644 --- a/Assets/BetterDataStructures/Runtime/Ranges/RangeExtensions.cs +++ b/Assets/BetterDataStructures/Runtime/Ranges/RangeExtensions.cs @@ -11,99 +11,153 @@ public static class RangeExtensions /// /// Clamps an integer value to the range defined by the Range object. /// - /// The Range object. + /// The Range object. /// The value to clamp. /// The clamped value. - public static int Clamp(this Range range, int value) + public static int Clamp(this Range self, int value) { - return Mathf.Clamp(value, range.Min, range.Max); + if (self == null) + { + Debug.LogException(new ArgumentException(nameof(self))); + return value; + } + + return Mathf.Clamp(value, self.Min, self.Max); } /// /// Clamps a float value to the range defined by the Range object. /// - /// The Range object. + /// The Range object. /// The value to clamp. /// The clamped value. - public static float Clamp(this Range range, float value) + public static float Clamp(this Range self, float value) { - return Mathf.Clamp(value, range.Min, range.Max); + if (self == null) + { + Debug.LogException(new ArgumentException(nameof(self))); + return value; + } + + return Mathf.Clamp(value, self.Min, self.Max); } /// /// Returns a random float value within the range defined by the Range object. /// - /// The Range object. + /// The Range object. /// A random float value within the range. - public static float Random(this Range range) + public static float Random(this Range self) { - return UnityEngine.Random.Range(range.Min, range.Max); + if (self == null) + { + Debug.LogException(new ArgumentException(nameof(self))); + return default; + } + + return UnityEngine.Random.Range(self.Min, self.Max); } /// /// Returns a random integer value within the range defined by the Range object. /// - /// The Range object. + /// The Range object. /// A random integer value within the range. - public static int Random(this Range range) + public static int Random(this Range self) { - return UnityEngine.Random.Range(range.Min, range.Max + 1); // +1 to include the maximum value in the range. + if (self == null) + { + Debug.LogException(new ArgumentException(nameof(self))); + return default; + } + + return UnityEngine.Random.Range(self.Min, self.Max + 1); // +1 to include the maximum value in the range. } /// /// Linearly interpolates between the minimum and maximum values of the range based on the given interpolation parameter. /// - /// The Range object. + /// The Range object. /// The interpolation parameter. /// The interpolated float value. - public static float Lerp(this Range range, float t) + public static float Lerp(this Range self, float t) { - return Mathf.Lerp(range.Min, range.Max, t); + if (self == null) + { + Debug.LogException(new ArgumentException(nameof(self))); + return default; + } + + return Mathf.Lerp(self.Min, self.Max, t); } /// /// Linearly interpolates between the minimum and maximum values of the range based on the given interpolation parameter and rounds the result to the nearest integer. /// - /// The Range object. + /// The Range object. /// The interpolation parameter. /// The interpolated and rounded integer value. - public static int Lerp(this Range range, float t) + public static int Lerp(this Range self, float t) { - return Mathf.RoundToInt(Mathf.Lerp(range.Min, range.Max, t)); + if (self == null) + { + Debug.LogException(new ArgumentException(nameof(self))); + return default; + } + + return Mathf.RoundToInt(Mathf.Lerp(self.Min, self.Max, t)); } /// /// Checks if a float value is contained within the range defined by the Range object. /// - /// The Range object. + /// The Range object. /// The value to check. /// True if the value is within the range; otherwise, false. - public static bool Contains(this Range range, float value) + public static bool Contains(this Range self, float value) { - return value >= range.Min && value <= range.Max; + if (self == null) + { + Debug.LogException(new ArgumentException(nameof(self))); + return default; + } + + return value >= self.Min && value <= self.Max; } /// /// Checks if an integer value is contained within the range defined by the Range object. /// - /// The Range object. + /// The Range object. /// The value to check. /// True if the value is within the range; otherwise, false. - public static bool Contains(this Range range, int value) + public static bool Contains(this Range self, int value) { - return value >= range.Min && value <= range.Max; + if (self == null) + { + Debug.LogException(new ArgumentException(nameof(self))); + return default; + } + + return value >= self.Min && value <= self.Max; } /// /// Clamps a double value to the range defined by the Range object. Uses Math.Clamp if available. /// - /// The Range object. + /// The Range object. /// The value to clamp. /// The clamped value. - public static double Clamp(this Range range, double value) + public static double Clamp(this Range self, double value) { + if (self == null) + { + Debug.LogException(new ArgumentException(nameof(self))); + return default; + } + #if UNITY_2021_3_OR_NEWER - return Math.Clamp(value, range.Min, range.Max); + return Math.Clamp(value, self.Min, self.Max); #else if (value < range.Min) value = range.Min; @@ -116,13 +170,19 @@ public static double Clamp(this Range range, double value) /// /// Clamps a byte value to the range defined by the Range object. Uses Math.Clamp if available. /// - /// The Range object. + /// The Range object. /// The value to clamp. /// The clamped value. - public static byte Clamp(this Range range, byte value) + public static byte Clamp(this Range self, byte value) { + if (self == null) + { + Debug.LogException(new ArgumentException(nameof(self))); + return default; + } + #if UNITY_2021_3_OR_NEWER - return Math.Clamp(value, range.Min, range.Max); + return Math.Clamp(value, self.Min, self.Max); #else if (value < range.Min) value = range.Min; diff --git a/Assets/BetterDataStructures/Runtime/Tree/TreeNode.cs b/Assets/BetterDataStructures/Runtime/Tree/TreeNode.cs index c44d65c..e03163a 100644 --- a/Assets/BetterDataStructures/Runtime/Tree/TreeNode.cs +++ b/Assets/BetterDataStructures/Runtime/Tree/TreeNode.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using UnityEngine; namespace Better.DataStructures.Tree { @@ -13,7 +14,7 @@ public class TreeNode { // Holds the value of the node private protected readonly T _value; - + // List to hold child nodes private readonly List> _children = new List>(); @@ -83,6 +84,12 @@ public TreeNode[] AddChildren(params T[] values) /// true if the node was successfully removed; otherwise, false. public bool RemoveChild(TreeNode node) { + if (node == null) + { + Debug.LogException(new ArgumentException(nameof(node))); + return default; + } + return _children.Remove(node); } @@ -106,4 +113,4 @@ public IEnumerable Flatten() return new[] { Value }.Concat(_children.SelectMany(x => x.Flatten())); } } -} +} \ No newline at end of file diff --git a/Assets/BetterDataStructures/package.json b/Assets/BetterDataStructures/package.json index 1cfcca1..1f69ed3 100644 --- a/Assets/BetterDataStructures/package.json +++ b/Assets/BetterDataStructures/package.json @@ -2,8 +2,11 @@ "name": "com.uurha.betterdatastructures", "displayName": "Better Data Structures", "description": "Collection of useful data structures for Unity", - "version": "0.1.5", + "version": "0.1.6", "unity": "2021.3", + "dependencies": { + "com.tdw.better.internal.core": "0.0.2" + }, "author": { "name": "Better Plugins", "url": "https://github.com/techno-dwarf-works/"