1
0
mirror of synced 2024-12-03 11:37:21 +01:00
Switch-Toolbox/Updater/Program.cs
DanielSvoboda 0d33f4ead1
Updater Fix/Improvements (#606)
* Updater Fix/Improvements

In SetAccessRule():
A call to the Directory.SetAccessControl method was added to apply the changes made to the DirectorySecurity object. Additionally, the function does not handle possible exceptions that may occur during code execution. This version of the function attempts to set the access rule for the specified directory and, if an error occurs during the process, prints an error message on the screen. This helps to identify and fix possible problems that may occur during code execution.

In Install():
The function checks if the destination directory exists before attempting to delete it. Additionally, a destDir variable was created to store the path of the 'destination directory' and a destFile variable to store the path of the 'destination file'. This makes the code a little more readable.

In Application_Idle():
A MessageBox.Show was included to display a dialog box with the message "Updates are available. Do you want to update now?" and two buttons: "Yes" and "No". If the user clicks on the "Yes" button, the UpdateNotifcationClick method is called to start the update process. Otherwise, the dialog box is closed and no action is taken.

The dialog box will only be shown once for each available update, as the UsePrompt variable is set to false after the dialog box is shown for the first time.

when trying to update, updater.exe crashed and presented the following error, as shown in the print below:
unhandled exception: Unable to create a file that already exists.

* Updater Fix/Improvements
2023-05-26 17:07:09 -04:00

206 lines
7.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Octokit;
using System.Net;
using System.IO.Compression;
using System.IO;
using System.Globalization;
using System.Security.AccessControl;
namespace Updater
{
class Program
{
static Release[] releases;
static string execDirectory = "";
static string folderDir = "";
static bool foundRelease = false;
static void Main(string[] args)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
execDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
folderDir = execDirectory;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var client = new GitHubClient(new ProductHeaderValue("ST_UpdateTool"));
GetReleases(client).Wait();
string versionTxt = Path.Combine(execDirectory, "Version.txt");
if (!File.Exists(versionTxt))
File.Create(versionTxt);
string[] versionInfo = File.ReadLines(versionTxt).ToArray();
string ProgramVersion = "";
string CompileDate = "";
string CommitInfo = "";
if (versionInfo.Length >= 3)
{
ProgramVersion = versionInfo[0];
CompileDate = versionInfo[1];
CommitInfo = versionInfo[2];
}
foreach (string arg in args)
{
switch (arg)
{
case "-d":
case "--download":
Download(CompileDate);
break;
case "-i":
case "--install":
Install();
break;
case "-b":
case "--boot":
Boot();
Environment.Exit(0);
break;
case "-e":
case "--exit":
Environment.Exit(0);
break;
}
}
Console.Read();
}
static void Boot()
{
Console.WriteLine("Booting...");
Thread.Sleep(3000);
System.Diagnostics.Process.Start(Path.Combine(folderDir, "Toolbox.exe"));
}
static void Install()
{
Console.WriteLine("Installing...");
foreach (string dir in Directory.GetDirectories("master/"))
{
SetAccessRule(folderDir);
SetAccessRule(dir);
string dirName = new DirectoryInfo(dir).Name;
string destDir = Path.Combine(folderDir, dirName + @"\");
if (!dirName.Equals("Hashes", StringComparison.CurrentCultureIgnoreCase) // Let's keep the users custom hashes in tact
&& Directory.Exists(destDir))
{
Directory.Delete(destDir, true);
}
if (Directory.Exists(destDir))
Directory.Delete(destDir, true);
Directory.Move(dir, destDir);
}
foreach (string file in Directory.GetFiles("master/"))
{
if (file.Contains("Updater.exe") || file.Contains("Updater.exe.config")
|| file.Contains("Updater.pdb") || file.Contains("Octokit.dll"))
continue;
SetAccessRule(file);
SetAccessRule(folderDir);
string destFile = Path.Combine(folderDir, Path.GetFileName(file));
if (File.Exists(destFile))
File.Delete(destFile);
File.Move(file, destFile);
}
}
static void SetAccessRule(string directory)
{
try
{
System.Security.AccessControl.DirectorySecurity sec = System.IO.Directory.GetAccessControl(directory);
FileSystemAccessRule accRule = new FileSystemAccessRule(Environment.UserDomainName + "\\" + Environment.UserName, FileSystemRights.FullControl, AccessControlType.Allow);
sec.AddAccessRule(accRule);
Directory.SetAccessControl(directory, sec);
}
catch (Exception ex)
{
Console.WriteLine($"Failed to set access rule for directory '{directory}': {ex.Message}");
}
}
static void Download(string CompileDate)
{
foreach (Release latest in releases)
{
Console.WriteLine("Checking Update");
if (!foundRelease)
{
if (!latest.Assets[0].UpdatedAt.ToString().Equals(CompileDate))
{
Console.WriteLine("Downloading release...");
bool IsDownloaded = DownloadedProgram(latest);
if (IsDownloaded)
Console.WriteLine("Downloaded update successfully!");
else
Console.WriteLine("Failed to download update!");
}
}
foundRelease = true;
}
}
static bool DownloadedProgram(Release release)
{
return DownloadRelease("master",
release.Assets[0].BrowserDownloadUrl,
release.TagName,
release.Assets[0].UpdatedAt.ToString(),
release.TargetCommitish);
}
static bool DownloadRelease(string downloadName, string url, string ProgramVersion, string CompileDate, string CommitInfo)
{
try
{
using (var webClient = new WebClient())
{
webClient.DownloadFile(url, downloadName + ".zip");
}
if (Directory.Exists(downloadName + "/"))
Directory.Delete(downloadName + "/", true);
ZipFile.ExtractToDirectory(downloadName + ".zip", downloadName + "/");
//Zip not needed anymore
File.Delete(downloadName + ".zip");
string versionTxt = Path.Combine(Path.GetFullPath(downloadName + "/"), "Version.txt");
using (StreamWriter writer = new StreamWriter(versionTxt))
{
writer.WriteLine($"{ProgramVersion}");
writer.WriteLine($"{CompileDate}");
writer.WriteLine($"{CommitInfo}");
}
return true;
}
catch (Exception ex)
{
Console.WriteLine($"Failed to download update! {ex.ToString()}");
return false;
}
}
static async Task GetReleases(GitHubClient client)
{
List<Release> Releases = new List<Release>();
foreach (Release r in await client.Repository.Release.GetAll("KillzXGaming", "Switch-Toolbox"))
Releases.Add(r);
releases = Releases.ToArray();
}
}
}