Invert some conditionals to reduce nesting

This commit is contained in:
YoshiRulz 2022-08-07 17:37:39 +10:00
parent df63853cd5
commit c153505b58
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
8 changed files with 167 additions and 197 deletions

View File

@ -1544,31 +1544,25 @@ namespace BizHawk.Client.EmuHawk
private void Ti83LoadTIFileMenuItem_Click(object sender, EventArgs e)
{
if (Emulator is TI83 ti83)
if (Emulator is not TI83 ti83) return;
using var ofd = new OpenFileDialog
{
using var ofd = new OpenFileDialog
Filter = new FilesystemFilterSet(new FilesystemFilter("TI-83 Program Files", new[] { "83p", "8xp" })).ToString(),
InitialDirectory = Config.PathEntries.RomAbsolutePath(Emulator.SystemId),
RestoreDirectory = true
};
if (!ofd.ShowDialog().IsOk()) return;
try
{
ti83.LinkPort.SendFileToCalc(File.OpenRead(ofd.FileName), true);
}
catch (IOException ex)
{
var message =
$"Invalid file format. Reason: {ex.Message} \nForce transfer? This may cause the calculator to crash.";
if (this.ShowMessageBox3(owner: null, message, "Upload Failed", EMsgBoxIcon.Question) == true)
{
Filter = new FilesystemFilterSet(new FilesystemFilter("TI-83 Program Files", new[] { "83p", "8xp" })).ToString(),
InitialDirectory = Config.PathEntries.RomAbsolutePath(Emulator.SystemId),
RestoreDirectory = true
};
if (ofd.ShowDialog().IsOk())
{
try
{
ti83.LinkPort.SendFileToCalc(File.OpenRead(ofd.FileName), true);
}
catch (IOException ex)
{
var message =
$"Invalid file format. Reason: {ex.Message} \nForce transfer? This may cause the calculator to crash.";
if (this.ShowMessageBox3(owner: null, message, "Upload Failed", EMsgBoxIcon.Question) == true)
{
ti83.LinkPort.SendFileToCalc(File.OpenRead(ofd.FileName), false);
}
}
ti83.LinkPort.SendFileToCalc(File.OpenRead(ofd.FileName), false);
}
}
}

View File

@ -268,37 +268,36 @@ namespace BizHawk.Client.EmuHawk
Filter = new FilesystemFilter(".CGP Files", new[] { "cgp" }).ToString(),
FileName = _pathSelection
};
if (this.ShowDialogAsChild(ofd).IsOk())
{
rbUser.Checked = true;
var choice = Path.GetFullPath(ofd.FileName);
if (!this.ShowDialogAsChild(ofd).IsOk()) return;
rbUser.Checked = true;
var choice = Path.GetFullPath(ofd.FileName);
//test the preset
using (var stream = File.OpenRead(choice))
//test the preset
using (var stream = File.OpenRead(choice))
{
var cgp = new RetroShaderPreset(stream);
// try compiling it
bool ok = false;
string errors = "";
try
{
var cgp = new RetroShaderPreset(stream);
// try compiling it
bool ok = false;
string errors = "";
try
{
var filter = new RetroShaderChain(_gl, cgp, Path.GetDirectoryName(choice));
ok = filter.Available;
errors = filter.Errors;
}
catch {}
if (!ok)
{
using var errorForm = new ExceptionBox(errors);
this.ShowDialogAsChild(errorForm);
return;
}
var filter = new RetroShaderChain(_gl, cgp, Path.GetDirectoryName(choice));
ok = filter.Available;
errors = filter.Errors;
}
catch {}
if (!ok)
{
using var errorForm = new ExceptionBox(errors);
this.ShowDialogAsChild(errorForm);
return;
}
_pathSelection = choice;
RefreshState();
}
_pathSelection = choice;
RefreshState();
}
private void CheckLetterbox_CheckedChanged(object sender, EventArgs e)

View File

