From b21f2294c5449fa041de7a67670d053533240de9 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 7 Aug 2021 21:18:49 -0700 Subject: [PATCH] Update messages for exporting saves Without the required permissions, the FilePicker caches the file to the app's documents. Indicate where the save file is exported to. --- PKHeX.Mobile/Logic/FileUtil.cs | 49 +++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/PKHeX.Mobile/Logic/FileUtil.cs b/PKHeX.Mobile/Logic/FileUtil.cs index 151784a..8faa7cb 100644 --- a/PKHeX.Mobile/Logic/FileUtil.cs +++ b/PKHeX.Mobile/Logic/FileUtil.cs @@ -12,7 +12,7 @@ namespace PKHeX.Mobile.Logic // todo: rename this class to not clash with PKHeX.Core public static class FileUtil { - public static async Task PickFile(params string[] ext) + public static async Task PickFile() { var fileData = await FilePicker.PickAsync(PickOptions.Default).ConfigureAwait(false); if (fileData == null) @@ -29,7 +29,7 @@ public static async Task TryGetSaveFile() if (file == null) return null; - await using var stream = await file.OpenReadAsync(); + await using var stream = await file.OpenReadAsync().ConfigureAwait(false); var len = stream.Length; bool isPossibleSAV = SaveUtil.IsSizeValid((int) len); if (!isPossibleSAV) @@ -46,7 +46,9 @@ public static async Task TryGetSaveFile() await UserDialogs.Instance.AlertAsync($"The file is being passed as a URI instead of a path. Please try moving your saves to a different folder.\n\nStack Trace:\n{ex}").ConfigureAwait(false); return null; } +#pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) +#pragma warning restore CA1031 // Do not catch general exception types { await UserDialogs.Instance.AlertAsync($"Exception choosing file: {ex}").ConfigureAwait(false); return null; @@ -65,35 +67,44 @@ public static async Task ExportSAV(SaveFile sav) var path = sav.Metadata.FilePath; try { - var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); - var bakPath = Path.Combine(docPath, "PKHeX Backups"); - Directory.CreateDirectory(bakPath); + if (sav.State.Exportable) + await SaveBackup(sav).ConfigureAwait(false); - var bakName = Path.Combine(bakPath, Util.CleanFileName(sav.Metadata.BAKName)); - if (sav.State.Exportable && Directory.Exists(bakPath) && !File.Exists(bakName)) - File.WriteAllBytes(bakName, sav.State.BAK); - bool success = File.Exists(bakName); - Console.WriteLine($"Backed up: {success}"); - - File.WriteAllBytes(path, data); - await UserDialogs.Instance.AlertAsync("Exported save file!").ConfigureAwait(false); + await File.WriteAllBytesAsync(path, data).ConfigureAwait(false); + await UserDialogs.Instance.AlertAsync($"Exported save file to: {path}").ConfigureAwait(false); return true; } +#pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) +#pragma warning restore CA1031 // Do not catch general exception types { await UserDialogs.Instance.AlertAsync($"Failed: {ex}").ConfigureAwait(false); return false; } } - private static bool IsFileLocked(string path) + private static async Task SaveBackup(SaveFile sav) { - try + var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); + var bakPath = Path.Combine(docPath, "PKHeX Backups"); + Directory.CreateDirectory(bakPath); + + if (!Directory.Exists(bakPath)) { - var fat = File.GetAttributes(path); - return (fat & FileAttributes.ReadOnly) != 0; + Console.WriteLine("Backup path does not exist."); + return; } - catch { return true; } + + var bakName = Path.Combine(bakPath, Util.CleanFileName(sav.Metadata.BAKName)); + if (File.Exists(bakName)) + { + Console.WriteLine("Backup already exists for this file."); + return; + } + + await File.WriteAllBytesAsync(bakName, sav.State.BAK).ConfigureAwait(false); + bool success = File.Exists(bakName); + Console.WriteLine($"Backed up: {success}"); } } -} \ No newline at end of file +}