Utility.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. namespace BepInEx
  7. {
  8. /// <summary>
  9. /// Generic helper properties and methods.
  10. /// </summary>
  11. public static class Utility
  12. {
  13. /// <summary>
  14. /// Combines multiple paths together, as the specific method is not available in .NET 3.5.
  15. /// </summary>
  16. /// <param name="parts">The multiple paths to combine together.</param>
  17. /// <returns>A combined path.</returns>
  18. public static string CombinePaths(params string[] parts) => parts.Aggregate(Path.Combine);
  19. /// <summary>
  20. /// Tries to parse a bool, with a default value if unable to parse.
  21. /// </summary>
  22. /// <param name="input">The string to parse</param>
  23. /// <param name="defaultValue">The value to return if parsing is unsuccessful.</param>
  24. /// <returns>Boolean value of input if able to be parsed, otherwise default value.</returns>
  25. public static bool SafeParseBool(string input, bool defaultValue = false)
  26. {
  27. return bool.TryParse(input, out bool result) ? result : defaultValue;
  28. }
  29. /// <summary>
  30. /// Converts a file path into a UnityEngine.WWW format.
  31. /// </summary>
  32. /// <param name="path">The file path to convert.</param>
  33. /// <returns>A converted file path.</returns>
  34. public static string ConvertToWWWFormat(string path)
  35. {
  36. return $"file://{path.Replace('\\', '/')}";
  37. }
  38. /// <summary>
  39. /// Indicates whether a specified string is null, empty, or consists only of white-space characters.
  40. /// </summary>
  41. /// <param name="self">The string to test.</param>
  42. /// <returns>True if the value parameter is null or empty, or if value consists exclusively of white-space characters.</returns>
  43. public static bool IsNullOrWhiteSpace(this string self)
  44. {
  45. return self == null || self.All(char.IsWhiteSpace);
  46. }
  47. public static IEnumerable<TNode> TopologicalSort<TNode>(IEnumerable<TNode> nodes, Func<TNode, IEnumerable<TNode>> dependencySelector)
  48. {
  49. List<TNode> sorted_list = new List<TNode>();
  50. HashSet<TNode> visited = new HashSet<TNode>();
  51. HashSet<TNode> sorted = new HashSet<TNode>();
  52. foreach (TNode input in nodes)
  53. {
  54. Stack<TNode> currentStack = new Stack<TNode>();
  55. if (!Visit(input, currentStack))
  56. {
  57. throw new Exception("Cyclic Dependency:\r\n" + currentStack
  58. .Select(x => $" - {x}") //append dashes
  59. .Aggregate((a, b) => $"{a}\r\n{b}")); //add new lines inbetween
  60. }
  61. }
  62. return sorted_list;
  63. bool Visit(TNode node, Stack<TNode> stack)
  64. {
  65. if (visited.Contains(node))
  66. {
  67. if (!sorted.Contains(node))
  68. {
  69. return false;
  70. }
  71. }
  72. else
  73. {
  74. visited.Add(node);
  75. stack.Push(node);
  76. foreach (var dep in dependencySelector(node))
  77. if (!Visit(dep, stack))
  78. return false;
  79. sorted.Add(node);
  80. sorted_list.Add(node);
  81. stack.Pop();
  82. }
  83. return true;
  84. }
  85. }
  86. /// <summary>
  87. /// Try to resolve and load the given assembly DLL.
  88. /// </summary>
  89. /// <param name="assemblyName">Name of the assembly, of the type <see cref="AssemblyName" />.</param>
  90. /// <param name="directory">Directory to search the assembly from.</param>
  91. /// <param name="assembly">The loaded assembly.</param>
  92. /// <returns>True, if the assembly was found and loaded. Otherwise, false.</returns>
  93. public static bool TryResolveDllAssembly(AssemblyName assemblyName, string directory, out Assembly assembly)
  94. {
  95. assembly = null;
  96. var potentialDirectories = new List<string> { directory };
  97. potentialDirectories.AddRange(Directory.GetDirectories(directory, "*", SearchOption.AllDirectories));
  98. foreach (string subDirectory in potentialDirectories)
  99. {
  100. string path = Path.Combine(subDirectory, $"{assemblyName.Name}.dll");
  101. if (!File.Exists(path))
  102. continue;
  103. try
  104. {
  105. assembly = Assembly.LoadFile(path);
  106. }
  107. catch (Exception)
  108. {
  109. continue;
  110. }
  111. return true;
  112. }
  113. return false;
  114. }
  115. }
  116. }