123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Reflection.Emit;
- using Mono.Cecil;
- using MonoMod.Utils;
- namespace BepInEx
- {
-
-
-
- public static class Utility
- {
- private static bool? sreEnabled;
-
-
-
- public static bool CLRSupportsDynamicAssemblies => CheckSRE();
- private static bool CheckSRE()
- {
- if (sreEnabled.HasValue)
- return sreEnabled.Value;
-
- try
- {
-
- _ = new CustomAttributeBuilder(null, new object[0]);
- }
- catch (PlatformNotSupportedException)
- {
- sreEnabled = false;
- return sreEnabled.Value;
- }
- catch (ArgumentNullException)
- {
-
- }
- sreEnabled = true;
- return sreEnabled.Value;
- }
-
- static Utility()
- {
- CheckPlatform();
- }
-
-
-
-
-
-
- public static bool TryDo(Action action, out Exception exception)
- {
- exception = null;
- try
- {
- action();
- return true;
- }
- catch (Exception e)
- {
- exception = e;
- return false;
- }
- }
-
-
-
-
-
- public static string CombinePaths(params string[] parts) => parts.Aggregate(Path.Combine);
-
-
-
-
-
-
- public static string ParentDirectory(string path, int levels = 1)
- {
- for (int i = 0; i < levels; i++)
- path = Path.GetDirectoryName(path);
- return path;
- }
-
-
-
-
-
-
- public static bool SafeParseBool(string input, bool defaultValue = false)
- {
- return Boolean.TryParse(input, out bool result) ? result : defaultValue;
- }
-
-
-
-
-
- public static string ConvertToWWWFormat(string path)
- {
- return $"file://{path.Replace('\\', '/')}";
- }
-
-
-
-
-
- public static bool IsNullOrWhiteSpace(this string self)
- {
- return self == null || self.All(Char.IsWhiteSpace);
- }
-
-
-
-
-
-
-
-
- public static IEnumerable<TNode> TopologicalSort<TNode>(IEnumerable<TNode> nodes, Func<TNode, IEnumerable<TNode>> dependencySelector)
- {
- List<TNode> sorted_list = new List<TNode>();
- HashSet<TNode> visited = new HashSet<TNode>();
- HashSet<TNode> sorted = new HashSet<TNode>();
- foreach (TNode input in nodes)
- {
- Stack<TNode> currentStack = new Stack<TNode>();
- if (!Visit(input, currentStack))
- {
- throw new Exception("Cyclic Dependency:\r\n" + currentStack.Select(x => $" - {x}")
- .Aggregate((a, b) => $"{a}\r\n{b}"));
- }
- }
- return sorted_list;
- bool Visit(TNode node, Stack<TNode> stack)
- {
- if (visited.Contains(node))
- {
- if (!sorted.Contains(node))
- {
- return false;
- }
- }
- else
- {
- visited.Add(node);
- stack.Push(node);
- foreach (var dep in dependencySelector(node))
- if (!Visit(dep, stack))
- return false;
- sorted.Add(node);
- sorted_list.Add(node);
- stack.Pop();
- }
- return true;
- }
- }
- private static bool TryResolveDllAssembly<T>(AssemblyName assemblyName, string directory, Func<string, T> loader, out T assembly) where T : class
- {
- assembly = null;
- var potentialDirectories = new List<string> { directory };
- potentialDirectories.AddRange(Directory.GetDirectories(directory, "*", SearchOption.AllDirectories));
- foreach (string subDirectory in potentialDirectories)
- {
- string path = Path.Combine(subDirectory, $"{assemblyName.Name}.dll");
- if (!File.Exists(path))
- continue;
- try
- {
- assembly = loader(path);
- }
- catch (Exception)
- {
- continue;
- }
- return true;
- }
- return false;
- }
-
-
-
-
-
-
- public static bool IsSubtypeOf(this TypeDefinition self, Type td)
- {
- if (self.FullName == td.FullName)
- return true;
- return self.FullName != "System.Object" && (self.BaseType?.Resolve()?.IsSubtypeOf(td) ?? false);
- }
-
-
-
-
-
-
-
- public static bool TryResolveDllAssembly(AssemblyName assemblyName, string directory, out Assembly assembly)
- {
- return TryResolveDllAssembly(assemblyName, directory, Assembly.LoadFile, out assembly);
- }
-
-
-
-
-
-
-
-
- public static bool TryResolveDllAssembly(AssemblyName assemblyName, string directory, ReaderParameters readerParameters, out AssemblyDefinition assembly)
- {
- return TryResolveDllAssembly(assemblyName, directory, s => AssemblyDefinition.ReadAssembly(s, readerParameters), out assembly);
- }
-
-
-
-
-
-
-
-
-
- public static bool TryOpenFileStream(string path, FileMode mode, out FileStream fileStream, FileAccess access = FileAccess.ReadWrite, FileShare share = FileShare.Read)
- {
- try
- {
- fileStream = new FileStream(path, mode, access, share);
- return true;
- }
- catch (IOException)
- {
- fileStream = null;
- return false;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
- public static bool TryParseAssemblyName(string fullName, out AssemblyName assemblyName)
- {
- try
- {
- assemblyName = new AssemblyName(fullName);
- return true;
- }
- catch (Exception e)
- {
- File.AppendAllText("tryparseerr.log", $"Failed to parse {fullName}: {e}");
- assemblyName = null;
- return false;
- }
- }
-
- private static void CheckPlatform()
- {
- var pPlatform = typeof(Environment).GetProperty("Platform", BindingFlags.NonPublic | BindingFlags.Static);
- string platId = pPlatform != null ? pPlatform.GetValue(null, new object[0]).ToString() : Environment.OSVersion.Platform.ToString();
- platId = platId.ToLowerInvariant();
- var cur = Platform.Unknown;
- if (platId.Contains("win"))
- cur = Platform.Windows;
- else if (platId.Contains("mac") || platId.Contains("osx"))
- cur = Platform.MacOS;
- else if (platId.Contains("lin") || platId.Contains("unix"))
- cur = Platform.Linux;
- CurrentOs = cur;
- }
-
-
-
- public static Platform CurrentOs { get; private set; }
- }
- }
|