Skip to content

Commit

Permalink
Fix invalid export path causing exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
JetStream96 committed Jul 17, 2018
1 parent 28de0a1 commit d876cc0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 12 deletions.
5 changes: 4 additions & 1 deletion src/QSP/ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/QSP/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
15 changes: 10 additions & 5 deletions src/QSP/RouteFinding/FileExport/FileExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public IEnumerable<Status> Export()
// Find a file name which allows us to export without name conflicts.
private int FileNameNum(IReadOnlyList<ExportCommand> 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;
Expand All @@ -62,10 +62,10 @@ private int FileNameNum(IReadOnlyList<ExportCommand> 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)
Expand Down Expand Up @@ -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());

Expand All @@ -109,6 +113,7 @@ private void TryCreateDirectories(IEnumerable<ExportCommand> 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;
Expand Down
16 changes: 14 additions & 2 deletions src/QSP/RouteFinding/FileExport/IExportPath.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using QSP.Common.Options;
using QSP.Utilities;
using System.IO;

namespace QSP.RouteFinding.FileExport
{
public interface IExportPath
{
/// <summary>
/// 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.
/// </summary>
string FullPath(SimulatorType Type, AppOptions Option);
Expand All @@ -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));

/// <summary>
/// Return value may be null, or the path may not exist.
/// </summary>
public string FullPath(SimulatorType Type, AppOptions Option)
{
var simPath = Option.SimulatorPaths[Type];
return ExceptionHelpers.DefaultIfThrows(
() => Path.GetFullPath(Path.Combine(simPath, relativePath)),
null);
}
}
}
4 changes: 3 additions & 1 deletion src/QSP/RouteFinding/FileExport/Providers/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ public static string GetExportText(ProviderType type, Route route,
return Lookup[type].Export(input);
}

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

/// <summary>
/// Gets the export directory for the specified simulator.
/// Returned path may be null or not exist.
/// </summary>
/// <param name="sim">null if using the custom export directory</param>
/// <exception cref="Exception">The ExportCommand does not support
Expand Down
6 changes: 4 additions & 2 deletions src/QSP/UI/UserControls/ExportMenuRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private SimulatorType? SelectedSimType
}
}

// Returned path may be null or not exist.
private string GetDirectoryPath()
{
var simType = SelectedSimType;
Expand Down Expand Up @@ -71,12 +72,13 @@ public void Init(ExportCommand c, Func<AppOptions> 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);
Expand Down

0 comments on commit d876cc0

Please sign in to comment.