Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added error when Nitrox is not able to write/read files #1564

Merged
merged 1 commit into from
Aug 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions NitroxLauncher/LauncherLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using NitroxLauncher.Events;
using NitroxLauncher.Pages;
using NitroxLauncher.Patching;
using NitroxModel;
using NitroxModel.Discovery;
using NitroxModel.Helper;
using NitroxModel.Logger;
using NitroxModel.OS;

namespace NitroxLauncher
{
Expand Down Expand Up @@ -128,6 +130,18 @@ public async Task<string> SetTargetedSubnauticaPath(string path)
lastFindSubnauticaTask = Task.Factory.StartNew(() =>
{
PirateDetection.TriggerOnDirectory(path);

// TODO: Move this if block to another place where Nitrox installation is verified (will be clear with new Nitrox Launcher design).
if (!FileSystem.Instance.SetFullAccessToCurrentUser(Directory.GetCurrentDirectory()) || !FileSystem.Instance.SetFullAccessToCurrentUser(path))
{
Dispatcher.CurrentDispatcher.BeginInvoke(() =>
{
MessageBox.Show(Application.Current.MainWindow!, "Restart Nitrox Launcher as admin to allow Nitrox to change permissions as needed. This is only needed once. Nitrox will close after this message.", "Required file permission error", MessageBoxButton.OK,
MessageBoxImage.Error);
Environment.Exit(1);
}, DispatcherPriority.ApplicationIdle);
}

try
{
File.WriteAllText("path.txt", path);
Expand Down
2 changes: 1 addition & 1 deletion NitroxLauncher/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public MainWindow()
{
LauncherLogic.Instance.NavigateTo<OptionPage>();
}

logic.CheckNitroxVersion();
}, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.FromCurrentSynchronizationContext());
}
Expand Down
8 changes: 3 additions & 5 deletions NitroxModel/OS/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace NitroxModel.OS
{
public class FileSystem
public abstract class FileSystem
{
private static readonly Lazy<FileSystem> instance = new(() => Environment.OSVersion.Platform switch
{
Expand All @@ -26,10 +26,6 @@ public class FileSystem
public static FileSystem Instance => instance.Value;
public virtual string TextEditor => throw new NotSupportedException();

protected FileSystem()
{
}

public virtual IEnumerable<string> GetDefaultPrograms(string file) => throw new NotSupportedException();

/// <summary>
Expand Down Expand Up @@ -248,5 +244,7 @@ public bool ReplaceFile(string source, string target)
}
return true;
}

public abstract bool SetFullAccessToCurrentUser(string directory);
}
}
5 changes: 5 additions & 0 deletions NitroxModel/OS/MacOS/MacFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ public override IEnumerable<string> GetDefaultPrograms(string file)
{
yield return "open";
}

public override bool SetFullAccessToCurrentUser(string directory)
{
throw new System.NotImplementedException();
}
}
}
5 changes: 5 additions & 0 deletions NitroxModel/OS/Unix/UnixFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ public override IEnumerable<string> GetDefaultPrograms(string file)
{
yield return "xdg-open";
}

public override bool SetFullAccessToCurrentUser(string directory)
{
throw new System.NotImplementedException();
}
}
}
29 changes: 28 additions & 1 deletion NitroxModel/OS/Windows/WinFileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
using Microsoft.Win32;

namespace NitroxModel.OS.Windows
Expand Down Expand Up @@ -60,5 +63,29 @@ string SearchExecutableInSameDirectory(string path)
yield return fullPath;
}
}

/// <summary>
/// Adds full access flag to the directory (and sub files/directories) for the current user.
/// </summary>
/// <param name="directory"></param>
/// <returns>True if set, false if program is not allowed to change permissions.</returns>
public override bool SetFullAccessToCurrentUser(string directory)
{
try
{
string identity = WindowsIdentity.GetCurrent().Name;

DirectorySecurity flags = Directory.GetAccessControl(directory);
flags.AddAccessRule(new(identity, FileSystemRights.FullControl, InheritanceFlags.None, PropagationFlags.InheritOnly, AccessControlType.Allow));
flags.AddAccessRule(new (identity, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
flags.AddAccessRule(new (identity, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
Directory.SetAccessControl(directory, flags);
return true;
}
catch (UnauthorizedAccessException)
{
return false;
}
}
}
}