@ -436,79 +436,77 @@ namespace BizHawk.Client.EmuHawk
RestoreDirectory = true
};
string firmwarePath = _pathEntries.FirmwareAbsolutePath();
if (!ofd.ShowDialog().IsOk()) return;
if (ofd.ShowDialog() == DialogResult.OK)
// remember the location we selected this firmware from, maybe there are others
_currSelectorDir = Path.GetDirectoryName(ofd.FileName);
try
{
// remember the location we selected this firmware from, maybe there are others
_currSelectorDir = Path.GetDirectoryName(ofd.FileName);
try
using var hf = new HawkFile(ofd.FileName);
// for each selected item, set the user choice (even though multiple selection for this operation is no longer allowed)
foreach (ListViewItem lvi in lvFirmwares.SelectedItems)
{
using var hf = new HawkFile(ofd.FileName);
// for each selected item, set the user choice (even though multiple selection for this operation is no longer allowed)
foreach (ListViewItem lvi in lvFirmwares.SelectedItems)
var fr = (FirmwareRecord) lvi.Tag;
string filePath = ofd.FileName;
// if the selected file is an archive, allow the user to pick the inside file
// to always be copied to the global firmwares directory
if (hf.IsArchive)
{
var fr = (FirmwareRecord) lvi.Tag;
string filePath = ofd.FileName;
var ac = new ArchiveChooser(new HawkFile(filePath));
int memIdx;
// if the selected file is an archive, allow the user to pick the inside file
// to always be copied to the global firmwares directory
if (hf.IsArchive)
if (ac.ShowDialog(this) == DialogResult.OK)
{
var ac = new ArchiveChooser(new HawkFile(filePath));
int memIdx;
if (ac.ShowDialog(this) == DialogResult.OK)
{
memIdx = ac.SelectedMemberIndex;
}
else
{
return;
}
var insideFile = hf.BindArchiveMember(memIdx);
var fileData = insideFile.ReadAllBytes();
// write to file in the firmwares folder
File.WriteAllBytes(Path.Combine(firmwarePath, insideFile.Name), fileData);
filePath = Path.Combine(firmwarePath, insideFile.Name);
memIdx = ac.SelectedMemberIndex;
}
else
{
// selected file is not an archive
// check whether this file is currently outside of the global firmware directory
if (_currSelectorDir != firmwarePath)
return;
}
var insideFile = hf.BindArchiveMember(memIdx);
var fileData = insideFile.ReadAllBytes();
// write to file in the firmwares folder
File.WriteAllBytes(Path.Combine(firmwarePath, insideFile.Name), fileData);
filePath = Path.Combine(firmwarePath, insideFile.Name);
}
else
{
// selected file is not an archive
// check whether this file is currently outside of the global firmware directory
if (_currSelectorDir != firmwarePath)
{
var askMoveResult = this.ModalMessageBox2("The selected custom firmware does not reside in the root of the global firmware directory.\nDo you want to copy it there?", "Import Custom Firmware");
if (askMoveResult)
{
var askMoveResult = this.ModalMessageBox2("The selected custom firmware does not reside in the root of the global firmware directory.\nDo you want to copy it there?", "Import Custom Firmware");
if (askMoveResult)
try
{
try
{
var fi = new FileInfo(filePath);
filePath = Path.Combine(firmwarePath, fi.Name);
File.Copy(ofd.FileName, filePath);
}
catch (Exception ex)
{
this.ModalMessageBox($"There was an issue copying the file. The customization has NOT been set.\n\n{ex.StackTrace}");
continue;
}
var fi = new FileInfo(filePath);
filePath = Path.Combine(firmwarePath, fi.Name);
File.Copy(ofd.FileName, filePath);
}
catch (Exception ex)
{
this.ModalMessageBox($"There was an issue copying the file. The customization has NOT been set.\n\n{ex.StackTrace}");
continue;
}
}
}
_firmwareUserSpecifications[fr.ID.ConfigKey] = filePath;
}
}
catch (Exception ex)
{
this.ModalMessageBox($"There was an issue during the process. The customization has NOT been set.\n\n{ex.StackTrace}");
return;
}
DoScan();
_firmwareUserSpecifications[fr.ID.ConfigKey] = filePath;
}
}
catch (Exception ex)
{
this.ModalMessageBox($"There was an issue during the process. The customization has NOT been set.\n\n{ex.StackTrace}");
return;
}
DoScan();
}
private void TsmiClearCustomization_Click(object sender, EventArgs e)

