2021-09-27 12:42:46 +00:00
|
|
|
using System;
|
2022-05-23 19:54:46 +00:00
|
|
|
using System.Diagnostics;
|
2021-10-08 13:16:51 +00:00
|
|
|
using System.IO;
|
2022-09-24 21:13:29 +00:00
|
|
|
using System.Linq;
|
2021-09-27 12:42:46 +00:00
|
|
|
using System.Reflection;
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
namespace Wabbajack.Paths.IO;
|
|
|
|
|
|
|
|
public static class KnownFolders
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2022-05-23 19:54:46 +00:00
|
|
|
public static AbsolutePath EntryPoint
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2022-12-28 16:21:58 +00:00
|
|
|
return AppDomain.CurrentDomain.BaseDirectory.ToAbsolutePath();
|
2022-06-24 22:16:40 +00:00
|
|
|
var result = Process.GetCurrentProcess().MainModule?.FileName?.ToAbsolutePath() ?? default;
|
|
|
|
|
2022-09-24 21:13:29 +00:00
|
|
|
if (result != default &&
|
|
|
|
result.PathParts.Any(p => p.Equals("TestRunner", StringComparison.CurrentCultureIgnoreCase)))
|
|
|
|
{
|
|
|
|
return Assembly.GetExecutingAssembly().Location.ToAbsolutePath().Parent;
|
|
|
|
}
|
|
|
|
|
2022-06-24 22:16:40 +00:00
|
|
|
|
2022-09-24 21:13:29 +00:00
|
|
|
if ((result != default && result.Depth > 1 && result.FileName == "dotnet".ToRelativePath())
|
|
|
|
|| Assembly.GetEntryAssembly() != null)
|
2022-06-10 11:36:45 +00:00
|
|
|
{
|
2022-06-24 22:16:40 +00:00
|
|
|
result = Assembly.GetEntryAssembly()!.Location.ToAbsolutePath();
|
2022-06-10 11:36:45 +00:00
|
|
|
}
|
|
|
|
|
2022-06-24 22:16:40 +00:00
|
|
|
return result == default ? Environment.CurrentDirectory.ToAbsolutePath() : result.Parent;
|
2022-05-23 19:54:46 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2022-10-01 13:02:47 +00:00
|
|
|
public static AbsolutePath LauncherAwarePath
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
var path = EntryPoint;
|
|
|
|
if (path.Depth <= 2) return path;
|
|
|
|
if (Version.TryParse(path.Parent.FileName.ToString(), out var version) && version > new Version("1.0.0.0"))
|
|
|
|
return path.Parent;
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-16 21:22:56 +00:00
|
|
|
public static bool IsInSpecialFolder(this AbsolutePath candidate)
|
|
|
|
{
|
|
|
|
foreach (var val in Enum.GetValues(typeof(Environment.SpecialFolder)))
|
|
|
|
{
|
|
|
|
AbsolutePath specialPath = Environment.GetFolderPath((Environment.SpecialFolder)val).ToAbsolutePath();
|
|
|
|
if ((candidate.ToString().Length > 0 && candidate == specialPath)
|
|
|
|
|| KnownFolders.IsSubDirectoryOf(candidate.ToString(), specialPath.ToString()))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
public static bool IsSubDirectoryOf(this string candidate, string other)
|
|
|
|
{
|
|
|
|
if (candidate.Length == 0) return false;
|
|
|
|
if (other.Length == 0) return false;
|
|
|
|
var isChild = false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var candidateInfo = new DirectoryInfo(candidate);
|
|
|
|
var otherInfo = new DirectoryInfo(other);
|
|
|
|
|
|
|
|
while (candidateInfo.Parent != null)
|
|
|
|
{
|
|
|
|
if (candidateInfo.Parent.FullName == otherInfo.FullName)
|
|
|
|
{
|
|
|
|
isChild = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else candidateInfo = candidateInfo.Parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
var message = String.Format("Unable to check directories {0} and {1}: {2}", candidate, other, error);
|
|
|
|
Trace.WriteLine(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return isChild;
|
|
|
|
}
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public static AbsolutePath AppDataLocal =>
|
|
|
|
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).ToAbsolutePath();
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-11-10 23:13:02 +00:00
|
|
|
public static AbsolutePath WindowsSystem32 => Environment.GetFolderPath(Environment.SpecialFolder.System).ToAbsolutePath();
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public static AbsolutePath WabbajackAppLocal => AppDataLocal.Combine("Wabbajack");
|
|
|
|
public static AbsolutePath CurrentDirectory => Directory.GetCurrentDirectory().ToAbsolutePath();
|
2021-11-10 23:13:02 +00:00
|
|
|
public static AbsolutePath Windows => Environment.GetFolderPath(Environment.SpecialFolder.Windows).ToAbsolutePath();
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|