123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- namespace BepInEx
- {
-
-
-
- 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.All(char.IsWhiteSpace);
- }
- public static IEnumerable<TNode> TopologicalSort<TNode>(IEnumerable<TNode> nodes, Func<TNode, IEnumerable<TNode>> dependencySelector)
- {
- List<TNode> sorted_list = new List<TNode>();
- HashSet<TNode> visited = new HashSet<TNode>();
- HashSet<TNode> sorted = new HashSet<TNode>();
- foreach (TNode input in nodes)
- {
- Stack<TNode> currentStack = new Stack<TNode>();
- if (!Visit(input, currentStack))
- {
- throw new Exception("Cyclic Dependency:\r\n" + currentStack
- .Select(x => $" - {x}")
- .Aggregate((a, b) => $"{a}\r\n{b}"));
- }
- }
- return sorted_list;
- bool Visit(TNode node, Stack<TNode> stack)
- {
- if (visited.Contains(node))
- {
- if (!sorted.Contains(node))
- {
- return false;
- }
- }
- else
- {
- visited.Add(node);
- stack.Push(node);
- foreach (var dep in dependencySelector(node))
- if (!Visit(dep, stack))
- return false;
- sorted.Add(node);
- sorted_list.Add(node);
- stack.Pop();
- }
- return true;
- }
- }
-
-
-
-
-
-
-
- public static bool TryResolveDllAssembly(AssemblyName assemblyName, string directory, out Assembly assembly)
- {
- assembly = null;
- var potentialDirectories = new List<string> { directory };
- potentialDirectories.AddRange(Directory.GetDirectories(directory, "*", SearchOption.AllDirectories));
- foreach (string subDirectory in potentialDirectories)
- {
- string path = Path.Combine(subDirectory, $"{assemblyName.Name}.dll");
- if (!File.Exists(path))
- continue;
- try
- {
- assembly = Assembly.LoadFile(path);
- }
- catch (Exception)
- {
- continue;
- }
- return true;
- }
- return false;
- }
- }
- }
|