Skip to content

Commit

Permalink
Merge pull request #208 from Shivansps/mod-install-rework-1
Browse files Browse the repository at this point in the history
Search other mod versions for files instead of download
  • Loading branch information
Shivansps authored Jun 21, 2024
2 parents 96e078e + 71c6e7c commit a7cffae
Show file tree
Hide file tree
Showing 5 changed files with 397 additions and 54 deletions.
81 changes: 81 additions & 0 deletions Knossos.NET/Classes/HardLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.IO;
using System.Runtime.InteropServices;


namespace Knossos.NET.Classes
{
/// <summary>
/// Helper class to create file Hardlinks in Windows, Linux and MacOS
/// </summary>
public static class HardLink
{
/// <summary>
/// Creates a file HardLink
/// Cross-Platform
/// Note: It only works with files, not folders
/// </summary>
/// <param name="origin"></param>
/// <param name="destination"></param>
/// <returns>true if successfull, false otherwise</returns>
public static bool CreateFileLink(string origin, string destination)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return CreateWindowsLink(origin, destination);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return CreateUnixLink(origin, destination);
}
return false;
}

private static bool CreateWindowsLink(string origin, string destination)
{
try
{
if (CreateHardLinkWindows(destination, origin))
return true;

//Fail, throw exception
int hR = Marshal.GetHRForLastWin32Error();
var inner = Marshal.GetExceptionForHR(hR);
throw new IOException("Error while creating hardlink: " + inner);
}
catch (Exception ex)
{
//Log exception
Log.Add(Log.LogSeverity.Error, "Hardlink.CreateWindowsLink()", ex);
}
return false;
}

private static bool CreateUnixLink(string origin, string destination)
{
try
{
int ret = CreateHardLinkUnix(origin, destination);
if (ret == 0)
return true;

//Fail, throw exception
int hR = Marshal.GetHRForLastWin32Error();
var inner = Marshal.GetExceptionForHR(hR);
throw new IOException("Error while creating hardlink: " + inner);
}
catch (Exception ex)
{
//Log exception
Log.Add(Log.LogSeverity.Error, "Hardlink.CreateUnixLink()", ex);
}
return false;
}

[DllImport("kernel32.dll", EntryPoint = "CreateHardLink", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern bool CreateHardLinkWindows(string dest, string org, int flags = 0);

[DllImport("libc", EntryPoint = "link", CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int CreateHardLinkUnix(string org, string dest);
}
}
4 changes: 2 additions & 2 deletions Knossos.NET/ViewModels/TaskViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ await Dispatcher.UIThread.InvokeAsync(async () =>
/// <param name="mod"></param>
/// <param name="reinstallPkgs"></param>
/// <param name="manualCompress"></param>
public async void InstallMod(Mod mod, List<ModPackage>? reinstallPkgs = null, bool manualCompress = false, bool cleanupOldVersions = false)
public async void InstallMod(Mod mod, List<ModPackage>? reinstallPkgs = null, bool manualCompress = false, bool cleanupOldVersions = false, bool cleanInstall = false, bool allowHardlinks = true)
{
if (Knossos.GetKnossosLibraryPath() == null)
{
Expand All @@ -325,7 +325,7 @@ await Dispatcher.UIThread.InvokeAsync(async () =>
TaskList.Add(newTask);
taskQueue.Enqueue(newTask);
});
await newTask.InstallMod(mod, cancelSource, reinstallPkgs, manualCompress, cleanupOldVersions).ConfigureAwait(false);
await newTask.InstallMod(mod, cancelSource, reinstallPkgs, manualCompress, cleanupOldVersions, cleanInstall, allowHardlinks).ConfigureAwait(false);
}
}
}
Expand Down
Loading

0 comments on commit a7cffae

Please sign in to comment.