From d876cc0a828a7caa32ad7ceff0d94308d4257803 Mon Sep 17 00:00:00 2001 From: David Chen Date: Tue, 17 Jul 2018 10:41:38 +0800 Subject: [PATCH] Fix invalid export path causing exceptions. --- src/QSP/ChangeLog.txt | 5 ++++- src/QSP/Properties/AssemblyInfo.cs | 2 +- src/QSP/RouteFinding/FileExport/FileExporter.cs | 15 ++++++++++----- src/QSP/RouteFinding/FileExport/IExportPath.cs | 16 ++++++++++++++-- .../RouteFinding/FileExport/Providers/Types.cs | 4 +++- src/QSP/UI/UserControls/ExportMenuRow.cs | 6 ++++-- 6 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/QSP/ChangeLog.txt b/src/QSP/ChangeLog.txt index 1cf0b509..66cee8fa 100644 --- a/src/QSP/ChangeLog.txt +++ b/src/QSP/ChangeLog.txt @@ -1,4 +1,7 @@ -version 0.4.5 +version 0.4.6 +* Hot fix for flight plan export path issue that prevents the program from starting. + +version 0.4.5 * Add several flight plan export formats: Aerosoft Airbus, Flight Factor 777, Flight Factor A320, Ifly 737, Ifly 747 v2, JarDesign Airbus, Pmdg wind uplink, X-plane. * Fix Google map API key issue. * Minor UI adjustments. diff --git a/src/QSP/Properties/AssemblyInfo.cs b/src/QSP/Properties/AssemblyInfo.cs index 2b22c02f..c0739a32 100644 --- a/src/QSP/Properties/AssemblyInfo.cs +++ b/src/QSP/Properties/AssemblyInfo.cs @@ -31,5 +31,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.4.5.0")] +[assembly: AssemblyVersion("0.4.6.0")] [assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/QSP/RouteFinding/FileExport/FileExporter.cs b/src/QSP/RouteFinding/FileExport/FileExporter.cs index 89b54d16..a1c64825 100644 --- a/src/QSP/RouteFinding/FileExport/FileExporter.cs +++ b/src/QSP/RouteFinding/FileExport/FileExporter.cs @@ -51,7 +51,7 @@ public IEnumerable Export() // Find a file name which allows us to export without name conflicts. private int FileNameNum(IReadOnlyList cmdToExport, string nameBase) { - const int maxAttemptCount = 10000; + const int maxAttemptCount = 1000; for (int i = 1; i <= maxAttemptCount; i++) { if (cmdToExport.All(c => !FileExist(nameBase, c, i))) return i; @@ -62,10 +62,10 @@ private int FileNameNum(IReadOnlyList cmdToExport, string nameBas private Status Export(string nameBase, ExportCommand c, int i) { - var fileName = GetFileFullPath(nameBase, c, i); - try { + var fileName = GetFileFullPath(nameBase, c, i); + // Although the file name has been checked to have no conflict, if the user choose // to export multiple files to the same folder, the file names can still collide. var newName = File.Exists(fileName) @@ -94,10 +94,14 @@ private static string GenerateFileName(string nameBase, ExportCommand c) private bool FileExist(string nameBase, ExportCommand cmd, int n) { - var filePath = GetFileFullPath(nameBase, cmd, n); - return File.Exists(filePath); + return DefaultIfThrows(() => + { + var filePath = GetFileFullPath(nameBase, cmd, n); + return File.Exists(filePath); + }, false); } + /// Returned path may be null or not exist. private string ExportDirectory(ExportCommand c) => Providers.Types.ExportDirectory(c.DefaultSimulator, c, options()); @@ -109,6 +113,7 @@ private void TryCreateDirectories(IEnumerable enabledCommands) } } + // May throw exception. Returned path may not exist. private string GetFileFullPath(string nameBase, ExportCommand cmd, int n) { var fileName = nameBase + n.ToString().PadLeft(2, '0') + cmd.Extension; diff --git a/src/QSP/RouteFinding/FileExport/IExportPath.cs b/src/QSP/RouteFinding/FileExport/IExportPath.cs index 2aa5a11b..98b3fe51 100644 --- a/src/QSP/RouteFinding/FileExport/IExportPath.cs +++ b/src/QSP/RouteFinding/FileExport/IExportPath.cs @@ -1,4 +1,5 @@ using QSP.Common.Options; +using QSP.Utilities; using System.IO; namespace QSP.RouteFinding.FileExport @@ -6,6 +7,8 @@ namespace QSP.RouteFinding.FileExport public interface IExportPath { /// + /// The returned path may be null, if the root directory of + /// the given simulator is not set in AppOptions. /// The returned path may not exist. /// string FullPath(SimulatorType Type, AppOptions Option); @@ -29,7 +32,16 @@ public class RelativePath : IExportPath { private readonly string relativePath; public RelativePath(string relativePath) { this.relativePath = relativePath; } - public string FullPath(SimulatorType Type, AppOptions Option) => - Path.GetFullPath(Path.Combine(Option.SimulatorPaths[Type], relativePath)); + + /// + /// Return value may be null, or the path may not exist. + /// + public string FullPath(SimulatorType Type, AppOptions Option) + { + var simPath = Option.SimulatorPaths[Type]; + return ExceptionHelpers.DefaultIfThrows( + () => Path.GetFullPath(Path.Combine(simPath, relativePath)), + null); + } } } diff --git a/src/QSP/RouteFinding/FileExport/Providers/Types.cs b/src/QSP/RouteFinding/FileExport/Providers/Types.cs index 0ad66369..7dab5075 100644 --- a/src/QSP/RouteFinding/FileExport/Providers/Types.cs +++ b/src/QSP/RouteFinding/FileExport/Providers/Types.cs @@ -212,7 +212,8 @@ public static string GetExportText(ProviderType type, Route route, return Lookup[type].Export(input); } - public static readonly IReadOnlyDictionary SimDisplayName = Dict + public static readonly IReadOnlyDictionary SimDisplayName = + Dict ( (SimulatorType.FSX, "FSX"), (SimulatorType.FSX_Steam, "FSX: Steam edition"), @@ -226,6 +227,7 @@ public static string GetExportText(ProviderType type, Route route, /// /// Gets the export directory for the specified simulator. + /// Returned path may be null or not exist. /// /// null if using the custom export directory /// The ExportCommand does not support diff --git a/src/QSP/UI/UserControls/ExportMenuRow.cs b/src/QSP/UI/UserControls/ExportMenuRow.cs index bbf81a37..d6562e9a 100644 --- a/src/QSP/UI/UserControls/ExportMenuRow.cs +++ b/src/QSP/UI/UserControls/ExportMenuRow.cs @@ -42,6 +42,7 @@ private SimulatorType? SelectedSimType } } + // Returned path may be null or not exist. private string GetDirectoryPath() { var simType = SelectedSimType; @@ -71,12 +72,13 @@ public void Init(ExportCommand c, Func option) BrowseBtnEnabled = SimComboBox.SelectedIndex == SimComboBox.Items.Count - 1; var path = GetDirectoryPath(); - PathTextBox.Text = Directory.Exists(path) ? path : ""; + var pathValid = path != null; + PathTextBox.Text = pathValid ? path : ""; }; SimComboBox.SetItems(sims); if (sims.Length > 0) SimComboBox.SelectedIndex = 0; - SimComboBox.Text = c.DefaultSimulator == null ? Custom: + SimComboBox.Text = c.DefaultSimulator == null ? Custom : SimDisplayName[c.DefaultSimulator.Value]; FileFolderBrowse.LinkFolderBrowse(BrowseBtn, PathTextBox);