using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace BepInEx
{
///
/// Generic helper properties and methods.
///
public static class Utility
{
///
/// Combines multiple paths together, as the specfic method is not availble in .NET 3.5.
///
/// The multiple paths to combine together.
/// A combined path.
public static string CombinePaths(params string[] parts) => parts.Aggregate(Path.Combine);
///
/// Tries to parse a bool, with a default value if unable to parse.
///
/// The string to parse
/// The value to return if parsing is unsuccessful.
/// Boolean value of input if able to be parsed, otherwise default value.
public static bool SafeParseBool(string input, bool defaultValue = false)
{
return bool.TryParse(input, out bool result) ? result : defaultValue;
}
///
/// Converts a file path into a UnityEngine.WWW format.
///
/// The file path to convert.
/// A converted file path.
public static string ConvertToWWWFormat(string path)
{
return $"file://{path.Replace('\\', '/')}";
}
///
/// Indicates whether a specified string is null, empty, or consists only of white-space characters.
///
/// The string to test.
/// True if the value parameter is null or empty, or if value consists exclusively of white-space characters.
public static bool IsNullOrWhiteSpace(this string self)
{
return self == null || self.Trim().Length == 0;
}
public static IEnumerable TopologicalSort(IEnumerable nodes, Func> dependencySelector)
{
List nodeQueue = new List(nodes);
List sorted = new List();
while (nodeQueue.Count > 0)
{
List nextBatch = nodeQueue.Where(x => !dependencySelector(x).Except(sorted).Intersect(nodeQueue).Any()).ToList();
if (!nextBatch.Any())
throw new Exception("Cyclic Dependency:\r\n" +
nodeQueue.Select(x => x.ToString()).Aggregate((a , b) => $"{a}\r\n{b}"));
sorted.AddRange(nextBatch);
foreach (TNode item in nextBatch)
nodeQueue.Remove(item);
}
return sorted;
}
///
/// Try to resolve and load the given assembly DLL.
///
/// Name of the assembly, of the type .
/// Directory to search the assembly from.
/// The loaded assembly.
/// True, if the assembly was found and loaded. Otherwise, false.
public static bool TryResolveDllAssembly(AssemblyName assemblyName, string directory, out Assembly assembly)
{
assembly = null;
string path = Path.Combine(directory, $"{assemblyName.Name}.dll");
if (!File.Exists(path))
return false;
try
{
assembly = Assembly.LoadFile(path);
}
catch (Exception)
{
return false;
}
return true;
}
}
}