Skip to content

Commit d876cc0

Browse files
committed
Fix invalid export path causing exceptions.
1 parent 28de0a1 commit d876cc0

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
lines changed

src/QSP/ChangeLog.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
version 0.4.5
1+
version 0.4.6
2+
* Hot fix for flight plan export path issue that prevents the program from starting.
3+
4+
version 0.4.5
25
* 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.
36
* Fix Google map API key issue.
47
* Minor UI adjustments.

src/QSP/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("0.4.5.0")]
34+
[assembly: AssemblyVersion("0.4.6.0")]
3535
[assembly: AssemblyFileVersion("1.0.0.0")]

src/QSP/RouteFinding/FileExport/FileExporter.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public IEnumerable<Status> Export()
5151
// Find a file name which allows us to export without name conflicts.
5252
private int FileNameNum(IReadOnlyList<ExportCommand> cmdToExport, string nameBase)
5353
{
54-
const int maxAttemptCount = 10000;
54+
const int maxAttemptCount = 1000;
5555
for (int i = 1; i <= maxAttemptCount; i++)
5656
{
5757
if (cmdToExport.All(c => !FileExist(nameBase, c, i))) return i;
@@ -62,10 +62,10 @@ private int FileNameNum(IReadOnlyList<ExportCommand> cmdToExport, string nameBas
6262

6363
private Status Export(string nameBase, ExportCommand c, int i)
6464
{
65-
var fileName = GetFileFullPath(nameBase, c, i);
66-
6765
try
6866
{
67+
var fileName = GetFileFullPath(nameBase, c, i);
68+
6969
// Although the file name has been checked to have no conflict, if the user choose
7070
// to export multiple files to the same folder, the file names can still collide.
7171
var newName = File.Exists(fileName)
@@ -94,10 +94,14 @@ private static string GenerateFileName(string nameBase, ExportCommand c)
9494

9595
private bool FileExist(string nameBase, ExportCommand cmd, int n)
9696
{
97-
var filePath = GetFileFullPath(nameBase, cmd, n);
98-
return File.Exists(filePath);
97+
return DefaultIfThrows(() =>
98+
{
99+
var filePath = GetFileFullPath(nameBase, cmd, n);
100+
return File.Exists(filePath);
101+
}, false);
99102
}
100103

104+
/// Returned path may be null or not exist.
101105
private string ExportDirectory(ExportCommand c) =>
102106
Providers.Types.ExportDirectory(c.DefaultSimulator, c, options());
103107

@@ -109,6 +113,7 @@ private void TryCreateDirectories(IEnumerable<ExportCommand> enabledCommands)
109113
}
110114
}
111115

116+
// May throw exception. Returned path may not exist.
112117
private string GetFileFullPath(string nameBase, ExportCommand cmd, int n)
113118
{
114119
var fileName = nameBase + n.ToString().PadLeft(2, '0') + cmd.Extension;

src/QSP/RouteFinding/FileExport/IExportPath.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using QSP.Common.Options;
2+
using QSP.Utilities;
23
using System.IO;
34

45
namespace QSP.RouteFinding.FileExport
56
{
67
public interface IExportPath
78
{
89
/// <summary>
10+
/// The returned path may be null, if the root directory of
11+
/// the given simulator is not set in AppOptions.
912
/// The returned path may not exist.
1013
/// </summary>
1114
string FullPath(SimulatorType Type, AppOptions Option);
@@ -29,7 +32,16 @@ public class RelativePath : IExportPath
2932
{
3033
private readonly string relativePath;
3134
public RelativePath(string relativePath) { this.relativePath = relativePath; }
32-
public string FullPath(SimulatorType Type, AppOptions Option) =>
33-
Path.GetFullPath(Path.Combine(Option.SimulatorPaths[Type], relativePath));
35+
36+
/// <summary>
37+
/// Return value may be null, or the path may not exist.
38+
/// </summary>
39+
public string FullPath(SimulatorType Type, AppOptions Option)
40+
{
41+
var simPath = Option.SimulatorPaths[Type];
42+
return ExceptionHelpers.DefaultIfThrows(
43+
() => Path.GetFullPath(Path.Combine(simPath, relativePath)),
44+
null);
45+
}
3446
}
3547
}

src/QSP/RouteFinding/FileExport/Providers/Types.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ public static string GetExportText(ProviderType type, Route route,
212212
return Lookup[type].Export(input);
213213
}
214214

215-
public static readonly IReadOnlyDictionary<SimulatorType, string> SimDisplayName = Dict
215+
public static readonly IReadOnlyDictionary<SimulatorType, string> SimDisplayName =
216+
Dict
216217
(
217218
(SimulatorType.FSX, "FSX"),
218219
(SimulatorType.FSX_Steam, "FSX: Steam edition"),
@@ -226,6 +227,7 @@ public static string GetExportText(ProviderType type, Route route,
226227

227228
/// <summary>
228229
/// Gets the export directory for the specified simulator.
230+
/// Returned path may be null or not exist.
229231
/// </summary>
230232
/// <param name="sim">null if using the custom export directory</param>
231233
/// <exception cref="Exception">The ExportCommand does not support

src/QSP/UI/UserControls/ExportMenuRow.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ private SimulatorType? SelectedSimType
4242
}
4343
}
4444

45+
// Returned path may be null or not exist.
4546
private string GetDirectoryPath()
4647
{
4748
var simType = SelectedSimType;
@@ -71,12 +72,13 @@ public void Init(ExportCommand c, Func<AppOptions> option)
7172
BrowseBtnEnabled = SimComboBox.SelectedIndex == SimComboBox.Items.Count - 1;
7273

7374
var path = GetDirectoryPath();
74-
PathTextBox.Text = Directory.Exists(path) ? path : "";
75+
var pathValid = path != null;
76+
PathTextBox.Text = pathValid ? path : "";
7577
};
7678

7779
SimComboBox.SetItems(sims);
7880
if (sims.Length > 0) SimComboBox.SelectedIndex = 0;
79-
SimComboBox.Text = c.DefaultSimulator == null ? Custom:
81+
SimComboBox.Text = c.DefaultSimulator == null ? Custom :
8082
SimDisplayName[c.DefaultSimulator.Value];
8183

8284
FileFolderBrowse.LinkFolderBrowse(BrowseBtn, PathTextBox);

0 commit comments

Comments
 (0)