Utility.cs 4.3 KB

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