Skip to content

Commit

Permalink
General Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobsen9026 committed May 18, 2023
1 parent c32f165 commit bb8ecc6
Show file tree
Hide file tree
Showing 11 changed files with 380 additions and 123 deletions.
64 changes: 61 additions & 3 deletions BlazamSetup/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Org.BouncyCastle.Bcpg.OpenPgp;
using Serilog;
using Serilog.Events;
using System.Windows.Threading;

namespace BlazamSetup
{
Expand Down Expand Up @@ -44,7 +45,7 @@ protected override void OnStartup(StartupEventArgs args)
.Enrich.WithMachineName()
.Enrich.WithEnvironmentName()
.Enrich.WithEnvironmentUserName()

.Enrich.WithProperty("Application Name","Blazam Setup")

.WriteTo.File(InstallationConfiguraion.SetupTempDirectory + @"setuplog.txt",
rollingInterval: RollingInterval.Infinite,
Expand All @@ -55,8 +56,9 @@ protected override void OnStartup(StartupEventArgs args)
//lc.WriteTo.Console();
lc.Filter.ByExcluding(e => e.Level == LogEventLevel.Information).WriteTo.Console();
})
.WriteTo.Seq("http://logs.blazam.org:5341", apiKey: "S3JdoIIfIKcX4L3howh1", restrictedToMinimumLevel: LogEventLevel.Warning)
.WriteTo.Seq("http://logs.blazam.org:5341", apiKey: "S3JdoIIfIKcX4L3howh1", restrictedToMinimumLevel: LogEventLevel.Information)
.CreateLogger();
SetupUnhandledExceptionHandling();
StartupArgs = args;

if (!Debugger.IsAttached)
Expand All @@ -74,7 +76,7 @@ protected override void OnStartup(StartupEventArgs args)


}

private void SetupAppCenter()
{
try
Expand Down Expand Up @@ -150,5 +152,61 @@ public static bool IsRunningAsAdministrator()
// Return TRUE if user is in role "Administrator"
return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}

private void SetupUnhandledExceptionHandling()
{
// Catch exceptions from all threads in the AppDomain.
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
ShowUnhandledException(args.ExceptionObject as Exception, "AppDomain.CurrentDomain.UnhandledException", false);

// Catch exceptions from each AppDomain that uses a task scheduler for async operations.
TaskScheduler.UnobservedTaskException += (sender, args) =>
ShowUnhandledException(args.Exception, "TaskScheduler.UnobservedTaskException", false);

// Catch exceptions from a single specific UI dispatcher thread.
Dispatcher.UnhandledException += (sender, args) =>
{
// If we are debugging, let Visual Studio handle the exception and take us to the code that threw it.
if (!Debugger.IsAttached)
{
args.Handled = true;
ShowUnhandledException(args.Exception, "Dispatcher.UnhandledException", true);
}
};

// Catch exceptions from the main UI dispatcher thread.
// Typically we only need to catch this OR the Dispatcher.UnhandledException.
// Handling both can result in the exception getting handled twice.
//Application.Current.DispatcherUnhandledException += (sender, args) =>
//{
// // If we are debugging, let Visual Studio handle the exception and take us to the code that threw it.
// if (!Debugger.IsAttached)
// {
// args.Handled = true;
// ShowUnhandledException(args.Exception, "Application.Current.DispatcherUnhandledException", true);
// }
//};
}

void ShowUnhandledException(Exception e, string unhandledExceptionType, bool promptUserForShutdown)
{
Log.Error("Uncaught Exception: {@Error}", e);

var messageBoxTitle = $"Fatal Error!";
var messageBoxMessage = $"We apologize for the error. A report has been sent to the developers.";
var messageBoxButtons = MessageBoxButton.OK;

if (promptUserForShutdown)
{
messageBoxMessage += "\n\nNormally the installer would close now. Should we close it?";
messageBoxButtons = MessageBoxButton.YesNo;
}

// Let the user decide if the app should die or not (if applicable).
if (MessageBox.Show(messageBoxMessage, messageBoxTitle, messageBoxButtons) == MessageBoxResult.Yes)
{
Application.Current.Shutdown();
}
}
}
}
7 changes: 7 additions & 0 deletions BlazamSetup/BlazamSetup.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@
<Compile Include="Steps\InstallDirectory.xaml.cs">
<DependentUpon>InstallDirectory.xaml</DependentUpon>
</Compile>
<Compile Include="Steps\InstalledActionDialog.xaml.cs">
<DependentUpon>InstalledActionDialog.xaml</DependentUpon>
</Compile>
<Compile Include="Steps\LicenseAgreement.xaml.cs">
<DependentUpon>LicenseAgreement.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -500,6 +503,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Steps\InstalledActionDialog.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Steps\LicenseAgreement.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
3 changes: 3 additions & 0 deletions BlazamSetup/Data/InstallationConfiguraion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace BlazamSetup
{
public enum InstallType { IIS, Service }
public enum InstalledAction { Update, Repair,Remove}
public enum DBType { Sqlite, SQL, MySQL }
internal static class InstallationConfiguraion
{
Expand Down Expand Up @@ -49,5 +50,7 @@ internal static string InstallDirPath
public static string ProgramDataDir => Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + Path.DirectorySeparatorChar + "Blazam";

public static string SetupTempDirectory => Path.GetTempPath() + "BlazamSetup\\";

public static InstalledAction InstalledAction { get; internal set; }
}
}
4 changes: 2 additions & 2 deletions BlazamSetup/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
MouseDown="DockPanel_MouseDown"
VerticalAlignment="Top"

