Skip to content

Commit

Permalink
Path detection fix (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
zabszk authored Dec 31, 2022
1 parent a9b5e80 commit 860c222
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
21 changes: 18 additions & 3 deletions Core/LocalAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,27 @@ internal async Task Start(string[] args)

HeartbeatStopwatch.Reset();

if (!PathManager.IsLinuxCorrectPath && !args.Contains("--skipHomeCheck", StringComparer.Ordinal))
if (!PathManager.CorrectPathFound && !args.Contains("--skipHomeCheck", StringComparer.Ordinal))
{
ConsoleUtil.WriteLine($"Welcome to LocalAdmin version {VersionString}!", ConsoleColor.Red);
ConsoleUtil.WriteLine("Can't obtain a valid user home directory path!", ConsoleColor.Red);
ConsoleUtil.WriteLine("Make sure to export a valid path, for example using command: export HOME=/home/username-here", ConsoleColor.Red);
ConsoleUtil.WriteLine("You may want to add that command to the top of ~/.bashrc file and restart the terminal session to avoid having to enter that command every time.", ConsoleColor.Red);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
ConsoleUtil.WriteLine("Make sure to export a valid path, for example using command: export HOME=/home/username-here", ConsoleColor.Red);
ConsoleUtil.WriteLine("You may want to add that command to the top of ~/.bashrc file and restart the terminal session to avoid having to enter that command every time.", ConsoleColor.Red);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
ConsoleUtil.WriteLine("Such error should never occur on Windows.", ConsoleColor.Red);
ConsoleUtil.WriteLine("Open issue on the LocalAdmin GitHub repository (https://github.com/northwood-studios/LocalAdmin-V2/issues) or contact our technical support!", ConsoleColor.Red);
}
else
{
ConsoleUtil.WriteLine("You are running LocalAdmin on an unsupported platform!", ConsoleColor.Red);
throw new PlatformNotSupportedException();
}

ConsoleUtil.WriteLine("To skip this check, use --skipHomeCheck argument.", ConsoleColor.Red);

Terminate();
Expand Down
50 changes: 44 additions & 6 deletions IO/PathManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,62 @@ internal static class PathManager
internal static readonly string ConfigPath;
internal static readonly string InternalJsonDataPath;

internal static bool CorrectPathFound { get; private set; }

static PathManager()
{
ProcessHostPolicy();

GameUserDataRoot = _configDirOverride
? "AppData" + Path.DirectorySeparatorChar
: Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar +
"SCP Secret Laboratory" + Path.DirectorySeparatorChar;
: GetSpecialFolderPath() + "SCP Secret Laboratory" + Path.DirectorySeparatorChar;

ConfigPath = $"{GameUserDataRoot}config{Path.DirectorySeparatorChar}";

InternalJsonDataPath = ConfigPath + "localadmin_internal_data.json";
}

internal static bool IsLinuxCorrectPath => !RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
!string.IsNullOrWhiteSpace(
Environment.GetFolderPath(Environment.SpecialFolder
.ApplicationData));
private static string GetSpecialFolderPath()
{
try
{
var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

if (!string.IsNullOrWhiteSpace(path))
{
CorrectPathFound = true;
return path + Path.DirectorySeparatorChar;
}

path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

if (!string.IsNullOrWhiteSpace(path))
{
CorrectPathFound = true;

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
return path + Path.DirectorySeparatorChar + ".config" + Path.DirectorySeparatorChar;

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return path + Path.DirectorySeparatorChar + "AppData" + Path.DirectorySeparatorChar + "Roaming" + Path.DirectorySeparatorChar;

ConsoleUtil.WriteLine("Failed to get special folder path - unsupported platform!", ConsoleColor.Red);
throw new PlatformNotSupportedException();
}

CorrectPathFound = false;
ConsoleUtil.WriteLine($"Failed to get special folder path - it's always null or empty!", ConsoleColor.Red);

return string.Empty;
}
catch (Exception e)
{
CorrectPathFound = false;
ConsoleUtil.WriteLine($"Failed to get special folder path! Exception: {e.Message}", ConsoleColor.Red);

throw;
}
}

private static void ProcessHostPolicy()
{
Expand Down

0 comments on commit 860c222

Please sign in to comment.