Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed exception when trying to close apps #22

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Elite Dangerous Addon Launcher V2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<RootNamespace>Elite_Dangerous_Addon_Launcher_V2</RootNamespace>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<AssemblyVersion>1.1.5.377</AssemblyVersion>
<FileVersion>1.1.5.377</FileVersion>
<AssemblyVersion>1.1.5.382</AssemblyVersion>
<FileVersion>1.1.5.382</FileVersion>
<ApplicationIcon>elite-dangerous-icon.ico</ApplicationIcon>
<PackageIcon>app.png</PackageIcon>
<PackageProjectUrl>https://github.com/jimmyeao/Elite-Dangerous-Addon-Launcher-V2</PackageProjectUrl>
Expand Down Expand Up @@ -39,6 +39,9 @@
<PackageReference Include="MaterialDesignColors" Version="2.1.4" />
<PackageReference Include="MaterialDesignThemes" Version="4.9.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
67 changes: 67 additions & 0 deletions LoggingConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Serilog;
using Serilog.Sinks.File;
using System;
using System.IO;
using System.Linq;
using System.Text;


namespace Elite_Dangerous_Addon_Launcher_V2
{
public static class LoggingConfig
{
#region Public Fields

public static string? logFileFullPath;

#endregion Public Fields

#region Public Methods

public static void Configure()
{
var filePathHook = new CaptureFilePathHook();
string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string folderPath = Path.Combine(appDataFolder, "Elite_Dangerous_Addon_Lau");
var logFilePath = Path.Combine(folderPath, "EDAL.log");

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.File(logFilePath,
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: 10_000_000, // 10 MB file size limit
retainedFileCountLimit: 1, // Retain only the last file
hooks: filePathHook,
rollOnFileSizeLimit: true) // Roll over on file size limit
.CreateLogger();

Log.Information("Logger Created");
logFileFullPath = filePathHook.Path;
}



#endregion Public Methods
}

internal class CaptureFilePathHook : FileLifecycleHooks
{
#region Public Properties

public string? Path { get; private set; }

#endregion Public Properties

#region Public Methods

public override Stream OnFileOpened(string path, Stream underlyingStream, Encoding encoding)
{
Path = path;
return base.OnFileOpened(path, underlyingStream, encoding);
}

#endregion Public Methods
}
}
101 changes: 54 additions & 47 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Win32;
using Newtonsoft.Json;
using System;
using Serilog;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
Expand Down Expand Up @@ -63,7 +64,7 @@ public string ApplicationVersion
public MainWindow(string profileName = null)
{
InitializeComponent();

LoggingConfig.Configure();
if (!string.IsNullOrEmpty(profileName))
{
// Use the profileName to load the appropriate profile
Expand Down Expand Up @@ -420,12 +421,14 @@ public void ShowWhatsNew()
private void Btn_Launch_Click(object sender, RoutedEventArgs e)
{
// Code to launch all enabled apps
Log.Information("Launching all enabled apps..");
foreach (var app in AppState.Instance.CurrentProfile.Apps)
{
if (app.IsEnabled)
{
Btn_Launch.IsEnabled = false;
LaunchApp(app);
Log.Information("Launching {AppName}..", app.Name);
}
}
}
Expand Down Expand Up @@ -623,7 +626,7 @@ private async Task<Settings> LoadSettingsAsync()
// If the settings file doesn't exist, use defaults
settings = new Settings { Theme = "Default" };
}

Log.Information("Settings loaded: {Settings}", settings);
return settings;
}

Expand Down Expand Up @@ -685,61 +688,65 @@ private void OnProfileChanged(Profile oldProfile, Profile newProfile)

private void ProcessExitHandler(object sender, EventArgs e) //triggered when EDLaunch exits
{
Btn_Launch.IsEnabled = true;
bool closeAllApps = false;
Application.Current.Dispatcher.Invoke(() =>
{
closeAllApps = CloseAllAppsCheckbox.IsChecked == true;
});
// if EDLaunch has quit, does the user want us to kill all the apps?
if (closeAllApps)
{
try
Btn_Launch.IsEnabled = true;
bool closeAllApps = CloseAllAppsCheckbox.IsChecked == true;

// if EDLaunch has quit, does the user want us to kill all the apps?
if (closeAllApps)
{
foreach (string p in processList)
Log.Information("CloseAllAppsOnExit is enabled, closing all apps..");
try
{
foreach (Process process in Process.GetProcessesByName(p))
foreach (string p in processList)
{
// Temp is a document which you need to kill.
if (process.ProcessName.Contains(p))
process.CloseMainWindow();
Log.Information("Closing {0}", p);
foreach (Process process in Process.GetProcessesByName(p))
{
// Temp is a document which you need to kill.
if (process.ProcessName.Contains(p))
process.CloseMainWindow();
}
}
}
catch
{
// if something went wrong, don't raise an exception
Log.Error("An error occurred trying to close all apps..");
}
// doesn't seem to want to kill VoiceAttack nicely..
try
{
Process[] procs = Process.GetProcessesByName("VoiceAttack");
foreach (var proc in procs) { proc.Kill(); } //sadly this means next time it starts, it will complain it was shutdown in an unclean fashion
}
catch
{
// if something went wrong, don't raise an exception
}
// Elite Dangerous Odyssey Materials Helper is a little strange, let's deal with its
// multiple running processes..
try
{
Process[] procs = Process.GetProcessesByName("Elite Dangerous Odyssey Materials Helper");
foreach (var proc in procs) { proc.CloseMainWindow(); }
}
catch
{
// if something went wrong, don't raise an exception
}
// sleep for 5 seconds then quit
for (int i = 5; i != 0; i--)
{
Thread.Sleep(1000);
}
Environment.Exit(0);
}
catch
{
// if something went wrong, don't raise an exception
}
// doesn't seem to want to kill VoiceAttack nicely..
try
{
Process[] procs = Process.GetProcessesByName("VoiceAttack");
foreach (var proc in procs) { proc.Kill(); } //sadly this means next time it starts, it will complain it was shutdown in an unclean fashion
}
catch
{
// if something went wrong, don't raise an exception
}
// Elite Dangerous Odyssey Materials Helper is a little strange, let's deal with its
// multiple running processes..
try
{
Process[] procs = Process.GetProcessesByName("Elite Dangerous Odyssey Materials Helper");
foreach (var proc in procs) { proc.CloseMainWindow(); }
}
catch
{
// if something went wrong, don't raise an exception
}
// sleep for 5 seconds then quit
for (int i = 5; i != 0; i--)
{
Thread.Sleep(1000);
}
Environment.Exit(0);
}
});
}


private async Task SaveSettingsAsync(Settings settings)
{
string localFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
Expand Down