s&box docs

public static abstract sealed class Sandbox.SandboxSystemExtensions

Methods

Showing 122 methods

public static Angles Angles(System.Random self)

Returns the angles of a uniformly random rotation.

public static int AsInt(T value)

public static string Base64Decode(string base64EncodedData)

Convert from a base64 encoded string

public static string Base64Encode(string plainText)

Convert to a base64 encoded string

public static int BitsSet(int i)

Returns the number of bits set in an integer. This us usually used for flags to count the amount of flags set.

public static System.Collections.Generic.IEnumerable`1<Vector3> CatmullRomSpline(System.Collections.Generic.IEnumerable`1<Vector3> points, int interpolation)

public static bool CheckValidationAttributes(System.Reflection.PropertyInfo prop, object obj, string[] errors, string name = null)

Check all `System.ComponentModel.DataAnnotations.ValidationAttribute`s on this property, and get the error messages if there are any.

Parameters

  • prop: The property whose arguments to test.
  • obj: Instance of the object this property is of.
  • errors: If returned false, these will be the error messages to display.
  • name: Override the property name in error messages. Default: null

Returns

Returns true if all checks have passed or there is no attributes to test, false if there were errors.

public static T Clamp(T input, T min, T max)

Clamp a number between two values.

public static System.Collections.Generic.Dictionary`2<TKey,TValue> Clone(System.Collections.Generic.Dictionary`2<TKey,TValue> dict)

public static string CollapseSpacesAndPreserveLines(string str)

Collapse sequences of spaces and tabs into a single space, preserving newlines

public static string CollapseWhiteSpace(string str)

Collapse sequences of whitespace into a single whitespace

public static Color Color(System.Random self)

Returns a random Color

public static string Columnize(string str, int maxLength, bool right = False)

convert "string" into "string " or " string"

Parameters

  • right: Default: False

public static int Distance(string source, string target)

Return the distance between two strings. Useful for ordering strings by similarity

public static int FastHash(string str)

Generate a 32-bit truncated xxhash3 hash from given string.

public static ulong FastHash64(string str)

Generate a 64-bit xxhash3 hash from given string.

public static float FloatDeterministic(System.Random self, int i)

Get a random float (0-1) from a pre-calculated list. This is faster, and if you put the same seed in, it will always return the same number. The downside is that it only has 8192 variations of floats, but that seem like enough for most things.

public static System.Threading.Tasks.Task ForEachTaskAsync(System.Collections.Generic.IEnumerable`1<T> source, System.Func`2<T,System.Threading.Tasks.Task> body, int maxRunning = 8, System.Threading.CancellationToken token = null)

Parameters

  • maxRunning: Default: 8
  • token: Default: null

public static string FormatBytes(T input, bool shortFormat = False)

Given a number, will format as a memory value, ie 10gb, 4mb

Parameters

  • shortFormat: Default: False

public static string FormatWithSuffix(int num)

Change 1 to 1st, 2 to 2nd etc

public static T FromArray(System.Random self, T[] array, T defVal = null)

Returns a random value in an array

Parameters

  • defVal: Default: null

public static long FromBase36(string input)

Decode the Base36 Encoded string into a number

public static T FromList(System.Random self, System.Collections.Generic.List`1<T> array, T defVal = null)

Parameters

  • defVal: Default: null

public static float Gaussian(System.Random self, float mean = 0, float stdDev = 1)

Sample from a Gaussian distribution with a given mean and standard deviation.

Parameters

  • mean: Default: 0
  • stdDev: Default: 1

public static Vector2 Gaussian2D(System.Random self, System.Nullable`1<Vector2> mean = null, System.Nullable`1<Vector2> stdDev = null)

Parameters

  • mean: Default: null
  • stdDev: Default: null

public static Vector3 Gaussian3D(System.Random self, System.Nullable`1<Vector3> mean = null, System.Nullable`1<Vector3> stdDev = null)

Parameters

  • mean: Default: null
  • stdDev: Default: null

public static Vector4 Gaussian4D(System.Random self, System.Nullable`1<Vector4> mean = null, System.Nullable`1<Vector4> stdDev = null)

Parameters

  • mean: Default: null
  • stdDev: Default: null

public static T GetAttributeOfType(System.Enum enumVal)

Gets an attribute on an enum field value

Parameters

  • enumVal: The enum value

Returns

The attribute of type T that exists on the enum value

Example

Built-in examples define the default contract and should be read first.

public static System.Type GetCommonBaseType(System.Collections.Generic.IEnumerable`1<System.Type> types)

