Utility.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. /// Converts a file path into a UnityEngine.WWW format.
  30. /// </summary>
  31. /// <param name="path">The file path to convert.</param>
  32. /// <returns>A converted file path.</returns>
  33. public static string ConvertToWWWFormat(string path)
  34. {
  35. return $"file://{path.Replace('\\', '/')}";
  36. }
  37. /// <summary>
  38. /// Indicates whether a specified string is null, empty, or consists only of white-space characters.
  39. /// </summary>
  40. /// <param name="self">The string to test.</param>
  41. /// <returns>True if the value parameter is null or empty, or if value consists exclusively of white-space characters.</returns>
  42. public static bool IsNullOrWhiteSpace(this string self)
  43. {
  44. return self == null || self.Trim().Length == 0;
  45. }
  46. public static IEnumerable<TNode> TopologicalSort<TNode>(IEnumerable<TNode> nodes, Func<TNode, IEnumerable<TNode>> dependencySelector)
  47. {
  48. List<TNode> sorted_list = new List<TNode>();
  49. HashSet<TNode> visited = new HashSet<TNode>();
  50. HashSet<TNode> sorted = new HashSet<TNode>();
  51. foreach (TNode input in nodes)
  52. Visit(input);
  53. return sorted_list;
  54. void Visit(TNode node)
  55. {
  56. if (visited.Contains(node))
  57. {
  58. if (!sorted.Contains(node))
  59. throw new Exception("Cyclic Dependency");
  60. }
  61. else
  62. {
  63. visited.Add(node);
  64. foreach (var dep in dependencySelector(node))
  65. Visit(dep);
  66. sorted.Add(node);
  67. sorted_list.Add(node);
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// Try to resolve and load the given assembly DLL.
  73. /// </summary>
  74. /// <param name="assemblyName">Name of the assembly, of the type <see cref="AssemblyName" />.</param>
  75. /// <param name="directory">Directory to search the assembly from.</param>
  76. /// <param name="assembly">The loaded assembly.</param>
  77. /// <returns>True, if the assembly was found and loaded. Otherwise, false.</returns>
  78. public static bool TryResolveDllAssembly(AssemblyName assemblyName, string directory, out Assembly assembly)
  79. {
  80. assembly = null;
  81. string path = Path.Combine(directory, $"{assemblyName.Name}.dll");
  82. if (!File.Exists(path))
  83. return false;
  84. try
  85. {
  86. assembly = Assembly.LoadFile(path);
  87. }
  88. catch (Exception)
  89. {
  90. return false;
  91. }
  92. return true;
  93. }
  94. }
  95. }