Add automatic updating (finally! - #38/#67)

This commit is contained in:
pizzaboxer 2023-01-08 18:52:01 +00:00
parent 445018f1b9
commit 2d81c357f6
5 changed files with 104 additions and 89 deletions

View File

@ -109,13 +109,16 @@ namespace Bloxstrap
return;
}
#if !DEBUG
if (!Program.IsFirstRun && Program.Settings.CheckForUpdates)
await CheckForUpdates();
#endif
await CheckLatestVersion();
// if bloxstrap is installing for the first time but is running, prompt to close roblox
// if roblox needs updating but is running, ignore update for now
#if !DEBUG
if (!Directory.Exists(VersionFolder) && CheckIfRunning(true) || Program.Settings.VersionGuid != VersionGuid && !CheckIfRunning(false))
#endif
await InstallLatestVersion();
await ApplyModifications();
@ -141,6 +144,50 @@ namespace Bloxstrap
Program.Exit();
}
private async Task CheckForUpdates()
{
string currentVersion = $"Bloxstrap v{Program.Version}";
var releaseInfo = await Utilities.GetJson<GithubRelease>($"https://api.github.com/repos/{Program.ProjectRepository}/releases/latest");
if (releaseInfo is null || releaseInfo.Name is null || releaseInfo.Assets is null || currentVersion == releaseInfo.Name)
return;
Dialog.Message = "Getting the latest Bloxstrap...";
// 64-bit is always the first option
GithubReleaseAsset asset = releaseInfo.Assets[Environment.Is64BitOperatingSystem ? 0 : 1];
string downloadLocation = Path.Combine(Directories.Updates, asset.Name);
Directory.CreateDirectory(Directories.Updates);
Debug.WriteLine($"Downloading {releaseInfo.Name}...");
if (!File.Exists(downloadLocation))
{
var response = await Program.HttpClient.GetAsync(asset.BrowserDownloadUrl);
using (var fileStream = new FileStream(Path.Combine(Directories.Updates, asset.Name), FileMode.CreateNew))
{
await response.Content.CopyToAsync(fileStream);
}
}
Debug.WriteLine($"Starting {releaseInfo.Name}...");
ProcessStartInfo startInfo = new()
{
FileName = downloadLocation,
};
foreach (string arg in Program.LaunchArgs)
startInfo.ArgumentList.Add(arg);
Process.Start(startInfo);
Program.Exit();
}
private async Task CheckLatestVersion()
{
Dialog.Message = "Connecting to Roblox...";
@ -215,9 +262,11 @@ namespace Bloxstrap
if (Program.Settings.RFUEnabled && Process.GetProcessesByName("rbxfpsunlocker").Length == 0)
{
ProcessStartInfo startInfo = new();
startInfo.FileName = Path.Combine(Directories.Integrations, @"rbxfpsunlocker\rbxfpsunlocker.exe");
startInfo.WorkingDirectory = Path.Combine(Directories.Integrations, "rbxfpsunlocker");
ProcessStartInfo startInfo = new()
{
FileName = Path.Combine(Directories.Integrations, @"rbxfpsunlocker\rbxfpsunlocker.exe"),
WorkingDirectory = Path.Combine(Directories.Integrations, "rbxfpsunlocker")
};
rbxFpsUnlocker = Process.Start(startInfo);
@ -273,9 +322,9 @@ namespace Bloxstrap
Program.Exit(ERROR_INSTALL_USEREXIT);
}
#endregion
#endregion
#region App Install
#region App Install
public static void Register()
{
RegistryKey applicationKey = Registry.CurrentUser.CreateSubKey($@"Software\{Program.ProjectName}");
@ -393,11 +442,11 @@ namespace Bloxstrap
Dialog.ShowSuccess($"{Program.ProjectName} has been uninstalled");
Environment.Exit(ERROR_PRODUCT_UNINSTALLED);
Program.Exit();
}
#endregion
#endregion
#region Roblox Install
#region Roblox Install
private void UpdateProgressbar()
{
int newProgress = (int)Math.Floor(ProgressIncrement * TotalDownloadedBytes);
@ -541,8 +590,7 @@ namespace Bloxstrap
File.SetAttributes(fileVersionFolder, File.GetAttributes(fileModFolder) & ~FileAttributes.ReadOnly);
}
// now check for files that have been deleted from the mod folder
// according to the manifest
// now check for files that have been deleted from the mod folder according to the manifest
foreach (string fileLocation in manifestFiles)
{
if (modFolderFiles.Contains(fileLocation))

View File

@ -34,7 +34,7 @@
<GroupBox Grid.Column="0" Header="Discord Rich Presence" Margin="10,10,5,5">
<StackPanel VerticalAlignment="Center">
<CheckBox x:Name="CheckBoxDRPEnabled" Content=" Show game activity" Margin="5" VerticalAlignment="Top" IsChecked="{Binding DRPEnabled, Mode=TwoWay}" />
<CheckBox x:Name="CheckBoxDRPButtons" Content=" Allow people to join" Margin="5" VerticalAlignment="Top" IsEnabled="{Binding IsChecked, ElementName=CheckBoxDRPEnabled, Mode=OneWay}" IsChecked="{Binding DRPButtons, Mode=TwoWay}" />
<CheckBox x:Name="CheckBoxDRPButtons" Content=" Allow activity joining" Margin="5" VerticalAlignment="Top" IsEnabled="{Binding IsChecked, ElementName=CheckBoxDRPEnabled, Mode=OneWay}" IsChecked="{Binding DRPButtons, Mode=TwoWay}" />
</StackPanel>
</GroupBox>
<GroupBox Grid.Column="1" Header="FPS Unlocking" Margin="5,10,10,5">

View File

@ -9,6 +9,7 @@ namespace Bloxstrap.Helpers
public static string Integrations { get; private set; } = "";
public static string Versions { get; private set; } = "";
public static string Modifications { get; private set; } = "";
public static string Updates { get; private set; } = "";
public static string App { get; private set; } = "";
@ -21,6 +22,7 @@ namespace Bloxstrap.Helpers
Integrations = Path.Combine(Base, "Integrations");
Versions = Path.Combine(Base, "Versions");
Modifications = Path.Combine(Base, "Modifications");
Updates = Path.Combine(Base, "Updates");
App = Path.Combine(Base, $"{Program.ProjectName}.exe");
}

View File

@ -17,90 +17,52 @@ namespace Bloxstrap.Helpers
if (Environment.ProcessPath is null || !File.Exists(Directories.App) || Environment.ProcessPath == Directories.App)
return;
bool isAutoUpgrade = Environment.ProcessPath.StartsWith(Directories.Updates);
// if downloaded version doesn't match, replace installed version with downloaded version
FileVersionInfo currentVersionInfo = FileVersionInfo.GetVersionInfo(Environment.ProcessPath);
FileVersionInfo installedVersionInfo = FileVersionInfo.GetVersionInfo(Directories.App);
if (installedVersionInfo.ProductVersion != currentVersionInfo.ProductVersion)
if (installedVersionInfo.ProductVersion == currentVersionInfo.ProductVersion)
return;
DialogResult result;
// silently upgrade version if the command line flag is set or if we're launching from an auto update
if (Program.IsUpgrade || isAutoUpgrade)
{
DialogResult result;
if (Program.IsUpgrade)
{
result = DialogResult.Yes;
}
else
{
result = Program.ShowMessageBox(
$"The version of {Program.ProjectName} you've launched is different to the version you currently have installed.\nWould you like to upgrade your currently installed version?",
MessageBoxIcon.Question,
MessageBoxButtons.YesNo
);
}
if (result == DialogResult.Yes)
{
File.Delete(Directories.App);
File.Copy(Environment.ProcessPath, Directories.App);
Bootstrapper.Register();
Program.ShowMessageBox(
$"{Program.ProjectName} has been updated to v{currentVersionInfo.ProductVersion}",
MessageBoxIcon.Information,
MessageBoxButtons.OK
);
if (!Program.IsQuiet)
{
new Preferences().ShowDialog();
Program.Exit();
}
}
result = DialogResult.Yes;
}
return;
}
public static async Task Check()
{
if (Environment.ProcessPath is null || Program.IsUninstall || Program.IsQuiet && Program.IsFirstRun)
return;
if (!Program.IsFirstRun)
CheckInstalledVersion();
if (!Program.Settings.CheckForUpdates)
return;
FileVersionInfo currentVersionInfo = FileVersionInfo.GetVersionInfo(Environment.ProcessPath);
string currentVersion = $"Bloxstrap v{currentVersionInfo.ProductVersion}";
string latestVersion;
string releaseNotes;
var releaseInfo = await Utilities.GetJson<GithubRelease>($"https://api.github.com/repos/{Program.ProjectRepository}/releases/latest");
if (releaseInfo is null || releaseInfo.Name is null || releaseInfo.Body is null)
return;
latestVersion = releaseInfo.Name;
releaseNotes = releaseInfo.Body;
if (currentVersion != latestVersion)
else
{
DialogResult result = Program.ShowMessageBox(
$"A new version of {Program.ProjectName} is available\n\n[{latestVersion}]\n{releaseNotes}\n\nWould you like to download it?",
result = Program.ShowMessageBox(
$"The version of {Program.ProjectName} you've launched is different to the version you currently have installed.\nWould you like to upgrade your currently installed version?",
MessageBoxIcon.Question,
MessageBoxButtons.YesNo
);
if (result == DialogResult.Yes)
{
Utilities.OpenWebsite($"https://github.com/{Program.ProjectRepository}/releases/latest");
Program.Exit(Bootstrapper.ERROR_INSTALL_USEREXIT);
}
);
}
if (result != DialogResult.Yes)
return;
File.Delete(Directories.App);
File.Copy(Environment.ProcessPath, Directories.App);
Bootstrapper.Register();
if (Program.IsQuiet || isAutoUpgrade)
return;
Program.ShowMessageBox(
$"{Program.ProjectName} has been updated to v{currentVersionInfo.ProductVersion}",
MessageBoxIcon.Information,
MessageBoxButtons.OK
);
new Preferences().ShowDialog();
Program.Exit();
}
}
}

View File

@ -30,6 +30,7 @@ namespace Bloxstrap
public static bool IsUninstall { get; private set; } = false;
public static bool IsNoLaunch { get; private set; } = false;
public static bool IsUpgrade { get; private set; } = false;
public static string[] LaunchArgs { get; private set; } = null!;
public static string LocalAppData { get; private set; } = null!;
public static string StartMenu { get; private set; } = null!;
@ -65,6 +66,8 @@ namespace Bloxstrap
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
LaunchArgs = args;
HttpClient.Timeout = TimeSpan.FromMinutes(5);
HttpClient.DefaultRequestHeaders.Add("User-Agent", ProjectRepository);
@ -123,8 +126,8 @@ namespace Bloxstrap
}
#if !DEBUG
if (!IsUninstall)
Updater.Check().Wait();
if (!IsUninstall && !IsFirstRun)
Updater.CheckInstalledVersion();
#endif
string commandLine = "";