mirror of
https://github.com/rmcrackan/Libation.git
synced 2026-05-09 16:16:13 -04:00
74 lines
2.1 KiB
C#
74 lines
2.1 KiB
C#
using Dinah.Core;
|
|
using LibationFileManager;
|
|
using SixLabors.ImageSharp;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace WindowsConfigApp;
|
|
|
|
internal class WinInterop : IInteropFunctions
|
|
{
|
|
public WinInterop() { }
|
|
public WinInterop(params object[] values) { }
|
|
public void SetFolderIcon(string image, string directory)
|
|
{
|
|
using var img = Image.Load(image);
|
|
var icon = img.ToIcon();
|
|
new DirectoryInfo(directory)?.SetIcon(icon, "Music");
|
|
}
|
|
|
|
public void SetFolderIcon(byte[] imageJpegBytes, string directory)
|
|
{
|
|
using var img = Image.Load(new MemoryStream(imageJpegBytes, writable: false));
|
|
var icon = img.ToIcon();
|
|
new DirectoryInfo(directory)?.SetIcon(icon, "Music");
|
|
}
|
|
|
|
public void DeleteFolderIcon(string directory)
|
|
=> new DirectoryInfo(directory)?.DeleteIcon();
|
|
|
|
public bool CanUpgrade => true;
|
|
|
|
public string ReleaseIdString => AppScaffolding.LibationScaffolding.ReleaseIdentifier.ToString();
|
|
|
|
public async Task InstallUpgradeAsync(string upgradeBundle)
|
|
{
|
|
const string ExtractorExeName = "ZipExtractor.exe";
|
|
var thisExe = Environment.ProcessPath;
|
|
var thisDir = Path.GetDirectoryName(thisExe);
|
|
if (!File.Exists(thisExe) || !Directory.Exists(thisDir))
|
|
return;
|
|
|
|
var zipExtractor = Path.Combine(Path.GetTempPath(), ExtractorExeName);
|
|
|
|
File.Copy(Path.Combine(thisDir, ExtractorExeName), zipExtractor, overwrite: true);
|
|
|
|
var proc = RunAsRoot(zipExtractor,
|
|
$"--input {upgradeBundle.SurroundWithQuotes()} " +
|
|
$"--output {thisDir.SurroundWithQuotes()} " +
|
|
$"--executable {thisExe.SurroundWithQuotes()}");
|
|
if (proc is null)
|
|
throw new InvalidOperationException("Could not start the elevated upgrade process.");
|
|
await proc.WaitForExitAsync();
|
|
if (proc.ExitCode != 0)
|
|
throw new InvalidOperationException($"ZipExtractor exited with code {proc.ExitCode}.");
|
|
}
|
|
|
|
public Process? RunAsRoot(string exe, string args)
|
|
{
|
|
var psi = new ProcessStartInfo()
|
|
{
|
|
FileName = exe,
|
|
UseShellExecute = true,
|
|
Verb = "runas",
|
|
WindowStyle = ProcessWindowStyle.Normal,
|
|
CreateNoWindow = true,
|
|
Arguments = args
|
|
};
|
|
|
|
return Process.Start(psi);
|
|
}
|
|
}
|