Skip to content

Commit

Permalink
add better error handling, fix bug in zip unpacker
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargaj committed Jul 15, 2018
1 parent df8cb93 commit ea78565
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 41 deletions.
72 changes: 53 additions & 19 deletions Source/Dialogs/DownloadDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,26 +144,31 @@ public static string Sanitize(string s)
return System.Text.RegularExpressions.Regex.Replace(s == null ? "[unknown]" : s, @"[^a-zA-Z0-9\-_\.]+", "-").ToLower();
}

private async Task<bool> UnpackFile(string archiveFile, string targetDir)
private IUnpacker FindUnpacker(string archiveFile)
{
foreach (var unpacker in Registry.Unpackers)
{
if (unpacker.CanUnpack(archiveFile))
{
unpacker.ProgressChanged += (object sender, UnpackingProgressArgs e) =>
{
Dispatcher.CurrentDispatcher.Invoke(() =>
{
unpackProgress.Maximum = e.TotalFiles;
unpackProgress.Value = e.CurrentFile;
unpackText.Text = $"Unpacking {e.CurrentFilename}";
});
};
Directory.CreateDirectory(targetDir);
return await unpacker.Unpack(archiveFile, targetDir);
return unpacker;
}
}
return false;
return null;
}

private async Task<bool> UnpackFile(IUnpacker unpacker, string archiveFile, string targetDir)
{
unpacker.ProgressChanged += (object sender, UnpackingProgressArgs e) =>
{
Dispatcher.CurrentDispatcher.Invoke(() =>
{
unpackProgress.Maximum = e.TotalFiles;
unpackProgress.Value = e.CurrentFile;
unpackText.Text = $"Unpacking {e.CurrentFilename}";
});
};
Directory.CreateDirectory(targetDir);
return await unpacker.Unpack(archiveFile, targetDir);
}

private async Task WatchDemo()
Expand Down Expand Up @@ -197,11 +202,17 @@ private async Task WatchDemo()
}

var extractPath = Path.Combine(path, Path.GetFileNameWithoutExtension(localFileName));
if (!await UnpackFile(localFileName, extractPath))
var unpacker = FindUnpacker(localFileName);
var extractSuccessful = false;
if (unpacker == null)
{
// We couldn't unpack the file; maybe we can just run it?
// We couldn't find an unpacker to the file; maybe we can just run it?
extractPath = localFileName;
}
else
{
extractSuccessful = await UnpackFile(unpacker, localFileName, extractPath);
}
List<Runners.Runnable> runnables = new List<Runners.Runnable>();
var runners = Registry.Runners.OrderByDescending(s => s.Priority);
var lastPriority = runners.First().Priority;
Expand All @@ -217,20 +228,43 @@ private async Task WatchDemo()
if (runnables.Count == 0)
{
// Nothing found, error
if (unpacker != null && !extractSuccessful)
{
// We found an unpacker but it failed
MessageBox.Show($"There's been an error unpacking the following file:\n{localFileName}", "Conduit error: Unpack failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (unpacker != null && extractSuccessful)
{
// We found and successfully used an unpacker, but couldn't find a runner
MessageBox.Show($"We couldn't find a way to run the following file:\n{localFileName}", "Conduit error: Run failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (unpacker == null)
{
// We couldn't find an unpacker
MessageBox.Show($"We couldn't find a way to unpack the following file:\n{localFileName}", "Conduit error: Unpack failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else if (runnables.Count == 1)
{
runnables[0].Runner.Run(runnables[0].Path);
var result = runnables[0].Runner.Run(runnables[0].Path);
if (!result)
{
MessageBox.Show($"There was an error starting this file:\n{runnables[0].Path}", "Conduit error: Run failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else if (runnables.Count > 1)
{
SelectRunnableDialog dlg = new SelectRunnableDialog(runnables);
DialogResult result = dlg.ShowDialog();
if (result == DialogResult.OK)
DialogResult dlgResult = dlg.ShowDialog();
if (dlgResult == DialogResult.OK)
{
if (dlg.SelectedRunnable != null)
{
dlg.SelectedRunnable.Runner.Run(dlg.SelectedRunnable.Path);
var result = dlg.SelectedRunnable.Runner.Run(dlg.SelectedRunnable.Path);
if (!result)
{
MessageBox.Show($"There was an error starting this file:\n{dlg.SelectedRunnable.Path}", "Conduit error: Run failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,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("1.4.1.0")]
[assembly: AssemblyFileVersion("1.4.1.0")]
[assembly: AssemblyVersion("1.4.2.0")]
[assembly: AssemblyFileVersion("1.4.2.0")]
6 changes: 4 additions & 2 deletions Source/Runners/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public List<string> GetRunnableFiles(string demoDir)
}
}

public void Run(string path)
public bool Run(string path)
{
string browserPath = Settings.Options.BrowserPath;
bool browserFound = false;
Expand Down Expand Up @@ -78,8 +78,10 @@ public void Run(string path)
arguments += $"\"file://{path}\"";
startInfo.Arguments = arguments;
startInfo.WorkingDirectory = Path.GetDirectoryName(path);
Process.Start(startInfo);
var process = Process.Start(startInfo);
return process != null;
}
return false;
}
}
}
6 changes: 4 additions & 2 deletions Source/Runners/DOSBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public List<string> GetRunnableFiles(string demoDir)
return result.Where(s => !WindowsExecutable.IsWindowsExecutable(s)).ToList();
}

public void Run(string path)
public bool Run(string path)
{
string dosboxPath = Settings.Options.DOSBoxPath;
bool dosboxFound = false;
Expand Down Expand Up @@ -77,8 +77,10 @@ public void Run(string path)
arguments.Add($"-noautoexec");
arguments.Add($"-exit");
startInfo.Arguments = string.Join(" ",arguments);
Process.Start(startInfo);
var process = Process.Start(startInfo);
return process != null;
}
return false;
}
}
}
2 changes: 1 addition & 1 deletion Source/Runners/IRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public interface IRunner
// are not shown.
uint Priority { get; }

void Run(string path);
bool Run(string path);
}
}
5 changes: 3 additions & 2 deletions Source/Runners/Media.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ public List<string> GetRunnableFiles(string demoDir)
return result;
}

public void Run(string path)
public bool Run(string path)
{
ProcessStartInfo startInfo = new ProcessStartInfo(Path.GetFileName(path));
startInfo.WorkingDirectory = Path.GetDirectoryName(path);
Process.Start(startInfo);
var process = Process.Start(startInfo);
return process != null;
}
}
}
6 changes: 4 additions & 2 deletions Source/Runners/PICO8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public List<string> GetRunnableFiles(string demoDir)
return result.Where(s => !WindowsExecutable.IsWindowsExecutable(s)).ToList();
}

