Utility.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. namespace BepInEx.Common
  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 specfic method is not availble 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.Trim().Length == 0;
  46. }
  47. public static IEnumerable<TNode> TopologicalSort<TNode>(IEnumerable<TNode> nodes, Func<TNode, IEnumerable<TNode>> dependencySelector)
  48. {
  49. List<TNode> nodeQueue = new List<TNode>(nodes);
  50. List<TNode> sorted = new List<TNode>();
  51. while (nodeQueue.Count > 0)
  52. {
  53. List<TNode> nextBatch = nodeQueue.Where(x => !dependencySelector(x).Except(sorted).Any()).ToList();
  54. if (!nextBatch.Any())
  55. throw new Exception("Cyclic Dependency:\r\n" +
  56. nodeQueue.Select(x => x.ToString()).Aggregate((a , b) => $"{a}\r\n{b}"));
  57. sorted.AddRange(nextBatch);
  58. foreach (TNode item in nextBatch)
  59. nodeQueue.Remove(item);
  60. }
  61. return sorted;
  62. }
  63. /// <summary>
  64. /// Try to resolve and load the given assembly DLL.
  65. /// </summary>
  66. /// <param name="assemblyName">Name of the assembly, of the type <see cref="AssemblyName" />.</param>
  67. /// <param name="directory">Directory to search the assembly from.</param>
  68. /// <param name="assembly">The loaded assembly.</param>
  69. /// <returns>True, if the assembly was found and loaded. Otherwise, false.</returns>
  70. public static bool TryResolveDllAssembly(AssemblyName assemblyName, string directory, out Assembly assembly)
  71. {
  72. assembly = null;
  73. string path = Path.Combine(directory, $"{assemblyName.Name}.dll");
  74. if (!File.Exists(path))
  75. return false;
  76. try
  77. {
  78. assembly = Assembly.LoadFile(path);
  79. }
  80. catch (Exception)
  81. {
  82. return false;
  83. }
  84. return true;
  85. }
  86. }
  87. }