Skip to content

Utility Class

Generic helper properties and methods.

C#
public static class Utility

Inheritance object

Assembly Load Context for BepInEx, all game assemblies should be loaded here.

C#
public static AssemblyLoadContext LoadContext

BepInEx version.

C#
public static Version BepInExVersion

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

C#
public static bool CLRSupportsDynamicAssemblies { get; }

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

C#
public static Encoding UTF8NoBom { get; }

Try to perform an action.

C#
public static bool TryDo(Action action, out Exception exception)

action Action
Action to perform.

exception Exception
Possible exception that gets returned.

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

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

C#
public static string CombinePaths(params string[] parts)

parts string[]
The multiple paths to combine together.

string
A combined path.

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

C#
public static string ParentDirectory(string path, int levels = 1)

path string
The path to get the parent directory of.

levels int
The amount of levels to traverse. Defaults to 1

string
The parent directory.

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

C#
public static bool SafeParseBool(string input, bool defaultValue = false)

input string
The string to parse

defaultValue bool
The value to return if parsing is unsuccessful.

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

Converts a file path into a UnityEngine.WWW format.

C#
public static string ConvertToWWWFormat(string path)

path string
The file path to convert.

string
A converted file path.

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

C#
public static bool IsNullOrWhiteSpace(this string self)

self string
The string to test.

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

TopologicalSort(IEnumerable, Func<TNode, IEnumerable>)

Section titled “TopologicalSort(IEnumerable, Func<TNode, IEnumerable>)”

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

C#
public static IEnumerable<TNode> TopologicalSort<TNode>(IEnumerable<TNode> nodes, Func<TNode, IEnumerable<TNode>> dependencySelector)

nodes IEnumerable<TNode>
Nodes to sort

dependencySelector Func<TNode, IEnumerable<TNode>>
Function that maps a node to a collection of its dependencies.

IEnumerable<TNode>
Collection of nodes sorted in the order of least dependencies to the most.

Exception
Thrown when a cyclic dependency occurs.

TryResolveDllAssembly(AssemblyName, string, Func<string, T>, out T)

Section titled “TryResolveDllAssembly(AssemblyName, string, Func<string, T>, out T)”

Try to resolve and load the given assembly DLL.

C#
public static bool TryResolveDllAssembly<T>(AssemblyName assemblyName, string directory, Func<string, T> loader, out T assembly) where T : class

assemblyName AssemblyName
Name of the assembly, of the type .

directory string
Directory to search the assembly from.

loader Func<string, T>

assembly T
The loaded assembly.

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

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

C#
public static bool IsSubtypeOf(this TypeDefinition self, Type td)

self TypeDefinition
Cecil type definition

td Type
Type to check against

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

TryResolveDllAssembly(AssemblyName, string, out Assembly)

Section titled “TryResolveDllAssembly(AssemblyName, string, out Assembly)”

Try to resolve and load the given assembly DLL.

C#
public static bool TryResolveDllAssembly(AssemblyName assemblyName, string directory, out Assembly assembly)

assemblyName AssemblyName
Name of the assembly, of the type .

directory string
Directory to search the assembly from.

assembly Assembly
The loaded assembly.

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

TryResolveDllAssembly(AssemblyName, string, ReaderParameters, out AssemblyDefinition)

Section titled “TryResolveDllAssembly(AssemblyName, string, ReaderParameters, out AssemblyDefinition)”

Try to resolve and load the given assembly DLL.

C#
public static bool TryResolveDllAssembly(AssemblyName assemblyName, string directory, ReaderParameters readerParameters, out AssemblyDefinition assembly)

assemblyName AssemblyName
Name of the assembly, of the type .

directory string
Directory to search the assembly from.

readerParameters ReaderParameters
Reader parameters that contain possible custom assembly resolver.

assembly AssemblyDefinition
The loaded assembly.

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

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

Section titled “TryOpenFileStream(string, FileMode, out FileStream, FileAccess, FileShare)”

Tries to create a file with the given name

C#
public static bool TryOpenFileStream(string path, FileMode mode, out FileStream fileStream, FileAccess access = FileAccess.ReadWrite, FileShare share = FileShare.Read)

path string
Path of the file to create

mode FileMode
File open mode

fileStream FileStream
Resulting filestream

access FileAccess
File access options

share FileShare
File share options

bool

C#
public static IEnumerable<MethodDefinition> EnumerateAllMethods(this TypeDefinition type)

type TypeDefinition

IEnumerable<MethodDefinition>

Compute a MD5 hash of the given stream.

C#
public static string HashStream(Stream stream)

stream Stream
Stream to hash

string
MD5 hash as a hex string

Hash a list of strings using MD5

C#
public static string HashStrings(params string[] strings)

strings string[]
Strings to hash

string
MD5 of the strings

Convert the given array to a hex string.

C#
public static string ByteArrayToString(byte[] data)

data byte[]
Bytes to convert.

string
Bytes reinterpreted as a hex number.

Get a value of a command line argument

C#
public static string GetCommandLineArgValue(string arg)

arg string
Argument name

string
Next argument after the given argument name. If not found, returns null.

TryParseAssemblyName(string, out AssemblyName)

Section titled “TryParseAssemblyName(string, out AssemblyName)”

Try to parse given string as an assembly name

C#
public static bool TryParseAssemblyName(string fullName, out AssemblyName assemblyName)

fullName string
Fully qualified assembly name

assemblyName AssemblyName
Resulting instance

bool
true, if parsing was successful, otherwise false

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

GetUniqueFilesInDirectories(IEnumerable, string)

Section titled “GetUniqueFilesInDirectories(IEnumerable, 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.

C#
public static IEnumerable<string> GetUniqueFilesInDirectories(IEnumerable<string> directories, string pattern = "*")

directories IEnumerable<string>
Directories to search from.

pattern string
File pattern to search.

IEnumerable<string>
Collection of all files in the directories.