public void Run(string path)
public bool Run(string path)
{
string pico8Path = Settings.Options.PICO8Path;
bool pico8Found = false;
Expand Down Expand Up @@ -76,8 +76,10 @@ public void Run(string path)
arguments.Add($"-run");
arguments.Add($"\"{path}\"");
startInfo.Arguments = string.Join(" ",arguments);
Process.Start(startInfo);
var process = Process.Start(startInfo);
return process != null;
}
return false;
}
}
}
6 changes: 4 additions & 2 deletions Source/Runners/VICE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public List<string> GetRunnableFiles(string demoDir)
}
}

public void Run(string path)
public bool Run(string path)
{
string vicePath = Settings.Options.VicePath;
bool viceFound = false;
Expand Down Expand Up @@ -73,8 +73,10 @@ public void Run(string path)
ProcessStartInfo startInfo = new ProcessStartInfo(vicePath);
startInfo.Arguments = $"\"{Path.GetFileName(path)}\"";
startInfo.WorkingDirectory = Path.GetDirectoryName(path);
Process.Start(startInfo);
var process = Process.Start(startInfo);
return process != null;
}
return false;
}
}
}
5 changes: 3 additions & 2 deletions Source/Runners/WindowsExecutable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ public List<string> GetRunnableFiles(string demoDir)
return result.Where(s => IsWindowsExecutable(s)).ToList();
}

public void Run(string path)
public bool Run(string path)
{
ProcessStartInfo startInfo = new ProcessStartInfo(Path.GetFileName(path));
startInfo.WorkingDirectory = Path.GetDirectoryName(path);
Process.Start(startInfo);
var process = Process.Start(startInfo);
return process != null;
}
}
}
26 changes: 19 additions & 7 deletions Source/Unpackers/Zip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,35 @@ public async Task<bool> Unpack(string archiveFile, string targetDirectoryPath)
CurrentFile = i++,
CurrentFilename = entry.FullName,
});
await Task.Run(() =>
bool result = await Task.Run(() =>
{
try
{
if (entry.FullName.EndsWith("/"))
if (!entry.FullName.EndsWith("/") )
{
Directory.CreateDirectory(Path.Combine(targetDirectoryPath, entry.FullName));
}
else
{
entry.ExtractToFile(Path.Combine(targetDirectoryPath, entry.FullName));
var path = Path.Combine(targetDirectoryPath, entry.FullName);
if(File.Exists(path))
{
return true;
}
var dir = Path.GetDirectoryName(path);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
entry.ExtractToFile(path);
}
return true;
}
catch (IOException)
{
return false;
}
});
if (!result)
{
return false;
}
}
}
return true;
Expand Down

0 comments on commit ea78565

Please sign in to comment.