LastChildFill="True" Height="30" Background="Black">
LastChildFill="True" Height="30" Background="#202747">
<Grid Margin="5,5,5,5" DockPanel.Dock="Left">

<StackPanel Orientation="Horizontal">
Expand Down Expand Up @@ -60,7 +60,7 @@
<ColumnDefinition Width="125"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Background="#031985" Width="125" Orientation="Vertical">
<StackPanel Background="#202747" Width="125" Orientation="Vertical">
<TextBlock Text="Blazam Web Installer" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Width="125 " Foreground="White" FontWeight="Bold" FontSize="18" Height="48" FontStretch="Medium" TextAlignment="Center"/>

</StackPanel>
Expand Down
50 changes: 35 additions & 15 deletions BlazamSetup/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using BlazamSetup.Services;
using BlazamSetup.Steps;
using BlazamSetup.Steps.Uninstall;
using Microsoft.AppCenter.Ingestion.Models.Serialization;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -28,26 +30,44 @@ public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CurrentDispatcher = Dispatcher;
try
{
InitializeComponent();
CurrentDispatcher = Dispatcher;

InstallerFrame = Frame;
LastStepButton = BackButton;
NextStepButton = NextButton;
MainWindow.InstallerFrame.ContentRendered += InstallerFrame_ContentRendered;
if (RegistryService.InstallationExists)
InstallationConfiguraion.ProductInformation = RegistryService.GetProductInformation();
InstallerFrame = Frame;
LastStepButton = BackButton;
NextStepButton = NextButton;
MainWindow.InstallerFrame.ContentRendered += InstallerFrame_ContentRendered;
if (RegistryService.InstallationExists)
InstallationConfiguraion.ProductInformation = RegistryService.GetProductInformation();

if (App.StartupArgs.Args.Any(arg => arg.StartsWith("/u"))){
MainWindow.InstallerFrame.Navigate(new WelcomeUninstall());
if (App.StartupArgs.Args.Any(arg => arg.StartsWith("/u")))
{
Log.Information("Uninstaller Started");
MainWindow.InstallerFrame.Navigate(new WelcomeUninstall());

}
else
{
MainWindow.InstallerFrame.Navigate(NavigationManager.CurrentPage);
}
else
{

Log.Information("Installer Started");
if (RegistryService.InstallationExists)
{
MainWindow.InstallerFrame.Navigate(new InstalledActionDialog());

}
}
else
{
MainWindow.InstallerFrame.Navigate(NavigationManager.CurrentPage);

}

}
}catch (Exception ex)
{
Log.Error("Uncaught Exception: {@Error}", ex);
}

}

Expand Down
85 changes: 55 additions & 30 deletions BlazamSetup/Services/DownloadService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Octokit;
using Serilog;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -15,8 +16,8 @@ namespace BlazamSetup.Services
internal static class DownloadService
{

public static string SetupTempDirectory = InstallationConfiguraion.SetupTempDirectory;
public static string SourceDirectory = InstallationConfiguraion.SetupTempDirectory+"setup\\";
public static string SetupTempDirectory = Path.GetTempPath() + "BlazamSetup\\";
public static string SourceDirectory = Path.GetTempPath() + "BlazamSetup\\setup\\";
public static string UpdateFile = SetupTempDirectory + "blazam.zip";
private static ReleaseAsset latestRelease;

Expand All @@ -27,6 +28,8 @@ internal static class DownloadService

public static async Task<bool> Download()
{
Log.Information("Download started");

var githubclient = new GitHubClient(new ProductHeaderValue("BLAZAM-APP"));


Expand Down Expand Up @@ -114,62 +117,84 @@ public static async Task<bool> Download()
}

}
return false;
return false;
}
public static void CleanDownload()
public static void CleanDownload()
{

cancellationTokenSource.Cancel();
Task.Run(() => {
int retries =5 ;
while (retries-->0)

cancellationTokenSource.Cancel();
Task.Run(() =>
{
int retries = 5;
while (retries-- > 0)
{
try
{
try
{
File.Delete(UpdateFile);
File.Delete(UpdateFile);
Directory.Delete(SetupTempDirectory, true);
retries = 0;
retries = 0;

}
catch
{
Task.Delay(50).Wait();
}
}
});


catch
{
Task.Delay(50).Wait();
}
}
});


}
public static void CleanSource()
{

cancellationTokenSource.Cancel();
Task.Run(() => {
Task.Run(() =>
{
int retries = 5;
while (retries-- > 0)
{
try
{
Log.Information("Cleaning old extracted files: " + SourceDirectory);

Directory.Delete(SourceDirectory, true);
retries = 0;
}
catch
catch (Exception ex)
{
Log.Error("Error cleaning old installation files: {@Error}", ex);

Task.Delay(50).Wait();
}
}
});


}
internal static void UnpackDownload()
internal static async Task<bool> UnpackDownload()
{
CleanSource();
Directory.CreateDirectory(SourceDirectory);
ZipArchive download = new ZipArchive(File.OpenRead(UpdateFile));
download.ExtractToDirectory(SourceDirectory);
download.Dispose();
File.Delete(UpdateFile);
return await Task.Run(() =>
{
try
{
Log.Information("Extracting files: " + SourceDirectory);

CleanSource();
Directory.CreateDirectory(SourceDirectory);
ZipArchive download = new ZipArchive(File.OpenRead(UpdateFile));
download.ExtractToDirectory(SourceDirectory);
download.Dispose();
File.Delete(UpdateFile);
return true;
}
catch (Exception ex)
{
Log.Error("Error unpacking download: {@Error}", ex);

}
return false;
});

}
}
}
Loading

0 comments on commit bb8ecc6

Please sign in to comment.