Utility.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. namespace BepInEx.Common
  6. {
  7. /// <summary>
  8. /// Generic helper properties and methods.
  9. /// </summary>
  10. public static class Utility
  11. {
  12. /// <summary>
  13. /// The directory that the Koikatsu .exe is being run from.
  14. /// </summary>
  15. public static string ExecutingDirectory => Path.GetDirectoryName(Environment.CommandLine);
  16. /// <summary>
  17. /// The path that the plugins folder is located.
  18. /// </summary>
  19. public static string PluginsDirectory => Path.Combine(ExecutingDirectory, "BepInEx");
  20. /// <summary>
  21. /// Combines multiple paths together, as the specfic method is not availble in .NET 3.5.
  22. /// </summary>
  23. /// <param name="parts">The multiple paths to combine together.</param>
  24. /// <returns>A combined path.</returns>
  25. public static string CombinePaths(params string[] parts) => parts.Aggregate(Path.Combine);
  26. /// <summary>
  27. /// Converts a file path into a UnityEngine.WWW format.
  28. /// </summary>
  29. /// <param name="path">The file path to convert.</param>
  30. /// <returns>A converted file path.</returns>
  31. public static string ConvertToWWWFormat(string path)
  32. {
  33. return $"file://{path.Replace('\\', '/')}";
  34. }
  35. /// <summary>
  36. /// Indicates whether a specified string is null, empty, or consists only of white-space characters.
  37. /// </summary>
  38. /// <param name="self">The string to test.</param>
  39. /// <returns>True if the value parameter is null or empty, or if value consists exclusively of white-space characters.</returns>
  40. public static bool IsNullOrWhiteSpace(this string self)
  41. {
  42. return self == null || self.Trim().Length == 0;
  43. }
  44. public static IEnumerable<TNode> TopologicalSort<TNode>(IEnumerable<TNode> nodes, Func<TNode, IEnumerable<TNode>> dependencySelector)
  45. {
  46. List<TNode> sorted_list = new List<TNode>();
  47. HashSet<TNode> visited = new HashSet<TNode>();
  48. HashSet<TNode> sorted = new HashSet<TNode>();
  49. foreach (TNode input in nodes)
  50. Visit(input);
  51. return sorted_list;
  52. void Visit(TNode node)
  53. {
  54. if (visited.Contains(node))
  55. {
  56. if (!sorted.Contains(node))
  57. throw new Exception("Cyclic Dependency");
  58. }
  59. else
  60. {
  61. visited.Add(node);
  62. foreach (var dep in dependencySelector(node))
  63. Visit(dep);
  64. sorted.Add(node);
  65. sorted_list.Add(node);
  66. }
  67. }
  68. }
  69. }
  70. }