View File

@ -511,23 +511,19 @@ namespace BizHawk.Client.EmuHawk
Filter = new FilesystemFilterSet(FilesystemFilter.BizHawkMovies, FilesystemFilter.TAStudioProjects).ToString(),
InitialDirectory = _config.PathEntries.MovieAbsolutePath()
};
var result = this.ShowDialogWithTempMute(ofd);
if (result == DialogResult.OK)
if (!this.ShowDialogWithTempMute(ofd).IsOk()) return;
var file = new FileInfo(ofd.FileName);
if (!file.Exists)
{
var file = new FileInfo(ofd.FileName);
if (!file.Exists)
{
return;
}
return;
}
int? index = AddMovieToList(ofd.FileName, true);
RefreshMovieList();
if (index.HasValue)
{
MovieView.SelectedIndices.Clear();
MovieView.Items[index.Value].Selected = true;
}
int? index = AddMovieToList(ofd.FileName, true);
RefreshMovieList();
if (index.HasValue)
{
MovieView.SelectedIndices.Clear();
MovieView.Items[index.Value].Selected = true;
}
}

View File

@ -125,22 +125,17 @@ namespace BizHawk.Client.EmuHawk
private void ButtonDump_Click(object sender, EventArgs e)
{
if (_mostRecentResults != null)
{
using var sfd = new SaveFileDialog();
var result = sfd.ShowDialog(this);
if (result == DialogResult.OK)
{
using TextWriter tw = new StreamWriter(sfd.FileName);
foreach (var r in _mostRecentResults)
{
r.DumpTo(tw);
}
}
}
else
if (_mostRecentResults is null)
{
DialogController.ShowMessageBox("No results to save!");
return;
}
using var sfd = new SaveFileDialog();
if (!sfd.ShowDialog(this).IsOk()) return;
using TextWriter tw = new StreamWriter(sfd.FileName);
foreach (var r in _mostRecentResults)
{
r.DumpTo(tw);
}
}
}

View File

@ -1326,13 +1326,10 @@ namespace BizHawk.Client.EmuHawk
Filter = new FilesystemFilterSet(new FilesystemFilter("Text Table Files", new[] { "tbl" })).ToString(),
RestoreDirectory = false
};
if (this.ShowDialogWithTempMute(ofd) == DialogResult.OK)
{
LoadTable(ofd.FileName);
RecentTables.Add(ofd.FileName);
GeneralUpdate();
}
if (!this.ShowDialogWithTempMute(ofd).IsOk()) return;
LoadTable(ofd.FileName);
RecentTables.Add(ofd.FileName);
GeneralUpdate();
}
private void CloseTableFileMenuItem_Click(object sender, EventArgs e)

View File

