123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- namespace BepInEx.Common
- {
-
-
-
- public static class Utility
- {
-
-
-
-
-
- public static string CombinePaths(params string[] parts) => parts.Aggregate(Path.Combine);
-
-
-
-
-
-
- public static bool SafeParseBool(string input, bool defaultValue = false)
- {
- return bool.TryParse(input, out bool result) ? result : defaultValue;
- }
-
-
-
-
-
- public static string ConvertToWWWFormat(string path)
- {
- return $"file://{path.Replace('\\', '/')}";
- }
-
-
-
-
-
- public static bool IsNullOrWhiteSpace(this string self)
- {
- return self == null || self.Trim().Length == 0;
- }
- public static IEnumerable<TNode> TopologicalSort<TNode>(IEnumerable<TNode> nodes, Func<TNode, IEnumerable<TNode>> dependencySelector)
- {
- List<TNode> nodeQueue = new List<TNode>(nodes);
- List<TNode> sorted = new List<TNode>();
- while (nodeQueue.Count > 0)
- {
- List<TNode> nextBatch = nodeQueue.Where(x => !dependencySelector(x).Except(sorted).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;
- }
-
-
-
-
-
-
-
- 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;
- }
- }
- }
|