Utility.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /// The directory that the Koikatsu .exe is being run from.
  15. /// </summary>
  16. public static string ExecutingDirectory => Path.GetDirectoryName(Environment.CommandLine);
  17. /// <summary>
  18. /// The path that the plugins folder is located.
  19. /// </summary>
  20. public static string PluginsDirectory => Path.Combine(ExecutingDirectory, "BepInEx");
  21. /// <summary>
  22. /// Combines multiple paths together, as the specfic method is not availble in .NET 3.5.
  23. /// </summary>
  24. /// <param name="parts">The multiple paths to combine together.</param>
  25. /// <returns>A combined path.</returns>
  26. public static string CombinePaths(params string[] parts) => parts.Aggregate(Path.Combine);
  27. /// <summary>
  28. /// Converts a file path into a UnityEngine.WWW format.
  29. /// </summary>
  30. /// <param name="path">The file path to convert.</param>
  31. /// <returns>A converted file path.</returns>
  32. public static string ConvertToWWWFormat(string path)
  33. {
  34. return $"file://{path.Replace('\\', '/')}";
  35. }
  36. /// <summary>
  37. /// Indicates whether a specified string is null, empty, or consists only of white-space characters.
  38. /// </summary>
  39. /// <param name="self">The string to test.</param>
  40. /// <returns>True if the value parameter is null or empty, or if value consists exclusively of white-space characters.</returns>
  41. public static bool IsNullOrWhiteSpace(this string self)
  42. {
  43. return self == null || self.Trim().Length == 0;
  44. }
  45. public static IEnumerable<TNode> TopologicalSort<TNode>(IEnumerable<TNode> nodes, Func<TNode, IEnumerable<TNode>> dependencySelector)
  46. {
  47. List<TNode> sorted_list = new List<TNode>();
  48. HashSet<TNode> visited = new HashSet<TNode>();
  49. HashSet<TNode> sorted = new HashSet<TNode>();
  50. foreach (TNode input in nodes)
  51. Visit(input);
  52. return sorted_list;
  53. void Visit(TNode node)
  54. {
  55. if (visited.Contains(node))
  56. {
  57. if (!sorted.Contains(node))
  58. throw new Exception("Cyclic Dependency");
  59. }
  60. else
  61. {
  62. visited.Add(node);
  63. foreach (var dep in dependencySelector(node))
  64. Visit(dep);
  65. sorted.Add(node);
  66. sorted_list.Add(node);
  67. }
  68. }
  69. }
  70. /// <summary>
  71. /// Try to resolve and load the given assembly DLL.
  72. /// </summary>
  73. /// <param name="name">Name of the assembly. Follows the format of <see cref="AssemblyName" />.</param>
  74. /// <param name="directory">Directory to search the assembly from.</param>
  75. /// <param name="assembly">The loaded assembly.</param>
  76. /// <returns>True, if the assembly was found and loaded. Otherwise, false.</returns>
  77. public static bool TryResolveDllAssembly(string name, string directory, out Assembly assembly)
  78. {
  79. assembly = null;
  80. string path = Path.Combine(directory, $"{new AssemblyName(name).Name}.dll");
  81. if (!File.Exists(path))
  82. return false;
  83. try
  84. {
  85. assembly = Assembly.LoadFile(path);
  86. }
  87. catch (Exception)
  88. {
  89. return false;
  90. }
  91. return true;
  92. }
  93. }
  94. }