Clean up ToolHelpers.cs and reduce a lot of boilerplate code
This commit is contained in:
parent
df2dd2df9b
commit
74025d82c9
|
@ -139,7 +139,12 @@ namespace BizHawk.Client.EmuHawk
|
|||
|
||||
private static bool SaveAs()
|
||||
{
|
||||
var file = ToolHelpers.GetCheatSaveFileFromUser(Global.CheatList.CurrentFileName);
|
||||
var file = ToolHelpers.SaveFileDialog(
|
||||
Global.CheatList.CurrentFileName,
|
||||
PathManager.GetCheatsPath(Global.Game),
|
||||
"Cheat Files",
|
||||
"cht");
|
||||
|
||||
return file != null && Global.CheatList.SaveFile(file.FullName);
|
||||
}
|
||||
|
||||
|
@ -384,7 +389,13 @@ namespace BizHawk.Client.EmuHawk
|
|||
private void OpenMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
var append = sender == AppendMenuItem;
|
||||
LoadFile(ToolHelpers.GetCheatFileFromUser(Global.CheatList.CurrentFileName), append);
|
||||
var file = ToolHelpers.OpenFileDialog(
|
||||
Global.CheatList.CurrentFileName,
|
||||
PathManager.GetCheatsPath(Global.Game),
|
||||
"Cheat Files",
|
||||
"cht");
|
||||
|
||||
LoadFile(file, append);
|
||||
}
|
||||
|
||||
private void SaveMenuItem_Click(object sender, EventArgs e)
|
||||
|
|
|
@ -165,7 +165,12 @@ namespace BizHawk.Client.EmuHawk
|
|||
var result = MessageBox.Show(this, "OK to load new CDL?", "Query", MessageBoxButtons.YesNo);
|
||||
if (result == DialogResult.Yes)
|
||||
{
|
||||
var file = ToolHelpers.GetCdlFileFromUser(_currentFileName);
|
||||
var file = ToolHelpers.OpenFileDialog(
|
||||
_currentFileName,
|
||||
PathManager.MakeAbsolutePath(Global.Config.PathEntries.LogPathFragment, null),
|
||||
"Code Data Logger Files",
|
||||
"cdl");
|
||||
|
||||
if (file != null)
|
||||
{
|
||||
using (var fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
|
||||
|
@ -207,7 +212,12 @@ namespace BizHawk.Client.EmuHawk
|
|||
}
|
||||
else
|
||||
{
|
||||
var file = ToolHelpers.GetCdlSaveFileFromUser(_currentFileName);
|
||||
var file = ToolHelpers.SaveFileDialog(
|
||||
_currentFileName,
|
||||
PathManager.MakeAbsolutePath(Global.Config.PathEntries.LogPathFragment, null),
|
||||
"Code Data Logger Files",
|
||||
"cdl");
|
||||
|
||||
if (file != null)
|
||||
{
|
||||
using (var fs = new FileStream(file.FullName, FileMode.Create, FileAccess.Write))
|
||||
|
@ -228,7 +238,12 @@ namespace BizHawk.Client.EmuHawk
|
|||
}
|
||||
else
|
||||
{
|
||||
var file = ToolHelpers.GetCdlFileFromUser(_currentFileName);
|
||||
var file = ToolHelpers.OpenFileDialog(
|
||||
_currentFileName,
|
||||
PathManager.MakeAbsolutePath(Global.Config.PathEntries.LogPathFragment, null),
|
||||
"Code Data Logger Files",
|
||||
"cdl");
|
||||
|
||||
if (file != null)
|
||||
{
|
||||
using (var fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
|
||||
|
|
|
@ -57,10 +57,15 @@ namespace BizHawk.Client.EmuHawk
|
|||
var filename = CurrentTasMovie.Filename;
|
||||
if (string.IsNullOrWhiteSpace(filename) || filename == DefaultTasProjName())
|
||||
{
|
||||
filename = "";
|
||||
filename = string.Empty;
|
||||
}
|
||||
|
||||
var file = ToolHelpers.GetTasProjFileFromUser(filename);
|
||||
var file = ToolHelpers.OpenFileDialog(
|
||||
filename,
|
||||
PathManager.MakeAbsolutePath(Global.Config.PathEntries.MoviesPathFragment, null),
|
||||
"Tas Project Files",
|
||||
"tasproj");
|
||||
|
||||
if (file != null)
|
||||
{
|
||||
LoadFile(file);
|
||||
|
@ -99,7 +104,12 @@ namespace BizHawk.Client.EmuHawk
|
|||
filename = SuggestedTasProjName();
|
||||
}
|
||||
|
||||
var file = ToolHelpers.GetTasProjSaveFileFromUser(filename);
|
||||
var file = ToolHelpers.SaveFileDialog(
|
||||
filename,
|
||||
PathManager.MakeAbsolutePath(Global.Config.PathEntries.MoviesPathFragment, null),
|
||||
"Tas Project Files",
|
||||
"tasproj");
|
||||
|
||||
if (file != null)
|
||||
{
|
||||
CurrentTasMovie.Filename = file.FullName;
|
||||
|
|
|
@ -14,17 +14,22 @@ namespace BizHawk.Client.EmuHawk
|
|||
{
|
||||
public static class ToolHelpers
|
||||
{
|
||||
public static FileInfo GetTasProjFileFromUser(string currentFile)
|
||||
public static FileInfo OpenFileDialog(string currentFile, string path, string fileType, string fileExt)
|
||||
{
|
||||
var ofd = new OpenFileDialog();
|
||||
if (!string.IsNullOrWhiteSpace(currentFile))
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
ofd.FileName = Path.GetFileNameWithoutExtension(currentFile);
|
||||
Directory.CreateDirectory(path);
|
||||
}
|
||||
|
||||
ofd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.MoviesPathFragment, null);
|
||||
ofd.Filter = "Tas Project Files (*.tasproj)|*.tasproj|All Files|*.*";
|
||||
ofd.RestoreDirectory = true;
|
||||
var ofd = new OpenFileDialog
|
||||
{
|
||||
FileName = !string.IsNullOrWhiteSpace(currentFile)
|
||||
? Path.GetFileName(currentFile)
|
||||
: PathManager.FilesystemSafeName(Global.Game) + "." + fileExt,
|
||||
InitialDirectory = path,
|
||||
Filter = string.Format("{0} (*.{1})|*.{1}|All Files|*.*", fileType, fileExt),
|
||||
RestoreDirectory = true
|
||||
};
|
||||
|
||||
var result = ofd.ShowHawkDialog();
|
||||
if (result != DialogResult.OK)
|
||||
|
@ -35,22 +40,23 @@ namespace BizHawk.Client.EmuHawk
|
|||
return new FileInfo(ofd.FileName);
|
||||
}
|
||||
|
||||
public static FileInfo GetTasProjSaveFileFromUser(string currentFile)
|
||||
public static FileInfo SaveFileDialog(string currentFile, string path, string fileType, string fileExt)
|
||||
{
|
||||
var sfd = new SaveFileDialog();
|
||||
if (!string.IsNullOrWhiteSpace(currentFile))
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
sfd.FileName = Path.GetFileNameWithoutExtension(currentFile);
|
||||
sfd.InitialDirectory = Path.GetDirectoryName(currentFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
sfd.FileName = PathManager.FilesystemSafeName(Global.Game);
|
||||
sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.MoviesPathFragment, null);
|
||||
Directory.CreateDirectory(path);
|
||||
}
|
||||
|
||||
sfd.Filter = "Tas Project Files (*.tasproj)|*.tasproj|All Files|*.*";
|
||||
sfd.RestoreDirectory = true;
|
||||
var sfd = new SaveFileDialog
|
||||
{
|
||||
FileName = !string.IsNullOrWhiteSpace(currentFile)
|
||||
? Path.GetFileName(currentFile)
|
||||
: PathManager.FilesystemSafeName(Global.Game) + "." + fileExt,
|
||||
InitialDirectory = path,
|
||||
Filter = string.Format("{0} (*.{1})|*.{1}|All Files|*.*", fileType, fileExt),
|
||||
RestoreDirectory = true,
|
||||
};
|
||||
|
||||
var result = sfd.ShowHawkDialog();
|
||||
if (result != DialogResult.OK)
|
||||
{
|
||||
|
@ -62,143 +68,13 @@ namespace BizHawk.Client.EmuHawk
|
|||
|
||||
public static FileInfo GetWatchFileFromUser(string currentFile)
|
||||
{
|
||||
var ofd = new OpenFileDialog();
|
||||
if (!string.IsNullOrWhiteSpace(currentFile))
|
||||
{
|
||||
ofd.FileName = Path.GetFileNameWithoutExtension(currentFile);
|
||||
}
|
||||
|
||||
ofd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.WatchPathFragment, null);
|
||||
ofd.Filter = "Watch Files (*.wch)|*.wch|All Files|*.*";
|
||||
ofd.RestoreDirectory = true;
|
||||
|
||||
var result = ofd.ShowHawkDialog();
|
||||
if (result != DialogResult.OK)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new FileInfo(ofd.FileName);
|
||||
return OpenFileDialog(currentFile, PathManager.MakeAbsolutePath(Global.Config.PathEntries.WatchPathFragment, null), "Watch Files", "wch");
|
||||
}
|
||||
|
||||
public static FileInfo GetWatchSaveFileFromUser(string currentFile)
|
||||
{
|
||||
var sfd = new SaveFileDialog();
|
||||
if (!string.IsNullOrWhiteSpace(currentFile))
|
||||
{
|
||||
sfd.FileName = Path.GetFileNameWithoutExtension(currentFile);
|
||||
sfd.InitialDirectory = Path.GetDirectoryName(currentFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
sfd.FileName = PathManager.FilesystemSafeName(Global.Game);
|
||||
sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.WatchPathFragment, null);
|
||||
}
|
||||
|
||||
sfd.Filter = "Watch Files (*.wch)|*.wch|All Files|*.*";
|
||||
sfd.RestoreDirectory = true;
|
||||
var result = sfd.ShowHawkDialog();
|
||||
if (result != DialogResult.OK)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new FileInfo(sfd.FileName);
|
||||
}
|
||||
|
||||
public static FileInfo GetCheatFileFromUser(string currentFile)
|
||||
{
|
||||
var ofd = new OpenFileDialog
|
||||
{
|
||||
FileName = !string.IsNullOrWhiteSpace(currentFile)
|
||||
? Path.GetFileNameWithoutExtension(currentFile)
|
||||
: PathManager.FilesystemSafeName(Global.Game),
|
||||
InitialDirectory = PathManager.GetCheatsPath(Global.Game),
|
||||
Filter = "Cheat Files (*.cht)|*.cht|All Files|*.*",
|
||||
RestoreDirectory = true
|
||||
};
|
||||
|
||||
var result = ofd.ShowHawkDialog();
|
||||
if (result != DialogResult.OK)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new FileInfo(ofd.FileName);
|
||||
}
|
||||
|
||||
public static FileInfo GetCheatSaveFileFromUser(string currentFile)
|
||||
{
|
||||
var cheatsPath = PathManager.GetCheatsPath(Global.Game);
|
||||
if (!Directory.Exists(cheatsPath))
|
||||
{
|
||||
Directory.CreateDirectory(cheatsPath);
|
||||
}
|
||||
|
||||
var sfd = new SaveFileDialog
|
||||
{
|
||||
Filter = "Cheat Files (*.cht)|*.cht|All Files|*.*",
|
||||
RestoreDirectory = true,
|
||||
InitialDirectory = cheatsPath,
|
||||
FileName = !string.IsNullOrWhiteSpace(currentFile)
|
||||
? Path.GetFileNameWithoutExtension(currentFile)
|
||||
: PathManager.FilesystemSafeName(Global.Game)
|
||||
};
|
||||
|
||||
var result = sfd.ShowHawkDialog();
|
||||
if (result != DialogResult.OK)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new FileInfo(sfd.FileName);
|
||||
}
|
||||
|
||||
public static FileInfo GetCdlFileFromUser(string currentFile)
|
||||
{
|
||||
var ofd = new OpenFileDialog
|
||||
{
|
||||
Filter = "Code Data Logger Files (*.cdl)|*.cdl|All Files|*.*",
|
||||
InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.LogPathFragment, null),
|
||||
RestoreDirectory = true
|
||||
};
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(currentFile))
|
||||
{
|
||||
ofd.FileName = Path.GetFileNameWithoutExtension(currentFile);
|
||||
}
|
||||
|
||||
var result = ofd.ShowHawkDialog();
|
||||
if (result != DialogResult.OK)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new FileInfo(ofd.FileName);
|
||||
}
|
||||
|
||||
public static FileInfo GetCdlSaveFileFromUser(string currentFile)
|
||||
{
|
||||
var sfd = new SaveFileDialog
|
||||
{
|
||||
Filter = "Code Data Logger Files (*.cdl)|*.cdl|All Files|*.*",
|
||||
InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.LogPathFragment, null),
|
||||
RestoreDirectory = true
|
||||
};
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(currentFile))
|
||||
{
|
||||
sfd.FileName = Path.GetFileNameWithoutExtension(currentFile);
|
||||
}
|
||||
|
||||
var result = sfd.ShowHawkDialog();
|
||||
if (result != DialogResult.OK)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new FileInfo(sfd.FileName);
|
||||
}
|
||||
return SaveFileDialog(currentFile, PathManager.MakeAbsolutePath(Global.Config.PathEntries.WatchPathFragment, null), "Watch Files", "wch");
|
||||
}
|
||||
|
||||
public static void UpdateCheatRelatedTools(object sender, CheatCollection.CheatListEventArgs e)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue