Skip to content

Commit

Permalink
Expand upon fix to see if the mod folder is writeable
Browse files Browse the repository at this point in the history
  • Loading branch information
bcssov committed Jan 7, 2024
1 parent 3a9b3f6 commit 6449b32
Showing 1 changed file with 55 additions and 10 deletions.
65 changes: 55 additions & 10 deletions src/IronyModManager.IO/Mods/ModWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Threading.Tasks;
using AutoMapper;
using IronyModManager.IO.Common;
Expand Down Expand Up @@ -136,25 +137,48 @@ public async Task<bool> ApplyModsAsync(ModWriterParameters parameters)
public Task<bool> CanWriteToModDirectoryAsync(ModWriterParameters parameters)
{
var fullPath = Path.Combine(parameters.RootDirectory ?? string.Empty, parameters.Path ?? string.Empty);

// If drive doesn't exist it should not return a value
var size = driveInfoProvider.GetFreeSpace(fullPath);
if (size.HasValue)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
try
// Partially prepare handling for #478
var parentFolder = parameters.RootDirectory ?? parameters.Path;
var integrityCheck = Path.Combine(parentFolder, IntegrityCheck);
if (!File.Exists(integrityCheck))
{
// Partially prepare handling for #478
var integrityCheck = Path.Combine(parameters.RootDirectory ?? parameters.Path, IntegrityCheck);
if (!File.Exists(integrityCheck))
if (!Directory.Exists(parentFolder))
{
File.Create(integrityCheck);
// If the base directory does not exist, bail out and fail
var el = parentFolder.StandardizeDirectorySeparator().Split(Path.DirectorySeparatorChar);
if (el.Length <= 1)
{
// Real genious you picked C, D or whatever drive
return Task.FromResult(false);
}
var root = Path.Combine(el[0], el[1]);
if (!Directory.Exists(root) && !IsAdmin())
{
return Task.FromResult(false);
}
}
else
{
try
{
if (!File.Exists(integrityCheck))
{
File.Create(integrityCheck);
}
}
catch
{
return Task.FromResult(false);
}
return Task.FromResult(true);
}
return Task.FromResult(true);
}
catch (UnauthorizedAccessException)
{
return Task.FromResult(false);
}
}
return Task.FromResult(true);
Expand Down Expand Up @@ -508,6 +532,27 @@ static async Task serializeJsonDescriptorMod(IMod content, StreamWriter sw)
return true;
}

/// <summary>
/// Determines whether this instance is admin.
/// </summary>
/// <returns>bool.</returns>
private bool IsAdmin()
{
try
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
using var identity = WindowsIdentity.GetCurrent();
return new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator);
}
return false;
}
catch
{
return false;
}
}

#endregion Methods

#region Classes
Expand Down

0 comments on commit 6449b32

Please sign in to comment.