public static abstract sealed class Sandbox.SandboxSystemExtensions
Methods
Showing 122 methods
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 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: 8token: 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: 0stdDev: Default: 1
public static Vector2 Gaussian2D(System.Random self, System.Nullable`1<Vector2> mean = null, System.Nullable`1<Vector2> stdDev = null)
Parameters
mean: Default: nullstdDev: Default: null
public static Vector3 Gaussian3D(System.Random self, System.Nullable`1<Vector3> mean = null, System.Nullable`1<Vector3> stdDev = null)
Parameters
mean: Default: nullstdDev: Default: null
public static Vector4 Gaussian4D(System.Random self, System.Nullable`1<Vector4> mean = null, System.Nullable`1<Vector4> stdDev = null)
Parameters
mean: Default: nullstdDev: 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 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)
No results match this filter.
Metadata
| Field | Value |
|---|---|
| Namespace | Sandbox |
| Type | class |
| Assembly | Sandbox.System |
| Doc ID | T:Sandbox.SandboxSystemExtensions |