@ -856,20 +856,18 @@ namespace BizHawk.Client.EmuHawk
};
var result = this.ShowDialogWithTempMute(sfd);
if (result.IsOk() && !string.IsNullOrWhiteSpace(sfd.FileName))
if (!result.IsOk() || string.IsNullOrWhiteSpace(sfd.FileName)) return;
string defaultTemplate = "while true do\n\temu.frameadvance();\nend";
File.WriteAllText(sfd.FileName, defaultTemplate);
LuaImp.ScriptList.Add(new LuaFile(Path.GetFileNameWithoutExtension(sfd.FileName), sfd.FileName));
Config.RecentLua.Add(sfd.FileName);
UpdateDialog();
Process.Start(new ProcessStartInfo
{
string defaultTemplate = "while true do\n\temu.frameadvance();\nend";
File.WriteAllText(sfd.FileName, defaultTemplate);
LuaImp.ScriptList.Add(new LuaFile(Path.GetFileNameWithoutExtension(sfd.FileName), sfd.FileName));
Config.RecentLua.Add(sfd.FileName);
UpdateDialog();
Process.Start(new ProcessStartInfo
{
Verb = "Open",
FileName = sfd.FileName
});
AddFileWatches();
}
Verb = "Open",
FileName = sfd.FileName
});
AddFileWatches();
}
private void OpenScriptMenuItem_Click(object sender, EventArgs e)
@ -884,16 +882,14 @@ namespace BizHawk.Client.EmuHawk
Multiselect = true
};
var result = this.ShowDialogWithTempMute(ofd);
if (result.IsOk() && ofd.FileNames != null)
if (!result.IsOk() || ofd.FileNames is null) return;
foreach (var file in ofd.FileNames)
{
foreach (var file in ofd.FileNames)
{
LoadLuaFile(file);
Config.RecentLua.Add(file);
}
UpdateDialog();
LoadLuaFile(file);
Config.RecentLua.Add(file);
}
UpdateDialog();
}
private void ToggleScriptMenuItem_Click(object sender, EventArgs e)
@ -1006,20 +1002,17 @@ namespace BizHawk.Client.EmuHawk
OverwritePrompt = true,
Filter = new FilesystemFilterSet(FilesystemFilter.LuaScripts).ToString()
};
if (sfd.ShowDialog().IsOk())
if (!sfd.ShowDialog().IsOk()) return;
string text = File.ReadAllText(script.Path);
File.WriteAllText(sfd.FileName, text);
LuaImp.ScriptList.Add(new LuaFile(Path.GetFileNameWithoutExtension(sfd.FileName), sfd.FileName));
Config.RecentLua.Add(sfd.FileName);
UpdateDialog();
Process.Start(new ProcessStartInfo
{
string text = File.ReadAllText(script.Path);
File.WriteAllText(sfd.FileName, text);
LuaImp.ScriptList.Add(new LuaFile(Path.GetFileNameWithoutExtension(sfd.FileName), sfd.FileName));
Config.RecentLua.Add(sfd.FileName);
UpdateDialog();
Process.Start(new ProcessStartInfo
{
Verb = "Open",
FileName = sfd.FileName
});
}
Verb = "Open",
FileName = sfd.FileName
});
}
}

View File

@ -77,30 +77,28 @@ namespace BizHawk.Client.EmuHawk
private void OpenTasMenuItem_Click(object sender, EventArgs e)
{
if (AskSaveChanges())
if (!AskSaveChanges()) return;
var filename = CurrentTasMovie.Filename;
if (string.IsNullOrWhiteSpace(filename) || filename == DefaultTasProjName())
{
var filename = CurrentTasMovie.Filename;
if (string.IsNullOrWhiteSpace(filename) || filename == DefaultTasProjName())
{
filename = "";
}
filename = "";
}
// need to be fancy here, so call the ofd constructor directly instead of helper
var ofd = new OpenFileDialog
{
FileName = filename,
InitialDirectory = Config.PathEntries.MovieAbsolutePath(),
Filter = new FilesystemFilterSet(
new FilesystemFilter("All Available Files", MovieService.MovieExtensions.Reverse().ToArray()),
FilesystemFilter.TAStudioProjects,
FilesystemFilter.BizHawkMovies
).ToString()
};
// need to be fancy here, so call the ofd constructor directly instead of helper
var ofd = new OpenFileDialog
{
FileName = filename,
InitialDirectory = Config.PathEntries.MovieAbsolutePath(),
Filter = new FilesystemFilterSet(
new FilesystemFilter("All Available Files", MovieService.MovieExtensions.Reverse().ToArray()),
FilesystemFilter.TAStudioProjects,
FilesystemFilter.BizHawkMovies
).ToString()
};
if (this.ShowDialogWithTempMute(ofd).IsOk())
{
LoadMovieFile(ofd.FileName, false);
}
if (this.ShowDialogWithTempMute(ofd).IsOk())
{
LoadMovieFile(ofd.FileName, false);
}
}