public static int GetEpoch(System.DateTime d)

Returns the UNIX time stamp - number of seconds since 1st of January, 1970.

public static string GetFilenameSafe(string input)

Make the passed in string filename safe. This replaces any invalid characters with "_".

public static int GetManagedSize(System.Type t)

Get the managed size of a given type. This matches an IL-level sizeof(t), even if it cannot be determined normally in C#. Note that `sizeof(t) != Marshal.SizeOf(t)` when t is char or bool.

Note

An IL-level `sizeof(t)` will return `sizeof(IntPtr)` for reference types, as it refers to the size on stack or in an object, not the size on heap.

public static TValue GetOrCreate(System.Collections.Generic.IDictionary`2<TKey,TValue> dict, TKey key)

public static T GetPropertyValue(System.Text.Json.Nodes.JsonObject jso, string membername, T defaultvalue)

Get a property value by name, from a JsonObject. Return defaultValue if it's not found.

public static string HtmlEncode(string str)

public static bool IsBasedOnGenericType(System.Type src, System.Type test)

Returns if this type is based on a given generic type.

Parameters

  • src: The type to test.
  • test: The type to test against. Typically this will be something like typeof( MyType<> )

public static bool IsInitOnly(System.Reflection.PropertyInfo property)

Determine if this property is init-only.

Parameters

  • property: The property to test.

Returns

Returns true if the property is init-only, false otherwise.

public static bool IsPowerOfTwo(int x)

Return true if the number is a power of two (2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, etc)

public static bool IsValid(Sandbox.IValid obj)

Returns false if `Sandbox.IValid` object is null or if `Sandbox.IValid.IsValid` returns false.

public static bool IsValidTag(string source)

Is this string a valid Tag. This is a way to check if a string is a valid tag, project wide. So our logic is always the same. - not null - between 1 and 32 chars - a-z

public static string Md5(string input)

Get the md5 hash of a string.

public static string Plural(int a, string single, string plural)

Return single if 1 else plural

public static string QuoteSafe(string str, bool optional = False)

Puts quote marks around a string. Internal quotes are backslashed.

Parameters

  • optional: Default: False

public static string ReadNullTerminatedString(System.IO.Stream stream, long offset)

Read a null terminated string from the stream, at given offset.

Parameters

  • stream: The stream to read from.
  • offset: Offset where to start reading, from the beginning of the stream.

public static string RemoveBadCharacters(string str)

Removes bad, invisible characters that are commonly used to exploit. https://en.wikipedia.org/wiki/Zero-width_non-joiner

public static Rotation Rotation(System.Random self)

Returns a uniformly random rotation.

public static string SimplifyPath(string str)

Gets rid of ../'s (from /path/folder/../file.txt to /path/file.txt)

public static string Snippet(string source, string find, int padding)

Given a large string, find all occurrences of a substring and return them with padding. This is useful in situations where you're searching for a word in a hug body of text, and want to show how it's used without displaying the whole text.

public static string[] SplitQuotesStrings(string input)

in : I am "splitting a" string "because it's fun " out : ["I", "am", "splitting a", "string", "because it's fun"]

public static System.Collections.Generic.IEnumerable`1<Vector3> TcbSpline(System.Collections.Generic.IEnumerable`1<Vector3> points, int interpolation, float tension, float continuity, float bias)

public static string ToBase36(T i)

Encode the given number into a Base36 string

public static bool ToBool(string str)

Try to convert to bool. Inputs can be true, false, yes, no, 0, 1, null (caps insensitive)

public static decimal ToDecimal(string str, decimal Default = default)

128-bit data type that returns sane results for almost any input. All other numeric types can cast from this.

Parameters

  • Default: Default: default

public static float ToDouble(string str, float Default = 0)

Convert to `System.Double`, if not then return `Default`.

Parameters

  • Default: Default: 0

public static double ToDoubleEval(string expression, double Default = 0)

Convert to `System.Double`. Might be a string formula. This is always going to be slower than a call to `Sandbox.SandboxSystemExtensions.ToDouble(System.String,System.Single)`.

Parameters

  • Default: Default: 0

public static float ToFloat(string str, float Default = 0)

Convert to `System.Single`, if not then return `Default`.

Parameters

  • Default: Default: 0

public static float ToFloatEval(string expression, float Default = 0)

Convert to `System.Single`. Might be a string formula. This is always going to be slower than a call to `Sandbox.SandboxSystemExtensions.ToFloat(System.String,System.Single)`.

Parameters

  • Default: Default: 0

public static System.Guid ToGuid(string input)

Take a string and create a deterministic guid.

public static int ToInt(string str, int Default = 0)

Convert to `System.Int32`, if not then return `Default`.

Parameters

  • Default: Default: 0

public static int ToIntEval(string expression, int Default = 0)

Convert to `System.Int32`. Might be a string formula. This is always going to be slower than a call to `Sandbox.SandboxSystemExtensions.ToInt(System.String,System.Int32)`.

Parameters

  • Default: Default: 0

public static long ToLong(string str, long Default = 0)

Convert to `System.Int64`, if not then return `Default`.

Parameters

  • Default: Default: 0

public static long ToLongEval(string expression, long Default = 0)

Convert to `System.Int64`. Might be a string formula. This is always going to be slower than a call to `Sandbox.SandboxSystemExtensions.ToLong(System.String,System.Int64)`.

Parameters

  • Default: Default: 0

public static string ToRemainingTimeString(System.TimeSpan span)

public static string ToTitleCase(string source)

Convert a variable name to something more user friendly.

public static object ToType(string str, System.Type t)

Try to politely convert from a string to another type

public static uint ToUInt(string str, int Default = 0)

Convert to `System.UInt32`, if not then return `Default`.

Parameters

  • Default: Default: 0

public static ulong ToULong(string str, ulong Default = 0)

Convert to `System.UInt64`, if not then return `Default`.

Parameters

  • Default: Default: 0

public static string TrimQuoted(string str, bool ignoreSurroundingSpaces = False)

The string might start and end in quotes ( ", ' ), remove those if that is the case.

Parameters

  • ignoreSurroundingSpaces: Default: False

public static string Truncate(string str, int maxLength, string appendage = null)

If the string is longer than this amount of characters then truncate it If appendage is defined, it will be appended to the end of truncated strings (ie, "..")

Parameters

  • appendage: Default: null

public static string TruncateFilename(string str, int maxLength, string appendage = null)

If the string is longer than this amount of characters then truncate it If appendage is defined, it will be appended to the end of truncated strings (ie, "..")

Parameters

  • appendage: Default: null

public static bool TryToType(string str, System.Type t, object Value)

Try to politely convert from a string to another type

public static int UnsignedMod(int a, int b)

Does what you expected to happen when you did "a % b", that is, handles negative `a` values by returning a positive number from the end.

public static string UrlEncode(string str)

public static Vector2 VectorInCircle(System.Random self, float radius = 1)

Uniformly samples a 2D position from all points with distance at most `radius` from the origin.

Parameters

  • radius: Default: 1

public static Vector3 VectorInSphere(System.Random self, float radius = 1)

Uniformly samples a 3D position from all points with distance at most `radius` from the origin.

Parameters

  • radius: Default: 1

public static Vector2 VectorInSquare(System.Random self, float extents = 1)

Uniformly samples a 2D position from a square with coordinates in the range -`extents` to +`extents`.

Parameters

  • extents: Default: 1

public static bool WildcardMatch(string str, string wildcard)

Returns true if this string matches a wildcard match. Check is case insensitive. Supports '*' (zero or more chars) and '?' (exactly one char) wildcards. The backslash character '\' escapes '*' to match it literally.

public static string WithExtension(string path, string ext)

Adds or replaces the extension of `path` to `ext`.

Parameters

  • path: A file path with or without an extension.
  • ext: A file extension with or without a leading period.

public static T WithFlag(T value, T flag, bool set)

Metadata

FieldValue
NamespaceSandbox
Typeclass
AssemblySandbox.System
Doc IDT:Sandbox.SandboxSystemExtensions

On this page

Methodspublic static Angles Angles(System.Random self)public static System.Int32 AsInt(T value)public static System.String Base64Decode(System.String base64EncodedData)public static System.String Base64Encode(System.String plainText)public static System.Int32 BitsSet(System.Int32 i)public static System.Collections.Generic.IEnumerable`1<Vector3> CatmullRomSpline(System.Collections.Generic.IEnumerable`1<Vector3> points, System.Int32 interpolation)public static System.Boolean CheckValidationAttributes(System.Reflection.PropertyInfo prop, System.Object obj, System.String[] errors, System.String name = null)public static T Clamp(T input, T min, T max)public static System.Collections.Generic.Dictionary`2<TKey,TValue> Clone(System.Collections.Generic.Dictionary`2<TKey,TValue> dict)public static System.String CollapseSpacesAndPreserveLines(System.String str)public static System.String CollapseWhiteSpace(System.String str)public static Color Color(System.Random self)public static System.String Columnize(System.String str, System.Int32 maxLength, System.Boolean right = False)public static System.Boolean Contains(System.String source, System.String toCheck, System.StringComparison comp)public static System.Boolean Contains(T value, T flag)public static System.Int32 Distance(System.String source, System.String target)public static System.Double Double(System.Random self, System.Double min, System.Double max)public static System.Double Double(System.Random self, System.Double max = 1)public static System.Int32 FastHash(System.String str)public static System.UInt64 FastHash64(System.String str)public static System.Single Float(System.Random self, System.Single min, System.Single max)public static System.Single Float(System.Random self, System.Single max)public static System.Single Float(System.Random self)public static System.Single FloatDeterministic(System.Random self, System.Int32 i)public static System.Threading.Tasks.Task ForEachTaskAsync(System.Collections.Generic.IEnumerable`1<T> source, System.Func`2<T,System.Threading.Tasks.Task> body, System.Int32 maxRunning = 8, System.Threading.CancellationToken token = null)public static System.String FormatBytes(T input, System.Boolean shortFormat = False)public static System.String FormatNumberShort(System.Int64 num)public static System.String FormatNumberShort(System.UInt64 num)public static System.String FormatSeconds(System.Int64 secs)public static System.String FormatSeconds(System.UInt64 secs)public static System.String FormatSecondsLong(System.Int64 secs)public static System.String FormatSecondsLong(System.UInt64 secs)public static System.String FormatWithSuffix(System.Int32 num)public static T FromArray(System.Random self, T[] array, T defVal = null)public static System.Int64 FromBase36(System.String input)public static T FromList(System.Random self, System.Collections.Generic.List`1<T> array, T defVal = null)public static System.Single Gaussian(System.Random self, System.Single mean = 0, System.Single stdDev = 1)public static Vector2 Gaussian2D(System.Random self, System.Nullable`1<Vector2> mean = null, System.Nullable`1<Vector2> stdDev = null)public static Vector3 Gaussian3D(System.Random self, System.Nullable`1<Vector3> mean = null, System.Nullable`1<Vector3> stdDev = null)public static Vector4 Gaussian4D(System.Random self, System.Nullable`1<Vector4> mean = null, System.Nullable`1<Vector4> stdDev = null)public static T GetAttributeOfType(System.Enum enumVal)public static System.Type GetCommonBaseType(System.Collections.Generic.IEnumerable`1<System.Type> types)public static System.Int32 GetEpoch(System.DateTime d)public static System.String GetFilenameSafe(System.String input)public static System.Int32 GetManagedSize(System.Type t)public static TValue GetOrCreate(System.Collections.Generic.IDictionary`2<TKey,TValue> dict, TKey key)public static T GetPropertyValue(System.Text.Json.Nodes.JsonObject jso, System.String membername, T defaultvalue)public static System.String HtmlEncode(System.String str)public static System.String Humanize(System.DateOnly dateTime)public static System.String Humanize(System.DateTime dateTime)public static System.String Humanize(System.DateTimeOffset dateTime)public static System.String Humanize(System.TimeOnly dateTime)public static System.String Humanize(System.TimeSpan timespan, System.Boolean shortVersion = False, System.Boolean minutes = True, System.Boolean hours = True, System.Boolean days = True)public static System.String Humanize(System.TimeSpan dateTime, System.Int32 precision = 1)public static System.Int32 Int(System.Random self, System.Int32 min, System.Int32 max)public static System.Int32 Int(System.Random self, System.Int32 max)public static System.Void InvokeWithWarning(System.Action action)public static System.Void InvokeWithWarning(System.Action`1<T> action, T arg0)public static System.Void InvokeWithWarning(System.Action`2<T1,T2> action, T1 arg0, T2 arg1)public static System.Boolean IsBasedOnGenericType(System.Type src, System.Type test)public static System.Boolean IsInitOnly(System.Reflection.PropertyInfo property)public static System.Boolean IsPowerOfTwo(System.Int32 x)public static System.Boolean IsValid(Sandbox.IValid obj)public static System.Boolean IsValidTag(System.String source)public static System.String KiloFormat(System.Int32 num)public static System.String KiloFormat(System.Int64 num)public static System.String Md5(System.String input)public static System.String NormalizeFilename(System.String str, System.Boolean enforceInitialSlash, System.Boolean enforceLowerCase, System.Char targetSeparator = /)public static System.String NormalizeFilename(System.String str, System.Boolean enforceInitialSlash = True)public static System.String Plural(System.Int32 a, System.String single, System.String plural)public static System.String QuoteSafe(System.String str, System.Boolean optional = False)public static System.String ReadNullTerminatedString(System.IO.Stream stream, System.Int64 offset)public static System.String RemoveBadCharacters(System.String str)public static Rotation Rotation(System.Random self)public static System.String SimplifyPath(System.String str)public static System.String SizeFormat(System.Int32 bytes)public static System.String SizeFormat(System.Int64 bytes)public static System.String Snippet(System.String source, System.String find, System.Int32 padding)public static System.String[] SplitQuotesStrings(System.String input)public static System.Collections.Generic.IEnumerable`1<Vector3> TcbSpline(System.Collections.Generic.IEnumerable`1<Vector3> points, System.Int32 interpolation, System.Single tension, System.Single continuity, System.Single bias)public static System.String ToBase36(T i)public static System.Boolean ToBool(System.String str)public static System.DateTime ToDateTime(System.Int32 seconds)public static System.DateTime ToDateTime(System.Int64 seconds)public static System.Decimal ToDecimal(System.String str, System.Decimal Default = default)public static System.Single ToDouble(System.String str, System.Single Default = 0)public static System.Double ToDoubleEval(System.String expression, System.Double Default = 0)public static System.Single ToFloat(System.String str, System.Single Default = 0)public static System.Single ToFloatEval(System.String expression, System.Single Default = 0)public static System.Guid ToGuid(System.String input)public static System.Int32 ToInt(System.String str, System.Int32 Default = 0)public static System.Int32 ToIntEval(System.String expression, System.Int32 Default = 0)public static System.Int64 ToLong(System.String str, System.Int64 Default = 0)public static System.Int64 ToLongEval(System.String expression, System.Int64 Default = 0)public static System.String ToMetric(System.Double input, System.Int32 decimals = 2)public static System.String ToMetric(System.Int32 input, System.Int32 decimals = 2)public static System.String ToMetric(System.Int64 input, System.Int32 decimals = 2)public static System.String ToMetric(System.Single input, System.Int32 decimals = 2)public static System.String ToRelativeTimeString(System.DateTime dateTime)public static System.String ToRelativeTimeString(System.TimeSpan span)public static System.String ToRemainingTimeString(System.TimeSpan span)public static System.String ToSimpleString(System.Delegate deleg, System.Boolean includeNamespace = True)public static System.String ToSimpleString(System.Reflection.MemberInfo member, System.Boolean includeNamespace = True)public static System.String ToSimpleString(System.Type type, System.Boolean includeNamespace = True)public static System.String ToTitleCase(System.String source)public static System.Object ToType(System.String str, System.Type t)public static System.UInt32 ToUInt(System.String str, System.Int32 Default = 0)public static System.UInt64 ToULong(System.String str, System.UInt64 Default = 0)public static System.String TrimQuoted(System.String str, System.Boolean ignoreSurroundingSpaces = False)public static System.String Truncate(System.String str, System.Int32 maxLength, System.String appendage = null)public static System.String TruncateFilename(System.String str, System.Int32 maxLength, System.String appendage = null)public static System.Boolean TryToType(System.String str, System.Type t, System.Object Value)public static System.Int32 UnsignedMod(System.Int32 a, System.Int32 b)public static System.String UrlEncode(System.String str)public static Vector2 VectorInCircle(System.Random self, System.Single radius = 1)public static Vector3 VectorInCube(System.Random self, BBox box)public static Vector3 VectorInCube(System.Random self, System.Single extents = 1)public static Vector3 VectorInSphere(System.Random self, System.Single radius = 1)public static Vector2 VectorInSquare(System.Random self, System.Single extents = 1)public static System.Boolean WildcardMatch(System.String str, System.String wildcard)public static System.String WithExtension(System.String path, System.String ext)public static T WithFlag(T value, T flag, System.Boolean set)Metadata