Class Utility

Generic helper properties and methods.

Inheritance
System.Object Utility
Inherited Members
System.Object.ToString() System.Object.Equals(System.Object) System.Object.Equals(System.Object, System.Object) System.Object.ReferenceEquals(System.Object, System.Object) System.Object.GetHashCode() System.Object.GetType() System.Object.MemberwiseClone()
Namespace: BepInEx
Assembly: BepInEx.dll
Syntax
public static class Utility

Properties

CLRSupportsDynamicAssemblies

Whether current Common Language Runtime supports dynamic method generation using System.Reflection.Emit namespace.

Declaration
public static bool CLRSupportsDynamicAssemblies { get; }
Property Value
Type Description
System.Boolean

UTF8NoBom

An encoding for UTF-8 which does not emit a byte order mark (BOM).

Declaration
public static Encoding UTF8NoBom { get; }
Property Value
Type Description
System.Text.Encoding

Methods

CombinePaths(String[])

Combines multiple paths together, as the specific method is not available in .NET 3.5.

Declaration
public static string CombinePaths(params string[] parts)
Parameters
Type Name Description
System.String[] parts

The multiple paths to combine together.

Returns
Type Description
System.String

A combined path.

ConvertToWWWFormat(String)

Converts a file path into a UnityEngine.WWW format.

Declaration
public static string ConvertToWWWFormat(string path)
Parameters
Type Name Description
System.String path

The file path to convert.

Returns
Type Description
System.String

A converted file path.

GetUniqueFilesInDirectories(IEnumerable<String>, String)

Gets unique files in all given directories. If the file with the same name exists in multiple directories, only the first occurrence is returned.

Declaration
public static IEnumerable<string> GetUniqueFilesInDirectories(IEnumerable<string> directories, string pattern = "*")
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<System.String> directories

Directories to search from.

System.String pattern

File pattern to search.

Returns
Type Description
System.Collections.Generic.IEnumerable<System.String>

Collection of all files in the directories.

IsNullOrWhiteSpace(String)

Indicates whether a specified string is null, empty, or consists only of white-space characters.

Declaration
public static bool IsNullOrWhiteSpace(this string self)
Parameters
Type Name Description
System.String self

The string to test.

Returns
Type Description
System.Boolean

True if the value parameter is null or empty, or if value consists exclusively of white-space characters.

IsSubtypeOf(TypeDefinition, Type)

Checks whether a given cecil type definition is a subtype of a provided type.

Declaration
public static bool IsSubtypeOf(this TypeDefinition self, Type td)
Parameters
Type Name Description
TypeDefinition self

Cecil type definition

System.Type td

Type to check against

Returns
Type Description
System.Boolean

Whether the given cecil type is a subtype of the type.

ParentDirectory(String, Int32)

Returns the parent directory of a path, optionally specifying the amount of levels.

Declaration
public static string ParentDirectory(string path, int levels = 1)
Parameters
Type Name Description
System.String path

The path to get the parent directory of.

System.Int32 levels

The amount of levels to traverse. Defaults to 1

Returns
Type Description
System.String

The parent directory.

SafeParseBool(String, Boolean)

Tries to parse a bool, with a default value if unable to parse.

Declaration
public static bool SafeParseBool(string input, bool defaultValue = false)
Parameters
Type Name Description
System.String input

The string to parse

System.Boolean defaultValue

The value to return if parsing is unsuccessful.

Returns
Type Description
System.Boolean

Boolean value of input if able to be parsed, otherwise default value.

TopologicalSort<TNode>(IEnumerable<TNode>, Func<TNode, IEnumerable<TNode>>)

Sorts a given dependency graph using a direct toposort, reporting possible cyclic dependencies.

Declaration
public static IEnumerable<TNode> TopologicalSort<TNode>(IEnumerable<TNode> nodes, Func<TNode, IEnumerable<TNode>> dependencySelector)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<TNode> nodes

Nodes to sort

System.Func<TNode, System.Collections.Generic.IEnumerable<TNode>> dependencySelector

Function that maps a node to a collection of its dependencies.

Returns
Type Description
System.Collections.Generic.IEnumerable<TNode>

Collection of nodes sorted in the order of least dependencies to the most.

Type Parameters
Name Description
TNode

Type of the node in a dependency graph.

Exceptions
Type Condition
System.Exception

Thrown when a cyclic dependency occurs.

TryDo(Action, out Exception)

Try to perform an action.

Declaration
public static bool TryDo(Action action, out Exception exception)
Parameters
Type Name Description
System.Action action

Action to perform.

System.Exception exception

Possible exception that gets returned.

Returns
Type Description
System.Boolean

True, if action succeeded, false if an exception occured.

TryOpenFileStream(String, FileMode, out FileStream, FileAccess, FileShare)

Tries to create a file with the given name

Declaration
public static bool TryOpenFileStream(string path, FileMode mode, out FileStream fileStream, FileAccess access = FileAccess.ReadWrite, FileShare share = FileShare.Read)
Parameters
Type Name Description
System.String path

Path of the file to create

System.IO.FileMode mode

File open mode

System.IO.FileStream fileStream

Resulting filestream

System.IO.FileAccess access

File access options

System.IO.FileShare share

File share options

Returns
Type Description
System.Boolean

TryParseAssemblyName(String, out AssemblyName)

Try to parse given string as an assembly name

Declaration
public static bool TryParseAssemblyName(string fullName, out AssemblyName assemblyName)
Parameters
Type Name Description
System.String fullName

Fully qualified assembly name

System.Reflection.AssemblyName assemblyName

Resulting System.Reflection.AssemblyName instance

Returns
Type Description
System.Boolean

true, if parsing was successful, otherwise false

Remarks

On some versions of mono, using System.Reflection.Assembly.GetName fails because it runs on unmanaged side which has problems with encoding. Using System.Reflection.AssemblyName solves this by doing parsing on managed side instead.

TryResolveDllAssembly(AssemblyName, String, ReaderParameters, out AssemblyDefinition)

Try to resolve and load the given assembly DLL.

Declaration
public static bool TryResolveDllAssembly(AssemblyName assemblyName, string directory, ReaderParameters readerParameters, out AssemblyDefinition assembly)
Parameters
Type Name Description
System.Reflection.AssemblyName assemblyName

Name of the assembly, of the type System.Reflection.AssemblyName.

System.String directory

Directory to search the assembly from.

ReaderParameters readerParameters

Reader parameters that contain possible custom assembly resolver.

AssemblyDefinition assembly

The loaded assembly.

Returns
Type Description
System.Boolean

True, if the assembly was found and loaded. Otherwise, false.

TryResolveDllAssembly(AssemblyName, String, out Assembly)

Try to resolve and load the given assembly DLL.

Declaration
public static bool TryResolveDllAssembly(AssemblyName assemblyName, string directory, out Assembly assembly)
Parameters
Type Name Description
System.Reflection.AssemblyName assemblyName

Name of the assembly, of the type System.Reflection.AssemblyName.

System.String directory

Directory to search the assembly from.

System.Reflection.Assembly assembly

The loaded assembly.

Returns
Type Description
System.Boolean

True, if the assembly was found and loaded. Otherwise, false.