Skip to content

Commit

Permalink
fix: Crash window showing on shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorixon committed Dec 6, 2023
1 parent a982499 commit 88ab01b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
21 changes: 17 additions & 4 deletions src/GIMI-ModManager.WinUI/Services/ActivationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using CommunityToolkit.WinUI;
using GIMI_ModManager.Core.Contracts.Services;
using GIMI_ModManager.Core.GamesService;
using GIMI_ModManager.Core.Helpers;
using GIMI_ModManager.WinUI.Activation;
using GIMI_ModManager.WinUI.Contracts.Services;
using GIMI_ModManager.WinUI.Helpers;
Expand Down Expand Up @@ -148,6 +149,7 @@ private async Task StartupAsync()
}

const int MinimizedPosition = -32000;

private async Task SetWindowSettings()
{
var screenSize = await _localSettingsService.ReadSettingAsync<ScreenSizeSettings>(ScreenSizeSettings.Key);
Expand Down Expand Up @@ -195,23 +197,34 @@ private async void OnApplicationExit(object sender, WindowEventArgs args)
args.Handled = true;

var notificationCleanup = Task.Run(_modNotificationManager.CleanupAsync).ConfigureAwait(false);
var saveWindowSettingsTask = SaveWindowSettingsAsync().ConfigureAwait(false);
var saveWindowSettingsTask = SaveWindowSettingsAsync();

_logger.Debug("JASM shutting down...");
_modUpdateAvailableChecker.CancelAndStop();
_updateChecker.CancelAndStop();


await saveWindowSettingsTask;
await _windowManagerService.CloseWindowsAsync().ConfigureAwait(false);

var tmpDir = new DirectoryInfo(App.TMP_DIR);
if (tmpDir.Exists)
{
tmpDir.Refresh();
_logger.Debug("Deleting temporary directory: {Path}", tmpDir.FullName);
tmpDir.Delete(true);
try
{
tmpDir.EnumerateFiles("*", SearchOption.AllDirectories)
.ForEach(f => f.Attributes = FileAttributes.Normal);
tmpDir.EnumerateDirectories("*", SearchOption.AllDirectories)
.ForEach(d => d.Attributes = FileAttributes.Normal);
tmpDir.Delete(true);
}
catch (Exception e)
{
_logger.Warning(e, "Failed to delete temporary directory: {Path}", tmpDir.FullName);
}
}

await saveWindowSettingsTask;
await notificationCleanup;
_logger.Debug("JASM shutdown complete.");
}
Expand Down
17 changes: 10 additions & 7 deletions src/GIMI-ModManager.WinUI/Services/ImageHandlerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace GIMI_ModManager.WinUI.Services;

public class ImageHandlerService
{
private readonly DirectoryInfo _tmpFolder = new(Path.Combine(App.TMP_DIR, "Images"));
private readonly string _tmpFolder = Path.Combine(App.TMP_DIR, "Images");

public readonly Uri PlaceholderImageUri =
new(Path.Combine(App.ASSET_DIR, "ModPanePlaceholder.webp"));
Expand Down Expand Up @@ -60,13 +60,14 @@ public async Task<StorageFile> DownloadImageAsync(Uri url, CancellationToken can
throw new ArgumentException($"Url must be a valid image url. Invalid extension: {invalidExtension}");
}

var tmpFolder = new DirectoryInfo(_tmpFolder);

var tmpFile = Path.Combine(_tmpFolder.FullName,
var tmpFile = Path.Combine(tmpFolder.FullName,
$"WEB_DOWNLOAD_{Guid.NewGuid():N}{Path.GetExtension(url.ToString())}");


if (!_tmpFolder.Exists)
_tmpFolder.Create();
if (!tmpFolder.Exists)
tmpFolder.Create();

var client = _httpClientFactory.CreateClient();

Expand All @@ -81,12 +82,14 @@ public async Task<StorageFile> DownloadImageAsync(Uri url, CancellationToken can

private async Task<StorageFile> CopyImageToTmpFolder(StorageFile file)
{
if (!_tmpFolder.Exists) _tmpFolder.Create();
var tmpFolder = new DirectoryInfo(_tmpFolder);

var tmpFile = new FileInfo(Path.Combine(_tmpFolder.FullName, file.Name));
if (!tmpFolder.Exists) tmpFolder.Create();

var tmpFile = new FileInfo(Path.Combine(tmpFolder.FullName, file.Name));
if (tmpFile.Exists) tmpFile.Delete();

var tmpImage = await file.CopyAsync(await StorageFolder.GetFolderFromPathAsync(_tmpFolder.FullName));
var tmpImage = await file.CopyAsync(await StorageFolder.GetFolderFromPathAsync(tmpFolder.FullName));
var extension = tmpImage.FileType;

var newFileName = $"{Path.GetFileNameWithoutExtension(tmpImage.Name)}_{Guid.NewGuid()}{extension}";
Expand Down

0 comments on commit 88ab01b

Please sign in to comment.