Add and use 2 extension methods for splitting path into dir+filename

This commit is contained in:
YoshiRulz 2022-08-17 14:34:52 +10:00
parent dce961357a
commit 7cde8bb466
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
20 changed files with 100 additions and 76 deletions

View File

@ -3,6 +3,7 @@ using System.IO;
using System.Linq;
using BizHawk.Common;
using BizHawk.Common.PathExtensions;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores.Consoles.Nintendo.Gameboy;
using BizHawk.Emulation.Cores.Consoles.Nintendo.NDS;
@ -289,13 +290,13 @@ namespace BizHawk.Client.Common
internal static string ConvertFileNameToTasMovie(string oldFileName)
{
string newFileName = Path.ChangeExtension(oldFileName, $".{TasMovie.Extension}");
var (dir, fileNoExt, _) = oldFileName.SplitPathToDirFileAndExt();
var newFileName = Path.Combine(dir, $"{fileNoExt}.{TasMovie.Extension}");
int fileSuffix = 0;
while (File.Exists(newFileName))
{
// Using this should hopefully be system agnostic
var temp_path = Path.Combine(Path.GetDirectoryName(oldFileName), Path.GetFileNameWithoutExtension(oldFileName));
newFileName = $"{temp_path} {++fileSuffix}.{TasMovie.Extension}";
newFileName = Path.Combine(dir, $"{fileNoExt} {++fileSuffix}.{TasMovie.Extension}");
}
return newFileName;

View File

@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using BizHawk.Common.PathExtensions;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.DiscSystem;
@ -120,8 +122,7 @@ namespace BizHawk.Client.DiscoHawk
foreach (var file in files)
{
using var disc = Disc.LoadAutomagic(file);
var path = Path.GetDirectoryName(file);
var filename = Path.GetFileNameWithoutExtension(file);
var (path, filename, _) = file.SplitPathToDirFileAndExt();
static bool? PromptForOverwrite(string mp3Path)
=> MessageBox.Show(
$"Do you want to overwrite existing files? Choosing \"No\" will simply skip those. You could also \"Cancel\" the extraction entirely.\n\ncaused by file: {mp3Path}",

View File

@ -9,6 +9,7 @@ using System.Threading;
using BizHawk.Client.Common;
using BizHawk.Common;
using BizHawk.Common.PathExtensions;
using BizHawk.Emulation.Common;
// some helpful p/invoke from http://www.codeproject.com/KB/audio-video/Motion_Detection.aspx?msg=1142967
@ -54,9 +55,7 @@ namespace BizHawk.Client.EmuHawk
public static IEnumerator<string> CreateBasicNameProvider(string template)
{
string dir = Path.GetDirectoryName(template);
string baseName = Path.GetFileNameWithoutExtension(template);
string ext = Path.GetExtension(template);
var (dir, baseName, ext) = template.SplitPathToDirFileAndExt();
yield return template;
int counter = 1;
for (;;)

View File

@ -72,12 +72,9 @@ namespace BizHawk.Client.EmuHawk
public void OpenFile(string baseName)
{
_baseName = Path.Combine(
Path.GetDirectoryName(baseName),
Path.GetFileNameWithoutExtension(baseName));
_ext = Path.GetExtension(baseName);
var (dir, fileNoExt, ext) = baseName.SplitPathToDirFileAndExt();
_baseName = Path.Combine(dir!, fileNoExt);
_ext = ext;
_segment = 0;
OpenFileSegment();
}

View File

@ -4,6 +4,7 @@ using System.Drawing.Imaging;
using BizHawk.Bizware.BizwareGL;
using BizHawk.Client.Common;
using BizHawk.Common.PathExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk
@ -45,8 +46,8 @@ namespace BizHawk.Client.EmuHawk
public void AddFrame(IVideoProvider source)
{
string ext = Path.GetExtension(_baseName);
var name = Path.Combine(Path.GetDirectoryName(_baseName), $"{Path.GetFileNameWithoutExtension(_baseName)}_{_frame}{ext}");
var (dir, fileNoExt, ext) = _baseName.SplitPathToDirFileAndExt();
var name = Path.Combine(dir!, $"{fileNoExt}_{_frame}{ext}");
BitmapBuffer bb = new BitmapBuffer(source.BufferWidth, source.BufferHeight, source.GetVideoBuffer());
using var bmp = bb.ToSysdrawingBitmap();
if (ext.ToUpper() == ".PNG")

View File

@ -2,6 +2,7 @@
using System.IO;
using BizHawk.Client.Common;
using BizHawk.Common.PathExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk
@ -44,9 +45,8 @@ namespace BizHawk.Client.EmuHawk
public void OpenFile(string baseName)
{
_baseName = Path.Combine(
Path.GetDirectoryName(baseName) ?? "",
Path.GetFileNameWithoutExtension(baseName) ?? "");
var (dir, fileNoExt, _) = baseName.SplitPathToDirFileAndExt();
_baseName = Path.Combine(dir ?? string.Empty, fileNoExt);
_segment = 0;
StartSegment();

View File

@ -7,6 +7,7 @@ using System.Text;
using BizHawk.Emulation.Common;
using BizHawk.Bizware.BizwareGL;
using BizHawk.Client.Common;
using BizHawk.Common.PathExtensions;
namespace BizHawk.Client.EmuHawk
{
@ -37,9 +38,9 @@ namespace BizHawk.Client.EmuHawk
public void OpenFile(string projFile)
{
_mProjectFile = projFile;
_mBaseDirectory = Path.GetDirectoryName(_mProjectFile) ?? "";
string basename = Path.GetFileNameWithoutExtension(projFile);
string framesDirFragment = $"{basename}_frames";
var (dir, fileNoExt, _) = projFile.SplitPathToDirFileAndExt();
_mBaseDirectory = dir ?? string.Empty;
var framesDirFragment = $"{fileNoExt}_frames";
_mFramesDirectory = Path.Combine(_mBaseDirectory, framesDirFragment);
var sb = new StringBuilder();
sb.AppendLine("version=1");

View File

@ -5,6 +5,7 @@ using System.IO;
using BizHawk.Client.Common;
using BizHawk.Common.IOExtensions;
using BizHawk.Common.PathExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk
@ -261,14 +262,12 @@ namespace BizHawk.Client.EmuHawk
/// </summary>
private static IEnumerator<Stream> CreateStreamIterator(string template)
{
string dir = Path.GetDirectoryName(template) ?? "";
string baseName = Path.GetFileNameWithoutExtension(template) ?? "";
string ext = Path.GetExtension(template);
var (dir, baseName, ext) = template.SplitPathToDirFileAndExt();
yield return new FileStream(template, FileMode.Create);
int counter = 1;
while (true)
{
yield return new FileStream($"{Path.Combine(dir, baseName)}_{counter}{ext}", FileMode.Create);
yield return new FileStream($"{Path.Combine(dir ?? string.Empty, baseName)}_{counter}{ext}", FileMode.Create);
counter++;
}
}

View File

@ -12,6 +12,7 @@ using BizHawk.Client.Common;
using BizHawk.Client.EmuHawk.CustomControls;
using BizHawk.Client.EmuHawk.ToolExtensions;
using BizHawk.Common;
using BizHawk.Common.PathExtensions;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores;
using BizHawk.Emulation.Cores.Arcades.MAME;
@ -606,12 +607,11 @@ namespace BizHawk.Client.EmuHawk
private void ScreenshotAsMenuItem_Click(object sender, EventArgs e)
{
var path = $"{ScreenshotPrefix()}.{DateTime.Now:yyyy-MM-dd HH.mm.ss}.png";
var (dir, file) = $"{ScreenshotPrefix()}.{DateTime.Now:yyyy-MM-dd HH.mm.ss}.png".SplitPathToDirAndFile();
using var sfd = new SaveFileDialog
{
InitialDirectory = Path.GetDirectoryName(path),
FileName = Path.GetFileName(path),
InitialDirectory = dir,
FileName = file,
Filter = FilesystemFilter.PNGs.ToString()
};
@ -1146,11 +1146,11 @@ namespace BizHawk.Client.EmuHawk
private void SaveConfigAsMenuItem_Click(object sender, EventArgs e)
{
var path = _getConfigPath();
var (dir, file) = _getConfigPath().SplitPathToDirAndFile();
using var sfd = new SaveFileDialog
{
InitialDirectory = Path.GetDirectoryName(path),
FileName = Path.GetFileName(path),
InitialDirectory = dir,
FileName = file,
Filter = ConfigFileFSFilterString
};
@ -1168,11 +1168,11 @@ namespace BizHawk.Client.EmuHawk
private void LoadConfigFromMenuItem_Click(object sender, EventArgs e)
{
var path = _getConfigPath();
var (dir, file) = _getConfigPath().SplitPathToDirAndFile();
using var ofd = new OpenFileDialog
{
InitialDirectory = Path.GetDirectoryName(path),
FileName = Path.GetFileName(path),
InitialDirectory = dir,
FileName = file,
Filter = ConfigFileFSFilterString
};

View File

@ -7,6 +7,7 @@ using System.Collections.Generic;
using BizHawk.Common;
using BizHawk.Emulation.Common;
using BizHawk.Client.Common;
using BizHawk.Common.PathExtensions;
namespace BizHawk.Client.EmuHawk
{
@ -31,10 +32,10 @@ namespace BizHawk.Client.EmuHawk
public string FileName { get; }
public string ArchiveName { get; }
public FileInformation(string directory, string file, string archive)
public FileInformation((string Dir, string File) FilePathSplit, string archive)
{
DirectoryName = directory;
FileName = file;
DirectoryName = FilePathSplit.Dir;
FileName = FilePathSplit.File;
ArchiveName = archive;
}
}
@ -132,7 +133,7 @@ namespace BizHawk.Client.EmuHawk
foreach (string file in fileList)
{
var ext = Path.GetExtension(file)?.ToUpperInvariant() ?? "";
FileInformation fileInformation = new FileInformation(Path.GetDirectoryName(file), Path.GetFileName(file), archive);
FileInformation fileInformation = new(file.SplitPathToDirAndFile(), archive);
switch (ext)
{

View File

@ -1601,8 +1601,7 @@ namespace BizHawk.Client.EmuHawk
if (Config.LibretroCore != null)
{
ofd.FileName = Path.GetFileName(Config.LibretroCore);
ofd.InitialDirectory = Path.GetDirectoryName(Config.LibretroCore);
(ofd.InitialDirectory, ofd.FileName) = Config.LibretroCore.SplitPathToDirAndFile();
}
else
{

View File

@ -11,6 +11,7 @@ using BizHawk.Common;
using BizHawk.Client.Common;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Common.IOExtensions;
using BizHawk.Common.PathExtensions;
using BizHawk.Emulation.Common;
// notes: eventually, we intend to have a "firmware acquisition interface" exposed to the emulator cores.
@ -611,11 +612,8 @@ namespace BizHawk.Client.EmuHawk
}
// hmm they're different. import but rename it
string dir = Path.GetDirectoryName(target);
string ext = Path.GetExtension(target);
string name = Path.GetFileNameWithoutExtension(target);
name += " (variant)";
target = Path.Combine(dir, name) + ext;
var (dir, name, ext) = target.SplitPathToDirFileAndExt();
target = Path.Combine(dir!, $"{name} (variant)") + ext;
}
Directory.CreateDirectory(Path.GetDirectoryName(target));

View File

@ -278,10 +278,11 @@ namespace BizHawk.Client.EmuHawk
private void CreateFileWatcher(string path)
{
var (dir, file) = path.SplitPathToDirAndFile();
var watcher = new FileSystemWatcher
{
Path = Path.GetDirectoryName(path),
Filter = Path.GetFileName(path),
Path = dir,
Filter = file,
NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName,
EnableRaisingEvents = true
};
@ -652,8 +653,7 @@ namespace BizHawk.Client.EmuHawk
var sfd = new SaveFileDialog();
if (!string.IsNullOrWhiteSpace(LuaImp.ScriptList.Filename))
{
sfd.FileName = Path.GetFileNameWithoutExtension(LuaImp.ScriptList.Filename);
sfd.InitialDirectory = Path.GetDirectoryName(LuaImp.ScriptList.Filename);
(sfd.InitialDirectory, sfd.FileName, _) = LuaImp.ScriptList.Filename.SplitPathToDirFileAndExt();
}
else if (!Game.IsNullInstance())
{
@ -835,15 +835,22 @@ namespace BizHawk.Client.EmuHawk
private void NewScriptMenuItem_Click(object sender, EventArgs e)
{
string initDir;
string ext;
if (!string.IsNullOrWhiteSpace(LuaImp.ScriptList.Filename))
{
(initDir, ext, _) = LuaImp.ScriptList.Filename.SplitPathToDirFileAndExt();
}
else
{
initDir = Config!.PathEntries.LuaAbsolutePath();
ext = Path.GetFileNameWithoutExtension(Game.Name);
}
var sfd = new SaveFileDialog
{
InitialDirectory = !string.IsNullOrWhiteSpace(LuaImp.ScriptList.Filename)
? Path.GetDirectoryName(LuaImp.ScriptList.Filename)
: Config.PathEntries.LuaAbsolutePath(),
InitialDirectory = initDir,
DefaultExt = ".lua",
FileName = !string.IsNullOrWhiteSpace(LuaImp.ScriptList.Filename)
? Path.GetFileNameWithoutExtension(LuaImp.ScriptList.Filename)
: Path.GetFileNameWithoutExtension(Game.Name),
FileName = ext,
OverwritePrompt = true,
Filter = new FilesystemFilterSet(FilesystemFilter.LuaScripts).ToString()
};
@ -990,11 +997,12 @@ namespace BizHawk.Client.EmuHawk
return;
}
var (dir, fileNoExt, _) = script.Path.SplitPathToDirFileAndExt();
var sfd = new SaveFileDialog
{
InitialDirectory = Path.GetDirectoryName(script.Path),
InitialDirectory = dir,
DefaultExt = ".lua",
FileName = $"{Path.GetFileNameWithoutExtension(script.Path)} (1)",
FileName = $"{fileNoExt} (1)",
OverwritePrompt = true,
Filter = new FilesystemFilterSet(FilesystemFilter.LuaScripts).ToString()
};

View File

@ -150,6 +150,17 @@ namespace BizHawk.Common.PathExtensions
public static bool PathIsSet(this string path) => !string.IsNullOrWhiteSpace(path) && path != "." && path != "./" && path != ".\\";
public static string RemoveInvalidFileSystemChars(this string name) => string.Concat(name.Split(Path.GetInvalidFileNameChars()));
public static (string? Dir, string File) SplitPathToDirAndFile(this string path)
=> (Path.GetDirectoryName(path), Path.GetFileName(path));
public static (string? Dir, string FileNoExt, string? FileExt) SplitPathToDirFileAndExt(this string path)
=> (
Path.GetDirectoryName(path),
Path.GetFileNameWithoutExtension(path),
Path.GetExtension(path) is { Length: not 0 } ext
? ext
: null);
}
public static class PathUtils

View File

@ -4,6 +4,8 @@ using System.Collections.Generic;
using System.IO;
using System.Threading;
using BizHawk.Common.PathExtensions;
namespace BizHawk.Common
{
/// <summary>
@ -30,8 +32,8 @@ namespace BizHawk.Common
/// <exception cref="InvalidOperationException">filename in <paramref name="path"/> is not one generated by <see cref="GetTempFilename"/></exception>
public static string RenameTempFilenameForDelete(string path)
{
string filename = Path.GetFileName(path);
var dir = Path.GetDirectoryName(path) ?? throw new InvalidOperationException();
var (dir, filename) = path.SplitPathToDirAndFile();
_ = dir ?? throw new InvalidOperationException();
if (!filename.StartsWith("biz-"))
{
throw new InvalidOperationException();

View File

@ -2,6 +2,7 @@
using System.IO;
using BizHawk.Common;
using BizHawk.Common.PathExtensions;
namespace BizHawk.Emulation.DiscSystem
{
@ -28,9 +29,9 @@ namespace BizHawk.Emulation.DiscSystem
/// </summary>
private string FindAudio(string audioPath)
{
string basePath = Path.GetFileNameWithoutExtension(audioPath);
var (dir, basePath, _) = audioPath.SplitPathToDirFileAndExt();
//look for potential candidates
var di = new DirectoryInfo(Path.GetDirectoryName(audioPath));
DirectoryInfo di = new(dir!);
var fis = di.GetFiles();
//first, look for the file type we actually asked for
foreach (var fi in fis)

View File

@ -1,6 +1,8 @@
using System.IO;
using System.Collections.Generic;
using BizHawk.Common.PathExtensions;
namespace BizHawk.Emulation.DiscSystem.CUE
{
/// <summary>
@ -70,9 +72,7 @@ namespace BizHawk.Emulation.DiscSystem.CUE
/// </summary>
public List<string> Resolve(string path)
{
string targetFile = Path.GetFileName(path);
string targetFragment = Path.GetFileNameWithoutExtension(path);
var (targetFile, targetFragment, _) = path.SplitPathToDirFileAndExt();
DirectoryInfo di = null;
MyFileInfo[] fileInfos;
if (!string.IsNullOrEmpty(Path.GetDirectoryName(path)))

View File

@ -4,6 +4,8 @@ using System.IO;
using System.Collections.Generic;
using System.Linq;
using BizHawk.Common.PathExtensions;
using ISOParser;
namespace BizHawk.Emulation.DiscSystem
@ -471,12 +473,12 @@ namespace BizHawk.Emulation.DiscSystem
// assume an MDF file with the same name as the MDS
}
string dir = Path.GetDirectoryName(aFile.MDSPath);
var (dir, fileNoExt, _) = aFile.MDSPath.SplitPathToDirFileAndExt();
if (f.FilenameOffset == 0 ||
string.Compare(fileName, "*.mdf", StringComparison.InvariantCultureIgnoreCase) == 0)
{
fileName = $@"{dir}\{Path.GetFileNameWithoutExtension(aFile.MDSPath)}.mdf";
fileName = $@"{dir}\{fileNoExt}.mdf";
}
else
{

View File

@ -1,5 +1,7 @@
using System;
using System.IO;
using BizHawk.Common.PathExtensions;
using BizHawk.Emulation.DiscSystem.CUE;
namespace BizHawk.Emulation.DiscSystem
@ -180,13 +182,14 @@ namespace BizHawk.Emulation.DiscSystem
// OUT_Disc.DiscMountPolicy = IN_DiscMountPolicy; // NOT SURE WE NEED THIS (only makes sense for cue probably)
}
switch (Path.GetExtension(IN_FromPath).ToLowerInvariant())
var (dir, file, ext) = IN_FromPath.SplitPathToDirFileAndExt();
switch (ext.ToLowerInvariant())
{
case ".ccd":
OUT_Disc = new CCD_Format().LoadCCDToDisc(IN_FromPath, IN_DiscMountPolicy);
break;
case ".cue":
LoadCue(Path.GetDirectoryName(IN_FromPath), File.ReadAllText(IN_FromPath));
LoadCue(dir, File.ReadAllText(IN_FromPath));
break;
case ".iso":
// make a fake .cue file to represent this .iso and mount that
@ -194,9 +197,9 @@ namespace BizHawk.Emulation.DiscSystem
//TODO try it both ways and check the disc type to use whichever one succeeds in identifying a disc type
var len = new FileInfo(IN_FromPath).Length;
LoadCue(
Path.GetDirectoryName(IN_FromPath),
dir,
$@"
FILE ""{Path.GetFileName(IN_FromPath)}"" BINARY
FILE ""{file}"" BINARY
TRACK 01 {(len % 2048 is not 0 && len % 2352 is 0 ? "MODE2/2352" : "MODE1/2048")}
INDEX 01 00:00:00");
break;

View File

@ -5,6 +5,7 @@ using System.Threading;
using System.Threading.Tasks;
using BizHawk.Client.DiscoHawk;
using BizHawk.Common.PathExtensions;
namespace BizHawk.Emulation.DiscSystem
{
@ -255,8 +256,8 @@ namespace BizHawk.Emulation.DiscSystem
errorCallback(job.OUT_Log);
return false;
}
var baseName = Path.GetFileNameWithoutExtension(inputPath);
var outfile = Path.Combine(Path.GetDirectoryName(inputPath), $"{baseName}_hawked.ccd");
var (dir, baseName, _) = inputPath.SplitPathToDirFileAndExt();
var outfile = Path.Combine(dir!, $"{baseName}_hawked.ccd");
CCD_Format.Dump(disc, outfile);
return true;
}
@ -311,8 +312,7 @@ namespace BizHawk.Emulation.DiscSystem
{
if (infile is null) return;
using var disc = Disc.LoadAutomagic(infile);
var path = Path.GetDirectoryName(infile);
var filename = Path.GetFileNameWithoutExtension(infile);
var (path, filename, _) = infile.SplitPathToDirFileAndExt();
bool? CheckOverwrite(string mp3Path)
{
if (overwrite) return true; // overwrite