Utility Class
Definition
Section titled “Definition”Generic helper properties and methods.
public static class Utility
Inheritance object
Fields
Section titled “Fields”LoadContext
Section titled “LoadContext”Assembly Load Context for BepInEx, all game assemblies should be loaded here.
public static AssemblyLoadContext LoadContext
BepInExVersion
Section titled “BepInExVersion”BepInEx version.
public static Version BepInExVersion
Properties
Section titled “Properties”CLRSupportsDynamicAssemblies
Section titled “CLRSupportsDynamicAssemblies”Whether current Common Language Runtime supports dynamic method generation using System.Reflection.Emit
namespace.
public static bool CLRSupportsDynamicAssemblies { get; }
UTF8NoBom
Section titled “UTF8NoBom”An encoding for UTF-8 which does not emit a byte order mark (BOM).
public static Encoding UTF8NoBom { get; }
Methods
Section titled “Methods”TryDo(Action, out Exception)
Section titled “TryDo(Action, out Exception)”Try to perform an action.
public static bool TryDo(Action action, out Exception exception)
Parameters
Section titled “Parameters”action
Action
Action to perform.
exception
Exception
Possible exception that gets returned.
Returns
Section titled “Returns”bool
True, if action succeeded, false if an exception occured.
CombinePaths(params string[])
Section titled “CombinePaths(params string[])”Combines multiple paths together, as the specific method is not available in .NET 3.5.
public static string CombinePaths(params string[] parts)
Parameters
Section titled “Parameters”parts
string[]
The multiple paths to combine together.
Returns
Section titled “Returns”string
A combined path.
ParentDirectory(string, int)
Section titled “ParentDirectory(string, int)”Returns the parent directory of a path, optionally specifying the amount of levels.
public static string ParentDirectory(string path, int levels = 1)
Parameters
Section titled “Parameters”path
string
The path to get the parent directory of.
levels
int
The amount of levels to traverse. Defaults to 1
Returns
Section titled “Returns”string
The parent directory.
SafeParseBool(string, bool)
Section titled “SafeParseBool(string, bool)”Tries to parse a bool, with a default value if unable to parse.
public static bool SafeParseBool(string input, bool defaultValue = false)
Parameters
Section titled “Parameters”input
string
The string to parse
defaultValue
bool
The value to return if parsing is unsuccessful.
Returns
Section titled “Returns”bool
Boolean value of input if able to be parsed, otherwise default value.
ConvertToWWWFormat(string)
Section titled “ConvertToWWWFormat(string)”Converts a file path into a UnityEngine.WWW format.
public static string ConvertToWWWFormat(string path)
Parameters
Section titled “Parameters”path
string
The file path to convert.
Returns
Section titled “Returns”string
A converted file path.
IsNullOrWhiteSpace(string)
Section titled “IsNullOrWhiteSpace(string)”Indicates whether a specified string is null, empty, or consists only of white-space characters.
public static bool IsNullOrWhiteSpace(this string self)
Parameters
Section titled “Parameters”self
string
The string to test.
Returns
Section titled “Returns”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.
public static IEnumerable<TNode> TopologicalSort<TNode>(IEnumerable<TNode> nodes, Func<TNode, IEnumerable<TNode>> dependencySelector)
Parameters
Section titled “Parameters”nodes
IEnumerable<TNode>
Nodes to sort
dependencySelector
Func<TNode, IEnumerable<TNode>>
Function that maps a node to a collection of its dependencies.
Returns
Section titled “Returns”IEnumerable<TNode>
Collection of nodes sorted in the order of least dependencies to the most.
Exceptions
Section titled “Exceptions”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.
public static bool TryResolveDllAssembly<T>(AssemblyName assemblyName, string directory, Func<string, T> loader, out T assembly) where T : class
Parameters
Section titled “Parameters”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.
Returns
Section titled “Returns”bool
True, if the assembly was found and loaded. Otherwise, false.
IsSubtypeOf(TypeDefinition, Type)
Section titled “IsSubtypeOf(TypeDefinition, Type)”Checks whether a given cecil type definition is a subtype of a provided type.
public static bool IsSubtypeOf(this TypeDefinition self, Type td)
Parameters
Section titled “Parameters”self
TypeDefinition
Cecil type definition
td
Type
Type to check against
Returns
Section titled “Returns”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.
public static bool TryResolveDllAssembly(AssemblyName assemblyName, string directory, out Assembly assembly)
Parameters
Section titled “Parameters”assemblyName
AssemblyName
Name of the assembly, of the type
directory
string
Directory to search the assembly from.
assembly
Assembly
The loaded assembly.
Returns
Section titled “Returns”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.
public static bool TryResolveDllAssembly(AssemblyName assemblyName, string directory, ReaderParameters readerParameters, out AssemblyDefinition assembly)
Parameters
Section titled “Parameters”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.
Returns
Section titled “Returns”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
public static bool TryOpenFileStream(string path, FileMode mode, out FileStream fileStream, FileAccess access = FileAccess.ReadWrite, FileShare share = FileShare.Read)
Parameters
Section titled “Parameters”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
Returns
Section titled “Returns”EnumerateAllMethods(TypeDefinition)
Section titled “EnumerateAllMethods(TypeDefinition)”public static IEnumerable<MethodDefinition> EnumerateAllMethods(this TypeDefinition type)
Parameters
Section titled “Parameters”type
TypeDefinition
Returns
Section titled “Returns”HashStream(Stream)
Section titled “HashStream(Stream)”Compute a MD5 hash of the given stream.
public static string HashStream(Stream stream)
Parameters
Section titled “Parameters”stream
Stream
Stream to hash
Returns
Section titled “Returns”string
MD5 hash as a hex string
HashStrings(params string[])
Section titled “HashStrings(params string[])”Hash a list of strings using MD5
public static string HashStrings(params string[] strings)
Parameters
Section titled “Parameters”strings
string[]
Strings to hash
Returns
Section titled “Returns”string
MD5 of the strings
ByteArrayToString(byte[])
Section titled “ByteArrayToString(byte[])”Convert the given array to a hex string.
public static string ByteArrayToString(byte[] data)
Parameters
Section titled “Parameters”data
byte[]
Bytes to convert.
Returns
Section titled “Returns”string
Bytes reinterpreted as a hex number.
GetCommandLineArgValue(string)
Section titled “GetCommandLineArgValue(string)”Get a value of a command line argument
public static string GetCommandLineArgValue(string arg)
Parameters
Section titled “Parameters”arg
string
Argument name
Returns
Section titled “Returns”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
public static bool TryParseAssemblyName(string fullName, out AssemblyName assemblyName)
Parameters
Section titled “Parameters”fullName
string
Fully qualified assembly name
assemblyName
AssemblyName
Resulting
Returns
Section titled “Returns”bool
true
, if parsing was successful, otherwise false
Remarks
Section titled “Remarks”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.
public static IEnumerable<string> GetUniqueFilesInDirectories(IEnumerable<string> directories, string pattern = "*")
Parameters
Section titled “Parameters”directories
IEnumerable<string>
Directories to search from.
pattern
string
File pattern to search.
Returns
Section titled “Returns”IEnumerable<string>
Collection of all files in the directories.