using System.Runtime.CompilerServices; using System.Text; namespace GameKit.Dependencies.Utilities { /// /// Various utility classes relating to floats. /// public static class Bytes { /// /// Used to encode and decode strings. /// private static readonly UTF8Encoding _encoding = new(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true); /// /// Pads an index a specified value. Preferred over typical padding so that pad values used with skins can be easily found in the code. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string Pad(this byte value, int padding) => Ints.PadInt(value, padding); /// /// Provides a random inclusive int within a given range. Preferred over Unity's Random to eliminate confusion as Unity uses inclusive for floats max, and exclusive for int max. /// /// Inclusive minimum value. /// Inclusive maximum value. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte RandomInclusiveRange(byte minimum, byte maximum) => (byte)Ints.RandomInclusiveRange(minimum, maximum); /// /// Provides a random exclusive int within a given range. Preferred over Unity's Random to eliminate confusion as Unity uses inclusive for floats max, and exclusive for int max. /// /// Inclusive minimum value. /// Exclusive maximum value. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte RandomExclusiveRange(byte minimum, byte maximum) => (byte)Ints.RandomExclusiveRange(minimum, maximum); /// /// Returns a clamped int within a specified range. /// /// Value to clamp. /// Minimum value. /// Maximum value. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte Clamp(byte value, byte minimum, byte maximum) => (byte)Ints.Clamp(value, minimum, maximum); /// /// Returns whichever value is lower. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte Min(byte a, byte b) => (a < b) ? a : b; /// /// Determins if all values passed in are the same. /// /// Values to check. /// True if all values are the same. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool ValuesMatch(params byte[] values) => Ints.ValuesMatch((int[])(object)values); /// /// Converts bytes to a string without error checking. /// public static string ToString(this byte[] bytes, int offset, int count) => _encoding.GetString(bytes, offset, count); } }